856f865cf98d
paths.rs
use super::{EngineError, Result, Session};
// ---------- file system (native only) ----------
#[cfg(not(target_arch = "wasm32"))]
pub(super) fn is_file(path: &str) -> bool {
std::path::Path::new(path).is_file()
}
#[cfg(target_arch = "wasm32")]
pub(super) fn is_file(_path: &str) -> bool {
false
}
#[cfg(not(target_arch = "wasm32"))]
/// Every file the engine opens is read here: in bounded reads, with a clear error when it does
/// not fit in memory (see [`photocraft_format::read`]).
pub(crate) fn read_file(path: &str) -> Result<Vec<u8>> {
photocraft_format::read_file(std::path::Path::new(path)).map_err(|e| EngineError::Other(format!("{path}: {e}")))
}
#[cfg(target_arch = "wasm32")]
pub(crate) fn read_file(path: &str) -> Result<Vec<u8>> {
Err(EngineError::Other(format!("cannot read {path}: no file system on the web")))
}
#[cfg(not(target_arch = "wasm32"))]
/// [`read_file`] with the caller's own byte limit: a larger file is refused without being read,
/// for a caller that has to fit it into what is left of a memory budget. The `io::Error` is
/// returned as is, so the caller can tell a file over its limit (`FileTooLarge`, `OutOfMemory`)
/// from one it could not open or read.
pub(crate) fn read_file_limited(path: &str, max: u64) -> std::io::Result<Vec<u8>> {
photocraft_format::read_file_limited(std::path::Path::new(path), max)
}
#[cfg(target_arch = "wasm32")]
pub(crate) fn read_file_limited(path: &str, _max: u64) -> std::io::Result<Vec<u8>> {
Err(std::io::Error::new(std::io::ErrorKind::Unsupported, format!("cannot read {path}: no file system on the web")))
}
#[cfg(not(target_arch = "wasm32"))]
/// Every file the engine writes goes through here: crash-safe (temp file + fsync + rename, see
/// [`photocraft_format::atomic`]), so a failed save never destroys the previous file.
pub(crate) fn write_file(path: &str, bytes: &[u8]) -> Result<()> {
photocraft_format::atomic_write(std::path::Path::new(path), bytes).map_err(|e| EngineError::Other(e.to_string()))
}
#[cfg(target_arch = "wasm32")]
pub(crate) fn write_file(path: &str, _bytes: &[u8]) -> Result<()> {
Err(EngineError::Other(format!("cannot write {path}: no file system on the web")))
}
/// Files of a folder that look like images we can open, sorted by name.
pub fn list_images(dir: &str) -> Result<Vec<String>> {
list_files(dir, false)
}
/// Sorted regular files, optionally retaining unsupported names for per-file Batch errors.
#[cfg(not(target_arch = "wasm32"))]
pub(super) fn list_files(dir: &str, include_non_images: bool) -> Result<Vec<String>> {
let rd = std::fs::read_dir(dir).map_err(|e| EngineError::Other(format!("{dir}: {e}")))?;
let mut out = Vec::new();
for entry in rd {
let path = entry.map_err(|e| EngineError::Other(format!("{dir}: {e}")))?.path();
if !include_non_images && !openable_name(&path.to_string_lossy()) {
continue;
}
match std::fs::metadata(&path) {
Ok(metadata) if metadata.is_file() => out.push(path.to_string_lossy().into_owned()),
Ok(_) => {}
Err(error) if error.kind() == std::io::ErrorKind::NotFound => {} // broken link or entry removed during traversal
Err(error) => return Err(EngineError::Other(format!("{}: {error}", path.display()))),
}
}
out.sort();
Ok(out)
}
#[cfg(target_arch = "wasm32")]
pub(super) fn list_files(dir: &str, _include_non_images: bool) -> Result<Vec<String>> {
Err(EngineError::Other(format!("cannot list {dir}: no file system on the web")))
}
/// The web has no folder entries to filter: every name passes, as before.
#[cfg(target_arch = "wasm32")]
pub(crate) fn openable_name(_path: &str) -> bool {
true
}
/// Whether a folder entry is one the batch commands open: an `OPENABLE` extension, an extension
/// of a format `photocraft-codecs` reads in this build, or GIMP's gzipped `.xcf.gz`, whose last
/// extension alone says nothing.
#[cfg(not(target_arch = "wasm32"))]
pub(crate) fn openable_name(path: &str) -> bool {
let lower = file_name(path).to_ascii_lowercase();
lower.ends_with(".xcf.gz")
|| lower.rsplit_once('.').is_some_and(|(_, ext)| OPENABLE.contains(&ext) || photocraft_codecs::from_extension(ext).is_some_and(|f| f.caps().read))
}
/// Extensions the batch commands pick up from a folder besides the codec formats: the document
/// and vector formats `photocraft_io::import` reads outside `photocraft-codecs`, and the camera
/// raws whose usual files `photocraft_raw::decode` develops. Not CR3, ORF, RAF, RW2, SRF or SR2:
/// CR3, compressed ORF and RAF and RW2 raw formats other than 3, 4 and 5 aren't decoded yet, and
/// import falls back to the embedded JPEG preview with only a warning, which a batch must not
/// process silently.
#[cfg(not(target_arch = "wasm32"))]
const OPENABLE: &[&str] = &[
"pdn", "ora", "xcf", "xcfgz", "kra", "krz", "psd", "psb", "psdt", "pcraft", "svg", "svgz", "af", "afdesign", "afphoto", "afpub", "dng", "cr2", "nef",
"nrw", "arw", "pef",
];
pub(crate) fn file_name(path: &str) -> String {
path.rsplit(['/', '\\']).next().unwrap_or(path).to_string()
}
/// The lower-case extension of the file name in `path` (none for `.hidden` or `name`).
pub fn extension(path: &str) -> Option<String> {
file_name(path).rsplit_once('.').filter(|(base, ext)| !base.is_empty() && !ext.is_empty()).map(|(_, ext)| ext.to_ascii_lowercase())
}
/// Whether a save without a new path may write back to `path` as far as its format goes: only
/// layered files (PSD, PSB, OpenRaster, .pcraft). A flat file goes through Save As instead, so it
/// is never flattened over the original. Whether this document fits is [`holds_at`].
pub fn saves_in_place(path: &str) -> bool {
extension(path).is_some_and(|ext| layered_extension(&ext))
}
/// Whether the lower-case extension `ext` names a layered document format (PSD, PSB, OpenRaster,
/// .pcraft). A save to one becomes the document's file; any other format is a copy for automation
/// saves, desktop and headless alike (#1547, #2579).
pub fn layered_extension(ext: &str) -> bool {
matches!(ext, "psd" | "psb" | "pcraft" | "ora")
}
/// Whether a file in format `ext` (lower-case) holds the whole of `doc`, so it can be the
/// document's own file: PSD, PSB and .pcraft always; OpenRaster only when it drops nothing (no
/// adjustment layers, styles, masks…, [`photocraft_io::ora_losses`]). A save to a format that
/// doesn't hold the document is a copy, and File › Save goes through Save As, so an editable
/// original is never overwritten with less than it had (#3806).
pub fn holds(doc: &photocraft_doc::Document, ext: &str) -> bool {
match ext {
"psd" | "psb" | "pcraft" => true,
"ora" => photocraft_io::ora_losses(doc).is_empty(),
_ => false,
}
}
/// [`holds`] for the file at `path`.
pub fn holds_at(doc: &photocraft_doc::Document, path: &str) -> bool {
extension(path).is_some_and(|ext| holds(doc, &ext))
}
/// What a save of `doc` to `path` would leave out when its format can't hold it: OpenRaster's
/// [`photocraft_io::ora_losses`]; empty for other formats.
pub fn losses_at(doc: &photocraft_doc::Document, path: &str) -> Vec<String> {
match extension(path).as_deref() {
Some("ora") => photocraft_io::ora_losses(doc),
_ => Vec::new(),
}
}
/// Whether `path` names a document template (.psdt). A template opens as a new untitled document
/// without its path, so a save never writes over the template.
pub fn is_template(path: &str) -> bool {
extension(path).as_deref() == Some("psdt")
}
/// The first "`base`-N" that isn't `taken`.
pub fn untitled_name(base: &str, taken: impl Fn(&str) -> bool) -> String {
(1..).map(|i| format!("{base}-{i}")).find(|n| !taken(n)).unwrap_or_default()
}
/// The name a document opened from `path` gets when `path` is a template: the first "Untitled-N"
/// no open document has.
pub fn template_name(s: &Session, path: &str) -> Option<String> {
is_template(path).then(|| untitled_name("Untitled", |n| s.documents().iter().any(|d| d.doc.name == n)))
}
pub(crate) fn stem(path: &str) -> String {
let n = file_name(path);
match n.rfind('.') {
Some(i) if i > 0 => n[..i].to_string(),
_ => n,
}
}
/// Whether `a` and `b` name the same file: the same text, or, when both exist, the same file on
/// disk (a hard link, a different case on a case-insensitive volume, `./` or `..` in one of them).
pub fn same_file(a: impl AsRef<std::path::Path>, b: impl AsRef<std::path::Path>) -> bool {
let (a, b) = (a.as_ref(), b.as_ref());
if a == b {
return true;
}
#[cfg(unix)]
{
use std::os::unix::fs::MetadataExt;
if let (Ok(x), Ok(y)) = (std::fs::metadata(a), std::fs::metadata(b)) {
return x.dev() == y.dev() && x.ino() == y.ino();
}
}
#[cfg(windows)]
{
// The volume serial number and file index of each, as unix compares dev and inode, so a
// hard link (another name for the file) is the same file.
if let Ok(same) = same_file::is_same_file(a, b) {
return same;
}
// Should a file not open: NTFS and FAT compare names without case.
if let (Ok(x), Ok(y)) = (std::fs::canonicalize(a), std::fs::canonicalize(b)) {
return x.to_string_lossy().to_lowercase() == y.to_string_lossy().to_lowercase();
}
}
false
}
/// Resolve a future output path component by component. Existing symlinks are followed before
/// applying a later `..`, while components not created yet are kept so a new `x/../file` still
/// resolves to the file it would reach after `x` is created.
#[cfg(not(target_arch = "wasm32"))]
pub fn destination_identity(path: &std::path::Path) -> Option<std::path::PathBuf> {
use std::path::{Component, PathBuf};
let absolute = if path.is_absolute() { path.to_path_buf() } else { std::env::current_dir().ok()?.join(path) };
let mut resolved = PathBuf::new();
for component in absolute.components() {
match component {
Component::CurDir => {}
Component::ParentDir => {
resolved.pop();
}
Component::Normal(_) => {
resolved.push(component.as_os_str());
match std::fs::symlink_metadata(&resolved) {
Ok(_) => resolved = std::fs::canonicalize(&resolved).ok()?,
Err(error) if error.kind() == std::io::ErrorKind::NotFound => {}
Err(_) => return None,
}
}
Component::Prefix(_) | Component::RootDir => resolved.push(component.as_os_str()),
}
}
Some(resolved)
}
/// Probe the actual destination directory. macOS volumes and Windows directories may use either
/// case behavior; the OS name alone cannot tell whether two future names will alias.
#[cfg(any(target_os = "macos", target_os = "windows"))]
pub(super) fn case_sensitive_destination(path: &std::path::Path) -> Option<bool> {
let parent = path.ancestors().skip(1).find(|ancestor| ancestor.is_dir())?;
let probe = tempfile::Builder::new().prefix(".PhotoCraftCaseProbe-").tempfile_in(parent).ok()?;
let name = probe.path().file_name()?.to_str()?;
let alternate = parent.join(name.replacen("PhotoCraft", "photocraft", 1));
match std::fs::metadata(&alternate) {
Ok(_) => Some(!same_file(probe.path(), &alternate)),
Err(error) if error.kind() == std::io::ErrorKind::NotFound => Some(true),
Err(_) => None,
}
}
#[cfg(any(target_os = "macos", target_os = "windows"))]
fn resolved_paths_match(a: &std::path::Path, b: &std::path::Path, case_sensitive: impl FnOnce() -> Option<bool>) -> bool {
if a == b {
return true;
}
match (a.to_str(), b.to_str()) {
(Some(a_text), Some(b_text)) if a_text.to_lowercase() == b_text.to_lowercase() => !case_sensitive().unwrap_or(false),
_ => false,
}
}
#[cfg(all(test, any(target_os = "macos", target_os = "windows")))]
#[test]
fn identical_resolved_paths_conflict_on_a_case_sensitive_volume() {
let path = std::path::Path::new("/future/result.png");
assert!(resolved_paths_match(path, path, || Some(true)));
assert!(!resolved_paths_match(path, std::path::Path::new("/future/other.png"), || panic!("unrelated names must not probe the volume")));
}
/// Whether two paths would name the same destination even before the file exists. This is used
/// when a batch error log must not be allowed to replace a result that will be written later.
pub fn same_destination(a: impl AsRef<std::path::Path>, b: impl AsRef<std::path::Path>) -> bool {
let (a, b) = (a.as_ref(), b.as_ref());
if same_file(a, b) {
return true;
}
#[cfg(not(target_arch = "wasm32"))]
{
match (destination_identity(a), destination_identity(b)) {
(Some(a), Some(b)) => {
#[cfg(any(target_os = "windows", target_os = "macos"))]
{
resolved_paths_match(&a, &b, || case_sensitive_destination(&a))
}
#[cfg(not(any(target_os = "windows", target_os = "macos")))]
{
a == b
}
}
_ => true,
}
}
#[cfg(target_arch = "wasm32")]
false
}
/// A path as command results report it: `/` between components, whatever the platform.
///
/// `join` already builds with `/`, but a directory from the caller or from `std::path::absolute`
/// carries the native separator, which would leave a reported path mixed. The control channel
/// takes `/` and rejects alternate separators (`AuthorizedWorkspace` in `crates/automation`), so
/// a result spelt this way can be handed straight back to it. Verbatim and UNC prefixes keep
/// theirs: Win32 does not accept `/` after a leading `\\`.
pub(crate) fn forward_slashes(path: String) -> String {
if path.starts_with(r"\\") { path } else { path.replace('\\', "/") }
}
pub(crate) fn join(dir: &str, name: &str) -> String {
if dir.is_empty() {
name.to_string()
} else if dir.ends_with('/') || dir.ends_with('\\') {
format!("{dir}{name}")
} else {
format!("{dir}/{name}")
}
}
/// A file-name-safe version of a layer name. A Windows device name (`nul`, `con.v2`) gets a `_`
/// before its first dot, so the file or folder is usable on Windows, wherever it was written.
pub(crate) fn sanitize(name: &str) -> String {
let s: String = name.chars().map(|c| if c.is_alphanumeric() || matches!(c, '-' | '_' | ' ' | '.') { c } else { '_' }).collect();
let s = s.trim().trim_matches('.').to_string();
if s.is_empty() { "layer".into() } else { portable_device_name(s) }
}
/// `name` with a `_` before its first dot when it is a Windows device name (`nul` → `nul_`,
/// `com1.v2` → `com1_.v2`); a `_` at the end would leave the device stem in place.
pub fn portable_device_name(name: String) -> String {
if !is_windows_device_name(&name) {
return name;
}
match name.split_once('.') {
Some((device, rest)) => format!("{device}_.{rest}"),
None => format!("{name}_"),
}
}
/// Whether `component` names a Windows device (`CON`, `NUL`, `COM1`…, with any extension):
/// Windows opens the device instead of a file or folder of that name. `COM` and `LPT` take the
/// superscript digits ¹ ² ³ as well, as Windows does.
pub(crate) fn is_windows_device_name(component: &str) -> bool {
let stem = component.split('.').next().unwrap_or(component).trim_end_matches(' ').to_ascii_uppercase();
let port = |n: &str| matches!(n, "1" | "2" | "3" | "4" | "5" | "6" | "7" | "8" | "9" | "¹" | "²" | "³");
matches!(stem.as_str(), "CON" | "PRN" | "AUX" | "NUL" | "CLOCK$" | "CONIN$" | "CONOUT$")
|| stem.strip_prefix("COM").is_some_and(port)
|| stem.strip_prefix("LPT").is_some_and(port)
}
No file is open. Choose one in the tree, or press Ctrl+O (⌘O on a Mac) to find one by name.
Syntax highlighting could not load. The unmodified read-only text remains available above.