mirror of
https://github.com/storytold/vectorcraft.git
synced 2026-11-27 16:59:43 +01:00
Scripts: a sidecar XMPFile replaces keeps its Unix permission bits
closeFile() writes a sidecar through a new temporary file and renames it over the sidecar, so a 0600 sidecar became 0644 under a 022 umask. The temporary file now takes the sidecar's permission bits before the rename, as File.copy does for its target. Test: a_replaced_sidecar_keeps_its_permissions (Unix only) in crates/engine/src/tests/script_xmp.rs.
This commit is contained in:
1 parent
84acd52710
commit
58dab25cc6
2 files changed
+35
-3
No files matched your search
@@ -1981,9 +1981,16 @@ fn xmp_write(host: &Host, a: &[Data]) -> Result<Data, HostError> {
|
||||
};
|
||||
let r = write_temp();
|
||||
drop(out);
|
||||
let r = r.and_then(|()| std::fs::rename(&temp, &target)).inspect_err(|_| {
|
||||
let _ = std::fs::remove_file(&temp);
|
||||
});
|
||||
// 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);
|
||||
});
|
||||
let changed = r.is_ok();
|
||||
(r, changed)
|
||||
} else {
|
||||
|
||||
@@ -356,6 +356,31 @@ fn a_link_to_another_kind_of_file_is_not_written() {
|
||||
let _ = std::fs::remove_dir_all(base);
|
||||
}
|
||||
|
||||
/// A sidecar replaced through a temporary file keeps its permission bits.
|
||||
#[cfg(unix)]
|
||||
#[test]
|
||||
fn a_replaced_sidecar_keeps_its_permissions() {
|
||||
use std::os::unix::fs::PermissionsExt;
|
||||
let base = folder("mode");
|
||||
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());
|
||||
std::fs::set_permissions(&path, std::fs::Permissions::from_mode(0o600)).unwrap();
|
||||
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"));
|
||||
assert_eq!(std::fs::metadata(&path).unwrap().permissions().mode() & 0o777, 0o600);
|
||||
let _ = std::fs::remove_dir_all(base);
|
||||
}
|
||||
|
||||
/// A sidecar in UTF-16 is read by its byte order mark, and one that is not text is refused.
|
||||
#[test]
|
||||
fn a_utf16_sidecar_reads_by_its_byte_order_mark() {
|
||||
|
||||
Reference in new issue
Block a user