Skip to main content

VirtualFileSystem

Struct VirtualFileSystem 

Source
pub struct VirtualFileSystem { /* private fields */ }
Expand description

Virtual filesystem manager for lessons

Implementations§

Source§

impl VirtualFileSystem

Source

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)

Source

pub fn empty(lesson_id: &str, session_id: &str) -> Result<Self>

Create an empty virtual filesystem on disk: just the lesson home

Source

pub fn in_memory() -> Self

Create an empty virtual filesystem held in memory: just the lesson home. Nothing is ever written to disk.

Source

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.

Source

pub fn for_lesson_in_memory(lesson: &Lesson) -> Result<Self>

Source

pub fn is_in_memory(&self) -> bool

Whether this sandbox is held in memory (rather than on disk)

Source

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

Source

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.

Source

pub fn get_current_dir(&self) -> &Path

Get current directory (virtual path)

Source

pub fn get_current_dir_real(&self) -> PathBuf

Get current directory (real filesystem path; the virtual path for a sandbox held in memory)

Source

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)

Source

pub fn exists(&self, path: &str) -> bool

Something exists at this path

Source

pub fn is_dir(&self, path: &str) -> bool

A folder exists at this path

Source

pub fn is_file(&self, path: &str) -> bool

A file exists at this path

Source

pub fn change_directory(&mut self, path: &str) -> Result<String>

Change directory (returns new virtual path)

Source

pub fn list_directory(&self, path: Option<&str>) -> Result<Vec<DirEntry>>

List directory contents

Source

pub fn get_tree(&self, max_depth: usize) -> Vec<TreeNode>

Get directory tree structure

Source

pub fn read_file(&self, path: &str) -> Result<String>

Read file contents (cat command)

Source

pub fn read_bytes(&self, path: &str) -> Result<Vec<u8>>

Read a file’s raw bytes

Source

pub fn write_bytes(&self, path: &str, bytes: &[u8]) -> Result<()>

Write raw bytes to a file, replacing what it held

Source

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.

Source

pub fn create_directory(&self, path: &str, parents: bool) -> Result<()>

Create directory (mkdir command)

Source

pub fn touch_file(&self, path: &str) -> Result<()>

Create or update file (touch command)

Source

pub fn remove(&self, path: &str, recursive: bool, force: bool) -> Result<()>

Remove file or directory (rm command)

Source

pub fn move_item(&self, source: &str, destination: &str) -> Result<()>

Move or rename file/directory (mv command)

Source

pub fn copy( &self, source: &str, destination: &str, recursive: bool, ) -> Result<()>

Copy file or directory (cp command)

Source

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.

Source

pub fn head_file(&self, path: &str, n: usize) -> Result<String>

Return the first n lines of a file (head command)

Source

pub fn tail_file(&self, path: &str, n: usize) -> Result<String>

Return the last n lines of a file (tail command)

Source

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).

Source

pub fn write_file(&self, path: &str, content: &str, append: bool) -> Result<()>

Write content to file (for echo redirection, etc.)

Source

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

pub fn cleanup(&self) -> Result<()>

Clean up virtual filesystem

Source§

impl VirtualFileSystem

Source

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.

Source

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

Source

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

Source

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.

Source

pub fn tar_list(&self, archive: &str) -> Result<Vec<String>>

tar -t: list an archive

Source

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.

Source

pub fn gzip(&self, path: &str) -> Result<String>

gzip FILE: compress to FILE.gz and remove FILE

Source

pub fn gunzip(&self, path: &str) -> Result<String>

gunzip FILE.gz: decompress to FILE and remove FILE.gz

Source

pub fn practice_ssh_key( &self, path: &str, kind: &str, comment: &str, ) -> Result<(String, String)>

ssh-keygen -f PATH -C COMMENT: write a PRACTICE key pair. The files have the real shape (a private key readable only by you, a one-line public key) but hold no key material.

Trait Implementations§

Source§

impl Drop for VirtualFileSystem

Source§

fn drop(&mut self)

Executes the destructor for this type. Read more
Source§

fn pin_drop(self: Pin<&mut Self>)

🔬This is a nightly-only experimental API. (pin_ergonomics)
Execute the destructor for this type, but different to Drop::drop, it requires self to be pinned. Read more
Source§

impl PracticeWorld for VirtualFileSystem

Source§

fn locate(&self, lesson_path: &str) -> Option<PathBuf>

Real path for a path relative to the lesson home, if it stays inside
Source§

fn cwd_relative(&self) -> String

Current directory relative to the lesson home (“” at the home)
Source§

fn kind_at(&self, lesson_path: &str) -> Option<EntryKind>

What is at a path relative to the lesson home; None when the path would leave it. By default, the real file PracticeWorld::locate gives.
Source§

fn read_text(&self, lesson_path: &str) -> Option<String>

A text file’s contents, by a path relative to the lesson home. By default, read from the real file PracticeWorld::locate gives.

Auto Trait Implementations§

Blanket Implementations§

Source§

impl<T> Any for T
where T: 'static + ?Sized,

Source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
Source§

impl<T> Borrow<T> for T
where T: ?Sized,

Source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
Source§

impl<T> BorrowMut<T> for T
where T: ?Sized,

Source§

fn borrow_mut(&mut self) -> &mut T

Mutably borrows from an owned value. Read more
Source§

impl<T> From<T> for T

Source§

fn from(t: T) -> T

Returns the argument unchanged.

Source§

impl<T> Instrument for T

Source§

fn instrument(self, span: Span) -> Instrumented<Self> ⓘ

Instruments this type with the provided Span, returning an Instrumented wrapper. Read more
Source§

fn in_current_span(self) -> Instrumented<Self> ⓘ

Instruments this type with the current Span, returning an Instrumented wrapper. Read more
Source§

impl<T, U> Into<U> for T
where U: From<T>,

Source§

fn into(self) -> U

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

Source§

impl<T, U> TryFrom<U> for T
where U: Into<T>,

Source§

type Error = !

The type returned in the event of a conversion error.
Source§

fn try_from(value: U) -> Result<T, !>

Performs the conversion.
Source§

impl<T, U> TryInto<U> for T
where U: TryFrom<T>,

Source§

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.
Source§

fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>

Performs the conversion.
Source§

impl<T> WithSubscriber for T

Source§

fn with_subscriber<S>(self, subscriber: S) -> WithDispatch<Self> ⓘ
where S: Into<Dispatch>,

Attaches the provided Subscriber to this type, returning a WithDispatch wrapper. Read more
Source§

fn with_current_subscriber(self) -> WithDispatch<Self> ⓘ

Attaches the current default Subscriber to this type, returning a WithDispatch wrapper. Read more