LightCraft: An open-source non-destructive photo editor written by scratch in pure Rust
Merge remote-tracking branch 'origin/main' into fix/guard-new-sidecar
Michael Anderson18a19325c113
diff --cc crates/engine/src/originals.rsindex a66c4ec3f,890e98f81..f16d57c71--- a/crates/engine/src/originals.rs+++ b/crates/engine/src/originals.rs@@@ -42,11 -127,21 +127,22 @@@ impl OriginalGuardput(format!("{name}.xmp"), PathBuf::from(format!("{}.xmp", path.to_string_lossy())), format!("the XMP sidecar of {label}"));}- /// `Err` (a message for the user) when writing `target` would replace a protected file.+ /// `Err` (a message for the user) when writing `target` would replace a protected file, or+ /// create a protected sidecar that isn't there yet.pub fn check(&self, target: &Path) -> Result<(), String> {+ self.check_target(target, false)+ }++ /// [`Self::check`] plus hard links: a screenshot target must not replace an original reached+ /// through a link with a different name. The per-entry identity scan is limited to this+ /// long-lived, user-visible destination check, not every ordinary export overwrite.+ pub fn check_identity(&self, target: &Path) -> Result<(), String> {+ self.check_target(target, true)+ }++ fn check_target(&self, target: &Path, hard_links: bool) -> Result<(), String> {if std::fs::symlink_metadata(target).is_err() {- return Ok(()); // nothing there to replace+ return self.check_new(target); // nothing there to replace}let mut names: Vec<String> = target.file_name().map(|n| n.to_string_lossy().to_lowercase()).into_iter().collect();if let Some(n) = std::fs::canonicalize(target).ok().and_then(|c| c.file_name().map(|n| n.to_string_lossy().to_lowercase())) {@@@ -147,23 -283,60 +309,80 @@@ mod testslet _ = std::fs::remove_dir_all(&d);}+ #[test]+ fn a_sidecar_that_isnt_there_yet_is_protected_too() {+ let d = std::env::temp_dir().join(format!("lc-originals-new-sidecar-{}", std::process::id()));+ let _ = std::fs::remove_dir_all(&d);+ std::fs::create_dir_all(d.join("sub")).unwrap();+ let orig = d.join("IMG_1.jpg");+ std::fs::write(&orig, b"original").unwrap();+ let mut c = Catalog::default();+ let p = Photo::new(PhotoId(1), Source::File { path: orig.to_string_lossy().to_string() }, "IMG_1.jpg", "JPEG", 10, 10, "2026-01-01");+ c.apply(Op::AddPhoto { photo: Box::new(p) }).unwrap();+ let g = OriginalGuard::new(&c);+ assert!(g.check(&d.join("IMG_1.xmp")).unwrap_err().contains("XMP sidecar of IMG_1.jpg"));+ assert!(g.check(&d.join("IMG_1.jpg.xmp")).is_err(), "the appended spelling");+ assert!(g.check(&d.join("sub/../IMG_1.xmp")).is_err(), "another spelling of the folder");+ assert!(g.check(&d.join("sub/IMG_1.xmp")).is_ok(), "the same name in another folder");+ assert!(g.check(&d.join("other.xmp")).is_ok(), "no photo of that name");+ assert!(g.check(&d.join("missing/IMG_1.xmp")).is_ok(), "a folder that isn't there");+ let _ = std::fs::remove_dir_all(&d);+ }+++ #[test]+ fn write_targets_pin_absent_paths_and_recheck_the_current_library() {+ let d = std::env::temp_dir().join(format!("lc-write-targets-{}", std::process::id()));+ let _ = std::fs::remove_dir_all(&d);+ std::fs::create_dir_all(&d).unwrap();+ let mut s = crate::Session::new();+ let original = d.join("IMG_1.jpg");+ std::fs::write(&original, b"original").unwrap();+ let photo = Photo::new(PhotoId(1), Source::File { path: original.to_string_lossy().to_string() }, "IMG_1.jpg", "JPEG", 10, 10, "2026-01-01");+ s.catalog.apply(Op::AddPhoto { photo: Box::new(photo) }).unwrap();++ // A path that was absent at admission is refused when anything appears there.+ let free = d.join("new-shot.png");+ let absent = s.pin_write_target(&free.to_string_lossy()).expect("free path");+ assert!(absent.was_absent());+ std::fs::write(&free, b"appeared").unwrap();+ assert!(s.revalidate_write_target(&absent).unwrap_err().contains("appeared"));+ std::fs::remove_file(&free).unwrap();++ // An ordinary existing path can still be overwritten in place (same entry).+ let allowed = d.join("old-shot.png");+ std::fs::write(&allowed, b"old").unwrap();+ let existing = s.pin_write_target(&allowed.to_string_lossy()).expect("ordinary file");+ assert!(!existing.was_absent());+ std::fs::write(&allowed, b"new").unwrap();+ assert!(s.revalidate_write_target(&existing).is_ok(), "an allowed screenshot refresh keeps working");++ // A hard link to an original is refused at admission, and an absent path that becomes+ // such a link is refused at write time.+ let linked = d.join("linked.png");+ std::fs::hard_link(&original, &linked).unwrap();+ assert!(s.pin_write_target(&linked.to_string_lossy()).unwrap_err().contains("original"));+ std::fs::remove_file(&linked).unwrap();+ let appears_as_link = d.join("late-link.png");+ let late = s.pin_write_target(&appears_as_link.to_string_lossy()).expect("free path");+ std::fs::hard_link(&original, &appears_as_link).unwrap();+ assert!(s.revalidate_write_target(&late).unwrap_err().contains("appeared"));++ // A path pinned as an ordinary file becomes an original through a catalog import while+ // the capture is queued: the current guard refuses the write.+ let imported = d.join("imported.png");+ std::fs::write(&imported, b"not yet imported").unwrap();+ let queued = s.pin_write_target(&imported.to_string_lossy()).expect("ordinary file");+ let p = Photo::new(PhotoId(2), Source::File { path: imported.to_string_lossy().to_string() }, "imported.png", "PNG", 10, 10, "2026-01-01");+ s.catalog.apply(Op::AddPhoto { photo: Box::new(p) }).unwrap();+ assert!(s.revalidate_write_target(&queued).unwrap_err().contains("original"));++ #[cfg(unix)]+ {+ use std::os::unix::fs::symlink;+ let link = d.join("link-shot.png");+ symlink(&original, &link).unwrap();+ assert!(s.pin_write_target(&link.to_string_lossy()).unwrap_err().contains("original"));+ }+ let _ = std::fs::remove_dir_all(&d);+ }}