# Content-addressed object store: Merkle trees, sync plans, and reachability GC with proved laws. Source: https://github.com/paymog/bend-kit/tree/main/storage import Base import bend-kit-bytes@0.3.3.0/bytes.bend as Bytes import bend-kit-files@0.4.0.0/files.bend as Files import bend-kit-crypto@0.2.2.2/crypto.bend as Crypto import bend-kit-collections@0.1.5.0/omap.bend as OMap import bend-kit-collections@0.1.5.0/order.bend as Order # The content: a file, or a directory as a chain of named entries that ends in End. type Tree<-B: Data> is Data: TFile{data: B} TEnd{} TItem{name: String, child: Tree, rest: Tree} # One stored object. An OItem names its child and the rest of its directory by digest. type Obj<-D: Data, -B: Data> is Data: OFile{data: B} OEnd{} OItem{name: String, child: D, rest: D} # Objects by digest, as a list: the model the laws reason about. get answers the first # entry whose digest eq accepts. Index below answers the same in O(log n) comparisons. type Store<-D: Data, -B: Data> is Data: StNil{} StCon{key: D, obj: Obj, rest: Store} def Store.new(-D: Data, -B: Data) -> Store: StNil{} # eq(d, first key), or False when s is empty. def Store.head(~D: Data, ~B: Data, ~eq: D -> D -> Bool, s: Store, +d: D) -> Bool: match s: case StNil{}: False{} case StCon{k, o, r}: eq(d, k) # hit is Store.head(s, d), so the walk stops at the first match. def Store.get.go(~D: Data, ~B: Data, ~eq: D -> D -> Bool, s: Store, +d: D, hit: Bool) -> Maybe<&2, Obj>: match s hit: case StNil{} _: None{} case StCon{k, o, r} True{}: Some{o} case StCon{k, o, +r} False{}: Store.get.go(~D, ~B, ~eq, r, d, Store.head(~D, ~B, ~eq, r, d)) def Store.get(~D: Data, ~B: Data, ~eq: D -> D -> Bool, +s: Store, +d: D) -> Maybe<&2, Obj>: Store.get.go(~D, ~B, ~eq, s, d, Store.head(~D, ~B, ~eq, s, d)) def Store.has(~D: Data, ~B: Data, ~eq: D -> D -> Bool, +s: Store, +d: D) -> Bool: Maybe.is_some(&2, Obj, Store.get(~D, ~B, ~eq, s, d)) def Store.put.if(-D: Data, -B: Data, found: Bool, s: Store, d: D, o: Obj) -> Store: match found: case True{}: s case False{}: StCon{d, o, s} # Adds o under d unless d is present. The first object under a digest stays. def Store.put(~D: Data, ~B: Data, ~eq: D -> D -> Bool, +s: Store, +d: D, o: Obj) -> Store: Store.put.if(D, B, Store.has(~D, ~B, ~eq, s, d), s, d, o) # Puts each (digest, object) in order. def Store.apply(~D: Data, ~B: Data, ~eq: D -> D -> Bool, xs: Store, +s: Store) -> Store: match xs: case StNil{}: s case StCon{d, o, t}: Store.apply(~D, ~B, ~eq, t, Store.put(~D, ~B, ~eq, s, d, o)) def Store.size(-D: Data, -B: Data, s: Store) -> U32: match s: case StNil{}: 0 case StCon{k, o, r}: (1 + Store.size(D, B, r) : U32) # xs, then ys. def Store.cat(-D: Data, -B: Data, xs: Store, ys: Store) -> Store: match xs: case StNil{}: ys case StCon{k, o, r}: StCon{k, o, Store.cat(D, B, r, ys)} # Some only when both halves loaded. def Tree.item(-B: Data, n: String, c: Maybe<&2, Tree>, r: Maybe<&2, Tree>) -> Maybe<&2, Tree>: match c r: case Some{x} Some{y}: Some{TItem{n, x, y}} case Some{x} None{}: None{} case None{} r: None{} # m is the object under the digest being loaded. def load.go(~D: Data, ~B: Data, ~eq: D -> D -> Bool, fuel: Nat, +s: Store, m: Maybe<&2, Obj>) -> Maybe<&2, Tree>: match fuel m: case 0n _: None{} case 1n+p None{}: None{} case 1n+p Some{OFile{b}}: Some{TFile{b}} case 1n+p Some{OEnd{}}: Some{TEnd{}} case 1n+(+p) Some{OItem{n, c, r}}: Tree.item(B, n, load.go(~D, ~B, ~eq, p, s, Store.get(~D, ~B, ~eq, s, c)), load.go(~D, ~B, ~eq, p, s, Store.get(~D, ~B, ~eq, s, r))) # The tree under d, or None when an object is missing or the tree is deeper than fuel. def load(~D: Data, ~B: Data, ~eq: D -> D -> Bool, fuel: Nat, +s: Store, +d: D) -> Maybe<&2, Tree>: load.go(~D, ~B, ~eq, fuel, s, Store.get(~D, ~B, ~eq, s, d)) def closure.item(-D: Data, -B: Data, d: D, o: Obj, c: Maybe<&2, Store>, r: Maybe<&2, Store>) -> Maybe<&2, Store>: match c r: case Some{xs} Some{ys}: Some{StCon{d, o, Store.cat(D, B, xs, ys)}} case Some{xs} None{}: None{} case None{} r: None{} def closure.go(~D: Data, ~B: Data, ~eq: D -> D -> Bool, fuel: Nat, +s: Store, +d: D, m: Maybe<&2, Obj>) -> Maybe<&2, Store>: match fuel m: case 0n _: None{} case 1n+p None{}: None{} case 1n+p Some{OFile{b}}: Some{StCon{d, OFile{b}, StNil{}}} case 1n+p Some{OEnd{}}: Some{StCon{d, OEnd{}, StNil{}}} case 1n+(+p) Some{OItem{n, +c, +r}}: closure.item(D, B, d, OItem{n, c, r}, closure.go(~D, ~B, ~eq, p, s, c, Store.get(~D, ~B, ~eq, s, c)), closure.go(~D, ~B, ~eq, p, s, r, Store.get(~D, ~B, ~eq, s, r))) # Every object that load(fuel, s, d) reads, parents first, or None when load fails. def closure(~D: Data, ~B: Data, ~eq: D -> D -> Bool, fuel: Nat, +s: Store, +d: D) -> Maybe<&2, Store>: closure.go(~D, ~B, ~eq, fuel, s, d, Store.get(~D, ~B, ~eq, s, d)) # Store.has(t, first digest of xs), or False when xs is empty. def need.head(~D: Data, ~B: Data, ~eq: D -> D -> Bool, +t: Store, xs: Store) -> Bool: match xs: case StNil{}: False{} case StCon{d, o, rest}: Store.has(~D, ~B, ~eq, t, d) # found is need.head(t, xs), so only the taken branch recurses. def need.go(~D: Data, ~B: Data, ~eq: D -> D -> Bool, xs: Store, +t: Store, found: Bool) -> Store: match xs found: case StNil{} _: StNil{} case StCon{d, o, +rest} True{}: need.go(~D, ~B, ~eq, rest, t, need.head(~D, ~B, ~eq, t, rest)) case StCon{+d, +o, +rest} False{}: +u = Store.put(~D, ~B, ~eq, t, d, o) StCon{d, o, need.go(~D, ~B, ~eq, rest, u, need.head(~D, ~B, ~eq, u, rest))} # The entries of xs whose digest t lacks, each digest once. t grows as entries are taken. def need(~D: Data, ~B: Data, ~eq: D -> D -> Bool, +xs: Store, +t: Store) -> Store: need.go(~D, ~B, ~eq, xs, t, need.head(~D, ~B, ~eq, t, xs)) def plan.of(~D: Data, ~B: Data, ~eq: D -> D -> Bool, +dst: Store, m: Maybe<&2, Store>) -> Maybe<&2, Store>: match m: case None{}: None{} case Some{xs}: Some{need(~D, ~B, ~eq, xs, dst)} # The objects dst needs to load root as src does, or None when src cannot load root. def plan(~D: Data, ~B: Data, ~eq: D -> D -> Bool, fuel: Nat, +src: Store, +dst: Store, +root: D) -> Maybe<&2, Store>: plan.of(~D, ~B, ~eq, dst, closure(~D, ~B, ~eq, fuel, src, root)) def live.cat(-D: Data, -B: Data, a: Maybe<&2, Store>, b: Maybe<&2, Store>) -> Maybe<&2, Store>: match a b: case Some{xs} Some{ys}: Some{Store.cat(D, B, xs, ys)} case Some{xs} None{}: None{} case None{} b: None{} # The closures of every root, or None when one root does not load. def live(~D: Data, ~B: Data, ~eq: D -> D -> Bool, +fuel: Nat, +s: Store, roots: List<&2, D>) -> Maybe<&2, Store>: match roots: case Nil{}: Some{StNil{}} case Con{r, t}: live.cat(D, B, closure(~D, ~B, ~eq, fuel, s, r), live(~D, ~B, ~eq, fuel, s, t)) # eq(d, k) for some entry (k, o) of ls. def mem(~D: Data, ~B: Data, ~eq: D -> D -> Bool, ls: Store, +d: D) -> Bool: match ls: case StNil{}: False{} case StCon{k, o, t}: Bool.or(eq(d, k), mem(~D, ~B, ~eq, t, d)) def keep.if(-D: Data, -B: Data, live: Bool, k: D, o: Obj, r: Store) -> Store: match live: case True{}: StCon{k, o, r} case False{}: r # The entries of s whose digest is in ls. def keep(~D: Data, ~B: Data, ~eq: D -> D -> Bool, s: Store, +ls: Store) -> Store: match s: case StNil{}: StNil{} case StCon{+k, o, r}: keep.if(D, B, mem(~D, ~B, ~eq, ls, k), k, o, keep(~D, ~B, ~eq, r, ls)) def gc.of(~D: Data, ~B: Data, ~eq: D -> D -> Bool, s: Store, m: Maybe<&2, Store>) -> Maybe<&2, Store>: match m: case None{}: None{} case Some{ls}: Some{keep(~D, ~B, ~eq, s, ls)} # s without the objects that no retained root reaches, or None when a root does not load. def gc(~D: Data, ~B: Data, ~eq: D -> D -> Bool, fuel: Nat, +s: Store, +roots: List<&2, D>) -> Maybe<&2, Store>: gc.of(~D, ~B, ~eq, s, live(~D, ~B, ~eq, fuel, s, roots)) def admit.if(-D: Data, -B: Data, ok: Bool, s: Store, keep: Store) -> Maybe<&2, Store>: match ok: case True{}: Some{s} case False{}: None{} # Ingress: computed is the digest of o's encoding. None when it is not the claimed digest. def admit(~D: Data, ~B: Data, ~eq: D -> D -> Bool, +s: Store, +claimed: D, +computed: D, o: Obj) -> Maybe<&2, Store>: admit.if(D, B, eq(claimed, computed), Store.put(~D, ~B, ~eq, s, claimed, o), s) # The Merkle digest of t under the object hash H. def hash(~D: Data, ~B: Data, ~H: Obj -> D, t: Tree) -> D: match t: case TFile{b}: H(OFile{b}) case TEnd{}: H(OEnd{}) case TItem{n, c, r}: H(OItem{n, hash(~D, ~B, ~H, c), hash(~D, ~B, ~H, r)}) # The objects of t with their digests, children before parents; hash(t) is the last digest. def objects(~D: Data, ~B: Data, ~H: Obj -> D, t: Tree) -> Store: match t: case TFile{+b}: StCon{H(OFile{b}), OFile{b}, StNil{}} case TEnd{}: StCon{H(OEnd{}), OEnd{}, StNil{}} case TItem{+n, +c, +r}: +o = {OItem{n, hash(~D, ~B, ~H, c), hash(~D, ~B, ~H, r)} : Obj} Store.cat(D, B, objects(~D, ~B, ~H, c), Store.cat(D, B, objects(~D, ~B, ~H, r), StCon{H(o), o, StNil{}})) # True when load with this fuel reaches every node of t. def fits(-B: Data, fuel: Nat, t: Tree) -> Bool: match fuel t: case 0n _: False{} case 1n+p TFile{b}: True{} case 1n+p TEnd{}: True{} case 1n+(+p) TItem{n, c, r}: Bool.and(fits(B, p, c), fits(B, p, r)) # True when every entry (k, o) has eq(k, H(o)). def valid(~D: Data, ~B: Data, ~H: Obj -> D, ~eq: D -> D -> Bool, s: Store) -> Bool: match s: case StNil{}: True{} case StCon{k, +o, r}: Bool.and(eq(k, H(o)), valid(~D, ~B, ~H, ~eq, r)) # ---- Index: objects by digest in an ordered map under cmp. Index.of(xs) answers every # get, load, plan, and live as Store.apply(xs, Store.new) does under eq (the index_ laws). def Index(-D: Data, -B: Data) -> Data: OMap.OMap> def Index.new(-D: Data, -B: Data) -> Index(D, B): OMap.OTip{} def Index.size(-D: Data, -B: Data, m: Index(D, B)) -> U32: OMap.OMap.size(D, Obj, m) def Index.get(~D: Data, ~B: Data, ~cmp: D -> D -> Cmp, m: Index(D, B), +d: D) -> Maybe<&2, Obj>: OMap.OMap.get(~D, ~Obj, ~cmp, m, d) def Index.has(~D: Data, ~B: Data, ~cmp: D -> D -> Cmp, m: Index(D, B), +d: D) -> Bool: Maybe.is_some(&2, Obj, Index.get(~D, ~B, ~cmp, m, d)) def Index.put.if(~D: Data, ~B: Data, ~cmp: D -> D -> Cmp, found: Bool, m: Index(D, B), +d: D, o: Obj) -> Index(D, B): match found: case True{}: m case False{}: OMap.OMap.put(~D, ~Obj, ~cmp, m, d, o) # Adds o under d unless d is present, as Store.put does. def Index.put(~D: Data, ~B: Data, ~cmp: D -> D -> Cmp, +m: Index(D, B), +d: D, o: Obj) -> Index(D, B): Index.put.if(~D, ~B, ~cmp, Index.has(~D, ~B, ~cmp, m, d), m, d, o) # Puts each (digest, object) in order. def Index.apply(~D: Data, ~B: Data, ~cmp: D -> D -> Cmp, xs: Store, +m: Index(D, B)) -> Index(D, B): match xs: case StNil{}: m case StCon{d, o, t}: Index.apply(~D, ~B, ~cmp, t, Index.put(~D, ~B, ~cmp, m, d, o)) def Index.of(~D: Data, ~B: Data, ~cmp: D -> D -> Cmp, xs: Store) -> Index(D, B): Index.apply(~D, ~B, ~cmp, xs, Index.new(D, B)) def Index.load.go(~D: Data, ~B: Data, ~cmp: D -> D -> Cmp, fuel: Nat, +s: Index(D, B), m: Maybe<&2, Obj>) -> Maybe<&2, Tree>: match fuel m: case 0n _: None{} case 1n+p None{}: None{} case 1n+p Some{OFile{b}}: Some{TFile{b}} case 1n+p Some{OEnd{}}: Some{TEnd{}} case 1n+(+p) Some{OItem{n, c, r}}: Tree.item(B, n, Index.load.go(~D, ~B, ~cmp, p, s, Index.get(~D, ~B, ~cmp, s, c)), Index.load.go(~D, ~B, ~cmp, p, s, Index.get(~D, ~B, ~cmp, s, r))) def Index.load(~D: Data, ~B: Data, ~cmp: D -> D -> Cmp, fuel: Nat, +s: Index(D, B), +d: D) -> Maybe<&2, Tree>: Index.load.go(~D, ~B, ~cmp, fuel, s, Index.get(~D, ~B, ~cmp, s, d)) def Index.closure.go(~D: Data, ~B: Data, ~cmp: D -> D -> Cmp, fuel: Nat, +s: Index(D, B), +d: D, m: Maybe<&2, Obj>) -> Maybe<&2, Store>: match fuel m: case 0n _: None{} case 1n+p None{}: None{} case 1n+p Some{OFile{b}}: Some{StCon{d, OFile{b}, StNil{}}} case 1n+p Some{OEnd{}}: Some{StCon{d, OEnd{}, StNil{}}} case 1n+(+p) Some{OItem{n, +c, +r}}: closure.item(D, B, d, OItem{n, c, r}, Index.closure.go(~D, ~B, ~cmp, p, s, c, Index.get(~D, ~B, ~cmp, s, c)), Index.closure.go(~D, ~B, ~cmp, p, s, r, Index.get(~D, ~B, ~cmp, s, r))) def Index.closure(~D: Data, ~B: Data, ~cmp: D -> D -> Cmp, fuel: Nat, +s: Index(D, B), +d: D) -> Maybe<&2, Store>: Index.closure.go(~D, ~B, ~cmp, fuel, s, d, Index.get(~D, ~B, ~cmp, s, d)) def Index.need.head(~D: Data, ~B: Data, ~cmp: D -> D -> Cmp, +t: Index(D, B), xs: Store) -> Bool: match xs: case StNil{}: False{} case StCon{d, o, rest}: Index.has(~D, ~B, ~cmp, t, d) def Index.need.go(~D: Data, ~B: Data, ~cmp: D -> D -> Cmp, xs: Store, +t: Index(D, B), found: Bool) -> Store: match xs found: case StNil{} _: StNil{} case StCon{d, o, +rest} True{}: Index.need.go(~D, ~B, ~cmp, rest, t, Index.need.head(~D, ~B, ~cmp, t, rest)) case StCon{+d, +o, +rest} False{}: +u = Index.put(~D, ~B, ~cmp, t, d, o) StCon{d, o, Index.need.go(~D, ~B, ~cmp, rest, u, Index.need.head(~D, ~B, ~cmp, u, rest))} def Index.need(~D: Data, ~B: Data, ~cmp: D -> D -> Cmp, +xs: Store, +t: Index(D, B)) -> Store: Index.need.go(~D, ~B, ~cmp, xs, t, Index.need.head(~D, ~B, ~cmp, t, xs)) def Index.plan.of(~D: Data, ~B: Data, ~cmp: D -> D -> Cmp, +dst: Index(D, B), m: Maybe<&2, Store>) -> Maybe<&2, Store>: match m: case None{}: None{} case Some{xs}: Some{Index.need(~D, ~B, ~cmp, xs, dst)} def Index.plan(~D: Data, ~B: Data, ~cmp: D -> D -> Cmp, fuel: Nat, +src: Index(D, B), +dst: Index(D, B), +root: D) -> Maybe<&2, Store>: Index.plan.of(~D, ~B, ~cmp, dst, Index.closure(~D, ~B, ~cmp, fuel, src, root)) def Index.live(~D: Data, ~B: Data, ~cmp: D -> D -> Cmp, +fuel: Nat, +s: Index(D, B), roots: List<&2, D>) -> Maybe<&2, Store>: match roots: case Nil{}: Some{StNil{}} case Con{r, t}: live.cat(D, B, Index.closure(~D, ~B, ~cmp, fuel, s, r), Index.live(~D, ~B, ~cmp, fuel, s, t)) def Index.admit.if(-D: Data, -B: Data, ok: Bool, s: Index(D, B)) -> Maybe<&2, Index(D, B)>: match ok: case True{}: Some{s} case False{}: None{} # admit on an index: None when claimed is not computed. def Index.admit(~D: Data, ~B: Data, ~cmp: D -> D -> Cmp, +s: Index(D, B), +claimed: D, +computed: D, o: Obj) -> Maybe<&2, Index(D, B)>: Index.admit.if(D, B, Cmp.is_eq(cmp(claimed, computed)), Index.put(~D, ~B, ~cmp, s, claimed, o)) # ---- SHA-256 content addressing on disk # Octets packed four to a U32, low byte first, as Bytes packs them. Words past len are 0. type Blob is Data: Blob{len: U32, words: List<&2, U32>} # r is the array beside the word read at i - 1. def Blob.read(n: Nat, r: Array & U32, +i: U32, acc: List<&2, U32>) -> List<&2, U32>: match n: case 0n: List.reverse(&2, U32, acc) case 1n+p: (a, w) = r Blob.read(p, Array.get(U32, a, i), (i + 1 : U32), Con{w, acc}) def Blob.of(b: Bytes.Bytes) -> Blob: Bytes.Bytes{+len, buf} = b Blob{len, Blob.read(U32.to_nat(((len + 3) / 4 : U32)), Array.get(U32, buf, 0), 1, Nil{})} def Blob.write(ws: List<&2, U32>, a: Array, +i: U32) -> Array: match ws: case Nil{}: a case Con{w, t}: Blob.write(t, Array.set(U32, a, i, w), (i + 1 : U32)) def Blob.bytes(x: Blob) -> Bytes.Bytes: Blob{+len, ws} = x Bytes.Bytes{len, Blob.write(ws, Bytes.alloc(len), 0)} # A zero last carry may lie past the array, where a write would wrap to word 0. def Blob.write.last(zero: Bool, a: Array, +j: U32, +carry: U32) -> Array: match zero: case True{}: a case False{}: Array.set(U32, a, j, carry) # Writes ws from octet 4 * j + lo / 8 on; carry holds the octets of word j below lo. def Blob.write.shift(ws: List<&2, U32>, a: Array, +j: U32, +lo: Nat, +hi: Nat, +carry: U32) -> Array: match ws: case Nil{}: Blob.write.last(U32.is_eq(carry, 0), a, j, carry) case Con{+w, t}: Blob.write.shift(t, Array.set(U32, a, j, (carry .|. U32.shln(w, lo) : U32)), (j + 1 : U32), lo, hi, U32.shrn(w, hi)) def Blob.string(+s: String) -> Blob: Blob.of(Bytes.from_string(s)) # Loads and collections give up past this many levels. Each directory entry is one level. def limit() -> Nat: U32.to_nat(1048576) def char.ok(+c: U32) -> Bool: Bool.and(U32.is_lt(c, 256), Bool.and(U32.is_ne(c, 0), Bool.and(U32.is_ne(c, 10), U32.is_ne(c, 47)))) def chars.ok(s: String) -> Bool: match s: case SNil{}: True{} case SCon{Chr{+c}, t}: Bool.and(char.ok(c), chars.ok(t)) # A directory entry name: one to 255 octets, not "." or "..", with no NUL, newline, or "/". def name.ok(+n: String) -> Bool: +k = String.length(n) Bool.and(Nat.is_lt(0n, k), Bool.and(Nat.is_le(k, 255n), Bool.and(Bool.not(String.eq(n, ".")), Bool.and(Bool.not(String.eq(n, "..")), chars.ok(n))))) def hex.ok(s: String) -> Bool: match s: case SNil{}: True{} case SCon{Chr{+c}, t}: Bool.and(Bool.or(Bool.and(U32.is_le(48, c), U32.is_le(c, 57)), Bool.and(U32.is_le(97, c), U32.is_le(c, 102))), hex.ok(t)) # 64 lowercase hex characters. def digest.ok(+d: String) -> Bool: Bool.and(Nat.is_eq(String.length(d), 64n), hex.ok(d)) # A SHA-256 digest: its 32 octets packed four to a U32, low octet first, as Bytes packs them. type Digest is Data: Digest{words: List<&2, U32>} def Digest.order(a: List<&2, U32>, b: List<&2, U32>) -> Cmp: match a b: case Nil{} Nil{}: EQ{} case Nil{} Con{y, t}: LT{} case Con{x, s} Nil{}: GT{} case Con{+x, s} Con{+y, t}: Order.string.fin(U32.cmp(x, y), Digest.order(s, t)) # A total order on digests (the digest_cmp_ laws). It is not hex order. def Digest.cmp(a: Digest, b: Digest) -> Cmp: match a b: case Digest{x} Digest{y}: Digest.order(x, y) def Digest.hex(d: Digest) -> String: match d: case Digest{ws}: Bytes.to_hex(Bytes.Bytes{32, Blob.write(ws, Bytes.alloc(32), 0)}) def nibble(+c: U32) -> U32: Bool.pick(U32, U32.is_lt(c, 97), (c - 48 : U32), (c - 87 : U32)) # Two hex digits per octet. def unhex(s: String) -> List<&2, U32>: match s: case SCon{Chr{+h}, SCon{Chr{+l}, t}}: Con{(nibble(h) * 16 + nibble(l) : U32), unhex(t)} case _: Nil{} # Four octets per word, low octet first. def pack(bs: List<&2, U32>) -> List<&2, U32>: match bs: case Con{+a, Con{+b, Con{+c, Con{+d, t}}}}: Con{(a + b * 256 + c * 65536 + d * 16777216 : U32), pack(t)} case _: Nil{} def Digest.read.if(ok: Bool, s: String) -> Maybe<&2, Digest>: match ok: case True{}: Some{Digest{pack(unhex(s))}} case False{}: None{} # The digest that 64 lowercase hex characters name, or None. def Digest.read(+s: String) -> Maybe<&2, Digest>: Digest.read.if(digest.ok(s), s) def encode.file.at(+len: U32, ws: List<&2, U32>, +hl: U32, r: Array & U32) -> Bytes.Bytes: (a, c) = r +lo = ((hl .&. 3 : U32) * 8 : U32) Bytes.Bytes{(hl + len : U32), Blob.write.shift(ws, a, (hl >> 2n : U32), U32.to_nat(lo), U32.to_nat((32 - lo : U32)), c)} def encode.file.pick(aligned: Bool, +len: U32, ws: List<&2, U32>, +hl: U32, a: Array) -> Bytes.Bytes: match aligned: case True{}: Bytes.Bytes{(hl + len : U32), Blob.write(ws, a, (hl >> 2n : U32))} case False{}: encode.file.at(len, ws, hl, Array.get(U32, a, (hl >> 2n : U32))) # The header h, then the words of a Blob of len octets, written once without a byte loop. def encode.file(+len: U32, ws: List<&2, U32>, h: Bytes.Bytes) -> Bytes.Bytes: Bytes.Bytes{+hl, hb} = h encode.file.pick(U32.is_eq((hl .&. 3 : U32), 0), len, ws, hl, Bytes.grow(hl, hb, (hl + len : U32))) # The canonical encoding: "file \n" and the octets, "end\n", or "item \n". def encode(o: Obj) -> Bytes.Bytes: match o: case OFile{b}: Blob{+len, ws} = b encode.file(len, ws, Bytes.from_string("file " ++ U32.show(len) ++ "\n")) case OEnd{}: Bytes.from_string("end\n") case OItem{n, c, r}: Bytes.from_string("item " ++ Digest.hex(c) ++ " " ++ Digest.hex(r) ++ " " ++ n ++ "\n") def decode.when(ok: Bool, o: Obj) -> Maybe<&2, Obj>: match ok: case True{}: Some{o} case False{}: None{} def decode.item.of(ok: Bool, n: String, c: Maybe<&2, Digest>, r: Maybe<&2, Digest>) -> Maybe<&2, Obj>: match ok c r: case True{} Some{x} Some{y}: Some{OItem{n, x, y}} case _ _ _: None{} # rest is " ". def decode.item(+rest: String) -> Maybe<&2, Obj>: +n = String.drop(rest, 130n) decode.item.of(Bool.and(name.ok(n), Bool.and(String.eq(String.take(String.drop(rest, 64n), 1n), " "), String.eq(String.take(String.drop(rest, 129n), 1n), " "))), n, Digest.read(String.take(rest, 64n)), Digest.read(String.take(String.drop(rest, 65n), 64n))) def decode.file(m: Maybe<&2, U32>, +digits: String, +at: U32, b: Bytes.Bytes) -> Maybe<&2, Obj>: match m: case None{}: None{} case Some{+n}: Bytes.Bytes{+len, buf} = b decode.when(Bool.and(String.eq(U32.show(n), digits), U32.is_eq((len - at : U32), n)), OFile{Blob.of(Bytes.snd(Bytes.slice(Bytes.Bytes{len, buf}, at, n)))}) def decode.head(+head: String, +at: U32, b: Bytes.Bytes) -> Maybe<&2, Obj>: Bytes.Bytes{+len, buf} = b +whole = U32.is_eq(len, at) Bool.pick(Maybe<&2, Obj>, String.starts_with(head, "file "), decode.file(U32.read(String.drop(head, 5n)), String.drop(head, 5n), at, Bytes.Bytes{len, buf}), Bool.pick(Maybe<&2, Obj>, Bool.and(whole, String.eq(head, "end")), Some{OEnd{}}, Bool.pick(Maybe<&2, Obj>, Bool.and(whole, String.starts_with(head, "item ")), decode.item(String.drop(head, 5n)), None{}))) def decode.cut(+at: U32, r: Bytes.Bytes & Bytes.Bytes) -> Maybe<&2, Obj>: (b, head) = r decode.head(Bytes.to_string(head), at, b) def decode.at(r: Bytes.Bytes & Maybe<&2, U32>) -> Maybe<&2, Obj>: match r: case (b, None{}): None{} case (b, Some{+i}): decode.cut((i + 1 : U32), Bytes.slice(b, 0, i)) # The object that encode wrote, or None for any other octets. def decode(b: Bytes.Bytes) -> Maybe<&2, Obj>: decode.at(Bytes.find(b, "\n")) # ---- IO that can fail def Run(-A: Type) -> Type: IO(Result<&1, &1, U32 & String, A>) def Run.pure(-A: Type, x: A) -> Run(A): IO.pure(Result<&1, &1, U32 & String, A>, Done{x}) def Run.fail(-A: Type, e: U32 & String) -> Run(A): IO.pure(Result<&1, &1, U32 & String, A>, Fail{e}) def Run.next(-A: Type, -B: Type, r: Result<&1, &1, U32 & String, A>, f: A -> Run(B)) -> Run(B): match r: case Done{x}: f(x) case Fail{e}: Run.fail(B, e) def Run.bind(-A: Type, -B: Type, m: Run(A), f: A -> Run(B)) -> Run(B): IO.bind(Result<&1, &1, U32 & String, A>, Result<&1, &1, U32 & String, B>, m, r => Run.next(A, B, r, f)) def Run.lift(-A: Type, m: IO(A)) -> Run(A): IO.bind(A, Result<&1, &1, U32 & String, A>, m, x => Run.pure(A, x)) def Run.check(ok: Bool, +why: String) -> Run(Unit): match ok: case True{}: Run.pure(Unit, Unit{}) case False{}: Run.fail(Unit, (22, "storage: " ++ why)) def Run.some(-A: Data, m: Maybe<&2, A>, +why: String) -> Run(A): match m: case Some{x}: Run.pure(A, x) case None{}: Run.fail(A, (22, "storage: " ++ why)) def digest.of(r: U32 & Array) -> Digest: (n, a) = r Digest{Blob.read(8n, Array.get(U32, a, 0), 1, Nil{})} # The SHA-256 of b. def digest(b: Bytes.Bytes) -> Run(Digest): Bytes.Bytes{len, buf} = b do Run: r : U32 & Array <- Crypto.sha256.words(len, buf) return digest.of(r) def commit.done(+d: Digest, o: Obj, xs: Store) -> Run(Digest & Store): Run.pure(Digest & Store, (d, Store.cat(Digest, Blob, xs, StCon{d, o, StNil{}}))) def commit.node(+o: Obj, xs: Store) -> Run(Digest & Store): do Run>: d : Digest <- digest(encode(o)) commit.done(d, o, xs) def commit.item(+n: String, c: Digest & Store, r: Digest & Store) -> Run(Digest & Store): (+dc, xs) = c (+dr, ys) = r do Run>: Run.check(name.ok(n), "invalid name " ++ n) commit.node(OItem{n, dc, dr}, Store.cat(Digest, Blob, xs, ys)) # hash and objects with H = SHA-256 of encode: the root digest and the objects, children first. def commit(t: Tree) -> Run(Digest & Store): match t: case TFile{b}: commit.node(OFile{b}, StNil{}) case TEnd{}: commit.node(OEnd{}, StNil{}) case TItem{+n, c, r}: do Run>: x : Digest & Store <- commit(c) y : Digest & Store <- commit(r) commit.item(n, x, y) def receive.two(+s: Index(Digest, Blob), +claimed: Digest, bb: Bytes.Bytes & Bytes.Bytes) -> Run(Index(Digest, Blob)): (b1, b2) = bb do Run: computed : Digest <- digest(b1) o : Obj <- Run.some(Obj, decode(b2), "object " ++ Digest.hex(claimed) ++ " does not decode") Run.some(Index(Digest, Blob), Index.admit(~Digest, ~Blob, ~Digest.cmp, s, claimed, computed, o), "object " ++ Digest.hex(claimed) ++ " does not match its digest") # Ingress: the store with the object that b encodes. Fails when b does not decode or its SHA-256 is not claimed. def receive(+s: Index(Digest, Blob), +claimed: Digest, b: Bytes.Bytes) -> Run(Index(Digest, Blob)): receive.two(s, claimed, Bytes.clone(b)) # ---- A store on disk: dir/objects/ holds each encoded object, dir/roots/ a root digest in hex. def Disk.object(+dir: String, +d: Digest) -> String: dir ++ "/objects/" ++ Digest.hex(d) def Disk.init(+dir: String) -> Run(Unit): do Run: Files.mkdir_all(dir ++ "/objects") Files.mkdir_all(dir ++ "/roots") Files.mkdir_all(dir ++ "/tmp") def write.close(r: File & Result<&1, &1, U32 & String, Unit>) -> Run(Unit): match r: case (f, x): do IO>: File.close(f) return x def write.file(+path: String, b: Bytes.Bytes) -> Run(Unit): do Run: f : File <- File.open(path, "w") r : File & Result<&1, &1, U32 & String, Unit> <- Run.lift(File & Result<&1, &1, U32 & String, Unit>, Files.write_bytes(f, b)) write.close(r) # Writes b to path through dir/tmp and a rename, so a reader sees all of b or nothing. def write(+dir: String, +name: String, +path: String, b: Bytes.Bytes) -> Run(Unit): +tmp = dir ++ "/tmp/" ++ name do Run: write.file(tmp, b) Files.rename(tmp, path) def read.chunk(r: File & Result<&1, &1, U32 & String, Bytes.Bytes>) -> Run(File & Bytes.Bytes): match r: case (f, Fail{e}): do IO>: File.close(f) return Fail{e} case (f, Done{b}): Run.pure(File & Bytes.Bytes, (f, b)) def read.piece(f: File) -> Run(File & Bytes.Bytes): IO.bind(File & Result<&1, &1, U32 & String, Bytes.Bytes>, Result<&1, &1, U32 & String, File & Bytes.Bytes>, Files.read_bytes(f, 1048576), r => read.chunk(r)) # n more reads of up to 1 MiB each; fb holds the file and the last piece. def read.go(n: Nat, fb: File & Bytes.Bytes, acc: Bytes.Bytes) -> Run(Bytes.Bytes): match n: case 0n: (f, b) = fb do IO>: File.close(f) return Done{Bytes.append(acc, b)} case 1n+p: (f, b) = fb Run.bind(File & Bytes.Bytes, Bytes.Bytes, read.piece(f), next => read.go(p, next, Bytes.append(acc, b))) def read.check(+size: U32, b: Bytes.Bytes) -> Run(Bytes.Bytes): Bytes.Bytes{+len, buf} = b do Run: Run.check(U32.is_eq(len, size), "short read") return Bytes.Bytes{len, buf} def info.size(i: Files.Info) -> Run(U32): Files.Info{k, (hi, lo), t} = i do Run: Run.check(U32.is_eq(hi, 0), "file exceeds the 32-bit Bytes length limit") return lo def read.sized(+path: String, +size: U32) -> Run(Bytes.Bytes): do Run: f : File <- File.open(path, "r") b : Bytes.Bytes <- read.go(U32.to_nat((size / 1048576 + 1 : U32)), (f, Bytes.new(0)), Bytes.new(0)) read.check(size, b) # The whole file at path. def read(+path: String) -> Run(Bytes.Bytes): do Run: info : Files.Info <- Files.stat(path) size : U32 <- info.size(info) read.sized(path, size) def Disk.put.if(there: Bool, +dir: String, +d: Digest, +path: String, b: Bytes.Bytes) -> Run(Unit): match there: case True{}: Run.pure(Unit, Unit{}) case False{}: write(dir, Digest.hex(d), path, b) # Writes the encoded object under its digest unless the file exists. Objects never change. def Disk.put(+dir: String, +d: Digest, b: Bytes.Bytes) -> Run(Unit): +path = Disk.object(dir, d) do Run: there : Bool <- Run.lift(Bool, Files.exists(path)) Disk.put.if(there, dir, d, path, b) def Disk.save(+dir: String, xs: Store) -> Run(Unit): match xs: case StNil{}: Run.pure(Unit, Unit{}) case StCon{+d, o, r}: do Run: Disk.put(dir, d, encode(o)) Disk.save(dir, r) # Ingress into the store at dir: receive, then write the octets under their digest. def Disk.receive.two(+dir: String, +s: Index(Digest, Blob), +claimed: Digest, bb: Bytes.Bytes & Bytes.Bytes) -> Run(Index(Digest, Blob)): (b1, b2) = bb do Run: t : Index(Digest, Blob) <- receive(s, claimed, b1) Disk.put(dir, claimed, b2) return t def Disk.receive(+dir: String, +s: Index(Digest, Blob), +claimed: Digest, b: Bytes.Bytes) -> Run(Index(Digest, Blob)): Disk.receive.two(dir, s, claimed, Bytes.clone(b)) # The object in file objects/n, checked against the digest that n names. def Disk.load.one(+dir: String, +n: String, m: Maybe<&2, Digest>, +s: Index(Digest, Blob)) -> Run(Index(Digest, Blob)): match m: case None{}: Run.fail(Index(Digest, Blob), (22, "storage: object " ++ n ++ " is not a digest")) case Some{+d}: do Run: b : Bytes.Bytes <- read(Disk.object(dir, d)) receive(s, d, b) def Disk.load.go(+dir: String, names: List<&2, String>, +s: Index(Digest, Blob)) -> Run(Index(Digest, Blob)): match names: case Nil{}: Run.pure(Index(Digest, Blob), s) case Con{+n, t}: do Run: u : Index(Digest, Blob) <- Disk.load.one(dir, n, Digest.read(n), s) Disk.load.go(dir, t, u) # Every object at dir, each checked against its file name. A tampered or undecodable object fails the load. def Disk.load(+dir: String) -> Run(Index(Digest, Blob)): do Run: names : List<&2, String> <- Files.list_dir(dir ++ "/objects") Disk.load.go(dir, names, Index.new(Digest, Blob)) # One step of Disk.fetch.go. type Fetch is Data: Want{d: Digest} Have{d: Digest, held: Bool} Read{d: Digest, there: Bool} Got{d: Digest} Seen{m: Maybe<&2, Obj>} # Adds to s every object that a Want reaches at dir, each read and checked through receive once. # A missing object is left out, so a load of the root then fails. def Disk.fetch.go(fuel: Nat, +dir: String, step: Fetch, +s: Index(Digest, Blob)) -> Run(Index(Digest, Blob)): match fuel step: case 0n _: Run.pure(Index(Digest, Blob), s) case 1n+p Want{+d}: Disk.fetch.go(p, dir, Have{d, Index.has(~Digest, ~Blob, ~Digest.cmp, s, d)}, s) case 1n+p Have{d, True{}}: Run.pure(Index(Digest, Blob), s) case 1n+p Have{+d, False{}}: Run.bind(Bool, Index(Digest, Blob), Run.lift(Bool, Files.exists(Disk.object(dir, d))), e => Disk.fetch.go(p, dir, Read{d, e}, s)) case 1n+p Read{d, False{}}: Run.pure(Index(Digest, Blob), s) case 1n+(+p) Read{+d, True{}}: do Run: b : Bytes.Bytes <- read(Disk.object(dir, d)) u : Index(Digest, Blob) <- receive(s, d, b) Disk.fetch.go(p, dir, Got{d}, u) case 1n+p Got{+d}: Disk.fetch.go(p, dir, Seen{Index.get(~Digest, ~Blob, ~Digest.cmp, s, d)}, s) case 1n+(+p) Seen{Some{OItem{n, +c, +r}}}: do Run: u : Index(Digest, Blob) <- Disk.fetch.go(p, dir, Want{c}, s) Disk.fetch.go(p, dir, Want{r}, u) case 1n+p Seen{_}: Run.pure(Index(Digest, Blob), s) # Five steps per level, so a fetch goes as deep as a load with limit(). def fetch.fuel() -> Nat: Nat.mul(5n, limit()) # The index of the objects that root reaches at dir. Other objects are not read. def Disk.fetch(+dir: String, +root: Digest) -> Run(Index(Digest, Blob)): Disk.fetch.go(fetch.fuel(), dir, Want{root}, Index.new(Digest, Blob)) def Disk.fetch.all(+dir: String, roots: List<&2, Digest>, +s: Index(Digest, Blob)) -> Run(Index(Digest, Blob)): match roots: case Nil{}: Run.pure(Index(Digest, Blob), s) case Con{+r, t}: do Run: u : Index(Digest, Blob) <- Disk.fetch.go(fetch.fuel(), dir, Want{r}, s) Disk.fetch.all(dir, t, u) def Disk.probe.one(there: Bool, +dir: String, +d: Digest, +s: Index(Digest, Blob)) -> Run(Index(Digest, Blob)): match there: case True{}: do Run: b : Bytes.Bytes <- read(Disk.object(dir, d)) receive(s, d, b) case False{}: Run.pure(Index(Digest, Blob), s) # Adds to s the objects of xs that dir holds, each read and checked. Other objects are not read. def Disk.probe(+dir: String, xs: Store, +s: Index(Digest, Blob)) -> Run(Index(Digest, Blob)): match xs: case StNil{}: Run.pure(Index(Digest, Blob), s) case StCon{+d, o, r}: do Run: e : Bool <- Run.lift(Bool, Files.exists(Disk.object(dir, d))) u : Index(Digest, Blob) <- Disk.probe.one(e, dir, d, s) Disk.probe(dir, r, u) def Disk.set_root(+dir: String, +name: String, +d: Digest) -> Run(Unit): do Run: Run.check(name.ok(name), "invalid root " ++ name) write(dir, "root." ++ name, dir ++ "/roots/" ++ name, Bytes.from_string(Digest.hex(d))) def Disk.root(+dir: String, +name: String) -> Run(Digest): do Run: b : Bytes.Bytes <- read(dir ++ "/roots/" ++ name) Run.some(Digest, Digest.read(Bytes.to_string(b)), "root " ++ name ++ " is not a digest") def Disk.roots.go(+dir: String, names: List<&2, String>) -> Run(List<&2, Digest>): match names: case Nil{}: Run.pure(List<&2, Digest>, Nil{}) case Con{+n, t}: do Run>: d : Digest <- Disk.root(dir, n) ds : List<&2, Digest> <- Disk.roots.go(dir, t) return Con{d, ds} # The digest of every named root. def Disk.roots(+dir: String) -> Run(List<&2, Digest>): do Run>: names : List<&2, String> <- Files.list_dir(dir ++ "/roots") Disk.roots.go(dir, names) def Disk.drop.one(live: Bool, +dir: String, +d: Digest) -> Run(U32): match live: case True{}: Run.pure(U32, 0) case False{}: do Run: Files.remove(Disk.object(dir, d)) return 1 def Disk.drop(+dir: String, ds: List<&2, Digest>, +live: Index(Digest, Blob), +n: U32) -> Run(U32): match ds: case Nil{}: Run.pure(U32, n) case Con{+d, r}: do Run: k : U32 <- Disk.drop.one(Index.has(~Digest, ~Blob, ~Digest.cmp, live, d), dir, d) Disk.drop(dir, r, live, (n + k : U32)) def digests.add(m: Maybe<&2, Digest>, ds: List<&2, Digest>) -> List<&2, Digest>: match m: case Some{d}: Con{d, ds} case None{}: ds # The digests that names name. Other names are left out. def digests(names: List<&2, String>) -> List<&2, Digest>: match names: case Nil{}: Nil{} case Con{+n, t}: digests.add(Digest.read(n), digests(t)) # Drops what gc drops: an object stays exactly when its digest is in the live set. def Disk.collect.of(+dir: String, +retained: List<&2, Digest>, +s: Index(Digest, Blob)) -> Run(U32): do Run: ls : Store <- Run.some(Store, Index.live(~Digest, ~Blob, ~Digest.cmp, limit(), s, retained), "a retained root does not load") names : List<&2, String> <- Files.list_dir(dir ++ "/objects") Disk.drop(dir, digests(names), Index.of(~Digest, ~Blob, ~Digest.cmp, ls), 0) # Deletes every object that no retained root reaches and answers how many it deleted. Fails, and # deletes nothing, when a retained root does not load. It reads only the objects the roots reach. # No writer may run during the collection. def Disk.collect(+dir: String, +retained: List<&2, Digest>) -> Run(U32): do Run: s : Index(Digest, Blob) <- Disk.fetch.all(dir, retained, Index.new(Digest, Blob)) Disk.collect.of(dir, retained, s) def sync.send(+dir: String, ys: Store, +dst: Index(Digest, Blob), +n: U32) -> Run(U32): match ys: case StNil{}: Run.pure(U32, n) case StCon{+d, o, r}: do Run: t : Index(Digest, Blob) <- Disk.receive(dir, dst, d, encode(o)) sync.send(dir, r, t, (n + 1 : U32)) def sync.of(+dst: String, +root: Digest, +a: Index(Digest, Blob), +b: Index(Digest, Blob)) -> Run(U32): do Run: ys : Store <- Run.some(Store, Index.plan(~Digest, ~Blob, ~Digest.cmp, limit(), a, b, root), "the source cannot load " ++ Digest.hex(root)) sync.send(dst, ys, b, 0) # b holds the objects of the closure ys that dst has, so plan sees what a full load of dst would show it. def sync.probe(+dst: String, +root: Digest, +a: Index(Digest, Blob), +ys: Store) -> Run(U32): do Run: b : Index(Digest, Blob) <- Disk.probe(dst, Index.need(~Digest, ~Blob, ~Digest.cmp, ys, Index.new(Digest, Blob)), Index.new(Digest, Blob)) sync.of(dst, root, a, b) def sync.from(+dst: String, +root: Digest, +a: Index(Digest, Blob)) -> Run(U32): do Run: ys : Store <- Run.some(Store, Index.closure(~Digest, ~Blob, ~Digest.cmp, limit(), a, root), "the source cannot load " ++ Digest.hex(root)) sync.probe(dst, root, a, ys) # Sends dst every object it needs to load root as src does, through ingress. Answers how many it sent. # It reads the objects root reaches in src, and only those of them that dst holds. def sync(+src: String, +dst: String, +root: Digest) -> Run(U32): do Run: a : Index(Digest, Blob) <- Disk.fetch(src, root) sync.from(dst, root, a) # The tree under root at dir. Fails when an object is missing or tampered, or the tree is too deep. # It reads only the objects root reaches. def Disk.checkout(+dir: String, +root: Digest) -> Run(Tree): do Run>: s : Index(Digest, Blob) <- Disk.fetch(dir, root) Run.some(Tree, Index.load(~Digest, ~Blob, ~Digest.cmp, limit(), s, root), "root " ++ Digest.hex(root) ++ " does not load") def Disk.commit.save(+dir: String, r: Digest & Store) -> Run(Digest): (+d, xs) = r do Run: Disk.save(dir, xs) return d # Commits t to the store at dir and answers its root digest. def Disk.commit(+dir: String, t: Tree) -> Run(Digest): do Run: r : Digest & Store <- commit(t) Disk.commit.save(dir, r) # ---- Artifact trees: a directory on disk as a Tree. type Step is Data: Stat{} Statted{info: Files.Info} Listing{names: List<&2, String>} # The tree at path. Regular files become TFile and directories become TItem chains in name order. def snap(fuel: Nat, +path: String, step: Step) -> Run(Tree): match fuel step: case 0n _: Run.fail(Tree, (36, "storage: " ++ path ++ " is too deep")) case 1n+p Stat{}: Run.bind(Files.Info, Tree, Files.stat(path), i => snap(p, path, Statted{i})) case 1n+p Statted{Files.Info{Files.Regular{}, s, t}}: Run.bind(Bytes.Bytes, Tree, read(path), b => Run.pure(Tree, TFile{Blob.of(b)})) case 1n+p Statted{Files.Info{Files.Directory{}, s, t}}: Run.bind(List<&2, String>, Tree, Files.list_dir(path), ns => snap(p, path, Listing{ns})) case 1n+p Statted{Files.Info{Files.Other{}, s, t}}: Run.fail(Tree, (22, "storage: " ++ path ++ " is not a file or a directory")) case 1n+p Listing{Nil{}}: Run.pure(Tree, TEnd{}) case 1n+(+p) Listing{Con{+n, ns}}: do Run>: Run.check(name.ok(n), "invalid name " ++ n) c : Tree <- snap(p, path ++ "/" ++ n, Stat{}) r : Tree <- snap(p, path, Listing{ns}) return TItem{n, c, r} def snapshot(+path: String) -> Run(Tree): snap(limit(), path, Stat{}) # Writes t at path. chain is True when t is the rest of the listing of directory path. def restore.go(t: Tree, +path: String, chain: Bool) -> Run(Unit): match t chain: case TFile{b} False{}: write.file(path, Blob.bytes(b)) case TEnd{} False{}: Files.mkdir(path) case TItem{+n, c, r} False{}: do Run: Files.mkdir(path) Run.check(name.ok(n), "invalid name " ++ n) restore.go(c, path ++ "/" ++ n, False{}) restore.go(r, path, True{}) case TEnd{} True{}: Run.pure(Unit, Unit{}) case TItem{+n, c, r} True{}: do Run: Run.check(name.ok(n), "invalid name " ++ n) restore.go(c, path ++ "/" ++ n, False{}) restore.go(r, path, True{}) case TFile{b} True{}: Run.fail(Unit, (22, "storage: a file stands in the listing of " ++ path)) # Writes t as a new file or directory at path. Fails if path exists. def restore(t: Tree, +path: String) -> Run(Unit): restore.go(t, path, False{})