diff --git a/crates/automation/src/workspace.rs b/crates/automation/src/workspace.rs index b2c10e7182..176541f258 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 e60a0b6fdf..d7ff20a3ed 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)| {