//! The remote interface: five operations, two backends. //! //! A remote is dumb storage that can do one safe swap. Everything clever — //! what to upload, what diverged, how to combine — lives in `src/sync`, above //! this interface, so the folder and S3 backends stay thin and the sync logic //! is written once and tested offline. module remote // get not finding a key is a normal answer, not an error, so `Object.found` // carries it and errors mean real trouble (a refused connection, bad keys). pub struct Object { pub: found bool body []u8 tag string } // Backend is one place to put bytes. `put_if_absent` never overwrites and // `put_if_match` only overwrites when the version still matches, which is what // makes two machines offering at once safe. pub interface Backend { mut: get(key string) !Object list(prefix string) ![]string put_if_absent(key string, body []u8) !bool put_if_match(key string, body []u8, tag string) !bool delete(key string) ! } pub const probe_key = 'koh-probe' // check runs the connection test `koh remote` insists on before saving a // setting: write a probe object, confirm a second write of it is refused, then // delete it. A backend that cannot do that cannot keep two machines honest. pub fn check(mut backend Backend) ! { payload := 'koh probe'.bytes() if !backend.put_if_absent(probe_key, payload)! { return error('the remote already has a probe object — it is not empty, or it cannot refuse a second write') } if backend.put_if_absent(probe_key, payload)! { backend.delete(probe_key)! return error('the remote overwrote an existing object — conditional writes are not working') } probe := backend.get(probe_key)! if !probe.found || probe.body != payload { return error('the remote did not give back what was written') } backend.delete(probe_key)! }