From 175a02deda8c10cc06f1fb573d8e84869f275915 Mon Sep 17 00:00:00 2001 From: RickiNano <81099017+RickiNano@users.noreply.github.com> Date: Sat, 14 Nov 2026 09:00:45 +0100 Subject: [PATCH 1/2] automation: write guard no longer replaces text and office files (#14355) #14236 moved `written_kind` and `holds_image` from the image codec registry (`photocraft_codecs`) to `photocraft_formats`. That registry also maps txt/md/csv/rtf and office files, and sniffs RTF, PDF and zip containers by their bytes, so the #9439 guard let a save or render replace an existing notes file. Use the codec registry again and drop the `photocraft-formats` dependency that only these two lines used. Fixes #14355 --- Cargo.lock | 1 - crates/automation/Cargo.toml | 1 - crates/automation/src/workspace.rs | 8 +++++--- crates/automation/src/workspace/tests.rs | 18 ++++++++++++++++++ 4 files changed, 23 insertions(+), 5 deletions(-) diff --git a/Cargo.lock b/Cargo.lock index 5c5036c9afd..4428cb4306f 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -5479,7 +5479,6 @@ dependencies = [ "cap-std", "clap", "craft-ai-agents", - "craft-file-formats", "craft-flags", "craft-image-codecs", "craft-psd", diff --git a/crates/automation/Cargo.toml b/crates/automation/Cargo.toml index 63991e8044a..034bf01e701 100644 --- a/crates/automation/Cargo.toml +++ b/crates/automation/Cargo.toml @@ -23,7 +23,6 @@ photocraft-doc = { workspace = true } photocraft-raster = { workspace = true } photocraft-compose = { workspace = true } photocraft-codecs = { workspace = true } -photocraft-formats = { workspace = true } photocraft-io = { workspace = true } photocraft-format = { path = "../format" } photocraft-engine = { workspace = true } diff --git a/crates/automation/src/workspace.rs b/crates/automation/src/workspace.rs index 82e90785d9c..b2c10e71829 100644 --- a/crates/automation/src/workspace.rs +++ b/crates/automation/src/workspace.rs @@ -248,11 +248,13 @@ impl Drop for TempRemoval<'_> { } } -/// Whether `name`'s extension is one of the formats PhotoCraft saves or exports. +/// Whether `name`'s extension is one of the formats PhotoCraft saves or exports. The image codec +/// registry, not `photocraft_formats`: that one also names text and office files PhotoCraft only +/// reads (`txt`, `md`, `csv`, `rtf`…), and notes must never be replaced (#14355). fn written_kind(name: &str) -> bool { let Some((_, ext)) = name.rsplit_once('.') else { return false }; let ext = ext.to_ascii_lowercase(); - ["psd", "psb", "ora", "pdf", photocraft_format::EXTENSION].contains(&ext.as_str()) || photocraft_formats::from_extension(&ext).is_some() + ["psd", "psb", "ora", "pdf", photocraft_format::EXTENSION].contains(&ext.as_str()) || photocraft_codecs::from_extension(&ext).is_some() } /// Whether the file at `relative` starts like an image PhotoCraft reads (PSD/PSB or a codec's @@ -262,7 +264,7 @@ fn holds_image(dir: &Dir, relative: &Path) -> bool { let Ok(mut file) = dir.open(relative) else { return false }; let Ok(n) = file.read(&mut head) else { return false }; let head = &head[..n]; - head.starts_with(b"8BPS") || photocraft_formats::detect("", head).is_some() + head.starts_with(b"8BPS") || photocraft_codecs::detect(head).is_some() } fn read_to_observed_length(reader: &mut impl Read, observed_length: u64) -> Result, AutomationError> { diff --git a/crates/automation/src/workspace/tests.rs b/crates/automation/src/workspace/tests.rs index 257d8d8c71d..e60a0b6fdf0 100644 --- a/crates/automation/src/workspace/tests.rs +++ b/crates/automation/src/workspace/tests.rs @@ -152,6 +152,24 @@ fn write_stream_seeks_back_and_keeps_the_original_on_failure() { assert_eq!(std::fs::read(inside.join("notes.txt")).unwrap(), b"keep"); } +/// #14355: text and office files PhotoCraft can read but never writes as an image (notes, data, +/// rich text) are not "a kind PhotoCraft writes", by name or by their bytes (#9439). +#[test] +fn write_refuses_to_replace_text_files_photocraft_reads_but_does_not_write() { + let (inside, _, workspace) = roots("text-kinds"); + let files: [(&str, &[u8]); 4] = [("notes.md", b"# keep"), ("data.csv", b"a,b\n1,2\n"), ("letter.rtf", b"{\\rtf1 keep}"), ("README", b"{\\rtf1 keep}")]; + let replaced: Vec<&str> = files + .into_iter() + .filter(|(name, bytes)| { + std::fs::write(inside.join(name), bytes).unwrap(); + workspace.write(name, b"\x89PNG\r\n\x1a\n").is_ok() || std::fs::read(inside.join(name)).unwrap() != *bytes + }) + .map(|(name, _)| name) + .collect(); + assert!(replaced.is_empty(), "replaced: {replaced:?}"); + assert!(temp_files(&inside).is_empty()); +} + /// A writer that panics leaves the original and no temporary file behind. #[test] fn write_stream_cleans_up_after_a_panicking_writer() { From 9d80876f70d647543a0a650e9816abe598c2eaa3 Mon Sep 17 00:00:00 2001 From: RickiNano <81099017+RickiNano@users.noreply.github.com> Date: Sat, 14 Nov 2026 09:19:52 +0100 Subject: [PATCH 2/2] automation: write guard counts only codecs this build writes `written_kind` and `holds_image` accepted any format the codec registry knows, so an existing PCX, XPM, DICOM or HEIF file (by name), or any file starting with an XPM header, could be replaced although PhotoCraft only reads those formats. Require the codec's write capability in both. --- crates/automation/src/workspace.rs | 12 +++++++----- crates/automation/src/workspace/tests.rs | 18 +++++++++++++----- 2 files changed, 20 insertions(+), 10 deletions(-) diff --git a/crates/automation/src/workspace.rs b/crates/automation/src/workspace.rs index b2c10e71829..176541f2586 100644 --- a/crates/automation/src/workspace.rs +++ b/crates/automation/src/workspace.rs @@ -250,21 +250,23 @@ impl Drop for TempRemoval<'_> { /// Whether `name`'s extension is one of the formats PhotoCraft saves or exports. The image codec /// registry, not `photocraft_formats`: that one also names text and office files PhotoCraft only -/// reads (`txt`, `md`, `csv`, `rtf`…), and notes must never be replaced (#14355). +/// reads (`txt`, `md`, `csv`, `rtf`…), and notes must never be replaced (#14355). A codec counts +/// only if this build writes it: PCX, XPM, DICOM and HEIF are read-only. fn written_kind(name: &str) -> bool { let Some((_, ext)) = name.rsplit_once('.') else { return false }; let ext = ext.to_ascii_lowercase(); - ["psd", "psb", "ora", "pdf", photocraft_format::EXTENSION].contains(&ext.as_str()) || photocraft_codecs::from_extension(&ext).is_some() + ["psd", "psb", "ora", "pdf", photocraft_format::EXTENSION].contains(&ext.as_str()) + || photocraft_codecs::from_extension(&ext).is_some_and(|format| format.caps().write) } -/// Whether the file at `relative` starts like an image PhotoCraft reads (PSD/PSB or a codec's -/// magic). Containers that also hold other things (zip, PDF) are judged by name only. +/// Whether the file at `relative` starts like an image PhotoCraft writes (PSD/PSB or a writable +/// codec's magic). Containers that also hold other things (zip, PDF) are judged by name only. fn holds_image(dir: &Dir, relative: &Path) -> bool { let mut head = [0u8; 64]; let Ok(mut file) = dir.open(relative) else { return false }; let Ok(n) = file.read(&mut head) else { return false }; let head = &head[..n]; - head.starts_with(b"8BPS") || photocraft_codecs::detect(head).is_some() + head.starts_with(b"8BPS") || photocraft_codecs::detect(head).is_some_and(|format| format.caps().write) } fn read_to_observed_length(reader: &mut impl Read, observed_length: u64) -> Result, AutomationError> { diff --git a/crates/automation/src/workspace/tests.rs b/crates/automation/src/workspace/tests.rs index e60a0b6fdf0..d7ff20a3ede 100644 --- a/crates/automation/src/workspace/tests.rs +++ b/crates/automation/src/workspace/tests.rs @@ -152,12 +152,20 @@ fn write_stream_seeks_back_and_keeps_the_original_on_failure() { assert_eq!(std::fs::read(inside.join("notes.txt")).unwrap(), b"keep"); } -/// #14355: text and office files PhotoCraft can read but never writes as an image (notes, data, -/// rich text) are not "a kind PhotoCraft writes", by name or by their bytes (#9439). +/// #14355: files PhotoCraft can read but never writes (notes, data, rich text, read-only image +/// formats such as XPM or DICOM) are not "a kind PhotoCraft writes", by name or by their bytes (#9439). #[test] -fn write_refuses_to_replace_text_files_photocraft_reads_but_does_not_write() { - let (inside, _, workspace) = roots("text-kinds"); - let files: [(&str, &[u8]); 4] = [("notes.md", b"# keep"), ("data.csv", b"a,b\n1,2\n"), ("letter.rtf", b"{\\rtf1 keep}"), ("README", b"{\\rtf1 keep}")]; +fn write_refuses_to_replace_files_photocraft_reads_but_does_not_write() { + let (inside, _, workspace) = roots("read-only-kinds"); + let files: [(&str, &[u8]); 7] = [ + ("notes.md", b"# keep"), + ("data.csv", b"a,b\n1,2\n"), + ("letter.rtf", b"{\\rtf1 keep}"), + ("README", b"{\\rtf1 keep}"), + ("icon.xpm", b"/* XPM */ keep"), + ("scan.dcm", b"keep"), + ("TODO", b"/* XPM */ keep"), + ]; let replaced: Vec<&str> = files .into_iter() .filter(|(name, bytes)| {