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};
|
2019-08-14 11:03:02 -04:00
|
|
|
use crate::deno_error::DenoError;
|
|
|
|
use crate::deno_error::ErrorKind;
|
|
|
|
use crate::fs as deno_fs;
|
2020-02-03 18:08:44 -05:00
|
|
|
use crate::ops::dispatch_json::JsonResult;
|
2019-10-11 14:41:54 -04:00
|
|
|
use crate::ops::json_op;
|
2020-02-08 14:34:31 -05:00
|
|
|
use crate::state::State;
|
2020-01-05 11:56:18 -05:00
|
|
|
use deno_core::*;
|
2019-08-14 11:03:02 -04:00
|
|
|
use remove_dir_all::remove_dir_all;
|
|
|
|
use std::convert::From;
|
|
|
|
use std::fs;
|
2020-01-20 09:45:44 -05:00
|
|
|
use std::path::{Path, PathBuf};
|
2019-08-14 11:03:02 -04:00
|
|
|
use std::time::UNIX_EPOCH;
|
|
|
|
|
2020-01-16 09:46:32 -05:00
|
|
|
#[cfg(unix)]
|
|
|
|
use std::os::unix::fs::MetadataExt;
|
2019-08-14 11:03:02 -04:00
|
|
|
#[cfg(unix)]
|
|
|
|
use std::os::unix::fs::PermissionsExt;
|
|
|
|
|
2020-02-08 14:34:31 -05:00
|
|
|
pub fn init(i: &mut Isolate, s: &State) {
|
2019-10-11 14:41:54 -04:00
|
|
|
i.register_op("chdir", s.core_op(json_op(s.stateful_op(op_chdir))));
|
|
|
|
i.register_op("mkdir", s.core_op(json_op(s.stateful_op(op_mkdir))));
|
|
|
|
i.register_op("chmod", s.core_op(json_op(s.stateful_op(op_chmod))));
|
|
|
|
i.register_op("chown", s.core_op(json_op(s.stateful_op(op_chown))));
|
|
|
|
i.register_op("remove", s.core_op(json_op(s.stateful_op(op_remove))));
|
|
|
|
i.register_op("copy_file", s.core_op(json_op(s.stateful_op(op_copy_file))));
|
|
|
|
i.register_op("stat", s.core_op(json_op(s.stateful_op(op_stat))));
|
2019-11-26 03:40:57 -05:00
|
|
|
i.register_op("realpath", s.core_op(json_op(s.stateful_op(op_realpath))));
|
2019-10-11 14:41:54 -04:00
|
|
|
i.register_op("read_dir", s.core_op(json_op(s.stateful_op(op_read_dir))));
|
|
|
|
i.register_op("rename", s.core_op(json_op(s.stateful_op(op_rename))));
|
|
|
|
i.register_op("link", s.core_op(json_op(s.stateful_op(op_link))));
|
|
|
|
i.register_op("symlink", s.core_op(json_op(s.stateful_op(op_symlink))));
|
|
|
|
i.register_op("read_link", s.core_op(json_op(s.stateful_op(op_read_link))));
|
|
|
|
i.register_op("truncate", s.core_op(json_op(s.stateful_op(op_truncate))));
|
|
|
|
i.register_op(
|
|
|
|
"make_temp_dir",
|
|
|
|
s.core_op(json_op(s.stateful_op(op_make_temp_dir))),
|
|
|
|
);
|
|
|
|
i.register_op("cwd", s.core_op(json_op(s.stateful_op(op_cwd))));
|
|
|
|
i.register_op("utime", s.core_op(json_op(s.stateful_op(op_utime))));
|
|
|
|
}
|
|
|
|
|
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-02-08 14:34:31 -05:00
|
|
|
_state: &State,
|
2019-08-26 10:18:42 -04:00
|
|
|
args: Value,
|
2020-01-24 15:10:49 -05:00
|
|
|
_zero_copy: Option<ZeroCopyBuf>,
|
2019-08-26 10:18:42 -04:00
|
|
|
) -> Result<JsonOp, ErrBox> {
|
|
|
|
let args: ChdirArgs = serde_json::from_value(args)?;
|
|
|
|
std::env::set_current_dir(&args.directory)?;
|
|
|
|
Ok(JsonOp::Sync(json!({})))
|
|
|
|
}
|
|
|
|
|
|
|
|
#[derive(Deserialize)]
|
|
|
|
#[serde(rename_all = "camelCase")]
|
|
|
|
struct MkdirArgs {
|
|
|
|
promise_id: Option<u64>,
|
|
|
|
path: String,
|
|
|
|
recursive: bool,
|
|
|
|
mode: 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-01-24 15:10:49 -05:00
|
|
|
_zero_copy: Option<ZeroCopyBuf>,
|
2019-08-26 10:18:42 -04:00
|
|
|
) -> Result<JsonOp, ErrBox> {
|
|
|
|
let args: MkdirArgs = serde_json::from_value(args)?;
|
2020-01-20 09:45:44 -05:00
|
|
|
let path = deno_fs::resolve_from_cwd(Path::new(&args.path))?;
|
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-01-20 09:45:44 -05:00
|
|
|
debug!("op_mkdir {}", path.display());
|
2019-08-26 10:18:42 -04:00
|
|
|
deno_fs::mkdir(&path, args.mode, args.recursive)?;
|
|
|
|
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,
|
2020-01-06 11:38:56 -05:00
|
|
|
#[allow(unused)]
|
2019-08-26 10:18:42 -04:00
|
|
|
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-01-24 15:10:49 -05:00
|
|
|
_zero_copy: Option<ZeroCopyBuf>,
|
2019-08-26 10:18:42 -04:00
|
|
|
) -> Result<JsonOp, ErrBox> {
|
|
|
|
let args: ChmodArgs = serde_json::from_value(args)?;
|
2020-01-20 09:45:44 -05:00
|
|
|
let path = deno_fs::resolve_from_cwd(Path::new(&args.path))?;
|
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-01-20 09:45:44 -05:00
|
|
|
debug!("op_chmod {}", path.display());
|
2019-08-14 11:03:02 -04:00
|
|
|
// Still check file/dir exists on windows
|
|
|
|
let _metadata = fs::metadata(&path)?;
|
|
|
|
#[cfg(any(unix))]
|
|
|
|
{
|
|
|
|
let mut permissions = _metadata.permissions();
|
2019-08-26 10:18:42 -04:00
|
|
|
permissions.set_mode(args.mode);
|
2019-08-14 11:03:02 -04:00
|
|
|
fs::set_permissions(&path, permissions)?;
|
|
|
|
}
|
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 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-01-24 15:10:49 -05:00
|
|
|
_zero_copy: Option<ZeroCopyBuf>,
|
2019-08-26 10:18:42 -04:00
|
|
|
) -> Result<JsonOp, ErrBox> {
|
|
|
|
let args: ChownArgs = serde_json::from_value(args)?;
|
2020-01-20 09:45:44 -05:00
|
|
|
let path = deno_fs::resolve_from_cwd(Path::new(&args.path))?;
|
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-01-20 09:45:44 -05:00
|
|
|
debug!("op_chown {}", path.display());
|
2019-08-26 10:18:42 -04:00
|
|
|
match deno_fs::chown(args.path.as_ref(), args.uid, args.gid) {
|
|
|
|
Ok(_) => Ok(json!({})),
|
2019-08-14 11:03:02 -04:00
|
|
|
Err(e) => Err(e),
|
|
|
|
}
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
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-01-24 15:10:49 -05:00
|
|
|
_zero_copy: Option<ZeroCopyBuf>,
|
2019-08-26 10:18:42 -04:00
|
|
|
) -> Result<JsonOp, ErrBox> {
|
|
|
|
let args: RemoveArgs = serde_json::from_value(args)?;
|
2020-01-20 09:45:44 -05:00
|
|
|
let path = deno_fs::resolve_from_cwd(Path::new(&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 || {
|
2019-08-14 11:03:02 -04:00
|
|
|
debug!("op_remove {}", path.display());
|
2020-02-03 08:20:15 -05:00
|
|
|
let metadata = fs::symlink_metadata(&path)?;
|
|
|
|
let file_type = metadata.file_type();
|
|
|
|
if file_type.is_file() || file_type.is_symlink() {
|
2019-08-14 11:03:02 -04:00
|
|
|
fs::remove_file(&path)?;
|
|
|
|
} else if recursive {
|
|
|
|
remove_dir_all(&path)?;
|
|
|
|
} else {
|
|
|
|
fs::remove_dir(&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 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-01-24 15:10:49 -05:00
|
|
|
_zero_copy: Option<ZeroCopyBuf>,
|
2019-08-26 10:18:42 -04:00
|
|
|
) -> Result<JsonOp, ErrBox> {
|
|
|
|
let args: CopyFileArgs = serde_json::from_value(args)?;
|
2020-01-20 09:45:44 -05:00
|
|
|
let from = deno_fs::resolve_from_cwd(Path::new(&args.from))?;
|
|
|
|
let to = deno_fs::resolve_from_cwd(Path::new(&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 || {
|
2019-08-14 11:03:02 -04:00
|
|
|
// On *nix, Rust deem non-existent path as invalid input
|
|
|
|
// See https://github.com/rust-lang/rust/issues/54800
|
|
|
|
// Once the issue is reolved, we should remove this workaround.
|
|
|
|
if cfg!(unix) && !from.is_file() {
|
|
|
|
return Err(
|
|
|
|
DenoError::new(ErrorKind::NotFound, "File not found".to_string())
|
|
|
|
.into(),
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
fs::copy(&from, &to)?;
|
2019-08-26 10:18:42 -04:00
|
|
|
Ok(json!({}))
|
2019-08-14 11:03:02 -04:00
|
|
|
})
|
|
|
|
}
|
|
|
|
|
|
|
|
macro_rules! to_seconds {
|
|
|
|
($time:expr) => {{
|
|
|
|
// Unwrap is safe here as if the file is before the unix epoch
|
|
|
|
// something is very wrong.
|
|
|
|
$time
|
|
|
|
.and_then(|t| Ok(t.duration_since(UNIX_EPOCH).unwrap().as_secs()))
|
|
|
|
.unwrap_or(0)
|
|
|
|
}};
|
|
|
|
}
|
|
|
|
|
2020-01-16 09:46:32 -05:00
|
|
|
#[inline(always)]
|
|
|
|
fn get_stat_json(
|
|
|
|
metadata: fs::Metadata,
|
|
|
|
maybe_name: Option<String>,
|
2020-02-03 18:08:44 -05:00
|
|
|
) -> 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
|
|
|
|
}
|
|
|
|
}};
|
|
|
|
}
|
|
|
|
|
|
|
|
let mut json_val = json!({
|
|
|
|
"isFile": metadata.is_file(),
|
|
|
|
"isSymlink": metadata.file_type().is_symlink(),
|
|
|
|
"len": metadata.len(),
|
|
|
|
// In seconds. Available on both Unix or Windows.
|
|
|
|
"modified":to_seconds!(metadata.modified()),
|
|
|
|
"accessed":to_seconds!(metadata.accessed()),
|
|
|
|
"created":to_seconds!(metadata.created()),
|
|
|
|
// 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),
|
|
|
|
});
|
|
|
|
|
|
|
|
// "name" is an optional field by our design.
|
|
|
|
if let Some(name) = maybe_name {
|
|
|
|
if let serde_json::Value::Object(ref mut m) = json_val {
|
|
|
|
m.insert("name".to_owned(), json!(name));
|
|
|
|
}
|
|
|
|
}
|
2019-08-14 11:03:02 -04:00
|
|
|
|
2020-01-16 09:46:32 -05:00
|
|
|
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>,
|
|
|
|
filename: String,
|
|
|
|
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-01-24 15:10:49 -05:00
|
|
|
_zero_copy: Option<ZeroCopyBuf>,
|
2019-08-26 10:18:42 -04:00
|
|
|
) -> Result<JsonOp, ErrBox> {
|
|
|
|
let args: StatArgs = serde_json::from_value(args)?;
|
2020-01-20 09:45:44 -05:00
|
|
|
let filename = deno_fs::resolve_from_cwd(Path::new(&args.filename))?;
|
2019-08-26 10:18:42 -04:00
|
|
|
let lstat = args.lstat;
|
2019-08-14 11:03:02 -04:00
|
|
|
|
2020-01-20 09:45:44 -05:00
|
|
|
state.check_read(&filename)?;
|
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_stat {} {}", filename.display(), lstat);
|
|
|
|
let metadata = if lstat {
|
|
|
|
fs::symlink_metadata(&filename)?
|
|
|
|
} else {
|
|
|
|
fs::metadata(&filename)?
|
|
|
|
};
|
2020-01-16 09:46:32 -05:00
|
|
|
get_stat_json(metadata, None)
|
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-01-24 15:10:49 -05:00
|
|
|
_zero_copy: Option<ZeroCopyBuf>,
|
2019-11-26 03:40:57 -05:00
|
|
|
) -> Result<JsonOp, ErrBox> {
|
|
|
|
let args: RealpathArgs = serde_json::from_value(args)?;
|
2020-01-20 09:45:44 -05:00
|
|
|
let path = deno_fs::resolve_from_cwd(Path::new(&args.path))?;
|
|
|
|
|
|
|
|
state.check_read(&path)?;
|
|
|
|
|
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
|
|
|
|
let realpath = fs::canonicalize(&path)?;
|
2019-12-01 14:23:35 -05:00
|
|
|
let mut realpath_str =
|
|
|
|
realpath.to_str().unwrap().to_owned().replace("\\", "/");
|
|
|
|
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-01-24 15:10:49 -05:00
|
|
|
_zero_copy: Option<ZeroCopyBuf>,
|
2019-08-26 10:18:42 -04:00
|
|
|
) -> Result<JsonOp, ErrBox> {
|
|
|
|
let args: ReadDirArgs = serde_json::from_value(args)?;
|
2020-01-20 09:45:44 -05:00
|
|
|
let path = deno_fs::resolve_from_cwd(Path::new(&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());
|
|
|
|
let entries: Vec<_> = 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();
|
|
|
|
let metadata = entry.metadata().unwrap();
|
2020-02-15 10:37:05 -05:00
|
|
|
// Not all filenames can be encoded as UTF-8. Skip those for now.
|
|
|
|
if let Some(filename) = entry.file_name().to_str() {
|
|
|
|
let filename = Some(filename.to_owned());
|
|
|
|
Some(get_stat_json(metadata, filename).unwrap())
|
|
|
|
} 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-01-24 15:10:49 -05:00
|
|
|
_zero_copy: Option<ZeroCopyBuf>,
|
2019-08-26 10:18:42 -04:00
|
|
|
) -> Result<JsonOp, ErrBox> {
|
|
|
|
let args: RenameArgs = serde_json::from_value(args)?;
|
2020-01-20 09:45:44 -05:00
|
|
|
let oldpath = deno_fs::resolve_from_cwd(Path::new(&args.oldpath))?;
|
|
|
|
let newpath = deno_fs::resolve_from_cwd(Path::new(&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());
|
|
|
|
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>,
|
|
|
|
oldname: String,
|
|
|
|
newname: String,
|
|
|
|
}
|
|
|
|
|
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-01-24 15:10:49 -05:00
|
|
|
_zero_copy: Option<ZeroCopyBuf>,
|
2019-08-26 10:18:42 -04:00
|
|
|
) -> Result<JsonOp, ErrBox> {
|
|
|
|
let args: LinkArgs = serde_json::from_value(args)?;
|
2020-01-20 09:45:44 -05:00
|
|
|
let oldname = deno_fs::resolve_from_cwd(Path::new(&args.oldname))?;
|
|
|
|
let newname = deno_fs::resolve_from_cwd(Path::new(&args.newname))?;
|
2019-08-14 11:03:02 -04:00
|
|
|
|
2020-01-20 09:45:44 -05:00
|
|
|
state.check_read(&oldname)?;
|
|
|
|
state.check_write(&newname)?;
|
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_link {} {}", oldname.display(), newname.display());
|
|
|
|
std::fs::hard_link(&oldname, &newname)?;
|
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>,
|
|
|
|
oldname: String,
|
|
|
|
newname: String,
|
|
|
|
}
|
|
|
|
|
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-01-24 15:10:49 -05:00
|
|
|
_zero_copy: Option<ZeroCopyBuf>,
|
2019-08-26 10:18:42 -04:00
|
|
|
) -> Result<JsonOp, ErrBox> {
|
|
|
|
let args: SymlinkArgs = serde_json::from_value(args)?;
|
2020-01-20 09:45:44 -05:00
|
|
|
let oldname = deno_fs::resolve_from_cwd(Path::new(&args.oldname))?;
|
|
|
|
let newname = deno_fs::resolve_from_cwd(Path::new(&args.newname))?;
|
2019-08-26 10:18:42 -04:00
|
|
|
|
2020-01-20 09:45:44 -05:00
|
|
|
state.check_write(&newname)?;
|
2019-08-14 11:03:02 -04:00
|
|
|
// TODO Use type for Windows.
|
|
|
|
if cfg!(windows) {
|
|
|
|
return Err(
|
|
|
|
DenoError::new(ErrorKind::Other, "Not implemented".to_string()).into(),
|
|
|
|
);
|
|
|
|
}
|
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_symlink {} {}", oldname.display(), newname.display());
|
|
|
|
#[cfg(any(unix))]
|
|
|
|
std::os::unix::fs::symlink(&oldname, &newname)?;
|
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 ReadLinkArgs {
|
|
|
|
promise_id: Option<u64>,
|
|
|
|
name: String,
|
|
|
|
}
|
|
|
|
|
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-01-24 15:10:49 -05:00
|
|
|
_zero_copy: Option<ZeroCopyBuf>,
|
2019-08-26 10:18:42 -04:00
|
|
|
) -> Result<JsonOp, ErrBox> {
|
|
|
|
let args: ReadLinkArgs = serde_json::from_value(args)?;
|
2020-01-20 09:45:44 -05:00
|
|
|
let name = deno_fs::resolve_from_cwd(Path::new(&args.name))?;
|
2019-08-26 10:18:42 -04:00
|
|
|
|
2020-01-20 09:45:44 -05:00
|
|
|
state.check_read(&name)?;
|
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_link {}", name.display());
|
|
|
|
let path = fs::read_link(&name)?;
|
2019-08-26 10:18:42 -04:00
|
|
|
let path_str = path.to_str().unwrap();
|
|
|
|
|
|
|
|
Ok(json!(path_str))
|
2019-08-14 11:03:02 -04:00
|
|
|
})
|
|
|
|
}
|
|
|
|
|
2019-08-26 10:18:42 -04:00
|
|
|
#[derive(Deserialize)]
|
|
|
|
#[serde(rename_all = "camelCase")]
|
|
|
|
struct TruncateArgs {
|
|
|
|
promise_id: Option<u64>,
|
|
|
|
name: String,
|
|
|
|
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-01-24 15:10:49 -05:00
|
|
|
_zero_copy: Option<ZeroCopyBuf>,
|
2019-08-26 10:18:42 -04:00
|
|
|
) -> Result<JsonOp, ErrBox> {
|
|
|
|
let args: TruncateArgs = serde_json::from_value(args)?;
|
2020-01-20 09:45:44 -05:00
|
|
|
let filename = deno_fs::resolve_from_cwd(Path::new(&args.name))?;
|
2019-08-26 10:18:42 -04:00
|
|
|
let len = args.len;
|
2019-08-14 11:03:02 -04:00
|
|
|
|
2020-01-20 09:45:44 -05:00
|
|
|
state.check_write(&filename)?;
|
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-01-20 09:45:44 -05:00
|
|
|
debug!("op_truncate {} {}", filename.display(), len);
|
2019-08-14 11:03:02 -04:00
|
|
|
let f = fs::OpenOptions::new().write(true).open(&filename)?;
|
2019-08-26 10:18:42 -04:00
|
|
|
f.set_len(len)?;
|
|
|
|
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 MakeTempDirArgs {
|
|
|
|
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-01-24 15:10:49 -05:00
|
|
|
_zero_copy: Option<ZeroCopyBuf>,
|
2019-08-26 10:18:42 -04:00
|
|
|
) -> Result<JsonOp, ErrBox> {
|
|
|
|
let args: MakeTempDirArgs = serde_json::from_value(args)?;
|
2019-08-14 11:03:02 -04:00
|
|
|
|
2019-08-26 10:18:42 -04:00
|
|
|
let dir = args.dir.map(PathBuf::from);
|
|
|
|
let prefix = args.prefix.map(String::from);
|
|
|
|
let suffix = args.suffix.map(String::from);
|
2019-08-14 11:03:02 -04:00
|
|
|
|
2020-01-29 23:21:41 -05:00
|
|
|
state
|
|
|
|
.check_write(dir.clone().unwrap_or_else(std::env::temp_dir).as_path())?;
|
|
|
|
|
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.
|
|
|
|
let path = deno_fs::make_temp_dir(
|
|
|
|
// Converting Option<String> to Option<&str>
|
|
|
|
dir.as_ref().map(|x| &**x),
|
|
|
|
prefix.as_ref().map(|x| &**x),
|
|
|
|
suffix.as_ref().map(|x| &**x),
|
|
|
|
)?;
|
2019-08-26 10:18:42 -04:00
|
|
|
let path_str = path.to_str().unwrap();
|
|
|
|
|
|
|
|
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")]
|
|
|
|
struct Utime {
|
|
|
|
promise_id: Option<u64>,
|
|
|
|
filename: String,
|
|
|
|
atime: u64,
|
|
|
|
mtime: u64,
|
|
|
|
}
|
|
|
|
|
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-01-24 15:10:49 -05:00
|
|
|
_zero_copy: Option<ZeroCopyBuf>,
|
2019-08-23 01:30:14 -04:00
|
|
|
) -> Result<JsonOp, ErrBox> {
|
|
|
|
let args: Utime = serde_json::from_value(args)?;
|
2020-01-20 09:45:44 -05:00
|
|
|
state.check_write(Path::new(&args.filename))?;
|
2019-08-23 01:30:14 -04:00
|
|
|
let is_sync = args.promise_id.is_none();
|
|
|
|
blocking_json(is_sync, move || {
|
|
|
|
debug!("op_utimes {} {} {}", args.filename, args.atime, args.mtime);
|
|
|
|
utime::set_file_times(args.filename, args.atime, args.mtime)?;
|
|
|
|
Ok(json!({}))
|
2019-08-14 11:03:02 -04:00
|
|
|
})
|
|
|
|
}
|
|
|
|
|
2019-10-11 14:41:54 -04:00
|
|
|
fn op_cwd(
|
2020-02-08 14:34:31 -05:00
|
|
|
_state: &State,
|
2019-08-26 10:18:42 -04:00
|
|
|
_args: Value,
|
2020-01-24 15:10:49 -05:00
|
|
|
_zero_copy: Option<ZeroCopyBuf>,
|
2019-08-26 10:18:42 -04:00
|
|
|
) -> Result<JsonOp, ErrBox> {
|
2019-08-14 11:03:02 -04:00
|
|
|
let path = std::env::current_dir()?;
|
2019-08-26 10:18:42 -04:00
|
|
|
let path_str = path.into_os_string().into_string().unwrap();
|
|
|
|
Ok(JsonOp::Sync(json!(path_str)))
|
2019-08-14 11:03:02 -04:00
|
|
|
}
|