From 8620f0611cfb02ea1e5ef7a7522c4dcf939ce8fb Mon Sep 17 00:00:00 2001 From: dotCooCoo Date: Sat, 14 Nov 2026 15:37:25 -0800 Subject: [PATCH] Scripts: a file whose write fails after it was emptied is named as written Writing an open file's bytes empties the file first. When the write then failed, for example on a full disk, the file had lost its old bytes but was left out of the files the run names as written. It is now named as soon as it has been opened for the write. --- crates/engine/src/script/files.rs | 16 ++++++++++++---- crates/engine/src/tests/script_io.rs | 17 +++++++++++++++++ 2 files changed, 29 insertions(+), 4 deletions(-) diff --git a/crates/engine/src/script/files.rs b/crates/engine/src/script/files.rs index 7f4052dfb8..05f148e714 100644 --- a/crates/engine/src/script/files.rs +++ b/crates/engine/src/script/files.rs @@ -762,11 +762,17 @@ impl Disk { if moved || std::fs::symlink_metadata(&real).is_ok_and(|m| m.file_type().is_symlink()) { return Err(ACCESS_DENIED); } - // The file stays changed until its bytes are written, so the run's end tries it again. - std::fs::write(&real, &file.bytes).map_err(|_| WRITE_FAILED)?; - file.dirty = false; + // The file stays changed until its bytes are written, so the run's end tries it again. Once + // it is opened its old bytes are gone, so a write that then fails names it in the report too. + let mut out = std::fs::File::create(&real).map_err(|_| WRITE_FAILED)?; + let written = std::io::Write::write_all(&mut out, &file.bytes).is_ok(); + #[cfg(test)] + let written = written && !FLUSH_WRITE_FAILS.with(std::cell::Cell::get); + if written { + file.dirty = false; + } self.wrote(&real); - Ok(()) + if written { Ok(()) } else { Err(WRITE_FAILED) } } /// Writes every open file that has changed: at the end of the run. → the files that could not @@ -2357,6 +2363,8 @@ pub(crate) type CopyHook = Box; #[cfg(test)] thread_local! { pub(crate) static BEFORE_COPY_READ: std::cell::RefCell> = const { std::cell::RefCell::new(None) }; + /// Tests: while set, the bytes `flush` writes fail after the file was opened for them. + pub(crate) static FLUSH_WRITE_FAILS: std::cell::Cell = const { std::cell::Cell::new(false) }; } #[cfg(test)] diff --git a/crates/engine/src/tests/script_io.rs b/crates/engine/src/tests/script_io.rs index 6637d3fe8d..eca6dd5638 100644 --- a/crates/engine/src/tests/script_io.rs +++ b/crates/engine/src/tests/script_io.rs @@ -515,6 +515,23 @@ fn script_files_write_in_the_temporary_and_data_folders() { let _ = std::fs::remove_dir_all(base); } +/// A file whose bytes fail to be written after it was opened for them has lost its old bytes, so the +/// failed run names it among the files that stay written. +#[test] +fn script_file_whose_write_fails_partway_is_named_as_written() { + let base = folder("flushfails"); + let mut s = with_data(&base); + let r = run_in(&mut s, &base, "var f = new File(Folder.userData.fsName + '/f.txt'); f.open('w'); f.write('old'); f.close(); vc.log(f.fsName);") + .unwrap(); + let path = r["log"][0].as_str().unwrap().to_string(); + crate::script::files::FLUSH_WRITE_FAILS.with(|f| f.set(true)); + let r = run_in(&mut s, &base, "var f = new File(Folder.userData.fsName + '/f.txt'); f.open('e'); f.write('new'); f.close();"); + crate::script::files::FLUSH_WRITE_FAILS.with(|f| f.set(false)); + let e = format!("{}", r.unwrap_err()); + assert!(e.split("these files stay written:").nth(1).is_some_and(|w| w.contains(&path)), "{e}"); + let _ = std::fs::remove_dir_all(base); +} + /// `copy` reads its source under the rules for reading: a source that a link to a file outside the /// script's places replaced after it was looked up is refused, and nothing is copied. #[cfg(unix)]