//! What a data directory holds, named once. //! //! The Electron app spread these paths across a dozen modules — `envFile.ts` //! knew `env`, `backup/config.ts` knew `backup/litestream.yml`. One type owns //! them here, so a reader never spells a path and the layout can be read in one //! screen. //! //! **`role.json` is NOT here**, and its absence is the point: nothing writes it //! any more (plan §A), so naming it beside the paths a running Mac uses would //! say it is part of the layout. The one module that still knows the name is //! the one that forgets the file — `cotton-ffi`'s `shell::relay_name`. //! //! ```text //! / //! env the server's environment file (0600) //! server.log the log the Activity page draws from //! registry.db the server's registry //! files/ the served workspaces (the server writes these) //! media/ the served object store //! identity/ backup.age — 0700 //! identity/backup-earlier/ the age identities earlier epochs backed up to //! device.key too, but ONLY for a rehearsal //! client/connection.json which server this Mac syncs against //! client/media-paths-root the folder the media rows' paths name (phone) //! client//replica.db the replica, with `_ingest_cursors` inside it //! client/media/ the content-addressed device store //! client/snapshots/ a source's copy-before-read scratch //! repos/.git a held repository's bare clone — a PER-MAC cache, //! never synced, rebuilt from its bundles //! repos/.staging/ bundle and asset scratch, same volume //! runner/config.yml the CI runner's generated config (0600) //! runner/.runner its registration, token inside (0600) //! runs// a CI run's logs, as files //! runs/work/ the runner's checkout parent //! cache/thumbs/ disposable thumbnails //! ``` //! //! **`repos/` IS THE SERVER'S, AND THIS FILE ONLY DOCUMENTS IT.** It is listed //! because it lives in the same data directory and a reader of this map would //! otherwise wonder what made it — but it is composed by `cotton-server` //! (`repos::reader::clone_path`, `repos::mirror`), which cannot call this //! crate: the dependency edge points the other way and //! `dependency_boundary.rs` holds it. So there is no accessor for it here. //! Adding one would be a SECOND spelling of a path with no reader, which is //! the thing this file exists to prevent. //! //! **`runner/` AND `runs/work/` DO HAVE ACCESSORS, because THIS crate writes //! them**: [`ci::runner`](crate::ci::runner) renders the config, holds the //! registration and points the runner's checkouts at `runs/work`. The //! per-run log directory `runs//` stays the server's own spelling — //! two crates agree on the word `runs` and nothing else about it. //! //! Nothing here creates a directory: knowing where a file goes and deciding to //! make one are different jobs, and the second belongs to whoever writes. use std::path::{Path, PathBuf}; #[derive(Debug, Clone, PartialEq, Eq)] pub struct Layout { root: PathBuf, } impl Layout { pub fn new(root: impl Into) -> Self { Self { root: root.into() } } /// The data directory itself. pub fn root(&self) -> &Path { &self.root } /// The server's environment file. `0600`; app-owned keys are refused with a /// warning rather than honoured. pub fn env_file(&self) -> PathBuf { self.root.join("env") } pub fn server_log(&self) -> PathBuf { self.root.join("server.log") } pub fn registry_db(&self) -> PathBuf { self.root.join("registry.db") } /// Where the server keeps each workspace's file. pub fn files_dir(&self) -> PathBuf { self.root.join("files") } /// The SERVED object store — keyed by the object key. pub fn media_dir(&self) -> PathBuf { self.root.join("media") } /// Device-local phone identity, retained independently of signing keys. pub fn installation_id(&self) -> PathBuf { self.root.join("installation-id") } /// `0700`. Holds `backup.age`. pub fn identity_dir(&self) -> PathBuf { self.root.join("identity") } pub fn backup_age_identity(&self) -> PathBuf { self.identity_dir().join("backup.age") } /// The age identities this Mac's backups were encrypted to BEFORE the /// current `backup.age` — one file each. The recovery kit carries them, /// because a restore of an earlier epoch's copy needs the key that epoch /// wrote with. Under `identity/`, so it is `0700` with the rest. pub fn backup_earlier_identities_dir(&self) -> PathBuf { self.identity_dir().join("backup-earlier") } /// A REHEARSAL's device seed, and only a rehearsal's. /// /// The packaged app keeps its seed in the Keychain and writes no file at /// all (design M8); this path exists so a build pointed at a scratch data /// directory has somewhere to keep one that is not the real install's. /// `identity::load_or_mint_rehearsal_seed` is the one writer. pub fn rehearsal_device_key(&self) -> PathBuf { self.identity_dir().join("device.key") } pub fn backup_dir(&self) -> PathBuf { self.root.join("backup") } pub fn client_dir(&self) -> PathBuf { self.root.join("client") } /// Pending removals belong to this signing identity, never its successor. pub fn device_removals_json(&self, device: &str) -> PathBuf { self.client_dir().join(format!("device-removals-{}.json", cotton_core::device::server_id::server_id_of(device))) } /// The ONE seam for "which server this Mac syncs against". pub fn connection_json(&self) -> PathBuf { self.client_dir().join("connection.json") } /// Present while this Mac is becoming a runner: its copies are closed to /// every opener from the moment its server stops until the drop is done. pub fn becoming_runner_journal(&self) -> PathBuf { self.client_dir().join("becoming-runner.json") } /// The data directory the media rows' `local_path`s were last written /// for, as text — `relocate_media::follow_data_dir` reads it on a phone's /// open and rewrites the rows when the folder has moved since. pub fn media_paths_root(&self) -> PathBuf { self.client_dir().join("media-paths-root") } /// The replica, and `_ingest_cursors` inside it — device-local, never /// synced, so deleting the replica forgets the cursors together. pub fn replica(&self, workspace_id: &str) -> PathBuf { self.client_dir().join(workspace_id).join("replica.db") } /// Where a phone keeps the unreadable copies of `database` the person /// removed, each whole in its own folder — kept, and counted in what the /// database holds on disk. pub fn refused_copies_dir(&self, database: &str) -> PathBuf { self.root.join("refused-copies").join(database) } /// The CLIENT store — content-addressed by sha256. pub fn client_media_dir(&self) -> PathBuf { self.client_dir().join("media") } /// How many bytes of viewed originals this DEVICE keeps. /// /// A file beside `connection.json` and not a column, because the budget is /// about THIS machine's disk: a phone with 64 GB and a Mac with 4 TB have /// no business agreeing on a number, and a replicated setting would make /// one of them wrong the moment the other was right. pub fn media_budget_json(&self) -> PathBuf { self.client_dir().join("media-budget.json") } /// THE CATCH-UPS IN FLIGHT ON THIS DEVICE, by database: what each one /// owed when it began and how far the home's line has said it is /// (`sync::line`). A file here because the fact is this device's alone /// and has to outlive the process: a phone catching up over hours is /// relaunched many times, and a percent that started again from nothing /// each time would be a line that moves backwards. pub fn home_lines_json(&self) -> PathBuf { self.client_dir().join("home-lines.json") } /// THIS MAC'S HOST SETTINGS (the Device page's `host` section): start at /// login, stay awake, error reports, updates. Per machine for the reason /// the media budget is: each says something about this Mac alone. pub fn host_settings_json(&self) -> PathBuf { self.client_dir().join("host-settings.json") } /// Where a source copies a live database plus its `-wal`/`-shm` before /// reading it. pub fn snapshots_dir(&self) -> PathBuf { self.client_dir().join("snapshots") } /// The WhatsApp linked-device helper's folder: its socket, its session /// store (the linked device's keys — this Mac's alone, never synced) and /// its outbox of messages not yet written. pub fn whatsapp_link_dir(&self) -> PathBuf { self.root.join("whatsapp-link") } /// Attachments the helper downloaded, waiting for the media leg. Disposable. pub fn whatsapp_link_media_dir(&self) -> PathBuf { self.root.join("cache").join("whatsapp-link") } /// Disposable: nothing here is a source of truth, and deleting it costs one /// re-render. pub fn thumbs_cache_dir(&self) -> PathBuf { self.root.join("cache").join("thumbs") } /// The CI runner's own directory — `0700`. /// /// Its generated `config.yml` and the `.runner` file the registration /// writes, which holds the runner token the server minted. A token is why /// the directory is the runner's own rather than a file beside the config: /// one directory to keep at `0700`, and nothing else in it. pub fn runner_dir(&self) -> PathBuf { self.root.join("runner") } /// The config the runner daemon is started with — GENERATED from Settings /// at every start, never hand-edited (`ci::config::render_runner_config`). pub fn runner_config(&self) -> PathBuf { self.runner_dir().join("config.yml") } /// The registration the runner writes once and reads for ever after — /// its uuid, its labels and its TOKEN. Never logged. /// /// **THE RUNNER WRITES THIS FILE, SO ITS MODE IS THE RUNNER'S** — an /// `os.Create`, which lands at `0644` under the ordinary umask. What keeps /// it private is [`Self::runner_dir`] at `0700`, which is the one mode this /// app does set; a comment claiming `0600` here was describing a chmod /// nobody performs. pub fn runner_registration(&self) -> PathBuf { self.runner_dir().join(".runner") } /// Where a CI run's logs live, as FILES. Logs never become rows. /// /// The per-run subdirectory is `cotton-server`'s to compose; what this /// crate needs is the root, and [`Layout::runs_work_dir`] under it. pub fn runs_dir(&self) -> PathBuf { self.root.join("runs") } /// Where a DEVELOPER JOB's output lives, as FILES — one `.log` per /// job (`session_host::dev_jobs`). Logs never become rows here either. /// /// Beside [`Layout::runs_dir`] rather than inside it: a CI run is the /// forge's and a developer job is one command an assistant asked for, and a /// disk sweep that emptied one should not reach the other. pub fn dev_jobs_dir(&self) -> PathBuf { self.root.join("dev-jobs") } /// The parent of a job's working directory. /// /// NAMED EXPLICITLY, always: the runner's own default for a host job is /// `$HOME/.cache/act/`, which puts a checkout of this repository inside /// the cache directory a disk sweep is allowed to empty. Under the data /// directory it is on the volume the person already gave Cotton. pub fn runs_work_dir(&self) -> PathBuf { self.runs_dir().join("work") } /// Where "Export everything" writes its JSON document. /// /// Under `cache/` beside the thumbnails, and disposable for the same /// reason: the file is a copy of the database made for the share sheet, and /// the database it was copied from is right there. The phone's TypeScript /// writes the same document to `Paths.cache`. pub fn exports_dir(&self) -> PathBuf { self.root.join("cache").join("exports") } } #[cfg(test)] mod tests { use super::*; fn layout() -> Layout { Layout::new("/Users/x/CottonDB") } #[test] fn every_path_hangs_off_the_data_dir_and_nothing_escapes_it() { let l = layout(); for path in [ l.env_file(), l.server_log(), l.registry_db(), l.files_dir(), l.media_dir(), l.identity_dir(), l.backup_age_identity(), l.backup_earlier_identities_dir(), l.rehearsal_device_key(), l.backup_dir(), l.client_dir(), l.connection_json(), l.replica("ws-1"), l.client_media_dir(), l.media_budget_json(), l.home_lines_json(), l.snapshots_dir(), l.thumbs_cache_dir(), l.exports_dir(), l.runner_dir(), l.runner_config(), l.runner_registration(), l.runs_dir(), l.runs_work_dir(), ] { assert!( path.starts_with(l.root()), "{} escaped the data directory", path.display() ); } } #[test] fn the_names_are_the_ones_the_electron_app_wrote_so_an_existing_folder_still_reads() { let l = layout(); assert_eq!(l.env_file(), PathBuf::from("/Users/x/CottonDB/env")); assert_eq!( l.server_log(), PathBuf::from("/Users/x/CottonDB/server.log") ); assert_eq!( l.registry_db(), PathBuf::from("/Users/x/CottonDB/registry.db") ); assert_eq!( l.backup_age_identity(), PathBuf::from("/Users/x/CottonDB/identity/backup.age") ); assert_eq!( l.rehearsal_device_key(), PathBuf::from("/Users/x/CottonDB/identity/device.key") ); assert_eq!( l.connection_json(), PathBuf::from("/Users/x/CottonDB/client/connection.json") ); assert_eq!( l.replica("ws-1"), PathBuf::from("/Users/x/CottonDB/client/ws-1/replica.db") ); assert_eq!( l.client_media_dir(), PathBuf::from("/Users/x/CottonDB/client/media") ); assert_eq!( l.snapshots_dir(), PathBuf::from("/Users/x/CottonDB/client/snapshots") ); assert_eq!( l.thumbs_cache_dir(), PathBuf::from("/Users/x/CottonDB/cache/thumbs") ); } #[test] fn it_creates_nothing_by_being_asked_where_a_file_goes() { let root = tempfile::tempdir().expect("a temp dir"); let l = Layout::new(root.path().join("CottonDB")); let _ = l.identity_dir(); let _ = l.replica("ws-1"); assert!(!l.root().exists()); } }