perf(resolver): use fs::symlink_metadata, which doesn't traverse symlinks (#581)

This commit is contained in:
Boshen 2023-07-21 17:54:55 +08:00 committed by GitHub
parent a27b60cfb4
commit 3c5333c828
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
3 changed files with 10 additions and 6 deletions

View file

@ -36,7 +36,7 @@ impl<Fs: FileSystem> Cache<Fs> {
if let Some(result) = self.cache.get(path) {
return *result;
}
let file_metadata = self.fs.metadata(path).ok();
let file_metadata = self.fs.symlink_metadata(path).ok();
self.cache.insert(path.to_path_buf().into_boxed_path(), file_metadata);
file_metadata
}

View file

@ -8,8 +8,12 @@ pub trait FileSystem: Default + Send + Sync {
/// # Errors
///
/// * Any [io::Error]
fn metadata<P: AsRef<Path>>(&self, path: P) -> io::Result<FileMetadata>;
/// This function will return an error in the following situations, but is not
/// limited to just these cases:
///
/// * The user lacks permissions to perform `metadata` call on `path`.
/// * `path` does not exist.
fn symlink_metadata<P: AsRef<Path>>(&self, path: P) -> io::Result<FileMetadata>;
}
#[derive(Debug, Clone, Copy)]
@ -32,7 +36,7 @@ impl FileSystem for FileSystemOs {
fs::read_to_string(path)
}
fn metadata<P: AsRef<Path>>(&self, path: P) -> io::Result<FileMetadata> {
fs::metadata(path).map(|metadata| FileMetadata { is_file: metadata.is_file() })
fn symlink_metadata<P: AsRef<Path>>(&self, path: P) -> io::Result<FileMetadata> {
fs::symlink_metadata(path).map(|metadata| FileMetadata { is_file: metadata.is_file() })
}
}

View file

@ -48,7 +48,7 @@ impl FileSystem for MemoryFS {
Ok(buffer)
}
fn metadata<P: AsRef<Path>>(&self, path: P) -> io::Result<FileMetadata> {
fn symlink_metadata<P: AsRef<Path>>(&self, path: P) -> io::Result<FileMetadata> {
use vfs::FileSystem;
let metadata = self
.fs