mirror of
https://github.com/storytold/vectorcraft.git
synced 2026-11-18 10:07:27 +00:00
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.
This commit is contained in:
@@ -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<dyn FnOnce(&Path, &Path)>;
|
||||
#[cfg(test)]
|
||||
thread_local! {
|
||||
pub(crate) static BEFORE_COPY_READ: std::cell::RefCell<Option<CopyHook>> = 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<bool> = const { std::cell::Cell::new(false) };
|
||||
}
|
||||
|
||||
#[cfg(test)]
|
||||
|
||||
@@ -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)]
|
||||
|
||||
Reference in New Issue
Block a user