2022-01-07 22:09:52 -05:00
|
|
|
// Copyright 2018-2022 the Deno authors. All rights reserved. MIT license.
|
2019-08-26 10:18:42 -04:00
|
|
|
// Some deserializer fields are only used on Unix and Windows build fails without it
|
2021-01-14 23:32:27 -05:00
|
|
|
use super::io::StdFileResource;
|
2021-04-06 04:33:43 -04:00
|
|
|
use super::utils::into_string;
|
2020-11-16 14:48:50 -05:00
|
|
|
use crate::fs_util::canonicalize_path;
|
2020-09-19 19:17:35 -04:00
|
|
|
use crate::permissions::Permissions;
|
2020-09-14 12:48:57 -04:00
|
|
|
use deno_core::error::custom_error;
|
|
|
|
use deno_core::error::type_error;
|
|
|
|
use deno_core::error::AnyError;
|
2022-03-14 13:44:15 -04:00
|
|
|
use deno_core::op;
|
2022-04-18 18:00:14 -04:00
|
|
|
use deno_core::CancelFuture;
|
|
|
|
use deno_core::CancelHandle;
|
|
|
|
use deno_core::ZeroCopyBuf;
|
2022-03-14 13:44:15 -04:00
|
|
|
|
2021-05-02 19:22:57 -04:00
|
|
|
use deno_core::Extension;
|
2020-09-10 09:57:45 -04:00
|
|
|
use deno_core::OpState;
|
2021-03-19 13:25:37 -04:00
|
|
|
use deno_core::ResourceId;
|
2020-11-13 16:01:57 -05:00
|
|
|
use deno_crypto::rand::thread_rng;
|
|
|
|
use deno_crypto::rand::Rng;
|
2021-03-26 12:34:25 -04:00
|
|
|
use log::debug;
|
2020-09-16 12:43:08 -04:00
|
|
|
use serde::Deserialize;
|
2021-04-05 12:40:24 -04:00
|
|
|
use serde::Serialize;
|
2022-05-01 22:13:05 -04:00
|
|
|
use std::borrow::Cow;
|
2020-09-10 09:57:45 -04:00
|
|
|
use std::cell::RefCell;
|
2019-08-14 11:03:02 -04:00
|
|
|
use std::convert::From;
|
2022-05-10 10:13:08 -04:00
|
|
|
use std::env::current_dir;
|
|
|
|
use std::env::set_current_dir;
|
|
|
|
use std::env::temp_dir;
|
2020-04-27 14:09:56 -04:00
|
|
|
use std::io;
|
2022-05-10 10:13:08 -04:00
|
|
|
use std::io::Error;
|
|
|
|
use std::io::Seek;
|
|
|
|
use std::io::SeekFrom;
|
2022-04-18 18:00:14 -04:00
|
|
|
use std::io::Write;
|
2022-05-10 10:13:08 -04:00
|
|
|
use std::path::Path;
|
|
|
|
use std::path::PathBuf;
|
2020-08-18 08:07:57 -04:00
|
|
|
use std::rc::Rc;
|
2020-04-27 14:09:56 -04:00
|
|
|
use std::time::SystemTime;
|
2019-08-14 11:03:02 -04:00
|
|
|
use std::time::UNIX_EPOCH;
|
|
|
|
|
2020-09-14 12:48:57 -04:00
|
|
|
#[cfg(not(unix))]
|
|
|
|
use deno_core::error::generic_error;
|
|
|
|
#[cfg(not(unix))]
|
|
|
|
use deno_core::error::not_supported;
|
|
|
|
|
2021-05-02 19:22:57 -04:00
|
|
|
pub fn init() -> Extension {
|
|
|
|
Extension::builder()
|
|
|
|
.ops(vec![
|
2022-03-14 13:44:15 -04:00
|
|
|
op_open_sync::decl(),
|
|
|
|
op_open_async::decl(),
|
2022-04-18 18:00:14 -04:00
|
|
|
op_write_file_sync::decl(),
|
|
|
|
op_write_file_async::decl(),
|
2022-03-14 13:44:15 -04: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(),
|
|
|
|
op_chdir::decl(),
|
|
|
|
op_mkdir_sync::decl(),
|
|
|
|
op_mkdir_async::decl(),
|
|
|
|
op_chmod_sync::decl(),
|
|
|
|
op_chmod_async::decl(),
|
|
|
|
op_chown_sync::decl(),
|
|
|
|
op_chown_async::decl(),
|
|
|
|
op_remove_sync::decl(),
|
|
|
|
op_remove_async::decl(),
|
|
|
|
op_copy_file_sync::decl(),
|
|
|
|
op_copy_file_async::decl(),
|
|
|
|
op_stat_sync::decl(),
|
|
|
|
op_stat_async::decl(),
|
|
|
|
op_realpath_sync::decl(),
|
|
|
|
op_realpath_async::decl(),
|
|
|
|
op_read_dir_sync::decl(),
|
|
|
|
op_read_dir_async::decl(),
|
|
|
|
op_rename_sync::decl(),
|
|
|
|
op_rename_async::decl(),
|
|
|
|
op_link_sync::decl(),
|
|
|
|
op_link_async::decl(),
|
|
|
|
op_symlink_sync::decl(),
|
|
|
|
op_symlink_async::decl(),
|
|
|
|
op_read_link_sync::decl(),
|
|
|
|
op_read_link_async::decl(),
|
|
|
|
op_ftruncate_sync::decl(),
|
|
|
|
op_ftruncate_async::decl(),
|
|
|
|
op_truncate_sync::decl(),
|
|
|
|
op_truncate_async::decl(),
|
|
|
|
op_make_temp_dir_sync::decl(),
|
|
|
|
op_make_temp_dir_async::decl(),
|
|
|
|
op_make_temp_file_sync::decl(),
|
|
|
|
op_make_temp_file_async::decl(),
|
|
|
|
op_cwd::decl(),
|
|
|
|
op_futime_sync::decl(),
|
|
|
|
op_futime_async::decl(),
|
|
|
|
op_utime_sync::decl(),
|
|
|
|
op_utime_async::decl(),
|
2022-04-27 10:03:44 -04:00
|
|
|
op_readfile_sync::decl(),
|
|
|
|
op_readfile_text_sync::decl(),
|
|
|
|
op_readfile_async::decl(),
|
|
|
|
op_readfile_text_async::decl(),
|
2021-05-02 19:22:57 -04:00
|
|
|
])
|
|
|
|
.build()
|
2019-10-11 14:41:54 -04:00
|
|
|
}
|
|
|
|
|
2020-03-07 07:20:27 -05:00
|
|
|
#[derive(Deserialize, Default, Debug)]
|
|
|
|
#[serde(rename_all = "camelCase")]
|
|
|
|
#[serde(default)]
|
2021-03-18 14:42:01 -04:00
|
|
|
pub struct OpenOptions {
|
2020-03-07 07:20:27 -05:00
|
|
|
read: bool,
|
|
|
|
write: bool,
|
|
|
|
create: bool,
|
|
|
|
truncate: bool,
|
|
|
|
append: bool,
|
|
|
|
create_new: bool,
|
|
|
|
}
|
|
|
|
|
2022-08-19 06:24:40 -04:00
|
|
|
#[inline]
|
2020-08-18 08:07:57 -04:00
|
|
|
fn open_helper(
|
2020-09-10 09:57:45 -04:00
|
|
|
state: &mut OpState,
|
2022-08-19 06:24:40 -04:00
|
|
|
path: &str,
|
|
|
|
mode: Option<u32>,
|
|
|
|
options: Option<&OpenOptions>,
|
2022-09-27 16:36:33 -04:00
|
|
|
api_name: &str,
|
2020-09-14 12:48:57 -04:00
|
|
|
) -> Result<(PathBuf, std::fs::OpenOptions), AnyError> {
|
2022-08-19 06:24:40 -04:00
|
|
|
let path = Path::new(path).to_path_buf();
|
2020-03-16 15:02:41 -04:00
|
|
|
|
2020-04-15 20:43:19 -04:00
|
|
|
let mut open_options = std::fs::OpenOptions::new();
|
|
|
|
|
2022-08-19 06:24:40 -04:00
|
|
|
if let Some(mode) = mode {
|
2020-03-16 15:02:41 -04:00
|
|
|
// mode only used if creating the file on Unix
|
|
|
|
// if not specified, defaults to 0o666
|
|
|
|
#[cfg(unix)]
|
2020-03-20 09:46:26 -04:00
|
|
|
{
|
|
|
|
use std::os::unix::fs::OpenOptionsExt;
|
2020-04-15 20:43:19 -04:00
|
|
|
open_options.mode(mode & 0o777);
|
2020-03-20 09:46:26 -04:00
|
|
|
}
|
2020-03-16 15:02:41 -04:00
|
|
|
#[cfg(not(unix))]
|
|
|
|
let _ = mode; // avoid unused warning
|
2020-04-15 20:43:19 -04:00
|
|
|
}
|
2020-03-07 07:20:27 -05:00
|
|
|
|
2021-04-11 22:15:43 -04:00
|
|
|
let permissions = state.borrow_mut::<Permissions>();
|
2020-09-19 19:17:35 -04:00
|
|
|
|
2022-08-19 06:24:40 -04:00
|
|
|
match options {
|
|
|
|
None => {
|
2022-09-27 16:36:33 -04:00
|
|
|
permissions.read.check(&path, Some(api_name))?;
|
2022-08-19 06:24:40 -04:00
|
|
|
open_options
|
|
|
|
.read(true)
|
|
|
|
.create(false)
|
|
|
|
.write(false)
|
|
|
|
.truncate(false)
|
|
|
|
.append(false)
|
|
|
|
.create_new(false);
|
|
|
|
}
|
|
|
|
Some(options) => {
|
|
|
|
if options.read {
|
2022-09-27 16:36:33 -04:00
|
|
|
permissions.read.check(&path, Some(api_name))?;
|
2022-08-19 06:24:40 -04:00
|
|
|
}
|
2020-03-07 07:20:27 -05:00
|
|
|
|
2022-08-19 06:24:40 -04:00
|
|
|
if options.write || options.append {
|
2022-09-27 16:36:33 -04:00
|
|
|
permissions.write.check(&path, Some(api_name))?;
|
2022-08-19 06:24:40 -04:00
|
|
|
}
|
2020-03-07 07:20:27 -05:00
|
|
|
|
2022-08-19 06:24:40 -04: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 07:20:27 -05:00
|
|
|
|
2020-08-18 08:07:57 -04:00
|
|
|
Ok((path, open_options))
|
|
|
|
}
|
|
|
|
|
2022-03-14 13:44:15 -04:00
|
|
|
#[op]
|
2020-08-18 08:07:57 -04:00
|
|
|
fn op_open_sync(
|
2020-09-10 09:57:45 -04:00
|
|
|
state: &mut OpState,
|
2022-08-19 06:24:40 -04:00
|
|
|
path: String,
|
|
|
|
options: Option<OpenOptions>,
|
|
|
|
mode: Option<u32>,
|
2021-04-05 12:40:24 -04:00
|
|
|
) -> Result<ResourceId, AnyError> {
|
2022-09-27 16:36:33 -04:00
|
|
|
let (path, open_options) =
|
|
|
|
open_helper(state, &path, mode, options.as_ref(), "Deno.openSync()")?;
|
2021-10-11 09:21:18 -04:00
|
|
|
let std_file = open_options.open(&path).map_err(|err| {
|
|
|
|
Error::new(err.kind(), format!("{}, open '{}'", err, path.display()))
|
|
|
|
})?;
|
2022-04-19 10:41:31 -04:00
|
|
|
let resource = StdFileResource::fs_file(std_file);
|
2020-12-16 11:14:12 -05:00
|
|
|
let rid = state.resource_table.add(resource);
|
2021-04-05 12:40:24 -04:00
|
|
|
Ok(rid)
|
2020-08-18 08:07:57 -04:00
|
|
|
}
|
|
|
|
|
2022-03-14 13:44:15 -04:00
|
|
|
#[op]
|
2020-08-18 08:07:57 -04:00
|
|
|
async fn op_open_async(
|
2020-09-10 09:57:45 -04:00
|
|
|
state: Rc<RefCell<OpState>>,
|
2022-08-19 06:24:40 -04:00
|
|
|
path: String,
|
|
|
|
options: Option<OpenOptions>,
|
|
|
|
mode: Option<u32>,
|
2021-04-05 12:40:24 -04:00
|
|
|
) -> Result<ResourceId, AnyError> {
|
2022-09-27 16:36:33 -04:00
|
|
|
let (path, open_options) = open_helper(
|
|
|
|
&mut state.borrow_mut(),
|
|
|
|
&path,
|
|
|
|
mode,
|
|
|
|
options.as_ref(),
|
|
|
|
"Deno.open()",
|
|
|
|
)?;
|
2022-04-19 10:41:31 -04:00
|
|
|
let std_file = tokio::task::spawn_blocking(move || {
|
|
|
|
open_options.open(path.clone()).map_err(|err| {
|
2021-10-11 09:21:18 -04:00
|
|
|
Error::new(err.kind(), format!("{}, open '{}'", err, path.display()))
|
2022-04-19 10:41:31 -04:00
|
|
|
})
|
|
|
|
})
|
|
|
|
.await?;
|
|
|
|
let resource = StdFileResource::fs_file(std_file?);
|
2020-12-16 11:14:12 -05:00
|
|
|
let rid = state.borrow_mut().resource_table.add(resource);
|
2021-04-05 12:40:24 -04:00
|
|
|
Ok(rid)
|
2020-03-07 07:20:27 -05:00
|
|
|
}
|
|
|
|
|
2022-09-05 07:50:48 -04:00
|
|
|
#[inline]
|
2022-12-12 23:12:49 -05:00
|
|
|
fn write_open_options(
|
|
|
|
create: bool,
|
|
|
|
append: bool,
|
|
|
|
create_new: bool,
|
|
|
|
) -> OpenOptions {
|
2022-09-05 07:50:48 -04:00
|
|
|
OpenOptions {
|
|
|
|
read: false,
|
|
|
|
write: true,
|
|
|
|
create,
|
|
|
|
truncate: !append,
|
|
|
|
append,
|
2022-12-12 23:12:49 -05:00
|
|
|
create_new,
|
2022-04-18 18:00:14 -04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
#[op]
|
|
|
|
fn op_write_file_sync(
|
|
|
|
state: &mut OpState,
|
2022-09-05 07:50:48 -04:00
|
|
|
path: String,
|
|
|
|
mode: Option<u32>,
|
|
|
|
append: bool,
|
|
|
|
create: bool,
|
2022-12-12 23:12:49 -05:00
|
|
|
create_new: bool,
|
2022-09-05 07:50:48 -04:00
|
|
|
data: ZeroCopyBuf,
|
2022-04-18 18:00:14 -04:00
|
|
|
) -> Result<(), AnyError> {
|
2022-08-19 06:24:40 -04:00
|
|
|
let (path, open_options) = open_helper(
|
|
|
|
state,
|
2022-09-05 07:50:48 -04:00
|
|
|
&path,
|
|
|
|
mode,
|
2022-12-12 23:12:49 -05:00
|
|
|
Some(&write_open_options(create, append, create_new)),
|
2022-09-27 16:36:33 -04:00
|
|
|
"Deno.writeFileSync()",
|
2022-08-19 06:24:40 -04:00
|
|
|
)?;
|
2022-09-05 07:50:48 -04:00
|
|
|
write_file(&path, open_options, mode, data)
|
2022-04-18 18:00:14 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
#[op]
|
|
|
|
async fn op_write_file_async(
|
|
|
|
state: Rc<RefCell<OpState>>,
|
2022-09-05 07:50:48 -04:00
|
|
|
path: String,
|
|
|
|
mode: Option<u32>,
|
|
|
|
append: bool,
|
|
|
|
create: bool,
|
2022-12-12 23:12:49 -05:00
|
|
|
create_new: bool,
|
2022-09-05 07:50:48 -04:00
|
|
|
data: ZeroCopyBuf,
|
|
|
|
cancel_rid: Option<ResourceId>,
|
2022-04-18 18:00:14 -04:00
|
|
|
) -> Result<(), AnyError> {
|
2022-09-05 07:50:48 -04:00
|
|
|
let cancel_handle = match cancel_rid {
|
2022-04-18 18:00:14 -04:00
|
|
|
Some(cancel_rid) => state
|
|
|
|
.borrow_mut()
|
|
|
|
.resource_table
|
|
|
|
.get::<CancelHandle>(cancel_rid)
|
|
|
|
.ok(),
|
|
|
|
None => None,
|
|
|
|
};
|
2022-08-19 06:24:40 -04:00
|
|
|
let (path, open_options) = open_helper(
|
2022-11-17 20:59:10 -05:00
|
|
|
&mut state.borrow_mut(),
|
2022-09-05 07:50:48 -04:00
|
|
|
&path,
|
|
|
|
mode,
|
2022-12-12 23:12:49 -05:00
|
|
|
Some(&write_open_options(create, append, create_new)),
|
2022-09-27 16:36:33 -04:00
|
|
|
"Deno.writeFile()",
|
2022-08-19 06:24:40 -04:00
|
|
|
)?;
|
2022-04-18 18:00:14 -04:00
|
|
|
let write_future = tokio::task::spawn_blocking(move || {
|
2022-09-05 07:50:48 -04:00
|
|
|
write_file(&path, open_options, mode, data)
|
2022-04-18 18:00:14 -04:00
|
|
|
});
|
|
|
|
if let Some(cancel_handle) = cancel_handle {
|
|
|
|
write_future.or_cancel(cancel_handle).await???;
|
|
|
|
} else {
|
|
|
|
write_future.await??;
|
|
|
|
}
|
|
|
|
Ok(())
|
|
|
|
}
|
|
|
|
|
|
|
|
fn write_file(
|
|
|
|
path: &Path,
|
|
|
|
open_options: std::fs::OpenOptions,
|
2022-09-05 07:50:48 -04:00
|
|
|
_mode: Option<u32>,
|
2022-04-18 18:00:14 -04:00
|
|
|
data: ZeroCopyBuf,
|
|
|
|
) -> Result<(), AnyError> {
|
|
|
|
let mut std_file = open_options.open(path).map_err(|err| {
|
|
|
|
Error::new(err.kind(), format!("{}, open '{}'", err, path.display()))
|
|
|
|
})?;
|
|
|
|
|
|
|
|
// need to chmod the file if it already exists and a mode is specified
|
|
|
|
#[cfg(unix)]
|
2022-09-05 07:50:48 -04:00
|
|
|
if let Some(mode) = _mode {
|
2022-04-18 18:00:14 -04:00
|
|
|
use std::os::unix::fs::PermissionsExt;
|
|
|
|
let permissions = PermissionsExt::from_mode(mode & 0o777);
|
|
|
|
std_file
|
|
|
|
.set_permissions(permissions)
|
|
|
|
.map_err(|err: Error| {
|
|
|
|
Error::new(err.kind(), format!("{}, chmod '{}'", err, path.display()))
|
|
|
|
})?;
|
|
|
|
}
|
|
|
|
|
|
|
|
std_file.write_all(&data)?;
|
|
|
|
Ok(())
|
|
|
|
}
|
|
|
|
|
2020-03-07 07:20:27 -05:00
|
|
|
#[derive(Deserialize)]
|
|
|
|
#[serde(rename_all = "camelCase")]
|
2021-03-18 14:42:01 -04:00
|
|
|
pub struct SeekArgs {
|
2021-03-19 13:25:37 -04:00
|
|
|
rid: ResourceId,
|
2020-06-27 15:58:35 -04:00
|
|
|
offset: i64,
|
2020-03-07 07:20:27 -05:00
|
|
|
whence: i32,
|
|
|
|
}
|
|
|
|
|
2021-03-18 14:42:01 -04:00
|
|
|
fn seek_helper(args: SeekArgs) -> Result<(u32, SeekFrom), AnyError> {
|
2021-03-18 20:55:31 -04:00
|
|
|
let rid = args.rid;
|
2020-03-07 07:20:27 -05: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 15:58:35 -04:00
|
|
|
1 => SeekFrom::Current(offset),
|
|
|
|
2 => SeekFrom::End(offset),
|
2020-03-07 07:20:27 -05:00
|
|
|
_ => {
|
2020-09-14 12:48:57 -04:00
|
|
|
return Err(type_error(format!("Invalid seek mode: {}", whence)));
|
2020-03-07 07:20:27 -05:00
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2020-08-28 11:08:24 -04:00
|
|
|
Ok((rid, seek_from))
|
|
|
|
}
|
2020-03-07 07:20:27 -05:00
|
|
|
|
2022-03-14 13:44:15 -04:00
|
|
|
#[op]
|
2022-03-14 18:38:53 -04:00
|
|
|
fn op_seek_sync(state: &mut OpState, args: SeekArgs) -> Result<u64, AnyError> {
|
2020-08-28 11:08:24 -04:00
|
|
|
let (rid, seek_from) = seek_helper(args)?;
|
2022-07-13 11:16:42 -04:00
|
|
|
StdFileResource::with_file(state, rid, |std_file| {
|
2022-05-11 12:48:38 -04:00
|
|
|
std_file.seek(seek_from).map_err(AnyError::from)
|
2022-07-13 11:16:42 -04:00
|
|
|
})
|
2020-08-28 11:08:24 -04:00
|
|
|
}
|
|
|
|
|
2022-03-14 13:44:15 -04:00
|
|
|
#[op]
|
2020-08-28 11:08:24 -04:00
|
|
|
async fn op_seek_async(
|
2020-09-10 09:57:45 -04:00
|
|
|
state: Rc<RefCell<OpState>>,
|
2021-03-18 14:42:01 -04:00
|
|
|
args: SeekArgs,
|
2021-04-05 12:40:24 -04:00
|
|
|
) -> Result<u64, AnyError> {
|
2020-08-28 11:08:24 -04:00
|
|
|
let (rid, seek_from) = seek_helper(args)?;
|
2021-01-08 11:55:17 -05:00
|
|
|
|
2022-07-13 11:16:42 -04:00
|
|
|
StdFileResource::with_file_blocking_task(state, rid, move |std_file| {
|
|
|
|
std_file.seek(seek_from).map_err(AnyError::from)
|
2022-04-19 10:41:31 -04:00
|
|
|
})
|
2022-07-13 11:16:42 -04:00
|
|
|
.await
|
2020-06-26 08:36:35 -04:00
|
|
|
}
|
|
|
|
|
2022-03-14 13:44:15 -04:00
|
|
|
#[op]
|
2020-08-28 11:08:24 -04:00
|
|
|
fn op_fdatasync_sync(
|
2020-09-10 09:57:45 -04:00
|
|
|
state: &mut OpState,
|
2021-04-05 12:40:24 -04:00
|
|
|
rid: ResourceId,
|
|
|
|
) -> Result<(), AnyError> {
|
2022-05-11 12:48:38 -04:00
|
|
|
StdFileResource::with_file(state, rid, |std_file| {
|
|
|
|
std_file.sync_data().map_err(AnyError::from)
|
2022-07-13 11:16:42 -04:00
|
|
|
})
|
2020-08-28 11:08:24 -04:00
|
|
|
}
|
2020-06-26 08:36:35 -04:00
|
|
|
|
2022-03-14 13:44:15 -04:00
|
|
|
#[op]
|
2020-08-28 11:08:24 -04:00
|
|
|
async fn op_fdatasync_async(
|
2020-09-10 09:57:45 -04:00
|
|
|
state: Rc<RefCell<OpState>>,
|
2021-04-05 12:40:24 -04:00
|
|
|
rid: ResourceId,
|
|
|
|
) -> Result<(), AnyError> {
|
2022-07-13 11:16:42 -04:00
|
|
|
StdFileResource::with_file_blocking_task(state, rid, move |std_file| {
|
|
|
|
std_file.sync_data().map_err(AnyError::from)
|
2022-04-19 10:41:31 -04:00
|
|
|
})
|
2022-07-13 11:16:42 -04:00
|
|
|
.await
|
2020-06-21 09:29:44 -04:00
|
|
|
}
|
|
|
|
|
2022-03-14 13:44:15 -04:00
|
|
|
#[op]
|
2022-03-14 18:38:53 -04:00
|
|
|
fn op_fsync_sync(state: &mut OpState, rid: ResourceId) -> Result<(), AnyError> {
|
2022-05-11 12:48:38 -04:00
|
|
|
StdFileResource::with_file(state, rid, |std_file| {
|
|
|
|
std_file.sync_all().map_err(AnyError::from)
|
2022-07-13 11:16:42 -04:00
|
|
|
})
|
2020-08-28 11:08:24 -04:00
|
|
|
}
|
2020-06-21 09:29:44 -04:00
|
|
|
|
2022-03-14 13:44:15 -04:00
|
|
|
#[op]
|
2020-08-28 11:08:24 -04:00
|
|
|
async fn op_fsync_async(
|
2020-09-10 09:57:45 -04:00
|
|
|
state: Rc<RefCell<OpState>>,
|
2021-04-05 12:40:24 -04:00
|
|
|
rid: ResourceId,
|
|
|
|
) -> Result<(), AnyError> {
|
2022-07-13 11:16:42 -04:00
|
|
|
StdFileResource::with_file_blocking_task(state, rid, move |std_file| {
|
|
|
|
std_file.sync_all().map_err(AnyError::from)
|
2022-04-19 10:41:31 -04:00
|
|
|
})
|
2022-07-13 11:16:42 -04:00
|
|
|
.await
|
2020-06-22 08:58:52 -04:00
|
|
|
}
|
|
|
|
|
2022-03-14 13:44:15 -04:00
|
|
|
#[op]
|
2020-08-28 11:08:24 -04:00
|
|
|
fn op_fstat_sync(
|
2020-09-10 09:57:45 -04:00
|
|
|
state: &mut OpState,
|
2021-04-05 12:40:24 -04:00
|
|
|
rid: ResourceId,
|
2022-09-22 05:09:25 -04:00
|
|
|
out_buf: &mut [u32],
|
|
|
|
) -> Result<(), AnyError> {
|
2022-05-11 12:48:38 -04:00
|
|
|
let metadata = StdFileResource::with_file(state, rid, |std_file| {
|
|
|
|
std_file.metadata().map_err(AnyError::from)
|
2020-08-28 11:08:24 -04:00
|
|
|
})?;
|
2022-09-22 05:09:25 -04:00
|
|
|
let stat = get_stat(metadata);
|
|
|
|
stat.write(out_buf);
|
|
|
|
Ok(())
|
2020-08-28 11:08:24 -04:00
|
|
|
}
|
2020-06-22 08:58:52 -04:00
|
|
|
|
2022-03-14 13:44:15 -04:00
|
|
|
#[op]
|
2020-08-28 11:08:24 -04:00
|
|
|
async fn op_fstat_async(
|
2020-09-10 09:57:45 -04:00
|
|
|
state: Rc<RefCell<OpState>>,
|
2021-04-05 12:40:24 -04:00
|
|
|
rid: ResourceId,
|
|
|
|
) -> Result<FsStat, AnyError> {
|
2022-07-13 11:16:42 -04:00
|
|
|
let metadata =
|
|
|
|
StdFileResource::with_file_blocking_task(state, rid, move |std_file| {
|
|
|
|
std_file.metadata().map_err(AnyError::from)
|
|
|
|
})
|
|
|
|
.await?;
|
2021-04-05 12:40:24 -04:00
|
|
|
Ok(get_stat(metadata))
|
2020-03-10 15:11:27 -04:00
|
|
|
}
|
|
|
|
|
2022-03-14 13:44:15 -04:00
|
|
|
#[op]
|
2021-08-24 09:21:31 -04:00
|
|
|
fn op_flock_sync(
|
|
|
|
state: &mut OpState,
|
|
|
|
rid: ResourceId,
|
|
|
|
exclusive: bool,
|
|
|
|
) -> Result<(), AnyError> {
|
|
|
|
use fs3::FileExt;
|
|
|
|
super::check_unstable(state, "Deno.flockSync");
|
|
|
|
|
2022-05-11 12:48:38 -04:00
|
|
|
StdFileResource::with_file(state, rid, |std_file| {
|
|
|
|
if exclusive {
|
|
|
|
std_file.lock_exclusive()?;
|
|
|
|
} else {
|
|
|
|
std_file.lock_shared()?;
|
2021-08-24 09:21:31 -04:00
|
|
|
}
|
2022-05-11 12:48:38 -04:00
|
|
|
Ok(())
|
2021-08-24 09:21:31 -04:00
|
|
|
})
|
|
|
|
}
|
|
|
|
|
2022-03-14 13:44:15 -04:00
|
|
|
#[op]
|
2021-08-24 09:21:31 -04:00
|
|
|
async fn op_flock_async(
|
|
|
|
state: Rc<RefCell<OpState>>,
|
|
|
|
rid: ResourceId,
|
|
|
|
exclusive: bool,
|
|
|
|
) -> Result<(), AnyError> {
|
|
|
|
use fs3::FileExt;
|
|
|
|
super::check_unstable2(&state, "Deno.flock");
|
|
|
|
|
2022-07-13 11:16:42 -04:00
|
|
|
StdFileResource::with_file_blocking_task(state, rid, move |std_file| {
|
2021-08-24 09:21:31 -04:00
|
|
|
if exclusive {
|
|
|
|
std_file.lock_exclusive()?;
|
|
|
|
} else {
|
|
|
|
std_file.lock_shared()?;
|
|
|
|
}
|
|
|
|
Ok(())
|
|
|
|
})
|
2022-07-13 11:16:42 -04:00
|
|
|
.await
|
2021-08-24 09:21:31 -04:00
|
|
|
}
|
|
|
|
|
2022-03-14 13:44:15 -04:00
|
|
|
#[op]
|
2021-08-24 09:21:31 -04:00
|
|
|
fn op_funlock_sync(
|
|
|
|
state: &mut OpState,
|
|
|
|
rid: ResourceId,
|
|
|
|
) -> Result<(), AnyError> {
|
|
|
|
use fs3::FileExt;
|
|
|
|
super::check_unstable(state, "Deno.funlockSync");
|
|
|
|
|
2022-05-11 12:48:38 -04:00
|
|
|
StdFileResource::with_file(state, rid, |std_file| {
|
|
|
|
std_file.unlock()?;
|
|
|
|
Ok(())
|
2021-08-24 09:21:31 -04:00
|
|
|
})
|
|
|
|
}
|
|
|
|
|
2022-03-14 13:44:15 -04:00
|
|
|
#[op]
|
2021-08-24 09:21:31 -04:00
|
|
|
async fn op_funlock_async(
|
|
|
|
state: Rc<RefCell<OpState>>,
|
|
|
|
rid: ResourceId,
|
|
|
|
) -> Result<(), AnyError> {
|
|
|
|
use fs3::FileExt;
|
|
|
|
super::check_unstable2(&state, "Deno.funlock");
|
|
|
|
|
2022-07-13 11:16:42 -04:00
|
|
|
StdFileResource::with_file_blocking_task(state, rid, move |std_file| {
|
2021-08-24 09:21:31 -04:00
|
|
|
std_file.unlock()?;
|
|
|
|
Ok(())
|
|
|
|
})
|
2022-07-13 11:16:42 -04:00
|
|
|
.await
|
2021-08-24 09:21:31 -04:00
|
|
|
}
|
|
|
|
|
2022-03-14 13:44:15 -04:00
|
|
|
#[op]
|
2022-03-14 18:38:53 -04:00
|
|
|
fn op_umask(state: &mut OpState, mask: Option<u32>) -> Result<u32, AnyError> {
|
2020-09-26 14:26:51 -04:00
|
|
|
super::check_unstable(state, "Deno.umask");
|
2020-03-10 15:11:27 -04: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 12:40:24 -04:00
|
|
|
let _ = mask; // avoid unused warning.
|
2020-09-14 12:48:57 -04:00
|
|
|
Err(not_supported())
|
2020-03-10 15:11:27 -04:00
|
|
|
}
|
|
|
|
#[cfg(unix)]
|
|
|
|
{
|
|
|
|
use nix::sys::stat::mode_t;
|
|
|
|
use nix::sys::stat::umask;
|
|
|
|
use nix::sys::stat::Mode;
|
2021-04-05 12:40:24 -04:00
|
|
|
let r = if let Some(mask) = mask {
|
2020-03-10 15:11:27 -04: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 17:20:15 -05:00
|
|
|
#[cfg(target_os = "linux")]
|
|
|
|
{
|
|
|
|
Ok(r.bits())
|
|
|
|
}
|
|
|
|
#[cfg(target_os = "macos")]
|
|
|
|
{
|
|
|
|
Ok(r.bits() as u32)
|
|
|
|
}
|
2020-03-10 15:11:27 -04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-03-14 13:44:15 -04:00
|
|
|
#[op]
|
2022-12-15 10:26:10 -05:00
|
|
|
fn op_chdir(state: &mut OpState, directory: String) -> Result<(), AnyError> {
|
2021-04-05 12:40:24 -04:00
|
|
|
let d = PathBuf::from(&directory);
|
2022-09-27 16:36:33 -04:00
|
|
|
state
|
|
|
|
.borrow_mut::<Permissions>()
|
|
|
|
.read
|
|
|
|
.check(&d, Some("Deno.chdir()"))?;
|
2022-12-15 10:26:10 -05:00
|
|
|
set_current_dir(&d).map_err(|err| {
|
2021-10-11 09:21:18 -04:00
|
|
|
Error::new(err.kind(), format!("{}, chdir '{}'", err, directory))
|
|
|
|
})?;
|
2021-04-05 12:40:24 -04:00
|
|
|
Ok(())
|
2019-08-26 10:18:42 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
#[derive(Deserialize)]
|
|
|
|
#[serde(rename_all = "camelCase")]
|
2021-03-18 14:42:01 -04:00
|
|
|
pub struct MkdirArgs {
|
2019-08-26 10:18:42 -04:00
|
|
|
path: String,
|
|
|
|
recursive: bool,
|
2020-03-11 16:14:23 -04:00
|
|
|
mode: Option<u32>,
|
2019-08-14 11:03:02 -04:00
|
|
|
}
|
|
|
|
|
2022-03-14 13:44:15 -04:00
|
|
|
#[op]
|
2022-03-14 18:38:53 -04:00
|
|
|
fn op_mkdir_sync(state: &mut OpState, args: MkdirArgs) -> Result<(), AnyError> {
|
2020-05-29 11:27:43 -04:00
|
|
|
let path = Path::new(&args.path).to_path_buf();
|
2020-03-20 09:46:26 -04:00
|
|
|
let mode = args.mode.unwrap_or(0o777) & 0o777;
|
2022-09-27 16:36:33 -04:00
|
|
|
state
|
|
|
|
.borrow_mut::<Permissions>()
|
|
|
|
.write
|
|
|
|
.check(&path, Some("Deno.mkdirSync()"))?;
|
2020-08-28 11:08:24 -04: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 09:21:18 -04:00
|
|
|
builder.create(&path).map_err(|err| {
|
|
|
|
Error::new(err.kind(), format!("{}, mkdir '{}'", err, path.display()))
|
|
|
|
})?;
|
2021-04-05 12:40:24 -04:00
|
|
|
Ok(())
|
2020-08-28 11:08:24 -04:00
|
|
|
}
|
2019-08-14 11:03:02 -04:00
|
|
|
|
2022-03-14 13:44:15 -04:00
|
|
|
#[op]
|
2020-08-28 11:08:24 -04:00
|
|
|
async fn op_mkdir_async(
|
2020-09-10 09:57:45 -04:00
|
|
|
state: Rc<RefCell<OpState>>,
|
2021-03-18 14:42:01 -04:00
|
|
|
args: MkdirArgs,
|
2021-04-05 12:40:24 -04:00
|
|
|
) -> Result<(), AnyError> {
|
2020-08-28 11:08:24 -04:00
|
|
|
let path = Path::new(&args.path).to_path_buf();
|
|
|
|
let mode = args.mode.unwrap_or(0o777) & 0o777;
|
2020-09-10 09:57:45 -04:00
|
|
|
|
2020-09-19 19:17:35 -04:00
|
|
|
{
|
2021-04-11 22:15:43 -04:00
|
|
|
let mut state = state.borrow_mut();
|
2022-09-27 16:36:33 -04:00
|
|
|
state
|
|
|
|
.borrow_mut::<Permissions>()
|
|
|
|
.write
|
|
|
|
.check(&path, Some("Deno.mkdir()"))?;
|
2020-09-19 19:17:35 -04:00
|
|
|
}
|
2020-09-10 09:57:45 -04:00
|
|
|
|
2020-08-28 11:08:24 -04:00
|
|
|
tokio::task::spawn_blocking(move || {
|
2020-03-11 16:14:23 -04:00
|
|
|
debug!("op_mkdir {} {:o} {}", path.display(), mode, args.recursive);
|
2020-03-20 09:46:26 -04: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 09:21:18 -04:00
|
|
|
builder.create(&path).map_err(|err| {
|
|
|
|
Error::new(err.kind(), format!("{}, mkdir '{}'", err, path.display()))
|
|
|
|
})?;
|
2021-04-05 12:40:24 -04:00
|
|
|
Ok(())
|
2019-08-14 11:03:02 -04:00
|
|
|
})
|
2020-08-28 11:08:24 -04:00
|
|
|
.await
|
|
|
|
.unwrap()
|
2019-08-14 11:03:02 -04:00
|
|
|
}
|
|
|
|
|
2022-09-22 05:09:25 -04:00
|
|
|
#[op]
|
|
|
|
fn op_chmod_sync(
|
|
|
|
state: &mut OpState,
|
2022-12-15 10:26:10 -05:00
|
|
|
path: String,
|
2019-08-26 10:18:42 -04:00
|
|
|
mode: u32,
|
2022-09-22 05:09:25 -04:00
|
|
|
) -> Result<(), AnyError> {
|
2022-12-15 10:26:10 -05:00
|
|
|
let path = Path::new(&path);
|
2022-09-22 05:09:25 -04:00
|
|
|
let mode = mode & 0o777;
|
2019-08-14 11:03:02 -04:00
|
|
|
|
2022-09-27 16:36:33 -04:00
|
|
|
state
|
|
|
|
.borrow_mut::<Permissions>()
|
|
|
|
.write
|
|
|
|
.check(path, Some("Deno.chmodSync()"))?;
|
2022-04-18 18:00:14 -04:00
|
|
|
raw_chmod(path, mode)
|
2020-08-28 11:08:24 -04:00
|
|
|
}
|
2019-08-14 11:03:02 -04:00
|
|
|
|
2022-03-14 13:44:15 -04:00
|
|
|
#[op]
|
2020-08-28 11:08:24 -04:00
|
|
|
async fn op_chmod_async(
|
2020-09-10 09:57:45 -04:00
|
|
|
state: Rc<RefCell<OpState>>,
|
2022-09-22 05:09:25 -04:00
|
|
|
path: String,
|
|
|
|
mode: u32,
|
2021-04-05 12:40:24 -04:00
|
|
|
) -> Result<(), AnyError> {
|
2022-09-22 05:09:25 -04:00
|
|
|
let path = Path::new(&path).to_path_buf();
|
|
|
|
let mode = mode & 0o777;
|
2020-09-10 09:57:45 -04:00
|
|
|
|
2020-09-19 19:17:35 -04:00
|
|
|
{
|
2021-04-11 22:15:43 -04:00
|
|
|
let mut state = state.borrow_mut();
|
2022-09-27 16:36:33 -04:00
|
|
|
state
|
|
|
|
.borrow_mut::<Permissions>()
|
|
|
|
.write
|
|
|
|
.check(&path, Some("Deno.chmod()"))?;
|
2020-09-19 19:17:35 -04:00
|
|
|
}
|
2020-09-10 09:57:45 -04:00
|
|
|
|
2022-09-22 05:09:25 -04:00
|
|
|
tokio::task::spawn_blocking(move || raw_chmod(&path, mode))
|
|
|
|
.await
|
|
|
|
.unwrap()
|
2019-08-14 11:03:02 -04:00
|
|
|
}
|
|
|
|
|
2022-04-18 18:00:14 -04:00
|
|
|
fn raw_chmod(path: &Path, _raw_mode: u32) -> Result<(), AnyError> {
|
|
|
|
let err_mapper = |err: Error| {
|
|
|
|
Error::new(err.kind(), format!("{}, chmod '{}'", err, path.display()))
|
|
|
|
};
|
|
|
|
#[cfg(unix)]
|
|
|
|
{
|
|
|
|
use std::os::unix::fs::PermissionsExt;
|
|
|
|
let permissions = PermissionsExt::from_mode(_raw_mode);
|
2022-11-17 20:59:10 -05:00
|
|
|
std::fs::set_permissions(path, permissions).map_err(err_mapper)?;
|
2022-04-18 18:00:14 -04:00
|
|
|
Ok(())
|
|
|
|
}
|
|
|
|
// TODO Implement chmod for Windows (#4357)
|
|
|
|
#[cfg(not(unix))]
|
|
|
|
{
|
|
|
|
// Still check file/dir exists on Windows
|
2022-11-18 12:54:01 -05:00
|
|
|
let _metadata = std::fs::metadata(path).map_err(err_mapper)?;
|
2022-04-18 18:00:14 -04:00
|
|
|
Err(not_supported())
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-09-22 05:09:25 -04:00
|
|
|
#[op]
|
|
|
|
fn op_chown_sync(
|
|
|
|
state: &mut OpState,
|
2022-12-15 10:26:10 -05:00
|
|
|
path: String,
|
2022-09-22 10:33:52 -04:00
|
|
|
#[cfg_attr(windows, allow(unused_variables))] uid: Option<u32>,
|
|
|
|
#[cfg_attr(windows, allow(unused_variables))] gid: Option<u32>,
|
2022-09-22 05:09:25 -04:00
|
|
|
) -> Result<(), AnyError> {
|
2022-12-15 10:26:10 -05:00
|
|
|
let path = Path::new(&path).to_path_buf();
|
2022-09-27 16:36:33 -04:00
|
|
|
state
|
|
|
|
.borrow_mut::<Permissions>()
|
|
|
|
.write
|
|
|
|
.check(&path, Some("Deno.chownSync()"))?;
|
2020-08-28 11:08:24 -04:00
|
|
|
#[cfg(unix)]
|
|
|
|
{
|
2021-10-11 09:21:18 -04:00
|
|
|
use crate::errors::get_nix_error_class;
|
2020-08-28 11:08:24 -04:00
|
|
|
use nix::unistd::{chown, Gid, Uid};
|
2022-09-22 05:09:25 -04:00
|
|
|
let nix_uid = uid.map(Uid::from_raw);
|
|
|
|
let nix_gid = gid.map(Gid::from_raw);
|
2021-10-11 09:21:18 -04: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 12:40:24 -04:00
|
|
|
Ok(())
|
2020-08-28 11:08:24 -04:00
|
|
|
}
|
|
|
|
// TODO Implement chown for Windows
|
|
|
|
#[cfg(not(unix))]
|
|
|
|
{
|
2020-09-14 12:48:57 -04:00
|
|
|
Err(generic_error("Not implemented"))
|
2020-08-28 11:08:24 -04:00
|
|
|
}
|
|
|
|
}
|
2019-08-26 10:18:42 -04:00
|
|
|
|
2022-03-14 13:44:15 -04:00
|
|
|
#[op]
|
2020-08-28 11:08:24 -04:00
|
|
|
async fn op_chown_async(
|
2020-09-10 09:57:45 -04:00
|
|
|
state: Rc<RefCell<OpState>>,
|
2022-09-22 05:09:25 -04:00
|
|
|
path: String,
|
2022-09-22 10:33:52 -04:00
|
|
|
#[cfg_attr(windows, allow(unused_variables))] uid: Option<u32>,
|
|
|
|
#[cfg_attr(windows, allow(unused_variables))] gid: Option<u32>,
|
2021-04-05 12:40:24 -04:00
|
|
|
) -> Result<(), AnyError> {
|
2022-09-22 05:09:25 -04:00
|
|
|
let path = Path::new(&path).to_path_buf();
|
2020-09-10 09:57:45 -04:00
|
|
|
|
2020-09-19 19:17:35 -04:00
|
|
|
{
|
2021-04-11 22:15:43 -04:00
|
|
|
let mut state = state.borrow_mut();
|
2022-09-27 16:36:33 -04:00
|
|
|
state
|
|
|
|
.borrow_mut::<Permissions>()
|
|
|
|
.write
|
|
|
|
.check(&path, Some("Deno.chown()"))?;
|
2020-09-19 19:17:35 -04:00
|
|
|
}
|
2020-09-10 09:57:45 -04:00
|
|
|
|
2020-08-28 11:08:24 -04:00
|
|
|
tokio::task::spawn_blocking(move || {
|
2020-03-20 09:46:26 -04:00
|
|
|
#[cfg(unix)]
|
|
|
|
{
|
2021-10-11 09:21:18 -04:00
|
|
|
use crate::errors::get_nix_error_class;
|
2020-03-20 09:46:26 -04:00
|
|
|
use nix::unistd::{chown, Gid, Uid};
|
2022-09-22 05:09:25 -04:00
|
|
|
let nix_uid = uid.map(Uid::from_raw);
|
|
|
|
let nix_gid = gid.map(Gid::from_raw);
|
2021-10-11 09:21:18 -04: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 12:40:24 -04:00
|
|
|
Ok(())
|
2020-03-20 09:46:26 -04:00
|
|
|
}
|
|
|
|
// TODO Implement chown for Windows
|
|
|
|
#[cfg(not(unix))]
|
2020-09-14 12:48:57 -04:00
|
|
|
Err(not_supported())
|
2019-08-14 11:03:02 -04:00
|
|
|
})
|
2020-08-28 11:08:24 -04:00
|
|
|
.await
|
|
|
|
.unwrap()
|
2019-08-14 11:03:02 -04:00
|
|
|
}
|
|
|
|
|
2022-03-14 13:44:15 -04:00
|
|
|
#[op]
|
2020-08-28 11:08:24 -04:00
|
|
|
fn op_remove_sync(
|
2020-09-10 09:57:45 -04:00
|
|
|
state: &mut OpState,
|
2022-12-15 10:26:10 -05:00
|
|
|
path: String,
|
2022-09-22 05:09:25 -04:00
|
|
|
recursive: bool,
|
2021-04-05 12:40:24 -04:00
|
|
|
) -> Result<(), AnyError> {
|
2022-12-15 10:26:10 -05:00
|
|
|
let path = PathBuf::from(&path);
|
2020-08-28 11:08:24 -04:00
|
|
|
|
2022-09-27 16:36:33 -04:00
|
|
|
state
|
|
|
|
.borrow_mut::<Permissions>()
|
|
|
|
.write
|
|
|
|
.check(&path, Some("Deno.removeSync()"))?;
|
2020-08-28 11:08:24 -04:00
|
|
|
|
|
|
|
#[cfg(not(unix))]
|
|
|
|
use std::os::windows::prelude::MetadataExt;
|
|
|
|
|
2021-10-11 09:21:18 -04:00
|
|
|
let err_mapper = |err: Error| {
|
|
|
|
Error::new(err.kind(), format!("{}, remove '{}'", err, path.display()))
|
|
|
|
};
|
|
|
|
let metadata = std::fs::symlink_metadata(&path).map_err(err_mapper)?;
|
2020-08-28 11:08:24 -04:00
|
|
|
|
|
|
|
let file_type = metadata.file_type();
|
|
|
|
if file_type.is_file() {
|
2021-10-11 09:21:18 -04:00
|
|
|
std::fs::remove_file(&path).map_err(err_mapper)?;
|
2020-08-28 11:08:24 -04:00
|
|
|
} else if recursive {
|
2021-10-11 09:21:18 -04:00
|
|
|
std::fs::remove_dir_all(&path).map_err(err_mapper)?;
|
2020-08-28 11:08:24 -04:00
|
|
|
} else if file_type.is_symlink() {
|
|
|
|
#[cfg(unix)]
|
2021-10-11 09:21:18 -04:00
|
|
|
std::fs::remove_file(&path).map_err(err_mapper)?;
|
2020-08-28 11:08:24 -04:00
|
|
|
#[cfg(not(unix))]
|
|
|
|
{
|
|
|
|
use winapi::um::winnt::FILE_ATTRIBUTE_DIRECTORY;
|
|
|
|
if metadata.file_attributes() & FILE_ATTRIBUTE_DIRECTORY != 0 {
|
2021-10-11 09:21:18 -04:00
|
|
|
std::fs::remove_dir(&path).map_err(err_mapper)?;
|
2020-08-28 11:08:24 -04:00
|
|
|
} else {
|
2021-10-11 09:21:18 -04:00
|
|
|
std::fs::remove_file(&path).map_err(err_mapper)?;
|
2020-08-28 11:08:24 -04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
} else if file_type.is_dir() {
|
2021-10-11 09:21:18 -04:00
|
|
|
std::fs::remove_dir(&path).map_err(err_mapper)?;
|
2020-08-28 11:08:24 -04:00
|
|
|
} else {
|
|
|
|
// pipes, sockets, etc...
|
2021-10-11 09:21:18 -04:00
|
|
|
std::fs::remove_file(&path).map_err(err_mapper)?;
|
2020-08-28 11:08:24 -04:00
|
|
|
}
|
2021-04-05 12:40:24 -04:00
|
|
|
Ok(())
|
2020-08-28 11:08:24 -04:00
|
|
|
}
|
|
|
|
|
2022-03-14 13:44:15 -04:00
|
|
|
#[op]
|
2020-08-28 11:08:24 -04:00
|
|
|
async fn op_remove_async(
|
2020-09-10 09:57:45 -04:00
|
|
|
state: Rc<RefCell<OpState>>,
|
2022-09-22 05:09:25 -04:00
|
|
|
path: String,
|
|
|
|
recursive: bool,
|
2021-04-05 12:40:24 -04:00
|
|
|
) -> Result<(), AnyError> {
|
2022-09-22 05:09:25 -04:00
|
|
|
let path = PathBuf::from(&path);
|
2019-08-14 11:03:02 -04:00
|
|
|
|
2020-09-19 19:17:35 -04:00
|
|
|
{
|
2021-04-11 22:15:43 -04:00
|
|
|
let mut state = state.borrow_mut();
|
2022-09-27 16:36:33 -04:00
|
|
|
state
|
|
|
|
.borrow_mut::<Permissions>()
|
|
|
|
.write
|
|
|
|
.check(&path, Some("Deno.remove()"))?;
|
2020-09-19 19:17:35 -04:00
|
|
|
}
|
2019-08-14 11:03:02 -04:00
|
|
|
|
2020-08-28 11:08:24 -04:00
|
|
|
tokio::task::spawn_blocking(move || {
|
2020-05-18 08:50:44 -04:00
|
|
|
#[cfg(not(unix))]
|
|
|
|
use std::os::windows::prelude::MetadataExt;
|
2021-10-11 09:21:18 -04:00
|
|
|
let err_mapper = |err: Error| {
|
|
|
|
Error::new(err.kind(), format!("{}, remove '{}'", err, path.display()))
|
|
|
|
};
|
|
|
|
let metadata = std::fs::symlink_metadata(&path).map_err(err_mapper)?;
|
2020-05-18 08:50:44 -04:00
|
|
|
|
2020-08-28 11:08:24 -04:00
|
|
|
debug!("op_remove_async {} {}", path.display(), recursive);
|
2020-02-03 08:20:15 -05:00
|
|
|
let file_type = metadata.file_type();
|
2020-05-18 08:50:44 -04:00
|
|
|
if file_type.is_file() {
|
2021-10-11 09:21:18 -04:00
|
|
|
std::fs::remove_file(&path).map_err(err_mapper)?;
|
2019-08-14 11:03:02 -04:00
|
|
|
} else if recursive {
|
2021-10-11 09:21:18 -04:00
|
|
|
std::fs::remove_dir_all(&path).map_err(err_mapper)?;
|
2020-05-18 08:50:44 -04:00
|
|
|
} else if file_type.is_symlink() {
|
|
|
|
#[cfg(unix)]
|
2021-10-11 09:21:18 -04:00
|
|
|
std::fs::remove_file(&path).map_err(err_mapper)?;
|
2020-05-18 08:50:44 -04:00
|
|
|
#[cfg(not(unix))]
|
|
|
|
{
|
|
|
|
use winapi::um::winnt::FILE_ATTRIBUTE_DIRECTORY;
|
|
|
|
if metadata.file_attributes() & FILE_ATTRIBUTE_DIRECTORY != 0 {
|
2021-10-11 09:21:18 -04:00
|
|
|
std::fs::remove_dir(&path).map_err(err_mapper)?;
|
2020-05-18 08:50:44 -04:00
|
|
|
} else {
|
2021-10-11 09:21:18 -04:00
|
|
|
std::fs::remove_file(&path).map_err(err_mapper)?;
|
2020-05-18 08:50:44 -04:00
|
|
|
}
|
|
|
|
}
|
2020-05-30 14:48:26 -04:00
|
|
|
} else if file_type.is_dir() {
|
2021-10-11 09:21:18 -04:00
|
|
|
std::fs::remove_dir(&path).map_err(err_mapper)?;
|
2020-05-30 14:48:26 -04:00
|
|
|
} else {
|
|
|
|
// pipes, sockets, etc...
|
2021-10-11 09:21:18 -04:00
|
|
|
std::fs::remove_file(&path).map_err(err_mapper)?;
|
2019-08-14 11:03:02 -04:00
|
|
|
}
|
2021-04-05 12:40:24 -04:00
|
|
|
Ok(())
|
2019-08-14 11:03:02 -04:00
|
|
|
})
|
2020-08-28 11:08:24 -04:00
|
|
|
.await
|
|
|
|
.unwrap()
|
2019-08-14 11:03:02 -04:00
|
|
|
}
|
|
|
|
|
2022-03-14 13:44:15 -04:00
|
|
|
#[op]
|
2020-08-28 11:08:24 -04:00
|
|
|
fn op_copy_file_sync(
|
2020-09-10 09:57:45 -04:00
|
|
|
state: &mut OpState,
|
2022-12-15 10:26:10 -05:00
|
|
|
from: String,
|
|
|
|
to: String,
|
2021-04-05 12:40:24 -04:00
|
|
|
) -> Result<(), AnyError> {
|
2022-12-15 10:26:10 -05:00
|
|
|
let from_path = PathBuf::from(&from);
|
|
|
|
let to_path = PathBuf::from(&to);
|
2020-08-28 11:08:24 -04:00
|
|
|
|
2021-04-11 22:15:43 -04:00
|
|
|
let permissions = state.borrow_mut::<Permissions>();
|
2022-09-27 16:36:33 -04:00
|
|
|
permissions
|
|
|
|
.read
|
|
|
|
.check(&from_path, Some("Deno.copyFileSync()"))?;
|
|
|
|
permissions
|
|
|
|
.write
|
|
|
|
.check(&to_path, Some("Deno.copyFileSync()"))?;
|
2020-08-28 11:08:24 -04: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 05:09:25 -04:00
|
|
|
if cfg!(unix) && !from_path.is_file() {
|
2021-10-11 09:21:18 -04:00
|
|
|
return Err(custom_error(
|
|
|
|
"NotFound",
|
|
|
|
format!(
|
|
|
|
"File not found, copy '{}' -> '{}'",
|
2022-09-22 05:09:25 -04:00
|
|
|
from_path.display(),
|
|
|
|
to_path.display()
|
2021-10-11 09:21:18 -04:00
|
|
|
),
|
|
|
|
));
|
2020-08-28 11:08:24 -04:00
|
|
|
}
|
|
|
|
|
2021-10-11 09:21:18 -04:00
|
|
|
let err_mapper = |err: Error| {
|
|
|
|
Error::new(
|
|
|
|
err.kind(),
|
2022-09-22 05:09:25 -04:00
|
|
|
format!(
|
|
|
|
"{}, copy '{}' -> '{}'",
|
|
|
|
err,
|
|
|
|
from_path.display(),
|
|
|
|
to_path.display()
|
|
|
|
),
|
2021-10-11 09:21:18 -04:00
|
|
|
)
|
|
|
|
};
|
2022-09-22 05:09:25 -04:00
|
|
|
|
|
|
|
#[cfg(target_os = "macos")]
|
|
|
|
{
|
|
|
|
use libc::clonefile;
|
|
|
|
use libc::stat;
|
|
|
|
use libc::unlink;
|
|
|
|
use std::ffi::CString;
|
|
|
|
use std::io::Read;
|
|
|
|
|
|
|
|
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)?;
|
|
|
|
let mut to_file =
|
|
|
|
std::fs::File::create(&to_path).map_err(err_mapper)?;
|
|
|
|
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 11:08:24 -04:00
|
|
|
// returns size of from as u64 (we ignore)
|
2022-09-22 05:09:25 -04:00
|
|
|
std::fs::copy(&from_path, &to_path).map_err(err_mapper)?;
|
2021-04-05 12:40:24 -04:00
|
|
|
Ok(())
|
2020-08-28 11:08:24 -04:00
|
|
|
}
|
|
|
|
|
2022-03-14 13:44:15 -04:00
|
|
|
#[op]
|
2020-08-28 11:08:24 -04:00
|
|
|
async fn op_copy_file_async(
|
2020-09-10 09:57:45 -04:00
|
|
|
state: Rc<RefCell<OpState>>,
|
2022-09-22 05:09:25 -04:00
|
|
|
from: String,
|
|
|
|
to: String,
|
2021-04-05 12:40:24 -04:00
|
|
|
) -> Result<(), AnyError> {
|
2022-09-22 05:09:25 -04:00
|
|
|
let from = PathBuf::from(&from);
|
|
|
|
let to = PathBuf::from(&to);
|
2019-08-26 10:18:42 -04:00
|
|
|
|
2020-09-19 19:17:35 -04:00
|
|
|
{
|
2021-04-11 22:15:43 -04:00
|
|
|
let mut state = state.borrow_mut();
|
|
|
|
let permissions = state.borrow_mut::<Permissions>();
|
2022-09-27 16:36:33 -04:00
|
|
|
permissions.read.check(&from, Some("Deno.copyFile()"))?;
|
|
|
|
permissions.write.check(&to, Some("Deno.copyFile()"))?;
|
2020-09-19 19:17:35 -04:00
|
|
|
}
|
2019-08-14 11:03:02 -04:00
|
|
|
|
2020-08-28 11:08:24 -04:00
|
|
|
tokio::task::spawn_blocking(move || {
|
2020-03-02 10:19:42 -05:00
|
|
|
// On *nix, Rust reports non-existent `from` as ErrorKind::InvalidInput
|
2019-08-14 11:03:02 -04:00
|
|
|
// See https://github.com/rust-lang/rust/issues/54800
|
2020-03-02 10:19:42 -05:00
|
|
|
// Once the issue is resolved, we should remove this workaround.
|
2019-08-14 11:03:02 -04:00
|
|
|
if cfg!(unix) && !from.is_file() {
|
2021-10-11 09:21:18 -04:00
|
|
|
return Err(custom_error(
|
|
|
|
"NotFound",
|
|
|
|
format!(
|
|
|
|
"File not found, copy '{}' -> '{}'",
|
|
|
|
from.display(),
|
|
|
|
to.display()
|
|
|
|
),
|
|
|
|
));
|
2019-08-14 11:03:02 -04:00
|
|
|
}
|
|
|
|
|
2021-10-11 09:21:18 -04:00
|
|
|
let err_mapper = |err: Error| {
|
|
|
|
Error::new(
|
|
|
|
err.kind(),
|
|
|
|
format!("{}, copy '{}' -> '{}'", err, from.display(), to.display()),
|
|
|
|
)
|
|
|
|
};
|
2020-03-14 22:57:42 -04:00
|
|
|
// returns size of from as u64 (we ignore)
|
2021-10-11 09:21:18 -04:00
|
|
|
std::fs::copy(&from, &to).map_err(err_mapper)?;
|
2021-04-05 12:40:24 -04:00
|
|
|
Ok(())
|
2019-08-14 11:03:02 -04:00
|
|
|
})
|
2020-08-28 11:08:24 -04:00
|
|
|
.await
|
|
|
|
.unwrap()
|
2019-08-14 11:03:02 -04:00
|
|
|
}
|
|
|
|
|
2022-09-22 05:09:25 -04:00
|
|
|
fn to_msec(maybe_time: Result<SystemTime, io::Error>) -> (u64, bool) {
|
2020-04-27 14:09:56 -04:00
|
|
|
match maybe_time {
|
2022-09-22 05:09:25 -04:00
|
|
|
Ok(time) => (
|
|
|
|
time
|
2020-04-27 14:09:56 -04:00
|
|
|
.duration_since(UNIX_EPOCH)
|
2021-04-05 12:40:24 -04:00
|
|
|
.map(|t| t.as_millis() as u64)
|
2022-09-22 05:09:25 -04:00
|
|
|
.unwrap_or_else(|err| err.duration().as_millis() as u64),
|
|
|
|
true,
|
|
|
|
),
|
|
|
|
Err(_) => (0, false),
|
2020-04-27 14:09:56 -04:00
|
|
|
}
|
2019-08-14 11:03:02 -04:00
|
|
|
}
|
|
|
|
|
2022-09-22 05:09:25 -04: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 12:40:24 -04:00
|
|
|
}
|
|
|
|
|
2020-01-16 09:46:32 -05:00
|
|
|
#[inline(always)]
|
2021-04-05 12:40:24 -04:00
|
|
|
fn get_stat(metadata: std::fs::Metadata) -> FsStat {
|
2020-01-16 09:46:32 -05:00
|
|
|
// Unix stat member (number types only). 0 if not on unix.
|
|
|
|
macro_rules! usm {
|
2020-09-05 20:34:02 -04:00
|
|
|
($member:ident) => {{
|
2020-01-16 09:46:32 -05:00
|
|
|
#[cfg(unix)]
|
|
|
|
{
|
|
|
|
metadata.$member()
|
|
|
|
}
|
|
|
|
#[cfg(not(unix))]
|
|
|
|
{
|
|
|
|
0
|
|
|
|
}
|
|
|
|
}};
|
|
|
|
}
|
|
|
|
|
2020-03-20 09:46:26 -04:00
|
|
|
#[cfg(unix)]
|
|
|
|
use std::os::unix::fs::MetadataExt;
|
2022-09-22 05:09:25 -04: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 12:40:24 -04: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 14:09:56 -04:00
|
|
|
// In milliseconds, like JavaScript. Available on both Unix or Windows.
|
2022-09-22 05:09:25 -04:00
|
|
|
mtime_set,
|
|
|
|
mtime,
|
|
|
|
atime_set,
|
|
|
|
atime,
|
|
|
|
birthtime_set,
|
|
|
|
birthtime,
|
2020-01-16 09:46:32 -05:00
|
|
|
// Following are only valid under Unix.
|
2021-04-05 12:40:24 -04: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 11:03:02 -04:00
|
|
|
}
|
|
|
|
|
2019-08-26 10:18:42 -04:00
|
|
|
#[derive(Deserialize)]
|
|
|
|
#[serde(rename_all = "camelCase")]
|
2021-03-18 14:42:01 -04:00
|
|
|
pub struct StatArgs {
|
2020-03-06 11:29:23 -05:00
|
|
|
path: String,
|
2019-08-26 10:18:42 -04:00
|
|
|
lstat: bool,
|
|
|
|
}
|
|
|
|
|
2022-03-14 13:44:15 -04:00
|
|
|
#[op]
|
2020-08-28 11:08:24 -04:00
|
|
|
fn op_stat_sync(
|
2020-09-10 09:57:45 -04:00
|
|
|
state: &mut OpState,
|
2022-12-15 10:26:10 -05:00
|
|
|
path: String,
|
2022-09-22 05:09:25 -04:00
|
|
|
lstat: bool,
|
|
|
|
out_buf: &mut [u32],
|
|
|
|
) -> Result<(), AnyError> {
|
2022-12-15 10:26:10 -05:00
|
|
|
let path = PathBuf::from(&path);
|
2022-09-27 16:36:33 -04:00
|
|
|
state
|
|
|
|
.borrow_mut::<Permissions>()
|
|
|
|
.read
|
|
|
|
.check(&path, Some("Deno.statSync()"))?;
|
2021-10-11 09:21:18 -04:00
|
|
|
let err_mapper = |err: Error| {
|
|
|
|
Error::new(err.kind(), format!("{}, stat '{}'", err, path.display()))
|
|
|
|
};
|
2020-08-28 11:08:24 -04:00
|
|
|
let metadata = if lstat {
|
2021-10-11 09:21:18 -04:00
|
|
|
std::fs::symlink_metadata(&path).map_err(err_mapper)?
|
2020-08-28 11:08:24 -04:00
|
|
|
} else {
|
2021-10-11 09:21:18 -04:00
|
|
|
std::fs::metadata(&path).map_err(err_mapper)?
|
2020-08-28 11:08:24 -04:00
|
|
|
};
|
2022-09-22 05:09:25 -04:00
|
|
|
|
|
|
|
let stat = get_stat(metadata);
|
|
|
|
stat.write(out_buf);
|
|
|
|
|
|
|
|
Ok(())
|
2020-08-28 11:08:24 -04:00
|
|
|
}
|
|
|
|
|
2022-03-14 13:44:15 -04:00
|
|
|
#[op]
|
2020-08-28 11:08:24 -04:00
|
|
|
async fn op_stat_async(
|
2020-09-10 09:57:45 -04:00
|
|
|
state: Rc<RefCell<OpState>>,
|
2021-03-18 14:42:01 -04:00
|
|
|
args: StatArgs,
|
2021-04-05 12:40:24 -04:00
|
|
|
) -> Result<FsStat, AnyError> {
|
2020-05-29 11:27:43 -04:00
|
|
|
let path = PathBuf::from(&args.path);
|
2019-08-26 10:18:42 -04:00
|
|
|
let lstat = args.lstat;
|
2019-08-14 11:03:02 -04:00
|
|
|
|
2020-09-19 19:17:35 -04:00
|
|
|
{
|
2021-04-11 22:15:43 -04:00
|
|
|
let mut state = state.borrow_mut();
|
2022-09-27 16:36:33 -04:00
|
|
|
state
|
|
|
|
.borrow_mut::<Permissions>()
|
|
|
|
.read
|
|
|
|
.check(&path, Some("Deno.stat()"))?;
|
2020-09-19 19:17:35 -04:00
|
|
|
}
|
2019-08-14 11:03:02 -04:00
|
|
|
|
2020-08-28 11:08:24 -04:00
|
|
|
tokio::task::spawn_blocking(move || {
|
|
|
|
debug!("op_stat_async {} {}", path.display(), lstat);
|
2021-10-11 09:21:18 -04:00
|
|
|
let err_mapper = |err: Error| {
|
|
|
|
Error::new(err.kind(), format!("{}, stat '{}'", err, path.display()))
|
|
|
|
};
|
2019-08-14 11:03:02 -04:00
|
|
|
let metadata = if lstat {
|
2021-10-11 09:21:18 -04:00
|
|
|
std::fs::symlink_metadata(&path).map_err(err_mapper)?
|
2019-08-14 11:03:02 -04:00
|
|
|
} else {
|
2021-10-11 09:21:18 -04:00
|
|
|
std::fs::metadata(&path).map_err(err_mapper)?
|
2019-08-14 11:03:02 -04:00
|
|
|
};
|
2021-04-05 12:40:24 -04:00
|
|
|
Ok(get_stat(metadata))
|
2019-08-14 11:03:02 -04:00
|
|
|
})
|
2020-08-28 11:08:24 -04:00
|
|
|
.await
|
|
|
|
.unwrap()
|
2019-08-14 11:03:02 -04:00
|
|
|
}
|
|
|
|
|
2022-03-14 13:44:15 -04:00
|
|
|
#[op]
|
2020-08-28 11:08:24 -04:00
|
|
|
fn op_realpath_sync(
|
2020-09-10 09:57:45 -04:00
|
|
|
state: &mut OpState,
|
2021-04-05 12:40:24 -04:00
|
|
|
path: String,
|
|
|
|
) -> Result<String, AnyError> {
|
|
|
|
let path = PathBuf::from(&path);
|
2020-01-20 09:45:44 -05:00
|
|
|
|
2021-04-11 22:15:43 -04:00
|
|
|
let permissions = state.borrow_mut::<Permissions>();
|
2022-09-27 16:36:33 -04:00
|
|
|
permissions.read.check(&path, Some("Deno.realPathSync()"))?;
|
2020-05-29 11:27:43 -04:00
|
|
|
if path.is_relative() {
|
2022-09-27 16:36:33 -04:00
|
|
|
permissions.read.check_blind(
|
|
|
|
¤t_dir()?,
|
|
|
|
"CWD",
|
|
|
|
"Deno.realPathSync()",
|
|
|
|
)?;
|
2020-05-29 11:27:43 -04:00
|
|
|
}
|
2020-01-20 09:45:44 -05:00
|
|
|
|
2020-08-28 11:08:24 -04:00
|
|
|
debug!("op_realpath_sync {}", path.display());
|
|
|
|
// corresponds to the realpath on Unix and
|
|
|
|
// CreateFile and GetFinalPathNameByHandle on Windows
|
2020-10-29 22:19:03 -04:00
|
|
|
let realpath = canonicalize_path(&path)?;
|
|
|
|
let realpath_str = into_string(realpath.into_os_string())?;
|
2021-04-05 12:40:24 -04:00
|
|
|
Ok(realpath_str)
|
2020-08-28 11:08:24 -04:00
|
|
|
}
|
|
|
|
|
2022-03-14 13:44:15 -04:00
|
|
|
#[op]
|
2020-08-28 11:08:24 -04:00
|
|
|
async fn op_realpath_async(
|
2020-09-10 09:57:45 -04:00
|
|
|
state: Rc<RefCell<OpState>>,
|
2021-04-05 12:40:24 -04:00
|
|
|
path: String,
|
|
|
|
) -> Result<String, AnyError> {
|
|
|
|
let path = PathBuf::from(&path);
|
2020-08-28 11:08:24 -04:00
|
|
|
|
2020-09-19 19:17:35 -04:00
|
|
|
{
|
2021-04-11 22:15:43 -04:00
|
|
|
let mut state = state.borrow_mut();
|
|
|
|
let permissions = state.borrow_mut::<Permissions>();
|
2022-09-27 16:36:33 -04:00
|
|
|
permissions.read.check(&path, Some("Deno.realPath()"))?;
|
2020-09-19 19:17:35 -04:00
|
|
|
if path.is_relative() {
|
2022-09-27 16:36:33 -04:00
|
|
|
permissions.read.check_blind(
|
|
|
|
¤t_dir()?,
|
|
|
|
"CWD",
|
|
|
|
"Deno.realPath()",
|
|
|
|
)?;
|
2020-09-19 19:17:35 -04:00
|
|
|
}
|
2020-08-28 11:08:24 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
tokio::task::spawn_blocking(move || {
|
|
|
|
debug!("op_realpath_async {}", path.display());
|
2019-11-26 03:40:57 -05:00
|
|
|
// corresponds to the realpath on Unix and
|
|
|
|
// CreateFile and GetFinalPathNameByHandle on Windows
|
2020-10-29 22:19:03 -04:00
|
|
|
let realpath = canonicalize_path(&path)?;
|
|
|
|
let realpath_str = into_string(realpath.into_os_string())?;
|
2021-04-05 12:40:24 -04:00
|
|
|
Ok(realpath_str)
|
2019-11-26 03:40:57 -05:00
|
|
|
})
|
2020-08-28 11:08:24 -04:00
|
|
|
.await
|
|
|
|
.unwrap()
|
2019-11-26 03:40:57 -05:00
|
|
|
}
|
|
|
|
|
2021-04-05 12:40:24 -04:00
|
|
|
#[derive(Serialize)]
|
2019-08-26 10:18:42 -04:00
|
|
|
#[serde(rename_all = "camelCase")]
|
2021-04-05 12:40:24 -04:00
|
|
|
pub struct DirEntry {
|
|
|
|
name: String,
|
|
|
|
is_file: bool,
|
|
|
|
is_directory: bool,
|
|
|
|
is_symlink: bool,
|
2019-08-26 10:18:42 -04:00
|
|
|
}
|
|
|
|
|
2022-03-14 13:44:15 -04:00
|
|
|
#[op]
|
2020-08-28 11:08:24 -04:00
|
|
|
fn op_read_dir_sync(
|
2020-09-10 09:57:45 -04:00
|
|
|
state: &mut OpState,
|
2021-04-05 12:40:24 -04:00
|
|
|
path: String,
|
|
|
|
) -> Result<Vec<DirEntry>, AnyError> {
|
|
|
|
let path = PathBuf::from(&path);
|
2020-08-28 11:08:24 -04:00
|
|
|
|
2022-09-27 16:36:33 -04:00
|
|
|
state
|
|
|
|
.borrow_mut::<Permissions>()
|
|
|
|
.read
|
|
|
|
.check(&path, Some("Deno.readDirSync()"))?;
|
2020-08-28 11:08:24 -04:00
|
|
|
|
|
|
|
debug!("op_read_dir_sync {}", path.display());
|
2021-10-11 09:21:18 -04:00
|
|
|
let err_mapper = |err: Error| {
|
|
|
|
Error::new(err.kind(), format!("{}, readdir '{}'", err, path.display()))
|
|
|
|
};
|
|
|
|
let entries: Vec<_> = std::fs::read_dir(&path)
|
|
|
|
.map_err(err_mapper)?
|
2020-08-28 11:08:24 -04: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 12:40:24 -04:00
|
|
|
Some(DirEntry {
|
|
|
|
name,
|
|
|
|
is_file: entry
|
|
|
|
.file_type()
|
|
|
|
.map_or(false, |file_type| file_type.is_file()),
|
|
|
|
is_directory: entry
|
|
|
|
.file_type()
|
|
|
|
.map_or(false, |file_type| file_type.is_dir()),
|
|
|
|
is_symlink: entry
|
|
|
|
.file_type()
|
|
|
|
.map_or(false, |file_type| file_type.is_symlink()),
|
|
|
|
})
|
2020-08-28 11:08:24 -04:00
|
|
|
} else {
|
|
|
|
None
|
|
|
|
}
|
|
|
|
})
|
2021-04-05 12:40:24 -04:00
|
|
|
.collect();
|
2020-08-28 11:08:24 -04:00
|
|
|
|
2021-04-05 12:40:24 -04:00
|
|
|
Ok(entries)
|
2020-08-28 11:08:24 -04:00
|
|
|
}
|
|
|
|
|
2022-03-14 13:44:15 -04:00
|
|
|
#[op]
|
2020-08-28 11:08:24 -04:00
|
|
|
async fn op_read_dir_async(
|
2020-09-10 09:57:45 -04:00
|
|
|
state: Rc<RefCell<OpState>>,
|
2021-04-05 12:40:24 -04:00
|
|
|
path: String,
|
|
|
|
) -> Result<Vec<DirEntry>, AnyError> {
|
|
|
|
let path = PathBuf::from(&path);
|
2020-09-19 19:17:35 -04:00
|
|
|
{
|
2021-04-11 22:15:43 -04:00
|
|
|
let mut state = state.borrow_mut();
|
2022-09-27 16:36:33 -04:00
|
|
|
state
|
|
|
|
.borrow_mut::<Permissions>()
|
|
|
|
.read
|
|
|
|
.check(&path, Some("Deno.readDir()"))?;
|
2020-09-19 19:17:35 -04:00
|
|
|
}
|
2020-08-28 11:08:24 -04:00
|
|
|
tokio::task::spawn_blocking(move || {
|
|
|
|
debug!("op_read_dir_async {}", path.display());
|
2021-10-11 09:21:18 -04:00
|
|
|
let err_mapper = |err: Error| {
|
|
|
|
Error::new(err.kind(), format!("{}, readdir '{}'", err, path.display()))
|
|
|
|
};
|
|
|
|
let entries: Vec<_> = std::fs::read_dir(&path)
|
|
|
|
.map_err(err_mapper)?
|
2020-02-15 10:37:05 -05:00
|
|
|
.filter_map(|entry| {
|
2019-08-14 11:03:02 -04:00
|
|
|
let entry = entry.unwrap();
|
2020-02-15 10:37:05 -05:00
|
|
|
// Not all filenames can be encoded as UTF-8. Skip those for now.
|
2020-04-29 16:00:31 -04:00
|
|
|
if let Ok(name) = into_string(entry.file_name()) {
|
2021-04-05 12:40:24 -04:00
|
|
|
Some(DirEntry {
|
|
|
|
name,
|
|
|
|
is_file: entry
|
|
|
|
.file_type()
|
|
|
|
.map_or(false, |file_type| file_type.is_file()),
|
|
|
|
is_directory: entry
|
|
|
|
.file_type()
|
|
|
|
.map_or(false, |file_type| file_type.is_dir()),
|
|
|
|
is_symlink: entry
|
|
|
|
.file_type()
|
|
|
|
.map_or(false, |file_type| file_type.is_symlink()),
|
|
|
|
})
|
2020-02-15 10:37:05 -05:00
|
|
|
} else {
|
|
|
|
None
|
|
|
|
}
|
2019-08-14 11:03:02 -04:00
|
|
|
})
|
2021-04-05 12:40:24 -04:00
|
|
|
.collect();
|
2019-08-14 11:03:02 -04:00
|
|
|
|
2021-04-05 12:40:24 -04:00
|
|
|
Ok(entries)
|
2019-08-14 11:03:02 -04:00
|
|
|
})
|
2020-08-28 11:08:24 -04:00
|
|
|
.await
|
2021-04-05 12:40:24 -04:00
|
|
|
.unwrap()
|
2019-08-14 11:03:02 -04:00
|
|
|
}
|
|
|
|
|
2022-03-14 13:44:15 -04:00
|
|
|
#[op]
|
2020-08-28 11:08:24 -04:00
|
|
|
fn op_rename_sync(
|
2020-09-10 09:57:45 -04:00
|
|
|
state: &mut OpState,
|
2022-12-15 10:26:10 -05:00
|
|
|
oldpath: String,
|
|
|
|
newpath: String,
|
2021-04-05 12:40:24 -04:00
|
|
|
) -> Result<(), AnyError> {
|
2022-12-15 10:26:10 -05:00
|
|
|
let oldpath = PathBuf::from(&oldpath);
|
|
|
|
let newpath = PathBuf::from(&newpath);
|
2020-08-28 11:08:24 -04:00
|
|
|
|
2021-04-11 22:15:43 -04:00
|
|
|
let permissions = state.borrow_mut::<Permissions>();
|
2022-09-27 16:36:33 -04:00
|
|
|
permissions
|
|
|
|
.read
|
|
|
|
.check(&oldpath, Some("Deno.renameSync()"))?;
|
|
|
|
permissions
|
|
|
|
.write
|
|
|
|
.check(&oldpath, Some("Deno.renameSync()"))?;
|
|
|
|
permissions
|
|
|
|
.write
|
|
|
|
.check(&newpath, Some("Deno.renameSync()"))?;
|
2022-09-22 05:09:25 -04:00
|
|
|
|
2021-10-11 09:21:18 -04:00
|
|
|
let err_mapper = |err: Error| {
|
|
|
|
Error::new(
|
|
|
|
err.kind(),
|
|
|
|
format!(
|
|
|
|
"{}, rename '{}' -> '{}'",
|
|
|
|
err,
|
|
|
|
oldpath.display(),
|
|
|
|
newpath.display()
|
|
|
|
),
|
|
|
|
)
|
|
|
|
};
|
|
|
|
std::fs::rename(&oldpath, &newpath).map_err(err_mapper)?;
|
2021-04-05 12:40:24 -04:00
|
|
|
Ok(())
|
2020-08-28 11:08:24 -04:00
|
|
|
}
|
|
|
|
|
2022-03-14 13:44:15 -04:00
|
|
|
#[op]
|
2020-08-28 11:08:24 -04:00
|
|
|
async fn op_rename_async(
|
2020-09-10 09:57:45 -04:00
|
|
|
state: Rc<RefCell<OpState>>,
|
2022-09-22 05:09:25 -04:00
|
|
|
oldpath: String,
|
|
|
|
newpath: String,
|
2021-04-05 12:40:24 -04:00
|
|
|
) -> Result<(), AnyError> {
|
2022-09-22 05:09:25 -04:00
|
|
|
let oldpath = PathBuf::from(&oldpath);
|
|
|
|
let newpath = PathBuf::from(&newpath);
|
2020-09-10 09:57:45 -04:00
|
|
|
{
|
2021-04-11 22:15:43 -04:00
|
|
|
let mut state = state.borrow_mut();
|
|
|
|
let permissions = state.borrow_mut::<Permissions>();
|
2022-09-27 16:36:33 -04:00
|
|
|
permissions.read.check(&oldpath, Some("Deno.rename()"))?;
|
|
|
|
permissions.write.check(&oldpath, Some("Deno.rename()"))?;
|
|
|
|
permissions.write.check(&newpath, Some("Deno.rename()"))?;
|
2020-09-10 09:57:45 -04:00
|
|
|
}
|
2020-08-28 11:08:24 -04:00
|
|
|
tokio::task::spawn_blocking(move || {
|
2021-10-11 09:21:18 -04:00
|
|
|
let err_mapper = |err: Error| {
|
|
|
|
Error::new(
|
|
|
|
err.kind(),
|
|
|
|
format!(
|
|
|
|
"{}, rename '{}' -> '{}'",
|
|
|
|
err,
|
|
|
|
oldpath.display(),
|
|
|
|
newpath.display()
|
|
|
|
),
|
|
|
|
)
|
|
|
|
};
|
|
|
|
std::fs::rename(&oldpath, &newpath).map_err(err_mapper)?;
|
2021-04-05 12:40:24 -04:00
|
|
|
Ok(())
|
2019-08-14 11:03:02 -04:00
|
|
|
})
|
2020-08-28 11:08:24 -04:00
|
|
|
.await
|
|
|
|
.unwrap()
|
2019-08-14 11:03:02 -04:00
|
|
|
}
|
|
|
|
|
2022-09-22 05:09:25 -04:00
|
|
|
#[op]
|
|
|
|
fn op_link_sync(
|
|
|
|
state: &mut OpState,
|
2022-12-15 10:26:10 -05:00
|
|
|
oldpath: String,
|
|
|
|
newpath: String,
|
2022-09-22 05:09:25 -04:00
|
|
|
) -> Result<(), AnyError> {
|
2022-12-15 10:26:10 -05:00
|
|
|
let oldpath = PathBuf::from(&oldpath);
|
|
|
|
let newpath = PathBuf::from(&newpath);
|
2020-08-28 11:08:24 -04:00
|
|
|
|
2021-04-11 22:15:43 -04:00
|
|
|
let permissions = state.borrow_mut::<Permissions>();
|
2022-09-27 16:36:33 -04:00
|
|
|
permissions.read.check(&oldpath, Some("Deno.linkSync()"))?;
|
|
|
|
permissions.write.check(&oldpath, Some("Deno.linkSync()"))?;
|
|
|
|
permissions.read.check(&newpath, Some("Deno.linkSync()"))?;
|
|
|
|
permissions.write.check(&newpath, Some("Deno.linkSync()"))?;
|
2020-08-28 11:08:24 -04:00
|
|
|
|
2021-10-11 09:21:18 -04:00
|
|
|
let err_mapper = |err: Error| {
|
|
|
|
Error::new(
|
|
|
|
err.kind(),
|
|
|
|
format!(
|
|
|
|
"{}, link '{}' -> '{}'",
|
|
|
|
err,
|
|
|
|
oldpath.display(),
|
|
|
|
newpath.display()
|
|
|
|
),
|
|
|
|
)
|
|
|
|
};
|
|
|
|
std::fs::hard_link(&oldpath, &newpath).map_err(err_mapper)?;
|
2021-04-05 12:40:24 -04:00
|
|
|
Ok(())
|
2020-08-28 11:08:24 -04:00
|
|
|
}
|
|
|
|
|
2022-03-14 13:44:15 -04:00
|
|
|
#[op]
|
2020-08-28 11:08:24 -04:00
|
|
|
async fn op_link_async(
|
2020-09-10 09:57:45 -04:00
|
|
|
state: Rc<RefCell<OpState>>,
|
2022-09-22 05:09:25 -04:00
|
|
|
oldpath: String,
|
|
|
|
newpath: String,
|
2021-04-05 12:40:24 -04:00
|
|
|
) -> Result<(), AnyError> {
|
2022-09-22 05:09:25 -04:00
|
|
|
let oldpath = PathBuf::from(&oldpath);
|
|
|
|
let newpath = PathBuf::from(&newpath);
|
2019-08-14 11:03:02 -04:00
|
|
|
|
2020-09-19 19:17:35 -04:00
|
|
|
{
|
2021-04-11 22:15:43 -04:00
|
|
|
let mut state = state.borrow_mut();
|
|
|
|
let permissions = state.borrow_mut::<Permissions>();
|
2022-09-27 16:36:33 -04:00
|
|
|
permissions.read.check(&oldpath, Some("Deno.link()"))?;
|
|
|
|
permissions.write.check(&oldpath, Some("Deno.link()"))?;
|
|
|
|
permissions.read.check(&newpath, Some("Deno.link()"))?;
|
|
|
|
permissions.write.check(&newpath, Some("Deno.link()"))?;
|
2020-09-19 19:17:35 -04:00
|
|
|
}
|
2019-08-14 11:03:02 -04:00
|
|
|
|
2020-08-28 11:08:24 -04:00
|
|
|
tokio::task::spawn_blocking(move || {
|
2021-10-11 09:21:18 -04:00
|
|
|
let err_mapper = |err: Error| {
|
|
|
|
Error::new(
|
|
|
|
err.kind(),
|
|
|
|
format!(
|
|
|
|
"{}, link '{}' -> '{}'",
|
|
|
|
err,
|
|
|
|
oldpath.display(),
|
|
|
|
newpath.display()
|
|
|
|
),
|
|
|
|
)
|
|
|
|
};
|
|
|
|
std::fs::hard_link(&oldpath, &newpath).map_err(err_mapper)?;
|
2021-04-05 12:40:24 -04:00
|
|
|
Ok(())
|
2019-08-14 11:03:02 -04:00
|
|
|
})
|
2020-08-28 11:08:24 -04:00
|
|
|
.await
|
|
|
|
.unwrap()
|
2019-08-14 11:03:02 -04:00
|
|
|
}
|
|
|
|
|
2022-03-14 13:44:15 -04:00
|
|
|
#[op]
|
2020-08-28 11:08:24 -04:00
|
|
|
fn op_symlink_sync(
|
2020-09-10 09:57:45 -04:00
|
|
|
state: &mut OpState,
|
2022-12-15 10:26:10 -05:00
|
|
|
oldpath: String,
|
|
|
|
newpath: String,
|
2022-09-22 05:09:25 -04:00
|
|
|
_type: Option<String>,
|
2021-04-05 12:40:24 -04:00
|
|
|
) -> Result<(), AnyError> {
|
2022-12-15 10:26:10 -05:00
|
|
|
let oldpath = PathBuf::from(&oldpath);
|
|
|
|
let newpath = PathBuf::from(&newpath);
|
2020-08-28 11:08:24 -04:00
|
|
|
|
2022-09-27 16:36:33 -04:00
|
|
|
state
|
|
|
|
.borrow_mut::<Permissions>()
|
|
|
|
.write
|
|
|
|
.check_all(Some("Deno.symlinkSync()"))?;
|
|
|
|
state
|
|
|
|
.borrow_mut::<Permissions>()
|
|
|
|
.read
|
|
|
|
.check_all(Some("Deno.symlinkSync()"))?;
|
2020-08-28 11:08:24 -04:00
|
|
|
|
2021-10-11 09:21:18 -04:00
|
|
|
let err_mapper = |err: Error| {
|
|
|
|
Error::new(
|
|
|
|
err.kind(),
|
|
|
|
format!(
|
|
|
|
"{}, symlink '{}' -> '{}'",
|
|
|
|
err,
|
|
|
|
oldpath.display(),
|
|
|
|
newpath.display()
|
|
|
|
),
|
|
|
|
)
|
|
|
|
};
|
2020-08-28 11:08:24 -04:00
|
|
|
#[cfg(unix)]
|
|
|
|
{
|
|
|
|
use std::os::unix::fs::symlink;
|
2021-10-11 09:21:18 -04:00
|
|
|
symlink(&oldpath, &newpath).map_err(err_mapper)?;
|
2021-04-05 12:40:24 -04:00
|
|
|
Ok(())
|
2020-08-28 11:08:24 -04:00
|
|
|
}
|
|
|
|
#[cfg(not(unix))]
|
|
|
|
{
|
|
|
|
use std::os::windows::fs::{symlink_dir, symlink_file};
|
|
|
|
|
2022-09-22 05:09:25 -04:00
|
|
|
match _type {
|
|
|
|
Some(ty) => match ty.as_ref() {
|
2021-10-11 09:21:18 -04:00
|
|
|
"file" => symlink_file(&oldpath, &newpath).map_err(err_mapper)?,
|
|
|
|
"dir" => symlink_dir(&oldpath, &newpath).map_err(err_mapper)?,
|
2020-09-14 12:48:57 -04:00
|
|
|
_ => return Err(type_error("unsupported type")),
|
2020-08-28 11:08:24 -04:00
|
|
|
},
|
|
|
|
None => {
|
|
|
|
let old_meta = std::fs::metadata(&oldpath);
|
|
|
|
match old_meta {
|
|
|
|
Ok(metadata) => {
|
|
|
|
if metadata.is_file() {
|
2021-10-11 09:21:18 -04:00
|
|
|
symlink_file(&oldpath, &newpath).map_err(err_mapper)?
|
2020-08-28 11:08:24 -04:00
|
|
|
} else if metadata.is_dir() {
|
2021-10-11 09:21:18 -04:00
|
|
|
symlink_dir(&oldpath, &newpath).map_err(err_mapper)?
|
2020-08-28 11:08:24 -04:00
|
|
|
}
|
|
|
|
}
|
2020-09-14 12:48:57 -04:00
|
|
|
Err(_) => return Err(type_error("you must pass a `options` argument for non-existent target path in windows".to_string())),
|
2020-08-28 11:08:24 -04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
};
|
2021-04-05 12:40:24 -04:00
|
|
|
Ok(())
|
2020-08-28 11:08:24 -04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-03-14 13:44:15 -04:00
|
|
|
#[op]
|
2020-08-28 11:08:24 -04:00
|
|
|
async fn op_symlink_async(
|
2020-09-10 09:57:45 -04:00
|
|
|
state: Rc<RefCell<OpState>>,
|
2022-09-22 05:09:25 -04:00
|
|
|
oldpath: String,
|
|
|
|
newpath: String,
|
|
|
|
_type: Option<String>,
|
2021-04-05 12:40:24 -04:00
|
|
|
) -> Result<(), AnyError> {
|
2022-09-22 05:09:25 -04:00
|
|
|
let oldpath = PathBuf::from(&oldpath);
|
|
|
|
let newpath = PathBuf::from(&newpath);
|
2020-03-20 09:46:26 -04:00
|
|
|
|
2020-09-19 19:17:35 -04:00
|
|
|
{
|
2021-04-11 22:15:43 -04:00
|
|
|
let mut state = state.borrow_mut();
|
2022-09-27 16:36:33 -04:00
|
|
|
state
|
|
|
|
.borrow_mut::<Permissions>()
|
|
|
|
.write
|
|
|
|
.check_all(Some("Deno.symlink()"))?;
|
|
|
|
state
|
|
|
|
.borrow_mut::<Permissions>()
|
|
|
|
.read
|
|
|
|
.check_all(Some("Deno.symlink()"))?;
|
2020-09-19 19:17:35 -04:00
|
|
|
}
|
2019-08-26 10:18:42 -04:00
|
|
|
|
2020-08-28 11:08:24 -04:00
|
|
|
tokio::task::spawn_blocking(move || {
|
2021-10-11 09:21:18 -04:00
|
|
|
let err_mapper = |err: Error| {
|
|
|
|
Error::new(
|
|
|
|
err.kind(),
|
|
|
|
format!(
|
|
|
|
"{}, symlink '{}' -> '{}'",
|
|
|
|
err,
|
|
|
|
oldpath.display(),
|
|
|
|
newpath.display()
|
|
|
|
),
|
|
|
|
)
|
|
|
|
};
|
2020-03-02 10:19:42 -05:00
|
|
|
#[cfg(unix)]
|
2020-03-20 09:46:26 -04:00
|
|
|
{
|
|
|
|
use std::os::unix::fs::symlink;
|
2021-10-11 09:21:18 -04:00
|
|
|
symlink(&oldpath, &newpath).map_err(err_mapper)?;
|
2021-04-05 12:40:24 -04:00
|
|
|
Ok(())
|
2020-03-20 09:46:26 -04:00
|
|
|
}
|
|
|
|
#[cfg(not(unix))]
|
|
|
|
{
|
2020-05-18 18:46:02 -04:00
|
|
|
use std::os::windows::fs::{symlink_dir, symlink_file};
|
|
|
|
|
2022-09-22 05:09:25 -04:00
|
|
|
match _type {
|
|
|
|
Some(ty) => match ty.as_ref() {
|
2021-10-11 09:21:18 -04:00
|
|
|
"file" => symlink_file(&oldpath, &newpath).map_err(err_mapper)?,
|
|
|
|
"dir" => symlink_dir(&oldpath, &newpath).map_err(err_mapper)?,
|
2020-09-14 12:48:57 -04:00
|
|
|
_ => return Err(type_error("unsupported type")),
|
2020-05-18 18:46:02 -04:00
|
|
|
},
|
|
|
|
None => {
|
|
|
|
let old_meta = std::fs::metadata(&oldpath);
|
|
|
|
match old_meta {
|
|
|
|
Ok(metadata) => {
|
|
|
|
if metadata.is_file() {
|
2021-10-11 09:21:18 -04:00
|
|
|
symlink_file(&oldpath, &newpath).map_err(err_mapper)?
|
2020-05-18 18:46:02 -04:00
|
|
|
} else if metadata.is_dir() {
|
2021-10-11 09:21:18 -04:00
|
|
|
symlink_dir(&oldpath, &newpath).map_err(err_mapper)?
|
2020-05-18 18:46:02 -04:00
|
|
|
}
|
|
|
|
}
|
2020-09-14 12:48:57 -04:00
|
|
|
Err(_) => return Err(type_error("you must pass a `options` argument for non-existent target path in windows".to_string())),
|
2020-05-18 18:46:02 -04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
};
|
2021-04-05 12:40:24 -04:00
|
|
|
Ok(())
|
2020-03-20 09:46:26 -04:00
|
|
|
}
|
2019-08-14 11:03:02 -04:00
|
|
|
})
|
2020-08-28 11:08:24 -04:00
|
|
|
.await
|
|
|
|
.unwrap()
|
2019-08-14 11:03:02 -04:00
|
|
|
}
|
|
|
|
|
2022-03-14 13:44:15 -04:00
|
|
|
#[op]
|
2020-08-28 11:08:24 -04:00
|
|
|
fn op_read_link_sync(
|
2020-09-10 09:57:45 -04:00
|
|
|
state: &mut OpState,
|
2021-04-05 12:40:24 -04:00
|
|
|
path: String,
|
|
|
|
) -> Result<String, AnyError> {
|
|
|
|
let path = PathBuf::from(&path);
|
2019-08-26 10:18:42 -04:00
|
|
|
|
2022-09-27 16:36:33 -04:00
|
|
|
state
|
|
|
|
.borrow_mut::<Permissions>()
|
|
|
|
.read
|
|
|
|
.check(&path, Some("Deno.readLink()"))?;
|
2019-08-14 11:03:02 -04:00
|
|
|
|
2020-08-28 11:08:24 -04:00
|
|
|
debug!("op_read_link_value {}", path.display());
|
2021-10-11 09:21:18 -04:00
|
|
|
let err_mapper = |err: Error| {
|
|
|
|
Error::new(
|
|
|
|
err.kind(),
|
|
|
|
format!("{}, readlink '{}'", err, path.display()),
|
|
|
|
)
|
|
|
|
};
|
|
|
|
let target = std::fs::read_link(&path)
|
|
|
|
.map_err(err_mapper)?
|
|
|
|
.into_os_string();
|
2020-08-28 11:08:24 -04:00
|
|
|
let targetstr = into_string(target)?;
|
2021-04-05 12:40:24 -04:00
|
|
|
Ok(targetstr)
|
2020-08-28 11:08:24 -04:00
|
|
|
}
|
|
|
|
|
2022-03-14 13:44:15 -04:00
|
|
|
#[op]
|
2020-08-28 11:08:24 -04:00
|
|
|
async fn op_read_link_async(
|
2020-09-10 09:57:45 -04:00
|
|
|
state: Rc<RefCell<OpState>>,
|
2021-04-05 12:40:24 -04:00
|
|
|
path: String,
|
|
|
|
) -> Result<String, AnyError> {
|
|
|
|
let path = PathBuf::from(&path);
|
2020-09-19 19:17:35 -04:00
|
|
|
{
|
2021-04-11 22:15:43 -04:00
|
|
|
let mut state = state.borrow_mut();
|
2022-09-27 16:36:33 -04:00
|
|
|
state
|
|
|
|
.borrow_mut::<Permissions>()
|
|
|
|
.read
|
|
|
|
.check(&path, Some("Deno.readLink()"))?;
|
2020-09-19 19:17:35 -04:00
|
|
|
}
|
2020-08-28 11:08:24 -04:00
|
|
|
tokio::task::spawn_blocking(move || {
|
|
|
|
debug!("op_read_link_async {}", path.display());
|
2021-10-11 09:21:18 -04:00
|
|
|
let err_mapper = |err: Error| {
|
|
|
|
Error::new(
|
|
|
|
err.kind(),
|
|
|
|
format!("{}, readlink '{}'", err, path.display()),
|
|
|
|
)
|
|
|
|
};
|
|
|
|
let target = std::fs::read_link(&path)
|
|
|
|
.map_err(err_mapper)?
|
|
|
|
.into_os_string();
|
2020-04-03 13:47:57 -04:00
|
|
|
let targetstr = into_string(target)?;
|
2021-04-05 12:40:24 -04:00
|
|
|
Ok(targetstr)
|
2019-08-14 11:03:02 -04:00
|
|
|
})
|
2020-08-28 11:08:24 -04:00
|
|
|
.await
|
|
|
|
.unwrap()
|
2019-08-14 11:03:02 -04:00
|
|
|
}
|
|
|
|
|
2022-03-14 13:44:15 -04:00
|
|
|
#[op]
|
2020-08-28 11:08:24 -04:00
|
|
|
fn op_ftruncate_sync(
|
2020-09-10 09:57:45 -04:00
|
|
|
state: &mut OpState,
|
2022-09-22 05:09:25 -04:00
|
|
|
rid: u32,
|
|
|
|
len: i32,
|
2021-04-05 12:40:24 -04:00
|
|
|
) -> Result<(), AnyError> {
|
2022-09-22 05:09:25 -04:00
|
|
|
let len = len as u64;
|
2022-05-11 12:48:38 -04:00
|
|
|
StdFileResource::with_file(state, rid, |std_file| {
|
|
|
|
std_file.set_len(len).map_err(AnyError::from)
|
2020-08-28 11:08:24 -04:00
|
|
|
})?;
|
2021-04-05 12:40:24 -04:00
|
|
|
Ok(())
|
2020-08-28 11:08:24 -04:00
|
|
|
}
|
2020-06-20 09:46:10 -04:00
|
|
|
|
2022-03-14 13:44:15 -04:00
|
|
|
#[op]
|
2020-08-28 11:08:24 -04:00
|
|
|
async fn op_ftruncate_async(
|
2020-09-10 09:57:45 -04:00
|
|
|
state: Rc<RefCell<OpState>>,
|
2022-09-22 05:09:25 -04:00
|
|
|
rid: ResourceId,
|
|
|
|
len: i32,
|
2021-04-05 12:40:24 -04:00
|
|
|
) -> Result<(), AnyError> {
|
2022-09-22 05:09:25 -04:00
|
|
|
let len = len as u64;
|
2021-01-08 11:55:17 -05:00
|
|
|
|
2022-07-13 11:16:42 -04:00
|
|
|
StdFileResource::with_file_blocking_task(state, rid, move |std_file| {
|
|
|
|
std_file.set_len(len)?;
|
|
|
|
Ok(())
|
2022-04-19 10:41:31 -04:00
|
|
|
})
|
2022-07-13 11:16:42 -04:00
|
|
|
.await
|
2020-06-20 09:46:10 -04:00
|
|
|
}
|
|
|
|
|
2022-03-14 13:44:15 -04:00
|
|
|
#[op]
|
2020-08-28 11:08:24 -04:00
|
|
|
fn op_truncate_sync(
|
2020-09-10 09:57:45 -04:00
|
|
|
state: &mut OpState,
|
2022-12-15 10:26:10 -05:00
|
|
|
path: String,
|
2022-09-22 05:09:25 -04:00
|
|
|
len: u64,
|
2021-04-05 12:40:24 -04:00
|
|
|
) -> Result<(), AnyError> {
|
2022-12-15 10:26:10 -05:00
|
|
|
let path = PathBuf::from(&path);
|
2020-08-28 11:08:24 -04:00
|
|
|
|
2022-09-27 16:36:33 -04:00
|
|
|
state
|
|
|
|
.borrow_mut::<Permissions>()
|
|
|
|
.write
|
|
|
|
.check(&path, Some("Deno.truncateSync()"))?;
|
2020-08-28 11:08:24 -04:00
|
|
|
|
|
|
|
debug!("op_truncate_sync {} {}", path.display(), len);
|
2021-10-11 09:21:18 -04:00
|
|
|
let err_mapper = |err: Error| {
|
|
|
|
Error::new(
|
|
|
|
err.kind(),
|
|
|
|
format!("{}, truncate '{}'", err, path.display()),
|
|
|
|
)
|
|
|
|
};
|
|
|
|
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 12:40:24 -04:00
|
|
|
Ok(())
|
2020-08-28 11:08:24 -04:00
|
|
|
}
|
|
|
|
|
2022-03-14 13:44:15 -04:00
|
|
|
#[op]
|
2020-08-28 11:08:24 -04:00
|
|
|
async fn op_truncate_async(
|
2020-09-10 09:57:45 -04:00
|
|
|
state: Rc<RefCell<OpState>>,
|
2022-09-22 05:09:25 -04:00
|
|
|
path: String,
|
|
|
|
len: u64,
|
2021-04-05 12:40:24 -04:00
|
|
|
) -> Result<(), AnyError> {
|
2022-09-22 05:09:25 -04:00
|
|
|
let path = PathBuf::from(&path);
|
|
|
|
|
2020-09-19 19:17:35 -04:00
|
|
|
{
|
2021-04-11 22:15:43 -04:00
|
|
|
let mut state = state.borrow_mut();
|
2022-09-27 16:36:33 -04:00
|
|
|
state
|
|
|
|
.borrow_mut::<Permissions>()
|
|
|
|
.write
|
|
|
|
.check(&path, Some("Deno.truncate()"))?;
|
2020-09-19 19:17:35 -04:00
|
|
|
}
|
2020-08-28 11:08:24 -04:00
|
|
|
tokio::task::spawn_blocking(move || {
|
|
|
|
debug!("op_truncate_async {} {}", path.display(), len);
|
2021-10-11 09:21:18 -04:00
|
|
|
let err_mapper = |err: Error| {
|
|
|
|
Error::new(
|
|
|
|
err.kind(),
|
|
|
|
format!("{}, truncate '{}'", err, path.display()),
|
|
|
|
)
|
|
|
|
};
|
|
|
|
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 12:40:24 -04:00
|
|
|
Ok(())
|
2019-08-14 11:03:02 -04:00
|
|
|
})
|
2020-08-28 11:08:24 -04:00
|
|
|
.await
|
|
|
|
.unwrap()
|
2019-08-14 11:03:02 -04:00
|
|
|
}
|
|
|
|
|
2020-03-20 09:46:26 -04: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 09:03:41 -04:00
|
|
|
Some(p) => p.to_path_buf(),
|
2020-03-20 09:46:26 -04:00
|
|
|
None => temp_dir(),
|
|
|
|
}
|
|
|
|
.join("_");
|
|
|
|
let mut rng = thread_rng();
|
|
|
|
loop {
|
|
|
|
let unique = rng.gen::<u32>();
|
|
|
|
buf.set_file_name(format!("{}{:08x}{}", prefix_, unique, suffix_));
|
|
|
|
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);
|
|
|
|
}
|
|
|
|
open_options.open(buf.as_path())?;
|
|
|
|
Ok(())
|
|
|
|
};
|
|
|
|
match r {
|
|
|
|
Err(ref e) if e.kind() == std::io::ErrorKind::AlreadyExists => continue,
|
|
|
|
Ok(_) => return Ok(buf),
|
|
|
|
Err(e) => return Err(e),
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-08-26 10:18:42 -04:00
|
|
|
#[derive(Deserialize)]
|
|
|
|
#[serde(rename_all = "camelCase")]
|
2021-03-18 14:42:01 -04:00
|
|
|
pub struct MakeTempArgs {
|
2019-08-26 10:18:42 -04:00
|
|
|
dir: Option<String>,
|
|
|
|
prefix: Option<String>,
|
|
|
|
suffix: Option<String>,
|
|
|
|
}
|
|
|
|
|
2022-03-14 13:44:15 -04:00
|
|
|
#[op]
|
2020-08-28 11:08:24 -04:00
|
|
|
fn op_make_temp_dir_sync(
|
2020-09-10 09:57:45 -04:00
|
|
|
state: &mut OpState,
|
2021-03-18 14:42:01 -04:00
|
|
|
args: MakeTempArgs,
|
2021-04-05 12:40:24 -04:00
|
|
|
) -> Result<String, AnyError> {
|
2020-08-28 11:08:24 -04:00
|
|
|
let dir = args.dir.map(|s| PathBuf::from(&s));
|
|
|
|
let prefix = args.prefix.map(String::from);
|
|
|
|
let suffix = args.suffix.map(String::from);
|
|
|
|
|
2022-09-27 16:36:33 -04:00
|
|
|
state.borrow_mut::<Permissions>().write.check(
|
|
|
|
dir.clone().unwrap_or_else(temp_dir).as_path(),
|
|
|
|
Some("Deno.makeTempDirSync()"),
|
|
|
|
)?;
|
2020-08-28 11:08:24 -04: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 12:40:24 -04:00
|
|
|
Ok(path_str)
|
2020-08-28 11:08:24 -04:00
|
|
|
}
|
|
|
|
|
2022-03-14 13:44:15 -04:00
|
|
|
#[op]
|
2020-08-28 11:08:24 -04:00
|
|
|
async fn op_make_temp_dir_async(
|
2020-09-10 09:57:45 -04:00
|
|
|
state: Rc<RefCell<OpState>>,
|
2021-03-18 14:42:01 -04:00
|
|
|
args: MakeTempArgs,
|
2021-04-05 12:40:24 -04:00
|
|
|
) -> Result<String, AnyError> {
|
2020-05-29 11:27:43 -04:00
|
|
|
let dir = args.dir.map(|s| PathBuf::from(&s));
|
2020-02-18 14:45:59 -05:00
|
|
|
let prefix = args.prefix.map(String::from);
|
|
|
|
let suffix = args.suffix.map(String::from);
|
2020-09-10 09:57:45 -04:00
|
|
|
{
|
2021-04-11 22:15:43 -04:00
|
|
|
let mut state = state.borrow_mut();
|
2022-09-27 16:36:33 -04:00
|
|
|
state.borrow_mut::<Permissions>().write.check(
|
|
|
|
dir.clone().unwrap_or_else(temp_dir).as_path(),
|
|
|
|
Some("Deno.makeTempDir()"),
|
|
|
|
)?;
|
2020-09-10 09:57:45 -04:00
|
|
|
}
|
2020-08-28 11:08:24 -04:00
|
|
|
tokio::task::spawn_blocking(move || {
|
2020-02-18 14:45:59 -05: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 09:46:26 -04:00
|
|
|
let path = make_temp(
|
2020-02-18 14:45:59 -05:00
|
|
|
// Converting Option<String> to Option<&str>
|
2020-05-15 15:26:16 -04:00
|
|
|
dir.as_deref(),
|
|
|
|
prefix.as_deref(),
|
|
|
|
suffix.as_deref(),
|
2020-02-18 14:45:59 -05:00
|
|
|
true,
|
|
|
|
)?;
|
2020-04-03 13:47:57 -04:00
|
|
|
let path_str = into_string(path.into_os_string())?;
|
2020-02-18 14:45:59 -05:00
|
|
|
|
2021-04-05 12:40:24 -04:00
|
|
|
Ok(path_str)
|
2020-02-18 14:45:59 -05:00
|
|
|
})
|
2020-08-28 11:08:24 -04:00
|
|
|
.await
|
|
|
|
.unwrap()
|
2020-02-18 14:45:59 -05:00
|
|
|
}
|
|
|
|
|
2022-03-14 13:44:15 -04:00
|
|
|
#[op]
|
2020-08-28 11:08:24 -04:00
|
|
|
fn op_make_temp_file_sync(
|
2020-09-10 09:57:45 -04:00
|
|
|
state: &mut OpState,
|
2021-03-18 14:42:01 -04:00
|
|
|
args: MakeTempArgs,
|
2021-04-05 12:40:24 -04:00
|
|
|
) -> Result<String, AnyError> {
|
2020-08-28 11:08:24 -04:00
|
|
|
let dir = args.dir.map(|s| PathBuf::from(&s));
|
|
|
|
let prefix = args.prefix.map(String::from);
|
|
|
|
let suffix = args.suffix.map(String::from);
|
|
|
|
|
2022-09-27 16:36:33 -04:00
|
|
|
state.borrow_mut::<Permissions>().write.check(
|
|
|
|
dir.clone().unwrap_or_else(temp_dir).as_path(),
|
|
|
|
Some("Deno.makeTempFileSync()"),
|
|
|
|
)?;
|
2020-08-28 11:08:24 -04: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 12:40:24 -04:00
|
|
|
Ok(path_str)
|
2020-08-28 11:08:24 -04:00
|
|
|
}
|
|
|
|
|
2022-03-14 13:44:15 -04:00
|
|
|
#[op]
|
2020-08-28 11:08:24 -04:00
|
|
|
async fn op_make_temp_file_async(
|
2020-09-10 09:57:45 -04:00
|
|
|
state: Rc<RefCell<OpState>>,
|
2021-03-18 14:42:01 -04:00
|
|
|
args: MakeTempArgs,
|
2021-04-05 12:40:24 -04:00
|
|
|
) -> Result<String, AnyError> {
|
2020-05-29 11:27:43 -04:00
|
|
|
let dir = args.dir.map(|s| PathBuf::from(&s));
|
2019-08-26 10:18:42 -04:00
|
|
|
let prefix = args.prefix.map(String::from);
|
|
|
|
let suffix = args.suffix.map(String::from);
|
2020-09-10 09:57:45 -04:00
|
|
|
{
|
2021-04-11 22:15:43 -04:00
|
|
|
let mut state = state.borrow_mut();
|
2022-09-27 16:36:33 -04:00
|
|
|
state.borrow_mut::<Permissions>().write.check(
|
|
|
|
dir.clone().unwrap_or_else(temp_dir).as_path(),
|
|
|
|
Some("Deno.makeTempFile()"),
|
|
|
|
)?;
|
2020-09-10 09:57:45 -04:00
|
|
|
}
|
2020-08-28 11:08:24 -04:00
|
|
|
tokio::task::spawn_blocking(move || {
|
2019-08-14 11:03:02 -04: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 09:46:26 -04:00
|
|
|
let path = make_temp(
|
2019-08-14 11:03:02 -04:00
|
|
|
// Converting Option<String> to Option<&str>
|
2020-05-15 15:26:16 -04:00
|
|
|
dir.as_deref(),
|
|
|
|
prefix.as_deref(),
|
|
|
|
suffix.as_deref(),
|
2020-02-18 14:45:59 -05:00
|
|
|
false,
|
2019-08-14 11:03:02 -04:00
|
|
|
)?;
|
2020-04-03 13:47:57 -04:00
|
|
|
let path_str = into_string(path.into_os_string())?;
|
2019-08-26 10:18:42 -04:00
|
|
|
|
2021-04-05 12:40:24 -04:00
|
|
|
Ok(path_str)
|
2019-08-14 11:03:02 -04:00
|
|
|
})
|
2020-08-28 11:08:24 -04:00
|
|
|
.await
|
|
|
|
.unwrap()
|
2019-08-14 11:03:02 -04:00
|
|
|
}
|
|
|
|
|
2022-03-14 13:44:15 -04:00
|
|
|
#[op]
|
2020-08-31 14:29:43 -04:00
|
|
|
fn op_futime_sync(
|
2020-09-10 09:57:45 -04:00
|
|
|
state: &mut OpState,
|
2022-09-22 05:09:25 -04:00
|
|
|
rid: ResourceId,
|
|
|
|
atime_secs: i64,
|
|
|
|
atime_nanos: u32,
|
|
|
|
mtime_secs: i64,
|
|
|
|
mtime_nanos: u32,
|
2021-04-05 12:40:24 -04:00
|
|
|
) -> Result<(), AnyError> {
|
2022-09-22 05:09:25 -04: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 14:29:43 -04:00
|
|
|
|
2022-05-11 12:48:38 -04: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 14:29:43 -04:00
|
|
|
})?;
|
|
|
|
|
2021-04-05 12:40:24 -04:00
|
|
|
Ok(())
|
2020-08-31 14:29:43 -04:00
|
|
|
}
|
|
|
|
|
2022-03-14 13:44:15 -04:00
|
|
|
#[op]
|
2020-08-31 14:29:43 -04:00
|
|
|
async fn op_futime_async(
|
2020-09-10 09:57:45 -04:00
|
|
|
state: Rc<RefCell<OpState>>,
|
2022-09-22 05:09:25 -04:00
|
|
|
rid: ResourceId,
|
|
|
|
atime_secs: i64,
|
|
|
|
atime_nanos: u32,
|
|
|
|
mtime_secs: i64,
|
|
|
|
mtime_nanos: u32,
|
2021-04-05 12:40:24 -04:00
|
|
|
) -> Result<(), AnyError> {
|
2022-09-22 05:09:25 -04: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 14:29:43 -04:00
|
|
|
|
2022-07-13 11:16:42 -04: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 12:40:24 -04:00
|
|
|
Ok(())
|
2021-01-08 11:55:17 -05:00
|
|
|
})
|
|
|
|
.await
|
2020-08-31 14:29:43 -04:00
|
|
|
}
|
|
|
|
|
2022-03-14 13:44:15 -04:00
|
|
|
#[op]
|
2022-09-22 05:09:25 -04:00
|
|
|
fn op_utime_sync(
|
|
|
|
state: &mut OpState,
|
2022-12-15 10:26:10 -05:00
|
|
|
path: String,
|
2022-09-22 05:09:25 -04:00
|
|
|
atime_secs: i64,
|
|
|
|
atime_nanos: u32,
|
|
|
|
mtime_secs: i64,
|
|
|
|
mtime_nanos: u32,
|
|
|
|
) -> Result<(), AnyError> {
|
2022-12-15 10:26:10 -05:00
|
|
|
let path = PathBuf::from(&path);
|
2022-09-22 05:09:25 -04: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 11:08:24 -04:00
|
|
|
|
2022-09-27 16:36:33 -04:00
|
|
|
state
|
|
|
|
.borrow_mut::<Permissions>()
|
|
|
|
.write
|
|
|
|
.check(&path, Some("Deno.utime()"))?;
|
2021-10-11 09:21:18 -04:00
|
|
|
filetime::set_file_times(&path, atime, mtime).map_err(|err| {
|
|
|
|
Error::new(err.kind(), format!("{}, utime '{}'", err, path.display()))
|
|
|
|
})?;
|
2021-04-05 12:40:24 -04:00
|
|
|
Ok(())
|
2020-08-28 11:08:24 -04:00
|
|
|
}
|
|
|
|
|
2022-03-14 13:44:15 -04:00
|
|
|
#[op]
|
2020-08-28 11:08:24 -04:00
|
|
|
async fn op_utime_async(
|
2020-09-10 09:57:45 -04:00
|
|
|
state: Rc<RefCell<OpState>>,
|
2022-09-22 05:09:25 -04:00
|
|
|
path: String,
|
|
|
|
atime_secs: i64,
|
|
|
|
atime_nanos: u32,
|
|
|
|
mtime_secs: i64,
|
|
|
|
mtime_nanos: u32,
|
2021-04-05 12:40:24 -04:00
|
|
|
) -> Result<(), AnyError> {
|
2022-09-22 05:09:25 -04: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 09:46:26 -04:00
|
|
|
|
2021-04-11 22:15:43 -04:00
|
|
|
state
|
|
|
|
.borrow_mut()
|
|
|
|
.borrow_mut::<Permissions>()
|
|
|
|
.write
|
2022-09-27 16:36:33 -04:00
|
|
|
.check(&path, Some("Deno.utime()"))?;
|
2020-03-20 09:46:26 -04:00
|
|
|
|
2020-08-28 11:08:24 -04:00
|
|
|
tokio::task::spawn_blocking(move || {
|
2021-10-11 09:21:18 -04:00
|
|
|
filetime::set_file_times(&path, atime, mtime).map_err(|err| {
|
|
|
|
Error::new(err.kind(), format!("{}, utime '{}'", err, path.display()))
|
|
|
|
})?;
|
2021-04-05 12:40:24 -04:00
|
|
|
Ok(())
|
2019-08-14 11:03:02 -04:00
|
|
|
})
|
2020-08-28 11:08:24 -04:00
|
|
|
.await
|
|
|
|
.unwrap()
|
2019-08-14 11:03:02 -04:00
|
|
|
}
|
|
|
|
|
2022-03-14 13:44:15 -04:00
|
|
|
#[op]
|
2022-03-14 18:38:53 -04:00
|
|
|
fn op_cwd(state: &mut OpState) -> Result<String, AnyError> {
|
2020-03-20 09:46:26 -04:00
|
|
|
let path = current_dir()?;
|
2022-09-27 16:36:33 -04:00
|
|
|
state.borrow_mut::<Permissions>().read.check_blind(
|
|
|
|
&path,
|
|
|
|
"CWD",
|
|
|
|
"Deno.cwd()",
|
|
|
|
)?;
|
2020-04-03 13:47:57 -04:00
|
|
|
let path_str = into_string(path.into_os_string())?;
|
2021-04-05 12:40:24 -04:00
|
|
|
Ok(path_str)
|
2019-08-14 11:03:02 -04:00
|
|
|
}
|
2022-04-27 10:03:44 -04:00
|
|
|
|
|
|
|
#[op]
|
|
|
|
fn op_readfile_sync(
|
|
|
|
state: &mut OpState,
|
|
|
|
path: String,
|
|
|
|
) -> Result<ZeroCopyBuf, AnyError> {
|
|
|
|
let permissions = state.borrow_mut::<Permissions>();
|
|
|
|
let path = Path::new(&path);
|
2022-09-27 16:36:33 -04:00
|
|
|
permissions.read.check(path, Some("Deno.readFileSync()"))?;
|
2022-04-27 10:03:44 -04:00
|
|
|
Ok(std::fs::read(path)?.into())
|
|
|
|
}
|
|
|
|
|
|
|
|
#[op]
|
|
|
|
fn op_readfile_text_sync(
|
|
|
|
state: &mut OpState,
|
|
|
|
path: String,
|
|
|
|
) -> Result<String, AnyError> {
|
|
|
|
let permissions = state.borrow_mut::<Permissions>();
|
|
|
|
let path = Path::new(&path);
|
2022-09-27 16:36:33 -04:00
|
|
|
permissions
|
|
|
|
.read
|
|
|
|
.check(path, Some("Deno.readTextFileSync()"))?;
|
2022-05-01 22:13:05 -04:00
|
|
|
Ok(string_from_utf8_lossy(std::fs::read(path)?))
|
2022-04-27 10:03:44 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
#[op]
|
|
|
|
async fn op_readfile_async(
|
|
|
|
state: Rc<RefCell<OpState>>,
|
|
|
|
path: String,
|
|
|
|
cancel_rid: Option<ResourceId>,
|
|
|
|
) -> Result<ZeroCopyBuf, AnyError> {
|
|
|
|
{
|
|
|
|
let path = Path::new(&path);
|
|
|
|
let mut state = state.borrow_mut();
|
2022-09-27 16:36:33 -04:00
|
|
|
state
|
|
|
|
.borrow_mut::<Permissions>()
|
|
|
|
.read
|
|
|
|
.check(path, Some("Deno.readFile()"))?;
|
2022-04-27 10:03:44 -04:00
|
|
|
}
|
|
|
|
let fut = tokio::task::spawn_blocking(move || {
|
|
|
|
let path = Path::new(&path);
|
|
|
|
Ok(std::fs::read(path).map(ZeroCopyBuf::from)?)
|
|
|
|
});
|
|
|
|
if let Some(cancel_rid) = cancel_rid {
|
|
|
|
let cancel_handle = state
|
|
|
|
.borrow_mut()
|
|
|
|
.resource_table
|
|
|
|
.get::<CancelHandle>(cancel_rid);
|
|
|
|
if let Ok(cancel_handle) = cancel_handle {
|
|
|
|
return fut.or_cancel(cancel_handle).await??;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
fut.await?
|
|
|
|
}
|
|
|
|
|
|
|
|
#[op]
|
|
|
|
async fn op_readfile_text_async(
|
|
|
|
state: Rc<RefCell<OpState>>,
|
|
|
|
path: String,
|
|
|
|
cancel_rid: Option<ResourceId>,
|
|
|
|
) -> Result<String, AnyError> {
|
|
|
|
{
|
|
|
|
let path = Path::new(&path);
|
|
|
|
let mut state = state.borrow_mut();
|
2022-09-27 16:36:33 -04:00
|
|
|
state
|
|
|
|
.borrow_mut::<Permissions>()
|
|
|
|
.read
|
|
|
|
.check(path, Some("Deno.readTextFile()"))?;
|
2022-04-27 10:03:44 -04:00
|
|
|
}
|
|
|
|
let fut = tokio::task::spawn_blocking(move || {
|
|
|
|
let path = Path::new(&path);
|
2022-05-01 22:13:05 -04:00
|
|
|
Ok(string_from_utf8_lossy(std::fs::read(path)?))
|
2022-04-27 10:03:44 -04:00
|
|
|
});
|
|
|
|
if let Some(cancel_rid) = cancel_rid {
|
|
|
|
let cancel_handle = state
|
|
|
|
.borrow_mut()
|
|
|
|
.resource_table
|
|
|
|
.get::<CancelHandle>(cancel_rid);
|
|
|
|
if let Ok(cancel_handle) = cancel_handle {
|
|
|
|
return fut.or_cancel(cancel_handle).await??;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
fut.await?
|
|
|
|
}
|
2022-05-01 22:13:05 -04: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) },
|
|
|
|
}
|
|
|
|
}
|