PhotoCraft: An open-source, photo editor written by scratch in pure Rust
photocraft-cli run: refuse an --out that is the input file unless --in-place (#7437)
Khawar Zamman Wanicc574bcf17fc
diff --git a/apps/photocraft-cli/src/lib.rs b/apps/photocraft-cli/src/lib.rsindex 69bc1a963..752d14aef 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 <file> [--compact]Print the document as JSON (size, mode, depth, layer tree).- photocraft-cli run (<file> | --new <json>) --cmd <id> [--params <json>] [--cmd …] [--out <file>] [--format <ext>] [--quality <1-100>] [--tiff-layers] [--tga-bits <24|32>] [--dpx-bits <8|10|12|16>]+ photocraft-cli run (<file> | --new <json>) --cmd <id> [--params <json>] [--cmd …] [--out <file>] [--format <ext>] [--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+ <file> is refused, as the result would replace the original; --in-place allows it.photocraft-cli batch --actions <actions.json|set.atn> [--action <name>] --in <dir> --out <dir> [--format <ext>] [--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 aset: 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::<String>::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<String> = 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.rsindex b8aa434b3..6687d082f 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 <file> --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]