LightCraft: An open-source non-destructive photo editor written by scratch in pure Rust
Originals: refuse a screenshot target whose file identity cannot be read
dotCooCoo545ed92d6a1b
diff --git a/crates/catalog/src/safe_file.rs b/crates/catalog/src/safe_file.rsindex f7737bf78..b0974961f 100644--- a/crates/catalog/src/safe_file.rs+++ b/crates/catalog/src/safe_file.rs@@ -271,30 +271,46 @@ fn same_content(path: &Path, data: &[u8]) -> io::Result<bool> {/// Do `a` and `b` name the same existing file (through hard links, symlinks, `..`,/// case-insensitive names)?pub fn same_file(a: &Path, b: &Path) -> bool {- let (Ok(ma), Ok(mb)) = (fs::metadata(a), fs::metadata(b)) else { return false };+ compare_files(a, b, false).unwrap_or(false)+}++/// [`same_file`] for a check that must not let an unknown identity pass: `Err` when both paths+/// exist with the same length but the file identity of either cannot be read (on Windows, when+/// either file cannot be opened). A path that does not exist is still `Ok(false)`.+pub fn same_file_strict(a: &Path, b: &Path) -> io::Result<bool> {+ compare_files(a, b, true)+}++fn compare_files(a: &Path, b: &Path, strict: bool) -> io::Result<bool> {+ #[cfg(not(windows))]+ let _ = strict;+ let (Ok(ma), Ok(mb)) = (fs::metadata(a), fs::metadata(b)) else { return Ok(false) };#[cfg(unix)]{use std::os::unix::fs::MetadataExt;- ma.dev() == mb.dev() && ma.ino() == mb.ino()+ Ok(ma.dev() == mb.dev() && ma.ino() == mb.ino())}#[cfg(not(unix))]{if ma.len() != mb.len() {- return false;+ return Ok(false);}// A hard link has its own path but shares the volume serial number and file index with// its original. A volume that does not report them is compared by resolved path below.// On ReFS two files can share the 64-bit file index, so the lengths are compared first.#[cfg(windows)]{- if ::same_file::is_same_file(a, b).unwrap_or(false) {- return true;+ match ::same_file::is_same_file(a, b) {+ Ok(true) => return Ok(true),+ Ok(false) => {}+ Err(e) if strict => return Err(e),+ Err(_) => {}}}- match (fs::canonicalize(a), fs::canonicalize(b)) {+ Ok(match (fs::canonicalize(a), fs::canonicalize(b)) {(Ok(x), Ok(y)) => x == y || x.to_string_lossy().to_lowercase() == y.to_string_lossy().to_lowercase(),_ => false,- }+ })}}diff --git a/crates/engine/src/originals.rs b/crates/engine/src/originals.rsindex 890e98f81..e20a9378b 100644--- a/crates/engine/src/originals.rs+++ b/crates/engine/src/originals.rs@@ -9,7 +9,7 @@ use std::path::{Path, PathBuf};#[cfg(not(any(unix, windows)))]use std::time::SystemTime;-use lightcraft_catalog::safe_file::same_file;+use lightcraft_catalog::safe_file::{same_file, same_file_strict};use lightcraft_catalog::{Catalog, Source};/// The directory entry a screenshot is about to write, pinned at request admission.@@ -147,14 +147,30 @@ impl OriginalGuard {if let Some(n) = std::fs::canonicalize(target).ok().and_then(|c| c.file_name().map(|n| n.to_string_lossy().to_lowercase())) {names.push(n);}+ // The identity check refuses a target whose file identity cannot be read, so an unreadable+ // hard link to an original is not taken for an ordinary file.+ let refuse_if_same = |p: &Path, what: &str| -> Result<(), String> {+ let same = if hard_links {+ same_file_strict(p, target).map_err(|e| {+ format!(+ "{}: LightCraft cannot tell whether this is {what} in the library ({e}) and never writes over an original (choose another folder or file name)",+ target.display()+ )+ })?+ } else {+ same_file(p, target)+ };+ if same {+ return Err(format!(+ "{} is {what} in the library: LightCraft never writes over an original (choose another folder or file name)",+ target.display()+ ));+ }+ Ok(())+ };for n in &names {for (p, what) in self.by_name.get(n).into_iter().flatten() {- if same_file(p, target) {- return Err(format!(- "{} is {what} in the library: LightCraft never writes over an original (choose another folder or file name)",- target.display()- ));- }+ refuse_if_same(p, what)?;}}// An imported hard link or symlink can have a different name from its original. A write@@ -166,12 +182,7 @@ impl OriginalGuard {if !hard_links && std::fs::read_link(p).is_err() {continue;}- if same_file(p, target) {- return Err(format!(- "{} is {what} in the library: LightCraft never writes over an original (choose another folder or file name)",- target.display()- ));- }+ refuse_if_same(p, what)?;}Ok(())}@@ -339,4 +350,30 @@ mod tests {}let _ = std::fs::remove_dir_all(&d);}++ #[cfg(windows)]+ #[test]+ fn a_hard_link_whose_identity_cannot_be_read_is_refused() {+ use std::os::windows::fs::OpenOptionsExt;+ let d = std::env::temp_dir().join(format!("lc-unreadable-link-{}", 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();+ let linked = d.join("shot.png");+ std::fs::hard_link(&original, &linked).unwrap();++ // A handle that shares nothing makes every other open of the file fail, so the file+ // identity of either name cannot be read while it is held.+ let held = std::fs::OpenOptions::new().read(true).share_mode(0).open(&original).unwrap();+ let refused = s.pin_write_target(&linked.to_string_lossy()).unwrap_err();+ assert!(refused.contains("original"), "{refused}");+ drop(held);++ assert!(s.pin_write_target(&linked.to_string_lossy()).unwrap_err().contains("original"));+ let _ = std::fs::remove_dir_all(&d);+ }}