PhotoCraft: An open-source, photo editor written by scratch in pure Rust
Merge storytold/main into save-replace-metadata
skylar taylor-barrick915779b9481d
diff --cc crates/format/src/atomic.rsindex e635b8825,8a02ce5c7..3a4aeca6c--- a/crates/format/src/atomic.rs+++ b/crates/format/src/atomic.rs@@@ -101,12 -97,13 +111,15 @@@ impl AtomicFs for RealFsOk(())}}- fn copy_permissions(&self, from: &Path, to: &Path) -> io::Result<()> {- let perms = std::fs::metadata(from)?.permissions();- std::fs::set_permissions(to, perms)+ fn copy_metadata(&self, from: &Path, to: &Path) -> io::Result<()> {+ meta::copy_metadata(from, to)+ }+ fn replace_existing(&self, from: &Path, to: &Path) -> io::Result<()> {+ meta::replace_existing(from, to)}+ fn is_readonly(&self, path: &Path) -> bool {+ std::fs::metadata(path).is_ok_and(|m| m.is_file() && m.permissions().readonly())+ }}/// Flush `file` to stable storage (`sync_all`). On Apple platforms that is a full flushdiff --cc crates/format/src/atomic/tests.rsindex 8bf89512c,ce3ed1b7c..d4381705a--- a/crates/format/src/atomic/tests.rs+++ b/crates/format/src/atomic/tests.rs@@@ -176,6 -176,81 +176,81 @@@ fn lock_that_outlasts_the_retries_keepsassert_no_temp(&d.0);}+ /// A file system that reports the target as read-only and records every write and rename.+ #[derive(Default)]+ struct ReadOnlyTarget {+ writes: Cell<u32>,+ renames: Cell<u32>,+ }++ impl AtomicFs for ReadOnlyTarget {+ fn is_readonly(&self, _path: &Path) -> bool {+ true+ }+ fn write_new(&self, tmp: &Path, bytes: &[u8]) -> io::Result<()> {+ self.writes.set(self.writes.get() + 1);+ RealFs.write_new(tmp, bytes)+ }+ fn rename(&self, from: &Path, to: &Path) -> io::Result<()> {+ self.renames.set(self.renames.get() + 1);+ RealFs.rename(from, to)+ }+ fn remove(&self, path: &Path) -> io::Result<()> {+ RealFs.remove(path)+ }+ fn sync_dir(&self, dir: &Path) -> io::Result<()> {+ RealFs.sync_dir(dir)+ }- fn copy_permissions(&self, from: &Path, to: &Path) -> io::Result<()> {- RealFs.copy_permissions(from, to)++ fn copy_metadata(&self, from: &Path, to: &Path) -> io::Result<()> {++ RealFs.copy_metadata(from, to)+ }+ }++ /// #6600: a read-only destination is refused before anything is written, with an error that says+ /// so, instead of being replaced by the rename (which only needs the folder to be writable).+ #[test]+ fn read_only_target_is_refused_before_anything_is_written() {+ let d = TempDir::new("rofake");+ let p = d.0.join("a.psd");+ std::fs::write(&p, ORIGINAL).unwrap();+ let fs = ReadOnlyTarget::default();+ let e = atomic_write_with(&fs, NO_RETRY, &p, b"new").unwrap_err();+ assert_eq!(e.kind(), io::ErrorKind::PermissionDenied);+ assert!(e.to_string().contains("a.psd") && e.to_string().contains("read-only") && e.to_string().contains("not changed"), "{e}");+ assert_eq!((fs.writes.get(), fs.renames.get()), (0, 0), "nothing is written or renamed");+ assert_eq!(std::fs::read(&p).unwrap(), ORIGINAL);+ assert_no_temp(&d.0);+ }++ /// #6600 on the real file system: a file marked read-only (`chmod 444`, or the Windows read-only+ /// attribute) keeps its bytes and the save says why; a writable one still saves.+ #[test]+ fn read_only_file_on_disk_is_never_replaced() {+ let d = TempDir::new("rofile");+ let p = d.0.join("a.psd");+ std::fs::write(&p, ORIGINAL).unwrap();+ let mut perms = std::fs::metadata(&p).unwrap().permissions();+ perms.set_readonly(true);+ std::fs::set_permissions(&p, perms.clone()).unwrap();+ let r = atomic_write(&p, b"new");+ let unchanged = std::fs::read(&p).unwrap();+ let still_readonly = std::fs::metadata(&p).unwrap().permissions().readonly();+ // Make it writable again so the folder can be removed (Windows refuses to delete it otherwise).+ #[allow(clippy::permissions_set_readonly_false)]+ perms.set_readonly(false);+ std::fs::set_permissions(&p, perms).unwrap();+ let e = r.unwrap_err();+ assert_eq!(e.kind(), io::ErrorKind::PermissionDenied);+ assert!(e.to_string().contains("the file is read-only;"), "a clear reason, not a failed rename: {e}");+ assert_eq!(unchanged, ORIGINAL);+ assert!(still_readonly);+ assert_no_temp(&d.0);+ // New files and writable files are unaffected.+ atomic_write(&p, b"new").unwrap();+ assert_eq!(std::fs::read(&p).unwrap(), b"new");+ atomic_write(&d.0.join("fresh.psd"), b"x").unwrap();+ }+#[cfg(unix)]#[test]fn read_only_folder_errors_clearly_and_never_writes_in_place() {