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.

Go on without file locks only where there are none

emanuele-eme095e71510d3
diff --git a/Cargo.lock b/Cargo.lock
index 9ec33fe..65368b1 100644
--- a/Cargo.lock
+++ b/Cargo.lock
@@ -656,6 +656,7 @@ dependencies = [
"axum",
"clap",
"dirs",
+ "libc",
"rmcp",
"rusqlite",
"schemars",
diff --git a/Cargo.toml b/Cargo.toml
index fc7b01c..b6604e2 100644
--- a/Cargo.toml
+++ b/Cargo.toml
@@ -19,6 +19,8 @@ anyhow = "1.0.104"
axum = { version = "0.8.9", default-features = false, features = ["http1", "json", "query", "tokio"] }
clap = { version = "4.6.7", features = ["derive"] }
dirs = "7.0.0"
+# Already built in by other crates; tells which lock errors mean "no locks here".
+libc = "0.2.191"
rmcp = { version = "=3.7.0", features = ["server", "transport-io", "transport-streamable-http-server", "macros"] }
rusqlite = { version = "0.41.0", features = ["bundled"] }
schemars = "1.2.3"
diff --git a/src/home.rs b/src/home.rs
index fae18cb..2266760 100644
--- a/src/home.rs
+++ b/src/home.rs
@@ -1,6 +1,7 @@
//! The data folder: where it is, what it holds, and the settings in `config.toml`.
use std::fs::{self, File, OpenOptions, TryLockError};
+use std::io;
use std::net::{IpAddr, Ipv4Addr};
use std::path::{Path, PathBuf};
use std::time::{Duration, Instant};
@@ -44,6 +45,8 @@ kanzo.lock
/// How long an operation waits for `kanzo sync` before giving up with "busy".
const LOCK_WAIT: Duration = Duration::from_secs(10);
+pub const NO_LOCKS: &str = "This folder doesn't support file locks: don't sync while agents are working.";
+
#[derive(Debug, Clone, Deserialize)]
#[serde(default, deny_unknown_fields)]
pub struct Config {
@@ -133,8 +136,14 @@ impl Home {
Err(TryLockError::WouldBlock) => {
return Err(store::Error::Busy("kanzo sync is updating the board; try again in a moment".into()));
}
- // Some filesystems (exFAT, network shares) have no locks; go on without.
- Err(TryLockError::Error(_)) => break None,
+ Err(TryLockError::Error(e)) if unsupported(&e) => {
+ static WARN: std::sync::Once = std::sync::Once::new();
+ WARN.call_once(|| eprintln!("kanzo: {NO_LOCKS}"));
+ break None;
+ }
+ Err(TryLockError::Error(e)) => {
+ return Err(store::Error::Invalid(format!("cannot lock kanzo.lock: {e}")));
+ }
}
};
Store::open(&self.db_path(), lock)
@@ -157,9 +166,14 @@ impl Home {
}
/// Blocks until no connection is open, and keeps new ones out until the file is dropped.
+ /// `None` when the filesystem has no locks.
pub fn lock_exclusive(&self) -> Result<Option<File>> {
let lock = self.lock_file()?;
- Ok(lock.lock().ok().map(|_| lock))
+ match lock.lock() {
+ Ok(()) => Ok(Some(lock)),
+ Err(e) if unsupported(&e) => Ok(None),
+ Err(e) => Err(e).context("cannot lock kanzo.lock"),
+ }
}
fn lock_file(&self) -> Result<File> {
@@ -173,6 +187,15 @@ impl Home {
}
}
+/// Some filesystems (exFAT, network shares) have no file locks: Kanzo goes on without.
+fn unsupported(e: &io::Error) -> bool {
+ #[cfg(unix)]
+ if matches!(e.raw_os_error(), Some(libc::ENOTSUP | libc::ENOLCK)) {
+ return true;
+ }
+ e.kind() == io::ErrorKind::Unsupported
+}
+
fn write_if_missing(path: &Path, contents: &str) -> Result<()> {
if !path.exists() {
fs::write(path, contents).with_context(|| format!("cannot write {}", path.display()))?;
diff --git a/src/sync.rs b/src/sync.rs
index da1a884..3133eb4 100644
--- a/src/sync.rs
+++ b/src/sync.rs
@@ -5,7 +5,8 @@ use std::process::Command;
use anyhow::{Context, Result, bail};
-use crate::home::Home;
+use crate::home::{Home, NO_LOCKS};
+use crate::store::Store;
pub enum Keep {
Local,
@@ -151,11 +152,13 @@ pub fn sync(home: &Home, keep: Option<Keep>, prompt: bool) -> Result<String> {
// Wait for open connections to finish and keep new ones out while the
// database is staged. The lock is held only while git reads or replaces
// kanzo.db, never across a commit (hooks, signing prompts) or the network.
- {
- let _lock = home.lock_exclusive()?;
- crate::store::Store::open(&home.db_path(), None)?.settle()?;
+ let note = {
+ let lock = home.lock_exclusive()?;
+ Store::open(&home.db_path(), None)?.settle()?;
git(&["add", "--all"])?;
- }
+ if lock.is_some() { String::new() } else { format!("\n{NO_LOCKS}") }
+ };
+
if !git_ok(&["diff", "--cached", "--quiet"]) {
let message = format!("kanzo sync from {}", hostname());
// A machine where git doesn't know the user yet still syncs.
@@ -178,7 +181,7 @@ pub fn sync(home: &Home, keep: Option<Keep>, prompt: bool) -> Result<String> {
match &others[..] {
[] => {
git(&["push", "--quiet", "--set-upstream", &remote, &branch])?;
- return Ok(format!("Pushed the board to {remote}/{branch} for the first time."));
+ return Ok(format!("Pushed the board to {remote}/{branch} for the first time.{note}"));
}
// The repo's board is on another branch, like master: follow it, rather than
// start a second board next to it.
@@ -205,7 +208,7 @@ pub fn sync(home: &Home, keep: Option<Keep>, prompt: bool) -> Result<String> {
bail!("unexpected git output: {counts}");
};
- Ok(match (ahead, behind, keep) {
+ let done = match (ahead, behind, keep) {
(0, 0, _) => format!("Already in sync with {upstream}."),
(_, 0, _) => {
git(&["push", "--quiet"])?;
@@ -250,7 +253,8 @@ pub fn sync(home: &Home, keep: Option<Keep>, prompt: bool) -> Result<String> {
home.store()?.migrate()?;
format!("Took the board from {upstream}; this machine's old board is in kanzo.db.bak.")
}
- })
+ };
+ Ok(format!("{done}{note}"))
}
/// Git in `dir`; without `prompt` it fails rather than ask for a password or passphrase.