Merge pull request #14370 from RickiNano/fix/automation-write-guard-14355

automation: write guard no longer replaces text and office files (#14355)
This commit is contained in:
David Mann authored and GitHub committed 2026-11-14 15:00:15 +01:00
commit cb4cb9a593
4 files changed
+35 -7

No files matched your search

Generated
-1
View File
@@ -5479,7 +5479,6 @@ dependencies = [
"cap-std",
"clap",
"craft-ai-agents",
"craft-file-formats",
"craft-flags",
"craft-image-codecs",
"craft-psd",
-1
View File
@@ -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 }
+9 -5
View File
@@ -248,21 +248,25 @@ 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). 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_formats::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_formats::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<Vec<u8>, AutomationError> {
+26
View File
@@ -152,6 +152,32 @@ fn write_stream_seeks_back_and_keeps_the_original_on_failure() {
assert_eq!(std::fs::read(inside.join("notes.txt")).unwrap(), b"keep");
}
/// #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_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)| {
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() {