From afe1a1b4342557e9bc166e11ed79068fe894a100 Mon Sep 17 00:00:00 2001 From: Weksa Date: Tue, 10 Nov 2026 07:10:43 +0000 Subject: [PATCH] create: copy the inventory with create_new, so a root that holds inventory.cc refuses and never truncates it Co-Authored-By: Claude Sonnet 5.5 --- crates/weksa/src/run.rs | 16 ++++++++++- crates/weksa/tests/golf_loop.rs | 48 +++++++++++++++++++++++++++++++-- 2 files changed, 61 insertions(+), 3 deletions(-) diff --git a/crates/weksa/src/run.rs b/crates/weksa/src/run.rs index 060f929..ab74f5b 100644 --- a/crates/weksa/src/run.rs +++ b/crates/weksa/src/run.rs @@ -418,6 +418,20 @@ fn one_run(cache: &CultCache) -> Result, RunError> { .next()) } +/// Copies `from` onto a `to` that must not exist: a root that already holds a +/// file of that name (the source itself, say) refuses before anything is written. +fn copy_new(from: &Path, to: &Path) -> Result<(), RunError> { + let io = |e: std::io::Error| RunError::Io(e.kind()); + let mut source = std::fs::File::open(from).map_err(io)?; + let mut dest = std::fs::OpenOptions::new() + .write(true) + .create_new(true) + .open(to) + .map_err(io)?; + std::io::copy(&mut source, &mut dest).map_err(io)?; + Ok(()) +} + impl RunStores { /// Pins the three stores at `root`, which must hold no run. A store file /// that does not exist is `Io(NotFound)`. @@ -457,7 +471,7 @@ impl RunStores { }; // The run works on its own copy; the caller's store is never opened again. let head = root.join(INVENTORY_FILE); - std::fs::copy(&record.inventory, &head).map_err(|e| RunError::Io(e.kind()))?; + copy_new(Path::new(&record.inventory), &head)?; let record = RunRecord { corpus_digest: file_digest(&record.corpus).ok_or(RunError::Store)?, entity_digest: file_digest(&record.entities).ok_or(RunError::Store)?, diff --git a/crates/weksa/tests/golf_loop.rs b/crates/weksa/tests/golf_loop.rs index 6d0157b..2397587 100644 --- a/crates/weksa/tests/golf_loop.rs +++ b/crates/weksa/tests/golf_loop.rs @@ -213,6 +213,50 @@ fn a_run_works_on_its_own_copy_of_the_inventory() { ); } +#[test] +fn create_refuses_a_root_that_holds_an_inventory_and_never_truncates_it() { + let dao = dao(); + let bytes = std::fs::read(&dao.inventory).unwrap(); + let create = |root: &Path, inventory: &Path| { + RunStores::create( + root, + "x", + RunPaths { + corpus: &dao.corpus, + inventory, + entities: &dao.entities, + }, + "fake-model", + "bag", + ) + }; + + // The source is the root's own inventory.cc: the copy would open it as its + // destination. It is refused and the source bytes survive. + let own = dao.root.join("own"); + std::fs::create_dir_all(&own).unwrap(); + std::fs::write(own.join("inventory.cc"), &bytes).unwrap(); + assert_eq!( + create(&own, &own.join("inventory.cc")).err(), + Some(RunError::Io(std::io::ErrorKind::AlreadyExists)) + ); + assert_eq!(std::fs::read(own.join("inventory.cc")).unwrap(), bytes); + + // Any other file of that name in the root is refused untouched, too. + let other = dao.root.join("other"); + std::fs::create_dir_all(&other).unwrap(); + std::fs::write(other.join("inventory.cc"), b"not yours").unwrap(); + assert_eq!( + create(&other, &dao.inventory).err(), + Some(RunError::Io(std::io::ErrorKind::AlreadyExists)) + ); + assert_eq!( + std::fs::read(other.join("inventory.cc")).unwrap(), + b"not yours" + ); + assert_eq!(std::fs::read(&dao.inventory).unwrap(), bytes); +} + /// `weksa.loop_run` as it was before the run kept its own inventory copy: keys /// 0 to 10. #[derive(Clone, cultcache_rs::DatabaseEntry)] @@ -1954,9 +1998,9 @@ fn source_tripwires() { let count = |needle: &str| code.iter().filter(|l| l.contains(needle)).count(); assert!(!run.contains("pub fn new")); assert_eq!(count("admit_revision("), 1, "one door call"); - assert_eq!(count("std::fs::copy"), 2, "create's copy and cycle's"); + assert_eq!(count("std::fs::copy"), 1, "the cycle's copy; create's is copy_new"); assert_eq!( - count("record.inventory,"), + count("Path::new(&record.inventory)"), 1, "the source path is read once" );