pub fn write(&mut self, path: &str, data: Vec<u8>) -> Result<()> { let p = VfsPath::normalize(path); self.check_write_allowed(&p)?; // Read-only mount check // Ensure parent directory exists let parent = VfsPath::parent(&p); if !self.is_dir(&parent) { bail!("no such directory"); } // Try host mount first if let Some(mp) = self.find_mount_mut(&p) { let host_path = /* map to host */; return std::fs::write(&host_path, &data); } // Store in memory self.nodes.insert(p, FsNode::File(data)); Ok(())}
// Create all parent directoriespub fn mkdir_p(&mut self, path: &str) -> Result<()> { let p = VfsPath::normalize(path); let mut current = String::new(); for component in p.split('/') { if component.is_empty() { current.push('/'); continue; } current = format!("{}/{}", current, component); if !self.nodes.contains_key(¤t) { self.nodes.insert(current.clone(), FsNode::Directory(HashMap::new())); } } Ok(())}
pub fn list_dir(&self, path: &str) -> Result<Vec<DirEntry>> { let p = VfsPath::normalize(path); let prefix = format!("{}/", p); let mut entries = Vec::new(); // Scan in-memory nodes for key in self.nodes.keys() { if key.starts_with(&prefix) { let name = key.strip_prefix(&prefix).unwrap(); if !name.contains('/') { // Direct child only entries.push(DirEntry { name, is_dir }); } } } // Merge host mount entries if present if let Some(mp) = self.find_mount(&p) { // ... read std::fs::read_dir and merge } entries.sort(); Ok(entries)}
Full list of VFS operations
exists(path) — Check if path exists
is_dir(path) — Check if path is directory
read(path) — Read file as bytes
read_to_string(path) — Read file as UTF-8 string
write(path, data) — Overwrite or create file
write_str(path, s) — Write UTF-8 string
append(path, data) — Append to file
mkdir(path) — Create directory (parent must exist)