//! A repo stolen from a Kepr that is not signed in: its own address, kept read-only.
//!
//! This is the addendum's one exception to the machine-wide remote rule. The
//! address lives in the repo's `state`, so nothing about it can leak into another
//! repo, and it is dropped the moment this machine signs in to that Kepr.
module exchange
import src.machine
import src.remote
import src.storage
const source_key = 'source'
// The address a stolen repo came from, `/`, or '' for an ordinary repo.
pub fn source_of(store &storage.Store) string {
return store.state(source_key) or { '' }
}
// The Kepr part of a source: what `koh remote ` would have to name.
pub fn source_host(source string) string {
return source.all_before_last('/')
}
// True when this machine was set up by `koh remote ` for the same host, so that host's
// addresses are this machine's own bucket (Part 2 of the addendum).
pub fn signed_in_to(host string) bool {
settings := machine.load_remote() or { return false }
return settings.kind == machine.kind_s3 && settings.kepr != '' && settings.kepr == host
}
// read_only opens the backend a stolen repo came from: public, and never written to.
pub fn read_only(source string) !remote.Backend {
return remote.new_web(source_host(source))!
}
// remember_source and forget_source are the only two writes: a repo is either a stolen one or an
// ordinary one, never both.
pub fn remember_source(store &storage.Store, source string) ! {
store.set_state(source_key, source)!
}
pub fn forget_source(store &storage.Store) ! {
store.set_state(source_key, '')!
}