pub struct VirtualFileSystem { /* private fields */ }Expand description
Virtual filesystem manager for lessons
Implementations§
Source§impl VirtualFileSystem
impl VirtualFileSystem
Sourcepub fn new(lesson_id: &str, session_id: &str) -> Result<Self>
pub fn new(lesson_id: &str, session_id: &str) -> Result<Self>
Create a new virtual filesystem for a lesson, on disk, holding the
generic practice home (see VirtualFileSystem::seed_generic)
Sourcepub fn empty(lesson_id: &str, session_id: &str) -> Result<Self>
pub fn empty(lesson_id: &str, session_id: &str) -> Result<Self>
Create an empty virtual filesystem on disk: just the lesson home
Sourcepub fn in_memory() -> Self
pub fn in_memory() -> Self
Create an empty virtual filesystem held in memory: just the lesson home. Nothing is ever written to disk.
Sourcepub fn for_lesson(lesson: &Lesson, session_id: &str) -> Result<Self>
pub fn for_lesson(lesson: &Lesson, session_id: &str) -> Result<Self>
A sandbox on disk for lesson: its own setup files, plus the
generic practice home for Operator lessons. Explorer lessons see
only the files they set up. A setup file that can’t be written is
logged and skipped.
Sourcepub fn for_lesson_in_memory(lesson: &Lesson) -> Result<Self>
pub fn for_lesson_in_memory(lesson: &Lesson) -> Result<Self>
VirtualFileSystem::for_lesson, held in memory
Sourcepub fn is_in_memory(&self) -> bool
pub fn is_in_memory(&self) -> bool
Whether this sandbox is held in memory (rather than on disk)
Sourcepub fn seed_generic(&self) -> Result<()>
pub fn seed_generic(&self) -> Result<()>
Seed the generic practice home: Documents, Downloads, Pictures,
Music, Videos, projects and a .bashrc, with a few sample files
Sourcepub fn resolve_path(&self, virtual_path: &str) -> Result<PathBuf>
pub fn resolve_path(&self, virtual_path: &str) -> Result<PathBuf>
Get the real filesystem path for a virtual path.
All ./.. components are resolved lexically and the result is
guaranteed to be contained within the sandbox root; paths that would
escape it (e.g. ../../../../etc/hostname) return an error. A
sandbox held in memory has no real paths: it returns the normalized
virtual path (/lesson-home/...), so use VirtualFileSystem::metadata
and friends rather than std::fs on the result.
Sourcepub fn get_current_dir(&self) -> &Path
pub fn get_current_dir(&self) -> &Path
Get current directory (virtual path)
Sourcepub fn get_current_dir_real(&self) -> PathBuf
pub fn get_current_dir_real(&self) -> PathBuf
Get current directory (real filesystem path; the virtual path for a sandbox held in memory)
Sourcepub fn metadata(&self, path: &str) -> Option<Meta>
pub fn metadata(&self, path: &str) -> Option<Meta>
What is at a path, or None when nothing is (or the path would
leave the sandbox)
Sourcepub fn change_directory(&mut self, path: &str) -> Result<String>
pub fn change_directory(&mut self, path: &str) -> Result<String>
Change directory (returns new virtual path)
Sourcepub fn list_directory(&self, path: Option<&str>) -> Result<Vec<DirEntry>>
pub fn list_directory(&self, path: Option<&str>) -> Result<Vec<DirEntry>>
List directory contents
Sourcepub fn write_bytes(&self, path: &str, bytes: &[u8]) -> Result<()>
pub fn write_bytes(&self, path: &str, bytes: &[u8]) -> Result<()>
Write raw bytes to a file, replacing what it held
Sourcepub fn set_mode(&self, path: &str, mode: u32) -> Result<()>
pub fn set_mode(&self, path: &str, mode: u32) -> Result<()>
Set a path’s permission bits. On a platform without them (a disk sandbox outside Unix) this does nothing.
Sourcepub fn create_directory(&self, path: &str, parents: bool) -> Result<()>
pub fn create_directory(&self, path: &str, parents: bool) -> Result<()>
Create directory (mkdir command)
Sourcepub fn touch_file(&self, path: &str) -> Result<()>
pub fn touch_file(&self, path: &str) -> Result<()>
Create or update file (touch command)
Sourcepub fn remove(&self, path: &str, recursive: bool, force: bool) -> Result<()>
pub fn remove(&self, path: &str, recursive: bool, force: bool) -> Result<()>
Remove file or directory (rm command)
Sourcepub fn move_item(&self, source: &str, destination: &str) -> Result<()>
pub fn move_item(&self, source: &str, destination: &str) -> Result<()>
Move or rename file/directory (mv command)
Sourcepub fn copy(
&self,
source: &str,
destination: &str,
recursive: bool,
) -> Result<()>
pub fn copy( &self, source: &str, destination: &str, recursive: bool, ) -> Result<()>
Copy file or directory (cp command)
Sourcepub fn grep_file(
&self,
pattern: &str,
path: &str,
case_insensitive: bool,
) -> Result<Vec<(usize, String)>>
pub fn grep_file( &self, pattern: &str, path: &str, case_insensitive: bool, ) -> Result<Vec<(usize, String)>>
Search a file for lines containing pattern (grep command).
Returns the matching lines as (line_number, line) pairs (line
numbers are 1-based). Matching is plain substring search; pass
case_insensitive for -i behavior.
Sourcepub fn head_file(&self, path: &str, n: usize) -> Result<String>
pub fn head_file(&self, path: &str, n: usize) -> Result<String>
Return the first n lines of a file (head command)
Sourcepub fn tail_file(&self, path: &str, n: usize) -> Result<String>
pub fn tail_file(&self, path: &str, n: usize) -> Result<String>
Return the last n lines of a file (tail command)
Sourcepub fn wc_file(&self, path: &str) -> Result<(usize, usize, usize)>
pub fn wc_file(&self, path: &str) -> Result<(usize, usize, usize)>
Count lines, words and bytes of a file (wc command).
Returns (lines, words, bytes).
Sourcepub fn write_file(&self, path: &str, content: &str, append: bool) -> Result<()>
pub fn write_file(&self, path: &str, content: &str, append: bool) -> Result<()>
Write content to file (for echo redirection, etc.)
Sourcepub fn seed_setup(&self, files: &[SetupFile]) -> Result<()>
pub fn seed_setup(&self, files: &[SetupFile]) -> Result<()>
Seed lesson starter files (the lesson’s setup list) into the
sandbox, creating parent directories as needed. Paths are resolved
through the same containment checks as every other operation, so a
malicious setup path cannot escape the sandbox.
Source§impl VirtualFileSystem
impl VirtualFileSystem
Sourcepub fn glob(&self, pattern: &str) -> Vec<String>
pub fn glob(&self, pattern: &str) -> Vec<String>
Expand a glob against the sandbox (*.txt, logs/day?.log).
Only the last part of the path may hold a pattern. Returns the
sorted matches, or nothing when none match.
Sourcepub fn find(&self, start: &str, query: &FindQuery) -> Result<Vec<String>>
pub fn find(&self, start: &str, query: &FindQuery) -> Result<Vec<String>>
find: walk each start path and list what matches, as paths
written from the start path the way it was typed
Sourcepub fn disk_usage(
&self,
path: &str,
summarize: bool,
) -> Result<Vec<(String, u64)>>
pub fn disk_usage( &self, path: &str, summarize: bool, ) -> Result<Vec<(String, u64)>>
du: size in bytes of each folder under path (deepest first),
or only path itself when summarize
Sourcepub fn tar_create(
&self,
archive: &str,
inputs: &[String],
gzip: bool,
) -> Result<Vec<String>>
pub fn tar_create( &self, archive: &str, inputs: &[String], gzip: bool, ) -> Result<Vec<String>>
tar -c: pack files and folders into an archive. Returns the
entries written, for -v.
Sourcepub fn tar_extract(&self, archive: &str, dest: &str) -> Result<Vec<String>>
pub fn tar_extract(&self, archive: &str, dest: &str) -> Result<Vec<String>>
tar -x: unpack into dest (files and folders only; nothing may
land outside dest). Returns the entries written.
Sourcepub fn gzip(&self, path: &str) -> Result<String>
pub fn gzip(&self, path: &str) -> Result<String>
gzip FILE: compress to FILE.gz and remove FILE