mirror of
https://github.com/storytold/photocraft.git
synced 2026-12-02 06:20:48 +01:00
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.
This commit is contained in:
1 parent
175a02deda
commit
9d80876f70
2 files changed
+20
-10
No files matched your search
@@ -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<Vec<u8>, AutomationError> {
|
||||
|
||||
@@ -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)| {
|
||||
|
||||
Reference in new issue
Block a user