diff options
Diffstat (limited to 'ext/fs')
-rw-r--r-- | ext/fs/interface.rs | 71 |
1 files changed, 58 insertions, 13 deletions
diff --git a/ext/fs/interface.rs b/ext/fs/interface.rs index f639a700b..8f791f4c2 100644 --- a/ext/fs/interface.rs +++ b/ext/fs/interface.rs @@ -343,26 +343,71 @@ impl<'a> DenoConfigFsAdapter<'a> { } impl<'a> deno_config::fs::DenoConfigFs for DenoConfigFsAdapter<'a> { - fn read_to_string(&self, path: &Path) -> Result<String, std::io::Error> { - use deno_io::fs::FsError; - use std::io::ErrorKind; + fn read_to_string_lossy( + &self, + path: &Path, + ) -> Result<String, std::io::Error> { self .0 .read_text_file_lossy_sync(path, None) - .map_err(|err| match err { - FsError::Io(io) => io, - FsError::FileBusy => std::io::Error::new(ErrorKind::Other, "file busy"), - FsError::NotSupported => { - std::io::Error::new(ErrorKind::Other, "not supported") - } - FsError::PermissionDenied(name) => std::io::Error::new( - ErrorKind::PermissionDenied, - format!("requires {}", name), - ), + .map_err(map_deno_fs_to_config_err) + } + + fn stat_sync( + &self, + path: &Path, + ) -> Result<deno_config::fs::FsMetadata, std::io::Error> { + self + .0 + .stat_sync(path) + .map(|stat| deno_config::fs::FsMetadata { + is_file: stat.is_file, + is_directory: stat.is_directory, + is_symlink: stat.is_symlink, + }) + .map_err(map_deno_fs_to_config_err) + } + + fn read_dir( + &self, + path: &Path, + ) -> Result<Vec<deno_config::fs::FsDirEntry>, std::io::Error> { + self + .0 + .read_dir_sync(path) + .map_err(map_deno_fs_to_config_err) + .map(|entries| { + entries + .into_iter() + .map(|e| deno_config::fs::FsDirEntry { + path: path.join(e.name), + metadata: deno_config::fs::FsMetadata { + is_file: e.is_file, + is_directory: e.is_directory, + is_symlink: e.is_symlink, + }, + }) + .collect() }) } } +fn map_deno_fs_to_config_err(fs_err: deno_io::fs::FsError) -> std::io::Error { + use deno_io::fs::FsError; + use std::io::ErrorKind; + match fs_err { + FsError::Io(io) => io, + FsError::FileBusy => std::io::Error::new(ErrorKind::Other, "file busy"), + FsError::NotSupported => { + std::io::Error::new(ErrorKind::Other, "not supported") + } + FsError::PermissionDenied(name) => std::io::Error::new( + ErrorKind::PermissionDenied, + format!("requires {}", name), + ), + } +} + // Like String::from_utf8_lossy but operates on owned values #[inline(always)] fn string_from_utf8_lossy(buf: Vec<u8>) -> String { |