diff --git a/crates/engine/src/script/files.rs b/crates/engine/src/script/files.rs index 6340912cb8..ba1bc60827 100644 --- a/crates/engine/src/script/files.rs +++ b/crates/engine/src/script/files.rs @@ -431,6 +431,7 @@ impl Disk { } let (Some((script, hash)), Some(store)) = (&self.script, self.store_path()) else { return }; let (script, code) = (shown(script), self.ran.iter().cloned().collect::>()); + let Ok(_lock) = lock_store(&store) else { return }; let Ok(mut grants) = try_read_store(&store) else { return }; for g in &mut grants { let ours = g["script"] == *script @@ -566,6 +567,7 @@ impl Disk { fn keep(&self, paths: &[std::path::PathBuf], write: bool, folder: bool) -> Result<(), &'static str> { let (Some((script, hash)), Some(store)) = (&self.script, self.store_path()) else { return Err(NO_ALWAYS) }; // A grants file that is there but cannot be read is left as it is. + let _lock = lock_store(&store).map_err(|_| NOT_SAVED)?; let mut grants = try_read_store(&store).map_err(|_| NOT_SAVED)?; let script = shown(script); let code: Vec<&String> = self.ran.iter().collect(); @@ -882,8 +884,10 @@ fn read_head(real: &Path, max: usize) -> std::io::Result> { } /// The grants the grants file at `store` holds; none when it is missing or cannot be read. A grant -/// that is not well formed is left out. +/// that is not well formed is left out. It is read under [`lock_store`]'s lock when that can be +/// taken, so it is read between two changes. fn read_store(store: &Path) -> Vec { + let _lock = lock_store(store).ok(); load_store(store, false).unwrap_or_default() } @@ -928,8 +932,13 @@ fn load_store(store: &Path, strict: bool) -> std::io::Result) -> std::io::Result<()> { let text = serde_json::to_vec_pretty(&serde_json::json!({"version": 1, "grants": grants})).map_err(std::io::Error::other)?; if text.len() > MAX_STORE_BYTES { @@ -938,14 +947,49 @@ fn write_store(store: &Path, grants: Vec) -> std::io::Result< if let Some(dir) = store.parent() { std::fs::create_dir_all(dir)?; } - let part = store.with_extension("json.part"); - std::fs::write(&part, text)?; + // A name an earlier write left behind is passed over, up to 64 times, and only the file made here + // is removed. + let mut tries = 0; + let (part, mut file) = loop { + let n = NEXT_PART.fetch_add(1, Ordering::Relaxed); + let part = store.with_extension(format!("json.{}-{n}.part", std::process::id())); + match std::fs::File::options().write(true).create_new(true).open(&part) { + Ok(file) => break (part, file), + Err(e) if e.kind() == std::io::ErrorKind::AlreadyExists && tries < 63 => tries += 1, + Err(e) => return Err(e), + } + }; + let written = std::io::Write::write_all(&mut file, &text); + drop(file); // `rename` replaces a grants file that is there, on Windows too (`MOVEFILE_REPLACE_EXISTING`). - std::fs::rename(&part, store).inspect_err(|_| { + written.and_then(|()| std::fs::rename(&part, store)).inspect_err(|_| { let _ = std::fs::remove_file(&part); }) } +/// The lock file beside the grants file at `store` (`Script Grants.lock`), locked until it is +/// dropped and waiting while another process holds it, so that processes that read and change the +/// grants file take turns. A change is not made when the lock cannot be taken. +#[cfg(not(target_arch = "wasm32"))] +fn lock_store(store: &Path) -> std::io::Result { + let lock = store.with_extension("lock"); + let opened = store + .parent() + .map_or(Ok(()), std::fs::create_dir_all) + .and_then(|()| std::fs::File::options().create(true).truncate(false).read(true).write(true).open(&lock)); + let file = opened.and_then(|f| f.lock().map(|()| f)); + if let Err(e) = &file { + log::warn!("can't lock {}: {e}", lock.display()); + } + file +} + +/// The browser has no folders to keep anything in. +#[cfg(target_arch = "wasm32")] +fn lock_store(_: &Path) -> std::io::Result { + Err(std::io::Error::new(std::io::ErrorKind::Unsupported, "no file system")) +} + /// The grants the grants file beside the data folder `data_dir` keeps: each script, the SHA-256 of /// its text, the path and whether it may be written (`script.grants`). pub(crate) fn stored_grants(data_dir: &Path) -> Vec { @@ -957,6 +1001,7 @@ pub(crate) fn stored_grants(data_dir: &Path) -> Vec { /// → how many were removed. pub(crate) fn revoke_grants(data_dir: &Path, script: Option<&str>, path: Option<&str>) -> std::io::Result { let store = data_dir.with_file_name(GRANTS_FILE); + let _lock = lock_store(&store)?; let grants = try_read_store(&store)?; let before = grants.len(); let kept: Vec = diff --git a/crates/engine/src/tests/script_io.rs b/crates/engine/src/tests/script_io.rs index 2f2d7fe6f5..9b9cee9439 100644 --- a/crates/engine/src/tests/script_io.rs +++ b/crates/engine/src/tests/script_io.rs @@ -576,6 +576,43 @@ fn a_copy_of_a_link_that_leads_elsewhere_after_it_was_looked_up_copies_nothing() let _ = std::fs::remove_dir_all(base); } +/// The grants file is written through a temporary file of its own, so a temporary file another +/// process left beside it, or one an earlier write left under the name this write takes first, +/// does not stop a change and is left where it is. +#[test] +fn a_grants_change_does_not_use_another_writers_temporary_file() { + let base = folder("grants-part"); + let data = base.join("settings/Script Data"); + let grant = json!({"script": "/s.jsx", "sha256": "0", "code": [], "path": "/p", "write": false, "folder": false}); + write(&base.join("settings/Script Grants.json"), json!({"version": 1, "grants": [grant]}).to_string().as_bytes()); + std::fs::create_dir_all(base.join("settings/Script Grants.json.part/held")).unwrap(); + let next = crate::script::files::NEXT_PART.load(std::sync::atomic::Ordering::Relaxed); + let left_behind: Vec = + (next..next + 8).map(|n| base.join(format!("settings/Script Grants.json.{}-{n}.part", std::process::id()))).collect(); + for p in &left_behind { + write(p, b"earlier"); + } + assert_eq!(crate::script::files::revoke_grants(&data, None, None).unwrap(), 1); + let left: Value = serde_json::from_slice(&std::fs::read(base.join("settings/Script Grants.json")).unwrap()).unwrap(); + assert_eq!(left["grants"], json!([])); + assert!(left_behind.iter().all(|p| std::fs::read(p).is_ok_and(|b| b == b"earlier")), "files this write did not make stay"); + let _ = std::fs::remove_dir_all(base); +} + +/// A change to the grants file is not made when its lock cannot be taken. +#[test] +fn a_grants_change_without_its_lock_changes_nothing() { + let base = folder("grants-lock"); + let data = base.join("settings/Script Data"); + let grant = json!({"script": "/s.jsx", "sha256": "0", "code": [], "path": "/p", "write": false, "folder": false}); + let text = json!({"version": 1, "grants": [grant]}).to_string(); + write(&base.join("settings/Script Grants.json"), text.as_bytes()); + std::fs::create_dir_all(base.join("settings/Script Grants.lock/held")).unwrap(); + assert!(crate::script::files::revoke_grants(&data, None, None).is_err()); + assert_eq!(std::fs::read_to_string(base.join("settings/Script Grants.json")).unwrap(), text); + let _ = std::fs::remove_dir_all(base); +} + /// `rename` of a file an earlier run wrote names both its old and its new path in `written`. #[test] fn a_renamed_file_names_its_old_and_new_paths_in_written() {