2023-01-02 21:00:42 +00:00
|
|
|
// Copyright 2018-2023 the Deno authors. All rights reserved. MIT license.
|
2019-08-26 14:18:42 +00:00
|
|
|
// Some deserializer fields are only used on Unix and Windows build fails without it
|
2023-03-07 09:13:44 +00:00
|
|
|
|
2020-09-14 16:48:57 +00:00
|
|
|
use deno_core::error::custom_error;
|
|
|
|
use deno_core::error::type_error;
|
|
|
|
use deno_core::error::AnyError;
|
2023-03-07 09:13:44 +00:00
|
|
|
use deno_core::include_js_files;
|
2022-03-14 17:44:15 +00:00
|
|
|
use deno_core::op;
|
2022-04-18 22:00:14 +00:00
|
|
|
use deno_core::CancelFuture;
|
|
|
|
use deno_core::CancelHandle;
|
2021-05-02 23:22:57 +00:00
|
|
|
use deno_core::Extension;
|
2023-03-09 14:56:19 +00:00
|
|
|
use deno_core::ExtensionBuilder;
|
2020-09-10 13:57:45 +00:00
|
|
|
use deno_core::OpState;
|
2021-03-19 17:25:37 +00:00
|
|
|
use deno_core::ResourceId;
|
2023-01-07 16:25:34 +00:00
|
|
|
use deno_core::ZeroCopyBuf;
|
2020-11-13 21:01:57 +00:00
|
|
|
use deno_crypto::rand::thread_rng;
|
|
|
|
use deno_crypto::rand::Rng;
|
2023-03-05 00:39:48 +00:00
|
|
|
use deno_io::StdFileResource;
|
2021-03-26 16:34:25 +00:00
|
|
|
use log::debug;
|
2020-09-16 16:43:08 +00:00
|
|
|
use serde::Deserialize;
|
2021-04-05 16:40:24 +00:00
|
|
|
use serde::Serialize;
|
2022-05-02 02:13:05 +00:00
|
|
|
use std::borrow::Cow;
|
2020-09-10 13:57:45 +00:00
|
|
|
use std::cell::RefCell;
|
2019-08-14 15:03:02 +00:00
|
|
|
use std::convert::From;
|
2022-05-10 14:13:08 +00:00
|
|
|
use std::env::current_dir;
|
|
|
|
use std::env::set_current_dir;
|
|
|
|
use std::env::temp_dir;
|
2020-04-27 18:09:56 +00:00
|
|
|
use std::io;
|
2022-05-10 14:13:08 +00:00
|
|
|
use std::io::Error;
|
|
|
|
use std::io::Seek;
|
|
|
|
use std::io::SeekFrom;
|
2022-04-18 22:00:14 +00:00
|
|
|
use std::io::Write;
|
2022-05-10 14:13:08 +00:00
|
|
|
use std::path::Path;
|
|
|
|
use std::path::PathBuf;
|
2020-08-18 12:07:57 +00:00
|
|
|
use std::rc::Rc;
|
2020-04-27 18:09:56 +00:00
|
|
|
use std::time::SystemTime;
|
2019-08-14 15:03:02 +00:00
|
|
|
use std::time::UNIX_EPOCH;
|
|
|
|
|
2023-03-07 09:13:44 +00:00
|
|
|
/// Similar to `std::fs::canonicalize()` but strips UNC prefixes on Windows.
|
|
|
|
fn canonicalize_path(path: &Path) -> Result<PathBuf, Error> {
|
|
|
|
let mut canonicalized_path = path.canonicalize()?;
|
|
|
|
if cfg!(windows) {
|
|
|
|
canonicalized_path = PathBuf::from(
|
|
|
|
canonicalized_path
|
|
|
|
.display()
|
|
|
|
.to_string()
|
|
|
|
.trim_start_matches("\\\\?\\"),
|
|
|
|
);
|
|
|
|
}
|
|
|
|
Ok(canonicalized_path)
|
|
|
|
}
|
|
|
|
|
|
|
|
/// A utility function to map OsStrings to Strings
|
|
|
|
fn into_string(s: std::ffi::OsString) -> Result<String, AnyError> {
|
|
|
|
s.into_string().map_err(|s| {
|
|
|
|
let message = format!("File name or path {s:?} is not valid UTF-8");
|
|
|
|
custom_error("InvalidData", message)
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
|
|
|
#[cfg(unix)]
|
|
|
|
pub fn get_nix_error_class(error: &nix::Error) -> &'static str {
|
|
|
|
match error {
|
|
|
|
nix::Error::ECHILD => "NotFound",
|
|
|
|
nix::Error::EINVAL => "TypeError",
|
|
|
|
nix::Error::ENOENT => "NotFound",
|
|
|
|
nix::Error::ENOTTY => "BadResource",
|
|
|
|
nix::Error::EPERM => "PermissionDenied",
|
|
|
|
nix::Error::ESRCH => "NotFound",
|
|
|
|
nix::Error::UnknownErrno => "Error",
|
|
|
|
&nix::Error::ENOTSUP => unreachable!(),
|
|
|
|
_ => "Error",
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
struct UnstableChecker {
|
|
|
|
pub unstable: bool,
|
|
|
|
}
|
|
|
|
|
|
|
|
impl UnstableChecker {
|
|
|
|
// NOTE(bartlomieju): keep in sync with `cli/program_state.rs`
|
|
|
|
pub fn check_unstable(&self, api_name: &str) {
|
|
|
|
if !self.unstable {
|
|
|
|
eprintln!(
|
|
|
|
"Unstable API '{api_name}'. The --unstable flag must be provided."
|
|
|
|
);
|
|
|
|
std::process::exit(70);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/// Helper for checking unstable features. Used for sync ops.
|
|
|
|
fn check_unstable(state: &OpState, api_name: &str) {
|
|
|
|
state.borrow::<UnstableChecker>().check_unstable(api_name)
|
|
|
|
}
|
|
|
|
|
|
|
|
/// Helper for checking unstable features. Used for async ops.
|
|
|
|
fn check_unstable2(state: &Rc<RefCell<OpState>>, api_name: &str) {
|
|
|
|
let state = state.borrow();
|
|
|
|
state.borrow::<UnstableChecker>().check_unstable(api_name)
|
|
|
|
}
|
|
|
|
|
2023-03-04 23:44:59 +00:00
|
|
|
pub trait FsPermissions {
|
|
|
|
fn check_read(&mut self, p: &Path, api_name: &str) -> Result<(), AnyError>;
|
|
|
|
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>;
|
|
|
|
fn check_write(&mut self, p: &Path, api_name: &str) -> Result<(), AnyError>;
|
|
|
|
fn check_write_all(&mut self, api_name: &str) -> Result<(), AnyError>;
|
|
|
|
}
|
|
|
|
|
2020-09-14 16:48:57 +00:00
|
|
|
#[cfg(not(unix))]
|
|
|
|
use deno_core::error::generic_error;
|
|
|
|
#[cfg(not(unix))]
|
|
|
|
use deno_core::error::not_supported;
|
|
|
|
|
2023-03-09 14:56:19 +00:00
|
|
|
fn ext() -> ExtensionBuilder {
|
2023-01-08 22:48:46 +00:00
|
|
|
Extension::builder("deno_fs")
|
2023-03-09 14:56:19 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
fn ops<P: FsPermissions + 'static>(
|
|
|
|
ext: &mut ExtensionBuilder,
|
|
|
|
unstable: bool,
|
|
|
|
) -> &mut ExtensionBuilder {
|
|
|
|
ext
|
2023-03-07 09:13:44 +00:00
|
|
|
.state(move |state| {
|
|
|
|
state.put(UnstableChecker { unstable });
|
|
|
|
})
|
2021-05-02 23:22:57 +00:00
|
|
|
.ops(vec![
|
2023-03-04 23:44:59 +00:00
|
|
|
op_open_sync::decl::<P>(),
|
|
|
|
op_open_async::decl::<P>(),
|
|
|
|
op_write_file_sync::decl::<P>(),
|
|
|
|
op_write_file_async::decl::<P>(),
|
2022-03-14 17:44:15 +00:00
|
|
|
op_seek_sync::decl(),
|
|
|
|
op_seek_async::decl(),
|
|
|
|
op_fdatasync_sync::decl(),
|
|
|
|
op_fdatasync_async::decl(),
|
|
|
|
op_fsync_sync::decl(),
|
|
|
|
op_fsync_async::decl(),
|
|
|
|
op_fstat_sync::decl(),
|
|
|
|
op_fstat_async::decl(),
|
|
|
|
op_flock_sync::decl(),
|
|
|
|
op_flock_async::decl(),
|
|
|
|
op_funlock_sync::decl(),
|
|
|
|
op_funlock_async::decl(),
|
|
|
|
op_umask::decl(),
|
2023-03-04 23:44:59 +00:00
|
|
|
op_chdir::decl::<P>(),
|
|
|
|
op_mkdir_sync::decl::<P>(),
|
|
|
|
op_mkdir_async::decl::<P>(),
|
|
|
|
op_chmod_sync::decl::<P>(),
|
|
|
|
op_chmod_async::decl::<P>(),
|
|
|
|
op_chown_sync::decl::<P>(),
|
|
|
|
op_chown_async::decl::<P>(),
|
|
|
|
op_remove_sync::decl::<P>(),
|
|
|
|
op_remove_async::decl::<P>(),
|
|
|
|
op_copy_file_sync::decl::<P>(),
|
|
|
|
op_copy_file_async::decl::<P>(),
|
|
|
|
op_stat_sync::decl::<P>(),
|
|
|
|
op_stat_async::decl::<P>(),
|
|
|
|
op_realpath_sync::decl::<P>(),
|
|
|
|
op_realpath_async::decl::<P>(),
|
|
|
|
op_read_dir_sync::decl::<P>(),
|
|
|
|
op_read_dir_async::decl::<P>(),
|
|
|
|
op_rename_sync::decl::<P>(),
|
|
|
|
op_rename_async::decl::<P>(),
|
|
|
|
op_link_sync::decl::<P>(),
|
|
|
|
op_link_async::decl::<P>(),
|
|
|
|
op_symlink_sync::decl::<P>(),
|
|
|
|
op_symlink_async::decl::<P>(),
|
|
|
|
op_read_link_sync::decl::<P>(),
|
|
|
|
op_read_link_async::decl::<P>(),
|
2022-03-14 17:44:15 +00:00
|
|
|
op_ftruncate_sync::decl(),
|
|
|
|
op_ftruncate_async::decl(),
|
2023-03-04 23:44:59 +00:00
|
|
|
op_truncate_sync::decl::<P>(),
|
|
|
|
op_truncate_async::decl::<P>(),
|
|
|
|
op_make_temp_dir_sync::decl::<P>(),
|
|
|
|
op_make_temp_dir_async::decl::<P>(),
|
|
|
|
op_make_temp_file_sync::decl::<P>(),
|
|
|
|
op_make_temp_file_async::decl::<P>(),
|
|
|
|
op_cwd::decl::<P>(),
|
2022-03-14 17:44:15 +00:00
|
|
|
op_futime_sync::decl(),
|
|
|
|
op_futime_async::decl(),
|
2023-03-04 23:44:59 +00:00
|
|
|
op_utime_sync::decl::<P>(),
|
|
|
|
op_utime_async::decl::<P>(),
|
|
|
|
op_readfile_sync::decl::<P>(),
|
|
|
|
op_readfile_text_sync::decl::<P>(),
|
|
|
|
op_readfile_async::decl::<P>(),
|
|
|
|
op_readfile_text_async::decl::<P>(),
|
2021-05-02 23:22:57 +00:00
|
|
|
])
|
2023-03-09 14:56:19 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
pub fn init_ops_and_esm<P: FsPermissions + 'static>(
|
|
|
|
unstable: bool,
|
|
|
|
) -> Extension {
|
|
|
|
ops::<P>(&mut ext(), unstable)
|
|
|
|
.esm(include_js_files!("30_fs.js",))
|
|
|
|
.build()
|
|
|
|
}
|
|
|
|
|
|
|
|
pub fn init_ops<P: FsPermissions + 'static>(unstable: bool) -> Extension {
|
2023-03-10 00:22:27 +00:00
|
|
|
ops::<P>(&mut ext(), unstable).build()
|
2019-10-11 18:41:54 +00:00
|
|
|
}
|
|
|
|
|
2023-02-16 19:21:03 +00:00
|
|
|
fn default_err_mapper(err: Error, desc: String) -> Error {
|
|
|
|
Error::new(err.kind(), format!("{err}, {desc}"))
|
|
|
|
}
|
|
|
|
|
2020-03-07 12:20:27 +00:00
|
|
|
#[derive(Deserialize, Default, Debug)]
|
|
|
|
#[serde(rename_all = "camelCase")]
|
|
|
|
#[serde(default)]
|
2021-03-18 18:42:01 +00:00
|
|
|
pub struct OpenOptions {
|
2020-03-07 12:20:27 +00:00
|
|
|
read: bool,
|
|
|
|
write: bool,
|
|
|
|
create: bool,
|
|
|
|
truncate: bool,
|
|
|
|
append: bool,
|
|
|
|
create_new: bool,
|
|
|
|
}
|
|
|
|
|
2022-08-19 10:24:40 +00:00
|
|
|
#[inline]
|
2023-03-04 23:44:59 +00:00
|
|
|
fn open_helper<P>(
|
2020-09-10 13:57:45 +00:00
|
|
|
state: &mut OpState,
|
2022-08-19 10:24:40 +00:00
|
|
|
path: &str,
|
|
|
|
mode: Option<u32>,
|
|
|
|
options: Option<&OpenOptions>,
|
2022-09-27 20:36:33 +00:00
|
|
|
api_name: &str,
|
2023-03-04 23:44:59 +00:00
|
|
|
) -> Result<(PathBuf, std::fs::OpenOptions), AnyError>
|
|
|
|
where
|
|
|
|
P: FsPermissions + 'static,
|
|
|
|
{
|
2022-08-19 10:24:40 +00:00
|
|
|
let path = Path::new(path).to_path_buf();
|
2020-03-16 19:02:41 +00:00
|
|
|
|
2020-04-16 00:43:19 +00:00
|
|
|
let mut open_options = std::fs::OpenOptions::new();
|
|
|
|
|
2022-08-19 10:24:40 +00:00
|
|
|
if let Some(mode) = mode {
|
2020-03-16 19:02:41 +00:00
|
|
|
// mode only used if creating the file on Unix
|
|
|
|
// if not specified, defaults to 0o666
|
|
|
|
#[cfg(unix)]
|
2020-03-20 13:46:26 +00:00
|
|
|
{
|
|
|
|
use std::os::unix::fs::OpenOptionsExt;
|
2020-04-16 00:43:19 +00:00
|
|
|
open_options.mode(mode & 0o777);
|
2020-03-20 13:46:26 +00:00
|
|
|
}
|
2020-03-16 19:02:41 +00:00
|
|
|
#[cfg(not(unix))]
|
|
|
|
let _ = mode; // avoid unused warning
|
2020-04-16 00:43:19 +00:00
|
|
|
}
|
2020-03-07 12:20:27 +00:00
|
|
|
|
2023-03-04 23:44:59 +00:00
|
|
|
let permissions = state.borrow_mut::<P>();
|
2020-09-19 23:17:35 +00:00
|
|
|
|
2022-08-19 10:24:40 +00:00
|
|
|
match options {
|
|
|
|
None => {
|
2023-01-07 16:25:34 +00:00
|
|
|
permissions.check_read(&path, api_name)?;
|
2022-08-19 10:24:40 +00:00
|
|
|
open_options
|
|
|
|
.read(true)
|
|
|
|
.create(false)
|
|
|
|
.write(false)
|
|
|
|
.truncate(false)
|
|
|
|
.append(false)
|
|
|
|
.create_new(false);
|
|
|
|
}
|
|
|
|
Some(options) => {
|
|
|
|
if options.read {
|
2023-01-07 16:25:34 +00:00
|
|
|
permissions.check_read(&path, api_name)?;
|
2022-08-19 10:24:40 +00:00
|
|
|
}
|
2020-03-07 12:20:27 +00:00
|
|
|
|
2022-08-19 10:24:40 +00:00
|
|
|
if options.write || options.append {
|
2023-01-07 16:25:34 +00:00
|
|
|
permissions.check_write(&path, api_name)?;
|
2022-08-19 10:24:40 +00:00
|
|
|
}
|
2020-03-07 12:20:27 +00:00
|
|
|
|
2022-08-19 10:24:40 +00:00
|
|
|
open_options
|
|
|
|
.read(options.read)
|
|
|
|
.create(options.create)
|
|
|
|
.write(options.write)
|
|
|
|
.truncate(options.truncate)
|
|
|
|
.append(options.append)
|
|
|
|
.create_new(options.create_new);
|
|
|
|
}
|
|
|
|
}
|
2020-03-07 12:20:27 +00:00
|
|
|
|
2020-08-18 12:07:57 +00:00
|
|
|
Ok((path, open_options))
|
|
|
|
}
|
|
|
|
|
2022-03-14 17:44:15 +00:00
|
|
|
#[op]
|
2023-03-04 23:44:59 +00:00
|
|
|
fn op_open_sync<P>(
|
2020-09-10 13:57:45 +00:00
|
|
|
state: &mut OpState,
|
2022-08-19 10:24:40 +00:00
|
|
|
path: String,
|
|
|
|
options: Option<OpenOptions>,
|
|
|
|
mode: Option<u32>,
|
2023-03-04 23:44:59 +00:00
|
|
|
) -> Result<ResourceId, AnyError>
|
|
|
|
where
|
|
|
|
P: FsPermissions + 'static,
|
|
|
|
{
|
2022-09-27 20:36:33 +00:00
|
|
|
let (path, open_options) =
|
2023-03-04 23:44:59 +00:00
|
|
|
open_helper::<P>(state, &path, mode, options.as_ref(), "Deno.openSync()")?;
|
2021-10-11 13:21:18 +00:00
|
|
|
let std_file = open_options.open(&path).map_err(|err| {
|
2023-02-16 19:21:03 +00:00
|
|
|
default_err_mapper(err, format!("open '{}'", path.display()))
|
2021-10-11 13:21:18 +00:00
|
|
|
})?;
|
2022-04-19 14:41:31 +00:00
|
|
|
let resource = StdFileResource::fs_file(std_file);
|
2020-12-16 16:14:12 +00:00
|
|
|
let rid = state.resource_table.add(resource);
|
2021-04-05 16:40:24 +00:00
|
|
|
Ok(rid)
|
2020-08-18 12:07:57 +00:00
|
|
|
}
|
|
|
|
|
2022-03-14 17:44:15 +00:00
|
|
|
#[op]
|
2023-03-04 23:44:59 +00:00
|
|
|
async fn op_open_async<P>(
|
2020-09-10 13:57:45 +00:00
|
|
|
state: Rc<RefCell<OpState>>,
|
2022-08-19 10:24:40 +00:00
|
|
|
path: String,
|
|
|
|
options: Option<OpenOptions>,
|
|
|
|
mode: Option<u32>,
|
2023-03-04 23:44:59 +00:00
|
|
|
) -> Result<ResourceId, AnyError>
|
|
|
|
where
|
|
|
|
P: FsPermissions + 'static,
|
|
|
|
{
|
|
|
|
let (path, open_options) = open_helper::<P>(
|
2022-09-27 20:36:33 +00:00
|
|
|
&mut state.borrow_mut(),
|
|
|
|
&path,
|
|
|
|
mode,
|
|
|
|
options.as_ref(),
|
|
|
|
"Deno.open()",
|
|
|
|
)?;
|
2022-04-19 14:41:31 +00:00
|
|
|
let std_file = tokio::task::spawn_blocking(move || {
|
2023-01-05 19:29:50 +00:00
|
|
|
open_options.open(&path).map_err(|err| {
|
2023-02-16 19:21:03 +00:00
|
|
|
default_err_mapper(err, format!("open '{}'", path.display()))
|
2022-04-19 14:41:31 +00:00
|
|
|
})
|
|
|
|
})
|
|
|
|
.await?;
|
|
|
|
let resource = StdFileResource::fs_file(std_file?);
|
2020-12-16 16:14:12 +00:00
|
|
|
let rid = state.borrow_mut().resource_table.add(resource);
|
2021-04-05 16:40:24 +00:00
|
|
|
Ok(rid)
|
2020-03-07 12:20:27 +00:00
|
|
|
}
|
|
|
|
|
2022-09-05 11:50:48 +00:00
|
|
|
#[inline]
|
2022-12-13 04:12:49 +00:00
|
|
|
fn write_open_options(
|
|
|
|
create: bool,
|
|
|
|
append: bool,
|
|
|
|
create_new: bool,
|
|
|
|
) -> OpenOptions {
|
2022-09-05 11:50:48 +00:00
|
|
|
OpenOptions {
|
|
|
|
read: false,
|
|
|
|
write: true,
|
|
|
|
create,
|
|
|
|
truncate: !append,
|
|
|
|
append,
|
2022-12-13 04:12:49 +00:00
|
|
|
create_new,
|
2022-04-18 22:00:14 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
#[op]
|
2023-03-04 23:44:59 +00:00
|
|
|
fn op_write_file_sync<P>(
|
2022-04-18 22:00:14 +00:00
|
|
|
state: &mut OpState,
|
2022-09-05 11:50:48 +00:00
|
|
|
path: String,
|
|
|
|
mode: Option<u32>,
|
|
|
|
append: bool,
|
|
|
|
create: bool,
|
2022-12-13 04:12:49 +00:00
|
|
|
create_new: bool,
|
2022-09-05 11:50:48 +00:00
|
|
|
data: ZeroCopyBuf,
|
2023-03-04 23:44:59 +00:00
|
|
|
) -> Result<(), AnyError>
|
|
|
|
where
|
|
|
|
P: FsPermissions + 'static,
|
|
|
|
{
|
|
|
|
let (path, open_options) = open_helper::<P>(
|
2022-08-19 10:24:40 +00:00
|
|
|
state,
|
2022-09-05 11:50:48 +00:00
|
|
|
&path,
|
|
|
|
mode,
|
2022-12-13 04:12:49 +00:00
|
|
|
Some(&write_open_options(create, append, create_new)),
|
2022-09-27 20:36:33 +00:00
|
|
|
"Deno.writeFileSync()",
|
2022-08-19 10:24:40 +00:00
|
|
|
)?;
|
2022-09-05 11:50:48 +00:00
|
|
|
write_file(&path, open_options, mode, data)
|
2022-04-18 22:00:14 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
#[op]
|
2023-03-04 23:44:59 +00:00
|
|
|
async fn op_write_file_async<P>(
|
2022-04-18 22:00:14 +00:00
|
|
|
state: Rc<RefCell<OpState>>,
|
2022-09-05 11:50:48 +00:00
|
|
|
path: String,
|
|
|
|
mode: Option<u32>,
|
|
|
|
append: bool,
|
|
|
|
create: bool,
|
2022-12-13 04:12:49 +00:00
|
|
|
create_new: bool,
|
2022-09-05 11:50:48 +00:00
|
|
|
data: ZeroCopyBuf,
|
|
|
|
cancel_rid: Option<ResourceId>,
|
2023-03-04 23:44:59 +00:00
|
|
|
) -> Result<(), AnyError>
|
|
|
|
where
|
|
|
|
P: FsPermissions + 'static,
|
|
|
|
{
|
|
|
|
let (path, open_options) = open_helper::<P>(
|
2022-11-18 01:59:10 +00:00
|
|
|
&mut state.borrow_mut(),
|
2022-09-05 11:50:48 +00:00
|
|
|
&path,
|
|
|
|
mode,
|
2022-12-13 04:12:49 +00:00
|
|
|
Some(&write_open_options(create, append, create_new)),
|
2022-09-27 20:36:33 +00:00
|
|
|
"Deno.writeFile()",
|
2022-08-19 10:24:40 +00:00
|
|
|
)?;
|
2023-02-11 12:19:13 +00:00
|
|
|
|
2022-04-18 22:00:14 +00:00
|
|
|
let write_future = tokio::task::spawn_blocking(move || {
|
2022-09-05 11:50:48 +00:00
|
|
|
write_file(&path, open_options, mode, data)
|
2022-04-18 22:00:14 +00:00
|
|
|
});
|
2023-02-11 12:19:13 +00:00
|
|
|
|
|
|
|
let cancel_handle = cancel_rid.and_then(|rid| {
|
|
|
|
state
|
|
|
|
.borrow_mut()
|
|
|
|
.resource_table
|
|
|
|
.get::<CancelHandle>(rid)
|
|
|
|
.ok()
|
|
|
|
});
|
|
|
|
|
2022-04-18 22:00:14 +00:00
|
|
|
if let Some(cancel_handle) = cancel_handle {
|
2023-02-11 12:19:13 +00:00
|
|
|
let write_future_rv = write_future.or_cancel(cancel_handle).await;
|
|
|
|
|
|
|
|
if let Some(cancel_rid) = cancel_rid {
|
|
|
|
state.borrow_mut().resource_table.close(cancel_rid).ok();
|
|
|
|
};
|
|
|
|
|
|
|
|
return write_future_rv??;
|
2022-04-18 22:00:14 +00:00
|
|
|
}
|
2023-02-11 12:19:13 +00:00
|
|
|
|
|
|
|
write_future.await?
|
2022-04-18 22:00:14 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
fn write_file(
|
|
|
|
path: &Path,
|
|
|
|
open_options: std::fs::OpenOptions,
|
2022-09-05 11:50:48 +00:00
|
|
|
_mode: Option<u32>,
|
2022-04-18 22:00:14 +00:00
|
|
|
data: ZeroCopyBuf,
|
|
|
|
) -> Result<(), AnyError> {
|
|
|
|
let mut std_file = open_options.open(path).map_err(|err| {
|
2023-02-16 19:21:03 +00:00
|
|
|
default_err_mapper(err, format!("open '{}'", path.display()))
|
2022-04-18 22:00:14 +00:00
|
|
|
})?;
|
|
|
|
|
|
|
|
// need to chmod the file if it already exists and a mode is specified
|
|
|
|
#[cfg(unix)]
|
2022-09-05 11:50:48 +00:00
|
|
|
if let Some(mode) = _mode {
|
2022-04-18 22:00:14 +00:00
|
|
|
use std::os::unix::fs::PermissionsExt;
|
|
|
|
let permissions = PermissionsExt::from_mode(mode & 0o777);
|
2023-02-16 19:21:03 +00:00
|
|
|
std_file.set_permissions(permissions).map_err(|err| {
|
|
|
|
default_err_mapper(err, format!("chmod '{}'", path.display()))
|
|
|
|
})?;
|
2022-04-18 22:00:14 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
std_file.write_all(&data)?;
|
|
|
|
Ok(())
|
|
|
|
}
|
|
|
|
|
2020-03-07 12:20:27 +00:00
|
|
|
#[derive(Deserialize)]
|
|
|
|
#[serde(rename_all = "camelCase")]
|
2021-03-18 18:42:01 +00:00
|
|
|
pub struct SeekArgs {
|
2021-03-19 17:25:37 +00:00
|
|
|
rid: ResourceId,
|
2020-06-27 19:58:35 +00:00
|
|
|
offset: i64,
|
2020-03-07 12:20:27 +00:00
|
|
|
whence: i32,
|
|
|
|
}
|
|
|
|
|
2021-03-18 18:42:01 +00:00
|
|
|
fn seek_helper(args: SeekArgs) -> Result<(u32, SeekFrom), AnyError> {
|
2021-03-19 00:55:31 +00:00
|
|
|
let rid = args.rid;
|
2020-03-07 12:20:27 +00:00
|
|
|
let offset = args.offset;
|
|
|
|
let whence = args.whence as u32;
|
|
|
|
// Translate seek mode to Rust repr.
|
|
|
|
let seek_from = match whence {
|
|
|
|
0 => SeekFrom::Start(offset as u64),
|
2020-06-27 19:58:35 +00:00
|
|
|
1 => SeekFrom::Current(offset),
|
|
|
|
2 => SeekFrom::End(offset),
|
2020-03-07 12:20:27 +00:00
|
|
|
_ => {
|
2023-01-27 15:43:16 +00:00
|
|
|
return Err(type_error(format!("Invalid seek mode: {whence}")));
|
2020-03-07 12:20:27 +00:00
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2020-08-28 15:08:24 +00:00
|
|
|
Ok((rid, seek_from))
|
|
|
|
}
|
2020-03-07 12:20:27 +00:00
|
|
|
|
2022-03-14 17:44:15 +00:00
|
|
|
#[op]
|
2022-03-14 22:38:53 +00:00
|
|
|
fn op_seek_sync(state: &mut OpState, args: SeekArgs) -> Result<u64, AnyError> {
|
2020-08-28 15:08:24 +00:00
|
|
|
let (rid, seek_from) = seek_helper(args)?;
|
2022-07-13 15:16:42 +00:00
|
|
|
StdFileResource::with_file(state, rid, |std_file| {
|
2022-05-11 16:48:38 +00:00
|
|
|
std_file.seek(seek_from).map_err(AnyError::from)
|
2022-07-13 15:16:42 +00:00
|
|
|
})
|
2020-08-28 15:08:24 +00:00
|
|
|
}
|
|
|
|
|
2022-03-14 17:44:15 +00:00
|
|
|
#[op]
|
2020-08-28 15:08:24 +00:00
|
|
|
async fn op_seek_async(
|
2020-09-10 13:57:45 +00:00
|
|
|
state: Rc<RefCell<OpState>>,
|
2021-03-18 18:42:01 +00:00
|
|
|
args: SeekArgs,
|
2021-04-05 16:40:24 +00:00
|
|
|
) -> Result<u64, AnyError> {
|
2020-08-28 15:08:24 +00:00
|
|
|
let (rid, seek_from) = seek_helper(args)?;
|
2021-01-08 16:55:17 +00:00
|
|
|
|
2022-07-13 15:16:42 +00:00
|
|
|
StdFileResource::with_file_blocking_task(state, rid, move |std_file| {
|
|
|
|
std_file.seek(seek_from).map_err(AnyError::from)
|
2022-04-19 14:41:31 +00:00
|
|
|
})
|
2022-07-13 15:16:42 +00:00
|
|
|
.await
|
2020-06-26 12:36:35 +00:00
|
|
|
}
|
|
|
|
|
2022-03-14 17:44:15 +00:00
|
|
|
#[op]
|
2020-08-28 15:08:24 +00:00
|
|
|
fn op_fdatasync_sync(
|
2020-09-10 13:57:45 +00:00
|
|
|
state: &mut OpState,
|
2021-04-05 16:40:24 +00:00
|
|
|
rid: ResourceId,
|
|
|
|
) -> Result<(), AnyError> {
|
2022-05-11 16:48:38 +00:00
|
|
|
StdFileResource::with_file(state, rid, |std_file| {
|
|
|
|
std_file.sync_data().map_err(AnyError::from)
|
2022-07-13 15:16:42 +00:00
|
|
|
})
|
2020-08-28 15:08:24 +00:00
|
|
|
}
|
2020-06-26 12:36:35 +00:00
|
|
|
|
2022-03-14 17:44:15 +00:00
|
|
|
#[op]
|
2020-08-28 15:08:24 +00:00
|
|
|
async fn op_fdatasync_async(
|
2020-09-10 13:57:45 +00:00
|
|
|
state: Rc<RefCell<OpState>>,
|
2021-04-05 16:40:24 +00:00
|
|
|
rid: ResourceId,
|
|
|
|
) -> Result<(), AnyError> {
|
2022-07-13 15:16:42 +00:00
|
|
|
StdFileResource::with_file_blocking_task(state, rid, move |std_file| {
|
|
|
|
std_file.sync_data().map_err(AnyError::from)
|
2022-04-19 14:41:31 +00:00
|
|
|
})
|
2022-07-13 15:16:42 +00:00
|
|
|
.await
|
2020-06-21 13:29:44 +00:00
|
|
|
}
|
|
|
|
|
2022-03-14 17:44:15 +00:00
|
|
|
#[op]
|
2022-03-14 22:38:53 +00:00
|
|
|
fn op_fsync_sync(state: &mut OpState, rid: ResourceId) -> Result<(), AnyError> {
|
2022-05-11 16:48:38 +00:00
|
|
|
StdFileResource::with_file(state, rid, |std_file| {
|
|
|
|
std_file.sync_all().map_err(AnyError::from)
|
2022-07-13 15:16:42 +00:00
|
|
|
})
|
2020-08-28 15:08:24 +00:00
|
|
|
}
|
2020-06-21 13:29:44 +00:00
|
|
|
|
2022-03-14 17:44:15 +00:00
|
|
|
#[op]
|
2020-08-28 15:08:24 +00:00
|
|
|
async fn op_fsync_async(
|
2020-09-10 13:57:45 +00:00
|
|
|
state: Rc<RefCell<OpState>>,
|
2021-04-05 16:40:24 +00:00
|
|
|
rid: ResourceId,
|
|
|
|
) -> Result<(), AnyError> {
|
2022-07-13 15:16:42 +00:00
|
|
|
StdFileResource::with_file_blocking_task(state, rid, move |std_file| {
|
|
|
|
std_file.sync_all().map_err(AnyError::from)
|
2022-04-19 14:41:31 +00:00
|
|
|
})
|
2022-07-13 15:16:42 +00:00
|
|
|
.await
|
2020-06-22 12:58:52 +00:00
|
|
|
}
|
|
|
|
|
2022-03-14 17:44:15 +00:00
|
|
|
#[op]
|
2020-08-28 15:08:24 +00:00
|
|
|
fn op_fstat_sync(
|
2020-09-10 13:57:45 +00:00
|
|
|
state: &mut OpState,
|
2021-04-05 16:40:24 +00:00
|
|
|
rid: ResourceId,
|
2022-09-22 09:09:25 +00:00
|
|
|
out_buf: &mut [u32],
|
|
|
|
) -> Result<(), AnyError> {
|
2022-05-11 16:48:38 +00:00
|
|
|
let metadata = StdFileResource::with_file(state, rid, |std_file| {
|
|
|
|
std_file.metadata().map_err(AnyError::from)
|
2020-08-28 15:08:24 +00:00
|
|
|
})?;
|
2022-09-22 09:09:25 +00:00
|
|
|
let stat = get_stat(metadata);
|
|
|
|
stat.write(out_buf);
|
|
|
|
Ok(())
|
2020-08-28 15:08:24 +00:00
|
|
|
}
|
2020-06-22 12:58:52 +00:00
|
|
|
|
2022-03-14 17:44:15 +00:00
|
|
|
#[op]
|
2020-08-28 15:08:24 +00:00
|
|
|
async fn op_fstat_async(
|
2020-09-10 13:57:45 +00:00
|
|
|
state: Rc<RefCell<OpState>>,
|
2021-04-05 16:40:24 +00:00
|
|
|
rid: ResourceId,
|
|
|
|
) -> Result<FsStat, AnyError> {
|
2022-07-13 15:16:42 +00:00
|
|
|
let metadata =
|
|
|
|
StdFileResource::with_file_blocking_task(state, rid, move |std_file| {
|
|
|
|
std_file.metadata().map_err(AnyError::from)
|
|
|
|
})
|
|
|
|
.await?;
|
2021-04-05 16:40:24 +00:00
|
|
|
Ok(get_stat(metadata))
|
2020-03-10 19:11:27 +00:00
|
|
|
}
|
|
|
|
|
2022-03-14 17:44:15 +00:00
|
|
|
#[op]
|
2021-08-24 13:21:31 +00:00
|
|
|
fn op_flock_sync(
|
|
|
|
state: &mut OpState,
|
|
|
|
rid: ResourceId,
|
|
|
|
exclusive: bool,
|
|
|
|
) -> Result<(), AnyError> {
|
|
|
|
use fs3::FileExt;
|
2023-03-07 09:13:44 +00:00
|
|
|
check_unstable(state, "Deno.flockSync");
|
2021-08-24 13:21:31 +00:00
|
|
|
|
2022-05-11 16:48:38 +00:00
|
|
|
StdFileResource::with_file(state, rid, |std_file| {
|
|
|
|
if exclusive {
|
|
|
|
std_file.lock_exclusive()?;
|
|
|
|
} else {
|
|
|
|
std_file.lock_shared()?;
|
2021-08-24 13:21:31 +00:00
|
|
|
}
|
2022-05-11 16:48:38 +00:00
|
|
|
Ok(())
|
2021-08-24 13:21:31 +00:00
|
|
|
})
|
|
|
|
}
|
|
|
|
|
2022-03-14 17:44:15 +00:00
|
|
|
#[op]
|
2021-08-24 13:21:31 +00:00
|
|
|
async fn op_flock_async(
|
|
|
|
state: Rc<RefCell<OpState>>,
|
|
|
|
rid: ResourceId,
|
|
|
|
exclusive: bool,
|
|
|
|
) -> Result<(), AnyError> {
|
|
|
|
use fs3::FileExt;
|
2023-03-07 09:13:44 +00:00
|
|
|
check_unstable2(&state, "Deno.flock");
|
2021-08-24 13:21:31 +00:00
|
|
|
|
2022-07-13 15:16:42 +00:00
|
|
|
StdFileResource::with_file_blocking_task(state, rid, move |std_file| {
|
2021-08-24 13:21:31 +00:00
|
|
|
if exclusive {
|
|
|
|
std_file.lock_exclusive()?;
|
|
|
|
} else {
|
|
|
|
std_file.lock_shared()?;
|
|
|
|
}
|
|
|
|
Ok(())
|
|
|
|
})
|
2022-07-13 15:16:42 +00:00
|
|
|
.await
|
2021-08-24 13:21:31 +00:00
|
|
|
}
|
|
|
|
|
2022-03-14 17:44:15 +00:00
|
|
|
#[op]
|
2021-08-24 13:21:31 +00:00
|
|
|
fn op_funlock_sync(
|
|
|
|
state: &mut OpState,
|
|
|
|
rid: ResourceId,
|
|
|
|
) -> Result<(), AnyError> {
|
|
|
|
use fs3::FileExt;
|
2023-03-07 09:13:44 +00:00
|
|
|
check_unstable(state, "Deno.funlockSync");
|
2021-08-24 13:21:31 +00:00
|
|
|
|
2022-05-11 16:48:38 +00:00
|
|
|
StdFileResource::with_file(state, rid, |std_file| {
|
|
|
|
std_file.unlock()?;
|
|
|
|
Ok(())
|
2021-08-24 13:21:31 +00:00
|
|
|
})
|
|
|
|
}
|
|
|
|
|
2022-03-14 17:44:15 +00:00
|
|
|
#[op]
|
2021-08-24 13:21:31 +00:00
|
|
|
async fn op_funlock_async(
|
|
|
|
state: Rc<RefCell<OpState>>,
|
|
|
|
rid: ResourceId,
|
|
|
|
) -> Result<(), AnyError> {
|
|
|
|
use fs3::FileExt;
|
2023-03-07 09:13:44 +00:00
|
|
|
check_unstable2(&state, "Deno.funlock");
|
2021-08-24 13:21:31 +00:00
|
|
|
|
2022-07-13 15:16:42 +00:00
|
|
|
StdFileResource::with_file_blocking_task(state, rid, move |std_file| {
|
2021-08-24 13:21:31 +00:00
|
|
|
std_file.unlock()?;
|
|
|
|
Ok(())
|
|
|
|
})
|
2022-07-13 15:16:42 +00:00
|
|
|
.await
|
2021-08-24 13:21:31 +00:00
|
|
|
}
|
|
|
|
|
2022-03-14 17:44:15 +00:00
|
|
|
#[op]
|
2022-03-14 22:38:53 +00:00
|
|
|
fn op_umask(state: &mut OpState, mask: Option<u32>) -> Result<u32, AnyError> {
|
2023-03-07 09:13:44 +00:00
|
|
|
check_unstable(state, "Deno.umask");
|
2020-03-10 19:11:27 +00:00
|
|
|
// TODO implement umask for Windows
|
|
|
|
// see https://github.com/nodejs/node/blob/master/src/node_process_methods.cc
|
|
|
|
// and https://docs.microsoft.com/fr-fr/cpp/c-runtime-library/reference/umask?view=vs-2019
|
|
|
|
#[cfg(not(unix))]
|
|
|
|
{
|
2021-04-05 16:40:24 +00:00
|
|
|
let _ = mask; // avoid unused warning.
|
2020-09-14 16:48:57 +00:00
|
|
|
Err(not_supported())
|
2020-03-10 19:11:27 +00:00
|
|
|
}
|
|
|
|
#[cfg(unix)]
|
|
|
|
{
|
|
|
|
use nix::sys::stat::mode_t;
|
|
|
|
use nix::sys::stat::umask;
|
|
|
|
use nix::sys::stat::Mode;
|
2021-04-05 16:40:24 +00:00
|
|
|
let r = if let Some(mask) = mask {
|
2020-03-10 19:11:27 +00:00
|
|
|
// If mask provided, return previous.
|
|
|
|
umask(Mode::from_bits_truncate(mask as mode_t))
|
|
|
|
} else {
|
|
|
|
// If no mask provided, we query the current. Requires two syscalls.
|
|
|
|
let prev = umask(Mode::from_bits_truncate(0o777));
|
|
|
|
let _ = umask(prev);
|
|
|
|
prev
|
|
|
|
};
|
2022-12-17 22:20:15 +00:00
|
|
|
#[cfg(target_os = "linux")]
|
|
|
|
{
|
|
|
|
Ok(r.bits())
|
|
|
|
}
|
|
|
|
#[cfg(target_os = "macos")]
|
|
|
|
{
|
|
|
|
Ok(r.bits() as u32)
|
|
|
|
}
|
2020-03-10 19:11:27 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-03-14 17:44:15 +00:00
|
|
|
#[op]
|
2023-03-04 23:44:59 +00:00
|
|
|
fn op_chdir<P>(state: &mut OpState, directory: &str) -> Result<(), AnyError>
|
|
|
|
where
|
|
|
|
P: FsPermissions + 'static,
|
|
|
|
{
|
2021-04-05 16:40:24 +00:00
|
|
|
let d = PathBuf::from(&directory);
|
2023-03-04 23:44:59 +00:00
|
|
|
state.borrow_mut::<P>().check_read(&d, "Deno.chdir()")?;
|
2023-02-16 19:21:03 +00:00
|
|
|
set_current_dir(&d)
|
|
|
|
.map_err(|err| default_err_mapper(err, format!("chdir '{directory}'")))?;
|
2021-04-05 16:40:24 +00:00
|
|
|
Ok(())
|
2019-08-26 14:18:42 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
#[derive(Deserialize)]
|
|
|
|
#[serde(rename_all = "camelCase")]
|
2021-03-18 18:42:01 +00:00
|
|
|
pub struct MkdirArgs {
|
2019-08-26 14:18:42 +00:00
|
|
|
path: String,
|
|
|
|
recursive: bool,
|
2020-03-11 20:14:23 +00:00
|
|
|
mode: Option<u32>,
|
2019-08-14 15:03:02 +00:00
|
|
|
}
|
|
|
|
|
2022-03-14 17:44:15 +00:00
|
|
|
#[op]
|
2023-03-04 23:44:59 +00:00
|
|
|
fn op_mkdir_sync<P>(
|
|
|
|
state: &mut OpState,
|
|
|
|
args: MkdirArgs,
|
|
|
|
) -> Result<(), AnyError>
|
|
|
|
where
|
|
|
|
P: FsPermissions + 'static,
|
|
|
|
{
|
2020-05-29 15:27:43 +00:00
|
|
|
let path = Path::new(&args.path).to_path_buf();
|
2020-03-20 13:46:26 +00:00
|
|
|
let mode = args.mode.unwrap_or(0o777) & 0o777;
|
2022-09-27 20:36:33 +00:00
|
|
|
state
|
2023-03-04 23:44:59 +00:00
|
|
|
.borrow_mut::<P>()
|
2023-01-07 16:25:34 +00:00
|
|
|
.check_write(&path, "Deno.mkdirSync()")?;
|
2020-08-28 15:08:24 +00:00
|
|
|
debug!("op_mkdir {} {:o} {}", path.display(), mode, args.recursive);
|
|
|
|
let mut builder = std::fs::DirBuilder::new();
|
|
|
|
builder.recursive(args.recursive);
|
|
|
|
#[cfg(unix)]
|
|
|
|
{
|
|
|
|
use std::os::unix::fs::DirBuilderExt;
|
|
|
|
builder.mode(mode);
|
|
|
|
}
|
2021-10-11 13:21:18 +00:00
|
|
|
builder.create(&path).map_err(|err| {
|
2023-02-16 19:21:03 +00:00
|
|
|
default_err_mapper(err, format!("mkdir '{}'", path.display()))
|
2021-10-11 13:21:18 +00:00
|
|
|
})?;
|
2021-04-05 16:40:24 +00:00
|
|
|
Ok(())
|
2020-08-28 15:08:24 +00:00
|
|
|
}
|
2019-08-14 15:03:02 +00:00
|
|
|
|
2022-03-14 17:44:15 +00:00
|
|
|
#[op]
|
2023-03-04 23:44:59 +00:00
|
|
|
async fn op_mkdir_async<P>(
|
2020-09-10 13:57:45 +00:00
|
|
|
state: Rc<RefCell<OpState>>,
|
2021-03-18 18:42:01 +00:00
|
|
|
args: MkdirArgs,
|
2023-03-04 23:44:59 +00:00
|
|
|
) -> Result<(), AnyError>
|
|
|
|
where
|
|
|
|
P: FsPermissions + 'static,
|
|
|
|
{
|
2020-08-28 15:08:24 +00:00
|
|
|
let path = Path::new(&args.path).to_path_buf();
|
|
|
|
let mode = args.mode.unwrap_or(0o777) & 0o777;
|
2020-09-10 13:57:45 +00:00
|
|
|
|
2020-09-19 23:17:35 +00:00
|
|
|
{
|
2021-04-12 02:15:43 +00:00
|
|
|
let mut state = state.borrow_mut();
|
2023-03-04 23:44:59 +00:00
|
|
|
state.borrow_mut::<P>().check_write(&path, "Deno.mkdir()")?;
|
2020-09-19 23:17:35 +00:00
|
|
|
}
|
2020-09-10 13:57:45 +00:00
|
|
|
|
2020-08-28 15:08:24 +00:00
|
|
|
tokio::task::spawn_blocking(move || {
|
2020-03-11 20:14:23 +00:00
|
|
|
debug!("op_mkdir {} {:o} {}", path.display(), mode, args.recursive);
|
2020-03-20 13:46:26 +00:00
|
|
|
let mut builder = std::fs::DirBuilder::new();
|
|
|
|
builder.recursive(args.recursive);
|
|
|
|
#[cfg(unix)]
|
|
|
|
{
|
|
|
|
use std::os::unix::fs::DirBuilderExt;
|
|
|
|
builder.mode(mode);
|
|
|
|
}
|
2021-10-11 13:21:18 +00:00
|
|
|
builder.create(&path).map_err(|err| {
|
2023-02-16 19:21:03 +00:00
|
|
|
default_err_mapper(err, format!("mkdir '{}'", path.display()))
|
2021-10-11 13:21:18 +00:00
|
|
|
})?;
|
2021-04-05 16:40:24 +00:00
|
|
|
Ok(())
|
2019-08-14 15:03:02 +00:00
|
|
|
})
|
2020-08-28 15:08:24 +00:00
|
|
|
.await
|
|
|
|
.unwrap()
|
2019-08-14 15:03:02 +00:00
|
|
|
}
|
|
|
|
|
2022-09-22 09:09:25 +00:00
|
|
|
#[op]
|
2023-03-04 23:44:59 +00:00
|
|
|
fn op_chmod_sync<P>(
|
2022-09-22 09:09:25 +00:00
|
|
|
state: &mut OpState,
|
2023-03-03 13:34:10 +00:00
|
|
|
path: &str,
|
2019-08-26 14:18:42 +00:00
|
|
|
mode: u32,
|
2023-03-04 23:44:59 +00:00
|
|
|
) -> Result<(), AnyError>
|
|
|
|
where
|
|
|
|
P: FsPermissions + 'static,
|
|
|
|
{
|
2023-03-03 13:34:10 +00:00
|
|
|
let path = Path::new(path);
|
2022-09-22 09:09:25 +00:00
|
|
|
let mode = mode & 0o777;
|
2019-08-14 15:03:02 +00:00
|
|
|
|
2022-09-27 20:36:33 +00:00
|
|
|
state
|
2023-03-04 23:44:59 +00:00
|
|
|
.borrow_mut::<P>()
|
2023-01-07 16:25:34 +00:00
|
|
|
.check_write(path, "Deno.chmodSync()")?;
|
2022-04-18 22:00:14 +00:00
|
|
|
raw_chmod(path, mode)
|
2020-08-28 15:08:24 +00:00
|
|
|
}
|
2019-08-14 15:03:02 +00:00
|
|
|
|
2022-03-14 17:44:15 +00:00
|
|
|
#[op]
|
2023-03-04 23:44:59 +00:00
|
|
|
async fn op_chmod_async<P>(
|
2020-09-10 13:57:45 +00:00
|
|
|
state: Rc<RefCell<OpState>>,
|
2022-09-22 09:09:25 +00:00
|
|
|
path: String,
|
|
|
|
mode: u32,
|
2023-03-04 23:44:59 +00:00
|
|
|
) -> Result<(), AnyError>
|
|
|
|
where
|
|
|
|
P: FsPermissions + 'static,
|
|
|
|
{
|
2022-09-22 09:09:25 +00:00
|
|
|
let path = Path::new(&path).to_path_buf();
|
|
|
|
let mode = mode & 0o777;
|
2020-09-10 13:57:45 +00:00
|
|
|
|
2020-09-19 23:17:35 +00:00
|
|
|
{
|
2021-04-12 02:15:43 +00:00
|
|
|
let mut state = state.borrow_mut();
|
2023-03-04 23:44:59 +00:00
|
|
|
state.borrow_mut::<P>().check_write(&path, "Deno.chmod()")?;
|
2020-09-19 23:17:35 +00:00
|
|
|
}
|
2020-09-10 13:57:45 +00:00
|
|
|
|
2022-09-22 09:09:25 +00:00
|
|
|
tokio::task::spawn_blocking(move || raw_chmod(&path, mode))
|
|
|
|
.await
|
|
|
|
.unwrap()
|
2019-08-14 15:03:02 +00:00
|
|
|
}
|
|
|
|
|
2022-04-18 22:00:14 +00:00
|
|
|
fn raw_chmod(path: &Path, _raw_mode: u32) -> Result<(), AnyError> {
|
2023-02-16 19:21:03 +00:00
|
|
|
let err_mapper =
|
|
|
|
|err| default_err_mapper(err, format!("chmod '{}'", path.display()));
|
2022-04-18 22:00:14 +00:00
|
|
|
#[cfg(unix)]
|
|
|
|
{
|
|
|
|
use std::os::unix::fs::PermissionsExt;
|
|
|
|
let permissions = PermissionsExt::from_mode(_raw_mode);
|
2022-11-18 01:59:10 +00:00
|
|
|
std::fs::set_permissions(path, permissions).map_err(err_mapper)?;
|
2022-04-18 22:00:14 +00:00
|
|
|
Ok(())
|
|
|
|
}
|
|
|
|
// TODO Implement chmod for Windows (#4357)
|
|
|
|
#[cfg(not(unix))]
|
|
|
|
{
|
|
|
|
// Still check file/dir exists on Windows
|
2022-11-18 17:54:01 +00:00
|
|
|
let _metadata = std::fs::metadata(path).map_err(err_mapper)?;
|
2022-04-18 22:00:14 +00:00
|
|
|
Err(not_supported())
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-09-22 09:09:25 +00:00
|
|
|
#[op]
|
2023-03-04 23:44:59 +00:00
|
|
|
fn op_chown_sync<P>(
|
2022-09-22 09:09:25 +00:00
|
|
|
state: &mut OpState,
|
2023-03-03 13:34:10 +00:00
|
|
|
path: &str,
|
2022-09-22 14:33:52 +00:00
|
|
|
#[cfg_attr(windows, allow(unused_variables))] uid: Option<u32>,
|
|
|
|
#[cfg_attr(windows, allow(unused_variables))] gid: Option<u32>,
|
2023-03-04 23:44:59 +00:00
|
|
|
) -> Result<(), AnyError>
|
|
|
|
where
|
|
|
|
P: FsPermissions + 'static,
|
|
|
|
{
|
2023-03-03 13:34:10 +00:00
|
|
|
let path = Path::new(path).to_path_buf();
|
2022-09-27 20:36:33 +00:00
|
|
|
state
|
2023-03-04 23:44:59 +00:00
|
|
|
.borrow_mut::<P>()
|
2023-01-07 16:25:34 +00:00
|
|
|
.check_write(&path, "Deno.chownSync()")?;
|
2020-08-28 15:08:24 +00:00
|
|
|
#[cfg(unix)]
|
|
|
|
{
|
2023-01-15 04:18:58 +00:00
|
|
|
use nix::unistd::chown;
|
|
|
|
use nix::unistd::Gid;
|
|
|
|
use nix::unistd::Uid;
|
2022-09-22 09:09:25 +00:00
|
|
|
let nix_uid = uid.map(Uid::from_raw);
|
|
|
|
let nix_gid = gid.map(Gid::from_raw);
|
2021-10-11 13:21:18 +00:00
|
|
|
chown(&path, nix_uid, nix_gid).map_err(|err| {
|
|
|
|
custom_error(
|
|
|
|
get_nix_error_class(&err),
|
|
|
|
format!("{}, chown '{}'", err.desc(), path.display()),
|
|
|
|
)
|
|
|
|
})?;
|
2021-04-05 16:40:24 +00:00
|
|
|
Ok(())
|
2020-08-28 15:08:24 +00:00
|
|
|
}
|
|
|
|
// TODO Implement chown for Windows
|
|
|
|
#[cfg(not(unix))]
|
|
|
|
{
|
2020-09-14 16:48:57 +00:00
|
|
|
Err(generic_error("Not implemented"))
|
2020-08-28 15:08:24 +00:00
|
|
|
}
|
|
|
|
}
|
2019-08-26 14:18:42 +00:00
|
|
|
|
2022-03-14 17:44:15 +00:00
|
|
|
#[op]
|
2023-03-04 23:44:59 +00:00
|
|
|
async fn op_chown_async<P>(
|
2020-09-10 13:57:45 +00:00
|
|
|
state: Rc<RefCell<OpState>>,
|
2022-09-22 09:09:25 +00:00
|
|
|
path: String,
|
2022-09-22 14:33:52 +00:00
|
|
|
#[cfg_attr(windows, allow(unused_variables))] uid: Option<u32>,
|
|
|
|
#[cfg_attr(windows, allow(unused_variables))] gid: Option<u32>,
|
2023-03-04 23:44:59 +00:00
|
|
|
) -> Result<(), AnyError>
|
|
|
|
where
|
|
|
|
P: FsPermissions + 'static,
|
|
|
|
{
|
2022-09-22 09:09:25 +00:00
|
|
|
let path = Path::new(&path).to_path_buf();
|
2020-09-10 13:57:45 +00:00
|
|
|
|
2020-09-19 23:17:35 +00:00
|
|
|
{
|
2021-04-12 02:15:43 +00:00
|
|
|
let mut state = state.borrow_mut();
|
2023-03-04 23:44:59 +00:00
|
|
|
state.borrow_mut::<P>().check_write(&path, "Deno.chown()")?;
|
2020-09-19 23:17:35 +00:00
|
|
|
}
|
2020-09-10 13:57:45 +00:00
|
|
|
|
2020-08-28 15:08:24 +00:00
|
|
|
tokio::task::spawn_blocking(move || {
|
2020-03-20 13:46:26 +00:00
|
|
|
#[cfg(unix)]
|
|
|
|
{
|
2023-01-15 04:18:58 +00:00
|
|
|
use nix::unistd::chown;
|
|
|
|
use nix::unistd::Gid;
|
|
|
|
use nix::unistd::Uid;
|
2022-09-22 09:09:25 +00:00
|
|
|
let nix_uid = uid.map(Uid::from_raw);
|
|
|
|
let nix_gid = gid.map(Gid::from_raw);
|
2021-10-11 13:21:18 +00:00
|
|
|
chown(&path, nix_uid, nix_gid).map_err(|err| {
|
|
|
|
custom_error(
|
|
|
|
get_nix_error_class(&err),
|
|
|
|
format!("{}, chown '{}'", err.desc(), path.display()),
|
|
|
|
)
|
|
|
|
})?;
|
2021-04-05 16:40:24 +00:00
|
|
|
Ok(())
|
2020-03-20 13:46:26 +00:00
|
|
|
}
|
|
|
|
// TODO Implement chown for Windows
|
|
|
|
#[cfg(not(unix))]
|
2020-09-14 16:48:57 +00:00
|
|
|
Err(not_supported())
|
2019-08-14 15:03:02 +00:00
|
|
|
})
|
2020-08-28 15:08:24 +00:00
|
|
|
.await
|
|
|
|
.unwrap()
|
2019-08-14 15:03:02 +00:00
|
|
|
}
|
|
|
|
|
2022-03-14 17:44:15 +00:00
|
|
|
#[op]
|
2023-03-04 23:44:59 +00:00
|
|
|
fn op_remove_sync<P>(
|
2020-09-10 13:57:45 +00:00
|
|
|
state: &mut OpState,
|
2023-03-03 13:34:10 +00:00
|
|
|
path: &str,
|
2022-09-22 09:09:25 +00:00
|
|
|
recursive: bool,
|
2023-03-04 23:44:59 +00:00
|
|
|
) -> Result<(), AnyError>
|
|
|
|
where
|
|
|
|
P: FsPermissions + 'static,
|
|
|
|
{
|
2023-03-03 13:34:10 +00:00
|
|
|
let path = PathBuf::from(path);
|
2020-08-28 15:08:24 +00:00
|
|
|
|
2022-09-27 20:36:33 +00:00
|
|
|
state
|
2023-03-04 23:44:59 +00:00
|
|
|
.borrow_mut::<P>()
|
2023-01-07 16:25:34 +00:00
|
|
|
.check_write(&path, "Deno.removeSync()")?;
|
2020-08-28 15:08:24 +00:00
|
|
|
|
|
|
|
#[cfg(not(unix))]
|
|
|
|
use std::os::windows::prelude::MetadataExt;
|
|
|
|
|
2023-02-16 19:21:03 +00:00
|
|
|
let err_mapper =
|
|
|
|
|err| default_err_mapper(err, format!("remove '{}'", path.display()));
|
2021-10-11 13:21:18 +00:00
|
|
|
let metadata = std::fs::symlink_metadata(&path).map_err(err_mapper)?;
|
2020-08-28 15:08:24 +00:00
|
|
|
|
|
|
|
let file_type = metadata.file_type();
|
|
|
|
if file_type.is_file() {
|
2021-10-11 13:21:18 +00:00
|
|
|
std::fs::remove_file(&path).map_err(err_mapper)?;
|
2020-08-28 15:08:24 +00:00
|
|
|
} else if recursive {
|
2021-10-11 13:21:18 +00:00
|
|
|
std::fs::remove_dir_all(&path).map_err(err_mapper)?;
|
2020-08-28 15:08:24 +00:00
|
|
|
} else if file_type.is_symlink() {
|
|
|
|
#[cfg(unix)]
|
2021-10-11 13:21:18 +00:00
|
|
|
std::fs::remove_file(&path).map_err(err_mapper)?;
|
2020-08-28 15:08:24 +00:00
|
|
|
#[cfg(not(unix))]
|
|
|
|
{
|
|
|
|
use winapi::um::winnt::FILE_ATTRIBUTE_DIRECTORY;
|
|
|
|
if metadata.file_attributes() & FILE_ATTRIBUTE_DIRECTORY != 0 {
|
2021-10-11 13:21:18 +00:00
|
|
|
std::fs::remove_dir(&path).map_err(err_mapper)?;
|
2020-08-28 15:08:24 +00:00
|
|
|
} else {
|
2021-10-11 13:21:18 +00:00
|
|
|
std::fs::remove_file(&path).map_err(err_mapper)?;
|
2020-08-28 15:08:24 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
} else if file_type.is_dir() {
|
2021-10-11 13:21:18 +00:00
|
|
|
std::fs::remove_dir(&path).map_err(err_mapper)?;
|
2020-08-28 15:08:24 +00:00
|
|
|
} else {
|
|
|
|
// pipes, sockets, etc...
|
2021-10-11 13:21:18 +00:00
|
|
|
std::fs::remove_file(&path).map_err(err_mapper)?;
|
2020-08-28 15:08:24 +00:00
|
|
|
}
|
2021-04-05 16:40:24 +00:00
|
|
|
Ok(())
|
2020-08-28 15:08:24 +00:00
|
|
|
}
|
|
|
|
|
2022-03-14 17:44:15 +00:00
|
|
|
#[op]
|
2023-03-04 23:44:59 +00:00
|
|
|
async fn op_remove_async<P>(
|
2020-09-10 13:57:45 +00:00
|
|
|
state: Rc<RefCell<OpState>>,
|
2022-09-22 09:09:25 +00:00
|
|
|
path: String,
|
|
|
|
recursive: bool,
|
2023-03-04 23:44:59 +00:00
|
|
|
) -> Result<(), AnyError>
|
|
|
|
where
|
|
|
|
P: FsPermissions + 'static,
|
|
|
|
{
|
2022-09-22 09:09:25 +00:00
|
|
|
let path = PathBuf::from(&path);
|
2019-08-14 15:03:02 +00:00
|
|
|
|
2020-09-19 23:17:35 +00:00
|
|
|
{
|
2021-04-12 02:15:43 +00:00
|
|
|
let mut state = state.borrow_mut();
|
2022-09-27 20:36:33 +00:00
|
|
|
state
|
2023-03-04 23:44:59 +00:00
|
|
|
.borrow_mut::<P>()
|
2023-01-07 16:25:34 +00:00
|
|
|
.check_write(&path, "Deno.remove()")?;
|
2020-09-19 23:17:35 +00:00
|
|
|
}
|
2019-08-14 15:03:02 +00:00
|
|
|
|
2020-08-28 15:08:24 +00:00
|
|
|
tokio::task::spawn_blocking(move || {
|
2020-05-18 12:50:44 +00:00
|
|
|
#[cfg(not(unix))]
|
|
|
|
use std::os::windows::prelude::MetadataExt;
|
2023-02-16 19:21:03 +00:00
|
|
|
let err_mapper =
|
|
|
|
|err| default_err_mapper(err, format!("remove '{}'", path.display()));
|
2021-10-11 13:21:18 +00:00
|
|
|
let metadata = std::fs::symlink_metadata(&path).map_err(err_mapper)?;
|
2020-05-18 12:50:44 +00:00
|
|
|
|
2020-08-28 15:08:24 +00:00
|
|
|
debug!("op_remove_async {} {}", path.display(), recursive);
|
2020-02-03 13:20:15 +00:00
|
|
|
let file_type = metadata.file_type();
|
2020-05-18 12:50:44 +00:00
|
|
|
if file_type.is_file() {
|
2021-10-11 13:21:18 +00:00
|
|
|
std::fs::remove_file(&path).map_err(err_mapper)?;
|
2019-08-14 15:03:02 +00:00
|
|
|
} else if recursive {
|
2021-10-11 13:21:18 +00:00
|
|
|
std::fs::remove_dir_all(&path).map_err(err_mapper)?;
|
2020-05-18 12:50:44 +00:00
|
|
|
} else if file_type.is_symlink() {
|
|
|
|
#[cfg(unix)]
|
2021-10-11 13:21:18 +00:00
|
|
|
std::fs::remove_file(&path).map_err(err_mapper)?;
|
2020-05-18 12:50:44 +00:00
|
|
|
#[cfg(not(unix))]
|
|
|
|
{
|
|
|
|
use winapi::um::winnt::FILE_ATTRIBUTE_DIRECTORY;
|
|
|
|
if metadata.file_attributes() & FILE_ATTRIBUTE_DIRECTORY != 0 {
|
2021-10-11 13:21:18 +00:00
|
|
|
std::fs::remove_dir(&path).map_err(err_mapper)?;
|
2020-05-18 12:50:44 +00:00
|
|
|
} else {
|
2021-10-11 13:21:18 +00:00
|
|
|
std::fs::remove_file(&path).map_err(err_mapper)?;
|
2020-05-18 12:50:44 +00:00
|
|
|
}
|
|
|
|
}
|
2020-05-30 18:48:26 +00:00
|
|
|
} else if file_type.is_dir() {
|
2021-10-11 13:21:18 +00:00
|
|
|
std::fs::remove_dir(&path).map_err(err_mapper)?;
|
2020-05-30 18:48:26 +00:00
|
|
|
} else {
|
|
|
|
// pipes, sockets, etc...
|
2021-10-11 13:21:18 +00:00
|
|
|
std::fs::remove_file(&path).map_err(err_mapper)?;
|
2019-08-14 15:03:02 +00:00
|
|
|
}
|
2021-04-05 16:40:24 +00:00
|
|
|
Ok(())
|
2019-08-14 15:03:02 +00:00
|
|
|
})
|
2020-08-28 15:08:24 +00:00
|
|
|
.await
|
|
|
|
.unwrap()
|
2019-08-14 15:03:02 +00:00
|
|
|
}
|
|
|
|
|
2022-03-14 17:44:15 +00:00
|
|
|
#[op]
|
2023-03-04 23:44:59 +00:00
|
|
|
fn op_copy_file_sync<P>(
|
2020-09-10 13:57:45 +00:00
|
|
|
state: &mut OpState,
|
2023-03-03 13:34:10 +00:00
|
|
|
from: &str,
|
|
|
|
to: &str,
|
2023-03-04 23:44:59 +00:00
|
|
|
) -> Result<(), AnyError>
|
|
|
|
where
|
|
|
|
P: FsPermissions + 'static,
|
|
|
|
{
|
2023-03-03 13:34:10 +00:00
|
|
|
let from_path = PathBuf::from(from);
|
|
|
|
let to_path = PathBuf::from(to);
|
2020-08-28 15:08:24 +00:00
|
|
|
|
2023-03-04 23:44:59 +00:00
|
|
|
let permissions = state.borrow_mut::<P>();
|
2023-01-07 16:25:34 +00:00
|
|
|
permissions.check_read(&from_path, "Deno.copyFileSync()")?;
|
|
|
|
permissions.check_write(&to_path, "Deno.copyFileSync()")?;
|
2020-08-28 15:08:24 +00:00
|
|
|
|
|
|
|
// On *nix, Rust reports non-existent `from` as ErrorKind::InvalidInput
|
|
|
|
// See https://github.com/rust-lang/rust/issues/54800
|
|
|
|
// Once the issue is resolved, we should remove this workaround.
|
2022-09-22 09:09:25 +00:00
|
|
|
if cfg!(unix) && !from_path.is_file() {
|
2021-10-11 13:21:18 +00:00
|
|
|
return Err(custom_error(
|
|
|
|
"NotFound",
|
|
|
|
format!(
|
|
|
|
"File not found, copy '{}' -> '{}'",
|
2022-09-22 09:09:25 +00:00
|
|
|
from_path.display(),
|
|
|
|
to_path.display()
|
2021-10-11 13:21:18 +00:00
|
|
|
),
|
|
|
|
));
|
2020-08-28 15:08:24 +00:00
|
|
|
}
|
|
|
|
|
2023-02-16 19:21:03 +00:00
|
|
|
let err_mapper = |err| {
|
|
|
|
default_err_mapper(
|
|
|
|
err,
|
|
|
|
format!("copy '{}' -> '{}'", from_path.display(), to_path.display()),
|
2021-10-11 13:21:18 +00:00
|
|
|
)
|
|
|
|
};
|
2022-09-22 09:09:25 +00:00
|
|
|
|
|
|
|
#[cfg(target_os = "macos")]
|
|
|
|
{
|
|
|
|
use libc::clonefile;
|
|
|
|
use libc::stat;
|
|
|
|
use libc::unlink;
|
|
|
|
use std::ffi::CString;
|
|
|
|
use std::io::Read;
|
2023-01-14 13:45:30 +00:00
|
|
|
use std::os::unix::fs::OpenOptionsExt;
|
|
|
|
use std::os::unix::fs::PermissionsExt;
|
2022-09-22 09:09:25 +00:00
|
|
|
|
|
|
|
let from = CString::new(from).unwrap();
|
|
|
|
let to = CString::new(to).unwrap();
|
|
|
|
|
|
|
|
// SAFETY: `from` and `to` are valid C strings.
|
|
|
|
// std::fs::copy does open() + fcopyfile() on macOS. We try to use
|
|
|
|
// clonefile() instead, which is more efficient.
|
|
|
|
unsafe {
|
|
|
|
let mut st = std::mem::zeroed();
|
|
|
|
let ret = stat(from.as_ptr(), &mut st);
|
|
|
|
if ret != 0 {
|
|
|
|
return Err(err_mapper(Error::last_os_error()).into());
|
|
|
|
}
|
|
|
|
|
|
|
|
if st.st_size > 128 * 1024 {
|
|
|
|
// Try unlink. If it fails, we are going to try clonefile() anyway.
|
|
|
|
let _ = unlink(to.as_ptr());
|
|
|
|
// Matches rust stdlib behavior for io::copy.
|
|
|
|
// https://github.com/rust-lang/rust/blob/3fdd578d72a24d4efc2fe2ad18eec3b6ba72271e/library/std/src/sys/unix/fs.rs#L1613-L1616
|
|
|
|
if clonefile(from.as_ptr(), to.as_ptr(), 0) == 0 {
|
|
|
|
return Ok(());
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
// Do a regular copy. fcopyfile() is an overkill for < 128KB
|
|
|
|
// files.
|
|
|
|
let mut buf = [0u8; 128 * 1024];
|
|
|
|
let mut from_file =
|
|
|
|
std::fs::File::open(&from_path).map_err(err_mapper)?;
|
2023-01-14 13:45:30 +00:00
|
|
|
let perm = from_file.metadata().map_err(err_mapper)?.permissions();
|
|
|
|
|
|
|
|
let mut to_file = std::fs::OpenOptions::new()
|
|
|
|
// create the file with the correct mode right away
|
|
|
|
.mode(perm.mode())
|
|
|
|
.write(true)
|
|
|
|
.create(true)
|
|
|
|
.truncate(true)
|
|
|
|
.open(&to_path)
|
|
|
|
.map_err(err_mapper)?;
|
|
|
|
let writer_metadata = to_file.metadata()?;
|
|
|
|
if writer_metadata.is_file() {
|
|
|
|
// Set the correct file permissions, in case the file already existed.
|
|
|
|
// Don't set the permissions on already existing non-files like
|
|
|
|
// pipes/FIFOs or device nodes.
|
|
|
|
to_file.set_permissions(perm)?;
|
|
|
|
}
|
2022-09-22 09:09:25 +00:00
|
|
|
loop {
|
|
|
|
let nread = from_file.read(&mut buf).map_err(err_mapper)?;
|
|
|
|
if nread == 0 {
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
to_file.write_all(&buf[..nread]).map_err(err_mapper)?;
|
|
|
|
}
|
|
|
|
return Ok(());
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// clonefile() failed, fall back to std::fs::copy().
|
|
|
|
}
|
|
|
|
|
2020-08-28 15:08:24 +00:00
|
|
|
// returns size of from as u64 (we ignore)
|
2022-09-22 09:09:25 +00:00
|
|
|
std::fs::copy(&from_path, &to_path).map_err(err_mapper)?;
|
2021-04-05 16:40:24 +00:00
|
|
|
Ok(())
|
2020-08-28 15:08:24 +00:00
|
|
|
}
|
|
|
|
|
2022-03-14 17:44:15 +00:00
|
|
|
#[op]
|
2023-03-04 23:44:59 +00:00
|
|
|
async fn op_copy_file_async<P>(
|
2020-09-10 13:57:45 +00:00
|
|
|
state: Rc<RefCell<OpState>>,
|
2022-09-22 09:09:25 +00:00
|
|
|
from: String,
|
|
|
|
to: String,
|
2023-03-04 23:44:59 +00:00
|
|
|
) -> Result<(), AnyError>
|
|
|
|
where
|
|
|
|
P: FsPermissions + 'static,
|
|
|
|
{
|
2022-09-22 09:09:25 +00:00
|
|
|
let from = PathBuf::from(&from);
|
|
|
|
let to = PathBuf::from(&to);
|
2019-08-26 14:18:42 +00:00
|
|
|
|
2020-09-19 23:17:35 +00:00
|
|
|
{
|
2021-04-12 02:15:43 +00:00
|
|
|
let mut state = state.borrow_mut();
|
2023-03-04 23:44:59 +00:00
|
|
|
let permissions = state.borrow_mut::<P>();
|
2023-01-07 16:25:34 +00:00
|
|
|
permissions.check_read(&from, "Deno.copyFile()")?;
|
|
|
|
permissions.check_write(&to, "Deno.copyFile()")?;
|
2020-09-19 23:17:35 +00:00
|
|
|
}
|
2019-08-14 15:03:02 +00:00
|
|
|
|
2020-08-28 15:08:24 +00:00
|
|
|
tokio::task::spawn_blocking(move || {
|
2020-03-02 15:19:42 +00:00
|
|
|
// On *nix, Rust reports non-existent `from` as ErrorKind::InvalidInput
|
2019-08-14 15:03:02 +00:00
|
|
|
// See https://github.com/rust-lang/rust/issues/54800
|
2020-03-02 15:19:42 +00:00
|
|
|
// Once the issue is resolved, we should remove this workaround.
|
2019-08-14 15:03:02 +00:00
|
|
|
if cfg!(unix) && !from.is_file() {
|
2021-10-11 13:21:18 +00:00
|
|
|
return Err(custom_error(
|
|
|
|
"NotFound",
|
|
|
|
format!(
|
|
|
|
"File not found, copy '{}' -> '{}'",
|
|
|
|
from.display(),
|
|
|
|
to.display()
|
|
|
|
),
|
|
|
|
));
|
2019-08-14 15:03:02 +00:00
|
|
|
}
|
2020-03-15 02:57:42 +00:00
|
|
|
// returns size of from as u64 (we ignore)
|
2023-02-16 19:21:03 +00:00
|
|
|
std::fs::copy(&from, &to).map_err(|err| {
|
|
|
|
default_err_mapper(
|
|
|
|
err,
|
|
|
|
format!("copy '{}' -> '{}'", from.display(), to.display()),
|
|
|
|
)
|
|
|
|
})?;
|
2021-04-05 16:40:24 +00:00
|
|
|
Ok(())
|
2019-08-14 15:03:02 +00:00
|
|
|
})
|
2020-08-28 15:08:24 +00:00
|
|
|
.await
|
|
|
|
.unwrap()
|
2019-08-14 15:03:02 +00:00
|
|
|
}
|
|
|
|
|
2022-09-22 09:09:25 +00:00
|
|
|
fn to_msec(maybe_time: Result<SystemTime, io::Error>) -> (u64, bool) {
|
2020-04-27 18:09:56 +00:00
|
|
|
match maybe_time {
|
2022-09-22 09:09:25 +00:00
|
|
|
Ok(time) => (
|
|
|
|
time
|
2020-04-27 18:09:56 +00:00
|
|
|
.duration_since(UNIX_EPOCH)
|
2021-04-05 16:40:24 +00:00
|
|
|
.map(|t| t.as_millis() as u64)
|
2022-09-22 09:09:25 +00:00
|
|
|
.unwrap_or_else(|err| err.duration().as_millis() as u64),
|
|
|
|
true,
|
|
|
|
),
|
|
|
|
Err(_) => (0, false),
|
2020-04-27 18:09:56 +00:00
|
|
|
}
|
2019-08-14 15:03:02 +00:00
|
|
|
}
|
|
|
|
|
2022-09-22 09:09:25 +00:00
|
|
|
macro_rules! create_struct_writer {
|
|
|
|
(pub struct $name:ident { $($field:ident: $type:ty),* $(,)? }) => {
|
|
|
|
impl $name {
|
|
|
|
fn write(self, buf: &mut [u32]) {
|
|
|
|
let mut offset = 0;
|
|
|
|
$(
|
|
|
|
let value = self.$field as u64;
|
|
|
|
buf[offset] = value as u32;
|
|
|
|
buf[offset + 1] = (value >> 32) as u32;
|
|
|
|
#[allow(unused_assignments)]
|
|
|
|
{
|
|
|
|
offset += 2;
|
|
|
|
}
|
|
|
|
)*
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
#[derive(Serialize)]
|
|
|
|
#[serde(rename_all = "camelCase")]
|
|
|
|
struct $name {
|
|
|
|
$($field: $type),*
|
|
|
|
}
|
|
|
|
};
|
|
|
|
}
|
|
|
|
|
|
|
|
create_struct_writer! {
|
|
|
|
pub struct FsStat {
|
|
|
|
is_file: bool,
|
|
|
|
is_directory: bool,
|
|
|
|
is_symlink: bool,
|
|
|
|
size: u64,
|
|
|
|
// In milliseconds, like JavaScript. Available on both Unix or Windows.
|
|
|
|
mtime_set: bool,
|
|
|
|
mtime: u64,
|
|
|
|
atime_set: bool,
|
|
|
|
atime: u64,
|
|
|
|
birthtime_set: bool,
|
|
|
|
birthtime: u64,
|
|
|
|
// Following are only valid under Unix.
|
|
|
|
dev: u64,
|
|
|
|
ino: u64,
|
|
|
|
mode: u32,
|
|
|
|
nlink: u64,
|
|
|
|
uid: u32,
|
|
|
|
gid: u32,
|
|
|
|
rdev: u64,
|
|
|
|
blksize: u64,
|
|
|
|
blocks: u64,
|
|
|
|
}
|
2021-04-05 16:40:24 +00:00
|
|
|
}
|
|
|
|
|
2020-01-16 14:46:32 +00:00
|
|
|
#[inline(always)]
|
2021-04-05 16:40:24 +00:00
|
|
|
fn get_stat(metadata: std::fs::Metadata) -> FsStat {
|
2020-01-16 14:46:32 +00:00
|
|
|
// Unix stat member (number types only). 0 if not on unix.
|
|
|
|
macro_rules! usm {
|
2020-09-06 00:34:02 +00:00
|
|
|
($member:ident) => {{
|
2020-01-16 14:46:32 +00:00
|
|
|
#[cfg(unix)]
|
|
|
|
{
|
|
|
|
metadata.$member()
|
|
|
|
}
|
|
|
|
#[cfg(not(unix))]
|
|
|
|
{
|
|
|
|
0
|
|
|
|
}
|
|
|
|
}};
|
|
|
|
}
|
|
|
|
|
2020-03-20 13:46:26 +00:00
|
|
|
#[cfg(unix)]
|
|
|
|
use std::os::unix::fs::MetadataExt;
|
2022-09-22 09:09:25 +00:00
|
|
|
let (mtime, mtime_set) = to_msec(metadata.modified());
|
|
|
|
let (atime, atime_set) = to_msec(metadata.accessed());
|
|
|
|
let (birthtime, birthtime_set) = to_msec(metadata.created());
|
|
|
|
|
2021-04-05 16:40:24 +00:00
|
|
|
FsStat {
|
|
|
|
is_file: metadata.is_file(),
|
|
|
|
is_directory: metadata.is_dir(),
|
|
|
|
is_symlink: metadata.file_type().is_symlink(),
|
|
|
|
size: metadata.len(),
|
2020-04-27 18:09:56 +00:00
|
|
|
// In milliseconds, like JavaScript. Available on both Unix or Windows.
|
2022-09-22 09:09:25 +00:00
|
|
|
mtime_set,
|
|
|
|
mtime,
|
|
|
|
atime_set,
|
|
|
|
atime,
|
|
|
|
birthtime_set,
|
|
|
|
birthtime,
|
2020-01-16 14:46:32 +00:00
|
|
|
// Following are only valid under Unix.
|
2021-04-05 16:40:24 +00:00
|
|
|
dev: usm!(dev),
|
|
|
|
ino: usm!(ino),
|
|
|
|
mode: usm!(mode),
|
|
|
|
nlink: usm!(nlink),
|
|
|
|
uid: usm!(uid),
|
|
|
|
gid: usm!(gid),
|
|
|
|
rdev: usm!(rdev),
|
|
|
|
blksize: usm!(blksize),
|
|
|
|
blocks: usm!(blocks),
|
|
|
|
}
|
2019-08-14 15:03:02 +00:00
|
|
|
}
|
|
|
|
|
2023-03-16 01:35:13 +00:00
|
|
|
#[cfg(windows)]
|
|
|
|
#[inline(always)]
|
|
|
|
fn get_stat2(metadata: std::fs::Metadata, dev: u64) -> FsStat {
|
|
|
|
let (mtime, mtime_set) = to_msec(metadata.modified());
|
|
|
|
let (atime, atime_set) = to_msec(metadata.accessed());
|
|
|
|
let (birthtime, birthtime_set) = to_msec(metadata.created());
|
|
|
|
|
|
|
|
FsStat {
|
|
|
|
is_file: metadata.is_file(),
|
|
|
|
is_directory: metadata.is_dir(),
|
|
|
|
is_symlink: metadata.file_type().is_symlink(),
|
|
|
|
size: metadata.len(),
|
|
|
|
mtime_set,
|
|
|
|
mtime,
|
|
|
|
atime_set,
|
|
|
|
atime,
|
|
|
|
birthtime_set,
|
|
|
|
birthtime,
|
|
|
|
dev,
|
|
|
|
ino: 0,
|
|
|
|
mode: 0,
|
|
|
|
nlink: 0,
|
|
|
|
uid: 0,
|
|
|
|
gid: 0,
|
|
|
|
rdev: 0,
|
|
|
|
blksize: 0,
|
|
|
|
blocks: 0,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
#[cfg(not(windows))]
|
|
|
|
#[inline(always)]
|
|
|
|
fn get_stat2(metadata: std::fs::Metadata) -> FsStat {
|
|
|
|
#[cfg(unix)]
|
|
|
|
use std::os::unix::fs::MetadataExt;
|
|
|
|
let (mtime, mtime_set) = to_msec(metadata.modified());
|
|
|
|
let (atime, atime_set) = to_msec(metadata.accessed());
|
|
|
|
let (birthtime, birthtime_set) = to_msec(metadata.created());
|
|
|
|
|
|
|
|
FsStat {
|
|
|
|
is_file: metadata.is_file(),
|
|
|
|
is_directory: metadata.is_dir(),
|
|
|
|
is_symlink: metadata.file_type().is_symlink(),
|
|
|
|
size: metadata.len(),
|
|
|
|
mtime_set,
|
|
|
|
mtime,
|
|
|
|
atime_set,
|
|
|
|
atime,
|
|
|
|
birthtime_set,
|
|
|
|
birthtime,
|
|
|
|
dev: metadata.dev(),
|
|
|
|
ino: metadata.ino(),
|
|
|
|
mode: metadata.mode(),
|
|
|
|
nlink: metadata.nlink(),
|
|
|
|
uid: metadata.uid(),
|
|
|
|
gid: metadata.gid(),
|
|
|
|
rdev: metadata.rdev(),
|
|
|
|
blksize: metadata.blksize(),
|
|
|
|
blocks: metadata.blocks(),
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-08-26 14:18:42 +00:00
|
|
|
#[derive(Deserialize)]
|
|
|
|
#[serde(rename_all = "camelCase")]
|
2021-03-18 18:42:01 +00:00
|
|
|
pub struct StatArgs {
|
2020-03-06 16:29:23 +00:00
|
|
|
path: String,
|
2019-08-26 14:18:42 +00:00
|
|
|
lstat: bool,
|
|
|
|
}
|
|
|
|
|
2023-03-16 01:35:13 +00:00
|
|
|
#[cfg(not(windows))]
|
|
|
|
fn do_stat(path: PathBuf, lstat: bool) -> Result<FsStat, AnyError> {
|
|
|
|
let err_mapper =
|
|
|
|
|err| default_err_mapper(err, format!("stat '{}'", path.display()));
|
|
|
|
let metadata = if lstat {
|
|
|
|
std::fs::symlink_metadata(&path).map_err(err_mapper)?
|
|
|
|
} else {
|
|
|
|
std::fs::metadata(&path).map_err(err_mapper)?
|
|
|
|
};
|
|
|
|
|
|
|
|
Ok(get_stat2(metadata))
|
|
|
|
}
|
|
|
|
|
|
|
|
#[cfg(windows)]
|
|
|
|
fn do_stat(path: PathBuf, lstat: bool) -> Result<FsStat, AnyError> {
|
|
|
|
use std::os::windows::prelude::OsStrExt;
|
|
|
|
|
|
|
|
use winapi::um::fileapi::CreateFileW;
|
|
|
|
use winapi::um::fileapi::OPEN_EXISTING;
|
|
|
|
use winapi::um::handleapi::CloseHandle;
|
|
|
|
use winapi::um::handleapi::INVALID_HANDLE_VALUE;
|
|
|
|
use winapi::um::winbase::FILE_FLAG_BACKUP_SEMANTICS;
|
|
|
|
use winapi::um::winbase::FILE_FLAG_OPEN_REPARSE_POINT;
|
|
|
|
use winapi::um::winnt::FILE_SHARE_DELETE;
|
|
|
|
use winapi::um::winnt::FILE_SHARE_READ;
|
|
|
|
use winapi::um::winnt::FILE_SHARE_WRITE;
|
|
|
|
|
|
|
|
let err_mapper =
|
|
|
|
|err| default_err_mapper(err, format!("stat '{}'", path.display()));
|
|
|
|
let metadata = if lstat {
|
|
|
|
std::fs::symlink_metadata(&path).map_err(err_mapper)?
|
|
|
|
} else {
|
|
|
|
std::fs::metadata(&path).map_err(err_mapper)?
|
|
|
|
};
|
|
|
|
|
|
|
|
let (p, file_flags) = if lstat {
|
|
|
|
(
|
|
|
|
path,
|
|
|
|
FILE_FLAG_BACKUP_SEMANTICS | FILE_FLAG_OPEN_REPARSE_POINT,
|
|
|
|
)
|
|
|
|
} else {
|
|
|
|
(path.canonicalize()?, FILE_FLAG_BACKUP_SEMANTICS)
|
|
|
|
};
|
|
|
|
unsafe {
|
|
|
|
let mut path: Vec<_> = p.as_os_str().encode_wide().collect();
|
|
|
|
path.push(0);
|
|
|
|
let file_handle = CreateFileW(
|
|
|
|
path.as_ptr(),
|
|
|
|
0,
|
|
|
|
FILE_SHARE_READ | FILE_SHARE_DELETE | FILE_SHARE_WRITE,
|
|
|
|
std::ptr::null_mut(),
|
|
|
|
OPEN_EXISTING,
|
|
|
|
file_flags,
|
|
|
|
std::ptr::null_mut(),
|
|
|
|
);
|
|
|
|
if file_handle == INVALID_HANDLE_VALUE {
|
|
|
|
return Err(std::io::Error::last_os_error().into());
|
|
|
|
}
|
|
|
|
|
|
|
|
let result = get_dev(file_handle);
|
|
|
|
CloseHandle(file_handle);
|
|
|
|
let dev = result?;
|
|
|
|
|
|
|
|
Ok(get_stat2(metadata, dev))
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
#[cfg(windows)]
|
|
|
|
use winapi::um::fileapi::GetFileInformationByHandle;
|
|
|
|
#[cfg(windows)]
|
|
|
|
use winapi::um::fileapi::BY_HANDLE_FILE_INFORMATION;
|
|
|
|
|
|
|
|
#[cfg(windows)]
|
|
|
|
unsafe fn get_dev(
|
|
|
|
handle: winapi::shared::ntdef::HANDLE,
|
|
|
|
) -> std::io::Result<u64> {
|
|
|
|
use winapi::shared::minwindef::FALSE;
|
|
|
|
|
|
|
|
let info = {
|
|
|
|
let mut info =
|
|
|
|
std::mem::MaybeUninit::<BY_HANDLE_FILE_INFORMATION>::zeroed();
|
|
|
|
if GetFileInformationByHandle(handle, info.as_mut_ptr()) == FALSE {
|
|
|
|
return Err(std::io::Error::last_os_error());
|
|
|
|
}
|
|
|
|
|
|
|
|
info.assume_init()
|
|
|
|
};
|
|
|
|
|
|
|
|
Ok(info.dwVolumeSerialNumber as u64)
|
|
|
|
}
|
|
|
|
|
2022-03-14 17:44:15 +00:00
|
|
|
#[op]
|
2023-03-04 23:44:59 +00:00
|
|
|
fn op_stat_sync<P>(
|
2020-09-10 13:57:45 +00:00
|
|
|
state: &mut OpState,
|
2023-03-03 13:34:10 +00:00
|
|
|
path: &str,
|
2022-09-22 09:09:25 +00:00
|
|
|
lstat: bool,
|
|
|
|
out_buf: &mut [u32],
|
2023-03-04 23:44:59 +00:00
|
|
|
) -> Result<(), AnyError>
|
|
|
|
where
|
|
|
|
P: FsPermissions + 'static,
|
|
|
|
{
|
2023-03-03 13:34:10 +00:00
|
|
|
let path = PathBuf::from(path);
|
2022-09-27 20:36:33 +00:00
|
|
|
state
|
2023-03-04 23:44:59 +00:00
|
|
|
.borrow_mut::<P>()
|
2023-01-07 16:25:34 +00:00
|
|
|
.check_read(&path, "Deno.statSync()")?;
|
2022-09-22 09:09:25 +00:00
|
|
|
|
2023-03-16 01:35:13 +00:00
|
|
|
let stat = do_stat(path, lstat)?;
|
2022-09-22 09:09:25 +00:00
|
|
|
stat.write(out_buf);
|
|
|
|
|
|
|
|
Ok(())
|
2020-08-28 15:08:24 +00:00
|
|
|
}
|
|
|
|
|
2022-03-14 17:44:15 +00:00
|
|
|
#[op]
|
2023-03-04 23:44:59 +00:00
|
|
|
async fn op_stat_async<P>(
|
2020-09-10 13:57:45 +00:00
|
|
|
state: Rc<RefCell<OpState>>,
|
2021-03-18 18:42:01 +00:00
|
|
|
args: StatArgs,
|
2023-03-04 23:44:59 +00:00
|
|
|
) -> Result<FsStat, AnyError>
|
|
|
|
where
|
|
|
|
P: FsPermissions + 'static,
|
|
|
|
{
|
2020-05-29 15:27:43 +00:00
|
|
|
let path = PathBuf::from(&args.path);
|
2019-08-26 14:18:42 +00:00
|
|
|
let lstat = args.lstat;
|
2019-08-14 15:03:02 +00:00
|
|
|
|
2020-09-19 23:17:35 +00:00
|
|
|
{
|
2021-04-12 02:15:43 +00:00
|
|
|
let mut state = state.borrow_mut();
|
2023-03-04 23:44:59 +00:00
|
|
|
state.borrow_mut::<P>().check_read(&path, "Deno.stat()")?;
|
2020-09-19 23:17:35 +00:00
|
|
|
}
|
2019-08-14 15:03:02 +00:00
|
|
|
|
2020-08-28 15:08:24 +00:00
|
|
|
tokio::task::spawn_blocking(move || {
|
|
|
|
debug!("op_stat_async {} {}", path.display(), lstat);
|
2023-03-16 01:35:13 +00:00
|
|
|
do_stat(path, lstat)
|
2019-08-14 15:03:02 +00:00
|
|
|
})
|
2020-08-28 15:08:24 +00:00
|
|
|
.await
|
|
|
|
.unwrap()
|
2019-08-14 15:03:02 +00:00
|
|
|
}
|
|
|
|
|
2022-03-14 17:44:15 +00:00
|
|
|
#[op]
|
2023-03-04 23:44:59 +00:00
|
|
|
fn op_realpath_sync<P>(
|
2020-09-10 13:57:45 +00:00
|
|
|
state: &mut OpState,
|
2021-04-05 16:40:24 +00:00
|
|
|
path: String,
|
2023-03-04 23:44:59 +00:00
|
|
|
) -> Result<String, AnyError>
|
|
|
|
where
|
|
|
|
P: FsPermissions + 'static,
|
|
|
|
{
|
2021-04-05 16:40:24 +00:00
|
|
|
let path = PathBuf::from(&path);
|
2020-01-20 14:45:44 +00:00
|
|
|
|
2023-03-04 23:44:59 +00:00
|
|
|
let permissions = state.borrow_mut::<P>();
|
2023-01-07 16:25:34 +00:00
|
|
|
permissions.check_read(&path, "Deno.realPathSync()")?;
|
2020-05-29 15:27:43 +00:00
|
|
|
if path.is_relative() {
|
2023-01-07 16:25:34 +00:00
|
|
|
permissions.check_read_blind(
|
2022-09-27 20:36:33 +00:00
|
|
|
¤t_dir()?,
|
|
|
|
"CWD",
|
|
|
|
"Deno.realPathSync()",
|
|
|
|
)?;
|
2020-05-29 15:27:43 +00:00
|
|
|
}
|
2020-01-20 14:45:44 +00:00
|
|
|
|
2020-08-28 15:08:24 +00:00
|
|
|
debug!("op_realpath_sync {}", path.display());
|
|
|
|
// corresponds to the realpath on Unix and
|
|
|
|
// CreateFile and GetFinalPathNameByHandle on Windows
|
2020-10-30 02:19:03 +00:00
|
|
|
let realpath = canonicalize_path(&path)?;
|
|
|
|
let realpath_str = into_string(realpath.into_os_string())?;
|
2021-04-05 16:40:24 +00:00
|
|
|
Ok(realpath_str)
|
2020-08-28 15:08:24 +00:00
|
|
|
}
|
|
|
|
|
2022-03-14 17:44:15 +00:00
|
|
|
#[op]
|
2023-03-04 23:44:59 +00:00
|
|
|
async fn op_realpath_async<P>(
|
2020-09-10 13:57:45 +00:00
|
|
|
state: Rc<RefCell<OpState>>,
|
2021-04-05 16:40:24 +00:00
|
|
|
path: String,
|
2023-03-04 23:44:59 +00:00
|
|
|
) -> Result<String, AnyError>
|
|
|
|
where
|
|
|
|
P: FsPermissions + 'static,
|
|
|
|
{
|
2021-04-05 16:40:24 +00:00
|
|
|
let path = PathBuf::from(&path);
|
2020-08-28 15:08:24 +00:00
|
|
|
|
2020-09-19 23:17:35 +00:00
|
|
|
{
|
2021-04-12 02:15:43 +00:00
|
|
|
let mut state = state.borrow_mut();
|
2023-03-04 23:44:59 +00:00
|
|
|
let permissions = state.borrow_mut::<P>();
|
2023-01-07 16:25:34 +00:00
|
|
|
permissions.check_read(&path, "Deno.realPath()")?;
|
2020-09-19 23:17:35 +00:00
|
|
|
if path.is_relative() {
|
2023-01-07 16:25:34 +00:00
|
|
|
permissions.check_read_blind(
|
2022-09-27 20:36:33 +00:00
|
|
|
¤t_dir()?,
|
|
|
|
"CWD",
|
|
|
|
"Deno.realPath()",
|
|
|
|
)?;
|
2020-09-19 23:17:35 +00:00
|
|
|
}
|
2020-08-28 15:08:24 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
tokio::task::spawn_blocking(move || {
|
|
|
|
debug!("op_realpath_async {}", path.display());
|
2019-11-26 08:40:57 +00:00
|
|
|
// corresponds to the realpath on Unix and
|
|
|
|
// CreateFile and GetFinalPathNameByHandle on Windows
|
2020-10-30 02:19:03 +00:00
|
|
|
let realpath = canonicalize_path(&path)?;
|
|
|
|
let realpath_str = into_string(realpath.into_os_string())?;
|
2021-04-05 16:40:24 +00:00
|
|
|
Ok(realpath_str)
|
2019-11-26 08:40:57 +00:00
|
|
|
})
|
2020-08-28 15:08:24 +00:00
|
|
|
.await
|
|
|
|
.unwrap()
|
2019-11-26 08:40:57 +00:00
|
|
|
}
|
|
|
|
|
2021-04-05 16:40:24 +00:00
|
|
|
#[derive(Serialize)]
|
2019-08-26 14:18:42 +00:00
|
|
|
#[serde(rename_all = "camelCase")]
|
2021-04-05 16:40:24 +00:00
|
|
|
pub struct DirEntry {
|
|
|
|
name: String,
|
|
|
|
is_file: bool,
|
|
|
|
is_directory: bool,
|
|
|
|
is_symlink: bool,
|
2019-08-26 14:18:42 +00:00
|
|
|
}
|
|
|
|
|
2022-03-14 17:44:15 +00:00
|
|
|
#[op]
|
2023-03-04 23:44:59 +00:00
|
|
|
fn op_read_dir_sync<P>(
|
2020-09-10 13:57:45 +00:00
|
|
|
state: &mut OpState,
|
2021-04-05 16:40:24 +00:00
|
|
|
path: String,
|
2023-03-04 23:44:59 +00:00
|
|
|
) -> Result<Vec<DirEntry>, AnyError>
|
|
|
|
where
|
|
|
|
P: FsPermissions + 'static,
|
|
|
|
{
|
2021-04-05 16:40:24 +00:00
|
|
|
let path = PathBuf::from(&path);
|
2020-08-28 15:08:24 +00:00
|
|
|
|
2022-09-27 20:36:33 +00:00
|
|
|
state
|
2023-03-04 23:44:59 +00:00
|
|
|
.borrow_mut::<P>()
|
2023-01-07 16:25:34 +00:00
|
|
|
.check_read(&path, "Deno.readDirSync()")?;
|
2020-08-28 15:08:24 +00:00
|
|
|
|
|
|
|
debug!("op_read_dir_sync {}", path.display());
|
2023-02-16 19:21:03 +00:00
|
|
|
|
2021-10-11 13:21:18 +00:00
|
|
|
let entries: Vec<_> = std::fs::read_dir(&path)
|
2023-02-16 19:21:03 +00:00
|
|
|
.map_err(|err| {
|
|
|
|
default_err_mapper(err, format!("readdir '{}'", path.display()))
|
|
|
|
})?
|
2020-08-28 15:08:24 +00:00
|
|
|
.filter_map(|entry| {
|
|
|
|
let entry = entry.unwrap();
|
|
|
|
// Not all filenames can be encoded as UTF-8. Skip those for now.
|
|
|
|
if let Ok(name) = into_string(entry.file_name()) {
|
2021-04-05 16:40:24 +00:00
|
|
|
Some(DirEntry {
|
|
|
|
name,
|
|
|
|
is_file: entry
|
|
|
|
.file_type()
|
2023-03-15 21:46:36 +00:00
|
|
|
.map(|file_type| file_type.is_file())
|
|
|
|
.unwrap_or(false),
|
2021-04-05 16:40:24 +00:00
|
|
|
is_directory: entry
|
|
|
|
.file_type()
|
2023-03-15 21:46:36 +00:00
|
|
|
.map(|file_type| file_type.is_dir())
|
|
|
|
.unwrap_or(false),
|
2021-04-05 16:40:24 +00:00
|
|
|
is_symlink: entry
|
|
|
|
.file_type()
|
2023-03-15 21:46:36 +00:00
|
|
|
.map(|file_type| file_type.is_symlink())
|
|
|
|
.unwrap_or(false),
|
2021-04-05 16:40:24 +00:00
|
|
|
})
|
2020-08-28 15:08:24 +00:00
|
|
|
} else {
|
|
|
|
None
|
|
|
|
}
|
|
|
|
})
|
2021-04-05 16:40:24 +00:00
|
|
|
.collect();
|
2020-08-28 15:08:24 +00:00
|
|
|
|
2021-04-05 16:40:24 +00:00
|
|
|
Ok(entries)
|
2020-08-28 15:08:24 +00:00
|
|
|
}
|
|
|
|
|
2022-03-14 17:44:15 +00:00
|
|
|
#[op]
|
2023-03-04 23:44:59 +00:00
|
|
|
async fn op_read_dir_async<P>(
|
2020-09-10 13:57:45 +00:00
|
|
|
state: Rc<RefCell<OpState>>,
|
2021-04-05 16:40:24 +00:00
|
|
|
path: String,
|
2023-03-04 23:44:59 +00:00
|
|
|
) -> Result<Vec<DirEntry>, AnyError>
|
|
|
|
where
|
|
|
|
P: FsPermissions + 'static,
|
|
|
|
{
|
2021-04-05 16:40:24 +00:00
|
|
|
let path = PathBuf::from(&path);
|
2020-09-19 23:17:35 +00:00
|
|
|
{
|
2021-04-12 02:15:43 +00:00
|
|
|
let mut state = state.borrow_mut();
|
2022-09-27 20:36:33 +00:00
|
|
|
state
|
2023-03-04 23:44:59 +00:00
|
|
|
.borrow_mut::<P>()
|
2023-01-07 16:25:34 +00:00
|
|
|
.check_read(&path, "Deno.readDir()")?;
|
2020-09-19 23:17:35 +00:00
|
|
|
}
|
2020-08-28 15:08:24 +00:00
|
|
|
tokio::task::spawn_blocking(move || {
|
|
|
|
debug!("op_read_dir_async {}", path.display());
|
2023-02-16 19:21:03 +00:00
|
|
|
|
2021-10-11 13:21:18 +00:00
|
|
|
let entries: Vec<_> = std::fs::read_dir(&path)
|
2023-02-16 19:21:03 +00:00
|
|
|
.map_err(|err| {
|
|
|
|
default_err_mapper(err, format!("readdir '{}'", path.display()))
|
|
|
|
})?
|
2020-02-15 15:37:05 +00:00
|
|
|
.filter_map(|entry| {
|
2019-08-14 15:03:02 +00:00
|
|
|
let entry = entry.unwrap();
|
2020-02-15 15:37:05 +00:00
|
|
|
// Not all filenames can be encoded as UTF-8. Skip those for now.
|
2020-04-29 20:00:31 +00:00
|
|
|
if let Ok(name) = into_string(entry.file_name()) {
|
2021-04-05 16:40:24 +00:00
|
|
|
Some(DirEntry {
|
|
|
|
name,
|
|
|
|
is_file: entry
|
|
|
|
.file_type()
|
2023-03-15 21:46:36 +00:00
|
|
|
.map(|file_type| file_type.is_file())
|
|
|
|
.unwrap_or(false),
|
2021-04-05 16:40:24 +00:00
|
|
|
is_directory: entry
|
|
|
|
.file_type()
|
2023-03-15 21:46:36 +00:00
|
|
|
.map(|file_type| file_type.is_dir())
|
|
|
|
.unwrap_or(false),
|
2021-04-05 16:40:24 +00:00
|
|
|
is_symlink: entry
|
|
|
|
.file_type()
|
2023-03-15 21:46:36 +00:00
|
|
|
.map(|file_type| file_type.is_symlink())
|
|
|
|
.unwrap_or(false),
|
2021-04-05 16:40:24 +00:00
|
|
|
})
|
2020-02-15 15:37:05 +00:00
|
|
|
} else {
|
|
|
|
None
|
|
|
|
}
|
2019-08-14 15:03:02 +00:00
|
|
|
})
|
2021-04-05 16:40:24 +00:00
|
|
|
.collect();
|
2019-08-14 15:03:02 +00:00
|
|
|
|
2021-04-05 16:40:24 +00:00
|
|
|
Ok(entries)
|
2019-08-14 15:03:02 +00:00
|
|
|
})
|
2020-08-28 15:08:24 +00:00
|
|
|
.await
|
2021-04-05 16:40:24 +00:00
|
|
|
.unwrap()
|
2019-08-14 15:03:02 +00:00
|
|
|
}
|
|
|
|
|
2022-03-14 17:44:15 +00:00
|
|
|
#[op]
|
2023-03-04 23:44:59 +00:00
|
|
|
fn op_rename_sync<P>(
|
2020-09-10 13:57:45 +00:00
|
|
|
state: &mut OpState,
|
2023-03-03 13:34:10 +00:00
|
|
|
oldpath: &str,
|
|
|
|
newpath: &str,
|
2023-03-04 23:44:59 +00:00
|
|
|
) -> Result<(), AnyError>
|
|
|
|
where
|
|
|
|
P: FsPermissions + 'static,
|
|
|
|
{
|
2023-03-03 13:34:10 +00:00
|
|
|
let oldpath = PathBuf::from(oldpath);
|
|
|
|
let newpath = PathBuf::from(newpath);
|
2020-08-28 15:08:24 +00:00
|
|
|
|
2023-03-04 23:44:59 +00:00
|
|
|
let permissions = state.borrow_mut::<P>();
|
2023-01-07 16:25:34 +00:00
|
|
|
permissions.check_read(&oldpath, "Deno.renameSync()")?;
|
|
|
|
permissions.check_write(&oldpath, "Deno.renameSync()")?;
|
|
|
|
permissions.check_write(&newpath, "Deno.renameSync()")?;
|
2022-09-22 09:09:25 +00:00
|
|
|
|
2023-02-16 19:21:03 +00:00
|
|
|
std::fs::rename(&oldpath, &newpath).map_err(|err| {
|
|
|
|
default_err_mapper(
|
|
|
|
err,
|
|
|
|
format!("rename '{}' -> '{}'", oldpath.display(), newpath.display()),
|
2021-10-11 13:21:18 +00:00
|
|
|
)
|
2023-02-16 19:21:03 +00:00
|
|
|
})?;
|
2021-04-05 16:40:24 +00:00
|
|
|
Ok(())
|
2020-08-28 15:08:24 +00:00
|
|
|
}
|
|
|
|
|
2022-03-14 17:44:15 +00:00
|
|
|
#[op]
|
2023-03-04 23:44:59 +00:00
|
|
|
async fn op_rename_async<P>(
|
2020-09-10 13:57:45 +00:00
|
|
|
state: Rc<RefCell<OpState>>,
|
2022-09-22 09:09:25 +00:00
|
|
|
oldpath: String,
|
|
|
|
newpath: String,
|
2023-03-04 23:44:59 +00:00
|
|
|
) -> Result<(), AnyError>
|
|
|
|
where
|
|
|
|
P: FsPermissions + 'static,
|
|
|
|
{
|
2022-09-22 09:09:25 +00:00
|
|
|
let oldpath = PathBuf::from(&oldpath);
|
|
|
|
let newpath = PathBuf::from(&newpath);
|
2020-09-10 13:57:45 +00:00
|
|
|
{
|
2021-04-12 02:15:43 +00:00
|
|
|
let mut state = state.borrow_mut();
|
2023-03-04 23:44:59 +00:00
|
|
|
let permissions = state.borrow_mut::<P>();
|
2023-01-07 16:25:34 +00:00
|
|
|
permissions.check_read(&oldpath, "Deno.rename()")?;
|
|
|
|
permissions.check_write(&oldpath, "Deno.rename()")?;
|
|
|
|
permissions.check_write(&newpath, "Deno.rename()")?;
|
2020-09-10 13:57:45 +00:00
|
|
|
}
|
2023-02-16 19:21:03 +00:00
|
|
|
|
2020-08-28 15:08:24 +00:00
|
|
|
tokio::task::spawn_blocking(move || {
|
2023-02-16 19:21:03 +00:00
|
|
|
std::fs::rename(&oldpath, &newpath).map_err(|err| {
|
|
|
|
default_err_mapper(
|
|
|
|
err,
|
|
|
|
format!("rename '{}' -> '{}'", oldpath.display(), newpath.display()),
|
2021-10-11 13:21:18 +00:00
|
|
|
)
|
2023-02-16 19:21:03 +00:00
|
|
|
})?;
|
2021-04-05 16:40:24 +00:00
|
|
|
Ok(())
|
2019-08-14 15:03:02 +00:00
|
|
|
})
|
2020-08-28 15:08:24 +00:00
|
|
|
.await
|
|
|
|
.unwrap()
|
2019-08-14 15:03:02 +00:00
|
|
|
}
|
|
|
|
|
2022-09-22 09:09:25 +00:00
|
|
|
#[op]
|
2023-03-04 23:44:59 +00:00
|
|
|
fn op_link_sync<P>(
|
2022-09-22 09:09:25 +00:00
|
|
|
state: &mut OpState,
|
2023-03-03 13:34:10 +00:00
|
|
|
oldpath: &str,
|
|
|
|
newpath: &str,
|
2023-03-04 23:44:59 +00:00
|
|
|
) -> Result<(), AnyError>
|
|
|
|
where
|
|
|
|
P: FsPermissions + 'static,
|
|
|
|
{
|
2023-03-03 13:34:10 +00:00
|
|
|
let oldpath = PathBuf::from(oldpath);
|
|
|
|
let newpath = PathBuf::from(newpath);
|
2020-08-28 15:08:24 +00:00
|
|
|
|
2023-03-04 23:44:59 +00:00
|
|
|
let permissions = state.borrow_mut::<P>();
|
2023-01-07 16:25:34 +00:00
|
|
|
permissions.check_read(&oldpath, "Deno.linkSync()")?;
|
|
|
|
permissions.check_write(&oldpath, "Deno.linkSync()")?;
|
|
|
|
permissions.check_read(&newpath, "Deno.linkSync()")?;
|
|
|
|
permissions.check_write(&newpath, "Deno.linkSync()")?;
|
2020-08-28 15:08:24 +00:00
|
|
|
|
2023-02-16 19:21:03 +00:00
|
|
|
std::fs::hard_link(&oldpath, &newpath).map_err(|err| {
|
|
|
|
default_err_mapper(
|
|
|
|
err,
|
|
|
|
format!("link '{}' -> '{}'", oldpath.display(), newpath.display()),
|
2021-10-11 13:21:18 +00:00
|
|
|
)
|
2023-02-16 19:21:03 +00:00
|
|
|
})?;
|
2021-04-05 16:40:24 +00:00
|
|
|
Ok(())
|
2020-08-28 15:08:24 +00:00
|
|
|
}
|
|
|
|
|
2022-03-14 17:44:15 +00:00
|
|
|
#[op]
|
2023-03-04 23:44:59 +00:00
|
|
|
async fn op_link_async<P>(
|
2020-09-10 13:57:45 +00:00
|
|
|
state: Rc<RefCell<OpState>>,
|
2022-09-22 09:09:25 +00:00
|
|
|
oldpath: String,
|
|
|
|
newpath: String,
|
2023-03-04 23:44:59 +00:00
|
|
|
) -> Result<(), AnyError>
|
|
|
|
where
|
|
|
|
P: FsPermissions + 'static,
|
|
|
|
{
|
2022-09-22 09:09:25 +00:00
|
|
|
let oldpath = PathBuf::from(&oldpath);
|
|
|
|
let newpath = PathBuf::from(&newpath);
|
2019-08-14 15:03:02 +00:00
|
|
|
|
2020-09-19 23:17:35 +00:00
|
|
|
{
|
2021-04-12 02:15:43 +00:00
|
|
|
let mut state = state.borrow_mut();
|
2023-03-04 23:44:59 +00:00
|
|
|
let permissions = state.borrow_mut::<P>();
|
2023-01-07 16:25:34 +00:00
|
|
|
permissions.check_read(&oldpath, "Deno.link()")?;
|
|
|
|
permissions.check_write(&oldpath, "Deno.link()")?;
|
|
|
|
permissions.check_read(&newpath, "Deno.link()")?;
|
|
|
|
permissions.check_write(&newpath, "Deno.link()")?;
|
2020-09-19 23:17:35 +00:00
|
|
|
}
|
2019-08-14 15:03:02 +00:00
|
|
|
|
2020-08-28 15:08:24 +00:00
|
|
|
tokio::task::spawn_blocking(move || {
|
2023-02-16 19:21:03 +00:00
|
|
|
let err_mapper = |err| {
|
|
|
|
default_err_mapper(
|
|
|
|
err,
|
|
|
|
format!("link '{}' -> '{}'", oldpath.display(), newpath.display()),
|
2021-10-11 13:21:18 +00:00
|
|
|
)
|
|
|
|
};
|
|
|
|
std::fs::hard_link(&oldpath, &newpath).map_err(err_mapper)?;
|
2021-04-05 16:40:24 +00:00
|
|
|
Ok(())
|
2019-08-14 15:03:02 +00:00
|
|
|
})
|
2020-08-28 15:08:24 +00:00
|
|
|
.await
|
|
|
|
.unwrap()
|
2019-08-14 15:03:02 +00:00
|
|
|
}
|
|
|
|
|
2022-03-14 17:44:15 +00:00
|
|
|
#[op]
|
2023-03-04 23:44:59 +00:00
|
|
|
fn op_symlink_sync<P>(
|
2020-09-10 13:57:45 +00:00
|
|
|
state: &mut OpState,
|
2023-03-03 13:34:10 +00:00
|
|
|
oldpath: &str,
|
|
|
|
newpath: &str,
|
2022-09-22 09:09:25 +00:00
|
|
|
_type: Option<String>,
|
2023-03-04 23:44:59 +00:00
|
|
|
) -> Result<(), AnyError>
|
|
|
|
where
|
|
|
|
P: FsPermissions + 'static,
|
|
|
|
{
|
2023-03-03 13:34:10 +00:00
|
|
|
let oldpath = PathBuf::from(oldpath);
|
|
|
|
let newpath = PathBuf::from(newpath);
|
2020-08-28 15:08:24 +00:00
|
|
|
|
2022-09-27 20:36:33 +00:00
|
|
|
state
|
2023-03-04 23:44:59 +00:00
|
|
|
.borrow_mut::<P>()
|
2023-01-07 16:25:34 +00:00
|
|
|
.check_write_all("Deno.symlinkSync()")?;
|
2022-09-27 20:36:33 +00:00
|
|
|
state
|
2023-03-04 23:44:59 +00:00
|
|
|
.borrow_mut::<P>()
|
2023-01-07 16:25:34 +00:00
|
|
|
.check_read_all("Deno.symlinkSync()")?;
|
2020-08-28 15:08:24 +00:00
|
|
|
|
2023-02-16 19:21:03 +00:00
|
|
|
let err_mapper = |err| {
|
|
|
|
default_err_mapper(
|
|
|
|
err,
|
|
|
|
format!("symlink '{}' -> '{}'", oldpath.display(), newpath.display()),
|
2021-10-11 13:21:18 +00:00
|
|
|
)
|
|
|
|
};
|
2023-02-16 19:21:03 +00:00
|
|
|
|
2020-08-28 15:08:24 +00:00
|
|
|
#[cfg(unix)]
|
|
|
|
{
|
|
|
|
use std::os::unix::fs::symlink;
|
2021-10-11 13:21:18 +00:00
|
|
|
symlink(&oldpath, &newpath).map_err(err_mapper)?;
|
2021-04-05 16:40:24 +00:00
|
|
|
Ok(())
|
2020-08-28 15:08:24 +00:00
|
|
|
}
|
|
|
|
#[cfg(not(unix))]
|
|
|
|
{
|
2023-01-15 04:18:58 +00:00
|
|
|
use std::os::windows::fs::symlink_dir;
|
|
|
|
use std::os::windows::fs::symlink_file;
|
2020-08-28 15:08:24 +00:00
|
|
|
|
2022-09-22 09:09:25 +00:00
|
|
|
match _type {
|
|
|
|
Some(ty) => match ty.as_ref() {
|
2021-10-11 13:21:18 +00:00
|
|
|
"file" => symlink_file(&oldpath, &newpath).map_err(err_mapper)?,
|
|
|
|
"dir" => symlink_dir(&oldpath, &newpath).map_err(err_mapper)?,
|
2020-09-14 16:48:57 +00:00
|
|
|
_ => return Err(type_error("unsupported type")),
|
2020-08-28 15:08:24 +00:00
|
|
|
},
|
|
|
|
None => {
|
|
|
|
let old_meta = std::fs::metadata(&oldpath);
|
|
|
|
match old_meta {
|
|
|
|
Ok(metadata) => {
|
|
|
|
if metadata.is_file() {
|
2021-10-11 13:21:18 +00:00
|
|
|
symlink_file(&oldpath, &newpath).map_err(err_mapper)?
|
2020-08-28 15:08:24 +00:00
|
|
|
} else if metadata.is_dir() {
|
2021-10-11 13:21:18 +00:00
|
|
|
symlink_dir(&oldpath, &newpath).map_err(err_mapper)?
|
2020-08-28 15:08:24 +00:00
|
|
|
}
|
|
|
|
}
|
2020-09-14 16:48:57 +00:00
|
|
|
Err(_) => return Err(type_error("you must pass a `options` argument for non-existent target path in windows".to_string())),
|
2020-08-28 15:08:24 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
};
|
2021-04-05 16:40:24 +00:00
|
|
|
Ok(())
|
2020-08-28 15:08:24 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-03-14 17:44:15 +00:00
|
|
|
#[op]
|
2023-03-04 23:44:59 +00:00
|
|
|
async fn op_symlink_async<P>(
|
2020-09-10 13:57:45 +00:00
|
|
|
state: Rc<RefCell<OpState>>,
|
2022-09-22 09:09:25 +00:00
|
|
|
oldpath: String,
|
|
|
|
newpath: String,
|
|
|
|
_type: Option<String>,
|
2023-03-04 23:44:59 +00:00
|
|
|
) -> Result<(), AnyError>
|
|
|
|
where
|
|
|
|
P: FsPermissions + 'static,
|
|
|
|
{
|
2022-09-22 09:09:25 +00:00
|
|
|
let oldpath = PathBuf::from(&oldpath);
|
|
|
|
let newpath = PathBuf::from(&newpath);
|
2020-03-20 13:46:26 +00:00
|
|
|
|
2020-09-19 23:17:35 +00:00
|
|
|
{
|
2021-04-12 02:15:43 +00:00
|
|
|
let mut state = state.borrow_mut();
|
2023-03-04 23:44:59 +00:00
|
|
|
state.borrow_mut::<P>().check_write_all("Deno.symlink()")?;
|
|
|
|
state.borrow_mut::<P>().check_read_all("Deno.symlink()")?;
|
2020-09-19 23:17:35 +00:00
|
|
|
}
|
2019-08-26 14:18:42 +00:00
|
|
|
|
2020-08-28 15:08:24 +00:00
|
|
|
tokio::task::spawn_blocking(move || {
|
2023-02-16 19:21:03 +00:00
|
|
|
let err_mapper = |err| default_err_mapper(err, format!(
|
|
|
|
"symlink '{}' -> '{}'",
|
|
|
|
oldpath.display(),
|
|
|
|
newpath.display()
|
|
|
|
));
|
|
|
|
|
2020-03-02 15:19:42 +00:00
|
|
|
#[cfg(unix)]
|
2020-03-20 13:46:26 +00:00
|
|
|
{
|
|
|
|
use std::os::unix::fs::symlink;
|
2021-10-11 13:21:18 +00:00
|
|
|
symlink(&oldpath, &newpath).map_err(err_mapper)?;
|
2021-04-05 16:40:24 +00:00
|
|
|
Ok(())
|
2020-03-20 13:46:26 +00:00
|
|
|
}
|
|
|
|
#[cfg(not(unix))]
|
|
|
|
{
|
2020-05-18 22:46:02 +00:00
|
|
|
use std::os::windows::fs::{symlink_dir, symlink_file};
|
|
|
|
|
2022-09-22 09:09:25 +00:00
|
|
|
match _type {
|
|
|
|
Some(ty) => match ty.as_ref() {
|
2021-10-11 13:21:18 +00:00
|
|
|
"file" => symlink_file(&oldpath, &newpath).map_err(err_mapper)?,
|
|
|
|
"dir" => symlink_dir(&oldpath, &newpath).map_err(err_mapper)?,
|
2020-09-14 16:48:57 +00:00
|
|
|
_ => return Err(type_error("unsupported type")),
|
2020-05-18 22:46:02 +00:00
|
|
|
},
|
|
|
|
None => {
|
|
|
|
let old_meta = std::fs::metadata(&oldpath);
|
|
|
|
match old_meta {
|
|
|
|
Ok(metadata) => {
|
|
|
|
if metadata.is_file() {
|
2021-10-11 13:21:18 +00:00
|
|
|
symlink_file(&oldpath, &newpath).map_err(err_mapper)?
|
2020-05-18 22:46:02 +00:00
|
|
|
} else if metadata.is_dir() {
|
2021-10-11 13:21:18 +00:00
|
|
|
symlink_dir(&oldpath, &newpath).map_err(err_mapper)?
|
2020-05-18 22:46:02 +00:00
|
|
|
}
|
|
|
|
}
|
2020-09-14 16:48:57 +00:00
|
|
|
Err(_) => return Err(type_error("you must pass a `options` argument for non-existent target path in windows".to_string())),
|
2020-05-18 22:46:02 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
};
|
2021-04-05 16:40:24 +00:00
|
|
|
Ok(())
|
2020-03-20 13:46:26 +00:00
|
|
|
}
|
2019-08-14 15:03:02 +00:00
|
|
|
})
|
2020-08-28 15:08:24 +00:00
|
|
|
.await
|
|
|
|
.unwrap()
|
2019-08-14 15:03:02 +00:00
|
|
|
}
|
|
|
|
|
2022-03-14 17:44:15 +00:00
|
|
|
#[op]
|
2023-03-04 23:44:59 +00:00
|
|
|
fn op_read_link_sync<P>(
|
2020-09-10 13:57:45 +00:00
|
|
|
state: &mut OpState,
|
2021-04-05 16:40:24 +00:00
|
|
|
path: String,
|
2023-03-04 23:44:59 +00:00
|
|
|
) -> Result<String, AnyError>
|
|
|
|
where
|
|
|
|
P: FsPermissions + 'static,
|
|
|
|
{
|
2021-04-05 16:40:24 +00:00
|
|
|
let path = PathBuf::from(&path);
|
2019-08-26 14:18:42 +00:00
|
|
|
|
2022-09-27 20:36:33 +00:00
|
|
|
state
|
2023-03-04 23:44:59 +00:00
|
|
|
.borrow_mut::<P>()
|
2023-01-07 16:25:34 +00:00
|
|
|
.check_read(&path, "Deno.readLink()")?;
|
2019-08-14 15:03:02 +00:00
|
|
|
|
2020-08-28 15:08:24 +00:00
|
|
|
debug!("op_read_link_value {}", path.display());
|
2021-10-11 13:21:18 +00:00
|
|
|
let target = std::fs::read_link(&path)
|
2023-02-16 19:21:03 +00:00
|
|
|
.map_err(|err| {
|
|
|
|
default_err_mapper(err, format!("readlink '{}'", path.display()))
|
|
|
|
})?
|
2021-10-11 13:21:18 +00:00
|
|
|
.into_os_string();
|
2020-08-28 15:08:24 +00:00
|
|
|
let targetstr = into_string(target)?;
|
2021-04-05 16:40:24 +00:00
|
|
|
Ok(targetstr)
|
2020-08-28 15:08:24 +00:00
|
|
|
}
|
|
|
|
|
2022-03-14 17:44:15 +00:00
|
|
|
#[op]
|
2023-03-04 23:44:59 +00:00
|
|
|
async fn op_read_link_async<P>(
|
2020-09-10 13:57:45 +00:00
|
|
|
state: Rc<RefCell<OpState>>,
|
2021-04-05 16:40:24 +00:00
|
|
|
path: String,
|
2023-03-04 23:44:59 +00:00
|
|
|
) -> Result<String, AnyError>
|
|
|
|
where
|
|
|
|
P: FsPermissions + 'static,
|
|
|
|
{
|
2021-04-05 16:40:24 +00:00
|
|
|
let path = PathBuf::from(&path);
|
2020-09-19 23:17:35 +00:00
|
|
|
{
|
2021-04-12 02:15:43 +00:00
|
|
|
let mut state = state.borrow_mut();
|
2022-09-27 20:36:33 +00:00
|
|
|
state
|
2023-03-04 23:44:59 +00:00
|
|
|
.borrow_mut::<P>()
|
2023-01-07 16:25:34 +00:00
|
|
|
.check_read(&path, "Deno.readLink()")?;
|
2020-09-19 23:17:35 +00:00
|
|
|
}
|
2020-08-28 15:08:24 +00:00
|
|
|
tokio::task::spawn_blocking(move || {
|
|
|
|
debug!("op_read_link_async {}", path.display());
|
2021-10-11 13:21:18 +00:00
|
|
|
let target = std::fs::read_link(&path)
|
2023-02-16 19:21:03 +00:00
|
|
|
.map_err(|err| {
|
|
|
|
default_err_mapper(err, format!("readlink '{}'", path.display()))
|
|
|
|
})?
|
2021-10-11 13:21:18 +00:00
|
|
|
.into_os_string();
|
2020-04-03 17:47:57 +00:00
|
|
|
let targetstr = into_string(target)?;
|
2021-04-05 16:40:24 +00:00
|
|
|
Ok(targetstr)
|
2019-08-14 15:03:02 +00:00
|
|
|
})
|
2020-08-28 15:08:24 +00:00
|
|
|
.await
|
|
|
|
.unwrap()
|
2019-08-14 15:03:02 +00:00
|
|
|
}
|
|
|
|
|
2022-03-14 17:44:15 +00:00
|
|
|
#[op]
|
2020-08-28 15:08:24 +00:00
|
|
|
fn op_ftruncate_sync(
|
2020-09-10 13:57:45 +00:00
|
|
|
state: &mut OpState,
|
2022-09-22 09:09:25 +00:00
|
|
|
rid: u32,
|
|
|
|
len: i32,
|
2021-04-05 16:40:24 +00:00
|
|
|
) -> Result<(), AnyError> {
|
2022-09-22 09:09:25 +00:00
|
|
|
let len = len as u64;
|
2022-05-11 16:48:38 +00:00
|
|
|
StdFileResource::with_file(state, rid, |std_file| {
|
|
|
|
std_file.set_len(len).map_err(AnyError::from)
|
2020-08-28 15:08:24 +00:00
|
|
|
})?;
|
2021-04-05 16:40:24 +00:00
|
|
|
Ok(())
|
2020-08-28 15:08:24 +00:00
|
|
|
}
|
2020-06-20 13:46:10 +00:00
|
|
|
|
2022-03-14 17:44:15 +00:00
|
|
|
#[op]
|
2020-08-28 15:08:24 +00:00
|
|
|
async fn op_ftruncate_async(
|
2020-09-10 13:57:45 +00:00
|
|
|
state: Rc<RefCell<OpState>>,
|
2022-09-22 09:09:25 +00:00
|
|
|
rid: ResourceId,
|
|
|
|
len: i32,
|
2021-04-05 16:40:24 +00:00
|
|
|
) -> Result<(), AnyError> {
|
2022-09-22 09:09:25 +00:00
|
|
|
let len = len as u64;
|
2021-01-08 16:55:17 +00:00
|
|
|
|
2022-07-13 15:16:42 +00:00
|
|
|
StdFileResource::with_file_blocking_task(state, rid, move |std_file| {
|
|
|
|
std_file.set_len(len)?;
|
|
|
|
Ok(())
|
2022-04-19 14:41:31 +00:00
|
|
|
})
|
2022-07-13 15:16:42 +00:00
|
|
|
.await
|
2020-06-20 13:46:10 +00:00
|
|
|
}
|
|
|
|
|
2022-03-14 17:44:15 +00:00
|
|
|
#[op]
|
2023-03-04 23:44:59 +00:00
|
|
|
fn op_truncate_sync<P>(
|
2020-09-10 13:57:45 +00:00
|
|
|
state: &mut OpState,
|
2023-03-03 13:34:10 +00:00
|
|
|
path: &str,
|
2022-09-22 09:09:25 +00:00
|
|
|
len: u64,
|
2023-03-04 23:44:59 +00:00
|
|
|
) -> Result<(), AnyError>
|
|
|
|
where
|
|
|
|
P: FsPermissions + 'static,
|
|
|
|
{
|
2023-03-03 13:34:10 +00:00
|
|
|
let path = PathBuf::from(path);
|
2020-08-28 15:08:24 +00:00
|
|
|
|
2022-09-27 20:36:33 +00:00
|
|
|
state
|
2023-03-04 23:44:59 +00:00
|
|
|
.borrow_mut::<P>()
|
2023-01-07 16:25:34 +00:00
|
|
|
.check_write(&path, "Deno.truncateSync()")?;
|
2020-08-28 15:08:24 +00:00
|
|
|
|
|
|
|
debug!("op_truncate_sync {} {}", path.display(), len);
|
2023-02-16 19:21:03 +00:00
|
|
|
let err_mapper =
|
|
|
|
|err| default_err_mapper(err, format!("truncate '{}'", path.display()));
|
2021-10-11 13:21:18 +00:00
|
|
|
let f = std::fs::OpenOptions::new()
|
|
|
|
.write(true)
|
|
|
|
.open(&path)
|
|
|
|
.map_err(err_mapper)?;
|
|
|
|
f.set_len(len).map_err(err_mapper)?;
|
2021-04-05 16:40:24 +00:00
|
|
|
Ok(())
|
2020-08-28 15:08:24 +00:00
|
|
|
}
|
|
|
|
|
2022-03-14 17:44:15 +00:00
|
|
|
#[op]
|
2023-03-04 23:44:59 +00:00
|
|
|
async fn op_truncate_async<P>(
|
2020-09-10 13:57:45 +00:00
|
|
|
state: Rc<RefCell<OpState>>,
|
2022-09-22 09:09:25 +00:00
|
|
|
path: String,
|
|
|
|
len: u64,
|
2023-03-04 23:44:59 +00:00
|
|
|
) -> Result<(), AnyError>
|
|
|
|
where
|
|
|
|
P: FsPermissions + 'static,
|
|
|
|
{
|
2022-09-22 09:09:25 +00:00
|
|
|
let path = PathBuf::from(&path);
|
|
|
|
|
2020-09-19 23:17:35 +00:00
|
|
|
{
|
2021-04-12 02:15:43 +00:00
|
|
|
let mut state = state.borrow_mut();
|
2022-09-27 20:36:33 +00:00
|
|
|
state
|
2023-03-04 23:44:59 +00:00
|
|
|
.borrow_mut::<P>()
|
2023-01-07 16:25:34 +00:00
|
|
|
.check_write(&path, "Deno.truncate()")?;
|
2020-09-19 23:17:35 +00:00
|
|
|
}
|
2020-08-28 15:08:24 +00:00
|
|
|
tokio::task::spawn_blocking(move || {
|
|
|
|
debug!("op_truncate_async {} {}", path.display(), len);
|
2023-02-16 19:21:03 +00:00
|
|
|
let err_mapper =
|
|
|
|
|err| default_err_mapper(err, format!("truncate '{}'", path.display()));
|
2021-10-11 13:21:18 +00:00
|
|
|
let f = std::fs::OpenOptions::new()
|
|
|
|
.write(true)
|
|
|
|
.open(&path)
|
|
|
|
.map_err(err_mapper)?;
|
|
|
|
f.set_len(len).map_err(err_mapper)?;
|
2021-04-05 16:40:24 +00:00
|
|
|
Ok(())
|
2019-08-14 15:03:02 +00:00
|
|
|
})
|
2020-08-28 15:08:24 +00:00
|
|
|
.await
|
|
|
|
.unwrap()
|
2019-08-14 15:03:02 +00:00
|
|
|
}
|
|
|
|
|
2020-03-20 13:46:26 +00:00
|
|
|
fn make_temp(
|
|
|
|
dir: Option<&Path>,
|
|
|
|
prefix: Option<&str>,
|
|
|
|
suffix: Option<&str>,
|
|
|
|
is_dir: bool,
|
|
|
|
) -> std::io::Result<PathBuf> {
|
|
|
|
let prefix_ = prefix.unwrap_or("");
|
|
|
|
let suffix_ = suffix.unwrap_or("");
|
|
|
|
let mut buf: PathBuf = match dir {
|
2021-07-30 13:03:41 +00:00
|
|
|
Some(p) => p.to_path_buf(),
|
2020-03-20 13:46:26 +00:00
|
|
|
None => temp_dir(),
|
|
|
|
}
|
|
|
|
.join("_");
|
|
|
|
let mut rng = thread_rng();
|
2023-03-13 19:54:34 +00:00
|
|
|
const MAX_TRIES: u32 = 10;
|
|
|
|
for _ in 0..MAX_TRIES {
|
2020-03-20 13:46:26 +00:00
|
|
|
let unique = rng.gen::<u32>();
|
2023-01-27 15:43:16 +00:00
|
|
|
buf.set_file_name(format!("{prefix_}{unique:08x}{suffix_}"));
|
2020-03-20 13:46:26 +00:00
|
|
|
let r = if is_dir {
|
|
|
|
#[allow(unused_mut)]
|
|
|
|
let mut builder = std::fs::DirBuilder::new();
|
|
|
|
#[cfg(unix)]
|
|
|
|
{
|
|
|
|
use std::os::unix::fs::DirBuilderExt;
|
|
|
|
builder.mode(0o700);
|
|
|
|
}
|
|
|
|
builder.create(buf.as_path())
|
|
|
|
} else {
|
|
|
|
let mut open_options = std::fs::OpenOptions::new();
|
|
|
|
open_options.write(true).create_new(true);
|
|
|
|
#[cfg(unix)]
|
|
|
|
{
|
|
|
|
use std::os::unix::fs::OpenOptionsExt;
|
|
|
|
open_options.mode(0o600);
|
|
|
|
}
|
2023-03-13 19:54:34 +00:00
|
|
|
open_options.open(buf.as_path()).map(drop)
|
2020-03-20 13:46:26 +00:00
|
|
|
};
|
|
|
|
match r {
|
|
|
|
Err(ref e) if e.kind() == std::io::ErrorKind::AlreadyExists => continue,
|
|
|
|
Ok(_) => return Ok(buf),
|
|
|
|
Err(e) => return Err(e),
|
|
|
|
}
|
|
|
|
}
|
2023-03-13 19:54:34 +00:00
|
|
|
Err(io::Error::new(
|
|
|
|
io::ErrorKind::AlreadyExists,
|
|
|
|
"too many temp files exist",
|
|
|
|
))
|
2020-03-20 13:46:26 +00:00
|
|
|
}
|
|
|
|
|
2019-08-26 14:18:42 +00:00
|
|
|
#[derive(Deserialize)]
|
|
|
|
#[serde(rename_all = "camelCase")]
|
2021-03-18 18:42:01 +00:00
|
|
|
pub struct MakeTempArgs {
|
2019-08-26 14:18:42 +00:00
|
|
|
dir: Option<String>,
|
|
|
|
prefix: Option<String>,
|
|
|
|
suffix: Option<String>,
|
|
|
|
}
|
|
|
|
|
2022-03-14 17:44:15 +00:00
|
|
|
#[op]
|
2023-03-04 23:44:59 +00:00
|
|
|
fn op_make_temp_dir_sync<P>(
|
2020-09-10 13:57:45 +00:00
|
|
|
state: &mut OpState,
|
2021-03-18 18:42:01 +00:00
|
|
|
args: MakeTempArgs,
|
2023-03-04 23:44:59 +00:00
|
|
|
) -> Result<String, AnyError>
|
|
|
|
where
|
|
|
|
P: FsPermissions + 'static,
|
|
|
|
{
|
2020-08-28 15:08:24 +00:00
|
|
|
let dir = args.dir.map(|s| PathBuf::from(&s));
|
|
|
|
let prefix = args.prefix.map(String::from);
|
|
|
|
let suffix = args.suffix.map(String::from);
|
|
|
|
|
2023-03-04 23:44:59 +00:00
|
|
|
state.borrow_mut::<P>().check_write(
|
2022-09-27 20:36:33 +00:00
|
|
|
dir.clone().unwrap_or_else(temp_dir).as_path(),
|
2023-01-07 16:25:34 +00:00
|
|
|
"Deno.makeTempDirSync()",
|
2022-09-27 20:36:33 +00:00
|
|
|
)?;
|
2020-08-28 15:08:24 +00:00
|
|
|
|
|
|
|
// TODO(piscisaureus): use byte vector for paths, not a string.
|
|
|
|
// See https://github.com/denoland/deno/issues/627.
|
|
|
|
// We can't assume that paths are always valid utf8 strings.
|
|
|
|
let path = make_temp(
|
|
|
|
// Converting Option<String> to Option<&str>
|
|
|
|
dir.as_deref(),
|
|
|
|
prefix.as_deref(),
|
|
|
|
suffix.as_deref(),
|
|
|
|
true,
|
|
|
|
)?;
|
|
|
|
let path_str = into_string(path.into_os_string())?;
|
|
|
|
|
2021-04-05 16:40:24 +00:00
|
|
|
Ok(path_str)
|
2020-08-28 15:08:24 +00:00
|
|
|
}
|
|
|
|
|
2022-03-14 17:44:15 +00:00
|
|
|
#[op]
|
2023-03-04 23:44:59 +00:00
|
|
|
async fn op_make_temp_dir_async<P>(
|
2020-09-10 13:57:45 +00:00
|
|
|
state: Rc<RefCell<OpState>>,
|
2021-03-18 18:42:01 +00:00
|
|
|
args: MakeTempArgs,
|
2023-03-04 23:44:59 +00:00
|
|
|
) -> Result<String, AnyError>
|
|
|
|
where
|
|
|
|
P: FsPermissions + 'static,
|
|
|
|
{
|
2020-05-29 15:27:43 +00:00
|
|
|
let dir = args.dir.map(|s| PathBuf::from(&s));
|
2020-02-18 19:45:59 +00:00
|
|
|
let prefix = args.prefix.map(String::from);
|
|
|
|
let suffix = args.suffix.map(String::from);
|
2020-09-10 13:57:45 +00:00
|
|
|
{
|
2021-04-12 02:15:43 +00:00
|
|
|
let mut state = state.borrow_mut();
|
2023-03-04 23:44:59 +00:00
|
|
|
state.borrow_mut::<P>().check_write(
|
2022-09-27 20:36:33 +00:00
|
|
|
dir.clone().unwrap_or_else(temp_dir).as_path(),
|
2023-01-07 16:25:34 +00:00
|
|
|
"Deno.makeTempDir()",
|
2022-09-27 20:36:33 +00:00
|
|
|
)?;
|
2020-09-10 13:57:45 +00:00
|
|
|
}
|
2020-08-28 15:08:24 +00:00
|
|
|
tokio::task::spawn_blocking(move || {
|
2020-02-18 19:45:59 +00:00
|
|
|
// TODO(piscisaureus): use byte vector for paths, not a string.
|
|
|
|
// See https://github.com/denoland/deno/issues/627.
|
|
|
|
// We can't assume that paths are always valid utf8 strings.
|
2020-03-20 13:46:26 +00:00
|
|
|
let path = make_temp(
|
2020-02-18 19:45:59 +00:00
|
|
|
// Converting Option<String> to Option<&str>
|
2020-05-15 19:26:16 +00:00
|
|
|
dir.as_deref(),
|
|
|
|
prefix.as_deref(),
|
|
|
|
suffix.as_deref(),
|
2020-02-18 19:45:59 +00:00
|
|
|
true,
|
|
|
|
)?;
|
2020-04-03 17:47:57 +00:00
|
|
|
let path_str = into_string(path.into_os_string())?;
|
2020-02-18 19:45:59 +00:00
|
|
|
|
2021-04-05 16:40:24 +00:00
|
|
|
Ok(path_str)
|
2020-02-18 19:45:59 +00:00
|
|
|
})
|
2020-08-28 15:08:24 +00:00
|
|
|
.await
|
|
|
|
.unwrap()
|
2020-02-18 19:45:59 +00:00
|
|
|
}
|
|
|
|
|
2022-03-14 17:44:15 +00:00
|
|
|
#[op]
|
2023-03-04 23:44:59 +00:00
|
|
|
fn op_make_temp_file_sync<P>(
|
2020-09-10 13:57:45 +00:00
|
|
|
state: &mut OpState,
|
2021-03-18 18:42:01 +00:00
|
|
|
args: MakeTempArgs,
|
2023-03-04 23:44:59 +00:00
|
|
|
) -> Result<String, AnyError>
|
|
|
|
where
|
|
|
|
P: FsPermissions + 'static,
|
|
|
|
{
|
2020-08-28 15:08:24 +00:00
|
|
|
let dir = args.dir.map(|s| PathBuf::from(&s));
|
|
|
|
let prefix = args.prefix.map(String::from);
|
|
|
|
let suffix = args.suffix.map(String::from);
|
|
|
|
|
2023-03-04 23:44:59 +00:00
|
|
|
state.borrow_mut::<P>().check_write(
|
2022-09-27 20:36:33 +00:00
|
|
|
dir.clone().unwrap_or_else(temp_dir).as_path(),
|
2023-01-07 16:25:34 +00:00
|
|
|
"Deno.makeTempFileSync()",
|
2022-09-27 20:36:33 +00:00
|
|
|
)?;
|
2020-08-28 15:08:24 +00:00
|
|
|
|
|
|
|
// TODO(piscisaureus): use byte vector for paths, not a string.
|
|
|
|
// See https://github.com/denoland/deno/issues/627.
|
|
|
|
// We can't assume that paths are always valid utf8 strings.
|
|
|
|
let path = make_temp(
|
|
|
|
// Converting Option<String> to Option<&str>
|
|
|
|
dir.as_deref(),
|
|
|
|
prefix.as_deref(),
|
|
|
|
suffix.as_deref(),
|
|
|
|
false,
|
|
|
|
)?;
|
|
|
|
let path_str = into_string(path.into_os_string())?;
|
|
|
|
|
2021-04-05 16:40:24 +00:00
|
|
|
Ok(path_str)
|
2020-08-28 15:08:24 +00:00
|
|
|
}
|
|
|
|
|
2022-03-14 17:44:15 +00:00
|
|
|
#[op]
|
2023-03-04 23:44:59 +00:00
|
|
|
async fn op_make_temp_file_async<P>(
|
2020-09-10 13:57:45 +00:00
|
|
|
state: Rc<RefCell<OpState>>,
|
2021-03-18 18:42:01 +00:00
|
|
|
args: MakeTempArgs,
|
2023-03-04 23:44:59 +00:00
|
|
|
) -> Result<String, AnyError>
|
|
|
|
where
|
|
|
|
P: FsPermissions + 'static,
|
|
|
|
{
|
2020-05-29 15:27:43 +00:00
|
|
|
let dir = args.dir.map(|s| PathBuf::from(&s));
|
2019-08-26 14:18:42 +00:00
|
|
|
let prefix = args.prefix.map(String::from);
|
|
|
|
let suffix = args.suffix.map(String::from);
|
2020-09-10 13:57:45 +00:00
|
|
|
{
|
2021-04-12 02:15:43 +00:00
|
|
|
let mut state = state.borrow_mut();
|
2023-03-04 23:44:59 +00:00
|
|
|
state.borrow_mut::<P>().check_write(
|
2022-09-27 20:36:33 +00:00
|
|
|
dir.clone().unwrap_or_else(temp_dir).as_path(),
|
2023-01-07 16:25:34 +00:00
|
|
|
"Deno.makeTempFile()",
|
2022-09-27 20:36:33 +00:00
|
|
|
)?;
|
2020-09-10 13:57:45 +00:00
|
|
|
}
|
2020-08-28 15:08:24 +00:00
|
|
|
tokio::task::spawn_blocking(move || {
|
2019-08-14 15:03:02 +00:00
|
|
|
// TODO(piscisaureus): use byte vector for paths, not a string.
|
|
|
|
// See https://github.com/denoland/deno/issues/627.
|
|
|
|
// We can't assume that paths are always valid utf8 strings.
|
2020-03-20 13:46:26 +00:00
|
|
|
let path = make_temp(
|
2019-08-14 15:03:02 +00:00
|
|
|
// Converting Option<String> to Option<&str>
|
2020-05-15 19:26:16 +00:00
|
|
|
dir.as_deref(),
|
|
|
|
prefix.as_deref(),
|
|
|
|
suffix.as_deref(),
|
2020-02-18 19:45:59 +00:00
|
|
|
false,
|
2019-08-14 15:03:02 +00:00
|
|
|
)?;
|
2020-04-03 17:47:57 +00:00
|
|
|
let path_str = into_string(path.into_os_string())?;
|
2019-08-26 14:18:42 +00:00
|
|
|
|
2021-04-05 16:40:24 +00:00
|
|
|
Ok(path_str)
|
2019-08-14 15:03:02 +00:00
|
|
|
})
|
2020-08-28 15:08:24 +00:00
|
|
|
.await
|
|
|
|
.unwrap()
|
2019-08-14 15:03:02 +00:00
|
|
|
}
|
|
|
|
|
2022-03-14 17:44:15 +00:00
|
|
|
#[op]
|
2020-08-31 18:29:43 +00:00
|
|
|
fn op_futime_sync(
|
2020-09-10 13:57:45 +00:00
|
|
|
state: &mut OpState,
|
2022-09-22 09:09:25 +00:00
|
|
|
rid: ResourceId,
|
|
|
|
atime_secs: i64,
|
|
|
|
atime_nanos: u32,
|
|
|
|
mtime_secs: i64,
|
|
|
|
mtime_nanos: u32,
|
2021-04-05 16:40:24 +00:00
|
|
|
) -> Result<(), AnyError> {
|
2022-09-22 09:09:25 +00:00
|
|
|
let atime = filetime::FileTime::from_unix_time(atime_secs, atime_nanos);
|
|
|
|
let mtime = filetime::FileTime::from_unix_time(mtime_secs, mtime_nanos);
|
2020-08-31 18:29:43 +00:00
|
|
|
|
2022-05-11 16:48:38 +00:00
|
|
|
StdFileResource::with_file(state, rid, |std_file| {
|
|
|
|
filetime::set_file_handle_times(std_file, Some(atime), Some(mtime))
|
|
|
|
.map_err(AnyError::from)
|
2020-08-31 18:29:43 +00:00
|
|
|
})?;
|
|
|
|
|
2021-04-05 16:40:24 +00:00
|
|
|
Ok(())
|
2020-08-31 18:29:43 +00:00
|
|
|
}
|
|
|
|
|
2022-03-14 17:44:15 +00:00
|
|
|
#[op]
|
2020-08-31 18:29:43 +00:00
|
|
|
async fn op_futime_async(
|
2020-09-10 13:57:45 +00:00
|
|
|
state: Rc<RefCell<OpState>>,
|
2022-09-22 09:09:25 +00:00
|
|
|
rid: ResourceId,
|
|
|
|
atime_secs: i64,
|
|
|
|
atime_nanos: u32,
|
|
|
|
mtime_secs: i64,
|
|
|
|
mtime_nanos: u32,
|
2021-04-05 16:40:24 +00:00
|
|
|
) -> Result<(), AnyError> {
|
2022-09-22 09:09:25 +00:00
|
|
|
let atime = filetime::FileTime::from_unix_time(atime_secs, atime_nanos);
|
|
|
|
let mtime = filetime::FileTime::from_unix_time(mtime_secs, mtime_nanos);
|
2020-08-31 18:29:43 +00:00
|
|
|
|
2022-07-13 15:16:42 +00:00
|
|
|
StdFileResource::with_file_blocking_task(state, rid, move |std_file| {
|
|
|
|
filetime::set_file_handle_times(std_file, Some(atime), Some(mtime))?;
|
2021-04-05 16:40:24 +00:00
|
|
|
Ok(())
|
2021-01-08 16:55:17 +00:00
|
|
|
})
|
|
|
|
.await
|
2020-08-31 18:29:43 +00:00
|
|
|
}
|
|
|
|
|
2022-03-14 17:44:15 +00:00
|
|
|
#[op]
|
2023-03-04 23:44:59 +00:00
|
|
|
fn op_utime_sync<P>(
|
2022-09-22 09:09:25 +00:00
|
|
|
state: &mut OpState,
|
2023-03-03 13:34:10 +00:00
|
|
|
path: &str,
|
2022-09-22 09:09:25 +00:00
|
|
|
atime_secs: i64,
|
|
|
|
atime_nanos: u32,
|
|
|
|
mtime_secs: i64,
|
|
|
|
mtime_nanos: u32,
|
2023-03-04 23:44:59 +00:00
|
|
|
) -> Result<(), AnyError>
|
|
|
|
where
|
|
|
|
P: FsPermissions + 'static,
|
|
|
|
{
|
2023-03-03 13:34:10 +00:00
|
|
|
let path = PathBuf::from(path);
|
2022-09-22 09:09:25 +00:00
|
|
|
let atime = filetime::FileTime::from_unix_time(atime_secs, atime_nanos);
|
|
|
|
let mtime = filetime::FileTime::from_unix_time(mtime_secs, mtime_nanos);
|
2020-08-28 15:08:24 +00:00
|
|
|
|
2023-03-04 23:44:59 +00:00
|
|
|
state.borrow_mut::<P>().check_write(&path, "Deno.utime()")?;
|
2021-10-11 13:21:18 +00:00
|
|
|
filetime::set_file_times(&path, atime, mtime).map_err(|err| {
|
2023-02-16 19:21:03 +00:00
|
|
|
default_err_mapper(err, format!("utime '{}'", path.display()))
|
2021-10-11 13:21:18 +00:00
|
|
|
})?;
|
2021-04-05 16:40:24 +00:00
|
|
|
Ok(())
|
2020-08-28 15:08:24 +00:00
|
|
|
}
|
|
|
|
|
2022-03-14 17:44:15 +00:00
|
|
|
#[op]
|
2023-03-04 23:44:59 +00:00
|
|
|
async fn op_utime_async<P>(
|
2020-09-10 13:57:45 +00:00
|
|
|
state: Rc<RefCell<OpState>>,
|
2022-09-22 09:09:25 +00:00
|
|
|
path: String,
|
|
|
|
atime_secs: i64,
|
|
|
|
atime_nanos: u32,
|
|
|
|
mtime_secs: i64,
|
|
|
|
mtime_nanos: u32,
|
2023-03-04 23:44:59 +00:00
|
|
|
) -> Result<(), AnyError>
|
|
|
|
where
|
|
|
|
P: FsPermissions + 'static,
|
|
|
|
{
|
2022-09-22 09:09:25 +00:00
|
|
|
let path = PathBuf::from(&path);
|
|
|
|
let atime = filetime::FileTime::from_unix_time(atime_secs, atime_nanos);
|
|
|
|
let mtime = filetime::FileTime::from_unix_time(mtime_secs, mtime_nanos);
|
2020-03-20 13:46:26 +00:00
|
|
|
|
2021-04-12 02:15:43 +00:00
|
|
|
state
|
|
|
|
.borrow_mut()
|
2023-03-04 23:44:59 +00:00
|
|
|
.borrow_mut::<P>()
|
2023-01-07 16:25:34 +00:00
|
|
|
.check_write(&path, "Deno.utime()")?;
|
2020-03-20 13:46:26 +00:00
|
|
|
|
2020-08-28 15:08:24 +00:00
|
|
|
tokio::task::spawn_blocking(move || {
|
2021-10-11 13:21:18 +00:00
|
|
|
filetime::set_file_times(&path, atime, mtime).map_err(|err| {
|
2023-02-16 19:21:03 +00:00
|
|
|
default_err_mapper(err, format!("utime '{}'", path.display()))
|
2021-10-11 13:21:18 +00:00
|
|
|
})?;
|
2021-04-05 16:40:24 +00:00
|
|
|
Ok(())
|
2019-08-14 15:03:02 +00:00
|
|
|
})
|
2020-08-28 15:08:24 +00:00
|
|
|
.await
|
|
|
|
.unwrap()
|
2019-08-14 15:03:02 +00:00
|
|
|
}
|
|
|
|
|
2022-03-14 17:44:15 +00:00
|
|
|
#[op]
|
2023-03-04 23:44:59 +00:00
|
|
|
fn op_cwd<P>(state: &mut OpState) -> Result<String, AnyError>
|
|
|
|
where
|
|
|
|
P: FsPermissions + 'static,
|
|
|
|
{
|
2020-03-20 13:46:26 +00:00
|
|
|
let path = current_dir()?;
|
2023-01-07 16:25:34 +00:00
|
|
|
state
|
2023-03-04 23:44:59 +00:00
|
|
|
.borrow_mut::<P>()
|
2023-01-07 16:25:34 +00:00
|
|
|
.check_read_blind(&path, "CWD", "Deno.cwd()")?;
|
2020-04-03 17:47:57 +00:00
|
|
|
let path_str = into_string(path.into_os_string())?;
|
2021-04-05 16:40:24 +00:00
|
|
|
Ok(path_str)
|
2019-08-14 15:03:02 +00:00
|
|
|
}
|
2022-04-27 14:03:44 +00:00
|
|
|
|
|
|
|
#[op]
|
2023-03-04 23:44:59 +00:00
|
|
|
fn op_readfile_sync<P>(
|
2022-04-27 14:03:44 +00:00
|
|
|
state: &mut OpState,
|
|
|
|
path: String,
|
2023-03-04 23:44:59 +00:00
|
|
|
) -> Result<ZeroCopyBuf, AnyError>
|
|
|
|
where
|
|
|
|
P: FsPermissions + 'static,
|
|
|
|
{
|
2022-04-27 14:03:44 +00:00
|
|
|
let path = Path::new(&path);
|
2023-01-07 16:25:34 +00:00
|
|
|
state
|
2023-03-04 23:44:59 +00:00
|
|
|
.borrow_mut::<P>()
|
2023-01-07 16:25:34 +00:00
|
|
|
.check_read(path, "Deno.readFileSync()")?;
|
2023-02-16 19:21:03 +00:00
|
|
|
Ok(
|
|
|
|
std::fs::read(path)
|
|
|
|
.map_err(|err| {
|
|
|
|
default_err_mapper(err, format!("readfile '{}'", path.display()))
|
|
|
|
})?
|
|
|
|
.into(),
|
|
|
|
)
|
2022-04-27 14:03:44 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
#[op]
|
2023-03-04 23:44:59 +00:00
|
|
|
fn op_readfile_text_sync<P>(
|
2022-04-27 14:03:44 +00:00
|
|
|
state: &mut OpState,
|
|
|
|
path: String,
|
2023-03-04 23:44:59 +00:00
|
|
|
) -> Result<String, AnyError>
|
|
|
|
where
|
|
|
|
P: FsPermissions + 'static,
|
|
|
|
{
|
2022-04-27 14:03:44 +00:00
|
|
|
let path = Path::new(&path);
|
2023-01-07 16:25:34 +00:00
|
|
|
state
|
2023-03-04 23:44:59 +00:00
|
|
|
.borrow_mut::<P>()
|
2023-01-07 16:25:34 +00:00
|
|
|
.check_read(path, "Deno.readTextFileSync()")?;
|
2023-02-16 19:21:03 +00:00
|
|
|
Ok(string_from_utf8_lossy(std::fs::read(path).map_err(
|
|
|
|
|err| default_err_mapper(err, format!("readfile '{}'", path.display())),
|
|
|
|
)?))
|
2022-04-27 14:03:44 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
#[op]
|
2023-03-04 23:44:59 +00:00
|
|
|
async fn op_readfile_async<P>(
|
2022-04-27 14:03:44 +00:00
|
|
|
state: Rc<RefCell<OpState>>,
|
|
|
|
path: String,
|
|
|
|
cancel_rid: Option<ResourceId>,
|
2023-03-04 23:44:59 +00:00
|
|
|
) -> Result<ZeroCopyBuf, AnyError>
|
|
|
|
where
|
|
|
|
P: FsPermissions + 'static,
|
|
|
|
{
|
2022-04-27 14:03:44 +00:00
|
|
|
{
|
|
|
|
let path = Path::new(&path);
|
|
|
|
let mut state = state.borrow_mut();
|
2022-09-27 20:36:33 +00:00
|
|
|
state
|
2023-03-04 23:44:59 +00:00
|
|
|
.borrow_mut::<P>()
|
2023-01-07 16:25:34 +00:00
|
|
|
.check_read(path, "Deno.readFile()")?;
|
2022-04-27 14:03:44 +00:00
|
|
|
}
|
2023-02-11 12:19:13 +00:00
|
|
|
|
|
|
|
let read_future = tokio::task::spawn_blocking(move || {
|
2022-04-27 14:03:44 +00:00
|
|
|
let path = Path::new(&path);
|
2023-02-16 19:21:03 +00:00
|
|
|
Ok(std::fs::read(path).map(ZeroCopyBuf::from).map_err(|err| {
|
|
|
|
default_err_mapper(err, format!("readfile '{}'", path.display()))
|
|
|
|
})?)
|
2022-04-27 14:03:44 +00:00
|
|
|
});
|
2023-02-11 12:19:13 +00:00
|
|
|
|
|
|
|
let cancel_handle = cancel_rid.and_then(|rid| {
|
|
|
|
state
|
2022-04-27 14:03:44 +00:00
|
|
|
.borrow_mut()
|
|
|
|
.resource_table
|
2023-02-11 12:19:13 +00:00
|
|
|
.get::<CancelHandle>(rid)
|
|
|
|
.ok()
|
|
|
|
});
|
|
|
|
|
|
|
|
if let Some(cancel_handle) = cancel_handle {
|
|
|
|
let read_future_rv = read_future.or_cancel(cancel_handle).await;
|
|
|
|
|
|
|
|
if let Some(cancel_rid) = cancel_rid {
|
|
|
|
state.borrow_mut().resource_table.close(cancel_rid).ok();
|
|
|
|
};
|
|
|
|
|
|
|
|
return read_future_rv??;
|
2022-04-27 14:03:44 +00:00
|
|
|
}
|
2023-02-11 12:19:13 +00:00
|
|
|
|
|
|
|
read_future.await?
|
2022-04-27 14:03:44 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
#[op]
|
2023-03-04 23:44:59 +00:00
|
|
|
async fn op_readfile_text_async<P>(
|
2022-04-27 14:03:44 +00:00
|
|
|
state: Rc<RefCell<OpState>>,
|
|
|
|
path: String,
|
|
|
|
cancel_rid: Option<ResourceId>,
|
2023-03-04 23:44:59 +00:00
|
|
|
) -> Result<String, AnyError>
|
|
|
|
where
|
|
|
|
P: FsPermissions + 'static,
|
|
|
|
{
|
2022-04-27 14:03:44 +00:00
|
|
|
{
|
|
|
|
let path = Path::new(&path);
|
|
|
|
let mut state = state.borrow_mut();
|
2022-09-27 20:36:33 +00:00
|
|
|
state
|
2023-03-04 23:44:59 +00:00
|
|
|
.borrow_mut::<P>()
|
2023-01-07 16:25:34 +00:00
|
|
|
.check_read(path, "Deno.readTextFile()")?;
|
2022-04-27 14:03:44 +00:00
|
|
|
}
|
2023-02-11 12:19:13 +00:00
|
|
|
|
|
|
|
let read_future = tokio::task::spawn_blocking(move || {
|
2022-04-27 14:03:44 +00:00
|
|
|
let path = Path::new(&path);
|
2023-02-16 19:21:03 +00:00
|
|
|
Ok(string_from_utf8_lossy(std::fs::read(path).map_err(
|
|
|
|
|err| default_err_mapper(err, format!("readfile '{}'", path.display())),
|
|
|
|
)?))
|
2022-04-27 14:03:44 +00:00
|
|
|
});
|
2023-02-11 12:19:13 +00:00
|
|
|
|
|
|
|
let cancel_handle = cancel_rid.and_then(|rid| {
|
|
|
|
state
|
2022-04-27 14:03:44 +00:00
|
|
|
.borrow_mut()
|
|
|
|
.resource_table
|
2023-02-11 12:19:13 +00:00
|
|
|
.get::<CancelHandle>(rid)
|
|
|
|
.ok()
|
|
|
|
});
|
|
|
|
|
|
|
|
if let Some(cancel_handle) = cancel_handle {
|
|
|
|
let read_future_rv = read_future.or_cancel(cancel_handle).await;
|
|
|
|
|
|
|
|
if let Some(cancel_rid) = cancel_rid {
|
|
|
|
state.borrow_mut().resource_table.close(cancel_rid).ok();
|
|
|
|
};
|
|
|
|
|
|
|
|
return read_future_rv??;
|
2022-04-27 14:03:44 +00:00
|
|
|
}
|
2023-02-11 12:19:13 +00:00
|
|
|
|
|
|
|
read_future.await?
|
2022-04-27 14:03:44 +00:00
|
|
|
}
|
2022-05-02 02:13:05 +00:00
|
|
|
|
|
|
|
// Like String::from_utf8_lossy but operates on owned values
|
|
|
|
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) },
|
|
|
|
}
|
|
|
|
}
|