Skip to content

LightCraft: An open-source non-destructive photo editor written by scratch in pure Rust

Do not read a sibling's shared stem sidecar for a non-owner photo (#1523)

MarekSpodBiedrye252fe94a19a
diff --git a/crates/engine/src/cmd/query.rs b/crates/engine/src/cmd/query.rs
index dfbf4bfc5..c5caf9ae4 100644
--- a/crates/engine/src/cmd/query.rs
+++ b/crates/engine/src/cmd/query.rs
@@ -140,7 +140,7 @@ pub fn specs() -> Vec<CommandSpec> {
.map(|r| json!({"group": r.group, "tag": r.tag, "name": r.name, "value": r.value}))
.collect();
// Keep sidecar precedence; reuse the loaded bytes for embedded XMP.
- let packet = match crate::sidecar::find_sidecar(path, s.sidecar_naming(ph.id)) {
+ let packet = match s.sidecar_lookup(ph.id).find(path) {
Some(sc) => std::fs::read_to_string(sc).ok(),
None => lightcraft_meta::embedded(&bytes).xmp,
};
diff --git a/crates/engine/src/sidecar.rs b/crates/engine/src/sidecar.rs
index d13b95125..56fb486dd 100644
--- a/crates/engine/src/sidecar.rs
+++ b/crates/engine/src/sidecar.rs
@@ -76,16 +76,37 @@ pub fn find_sidecar(original: &str, naming: SidecarNaming) -> Option<PathBuf> {
sidecar_candidates(original, naming).find(|p| p.is_file())
}
+/// Where to look for a catalogued photo's sidecar: how the library names it, and whether the photo
+/// may read the shared stem sidecar (`IMG_0001.xmp`) of its stem. A photo that shares its stem
+/// with a file that owns that sidecar (see [`Session::sidecar_naming`]) reads its own `<file>.xmp`
+/// and never the packet of the sibling that owns the stem one (issue #1523).
+#[derive(Clone, Copy, Debug, PartialEq, Eq)]
+pub struct SidecarLookup {
+ pub naming: SidecarNaming,
+ /// False when another file with the same stem owns the stem sidecar.
+ pub owns_stem: bool,
+}
+
+impl SidecarLookup {
+ /// The names a sidecar for `original` can have, in the order they are looked for: those of
+ /// [`sidecar_candidates`], without the shared stem sidecar when the photo does not own it.
+ pub(crate) fn candidates(self, original: &str) -> impl Iterator<Item = PathBuf> {
+ let shared = (!self.owns_stem).then(|| xmp_cases(sidecar_path(original, SidecarNaming::Stem)));
+ sidecar_candidates(original, self.naming).filter(move |p| shared.as_ref().is_none_or(|shared| !shared.contains(p)))
+ }
+
+ /// [`find_sidecar`], except that a photo that does not own its stem sidecar never reads it.
+ pub fn find(self, original: &str) -> Option<PathBuf> {
+ self.candidates(original).find(|p| p.is_file())
+ }
+}
+
/// Find the sidecar for a file an import is adding, by the ownership [`ImportStems`] worked out
/// (issue #1905): [`find_sidecar`] with the library's naming, except that a file which does not
/// own its stem sidecar reads its own `<file>.xmp` (the name it writes) and never the packet of
/// the sibling that owns the stem one.
pub(crate) fn find_sidecar_for_import(stems: &ImportStems, naming: SidecarNaming, original: &str) -> Option<PathBuf> {
- if stems.owns(original) {
- return find_sidecar(original, naming);
- }
- let shared = xmp_cases(sidecar_path(original, SidecarNaming::Stem));
- sidecar_candidates(original, SidecarNaming::Full).filter(|p| !shared.contains(p)).find(|p| p.is_file())
+ SidecarLookup { naming, owns_stem: stems.owns(original) }.find(original)
}
/// The names `original`'s sidecar can have, in the order they are looked for: under `naming`,
@@ -523,8 +544,8 @@ fn file_path(p: &Photo) -> Option<&str> {
}
/// The XMP packet that describes a photo on disk: its sidecar, else embedded raw/DNG or JPEG XMP.
-pub fn read_packet(original: &str, kind: MediaKind, naming: SidecarNaming) -> Option<(String, PathBuf)> {
- if let Some(sc) = find_sidecar(original, naming) {
+pub fn read_packet(original: &str, kind: MediaKind, lookup: SidecarLookup) -> Option<(String, PathBuf)> {
+ if let Some(sc) = lookup.find(original) {
// A discovered sidecar keeps precedence even when reading it fails.
return std::fs::read_to_string(&sc).ok().map(|s| (s, sc));
}
@@ -675,6 +696,11 @@ impl StemOwners {
StemOwners(best.into_iter().filter(|(_, v)| v.2 > 1).map(|(k, v)| (k, v.1)).collect())
}
+ /// Whether photo `p` may read the shared stem sidecar of its stem: no other file owns it.
+ fn owns(&self, p: &Photo) -> bool {
+ file_path(p).is_none_or(|path| self.0.get(&stem_key(path)).is_none_or(|owner| *owner == p.id))
+ }
+
/// `naming` for photo `p`, except that a stem sidecar another file owns becomes Full.
fn naming(&self, p: &Photo, naming: SidecarNaming) -> SidecarNaming {
match (naming, file_path(p)) {
@@ -747,11 +773,16 @@ impl ImportStems {
/// How the sidecar of each of photos `ids` is named in library `cat`, whose naming is `naming`
/// ([`Session::sidecar_namings`], for a thread that has the library and not the session).
pub(crate) fn namings_in(cat: &lightcraft_catalog::Catalog, naming: SidecarNaming, ids: &[PhotoId]) -> Vec<SidecarNaming> {
- let owners = (naming == SidecarNaming::Stem).then(|| StemOwners::among(cat, ids));
+ lookups_in(cat, naming, ids).into_iter().map(|l| l.naming).collect()
+}
+
+/// [`namings_in`], with whether each photo may read the stem sidecar of its stem.
+pub(crate) fn lookups_in(cat: &lightcraft_catalog::Catalog, naming: SidecarNaming, ids: &[PhotoId]) -> Vec<SidecarLookup> {
+ let owners = StemOwners::among(cat, ids);
ids.iter()
- .map(|id| match (cat.photo(*id), &owners) {
- (Some(p), Some(o)) => o.naming(p, naming),
- _ => naming,
+ .map(|id| match cat.photo(*id) {
+ Some(p) => SidecarLookup { naming: owners.naming(p, naming), owns_stem: owners.owns(p) },
+ None => SidecarLookup { naming, owns_stem: true },
})
.collect()
}
@@ -767,6 +798,16 @@ impl Session {
namings_in(&self.catalog, self.xmp.naming, ids)
}
+ /// [`Session::sidecar_naming`] and the stem-sidecar ownership of photo `id`, for reading its sidecar.
+ pub fn sidecar_lookup(&self, id: PhotoId) -> SidecarLookup {
+ lookups_in(&self.catalog, self.xmp.naming, &[id]).into_iter().next().unwrap_or(SidecarLookup { naming: self.xmp.naming, owns_stem: true })
+ }
+
+ /// [`Session::sidecar_lookup`] for many photos, their stems' owners worked out once.
+ pub fn sidecar_lookups(&self, ids: &[PhotoId]) -> Vec<SidecarLookup> {
+ lookups_in(&self.catalog, self.xmp.naming, ids)
+ }
+
pub fn sidecar_naming(&self, id: PhotoId) -> SidecarNaming {
match self.catalog.photo(id) {
Some(p) if self.xmp.naming == SidecarNaming::Stem => StemOwners::among(&self.catalog, &[id]).naming(p, self.xmp.naming),
@@ -830,7 +871,7 @@ impl Session {
pub fn read_sidecar_op_with_warnings(&self, id: PhotoId) -> Result<Option<(Op, PathBuf, Vec<crate::import::ImportWarning>)>> {
let p = self.catalog.photo(id).ok_or(lightcraft_catalog::CatalogError::NoPhoto(id))?;
let Some(orig) = file_path(p) else { return Ok(None) };
- let Some((packet, from)) = read_packet(orig, p.kind, self.sidecar_naming(id)) else { return Ok(None) };
+ let Some((packet, from)) = read_packet(orig, p.kind, self.sidecar_lookup(id)) else { return Ok(None) };
let sc = parse_sidecar(&packet, crate::crs::Target::for_photo(p)).map_err(|e| EngineError::Other(format!("{}: {e}", from.display())))?;
let mut warnings = sc.warnings.clone();
for warning in &mut warnings {
diff --git a/crates/engine/src/sync.rs b/crates/engine/src/sync.rs
index 713c697a7..9826f9205 100644
--- a/crates/engine/src/sync.rs
+++ b/crates/engine/src/sync.rs
@@ -46,7 +46,7 @@ use serde::Serialize;
use crate::import::{ImportCandidate, ImportMode, ImportOptions, ScanInput, ScanProgress};
use crate::media::ProbeInfo;
-use crate::sidecar::SidecarNaming;
+use crate::sidecar::{SidecarLookup, SidecarNaming};
use crate::{EngineError, Result, Session};
/// The largest XMP sidecar read when looking for metadata updates; a bigger one is skipped.
@@ -216,8 +216,8 @@ pub fn scan_with(input: SyncInput, progress: &SyncProgress) -> FolderChanges {
}
// (how each photo's sidecar is named: a pass over the library, made here and not while the
// session waits, and not at all for a folder that is not there)
- let namings = crate::sidecar::namings_in(&labels, naming, &ids);
- let photos: Vec<(Arc<Photo>, SidecarNaming)> =
+ let namings = crate::sidecar::lookups_in(&labels, naming, &ids);
+ let photos: Vec<(Arc<Photo>, SidecarLookup)> =
ids.iter().zip(namings).filter_map(|(id, naming)| labels.photo(*id).map(|p| (Arc::clone(p), naming))).collect();
// The folder is walked once, and its listing answers for the library's photos in it: on a
// network share that is one request for a folder's names where it was several for each
@@ -509,7 +509,7 @@ impl Listing {
}
}
-fn check_photo(p: &Photo, naming: SidecarNaming, labels: &Catalog, listing: &Listing) -> Check {
+fn check_photo(p: &Photo, naming: SidecarLookup, labels: &Catalog, listing: &Listing) -> Check {
let Some(file) = crate::cmd::missing::checked_path(p) else { return Check::Nothing };
let file = Path::new(file);
if !listing.or_ask(listing.exists(file), || file.exists()) {
@@ -523,11 +523,11 @@ fn check_photo(p: &Photo, naming: SidecarNaming, labels: &Catalog, listing: &Lis
/// [`check_photo`] for each photo, in order, on several threads (each file is a wait on the
/// disk, not work for the CPU). Photos not reached before a cancel count as nothing found.
-fn check_photos(photos: &[(Arc<Photo>, SidecarNaming)], labels: &Catalog, listing: &Listing, progress: &SyncProgress) -> Vec<Check> {
+fn check_photos(photos: &[(Arc<Photo>, SidecarLookup)], labels: &Catalog, listing: &Listing, progress: &SyncProgress) -> Vec<Check> {
use std::sync::atomic::Ordering::Relaxed;
progress.to_check.store(photos.len(), Relaxed);
let cancel = &progress.files.cancel;
- let check = |p: &Photo, naming: SidecarNaming| {
+ let check = |p: &Photo, naming: SidecarLookup| {
let c = check_photo(p, naming, labels, listing);
progress.checked.fetch_add(1, Relaxed);
c
@@ -596,12 +596,12 @@ impl Session {
/// Whether `p`'s XMP sidecar file was saved after the library last had its say about the photo
/// (imported, or edited here) and says something the library doesn't.
-fn sidecar_has_news(labels: &Catalog, p: &Photo, naming: SidecarNaming, listing: &Listing) -> bool {
+fn sidecar_has_news(labels: &Catalog, p: &Photo, naming: SidecarLookup, listing: &Listing) -> bool {
let Source::File { path } = &p.source else { return false };
// ([`crate::sidecar::find_sidecar`], each name looked up in the listing before the disk)
// (a name the listing holds is read for its date next, and one that can't be read is passed
// over for the next name, as a name that is no file was)
- let found = crate::sidecar::sidecar_candidates(path, naming).find_map(|c| {
+ let found = naming.candidates(path).find_map(|c| {
let there = listing.or_ask(listing.is_file(&c), || c.is_file());
let meta = there.then(|| std::fs::metadata(&c).ok()).flatten().filter(std::fs::Metadata::is_file)?;
Some((c, meta))
@@ -654,7 +654,7 @@ pub struct SyncWork {
moved: Vec<FolderMoved>,
missing: Vec<FolderPhoto>,
/// (photo, its file, how its sidecar is named)
- sidecars: Vec<(PhotoId, String, SidecarNaming, crate::crs::Target)>,
+ sidecars: Vec<(PhotoId, String, SidecarLookup, crate::crs::Target)>,
}
/// The catalog half of a synchronize (see [`SyncJob`]).
@@ -702,7 +702,7 @@ impl SyncJob {
};
let now = import.as_ref().map_or_else(|| (s.clock)(), |(j, _)| j.now().to_string());
let sidecars = if choice.read_metadata {
- let namings = s.sidecar_namings(&metadata.iter().map(|m| PhotoId(m.id)).collect::<Vec<_>>());
+ let namings = s.sidecar_lookups(&metadata.iter().map(|m| PhotoId(m.id)).collect::<Vec<_>>());
metadata
.into_iter()
.zip(namings)
@@ -794,8 +794,8 @@ impl SyncWork {
}
/// Read and parse photo file `path`'s XMP sidecar file.
-fn read_sidecar(path: &str, naming: SidecarNaming, target: crate::crs::Target) -> std::result::Result<crate::sidecar::SidecarData, String> {
- let file = crate::sidecar::find_sidecar(path, naming).ok_or_else(|| "no XMP sidecar".to_string())?;
+fn read_sidecar(path: &str, naming: SidecarLookup, target: crate::crs::Target) -> std::result::Result<crate::sidecar::SidecarData, String> {
+ let file = naming.find(path).ok_or_else(|| "no XMP sidecar".to_string())?;
let len = std::fs::metadata(&file).map_err(|e| format!("{}: {e}", file.display()))?.len();
if len > MAX_SIDECAR_BYTES {
return Err(format!("{}: too large for an XMP sidecar", file.display()));
diff --git a/crates/engine/src/tests_xmp.rs b/crates/engine/src/tests_xmp.rs
index 1a4d86fa3..e0452e973 100644
--- a/crates/engine/src/tests_xmp.rs
+++ b/crates/engine/src/tests_xmp.rs
@@ -1471,6 +1471,39 @@ fn files_sharing_a_stem_get_their_own_sidecars() {
let _ = std::fs::remove_dir_all(&lib);
}
+/// A file that shares its stem with another catalogued file never reads the stem sidecar the
+/// other one owns, whichever naming the library uses (issue #1523).
+#[test]
+fn a_non_owner_does_not_read_its_siblings_stem_sidecar() {
+ let src = temp_dir("stem-read");
+ let lib = temp_dir("stem-read-lib");
+ write_png(&src.join("IMG_1.png"), 1);
+ let img = lightcraft_raster::Rgba8::from_fn(40, 24, |x, y| [(x * 6) as u8, (y * 9) as u8, 7, 255]);
+ let jpg =
+ crate::export::encode_image(&img, &crate::export::ExportOptions { format: crate::export::ExportFormat::Jpeg, ..Default::default() }).unwrap();
+ std::fs::write(src.join("IMG_1.jpg"), jpg).unwrap();
+ let mut s = Session::new().with_fs();
+ s.open_library(&lib, false).unwrap();
+ s.execute("library.import", &json!({"paths": [src.to_string_lossy()]})).unwrap();
+ let id = |s: &Session, name: &str| s.catalog.photos().find(|p| p.file_name == name).unwrap().id;
+ let (j, p) = (id(&s, "IMG_1.jpg"), id(&s, "IMG_1.png"));
+ s.execute("photo.rate", &json!({"ids": [j.0], "rating": 2})).unwrap();
+ s.execute("photo.saveMetadataToFile", &json!({"ids": [j.0]})).unwrap();
+ assert!(src.join("IMG_1.xmp").is_file());
+ assert!(!src.join("IMG_1.png.xmp").exists());
+ for naming in [crate::sidecar::SidecarNaming::Stem, crate::sidecar::SidecarNaming::Full] {
+ s.xmp.naming = naming;
+ // the owner reads its packet; the other file has none of its own
+ assert!(s.read_sidecar_op(j).unwrap().is_some(), "{naming:?}");
+ assert!(s.read_sidecar_op(p).unwrap().is_none(), "{naming:?}");
+ let (jpg_file, png_file) = (src.join("IMG_1.jpg"), src.join("IMG_1.png"));
+ assert_eq!(s.sidecar_lookup(j).find(&jpg_file.to_string_lossy()), Some(src.join("IMG_1.xmp")), "{naming:?}");
+ assert_eq!(s.sidecar_lookup(p).find(&png_file.to_string_lossy()), None, "{naming:?}");
+ }
+ let _ = std::fs::remove_dir_all(&src);
+ let _ = std::fs::remove_dir_all(&lib);
+}
+
/// Lossy DNG export: demosaiced JPEG tiles at the resize long edge, smaller than lossless, colour tags kept.
#[test]
fn lossy_dng_export_scales_and_shrinks() {