From cc574bcf17fc5a55afe6c9f4aea516dd4b133c0c Mon Sep 17 00:00:00 2001 From: Khawar Zamman Wani Date: Sun, 18 Oct 2026 21:04:58 +0530 Subject: [PATCH] photocraft-cli run: refuse an --out that is the input file unless --in-place (#7437) MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit `run --cmd … --out ` silently replaced the original, so a lossy re-encode couldn't be undone; `batch` already refuses an --out that is its --in folder unless --in-place (#492). `run` now takes --in-place and, with a and an --out, refuses before opening the file or running any command when the two are the same file by `file_cmds::same_file` (same name, ./ spelling, absolute path, hard or symbolic link). --new runs have no input and are unaffected. It uses the `refuse_overwriting_input` helper the `convert` fix adds. Co-Authored-By: Claude Opus 5.5 --- apps/photocraft-cli/src/lib.rs | 11 ++++++--- apps/photocraft-cli/tests/cli.rs | 40 ++++++++++++++++++++++++++++++++ 2 files changed, 48 insertions(+), 3 deletions(-) diff --git a/apps/photocraft-cli/src/lib.rs b/apps/photocraft-cli/src/lib.rs index 69bc1a9632c..752d14aef45 100644 --- a/apps/photocraft-cli/src/lib.rs +++ b/apps/photocraft-cli/src/lib.rs @@ -26,9 +26,10 @@ USAGE: --dpx-bits sets a DPX's bits per sample. Without it: 8 for an 8-bit document, else 16. photocraft-cli info [--compact] Print the document as JSON (size, mode, depth, layer tree). - photocraft-cli run ( | --new ) --cmd [--params ] [--cmd …] [--out ] [--format ] [--quality <1-100>] [--tiff-layers] [--tga-bits <24|32>] [--dpx-bits <8|10|12|16>] + photocraft-cli run ( | --new ) --cmd [--params ] [--cmd …] [--out ] [--format ] [--quality <1-100>] [--in-place] [--tiff-layers] [--tga-bits <24|32>] [--dpx-bits <8|10|12|16>] Open a file, run engine commands in order, save the result. Each --params - applies to the preceding --cmd. Prints each command's JSON result. + applies to the preceding --cmd. Prints each command's JSON result. An --out that is the + is refused, as the result would replace the original; --in-place allows it. photocraft-cli batch --actions [--action ] --in --out [--format ] [--quality <1-100>] [--in-place] [--tiff-layers] [--tga-bits <24|32>] [--dpx-bits <8|10|12|16>] Apply an action list to every image in a directory. A Photoshop Actions file (.atn) is a set: its action --action names plays, as below. JSON steps are [id, params] pairs, @@ -76,7 +77,7 @@ const SUBCOMMANDS: &[Subcommand] = &[ Subcommand { name: "run", values: &["--new", "--cmd", "--params", "--out", "--format", "--quality", "--tga-bits", "--dpx-bits"], - bare: &["--tiff-layers"], + bare: &["--in-place", "--tiff-layers"], run: run_cmds, }, Subcommand { @@ -317,6 +318,10 @@ fn run_cmds(a: &Args, out: &mut dyn Write, err: &mut dyn Write) -> R { let mut reported_fonts = std::collections::HashSet::::new(); match (a.positional.as_slice(), a.get("--new")) { ([file], None) => { + // Refused before the file is read or any command runs. + if let Some(out) = a.get("--out") { + refuse_overwriting_input(a, file, out)?; + } let o = h.open(Path::new(file)).map_err(|e| e.to_string())?; let ws: Vec = serde_json::from_value(o["warnings"].clone()).unwrap_or_default(); reported_fonts.extend(ws.iter().cloned()); diff --git a/apps/photocraft-cli/tests/cli.rs b/apps/photocraft-cli/tests/cli.rs index b8aa434b3e4..6687d082f75 100644 --- a/apps/photocraft-cli/tests/cli.rs +++ b/apps/photocraft-cli/tests/cli.rs @@ -520,6 +520,46 @@ fn tga_bits_choose_the_targa_depth() { } } +/// #7437: `run --out` to that same file, however `--out` is spelt, is refused before the +/// file is read or any command runs, unless `--in-place` asks for it (as `batch` does, #492). +#[test] +fn run_refuses_to_write_over_its_input() { + let d = tmp("run-in-place"); + write_png(&d.join("in.png"), 8, 4, 7); + let before = std::fs::read(d.join("in.png")).unwrap(); + let run = |out: &Path, extra: &[&str]| { + bin().current_dir(&d).args(["run", "in.png", "--cmd", "image.adjustments.invert", "--out"]).arg(out).args(extra).output().unwrap() + }; + let mut outs = vec![PathBuf::from("in.png"), PathBuf::from("./in.png"), d.join("in.png"), d.join(".").join("in.png")]; + std::fs::hard_link(d.join("in.png"), d.join("hard.png")).unwrap(); + outs.push(PathBuf::from("hard.png")); + #[cfg(unix)] + { + std::os::unix::fs::symlink(d.join("in.png"), d.join("soft.png")).unwrap(); + outs.push(PathBuf::from("soft.png")); + } + for out in &outs { + for extra in [&[][..], &["--format", "png"][..]] { + let o = run(out, extra); + let (stdout, err) = (String::from_utf8_lossy(&o.stdout), String::from_utf8_lossy(&o.stderr)); + assert_eq!(o.status.code(), Some(1), "--out {} {extra:?}: {err}", out.display()); + assert!(err.contains("is the input file") && err.contains("--in-place"), "{err}"); + assert!(stdout.is_empty(), "--out {} {extra:?} ran commands: {stdout}", out.display()); + assert_eq!(std::fs::read(d.join("in.png")).unwrap(), before, "--out {} {extra:?} changed the original", out.display()); + } + } + // Another file is written as before, leaving the original alone. + ok(bin().current_dir(&d).args(["run", "in.png", "--cmd", "image.adjustments.invert", "--out", "out.png"])); + assert!(d.join("out.png").is_file()); + assert_eq!(std::fs::read(d.join("in.png")).unwrap(), before); + // `--in-place` opts in: the inverted result replaces the original, which still opens. + ok(bin().current_dir(&d).args(["run", "in.png", "--cmd", "image.adjustments.invert", "--out", "in.png", "--in-place"])); + assert_ne!(std::fs::read(d.join("in.png")).unwrap(), before, "--in-place left the original unchanged"); + let (out, _) = ok(bin().current_dir(&d).args(["info", "in.png", "--compact"])); + let info: Value = serde_json::from_str(&out).unwrap(); + assert_eq!((info["width"].as_u64(), info["height"].as_u64()), (Some(8), Some(4)), "{info}"); +} + /// #492: an `--out` folder that is the `--in` folder, however it is spelt, is refused before /// anything is written, unless `--in-place` asks for it. #[test]