diff options
Diffstat (limited to 'src/config.rs')
| -rw-r--r-- | src/config.rs | 33 |
1 files changed, 26 insertions, 7 deletions
diff --git a/src/config.rs b/src/config.rs index bda99e9..00ac3e4 100644 --- a/src/config.rs +++ b/src/config.rs @@ -167,8 +167,19 @@ impl Config { /// `anyhow::Result` here meant two error vocabularies in one crate, and the /// one thing a caller can actually branch on — "there is no config yet, run /// init" — arrived as an opaque string. + /// + /// **The root is the directory the config file was found in.** The `root` + /// field inside the file is written for whoever is looking at the tree, not + /// as a second source of truth: `load` is only ever handed the directory it + /// then reads the config from, so a field that disagreed with its own + /// location could only ever contradict it. Honouring a relative field by + /// re-joining it onto the config's directory is what turned `--root runtime` + /// (which `init` used to save verbatim) into `runtime/runtime/…` — every + /// command after `init` ran against a second tree with its own database, + /// while `init` had created and migrated the first one. pub fn load(root: &Path) -> crate::error::Result<Self> { - let p = root.join("config.json"); + let dir = absolute(root)?; + let p = dir.join("config.json"); if !p.exists() { return Err(crate::error::Error::not_found(format!( "no config at {}; run `aur-build init`", @@ -177,19 +188,20 @@ impl Config { } let s = std::fs::read_to_string(&p)?; let mut cfg: Config = serde_json::from_str(&s)?; - // root inside the config file is relative to the config's directory - // unless it is absolute; that keeps a moved tree usable. - if !cfg.root.is_absolute() { - cfg.root = root.join(&cfg.root); - } + cfg.root = dir; Ok(cfg) } pub fn save(&self) -> crate::error::Result<()> { std::fs::create_dir_all(&self.root)?; + let mut out = self.clone(); + // Absolutize on the way out, so a config written by a process that was + // handed a relative `--root` can never be misread — by the next command, + // or by a person editing it by hand. + out.root = absolute(&self.root)?; std::fs::write( self.root.join("config.json"), - serde_json::to_string_pretty(self)? + "\n", + serde_json::to_string_pretty(&out)? + "\n", )?; Ok(()) } @@ -319,6 +331,13 @@ impl Config { } } +/// Absolutize a path without resolving symlinks and without requiring it to +/// exist, both of which `canonicalize` insists on — and `init` saves its config +/// before the tree under it is complete. +fn absolute(p: &Path) -> crate::error::Result<PathBuf> { + Ok(std::path::absolute(p)?) +} + /// Filesystem-safe slug for a URL, used in cache filenames. pub fn slug(url: &str) -> String { url.chars() |
