From 82620a1b27aaed84b3f01fcb9c341274eb995034 Mon Sep 17 00:00:00 2001 From: dotCooCoo Date: Sat, 14 Nov 2026 12:58:51 -0800 Subject: [PATCH] Scripts: copy and length reach files open for reading A file opened with open('r') kept its old bytes after copy wrote over it or length changed it, while an object open for writing took the new bytes. Both now update every object that holds the file. An object that holds a renamed file is found at the new path, and an object whose file was removed is not taken for a file made later at its path. --- crates/engine/src/script/files.rs | 34 +++++++++++++++++++--------- crates/engine/src/tests/script_io.rs | 29 ++++++++++++++++++++++++ 2 files changed, 52 insertions(+), 11 deletions(-) diff --git a/crates/engine/src/script/files.rs b/crates/engine/src/script/files.rs index 0e863fa03d..559b660d08 100644 --- a/crates/engine/src/script/files.rs +++ b/crates/engine/src/script/files.rs @@ -1448,6 +1448,9 @@ struct Open { /// The run may no longer read the file the object holds: `read`, `readln`, `readch` and `seek` /// give [`ACCESS_DENIED`], and `eof` is true. unreadable: bool, + /// The file the object holds was removed, so a file made later at its path is not the one it + /// holds. + removed: bool, } impl Open { @@ -1477,9 +1480,19 @@ impl Open { dirty: false, withdrawn: false, unreadable: false, + removed: false, }) } + /// Does the object hold the bytes of the file at the real path `real`: the file it writes to, or, + /// when it writes to none, the file it was opened on while that file was not removed? + fn holds(&self, real: &Path) -> bool { + match self.target.as_deref() { + Some(t) => t == real, + None => !self.removed && self.read_from.as_deref() == Some(real), + } + } + /// Writes `text` at the position, over what is there and past the end, when it takes at most /// `left` bytes; → the bytes written. fn write(&mut self, text: &str, left: usize) -> Result { @@ -1797,7 +1810,7 @@ fn copy(host: &Host, a: &[Data]) -> Result { }; // The target's open objects each take a copy of the bytes, which count as written too, as does // the copy itself when it is written. - let holders = disk.open.values().filter(|o| o.target.as_deref() == Some(target.as_path())).count(); + let holders = disk.open.values().filter(|o| o.holds(&target)).count(); disk.charge_write(copied.len().saturating_mul(holders.saturating_add(usize::from(!itself))))?; // The copy takes the source's permissions, as `std::fs::copy` gives them. let permissions = std::fs::metadata(&source).map(|m| m.permissions()); @@ -1806,9 +1819,10 @@ fn copy(host: &Host, a: &[Data]) -> Result { if !itself && (r.is_ok() || target.exists()) { disk.wrote(&target); } - // Open objects of the target hold the copy, so closing them does not write their older bytes. + // Open objects of the target hold the copy, so those opened for reading read it and closing the + // others does not write their older bytes. if r.is_ok() { - for file in disk.open.values_mut().filter(|o| o.target.as_deref() == Some(target.as_path())) { + for file in disk.open.values_mut().filter(|o| o.holds(&target)) { file.bytes.clone_from(&copied); file.pos = file.pos.min(copied.len()); file.dirty = false; @@ -1843,10 +1857,12 @@ fn remove(host: &Host, a: &[Data]) -> Result { }; if r.is_ok() { // Other open objects of the file drop their changes and take no more writes, so neither - // their close nor the run's end makes the file again. - for file in disk.open.values_mut().filter(|o| o.target.as_deref() == Some(target.as_path())) { + // their close nor the run's end makes the file again, and no object holds a file made later + // at its path. + for file in disk.open.values_mut().filter(|o| o.holds(&target)) { file.dirty = false; file.target = None; + file.removed = true; } disk.wrote(&target); } @@ -1952,11 +1968,7 @@ fn set_length(host: &Host, a: &[Data]) -> Result { Ok(m) => { // Each open object of the file holds its own bytes, so what each one grows by counts too. let len = usize::try_from(n).unwrap_or(usize::MAX); - let held = disk - .open - .values() - .filter(|o| o.target.as_deref() == Some(target.as_path())) - .fold(0usize, |sum, o| sum.saturating_add(len.saturating_sub(o.bytes.len()))); + let held = disk.open.values().filter(|o| o.holds(&target)).fold(0usize, |sum, o| sum.saturating_add(len.saturating_sub(o.bytes.len()))); disk.charge_write(usize::try_from(n.saturating_sub(m.len())).unwrap_or(usize::MAX).saturating_add(held))?; // The file's open objects write what they hold first, and then hold the new length. let ids: Vec = disk.open.iter().filter(|(_, o)| o.target.as_deref() == Some(target.as_path())).map(|(id, _)| *id).collect(); @@ -1967,7 +1979,7 @@ fn set_length(host: &Host, a: &[Data]) -> Result { }; if r.is_ok() { let len = usize::try_from(n).unwrap_or(usize::MAX); - for file in disk.open.values_mut().filter(|o| o.target.as_deref() == Some(target.as_path())) { + for file in disk.open.values_mut().filter(|o| o.holds(&target)) { file.bytes.resize(len, 0); file.pos = file.pos.min(len); } diff --git a/crates/engine/src/tests/script_io.rs b/crates/engine/src/tests/script_io.rs index 7b5db047ef..62f031528b 100644 --- a/crates/engine/src/tests/script_io.rs +++ b/crates/engine/src/tests/script_io.rs @@ -889,6 +889,35 @@ fn script_file_copy_of_an_open_file_has_its_changes() { let _ = std::fs::remove_dir_all(base); } +/// An object that has a file open for reading holds what `copy` writes over the file and the length +/// `length` sets, as an object open for writing does, also after the file is renamed, and not after +/// it is removed. +#[test] +fn script_file_open_for_reading_holds_a_copy_and_a_new_length() { + let base = folder("copyreader"); + let r = run( + &base, + "var d = Folder.temp.fsName, s = new File(d + '/s.txt'); s.open('w'); s.write('copied'); s.close();\n\ + var t = new File(d + '/t.txt'); t.open('w'); t.write('old'); t.close(); t.open('r');\n\ + var out = [s.copy(t.fsName), t.read()]; t.close();\n\ + var u = new File(d + '/u.txt'); u.open('w'); u.write('abcdef'); u.close(); u.open('r');\n\ + new File(u.fsName).length = 2; out.push(u.read()); u.close(); vc.log(out);", + ); + assert_eq!(r["log"], json!([[true, "copied", "ab"]])); + // A renamed file is held at its new path; a removed one is not held by a file made at its path. + let r = run( + &base, + "var d = Folder.temp.fsName, s = new File(d + '/s.txt'); s.open('w'); s.write('copied'); s.close();\n\ + var v = new File(d + '/v.txt'); v.open('w'); v.write('old'); v.close(); v.open('r');\n\ + var w = new File(d + '/v.txt'); var out = [w.rename('w.txt'), s.copy(d + '/v.txt'), v.read()];\n\ + v.seek(0); out.push(s.copy(w.fsName), v.read()); v.close();\n\ + var x = new File(d + '/x.txt'); x.open('w'); x.write('kept'); x.close(); x.open('r');\n\ + out.push(new File(x.fsName).remove(), s.copy(d + '/x.txt'), x.read()); x.close(); vc.log(out);", + ); + assert_eq!(r["log"], json!([[true, true, "old", true, "copied", true, true, "kept"]])); + let _ = std::fs::remove_dir_all(base); +} + /// A file whose `close` could not write it stays open, so a later `close` writes it. #[test] fn script_file_close_that_fails_can_be_tried_again() {