#[cfg(not(target_arch = "wasm32"))] use super::write_atomic; use super::{EngineError, Result, Session, Value, bad, json}; // ---------- the file system (none on the web, where commands take and return bytes) ---------- // // Every path a command reads or writes goes through these, which check it against the automation // roots in force ([`crate::file_access`]): a path outside them is an error (`file_stamp` and // `file_created`: no file), as is any path once roots are in force without its kind of access. /// `path` checked for reading against the automation roots in force. #[cfg(not(target_arch = "wasm32"))] pub(crate) fn may_read(path: &str) -> Result<()> { #[cfg(test)] touch(path); crate::file_access::check_read(path).map_err(EngineError::Other) } /// Paths read while [`touched`] runs, on any thread (the SVG importer reads on one of its own). #[cfg(test)] static TOUCHED: std::sync::Mutex> = std::sync::Mutex::new(Vec::new()); #[cfg(test)] static RECORDING: std::sync::atomic::AtomicUsize = std::sync::atomic::AtomicUsize::new(0); #[cfg(test)] fn touch(path: &str) { if RECORDING.load(std::sync::atomic::Ordering::SeqCst) > 0 { TOUCHED.lock().unwrap_or_else(std::sync::PoisonError::into_inner).push(path.to_string()); } } /// Run `f` and list the paths read through this module meanwhile (by any test running then). #[cfg(test)] pub(crate) fn touched(f: impl FnOnce() -> T) -> (T, Vec) { use std::sync::atomic::Ordering::SeqCst; let start = TOUCHED.lock().unwrap_or_else(std::sync::PoisonError::into_inner).len(); RECORDING.fetch_add(1, SeqCst); let out = f(); RECORDING.fetch_sub(1, SeqCst); let seen = TOUCHED.lock().unwrap_or_else(std::sync::PoisonError::into_inner).get(start..).unwrap_or_default().to_vec(); (out, seen) } /// May a reply to the request being served name `path`, a file or folder the client did not give? /// Always without roots in force; with them, only inside the read roots ([`crate::file_access::check_read`]). pub(crate) fn shown(path: &str) -> bool { crate::file_access::check_read(path).is_ok() } /// `path` as a reply to the request being served names it: whole when [`shown`], else its file name, /// after the last `/` or `\` whichever platform wrote it (empty for a path that ends in no name, such /// as `..` or a folder ending in a separator). pub(crate) fn shown_path(path: &str) -> std::borrow::Cow<'_, str> { shown_link(path, None) } /// A link's `stored` path as a reply to the request being served names it: whole when the file it /// names (`resolved`, else `stored` itself) is [`shown`], else its file name as [`shown_path`] gives it. pub(crate) fn shown_link<'a>(stored: &'a str, resolved: Option<&str>) -> std::borrow::Cow<'a, str> { if shown(resolved.unwrap_or(stored)) { return std::borrow::Cow::Borrowed(stored); } let name = stored.rsplit(vectorcraft_doc::links::path_separators(stored)).next().unwrap_or_default(); std::borrow::Cow::Borrowed(if matches!(name, "." | "..") { "" } else { name }) } /// What an error names a file or folder outside the read roots by when [`shown_path`] gives no /// name for it, or gives one that is not valid text. pub(crate) const UNNAMED: &str = "a file or folder outside the read roots"; /// An error about `path`, a file or folder the client did not give, as the request being served /// may see it: where the message names `path` ([`naming_in_message`]), it names it as /// [`shown_path`] does, or as [`UNNAMED`] when that is empty or holds a replacement character. The /// roots the message names and the reason stay whole. Unchanged when `path` is [`shown`]. pub(crate) fn naming_in_error(e: EngineError, path: &str) -> EngineError { if path.is_empty() || shown(path) { return e; } naming_as(e, path, &error_name(path)) } /// What an error names `path` by when it is outside the read roots: [`shown_path`], or /// [`UNNAMED`] when that is empty or holds a replacement character. pub(crate) fn error_name(path: &str) -> String { let name = shown_path(path); if name.is_empty() || name.contains('\u{FFFD}') { UNNAMED.to_string() } else { name.into_owned() } } /// [`naming_in_error`] with `path` named `name`, whether or not it is [`shown`] (a name worked out /// earlier, for the request that prepared the work). pub(crate) fn naming_as(e: EngineError, path: &str, name: &str) -> EngineError { match e { EngineError::Other(msg) => EngineError::Other(naming_in_message(&msg, path, name)), EngineError::BadParams { cmd, msg } => EngineError::BadParams { cmd, msg: naming_in_message(&msg, path, name) }, EngineError::ReadOnlyLocation(msg) => EngineError::ReadOnlyLocation(naming_in_message(&msg, path, name)), e => e, } } /// `msg` with `path` given as `name` where it begins a path the message is about: at the start of /// `msg` (`: `), after a `: ` (`... outside the write root : `) or after /// `cannot resolve `, followed by the end, a separator, a `:` or a space. A root the message names /// stays whole, even one inside `path`. pub(crate) fn naming_in_message(msg: &str, path: &str, name: &str) -> String { const BEFORE: [&str; 2] = [": ", "cannot resolve "]; let mut out = String::with_capacity(msg.len()); let mut rest = msg; loop { if let Some(after) = rest.strip_prefix(path) && after.chars().next().is_none_or(|c| matches!(c, '/' | '\\' | ':' | ' ')) { out.push_str(name); rest = after; } let Some((i, len)) = BEFORE.iter().filter_map(|b| rest.find(b).map(|i| (i, b.len()))).min() else { break }; out.push_str(&rest[..i + len]); rest = &rest[i + len..]; } out.push_str(rest); out } /// The error for an SVG of a document that links a JavaScript file outside the read roots. pub(crate) const SCRIPT_OUTSIDE: &str = "a linked JavaScript file is outside the read roots"; /// `cmd` may write `st`'s document in format `format` (a format id): any format but SVG and SVGZ, /// and those too unless read roots are in force and the document links a JavaScript file outside /// them (`svg_scripts`, a relative path taken from the document's folder; a URL is not checked), /// which the file's `