nsh_core/filesystem.rs
1//! Host filesystem services for platforms with virtual app storage.
2use std::{future::Future, pin::Pin};
3
4use crate::ui::JsError;
5
6/// A host filesystem confined to one app's private data root.
7/// Requests contain normalized relative paths; implementations must not
8/// follow links outside that root and must enforce the supplied read bounds.
9pub trait FileSystem: Send + Sync {
10 /// Runs an operation without blocking the engine thread.
11 fn execute(
12 &self,
13 request: FileRequest,
14 ) -> Pin<Box<dyn Future<Output = Result<FileResponse, JsError>> + 'static>>;
15}
16/// One capability-checked filesystem request.
17pub struct FileRequest {
18 /// Path relative to the app's private root; empty denotes the root.
19 pub path: String,
20 /// Work to perform.
21 pub operation: FileOperation,
22}
23/// Filesystem operations provided by a host.
24pub enum FileOperation {
25 /// Read a regular file, refusing larger files before allocating them.
26 Read {
27 /// Maximum returned byte count.
28 max_bytes: usize,
29 },
30 /// Replace a regular file's contents.
31 Write(Vec<u8>),
32 /// List entries, stopping before either bound is exceeded.
33 Entries {
34 /// Maximum number of entries.
35 max_entries: usize,
36 /// Maximum total UTF-8 name bytes.
37 max_name_bytes: usize,
38 },
39 /// Test existence.
40 Exists,
41 /// Create a directory.
42 Mkdir {
43 /// Create missing parents too.
44 recursive: bool,
45 },
46 /// Remove a file.
47 Unlink,
48 /// Remove an empty directory.
49 Rmdir,
50 /// Read metadata, optionally for a link itself.
51 Stat {
52 /// Inspect the link itself.
53 link: bool,
54 },
55 /// Move to another normalized relative path.
56 Rename {
57 /// Destination relative to the same app root.
58 to: String,
59 },
60 /// Read a link's target.
61 Readlink,
62}
63/// A filesystem operation's result.
64pub enum FileResponse {
65 /// Completed mutation.
66 Unit,
67 /// File contents.
68 Bytes(Vec<u8>),
69 /// Directory entries.
70 Entries(Vec<FileEntry>),
71 /// Existence result.
72 Exists(bool),
73 /// Entry metadata.
74 Metadata(FileMetadata),
75 /// A link's target.
76 Link(String),
77}
78/// One directory entry.
79pub struct FileEntry {
80 /// Its base name.
81 pub name: String,
82 /// Whether it is a directory.
83 pub directory: bool,
84}
85/// Metadata represented by the standard `Stats` interface.
86pub struct FileMetadata {
87 /// Size in bytes.
88 pub size: u64,
89 /// Type and permission bits.
90 pub mode: u32,
91 /// Last modification, milliseconds since the epoch.
92 pub mtime_ms: f64,
93 /// Last access, milliseconds since the epoch.
94 pub atime_ms: f64,
95 /// Creation, milliseconds since the epoch.
96 pub birthtime_ms: f64,
97 /// Regular file.
98 pub file: bool,
99 /// Directory.
100 pub directory: bool,
101 /// Symbolic link.
102 pub symlink: bool,
103}