remote.v

1.8 KB · raw

1//! The remote interface: five operations, two backends.2//!3//! A remote is dumb storage that can do one safe swap. Everything clever —4//! what to upload, what diverged, how to combine — lives in `src/sync`, above5//! this interface, so the folder and S3 backends stay thin and the sync logic6//! is written once and tested offline.78module remote910// get not finding a key is a normal answer, not an error, so `Object.found`11// carries it and errors mean real trouble (a refused connection, bad keys).12pub struct Object {13pub:14	found bool15	body  []u816	tag   string17}1819// Backend is one place to put bytes. `put_if_absent` never overwrites and20// `put_if_match` only overwrites when the version still matches, which is what21// makes two machines offering at once safe.22pub interface Backend {23mut:24	get(key string) !Object25	list(prefix string) ![]string26	put_if_absent(key string, body []u8) !bool27	put_if_match(key string, body []u8, tag string) !bool28	delete(key string) !29}3031pub const probe_key = 'koh-probe'3233// check runs the connection test `koh remote` insists on before saving a34// setting: write a probe object, confirm a second write of it is refused, then35// delete it. A backend that cannot do that cannot keep two machines honest.36pub fn check(mut backend Backend) ! {37	payload := 'koh probe'.bytes()38	if !backend.put_if_absent(probe_key, payload)! {39		return error('the remote already has a probe object — it is not empty, or it cannot refuse a second write')40	}41	if backend.put_if_absent(probe_key, payload)! {42		backend.delete(probe_key)!43		return error('the remote overwrote an existing object — conditional writes are not working')44	}45	probe := backend.get(probe_key)!46	if !probe.found || probe.body != payload {47		return error('the remote did not give back what was written')48	}49	backend.delete(probe_key)!50}