diff --git a/crates/engine/src/script/files.rs b/crates/engine/src/script/files.rs index 2a6d2ec971..d0de6896c7 100644 --- a/crates/engine/src/script/files.rs +++ b/crates/engine/src/script/files.rs @@ -1999,16 +1999,16 @@ fn xmp_write(host: &Host, a: &[Data]) -> Result { }; let r = write_temp(); drop(out); - // The replacement keeps the sidecar's permission bits. On Windows the only permission is the - // read-only attribute, and a read-only temporary file could not be removed after a failed - // rename. - let permissions = if cfg!(unix) { std::fs::metadata(&target).map(|m| m.permissions()).ok() } else { None }; - let r = r - .and_then(|()| permissions.map_or(Ok(()), |p| std::fs::set_permissions(&temp, p))) - .and_then(|()| std::fs::rename(&temp, &target)) - .inspect_err(|_| { - let _ = std::fs::remove_file(&temp); - }); + // The replacement keeps the sidecar's permission bits, group and (Linux) access ACL, or is + // readable by its owner alone, as a saved document is (`vectorcraft_format::keep_permissions`). + // On Windows the only permission is the read-only attribute, and a read-only temporary file + // could not be removed after a failed rename. The web build has no such files. Permission + // bits that cannot be set fail the write, and the sidecar stays as it was. + #[cfg(not(target_arch = "wasm32"))] + let r = r.and_then(|()| vectorcraft_format::keep_permissions(&target, &temp)); + let r = r.and_then(|()| std::fs::rename(&temp, &target)).inspect_err(|_| { + let _ = std::fs::remove_file(&temp); + }); let changed = r.is_ok(); (r, changed) } else { diff --git a/crates/engine/src/tests/script_xmp.rs b/crates/engine/src/tests/script_xmp.rs index b39f3b28d7..40352cdd3d 100644 --- a/crates/engine/src/tests/script_xmp.rs +++ b/crates/engine/src/tests/script_xmp.rs @@ -583,6 +583,37 @@ fn a_temporary_file_starts_private() { let _ = std::fs::remove_dir_all(base); } +/// A replaced sidecar that had no access ACL takes none from its folder's default ACL, whose named +/// users the sidecar's group bits would let in. (Skipped where `setfacl` or ACLs are missing.) +#[cfg(target_os = "linux")] +#[test] +fn a_replaced_sidecar_takes_no_inherited_acl() { + let base = folder("acl"); + let mut s = with_data(&base); + let log = logged( + &mut s, + &base, + &format!("var p = Folder.userData.fsName + '/a.xmp'; var f = new File(p); f.open('w'); f.write({PACKET:?}); f.close(); vc.log(p);"), + ); + let path = PathBuf::from(log[0].as_str().unwrap()); + let set = std::process::Command::new("setfacl").args(["-d", "-m", "u:nobody:rw"]).arg(path.parent().unwrap()).status(); + if !set.is_ok_and(|s| s.success()) { + let _ = std::fs::remove_dir_all(base); + return; + } + logged( + &mut s, + &base, + "var x = new XMPFile(Folder.userData.fsName + '/a.xmp', XMPConst.FILE_UNKNOWN, XMPConst.OPEN_FOR_UPDATE);\n\ + var m = x.getXMP(); m.setProperty(XMPConst.NS_DC, 'format', 'changed'); x.putXMP(m); x.closeFile();", + ); + assert!(std::fs::read_to_string(&path).unwrap().contains("changed")); + let acl = std::process::Command::new("getfacl").args(["--omit-header", "--absolute-names"]).arg(&path).output().unwrap(); + let acl = String::from_utf8_lossy(&acl.stdout); + assert!(!acl.contains("nobody"), "the replacement takes no ACL entry the sidecar lacked: {acl}"); + let _ = std::fs::remove_dir_all(base); +} + /// A packet argument whose conversion closes the `XMPFile` leaves it closed: `canPutXMP` and /// `putXMP` throw that the file is closed, and nothing is written. #[test] diff --git a/crates/format/src/atomic.rs b/crates/format/src/atomic.rs index e8f3c42b8e..8d034ab5df 100644 --- a/crates/format/src/atomic.rs +++ b/crates/format/src/atomic.rs @@ -302,9 +302,10 @@ fn temp_path(target: &Path) -> io::Result { /// mode bits mean other people when the group differs, and an ACL can narrow who reads the file. /// When the group or the ACL can't be kept, the replacement is readable by its owner alone rather /// than by people the old file didn't let in. (Unix; Windows files have none to keep beyond -/// read-only, which makes the save fail as writing in place would.) +/// read-only, which makes the save fail as writing in place would.) `tmp` is the file that is about +/// to replace `target`. #[cfg(unix)] -fn keep_permissions(target: &Path, tmp: &Path) -> io::Result<()> { +pub fn keep_permissions(target: &Path, tmp: &Path) -> io::Result<()> { use std::os::unix::fs::{MetadataExt, PermissionsExt}; // A new file: the default permissions. let Ok(m) = std::fs::metadata(target) else { return Ok(()) }; @@ -340,8 +341,9 @@ fn keep_acl(_: &Path, _: &Path) -> bool { true } +/// Windows files have no permissions to keep beyond read-only (see the Unix version). #[cfg(not(unix))] -fn keep_permissions(_: &Path, _: &Path) -> io::Result<()> { +pub fn keep_permissions(_: &Path, _: &Path) -> io::Result<()> { Ok(()) } diff --git a/crates/format/src/lib.rs b/crates/format/src/lib.rs index 430769069d..996bad2b4a 100644 --- a/crates/format/src/lib.rs +++ b/crates/format/src/lib.rs @@ -56,7 +56,7 @@ use vectorcraft_brush::stored_art::StoredArt; use vectorcraft_doc::{Document, ImageBlob}; #[cfg(not(target_arch = "wasm32"))] -pub use atomic::{read_only_location, sync_file, write_atomic, write_atomic_with, write_new_with}; +pub use atomic::{keep_permissions, read_only_location, sync_file, write_atomic, write_atomic_with, write_new_with}; use craft_gzip::Gz; pub use craft_vectorcraft_format::base64::{decode as base64_decode, decoded_len as base64_decoded_len, encode as base64_encode}; use craft_vectorcraft_format::stream::{FileHeader, Indent, KeyScan, write_file_json}; diff --git a/docs/mcp.md b/docs/mcp.md index 11446fc177..d365ca743b 100644 --- a/docs/mcp.md +++ b/docs/mcp.md @@ -3084,7 +3084,9 @@ rules for these objects: the script may not write throws `Script file access denied`; other files open read only with that flag. After `putXMP`, `closeFile()` writes the packet in UTF-8 through a temporary file in the same folder when the script may write there, otherwise straight to the file; - when no temporary name is free, nothing is written. With `XMPConst.CLOSE_UPDATE_SAFELY`, `closeFile()` writes only + when no temporary name is free, nothing is written. On Unix the replacement keeps the file's permission bits, group + and (on Linux) access ACL, as a saved document does, and is readable by its owner alone where the group or the ACL + cannot be kept. With `XMPConst.CLOSE_UPDATE_SAFELY`, `closeFile()` writes only through a temporary file, and where the script may not make one it throws `Script file could not be written` and leaves the file as it is. A failed write throws and keeps the file open with its packet. While an open `File` of the same file holds changes it has not written, `closeFile()` throws `Script file could not be written` and the `File` keeps its changes. `getXMP`, `getPacketInfo`, `getFileInfo` and `canPutXMP` are available. An `XMPFile` is open only in the