diff --git a/crates/automation/src/workspace.rs b/crates/automation/src/workspace.rs index 52bfa7cff25..f7a437a47bd 100644 --- a/crates/automation/src/workspace.rs +++ b/crates/automation/src/workspace.rs @@ -119,7 +119,10 @@ impl AuthorizedWorkspace { // Best effort, and only when there is a file being replaced: a save still // succeeds for a document whose Finder tag could not come along. if let Ok(old) = root.dir.open(&relative) { + let old = old.into_std(); let _ = photocraft_format::atomic::meta::copy_xattrs(old.as_fd(), file.as_fd()); + // The creation time too (#9388), through the same handles. + let _ = photocraft_format::atomic::meta::copy_created_file(&old, &file); } } // Falls back to a plain fsync on shares that refuse a full flush (#1336). diff --git a/crates/automation/src/workspace/tests.rs b/crates/automation/src/workspace/tests.rs index c68c2d4b862..10063ba9e66 100644 --- a/crates/automation/src/workspace/tests.rs +++ b/crates/automation/src/workspace/tests.rs @@ -162,6 +162,22 @@ fn write_refuses_a_fifo_target_without_blocking() { assert!(temp_files(&inside).is_empty()); } +/// #9401 review: replacing a file through the automation path keeps its creation time, the +/// same as a save through `atomic_write` does on macOS (#9388). +#[cfg(target_os = "macos")] +#[test] +fn write_keeps_the_replaced_files_creation_time() { + use std::os::macos::fs::FileTimesExt; + let (inside, _, workspace) = roots("created"); + let path = inside.join("doc.psd"); + workspace.write("doc.psd", b"original").unwrap(); + let born = std::time::SystemTime::UNIX_EPOCH + std::time::Duration::from_secs(1_600_000_000); + std::fs::OpenOptions::new().write(true).open(&path).unwrap().set_times(std::fs::FileTimes::new().set_created(born)).unwrap(); + workspace.write("doc.psd", b"replacement").unwrap(); + assert_eq!(std::fs::read(&path).unwrap(), b"replacement"); + assert_eq!(std::fs::metadata(&path).unwrap().created().unwrap(), born, "the creation time survives the automation write"); +} + /// #7095: replacing a file through the automation path keeps the old file's extended /// attributes, the same as a save through `atomic_write` does. #[cfg(any(target_os = "linux", target_os = "android", target_vendor = "apple"))] diff --git a/crates/format/src/atomic/meta.rs b/crates/format/src/atomic/meta.rs index 10008faa9fc..6e1aaa15fea 100644 --- a/crates/format/src/atomic/meta.rs +++ b/crates/format/src/atomic/meta.rs @@ -12,7 +12,7 @@ //! | extended attributes (Finder tags, `user.*`) | `copy_xattrs` | `copy_xattrs` | — | //! | "downloaded from the internet" | `com.apple.quarantine`, an xattr | — | `Zone.Identifier`, a named stream kept by [`replace_existing`] | //! | access-control list | `copy_acl` (`exacl`) | not carried over (see below) | DACL, kept by [`replace_existing`] | -//! | creation time | not carried over (#6603) | — | kept by [`replace_existing`] | +//! | creation time | `copy_created` (#9388) | — | kept by [`replace_existing`] | //! //! Everything here except [`replace_existing`] is **best effort and never touches the file's //! bytes**: a save that cannot carry a piece of metadata across is still a save, because a @@ -22,7 +22,7 @@ //! //! Not carried over, and why: Linux POSIX ACLs live in `system.posix_acl_access`, which //! `listxattr` does not report, so copying the listed attributes misses them; they would need -//! their own step. macOS creation time needs `setattrlist`, which has no safe wrapper here. +//! their own step. use std::io; use std::path::Path; @@ -59,6 +59,13 @@ pub fn copy_metadata(from: &Path, to: &Path) -> io::Result<()> { #[cfg(not(unix))] let attributes = Ok(()); + // The creation time before the ACL: it needs the destination open for writing, which a + // copied ACL denying its owner write access would refuse. + #[cfg(target_os = "macos")] + let created = copy_created(from, to); + #[cfg(not(target_os = "macos"))] + let created = Ok(()); + #[cfg(target_os = "macos")] let acl = copy_acl(from, to); #[cfg(not(target_os = "macos"))] @@ -67,7 +74,35 @@ pub fn copy_metadata(from: &Path, to: &Path) -> io::Result<()> { // A save over a 0600 file must not widen it to whatever the process default is. let permissions = std::fs::metadata(from).and_then(|m| std::fs::set_permissions(to, m.permissions())); - attributes.and(acl).and(permissions) + attributes.and(acl).and(created).and(permissions) +} + +/// The creation time ("Created" in Finder) of the file being replaced, onto the file replacing +/// it: the rename leaves a new file born at the time of the save, where Photoshop keeps the +/// original's (#9388). The rename keeps the time set here. +#[cfg(target_os = "macos")] +fn copy_created(from: &Path, to: &Path) -> io::Result<()> { + let old = std::fs::File::open(from)?; + let new = std::fs::OpenOptions::new().write(true).open(to)?; + copy_created_file(&old, &new) +} + +/// [`copy_created`] through two open files rather than their paths, for the automation write +/// path's capability-checked handles (see [`copy_xattrs`]). `to` must be open for writing. +/// Nothing to do outside macOS: Windows keeps the time in [`replace_existing`], and Linux has no +/// settable creation time. +pub fn copy_created_file(from: &std::fs::File, to: &std::fs::File) -> io::Result<()> { + #[cfg(target_os = "macos")] + { + use std::os::macos::fs::FileTimesExt; + let created = from.metadata()?.created()?; + to.set_times(std::fs::FileTimes::new().set_created(created)) + } + #[cfg(not(target_os = "macos"))] + { + let _ = (from, to); + Ok(()) + } } /// [`copy_metadata`]'s extended-attribute step, by path: open both files and hand the diff --git a/crates/format/src/atomic/tests.rs b/crates/format/src/atomic/tests.rs index bc26ae6d0a8..c768044e06d 100644 --- a/crates/format/src/atomic/tests.rs +++ b/crates/format/src/atomic/tests.rs @@ -481,3 +481,45 @@ fn a_directory_flush_the_file_system_cannot_do_still_saves() { let fs = Faulty { fail_sync_dir: Some(Ok(5)), ..Default::default() }; assert!(atomic_write_with(&fs, NO_RETRY, &p, b"newer").is_err()); } + +/// #9388: on macOS a save keeps the replaced file's creation time ("Created" in Finder), as +/// Photoshop's does; the rename would otherwise leave a file born at the time of the save. +#[cfg(target_os = "macos")] +#[test] +fn keeps_the_original_creation_time() { + use std::os::macos::fs::FileTimesExt; + let d = TempDir::new("created"); + let p = d.0.join("doc.psd"); + std::fs::write(&p, ORIGINAL).unwrap(); + // A creation time well in the past, so a reset to "now" can't pass for it. + let born = std::time::SystemTime::UNIX_EPOCH + std::time::Duration::from_secs(1_600_000_000); + std::fs::OpenOptions::new().write(true).open(&p).unwrap().set_times(std::fs::FileTimes::new().set_created(born)).unwrap(); + assert_eq!(std::fs::metadata(&p).unwrap().created().unwrap(), born); + atomic_write(&p, b"new").unwrap(); + assert_eq!(std::fs::read(&p).unwrap(), b"new"); + assert_eq!(std::fs::metadata(&p).unwrap().created().unwrap(), born, "the creation time survives the save"); + assert_no_temp(&d.0); +} + +/// #9401 review: a replaced file whose ACL denies its owner writing still keeps its creation +/// time. The ACL lands on the temporary file too, so the time has to be set before it is. +#[cfg(target_os = "macos")] +#[test] +fn keeps_the_creation_time_under_a_deny_write_acl() { + use std::os::macos::fs::FileTimesExt; + let d = TempDir::new("created-acl"); + let p = d.0.join("doc.psd"); + std::fs::write(&p, ORIGINAL).unwrap(); + let born = std::time::SystemTime::UNIX_EPOCH + std::time::Duration::from_secs(1_600_000_000); + std::fs::OpenOptions::new().write(true).open(&p).unwrap().set_times(std::fs::FileTimes::new().set_created(born)).unwrap(); + let Ok(user) = std::env::var("USER") else { return }; + let deny = exacl::AclEntry::deny_user(&user, exacl::Perm::WRITE, None); + if exacl::setfacl(&[&p], &[deny], None).is_err() { + return; // a file system without ACLs: nothing to assert + } + atomic_write(&p, b"new").unwrap(); + assert_eq!(std::fs::read(&p).unwrap(), b"new"); + assert_eq!(std::fs::metadata(&p).unwrap().created().unwrap(), born, "the creation time survives a deny-write ACL"); + let _ = exacl::setfacl(&[&p], &[], None); + assert_no_temp(&d.0); +}