fix(ui-egui): bound the RAW re-read and treat symlinked names as taken in batch export

This commit is contained in:
Dylan M. Taylor committed 2026-11-04 00:53:21 +00:00
1 parent d07684a379
commit eca363234e
2 files changed
+46 -2

No files matched your search

@@ -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<Vec<u8>> {
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)
}
+23 -1
View File
@@ -256,7 +256,9 @@ fn batch_paths(path: &str, docs: &[Document]) -> Vec<String> {
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<Value, String> {
#[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();
}
}