-
Notifications
You must be signed in to change notification settings - Fork 182
Expand file tree
/
Copy pathfs.rs
More file actions
322 lines (283 loc) · 11.1 KB
/
fs.rs
File metadata and controls
322 lines (283 loc) · 11.1 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
use std::{
fs::File,
hash::Hasher as _,
io::{self, BufRead, Read},
sync::Arc,
};
use dashmap::DashMap;
use vite_path::{AbsolutePath, AbsolutePathBuf};
use vite_str::Str;
use crate::{
Error,
collections::HashMap,
execute::PathRead,
fingerprint::{DirEntryKind, PathFingerprint},
};
pub trait FileSystem: Sync {
fn fingerprint_path(
&self,
path: &Arc<AbsolutePath>,
read: PathRead,
) -> Result<PathFingerprint, Error>;
}
#[derive(Debug, Default)]
pub struct RealFileSystem(());
fn hash_content(mut stream: impl Read) -> io::Result<u64> {
let mut hasher = twox_hash::XxHash3_64::default();
let mut buf = [0u8; 8192];
loop {
let n = stream.read(&mut buf)?;
if n == 0 {
break;
}
hasher.write(&buf[..n]);
}
Ok(hasher.finish())
}
impl FileSystem for RealFileSystem {
fn fingerprint_path(
&self,
path: &Arc<AbsolutePath>,
path_read: PathRead,
) -> Result<PathFingerprint, Error> {
let std_path = path.as_path();
let file = match File::open(std_path) {
Ok(file) => file,
Err(err) => {
// On Windows, File::open fails specifically for directories with PermissionDenied
#[cfg(windows)]
{
if err.kind() == io::ErrorKind::PermissionDenied {
// This might be a directory - try reading it as such
return RealFileSystem::process_directory(std_path, path_read);
}
}
return if matches!(
err.kind(),
io::ErrorKind::NotFound |
// A component used as a directory in path is not a directory,
// e.g. "/foo.txt/bar" where "/foo.txt" is a file
io::ErrorKind::NotADirectory
) {
Ok(PathFingerprint::NotFound)
} else {
Err(Error::IoWithPath { err, path: path.clone() })
};
}
};
let mut reader = io::BufReader::new(file);
if let Err(io_err) = reader.fill_buf() {
if io_err.kind() != io::ErrorKind::IsADirectory {
return Err(io_err.into());
}
// Is a directory on Unix - use the optimized nix implementation first
#[cfg(unix)]
{
return Self::process_directory_unix(reader.into_inner(), path_read);
}
#[cfg(windows)]
{
// This shouldn't happen on Windows since File::open should have failed
// But if it does, fallback to std::fs::read_dir
return RealFileSystem::process_directory(std_path, path_read);
}
}
Ok(PathFingerprint::FileContentHash(hash_content(reader)?))
}
}
fn should_ignore_entry(name: &[u8]) -> bool {
matches!(name, b"." | b".." | b".DS_Store") || name.eq_ignore_ascii_case(b"dist")
}
impl RealFileSystem {
#[cfg(unix)]
fn process_directory_unix(fd: File, path_read: PathRead) -> Result<PathFingerprint, Error> {
use bstr::ByteSlice;
use nix::dir::{Dir, Type};
let dir_entries: Option<HashMap<Str, DirEntryKind>> = if path_read.read_dir_entries {
let mut dir_entries = HashMap::<Str, DirEntryKind>::new();
let dir = Dir::from_fd(fd.into())?;
for entry in dir {
let entry = entry?;
let entry_kind = match entry.file_type() {
None => todo!("handle DT_UNKNOWN (see readdir(3))"),
Some(Type::File) => DirEntryKind::File,
Some(Type::Directory) => DirEntryKind::Dir,
Some(Type::Symlink) => DirEntryKind::Symlink,
Some(other_type) => {
return Err(Error::UnsupportedFileType(other_type));
}
};
let filename: &[u8] = entry.file_name().to_bytes();
if should_ignore_entry(filename) {
continue;
}
dir_entries.insert(filename.to_str()?.into(), entry_kind);
}
Some(dir_entries)
} else {
None
};
Ok(PathFingerprint::Folder(dir_entries))
}
#[cfg(windows)]
fn process_directory(
path: &std::path::Path,
path_read: PathRead,
) -> Result<PathFingerprint, Error> {
let dir_entries: Option<HashMap<Str, DirEntryKind>> = if path_read.read_dir_entries {
let mut dir_entries = HashMap::<Str, DirEntryKind>::new();
let dir_iter = std::fs::read_dir(path)?;
for entry in dir_iter {
let entry = entry?;
let file_name = entry.file_name();
// Skip special entries (same as Unix version)
if should_ignore_entry(file_name.as_encoded_bytes()) {
continue;
}
// Get file type with minimal additional syscalls
let entry_kind = match entry.file_type() {
Ok(file_type) => {
if file_type.is_file() {
DirEntryKind::File
} else if file_type.is_dir() {
DirEntryKind::Dir
} else if file_type.is_symlink() {
DirEntryKind::Symlink
} else {
// Use Error::UnsupportedFileType instead of IoWithPath
return Err(Error::UnsupportedFileType(file_type));
}
}
Err(err) => {
// Return the original error instead of complex path handling
return Err(Error::Io(err));
}
};
// Convert filename to Str - return error for invalid UTF-8
match file_name.to_str() {
Some(filename_str) => {
dir_entries.insert(filename_str.into(), entry_kind);
}
None => {
// Return error instead of complex path handling
return Err(Error::Io(io::Error::new(
io::ErrorKind::InvalidData,
"Invalid UTF-8 in filename",
)));
}
}
}
Some(dir_entries)
} else {
None
};
Ok(PathFingerprint::Folder(dir_entries))
}
}
#[derive(Debug, Default)]
pub struct CachedFileSystem<FS = RealFileSystem> {
underlying: FS,
cache: DashMap<AbsolutePathBuf, PathFingerprint>,
}
impl<FS: FileSystem> FileSystem for CachedFileSystem<FS> {
fn fingerprint_path(
&self,
path: &Arc<AbsolutePath>,
path_read: PathRead,
) -> Result<PathFingerprint, Error> {
self.underlying.fingerprint_path(path, path_read)
// TODO: fingerprint memory cache
// Ok(match self
// .cache
// .entry(path.clone()) {
// Entry::Occupied(occupied_entry) => {
// match (occupied_entry.get(), path_read.read_dir_entries) {
// }
// },
// Entry::Vacant(vacant_entry) => {
// vacant_entry.insert(self.underlying.fingerprint_path(path, path_read)?).value().clone()
// },
// })
// Ok(fingerprint.value().clone())
}
}
impl<FS> CachedFileSystem<FS> {
#[expect(dead_code)]
pub fn invalidate_path(&self, path: &AbsolutePath) {
self.cache.remove(&path.to_absolute_path_buf());
}
}
#[cfg(test)]
mod tests {
use std::sync::Arc;
use tempfile::TempDir;
use super::*;
use crate::execute::PathRead;
#[test]
fn test_fingerprint_nonexistent_file() {
let fs = RealFileSystem::default();
let nonexistent_path = Arc::<AbsolutePath>::from(
AbsolutePathBuf::new(if cfg!(windows) {
"C:\\nonexistent\\path".into()
} else {
"/nonexistent/path".into()
})
.unwrap(),
);
let path_read = PathRead { read_dir_entries: false };
let result = fs.fingerprint_path(&nonexistent_path, path_read).unwrap();
assert!(matches!(result, PathFingerprint::NotFound));
}
#[test]
fn test_fingerprint_temp_file() {
let fs = RealFileSystem::default();
let temp_dir = TempDir::new().unwrap();
let temp_file = temp_dir.path().join("test_file.txt");
// Create a test file with known content
std::fs::write(&temp_file, "Hello, World!").unwrap();
let file_path =
Arc::<AbsolutePath>::from(AbsolutePathBuf::new(temp_file.to_path_buf()).unwrap());
let path_read = PathRead { read_dir_entries: false };
let result = fs.fingerprint_path(&file_path, path_read).unwrap();
assert!(matches!(result, PathFingerprint::FileContentHash(_)));
// Verify that the same file gives the same hash
let result2 = fs.fingerprint_path(&file_path, path_read).unwrap();
assert_eq!(result, result2);
}
#[test]
fn test_fingerprint_temp_directory() {
let fs = RealFileSystem::default();
let temp_dir = TempDir::new().unwrap();
// Create some files in the directory
std::fs::write(temp_dir.path().join("file1.txt"), "content1").unwrap();
std::fs::write(temp_dir.path().join("file2.txt"), "content2").unwrap();
let dir_path =
Arc::<AbsolutePath>::from(AbsolutePathBuf::new(temp_dir.path().to_path_buf()).unwrap());
let path_read = PathRead { read_dir_entries: true };
let result = fs.fingerprint_path(&dir_path, path_read).unwrap();
match result {
PathFingerprint::Folder(Some(entries)) => {
// Should contain our test files (but not . or .. or .DS_Store)
assert!(entries.contains_key("file1.txt"));
assert!(entries.contains_key("file2.txt"));
assert_eq!(entries.len(), 2);
}
_ => panic!("Expected folder with entries, got: {:?}", result),
}
// Test without reading entries
let path_read_no_entries = PathRead { read_dir_entries: false };
let result_no_entries = match fs.fingerprint_path(&dir_path, path_read_no_entries) {
Ok(result) => result,
Err(err) => {
// On Windows CI, temporary directories might have permission issues
// Skip the test if we get a permission denied error
if cfg!(windows) && err.to_string().contains("Access is denied") {
eprintln!("Skipping test due to Windows permission issue: {}", err);
return;
}
panic!("Unexpected error: {}", err);
}
};
assert!(matches!(result_no_entries, PathFingerprint::Folder(None)));
}
}