Skip to content
Open
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions ROADMAP.md
Original file line number Diff line number Diff line change
Expand Up @@ -114,6 +114,8 @@ Ranked toward beta; detail in [docs/roadmap.md](docs/roadmap.md), gaps in [docs/

## Progress log

- 2026-11-08: The catalog loader refuses a library with an unknown layout before anything is written (#3962): an unrecognized top-level key in `catalog.snap` (named in the error) or a `segments/` folder beside it now gets the "written by a newer version" refusal, at the current format and the previous one, instead of opening as an empty catalog and being overwritten on save.

- 2026-11-07: Compressed ORF support withdrawn pending provenance review; uncompressed support remains. Regenerated camera coverage: 557 verified, 665 unverified, 187 preview-only and 37 unsupported; 94% use-weighted coverage. See [ORF status](docs/orf-compression.md).

- 2026-11-04: Compressed ORF support was introduced in #3775; subsequently withdrawn pending provenance review. Earlier clean-room and verification claims are withdrawn; compressed files use embedded previews when available.
Expand Down Expand Up @@ -199,6 +201,7 @@ Ranked toward beta; detail in [docs/roadmap.md](docs/roadmap.md), gaps in [docs/

| Date | Change | Summary |
|---|---|---|
| 2026-11-08 | patch | Catalog loader refuses an unknown layout (unrecognized `catalog.snap` key or a `segments/` folder) before any write, #3962; headline numbers unchanged |
| 2026-10-24 | minor | Priority 7 (lens profile database) links its tracking issue #329; headline numbers unchanged |
| 2026-10-24 | patch | Alpha rationale: tests run in CI on Linux x64 (#2667) and FreeBSD nightly, not on macOS or Windows; ranked item 8 now reads "Tests in CI on macOS and Windows, and the GPU tests in CI"; percentages and hours unchanged |
| 2026-10-22 | minor | Print: Japanese photo and postcard paper sizes (L判, 2L判, KG判, はがき, 往復はがき, 六切, 四切, 名刺) in the page setup, Print dialog and PDF output; headline numbers unchanged |
Expand Down
40 changes: 39 additions & 1 deletion crates/catalog/src/journal.rs
Original file line number Diff line number Diff line change
Expand Up @@ -79,6 +79,10 @@
//! parse, fails [`Journal::open`] with [`CatalogError::Newer`] before any file is modified — it
//! is never mistaken for a torn tail or damage. (Builds up to v0.2.0 refuse a v2 snapshot with
//! "unsupported format".)
//! - **An unknown layout is refused, untouched** (#3962): a snapshot at a readable version with a
//! top-level key other than `format`, `version`, `seq` and `catalog`, or a `segments` folder
//! next to it, fails [`Journal::open`] with [`CatalogError::Newer`] the same way — the photos may
//! live where this version doesn't look, and the upgrade rewrite would save an empty catalog.

use crate::store::Store;
use crate::{Catalog, CatalogError, Op, Result};
Expand Down Expand Up @@ -418,6 +422,34 @@ struct SnapHeader {
version: u32,
}

/// The top-level keys a `catalog.snap` of this format may have. Anything else means the library is
/// laid out in a way this version doesn't read (#3962): ignoring the key would load part of it and
/// the upgrade-on-open rewrite would then save that part over the whole.
const SNAP_KEYS: [&str; 4] = ["format", "version", "seq", "catalog"];
/// A folder next to `catalog.snap` that this format never writes (a segment-file layout keeps the
/// photos there). Its presence means the snapshot alone isn't the library (#3962).
const SEGMENTS: &str = "segments";

/// Refuse a library whose layout this version doesn't read, before anything reads it into the
/// catalog or writes to it: a top-level key in `catalog.snap` outside [`SNAP_KEYS`] (`snap`), or a
/// [`SEGMENTS`] folder next to it. Without this guard such a library loads as an empty catalog and
/// the first snapshot overwrites it (#3962).
fn refuse_unknown_layout(store: &mut dyn Store, snap: Option<&[u8]>) -> Result<()> {
let layout = "this library's layout isn't one this version reads";
if let Some(bytes) = snap {
// (values are skipped, not built: the catalog itself is parsed once, by `Journal::open`)
type Keys = std::collections::BTreeMap<String, serde::de::IgnoredAny>;
let keys: Keys = serde_json::from_slice(bytes).map_err(|e| CatalogError::Corrupt(format!("{SNAPSHOT}: {e}")))?;
if let Some(key) = keys.keys().find(|k| !SNAP_KEYS.contains(&k.as_str())) {
return Err(newer(format!("{SNAPSHOT} has a top-level {key:?} this version doesn't know: {layout}")));
}
}
if store.has_folder(SEGMENTS).map_err(io)? {
return Err(newer(format!("the library has a {SEGMENTS:?} folder next to {SNAPSHOT}: {layout}")));
}
Ok(())
}

/// The whole `catalog.snap` (after [`SnapHeader`] was checked; `format` and `version` are ignored).
#[derive(serde::Deserialize)]
struct SnapFile {
Expand All @@ -443,6 +475,8 @@ impl Journal {
if h.version > VERSION {
return Err(newer(format!("{SNAPSHOT} is catalog format v{}", h.version)));
}
// a readable stamp, but a layout this version doesn't read: refused untouched (#3962)
refuse_unknown_layout(store.as_mut(), Some(bytes.as_slice()))?;
let s: SnapFile = serde_json::from_slice(&bytes).map_err(|e| CatalogError::Corrupt(format!("{SNAPSHOT}: {e}")))?;
// a seq this high can't come from real use; the next record's seq would wrap
if s.seq > MAX_SEQ {
Expand All @@ -455,7 +489,11 @@ impl Journal {
}
(catalog, s.seq, Some(h.version))
}
None => (Catalog::new(), 0, None),
None => {
// no snapshot: a fresh one would otherwise be written beside the segments
refuse_unknown_layout(store.as_mut(), None)?;
(Catalog::new(), 0, None)
}
};
report.snapshot_seq = snapshot_seq;
let mut j = Journal {
Expand Down
22 changes: 22 additions & 0 deletions crates/catalog/src/store.rs
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,13 @@ pub trait Store: Send {
fn unwritten(&self) -> Option<(usize, String)> {
None
}
/// Is there a folder called `name` among the files? The journal refuses a library laid out
/// with a folder this version doesn't read (#3962) rather than load part of it and overwrite
/// the rest. The default: a flat store (no folders), so `false`.
fn has_folder(&mut self, name: &str) -> io::Result<bool> {
let _ = name;
Ok(false)
}
Comment thread
coderabbitai[bot] marked this conversation as resolved.
Resolved
}

/// Appends and truncates open the name's own file, so anything else at the name is the library's
Expand Down Expand Up @@ -285,6 +292,16 @@ impl Store for FsStore {
fn unwritten(&self) -> Option<(usize, String)> {
self.log.as_ref().and_then(|w| w.unwritten())
}

fn has_folder(&mut self, name: &str) -> io::Result<bool> {
// (a link to a folder counts: what it points at is what another build would read)
match std::fs::metadata(self.path(name)) {
Ok(md) => Ok(md.is_dir()),
Err(e) if e.kind() == io::ErrorKind::NotFound => Ok(false),
// can't tell: an error, not a guess that would let the open go on to write
Err(e) => Err(e),
}
}
}

/// Counts the bytes written through it.
Expand Down Expand Up @@ -345,6 +362,11 @@ impl Store for MemStore {
fn describe(&self) -> String {
"memory".into()
}
/// A file named `<name>/…` stands for a file inside folder `name`.
fn has_folder(&mut self, name: &str) -> io::Result<bool> {
let prefix = format!("{name}/");
Ok(self.files.lock().unwrap_or_else(|e| e.into_inner()).keys().any(|k| k.starts_with(&prefix)))
}
fn background_writer(&self) -> Option<Box<dyn Store>> {
self.background.then(|| Box::new(self.clone()) as Box<dyn Store>)
}
Expand Down
97 changes: 97 additions & 0 deletions crates/catalog/src/tests_format_version.rs
Original file line number Diff line number Diff line change
Expand Up @@ -151,6 +151,103 @@ fn newer_snapshot_is_refused_untouched() {
assert!(e.contains(&format!("v{}", VERSION + 1)), "{e}");
}

/// Issue #3962: `catalog.snap` at stamp `version` holding `base`, with `extra` spliced in as more
/// top-level members (empty: an ordinary snapshot).
fn snap_at(version: u32, base: &Catalog, extra: &str) -> Vec<u8> {
format!("{{\"format\":\"lightcraft-catalog\",\"version\":{version},{extra}\"seq\":2,\"catalog\":{}}}\n", base.to_snapshot()).into_bytes()
}

/// The readable stamps the layout tests run at: this build's and an older one.
const READABLE: [u32; 2] = [VERSION, VERSION - 1];

/// A snapshot at a readable stamp with a top-level key this format never writes (a fork that kept
/// its photos elsewhere) is refused untouched, not loaded as a catalog without them and then saved
/// over by the upgrade or the next snapshot (#3962). Before the fix the open succeeded.
#[test]
fn unknown_top_level_key_is_refused_untouched() {
let (base, log, _) = legacy_parts();
for version in READABLE {
for (key, extra) in [("segments", r#""segments":["0001.seg","0002.seg"],"#), ("photoIndex", r#""photoIndex":{"count":2},"#)] {
let m = MemStore::new();
m.set(SNAPSHOT, snap_at(version, &base, extra));
m.set(LOG, log.clone().into_bytes());
assert_refused_untouched(&m);
let e = Journal::open(Box::new(m)).err().unwrap().to_string();
assert!(e.contains(&format!("{key:?}")) && e.contains("layout isn't one this version reads"), "v{version}: {e}");
}
}
}

/// A `segments` folder next to an otherwise ordinary snapshot: the snapshot isn't the whole
/// library, so it is refused untouched (#3962). Also with no snapshot at all, where a new empty
/// one would otherwise be written beside the folder. Before the fix the open succeeded.
#[test]
fn segments_folder_is_refused_untouched() {
let (base, log, _) = legacy_parts();
for version in READABLE {
let m = MemStore::new();
m.set(SNAPSHOT, snap_at(version, &base, ""));
m.set(LOG, log.clone().into_bytes());
m.set("segments/0001.seg", b"photos live here".to_vec());
assert_refused_untouched(&m);
let e = Journal::open(Box::new(m)).err().unwrap().to_string();
assert!(e.contains("\"segments\" folder") && e.contains("layout isn't one this version reads"), "v{version}: {e}");
}
let m = MemStore::new();
m.set("segments/0001.seg", b"photos live here".to_vec());
assert_refused_untouched(&m);
}

/// The same refusal through the native store, where the folder is a real directory: the snapshot
/// and log keep their bytes and no file is added (#3962). Before the fix the open succeeded.
#[test]
fn segments_folder_on_disk_is_refused_untouched() {
let (base, log, _) = legacy_parts();
for version in READABLE {
let dir = std::env::temp_dir().join(format!("lc-layout-{version}-{}", std::process::id()));
let _ = std::fs::remove_dir_all(&dir);
std::fs::create_dir_all(dir.join("segments")).unwrap();
std::fs::write(dir.join("segments").join("0001.seg"), b"photos live here").unwrap();
let snap = snap_at(version, &base, "");
std::fs::write(dir.join(SNAPSHOT), &snap).unwrap();
std::fs::write(dir.join(LOG), log.as_bytes()).unwrap();
let names = |d: &std::path::Path| {
let mut v: Vec<_> = std::fs::read_dir(d).unwrap().map(|e| e.unwrap().file_name()).collect();
v.sort();
v
};
let before = names(&dir);
let e = Journal::open(Box::new(FsStore::open(&dir).unwrap())).err().expect("refused");
assert!(matches!(e, CatalogError::Newer(_)), "{e:?}");
assert_eq!(std::fs::read(dir.join(SNAPSHOT)).unwrap(), snap);
assert_eq!(std::fs::read(dir.join(LOG)).unwrap(), log.as_bytes());
assert_eq!(names(&dir), before, "no file added or renamed");
std::fs::remove_dir_all(&dir).unwrap();
}
}

/// Controls for #3962: an ordinary library at a readable stamp still opens with its photos (a
/// `segments` *file* or a key-like string inside the catalog is no folder or top-level key), and a
/// stamp above [`VERSION`] is still refused for its version, even with an unknown key beside it.
#[test]
fn ordinary_layout_still_opens_and_a_newer_stamp_still_names_its_version() {
let (base, log, full) = legacy_parts();
for version in READABLE {
let m = MemStore::new();
m.set(SNAPSHOT, snap_at(version, &base, ""));
m.set(LOG, log.clone().into_bytes());
m.set("segments", b"not a folder".to_vec());
let (_, c, r) = Journal::open(Box::new(m)).unwrap();
assert_eq!(c.to_snapshot(), full.to_snapshot());
assert_eq!(r.upgraded_from, (version != VERSION).then_some(version));
}
let m = MemStore::new();
m.set(SNAPSHOT, snap_at(VERSION + 1, &base, r#""segments":[],"#));
assert_refused_untouched(&m);
let e = Journal::open(Box::new(m)).err().unwrap().to_string();
assert!(e.contains(&format!("is catalog format v{}", VERSION + 1)), "{e}");
}

/// A log record with a valid CRC whose op this version doesn't know: refused, not taken for a
/// torn tail (truncated) or damage (renamed) — wherever it is in the log.
#[test]
Expand Down