From 9ce51c80f975bf373271e8ee437fdf40bcd5e76a Mon Sep 17 00:00:00 2001 From: Tobias Koppers Date: Mon, 29 Jul 2024 14:32:06 +0200 Subject: [PATCH] allow to resolve symlinks on DirectoryEntry (vercel/turbo#8851) ### Description Just a little helper method to resolve symlinks when doing `read_dir` ### Testing Instructions --- crates/turbo-tasks-fs/src/lib.rs | 18 ++++++++++++++++++ 1 file changed, 18 insertions(+) diff --git a/crates/turbo-tasks-fs/src/lib.rs b/crates/turbo-tasks-fs/src/lib.rs index ce8bac1752816..5b84cca255cde 100644 --- a/crates/turbo-tasks-fs/src/lib.rs +++ b/crates/turbo-tasks-fs/src/lib.rs @@ -1807,6 +1807,24 @@ pub enum DirectoryEntry { Error, } +/// Handles the `DirectoryEntry::Symlink` variant by checking the symlink target +/// type and replacing it with `DirectoryEntry::File` or +/// `DirectoryEntry::Directory`. +impl DirectoryEntry { + pub async fn resolve_symlink(self) -> Result { + if let DirectoryEntry::Symlink(symlink) = self { + let real_path = symlink.realpath().resolve().await?; + match *real_path.get_type().await? { + FileSystemEntryType::Directory => Ok(DirectoryEntry::Directory(real_path)), + FileSystemEntryType::File => Ok(DirectoryEntry::File(real_path)), + _ => Ok(self), + } + } else { + Ok(self) + } + } +} + #[turbo_tasks::value] #[derive(Hash, Clone, Copy, Debug)] pub enum FileSystemEntryType {