2020-01-02 15:13:47 -05:00
|
|
|
// Copyright 2018-2020 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
|
2019-08-23 01:30:14 -04:00
|
|
|
use super::dispatch_json::{blocking_json, Deserialize, JsonOp, Value};
|
2020-04-15 20:43:19 -04:00
|
|
|
use super::io::std_file_resource;
|
2020-03-11 18:19:24 -04:00
|
|
|
use super::io::{FileMetadata, StreamResource, StreamResourceHolder};
|
2020-02-23 14:51:29 -05:00
|
|
|
use crate::op_error::OpError;
|
2020-02-03 18:08:44 -05:00
|
|
|
use crate::ops::dispatch_json::JsonResult;
|
2020-02-08 14:34:31 -05:00
|
|
|
use crate::state::State;
|
2020-04-23 05:51:07 -04:00
|
|
|
use deno_core::CoreIsolate;
|
2020-05-29 17:41:39 -04:00
|
|
|
use deno_core::CoreIsolateState;
|
2020-04-15 20:43:19 -04:00
|
|
|
use deno_core::ZeroCopyBuf;
|
2020-03-07 07:20:27 -05:00
|
|
|
use futures::future::FutureExt;
|
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-03-20 09:46:26 -04:00
|
|
|
use std::path::{Path, PathBuf};
|
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-03-20 09:46:26 -04:00
|
|
|
use rand::{thread_rng, Rng};
|
2019-08-14 11:03:02 -04:00
|
|
|
|
2020-04-23 05:51:07 -04:00
|
|
|
pub fn init(i: &mut CoreIsolate, s: &State) {
|
2020-04-21 09:48:44 -04:00
|
|
|
i.register_op("op_open", s.stateful_json_op2(op_open));
|
|
|
|
i.register_op("op_seek", s.stateful_json_op2(op_seek));
|
2020-06-26 08:36:35 -04:00
|
|
|
i.register_op("op_fdatasync", s.stateful_json_op2(op_fdatasync));
|
2020-06-21 09:29:44 -04:00
|
|
|
i.register_op("op_fsync", s.stateful_json_op2(op_fsync));
|
2020-06-22 08:58:52 -04:00
|
|
|
i.register_op("op_fstat", s.stateful_json_op2(op_fstat));
|
2020-03-10 15:11:27 -04:00
|
|
|
i.register_op("op_umask", s.stateful_json_op(op_umask));
|
2020-02-25 09:14:27 -05:00
|
|
|
i.register_op("op_chdir", s.stateful_json_op(op_chdir));
|
|
|
|
i.register_op("op_mkdir", s.stateful_json_op(op_mkdir));
|
|
|
|
i.register_op("op_chmod", s.stateful_json_op(op_chmod));
|
|
|
|
i.register_op("op_chown", s.stateful_json_op(op_chown));
|
|
|
|
i.register_op("op_remove", s.stateful_json_op(op_remove));
|
|
|
|
i.register_op("op_copy_file", s.stateful_json_op(op_copy_file));
|
|
|
|
i.register_op("op_stat", s.stateful_json_op(op_stat));
|
|
|
|
i.register_op("op_realpath", s.stateful_json_op(op_realpath));
|
|
|
|
i.register_op("op_read_dir", s.stateful_json_op(op_read_dir));
|
|
|
|
i.register_op("op_rename", s.stateful_json_op(op_rename));
|
|
|
|
i.register_op("op_link", s.stateful_json_op(op_link));
|
|
|
|
i.register_op("op_symlink", s.stateful_json_op(op_symlink));
|
|
|
|
i.register_op("op_read_link", s.stateful_json_op(op_read_link));
|
2020-06-20 09:46:10 -04:00
|
|
|
i.register_op("op_ftruncate", s.stateful_json_op2(op_ftruncate));
|
2020-02-25 09:14:27 -05:00
|
|
|
i.register_op("op_truncate", s.stateful_json_op(op_truncate));
|
|
|
|
i.register_op("op_make_temp_dir", s.stateful_json_op(op_make_temp_dir));
|
|
|
|
i.register_op("op_make_temp_file", s.stateful_json_op(op_make_temp_file));
|
|
|
|
i.register_op("op_cwd", s.stateful_json_op(op_cwd));
|
|
|
|
i.register_op("op_utime", s.stateful_json_op(op_utime));
|
2019-10-11 14:41:54 -04:00
|
|
|
}
|
|
|
|
|
2020-04-03 13:47:57 -04:00
|
|
|
fn into_string(s: std::ffi::OsString) -> Result<String, OpError> {
|
|
|
|
s.into_string().map_err(|_| OpError::invalid_utf8())
|
|
|
|
}
|
|
|
|
|
2020-03-07 07:20:27 -05:00
|
|
|
#[derive(Deserialize)]
|
|
|
|
#[serde(rename_all = "camelCase")]
|
|
|
|
struct OpenArgs {
|
|
|
|
promise_id: Option<u64>,
|
|
|
|
path: String,
|
2020-04-24 18:45:55 -04:00
|
|
|
options: OpenOptions,
|
2020-03-16 15:02:41 -04:00
|
|
|
mode: Option<u32>,
|
2020-03-07 07:20:27 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
#[derive(Deserialize, Default, Debug)]
|
|
|
|
#[serde(rename_all = "camelCase")]
|
|
|
|
#[serde(default)]
|
|
|
|
struct OpenOptions {
|
|
|
|
read: bool,
|
|
|
|
write: bool,
|
|
|
|
create: bool,
|
|
|
|
truncate: bool,
|
|
|
|
append: bool,
|
|
|
|
create_new: bool,
|
|
|
|
}
|
|
|
|
|
|
|
|
fn op_open(
|
2020-05-29 17:41:39 -04:00
|
|
|
isolate_state: &mut CoreIsolateState,
|
2020-03-07 07:20:27 -05:00
|
|
|
state: &State,
|
|
|
|
args: Value,
|
2020-06-01 14:20:47 -04:00
|
|
|
_zero_copy: &mut [ZeroCopyBuf],
|
2020-03-07 07:20:27 -05:00
|
|
|
) -> Result<JsonOp, OpError> {
|
|
|
|
let args: OpenArgs = serde_json::from_value(args)?;
|
2020-05-29 11:27:43 -04:00
|
|
|
let path = Path::new(&args.path).to_path_buf();
|
2020-05-29 17:41:39 -04:00
|
|
|
let resource_table = isolate_state.resource_table.clone();
|
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-04-24 18:45:55 -04:00
|
|
|
let options = args.options;
|
|
|
|
if options.read {
|
|
|
|
state.check_read(&path)?;
|
|
|
|
}
|
2020-03-07 07:20:27 -05:00
|
|
|
|
2020-04-24 18:45:55 -04:00
|
|
|
if options.write || options.append {
|
|
|
|
state.check_write(&path)?;
|
|
|
|
}
|
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
|
|
|
|
|
|
|
let is_sync = args.promise_id.is_none();
|
|
|
|
|
2020-04-15 20:43:19 -04:00
|
|
|
if is_sync {
|
|
|
|
let std_file = open_options.open(path)?;
|
|
|
|
let tokio_file = tokio::fs::File::from_std(std_file);
|
2020-04-21 09:48:44 -04:00
|
|
|
let mut resource_table = resource_table.borrow_mut();
|
|
|
|
let rid = resource_table.add(
|
2020-03-07 07:20:27 -05:00
|
|
|
"fsFile",
|
2020-04-15 20:43:19 -04:00
|
|
|
Box::new(StreamResourceHolder::new(StreamResource::FsFile(Some((
|
|
|
|
tokio_file,
|
2020-03-11 18:19:24 -04:00
|
|
|
FileMetadata::default(),
|
2020-04-15 20:43:19 -04:00
|
|
|
))))),
|
2020-03-07 07:20:27 -05:00
|
|
|
);
|
2020-04-15 20:43:19 -04:00
|
|
|
Ok(JsonOp::Sync(json!(rid)))
|
2020-03-07 07:20:27 -05:00
|
|
|
} else {
|
2020-04-15 20:43:19 -04:00
|
|
|
let fut = async move {
|
|
|
|
let tokio_file = tokio::fs::OpenOptions::from(open_options)
|
|
|
|
.open(path)
|
|
|
|
.await?;
|
2020-04-21 09:48:44 -04:00
|
|
|
let mut resource_table = resource_table.borrow_mut();
|
|
|
|
let rid = resource_table.add(
|
2020-04-15 20:43:19 -04:00
|
|
|
"fsFile",
|
|
|
|
Box::new(StreamResourceHolder::new(StreamResource::FsFile(Some((
|
|
|
|
tokio_file,
|
|
|
|
FileMetadata::default(),
|
|
|
|
))))),
|
|
|
|
);
|
|
|
|
Ok(json!(rid))
|
|
|
|
};
|
2020-03-07 07:20:27 -05:00
|
|
|
Ok(JsonOp::Async(fut.boxed_local()))
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
#[derive(Deserialize)]
|
|
|
|
#[serde(rename_all = "camelCase")]
|
|
|
|
struct SeekArgs {
|
|
|
|
promise_id: Option<u64>,
|
|
|
|
rid: i32,
|
|
|
|
offset: i32,
|
|
|
|
whence: i32,
|
|
|
|
}
|
|
|
|
|
|
|
|
fn op_seek(
|
2020-05-29 17:41:39 -04:00
|
|
|
isolate_state: &mut CoreIsolateState,
|
2020-04-21 09:48:44 -04:00
|
|
|
_state: &State,
|
2020-03-07 07:20:27 -05:00
|
|
|
args: Value,
|
2020-06-01 14:20:47 -04:00
|
|
|
_zero_copy: &mut [ZeroCopyBuf],
|
2020-03-07 07:20:27 -05:00
|
|
|
) -> Result<JsonOp, OpError> {
|
2020-04-15 20:43:19 -04:00
|
|
|
use std::io::{Seek, SeekFrom};
|
2020-03-07 07:20:27 -05:00
|
|
|
let args: SeekArgs = serde_json::from_value(args)?;
|
|
|
|
let rid = args.rid as u32;
|
|
|
|
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),
|
|
|
|
1 => SeekFrom::Current(i64::from(offset)),
|
|
|
|
2 => SeekFrom::End(i64::from(offset)),
|
|
|
|
_ => {
|
|
|
|
return Err(OpError::type_error(format!(
|
|
|
|
"Invalid seek mode: {}",
|
|
|
|
whence
|
|
|
|
)));
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2020-05-29 17:41:39 -04:00
|
|
|
let resource_table = isolate_state.resource_table.clone();
|
2020-04-03 13:45:44 -04:00
|
|
|
let is_sync = args.promise_id.is_none();
|
2020-03-07 07:20:27 -05:00
|
|
|
|
2020-04-03 13:45:44 -04:00
|
|
|
if is_sync {
|
2020-04-21 09:48:44 -04:00
|
|
|
let mut resource_table = resource_table.borrow_mut();
|
|
|
|
let pos = std_file_resource(&mut resource_table, rid, |r| match r {
|
2020-04-15 20:43:19 -04:00
|
|
|
Ok(std_file) => std_file.seek(seek_from).map_err(OpError::from),
|
|
|
|
Err(_) => Err(OpError::type_error(
|
|
|
|
"cannot seek on this type of resource".to_string(),
|
|
|
|
)),
|
|
|
|
})?;
|
|
|
|
Ok(JsonOp::Sync(json!(pos)))
|
2020-03-07 07:20:27 -05:00
|
|
|
} else {
|
2020-04-15 20:43:19 -04:00
|
|
|
// TODO(ry) This is a fake async op. We need to use poll_fn,
|
|
|
|
// tokio::fs::File::start_seek and tokio::fs::File::poll_complete
|
|
|
|
let fut = async move {
|
2020-04-21 09:48:44 -04:00
|
|
|
let mut resource_table = resource_table.borrow_mut();
|
|
|
|
let pos = std_file_resource(&mut resource_table, rid, |r| match r {
|
2020-04-15 20:43:19 -04:00
|
|
|
Ok(std_file) => std_file.seek(seek_from).map_err(OpError::from),
|
|
|
|
Err(_) => Err(OpError::type_error(
|
|
|
|
"cannot seek on this type of resource".to_string(),
|
|
|
|
)),
|
|
|
|
})?;
|
|
|
|
Ok(json!(pos))
|
|
|
|
};
|
2020-03-07 07:20:27 -05:00
|
|
|
Ok(JsonOp::Async(fut.boxed_local()))
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-06-26 08:36:35 -04:00
|
|
|
#[derive(Deserialize)]
|
|
|
|
#[serde(rename_all = "camelCase")]
|
|
|
|
struct FdatasyncArgs {
|
|
|
|
promise_id: Option<u64>,
|
|
|
|
rid: i32,
|
|
|
|
}
|
|
|
|
|
|
|
|
fn op_fdatasync(
|
|
|
|
isolate_state: &mut CoreIsolateState,
|
|
|
|
state: &State,
|
|
|
|
args: Value,
|
|
|
|
_zero_copy: &mut [ZeroCopyBuf],
|
|
|
|
) -> Result<JsonOp, OpError> {
|
|
|
|
state.check_unstable("Deno.fdatasync");
|
|
|
|
let args: FdatasyncArgs = serde_json::from_value(args)?;
|
|
|
|
let rid = args.rid as u32;
|
|
|
|
|
|
|
|
let resource_table = isolate_state.resource_table.clone();
|
|
|
|
let is_sync = args.promise_id.is_none();
|
|
|
|
|
|
|
|
if is_sync {
|
|
|
|
let mut resource_table = resource_table.borrow_mut();
|
|
|
|
std_file_resource(&mut resource_table, rid, |r| match r {
|
|
|
|
Ok(std_file) => std_file.sync_data().map_err(OpError::from),
|
|
|
|
Err(_) => Err(OpError::type_error(
|
|
|
|
"cannot sync this type of resource".to_string(),
|
|
|
|
)),
|
|
|
|
})?;
|
|
|
|
Ok(JsonOp::Sync(json!({})))
|
|
|
|
} else {
|
|
|
|
let fut = async move {
|
|
|
|
let mut resource_table = resource_table.borrow_mut();
|
|
|
|
std_file_resource(&mut resource_table, rid, |r| match r {
|
|
|
|
Ok(std_file) => std_file.sync_data().map_err(OpError::from),
|
|
|
|
Err(_) => Err(OpError::type_error(
|
|
|
|
"cannot sync this type of resource".to_string(),
|
|
|
|
)),
|
|
|
|
})?;
|
|
|
|
Ok(json!({}))
|
|
|
|
};
|
|
|
|
Ok(JsonOp::Async(fut.boxed_local()))
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-06-21 09:29:44 -04:00
|
|
|
#[derive(Deserialize)]
|
|
|
|
#[serde(rename_all = "camelCase")]
|
|
|
|
struct FsyncArgs {
|
|
|
|
promise_id: Option<u64>,
|
|
|
|
rid: i32,
|
|
|
|
}
|
|
|
|
|
|
|
|
fn op_fsync(
|
|
|
|
isolate_state: &mut CoreIsolateState,
|
|
|
|
state: &State,
|
|
|
|
args: Value,
|
|
|
|
_zero_copy: &mut [ZeroCopyBuf],
|
|
|
|
) -> Result<JsonOp, OpError> {
|
|
|
|
state.check_unstable("Deno.fsync");
|
|
|
|
let args: FsyncArgs = serde_json::from_value(args)?;
|
|
|
|
let rid = args.rid as u32;
|
|
|
|
|
|
|
|
let resource_table = isolate_state.resource_table.clone();
|
|
|
|
let is_sync = args.promise_id.is_none();
|
|
|
|
|
|
|
|
if is_sync {
|
|
|
|
let mut resource_table = resource_table.borrow_mut();
|
|
|
|
std_file_resource(&mut resource_table, rid, |r| match r {
|
|
|
|
Ok(std_file) => std_file.sync_all().map_err(OpError::from),
|
|
|
|
Err(_) => Err(OpError::type_error(
|
|
|
|
"cannot sync this type of resource".to_string(),
|
|
|
|
)),
|
|
|
|
})?;
|
|
|
|
Ok(JsonOp::Sync(json!({})))
|
|
|
|
} else {
|
|
|
|
let fut = async move {
|
|
|
|
let mut resource_table = resource_table.borrow_mut();
|
|
|
|
std_file_resource(&mut resource_table, rid, |r| match r {
|
|
|
|
Ok(std_file) => std_file.sync_all().map_err(OpError::from),
|
|
|
|
Err(_) => Err(OpError::type_error(
|
|
|
|
"cannot sync this type of resource".to_string(),
|
|
|
|
)),
|
|
|
|
})?;
|
|
|
|
Ok(json!({}))
|
|
|
|
};
|
|
|
|
Ok(JsonOp::Async(fut.boxed_local()))
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-06-22 08:58:52 -04:00
|
|
|
#[derive(Deserialize)]
|
|
|
|
#[serde(rename_all = "camelCase")]
|
|
|
|
struct FstatArgs {
|
|
|
|
promise_id: Option<u64>,
|
|
|
|
rid: i32,
|
|
|
|
}
|
|
|
|
|
|
|
|
fn op_fstat(
|
|
|
|
isolate_state: &mut CoreIsolateState,
|
|
|
|
state: &State,
|
|
|
|
args: Value,
|
|
|
|
_zero_copy: &mut [ZeroCopyBuf],
|
|
|
|
) -> Result<JsonOp, OpError> {
|
|
|
|
state.check_unstable("Deno.fstat");
|
|
|
|
let args: FstatArgs = serde_json::from_value(args)?;
|
|
|
|
let rid = args.rid as u32;
|
|
|
|
|
|
|
|
let resource_table = isolate_state.resource_table.clone();
|
|
|
|
let is_sync = args.promise_id.is_none();
|
|
|
|
|
|
|
|
if is_sync {
|
|
|
|
let mut resource_table = resource_table.borrow_mut();
|
|
|
|
let metadata = std_file_resource(&mut resource_table, rid, |r| match r {
|
|
|
|
Ok(std_file) => std_file.metadata().map_err(OpError::from),
|
|
|
|
Err(_) => Err(OpError::type_error(
|
|
|
|
"cannot stat this type of resource".to_string(),
|
|
|
|
)),
|
|
|
|
})?;
|
|
|
|
Ok(JsonOp::Sync(get_stat_json(metadata).unwrap()))
|
|
|
|
} else {
|
|
|
|
let fut = async move {
|
|
|
|
let mut resource_table = resource_table.borrow_mut();
|
|
|
|
let metadata =
|
|
|
|
std_file_resource(&mut resource_table, rid, |r| match r {
|
|
|
|
Ok(std_file) => std_file.metadata().map_err(OpError::from),
|
|
|
|
Err(_) => Err(OpError::type_error(
|
|
|
|
"cannot stat this type of resource".to_string(),
|
|
|
|
)),
|
|
|
|
})?;
|
|
|
|
Ok(get_stat_json(metadata).unwrap())
|
|
|
|
};
|
|
|
|
Ok(JsonOp::Async(fut.boxed_local()))
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-03-10 15:11:27 -04:00
|
|
|
#[derive(Deserialize)]
|
|
|
|
struct UmaskArgs {
|
|
|
|
mask: Option<u32>,
|
|
|
|
}
|
|
|
|
|
|
|
|
fn op_umask(
|
2020-04-27 16:07:57 -04:00
|
|
|
state: &State,
|
2020-03-10 15:11:27 -04:00
|
|
|
args: Value,
|
2020-06-01 14:20:47 -04:00
|
|
|
_zero_copy: &mut [ZeroCopyBuf],
|
2020-03-10 15:11:27 -04:00
|
|
|
) -> Result<JsonOp, OpError> {
|
2020-04-27 16:07:57 -04:00
|
|
|
state.check_unstable("Deno.umask");
|
2020-03-10 15:11:27 -04:00
|
|
|
let args: UmaskArgs = serde_json::from_value(args)?;
|
|
|
|
// 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-03-27 16:09:51 -04:00
|
|
|
Err(OpError::not_implemented())
|
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
|
|
|
|
};
|
|
|
|
Ok(JsonOp::Sync(json!(r.bits() as u32)))
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-08-26 10:18:42 -04:00
|
|
|
#[derive(Deserialize)]
|
|
|
|
struct ChdirArgs {
|
|
|
|
directory: String,
|
|
|
|
}
|
|
|
|
|
2019-10-11 14:41:54 -04:00
|
|
|
fn op_chdir(
|
2020-04-24 19:55:33 -04:00
|
|
|
state: &State,
|
2019-08-26 10:18:42 -04:00
|
|
|
args: Value,
|
2020-06-01 14:20:47 -04:00
|
|
|
_zero_copy: &mut [ZeroCopyBuf],
|
2020-02-23 14:51:29 -05:00
|
|
|
) -> Result<JsonOp, OpError> {
|
2019-08-26 10:18:42 -04:00
|
|
|
let args: ChdirArgs = serde_json::from_value(args)?;
|
2020-04-24 19:55:33 -04:00
|
|
|
let d = PathBuf::from(&args.directory);
|
2020-05-02 18:33:43 -04:00
|
|
|
state.check_read(&d)?;
|
2020-04-24 19:55:33 -04:00
|
|
|
set_current_dir(&d)?;
|
2019-08-26 10:18:42 -04:00
|
|
|
Ok(JsonOp::Sync(json!({})))
|
|
|
|
}
|
|
|
|
|
|
|
|
#[derive(Deserialize)]
|
|
|
|
#[serde(rename_all = "camelCase")]
|
|
|
|
struct MkdirArgs {
|
|
|
|
promise_id: Option<u64>,
|
|
|
|
path: String,
|
|
|
|
recursive: bool,
|
2020-03-11 16:14:23 -04:00
|
|
|
mode: Option<u32>,
|
2019-08-14 11:03:02 -04:00
|
|
|
}
|
|
|
|
|
2019-10-11 14:41:54 -04:00
|
|
|
fn op_mkdir(
|
2020-02-08 14:34:31 -05:00
|
|
|
state: &State,
|
2019-08-26 10:18:42 -04:00
|
|
|
args: Value,
|
2020-06-01 14:20:47 -04:00
|
|
|
_zero_copy: &mut [ZeroCopyBuf],
|
2020-02-23 14:51:29 -05:00
|
|
|
) -> Result<JsonOp, OpError> {
|
2019-08-26 10:18:42 -04:00
|
|
|
let args: MkdirArgs = serde_json::from_value(args)?;
|
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;
|
2019-08-14 11:03:02 -04:00
|
|
|
|
2020-01-20 09:45:44 -05:00
|
|
|
state.check_write(&path)?;
|
2019-08-14 11:03:02 -04:00
|
|
|
|
2019-08-26 10:18:42 -04:00
|
|
|
let is_sync = args.promise_id.is_none();
|
|
|
|
blocking_json(is_sync, 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
|
|
|
})
|
|
|
|
}
|
|
|
|
|
2019-08-26 10:18:42 -04:00
|
|
|
#[derive(Deserialize)]
|
|
|
|
#[serde(rename_all = "camelCase")]
|
|
|
|
struct ChmodArgs {
|
|
|
|
promise_id: Option<u64>,
|
|
|
|
path: String,
|
|
|
|
mode: u32,
|
|
|
|
}
|
|
|
|
|
2019-10-11 14:41:54 -04:00
|
|
|
fn op_chmod(
|
2020-02-08 14:34:31 -05:00
|
|
|
state: &State,
|
2019-08-26 10:18:42 -04:00
|
|
|
args: Value,
|
2020-06-01 14:20:47 -04:00
|
|
|
_zero_copy: &mut [ZeroCopyBuf],
|
2020-02-23 14:51:29 -05:00
|
|
|
) -> Result<JsonOp, OpError> {
|
2019-08-26 10:18:42 -04:00
|
|
|
let args: ChmodArgs = serde_json::from_value(args)?;
|
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
|
|
|
|
2020-01-20 09:45:44 -05:00
|
|
|
state.check_write(&path)?;
|
2019-08-14 11:03:02 -04:00
|
|
|
|
2019-08-26 10:18:42 -04:00
|
|
|
let is_sync = args.promise_id.is_none();
|
|
|
|
blocking_json(is_sync, move || {
|
2020-03-20 09:46:26 -04:00
|
|
|
debug!("op_chmod {} {: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-03-27 16:09:51 -04:00
|
|
|
Err(OpError::not_implemented())
|
2019-08-14 11:03:02 -04:00
|
|
|
}
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
2019-08-26 10:18:42 -04:00
|
|
|
#[derive(Deserialize)]
|
|
|
|
#[serde(rename_all = "camelCase")]
|
|
|
|
struct ChownArgs {
|
|
|
|
promise_id: Option<u64>,
|
|
|
|
path: String,
|
|
|
|
uid: u32,
|
|
|
|
gid: u32,
|
|
|
|
}
|
|
|
|
|
2019-10-11 14:41:54 -04:00
|
|
|
fn op_chown(
|
2020-02-08 14:34:31 -05:00
|
|
|
state: &State,
|
2019-08-26 10:18:42 -04:00
|
|
|
args: Value,
|
2020-06-01 14:20:47 -04:00
|
|
|
_zero_copy: &mut [ZeroCopyBuf],
|
2020-02-23 14:51:29 -05:00
|
|
|
) -> Result<JsonOp, OpError> {
|
2019-08-26 10:18:42 -04:00
|
|
|
let args: ChownArgs = serde_json::from_value(args)?;
|
2020-05-29 11:27:43 -04:00
|
|
|
let path = Path::new(&args.path).to_path_buf();
|
2019-08-26 10:18:42 -04:00
|
|
|
|
2020-01-20 09:45:44 -05:00
|
|
|
state.check_write(&path)?;
|
2019-08-26 10:18:42 -04:00
|
|
|
|
|
|
|
let is_sync = args.promise_id.is_none();
|
|
|
|
blocking_json(is_sync, move || {
|
2020-03-20 09:46:26 -04:00
|
|
|
debug!("op_chown {} {} {}", path.display(), args.uid, args.gid);
|
|
|
|
#[cfg(unix)]
|
|
|
|
{
|
|
|
|
use nix::unistd::{chown, Gid, Uid};
|
|
|
|
let nix_uid = Uid::from_raw(args.uid);
|
|
|
|
let nix_gid = Gid::from_raw(args.gid);
|
|
|
|
chown(&path, Option::Some(nix_uid), Option::Some(nix_gid))?;
|
|
|
|
Ok(json!({}))
|
|
|
|
}
|
|
|
|
// TODO Implement chown for Windows
|
|
|
|
#[cfg(not(unix))]
|
|
|
|
{
|
|
|
|
// Still check file/dir exists on Windows
|
|
|
|
let _metadata = std::fs::metadata(&path)?;
|
2020-03-27 16:09:51 -04:00
|
|
|
Err(OpError::not_implemented())
|
2020-03-20 09:46:26 -04:00
|
|
|
}
|
2019-08-14 11:03:02 -04:00
|
|
|
})
|
|
|
|
}
|
|
|
|
|
2019-08-26 10:18:42 -04:00
|
|
|
#[derive(Deserialize)]
|
|
|
|
#[serde(rename_all = "camelCase")]
|
|
|
|
struct RemoveArgs {
|
|
|
|
promise_id: Option<u64>,
|
|
|
|
path: String,
|
|
|
|
recursive: bool,
|
|
|
|
}
|
|
|
|
|
2019-10-11 14:41:54 -04:00
|
|
|
fn op_remove(
|
2020-02-08 14:34:31 -05:00
|
|
|
state: &State,
|
2019-08-26 10:18:42 -04:00
|
|
|
args: Value,
|
2020-06-01 14:20:47 -04:00
|
|
|
_zero_copy: &mut [ZeroCopyBuf],
|
2020-02-23 14:51:29 -05:00
|
|
|
) -> Result<JsonOp, OpError> {
|
2019-08-26 10:18:42 -04:00
|
|
|
let args: RemoveArgs = serde_json::from_value(args)?;
|
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-01-20 09:45:44 -05:00
|
|
|
state.check_write(&path)?;
|
2019-08-14 11:03:02 -04:00
|
|
|
|
2019-08-26 10:18:42 -04:00
|
|
|
let is_sync = args.promise_id.is_none();
|
|
|
|
blocking_json(is_sync, 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-03-20 09:46:26 -04:00
|
|
|
debug!("op_remove {} {}", 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
|
|
|
})
|
|
|
|
}
|
|
|
|
|
2019-08-26 10:18:42 -04:00
|
|
|
#[derive(Deserialize)]
|
|
|
|
#[serde(rename_all = "camelCase")]
|
|
|
|
struct CopyFileArgs {
|
|
|
|
promise_id: Option<u64>,
|
|
|
|
from: String,
|
|
|
|
to: String,
|
|
|
|
}
|
|
|
|
|
2019-10-11 14:41:54 -04:00
|
|
|
fn op_copy_file(
|
2020-02-08 14:34:31 -05:00
|
|
|
state: &State,
|
2019-08-26 10:18:42 -04:00
|
|
|
args: Value,
|
2020-06-01 14:20:47 -04:00
|
|
|
_zero_copy: &mut [ZeroCopyBuf],
|
2020-02-23 14:51:29 -05:00
|
|
|
) -> Result<JsonOp, OpError> {
|
2019-08-26 10:18:42 -04:00
|
|
|
let args: CopyFileArgs = serde_json::from_value(args)?;
|
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-01-20 09:45:44 -05:00
|
|
|
state.check_read(&from)?;
|
|
|
|
state.check_write(&to)?;
|
2019-08-14 11:03:02 -04:00
|
|
|
|
|
|
|
debug!("op_copy_file {} {}", from.display(), to.display());
|
2019-08-26 10:18:42 -04:00
|
|
|
let is_sync = args.promise_id.is_none();
|
|
|
|
blocking_json(is_sync, 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-02-23 14:51:29 -05:00
|
|
|
return Err(OpError::not_found("File not found".to_string()));
|
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-04-27 14:09:56 -04:00
|
|
|
fn to_msec(maybe_time: Result<SystemTime, io::Error>) -> serde_json::Value {
|
|
|
|
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)
|
|
|
|
.map(serde_json::Value::Number)
|
|
|
|
.unwrap_or(serde_json::Value::Null)
|
|
|
|
}
|
|
|
|
Err(_) => serde_json::Value::Null,
|
|
|
|
}
|
2019-08-14 11:03:02 -04:00
|
|
|
}
|
|
|
|
|
2020-01-16 09:46:32 -05:00
|
|
|
#[inline(always)]
|
2020-04-29 16:00:31 -04:00
|
|
|
fn get_stat_json(metadata: std::fs::Metadata) -> JsonResult {
|
2020-01-16 09:46:32 -05:00
|
|
|
// Unix stat member (number types only). 0 if not on unix.
|
|
|
|
macro_rules! usm {
|
|
|
|
($member: ident) => {{
|
|
|
|
#[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),
|
|
|
|
});
|
|
|
|
Ok(json_val)
|
2019-08-14 11:03:02 -04:00
|
|
|
}
|
|
|
|
|
2019-08-26 10:18:42 -04:00
|
|
|
#[derive(Deserialize)]
|
|
|
|
#[serde(rename_all = "camelCase")]
|
|
|
|
struct StatArgs {
|
|
|
|
promise_id: Option<u64>,
|
2020-03-06 11:29:23 -05:00
|
|
|
path: String,
|
2019-08-26 10:18:42 -04:00
|
|
|
lstat: bool,
|
|
|
|
}
|
|
|
|
|
2019-10-11 14:41:54 -04:00
|
|
|
fn op_stat(
|
2020-02-08 14:34:31 -05:00
|
|
|
state: &State,
|
2019-08-26 10:18:42 -04:00
|
|
|
args: Value,
|
2020-06-01 14:20:47 -04:00
|
|
|
_zero_copy: &mut [ZeroCopyBuf],
|
2020-02-23 14:51:29 -05:00
|
|
|
) -> Result<JsonOp, OpError> {
|
2019-08-26 10:18:42 -04:00
|
|
|
let args: StatArgs = serde_json::from_value(args)?;
|
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-03-06 11:29:23 -05:00
|
|
|
state.check_read(&path)?;
|
2019-08-14 11:03:02 -04:00
|
|
|
|
2019-08-26 10:18:42 -04:00
|
|
|
let is_sync = args.promise_id.is_none();
|
|
|
|
blocking_json(is_sync, move || {
|
2020-03-06 11:29:23 -05:00
|
|
|
debug!("op_stat {} {}", 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-04-29 16:00:31 -04:00
|
|
|
get_stat_json(metadata)
|
2019-08-14 11:03:02 -04:00
|
|
|
})
|
|
|
|
}
|
|
|
|
|
2019-11-26 03:40:57 -05:00
|
|
|
#[derive(Deserialize)]
|
|
|
|
#[serde(rename_all = "camelCase")]
|
|
|
|
struct RealpathArgs {
|
|
|
|
promise_id: Option<u64>,
|
|
|
|
path: String,
|
|
|
|
}
|
|
|
|
|
|
|
|
fn op_realpath(
|
2020-02-08 14:34:31 -05:00
|
|
|
state: &State,
|
2019-11-26 03:40:57 -05:00
|
|
|
args: Value,
|
2020-06-01 14:20:47 -04:00
|
|
|
_zero_copy: &mut [ZeroCopyBuf],
|
2020-02-23 14:51:29 -05:00
|
|
|
) -> Result<JsonOp, OpError> {
|
2019-11-26 03:40:57 -05:00
|
|
|
let args: RealpathArgs = serde_json::from_value(args)?;
|
2020-05-29 11:27:43 -04:00
|
|
|
let path = PathBuf::from(&args.path);
|
2020-01-20 09:45:44 -05:00
|
|
|
|
|
|
|
state.check_read(&path)?;
|
2020-05-29 11:27:43 -04:00
|
|
|
if path.is_relative() {
|
|
|
|
state.check_read_blind(¤t_dir()?, "CWD")?;
|
|
|
|
}
|
2020-01-20 09:45:44 -05:00
|
|
|
|
2019-11-26 03:40:57 -05:00
|
|
|
let is_sync = args.promise_id.is_none();
|
|
|
|
blocking_json(is_sync, move || {
|
2020-01-20 09:45:44 -05:00
|
|
|
debug!("op_realpath {}", path.display());
|
2019-11-26 03:40:57 -05:00
|
|
|
// corresponds to the realpath on Unix and
|
|
|
|
// CreateFile and GetFinalPathNameByHandle on Windows
|
2020-03-20 09:46:26 -04:00
|
|
|
let realpath = std::fs::canonicalize(&path)?;
|
2019-12-01 14:23:35 -05:00
|
|
|
let mut realpath_str =
|
2020-04-03 13:47:57 -04:00
|
|
|
into_string(realpath.into_os_string())?.replace("\\", "/");
|
2019-12-01 14:23:35 -05:00
|
|
|
if cfg!(windows) {
|
|
|
|
realpath_str = realpath_str.trim_start_matches("//?/").to_string();
|
|
|
|
}
|
2019-11-26 03:40:57 -05:00
|
|
|
Ok(json!(realpath_str))
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
2019-08-26 10:18:42 -04:00
|
|
|
#[derive(Deserialize)]
|
|
|
|
#[serde(rename_all = "camelCase")]
|
|
|
|
struct ReadDirArgs {
|
|
|
|
promise_id: Option<u64>,
|
|
|
|
path: String,
|
|
|
|
}
|
|
|
|
|
2019-10-11 14:41:54 -04:00
|
|
|
fn op_read_dir(
|
2020-02-08 14:34:31 -05:00
|
|
|
state: &State,
|
2019-08-26 10:18:42 -04:00
|
|
|
args: Value,
|
2020-06-01 14:20:47 -04:00
|
|
|
_zero_copy: &mut [ZeroCopyBuf],
|
2020-02-23 14:51:29 -05:00
|
|
|
) -> Result<JsonOp, OpError> {
|
2019-08-26 10:18:42 -04:00
|
|
|
let args: ReadDirArgs = serde_json::from_value(args)?;
|
2020-05-29 11:27:43 -04:00
|
|
|
let path = PathBuf::from(&args.path);
|
2019-08-14 11:03:02 -04:00
|
|
|
|
2020-01-20 09:45:44 -05:00
|
|
|
state.check_read(&path)?;
|
2019-08-14 11:03:02 -04:00
|
|
|
|
2019-08-26 10:18:42 -04:00
|
|
|
let is_sync = args.promise_id.is_none();
|
|
|
|
blocking_json(is_sync, move || {
|
2019-08-14 11:03:02 -04:00
|
|
|
debug!("op_read_dir {}", 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-04-29 16:00:31 -04:00
|
|
|
let file_type = entry.file_type().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,
|
|
|
|
"isFile": file_type.is_file(),
|
|
|
|
"isDirectory": file_type.is_dir(),
|
|
|
|
"isSymlink": file_type.is_symlink()
|
|
|
|
}))
|
2020-02-15 10:37:05 -05:00
|
|
|
} else {
|
|
|
|
None
|
|
|
|
}
|
2019-08-14 11:03:02 -04:00
|
|
|
})
|
|
|
|
.collect();
|
|
|
|
|
2019-08-26 10:18:42 -04:00
|
|
|
Ok(json!({ "entries": entries }))
|
2019-08-14 11:03:02 -04:00
|
|
|
})
|
|
|
|
}
|
|
|
|
|
2019-08-26 10:18:42 -04:00
|
|
|
#[derive(Deserialize)]
|
|
|
|
#[serde(rename_all = "camelCase")]
|
|
|
|
struct RenameArgs {
|
|
|
|
promise_id: Option<u64>,
|
|
|
|
oldpath: String,
|
|
|
|
newpath: String,
|
|
|
|
}
|
|
|
|
|
2019-10-11 14:41:54 -04:00
|
|
|
fn op_rename(
|
2020-02-08 14:34:31 -05:00
|
|
|
state: &State,
|
2019-08-26 10:18:42 -04:00
|
|
|
args: Value,
|
2020-06-01 14:20:47 -04:00
|
|
|
_zero_copy: &mut [ZeroCopyBuf],
|
2020-02-23 14:51:29 -05:00
|
|
|
) -> Result<JsonOp, OpError> {
|
2019-08-26 10:18:42 -04:00
|
|
|
let args: RenameArgs = serde_json::from_value(args)?;
|
2020-05-29 11:27:43 -04:00
|
|
|
let oldpath = PathBuf::from(&args.oldpath);
|
|
|
|
let newpath = PathBuf::from(&args.newpath);
|
2019-08-26 10:18:42 -04:00
|
|
|
|
2020-01-20 09:45:44 -05:00
|
|
|
state.check_read(&oldpath)?;
|
|
|
|
state.check_write(&oldpath)?;
|
|
|
|
state.check_write(&newpath)?;
|
2019-08-14 11:03:02 -04:00
|
|
|
|
2019-08-26 10:18:42 -04:00
|
|
|
let is_sync = args.promise_id.is_none();
|
|
|
|
blocking_json(is_sync, move || {
|
2019-08-14 11:03:02 -04:00
|
|
|
debug!("op_rename {} {}", 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
|
|
|
})
|
|
|
|
}
|
|
|
|
|
2019-08-26 10:18:42 -04:00
|
|
|
#[derive(Deserialize)]
|
|
|
|
#[serde(rename_all = "camelCase")]
|
|
|
|
struct LinkArgs {
|
|
|
|
promise_id: Option<u64>,
|
2020-03-20 09:46:26 -04:00
|
|
|
oldpath: String,
|
|
|
|
newpath: String,
|
2019-08-26 10:18:42 -04:00
|
|
|
}
|
|
|
|
|
2019-10-11 14:41:54 -04:00
|
|
|
fn op_link(
|
2020-02-08 14:34:31 -05:00
|
|
|
state: &State,
|
2019-08-26 10:18:42 -04:00
|
|
|
args: Value,
|
2020-06-01 14:20:47 -04:00
|
|
|
_zero_copy: &mut [ZeroCopyBuf],
|
2020-02-23 14:51:29 -05:00
|
|
|
) -> Result<JsonOp, OpError> {
|
2020-04-25 09:31:54 -04:00
|
|
|
state.check_unstable("Deno.link");
|
2019-08-26 10:18:42 -04:00
|
|
|
let args: LinkArgs = serde_json::from_value(args)?;
|
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-03-20 09:46:26 -04:00
|
|
|
state.check_read(&oldpath)?;
|
|
|
|
state.check_write(&newpath)?;
|
2019-08-14 11:03:02 -04:00
|
|
|
|
2019-08-26 10:18:42 -04:00
|
|
|
let is_sync = args.promise_id.is_none();
|
|
|
|
blocking_json(is_sync, move || {
|
2020-03-20 09:46:26 -04:00
|
|
|
debug!("op_link {} {}", oldpath.display(), newpath.display());
|
|
|
|
std::fs::hard_link(&oldpath, &newpath)?;
|
2019-08-26 10:18:42 -04:00
|
|
|
Ok(json!({}))
|
2019-08-14 11:03:02 -04:00
|
|
|
})
|
|
|
|
}
|
|
|
|
|
2019-08-26 10:18:42 -04:00
|
|
|
#[derive(Deserialize)]
|
|
|
|
#[serde(rename_all = "camelCase")]
|
|
|
|
struct SymlinkArgs {
|
|
|
|
promise_id: Option<u64>,
|
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")]
|
|
|
|
struct SymlinkOptions {
|
|
|
|
_type: String,
|
2019-08-26 10:18:42 -04:00
|
|
|
}
|
|
|
|
|
2019-10-11 14:41:54 -04:00
|
|
|
fn op_symlink(
|
2020-02-08 14:34:31 -05:00
|
|
|
state: &State,
|
2019-08-26 10:18:42 -04:00
|
|
|
args: Value,
|
2020-06-01 14:20:47 -04:00
|
|
|
_zero_copy: &mut [ZeroCopyBuf],
|
2020-02-23 14:51:29 -05:00
|
|
|
) -> Result<JsonOp, OpError> {
|
2020-04-25 09:31:54 -04:00
|
|
|
state.check_unstable("Deno.symlink");
|
2019-08-26 10:18:42 -04:00
|
|
|
let args: SymlinkArgs = serde_json::from_value(args)?;
|
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
|
|
|
|
|
|
|
state.check_write(&newpath)?;
|
2019-08-26 10:18:42 -04:00
|
|
|
|
|
|
|
let is_sync = args.promise_id.is_none();
|
|
|
|
blocking_json(is_sync, move || {
|
2020-03-20 09:46:26 -04:00
|
|
|
debug!("op_symlink {} {}", 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)?,
|
|
|
|
_ => return Err(OpError::type_error("unsupported type".to_string())),
|
|
|
|
},
|
|
|
|
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)?
|
|
|
|
}
|
|
|
|
}
|
|
|
|
Err(_) => return Err(OpError::type_error(
|
|
|
|
"you must pass a `options` argument for non-existent target path in windows"
|
|
|
|
.to_string(),
|
|
|
|
)),
|
|
|
|
}
|
|
|
|
}
|
|
|
|
};
|
|
|
|
Ok(json!({}))
|
2020-03-20 09:46:26 -04:00
|
|
|
}
|
2019-08-14 11:03:02 -04:00
|
|
|
})
|
|
|
|
}
|
|
|
|
|
2019-08-26 10:18:42 -04:00
|
|
|
#[derive(Deserialize)]
|
|
|
|
#[serde(rename_all = "camelCase")]
|
|
|
|
struct ReadLinkArgs {
|
|
|
|
promise_id: Option<u64>,
|
2020-03-06 11:29:23 -05:00
|
|
|
path: String,
|
2019-08-26 10:18:42 -04:00
|
|
|
}
|
|
|
|
|
2019-10-11 14:41:54 -04:00
|
|
|
fn op_read_link(
|
2020-02-08 14:34:31 -05:00
|
|
|
state: &State,
|
2019-08-26 10:18:42 -04:00
|
|
|
args: Value,
|
2020-06-01 14:20:47 -04:00
|
|
|
_zero_copy: &mut [ZeroCopyBuf],
|
2020-02-23 14:51:29 -05:00
|
|
|
) -> Result<JsonOp, OpError> {
|
2019-08-26 10:18:42 -04:00
|
|
|
let args: ReadLinkArgs = serde_json::from_value(args)?;
|
2020-05-29 11:27:43 -04:00
|
|
|
let path = PathBuf::from(&args.path);
|
2019-08-26 10:18:42 -04:00
|
|
|
|
2020-03-06 11:29:23 -05:00
|
|
|
state.check_read(&path)?;
|
2019-08-14 11:03:02 -04:00
|
|
|
|
2019-08-26 10:18:42 -04:00
|
|
|
let is_sync = args.promise_id.is_none();
|
|
|
|
blocking_json(is_sync, move || {
|
2020-03-06 11:29:23 -05:00
|
|
|
debug!("op_read_link {}", 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-06-20 09:46:10 -04:00
|
|
|
#[derive(Deserialize)]
|
|
|
|
#[serde(rename_all = "camelCase")]
|
|
|
|
struct FtruncateArgs {
|
|
|
|
promise_id: Option<u64>,
|
|
|
|
rid: i32,
|
|
|
|
len: i32,
|
|
|
|
}
|
|
|
|
|
|
|
|
fn op_ftruncate(
|
|
|
|
isolate_state: &mut CoreIsolateState,
|
|
|
|
state: &State,
|
|
|
|
args: Value,
|
|
|
|
_zero_copy: &mut [ZeroCopyBuf],
|
|
|
|
) -> Result<JsonOp, OpError> {
|
|
|
|
state.check_unstable("Deno.ftruncate");
|
|
|
|
let args: FtruncateArgs = serde_json::from_value(args)?;
|
|
|
|
let rid = args.rid as u32;
|
|
|
|
let len = args.len as u64;
|
|
|
|
|
|
|
|
let resource_table = isolate_state.resource_table.clone();
|
|
|
|
let is_sync = args.promise_id.is_none();
|
|
|
|
|
|
|
|
if is_sync {
|
|
|
|
let mut resource_table = resource_table.borrow_mut();
|
|
|
|
std_file_resource(&mut resource_table, rid, |r| match r {
|
|
|
|
Ok(std_file) => std_file.set_len(len).map_err(OpError::from),
|
|
|
|
Err(_) => Err(OpError::type_error(
|
|
|
|
"cannot truncate this type of resource".to_string(),
|
|
|
|
)),
|
|
|
|
})?;
|
|
|
|
Ok(JsonOp::Sync(json!({})))
|
|
|
|
} else {
|
|
|
|
let fut = async move {
|
|
|
|
let mut resource_table = resource_table.borrow_mut();
|
|
|
|
std_file_resource(&mut resource_table, rid, |r| match r {
|
|
|
|
Ok(std_file) => std_file.set_len(len).map_err(OpError::from),
|
|
|
|
Err(_) => Err(OpError::type_error(
|
|
|
|
"cannot truncate this type of resource".to_string(),
|
|
|
|
)),
|
|
|
|
})?;
|
|
|
|
Ok(json!({}))
|
|
|
|
};
|
|
|
|
Ok(JsonOp::Async(fut.boxed_local()))
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-08-26 10:18:42 -04:00
|
|
|
#[derive(Deserialize)]
|
|
|
|
#[serde(rename_all = "camelCase")]
|
|
|
|
struct TruncateArgs {
|
|
|
|
promise_id: Option<u64>,
|
2020-03-06 11:29:23 -05:00
|
|
|
path: String,
|
2019-08-26 10:18:42 -04:00
|
|
|
len: u64,
|
|
|
|
}
|
|
|
|
|
2019-10-11 14:41:54 -04:00
|
|
|
fn op_truncate(
|
2020-02-08 14:34:31 -05:00
|
|
|
state: &State,
|
2019-08-26 10:18:42 -04:00
|
|
|
args: Value,
|
2020-06-01 14:20:47 -04:00
|
|
|
_zero_copy: &mut [ZeroCopyBuf],
|
2020-02-23 14:51:29 -05:00
|
|
|
) -> Result<JsonOp, OpError> {
|
2019-08-26 10:18:42 -04:00
|
|
|
let args: TruncateArgs = serde_json::from_value(args)?;
|
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;
|
2019-08-14 11:03:02 -04:00
|
|
|
|
2020-03-06 11:29:23 -05:00
|
|
|
state.check_write(&path)?;
|
2019-08-14 11:03:02 -04:00
|
|
|
|
2019-08-26 10:18:42 -04:00
|
|
|
let is_sync = args.promise_id.is_none();
|
|
|
|
blocking_json(is_sync, move || {
|
2020-03-06 11:29:23 -05:00
|
|
|
debug!("op_truncate {} {}", 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-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")]
|
2020-02-18 14:45:59 -05:00
|
|
|
struct MakeTempArgs {
|
2019-08-26 10:18:42 -04:00
|
|
|
promise_id: Option<u64>,
|
|
|
|
dir: Option<String>,
|
|
|
|
prefix: Option<String>,
|
|
|
|
suffix: Option<String>,
|
|
|
|
}
|
|
|
|
|
2019-10-11 14:41:54 -04:00
|
|
|
fn op_make_temp_dir(
|
2020-02-08 14:34:31 -05:00
|
|
|
state: &State,
|
2019-08-26 10:18:42 -04:00
|
|
|
args: Value,
|
2020-06-01 14:20:47 -04:00
|
|
|
_zero_copy: &mut [ZeroCopyBuf],
|
2020-02-23 14:51:29 -05:00
|
|
|
) -> Result<JsonOp, OpError> {
|
2020-02-18 14:45:59 -05:00
|
|
|
let args: MakeTempArgs = serde_json::from_value(args)?;
|
|
|
|
|
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-03-20 09:46:26 -04:00
|
|
|
state.check_write(dir.clone().unwrap_or_else(temp_dir).as_path())?;
|
2020-02-18 14:45:59 -05:00
|
|
|
|
|
|
|
let is_sync = args.promise_id.is_none();
|
|
|
|
blocking_json(is_sync, move || {
|
|
|
|
// 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))
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
|
|
|
fn op_make_temp_file(
|
|
|
|
state: &State,
|
|
|
|
args: Value,
|
2020-06-01 14:20:47 -04:00
|
|
|
_zero_copy: &mut [ZeroCopyBuf],
|
2020-02-23 14:51:29 -05:00
|
|
|
) -> Result<JsonOp, OpError> {
|
2020-02-18 14:45:59 -05:00
|
|
|
let args: MakeTempArgs = serde_json::from_value(args)?;
|
2019-08-14 11:03:02 -04:00
|
|
|
|
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);
|
2019-08-14 11:03:02 -04:00
|
|
|
|
2020-03-20 09:46:26 -04:00
|
|
|
state.check_write(dir.clone().unwrap_or_else(temp_dir).as_path())?;
|
2020-01-29 23:21:41 -05:00
|
|
|
|
2019-08-26 10:18:42 -04:00
|
|
|
let is_sync = args.promise_id.is_none();
|
|
|
|
blocking_json(is_sync, 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
|
|
|
})
|
|
|
|
}
|
|
|
|
|
2019-08-23 01:30:14 -04:00
|
|
|
#[derive(Deserialize)]
|
|
|
|
#[serde(rename_all = "camelCase")]
|
2020-03-02 10:19:42 -05:00
|
|
|
struct UtimeArgs {
|
2019-08-23 01:30:14 -04:00
|
|
|
promise_id: Option<u64>,
|
2020-03-06 11:29:23 -05:00
|
|
|
path: String,
|
2020-05-30 05:35:44 -04:00
|
|
|
atime: i64,
|
|
|
|
mtime: i64,
|
2019-08-23 01:30:14 -04:00
|
|
|
}
|
|
|
|
|
2019-10-11 14:41:54 -04:00
|
|
|
fn op_utime(
|
2020-02-08 14:34:31 -05:00
|
|
|
state: &State,
|
2019-08-23 01:30:14 -04:00
|
|
|
args: Value,
|
2020-06-01 14:20:47 -04:00
|
|
|
_zero_copy: &mut [ZeroCopyBuf],
|
2020-02-23 14:51:29 -05:00
|
|
|
) -> Result<JsonOp, OpError> {
|
2020-04-27 21:14:47 -04:00
|
|
|
state.check_unstable("Deno.utime");
|
|
|
|
|
2020-03-02 10:19:42 -05:00
|
|
|
let args: UtimeArgs = serde_json::from_value(args)?;
|
2020-05-29 11:27:43 -04:00
|
|
|
let path = PathBuf::from(&args.path);
|
2020-03-20 09:46:26 -04:00
|
|
|
|
|
|
|
state.check_write(&path)?;
|
|
|
|
|
2019-08-23 01:30:14 -04:00
|
|
|
let is_sync = args.promise_id.is_none();
|
|
|
|
blocking_json(is_sync, move || {
|
2020-03-06 11:29:23 -05:00
|
|
|
debug!("op_utime {} {} {}", args.path, args.atime, args.mtime);
|
|
|
|
utime::set_file_times(args.path, args.atime, args.mtime)?;
|
2019-08-23 01:30:14 -04:00
|
|
|
Ok(json!({}))
|
2019-08-14 11:03:02 -04:00
|
|
|
})
|
|
|
|
}
|
|
|
|
|
2019-10-11 14:41:54 -04:00
|
|
|
fn op_cwd(
|
2020-05-04 14:23:06 -04:00
|
|
|
state: &State,
|
2019-08-26 10:18:42 -04:00
|
|
|
_args: Value,
|
2020-06-01 14:20:47 -04:00
|
|
|
_zero_copy: &mut [ZeroCopyBuf],
|
2020-02-23 14:51:29 -05:00
|
|
|
) -> Result<JsonOp, OpError> {
|
2020-03-20 09:46:26 -04:00
|
|
|
let path = current_dir()?;
|
2020-05-29 11:27:43 -04:00
|
|
|
state.check_read_blind(&path, "CWD")?;
|
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(JsonOp::Sync(json!(path_str)))
|
2019-08-14 11:03:02 -04:00
|
|
|
}
|