Skip to content

Commit

Permalink
fix(sb_fs): polishing tmp fs and s3 fs
Browse files Browse the repository at this point in the history
  • Loading branch information
nyannyacha committed Nov 12, 2024
1 parent d657bc3 commit 8942561
Show file tree
Hide file tree
Showing 4 changed files with 294 additions and 64 deletions.
3 changes: 2 additions & 1 deletion crates/base/src/deno_runtime.rs
Original file line number Diff line number Diff line change
Expand Up @@ -465,7 +465,8 @@ where
let build_file_system_fn =
|base_fs: Arc<dyn deno_fs::FileSystem>| -> Result<Arc<dyn deno_fs::FileSystem>, AnyError> {
let tmp_fs = TmpFs::try_from(maybe_tmp_fs_config.unwrap_or_default())?;
let fs = PrefixFs::new("/tmp", tmp_fs, Some(base_fs));
let fs = PrefixFs::new("/tmp", tmp_fs, Some(base_fs))
.tmp_dir("/tmp");

Ok(if let Some(s3_fs) = maybe_s3_fs_config.map(S3Fs::new).transpose()? {
maybe_s3_fs = Some(s3_fs.clone());
Expand Down
54 changes: 47 additions & 7 deletions crates/sb_fs/fs/prefix_fs.rs
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,8 @@ use deno_io::fs::{File, FsError, FsResult, FsStat};
#[derive(Debug, Clone)]
pub struct PrefixFs<FileSystem> {
prefix: PathBuf,
cwd: Option<PathBuf>,
tmp_dir: Option<PathBuf>,
fs: Arc<FileSystem>,
base_fs: Option<Arc<dyn deno_fs::FileSystem>>,
}
Expand All @@ -25,18 +27,52 @@ where
{
Self {
prefix: prefix.as_ref().to_path_buf(),
cwd: None,
tmp_dir: None,
fs: Arc::new(fs),
base_fs,
}
}

pub fn cwd<P>(mut self, v: P) -> Self
where
P: AsRef<Path>,
{
self.cwd = Some(v.as_ref().to_path_buf());
self
}

pub fn tmp_dir<P>(mut self, v: P) -> Self
where
P: AsRef<Path>,
{
self.tmp_dir = Some(v.as_ref().to_path_buf());
self
}

pub fn set_cwd<P>(&mut self, v: P) -> &mut Self
where
P: AsRef<Path>,
{
self.cwd = Some(v.as_ref().to_path_buf());
self
}

pub fn set_tmp_dir<P>(&mut self, v: P) -> &mut Self
where
P: AsRef<Path>,
{
self.tmp_dir = Some(v.as_ref().to_path_buf());
self
}
}

impl<FileSystem> PrefixFs<FileSystem>
where
FileSystem: deno_fs::FileSystem + 'static,
{
pub fn add_fs<P, FileSystemInner>(
self,
mut self,
prefix: P,
fs: FileSystemInner,
) -> PrefixFs<FileSystemInner>
Expand All @@ -47,6 +83,8 @@ where
PrefixFs {
prefix: prefix.as_ref().to_path_buf(),
fs: Arc::new(fs),
cwd: self.cwd.take(),
tmp_dir: self.tmp_dir.take(),
base_fs: Some(Arc::new(self)),
}
}
Expand All @@ -58,16 +96,18 @@ where
FileSystem: deno_fs::FileSystem,
{
fn cwd(&self) -> FsResult<PathBuf> {
self.base_fs
.as_ref()
.map(|it| it.cwd())
self.cwd
.clone()
.map(Ok)
.or_else(|| self.base_fs.as_ref().map(|it| it.cwd()))
.unwrap_or_else(|| Ok(PathBuf::new()))
}

fn tmp_dir(&self) -> FsResult<PathBuf> {
self.base_fs
.as_ref()
.map(|it| it.tmp_dir())
self.tmp_dir
.clone()
.map(Ok)
.or_else(|| self.base_fs.as_ref().map(|it| it.tmp_dir()))
.unwrap_or_else(|| Err(FsError::NotSupported))
}

Expand Down
Loading

0 comments on commit 8942561

Please sign in to comment.