file_system class provides a virtual file system layer that translates Windows paths to host filesystem paths, enabling safe and controlled file access in the emulator.
Overview
This class provides:- Windows-to-host path translation
- Drive letter emulation
- Path sandboxing to prevent escape attacks
- Custom path mappings
- Cross-platform support (Windows and Unix hosts)
Constructor
const std::filesystem::path&
Root directory for the virtual file system. All Windows paths will be resolved relative to this directory.
root is empty, the file system operates in passthrough mode, allowing direct access to the real Windows file system.
Methods
list_drives
Lists available drive letters.- On Windows with empty root: Returns actual system drives via
GetLogicalDrives() - Otherwise: Returns subdirectories in the root that are single characters
translate
Translates a Windows path to a host filesystem path.const windows_path&
Windows path to translate (must be absolute)
std::runtime_error if the path is not absolute.
Translation process:
- Check custom mappings first
- On Windows with empty root: Return path as-is
- Otherwise: Map to
<root>/<drive>/<path> - Prevent directory traversal attacks by checking for escape sequences
map
Creates a custom path mapping.windows_path
Source Windows path
std::filesystem::path
Destination host path
- Redirecting system directories
- Mapping specific files to host locations
- Creating virtual files or directories
access_mapped_entries
Iterates over mapped entries within a directory.const windows_path&
Directory path to search
const F&
Callback function invoked for each mapped child entry
std::pair<const windows_path&, const std::filesystem::path&> for each mapping.
Static Methods
is_escaping_relative_path
Checks if a relative path attempts to escape its parent directory.const std::filesystem::path&
Path to check
true if the path is empty or starts with ”..“.
is_subpath
Checks if a path is a subpath of a root directory.const std::filesystem::path&
Normalized root path
const std::filesystem::path&
Normalized target path to check
true if normal_target is within normal_root.
This method is used internally to prevent directory traversal attacks.
Usage Examples
Basic Usage
Custom Mappings
Windows Host Passthrough
Directory Enumeration with Mappings
Security: Preventing Path Traversal
Directory Structure Example
For a file system rooted at/tmp/windows, the directory structure would be:
Integration Example
See Also
- module_manager - Uses file_system for DLL resolution
- process_context - Uses file_system during setup