From af09fc206449a8d49400e0efeb9f9a775bcd5e7d Mon Sep 17 00:00:00 2001 From: dotCooCoo Date: Tue, 17 Nov 2026 07:14:42 -0800 Subject: [PATCH] Scripts: open files are written in the order they were opened When two File objects held changes to the same file, copy, length, a new open of the file and a withdrawn grant wrote them in no fixed order, so the copy or the file could end up with the older object's bytes. They now write them in the order the objects were opened, as the end of the run does. --- crates/engine/src/script/files.rs | 21 +++++++---- crates/engine/src/tests/script_io.rs | 54 ++++++++++++++++++++++++++++ 2 files changed, 68 insertions(+), 7 deletions(-) diff --git a/crates/engine/src/script/files.rs b/crates/engine/src/script/files.rs index 05f148e714..6eecfeabf8 100644 --- a/crates/engine/src/script/files.rs +++ b/crates/engine/src/script/files.rs @@ -407,8 +407,10 @@ impl Disk { self.writes.remove(&g.path, g.folder); } } - let ids: Vec = + let mut ids: Vec = self.open.iter().filter(|(_, o)| o.target.as_deref().is_some_and(|t| !self.writes.allows(t))).map(|(id, _)| *id).collect(); + // In the order the files were opened, as `flush_all` writes them. + ids.sort_unstable(); for id in ids { let written = self.flush(id).is_ok(); if let Some(o) = self.open.get_mut(&id) { @@ -701,8 +703,9 @@ impl Disk { return fail(NOT_FOUND); } // Changes other open objects hold for the file are written before it is opened again, so - // they do not later replace what this one writes. - let ids: Vec = self.open.iter().filter(|(_, o)| o.target.as_deref() == Some(real.as_path())).map(|(id, _)| *id).collect(); + // they do not later replace what this one writes, in the order they were opened. + let mut ids: Vec = self.open.iter().filter(|(_, o)| o.target.as_deref() == Some(real.as_path())).map(|(id, _)| *id).collect(); + ids.sort_unstable(); if let Err(m) = ids.iter().try_for_each(|id| self.flush(*id)) { return fail(m); } @@ -1795,8 +1798,10 @@ fn copy(host: &Host, a: &[Data]) -> Result { // its open objects are written, and copying could empty it. let one = |t: &std::fs::Metadata| std::fs::metadata(&source).is_ok_and(|s| same_file((&source, &s), (&target, t))); let itself = real_of(&target).is_some_and(|t| t == source) || std::fs::metadata(&target).is_ok_and(|t| one(&t)); - // The source's open objects write what they hold first, so the copy has it. - let ids: Vec = disk.open.iter().filter(|(_, o)| o.target.as_deref() == Some(source.as_path())).map(|(id, _)| *id).collect(); + // The source's open objects write what they hold first, in the order they were opened, so the + // copy has it. + let mut ids: Vec = disk.open.iter().filter(|(_, o)| o.target.as_deref() == Some(source.as_path())).map(|(id, _)| *id).collect(); + ids.sort_unstable(); if let Err(m) = ids.iter().try_for_each(|id| disk.flush(*id)) { return Ok(error(m)); } @@ -1988,8 +1993,10 @@ fn set_length(host: &Host, a: &[Data]) -> Result { let len = usize::try_from(n).unwrap_or(usize::MAX); 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(); + // The file's open objects write what they hold first, in the order they were opened, and + // then hold the new length. + let mut ids: Vec = disk.open.iter().filter(|(_, o)| o.target.as_deref() == Some(target.as_path())).map(|(id, _)| *id).collect(); + ids.sort_unstable(); ids.iter() .try_for_each(|id| disk.flush(*id)) .and_then(|()| std::fs::OpenOptions::new().write(true).open(&target).and_then(|f| f.set_len(n)).map_err(|_| WRITE_FAILED)) diff --git a/crates/engine/src/tests/script_io.rs b/crates/engine/src/tests/script_io.rs index eca6dd5638..7c8c167527 100644 --- a/crates/engine/src/tests/script_io.rs +++ b/crates/engine/src/tests/script_io.rs @@ -908,6 +908,29 @@ fn script_file_copy_of_an_open_file_has_its_changes() { let _ = std::fs::remove_dir_all(base); } +/// Two objects that changed the same file write it in the order they were opened, so the one opened +/// last is written last, before the file is copied, given a new length or opened again. +#[test] +fn script_files_changed_by_two_objects_are_written_in_the_order_they_were_opened() { + let base = folder("twowriters"); + let r = run( + &base, + "var d = Folder.temp.fsName, out = [];\n\ + function two(name) { var p = d + '/' + name, s = new File(p); s.open('w'); s.write('xx'); s.close();\n\ + var a = new File(p), b = new File(p); a.open('e'); b.open('e'); a.write('A'); b.write('B'); return [a, b, p]; }\n\ + function done(h) { h[0].close(); h[1].close(); }\n\ + function text(p) { var f = new File(p); f.open('r'); var t = f.read(); f.close(); return t; }\n\ + for (var i = 0; i < 24; i++) {\n\ + var h = two('c' + i + '.txt'); h[0].copy(d + '/copy.txt'); out.push(text(d + '/copy.txt')); done(h);\n\ + h = two('l' + i + '.txt'); new File(h[2]).length = 1; done(h); out.push(text(h[2]));\n\ + h = two('o' + i + '.txt'); var c = new File(h[2]); c.open('e'); out.push(c.read()); c.close(); done(h);\n\ + }\n\ + vc.log(out.join(','));", + ); + assert_eq!(r["log"], json!([vec!["Bx,B,Bx"; 24].join(",")]), "{r}"); + 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. @@ -1995,6 +2018,37 @@ fn script_files_open_under_a_withdrawn_grant_keep_their_unwritten_changes() { let _ = std::fs::remove_dir_all(base); } +/// Files opened for writing under a kept grant that new code withdraws are written in the order +/// they were opened, so the object opened last is written last. +#[test] +fn script_files_open_under_a_withdrawn_grant_are_written_in_the_order_they_were_opened() { + let base = folder("grantwithdrawnorder"); + let dir = base.join("picked"); + std::fs::create_dir_all(&dir).unwrap(); + let out = dir.join("out.txt"); + let text = format!( + "var f = new File({:?});\n\ + if (!f.open('w')) vc.log(['asked', !!Folder.selectDialog('p')]);\n\ + else {{ f.write('x'); var all = [f];\n\ + for (var i = 1; i < 64; i++) {{ var g = new File(f.fsName); g.open('e'); all.push(g); }}\n\ + for (var i = 0; i < 64; i++) all[i].write(('0' + i).slice(-2));\n\ + eval('1'); vc.log(['wrote']); }}", + out.to_string_lossy() + ); + let mut s = with_data(&base); + start_framed(&mut s, &base, &text); + assert!(pump_to_picker(&mut s).is_none()); + assert!(s.script_pick(crate::script::files::PickAnswer::Allow { paths: vec![dir.to_string_lossy().into_owned()], always: true })); + assert_eq!(pump_to_picker(&mut s).unwrap().unwrap()["log"], json!([["asked", true]])); + start_framed(&mut s, &base, &text); + let r = pump_to_picker(&mut s).unwrap(); + let report = s.script_report().unwrap(); + assert!(r.is_ok(), "{report}"); + assert_eq!(report["log"], json!([["wrote"]]), "{report}"); + assert_eq!(std::fs::read(&out).unwrap(), b"63", "{report}"); + let _ = std::fs::remove_dir_all(base); +} + /// A file opened for reading under a kept grant that new code withdraws gives no more of what it /// holds. #[test]