Back
+30 XP
Concept

In-Memory Virtual File System (VFS) Tree

Testing complex disk-intensive operations against physical drives is slow, creates permission flakiness, and litters the host OS with leftover temp files. An in-memory Virtual File System (VFS) simulates directories and byte files in memory trees for blisteringly fast, isolated operations.

python
# Tree node representation:
node = {
    'type': 'dir', # or 'file'
    'children': {},
    'content': b''
}

# In-Memory Virtual File System (VFS) Tree Running 5,000 unit tests that write real files to `/tmp` wears out developer SSDs, causes test flakiness when files collide, and drags test suite execution from 2 seconds to 40 seconds. 🏎️🌳 A **Virtual File System (VFS)** implements the POSIX filesystem contract entirely within Python memory. Directories are nested dictionaries, files are byte arrays, and path resolution (`/usr/local/bin`) traverses the tree in memory at nanosecond speeds. ```python class VFS: def __init__(self): self.root = {"type": "dir", "children": {}} ``` ## Pop Quiz Why do testing suites and embedded interpreters prefer in-memory VFS mocks over physical temporary folders? - [ ] In-memory trees run in RAM with zero disk I/O latency, perfect isolation, and instantaneous teardown. - [ ] Physical SSD drives refuse more than 100 file creation requests per second. - [ ] Operating systems disable write caching for files stored in /tmp. - [ ] Because Python garbage collection cannot deallocate physical disk files. ## Real-World Challenge Implement `InMemoryVFS`: - `mkdir(path: str)`: Creates directory node. Raises `FileExistsError` if already existing, `FileNotFoundError` if intermediate parents are missing. - `write_file(path: str, data: bytes)`: Creates or overwrites file node. Creates missing parent directories automatically. - `read_file(path: str) -> bytes`: Returns raw bytes. Raises `FileNotFoundError` if missing, or `IsADirectoryError` if path is a directory. - `list_dir(path: str) -> list[str]`: Returns sorted list of names inside directory. <details> <summary>Solution</summary> ```python class InMemoryVFS: def __init__(self): self._root = {"type": "dir", "children": {}} def _resolve(self, path: str, create_parents: bool = False): parts = [p for p in path.strip("/").split("/") if p] curr = self._root for idx, segment in enumerate(parts[:-1]): if segment not in curr["children"]: if create_parents: curr["children"][segment] = {"type": "dir", "children": {}} else: raise FileNotFoundError(f"Parent directory '{segment}' not found") curr = curr["children"][segment] if curr["type"] != "dir": raise NotADirectoryError(f"'{segment}' is a file, not a directory") target_name = parts[-1] if parts else "" return curr, target_name def mkdir(self, path: str): parent, name = self._resolve(path, create_parents=False) if not name: return # Root directory already exists if name in parent["children"]: raise FileExistsError(f"Cannot create directory '{name}': file exists") parent["children"][name] = {"type": "dir", "children": {}} def write_file(self, path: str, data: bytes): parent, name = self._resolve(path, create_parents=True) parent["children"][name] = {"type": "file", "content": bytes(data)} def read_file(self, path: str) -> bytes: parent, name = self._resolve(path, create_parents=False) if name not in parent["children"]: raise FileNotFoundError(f"File '{name}' not found") node = parent["children"][name] if node["type"] != "file": raise IsADirectoryError(f"'{name}' is a directory") return node["content"] def list_dir(self, path: str) -> list[str]: parts = [p for p in path.strip("/").split("/") if p] curr = self._root for segment in parts: if segment not in curr["children"]: raise FileNotFoundError(f"Directory '{segment}' not found") curr = curr["children"][segment] if curr["type"] != "dir": raise NotADirectoryError(f"'{path}' is a file") return sorted(curr["children"].keys()) # Verification vfs = InMemoryVFS() vfs.mkdir("/etc") vfs.write_file("/etc/nginx.conf", b"worker_processes 4;") vfs.write_file("/var/log/syslog", b"System booted cleanly.") assert vfs.list_dir("/etc") == ["nginx.conf"] assert vfs.list_dir("/var") == ["log"] assert vfs.list_dir("/var/log") == ["syslog"] assert vfs.read_file("/etc/nginx.conf") == b"worker_processes 4;" # Error handling test is_dir_err = False try: vfs.read_file("/etc") except IsADirectoryError: is_dir_err = True assert is_dir_err is True ``` </details>