From eca363234ec143cb74b347ff9b7f929a5ea49cd2 Mon Sep 17 00:00:00 2001 From: "Dylan M. Taylor" Date: Wed, 4 Nov 2026 00:53:21 +0000 Subject: [PATCH] fix(ui-egui): bound the RAW re-read and treat symlinked names as taken in batch export --- .../ui-egui/src/dialogs/camera_raw_ui/open.rs | 24 ++++++++++++++++++- crates/ui-egui/src/dialogs/export_dialog.rs | 24 ++++++++++++++++++- 2 files changed, 46 insertions(+), 2 deletions(-) diff --git a/crates/ui-egui/src/dialogs/camera_raw_ui/open.rs b/crates/ui-egui/src/dialogs/camera_raw_ui/open.rs index 41fbe6170e..e0a3e3cfb1 100644 --- a/crates/ui-egui/src/dialogs/camera_raw_ui/open.rs +++ b/crates/ui-egui/src/dialogs/camera_raw_ui/open.rs @@ -201,7 +201,7 @@ pub(super) fn commit_open_raw(app: &mut PhotocraftApp, params: &CameraRaw, raw: photocraft_engine::jobs::OpenSource::Bytes(b) => b, #[cfg(not(target_arch = "wasm32"))] photocraft_engine::jobs::OpenSource::Path(p) => { - std::sync::Arc::new(std::fs::read(&p).map_err(|e| photocraft_engine::EngineError::Other(format!("{p}: {e}")))?) + std::sync::Arc::new(read_raw_file(&p).map_err(|e| photocraft_engine::EngineError::Other(format!("{p}: {e}")))?) } #[cfg(target_arch = "wasm32")] photocraft_engine::jobs::OpenSource::Path(p) => return Err(photocraft_engine::EngineError::Other(format!("{p}: cannot read files here"))), @@ -497,3 +497,25 @@ pub fn open_smart_filter(app: &mut PhotocraftApp, ctx: &egui::Context, layer: La let coverage = sm.filter_mask.clone().filter(|m| m.enabled).map_or(Coverage::None, Coverage::Mask); open_pixels(app, ctx, layer, surf, params, coverage, Target::SmartFilter(index)) } + +/// Largest RAW file re-read for a tuned develop (the importer's own file ceiling is far lower). +#[cfg(not(target_arch = "wasm32"))] +const MAX_RAW_REREAD_BYTES: u64 = 4 << 30; + +/// The bytes of the regular file at `path`, at most [`MAX_RAW_REREAD_BYTES`]: the opened handle +/// must be a regular file, so a pathname swapped for a never-ending device is refused, and reading +/// stops one byte past the cap. +#[cfg(not(target_arch = "wasm32"))] +fn read_raw_file(path: &str) -> std::io::Result> { + use std::io::{Error, ErrorKind, Read}; + let file = std::fs::File::open(path)?; + if !file.metadata()?.is_file() { + return Err(Error::new(ErrorKind::InvalidInput, "not a regular file")); + } + let mut bytes = Vec::new(); + file.take(MAX_RAW_REREAD_BYTES + 1).read_to_end(&mut bytes)?; + if bytes.len() as u64 > MAX_RAW_REREAD_BYTES { + return Err(Error::new(ErrorKind::InvalidData, "file is too large")); + } + Ok(bytes) +} diff --git a/crates/ui-egui/src/dialogs/export_dialog.rs b/crates/ui-egui/src/dialogs/export_dialog.rs index 68f46af65f..5a266d9bb8 100644 --- a/crates/ui-egui/src/dialogs/export_dialog.rs +++ b/crates/ui-egui/src/dialogs/export_dialog.rs @@ -256,7 +256,9 @@ fn batch_paths(path: &str, docs: &[Document]) -> Vec { let candidate = { let mut candidate = candidate; let mut suffix = 1u64; - while candidate.exists() { + // A name any entry occupies is taken, a symlink (even a dangling one) included: the writer + // follows links, so one planted here would redirect the export. + while candidate.symlink_metadata().is_ok() { candidate = parent.join(format!("{stem}-{}-{name}-{suffix}.{ext}", i + 1)); suffix = suffix.saturating_add(1); } @@ -1167,3 +1169,23 @@ fn quick_export(app: &mut PhotocraftApp, p: Value) -> Result { #[cfg(test)] mod tests; + +#[cfg(all(test, unix))] +mod batch_name_tests { + use super::*; + use photocraft_doc::{ColorMode, SampleType, Size}; + + #[test] + fn a_planted_symlink_makes_a_batch_name_taken() { + let dir = std::env::temp_dir().join(format!("pc-batch-names-{}", std::process::id())); + std::fs::create_dir_all(&dir).unwrap(); + // Dangling: `exists()` follows it and reports the name free; the writer would follow it too. + let planted = dir.join("x-2-a.png"); + let _ = std::fs::remove_file(&planted); + std::os::unix::fs::symlink(dir.join("victim"), &planted).unwrap(); + let doc = Document::new("a", Size::new(2, 2), ColorMode::Rgb, SampleType::U8); + let paths = batch_paths(dir.join("x.png").to_str().unwrap(), &[doc.clone(), doc]); + assert_ne!(paths[1], planted.to_str().unwrap()); + std::fs::remove_file(&planted).unwrap(); + } +}