Files
vectorcraft/crates/engine/src/script/startup.rs
T
dotCooCoo a354d09af4 Scripts: folder listings take their time limit from the session's clock
script::folder::list and script::startup::list measured the 250 ms
listing limit (MAX_SCAN) with a real Instant. script.listInstalled,
script.startup.run and the File > Scripts menu read the folders through
them, so a test that went through any of these got a truncated listing
and missing scripts whenever the disk stalled for 250 ms.

- folder::Clock is either Wall (the real time since the listing started,
  the default) or Fixed (the same time at every check). Both list
  functions take one.
- Session::set_script_listing_clock sets the clock the two commands and
  the menu's listing job use; the app keeps the real clock.
- The script folder, startup script, scripts menu, script run and Safe
  Mode tests list with a fixed zero time, so a slow disk cannot cut a
  listing short. Direct listing calls use the test file's list helper.
- New tests check that script.listInstalled, script.startup.run and the
  menu stop at the session's clock once it is past the limit and list
  everything at the limit.
2026-11-27 16:10:28 -08:00

126 lines
5.3 KiB
Rust
Raw Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
//! The Startup Scripts folder: the `.jsx` files `script.startup.run` runs, one after another, when
//! the app starts and before a script chosen from File › Scripts runs ([`list`]), and the read of
//! one of them when its turn comes ([`read`]).
//!
//! Only the folder's own files are taken, never its subfolders. A listing follows no link: a
//! symbolic link or a Windows junction is left out, and so is a folder that is itself a link. Names
//! that start with a dot, files Windows marks hidden or system, names that are not Unicode, names
//! longer than [`folder::MAX_NAME_BYTES`] and files whose extension is not `.jsx` are left out too.
//! The files are sorted by name without regard to case, then by name.
//!
//! The limits are the Scripts folder's ([`folder::MAX_ENTRIES`], [`folder::MAX_SCRIPTS`] and
//! [`folder::MAX_SCAN`]), VectorCraft's own values. A listing that reaches one keeps what it found
//! and says so ([`Listing::truncated`]).
use std::io::Read;
use std::path::{Path, PathBuf};
use std::time::Duration;
use super::folder;
/// What a Startup Scripts folder holds.
#[derive(Clone, Debug, Default, PartialEq, Eq)]
pub struct Listing {
/// The scripts, in the order they run.
pub scripts: Vec<PathBuf>,
/// Some scripts may be missing: a limit was reached or an entry could not be read.
pub truncated: bool,
/// The folder is there and could not be read, or is a link.
pub unreadable: bool,
}
/// Does the file name `name` name a startup script: a `.jsx` file (in any case) whose name does not
/// start with a dot and has at most [`folder::MAX_NAME_BYTES`] bytes?
pub fn takes(name: &str) -> bool {
!name.starts_with('.')
&& name.len() <= folder::MAX_NAME_BYTES
&& Path::new(name).extension().and_then(|e| e.to_str()).is_some_and(|e| e.eq_ignore_ascii_case("jsx"))
}
/// The startup scripts in `root`, within the Scripts folder's limits, with the time measured by
/// `clock`. A folder that does not exist gives an empty listing.
pub fn list(root: &Path, clock: folder::Clock) -> Listing {
clock.time(|elapsed| list_with(root, &folder::Limits::default(), elapsed))
}
/// The startup scripts in `root`, within `limits` (the entries, scripts and time ones);
/// `elapsed` says how long the listing has taken.
pub fn list_with(root: &Path, limits: &folder::Limits, elapsed: &mut dyn FnMut() -> Duration) -> Listing {
let unreadable = Listing { unreadable: true, ..Listing::default() };
match std::fs::symlink_metadata(root) {
Err(e) if e.kind() == std::io::ErrorKind::NotFound => return Listing::default(),
Err(_) => return unreadable,
Ok(m) if m.file_type().is_symlink() || !m.is_dir() => return unreadable,
Ok(_) => {}
}
if root.to_str().is_none() {
return unreadable;
}
let Ok(entries) = std::fs::read_dir(root) else { return unreadable };
let mut listing = Listing::default();
let mut names: Vec<String> = vec![];
for (visited, entry) in entries.enumerate() {
if visited >= limits.entries || elapsed() > limits.time {
listing.truncated = true;
break;
}
let Ok(entry) = entry else {
listing.truncated = true;
continue;
};
let Ok(name) = entry.file_name().into_string() else { continue };
if !takes(&name) {
continue;
}
let Ok(kind) = entry.file_type() else {
listing.truncated = true;
continue;
};
if !kind.is_file() || kind.is_symlink() || entry.metadata().is_ok_and(|m| folder::hidden_meta(&m)) {
continue;
}
names.push(name);
}
names.sort_by(|a, b| (a.to_lowercase(), a).cmp(&(b.to_lowercase(), b)));
if names.len() > limits.scripts {
listing.truncated = true;
names.truncate(limits.scripts);
}
listing.scripts = names.into_iter().map(|n| root.join(n)).collect();
listing
}
/// Is `path` a startup script a listing of `root` can hold now: a file [`takes`] names, directly in
/// `root`, where neither `root` nor the file is a link and the file is not marked hidden or system?
pub fn holds(root: &Path, path: &Path) -> bool {
if path.parent() != Some(root) {
return false;
}
let Some(name) = path.file_name().and_then(|n| n.to_str()) else { return false };
takes(name) && folder::plain(root, true, false) && folder::plain(path, false, true)
}
/// The first `max` bytes of the startup script at `path`, or `None` when [`holds`] does not hold for
/// it before the read and again after it, or it cannot be read.
pub fn read(root: &Path, path: &Path, max: usize) -> Option<Vec<u8>> {
if !holds(root, path) {
return None;
}
let file = std::fs::File::open(path).ok()?;
if !file.metadata().is_ok_and(|m| m.is_file()) {
return None;
}
let mut bytes = vec![];
file.take(max as u64).read_to_end(&mut bytes).ok()?;
holds(root, path).then_some(bytes)
}
/// A run `script.startup.run` queued for the app, which starts it when the run before it ends.
#[derive(Clone, Debug, PartialEq)]
pub enum Queued {
/// A startup script, read when its turn comes, with its `maxFuel`.
Startup { path: PathBuf, fuel: u64 },
/// The script that runs after the startup scripts, as `script.run`'s parameters.
Then(serde_json::Value),
}