Skip to content

armgit / kanzo

Public Imported from GitHub
0

Self-hosted, sharable kanban for humans and agents. One 4.7 MB binary, 5 MB of RAM, 10 to 60 tokens per agent reply, starts in 3 ms, zero setup.

Keep config.toml on disk while a pull sets it aside

emanuele-em3ee8ced0095b
diff --git a/src/home.rs b/src/home.rs
index 1710df7..0fc7ec9 100644
--- a/src/home.rs
+++ b/src/home.rs
@@ -89,6 +89,11 @@ impl Home {
// During a pull, config.toml can be missing or another machine's, until kanzo sync puts it back.
let lock = home.lock_shared()?;
let path = home.dir.join("config.toml");
+ // Holding the lock, this can only be left by a sync that died during a pull.
+ let aside = home.config_aside();
+ if aside.exists() {
+ fs::rename(&aside, &path).with_context(|| format!("cannot restore {}", path.display()))?;
+ }
write_if_missing(&path, CONFIG_TEMPLATE)?;
let text = fs::read_to_string(&path).with_context(|| format!("cannot read {}", path.display()))?;
home.config = toml::from_str(&text).with_context(|| format!("{} is not valid", path.display()))?;
@@ -102,6 +107,11 @@ impl Home {
self.dir.join("kanzo.db")
}
+ /// Where `kanzo sync` keeps this machine's config.toml during a pull; git ignores `*.bak`.
+ pub fn config_aside(&self) -> PathBuf {
+ self.dir.join("config.toml.bak")
+ }
+
/// Changes when the database file itself is replaced, as `kanzo sync` does
/// when it pulls, so the board reloads even if the pulled change counter
/// happens to equal the old one.
@@ -296,6 +306,19 @@ mod tests {
assert!(opening.join().unwrap().unwrap().config.listen.is_loopback());
}
+ #[test]
+ fn open_restores_a_config_toml_left_aside_by_a_crash() {
+ let tmp = tempfile::tempdir().unwrap();
+ fs::write(tmp.path().join("config.toml.bak"), "listen = \"0.0.0.0\"\n").unwrap();
+ // What git had pulled when the sync died.
+ fs::write(tmp.path().join("config.toml"), "port = 4300\n").unwrap();
+
+ let home = Home::open(tmp.path().to_path_buf()).unwrap();
+ assert!(!home.config.listen.is_loopback());
+ assert_eq!(home.config.port, DEFAULT_PORT);
+ assert!(!home.config_aside().exists());
+ }
+
#[test]
fn no_lock_only_when_unsupported() {
assert!(unsupported(&io::Error::from(io::ErrorKind::Unsupported)));
diff --git a/src/sync.rs b/src/sync.rs
index 9d66e94..b907513 100644
--- a/src/sync.rs
+++ b/src/sync.rs
@@ -235,16 +235,17 @@ fn replace<T>(home: &Home, pull: impl FnOnce() -> Result<T>) -> Result<()> {
// would otherwise apply to the pulled file.
Store::open(&home.db_path(), None)?.settle()?;
// A board an older Kanzo synced has a config.toml in it, which a pull could change, delete, or
- // stop at. This machine's own steps aside, so git never sees it, and comes back as it was.
+ // stop at. This machine's own steps aside, so git never sees it, and comes back as it was. If
+ // this process dies first, it waits on disk for Home::open or the next pull.
let config = home.dir.join("config.toml");
+ let aside = home.config_aside();
let in_board = |at: &str| git_ok(&home.dir, false, &["cat-file", "-e", &format!("{at}:config.toml")]);
- let mine = if in_board("HEAD") || in_board("@{upstream}") { std::fs::read(&config).ok() } else { None };
- if mine.is_some() {
- std::fs::remove_file(&config).context("cannot move config.toml aside")?;
+ if (in_board("HEAD") || in_board("@{upstream}")) && config.exists() && !aside.exists() {
+ std::fs::rename(&config, &aside).context("cannot move config.toml aside")?;
}
let pulled = pull();
- if let Some(mine) = mine {
- crate::home::write_whole(&config, &mine).context("cannot put config.toml back")?;
+ if aside.exists() {
+ std::fs::rename(&aside, &config).context("cannot put config.toml back")?;
}
pulled?;
Store::open(&home.db_path(), None)?.migrate()?;
@@ -413,6 +414,34 @@ mod tests {
}
}
+ #[test]
+ fn config_toml_waits_on_disk_during_a_pull_that_fails() {
+ let tmp = tempfile::tempdir().unwrap();
+ let (_, pi) = older_pi(tmp.path());
+ let pulled = replace(&pi, || -> Result<()> {
+ assert!(!pi.dir.join("config.toml").exists());
+ assert!(std::fs::read_to_string(pi.config_aside()).unwrap().contains("0.0.0.0"));
+ bail!("no network")
+ });
+
+ assert!(pulled.is_err());
+ assert!(!pi.config_aside().exists());
+ assert!(!Home::open(pi.dir.clone()).unwrap().config.listen.is_loopback());
+ }
+
+ #[test]
+ fn a_pull_keeps_the_config_toml_a_dead_sync_left_aside() {
+ let tmp = tempfile::tempdir().unwrap();
+ let (_, pi) = older_pi(tmp.path());
+ std::fs::rename(pi.dir.join("config.toml"), pi.config_aside()).unwrap();
+ // What git had pulled when the sync died.
+ std::fs::write(pi.dir.join("config.toml"), "port = 4300\n").unwrap();
+
+ replace(&pi, || Ok(())).unwrap();
+ assert!(!pi.config_aside().exists());
+ assert!(!Home::open(pi.dir.clone()).unwrap().config.listen.is_loopback());
+ }
+
#[test]
fn urls() {
let ssh = "[email protected]:you/kanzo-data.git";