# 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.1/bytes.bend as Bytes
import bend-kit-files@0.4.0.1/files.bend as Files
import bend-kit-crypto@0.2.2.2/crypto.bend as Crypto
import ./pure.bend as P
# ---- 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))
# The SHA-256 of b.
def digest(b: Bytes.Bytes) -> Run(P.Digest):
Bytes.Bytes{len, buf} = b
do Run
:
r : U32 & Array <- Crypto.sha256.words(len, buf)
return P.digest.of(r)
def Blob.hash.words(len: U32, raw: Array) -> IO(Result<&1, &1, U32 & String, Array & (U32 & Array)>):
import "./effs/storage.c"
import "./effs/storage.js"
def Blob.of.hashed(+len: U32, buffers: Array & (U32 & Array)) -> P.Blob:
(raw, hash) = buffers
P.Blob.pack(P.digest.of(hash), Bytes.Bytes{len, raw})
# Hashes the packed input before it becomes a reusable word list.
def Blob.of(b: Bytes.Bytes) -> Run(P.Blob):
Bytes.Bytes{+len, raw} = b
do Run:
buffers : Array & (U32 & Array) <- Blob.hash.words(len, raw)
return Blob.of.hashed(len, buffers)
def Blob.string(s: String) -> Run(P.Blob):
Blob.of(Bytes.from_string(s))
def object.digest(o: P.Obj) -> Run(P.Digest):
match o:
case P.OFile{P.Blob{len, ws, d}}:
Run.pure(P.Digest, d)
case P.OEnd{}:
digest(P.encode(P.OEnd{}))
case P.OItem{n, c, r}:
digest(P.encode(P.OItem{n, c, r}))
def decode.two(bb: Bytes.Bytes & Bytes.Bytes) -> Run(Maybe<&2, P.Obj>):
(b, raw) = bb
do Run>>:
d : P.Digest <- digest(raw)
return P.decode.hashed(d, b)
# The object that encode wrote, or None for any other octets.
def decode(b: Bytes.Bytes) -> Run(Maybe<&2, P.Obj>):
decode.two(Bytes.clone(b))
def commit.done(+d: P.Digest, o: P.Obj, xs: P.Store) -> Run(P.Digest & P.Store):
Run.pure(P.Digest & P.Store, (d, P.Store.cat(P.Digest, P.Blob, xs, P.StCon{d, o, P.StNil{}})))
def commit.node(+o: P.Obj, xs: P.Store) -> Run(P.Digest & P.Store):
do Run>:
d : P.Digest <- object.digest(o)
commit.done(d, o, xs)
def commit.item(+n: String, c: P.Digest & P.Store, r: P.Digest & P.Store) -> Run(P.Digest & P.Store):
(+dc, xs) = c
(+dr, ys) = r
do Run>:
Run.check(P.name.ok(n), "invalid name " ++ n)
commit.node(P.OItem{n, dc, dr}, P.Store.cat(P.Digest, P.Blob, xs, ys))
# hash and objects with H = SHA-256 of encode: the root digest and the objects, children first.
def commit(t: P.Tree) -> Run(P.Digest & P.Store):
match t:
case P.TFile{b}:
commit.node(P.OFile{b}, P.StNil{})
case P.TEnd{}:
commit.node(P.OEnd{}, P.StNil{})
case P.TItem{+n, c, r}:
do Run>:
x : P.Digest & P.Store <- commit(c)
y : P.Digest & P.Store <- commit(r)
commit.item(n, x, y)
def receive.two(+s: P.Index(P.Digest, P.Blob), +claimed: P.Digest, bb: Bytes.Bytes & Bytes.Bytes) -> Run(P.Index(P.Digest, P.Blob)):
(b1, b2) = bb
do Run:
+computed : P.Digest <- digest(b1)
o : P.Obj <- Run.some(P.Obj, P.decode.hashed(computed, b2), "object " ++ P.Digest.hex(claimed) ++ " does not decode")
Run.some(P.Index(P.Digest, P.Blob), P.Index.admit(~P.Digest, ~P.Blob, ~P.Digest.cmp, s, claimed, computed, o), "object " ++ P.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: P.Index(P.Digest, P.Blob), +claimed: P.Digest, b: Bytes.Bytes) -> Run(P.Index(P.Digest, P.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: P.Digest) -> String:
dir ++ "/objects/" ++ P.Digest.hex(d)
def write.durable(tmp: String, path: String, fault: U32) -> IO(Result<&1, &1, U32 & String, Unit>):
import "./effs/storage.c"
import "./effs/storage.js"
def Disk.init(+dir: String) -> Run(Unit):
do Run:
Files.mkdir_all(dir ++ "/objects")
Files.mkdir_all(dir ++ "/roots")
Files.mkdir_all(dir ++ "/tmp")
write.durable("", dir ++ "/objects", 0)
write.durable("", dir ++ "/roots", 0)
write.durable("", dir ++ "/tmp", 0)
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)
def write.temp(prefix: String) -> IO(Result<&1, &1, U32 & String, String & File>):
import "./effs/storage.c"
import "./effs/storage.js"
def write.cleanup(+tmp: String, r: Result<&1, &1, U32 & String, Unit>) -> Run(Unit):
match r:
case Done{x}:
Run.pure(Unit, x)
case Fail{e}:
do IO>:
removed : Result<&1, &1, U32 & String, Unit> <- Files.remove(tmp)
return Fail{e}
def write.publish(r: Result<&1, &1, U32 & String, Unit>, +tmp: String, path: String) -> Run(Unit):
match r:
case Fail{e}:
write.cleanup(tmp, Fail{e})
case Done{x}:
do IO>:
renamed : Result<&1, &1, U32 & String, Unit> <- write.durable(tmp, path, 0)
write.cleanup(tmp, renamed)
def write.opened(pair: String & File, path: String, b: Bytes.Bytes) -> Run(Unit):
(+tmp, f) = pair
do IO>:
r : File & Result<&1, &1, U32 & String, Unit> <- Files.write_bytes(f, b)
closed : Result<&1, &1, U32 & String, Unit> <- write.close(r)
write.publish(closed, tmp, path)
# Each write owns its exclusively created temporary file until publication or cleanup.
def write(+dir: String, name: String, path: String, b: Bytes.Bytes) -> Run(Unit):
do Run:
pair : String & File <- write.temp(dir ++ "/tmp/" ++ name)
write.opened(pair, path, b)
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: P.Digest, +path: String, b: Bytes.Bytes) -> Run(Unit):
match there:
case True{}:
Run.pure(Unit, Unit{})
case False{}:
write(dir, P.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: P.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: P.Store) -> Run(Unit):
match xs:
case P.StNil{}:
Run.pure(Unit, Unit{})
case P.StCon{+d, o, r}:
do Run:
Disk.put(dir, d, P.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: P.Index(P.Digest, P.Blob), +claimed: P.Digest, bb: Bytes.Bytes & Bytes.Bytes) -> Run(P.Index(P.Digest, P.Blob)):
(b1, b2) = bb
do Run:
t : P.Index(P.Digest, P.Blob) <- receive(s, claimed, b1)
Disk.put(dir, claimed, b2)
return t
def Disk.receive(+dir: String, +s: P.Index(P.Digest, P.Blob), +claimed: P.Digest, b: Bytes.Bytes) -> Run(P.Index(P.Digest, P.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, P.Digest>, +s: P.Index(P.Digest, P.Blob)) -> Run(P.Index(P.Digest, P.Blob)):
match m:
case None{}:
Run.fail(P.Index(P.Digest, P.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: P.Index(P.Digest, P.Blob)) -> Run(P.Index(P.Digest, P.Blob)):
match names:
case Nil{}:
Run.pure(P.Index(P.Digest, P.Blob), s)
case Con{+n, t}:
do Run:
u : P.Index(P.Digest, P.Blob) <- Disk.load.one(dir, n, P.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(P.Index(P.Digest, P.Blob)):
do Run:
names : List<&2, String> <- Files.list_dir(dir ++ "/objects")
Disk.load.go(dir, names, P.Index.new(P.Digest, P.Blob))
# One step of Disk.fetch.go.
type Fetch is Data:
Want{d: P.Digest}
Have{d: P.Digest, held: Bool}
Read{d: P.Digest, there: Bool}
Got{d: P.Digest}
Seen{m: Maybe<&2, P.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: P.Index(P.Digest, P.Blob)) -> Run(P.Index(P.Digest, P.Blob)):
match fuel step:
case 0n _:
Run.pure(P.Index(P.Digest, P.Blob), s)
case 1n+p Want{+d}:
Disk.fetch.go(p, dir, Have{d, P.Index.has(~P.Digest, ~P.Blob, ~P.Digest.cmp, s, d)}, s)
case 1n+p Have{d, True{}}:
Run.pure(P.Index(P.Digest, P.Blob), s)
case 1n+p Have{+d, False{}}:
Run.bind(Bool, P.Index(P.Digest, P.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(P.Index(P.Digest, P.Blob), s)
case 1n+(+p) Read{+d, True{}}:
do Run:
b : Bytes.Bytes <- read(Disk.object(dir, d))
u : P.Index(P.Digest, P.Blob) <- receive(s, d, b)
Disk.fetch.go(p, dir, Got{d}, u)
case 1n+p Got{+d}:
Disk.fetch.go(p, dir, Seen{P.Index.get(~P.Digest, ~P.Blob, ~P.Digest.cmp, s, d)}, s)
case 1n+(+p) Seen{Some{P.OItem{n, +c, +r}}}:
do Run:
u : P.Index(P.Digest, P.Blob) <- Disk.fetch.go(p, dir, Want{c}, s)
Disk.fetch.go(p, dir, Want{r}, u)
case 1n+p Seen{_}:
Run.pure(P.Index(P.Digest, P.Blob), s)
# Five steps per level, so a fetch goes as deep as a load with limit().
def fetch.fuel() -> Nat:
Nat.mul(5n, P.limit())
# The index of the objects that root reaches at dir. Other objects are not read.
def Disk.fetch(+dir: String, +root: P.Digest) -> Run(P.Index(P.Digest, P.Blob)):
Disk.fetch.go(fetch.fuel(), dir, Want{root}, P.Index.new(P.Digest, P.Blob))
def Disk.fetch.all(+dir: String, roots: List<&2, P.Digest>, +s: P.Index(P.Digest, P.Blob)) -> Run(P.Index(P.Digest, P.Blob)):
match roots:
case Nil{}:
Run.pure(P.Index(P.Digest, P.Blob), s)
case Con{+r, t}:
do Run:
u : P.Index(P.Digest, P.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: P.Digest, +s: P.Index(P.Digest, P.Blob)) -> Run(P.Index(P.Digest, P.Blob)):
match there:
case True{}:
do Run:
b : Bytes.Bytes <- read(Disk.object(dir, d))
receive(s, d, b)
case False{}:
Run.pure(P.Index(P.Digest, P.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: P.Store, +s: P.Index(P.Digest, P.Blob)) -> Run(P.Index(P.Digest, P.Blob)):
match xs:
case P.StNil{}:
Run.pure(P.Index(P.Digest, P.Blob), s)
case P.StCon{+d, o, r}:
do Run:
e : Bool <- Run.lift(Bool, Files.exists(Disk.object(dir, d)))
u : P.Index(P.Digest, P.Blob) <- Disk.probe.one(e, dir, d, s)
Disk.probe(dir, r, u)
def write.sync(path: String) -> IO(Result<&1, &1, U32 & String, Unit>):
import "./effs/storage.c"
import "./effs/storage.js"
def Disk.fence(+dir: String, xs: P.Store) -> Run(Unit):
match xs:
case P.StNil{}:
Run.pure(Unit, Unit{})
case P.StCon{d, o, rest}:
do Run:
write.sync(Disk.object(dir, d))
Disk.fence(dir, rest)
def Disk.set_root(+dir: String, +name: String, +d: P.Digest) -> Run(Unit):
do Run:
Run.check(P.name.ok(name), "invalid root " ++ name)
+s : P.Index(P.Digest, P.Blob) <- Disk.fetch(dir, d)
objects : P.Store <- Run.some(P.Store, P.Index.closure(~P.Digest, ~P.Blob, ~P.Digest.cmp, P.limit(), s, d), "root has missing objects")
Disk.fence(dir, objects)
write.durable("", dir ++ "/objects", 0)
write(dir, "root", dir ++ "/roots/" ++ name, Bytes.from_string(P.Digest.hex(d)))
def Disk.root(+dir: String, +name: String) -> Run(P.Digest):
do Run:
b : Bytes.Bytes <- read(dir ++ "/roots/" ++ name)
Run.some(P.Digest, P.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, P.Digest>):
match names:
case Nil{}:
Run.pure(List<&2, P.Digest>, Nil{})
case Con{+n, t}:
do Run>:
d : P.Digest <- Disk.root(dir, n)
ds : List<&2, P.Digest> <- Disk.roots.go(dir, t)
return Con{d, ds}
# The digest of every named root.
def Disk.roots(+dir: String) -> Run(List<&2, P.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: P.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, P.Digest>, +live: P.Index(P.Digest, P.Blob), +n: U32) -> Run(U32):
match ds:
case Nil{}:
Run.pure(U32, n)
case Con{+d, r}:
do Run:
k : U32 <- Disk.drop.one(P.Index.has(~P.Digest, ~P.Blob, ~P.Digest.cmp, live, d), dir, d)
Disk.drop(dir, r, live, (n + k : U32))
# 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, P.Digest>, +s: P.Index(P.Digest, P.Blob)) -> Run(U32):
do Run:
ls : P.Store <- Run.some(P.Store, P.Index.live(~P.Digest, ~P.Blob, ~P.Digest.cmp, P.limit(), s, retained), "a retained root does not load")
names : List<&2, String> <- Files.list_dir(dir ++ "/objects")
Disk.drop(dir, P.digests(names), P.Index.of(~P.Digest, ~P.Blob, ~P.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, P.Digest>) -> Run(U32):
do Run:
s : P.Index(P.Digest, P.Blob) <- Disk.fetch.all(dir, retained, P.Index.new(P.Digest, P.Blob))
Disk.collect.of(dir, retained, s)
def Disk.clean_tmp.go(+dir: String, names: List<&2, String>, +n: U32) -> Run(U32):
match names:
case Nil{}:
Run.pure(U32, n)
case Con{name, rest}:
do Run:
Files.remove(dir ++ "/tmp/" ++ name)
Disk.clean_tmp.go(dir, rest, (n + 1 : U32))
# No writer may run during maintenance; the caller must hold the store exclusively.
def Disk.clean_tmp(+dir: String) -> Run(U32):
do Run:
names : List<&2, String> <- Files.list_dir(dir ++ "/tmp")
Disk.clean_tmp.go(dir, names, 0)
def sync.send(+dir: String, ys: P.Store, +dst: P.Index(P.Digest, P.Blob), +n: U32) -> Run(U32):
match ys:
case P.StNil{}:
Run.pure(U32, n)
case P.StCon{+d, o, r}:
do Run:
t : P.Index(P.Digest, P.Blob) <- Disk.receive(dir, dst, d, P.encode(o))
sync.send(dir, r, t, (n + 1 : U32))
def sync.of(+dst: String, +root: P.Digest, +a: P.Index(P.Digest, P.Blob), +b: P.Index(P.Digest, P.Blob)) -> Run(U32):
do Run:
ys : P.Store <- Run.some(P.Store, P.Index.plan(~P.Digest, ~P.Blob, ~P.Digest.cmp, P.limit(), a, b, root), "the source cannot load " ++ P.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: P.Digest, +a: P.Index(P.Digest, P.Blob), +ys: P.Store) -> Run(U32):
do Run:
b : P.Index(P.Digest, P.Blob) <- Disk.probe(dst, P.Index.need(~P.Digest, ~P.Blob, ~P.Digest.cmp, ys, P.Index.new(P.Digest, P.Blob)), P.Index.new(P.Digest, P.Blob))
sync.of(dst, root, a, b)
def sync.from(+dst: String, +root: P.Digest, +a: P.Index(P.Digest, P.Blob)) -> Run(U32):
do Run:
ys : P.Store <- Run.some(P.Store, P.Index.closure(~P.Digest, ~P.Blob, ~P.Digest.cmp, P.limit(), a, root), "the source cannot load " ++ P.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: P.Digest) -> Run(U32):
do Run:
a : P.Index(P.Digest, P.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: P.Digest) -> Run(P.Tree):
do Run>:
s : P.Index(P.Digest, P.Blob) <- Disk.fetch(dir, root)
Run.some(P.Tree, P.Index.load(~P.Digest, ~P.Blob, ~P.Digest.cmp, P.limit(), s, root), "root " ++ P.Digest.hex(root) ++ " does not load")
def Disk.commit.save(+dir: String, r: P.Digest & P.Store) -> Run(P.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: P.Tree) -> Run(P.Digest):
do Run:
r : P.Digest & P.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(P.Tree):
match fuel step:
case 0n _:
Run.fail(P.Tree, (36, "storage: " ++ path ++ " is too deep"))
case 1n+p Stat{}:
Run.bind(Files.Info, P.Tree, Files.stat(path), i => snap(p, path, Statted{i}))
case 1n+p Statted{Files.Info{Files.Regular{}, s, t}}:
do Run>:
b : Bytes.Bytes <- read(path)
blob : P.Blob <- Blob.of(b)
return P.TFile{blob}
case 1n+p Statted{Files.Info{Files.Directory{}, s, t}}:
Run.bind(List<&2, String>, P.Tree, Files.list_dir(path), ns => snap(p, path, Listing{ns}))
case 1n+p Statted{Files.Info{Files.Other{}, s, t}}:
Run.fail(P.Tree, (22, "storage: " ++ path ++ " is not a file or a directory"))
case 1n+p Listing{Nil{}}:
Run.pure(P.Tree, P.TEnd{})
case 1n+(+p) Listing{Con{+n, ns}}:
do Run>:
Run.check(P.name.ok(n), "invalid name " ++ n)
c : P.Tree <- snap(p, path ++ "/" ++ n, Stat{})
r : P.Tree <- snap(p, path, Listing{ns})
return P.TItem{n, c, r}
def snapshot(+path: String) -> Run(P.Tree):
snap(P.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: P.Tree, +path: String, chain: Bool) -> Run(Unit):
match t chain:
case P.TFile{b} False{}:
write.file(path, P.Blob.bytes(b))
case P.TEnd{} False{}:
Files.mkdir(path)
case P.TItem{+n, c, r} False{}:
do Run:
Files.mkdir(path)
Run.check(P.name.ok(n), "invalid name " ++ n)
restore.go(c, path ++ "/" ++ n, False{})
restore.go(r, path, True{})
case P.TEnd{} True{}:
Run.pure(Unit, Unit{})
case P.TItem{+n, c, r} True{}:
do Run:
Run.check(P.name.ok(n), "invalid name " ++ n)
restore.go(c, path ++ "/" ++ n, False{})
restore.go(r, path, True{})
case P.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: P.Tree, +path: String) -> Run(Unit):
restore.go(t, path, False{})