//! `File` and `Folder`: a script's objects for files and folders, as the help pages describe them
//! (,
//! and
//! ). The classes are
//! built-ins of the realm ([`Realm::define_file_classes`]); the host functions here make every
//! path check and every read.
//!
//! An object holds an absolute path: a relative path is taken from `Folder.current`, `~` is the home
//! folder, `%xx` escapes are read as UTF-8, and on Windows both `/` and `\` separate names and
//! `/c/…` names drive C. Naming a path does not look anything up.
//!
//! VectorCraft's own rules:
//!
//! - A script reads only inside its [`ScriptScope`]: a script run from a file reads in that file's
//! folder and the folders below it, and a script given as text reads nothing. A path whose names
//! lie outside, a Windows network or device path (`\\host\share`, `\\?\`, `\\.\`), and a path that
//! leads out through a link are refused before or instead of opening anything: `exists` is false,
//! the dates are `null`, `getFiles` gives `null`, and `error` is [`ACCESS_DENIED`]. The automation
//! read roots apply as well, so a run with roots in force reads only inside both.
//! - `File(path)` without `new` gives a `Folder` for an existing folder, and `Folder(path)` gives a
//! `File` for an existing file, only where the path may be read.
//! - Opening reads the whole file, at most [`MAX_FILE_BYTES`], checked from its size before any of
//! it is read; a run reads at most [`MAX_RUN_BYTES`] in all and ends past that, and has at most
//! [`MAX_OPEN`] files open at once. A file opened again after its objects closed counts only the
//! bytes it has grown by; a file open in another object counts again.
//! - The encoding of an opened file is the one its byte order mark gives (UTF-8, UTF-16LE or
//! UTF-16BE), otherwise the one the script set, otherwise UTF-8 when the bytes are valid UTF-8,
//! otherwise CP1252. `read`, `readln` and `readch` count characters and `seek` and `tell` bytes;
//! `readln` and `readch` take CR, LF, CRLF and LFCR as one line end, and `readch` gives a line end
//! as `"\n"`. A character the encoding cannot decode fails the read with [`UNDECODABLE`] and
//! leaves the position where it was.
//! - `getFiles` lists at most [`MAX_ENTRIES`] entries, sorted by name, and looks at most
//! [`crate::file_access::MAX_LISTED`] names; a mask matches `*` and `?`, without regard to case
//! on Windows and macOS, in at most [`MAX_MASK_STEPS`] steps for one listing.
//! - A path longer than [`MAX_PATH_CHARS`] characters is refused before it is split into names.
//! - A script writes only in its run's temporary folder (`Folder.temp`, made when it is first
//! named and deleted when the run ends), its own data folder (`Folder.userData`: for a script run
//! from a file a folder of its own beside the settings, kept between runs, and for a script given
//! as text a folder inside the temporary one), and the files and folders a person granted it.
//! Its own folder is only read. The automation write roots apply as well, and the folder that
//! holds the settings, with the Scripts and Plug-ins folders, is kept out of every grant but the
//! script's data folder.
//! - In the app, `File.openDialog`, `File.saveDialog`, `Folder.selectDialog` and the objects'
//! `openDlg`, `saveDlg` and `selectDlg` wait for the person, who picks a file or a folder and then
//! allows the script that one, once or always, or denies it ([`ScriptPicker`]). A picked file is
//! granted for reading (open) or for reading and writing (save), and a picked folder for reading
//! and writing with everything inside it. A `filter` that is a function is called with each file
//! picked, and the files it refuses are left out of what the dialog gives. Always keeps the grant in the grants file beside the
//! settings, for that script file with that text (its SHA-256): a changed or moved script has none,
//! and a kept grant whose path now leads elsewhere through a link is not used.
//! The grant also holds the SHA-256 of each source the run evaluated (a file `$.evalFile` ran, a
//! string given to `eval` or `Function`), up to the run's end: a later run that evaluates other
//! code, or whose named engine holds code its earlier runs compiled that the grant was not kept
//! for, loses the grant before that code runs. A run with nobody to ask gets `null` and
//! [`NO_PICKER`].
//! - The commands a script runs with `vc.run` read and write only in the places above and in the
//! files of the open documents ([`crate::file_access::ScriptGate`]).
//! - `open('w')` empties the file at once; what a script writes is kept in memory, at most
//! [`MAX_FILE_BYTES`] a file, and written when it closes the file or the run ends; a run writes at
//! most [`MAX_RUN_BYTES`] in all and ends past that. `write` encodes in the file's encoding (an
//! ASCII or ISO-8859-1 file refuses a character it cannot hold, BINARY writes each character's low
//! byte) and `writeln` adds the `lineFeed`'s line end. `copy` replaces a file that is there,
//! `remove` deletes at once and takes a folder only when it is empty, `rename` takes a name without
//! a folder and refuses one in use, and `length` and `readonly` change a file (on Unix, clearing
//! `readonly` gives back the write bits the run cleared, or else the owner's). Each object that has
//! a file open holds its own bytes, so what `length` adds and what `copy` writes count once for the
//! file and once more for each such object. `write` joins its values only for an open file and
//! only up to twice [`MAX_FILE_BYTES`] UTF-16 code units. Each file written is named in the
//! report's `written` (at most [`super::MAX_WRITTEN`]), also after a run that failed.
//! - Hidden settings, aliases and `execute` are not available: they return `false` and set `error`.
//! `execute` is counted in the report's `fileExecutes`.
//! - In a wasm build every lookup fails with [`NO_WEB`].
use std::collections::HashMap;
use std::path::Path;
use std::rc::Rc;
use std::sync::atomic::{AtomicU32, Ordering};
use vectorcraft_js::{Data, HostError, HostItem, Realm, RunError};
use super::{Host, bump};
use crate::file_access::{ScriptRead, ScriptScope, path_inside};
/// The most bytes of one file a script opens.
pub const MAX_FILE_BYTES: usize = 8 << 20;
/// The most files a run remembers the bytes counted for, so opening one again counts only what it
/// grew by; a file past them counts in full each time it is opened.
const MAX_CHARGED: usize = 4096;
/// The most bytes of files one run opens, all of them counted together.
pub const MAX_RUN_BYTES: usize = 32 << 20;
/// The most entries `getFiles` gives.
pub const MAX_ENTRIES: usize = 2048;
/// The most files a run has open at once.
pub const MAX_OPEN: usize = 256;
/// The most characters of a path a script gives.
pub const MAX_PATH_CHARS: usize = 32_767;
/// The most steps one `getFiles` listing spends matching its mask against names.
pub const MAX_MASK_STEPS: usize = 1 << 24;
pub(crate) const ACCESS_DENIED: &str = "Script file access denied";
pub(crate) const TOO_LARGE: &str = "Script file exceeds 8 MiB";
pub(crate) const NOT_OPEN: &str = "Script file is not open";
pub(crate) const UNDECODABLE: &str = "Script file cannot be decoded";
pub(crate) const NOT_REGULAR: &str = "Script file is not a regular file";
pub(crate) const NOT_FOUND: &str = "Script file does not exist";
pub(crate) const READ_FAILED: &str = "Script file could not be read";
pub(crate) const LISTING_TOO_LARGE: &str = "Script folder has more than 2,048 entries";
pub(crate) const BAD_MODE: &str = "Script file open mode must be r, w, e or a";
pub(crate) const NO_PICKER: &str = "Script file dialogs need a person to answer them";
pub(crate) const NO_EXECUTE: &str = "Script files cannot be opened in another application";
pub(crate) const NO_WEB: &str = "Script file access is unavailable on the web";
pub(crate) const TOO_MANY_OPEN: &str = "Script has 256 files open";
pub(crate) const PATH_TOO_LONG: &str = "Script file path exceeds 32,767 characters";
pub(crate) const MASK_TOO_COSTLY: &str = "Script folder mask takes too many steps to match";
pub(crate) const NOT_WRITABLE: &str = "Script file is not open for writing";
pub(crate) const UNENCODABLE: &str = "Script file cannot encode the text";
pub(crate) const WRITE_FAILED: &str = "Script file could not be written";
pub(crate) const NOT_EMPTY: &str = "Script folder is not empty";
pub(crate) const NAME_TAKEN: &str = "Script file name is already in use";
pub(crate) const BAD_NAME: &str = "Script file name must be a name without a folder";
pub(crate) const NO_ALIAS: &str = "Script files cannot make aliases";
pub(crate) const NO_HIDDEN: &str = "Script file hidden setting cannot be changed";
pub(crate) const PROTECTED: &str = "Script file access to the app's own folders is not allowed";
pub(crate) const NOT_SAVED: &str = "Script file permission could not be saved";
pub(crate) const NO_ALWAYS: &str = "Script file permission can be kept only for a script run from a file";
const OVER_RUN_BYTES: &str = "the script opened more file data than one run may open (32 MiB)";
const OVER_RUN_WRITES: &str = "the script wrote more file data than one run may write (32 MiB)";
/// The most grants the grants file keeps.
pub const MAX_STORED_GRANTS: usize = 4096;
/// The most bytes of the grants file that are read.
const MAX_STORE_BYTES: usize = 4 << 20;
/// The name of the grants file, beside the folder of the scripts' data folders.
pub const GRANTS_FILE: &str = "Script Grants.json";
/// The most distinct sources a run evaluates whose SHA-256 is kept; past them no kept grant is in
/// force for the rest of the run, and the run's grants keep the sources counted before.
pub const MAX_RUN_CODE: usize = 256;
/// The SHA-256 of `units`, source text a script evaluated, as UTF-16 code units in little-endian
/// order.
pub(crate) fn code_hash(units: &[u16]) -> String {
let bytes: Vec = units.iter().flat_map(|u| u.to_le_bytes()).collect();
vectorcraft_plugins::permissions::sha256_hex(&bytes)
}
/// The id of the next file a script opens. Ids are unique across runs, so a `File` that a named
/// engine kept from an earlier run does not name a file a later run opened.
static NEXT_HANDLE: AtomicU32 = AtomicU32::new(1);
/// The number in the name of the next run's temporary folder.
static NEXT_TEMP: AtomicU32 = AtomicU32::new(1);
/// Which file dialog a script opened.
#[derive(Clone, Copy, Debug, PartialEq, Eq)]
pub enum PickerKind {
/// `File.openDialog` and `openDlg`: a file to read.
Open,
/// `File.saveDialog` and `saveDlg`: a file to write, which may not exist yet.
Save,
/// `Folder.selectDialog` and `selectDlg`: a folder to read and write.
Folder,
}
impl PickerKind {
/// The kind's name: `open`, `save` or `folder`.
pub fn id(self) -> &'static str {
match self {
Self::Open => "open",
Self::Save => "save",
Self::Folder => "folder",
}
}
}
/// A file dialog a running script waits at: the person picks a file or a folder, then allows the
/// script that one, once or always, or denies it ([`PickAnswer`]).
#[derive(Clone, Debug, PartialEq, Eq)]
pub struct ScriptPicker {
pub kind: PickerKind,
/// The script's prompt, empty when it gave none.
pub prompt: String,
/// An open dialog that may pick more than one file.
pub multiple: bool,
/// The file or folder the dialog starts at, when the script's object gave one.
pub start: Option,
/// The file types the dialog shows, from the script's filter ([`dialog_filters`]): each one's
/// name and extensions (`*` for every file). Empty: every file.
pub filters: Vec<(String, Vec)>,
/// Whether the grant can be kept for later runs: the script was run from a file, and the app
/// has a settings folder for the grants file.
pub can_always: bool,
}
/// The answer to a [`ScriptPicker`].
#[derive(Clone, Debug, PartialEq, Eq)]
pub enum PickAnswer {
/// Cancel or Deny: the script gets `null`.
Deny,
/// The paths picked, allowed for this run, or with `always` for later runs of the same script too.
Allow { paths: Vec, always: bool },
}
/// What a run's `File` and `Folder` objects start with.
#[derive(Clone, Debug, Default)]
pub(crate) struct Setup {
/// The run is in the app, where a person answers its file dialogs.
pub framed: bool,
/// The file the script was read from.
pub script: Option,
/// The script's text with the files it includes, whose SHA-256 the grants it keeps are for.
pub source: String,
/// The folder that holds each script's data folder ([`crate::Session::attach_script_data`]).
pub data_dir: Option,
/// The code the run's named engine kept from its earlier runs: the SHA-256 of each source
/// ([`code_hash`] for evaluated code), and whether there was more than it keeps.
pub engine_code: (Vec, bool),
}
/// A grant the grants file keeps that is in force for the run: the real path, whether it may be
/// written, whether it is a folder's, and the evaluated code the script ran in the run that kept it.
struct Stored {
path: std::path::PathBuf,
write: bool,
folder: bool,
code: std::collections::BTreeSet,
}
/// The script's file state: where it reads and writes, where `Folder.current` starts, the files it
/// has open, how much it has read and written, and the file dialog it waits at.
pub(crate) struct Disk {
scope: ScriptScope,
/// Where the script writes: its temporary and data folders and what was granted for writing.
writes: ScriptScope,
home: String,
start: String,
open: HashMap,
temps: u32,
bytes: usize,
/// The most bytes counted in `bytes` for each file read, by its real path ([`Disk::charge_read`]).
charged: HashMap,
written: usize,
framed: bool,
/// The script's file with its links followed, and the SHA-256 of its text.
script: Option<(std::path::PathBuf, String)>,
/// The SHA-256 of the script's text with the files it includes, also for a script given as
/// text, which its named engine keeps as code the engine holds.
own: String,
data_dir: Option,
/// The folder that holds the settings, with its links followed, which no grant reaches.
fenced: Option,
/// The run's temporary folder and the script's data folder, with their links followed, once made.
temp: Option,
user_data: Option,
/// The file dialog the script waits at.
pub(crate) picker: Option,
/// The error that ended the run when a dialog's answer could not reach the script.
pub(crate) failed: Option,
/// The files written since the host last took them ([`Disk::take_written`]).
wrote: Vec,
/// The places granted at the start (the script's own folder) and by the run itself (its
/// temporary and data folders and what a person allowed in a dialog), each with whether it
/// may be written and whether it is a folder: a withdrawn kept grant leaves these in force.
held: Vec<(std::path::PathBuf, bool, bool)>,
/// The kept grants in force: each is withdrawn once the run evaluates code it was not kept for.
stored: Vec,
/// The SHA-256 of each source other than the script itself that the run's code may run: what
/// it evaluated ([`code_hash`]) and what its named engine kept from earlier runs.
ran: std::collections::BTreeSet,
/// The run evaluated more distinct sources than [`MAX_RUN_CODE`]: no kept grant is in force.
ran_too_much: bool,
/// The grants this run kept with Always (path, write, folder), and how many sources `ran` held
/// when they were last written to the grants file.
kept: Vec<(std::path::PathBuf, bool, bool)>,
recorded: usize,
/// The write bits setting `readonly` cleared from each file in this run, by its device and
/// inode, which a rename keeps, at most [`MAX_ENTRIES`] files: clearing `readonly` gives them back.
#[cfg(unix)]
cleared: HashMap<(u64, u64), u32>,
}
impl Drop for Disk {
fn drop(&mut self) {
// The run's end has flushed these already and reported what it could not write.
let _ = self.flush_all();
if let Some(temp) = self.temp.take() {
let _ = std::fs::remove_dir_all(temp);
}
}
}
impl Disk {
/// The state of a run whose own files are read in `scope`.
pub(crate) fn new(scope: &ScriptScope, setup: Setup) -> Self {
let home = home_folder();
let start = scope.folder().map_or_else(|| home.clone(), |p| p.to_string_lossy().into_owned());
let web = cfg!(target_arch = "wasm32");
let script = setup.script.as_deref().filter(|_| !web).and_then(|p| std::fs::canonicalize(p).ok());
let own = vectorcraft_plugins::permissions::sha256_hex(setup.source.as_bytes());
let script = script.map(|p| (p, own.clone()));
let fenced = setup.data_dir.as_deref().filter(|_| !web).and_then(Path::parent).and_then(real_or_planned);
let held = scope.roots().iter().map(|r| (r.clone(), false, true)).collect();
let mut d = Self {
scope: scope.clone(),
writes: ScriptScope::default(),
home,
start,
open: HashMap::new(),
temps: 0,
bytes: 0,
charged: HashMap::new(),
written: 0,
framed: setup.framed,
script,
own,
data_dir: setup.data_dir,
fenced: fenced.clone(),
temp: None,
user_data: None,
picker: None,
failed: None,
wrote: Vec::new(),
held,
stored: Vec::new(),
ran: std::collections::BTreeSet::new(),
ran_too_much: setup.engine_code.1,
kept: Vec::new(),
recorded: 0,
#[cfg(unix)]
cleared: HashMap::new(),
};
for hash in setup.engine_code.0 {
let _ = d.saw(hash);
}
if let Some(f) = fenced {
// A script's folder inside the settings folder stays open; the settings folder itself
// does not, only the script's own file there.
let roots = d.scope.roots();
let mut own: Vec = roots.iter().filter(|r| !path_inside(&f, r)).cloned().collect();
if roots.iter().any(|r| path_inside(r, &f) && path_inside(&f, r)) {
own.extend(d.script.as_ref().map(|(p, _)| p.clone()));
}
d.scope.fence(f.clone(), &own);
d.writes.fence(f, &[]);
}
if d.framed {
for g in d.stored() {
for (scope, granted) in [(&mut d.writes, g.write), (&mut d.scope, true)] {
match (granted, g.folder) {
(false, _) => {}
(true, true) => scope.add(g.path.clone()),
(true, false) => scope.add_file(g.path.clone()),
}
}
d.stored.push(g);
}
d.withdraw();
}
d
}
/// Notes that the run evaluates the source `units` (`eval`, `Function`, `$.evalFile`), before
/// it runs: the kept grants that were not kept for it are withdrawn.
pub(crate) fn saw_code(&mut self, units: &[u16]) {
if self.saw(code_hash(units)) {
self.withdraw();
}
}
/// Adds the source with SHA-256 `hash` to what the run's code may run. → whether it was new.
fn saw(&mut self, hash: String) -> bool {
if self.ran_too_much || self.ran.contains(&hash) || self.own == hash {
return false;
}
if self.ran.len() >= MAX_RUN_CODE {
self.ran_too_much = true;
} else {
self.ran.insert(hash);
}
true
}
/// What the run's code may run, for its named engine to keep: each source's SHA-256 (the
/// script's own included, also for a script given as text) and whether there was more than
/// [`MAX_RUN_CODE`].
pub(crate) fn code(&self) -> (Vec, bool) {
(std::iter::once(self.own.clone()).chain(self.ran.iter().cloned()).collect(), self.ran_too_much)
}
/// Withdraws each kept grant whose run did not evaluate every source this run has: its place
/// is no longer read or written, unless the run holds it some other way, and files opened for
/// writing there are written now and take no more writes.
fn withdraw(&mut self) {
let (ran, all) = (&self.ran, self.ran_too_much);
let (stay, gone): (Vec, Vec) = std::mem::take(&mut self.stored).into_iter().partition(|g| !all && ran.is_subset(&g.code));
self.stored = stay;
if gone.is_empty() {
return;
}
for g in &gone {
let held = |write: bool| {
self.held.iter().any(|(p, w, f)| *p == g.path && *f == g.folder && (*w || !write))
|| self.stored.iter().any(|s| s.path == g.path && s.folder == g.folder && (s.write || !write))
};
if !held(false) {
self.scope.remove(&g.path, g.folder);
}
if g.write && !held(true) {
self.writes.remove(&g.path, g.folder);
}
}
let mut ids: Vec =
self.open.iter().filter(|(_, o)| o.target.as_deref().is_some_and(|t| !self.writes.allows(t))).map(|(id, _)| *id).collect();
// In the order the files were opened, as `flush_all` writes them.
ids.sort_unstable();
for id in ids {
let written = self.flush(id).is_ok();
if let Some(o) = self.open.get_mut(&id) {
// Changes that could not be written stay, so the run's end tries them again and
// reports the file when it still cannot.
o.withdrawn = true;
if written {
o.target = None;
}
}
}
// Objects that hold a file the run may no longer read give none of it.
for o in self.open.values_mut() {
if o.read_from.as_deref().is_some_and(|p| !self.scope.allows(p)) {
o.unreadable = true;
}
}
}
/// Writes the evaluated code this run ran into the grants it kept with Always, so a later run
/// of the same script that evaluates the same code keeps them in force. A grant that cannot be
/// updated keeps the code it was kept with.
pub(crate) fn record_code(&mut self) {
if self.kept.is_empty() || self.ran_too_much || self.ran.len() == self.recorded {
return;
}
let (Some((script, hash)), Some(store)) = (&self.script, self.store_path()) else { return };
let (script, code) = (shown(script), self.ran.iter().cloned().collect::>());
let Ok(_lock) = lock_store(&store) else { return };
let Ok(mut grants) = try_read_store(&store) else { return };
for g in &mut grants {
let ours = g["script"] == *script
&& g["sha256"] == *hash
&& self.kept.iter().any(|(p, w, f)| g["path"] == *shown(p) && g["write"] == *w && g["folder"] == *f);
if ours {
g["code"] = serde_json::json!(code);
}
}
if write_store(&store, grants).is_ok() {
self.recorded = self.ran.len();
}
}
/// The gate for a command the script runs: the places it reads and writes now, and the files of
/// the open documents, `documents`.
pub(crate) fn gate(&self, documents: Vec) -> crate::file_access::ScriptGate {
crate::file_access::ScriptGate { read: self.scope.clone(), write: self.writes.clone(), documents }
}
/// The files written since this was last asked, to name in the report.
pub(crate) fn take_written(&mut self) -> Vec {
std::mem::take(&mut self.wrote)
}
fn wrote(&mut self, real: &Path) {
let shown = crate::cmd::findfiles::plain(real.to_path_buf()).to_string_lossy().into_owned();
if !self.wrote.contains(&shown) {
self.wrote.push(shown);
}
}
/// Grants the real path `real`, a folder with everything in it (`folder`) or a file, for
/// reading, and with `write` for writing too.
fn grant(&mut self, real: std::path::PathBuf, write: bool, folder: bool) {
let scopes = if write { vec![&mut self.writes, &mut self.scope] } else { vec![&mut self.scope] };
for scope in scopes {
if folder { scope.add(real.clone()) } else { scope.add_file(real.clone()) }
}
self.held.push((real, write, folder));
}
/// The run's temporary folder, made the first time it is asked for; `None` when it cannot be
/// made, when the automation write roots do not take it, and on the web.
fn temp_dir(&mut self) -> Option {
if cfg!(target_arch = "wasm32") {
return None;
}
if self.temp.is_none() {
let base = std::env::temp_dir();
let made = (0..64).find_map(|_| {
let n = NEXT_TEMP.fetch_add(1, Ordering::Relaxed);
let dir = base.join(format!("VectorCraft Script {}-{n}", std::process::id()));
crate::file_access::check_write(&dir.to_string_lossy()).ok()?;
std::fs::create_dir(&dir).ok().and_then(|()| std::fs::canonicalize(&dir).ok())
})?;
self.grant(made.clone(), true, true);
self.temp = Some(made);
}
self.temp.clone().map(crate::cmd::findfiles::plain)
}
/// The script's data folder, made the first time it is asked for: for a script run from a file,
/// a folder named by the SHA-256 of its path in the data folder, and otherwise a folder in the
/// run's temporary folder. `None` when that folder is a link, cannot be made, or is outside the
/// automation write roots.
fn user_data_dir(&mut self) -> Option {
if cfg!(target_arch = "wasm32") {
return None;
}
if self.user_data.is_none() {
let dir = match (&self.script, &self.data_dir) {
(Some((path, _)), Some(data)) => {
let key = vectorcraft_plugins::permissions::sha256_hex(path.to_string_lossy().as_bytes());
data.join(key.get(..16).unwrap_or(&key))
}
_ => self.temp_dir()?.join("userData"),
};
crate::file_access::check_write(&dir.to_string_lossy()).ok()?;
// Each missing folder above it that is made on the way must be inside the write roots too.
for missing in dir.ancestors().skip(1).take_while(|a| !a.as_os_str().is_empty() && std::fs::symlink_metadata(a).is_err()) {
crate::file_access::check_write(&missing.to_string_lossy()).ok()?;
}
// The data folder that holds the folder, and then the folder, are folders where they are
// named, not links; the folder is made only once the data folder is checked.
let direct = |d: &Path| -> Option {
let (parent, name) = (std::fs::canonicalize(d.parent()?).ok()?, d.file_name()?);
let real = std::fs::canonicalize(d).ok()?;
(real == parent.join(name) && std::fs::symlink_metadata(d).is_ok_and(|m| m.is_dir())).then_some(real)
};
std::fs::create_dir_all(dir.parent()?).ok()?;
direct(dir.parent()?)?;
std::fs::create_dir_all(&dir).ok()?;
let real = direct(&dir)?;
self.scope.fence_open(real.clone());
self.writes.fence_open(real.clone());
self.grant(real.clone(), true, true);
self.user_data = Some(real);
}
self.user_data.clone().map(crate::cmd::findfiles::plain)
}
/// The grants file, beside the scripts' data folders.
fn store_path(&self) -> Option {
self.data_dir.as_ref().map(|d| d.with_file_name(GRANTS_FILE))
}
/// The grants the grants file keeps for this script with this text: each path, whether it may
/// be written, whether it is a folder, and the evaluated code it was kept for. A grant whose
/// path no longer is its own real path is left out.
fn stored(&self) -> Vec {
let (Some((script, hash)), Some(store)) = (&self.script, self.store_path()) else { return Vec::new() };
let script = shown(script);
read_store(&store)
.into_iter()
.filter(|g| g["script"] == *script && g["sha256"] == *hash)
.filter_map(|g| {
let code = g["code"].as_array()?.iter().map(|c| c.as_str().map(str::to_string)).collect::