diff --git a/crates/engine/src/error.rs b/crates/engine/src/error.rs index 7fc7e4c808..30b9c495e0 100644 --- a/crates/engine/src/error.rs +++ b/crates/engine/src/error.rs @@ -41,28 +41,34 @@ pub enum EngineError { enum BuildMemoryLimit { /// Measured from the computer's memory (64-bit desktop). Measured, - /// Fixed by the 32-bit desktop build. - FixedDesktop32Bit, + /// The 32-bit desktop build: its cap, or the computer's memory if lower. + Desktop32Bit, /// Fixed by the browser build. FixedWeb, } -/// The wasm32 check comes first: wasm32 is also 32-bit. +/// This build's memory-limit source, from its target. fn build_memory_limit() -> BuildMemoryLimit { - if cfg!(target_arch = "wasm32") { + memory_limit_for(cfg!(target_arch = "wasm32"), cfg!(target_pointer_width = "32")) +} + +/// The limit source for a target. The wasm32 check comes first: wasm32 is also 32-bit. +fn memory_limit_for(is_wasm32: bool, is_32_bit: bool) -> BuildMemoryLimit { + if is_wasm32 { BuildMemoryLimit::FixedWeb - } else if cfg!(target_pointer_width = "32") { - BuildMemoryLimit::FixedDesktop32Bit + } else if is_32_bit { + BuildMemoryLimit::Desktop32Bit } else { BuildMemoryLimit::Measured } } +/// The text after "hard limit" that says where the limit comes from and what to do. fn build_limit_scope(limit: BuildMemoryLimit) -> &'static str { match limit { BuildMemoryLimit::Measured => " on this machine", - BuildMemoryLimit::FixedDesktop32Bit => { - " of this 32-bit build of PhotoCraft, however much memory this computer has; install the 64-bit build to open it" + BuildMemoryLimit::Desktop32Bit => { + " on this machine; 32-bit builds of PhotoCraft can use at most 2 GiB of memory for a file, so the 64-bit build may open it if this computer has more free" } BuildMemoryLimit::FixedWeb => " of the browser version of PhotoCraft; open it in the desktop app", } @@ -111,8 +117,8 @@ mod tests { fn hard_budget_scope_text_per_build() { assert_eq!(build_limit_scope(BuildMemoryLimit::Measured), " on this machine"); assert_eq!( - build_limit_scope(BuildMemoryLimit::FixedDesktop32Bit), - " of this 32-bit build of PhotoCraft, however much memory this computer has; install the 64-bit build to open it" + build_limit_scope(BuildMemoryLimit::Desktop32Bit), + " on this machine; 32-bit builds of PhotoCraft can use at most 2 GiB of memory for a file, so the 64-bit build may open it if this computer has more free" ); assert_eq!(build_limit_scope(BuildMemoryLimit::FixedWeb), " of the browser version of PhotoCraft; open it in the desktop app"); } @@ -120,8 +126,21 @@ mod tests { #[test] fn hard_budget_message_names_file_numbers_and_scope() { let text = EngineError::PsdHardBudget { name: "big.psd".into(), required: 9000, limit: 8000 }.to_string(); - for part in ["big.psd", "9000", "8000", build_limit_scope(build_memory_limit())] { + for part in ["big.psd", "9000", "8000", "hard limit", build_limit_scope(build_memory_limit())] { assert!(text.contains(part), "{text}"); } } + + #[test] + fn memory_limit_follows_the_target() { + assert_eq!(memory_limit_for(true, true), BuildMemoryLimit::FixedWeb); + assert_eq!(memory_limit_for(true, false), BuildMemoryLimit::FixedWeb); + assert_eq!(memory_limit_for(false, true), BuildMemoryLimit::Desktop32Bit); + assert_eq!(memory_limit_for(false, false), BuildMemoryLimit::Measured); + } + + #[test] + fn two_gib_in_the_text_matches_the_cap() { + assert_eq!(photocraft_io::memory::NATIVE_32BIT_PEAK_CAP, 2 << 30); + } } diff --git a/crates/raster/src/spill.rs b/crates/raster/src/spill.rs index 9866729950..c418919f2a 100644 --- a/crates/raster/src/spill.rs +++ b/crates/raster/src/spill.rs @@ -504,33 +504,33 @@ fn no_room(dir: &Path, cap: Option) -> String { } /// (available, total) bytes of the volume holding `dir`: the mount point that is its longest -/// prefix. `None` if unknown, which disables only the free-space check, not spilling. +/// prefix. `None` if unknown, which disables only the free-space check, not spilling. Network +/// scratch folders are not listed as disks, so they report `None`: they earn no scratch allowance +/// at import and the free-space reserve doesn't apply to them, though a Scratch Size Limit still does. #[cfg(not(target_arch = "wasm32"))] fn free_space(dir: &Path) -> Option<(u64, u64)> { - let mut dir = std::fs::canonicalize(dir).ok()?; - // Only on Windows: `\\?\C:\x` is a legal file name on Unix. - if cfg!(windows) - && let Some(plain) = dir.to_str().and_then(without_verbatim_prefix) - { - dir = std::path::PathBuf::from(plain); - } + let dir = std::fs::canonicalize(dir).ok()?; + let dir = without_verbatim_disk_prefix(&dir).unwrap_or(dir); let disks = sysinfo::Disks::new_with_refreshed_list_specifics(sysinfo::DiskRefreshKind::nothing().with_storage()).ok()?; disks.iter().filter(|d| dir.starts_with(d.mount_point())).max_by_key(|d| d.mount_point().as_os_str().len()).map(|d| (d.available_space(), d.total_space())) } -/// The plain form of a Windows verbatim path (`\\?\C:\x` to `C:\x`, `\\?\UNC\s\h\x` to `\\s\h\x`), -/// or `None` to keep the path as is. `canonicalize` returns verbatim paths, whose prefix never -/// compares equal to the plain prefix of a disk's mount point. +/// The plain form of a verbatim disk path (`\\?\C:\x` to `C:\x`), or `None` to keep the path as +/// is. `canonicalize` returns `\\?\C:\x` on Windows, whose prefix never equals a mount point's +/// plain `C:\`. Unix paths never have a prefix component, so this is `None` there. #[cfg(not(target_arch = "wasm32"))] -fn without_verbatim_prefix(path: &str) -> Option { - let rest = path.strip_prefix(r"\\?\")?; - if let Some(unc) = rest.strip_prefix(r"UNC\") { - return Some(format!(r"\\{unc}")); - } - match rest.as_bytes().get(..2) { - Some([letter, b':']) if letter.is_ascii_alphabetic() => Some(rest.to_string()), - _ => None, - } +fn without_verbatim_disk_prefix(path: &Path) -> Option { + use std::path::{Component, Prefix}; + let mut components = path.components(); + let Some(Component::Prefix(prefix)) = components.next() else { + return None; + }; + let Prefix::VerbatimDisk(letter) = prefix.kind() else { + return None; + }; + let mut out = PathBuf::from(format!("{}:", char::from(letter))); + out.extend(components); + Some(out) } #[cfg(target_arch = "wasm32")] @@ -805,27 +805,41 @@ fn log_error(msg: &str) { mod tests { use super::*; - #[cfg(not(target_arch = "wasm32"))] + #[cfg(windows)] #[test] - fn verbatim_prefix_is_stripped_only_for_disks_and_unc() { + fn verbatim_disk_prefix_is_stripped_only_for_disks() { let cases: &[(&str, Option<&str>)] = &[ - (r"\\?\UNC\server\share\x", Some(r"\\server\share\x")), - (r"\\?\C:\x", Some(r"C:\x")), - (r"\\?\c:", Some("c:")), + (r"\\?\C:\x\y", Some(r"C:\x\y")), + (r"\\?\C:\", Some(r"C:\")), + (r"\\?\UNC\server\share\x", None), (r"\\?\Volume{0000-1}\x", None), (r"C:\x", None), - ("/tmp/x", None), - (r"\\?\", None), - (r"\\?\C", None), - (r"\\?\1:\x", None), - (r"\\?\é:\x", None), - ("", None), + (r"\\server\share\x", None), ]; for (input, want) in cases { - assert_eq!(without_verbatim_prefix(input).as_deref(), *want, "{input}"); + assert_eq!(without_verbatim_disk_prefix(Path::new(input)).as_deref(), want.map(Path::new), "{input}"); } } + #[cfg(windows)] + #[test] + fn verbatim_disk_prefix_keeps_non_utf8_names() { + use std::os::windows::ffi::OsStringExt; + let mut wide: Vec = r"\\?\C:\a".encode_utf16().collect(); + wide.push(0xD800); + let path = PathBuf::from(std::ffi::OsString::from_wide(&wide)); + let plain = without_verbatim_disk_prefix(&path).unwrap(); + assert!(plain.starts_with(r"C:\")); + assert!(plain.to_str().is_none()); + } + + #[cfg(not(any(windows, target_arch = "wasm32")))] + #[test] + fn verbatim_disk_prefix_is_none_on_unix() { + assert_eq!(without_verbatim_disk_prefix(Path::new("/tmp/x")), None); + assert_eq!(without_verbatim_disk_prefix(Path::new(r"\\?\C:\x")), None); + } + // `free_space` returned `None` on Windows before the fix: `canonicalize` gives `\\?\C:\..`, // which never started with the mount point `C:\`. #[cfg(windows)] diff --git a/docs/development.md b/docs/development.md index 6dfcfea8b6..1771ab5b31 100644 --- a/docs/development.md +++ b/docs/development.md @@ -820,7 +820,8 @@ physical RAM (excluding swap) and, where measurable, system commit headroom: `file.approveMemory` permission is one-shot and is not journaled or recorded in an action. A refusal above the hard ceiling is the typed `PsdHardBudget` error (name, required, limit), not a string, and it releases the queued source. Its message says where the limit comes from: - the computer's memory on 64-bit desktop, or the fixed cap of the 32-bit desktop and web builds. + the computer's memory on 64-bit desktop; the web build names its fixed cap; on 32-bit desktop the + limit may be either, so the message mentions the 2 GiB cap and that the 64-bit build may open the file. - A layered TIFF (Photoshop layer data in tags 37724/34377) is admitted by the same policy before its composite is decoded. The page's tags are read into the PSD model first, without pixels; then that model's layer/workspace estimate, the decoded composite and its diff --git a/docs/scratch-disks.md b/docs/scratch-disks.md index acc784d242..5fc440b7d4 100644 --- a/docs/scratch-disks.md +++ b/docs/scratch-disks.md @@ -46,6 +46,9 @@ spilling resumes on its own once the directory is usable and has room, and chang preferences retries at once. Preferences → Scratch Disks → Scratch Size Limit (`scratchDisks.sizeLimitMb`, 0 = no limit; `spill::set_size_cap`) caps the scratch file: reaching it pauses spilling the same way, and the free-space reserve applies regardless. +Network scratch folders are not listed as disks, so their free space isn't measured: they earn +no scratch allowance when admitting large imports and the free-space reserve doesn't apply to +them (the Scratch Size Limit still does). A failed read retains its backing extent and blocks editing, saving and exporting of the document that holds the tile until it is readable again; other open documents keep working. Each commit checks the health of its own document (before and after an edit, the document a