mirror of
https://github.com/storytold/vectorcraft.git
synced 2026-11-18 13:33:51 +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)]
|
||||
|
||||
Reference in New Issue
Block a user