2021-01-10 21:59:07 -05:00
|
|
|
// Copyright 2018-2021 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;
|
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;
|
2021-01-08 11:55:17 -05:00
|
|
|
use deno_core::error::bad_resource_id;
|
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;
|
2020-09-21 12:36:37 -04:00
|
|
|
use deno_core::serde_json;
|
|
|
|
use deno_core::serde_json::json;
|
|
|
|
use deno_core::serde_json::Value;
|
2020-08-18 08:07:57 -04:00
|
|
|
use deno_core::BufVec;
|
2020-09-10 09:57:45 -04:00
|
|
|
use deno_core::OpState;
|
2021-01-08 11:55:17 -05:00
|
|
|
use deno_core::RcRef;
|
2021-03-19 13:25:37 -04:00
|
|
|
use deno_core::ResourceId;
|
2020-04-15 20:43:19 -04:00
|
|
|
use deno_core::ZeroCopyBuf;
|
2020-11-13 16:01:57 -05:00
|
|
|
use deno_crypto::rand::thread_rng;
|
|
|
|
use deno_crypto::rand::Rng;
|
2020-09-16 12:43:08 -04:00
|
|
|
use serde::Deserialize;
|
2020-09-10 09:57:45 -04:00
|
|
|
use std::cell::RefCell;
|
2019-08-14 11:03:02 -04:00
|
|
|
use std::convert::From;
|
2020-03-20 09:46:26 -04:00
|
|
|
use std::env::{current_dir, set_current_dir, temp_dir};
|
2020-04-27 14:09:56 -04:00
|
|
|
use std::io;
|
2020-08-28 11:08:24 -04:00
|
|
|
use std::io::{Seek, SeekFrom};
|
2020-03-20 09:46:26 -04:00
|
|
|
use std::path::{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;
|
2021-01-12 02:50:02 -05:00
|
|
|
use tokio::io::AsyncSeekExt;
|
2019-08-14 11:03:02 -04:00
|
|
|
|
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;
|
|
|
|
|
2020-09-10 09:57:45 -04:00
|
|
|
pub fn init(rt: &mut deno_core::JsRuntime) {
|
|
|
|
super::reg_json_sync(rt, "op_open_sync", op_open_sync);
|
|
|
|
super::reg_json_async(rt, "op_open_async", op_open_async);
|
2019-08-14 11:03:02 -04:00
|
|
|
|
2020-09-10 09:57:45 -04:00
|
|
|
super::reg_json_sync(rt, "op_seek_sync", op_seek_sync);
|
|
|
|
super::reg_json_async(rt, "op_seek_async", op_seek_async);
|
2020-08-18 08:07:57 -04:00
|
|
|
|
2020-09-10 09:57:45 -04:00
|
|
|
super::reg_json_sync(rt, "op_fdatasync_sync", op_fdatasync_sync);
|
|
|
|
super::reg_json_async(rt, "op_fdatasync_async", op_fdatasync_async);
|
2020-08-18 08:07:57 -04:00
|
|
|
|
2020-09-10 09:57:45 -04:00
|
|
|
super::reg_json_sync(rt, "op_fsync_sync", op_fsync_sync);
|
|
|
|
super::reg_json_async(rt, "op_fsync_async", op_fsync_async);
|
2020-08-28 11:08:24 -04:00
|
|
|
|
2020-09-10 09:57:45 -04:00
|
|
|
super::reg_json_sync(rt, "op_fstat_sync", op_fstat_sync);
|
|
|
|
super::reg_json_async(rt, "op_fstat_async", op_fstat_async);
|
2020-08-28 11:08:24 -04:00
|
|
|
|
2020-09-10 09:57:45 -04:00
|
|
|
super::reg_json_sync(rt, "op_umask", op_umask);
|
|
|
|
super::reg_json_sync(rt, "op_chdir", op_chdir);
|
2020-08-28 11:08:24 -04:00
|
|
|
|
2020-09-10 09:57:45 -04:00
|
|
|
super::reg_json_sync(rt, "op_mkdir_sync", op_mkdir_sync);
|
|
|
|
super::reg_json_async(rt, "op_mkdir_async", op_mkdir_async);
|
2020-08-28 11:08:24 -04:00
|
|
|
|
2020-09-10 09:57:45 -04:00
|
|
|
super::reg_json_sync(rt, "op_chmod_sync", op_chmod_sync);
|
|
|
|
super::reg_json_async(rt, "op_chmod_async", op_chmod_async);
|
2020-08-28 11:08:24 -04:00
|
|
|
|
2020-09-10 09:57:45 -04:00
|
|
|
super::reg_json_sync(rt, "op_chown_sync", op_chown_sync);
|
|
|
|
super::reg_json_async(rt, "op_chown_async", op_chown_async);
|
2020-08-28 11:08:24 -04:00
|
|
|
|
2020-09-10 09:57:45 -04:00
|
|
|
super::reg_json_sync(rt, "op_remove_sync", op_remove_sync);
|
|
|
|
super::reg_json_async(rt, "op_remove_async", op_remove_async);
|
2020-08-28 11:08:24 -04:00
|
|
|
|
2020-09-10 09:57:45 -04:00
|
|
|
super::reg_json_sync(rt, "op_copy_file_sync", op_copy_file_sync);
|
|
|
|
super::reg_json_async(rt, "op_copy_file_async", op_copy_file_async);
|
2020-08-28 11:08:24 -04:00
|
|
|
|
2020-09-10 09:57:45 -04:00
|
|
|
super::reg_json_sync(rt, "op_stat_sync", op_stat_sync);
|
|
|
|
super::reg_json_async(rt, "op_stat_async", op_stat_async);
|
2020-08-28 11:08:24 -04:00
|
|
|
|
2020-09-10 09:57:45 -04:00
|
|
|
super::reg_json_sync(rt, "op_realpath_sync", op_realpath_sync);
|
|
|
|
super::reg_json_async(rt, "op_realpath_async", op_realpath_async);
|
2020-08-28 11:08:24 -04:00
|
|
|
|
2020-09-10 09:57:45 -04:00
|
|
|
super::reg_json_sync(rt, "op_read_dir_sync", op_read_dir_sync);
|
|
|
|
super::reg_json_async(rt, "op_read_dir_async", op_read_dir_async);
|
2020-08-28 11:08:24 -04:00
|
|
|
|
2020-09-10 09:57:45 -04:00
|
|
|
super::reg_json_sync(rt, "op_rename_sync", op_rename_sync);
|
|
|
|
super::reg_json_async(rt, "op_rename_async", op_rename_async);
|
2020-08-28 11:08:24 -04:00
|
|
|
|
2020-09-10 09:57:45 -04:00
|
|
|
super::reg_json_sync(rt, "op_link_sync", op_link_sync);
|
|
|
|
super::reg_json_async(rt, "op_link_async", op_link_async);
|
2020-08-28 11:08:24 -04:00
|
|
|
|
2020-09-10 09:57:45 -04:00
|
|
|
super::reg_json_sync(rt, "op_symlink_sync", op_symlink_sync);
|
|
|
|
super::reg_json_async(rt, "op_symlink_async", op_symlink_async);
|
2020-08-28 11:08:24 -04:00
|
|
|
|
2020-09-10 09:57:45 -04:00
|
|
|
super::reg_json_sync(rt, "op_read_link_sync", op_read_link_sync);
|
|
|
|
super::reg_json_async(rt, "op_read_link_async", op_read_link_async);
|
2020-08-28 11:08:24 -04:00
|
|
|
|
2020-09-10 09:57:45 -04:00
|
|
|
super::reg_json_sync(rt, "op_ftruncate_sync", op_ftruncate_sync);
|
|
|
|
super::reg_json_async(rt, "op_ftruncate_async", op_ftruncate_async);
|
2020-08-28 11:08:24 -04:00
|
|
|
|
2020-09-10 09:57:45 -04:00
|
|
|
super::reg_json_sync(rt, "op_truncate_sync", op_truncate_sync);
|
|
|
|
super::reg_json_async(rt, "op_truncate_async", op_truncate_async);
|
2020-08-28 11:08:24 -04:00
|
|
|
|
2020-09-10 09:57:45 -04:00
|
|
|
super::reg_json_sync(rt, "op_make_temp_dir_sync", op_make_temp_dir_sync);
|
|
|
|
super::reg_json_async(rt, "op_make_temp_dir_async", op_make_temp_dir_async);
|
2020-08-28 11:08:24 -04:00
|
|
|
|
2020-09-10 09:57:45 -04:00
|
|
|
super::reg_json_sync(rt, "op_make_temp_file_sync", op_make_temp_file_sync);
|
|
|
|
super::reg_json_async(rt, "op_make_temp_file_async", op_make_temp_file_async);
|
2020-08-28 11:08:24 -04:00
|
|
|
|
2020-09-10 09:57:45 -04:00
|
|
|
super::reg_json_sync(rt, "op_cwd", op_cwd);
|
2020-08-28 11:08:24 -04:00
|
|
|
|
2020-09-10 09:57:45 -04:00
|
|
|
super::reg_json_sync(rt, "op_futime_sync", op_futime_sync);
|
|
|
|
super::reg_json_async(rt, "op_futime_async", op_futime_async);
|
2020-08-31 14:29:43 -04:00
|
|
|
|
2020-09-10 09:57:45 -04:00
|
|
|
super::reg_json_sync(rt, "op_utime_sync", op_utime_sync);
|
|
|
|
super::reg_json_async(rt, "op_utime_async", op_utime_async);
|
2019-10-11 14:41:54 -04:00
|
|
|
}
|
|
|
|
|
2020-09-14 12:48:57 -04:00
|
|
|
fn into_string(s: std::ffi::OsString) -> Result<String, AnyError> {
|
2020-08-25 18:22:15 -04:00
|
|
|
s.into_string().map_err(|s| {
|
|
|
|
let message = format!("File name or path {:?} is not valid UTF-8", s);
|
2020-09-14 12:48:57 -04:00
|
|
|
custom_error("InvalidData", message)
|
2020-08-25 18:22:15 -04:00
|
|
|
})
|
2020-04-03 13:47:57 -04:00
|
|
|
}
|
|
|
|
|
2020-03-07 07:20:27 -05:00
|
|
|
#[derive(Deserialize)]
|
|
|
|
#[serde(rename_all = "camelCase")]
|
2021-03-18 14:42:01 -04:00
|
|
|
pub struct OpenArgs {
|
2020-03-07 07:20:27 -05:00
|
|
|
path: String,
|
2020-03-16 15:02:41 -04:00
|
|
|
mode: Option<u32>,
|
2020-08-18 08:07:57 -04:00
|
|
|
options: OpenOptions,
|
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,
|
|
|
|
}
|
|
|
|
|
2020-08-18 08:07:57 -04:00
|
|
|
fn open_helper(
|
2020-09-10 09:57:45 -04:00
|
|
|
state: &mut OpState,
|
2021-03-18 14:42:01 -04:00
|
|
|
args: OpenArgs,
|
2020-09-14 12:48:57 -04:00
|
|
|
) -> Result<(PathBuf, std::fs::OpenOptions), AnyError> {
|
2020-05-29 11:27:43 -04:00
|
|
|
let path = Path::new(&args.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();
|
|
|
|
|
|
|
|
if let Some(mode) = args.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
|
|
|
|
2020-09-19 19:17:35 -04:00
|
|
|
let permissions = state.borrow::<Permissions>();
|
2020-04-24 18:45:55 -04:00
|
|
|
let options = args.options;
|
2020-09-19 19:17:35 -04:00
|
|
|
|
2020-04-24 18:45:55 -04:00
|
|
|
if options.read {
|
2021-03-17 17:45:12 -04:00
|
|
|
permissions.read.check(&path)?;
|
2020-04-24 18:45:55 -04:00
|
|
|
}
|
2020-03-07 07:20:27 -05:00
|
|
|
|
2020-04-24 18:45:55 -04:00
|
|
|
if options.write || options.append {
|
2021-03-17 17:45:12 -04:00
|
|
|
permissions.write.check(&path)?;
|
2020-04-24 18:45:55 -04:00
|
|
|
}
|
2020-03-07 07:20:27 -05:00
|
|
|
|
2020-04-24 18:45:55 -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))
|
|
|
|
}
|
|
|
|
|
|
|
|
fn op_open_sync(
|
2020-09-10 09:57:45 -04:00
|
|
|
state: &mut OpState,
|
2021-03-18 14:42:01 -04:00
|
|
|
args: OpenArgs,
|
2020-08-18 08:07:57 -04:00
|
|
|
_zero_copy: &mut [ZeroCopyBuf],
|
2020-09-14 12:48:57 -04:00
|
|
|
) -> Result<Value, AnyError> {
|
2020-08-18 08:07:57 -04:00
|
|
|
let (path, open_options) = open_helper(state, args)?;
|
|
|
|
let std_file = open_options.open(path)?;
|
|
|
|
let tokio_file = tokio::fs::File::from_std(std_file);
|
2021-01-14 23:32:27 -05:00
|
|
|
let resource = StdFileResource::fs_file(tokio_file);
|
2020-12-16 11:14:12 -05:00
|
|
|
let rid = state.resource_table.add(resource);
|
2020-08-18 08:07:57 -04:00
|
|
|
Ok(json!(rid))
|
|
|
|
}
|
|
|
|
|
|
|
|
async fn op_open_async(
|
2020-09-10 09:57:45 -04:00
|
|
|
state: Rc<RefCell<OpState>>,
|
2021-03-18 14:42:01 -04:00
|
|
|
args: OpenArgs,
|
2020-08-18 08:07:57 -04:00
|
|
|
_zero_copy: BufVec,
|
2020-09-14 12:48:57 -04:00
|
|
|
) -> Result<Value, AnyError> {
|
2020-09-10 09:57:45 -04:00
|
|
|
let (path, open_options) = open_helper(&mut state.borrow_mut(), args)?;
|
2020-08-18 08:07:57 -04:00
|
|
|
let tokio_file = tokio::fs::OpenOptions::from(open_options)
|
|
|
|
.open(path)
|
|
|
|
.await?;
|
2021-01-14 23:32:27 -05:00
|
|
|
let resource = StdFileResource::fs_file(tokio_file);
|
2020-12-16 11:14:12 -05:00
|
|
|
let rid = state.borrow_mut().resource_table.add(resource);
|
2020-08-18 08:07:57 -04:00
|
|
|
Ok(json!(rid))
|
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
|
|
|
|
2020-08-28 11:08:24 -04:00
|
|
|
fn op_seek_sync(
|
2020-09-10 09:57:45 -04:00
|
|
|
state: &mut OpState,
|
2021-03-18 14:42:01 -04:00
|
|
|
args: SeekArgs,
|
2020-08-28 11:08:24 -04:00
|
|
|
_zero_copy: &mut [ZeroCopyBuf],
|
2020-09-14 12:48:57 -04:00
|
|
|
) -> Result<Value, AnyError> {
|
2020-08-28 11:08:24 -04:00
|
|
|
let (rid, seek_from) = seek_helper(args)?;
|
2021-01-14 23:32:27 -05:00
|
|
|
let pos = StdFileResource::with(state, rid, |r| match r {
|
2020-09-14 12:48:57 -04:00
|
|
|
Ok(std_file) => std_file.seek(seek_from).map_err(AnyError::from),
|
|
|
|
Err(_) => Err(type_error(
|
2020-08-28 11:08:24 -04:00
|
|
|
"cannot seek on this type of resource".to_string(),
|
|
|
|
)),
|
|
|
|
})?;
|
|
|
|
Ok(json!(pos))
|
|
|
|
}
|
|
|
|
|
|
|
|
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,
|
2020-08-28 11:08:24 -04:00
|
|
|
_zero_copy: BufVec,
|
2020-09-14 12:48:57 -04:00
|
|
|
) -> Result<Value, AnyError> {
|
2020-08-28 11:08:24 -04:00
|
|
|
let (rid, seek_from) = seek_helper(args)?;
|
2021-01-08 11:55:17 -05:00
|
|
|
|
|
|
|
let resource = state
|
|
|
|
.borrow_mut()
|
|
|
|
.resource_table
|
2021-01-14 23:32:27 -05:00
|
|
|
.get::<StdFileResource>(rid)
|
2021-01-08 11:55:17 -05:00
|
|
|
.ok_or_else(bad_resource_id)?;
|
|
|
|
|
|
|
|
if resource.fs_file.is_none() {
|
|
|
|
return Err(bad_resource_id());
|
|
|
|
}
|
|
|
|
|
|
|
|
let mut fs_file = RcRef::map(&resource, |r| r.fs_file.as_ref().unwrap())
|
|
|
|
.borrow_mut()
|
|
|
|
.await;
|
|
|
|
|
|
|
|
let pos = (*fs_file).0.as_mut().unwrap().seek(seek_from).await?;
|
2020-08-28 11:08:24 -04:00
|
|
|
Ok(json!(pos))
|
2020-03-07 07:20:27 -05:00
|
|
|
}
|
|
|
|
|
2020-06-26 08:36:35 -04:00
|
|
|
#[derive(Deserialize)]
|
|
|
|
#[serde(rename_all = "camelCase")]
|
2021-03-18 14:42:01 -04:00
|
|
|
pub struct FdatasyncArgs {
|
2021-03-19 13:25:37 -04:00
|
|
|
rid: ResourceId,
|
2020-06-26 08:36:35 -04:00
|
|
|
}
|
|
|
|
|
2020-08-28 11:08:24 -04:00
|
|
|
fn op_fdatasync_sync(
|
2020-09-10 09:57:45 -04:00
|
|
|
state: &mut OpState,
|
2021-03-18 14:42:01 -04:00
|
|
|
args: FdatasyncArgs,
|
2020-06-26 08:36:35 -04:00
|
|
|
_zero_copy: &mut [ZeroCopyBuf],
|
2020-09-14 12:48:57 -04:00
|
|
|
) -> Result<Value, AnyError> {
|
2021-03-18 20:55:31 -04:00
|
|
|
let rid = args.rid;
|
2021-01-14 23:32:27 -05:00
|
|
|
StdFileResource::with(state, rid, |r| match r {
|
2020-09-14 12:48:57 -04:00
|
|
|
Ok(std_file) => std_file.sync_data().map_err(AnyError::from),
|
|
|
|
Err(_) => Err(type_error("cannot sync this type of resource".to_string())),
|
2020-08-28 11:08:24 -04:00
|
|
|
})?;
|
|
|
|
Ok(json!({}))
|
|
|
|
}
|
2020-06-26 08:36:35 -04:00
|
|
|
|
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-03-18 14:42:01 -04:00
|
|
|
args: FdatasyncArgs,
|
2020-08-28 11:08:24 -04:00
|
|
|
_zero_copy: BufVec,
|
2020-09-14 12:48:57 -04:00
|
|
|
) -> Result<Value, AnyError> {
|
2021-03-18 20:55:31 -04:00
|
|
|
let rid = args.rid;
|
2021-01-08 11:55:17 -05:00
|
|
|
|
|
|
|
let resource = state
|
|
|
|
.borrow_mut()
|
|
|
|
.resource_table
|
2021-01-14 23:32:27 -05:00
|
|
|
.get::<StdFileResource>(rid)
|
2021-01-08 11:55:17 -05:00
|
|
|
.ok_or_else(bad_resource_id)?;
|
|
|
|
|
|
|
|
if resource.fs_file.is_none() {
|
|
|
|
return Err(bad_resource_id());
|
|
|
|
}
|
|
|
|
|
|
|
|
let mut fs_file = RcRef::map(&resource, |r| r.fs_file.as_ref().unwrap())
|
|
|
|
.borrow_mut()
|
|
|
|
.await;
|
|
|
|
|
|
|
|
(*fs_file).0.as_mut().unwrap().sync_data().await?;
|
2020-08-28 11:08:24 -04:00
|
|
|
Ok(json!({}))
|
2020-06-26 08:36:35 -04:00
|
|
|
}
|
|
|
|
|
2020-06-21 09:29:44 -04:00
|
|
|
#[derive(Deserialize)]
|
|
|
|
#[serde(rename_all = "camelCase")]
|
2021-03-18 14:42:01 -04:00
|
|
|
pub struct FsyncArgs {
|
2021-03-19 13:25:37 -04:00
|
|
|
rid: ResourceId,
|
2020-06-21 09:29:44 -04:00
|
|
|
}
|
|
|
|
|
2020-08-28 11:08:24 -04:00
|
|
|
fn op_fsync_sync(
|
2020-09-10 09:57:45 -04:00
|
|
|
state: &mut OpState,
|
2021-03-18 14:42:01 -04:00
|
|
|
args: FsyncArgs,
|
2020-06-21 09:29:44 -04:00
|
|
|
_zero_copy: &mut [ZeroCopyBuf],
|
2020-09-14 12:48:57 -04:00
|
|
|
) -> Result<Value, AnyError> {
|
2021-03-18 20:55:31 -04:00
|
|
|
let rid = args.rid;
|
2021-01-14 23:32:27 -05:00
|
|
|
StdFileResource::with(state, rid, |r| match r {
|
2020-09-14 12:48:57 -04:00
|
|
|
Ok(std_file) => std_file.sync_all().map_err(AnyError::from),
|
|
|
|
Err(_) => Err(type_error("cannot sync this type of resource".to_string())),
|
2020-08-28 11:08:24 -04:00
|
|
|
})?;
|
|
|
|
Ok(json!({}))
|
|
|
|
}
|
2020-06-21 09:29:44 -04:00
|
|
|
|
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-03-18 14:42:01 -04:00
|
|
|
args: FsyncArgs,
|
2020-08-28 11:08:24 -04:00
|
|
|
_zero_copy: BufVec,
|
2020-09-14 12:48:57 -04:00
|
|
|
) -> Result<Value, AnyError> {
|
2021-03-18 20:55:31 -04:00
|
|
|
let rid = args.rid;
|
2021-01-08 11:55:17 -05:00
|
|
|
|
|
|
|
let resource = state
|
|
|
|
.borrow_mut()
|
|
|
|
.resource_table
|
2021-01-14 23:32:27 -05:00
|
|
|
.get::<StdFileResource>(rid)
|
2021-01-08 11:55:17 -05:00
|
|
|
.ok_or_else(bad_resource_id)?;
|
|
|
|
|
|
|
|
if resource.fs_file.is_none() {
|
|
|
|
return Err(bad_resource_id());
|
|
|
|
}
|
|
|
|
|
|
|
|
let mut fs_file = RcRef::map(&resource, |r| r.fs_file.as_ref().unwrap())
|
|
|
|
.borrow_mut()
|
|
|
|
.await;
|
|
|
|
|
|
|
|
(*fs_file).0.as_mut().unwrap().sync_all().await?;
|
2020-08-28 11:08:24 -04:00
|
|
|
Ok(json!({}))
|
2020-06-21 09:29:44 -04:00
|
|
|
}
|
|
|
|
|
2020-06-22 08:58:52 -04:00
|
|
|
#[derive(Deserialize)]
|
|
|
|
#[serde(rename_all = "camelCase")]
|
2021-03-18 14:42:01 -04:00
|
|
|
pub struct FstatArgs {
|
2021-03-19 13:25:37 -04:00
|
|
|
rid: ResourceId,
|
2020-06-22 08:58:52 -04:00
|
|
|
}
|
|
|
|
|
2020-08-28 11:08:24 -04:00
|
|
|
fn op_fstat_sync(
|
2020-09-10 09:57:45 -04:00
|
|
|
state: &mut OpState,
|
2021-03-18 14:42:01 -04:00
|
|
|
args: FstatArgs,
|
2020-06-22 08:58:52 -04:00
|
|
|
_zero_copy: &mut [ZeroCopyBuf],
|
2020-09-14 12:48:57 -04:00
|
|
|
) -> Result<Value, AnyError> {
|
2020-09-26 14:26:51 -04:00
|
|
|
super::check_unstable(state, "Deno.fstat");
|
2021-03-18 20:55:31 -04:00
|
|
|
let metadata = StdFileResource::with(state, args.rid, |r| match r {
|
2020-09-14 12:48:57 -04:00
|
|
|
Ok(std_file) => std_file.metadata().map_err(AnyError::from),
|
|
|
|
Err(_) => Err(type_error("cannot stat this type of resource".to_string())),
|
2020-08-28 11:08:24 -04:00
|
|
|
})?;
|
2020-11-27 14:47:35 -05:00
|
|
|
Ok(get_stat_json(metadata))
|
2020-08-28 11:08:24 -04:00
|
|
|
}
|
2020-06-22 08:58:52 -04:00
|
|
|
|
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-03-18 14:42:01 -04:00
|
|
|
args: FstatArgs,
|
2020-08-28 11:08:24 -04:00
|
|
|
_zero_copy: BufVec,
|
2020-09-14 12:48:57 -04:00
|
|
|
) -> Result<Value, AnyError> {
|
2020-09-26 14:26:51 -04:00
|
|
|
super::check_unstable2(&state, "Deno.fstat");
|
2020-09-10 09:57:45 -04:00
|
|
|
|
2021-03-18 20:55:31 -04:00
|
|
|
let rid = args.rid;
|
2021-01-08 11:55:17 -05:00
|
|
|
|
|
|
|
let resource = state
|
|
|
|
.borrow_mut()
|
|
|
|
.resource_table
|
2021-01-14 23:32:27 -05:00
|
|
|
.get::<StdFileResource>(rid)
|
2021-01-08 11:55:17 -05:00
|
|
|
.ok_or_else(bad_resource_id)?;
|
|
|
|
|
|
|
|
if resource.fs_file.is_none() {
|
|
|
|
return Err(bad_resource_id());
|
|
|
|
}
|
|
|
|
|
|
|
|
let mut fs_file = RcRef::map(&resource, |r| r.fs_file.as_ref().unwrap())
|
|
|
|
.borrow_mut()
|
|
|
|
.await;
|
|
|
|
|
|
|
|
let metadata = (*fs_file).0.as_mut().unwrap().metadata().await?;
|
2020-11-27 14:47:35 -05:00
|
|
|
Ok(get_stat_json(metadata))
|
2020-06-22 08:58:52 -04:00
|
|
|
}
|
|
|
|
|
2020-03-10 15:11:27 -04:00
|
|
|
#[derive(Deserialize)]
|
2021-03-18 14:42:01 -04:00
|
|
|
pub struct UmaskArgs {
|
2020-03-10 15:11:27 -04:00
|
|
|
mask: Option<u32>,
|
|
|
|
}
|
|
|
|
|
2021-03-18 14:42:01 -04:00
|
|
|
#[allow(clippy::unnecessary_wraps)]
|
2020-03-10 15:11:27 -04:00
|
|
|
fn op_umask(
|
2020-09-10 09:57:45 -04:00
|
|
|
state: &mut OpState,
|
2021-03-18 14:42:01 -04:00
|
|
|
args: UmaskArgs,
|
2020-06-01 14:20:47 -04:00
|
|
|
_zero_copy: &mut [ZeroCopyBuf],
|
2020-09-14 12:48:57 -04:00
|
|
|
) -> Result<Value, 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))]
|
|
|
|
{
|
|
|
|
let _ = args.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;
|
|
|
|
let r = if let Some(mask) = args.mask {
|
|
|
|
// 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
|
|
|
|
};
|
2020-08-28 11:08:24 -04:00
|
|
|
Ok(json!(r.bits() as u32))
|
2020-03-10 15:11:27 -04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-08-26 10:18:42 -04:00
|
|
|
#[derive(Deserialize)]
|
2021-03-18 14:42:01 -04:00
|
|
|
pub struct ChdirArgs {
|
2019-08-26 10:18:42 -04:00
|
|
|
directory: String,
|
|
|
|
}
|
|
|
|
|
2019-10-11 14:41:54 -04:00
|
|
|
fn op_chdir(
|
2020-09-10 09:57:45 -04:00
|
|
|
state: &mut OpState,
|
2021-03-18 14:42:01 -04:00
|
|
|
args: ChdirArgs,
|
2020-06-01 14:20:47 -04:00
|
|
|
_zero_copy: &mut [ZeroCopyBuf],
|
2020-09-14 12:48:57 -04:00
|
|
|
) -> Result<Value, AnyError> {
|
2020-04-24 19:55:33 -04:00
|
|
|
let d = PathBuf::from(&args.directory);
|
2021-03-17 17:45:12 -04:00
|
|
|
state.borrow::<Permissions>().read.check(&d)?;
|
2020-04-24 19:55:33 -04:00
|
|
|
set_current_dir(&d)?;
|
2020-08-28 11:08:24 -04:00
|
|
|
Ok(json!({}))
|
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
|
|
|
}
|
|
|
|
|
2020-08-28 11:08:24 -04:00
|
|
|
fn op_mkdir_sync(
|
2020-09-10 09:57:45 -04:00
|
|
|
state: &mut OpState,
|
2021-03-18 14:42:01 -04:00
|
|
|
args: MkdirArgs,
|
2020-06-01 14:20:47 -04:00
|
|
|
_zero_copy: &mut [ZeroCopyBuf],
|
2020-09-14 12:48:57 -04:00
|
|
|
) -> Result<Value, 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;
|
2021-03-17 17:45:12 -04:00
|
|
|
state.borrow::<Permissions>().write.check(&path)?;
|
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);
|
|
|
|
}
|
|
|
|
builder.create(path)?;
|
|
|
|
Ok(json!({}))
|
|
|
|
}
|
2019-08-14 11:03:02 -04:00
|
|
|
|
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,
|
2020-08-28 11:08:24 -04:00
|
|
|
_zero_copy: BufVec,
|
2020-09-14 12:48:57 -04:00
|
|
|
) -> Result<Value, 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
|
|
|
{
|
|
|
|
let state = state.borrow();
|
2021-03-17 17:45:12 -04:00
|
|
|
state.borrow::<Permissions>().write.check(&path)?;
|
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);
|
|
|
|
}
|
|
|
|
builder.create(path)?;
|
2019-08-26 10:18:42 -04:00
|
|
|
Ok(json!({}))
|
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
|
|
|
}
|
|
|
|
|
2019-08-26 10:18:42 -04:00
|
|
|
#[derive(Deserialize)]
|
|
|
|
#[serde(rename_all = "camelCase")]
|
2021-03-18 14:42:01 -04:00
|
|
|
pub struct ChmodArgs {
|
2019-08-26 10:18:42 -04:00
|
|
|
path: String,
|
|
|
|
mode: u32,
|
|
|
|
}
|
|
|
|
|
2020-08-28 11:08:24 -04:00
|
|
|
fn op_chmod_sync(
|
2020-09-10 09:57:45 -04:00
|
|
|
state: &mut OpState,
|
2021-03-18 14:42:01 -04:00
|
|
|
args: ChmodArgs,
|
2020-06-01 14:20:47 -04:00
|
|
|
_zero_copy: &mut [ZeroCopyBuf],
|
2020-09-14 12:48:57 -04:00
|
|
|
) -> Result<Value, 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 & 0o777;
|
2019-08-14 11:03:02 -04:00
|
|
|
|
2021-03-17 17:45:12 -04:00
|
|
|
state.borrow::<Permissions>().write.check(&path)?;
|
2020-08-28 11:08:24 -04:00
|
|
|
debug!("op_chmod_sync {} {:o}", path.display(), mode);
|
|
|
|
#[cfg(unix)]
|
|
|
|
{
|
|
|
|
use std::os::unix::fs::PermissionsExt;
|
|
|
|
let permissions = PermissionsExt::from_mode(mode);
|
|
|
|
std::fs::set_permissions(&path, permissions)?;
|
|
|
|
Ok(json!({}))
|
|
|
|
}
|
|
|
|
// TODO Implement chmod for Windows (#4357)
|
|
|
|
#[cfg(not(unix))]
|
|
|
|
{
|
|
|
|
// Still check file/dir exists on Windows
|
|
|
|
let _metadata = std::fs::metadata(&path)?;
|
2020-09-14 12:48:57 -04:00
|
|
|
Err(generic_error("Not implemented"))
|
2020-08-28 11:08:24 -04:00
|
|
|
}
|
|
|
|
}
|
2019-08-14 11:03:02 -04:00
|
|
|
|
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>>,
|
2021-03-18 14:42:01 -04:00
|
|
|
args: ChmodArgs,
|
2020-08-28 11:08:24 -04:00
|
|
|
_zero_copy: BufVec,
|
2020-09-14 12:48:57 -04:00
|
|
|
) -> Result<Value, AnyError> {
|
2020-08-28 11:08:24 -04:00
|
|
|
let path = Path::new(&args.path).to_path_buf();
|
|
|
|
let mode = args.mode & 0o777;
|
2020-09-10 09:57:45 -04:00
|
|
|
|
2020-09-19 19:17:35 -04:00
|
|
|
{
|
|
|
|
let state = state.borrow();
|
2021-03-17 17:45:12 -04:00
|
|
|
state.borrow::<Permissions>().write.check(&path)?;
|
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 || {
|
|
|
|
debug!("op_chmod_async {} {:o}", path.display(), mode);
|
2020-03-02 10:19:42 -05:00
|
|
|
#[cfg(unix)]
|
2019-08-14 11:03:02 -04:00
|
|
|
{
|
2020-03-20 09:46:26 -04:00
|
|
|
use std::os::unix::fs::PermissionsExt;
|
|
|
|
let permissions = PermissionsExt::from_mode(mode);
|
|
|
|
std::fs::set_permissions(&path, permissions)?;
|
2020-03-20 16:03:04 -04:00
|
|
|
Ok(json!({}))
|
2020-03-20 09:46:26 -04:00
|
|
|
}
|
|
|
|
// TODO Implement chmod for Windows (#4357)
|
|
|
|
#[cfg(not(unix))]
|
|
|
|
{
|
|
|
|
// Still check file/dir exists on Windows
|
|
|
|
let _metadata = std::fs::metadata(&path)?;
|
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
|
|
|
}
|
|
|
|
|
2019-08-26 10:18:42 -04:00
|
|
|
#[derive(Deserialize)]
|
|
|
|
#[serde(rename_all = "camelCase")]
|
2021-03-18 14:42:01 -04:00
|
|
|
pub struct ChownArgs {
|
2019-08-26 10:18:42 -04:00
|
|
|
path: String,
|
2020-07-06 07:15:13 -04:00
|
|
|
uid: Option<u32>,
|
|
|
|
gid: Option<u32>,
|
2019-08-26 10:18:42 -04:00
|
|
|
}
|
|
|
|
|
2020-08-28 11:08:24 -04:00
|
|
|
fn op_chown_sync(
|
2020-09-10 09:57:45 -04:00
|
|
|
state: &mut OpState,
|
2021-03-18 14:42:01 -04:00
|
|
|
args: ChownArgs,
|
2020-06-01 14:20:47 -04:00
|
|
|
_zero_copy: &mut [ZeroCopyBuf],
|
2020-09-14 12:48:57 -04:00
|
|
|
) -> Result<Value, AnyError> {
|
2020-05-29 11:27:43 -04:00
|
|
|
let path = Path::new(&args.path).to_path_buf();
|
2021-03-17 17:45:12 -04:00
|
|
|
state.borrow::<Permissions>().write.check(&path)?;
|
2020-08-28 11:08:24 -04:00
|
|
|
debug!(
|
|
|
|
"op_chown_sync {} {:?} {:?}",
|
|
|
|
path.display(),
|
|
|
|
args.uid,
|
|
|
|
args.gid,
|
|
|
|
);
|
|
|
|
#[cfg(unix)]
|
|
|
|
{
|
|
|
|
use nix::unistd::{chown, Gid, Uid};
|
|
|
|
let nix_uid = args.uid.map(Uid::from_raw);
|
|
|
|
let nix_gid = args.gid.map(Gid::from_raw);
|
|
|
|
chown(&path, nix_uid, nix_gid)?;
|
|
|
|
Ok(json!({}))
|
|
|
|
}
|
|
|
|
// 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
|
|
|
|
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>>,
|
2021-03-18 14:42:01 -04:00
|
|
|
args: ChownArgs,
|
2020-08-28 11:08:24 -04:00
|
|
|
_zero_copy: BufVec,
|
2020-09-14 12:48:57 -04:00
|
|
|
) -> Result<Value, AnyError> {
|
2020-08-28 11:08:24 -04:00
|
|
|
let path = Path::new(&args.path).to_path_buf();
|
2020-09-10 09:57:45 -04:00
|
|
|
|
2020-09-19 19:17:35 -04:00
|
|
|
{
|
|
|
|
let state = state.borrow();
|
2021-03-17 17:45:12 -04:00
|
|
|
state.borrow::<Permissions>().write.check(&path)?;
|
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 || {
|
|
|
|
debug!(
|
|
|
|
"op_chown_async {} {:?} {:?}",
|
|
|
|
path.display(),
|
|
|
|
args.uid,
|
|
|
|
args.gid,
|
|
|
|
);
|
2020-03-20 09:46:26 -04:00
|
|
|
#[cfg(unix)]
|
|
|
|
{
|
|
|
|
use nix::unistd::{chown, Gid, Uid};
|
2020-07-06 07:15:13 -04:00
|
|
|
let nix_uid = args.uid.map(Uid::from_raw);
|
|
|
|
let nix_gid = args.gid.map(Gid::from_raw);
|
|
|
|
chown(&path, nix_uid, nix_gid)?;
|
2020-03-20 09:46:26 -04:00
|
|
|
Ok(json!({}))
|
|
|
|
}
|
|
|
|
// 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
|
|
|
}
|
|
|
|
|
2019-08-26 10:18:42 -04:00
|
|
|
#[derive(Deserialize)]
|
|
|
|
#[serde(rename_all = "camelCase")]
|
2021-03-18 14:42:01 -04:00
|
|
|
pub struct RemoveArgs {
|
2019-08-26 10:18:42 -04:00
|
|
|
path: String,
|
|
|
|
recursive: bool,
|
|
|
|
}
|
|
|
|
|
2020-08-28 11:08:24 -04:00
|
|
|
fn op_remove_sync(
|
2020-09-10 09:57:45 -04:00
|
|
|
state: &mut OpState,
|
2021-03-18 14:42:01 -04:00
|
|
|
args: RemoveArgs,
|
2020-06-01 14:20:47 -04:00
|
|
|
_zero_copy: &mut [ZeroCopyBuf],
|
2020-09-14 12:48:57 -04:00
|
|
|
) -> Result<Value, AnyError> {
|
2020-08-28 11:08:24 -04:00
|
|
|
let path = PathBuf::from(&args.path);
|
|
|
|
let recursive = args.recursive;
|
|
|
|
|
2021-03-17 17:45:12 -04:00
|
|
|
state.borrow::<Permissions>().write.check(&path)?;
|
2020-08-28 11:08:24 -04:00
|
|
|
|
|
|
|
#[cfg(not(unix))]
|
|
|
|
use std::os::windows::prelude::MetadataExt;
|
|
|
|
|
|
|
|
let metadata = std::fs::symlink_metadata(&path)?;
|
|
|
|
|
|
|
|
debug!("op_remove_sync {} {}", path.display(), recursive);
|
|
|
|
let file_type = metadata.file_type();
|
|
|
|
if file_type.is_file() {
|
|
|
|
std::fs::remove_file(&path)?;
|
|
|
|
} else if recursive {
|
|
|
|
std::fs::remove_dir_all(&path)?;
|
|
|
|
} else if file_type.is_symlink() {
|
|
|
|
#[cfg(unix)]
|
|
|
|
std::fs::remove_file(&path)?;
|
|
|
|
#[cfg(not(unix))]
|
|
|
|
{
|
|
|
|
use winapi::um::winnt::FILE_ATTRIBUTE_DIRECTORY;
|
|
|
|
if metadata.file_attributes() & FILE_ATTRIBUTE_DIRECTORY != 0 {
|
|
|
|
std::fs::remove_dir(&path)?;
|
|
|
|
} else {
|
|
|
|
std::fs::remove_file(&path)?;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
} else if file_type.is_dir() {
|
|
|
|
std::fs::remove_dir(&path)?;
|
|
|
|
} else {
|
|
|
|
// pipes, sockets, etc...
|
|
|
|
std::fs::remove_file(&path)?;
|
|
|
|
}
|
|
|
|
Ok(json!({}))
|
|
|
|
}
|
|
|
|
|
|
|
|
async fn op_remove_async(
|
2020-09-10 09:57:45 -04:00
|
|
|
state: Rc<RefCell<OpState>>,
|
2021-03-18 14:42:01 -04:00
|
|
|
args: RemoveArgs,
|
2020-08-28 11:08:24 -04:00
|
|
|
_zero_copy: BufVec,
|
2020-09-14 12:48:57 -04:00
|
|
|
) -> Result<Value, AnyError> {
|
2020-05-29 11:27:43 -04:00
|
|
|
let path = PathBuf::from(&args.path);
|
2019-08-26 10:18:42 -04:00
|
|
|
let recursive = args.recursive;
|
2019-08-14 11:03:02 -04:00
|
|
|
|
2020-09-19 19:17:35 -04:00
|
|
|
{
|
|
|
|
let state = state.borrow();
|
2021-03-17 17:45:12 -04:00
|
|
|
state.borrow::<Permissions>().write.check(&path)?;
|
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;
|
|
|
|
|
2020-03-20 09:46:26 -04:00
|
|
|
let metadata = std::fs::symlink_metadata(&path)?;
|
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() {
|
2020-03-20 09:46:26 -04:00
|
|
|
std::fs::remove_file(&path)?;
|
2019-08-14 11:03:02 -04:00
|
|
|
} else if recursive {
|
2020-03-20 09:46:26 -04:00
|
|
|
std::fs::remove_dir_all(&path)?;
|
2020-05-18 08:50:44 -04:00
|
|
|
} else if file_type.is_symlink() {
|
|
|
|
#[cfg(unix)]
|
|
|
|
std::fs::remove_file(&path)?;
|
|
|
|
#[cfg(not(unix))]
|
|
|
|
{
|
|
|
|
use winapi::um::winnt::FILE_ATTRIBUTE_DIRECTORY;
|
|
|
|
if metadata.file_attributes() & FILE_ATTRIBUTE_DIRECTORY != 0 {
|
|
|
|
std::fs::remove_dir(&path)?;
|
|
|
|
} else {
|
|
|
|
std::fs::remove_file(&path)?;
|
|
|
|
}
|
|
|
|
}
|
2020-05-30 14:48:26 -04:00
|
|
|
} else if file_type.is_dir() {
|
2020-03-20 09:46:26 -04:00
|
|
|
std::fs::remove_dir(&path)?;
|
2020-05-30 14:48:26 -04:00
|
|
|
} else {
|
|
|
|
// pipes, sockets, etc...
|
|
|
|
std::fs::remove_file(&path)?;
|
2019-08-14 11:03:02 -04:00
|
|
|
}
|
2019-08-26 10:18:42 -04:00
|
|
|
Ok(json!({}))
|
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
|
|
|
}
|
|
|
|
|
2019-08-26 10:18:42 -04:00
|
|
|
#[derive(Deserialize)]
|
|
|
|
#[serde(rename_all = "camelCase")]
|
2021-03-18 14:42:01 -04:00
|
|
|
pub struct CopyFileArgs {
|
2019-08-26 10:18:42 -04:00
|
|
|
from: String,
|
|
|
|
to: String,
|
|
|
|
}
|
|
|
|
|
2020-08-28 11:08:24 -04:00
|
|
|
fn op_copy_file_sync(
|
2020-09-10 09:57:45 -04:00
|
|
|
state: &mut OpState,
|
2021-03-18 14:42:01 -04:00
|
|
|
args: CopyFileArgs,
|
2020-06-01 14:20:47 -04:00
|
|
|
_zero_copy: &mut [ZeroCopyBuf],
|
2020-09-14 12:48:57 -04:00
|
|
|
) -> Result<Value, AnyError> {
|
2020-08-28 11:08:24 -04:00
|
|
|
let from = PathBuf::from(&args.from);
|
|
|
|
let to = PathBuf::from(&args.to);
|
|
|
|
|
2020-09-19 19:17:35 -04:00
|
|
|
let permissions = state.borrow::<Permissions>();
|
2021-03-17 17:45:12 -04:00
|
|
|
permissions.read.check(&from)?;
|
|
|
|
permissions.write.check(&to)?;
|
2020-08-28 11:08:24 -04:00
|
|
|
|
|
|
|
debug!("op_copy_file_sync {} {}", from.display(), to.display());
|
|
|
|
// 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.
|
|
|
|
if cfg!(unix) && !from.is_file() {
|
2020-09-14 12:48:57 -04:00
|
|
|
return Err(custom_error("NotFound", "File not found"));
|
2020-08-28 11:08:24 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
// returns size of from as u64 (we ignore)
|
|
|
|
std::fs::copy(&from, &to)?;
|
|
|
|
Ok(json!({}))
|
|
|
|
}
|
|
|
|
|
|
|
|
async fn op_copy_file_async(
|
2020-09-10 09:57:45 -04:00
|
|
|
state: Rc<RefCell<OpState>>,
|
2021-03-18 14:42:01 -04:00
|
|
|
args: CopyFileArgs,
|
2020-08-28 11:08:24 -04:00
|
|
|
_zero_copy: BufVec,
|
2020-09-14 12:48:57 -04:00
|
|
|
) -> Result<Value, AnyError> {
|
2020-05-29 11:27:43 -04:00
|
|
|
let from = PathBuf::from(&args.from);
|
|
|
|
let to = PathBuf::from(&args.to);
|
2019-08-26 10:18:42 -04:00
|
|
|
|
2020-09-19 19:17:35 -04:00
|
|
|
{
|
|
|
|
let state = state.borrow();
|
|
|
|
let permissions = state.borrow::<Permissions>();
|
2021-03-17 17:45:12 -04:00
|
|
|
permissions.read.check(&from)?;
|
|
|
|
permissions.write.check(&to)?;
|
2020-09-19 19:17:35 -04:00
|
|
|
}
|
2019-08-14 11:03:02 -04:00
|
|
|
|
2020-08-28 11:08:24 -04:00
|
|
|
debug!("op_copy_file_async {} {}", from.display(), to.display());
|
|
|
|
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() {
|
2020-09-14 12:48:57 -04:00
|
|
|
return Err(custom_error("NotFound", "File not found"));
|
2019-08-14 11:03:02 -04:00
|
|
|
}
|
|
|
|
|
2020-03-14 22:57:42 -04:00
|
|
|
// returns size of from as u64 (we ignore)
|
2020-03-20 09:46:26 -04:00
|
|
|
std::fs::copy(&from, &to)?;
|
2019-08-26 10:18:42 -04:00
|
|
|
Ok(json!({}))
|
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-09-05 20:34:02 -04:00
|
|
|
fn to_msec(maybe_time: Result<SystemTime, io::Error>) -> Value {
|
2020-04-27 14:09:56 -04:00
|
|
|
match maybe_time {
|
|
|
|
Ok(time) => {
|
|
|
|
let msec = time
|
|
|
|
.duration_since(UNIX_EPOCH)
|
|
|
|
.map(|t| t.as_secs_f64() * 1000f64)
|
|
|
|
.unwrap_or_else(|err| err.duration().as_secs_f64() * -1000f64);
|
|
|
|
serde_json::Number::from_f64(msec)
|
2020-09-05 20:34:02 -04:00
|
|
|
.map(Value::Number)
|
|
|
|
.unwrap_or(Value::Null)
|
2020-04-27 14:09:56 -04:00
|
|
|
}
|
2020-09-05 20:34:02 -04:00
|
|
|
Err(_) => Value::Null,
|
2020-04-27 14:09:56 -04:00
|
|
|
}
|
2019-08-14 11:03:02 -04:00
|
|
|
}
|
|
|
|
|
2020-01-16 09:46:32 -05:00
|
|
|
#[inline(always)]
|
2020-11-27 14:47:35 -05:00
|
|
|
fn get_stat_json(metadata: std::fs::Metadata) -> Value {
|
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;
|
2020-04-29 16:00:31 -04:00
|
|
|
let json_val = json!({
|
2020-01-16 09:46:32 -05:00
|
|
|
"isFile": metadata.is_file(),
|
2020-03-31 13:46:25 -04:00
|
|
|
"isDirectory": metadata.is_dir(),
|
2020-01-16 09:46:32 -05:00
|
|
|
"isSymlink": metadata.file_type().is_symlink(),
|
2020-03-14 22:57:42 -04:00
|
|
|
"size": metadata.len(),
|
2020-04-27 14:09:56 -04:00
|
|
|
// In milliseconds, like JavaScript. Available on both Unix or Windows.
|
|
|
|
"mtime": to_msec(metadata.modified()),
|
|
|
|
"atime": to_msec(metadata.accessed()),
|
|
|
|
"birthtime": to_msec(metadata.created()),
|
2020-01-16 09:46:32 -05:00
|
|
|
// Following are only valid under Unix.
|
|
|
|
"dev": usm!(dev),
|
|
|
|
"ino": usm!(ino),
|
|
|
|
"mode": usm!(mode),
|
|
|
|
"nlink": usm!(nlink),
|
|
|
|
"uid": usm!(uid),
|
|
|
|
"gid": usm!(gid),
|
|
|
|
"rdev": usm!(rdev),
|
|
|
|
// TODO(kevinkassimo): *time_nsec requires BigInt.
|
|
|
|
// Probably should be treated as String if we need to add them.
|
|
|
|
"blksize": usm!(blksize),
|
|
|
|
"blocks": usm!(blocks),
|
|
|
|
});
|
2020-11-27 14:47:35 -05:00
|
|
|
json_val
|
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,
|
|
|
|
}
|
|
|
|
|
2020-08-28 11:08:24 -04:00
|
|
|
fn op_stat_sync(
|
2020-09-10 09:57:45 -04:00
|
|
|
state: &mut OpState,
|
2021-03-18 14:42:01 -04:00
|
|
|
args: StatArgs,
|
2020-06-01 14:20:47 -04:00
|
|
|
_zero_copy: &mut [ZeroCopyBuf],
|
2020-09-14 12:48:57 -04:00
|
|
|
) -> Result<Value, AnyError> {
|
2020-08-28 11:08:24 -04:00
|
|
|
let path = PathBuf::from(&args.path);
|
|
|
|
let lstat = args.lstat;
|
2021-03-17 17:45:12 -04:00
|
|
|
state.borrow::<Permissions>().read.check(&path)?;
|
2020-08-28 11:08:24 -04:00
|
|
|
debug!("op_stat_sync {} {}", path.display(), lstat);
|
|
|
|
let metadata = if lstat {
|
|
|
|
std::fs::symlink_metadata(&path)?
|
|
|
|
} else {
|
|
|
|
std::fs::metadata(&path)?
|
|
|
|
};
|
2020-11-27 14:47:35 -05:00
|
|
|
Ok(get_stat_json(metadata))
|
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,
|
2020-08-28 11:08:24 -04:00
|
|
|
_zero_copy: BufVec,
|
2020-09-14 12:48:57 -04:00
|
|
|
) -> Result<Value, 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
|
|
|
{
|
|
|
|
let state = state.borrow();
|
2021-03-17 17:45:12 -04:00
|
|
|
state.borrow::<Permissions>().read.check(&path)?;
|
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);
|
2019-08-14 11:03:02 -04:00
|
|
|
let metadata = if lstat {
|
2020-03-20 09:46:26 -04:00
|
|
|
std::fs::symlink_metadata(&path)?
|
2019-08-14 11:03:02 -04:00
|
|
|
} else {
|
2020-03-20 09:46:26 -04:00
|
|
|
std::fs::metadata(&path)?
|
2019-08-14 11:03:02 -04:00
|
|
|
};
|
2020-11-27 14:47:35 -05:00
|
|
|
Ok(get_stat_json(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
|
|
|
}
|
|
|
|
|
2019-11-26 03:40:57 -05:00
|
|
|
#[derive(Deserialize)]
|
|
|
|
#[serde(rename_all = "camelCase")]
|
2021-03-18 14:42:01 -04:00
|
|
|
pub struct RealpathArgs {
|
2019-11-26 03:40:57 -05:00
|
|
|
path: String,
|
|
|
|
}
|
|
|
|
|
2020-08-28 11:08:24 -04:00
|
|
|
fn op_realpath_sync(
|
2020-09-10 09:57:45 -04:00
|
|
|
state: &mut OpState,
|
2021-03-18 14:42:01 -04:00
|
|
|
args: RealpathArgs,
|
2020-06-01 14:20:47 -04:00
|
|
|
_zero_copy: &mut [ZeroCopyBuf],
|
2020-09-14 12:48:57 -04:00
|
|
|
) -> Result<Value, AnyError> {
|
2020-05-29 11:27:43 -04:00
|
|
|
let path = PathBuf::from(&args.path);
|
2020-01-20 09:45:44 -05:00
|
|
|
|
2020-09-19 19:17:35 -04:00
|
|
|
let permissions = state.borrow::<Permissions>();
|
2021-03-17 17:45:12 -04:00
|
|
|
permissions.read.check(&path)?;
|
2020-05-29 11:27:43 -04:00
|
|
|
if path.is_relative() {
|
2021-03-17 17:45:12 -04:00
|
|
|
permissions.read.check_blind(¤t_dir()?, "CWD")?;
|
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())?;
|
2020-08-28 11:08:24 -04:00
|
|
|
Ok(json!(realpath_str))
|
|
|
|
}
|
|
|
|
|
|
|
|
async fn op_realpath_async(
|
2020-09-10 09:57:45 -04:00
|
|
|
state: Rc<RefCell<OpState>>,
|
2021-03-18 14:42:01 -04:00
|
|
|
args: RealpathArgs,
|
2020-08-28 11:08:24 -04:00
|
|
|
_zero_copy: BufVec,
|
2020-09-14 12:48:57 -04:00
|
|
|
) -> Result<Value, AnyError> {
|
2020-08-28 11:08:24 -04:00
|
|
|
let path = PathBuf::from(&args.path);
|
|
|
|
|
2020-09-19 19:17:35 -04:00
|
|
|
{
|
|
|
|
let state = state.borrow();
|
|
|
|
let permissions = state.borrow::<Permissions>();
|
2021-03-17 17:45:12 -04:00
|
|
|
permissions.read.check(&path)?;
|
2020-09-19 19:17:35 -04:00
|
|
|
if path.is_relative() {
|
2021-03-17 17:45:12 -04:00
|
|
|
permissions.read.check_blind(¤t_dir()?, "CWD")?;
|
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())?;
|
2019-11-26 03:40:57 -05:00
|
|
|
Ok(json!(realpath_str))
|
|
|
|
})
|
2020-08-28 11:08:24 -04:00
|
|
|
.await
|
|
|
|
.unwrap()
|
2019-11-26 03:40:57 -05: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 ReadDirArgs {
|
2019-08-26 10:18:42 -04:00
|
|
|
path: String,
|
|
|
|
}
|
|
|
|
|
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-03-18 14:42:01 -04:00
|
|
|
args: ReadDirArgs,
|
2020-06-01 14:20:47 -04:00
|
|
|
_zero_copy: &mut [ZeroCopyBuf],
|
2020-09-14 12:48:57 -04:00
|
|
|
) -> Result<Value, AnyError> {
|
2020-08-28 11:08:24 -04:00
|
|
|
let path = PathBuf::from(&args.path);
|
|
|
|
|
2021-03-17 17:45:12 -04:00
|
|
|
state.borrow::<Permissions>().read.check(&path)?;
|
2020-08-28 11:08:24 -04:00
|
|
|
|
|
|
|
debug!("op_read_dir_sync {}", path.display());
|
|
|
|
let entries: Vec<_> = std::fs::read_dir(path)?
|
|
|
|
.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()) {
|
|
|
|
Some(json!({
|
|
|
|
"name": name,
|
2021-02-25 05:16:18 -05:00
|
|
|
"isFile": entry.file_type().map_or(false, |file_type| file_type.is_file()),
|
|
|
|
"isDirectory": entry.file_type().map_or(false, |file_type| file_type.is_dir()),
|
|
|
|
"isSymlink": entry.file_type().map_or(false, |file_type| file_type.is_symlink()),
|
2020-08-28 11:08:24 -04:00
|
|
|
}))
|
|
|
|
} else {
|
|
|
|
None
|
|
|
|
}
|
|
|
|
})
|
2021-02-25 05:16:18 -05:00
|
|
|
.collect();
|
2020-08-28 11:08:24 -04:00
|
|
|
|
|
|
|
Ok(json!({ "entries": entries }))
|
|
|
|
}
|
|
|
|
|
|
|
|
async fn op_read_dir_async(
|
2020-09-10 09:57:45 -04:00
|
|
|
state: Rc<RefCell<OpState>>,
|
2021-03-18 14:42:01 -04:00
|
|
|
args: ReadDirArgs,
|
2020-08-28 11:08:24 -04:00
|
|
|
_zero_copy: BufVec,
|
2020-09-14 12:48:57 -04:00
|
|
|
) -> Result<Value, AnyError> {
|
2020-05-29 11:27:43 -04:00
|
|
|
let path = PathBuf::from(&args.path);
|
2020-09-19 19:17:35 -04:00
|
|
|
{
|
|
|
|
let state = state.borrow();
|
2021-03-17 17:45:12 -04:00
|
|
|
state.borrow::<Permissions>().read.check(&path)?;
|
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());
|
2020-03-20 09:46:26 -04:00
|
|
|
let entries: Vec<_> = std::fs::read_dir(path)?
|
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()) {
|
|
|
|
Some(json!({
|
|
|
|
"name": name,
|
2021-02-25 05:16:18 -05:00
|
|
|
"isFile": entry.file_type().map_or(false, |file_type| file_type.is_file()),
|
|
|
|
"isDirectory": entry.file_type().map_or(false, |file_type| file_type.is_dir()),
|
|
|
|
"isSymlink": entry.file_type().map_or(false, |file_type| file_type.is_symlink()),
|
2020-04-29 16:00:31 -04:00
|
|
|
}))
|
2020-02-15 10:37:05 -05:00
|
|
|
} else {
|
|
|
|
None
|
|
|
|
}
|
2019-08-14 11:03:02 -04:00
|
|
|
})
|
2021-02-25 05:16:18 -05:00
|
|
|
.collect();
|
2019-08-14 11:03:02 -04:00
|
|
|
|
2019-08-26 10:18:42 -04:00
|
|
|
Ok(json!({ "entries": entries }))
|
2019-08-14 11:03:02 -04:00
|
|
|
})
|
2020-08-28 11:08:24 -04:00
|
|
|
.await
|
2021-02-25 05:16:18 -05:00
|
|
|
.unwrap()
|
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 RenameArgs {
|
2019-08-26 10:18:42 -04:00
|
|
|
oldpath: String,
|
|
|
|
newpath: String,
|
|
|
|
}
|
|
|
|
|
2020-08-28 11:08:24 -04:00
|
|
|
fn op_rename_sync(
|
2020-09-10 09:57:45 -04:00
|
|
|
state: &mut OpState,
|
2021-03-18 14:42:01 -04:00
|
|
|
args: RenameArgs,
|
2020-06-01 14:20:47 -04:00
|
|
|
_zero_copy: &mut [ZeroCopyBuf],
|
2020-09-14 12:48:57 -04:00
|
|
|
) -> Result<Value, AnyError> {
|
2020-08-28 11:08:24 -04:00
|
|
|
let oldpath = PathBuf::from(&args.oldpath);
|
|
|
|
let newpath = PathBuf::from(&args.newpath);
|
|
|
|
|
2020-09-19 19:17:35 -04:00
|
|
|
let permissions = state.borrow::<Permissions>();
|
2021-03-17 17:45:12 -04:00
|
|
|
permissions.read.check(&oldpath)?;
|
|
|
|
permissions.write.check(&oldpath)?;
|
|
|
|
permissions.write.check(&newpath)?;
|
2020-08-28 11:08:24 -04:00
|
|
|
debug!("op_rename_sync {} {}", oldpath.display(), newpath.display());
|
|
|
|
std::fs::rename(&oldpath, &newpath)?;
|
|
|
|
Ok(json!({}))
|
|
|
|
}
|
|
|
|
|
|
|
|
async fn op_rename_async(
|
2020-09-10 09:57:45 -04:00
|
|
|
state: Rc<RefCell<OpState>>,
|
2021-03-18 14:42:01 -04:00
|
|
|
args: RenameArgs,
|
2020-08-28 11:08:24 -04:00
|
|
|
_zero_copy: BufVec,
|
2020-09-14 12:48:57 -04:00
|
|
|
) -> Result<Value, AnyError> {
|
2020-05-29 11:27:43 -04:00
|
|
|
let oldpath = PathBuf::from(&args.oldpath);
|
|
|
|
let newpath = PathBuf::from(&args.newpath);
|
2020-09-10 09:57:45 -04:00
|
|
|
{
|
2020-09-19 19:17:35 -04:00
|
|
|
let state = state.borrow();
|
|
|
|
let permissions = state.borrow::<Permissions>();
|
2021-03-17 17:45:12 -04:00
|
|
|
permissions.read.check(&oldpath)?;
|
|
|
|
permissions.write.check(&oldpath)?;
|
|
|
|
permissions.write.check(&newpath)?;
|
2020-09-10 09:57:45 -04:00
|
|
|
}
|
2020-08-28 11:08:24 -04:00
|
|
|
tokio::task::spawn_blocking(move || {
|
|
|
|
debug!(
|
|
|
|
"op_rename_async {} {}",
|
|
|
|
oldpath.display(),
|
|
|
|
newpath.display()
|
|
|
|
);
|
2020-03-20 09:46:26 -04:00
|
|
|
std::fs::rename(&oldpath, &newpath)?;
|
2019-08-26 10:18:42 -04:00
|
|
|
Ok(json!({}))
|
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
|
|
|
}
|
|
|
|
|
2019-08-26 10:18:42 -04:00
|
|
|
#[derive(Deserialize)]
|
|
|
|
#[serde(rename_all = "camelCase")]
|
2021-03-18 14:42:01 -04:00
|
|
|
pub struct LinkArgs {
|
2020-03-20 09:46:26 -04:00
|
|
|
oldpath: String,
|
|
|
|
newpath: String,
|
2019-08-26 10:18:42 -04:00
|
|
|
}
|
|
|
|
|
2020-08-28 11:08:24 -04:00
|
|
|
fn op_link_sync(
|
2020-09-10 09:57:45 -04:00
|
|
|
state: &mut OpState,
|
2021-03-18 14:42:01 -04:00
|
|
|
args: LinkArgs,
|
2020-06-01 14:20:47 -04:00
|
|
|
_zero_copy: &mut [ZeroCopyBuf],
|
2020-09-14 12:48:57 -04:00
|
|
|
) -> Result<Value, AnyError> {
|
2020-08-28 11:08:24 -04:00
|
|
|
let oldpath = PathBuf::from(&args.oldpath);
|
|
|
|
let newpath = PathBuf::from(&args.newpath);
|
|
|
|
|
2020-09-19 19:17:35 -04:00
|
|
|
let permissions = state.borrow::<Permissions>();
|
2021-03-17 17:45:12 -04:00
|
|
|
permissions.read.check(&oldpath)?;
|
|
|
|
permissions.write.check(&oldpath)?;
|
|
|
|
permissions.read.check(&newpath)?;
|
|
|
|
permissions.write.check(&newpath)?;
|
2020-08-28 11:08:24 -04:00
|
|
|
|
|
|
|
debug!("op_link_sync {} {}", oldpath.display(), newpath.display());
|
|
|
|
std::fs::hard_link(&oldpath, &newpath)?;
|
|
|
|
Ok(json!({}))
|
|
|
|
}
|
|
|
|
|
|
|
|
async fn op_link_async(
|
2020-09-10 09:57:45 -04:00
|
|
|
state: Rc<RefCell<OpState>>,
|
2021-03-18 14:42:01 -04:00
|
|
|
args: LinkArgs,
|
2020-08-28 11:08:24 -04:00
|
|
|
_zero_copy: BufVec,
|
2020-09-14 12:48:57 -04:00
|
|
|
) -> Result<Value, AnyError> {
|
2020-05-29 11:27:43 -04:00
|
|
|
let oldpath = PathBuf::from(&args.oldpath);
|
|
|
|
let newpath = PathBuf::from(&args.newpath);
|
2019-08-14 11:03:02 -04:00
|
|
|
|
2020-09-19 19:17:35 -04:00
|
|
|
{
|
|
|
|
let state = state.borrow();
|
|
|
|
let permissions = state.borrow::<Permissions>();
|
2021-03-17 17:45:12 -04:00
|
|
|
permissions.read.check(&oldpath)?;
|
|
|
|
permissions.write.check(&oldpath)?;
|
|
|
|
permissions.read.check(&newpath)?;
|
|
|
|
permissions.write.check(&newpath)?;
|
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_link_async {} {}", oldpath.display(), newpath.display());
|
2020-03-20 09:46:26 -04:00
|
|
|
std::fs::hard_link(&oldpath, &newpath)?;
|
2019-08-26 10:18:42 -04:00
|
|
|
Ok(json!({}))
|
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
|
|
|
}
|
|
|
|
|
2019-08-26 10:18:42 -04:00
|
|
|
#[derive(Deserialize)]
|
|
|
|
#[serde(rename_all = "camelCase")]
|
2021-03-18 14:42:01 -04:00
|
|
|
pub struct SymlinkArgs {
|
2020-03-20 09:46:26 -04:00
|
|
|
oldpath: String,
|
|
|
|
newpath: String,
|
2020-05-18 18:46:02 -04:00
|
|
|
#[cfg(not(unix))]
|
|
|
|
options: Option<SymlinkOptions>,
|
|
|
|
}
|
|
|
|
|
|
|
|
#[cfg(not(unix))]
|
|
|
|
#[derive(Deserialize)]
|
|
|
|
#[serde(rename_all = "camelCase")]
|
2021-03-18 14:42:01 -04:00
|
|
|
pub struct SymlinkOptions {
|
2020-05-18 18:46:02 -04:00
|
|
|
_type: String,
|
2019-08-26 10:18:42 -04:00
|
|
|
}
|
|
|
|
|
2020-08-28 11:08:24 -04:00
|
|
|
fn op_symlink_sync(
|
2020-09-10 09:57:45 -04:00
|
|
|
state: &mut OpState,
|
2021-03-18 14:42:01 -04:00
|
|
|
args: SymlinkArgs,
|
2020-06-01 14:20:47 -04:00
|
|
|
_zero_copy: &mut [ZeroCopyBuf],
|
2020-09-14 12:48:57 -04:00
|
|
|
) -> Result<Value, AnyError> {
|
2020-08-28 11:08:24 -04:00
|
|
|
let oldpath = PathBuf::from(&args.oldpath);
|
|
|
|
let newpath = PathBuf::from(&args.newpath);
|
|
|
|
|
2021-03-17 17:45:12 -04:00
|
|
|
state.borrow::<Permissions>().write.check(&newpath)?;
|
2020-08-28 11:08:24 -04:00
|
|
|
|
|
|
|
debug!(
|
|
|
|
"op_symlink_sync {} {}",
|
|
|
|
oldpath.display(),
|
|
|
|
newpath.display()
|
|
|
|
);
|
|
|
|
#[cfg(unix)]
|
|
|
|
{
|
|
|
|
use std::os::unix::fs::symlink;
|
|
|
|
symlink(&oldpath, &newpath)?;
|
|
|
|
Ok(json!({}))
|
|
|
|
}
|
|
|
|
#[cfg(not(unix))]
|
|
|
|
{
|
|
|
|
use std::os::windows::fs::{symlink_dir, symlink_file};
|
|
|
|
|
|
|
|
match args.options {
|
|
|
|
Some(options) => match options._type.as_ref() {
|
|
|
|
"file" => symlink_file(&oldpath, &newpath)?,
|
|
|
|
"dir" => symlink_dir(&oldpath, &newpath)?,
|
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() {
|
|
|
|
symlink_file(&oldpath, &newpath)?
|
|
|
|
} else if metadata.is_dir() {
|
|
|
|
symlink_dir(&oldpath, &newpath)?
|
|
|
|
}
|
|
|
|
}
|
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
|
|
|
}
|
|
|
|
}
|
|
|
|
};
|
|
|
|
Ok(json!({}))
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
async fn op_symlink_async(
|
2020-09-10 09:57:45 -04:00
|
|
|
state: Rc<RefCell<OpState>>,
|
2021-03-18 14:42:01 -04:00
|
|
|
args: SymlinkArgs,
|
2020-08-28 11:08:24 -04:00
|
|
|
_zero_copy: BufVec,
|
2020-09-14 12:48:57 -04:00
|
|
|
) -> Result<Value, AnyError> {
|
2020-05-29 11:27:43 -04:00
|
|
|
let oldpath = PathBuf::from(&args.oldpath);
|
|
|
|
let newpath = PathBuf::from(&args.newpath);
|
2020-03-20 09:46:26 -04:00
|
|
|
|
2020-09-19 19:17:35 -04:00
|
|
|
{
|
|
|
|
let state = state.borrow();
|
2021-03-17 17:45:12 -04:00
|
|
|
state.borrow::<Permissions>().write.check(&newpath)?;
|
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 || {
|
|
|
|
debug!("op_symlink_async {} {}", 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;
|
|
|
|
symlink(&oldpath, &newpath)?;
|
|
|
|
Ok(json!({}))
|
|
|
|
}
|
|
|
|
#[cfg(not(unix))]
|
|
|
|
{
|
2020-05-18 18:46:02 -04:00
|
|
|
use std::os::windows::fs::{symlink_dir, symlink_file};
|
|
|
|
|
|
|
|
match args.options {
|
|
|
|
Some(options) => match options._type.as_ref() {
|
|
|
|
"file" => symlink_file(&oldpath, &newpath)?,
|
|
|
|
"dir" => symlink_dir(&oldpath, &newpath)?,
|
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() {
|
|
|
|
symlink_file(&oldpath, &newpath)?
|
|
|
|
} else if metadata.is_dir() {
|
|
|
|
symlink_dir(&oldpath, &newpath)?
|
|
|
|
}
|
|
|
|
}
|
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
|
|
|
}
|
|
|
|
}
|
|
|
|
};
|
|
|
|
Ok(json!({}))
|
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
|
|
|
}
|
|
|
|
|
2019-08-26 10:18:42 -04:00
|
|
|
#[derive(Deserialize)]
|
|
|
|
#[serde(rename_all = "camelCase")]
|
2021-03-18 14:42:01 -04:00
|
|
|
pub struct ReadLinkArgs {
|
2020-03-06 11:29:23 -05:00
|
|
|
path: String,
|
2019-08-26 10:18:42 -04:00
|
|
|
}
|
|
|
|
|
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-03-18 14:42:01 -04:00
|
|
|
args: ReadLinkArgs,
|
2020-06-01 14:20:47 -04:00
|
|
|
_zero_copy: &mut [ZeroCopyBuf],
|
2020-09-14 12:48:57 -04:00
|
|
|
) -> Result<Value, AnyError> {
|
2020-05-29 11:27:43 -04:00
|
|
|
let path = PathBuf::from(&args.path);
|
2019-08-26 10:18:42 -04:00
|
|
|
|
2021-03-17 17:45:12 -04:00
|
|
|
state.borrow::<Permissions>().read.check(&path)?;
|
2019-08-14 11:03:02 -04:00
|
|
|
|
2020-08-28 11:08:24 -04:00
|
|
|
debug!("op_read_link_value {}", path.display());
|
|
|
|
let target = std::fs::read_link(&path)?.into_os_string();
|
|
|
|
let targetstr = into_string(target)?;
|
|
|
|
Ok(json!(targetstr))
|
|
|
|
}
|
|
|
|
|
|
|
|
async fn op_read_link_async(
|
2020-09-10 09:57:45 -04:00
|
|
|
state: Rc<RefCell<OpState>>,
|
2021-03-18 14:42:01 -04:00
|
|
|
args: ReadLinkArgs,
|
2020-08-28 11:08:24 -04:00
|
|
|
_zero_copy: BufVec,
|
2020-09-14 12:48:57 -04:00
|
|
|
) -> Result<Value, AnyError> {
|
2020-08-28 11:08:24 -04:00
|
|
|
let path = PathBuf::from(&args.path);
|
2020-09-19 19:17:35 -04:00
|
|
|
{
|
|
|
|
let state = state.borrow();
|
2021-03-17 17:45:12 -04:00
|
|
|
state.borrow::<Permissions>().read.check(&path)?;
|
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());
|
2020-04-03 13:47:57 -04:00
|
|
|
let target = std::fs::read_link(&path)?.into_os_string();
|
|
|
|
let targetstr = into_string(target)?;
|
|
|
|
Ok(json!(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
|
|
|
}
|
|
|
|
|
2020-06-20 09:46:10 -04:00
|
|
|
#[derive(Deserialize)]
|
|
|
|
#[serde(rename_all = "camelCase")]
|
2021-03-18 14:42:01 -04:00
|
|
|
pub struct FtruncateArgs {
|
2021-03-19 13:25:37 -04:00
|
|
|
rid: ResourceId,
|
2020-06-20 09:46:10 -04:00
|
|
|
len: i32,
|
|
|
|
}
|
|
|
|
|
2020-08-28 11:08:24 -04:00
|
|
|
fn op_ftruncate_sync(
|
2020-09-10 09:57:45 -04:00
|
|
|
state: &mut OpState,
|
2021-03-18 14:42:01 -04:00
|
|
|
args: FtruncateArgs,
|
2020-06-20 09:46:10 -04:00
|
|
|
_zero_copy: &mut [ZeroCopyBuf],
|
2020-09-14 12:48:57 -04:00
|
|
|
) -> Result<Value, AnyError> {
|
2020-09-26 14:26:51 -04:00
|
|
|
super::check_unstable(state, "Deno.ftruncate");
|
2021-03-18 20:55:31 -04:00
|
|
|
let rid = args.rid;
|
2020-06-20 09:46:10 -04:00
|
|
|
let len = args.len as u64;
|
2021-01-14 23:32:27 -05:00
|
|
|
StdFileResource::with(state, rid, |r| match r {
|
2020-09-14 12:48:57 -04:00
|
|
|
Ok(std_file) => std_file.set_len(len).map_err(AnyError::from),
|
|
|
|
Err(_) => Err(type_error("cannot truncate this type of resource")),
|
2020-08-28 11:08:24 -04:00
|
|
|
})?;
|
|
|
|
Ok(json!({}))
|
|
|
|
}
|
2020-06-20 09:46:10 -04:00
|
|
|
|
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>>,
|
2021-03-18 14:42:01 -04:00
|
|
|
args: FtruncateArgs,
|
2020-08-28 11:08:24 -04:00
|
|
|
_zero_copy: BufVec,
|
2020-09-14 12:48:57 -04:00
|
|
|
) -> Result<Value, AnyError> {
|
2020-09-26 14:26:51 -04:00
|
|
|
super::check_unstable2(&state, "Deno.ftruncate");
|
2021-03-18 20:55:31 -04:00
|
|
|
let rid = args.rid;
|
2020-08-28 11:08:24 -04:00
|
|
|
let len = args.len as u64;
|
2021-01-08 11:55:17 -05:00
|
|
|
|
|
|
|
let resource = state
|
|
|
|
.borrow_mut()
|
|
|
|
.resource_table
|
2021-01-14 23:32:27 -05:00
|
|
|
.get::<StdFileResource>(rid)
|
2021-01-08 11:55:17 -05:00
|
|
|
.ok_or_else(bad_resource_id)?;
|
|
|
|
|
|
|
|
if resource.fs_file.is_none() {
|
|
|
|
return Err(bad_resource_id());
|
|
|
|
}
|
|
|
|
|
|
|
|
let mut fs_file = RcRef::map(&resource, |r| r.fs_file.as_ref().unwrap())
|
|
|
|
.borrow_mut()
|
|
|
|
.await;
|
|
|
|
|
|
|
|
(*fs_file).0.as_mut().unwrap().set_len(len).await?;
|
2020-08-28 11:08:24 -04:00
|
|
|
Ok(json!({}))
|
2020-06-20 09:46:10 -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 TruncateArgs {
|
2020-03-06 11:29:23 -05:00
|
|
|
path: String,
|
2019-08-26 10:18:42 -04:00
|
|
|
len: u64,
|
|
|
|
}
|
|
|
|
|
2020-08-28 11:08:24 -04:00
|
|
|
fn op_truncate_sync(
|
2020-09-10 09:57:45 -04:00
|
|
|
state: &mut OpState,
|
2021-03-18 14:42:01 -04:00
|
|
|
args: TruncateArgs,
|
2020-06-01 14:20:47 -04:00
|
|
|
_zero_copy: &mut [ZeroCopyBuf],
|
2020-09-14 12:48:57 -04:00
|
|
|
) -> Result<Value, AnyError> {
|
2020-08-28 11:08:24 -04:00
|
|
|
let path = PathBuf::from(&args.path);
|
|
|
|
let len = args.len;
|
|
|
|
|
2021-03-17 17:45:12 -04:00
|
|
|
state.borrow::<Permissions>().write.check(&path)?;
|
2020-08-28 11:08:24 -04:00
|
|
|
|
|
|
|
debug!("op_truncate_sync {} {}", path.display(), len);
|
|
|
|
let f = std::fs::OpenOptions::new().write(true).open(&path)?;
|
|
|
|
f.set_len(len)?;
|
|
|
|
Ok(json!({}))
|
|
|
|
}
|
|
|
|
|
|
|
|
async fn op_truncate_async(
|
2020-09-10 09:57:45 -04:00
|
|
|
state: Rc<RefCell<OpState>>,
|
2021-03-18 14:42:01 -04:00
|
|
|
args: TruncateArgs,
|
2020-08-28 11:08:24 -04:00
|
|
|
_zero_copy: BufVec,
|
2020-09-14 12:48:57 -04:00
|
|
|
) -> Result<Value, AnyError> {
|
2020-05-29 11:27:43 -04:00
|
|
|
let path = PathBuf::from(&args.path);
|
2019-08-26 10:18:42 -04:00
|
|
|
let len = args.len;
|
2020-09-19 19:17:35 -04:00
|
|
|
{
|
|
|
|
let state = state.borrow();
|
2021-03-17 17:45:12 -04:00
|
|
|
state.borrow::<Permissions>().write.check(&path)?;
|
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);
|
2020-03-20 09:46:26 -04:00
|
|
|
let f = std::fs::OpenOptions::new().write(true).open(&path)?;
|
2019-08-26 10:18:42 -04:00
|
|
|
f.set_len(len)?;
|
|
|
|
Ok(json!({}))
|
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 {
|
|
|
|
Some(ref p) => p.to_path_buf(),
|
|
|
|
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>,
|
|
|
|
}
|
|
|
|
|
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,
|
2020-06-01 14:20:47 -04:00
|
|
|
_zero_copy: &mut [ZeroCopyBuf],
|
2020-09-14 12:48:57 -04:00
|
|
|
) -> Result<Value, 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);
|
|
|
|
|
2020-09-19 19:17:35 -04:00
|
|
|
state
|
|
|
|
.borrow::<Permissions>()
|
2021-03-17 17:45:12 -04:00
|
|
|
.write
|
|
|
|
.check(dir.clone().unwrap_or_else(temp_dir).as_path())?;
|
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())?;
|
|
|
|
|
|
|
|
Ok(json!(path_str))
|
|
|
|
}
|
|
|
|
|
|
|
|
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,
|
2020-08-28 11:08:24 -04:00
|
|
|
_zero_copy: BufVec,
|
2020-09-14 12:48:57 -04:00
|
|
|
) -> Result<Value, 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
|
|
|
{
|
2020-09-19 19:17:35 -04:00
|
|
|
let state = state.borrow();
|
|
|
|
state
|
|
|
|
.borrow::<Permissions>()
|
2021-03-17 17:45:12 -04:00
|
|
|
.write
|
|
|
|
.check(dir.clone().unwrap_or_else(temp_dir).as_path())?;
|
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
|
|
|
|
|
|
|
Ok(json!(path_str))
|
|
|
|
})
|
2020-08-28 11:08:24 -04:00
|
|
|
.await
|
|
|
|
.unwrap()
|
2020-02-18 14:45:59 -05:00
|
|
|
}
|
|
|
|
|
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,
|
2020-06-01 14:20:47 -04:00
|
|
|
_zero_copy: &mut [ZeroCopyBuf],
|
2020-09-14 12:48:57 -04:00
|
|
|
) -> Result<Value, 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);
|
|
|
|
|
2020-09-19 19:17:35 -04:00
|
|
|
state
|
|
|
|
.borrow::<Permissions>()
|
2021-03-17 17:45:12 -04:00
|
|
|
.write
|
|
|
|
.check(dir.clone().unwrap_or_else(temp_dir).as_path())?;
|
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())?;
|
|
|
|
|
|
|
|
Ok(json!(path_str))
|
|
|
|
}
|
|
|
|
|
|
|
|
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,
|
2020-08-28 11:08:24 -04:00
|
|
|
_zero_copy: BufVec,
|
2020-09-14 12:48:57 -04:00
|
|
|
) -> Result<Value, 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
|
|
|
{
|
|
|
|
let state = state.borrow();
|
2020-09-19 19:17:35 -04:00
|
|
|
state
|
|
|
|
.borrow::<Permissions>()
|
2021-03-17 17:45:12 -04:00
|
|
|
.write
|
|
|
|
.check(dir.clone().unwrap_or_else(temp_dir).as_path())?;
|
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
|
|
|
|
|
|
|
Ok(json!(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
|
|
|
}
|
|
|
|
|
2020-08-31 14:29:43 -04:00
|
|
|
#[derive(Deserialize)]
|
|
|
|
#[serde(rename_all = "camelCase")]
|
2021-03-18 14:42:01 -04:00
|
|
|
pub struct FutimeArgs {
|
2021-03-19 13:25:37 -04:00
|
|
|
rid: ResourceId,
|
2020-09-01 10:03:07 -04:00
|
|
|
atime: (i64, u32),
|
|
|
|
mtime: (i64, u32),
|
2020-08-31 14:29:43 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
fn op_futime_sync(
|
2020-09-10 09:57:45 -04:00
|
|
|
state: &mut OpState,
|
2021-03-18 14:42:01 -04:00
|
|
|
args: FutimeArgs,
|
2020-08-31 14:29:43 -04:00
|
|
|
_zero_copy: &mut [ZeroCopyBuf],
|
2020-09-14 12:48:57 -04:00
|
|
|
) -> Result<Value, AnyError> {
|
2020-09-26 14:26:51 -04:00
|
|
|
super::check_unstable(state, "Deno.futimeSync");
|
2021-03-18 20:55:31 -04:00
|
|
|
let rid = args.rid;
|
2020-09-01 10:03:07 -04:00
|
|
|
let atime = filetime::FileTime::from_unix_time(args.atime.0, args.atime.1);
|
|
|
|
let mtime = filetime::FileTime::from_unix_time(args.mtime.0, args.mtime.1);
|
2020-08-31 14:29:43 -04:00
|
|
|
|
2021-01-14 23:32:27 -05:00
|
|
|
StdFileResource::with(state, rid, |r| match r {
|
2020-08-31 14:29:43 -04:00
|
|
|
Ok(std_file) => {
|
|
|
|
filetime::set_file_handle_times(std_file, Some(atime), Some(mtime))
|
2020-09-14 12:48:57 -04:00
|
|
|
.map_err(AnyError::from)
|
2020-08-31 14:29:43 -04:00
|
|
|
}
|
2020-09-14 12:48:57 -04:00
|
|
|
Err(_) => Err(type_error(
|
2020-08-31 14:29:43 -04:00
|
|
|
"cannot futime on this type of resource".to_string(),
|
|
|
|
)),
|
|
|
|
})?;
|
|
|
|
|
|
|
|
Ok(json!({}))
|
|
|
|
}
|
|
|
|
|
|
|
|
async fn op_futime_async(
|
2020-09-10 09:57:45 -04:00
|
|
|
state: Rc<RefCell<OpState>>,
|
2021-03-18 14:42:01 -04:00
|
|
|
args: FutimeArgs,
|
2020-08-31 14:29:43 -04:00
|
|
|
_zero_copy: BufVec,
|
2020-09-14 12:48:57 -04:00
|
|
|
) -> Result<Value, AnyError> {
|
2021-01-08 11:55:17 -05:00
|
|
|
super::check_unstable2(&state, "Deno.futime");
|
2021-03-18 20:55:31 -04:00
|
|
|
let rid = args.rid;
|
2020-09-01 10:03:07 -04:00
|
|
|
let atime = filetime::FileTime::from_unix_time(args.atime.0, args.atime.1);
|
|
|
|
let mtime = filetime::FileTime::from_unix_time(args.mtime.0, args.mtime.1);
|
2020-08-31 14:29:43 -04:00
|
|
|
|
2021-01-08 11:55:17 -05:00
|
|
|
let resource = state
|
|
|
|
.borrow_mut()
|
|
|
|
.resource_table
|
2021-01-14 23:32:27 -05:00
|
|
|
.get::<StdFileResource>(rid)
|
2021-01-08 11:55:17 -05:00
|
|
|
.ok_or_else(bad_resource_id)?;
|
|
|
|
|
|
|
|
if resource.fs_file.is_none() {
|
|
|
|
return Err(bad_resource_id());
|
|
|
|
}
|
|
|
|
|
|
|
|
let mut fs_file = RcRef::map(&resource, |r| r.fs_file.as_ref().unwrap())
|
|
|
|
.borrow_mut()
|
|
|
|
.await;
|
|
|
|
|
|
|
|
let std_file = (*fs_file)
|
|
|
|
.0
|
|
|
|
.as_mut()
|
|
|
|
.unwrap()
|
|
|
|
.try_clone()
|
|
|
|
.await?
|
|
|
|
.into_std()
|
|
|
|
.await;
|
|
|
|
|
|
|
|
tokio::task::spawn_blocking(move || {
|
|
|
|
filetime::set_file_handle_times(&std_file, Some(atime), Some(mtime))?;
|
|
|
|
Ok(json!({}))
|
|
|
|
})
|
|
|
|
.await
|
|
|
|
.unwrap()
|
2020-08-31 14:29:43 -04:00
|
|
|
}
|
|
|
|
|
2019-08-23 01:30:14 -04:00
|
|
|
#[derive(Deserialize)]
|
|
|
|
#[serde(rename_all = "camelCase")]
|
2021-03-18 14:42:01 -04:00
|
|
|
pub struct UtimeArgs {
|
2020-03-06 11:29:23 -05:00
|
|
|
path: String,
|
2020-09-01 10:03:07 -04:00
|
|
|
atime: (i64, u32),
|
|
|
|
mtime: (i64, u32),
|
2019-08-23 01:30:14 -04:00
|
|
|
}
|
|
|
|
|
2020-08-28 11:08:24 -04:00
|
|
|
fn op_utime_sync(
|
2020-09-10 09:57:45 -04:00
|
|
|
state: &mut OpState,
|
2021-03-18 14:42:01 -04:00
|
|
|
args: UtimeArgs,
|
2020-06-01 14:20:47 -04:00
|
|
|
_zero_copy: &mut [ZeroCopyBuf],
|
2020-09-14 12:48:57 -04:00
|
|
|
) -> Result<Value, AnyError> {
|
2020-09-26 14:26:51 -04:00
|
|
|
super::check_unstable(state, "Deno.utime");
|
2020-08-28 11:08:24 -04:00
|
|
|
|
|
|
|
let path = PathBuf::from(&args.path);
|
2020-09-01 10:03:07 -04:00
|
|
|
let atime = filetime::FileTime::from_unix_time(args.atime.0, args.atime.1);
|
|
|
|
let mtime = filetime::FileTime::from_unix_time(args.mtime.0, args.mtime.1);
|
2020-08-28 11:08:24 -04:00
|
|
|
|
2021-03-17 17:45:12 -04:00
|
|
|
state.borrow::<Permissions>().write.check(&path)?;
|
2020-08-31 22:24:17 -04:00
|
|
|
filetime::set_file_times(path, atime, mtime)?;
|
2020-08-28 11:08:24 -04:00
|
|
|
Ok(json!({}))
|
|
|
|
}
|
|
|
|
|
|
|
|
async fn op_utime_async(
|
2020-09-10 09:57:45 -04:00
|
|
|
state: Rc<RefCell<OpState>>,
|
2021-03-18 14:42:01 -04:00
|
|
|
args: UtimeArgs,
|
2020-08-28 11:08:24 -04:00
|
|
|
_zero_copy: BufVec,
|
2020-09-14 12:48:57 -04:00
|
|
|
) -> Result<Value, AnyError> {
|
2021-01-02 07:52:42 -05:00
|
|
|
super::check_unstable(&state.borrow(), "Deno.utime");
|
2020-04-27 21:14:47 -04:00
|
|
|
|
2020-05-29 11:27:43 -04:00
|
|
|
let path = PathBuf::from(&args.path);
|
2020-09-01 10:03:07 -04:00
|
|
|
let atime = filetime::FileTime::from_unix_time(args.atime.0, args.atime.1);
|
|
|
|
let mtime = filetime::FileTime::from_unix_time(args.mtime.0, args.mtime.1);
|
2020-03-20 09:46:26 -04:00
|
|
|
|
2021-03-17 17:45:12 -04:00
|
|
|
state.borrow().borrow::<Permissions>().write.check(&path)?;
|
2020-03-20 09:46:26 -04:00
|
|
|
|
2020-08-28 11:08:24 -04:00
|
|
|
tokio::task::spawn_blocking(move || {
|
2020-08-31 22:24:17 -04:00
|
|
|
filetime::set_file_times(path, atime, mtime)?;
|
2019-08-23 01:30:14 -04:00
|
|
|
Ok(json!({}))
|
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
|
|
|
}
|
|
|
|
|
2019-10-11 14:41:54 -04:00
|
|
|
fn op_cwd(
|
2020-09-10 09:57:45 -04:00
|
|
|
state: &mut OpState,
|
2019-08-26 10:18:42 -04:00
|
|
|
_args: Value,
|
2020-06-01 14:20:47 -04:00
|
|
|
_zero_copy: &mut [ZeroCopyBuf],
|
2020-09-14 12:48:57 -04:00
|
|
|
) -> Result<Value, AnyError> {
|
2020-03-20 09:46:26 -04:00
|
|
|
let path = current_dir()?;
|
2020-09-19 19:17:35 -04:00
|
|
|
state
|
|
|
|
.borrow::<Permissions>()
|
2021-03-17 17:45:12 -04:00
|
|
|
.read
|
|
|
|
.check_blind(&path, "CWD")?;
|
2020-04-03 13:47:57 -04:00
|
|
|
let path_str = into_string(path.into_os_string())?;
|
2020-08-28 11:08:24 -04:00
|
|
|
Ok(json!(path_str))
|
2019-08-14 11:03:02 -04:00
|
|
|
}
|