From 89d7d79797df84dae47301470da4e3e62b0d7d97 Mon Sep 17 00:00:00 2001 From: Hector Villarreal Date: Sun, 25 Oct 2026 04:16:44 -0600 Subject: [PATCH 1/2] fix(format): a save on macOS keeps the file's creation time (#9388) Saving writes a temporary file and renames it over the original, so on macOS the saved file was born at the time of the save and Finder's "Created" date moved forward. Photoshop keeps it. `copy_metadata` now copies the replaced file's creation time onto the temporary file before the rename, with std's `FileTimesExt::set_created` (stable since Rust 1.75). Best effort, like the other metadata steps. Co-Authored-By: Claude Opus 5.5 --- crates/format/src/atomic/meta.rs | 22 +++++++++++++++++++--- crates/format/src/atomic/tests.rs | 19 +++++++++++++++++++ 2 files changed, 38 insertions(+), 3 deletions(-) diff --git a/crates/format/src/atomic/meta.rs b/crates/format/src/atomic/meta.rs index 10008faa9fc..31363c6d49c 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; @@ -64,10 +64,26 @@ pub fn copy_metadata(from: &Path, to: &Path) -> io::Result<()> { #[cfg(not(target_os = "macos"))] let acl = Ok(()); + #[cfg(target_os = "macos")] + let created = copy_created(from, to); + #[cfg(not(target_os = "macos"))] + let created = Ok(()); + // 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<()> { + use std::os::macos::fs::FileTimesExt; + let created = std::fs::metadata(from)?.created()?; + let file = std::fs::OpenOptions::new().write(true).open(to)?; + file.set_times(std::fs::FileTimes::new().set_created(created)) } /// [`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..102d9b3547d 100644 --- a/crates/format/src/atomic/tests.rs +++ b/crates/format/src/atomic/tests.rs @@ -481,3 +481,22 @@ 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); +} From f241bba1adfb9c7ec5dd5ed9d86f95b63c7bc2c3 Mon Sep 17 00:00:00 2001 From: Hector Villarreal Date: Sun, 25 Oct 2026 04:43:52 -0600 Subject: [PATCH 2/2] Saves keep the creation time under a deny-write ACL and through automation CodeRabbit (discussion_r4370498176, two findings): - copy_metadata copied the ACL before the creation time; an ACL that denies its owner write access, copied onto the temporary file first, made the creation-time step fail to open it. The time goes first now. - AuthorizedWorkspace::write (the automation save path) copied only the extended attributes, so an automation save reset Finder's Created date. It now also copies the creation time through the same capability-checked handles, with the new descriptor-based meta::copy_created_file. Tests: keeps_the_creation_time_under_a_deny_write_acl, write_keeps_the_replaced_files_creation_time. Co-Authored-By: Claude Opus 5.5 --- crates/automation/src/workspace.rs | 19 +++++++++++++++ crates/format/src/atomic/meta.rs | 37 ++++++++++++++++++++++-------- crates/format/src/atomic/tests.rs | 23 +++++++++++++++++++ 3 files changed, 70 insertions(+), 9 deletions(-) diff --git a/crates/automation/src/workspace.rs b/crates/automation/src/workspace.rs index e8566b439ed..e3316a07913 100644 --- a/crates/automation/src/workspace.rs +++ b/crates/automation/src/workspace.rs @@ -101,7 +101,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). @@ -608,6 +611,22 @@ mod tests { 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 31363c6d49c..6e1aaa15fea 100644 --- a/crates/format/src/atomic/meta.rs +++ b/crates/format/src/atomic/meta.rs @@ -59,16 +59,18 @@ pub fn copy_metadata(from: &Path, to: &Path) -> io::Result<()> { #[cfg(not(unix))] let attributes = Ok(()); - #[cfg(target_os = "macos")] - let acl = copy_acl(from, to); - #[cfg(not(target_os = "macos"))] - let acl = 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"))] + let acl = Ok(()); + // 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())); @@ -80,10 +82,27 @@ pub fn copy_metadata(from: &Path, to: &Path) -> io::Result<()> { /// original's (#9388). The rename keeps the time set here. #[cfg(target_os = "macos")] fn copy_created(from: &Path, to: &Path) -> io::Result<()> { - use std::os::macos::fs::FileTimesExt; - let created = std::fs::metadata(from)?.created()?; - let file = std::fs::OpenOptions::new().write(true).open(to)?; - file.set_times(std::fs::FileTimes::new().set_created(created)) + 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 102d9b3547d..c768044e06d 100644 --- a/crates/format/src/atomic/tests.rs +++ b/crates/format/src/atomic/tests.rs @@ -500,3 +500,26 @@ fn keeps_the_original_creation_time() { 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); +}