2024-01-01 14:58:21 -05:00
|
|
|
// Copyright 2018-2024 the Deno authors. All rights reserved. MIT license.
|
2023-04-12 09:13:32 -04:00
|
|
|
|
2024-06-08 12:36:13 -04:00
|
|
|
use std::borrow::Cow;
|
2023-04-12 09:13:32 -04:00
|
|
|
use std::path::Path;
|
|
|
|
use std::path::PathBuf;
|
|
|
|
use std::rc::Rc;
|
|
|
|
|
|
|
|
use serde::Deserialize;
|
|
|
|
use serde::Serialize;
|
|
|
|
|
2023-05-04 14:28:42 -04:00
|
|
|
use deno_io::fs::File;
|
|
|
|
use deno_io::fs::FsResult;
|
|
|
|
use deno_io::fs::FsStat;
|
|
|
|
|
2023-05-08 11:02:02 -04:00
|
|
|
use crate::sync::MaybeSend;
|
|
|
|
use crate::sync::MaybeSync;
|
|
|
|
|
2023-04-12 09:13:32 -04:00
|
|
|
#[derive(Deserialize, Default, Debug, Clone, Copy)]
|
|
|
|
#[serde(rename_all = "camelCase")]
|
|
|
|
#[serde(default)]
|
|
|
|
pub struct OpenOptions {
|
|
|
|
pub read: bool,
|
|
|
|
pub write: bool,
|
|
|
|
pub create: bool,
|
|
|
|
pub truncate: bool,
|
|
|
|
pub append: bool,
|
|
|
|
pub create_new: bool,
|
|
|
|
pub mode: Option<u32>,
|
|
|
|
}
|
|
|
|
|
|
|
|
impl OpenOptions {
|
|
|
|
pub fn read() -> Self {
|
|
|
|
Self {
|
|
|
|
read: true,
|
|
|
|
write: false,
|
|
|
|
create: false,
|
|
|
|
truncate: false,
|
|
|
|
append: false,
|
|
|
|
create_new: false,
|
|
|
|
mode: None,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
pub fn write(
|
|
|
|
create: bool,
|
|
|
|
append: bool,
|
|
|
|
create_new: bool,
|
|
|
|
mode: Option<u32>,
|
|
|
|
) -> Self {
|
|
|
|
Self {
|
|
|
|
read: false,
|
|
|
|
write: true,
|
|
|
|
create,
|
|
|
|
truncate: !append,
|
|
|
|
append,
|
|
|
|
create_new,
|
|
|
|
mode,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
#[derive(Deserialize)]
|
|
|
|
pub enum FsFileType {
|
|
|
|
#[serde(rename = "file")]
|
|
|
|
File,
|
|
|
|
#[serde(rename = "dir")]
|
|
|
|
Directory,
|
2024-03-08 22:37:29 -05:00
|
|
|
#[serde(rename = "junction")]
|
|
|
|
Junction,
|
2023-04-12 09:13:32 -04:00
|
|
|
}
|
|
|
|
|
2024-07-04 20:58:30 -04:00
|
|
|
/// WARNING: This is part of the public JS Deno API.
|
2023-04-12 09:13:32 -04:00
|
|
|
#[derive(Serialize)]
|
|
|
|
#[serde(rename_all = "camelCase")]
|
|
|
|
pub struct FsDirEntry {
|
|
|
|
pub name: String,
|
|
|
|
pub is_file: bool,
|
|
|
|
pub is_directory: bool,
|
|
|
|
pub is_symlink: bool,
|
|
|
|
}
|
|
|
|
|
2023-05-08 11:02:02 -04:00
|
|
|
#[allow(clippy::disallowed_types)]
|
|
|
|
pub type FileSystemRc = crate::sync::MaybeArc<dyn FileSystem>;
|
|
|
|
|
2024-04-19 20:12:03 -04:00
|
|
|
pub trait AccessCheckFn:
|
|
|
|
for<'a> FnMut(
|
|
|
|
bool,
|
|
|
|
&'a Path,
|
|
|
|
&'a OpenOptions,
|
|
|
|
) -> FsResult<std::borrow::Cow<'a, Path>>
|
|
|
|
{
|
|
|
|
}
|
|
|
|
impl<T> AccessCheckFn for T where
|
|
|
|
T: for<'a> FnMut(
|
|
|
|
bool,
|
|
|
|
&'a Path,
|
|
|
|
&'a OpenOptions,
|
|
|
|
) -> FsResult<std::borrow::Cow<'a, Path>>
|
|
|
|
{
|
|
|
|
}
|
|
|
|
|
|
|
|
pub type AccessCheckCb<'a> = &'a mut (dyn AccessCheckFn + 'a);
|
|
|
|
|
2023-04-12 09:13:32 -04:00
|
|
|
#[async_trait::async_trait(?Send)]
|
2023-05-08 11:02:02 -04:00
|
|
|
pub trait FileSystem: std::fmt::Debug + MaybeSend + MaybeSync {
|
2023-04-12 09:13:32 -04:00
|
|
|
fn cwd(&self) -> FsResult<PathBuf>;
|
|
|
|
fn tmp_dir(&self) -> FsResult<PathBuf>;
|
2023-05-04 14:28:42 -04:00
|
|
|
fn chdir(&self, path: &Path) -> FsResult<()>;
|
2023-04-12 09:13:32 -04:00
|
|
|
fn umask(&self, mask: Option<u32>) -> FsResult<u32>;
|
|
|
|
|
|
|
|
fn open_sync(
|
|
|
|
&self,
|
2023-05-04 14:28:42 -04:00
|
|
|
path: &Path,
|
2023-04-12 09:13:32 -04:00
|
|
|
options: OpenOptions,
|
2024-04-19 20:12:03 -04:00
|
|
|
access_check: Option<AccessCheckCb>,
|
2023-05-04 14:28:42 -04:00
|
|
|
) -> FsResult<Rc<dyn File>>;
|
2024-04-19 20:12:03 -04:00
|
|
|
async fn open_async<'a>(
|
|
|
|
&'a self,
|
2023-04-12 09:13:32 -04:00
|
|
|
path: PathBuf,
|
|
|
|
options: OpenOptions,
|
2024-04-19 20:12:03 -04:00
|
|
|
access_check: Option<AccessCheckCb<'a>>,
|
2023-05-04 14:28:42 -04:00
|
|
|
) -> FsResult<Rc<dyn File>>;
|
2023-04-12 09:13:32 -04:00
|
|
|
|
2024-09-28 08:50:16 -04:00
|
|
|
fn mkdir_sync(
|
|
|
|
&self,
|
|
|
|
path: &Path,
|
|
|
|
recursive: bool,
|
|
|
|
mode: Option<u32>,
|
|
|
|
) -> FsResult<()>;
|
2023-04-12 09:13:32 -04:00
|
|
|
async fn mkdir_async(
|
|
|
|
&self,
|
|
|
|
path: PathBuf,
|
2023-05-10 20:06:59 -04:00
|
|
|
recursive: bool,
|
2024-09-28 08:50:16 -04:00
|
|
|
mode: Option<u32>,
|
2023-04-12 09:13:32 -04:00
|
|
|
) -> FsResult<()>;
|
|
|
|
|
2023-05-04 14:28:42 -04:00
|
|
|
fn chmod_sync(&self, path: &Path, mode: u32) -> FsResult<()>;
|
2023-04-12 09:13:32 -04:00
|
|
|
async fn chmod_async(&self, path: PathBuf, mode: u32) -> FsResult<()>;
|
|
|
|
|
|
|
|
fn chown_sync(
|
|
|
|
&self,
|
2023-05-04 14:28:42 -04:00
|
|
|
path: &Path,
|
2023-04-12 09:13:32 -04:00
|
|
|
uid: Option<u32>,
|
|
|
|
gid: Option<u32>,
|
|
|
|
) -> FsResult<()>;
|
|
|
|
async fn chown_async(
|
|
|
|
&self,
|
|
|
|
path: PathBuf,
|
|
|
|
uid: Option<u32>,
|
|
|
|
gid: Option<u32>,
|
|
|
|
) -> FsResult<()>;
|
|
|
|
|
2024-07-05 14:32:51 -04:00
|
|
|
fn lchown_sync(
|
|
|
|
&self,
|
|
|
|
path: &Path,
|
|
|
|
uid: Option<u32>,
|
|
|
|
gid: Option<u32>,
|
|
|
|
) -> FsResult<()>;
|
|
|
|
async fn lchown_async(
|
|
|
|
&self,
|
|
|
|
path: PathBuf,
|
|
|
|
uid: Option<u32>,
|
|
|
|
gid: Option<u32>,
|
|
|
|
) -> FsResult<()>;
|
|
|
|
|
2023-05-04 14:28:42 -04:00
|
|
|
fn remove_sync(&self, path: &Path, recursive: bool) -> FsResult<()>;
|
2023-04-12 09:13:32 -04:00
|
|
|
async fn remove_async(&self, path: PathBuf, recursive: bool) -> FsResult<()>;
|
|
|
|
|
2023-05-04 14:28:42 -04:00
|
|
|
fn copy_file_sync(&self, oldpath: &Path, newpath: &Path) -> FsResult<()>;
|
2023-04-12 09:13:32 -04:00
|
|
|
async fn copy_file_async(
|
|
|
|
&self,
|
|
|
|
oldpath: PathBuf,
|
|
|
|
newpath: PathBuf,
|
|
|
|
) -> FsResult<()>;
|
|
|
|
|
2024-01-05 07:58:33 -05:00
|
|
|
fn cp_sync(&self, path: &Path, new_path: &Path) -> FsResult<()>;
|
|
|
|
async fn cp_async(&self, path: PathBuf, new_path: PathBuf) -> FsResult<()>;
|
|
|
|
|
2023-05-04 14:28:42 -04:00
|
|
|
fn stat_sync(&self, path: &Path) -> FsResult<FsStat>;
|
2023-04-12 09:13:32 -04:00
|
|
|
async fn stat_async(&self, path: PathBuf) -> FsResult<FsStat>;
|
|
|
|
|
2023-05-04 14:28:42 -04:00
|
|
|
fn lstat_sync(&self, path: &Path) -> FsResult<FsStat>;
|
2023-04-12 09:13:32 -04:00
|
|
|
async fn lstat_async(&self, path: PathBuf) -> FsResult<FsStat>;
|
|
|
|
|
2023-05-04 14:28:42 -04:00
|
|
|
fn realpath_sync(&self, path: &Path) -> FsResult<PathBuf>;
|
2023-04-12 09:13:32 -04:00
|
|
|
async fn realpath_async(&self, path: PathBuf) -> FsResult<PathBuf>;
|
|
|
|
|
2023-05-04 14:28:42 -04:00
|
|
|
fn read_dir_sync(&self, path: &Path) -> FsResult<Vec<FsDirEntry>>;
|
2023-04-12 09:13:32 -04:00
|
|
|
async fn read_dir_async(&self, path: PathBuf) -> FsResult<Vec<FsDirEntry>>;
|
|
|
|
|
2023-05-04 14:28:42 -04:00
|
|
|
fn rename_sync(&self, oldpath: &Path, newpath: &Path) -> FsResult<()>;
|
2023-04-12 09:13:32 -04:00
|
|
|
async fn rename_async(
|
|
|
|
&self,
|
|
|
|
oldpath: PathBuf,
|
|
|
|
newpath: PathBuf,
|
|
|
|
) -> FsResult<()>;
|
|
|
|
|
2023-05-04 14:28:42 -04:00
|
|
|
fn link_sync(&self, oldpath: &Path, newpath: &Path) -> FsResult<()>;
|
2023-04-12 09:13:32 -04:00
|
|
|
async fn link_async(
|
|
|
|
&self,
|
|
|
|
oldpath: PathBuf,
|
|
|
|
newpath: PathBuf,
|
|
|
|
) -> FsResult<()>;
|
|
|
|
|
|
|
|
fn symlink_sync(
|
|
|
|
&self,
|
2023-05-04 14:28:42 -04:00
|
|
|
oldpath: &Path,
|
|
|
|
newpath: &Path,
|
2023-04-12 09:13:32 -04:00
|
|
|
file_type: Option<FsFileType>,
|
|
|
|
) -> FsResult<()>;
|
|
|
|
async fn symlink_async(
|
|
|
|
&self,
|
|
|
|
oldpath: PathBuf,
|
|
|
|
newpath: PathBuf,
|
|
|
|
file_type: Option<FsFileType>,
|
|
|
|
) -> FsResult<()>;
|
|
|
|
|
2023-05-04 14:28:42 -04:00
|
|
|
fn read_link_sync(&self, path: &Path) -> FsResult<PathBuf>;
|
2023-04-12 09:13:32 -04:00
|
|
|
async fn read_link_async(&self, path: PathBuf) -> FsResult<PathBuf>;
|
|
|
|
|
2023-05-04 14:28:42 -04:00
|
|
|
fn truncate_sync(&self, path: &Path, len: u64) -> FsResult<()>;
|
2023-04-12 09:13:32 -04:00
|
|
|
async fn truncate_async(&self, path: PathBuf, len: u64) -> FsResult<()>;
|
|
|
|
|
|
|
|
fn utime_sync(
|
|
|
|
&self,
|
2023-05-04 14:28:42 -04:00
|
|
|
path: &Path,
|
2023-04-12 09:13:32 -04:00
|
|
|
atime_secs: i64,
|
|
|
|
atime_nanos: u32,
|
|
|
|
mtime_secs: i64,
|
|
|
|
mtime_nanos: u32,
|
|
|
|
) -> FsResult<()>;
|
|
|
|
async fn utime_async(
|
|
|
|
&self,
|
|
|
|
path: PathBuf,
|
|
|
|
atime_secs: i64,
|
|
|
|
atime_nanos: u32,
|
|
|
|
mtime_secs: i64,
|
|
|
|
mtime_nanos: u32,
|
|
|
|
) -> FsResult<()>;
|
|
|
|
|
2024-07-02 22:33:32 -04:00
|
|
|
fn lutime_sync(
|
|
|
|
&self,
|
|
|
|
path: &Path,
|
|
|
|
atime_secs: i64,
|
|
|
|
atime_nanos: u32,
|
|
|
|
mtime_secs: i64,
|
|
|
|
mtime_nanos: u32,
|
|
|
|
) -> FsResult<()>;
|
|
|
|
async fn lutime_async(
|
|
|
|
&self,
|
|
|
|
path: PathBuf,
|
|
|
|
atime_secs: i64,
|
|
|
|
atime_nanos: u32,
|
|
|
|
mtime_secs: i64,
|
|
|
|
mtime_nanos: u32,
|
|
|
|
) -> FsResult<()>;
|
|
|
|
|
2023-04-12 09:13:32 -04:00
|
|
|
fn write_file_sync(
|
|
|
|
&self,
|
2023-05-04 14:28:42 -04:00
|
|
|
path: &Path,
|
2023-04-12 09:13:32 -04:00
|
|
|
options: OpenOptions,
|
2024-04-19 20:12:03 -04:00
|
|
|
access_check: Option<AccessCheckCb>,
|
2023-04-12 09:13:32 -04:00
|
|
|
data: &[u8],
|
|
|
|
) -> FsResult<()> {
|
2024-04-19 20:12:03 -04:00
|
|
|
let file = self.open_sync(path, options, access_check)?;
|
2023-04-12 09:13:32 -04:00
|
|
|
if let Some(mode) = options.mode {
|
|
|
|
file.clone().chmod_sync(mode)?;
|
|
|
|
}
|
|
|
|
file.write_all_sync(data)?;
|
|
|
|
Ok(())
|
|
|
|
}
|
2024-04-19 20:12:03 -04:00
|
|
|
async fn write_file_async<'a>(
|
|
|
|
&'a self,
|
2023-04-12 09:13:32 -04:00
|
|
|
path: PathBuf,
|
|
|
|
options: OpenOptions,
|
2024-04-19 20:12:03 -04:00
|
|
|
access_check: Option<AccessCheckCb<'a>>,
|
2023-04-12 09:13:32 -04:00
|
|
|
data: Vec<u8>,
|
|
|
|
) -> FsResult<()> {
|
2024-04-19 20:12:03 -04:00
|
|
|
let file = self.open_async(path, options, access_check).await?;
|
2023-04-12 09:13:32 -04:00
|
|
|
if let Some(mode) = options.mode {
|
|
|
|
file.clone().chmod_async(mode).await?;
|
|
|
|
}
|
2023-05-04 14:28:42 -04:00
|
|
|
file.write_all(data.into()).await?;
|
2023-04-12 09:13:32 -04:00
|
|
|
Ok(())
|
|
|
|
}
|
|
|
|
|
2024-04-19 20:12:03 -04:00
|
|
|
fn read_file_sync(
|
|
|
|
&self,
|
|
|
|
path: &Path,
|
|
|
|
access_check: Option<AccessCheckCb>,
|
|
|
|
) -> FsResult<Vec<u8>> {
|
2023-04-12 09:13:32 -04:00
|
|
|
let options = OpenOptions::read();
|
2024-04-19 20:12:03 -04:00
|
|
|
let file = self.open_sync(path, options, access_check)?;
|
2023-04-12 09:13:32 -04:00
|
|
|
let buf = file.read_all_sync()?;
|
|
|
|
Ok(buf)
|
|
|
|
}
|
2024-04-19 20:12:03 -04:00
|
|
|
async fn read_file_async<'a>(
|
|
|
|
&'a self,
|
|
|
|
path: PathBuf,
|
|
|
|
access_check: Option<AccessCheckCb<'a>>,
|
|
|
|
) -> FsResult<Vec<u8>> {
|
2023-04-12 09:13:32 -04:00
|
|
|
let options = OpenOptions::read();
|
2024-04-19 20:12:03 -04:00
|
|
|
let file = self.open_async(path, options, access_check).await?;
|
2023-04-12 09:13:32 -04:00
|
|
|
let buf = file.read_all_async().await?;
|
|
|
|
Ok(buf)
|
|
|
|
}
|
2023-05-05 12:44:24 -04:00
|
|
|
|
2023-08-08 16:28:18 -04:00
|
|
|
fn is_file_sync(&self, path: &Path) -> bool {
|
2023-05-05 12:44:24 -04:00
|
|
|
self.stat_sync(path).map(|m| m.is_file).unwrap_or(false)
|
|
|
|
}
|
|
|
|
|
2023-08-08 16:28:18 -04:00
|
|
|
fn is_dir_sync(&self, path: &Path) -> bool {
|
2023-05-05 12:44:24 -04:00
|
|
|
self
|
|
|
|
.stat_sync(path)
|
|
|
|
.map(|m| m.is_directory)
|
|
|
|
.unwrap_or(false)
|
|
|
|
}
|
|
|
|
|
2023-08-08 16:28:18 -04:00
|
|
|
fn exists_sync(&self, path: &Path) -> bool {
|
2023-05-05 12:44:24 -04:00
|
|
|
self.stat_sync(path).is_ok()
|
|
|
|
}
|
2024-07-17 08:35:51 -04:00
|
|
|
async fn exists_async(&self, path: PathBuf) -> FsResult<bool> {
|
|
|
|
Ok(self.stat_async(path).await.is_ok())
|
|
|
|
}
|
2023-05-05 12:44:24 -04:00
|
|
|
|
2024-06-08 12:36:13 -04:00
|
|
|
fn read_text_file_lossy_sync(
|
2024-04-19 20:12:03 -04:00
|
|
|
&self,
|
|
|
|
path: &Path,
|
|
|
|
access_check: Option<AccessCheckCb>,
|
|
|
|
) -> FsResult<String> {
|
|
|
|
let buf = self.read_file_sync(path, access_check)?;
|
2024-06-08 12:36:13 -04:00
|
|
|
Ok(string_from_utf8_lossy(buf))
|
2023-05-05 12:44:24 -04:00
|
|
|
}
|
2024-06-08 12:36:13 -04:00
|
|
|
async fn read_text_file_lossy_async<'a>(
|
2024-04-19 20:12:03 -04:00
|
|
|
&'a self,
|
|
|
|
path: PathBuf,
|
|
|
|
access_check: Option<AccessCheckCb<'a>>,
|
|
|
|
) -> FsResult<String> {
|
|
|
|
let buf = self.read_file_async(path, access_check).await?;
|
2024-06-08 12:36:13 -04:00
|
|
|
Ok(string_from_utf8_lossy(buf))
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Like String::from_utf8_lossy but operates on owned values
|
|
|
|
#[inline(always)]
|
|
|
|
fn string_from_utf8_lossy(buf: Vec<u8>) -> String {
|
|
|
|
match String::from_utf8_lossy(&buf) {
|
|
|
|
// buf contained non-utf8 chars than have been patched
|
|
|
|
Cow::Owned(s) => s,
|
|
|
|
// SAFETY: if Borrowed then the buf only contains utf8 chars,
|
|
|
|
// we do this instead of .into_owned() to avoid copying the input buf
|
|
|
|
Cow::Borrowed(_) => unsafe { String::from_utf8_unchecked(buf) },
|
2023-08-08 16:28:18 -04:00
|
|
|
}
|
2023-04-12 09:13:32 -04:00
|
|
|
}
|