2024-01-01 19:58:21 +00:00
|
|
|
// Copyright 2018-2024 the Deno authors. All rights reserved. MIT license.
|
2023-03-07 09:13:44 +00:00
|
|
|
|
2024-03-08 01:16:32 +00:00
|
|
|
mod in_memory_fs;
|
2023-04-12 13:13:32 +00:00
|
|
|
mod interface;
|
|
|
|
mod ops;
|
|
|
|
mod std_fs;
|
2023-05-08 15:02:02 +00:00
|
|
|
pub mod sync;
|
2023-04-12 13:13:32 +00:00
|
|
|
|
2024-03-08 01:16:32 +00:00
|
|
|
pub use crate::in_memory_fs::InMemoryFs;
|
2024-04-20 00:12:03 +00:00
|
|
|
pub use crate::interface::AccessCheckCb;
|
|
|
|
pub use crate::interface::AccessCheckFn;
|
2023-04-12 13:13:32 +00:00
|
|
|
pub use crate::interface::FileSystem;
|
2023-05-08 15:02:02 +00:00
|
|
|
pub use crate::interface::FileSystemRc;
|
2023-04-12 13:13:32 +00:00
|
|
|
pub use crate::interface::FsDirEntry;
|
|
|
|
pub use crate::interface::FsFileType;
|
|
|
|
pub use crate::interface::OpenOptions;
|
2023-05-04 18:28:42 +00:00
|
|
|
pub use crate::std_fs::RealFs;
|
2023-05-08 15:02:02 +00:00
|
|
|
pub use crate::sync::MaybeSend;
|
|
|
|
pub use crate::sync::MaybeSync;
|
|
|
|
|
|
|
|
use crate::ops::*;
|
2023-04-12 13:13:32 +00:00
|
|
|
|
2023-04-17 14:10:59 +00:00
|
|
|
use deno_core::error::AnyError;
|
2020-09-10 13:57:45 +00:00
|
|
|
use deno_core::OpState;
|
2024-04-20 00:12:03 +00:00
|
|
|
use deno_io::fs::FsError;
|
2023-04-17 14:10:59 +00:00
|
|
|
use std::path::Path;
|
2023-03-07 09:13:44 +00:00
|
|
|
|
2024-04-20 00:12:03 +00:00
|
|
|
pub trait FsPermissions: Send + Sync {
|
|
|
|
fn check_open<'a>(
|
|
|
|
&mut self,
|
|
|
|
resolved: bool,
|
|
|
|
read: bool,
|
|
|
|
write: bool,
|
|
|
|
path: &'a Path,
|
|
|
|
api_name: &str,
|
|
|
|
) -> Result<std::borrow::Cow<'a, Path>, FsError>;
|
2023-08-03 11:19:19 +00:00
|
|
|
fn check_read(&mut self, path: &Path, api_name: &str)
|
|
|
|
-> Result<(), AnyError>;
|
2023-04-17 14:10:59 +00:00
|
|
|
fn check_read_all(&mut self, api_name: &str) -> Result<(), AnyError>;
|
|
|
|
fn check_read_blind(
|
|
|
|
&mut self,
|
|
|
|
p: &Path,
|
|
|
|
display: &str,
|
|
|
|
api_name: &str,
|
|
|
|
) -> Result<(), AnyError>;
|
2023-08-03 11:19:19 +00:00
|
|
|
fn check_write(
|
|
|
|
&mut self,
|
|
|
|
path: &Path,
|
|
|
|
api_name: &str,
|
|
|
|
) -> Result<(), AnyError>;
|
|
|
|
fn check_write_partial(
|
|
|
|
&mut self,
|
|
|
|
path: &Path,
|
|
|
|
api_name: &str,
|
|
|
|
) -> Result<(), AnyError>;
|
2023-04-17 14:10:59 +00:00
|
|
|
fn check_write_all(&mut self, api_name: &str) -> Result<(), AnyError>;
|
|
|
|
fn check_write_blind(
|
|
|
|
&mut self,
|
|
|
|
p: &Path,
|
|
|
|
display: &str,
|
|
|
|
api_name: &str,
|
|
|
|
) -> Result<(), AnyError>;
|
|
|
|
|
2024-04-20 00:12:03 +00:00
|
|
|
fn check<'a>(
|
2023-04-17 14:10:59 +00:00
|
|
|
&mut self,
|
2024-04-20 00:12:03 +00:00
|
|
|
resolved: bool,
|
2023-04-17 14:10:59 +00:00
|
|
|
open_options: &OpenOptions,
|
2024-04-20 00:12:03 +00:00
|
|
|
path: &'a Path,
|
2023-04-17 14:10:59 +00:00
|
|
|
api_name: &str,
|
2024-04-20 00:12:03 +00:00
|
|
|
) -> Result<std::borrow::Cow<'a, Path>, FsError> {
|
|
|
|
self.check_open(
|
|
|
|
resolved,
|
|
|
|
open_options.read,
|
|
|
|
open_options.write || open_options.append,
|
|
|
|
path,
|
|
|
|
api_name,
|
|
|
|
)
|
2023-04-17 14:10:59 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-10-12 15:55:50 +00:00
|
|
|
pub const UNSTABLE_FEATURE_NAME: &str = "fs";
|
|
|
|
|
2023-03-07 09:13:44 +00:00
|
|
|
/// Helper for checking unstable features. Used for sync ops.
|
2023-10-04 19:42:17 +00:00
|
|
|
fn check_unstable(state: &OpState, api_name: &str) {
|
2023-10-12 15:55:50 +00:00
|
|
|
// TODO(bartlomieju): replace with `state.feature_checker.check_or_exit`
|
|
|
|
// once we phase out `check_or_exit_with_legacy_fallback`
|
2023-10-04 19:42:17 +00:00
|
|
|
state
|
|
|
|
.feature_checker
|
2023-10-12 15:55:50 +00:00
|
|
|
.check_or_exit_with_legacy_fallback(UNSTABLE_FEATURE_NAME, api_name);
|
2023-03-07 09:13:44 +00:00
|
|
|
}
|
|
|
|
|
2023-03-17 18:22:15 +00:00
|
|
|
deno_core::extension!(deno_fs,
|
2023-04-12 13:13:32 +00:00
|
|
|
deps = [ deno_web ],
|
2023-05-04 18:28:42 +00:00
|
|
|
parameters = [P: FsPermissions],
|
2023-03-17 18:22:15 +00:00
|
|
|
ops = [
|
2023-05-08 21:07:45 +00:00
|
|
|
op_fs_cwd<P>,
|
|
|
|
op_fs_umask,
|
|
|
|
op_fs_chdir<P>,
|
2023-04-12 13:13:32 +00:00
|
|
|
|
2023-05-08 21:07:45 +00:00
|
|
|
op_fs_open_sync<P>,
|
|
|
|
op_fs_open_async<P>,
|
|
|
|
op_fs_mkdir_sync<P>,
|
|
|
|
op_fs_mkdir_async<P>,
|
|
|
|
op_fs_chmod_sync<P>,
|
|
|
|
op_fs_chmod_async<P>,
|
|
|
|
op_fs_chown_sync<P>,
|
|
|
|
op_fs_chown_async<P>,
|
|
|
|
op_fs_remove_sync<P>,
|
|
|
|
op_fs_remove_async<P>,
|
|
|
|
op_fs_copy_file_sync<P>,
|
|
|
|
op_fs_copy_file_async<P>,
|
|
|
|
op_fs_stat_sync<P>,
|
|
|
|
op_fs_stat_async<P>,
|
|
|
|
op_fs_lstat_sync<P>,
|
|
|
|
op_fs_lstat_async<P>,
|
|
|
|
op_fs_realpath_sync<P>,
|
|
|
|
op_fs_realpath_async<P>,
|
|
|
|
op_fs_read_dir_sync<P>,
|
|
|
|
op_fs_read_dir_async<P>,
|
|
|
|
op_fs_rename_sync<P>,
|
|
|
|
op_fs_rename_async<P>,
|
|
|
|
op_fs_link_sync<P>,
|
|
|
|
op_fs_link_async<P>,
|
|
|
|
op_fs_symlink_sync<P>,
|
|
|
|
op_fs_symlink_async<P>,
|
|
|
|
op_fs_read_link_sync<P>,
|
|
|
|
op_fs_read_link_async<P>,
|
|
|
|
op_fs_truncate_sync<P>,
|
|
|
|
op_fs_truncate_async<P>,
|
|
|
|
op_fs_utime_sync<P>,
|
|
|
|
op_fs_utime_async<P>,
|
|
|
|
op_fs_make_temp_dir_sync<P>,
|
|
|
|
op_fs_make_temp_dir_async<P>,
|
|
|
|
op_fs_make_temp_file_sync<P>,
|
|
|
|
op_fs_make_temp_file_async<P>,
|
|
|
|
op_fs_write_file_sync<P>,
|
|
|
|
op_fs_write_file_async<P>,
|
|
|
|
op_fs_read_file_sync<P>,
|
|
|
|
op_fs_read_file_async<P>,
|
|
|
|
op_fs_read_file_text_sync<P>,
|
|
|
|
op_fs_read_file_text_async<P>,
|
2023-04-12 13:13:32 +00:00
|
|
|
|
2023-05-08 21:07:45 +00:00
|
|
|
op_fs_seek_sync,
|
|
|
|
op_fs_seek_async,
|
|
|
|
op_fs_fdatasync_sync,
|
|
|
|
op_fs_fdatasync_async,
|
2024-01-24 22:38:18 +00:00
|
|
|
op_fs_fdatasync_sync_unstable,
|
|
|
|
op_fs_fdatasync_async_unstable,
|
2023-05-08 21:07:45 +00:00
|
|
|
op_fs_fsync_sync,
|
|
|
|
op_fs_fsync_async,
|
2024-01-24 22:38:18 +00:00
|
|
|
op_fs_fsync_sync_unstable,
|
|
|
|
op_fs_fsync_async_unstable,
|
2024-02-19 13:33:42 +00:00
|
|
|
op_fs_file_stat_sync,
|
|
|
|
op_fs_file_stat_async,
|
2023-05-08 21:07:45 +00:00
|
|
|
op_fs_flock_sync,
|
|
|
|
op_fs_flock_async,
|
|
|
|
op_fs_funlock_sync,
|
|
|
|
op_fs_funlock_async,
|
|
|
|
op_fs_ftruncate_sync,
|
|
|
|
op_fs_ftruncate_async,
|
|
|
|
op_fs_futime_sync,
|
|
|
|
op_fs_futime_async,
|
2023-04-12 13:13:32 +00:00
|
|
|
|
2023-03-17 18:22:15 +00:00
|
|
|
],
|
|
|
|
esm = [ "30_fs.js" ],
|
2023-03-17 22:15:27 +00:00
|
|
|
options = {
|
2023-05-08 15:02:02 +00:00
|
|
|
fs: FileSystemRc,
|
2023-03-17 18:22:15 +00:00
|
|
|
},
|
2023-03-17 22:15:27 +00:00
|
|
|
state = |state, options| {
|
2023-04-12 13:13:32 +00:00
|
|
|
state.put(options.fs);
|
2023-03-17 18:22:15 +00:00
|
|
|
},
|
|
|
|
);
|