Mountain/Environment/
FileSystemProvider.rs

1//! # FileSystemProvider (Environment)
2//!
3//! RESPONSIBILITIES:
4//! - Implements
5//!   [`FileSystemReader`](CommonLibrary::FileSystem::FileSystemReader) and
6//!   [`FileSystemWriter`](CommonLibrary::FileSystem::FileSystemWriter) for
7//! `MountainEnvironment`
8//! - Provides secure, validated filesystem access with workspace trust
9//!   enforcement
10//! - Handles file operations: read, write, stat, delete, rename, copy,
11//!   directory traversal
12//! - Detects and handles symbolic links properly
13//! - Enforces path validation to prevent directory traversal attacks
14//!
15//! SECURITY MODEL:
16//! - Sandboxed filesystem access limited to registered workspace folders
17//! - All operations call `Utility::IsPathAllowedForAccess`
18//!   first
19//! - Requires workspace trust to be enabled for any file access
20//! - Path normalization prevents `../` attacks
21//! - Symbolic link detection avoids following untrusted links outside
22//!   workspaces
23//!
24//! ERROR HANDLING:
25//! - Uses [`CommonError`](CommonLibrary::Error::CommonError) for all operations
26//! - File operation errors are mapped via `CommonError::FromStandardIOError`
27//! - Validates paths are within workspace boundaries (IsPathAllowedForAccess)
28//! - Rejects directory reads when file expected (ReadFile)
29//!
30//! PERFORMANCE:
31//! - Uses async tokio::fs for non-blocking I/O operations
32//! - Symbolic link detection uses `symlink_metadata` in addition to `metadata`
33//! - TODO: Consider caching file metadata for frequently accessed files
34//!
35//! VS CODE REFERENCE:
36//! - `vs/workbench/services/files/electron-browser/diskFileSystemProvider.ts` -
37//!   secure FS access
38//! - `vs/platform/files/common/files.ts` - file system interfaces
39//! - `vs/base/common/network.ts` - URI and path handling
40//!
41//! TODO:
42//! - Implement filesystem change watching (notify, inotify, FSEvents)
43//! - Add path normalization to prevent directory traversal
44//! - Implement proper symbolic link resolution with security checks
45//! - Add support for file permissions and ownership metadata
46//! - Implement atomic file writes using temp file + rename pattern
47//! - Add filesystem usage statistics (disk space, file counts)
48//! - Implement file attribute querying (hidden, readonly, executable)
49//! - Add support for extended file attributes on Unix/macOS
50//! - Consider adding filesystem cache for metadata
51//! - Implement trash operation using platform trash API (not delete)
52//! - Add support for file system encoding detection
53//! - Implement case sensitivity handling based on filesystem type
54//!
55//! MODULE STRUCTURE:
56//! - [`read_operations.rs`](read_operations.rs) - `FileSystemReader`
57//!   implementation
58//! - [`write_operations.rs`](write_operations.rs) - `FileSystemWriter`
59//!   implementation
60
61use std::path::PathBuf;
62
63use CommonLibrary::{
64	Error::CommonError::CommonError,
65	FileSystem::{
66		DTO::{FileSystemStatDTO::FileSystemStatDTO, FileTypeDTO::FileTypeDTO},
67		FileSystemReader::FileSystemReader,
68		FileSystemWriter::FileSystemWriter,
69	},
70};
71use async_trait::async_trait;
72
73use super::{MountainEnvironment::MountainEnvironment, Utility};
74
75// Private submodules containing the actual implementation
76#[path = "FileSystemProvider/read_operations.rs"]
77mod read_operations;
78#[path = "FileSystemProvider/write_operations.rs"]
79mod write_operations;
80
81#[async_trait]
82impl FileSystemReader for MountainEnvironment {
83	/// Delegates to read_operations module
84	async fn ReadFile(&self, path:&PathBuf) -> Result<Vec<u8>, CommonError> {
85		read_operations::read_file_impl(self, path).await
86	}
87
88	/// Delegates to read_operations module
89	async fn StatFile(&self, path:&PathBuf) -> Result<FileSystemStatDTO, CommonError> {
90		read_operations::stat_file_impl(self, path).await
91	}
92
93	/// Delegates to read_operations module
94	async fn ReadDirectory(&self, path:&PathBuf) -> Result<Vec<(String, FileTypeDTO)>, CommonError> {
95		read_operations::read_directory_impl(self, path).await
96	}
97}
98
99#[async_trait]
100impl FileSystemWriter for MountainEnvironment {
101	/// Delegates to write_operations module
102	async fn WriteFile(&self, path:&PathBuf, content:Vec<u8>, create:bool, overwrite:bool) -> Result<(), CommonError> {
103		write_operations::write_file_impl(self, path, content, create, overwrite).await
104	}
105
106	/// Delegates to write_operations module
107	async fn CreateDirectory(&self, path:&PathBuf, recursive:bool) -> Result<(), CommonError> {
108		write_operations::create_directory_impl(self, path, recursive).await
109	}
110
111	/// Delegates to write_operations module
112	async fn Delete(&self, path:&PathBuf, recursive:bool, use_trash:bool) -> Result<(), CommonError> {
113		write_operations::delete_impl(self, path, recursive, use_trash).await
114	}
115
116	/// Delegates to write_operations module
117	async fn Rename(&self, source:&PathBuf, target:&PathBuf, overwrite:bool) -> Result<(), CommonError> {
118		write_operations::rename_impl(self, source, target, overwrite).await
119	}
120
121	/// Delegates to write_operations module
122	async fn Copy(&self, source:&PathBuf, target:&PathBuf, overwrite:bool) -> Result<(), CommonError> {
123		write_operations::copy_impl(self, source, target, overwrite).await
124	}
125
126	/// Delegates to write_operations module
127	async fn CreateFile(&self, path:&PathBuf) -> Result<(), CommonError> {
128		write_operations::create_file_impl(self, path).await
129	}
130}