Files: check a write again before it is put in place off Unix

Off Unix, write_atomic_checked asked `allowed` once, before the file
was written, so a destination that stopped being allowed while the file
was filled still received it. The check now runs again after the fill,
right before the rename, and again before the content is copied over a
file whose folder refuses the rename. A refusal removes the staging
file and leaves the target as it was.

The hard-link fixtures in the capture tests unwrap the link again, as
the other hard-link tests in the engine do (script_io.rs, plugins.rs).
This commit is contained in:
dotCooCoo committed 2026-12-02 23:09:11 -08:00
1 parent 14e8959bc0
commit 96ba49f5bd
2 files changed
+85 -19

No files matched your search

@@ -728,11 +728,7 @@ fn capture_refuses_a_file_with_other_hard_links() {
let other = dir.join("other.bin");
std::fs::write(&other, b"kept").unwrap();
let alias = dir.join("alias.png");
if std::fs::hard_link(&other, &alias).is_err() {
// The volume has no hard links.
let _ = std::fs::remove_dir_all(base);
return;
}
std::fs::hard_link(&other, &alias).unwrap();
let e = file_failure(&mut s, &base, &format!("Folder.userData; app.activeDocument.imageCapture(new File({}));", quoted(&alias)));
assert!(e.contains("other hard links"), "{e}");
assert_eq!(std::fs::read(&other).unwrap(), b"kept");
@@ -917,15 +913,11 @@ fn capture_to_a_missing_links_path_writes_nothing() {
} else {
script(&mut s, &base, &text);
assert!(upper.exists() && !gone.exists(), "a different file on a volume that keeps case");
// Two names for one file that differ only in case show that the folder keeps case. A
// volume without hard links skips this part.
// Two names for one file that differ only in case show that the folder keeps case.
let sub = dir.join("sub");
std::fs::create_dir(&sub).unwrap();
std::fs::write(sub.join("probe.txt"), b"x").unwrap();
if std::fs::hard_link(sub.join("probe.txt"), sub.join("PROBE.TXT")).is_err() {
let _ = std::fs::remove_dir_all(base);
return;
}
std::fs::hard_link(sub.join("probe.txt"), sub.join("PROBE.TXT")).unwrap();
s.edit("Place", |d, _| {
let id = d.alloc_id();
let link = Some(vectorcraft_doc::LinkInfo { path: "sub/gone.png".into(), relative: Some("sub/gone.png".into()), ..Default::default() });
+82 -8
View File
@@ -265,15 +265,19 @@ pub fn write_atomic_checked(
}
}
/// See the Unix version.
/// See the Unix version. Here `allowed(path)` runs before the file is written and again after it is
/// filled, right before it is put in place; a refusal then removes the temporary file and leaves
/// `path` unchanged.
#[cfg(not(unix))]
pub fn write_atomic_checked(
path: &Path,
fill: impl FnOnce(&mut File) -> io::Result<()>,
allowed: impl FnOnce(&Path) -> io::Result<()>,
allowed: impl Fn(&Path) -> io::Result<()>,
) -> io::Result<()> {
allowed(path)?;
write_atomic_with(path, fill)
let replacing = path.exists();
let create = |tmp: &Path| if replacing { create_private(tmp) } else { create_new(tmp) };
write_atomic_staged(path, fill, create, || allowed(path), |tmp, target| std::fs::rename(tmp, target))
}
/// The path folder `dir` has now, which `parent` named when it was opened: `parent` with its links
@@ -404,6 +408,18 @@ fn write_atomic_stepping(
fill: impl FnOnce(&mut File) -> io::Result<()>,
create: impl FnOnce(&Path) -> io::Result<File>,
rename: impl FnOnce(&Path, &Path) -> io::Result<()>,
) -> io::Result<()> {
write_atomic_staged(path, fill, create, || Ok(()), rename)
}
/// [`write_atomic_stepping`], with `check` run after the temporary file is filled and right before
/// it is put in place. An error from `check` removes the temporary file and leaves `path` unchanged.
fn write_atomic_staged(
path: &Path,
fill: impl FnOnce(&mut File) -> io::Result<()>,
create: impl FnOnce(&Path) -> io::Result<File>,
check: impl Fn() -> io::Result<()>,
rename: impl FnOnce(&Path, &Path) -> io::Result<()>,
) -> io::Result<()> {
let target = resolve_link(path)?;
let beside = temp_path(&target)?;
@@ -420,7 +436,7 @@ fn write_atomic_stepping(
};
let filled = fill(&mut f).and_then(|()| sync_file(&f)).and_then(|()| not_cancelled());
drop(f);
if let Err(e) = filled {
if let Err(e) = filled.and_then(|()| check()) {
// Best effort: the temporary file is all there is to clean up.
let _ = std::fs::remove_file(&tmp);
return Err(e);
@@ -435,7 +451,7 @@ fn write_atomic_stepping(
e
}
};
write_over(&tmp, &target, refused)
write_over(&tmp, &target, refused, &check)
}
/// The content of `tmp`, which is whole and flushed, copied into `target` itself, and `tmp`
@@ -443,8 +459,8 @@ fn write_atomic_stepping(
/// writing it is untouched: `tmp` is removed and `refused` returned. When what it holds can't be
/// copied aside first ([`keep_old`]) it is untouched too: `tmp` stays and the error names it. When
/// the copy into `target` fails it is damaged: `tmp` and the copy of the old content stay and the
/// error says where they are.
fn write_over(tmp: &Path, target: &Path, refused: io::Error) -> io::Result<()> {
/// error says where they are. `check` runs again right before `target` is overwritten.
fn write_over(tmp: &Path, target: &Path, refused: io::Error, check: &dyn Fn() -> io::Result<()>) -> io::Result<()> {
let old = tmp.with_extension("old");
let over = Over {
tmp: &|| tmp.to_path_buf(),
@@ -461,7 +477,7 @@ fn write_over(tmp: &Path, target: &Path, refused: io::Error) -> io::Result<()> {
#[cfg(unix)]
create_target: &|| create_new(target),
synced: &|| sync_written_parent(target),
still_allowed: &|| Ok(()),
still_allowed: check,
};
write_over_in(&over, refused)
}
@@ -1753,6 +1769,64 @@ mod tests {
let _ = std::fs::remove_dir_all(&d);
}
/// A check that refuses after the file is filled keeps the old file, removes the temporary file
/// and tries no rename, whether the folder takes the temporary file or not.
#[test]
fn a_check_refused_after_the_fill_keeps_the_file() {
let d = dir("staged-check");
let path = d.join("staged-check.vectorcraft");
std::fs::write(&path, b"old").unwrap();
let renamed = std::cell::Cell::new(false);
let rename = |_: &Path, _: &Path| {
renamed.set(true);
Ok(())
};
let r = write_atomic_staged(&path, |f| f.write_all(b"new"), create_new, || Err(io::Error::other("moved away")), rename);
assert_eq!(r.unwrap_err().to_string(), "moved away");
let r = write_atomic_staged(&path, |f| f.write_all(b"new"), |_| denied(), || Err(io::Error::other("moved away")), rename);
assert_eq!(r.unwrap_err().to_string(), "moved away");
assert_eq!(std::fs::read(&path).unwrap(), b"old");
assert!(!renamed.get() && leftovers(&d, "staged-check.vectorcraft").is_empty());
// A folder that refuses the rename: the check runs again before the file is written over.
let calls = std::cell::Cell::new(0);
let check = || {
calls.set(calls.get() + 1);
if calls.get() > 1 { Err(io::Error::other("moved away")) } else { Ok(()) }
};
let r = write_atomic_staged(&path, |f| f.write_all(b"new"), create_new, check, |_, _| denied());
assert!(r.is_err_and(|e| e.to_string().contains("moved away")));
assert_eq!(std::fs::read(&path).unwrap(), b"old");
assert!(leftovers(&d, "staged-check.vectorcraft").is_empty());
let staged = format!(".staged-check.vectorcraft.{}-", std::process::id());
let in_system = std::fs::read_dir(std::env::temp_dir()).unwrap().filter_map(Result::ok);
assert!(!in_system.into_iter().any(|e| e.file_name().to_string_lossy().starts_with(&staged)), "the system's folder keeps no staging file");
let _ = std::fs::remove_dir_all(&d);
}
/// Off Unix the check runs again after the file is filled: a refusal at that point keeps the
/// old file and leaves no temporary file.
#[cfg(not(unix))]
#[test]
fn a_checked_write_checks_again_before_the_file_is_put_in_place() {
let d = dir("checked-twice");
let path = d.join("doc.vectorcraft");
std::fs::write(&path, b"old").unwrap();
let calls = std::cell::Cell::new(0);
let r = write_atomic_checked(
&path,
|f| f.write_all(b"new"),
|_| {
calls.set(calls.get() + 1);
if calls.get() > 1 { Err(io::Error::other("no longer allowed")) } else { Ok(()) }
},
);
assert_eq!(r.unwrap_err().to_string(), "no longer allowed");
assert_eq!(calls.get(), 2);
assert_eq!(std::fs::read(&path).unwrap(), b"old");
assert!(leftovers(&d, "doc.vectorcraft").is_empty());
let _ = std::fs::remove_dir_all(&d);
}
#[test]
fn write_new_never_replaces_what_is_there() {
let d = dir("new");