mirror of
https://github.com/storytold/vectorcraft.git
synced 2026-11-18 07:49:26 +00:00
Scripts: an open file that is renamed is read under its new path's rules
`rename` moved the write path of open File objects but left the path they were read from at the old name. When new code then withdrew the kept grant for the folder, a grant the run still held for the old path let the object go on giving the file's bytes. The read path now moves with the write path.
This commit is contained in:
@@ -1844,11 +1844,16 @@ fn rename(host: &Host, a: &[Data]) -> Result<Data, HostError> {
|
||||
if std::fs::rename(&from, &to).is_err() {
|
||||
return Ok(error(WRITE_FAILED));
|
||||
}
|
||||
// Open objects of the file, or of files in the folder, write to the new path.
|
||||
for file in disk.open.values_mut() {
|
||||
if let Some(rest) = file.target.as_deref().and_then(|t| t.strip_prefix(&from).ok()) {
|
||||
file.target = Some(if rest.as_os_str().is_empty() { to.clone() } else { to.join(rest) });
|
||||
// Open objects of the file, or of files in the folder, write to the new path, and the rules for
|
||||
// reading apply to them there.
|
||||
let moved = |p: &mut Option<std::path::PathBuf>| {
|
||||
if let Some(rest) = p.as_deref().and_then(|t| t.strip_prefix(&from).ok()) {
|
||||
*p = Some(if rest.as_os_str().is_empty() { to.clone() } else { to.join(rest) });
|
||||
}
|
||||
};
|
||||
for file in disk.open.values_mut() {
|
||||
moved(&mut file.target);
|
||||
moved(&mut file.read_from);
|
||||
}
|
||||
// The old path no longer holds the file, so the report names it as well as the new one.
|
||||
disk.wrote(&from);
|
||||
|
||||
@@ -1913,6 +1913,35 @@ fn script_files_open_under_a_withdrawn_grant_are_not_read() {
|
||||
let _ = std::fs::remove_dir_all(base);
|
||||
}
|
||||
|
||||
/// A file opened for reading and then renamed is read under the rules for its new path: when new code
|
||||
/// withdraws the kept grant for its folder, a grant the run still holds for its old path does not let
|
||||
/// the object give more of it.
|
||||
#[test]
|
||||
fn script_files_renamed_while_open_are_read_under_their_new_path() {
|
||||
let base = folder("grantwithdrawnrenamed");
|
||||
let dir = base.join("picked");
|
||||
let file = dir.join("in.txt");
|
||||
write(&file, b"secret");
|
||||
let text = format!(
|
||||
"var f = new File({:?});\n\
|
||||
if (!f.open('r')) vc.log(['asked', !!Folder.selectDialog('p')]);\n\
|
||||
else {{ var a = f.readch(); File.openDialog('p'); var n = new File(f.fsName).rename('r.txt'); eval('1');\n\
|
||||
var r = f.read(), e = f.error; vc.log([a, n, r, e]); }}",
|
||||
file.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);
|
||||
assert!(pump_to_picker(&mut s).is_none());
|
||||
assert!(s.script_pick(crate::script::files::PickAnswer::Allow { paths: vec![file.to_string_lossy().into_owned()], always: false }));
|
||||
let r = pump_to_picker(&mut s).unwrap().unwrap();
|
||||
assert_eq!(r["log"], json!([["s", true, "", DENIED]]));
|
||||
let _ = std::fs::remove_dir_all(base);
|
||||
}
|
||||
|
||||
/// The folder that holds the settings is kept out of every grant: a pick there gives `null` with the
|
||||
/// error, a grant written into the grants file for it is not taken, and the script cannot write the
|
||||
/// grants file through its data folder.
|
||||
|
||||
Reference in New Issue
Block a user