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 <noreply@anthropic.com>
This commit is contained in:
Weksa 2026-11-10 07:10:43 +00:00
commit afe1a1b434
2 changed files with 61 additions and 3 deletions

View file

@ -418,6 +418,20 @@ fn one_run(cache: &CultCache) -> Result<Option<RunRecord>, 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)?,

View file

@ -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"
);