mirror of
https://github.com/denoland/deno.git
synced 2025-08-04 19:08:15 +00:00
refactor(ext/fs): boxed deno_fs::FileSystem (#18945)
1. Boxed `File` and `FileSystem` to allow more easily passing this through the CLI code (as shown within this pr). 2. `StdFileResource` is now `FileResource`. `FileResource` now contains an `Rc<dyn File>`.
This commit is contained in:
parent
4b645676d6
commit
5270c43e41
20 changed files with 1190 additions and 1258 deletions
|
@ -1,6 +1,5 @@
|
|||
// Copyright 2018-2023 the Deno authors. All rights reserved. MIT license.
|
||||
|
||||
use std::io;
|
||||
use std::path::Path;
|
||||
use std::path::PathBuf;
|
||||
use std::rc::Rc;
|
||||
|
@ -8,6 +7,10 @@ use std::rc::Rc;
|
|||
use serde::Deserialize;
|
||||
use serde::Serialize;
|
||||
|
||||
use deno_io::fs::File;
|
||||
use deno_io::fs::FsResult;
|
||||
use deno_io::fs::FsStat;
|
||||
|
||||
#[derive(Deserialize, Default, Debug, Clone, Copy)]
|
||||
#[serde(rename_all = "camelCase")]
|
||||
#[serde(default)]
|
||||
|
@ -52,27 +55,6 @@ impl OpenOptions {
|
|||
}
|
||||
}
|
||||
|
||||
pub struct FsStat {
|
||||
pub is_file: bool,
|
||||
pub is_directory: bool,
|
||||
pub is_symlink: bool,
|
||||
pub size: u64,
|
||||
|
||||
pub mtime: Option<u64>,
|
||||
pub atime: Option<u64>,
|
||||
pub birthtime: Option<u64>,
|
||||
|
||||
pub dev: u64,
|
||||
pub ino: u64,
|
||||
pub mode: u32,
|
||||
pub nlink: u64,
|
||||
pub uid: u32,
|
||||
pub gid: u32,
|
||||
pub rdev: u64,
|
||||
pub blksize: u64,
|
||||
pub blocks: u64,
|
||||
}
|
||||
|
||||
#[derive(Deserialize)]
|
||||
pub enum FsFileType {
|
||||
#[serde(rename = "file")]
|
||||
|
@ -90,93 +72,25 @@ pub struct FsDirEntry {
|
|||
pub is_symlink: bool,
|
||||
}
|
||||
|
||||
pub enum FsError {
|
||||
Io(io::Error),
|
||||
FileBusy,
|
||||
NotSupported,
|
||||
}
|
||||
|
||||
impl From<io::Error> for FsError {
|
||||
fn from(err: io::Error) -> Self {
|
||||
Self::Io(err)
|
||||
}
|
||||
}
|
||||
|
||||
pub type FsResult<T> = Result<T, FsError>;
|
||||
|
||||
#[async_trait::async_trait(?Send)]
|
||||
pub trait File {
|
||||
fn write_all_sync(self: Rc<Self>, buf: &[u8]) -> FsResult<()>;
|
||||
async fn write_all_async(self: Rc<Self>, buf: Vec<u8>) -> FsResult<()>;
|
||||
|
||||
fn read_all_sync(self: Rc<Self>) -> FsResult<Vec<u8>>;
|
||||
async fn read_all_async(self: Rc<Self>) -> FsResult<Vec<u8>>;
|
||||
|
||||
fn chmod_sync(self: Rc<Self>, pathmode: u32) -> FsResult<()>;
|
||||
async fn chmod_async(self: Rc<Self>, mode: u32) -> FsResult<()>;
|
||||
|
||||
fn seek_sync(self: Rc<Self>, pos: io::SeekFrom) -> FsResult<u64>;
|
||||
async fn seek_async(self: Rc<Self>, pos: io::SeekFrom) -> FsResult<u64>;
|
||||
|
||||
fn datasync_sync(self: Rc<Self>) -> FsResult<()>;
|
||||
async fn datasync_async(self: Rc<Self>) -> FsResult<()>;
|
||||
|
||||
fn sync_sync(self: Rc<Self>) -> FsResult<()>;
|
||||
async fn sync_async(self: Rc<Self>) -> FsResult<()>;
|
||||
|
||||
fn stat_sync(self: Rc<Self>) -> FsResult<FsStat>;
|
||||
async fn stat_async(self: Rc<Self>) -> FsResult<FsStat>;
|
||||
|
||||
fn lock_sync(self: Rc<Self>, exclusive: bool) -> FsResult<()>;
|
||||
async fn lock_async(self: Rc<Self>, exclusive: bool) -> FsResult<()>;
|
||||
fn unlock_sync(self: Rc<Self>) -> FsResult<()>;
|
||||
async fn unlock_async(self: Rc<Self>) -> FsResult<()>;
|
||||
|
||||
fn truncate_sync(self: Rc<Self>, len: u64) -> FsResult<()>;
|
||||
async fn truncate_async(self: Rc<Self>, len: u64) -> FsResult<()>;
|
||||
|
||||
fn utime_sync(
|
||||
self: Rc<Self>,
|
||||
atime_secs: i64,
|
||||
atime_nanos: u32,
|
||||
mtime_secs: i64,
|
||||
mtime_nanos: u32,
|
||||
) -> FsResult<()>;
|
||||
async fn utime_async(
|
||||
self: Rc<Self>,
|
||||
atime_secs: i64,
|
||||
atime_nanos: u32,
|
||||
mtime_secs: i64,
|
||||
mtime_nanos: u32,
|
||||
) -> FsResult<()>;
|
||||
}
|
||||
|
||||
#[async_trait::async_trait(?Send)]
|
||||
pub trait FileSystem: Clone {
|
||||
type File: File;
|
||||
|
||||
pub trait FileSystem: Send + Sync {
|
||||
fn cwd(&self) -> FsResult<PathBuf>;
|
||||
fn tmp_dir(&self) -> FsResult<PathBuf>;
|
||||
fn chdir(&self, path: impl AsRef<Path>) -> FsResult<()>;
|
||||
fn chdir(&self, path: &Path) -> FsResult<()>;
|
||||
fn umask(&self, mask: Option<u32>) -> FsResult<u32>;
|
||||
|
||||
fn open_sync(
|
||||
&self,
|
||||
path: impl AsRef<Path>,
|
||||
path: &Path,
|
||||
options: OpenOptions,
|
||||
) -> FsResult<Self::File>;
|
||||
) -> FsResult<Rc<dyn File>>;
|
||||
async fn open_async(
|
||||
&self,
|
||||
path: PathBuf,
|
||||
options: OpenOptions,
|
||||
) -> FsResult<Self::File>;
|
||||
) -> FsResult<Rc<dyn File>>;
|
||||
|
||||
fn mkdir_sync(
|
||||
&self,
|
||||
path: impl AsRef<Path>,
|
||||
recusive: bool,
|
||||
mode: u32,
|
||||
) -> FsResult<()>;
|
||||
fn mkdir_sync(&self, path: &Path, recusive: bool, mode: u32) -> FsResult<()>;
|
||||
async fn mkdir_async(
|
||||
&self,
|
||||
path: PathBuf,
|
||||
|
@ -184,12 +98,12 @@ pub trait FileSystem: Clone {
|
|||
mode: u32,
|
||||
) -> FsResult<()>;
|
||||
|
||||
fn chmod_sync(&self, path: impl AsRef<Path>, mode: u32) -> FsResult<()>;
|
||||
fn chmod_sync(&self, path: &Path, mode: u32) -> FsResult<()>;
|
||||
async fn chmod_async(&self, path: PathBuf, mode: u32) -> FsResult<()>;
|
||||
|
||||
fn chown_sync(
|
||||
&self,
|
||||
path: impl AsRef<Path>,
|
||||
path: &Path,
|
||||
uid: Option<u32>,
|
||||
gid: Option<u32>,
|
||||
) -> FsResult<()>;
|
||||
|
@ -200,52 +114,36 @@ pub trait FileSystem: Clone {
|
|||
gid: Option<u32>,
|
||||
) -> FsResult<()>;
|
||||
|
||||
fn remove_sync(
|
||||
&self,
|
||||
path: impl AsRef<Path>,
|
||||
recursive: bool,
|
||||
) -> FsResult<()>;
|
||||
fn remove_sync(&self, path: &Path, recursive: bool) -> FsResult<()>;
|
||||
async fn remove_async(&self, path: PathBuf, recursive: bool) -> FsResult<()>;
|
||||
|
||||
fn copy_file_sync(
|
||||
&self,
|
||||
oldpath: impl AsRef<Path>,
|
||||
newpath: impl AsRef<Path>,
|
||||
) -> FsResult<()>;
|
||||
fn copy_file_sync(&self, oldpath: &Path, newpath: &Path) -> FsResult<()>;
|
||||
async fn copy_file_async(
|
||||
&self,
|
||||
oldpath: PathBuf,
|
||||
newpath: PathBuf,
|
||||
) -> FsResult<()>;
|
||||
|
||||
fn stat_sync(&self, path: impl AsRef<Path>) -> FsResult<FsStat>;
|
||||
fn stat_sync(&self, path: &Path) -> FsResult<FsStat>;
|
||||
async fn stat_async(&self, path: PathBuf) -> FsResult<FsStat>;
|
||||
|
||||
fn lstat_sync(&self, path: impl AsRef<Path>) -> FsResult<FsStat>;
|
||||
fn lstat_sync(&self, path: &Path) -> FsResult<FsStat>;
|
||||
async fn lstat_async(&self, path: PathBuf) -> FsResult<FsStat>;
|
||||
|
||||
fn realpath_sync(&self, path: impl AsRef<Path>) -> FsResult<PathBuf>;
|
||||
fn realpath_sync(&self, path: &Path) -> FsResult<PathBuf>;
|
||||
async fn realpath_async(&self, path: PathBuf) -> FsResult<PathBuf>;
|
||||
|
||||
fn read_dir_sync(&self, path: impl AsRef<Path>) -> FsResult<Vec<FsDirEntry>>;
|
||||
fn read_dir_sync(&self, path: &Path) -> FsResult<Vec<FsDirEntry>>;
|
||||
async fn read_dir_async(&self, path: PathBuf) -> FsResult<Vec<FsDirEntry>>;
|
||||
|
||||
fn rename_sync(
|
||||
&self,
|
||||
oldpath: impl AsRef<Path>,
|
||||
newpath: impl AsRef<Path>,
|
||||
) -> FsResult<()>;
|
||||
fn rename_sync(&self, oldpath: &Path, newpath: &Path) -> FsResult<()>;
|
||||
async fn rename_async(
|
||||
&self,
|
||||
oldpath: PathBuf,
|
||||
newpath: PathBuf,
|
||||
) -> FsResult<()>;
|
||||
|
||||
fn link_sync(
|
||||
&self,
|
||||
oldpath: impl AsRef<Path>,
|
||||
newpath: impl AsRef<Path>,
|
||||
) -> FsResult<()>;
|
||||
fn link_sync(&self, oldpath: &Path, newpath: &Path) -> FsResult<()>;
|
||||
async fn link_async(
|
||||
&self,
|
||||
oldpath: PathBuf,
|
||||
|
@ -254,8 +152,8 @@ pub trait FileSystem: Clone {
|
|||
|
||||
fn symlink_sync(
|
||||
&self,
|
||||
oldpath: impl AsRef<Path>,
|
||||
newpath: impl AsRef<Path>,
|
||||
oldpath: &Path,
|
||||
newpath: &Path,
|
||||
file_type: Option<FsFileType>,
|
||||
) -> FsResult<()>;
|
||||
async fn symlink_async(
|
||||
|
@ -265,15 +163,15 @@ pub trait FileSystem: Clone {
|
|||
file_type: Option<FsFileType>,
|
||||
) -> FsResult<()>;
|
||||
|
||||
fn read_link_sync(&self, path: impl AsRef<Path>) -> FsResult<PathBuf>;
|
||||
fn read_link_sync(&self, path: &Path) -> FsResult<PathBuf>;
|
||||
async fn read_link_async(&self, path: PathBuf) -> FsResult<PathBuf>;
|
||||
|
||||
fn truncate_sync(&self, path: impl AsRef<Path>, len: u64) -> FsResult<()>;
|
||||
fn truncate_sync(&self, path: &Path, len: u64) -> FsResult<()>;
|
||||
async fn truncate_async(&self, path: PathBuf, len: u64) -> FsResult<()>;
|
||||
|
||||
fn utime_sync(
|
||||
&self,
|
||||
path: impl AsRef<Path>,
|
||||
path: &Path,
|
||||
atime_secs: i64,
|
||||
atime_nanos: u32,
|
||||
mtime_secs: i64,
|
||||
|
@ -290,12 +188,11 @@ pub trait FileSystem: Clone {
|
|||
|
||||
fn write_file_sync(
|
||||
&self,
|
||||
path: impl AsRef<Path>,
|
||||
path: &Path,
|
||||
options: OpenOptions,
|
||||
data: &[u8],
|
||||
) -> FsResult<()> {
|
||||
let file = self.open_sync(path, options)?;
|
||||
let file = Rc::new(file);
|
||||
if let Some(mode) = options.mode {
|
||||
file.clone().chmod_sync(mode)?;
|
||||
}
|
||||
|
@ -309,25 +206,22 @@ pub trait FileSystem: Clone {
|
|||
data: Vec<u8>,
|
||||
) -> FsResult<()> {
|
||||
let file = self.open_async(path, options).await?;
|
||||
let file = Rc::new(file);
|
||||
if let Some(mode) = options.mode {
|
||||
file.clone().chmod_async(mode).await?;
|
||||
}
|
||||
file.write_all_async(data).await?;
|
||||
file.write_all(data.into()).await?;
|
||||
Ok(())
|
||||
}
|
||||
|
||||
fn read_file_sync(&self, path: impl AsRef<Path>) -> FsResult<Vec<u8>> {
|
||||
fn read_file_sync(&self, path: &Path) -> FsResult<Vec<u8>> {
|
||||
let options = OpenOptions::read();
|
||||
let file = self.open_sync(path, options)?;
|
||||
let file = Rc::new(file);
|
||||
let buf = file.read_all_sync()?;
|
||||
Ok(buf)
|
||||
}
|
||||
async fn read_file_async(&self, path: PathBuf) -> FsResult<Vec<u8>> {
|
||||
let options = OpenOptions::read();
|
||||
let file = self.clone().open_async(path, options).await?;
|
||||
let file = Rc::new(file);
|
||||
let file = self.open_async(path, options).await?;
|
||||
let buf = file.read_all_async().await?;
|
||||
Ok(buf)
|
||||
}
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue