From 02a80e45be3d67f21eaa946300cd443fbf3e1a4a Mon Sep 17 00:00:00 2001 From: Rohit Goswami Date: Mon, 16 Nov 2026 02:22:05 +0000 Subject: [PATCH 1/4] test(tiles): two PosixStore handles on one root share temporary names Each PosixStore::open starts its own counter at 0 and names temporary files after the process id and that counter, so two handles on one root in one process draw the same name and File::create truncates the other's file before its rename. The test writes a temporary file through each of two handles and expects two paths, each with its own bytes. --- crates/rfc9162-tiles/src/store.rs | 17 +++++++++++++++++ 1 file changed, 17 insertions(+) diff --git a/crates/rfc9162-tiles/src/store.rs b/crates/rfc9162-tiles/src/store.rs index 737e47b..018c019 100644 --- a/crates/rfc9162-tiles/src/store.rs +++ b/crates/rfc9162-tiles/src/store.rs @@ -339,3 +339,20 @@ impl TileStore for MemoryStore { .collect()) } } + +#[cfg(test)] +mod tests { + use super::*; + + #[test] + fn two_handles_on_one_root_never_share_a_temporary_file() { + let dir = tempfile::tempdir().unwrap(); + let a = PosixStore::open(dir.path()).unwrap(); + let b = PosixStore::open(dir.path()).unwrap(); + let from_a = a.write_temp(b"from a").unwrap(); + let from_b = b.write_temp(b"from b").unwrap(); + assert_ne!(from_a, from_b); + assert_eq!(fs::read(&from_a).unwrap(), b"from a"); + assert_eq!(fs::read(&from_b).unwrap(), b"from b"); + } +} From f524d0dda299cf4ace2e0a162834288a9b070e3d Mon Sep 17 00:00:00 2001 From: Rohit Goswami Date: Mon, 16 Nov 2026 02:22:36 +0000 Subject: [PATCH 2/4] fix(tiles): one temporary name counter for every PosixStore The counter behind temporary file names is now a static shared by all handles in the process instead of one per open, so two handles on one root never draw the same name. The file is created with create_new, and a name that is taken, as by an earlier process with the same id, is passed over rather than truncated. A temporary file whose write or sync fails is removed. --- changelog.d/+posix-temp-names.bugfix.md | 2 + crates/rfc9162-tiles/src/store.rs | 60 ++++++++++++++++++++----- 2 files changed, 50 insertions(+), 12 deletions(-) create mode 100644 changelog.d/+posix-temp-names.bugfix.md diff --git a/changelog.d/+posix-temp-names.bugfix.md b/changelog.d/+posix-temp-names.bugfix.md new file mode 100644 index 0000000..2952955 --- /dev/null +++ b/changelog.d/+posix-temp-names.bugfix.md @@ -0,0 +1,2 @@ +`rfc9162-tiles`: `PosixStore` handles in one process draw temporary file names from one counter, so two handles on one root no longer write the same temporary file and rename each other's bytes into place. +A temporary name that is already taken is passed over rather than truncated, and a temporary file whose write fails is removed. diff --git a/crates/rfc9162-tiles/src/store.rs b/crates/rfc9162-tiles/src/store.rs index 018c019..b19986f 100644 --- a/crates/rfc9162-tiles/src/store.rs +++ b/crates/rfc9162-tiles/src/store.rs @@ -83,12 +83,15 @@ impl TileStore for Arc { } } +/// Numbers for temporary file names, drawn by every `PosixStore` in the +/// process, so that two handles on one root never pick the same name. +static TEMP_NUMBER: AtomicU64 = AtomicU64::new(0); + /// A directory. Every path is resolved under the root; `..`, absolute paths /// and empty elements are refused. #[derive(Debug, Clone)] pub struct PosixStore { root: PathBuf, - counter: Arc, } impl PosixStore { @@ -97,10 +100,7 @@ impl PosixStore { let root = root.into(); fs::create_dir_all(&root)?; fs::create_dir_all(root.join(".tmp"))?; - Ok(Self { - root, - counter: Arc::new(AtomicU64::new(0)), - }) + Ok(Self { root }) } /// The directory the store lives in. @@ -126,14 +126,27 @@ impl PosixStore { Ok(self.root.join(relative)) } + /// A new file under `.tmp` holding `bytes`, synced. It is named after the + /// process id and a number from `TEMP_NUMBER`; a name that is taken, as + /// by an earlier process with the same id, is passed over rather than + /// truncated. A write that fails removes the file. fn write_temp(&self, bytes: &[u8]) -> io::Result { - let n = self.counter.fetch_add(1, Ordering::Relaxed); - let name = format!("{}-{n}", std::process::id()); - let temp = self.root.join(".tmp").join(name); - let mut file = File::create(&temp)?; - file.write_all(bytes)?; - file.sync_all()?; - Ok(temp) + loop { + let n = TEMP_NUMBER.fetch_add(1, Ordering::Relaxed); + let name = format!("{}-{n}", std::process::id()); + let temp = self.root.join(".tmp").join(name); + let mut file = match OpenOptions::new().write(true).create_new(true).open(&temp) { + Ok(file) => file, + Err(e) if e.kind() == io::ErrorKind::AlreadyExists => continue, + Err(e) => return Err(e), + }; + if let Err(e) = file.write_all(bytes).and_then(|()| file.sync_all()) { + drop(file); + let _ = fs::remove_file(&temp); + return Err(e); + } + return Ok(temp); + } } fn sync_dir(dir: &Path) -> io::Result<()> { @@ -355,4 +368,27 @@ mod tests { assert_eq!(fs::read(&from_a).unwrap(), b"from a"); assert_eq!(fs::read(&from_b).unwrap(), b"from b"); } + + #[test] + fn a_temporary_name_already_taken_is_passed_over() { + let dir = tempfile::tempdir().unwrap(); + let store = PosixStore::open(dir.path()).unwrap(); + let next = TEMP_NUMBER.load(Ordering::Relaxed); + let taken: Vec = (next..next + 3) + .map(|n| { + dir.path() + .join(".tmp") + .join(format!("{}-{n}", std::process::id())) + }) + .collect(); + for path in &taken { + fs::write(path, b"left behind").unwrap(); + } + let temp = store.write_temp(b"new").unwrap(); + assert!(!taken.contains(&temp)); + assert_eq!(fs::read(&temp).unwrap(), b"new"); + for path in &taken { + assert_eq!(fs::read(path).unwrap(), b"left behind"); + } + } } From 620508c07dcf6d5d56b26e43cd1cc3e2c1a693af Mon Sep 17 00:00:00 2001 From: Rohit Goswami Date: Mon, 16 Nov 2026 02:22:55 +0000 Subject: [PATCH 3/4] test(tiles): a create_new cut short leaves a partial file at its path The module doc says a write lands whole or not at all, but PosixStore::create_new opens the path itself with create_new and writes into it. A write that fails part way, or a crash, leaves the head of the bytes at the path, and every later create_new there answers that something is already stored. The test runs itself again under a file size limit, where create_new of 64 KiB fails with EFBIG, then expects nothing at the path or under .tmp and a create_new that succeeds. --- crates/rfc9162-tiles/tests/store.rs | 53 +++++++++++++++++++++++++++++ 1 file changed, 53 insertions(+) create mode 100644 crates/rfc9162-tiles/tests/store.rs diff --git a/crates/rfc9162-tiles/tests/store.rs b/crates/rfc9162-tiles/tests/store.rs new file mode 100644 index 0000000..3f76466 --- /dev/null +++ b/crates/rfc9162-tiles/tests/store.rs @@ -0,0 +1,53 @@ +//! PosixStore writes land whole or not at all. + +use std::ffi::OsStr; +use std::process::{Command, Output}; + +use rfc9162_tiles::{PosixStore, TileStore}; + +/// Set in a child run of this binary to the store root it writes under. +const CHILD_ROOT: &str = "RFC9162_TILES_STORE_TEST_ROOT"; + +/// Runs the test `name` of this binary again with `root` in `CHILD_ROOT`, +/// under a file size limit of at most 8 KiB (`ulimit -f 8` counts 512-byte +/// blocks in dash and 1 KiB ones in bash) and with SIGXFSZ ignored, so a +/// write past the limit fails with EFBIG instead of killing the process. +fn run_under_file_size_limit(name: &str, root: &OsStr) -> Output { + Command::new("sh") + .arg("-c") + .arg(r#"ulimit -f 8 && trap '' XFSZ && exec "$0" --exact "$1" --test-threads=1"#) + .arg(std::env::current_exe().unwrap()) + .arg(name) + .env(CHILD_ROOT, root) + .output() + .unwrap() +} + +#[test] +fn a_create_new_cut_short_leaves_nothing_at_the_path() { + let bytes = vec![7u8; 64 * 1024]; + if let Some(root) = std::env::var_os(CHILD_ROOT) { + let store = PosixStore::open(root).unwrap(); + assert!(store.create_new("tile/0/000", &bytes).is_err()); + return; + } + let dir = tempfile::tempdir().unwrap(); + let child = run_under_file_size_limit( + "a_create_new_cut_short_leaves_nothing_at_the_path", + dir.path().as_os_str(), + ); + assert!( + child.status.success(), + "the child failed: {}{}", + String::from_utf8_lossy(&child.stdout), + String::from_utf8_lossy(&child.stderr) + ); + let store = PosixStore::open(dir.path()).unwrap(); + assert_eq!(store.get("tile/0/000").unwrap().map(|b| b.len()), None); + assert_eq!( + std::fs::read_dir(dir.path().join(".tmp")).unwrap().count(), + 0 + ); + assert!(store.create_new("tile/0/000", &bytes).unwrap()); + assert_eq!(store.get("tile/0/000").unwrap(), Some(bytes)); +} From 5a63eb9db354e17fad89a123cf7b97d0c7ffc683 Mon Sep 17 00:00:00 2001 From: Rohit Goswami Date: Mon, 16 Nov 2026 02:23:27 +0000 Subject: [PATCH 4/4] fix(tiles): PosixStore::create_new lands whole or not at all create_new now writes and syncs a temporary file, links it at the path with hard_link, which fails with AlreadyExists when something is there, removes the temporary name and syncs the directory. A failed write leaves nothing at the path, as put already did, so a retry can create it. The tree-state and publish locks and rfc9162-log's run files are made this way. The module and trait docs say so, and that the root needs hard links. --- changelog.d/+posix-create-new.bugfix.md | 2 ++ crates/rfc9162-tiles/src/store.rs | 27 +++++++++++++------------ 2 files changed, 16 insertions(+), 13 deletions(-) create mode 100644 changelog.d/+posix-create-new.bugfix.md diff --git a/changelog.d/+posix-create-new.bugfix.md b/changelog.d/+posix-create-new.bugfix.md new file mode 100644 index 0000000..75c6365 --- /dev/null +++ b/changelog.d/+posix-create-new.bugfix.md @@ -0,0 +1,2 @@ +`rfc9162-tiles`: `PosixStore::create_new` writes a temporary file and links it at the path, so a write cut short by an error or a crash leaves nothing there, as the store promises, instead of a partial file that every later `create_new` takes for one already stored. +The root must be on a file system with hard links. diff --git a/crates/rfc9162-tiles/src/store.rs b/crates/rfc9162-tiles/src/store.rs index b19986f..d0c86b5 100644 --- a/crates/rfc9162-tiles/src/store.rs +++ b/crates/rfc9162-tiles/src/store.rs @@ -2,9 +2,11 @@ //! and an in-memory store for tests and single-process use. //! //! Paths are `/`-separated and relative to the store root. A write lands -//! whole or not at all: the POSIX store writes a temporary file beside the -//! target, syncs it, renames it over the path and syncs the directory, as -//! Tessera's POSIX storage does. +//! whole or not at all: the POSIX store writes a temporary file under the +//! root's `.tmp`, syncs it, renames it over the path and syncs the +//! directory, as Tessera's POSIX storage does. `create_new` links the +//! temporary file at the path instead, which fails when something is +//! there, so the root must be on a file system with hard links. use std::collections::BTreeMap; use std::fs::{self, File, OpenOptions}; @@ -19,8 +21,8 @@ pub trait TileStore { fn get(&self, path: &str) -> io::Result>>; /// Writes `bytes` at `path`, whole or not at all, replacing what is there. fn put(&self, path: &str, bytes: &[u8]) -> io::Result<()>; - /// Writes `bytes` at `path` only when nothing is there; `false` when - /// something is. + /// Writes `bytes` at `path`, whole or not at all, only when nothing is + /// there; `false` when something is. fn create_new(&self, path: &str, bytes: &[u8]) -> io::Result; /// Removes `path`; `false` when there was nothing to remove. fn delete(&self, path: &str) -> io::Result; @@ -199,14 +201,13 @@ impl TileStore for PosixStore { .parent() .ok_or_else(|| io::Error::new(io::ErrorKind::InvalidInput, "no parent"))?; fs::create_dir_all(parent)?; - match OpenOptions::new() - .write(true) - .create_new(true) - .open(&target) - { - Ok(mut file) => { - file.write_all(bytes)?; - file.sync_all()?; + let temp = self.write_temp(bytes)?; + let linked = fs::hard_link(&temp, &target); + // The bytes are at the path or nowhere; the temporary name goes + // either way, and failing to remove it leaves only a stray file. + let _ = fs::remove_file(&temp); + match linked { + Ok(()) => { Self::sync_dir(parent)?; Ok(true) }