2019-01-02 00:58:40 +00:00
|
|
|
// Copyright 2018-2019 the Deno authors. All rights reserved. MIT license.
|
2019-02-09 03:13:04 +00:00
|
|
|
use atty;
|
|
|
|
use crate::ansi;
|
2019-05-09 16:20:34 +00:00
|
|
|
use crate::deno_dir::resolve_path;
|
2019-05-03 04:06:43 +00:00
|
|
|
use crate::dispatch_minimal::dispatch_minimal;
|
|
|
|
use crate::dispatch_minimal::parse_min_record;
|
2019-01-14 06:30:38 +00:00
|
|
|
use crate::errors;
|
2019-03-20 22:55:52 +00:00
|
|
|
use crate::errors::{DenoError, DenoResult, ErrorKind};
|
2019-01-14 06:30:38 +00:00
|
|
|
use crate::fs as deno_fs;
|
|
|
|
use crate::http_util;
|
2019-02-28 21:19:04 +00:00
|
|
|
use crate::js_errors::apply_source_map;
|
|
|
|
use crate::js_errors::JSErrorColor;
|
2019-01-14 06:30:38 +00:00
|
|
|
use crate::msg;
|
|
|
|
use crate::msg_util;
|
2019-05-17 18:03:01 +00:00
|
|
|
use crate::rand;
|
2019-01-14 06:30:38 +00:00
|
|
|
use crate::repl;
|
|
|
|
use crate::resolve_addr::resolve_addr;
|
|
|
|
use crate::resources;
|
|
|
|
use crate::resources::table_entries;
|
|
|
|
use crate::resources::Resource;
|
2019-04-22 01:26:56 +00:00
|
|
|
use crate::signal::kill;
|
2019-04-08 14:12:43 +00:00
|
|
|
use crate::startup_data;
|
2019-04-09 17:11:25 +00:00
|
|
|
use crate::state::ThreadSafeState;
|
2019-01-14 06:30:38 +00:00
|
|
|
use crate::tokio_util;
|
2019-03-18 23:06:48 +00:00
|
|
|
use crate::tokio_write;
|
2019-01-14 06:30:38 +00:00
|
|
|
use crate::version;
|
2019-04-08 21:10:00 +00:00
|
|
|
use crate::worker::Worker;
|
|
|
|
use deno::js_check;
|
2019-03-30 23:30:40 +00:00
|
|
|
use deno::Buf;
|
2019-06-18 01:02:08 +00:00
|
|
|
use deno::CoreOp;
|
2019-03-30 23:30:40 +00:00
|
|
|
use deno::JSError;
|
2019-06-12 23:55:59 +00:00
|
|
|
use deno::ModuleSpecifier;
|
2019-03-30 23:30:40 +00:00
|
|
|
use deno::Op;
|
2019-06-18 01:02:08 +00:00
|
|
|
use deno::OpResult;
|
2019-04-28 19:31:10 +00:00
|
|
|
use deno::PinnedBuf;
|
2018-09-18 18:53:16 +00:00
|
|
|
use flatbuffers::FlatBufferBuilder;
|
2018-08-09 19:17:08 +00:00
|
|
|
use futures;
|
2019-01-08 19:44:06 +00:00
|
|
|
use futures::Async;
|
2018-09-22 08:42:07 +00:00
|
|
|
use futures::Poll;
|
2019-01-08 19:44:06 +00:00
|
|
|
use futures::Sink;
|
|
|
|
use futures::Stream;
|
2018-08-16 00:57:36 +00:00
|
|
|
use hyper;
|
2018-10-26 16:14:06 +00:00
|
|
|
use hyper::rt::Future;
|
2019-05-17 18:03:01 +00:00
|
|
|
use rand::{thread_rng, Rng};
|
2018-09-11 03:40:03 +00:00
|
|
|
use remove_dir_all::remove_dir_all;
|
2018-08-13 23:55:10 +00:00
|
|
|
use std;
|
2018-11-30 03:03:00 +00:00
|
|
|
use std::convert::From;
|
2018-08-26 09:51:25 +00:00
|
|
|
use std::fs;
|
2019-01-14 03:14:59 +00:00
|
|
|
use std::net::Shutdown;
|
2018-09-22 08:42:07 +00:00
|
|
|
use std::path::PathBuf;
|
2018-11-16 04:07:40 +00:00
|
|
|
use std::process::Command;
|
2019-04-08 20:22:40 +00:00
|
|
|
use std::time::{Duration, Instant, UNIX_EPOCH};
|
2018-09-27 04:56:39 +00:00
|
|
|
use tokio;
|
2018-10-04 03:58:29 +00:00
|
|
|
use tokio::net::TcpListener;
|
|
|
|
use tokio::net::TcpStream;
|
2018-11-16 04:07:40 +00:00
|
|
|
use tokio_process::CommandExt;
|
2018-09-22 08:42:07 +00:00
|
|
|
use tokio_threadpool;
|
2019-05-01 09:08:12 +00:00
|
|
|
use utime;
|
2018-08-16 03:36:48 +00:00
|
|
|
|
2019-02-09 03:13:04 +00:00
|
|
|
#[cfg(unix)]
|
|
|
|
use std::os::unix::fs::PermissionsExt;
|
|
|
|
#[cfg(unix)]
|
|
|
|
use std::os::unix::process::ExitStatusExt;
|
|
|
|
|
2019-06-18 01:02:08 +00:00
|
|
|
type CliOpResult = OpResult<DenoError>;
|
2018-09-06 02:13:36 +00:00
|
|
|
|
2019-06-18 01:02:08 +00:00
|
|
|
type CliDispatchFn =
|
2019-04-28 19:31:10 +00:00
|
|
|
fn(state: &ThreadSafeState, base: &msg::Base<'_>, data: Option<PinnedBuf>)
|
2019-06-18 01:02:08 +00:00
|
|
|
-> CliOpResult;
|
2019-03-20 00:55:59 +00:00
|
|
|
|
2019-06-18 01:02:08 +00:00
|
|
|
pub type OpSelector = fn(inner_type: msg::Any) -> Option<CliDispatchFn>;
|
2018-07-06 15:27:36 +00:00
|
|
|
|
2018-12-05 04:36:10 +00:00
|
|
|
#[inline]
|
2018-09-18 18:53:16 +00:00
|
|
|
fn empty_buf() -> Buf {
|
|
|
|
Box::new([])
|
|
|
|
}
|
|
|
|
|
2019-05-03 04:06:43 +00:00
|
|
|
pub fn dispatch_all(
|
|
|
|
state: &ThreadSafeState,
|
|
|
|
control: &[u8],
|
|
|
|
zero_copy: Option<PinnedBuf>,
|
|
|
|
op_selector: OpSelector,
|
2019-06-18 01:02:08 +00:00
|
|
|
) -> CoreOp {
|
2019-05-03 04:06:43 +00:00
|
|
|
let bytes_sent_control = control.len();
|
|
|
|
let bytes_sent_zero_copy = zero_copy.as_ref().map(|b| b.len()).unwrap_or(0);
|
|
|
|
let op = if let Some(min_record) = parse_min_record(control) {
|
|
|
|
dispatch_minimal(state, min_record, zero_copy)
|
|
|
|
} else {
|
|
|
|
dispatch_all_legacy(state, control, zero_copy, op_selector)
|
|
|
|
};
|
|
|
|
state.metrics_op_dispatched(bytes_sent_control, bytes_sent_zero_copy);
|
|
|
|
op
|
|
|
|
}
|
|
|
|
|
2018-10-04 00:51:20 +00:00
|
|
|
/// Processes raw messages from JavaScript.
|
2019-03-26 12:22:07 +00:00
|
|
|
/// This functions invoked every time Deno.core.dispatch() is called.
|
|
|
|
/// control corresponds to the first argument of Deno.core.dispatch().
|
|
|
|
/// data corresponds to the second argument of Deno.core.dispatch().
|
2019-05-03 04:06:43 +00:00
|
|
|
pub fn dispatch_all_legacy(
|
2019-04-09 17:11:25 +00:00
|
|
|
state: &ThreadSafeState,
|
2019-03-14 23:17:52 +00:00
|
|
|
control: &[u8],
|
2019-04-28 19:31:10 +00:00
|
|
|
zero_copy: Option<PinnedBuf>,
|
2019-03-20 00:55:59 +00:00
|
|
|
op_selector: OpSelector,
|
2019-06-18 01:02:08 +00:00
|
|
|
) -> CoreOp {
|
2018-12-04 21:21:02 +00:00
|
|
|
let base = msg::get_root_as_base(&control);
|
2019-06-14 17:58:20 +00:00
|
|
|
let inner_type = base.inner_type();
|
2019-06-18 01:02:08 +00:00
|
|
|
let is_sync = base.sync();
|
2019-06-14 17:58:20 +00:00
|
|
|
let cmd_id = base.cmd_id();
|
2018-10-03 00:47:40 +00:00
|
|
|
|
2019-06-18 01:02:08 +00:00
|
|
|
debug!(
|
|
|
|
"msg_from_js {} sync {}",
|
|
|
|
msg::enum_name_any(inner_type),
|
|
|
|
is_sync
|
|
|
|
);
|
|
|
|
|
|
|
|
let op_func: CliDispatchFn = match op_selector(inner_type) {
|
2019-03-20 22:55:52 +00:00
|
|
|
Some(v) => v,
|
|
|
|
None => panic!("Unhandled message {}", msg::enum_name_any(inner_type)),
|
2018-08-16 03:36:48 +00:00
|
|
|
};
|
|
|
|
|
2019-06-18 01:02:08 +00:00
|
|
|
let op_result = op_func(state, &base, zero_copy);
|
2019-03-20 00:55:59 +00:00
|
|
|
|
2019-04-08 21:10:00 +00:00
|
|
|
let state = state.clone();
|
2019-03-14 23:17:52 +00:00
|
|
|
|
2019-06-18 01:02:08 +00:00
|
|
|
match op_result {
|
|
|
|
Ok(Op::Sync(buf)) => {
|
|
|
|
state.metrics_op_completed(buf.len());
|
|
|
|
Op::Sync(buf)
|
|
|
|
}
|
|
|
|
Ok(Op::Async(fut)) => {
|
|
|
|
let result_fut = Box::new(
|
|
|
|
fut.or_else(move |err: DenoError| -> Result<Buf, ()> {
|
|
|
|
debug!("op err {}", err);
|
|
|
|
// No matter whether we got an Err or Ok, we want a serialized message to
|
|
|
|
// send back. So transform the DenoError into a Buf.
|
|
|
|
let builder = &mut FlatBufferBuilder::new();
|
|
|
|
let errmsg_offset = builder.create_string(&format!("{}", err));
|
|
|
|
Ok(serialize_response(
|
|
|
|
cmd_id,
|
|
|
|
builder,
|
|
|
|
msg::BaseArgs {
|
|
|
|
error: Some(errmsg_offset),
|
|
|
|
error_kind: err.kind(),
|
|
|
|
..Default::default()
|
|
|
|
},
|
|
|
|
))
|
|
|
|
}).and_then(move |buf: Buf| -> Result<Buf, ()> {
|
|
|
|
// Handle empty responses. For sync responses we just want
|
|
|
|
// to send null. For async we want to send a small message
|
|
|
|
// with the cmd_id.
|
|
|
|
let buf = if buf.len() > 0 {
|
|
|
|
buf
|
|
|
|
} else {
|
|
|
|
let builder = &mut FlatBufferBuilder::new();
|
|
|
|
serialize_response(
|
|
|
|
cmd_id,
|
|
|
|
builder,
|
|
|
|
msg::BaseArgs {
|
|
|
|
..Default::default()
|
|
|
|
},
|
|
|
|
)
|
|
|
|
};
|
|
|
|
state.metrics_op_completed(buf.len());
|
|
|
|
Ok(buf)
|
|
|
|
}).map_err(|err| panic!("unexpected error {:?}", err)),
|
|
|
|
);
|
|
|
|
Op::Async(result_fut)
|
|
|
|
}
|
|
|
|
Err(err) => {
|
2018-09-18 18:53:16 +00:00
|
|
|
debug!("op err {}", err);
|
|
|
|
// No matter whether we got an Err or Ok, we want a serialized message to
|
2019-05-09 20:44:30 +00:00
|
|
|
// send back. So transform the DenoError into a Buf.
|
2018-09-18 18:53:16 +00:00
|
|
|
let builder = &mut FlatBufferBuilder::new();
|
|
|
|
let errmsg_offset = builder.create_string(&format!("{}", err));
|
2019-06-18 01:02:08 +00:00
|
|
|
let response_buf = serialize_response(
|
2019-06-14 17:58:20 +00:00
|
|
|
cmd_id,
|
2018-09-18 18:53:16 +00:00
|
|
|
builder,
|
|
|
|
msg::BaseArgs {
|
|
|
|
error: Some(errmsg_offset),
|
|
|
|
error_kind: err.kind(),
|
|
|
|
..Default::default()
|
|
|
|
},
|
2019-06-18 01:02:08 +00:00
|
|
|
);
|
|
|
|
state.metrics_op_completed(response_buf.len());
|
|
|
|
Op::Sync(response_buf)
|
|
|
|
}
|
2019-05-01 22:22:32 +00:00
|
|
|
}
|
2018-07-13 07:24:07 +00:00
|
|
|
}
|
|
|
|
|
2019-04-01 19:09:59 +00:00
|
|
|
/// Standard ops set for most isolates
|
2019-06-18 01:02:08 +00:00
|
|
|
pub fn op_selector_std(inner_type: msg::Any) -> Option<CliDispatchFn> {
|
2019-03-20 22:55:52 +00:00
|
|
|
match inner_type {
|
|
|
|
msg::Any::Accept => Some(op_accept),
|
2019-05-20 16:06:57 +00:00
|
|
|
msg::Any::Cache => Some(op_cache),
|
2019-03-20 22:55:52 +00:00
|
|
|
msg::Any::Chdir => Some(op_chdir),
|
|
|
|
msg::Any::Chmod => Some(op_chmod),
|
2019-05-08 01:58:58 +00:00
|
|
|
msg::Any::Chown => Some(op_chown),
|
2019-03-20 22:55:52 +00:00
|
|
|
msg::Any::Close => Some(op_close),
|
|
|
|
msg::Any::CopyFile => Some(op_copy_file),
|
2019-05-20 16:06:57 +00:00
|
|
|
msg::Any::CreateWorker => Some(op_create_worker),
|
2019-03-20 22:55:52 +00:00
|
|
|
msg::Any::Cwd => Some(op_cwd),
|
|
|
|
msg::Any::Dial => Some(op_dial),
|
|
|
|
msg::Any::Environ => Some(op_env),
|
|
|
|
msg::Any::Exit => Some(op_exit),
|
|
|
|
msg::Any::Fetch => Some(op_fetch),
|
2019-05-20 16:06:57 +00:00
|
|
|
msg::Any::FetchModuleMetaData => Some(op_fetch_module_meta_data),
|
2019-03-20 22:55:52 +00:00
|
|
|
msg::Any::FormatError => Some(op_format_error),
|
2019-05-17 18:03:01 +00:00
|
|
|
msg::Any::GetRandomValues => Some(op_get_random_values),
|
2019-03-20 22:55:52 +00:00
|
|
|
msg::Any::GlobalTimer => Some(op_global_timer),
|
|
|
|
msg::Any::GlobalTimerStop => Some(op_global_timer_stop),
|
2019-05-20 16:06:57 +00:00
|
|
|
msg::Any::HostGetMessage => Some(op_host_get_message),
|
|
|
|
msg::Any::HostGetWorkerClosed => Some(op_host_get_worker_closed),
|
|
|
|
msg::Any::HostPostMessage => Some(op_host_post_message),
|
2019-03-20 22:55:52 +00:00
|
|
|
msg::Any::IsTTY => Some(op_is_tty),
|
2019-04-22 01:26:56 +00:00
|
|
|
msg::Any::Kill => Some(op_kill),
|
2019-04-08 13:11:32 +00:00
|
|
|
msg::Any::Link => Some(op_link),
|
2019-03-20 22:55:52 +00:00
|
|
|
msg::Any::Listen => Some(op_listen),
|
|
|
|
msg::Any::MakeTempDir => Some(op_make_temp_dir),
|
|
|
|
msg::Any::Metrics => Some(op_metrics),
|
|
|
|
msg::Any::Mkdir => Some(op_mkdir),
|
|
|
|
msg::Any::Now => Some(op_now),
|
|
|
|
msg::Any::Open => Some(op_open),
|
|
|
|
msg::Any::PermissionRevoke => Some(op_revoke_permission),
|
|
|
|
msg::Any::Permissions => Some(op_permissions),
|
|
|
|
msg::Any::Read => Some(op_read),
|
|
|
|
msg::Any::ReadDir => Some(op_read_dir),
|
|
|
|
msg::Any::Readlink => Some(op_read_link),
|
|
|
|
msg::Any::Remove => Some(op_remove),
|
|
|
|
msg::Any::Rename => Some(op_rename),
|
|
|
|
msg::Any::ReplReadline => Some(op_repl_readline),
|
|
|
|
msg::Any::ReplStart => Some(op_repl_start),
|
|
|
|
msg::Any::Resources => Some(op_resources),
|
|
|
|
msg::Any::Run => Some(op_run),
|
|
|
|
msg::Any::RunStatus => Some(op_run_status),
|
|
|
|
msg::Any::Seek => Some(op_seek),
|
|
|
|
msg::Any::SetEnv => Some(op_set_env),
|
|
|
|
msg::Any::Shutdown => Some(op_shutdown),
|
|
|
|
msg::Any::Start => Some(op_start),
|
|
|
|
msg::Any::Stat => Some(op_stat),
|
|
|
|
msg::Any::Symlink => Some(op_symlink),
|
|
|
|
msg::Any::Truncate => Some(op_truncate),
|
2019-05-01 09:08:12 +00:00
|
|
|
msg::Any::Utime => Some(op_utime),
|
2019-03-20 22:55:52 +00:00
|
|
|
msg::Any::Write => Some(op_write),
|
2019-04-08 21:10:00 +00:00
|
|
|
|
|
|
|
// TODO(ry) split these out so that only the appropriate Workers can access
|
2019-04-11 14:58:31 +00:00
|
|
|
// them.
|
2019-04-08 21:10:00 +00:00
|
|
|
msg::Any::WorkerGetMessage => Some(op_worker_get_message),
|
|
|
|
msg::Any::WorkerPostMessage => Some(op_worker_post_message),
|
|
|
|
|
2019-03-20 22:55:52 +00:00
|
|
|
_ => None,
|
|
|
|
}
|
2019-03-20 00:55:59 +00:00
|
|
|
}
|
|
|
|
|
2019-04-08 20:22:40 +00:00
|
|
|
// Returns a milliseconds and nanoseconds subsec
|
|
|
|
// since the start time of the deno runtime.
|
|
|
|
// If the High precision flag is not set, the
|
|
|
|
// nanoseconds are rounded on 2ms.
|
2019-02-02 06:27:42 +00:00
|
|
|
fn op_now(
|
2019-04-09 17:11:25 +00:00
|
|
|
state: &ThreadSafeState,
|
2019-06-14 17:58:20 +00:00
|
|
|
base: &msg::Base<'_>,
|
2019-04-28 19:31:10 +00:00
|
|
|
data: Option<PinnedBuf>,
|
2019-06-18 01:02:08 +00:00
|
|
|
) -> CliOpResult {
|
2019-04-28 19:31:10 +00:00
|
|
|
assert!(data.is_none());
|
2019-04-08 21:10:00 +00:00
|
|
|
let seconds = state.start_time.elapsed().as_secs();
|
|
|
|
let mut subsec_nanos = state.start_time.elapsed().subsec_nanos();
|
2019-04-09 05:15:49 +00:00
|
|
|
let reduced_time_precision = 2_000_000; // 2ms in nanoseconds
|
2019-04-08 20:22:40 +00:00
|
|
|
|
|
|
|
// If the permission is not enabled
|
|
|
|
// Round the nano result on 2 milliseconds
|
|
|
|
// see: https://developer.mozilla.org/en-US/docs/Web/API/DOMHighResTimeStamp#Reduced_time_precision
|
2019-05-23 16:28:29 +00:00
|
|
|
if !state.permissions.allows_hrtime() {
|
2019-04-08 20:22:40 +00:00
|
|
|
subsec_nanos -= subsec_nanos % reduced_time_precision
|
|
|
|
}
|
2019-02-02 06:27:42 +00:00
|
|
|
|
|
|
|
let builder = &mut FlatBufferBuilder::new();
|
2019-04-08 20:22:40 +00:00
|
|
|
let inner = msg::NowRes::create(
|
|
|
|
builder,
|
|
|
|
&msg::NowResArgs {
|
|
|
|
seconds,
|
|
|
|
subsec_nanos,
|
|
|
|
},
|
|
|
|
);
|
2019-06-18 01:02:08 +00:00
|
|
|
|
|
|
|
ok_buf(serialize_response(
|
2019-06-14 17:58:20 +00:00
|
|
|
base.cmd_id(),
|
2019-02-02 06:27:42 +00:00
|
|
|
builder,
|
|
|
|
msg::BaseArgs {
|
|
|
|
inner: Some(inner.as_union_value()),
|
|
|
|
inner_type: msg::Any::NowRes,
|
|
|
|
..Default::default()
|
|
|
|
},
|
|
|
|
))
|
|
|
|
}
|
|
|
|
|
2019-02-03 03:05:30 +00:00
|
|
|
fn op_is_tty(
|
2019-04-09 17:11:25 +00:00
|
|
|
_state: &ThreadSafeState,
|
2019-06-14 17:58:20 +00:00
|
|
|
base: &msg::Base<'_>,
|
2019-04-28 19:31:10 +00:00
|
|
|
_data: Option<PinnedBuf>,
|
2019-06-18 01:02:08 +00:00
|
|
|
) -> CliOpResult {
|
2019-02-03 03:05:30 +00:00
|
|
|
let builder = &mut FlatBufferBuilder::new();
|
|
|
|
let inner = msg::IsTTYRes::create(
|
|
|
|
builder,
|
|
|
|
&msg::IsTTYResArgs {
|
|
|
|
stdin: atty::is(atty::Stream::Stdin),
|
|
|
|
stdout: atty::is(atty::Stream::Stdout),
|
|
|
|
stderr: atty::is(atty::Stream::Stderr),
|
|
|
|
},
|
|
|
|
);
|
2019-06-18 01:02:08 +00:00
|
|
|
ok_buf(serialize_response(
|
2019-06-14 17:58:20 +00:00
|
|
|
base.cmd_id(),
|
2019-02-03 03:05:30 +00:00
|
|
|
builder,
|
|
|
|
msg::BaseArgs {
|
|
|
|
inner: Some(inner.as_union_value()),
|
|
|
|
inner_type: msg::Any::IsTTYRes,
|
|
|
|
..Default::default()
|
|
|
|
},
|
|
|
|
))
|
|
|
|
}
|
|
|
|
|
2018-10-04 00:48:02 +00:00
|
|
|
fn op_exit(
|
2019-04-09 17:11:25 +00:00
|
|
|
_state: &ThreadSafeState,
|
2019-01-14 06:30:38 +00:00
|
|
|
base: &msg::Base<'_>,
|
2019-04-28 19:31:10 +00:00
|
|
|
_data: Option<PinnedBuf>,
|
2019-06-18 01:02:08 +00:00
|
|
|
) -> CliOpResult {
|
2018-10-04 01:12:23 +00:00
|
|
|
let inner = base.inner_as_exit().unwrap();
|
|
|
|
std::process::exit(inner.code())
|
2018-09-02 06:33:24 +00:00
|
|
|
}
|
|
|
|
|
2018-10-04 00:48:02 +00:00
|
|
|
fn op_start(
|
2019-04-09 17:11:25 +00:00
|
|
|
state: &ThreadSafeState,
|
2019-06-14 17:58:20 +00:00
|
|
|
base: &msg::Base<'_>,
|
2019-04-28 19:31:10 +00:00
|
|
|
data: Option<PinnedBuf>,
|
2019-06-18 01:02:08 +00:00
|
|
|
) -> CliOpResult {
|
2019-04-28 19:31:10 +00:00
|
|
|
assert!(data.is_none());
|
2018-09-06 02:13:36 +00:00
|
|
|
let mut builder = FlatBufferBuilder::new();
|
2018-08-13 23:55:10 +00:00
|
|
|
|
2019-04-08 21:10:00 +00:00
|
|
|
let state = state;
|
2019-04-17 13:25:51 +00:00
|
|
|
let argv = state.argv.iter().map(String::as_str).collect::<Vec<_>>();
|
2018-08-13 23:55:10 +00:00
|
|
|
let argv_off = builder.create_vector_of_strings(argv.as_slice());
|
|
|
|
|
|
|
|
let cwd_path = std::env::current_dir().unwrap();
|
2018-08-23 07:58:59 +00:00
|
|
|
let cwd_off =
|
2018-08-26 09:51:25 +00:00
|
|
|
builder.create_string(deno_fs::normalize_path(cwd_path.as_ref()).as_ref());
|
2018-08-13 23:55:10 +00:00
|
|
|
|
2019-02-15 16:22:02 +00:00
|
|
|
let exec_path =
|
|
|
|
builder.create_string(std::env::current_exe().unwrap().to_str().unwrap());
|
|
|
|
|
2018-11-05 06:21:21 +00:00
|
|
|
let v8_version = version::v8();
|
2018-10-16 00:52:33 +00:00
|
|
|
let v8_version_off = builder.create_string(v8_version);
|
|
|
|
|
2018-11-05 06:21:21 +00:00
|
|
|
let deno_version = version::DENO;
|
2018-10-16 00:52:33 +00:00
|
|
|
let deno_version_off = builder.create_string(deno_version);
|
|
|
|
|
2019-06-12 19:00:08 +00:00
|
|
|
let main_module = state
|
|
|
|
.main_module()
|
|
|
|
.map(|m| builder.create_string(&m.to_string()));
|
2019-02-13 02:14:02 +00:00
|
|
|
|
2019-05-03 20:24:09 +00:00
|
|
|
let xeval_delim = state
|
|
|
|
.flags
|
|
|
|
.xeval_delim
|
|
|
|
.clone()
|
|
|
|
.map(|m| builder.create_string(&m));
|
|
|
|
|
2018-10-04 01:12:23 +00:00
|
|
|
let inner = msg::StartRes::create(
|
2018-09-06 02:13:36 +00:00
|
|
|
&mut builder,
|
2018-08-13 23:55:10 +00:00
|
|
|
&msg::StartResArgs {
|
|
|
|
cwd: Some(cwd_off),
|
2019-01-06 19:16:42 +00:00
|
|
|
pid: std::process::id(),
|
2018-08-13 23:55:10 +00:00
|
|
|
argv: Some(argv_off),
|
2019-02-13 02:14:02 +00:00
|
|
|
main_module,
|
2019-04-08 21:10:00 +00:00
|
|
|
debug_flag: state.flags.log_debug,
|
|
|
|
version_flag: state.flags.version,
|
2018-10-16 00:52:33 +00:00
|
|
|
v8_version: Some(v8_version_off),
|
|
|
|
deno_version: Some(deno_version_off),
|
2019-02-09 03:13:04 +00:00
|
|
|
no_color: !ansi::use_color(),
|
2019-02-15 16:22:02 +00:00
|
|
|
exec_path: Some(exec_path),
|
2019-05-03 20:24:09 +00:00
|
|
|
xeval_delim,
|
2018-08-13 23:55:10 +00:00
|
|
|
..Default::default()
|
|
|
|
},
|
|
|
|
);
|
|
|
|
|
2019-06-18 01:02:08 +00:00
|
|
|
ok_buf(serialize_response(
|
2019-06-14 17:58:20 +00:00
|
|
|
base.cmd_id(),
|
2018-09-06 02:13:36 +00:00
|
|
|
&mut builder,
|
|
|
|
msg::BaseArgs {
|
2018-10-04 01:12:23 +00:00
|
|
|
inner_type: msg::Any::StartRes,
|
|
|
|
inner: Some(inner.as_union_value()),
|
2018-08-13 23:55:10 +00:00
|
|
|
..Default::default()
|
|
|
|
},
|
2018-08-16 03:36:48 +00:00
|
|
|
))
|
2018-07-23 18:13:12 +00:00
|
|
|
}
|
|
|
|
|
2019-02-09 21:55:40 +00:00
|
|
|
fn op_format_error(
|
2019-04-09 17:11:25 +00:00
|
|
|
state: &ThreadSafeState,
|
2019-02-09 21:55:40 +00:00
|
|
|
base: &msg::Base<'_>,
|
2019-04-28 19:31:10 +00:00
|
|
|
data: Option<PinnedBuf>,
|
2019-06-18 01:02:08 +00:00
|
|
|
) -> CliOpResult {
|
2019-04-28 19:31:10 +00:00
|
|
|
assert!(data.is_none());
|
2019-02-09 21:55:40 +00:00
|
|
|
let inner = base.inner_as_format_error().unwrap();
|
|
|
|
let orig_error = String::from(inner.error().unwrap());
|
|
|
|
|
|
|
|
let js_error = JSError::from_v8_exception(&orig_error).unwrap();
|
2019-04-08 21:10:00 +00:00
|
|
|
let js_error_mapped = apply_source_map(&js_error, &state.dir);
|
2019-02-28 21:19:04 +00:00
|
|
|
let js_error_string = JSErrorColor(&js_error_mapped).to_string();
|
2019-02-09 21:55:40 +00:00
|
|
|
|
|
|
|
let mut builder = FlatBufferBuilder::new();
|
|
|
|
let new_error = builder.create_string(&js_error_string);
|
|
|
|
|
|
|
|
let inner = msg::FormatErrorRes::create(
|
|
|
|
&mut builder,
|
|
|
|
&msg::FormatErrorResArgs {
|
|
|
|
error: Some(new_error),
|
|
|
|
},
|
|
|
|
);
|
|
|
|
|
2019-06-18 01:02:08 +00:00
|
|
|
let response_buf = serialize_response(
|
2019-06-14 17:58:20 +00:00
|
|
|
base.cmd_id(),
|
2019-02-09 21:55:40 +00:00
|
|
|
&mut builder,
|
|
|
|
msg::BaseArgs {
|
|
|
|
inner_type: msg::Any::FormatErrorRes,
|
|
|
|
inner: Some(inner.as_union_value()),
|
|
|
|
..Default::default()
|
|
|
|
},
|
2019-06-18 01:02:08 +00:00
|
|
|
);
|
|
|
|
|
|
|
|
ok_buf(response_buf)
|
2019-02-09 21:55:40 +00:00
|
|
|
}
|
|
|
|
|
2018-09-09 23:48:10 +00:00
|
|
|
fn serialize_response(
|
2019-06-14 17:58:20 +00:00
|
|
|
cmd_id: u32,
|
2019-01-14 06:30:38 +00:00
|
|
|
builder: &mut FlatBufferBuilder<'_>,
|
2019-06-14 17:58:20 +00:00
|
|
|
mut args: msg::BaseArgs<'_>,
|
2018-09-06 02:13:36 +00:00
|
|
|
) -> Buf {
|
2019-06-14 17:58:20 +00:00
|
|
|
args.cmd_id = cmd_id;
|
2018-08-17 19:11:28 +00:00
|
|
|
let base = msg::Base::create(builder, &args);
|
|
|
|
msg::finish_base_buffer(builder, base);
|
2018-09-02 05:58:23 +00:00
|
|
|
let data = builder.finished_data();
|
2018-09-09 23:48:10 +00:00
|
|
|
// println!("serialize_response {:x?}", data);
|
2018-12-05 04:36:10 +00:00
|
|
|
data.into()
|
2018-08-09 19:17:08 +00:00
|
|
|
}
|
|
|
|
|
2018-12-11 13:36:34 +00:00
|
|
|
#[inline]
|
2019-06-18 01:02:08 +00:00
|
|
|
pub fn ok_future(buf: Buf) -> CliOpResult {
|
|
|
|
Ok(Op::Async(Box::new(futures::future::ok(buf))))
|
2018-09-06 02:13:36 +00:00
|
|
|
}
|
|
|
|
|
2018-12-11 13:36:34 +00:00
|
|
|
#[inline]
|
2019-06-18 01:02:08 +00:00
|
|
|
pub fn ok_buf(buf: Buf) -> CliOpResult {
|
|
|
|
Ok(Op::Sync(buf))
|
2018-08-09 19:17:08 +00:00
|
|
|
}
|
|
|
|
|
2019-05-20 16:06:57 +00:00
|
|
|
fn op_cache(
|
|
|
|
state: &ThreadSafeState,
|
|
|
|
base: &msg::Base<'_>,
|
|
|
|
data: Option<PinnedBuf>,
|
2019-06-18 01:02:08 +00:00
|
|
|
) -> CliOpResult {
|
2019-05-20 16:06:57 +00:00
|
|
|
assert!(data.is_none());
|
|
|
|
let inner = base.inner_as_cache().unwrap();
|
|
|
|
let extension = inner.extension().unwrap();
|
|
|
|
let module_id = inner.module_id().unwrap();
|
|
|
|
let contents = inner.contents().unwrap();
|
|
|
|
|
|
|
|
state.mark_compiled(&module_id);
|
|
|
|
|
|
|
|
// TODO It shouldn't be necessary to call fetch_module_meta_data() here.
|
|
|
|
// However, we need module_meta_data.source_code in order to calculate the
|
|
|
|
// cache path. In the future, checksums will not be used in the cache
|
|
|
|
// filenames and this requirement can be removed. See
|
|
|
|
// https://github.com/denoland/deno/issues/2057
|
2019-06-18 01:02:08 +00:00
|
|
|
let module_meta_data = state
|
|
|
|
.dir
|
|
|
|
.fetch_module_meta_data(module_id, ".", true, true)?;
|
2019-05-20 16:06:57 +00:00
|
|
|
|
|
|
|
let (js_cache_path, source_map_path) = state
|
|
|
|
.dir
|
|
|
|
.cache_path(&module_meta_data.filename, &module_meta_data.source_code);
|
|
|
|
|
|
|
|
if extension == ".map" {
|
|
|
|
debug!("cache {:?}", source_map_path);
|
2019-06-18 01:02:08 +00:00
|
|
|
fs::write(source_map_path, contents).map_err(DenoError::from)?;
|
2019-05-20 16:06:57 +00:00
|
|
|
} else if extension == ".js" {
|
|
|
|
debug!("cache {:?}", js_cache_path);
|
2019-06-18 01:02:08 +00:00
|
|
|
fs::write(js_cache_path, contents).map_err(DenoError::from)?;
|
2019-05-20 16:06:57 +00:00
|
|
|
} else {
|
|
|
|
unreachable!();
|
|
|
|
}
|
|
|
|
|
2019-06-18 01:02:08 +00:00
|
|
|
ok_buf(empty_buf())
|
2019-05-20 16:06:57 +00:00
|
|
|
}
|
|
|
|
|
2018-10-07 03:06:20 +00:00
|
|
|
// https://github.com/denoland/deno/blob/golang/os.go#L100-L154
|
2019-02-18 15:42:15 +00:00
|
|
|
fn op_fetch_module_meta_data(
|
2019-04-09 17:11:25 +00:00
|
|
|
state: &ThreadSafeState,
|
2019-01-14 06:30:38 +00:00
|
|
|
base: &msg::Base<'_>,
|
2019-04-28 19:31:10 +00:00
|
|
|
data: Option<PinnedBuf>,
|
2019-06-18 01:02:08 +00:00
|
|
|
) -> CliOpResult {
|
|
|
|
if !base.sync() {
|
|
|
|
return Err(errors::no_async_support());
|
|
|
|
}
|
2019-04-28 19:31:10 +00:00
|
|
|
assert!(data.is_none());
|
2019-02-18 15:42:15 +00:00
|
|
|
let inner = base.inner_as_fetch_module_meta_data().unwrap();
|
2019-06-14 17:58:20 +00:00
|
|
|
let cmd_id = base.cmd_id();
|
2018-12-11 16:23:59 +00:00
|
|
|
let specifier = inner.specifier().unwrap();
|
|
|
|
let referrer = inner.referrer().unwrap();
|
2018-07-20 01:14:32 +00:00
|
|
|
|
2019-04-08 21:10:00 +00:00
|
|
|
assert_eq!(state.dir.root.join("gen"), state.dir.gen, "Sanity check");
|
2018-07-06 15:27:36 +00:00
|
|
|
|
2019-04-08 21:10:00 +00:00
|
|
|
let use_cache = !state.flags.reload;
|
2019-05-03 15:09:51 +00:00
|
|
|
let no_fetch = state.flags.no_fetch;
|
2019-03-28 20:05:41 +00:00
|
|
|
|
2019-06-09 13:08:20 +00:00
|
|
|
// TODO(bartlomieju): I feel this is wrong - specifier is only resolved if there's an
|
|
|
|
// import map - why it is not always resolved? Eg. "bad-module.ts" will return NotFound
|
|
|
|
// error whilst it should return RelativeUrlWithCannotBeABaseBase error
|
|
|
|
let resolved_specifier = match &state.import_map {
|
2019-06-12 23:55:59 +00:00
|
|
|
Some(import_map) => match import_map.resolve(specifier, referrer) {
|
|
|
|
Ok(result) => match result {
|
|
|
|
Some(module_specifier) => module_specifier.to_string(),
|
|
|
|
None => specifier.to_string(),
|
|
|
|
},
|
2019-06-18 01:02:08 +00:00
|
|
|
Err(err) => return Err(DenoError::from(err)),
|
2019-06-12 23:55:59 +00:00
|
|
|
},
|
2019-06-09 13:08:20 +00:00
|
|
|
None => specifier.to_string(),
|
|
|
|
};
|
|
|
|
|
2019-05-28 13:32:43 +00:00
|
|
|
let fut = state
|
|
|
|
.dir
|
2019-06-09 13:08:20 +00:00
|
|
|
.fetch_module_meta_data_async(
|
|
|
|
&resolved_specifier,
|
|
|
|
referrer,
|
|
|
|
use_cache,
|
|
|
|
no_fetch,
|
|
|
|
).and_then(move |out| {
|
2019-05-28 13:32:43 +00:00
|
|
|
let builder = &mut FlatBufferBuilder::new();
|
|
|
|
let data_off = builder.create_vector(out.source_code.as_slice());
|
|
|
|
let msg_args = msg::FetchModuleMetaDataResArgs {
|
|
|
|
module_name: Some(builder.create_string(&out.module_name)),
|
|
|
|
filename: Some(builder.create_string(&out.filename)),
|
|
|
|
media_type: out.media_type,
|
|
|
|
data: Some(data_off),
|
|
|
|
};
|
|
|
|
let inner = msg::FetchModuleMetaDataRes::create(builder, &msg_args);
|
|
|
|
Ok(serialize_response(
|
2019-06-14 17:58:20 +00:00
|
|
|
cmd_id,
|
2019-05-28 13:32:43 +00:00
|
|
|
builder,
|
|
|
|
msg::BaseArgs {
|
|
|
|
inner: Some(inner.as_union_value()),
|
|
|
|
inner_type: msg::Any::FetchModuleMetaDataRes,
|
|
|
|
..Default::default()
|
|
|
|
},
|
|
|
|
))
|
|
|
|
});
|
|
|
|
|
|
|
|
// WARNING: Here we use tokio_util::block_on() which starts a new Tokio
|
|
|
|
// runtime for executing the future. This is so we don't inadvernently run
|
|
|
|
// out of threads in the main runtime.
|
2019-06-18 01:02:08 +00:00
|
|
|
let result_buf = tokio_util::block_on(fut)?;
|
|
|
|
Ok(Op::Sync(result_buf))
|
2018-07-13 07:24:07 +00:00
|
|
|
}
|
|
|
|
|
2018-10-13 20:03:27 +00:00
|
|
|
fn op_chdir(
|
2019-04-09 17:11:25 +00:00
|
|
|
_state: &ThreadSafeState,
|
2019-01-14 06:30:38 +00:00
|
|
|
base: &msg::Base<'_>,
|
2019-04-28 19:31:10 +00:00
|
|
|
data: Option<PinnedBuf>,
|
2019-06-18 01:02:08 +00:00
|
|
|
) -> CliOpResult {
|
2019-04-28 19:31:10 +00:00
|
|
|
assert!(data.is_none());
|
2018-10-13 20:03:27 +00:00
|
|
|
let inner = base.inner_as_chdir().unwrap();
|
|
|
|
let directory = inner.directory().unwrap();
|
2019-06-18 01:02:08 +00:00
|
|
|
std::env::set_current_dir(&directory)?;
|
|
|
|
ok_buf(empty_buf())
|
2018-10-13 20:03:27 +00:00
|
|
|
}
|
|
|
|
|
2019-03-10 19:37:05 +00:00
|
|
|
fn op_global_timer_stop(
|
2019-04-09 17:11:25 +00:00
|
|
|
state: &ThreadSafeState,
|
2019-01-14 06:30:38 +00:00
|
|
|
base: &msg::Base<'_>,
|
2019-04-28 19:31:10 +00:00
|
|
|
data: Option<PinnedBuf>,
|
2019-06-18 01:02:08 +00:00
|
|
|
) -> CliOpResult {
|
|
|
|
if !base.sync() {
|
|
|
|
return Err(errors::no_async_support());
|
|
|
|
}
|
2019-04-28 19:31:10 +00:00
|
|
|
assert!(data.is_none());
|
2019-04-08 21:10:00 +00:00
|
|
|
let state = state;
|
2019-03-20 00:55:59 +00:00
|
|
|
let mut t = state.global_timer.lock().unwrap();
|
2019-03-10 19:37:05 +00:00
|
|
|
t.cancel();
|
2019-06-18 01:02:08 +00:00
|
|
|
Ok(Op::Sync(empty_buf()))
|
2018-09-25 02:46:36 +00:00
|
|
|
}
|
|
|
|
|
2019-03-10 19:37:05 +00:00
|
|
|
fn op_global_timer(
|
2019-04-09 17:11:25 +00:00
|
|
|
state: &ThreadSafeState,
|
2019-03-10 19:37:05 +00:00
|
|
|
base: &msg::Base<'_>,
|
2019-04-28 19:31:10 +00:00
|
|
|
data: Option<PinnedBuf>,
|
2019-06-18 01:02:08 +00:00
|
|
|
) -> CliOpResult {
|
|
|
|
if base.sync() {
|
|
|
|
return Err(errors::no_sync_support());
|
|
|
|
}
|
2019-04-28 19:31:10 +00:00
|
|
|
assert!(data.is_none());
|
2019-06-14 17:58:20 +00:00
|
|
|
let cmd_id = base.cmd_id();
|
2019-03-10 19:37:05 +00:00
|
|
|
let inner = base.inner_as_global_timer().unwrap();
|
|
|
|
let val = inner.timeout();
|
|
|
|
assert!(val >= 0);
|
|
|
|
|
2019-04-08 21:10:00 +00:00
|
|
|
let state = state;
|
2019-03-20 00:55:59 +00:00
|
|
|
let mut t = state.global_timer.lock().unwrap();
|
2019-03-10 19:37:05 +00:00
|
|
|
let deadline = Instant::now() + Duration::from_millis(val as u64);
|
|
|
|
let f = t.new_timeout(deadline);
|
|
|
|
|
2019-06-18 01:02:08 +00:00
|
|
|
Ok(Op::Async(Box::new(f.then(move |_| {
|
2019-03-10 19:37:05 +00:00
|
|
|
let builder = &mut FlatBufferBuilder::new();
|
|
|
|
let inner =
|
|
|
|
msg::GlobalTimerRes::create(builder, &msg::GlobalTimerResArgs {});
|
|
|
|
Ok(serialize_response(
|
2019-06-14 17:58:20 +00:00
|
|
|
cmd_id,
|
2019-03-10 19:37:05 +00:00
|
|
|
builder,
|
|
|
|
msg::BaseArgs {
|
|
|
|
inner: Some(inner.as_union_value()),
|
|
|
|
inner_type: msg::Any::GlobalTimerRes,
|
|
|
|
..Default::default()
|
|
|
|
},
|
|
|
|
))
|
2019-06-18 01:02:08 +00:00
|
|
|
}))))
|
2019-03-10 19:37:05 +00:00
|
|
|
}
|
|
|
|
|
2018-10-04 00:48:02 +00:00
|
|
|
fn op_set_env(
|
2019-04-09 17:11:25 +00:00
|
|
|
state: &ThreadSafeState,
|
2019-01-14 06:30:38 +00:00
|
|
|
base: &msg::Base<'_>,
|
2019-04-28 19:31:10 +00:00
|
|
|
data: Option<PinnedBuf>,
|
2019-06-18 01:02:08 +00:00
|
|
|
) -> CliOpResult {
|
2019-04-28 19:31:10 +00:00
|
|
|
assert!(data.is_none());
|
2018-10-04 01:12:23 +00:00
|
|
|
let inner = base.inner_as_set_env().unwrap();
|
|
|
|
let key = inner.key().unwrap();
|
|
|
|
let value = inner.value().unwrap();
|
2019-06-18 01:02:08 +00:00
|
|
|
state.check_env()?;
|
2018-08-31 11:51:12 +00:00
|
|
|
std::env::set_var(key, value);
|
2019-06-18 01:02:08 +00:00
|
|
|
ok_buf(empty_buf())
|
2018-08-31 11:51:12 +00:00
|
|
|
}
|
|
|
|
|
2019-03-20 00:55:59 +00:00
|
|
|
fn op_env(
|
2019-04-09 17:11:25 +00:00
|
|
|
state: &ThreadSafeState,
|
2019-06-14 17:58:20 +00:00
|
|
|
base: &msg::Base<'_>,
|
2019-04-28 19:31:10 +00:00
|
|
|
data: Option<PinnedBuf>,
|
2019-06-18 01:02:08 +00:00
|
|
|
) -> CliOpResult {
|
2019-04-28 19:31:10 +00:00
|
|
|
assert!(data.is_none());
|
2019-06-14 17:58:20 +00:00
|
|
|
let cmd_id = base.cmd_id();
|
2018-09-18 18:53:16 +00:00
|
|
|
|
2019-06-18 01:02:08 +00:00
|
|
|
state.check_env()?;
|
2018-08-31 11:51:12 +00:00
|
|
|
|
2018-09-06 02:13:36 +00:00
|
|
|
let builder = &mut FlatBufferBuilder::new();
|
2018-09-02 05:59:16 +00:00
|
|
|
let vars: Vec<_> = std::env::vars()
|
2018-11-03 00:09:10 +00:00
|
|
|
.map(|(key, value)| msg_util::serialize_key_value(builder, &key, &value))
|
|
|
|
.collect();
|
2018-09-02 05:58:23 +00:00
|
|
|
let tables = builder.create_vector(&vars);
|
2018-10-04 01:12:23 +00:00
|
|
|
let inner = msg::EnvironRes::create(
|
2018-08-31 11:51:12 +00:00
|
|
|
builder,
|
2018-11-04 14:04:24 +00:00
|
|
|
&msg::EnvironResArgs { map: Some(tables) },
|
2018-08-31 11:51:12 +00:00
|
|
|
);
|
2019-06-18 01:02:08 +00:00
|
|
|
let response_buf = serialize_response(
|
2019-06-14 17:58:20 +00:00
|
|
|
cmd_id,
|
2018-08-31 11:51:12 +00:00
|
|
|
builder,
|
2018-09-06 02:13:36 +00:00
|
|
|
msg::BaseArgs {
|
2018-10-04 01:12:23 +00:00
|
|
|
inner: Some(inner.as_union_value()),
|
|
|
|
inner_type: msg::Any::EnvironRes,
|
2018-08-31 11:51:12 +00:00
|
|
|
..Default::default()
|
|
|
|
},
|
2019-06-18 01:02:08 +00:00
|
|
|
);
|
|
|
|
ok_buf(response_buf)
|
2018-08-31 11:51:12 +00:00
|
|
|
}
|
|
|
|
|
2019-03-04 16:04:19 +00:00
|
|
|
fn op_permissions(
|
2019-04-09 17:11:25 +00:00
|
|
|
state: &ThreadSafeState,
|
2019-06-14 17:58:20 +00:00
|
|
|
base: &msg::Base<'_>,
|
2019-04-28 19:31:10 +00:00
|
|
|
data: Option<PinnedBuf>,
|
2019-06-18 01:02:08 +00:00
|
|
|
) -> CliOpResult {
|
2019-04-28 19:31:10 +00:00
|
|
|
assert!(data.is_none());
|
2019-06-14 17:58:20 +00:00
|
|
|
let cmd_id = base.cmd_id();
|
2019-03-04 16:04:19 +00:00
|
|
|
let builder = &mut FlatBufferBuilder::new();
|
|
|
|
let inner = msg::PermissionsRes::create(
|
|
|
|
builder,
|
|
|
|
&msg::PermissionsResArgs {
|
2019-04-08 21:10:00 +00:00
|
|
|
run: state.permissions.allows_run(),
|
|
|
|
read: state.permissions.allows_read(),
|
|
|
|
write: state.permissions.allows_write(),
|
|
|
|
net: state.permissions.allows_net(),
|
|
|
|
env: state.permissions.allows_env(),
|
2019-05-23 16:28:29 +00:00
|
|
|
hrtime: state.permissions.allows_hrtime(),
|
2019-03-04 16:04:19 +00:00
|
|
|
},
|
|
|
|
);
|
2019-06-18 01:02:08 +00:00
|
|
|
let response_buf = serialize_response(
|
2019-06-14 17:58:20 +00:00
|
|
|
cmd_id,
|
2019-03-04 16:04:19 +00:00
|
|
|
builder,
|
|
|
|
msg::BaseArgs {
|
|
|
|
inner: Some(inner.as_union_value()),
|
|
|
|
inner_type: msg::Any::PermissionsRes,
|
|
|
|
..Default::default()
|
|
|
|
},
|
2019-06-18 01:02:08 +00:00
|
|
|
);
|
|
|
|
ok_buf(response_buf)
|
2019-03-04 16:04:19 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
fn op_revoke_permission(
|
2019-04-09 17:11:25 +00:00
|
|
|
state: &ThreadSafeState,
|
2019-03-04 16:04:19 +00:00
|
|
|
base: &msg::Base<'_>,
|
2019-04-28 19:31:10 +00:00
|
|
|
data: Option<PinnedBuf>,
|
2019-06-18 01:02:08 +00:00
|
|
|
) -> CliOpResult {
|
2019-04-28 19:31:10 +00:00
|
|
|
assert!(data.is_none());
|
2019-03-04 16:04:19 +00:00
|
|
|
let inner = base.inner_as_permission_revoke().unwrap();
|
|
|
|
let permission = inner.permission().unwrap();
|
2019-06-18 01:02:08 +00:00
|
|
|
match permission {
|
2019-04-08 21:10:00 +00:00
|
|
|
"run" => state.permissions.revoke_run(),
|
|
|
|
"read" => state.permissions.revoke_read(),
|
|
|
|
"write" => state.permissions.revoke_write(),
|
|
|
|
"net" => state.permissions.revoke_net(),
|
|
|
|
"env" => state.permissions.revoke_env(),
|
2019-05-23 16:28:29 +00:00
|
|
|
"hrtime" => state.permissions.revoke_hrtime(),
|
2019-03-04 16:04:19 +00:00
|
|
|
_ => Ok(()),
|
2019-06-18 01:02:08 +00:00
|
|
|
}?;
|
|
|
|
ok_buf(empty_buf())
|
2019-03-04 16:04:19 +00:00
|
|
|
}
|
|
|
|
|
2018-10-30 19:02:18 +00:00
|
|
|
fn op_fetch(
|
2019-04-09 17:11:25 +00:00
|
|
|
state: &ThreadSafeState,
|
2019-01-14 06:30:38 +00:00
|
|
|
base: &msg::Base<'_>,
|
2019-04-28 19:31:10 +00:00
|
|
|
data: Option<PinnedBuf>,
|
2019-06-18 01:02:08 +00:00
|
|
|
) -> CliOpResult {
|
2018-10-30 19:02:18 +00:00
|
|
|
let inner = base.inner_as_fetch().unwrap();
|
2019-06-14 17:58:20 +00:00
|
|
|
let cmd_id = base.cmd_id();
|
2018-11-15 01:36:34 +00:00
|
|
|
|
|
|
|
let header = inner.header().unwrap();
|
|
|
|
assert!(header.is_request());
|
|
|
|
let url = header.url().unwrap();
|
|
|
|
|
2019-04-28 19:31:10 +00:00
|
|
|
let body = match data {
|
|
|
|
None => hyper::Body::empty(),
|
|
|
|
Some(buf) => hyper::Body::from(Vec::from(&*buf)),
|
2018-11-15 02:19:38 +00:00
|
|
|
};
|
|
|
|
|
2019-06-18 01:02:08 +00:00
|
|
|
let req = msg_util::deserialize_request(header, body)?;
|
2018-09-06 02:13:36 +00:00
|
|
|
|
2019-06-18 01:02:08 +00:00
|
|
|
let url_ = url::Url::parse(url).map_err(DenoError::from)?;
|
|
|
|
state.check_net_url(url_)?;
|
2018-09-06 02:13:36 +00:00
|
|
|
|
2018-10-26 15:01:43 +00:00
|
|
|
let client = http_util::get_client();
|
2018-08-16 00:57:36 +00:00
|
|
|
|
2018-09-18 18:53:16 +00:00
|
|
|
debug!("Before fetch {}", url);
|
2018-11-15 01:36:34 +00:00
|
|
|
let future =
|
|
|
|
client
|
|
|
|
.request(req)
|
|
|
|
.map_err(DenoError::from)
|
|
|
|
.and_then(move |res| {
|
|
|
|
let builder = &mut FlatBufferBuilder::new();
|
|
|
|
let header_off = msg_util::serialize_http_response(builder, &res);
|
|
|
|
let body = res.into_body();
|
|
|
|
let body_resource = resources::add_hyper_body(body);
|
|
|
|
let inner = msg::FetchRes::create(
|
|
|
|
builder,
|
|
|
|
&msg::FetchResArgs {
|
|
|
|
header: Some(header_off),
|
|
|
|
body_rid: body_resource.rid,
|
|
|
|
},
|
|
|
|
);
|
2018-09-12 19:16:42 +00:00
|
|
|
|
2018-11-15 01:36:34 +00:00
|
|
|
Ok(serialize_response(
|
2019-06-14 17:58:20 +00:00
|
|
|
cmd_id,
|
2018-11-15 01:36:34 +00:00
|
|
|
builder,
|
|
|
|
msg::BaseArgs {
|
|
|
|
inner: Some(inner.as_union_value()),
|
|
|
|
inner_type: msg::Any::FetchRes,
|
|
|
|
..Default::default()
|
|
|
|
},
|
|
|
|
))
|
|
|
|
});
|
2019-06-18 01:02:08 +00:00
|
|
|
if base.sync() {
|
|
|
|
let result_buf = future.wait()?;
|
|
|
|
Ok(Op::Sync(result_buf))
|
|
|
|
} else {
|
|
|
|
Ok(Op::Async(Box::new(future)))
|
|
|
|
}
|
2018-08-16 00:57:36 +00:00
|
|
|
}
|
|
|
|
|
2018-09-22 08:42:07 +00:00
|
|
|
// This is just type conversion. Implement From trait?
|
|
|
|
// See https://github.com/tokio-rs/tokio/blob/ffd73a64e7ec497622b7f939e38017afe7124dc4/tokio-fs/src/lib.rs#L76-L85
|
|
|
|
fn convert_blocking<F>(f: F) -> Poll<Buf, DenoError>
|
|
|
|
where
|
|
|
|
F: FnOnce() -> DenoResult<Buf>,
|
|
|
|
{
|
|
|
|
use futures::Async::*;
|
|
|
|
match tokio_threadpool::blocking(f) {
|
|
|
|
Ok(Ready(Ok(v))) => Ok(v.into()),
|
|
|
|
Ok(Ready(Err(err))) => Err(err),
|
|
|
|
Ok(NotReady) => Ok(NotReady),
|
2019-03-14 23:17:52 +00:00
|
|
|
Err(err) => panic!("blocking error {}", err),
|
2018-09-22 08:42:07 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-06-18 01:02:08 +00:00
|
|
|
fn blocking<F>(is_sync: bool, f: F) -> CliOpResult
|
2018-12-11 13:36:34 +00:00
|
|
|
where
|
|
|
|
F: 'static + Send + FnOnce() -> DenoResult<Buf>,
|
|
|
|
{
|
|
|
|
if is_sync {
|
2019-06-18 01:02:08 +00:00
|
|
|
let result_buf = f()?;
|
|
|
|
Ok(Op::Sync(result_buf))
|
2018-12-11 13:36:34 +00:00
|
|
|
} else {
|
2019-06-18 01:02:08 +00:00
|
|
|
Ok(Op::Async(Box::new(tokio_util::poll_fn(move || {
|
|
|
|
convert_blocking(f)
|
|
|
|
}))))
|
2018-12-11 13:36:34 +00:00
|
|
|
}
|
2018-09-22 08:42:07 +00:00
|
|
|
}
|
|
|
|
|
2018-10-04 00:48:02 +00:00
|
|
|
fn op_make_temp_dir(
|
2019-04-09 17:11:25 +00:00
|
|
|
state: &ThreadSafeState,
|
2019-01-14 06:30:38 +00:00
|
|
|
base: &msg::Base<'_>,
|
2019-04-28 19:31:10 +00:00
|
|
|
data: Option<PinnedBuf>,
|
2019-06-18 01:02:08 +00:00
|
|
|
) -> CliOpResult {
|
2019-04-28 19:31:10 +00:00
|
|
|
assert!(data.is_none());
|
2018-09-06 02:13:36 +00:00
|
|
|
let base = Box::new(*base);
|
2018-10-04 01:12:23 +00:00
|
|
|
let inner = base.inner_as_make_temp_dir().unwrap();
|
2019-06-14 17:58:20 +00:00
|
|
|
let cmd_id = base.cmd_id();
|
2018-09-06 02:13:36 +00:00
|
|
|
|
2018-10-27 13:11:39 +00:00
|
|
|
// FIXME
|
2019-06-18 01:02:08 +00:00
|
|
|
state.check_write("make_temp")?;
|
2018-09-22 08:42:07 +00:00
|
|
|
|
2018-10-04 01:12:23 +00:00
|
|
|
let dir = inner.dir().map(PathBuf::from);
|
|
|
|
let prefix = inner.prefix().map(String::from);
|
|
|
|
let suffix = inner.suffix().map(String::from);
|
2018-09-22 08:42:07 +00:00
|
|
|
|
2019-06-18 01:02:08 +00:00
|
|
|
blocking(base.sync(), move || {
|
2018-09-06 02:13:36 +00:00
|
|
|
// TODO(piscisaureus): use byte vector for paths, not a string.
|
2018-10-07 03:06:20 +00:00
|
|
|
// See https://github.com/denoland/deno/issues/627.
|
2018-09-06 02:13:36 +00:00
|
|
|
// We can't assume that paths are always valid utf8 strings.
|
2018-09-22 08:42:07 +00:00
|
|
|
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),
|
|
|
|
)?;
|
2018-09-06 02:13:36 +00:00
|
|
|
let builder = &mut FlatBufferBuilder::new();
|
|
|
|
let path_off = builder.create_string(path.to_str().unwrap());
|
2018-10-04 01:12:23 +00:00
|
|
|
let inner = msg::MakeTempDirRes::create(
|
2018-09-06 02:13:36 +00:00
|
|
|
builder,
|
|
|
|
&msg::MakeTempDirResArgs {
|
|
|
|
path: Some(path_off),
|
|
|
|
},
|
|
|
|
);
|
2018-09-09 23:48:10 +00:00
|
|
|
Ok(serialize_response(
|
2019-06-14 17:58:20 +00:00
|
|
|
cmd_id,
|
2018-09-06 02:13:36 +00:00
|
|
|
builder,
|
|
|
|
msg::BaseArgs {
|
2018-10-04 01:12:23 +00:00
|
|
|
inner: Some(inner.as_union_value()),
|
|
|
|
inner_type: msg::Any::MakeTempDirRes,
|
2018-09-06 02:13:36 +00:00
|
|
|
..Default::default()
|
|
|
|
},
|
|
|
|
))
|
2018-09-22 08:42:07 +00:00
|
|
|
})
|
2018-08-23 22:36:45 +00:00
|
|
|
}
|
|
|
|
|
2018-10-04 00:48:02 +00:00
|
|
|
fn op_mkdir(
|
2019-04-09 17:11:25 +00:00
|
|
|
state: &ThreadSafeState,
|
2019-01-14 06:30:38 +00:00
|
|
|
base: &msg::Base<'_>,
|
2019-04-28 19:31:10 +00:00
|
|
|
data: Option<PinnedBuf>,
|
2019-06-18 01:02:08 +00:00
|
|
|
) -> CliOpResult {
|
2019-04-28 19:31:10 +00:00
|
|
|
assert!(data.is_none());
|
2018-10-04 01:12:23 +00:00
|
|
|
let inner = base.inner_as_mkdir().unwrap();
|
2019-06-18 01:02:08 +00:00
|
|
|
let (path, path_) = resolve_path(inner.path().unwrap())?;
|
2019-01-18 04:39:06 +00:00
|
|
|
let recursive = inner.recursive();
|
|
|
|
let mode = inner.mode();
|
2018-09-18 18:53:16 +00:00
|
|
|
|
2019-06-18 01:02:08 +00:00
|
|
|
state.check_write(&path_)?;
|
2019-01-18 04:39:06 +00:00
|
|
|
|
2018-12-11 13:36:34 +00:00
|
|
|
blocking(base.sync(), move || {
|
2019-05-08 23:20:30 +00:00
|
|
|
debug!("op_mkdir {}", path_);
|
|
|
|
deno_fs::mkdir(&path, mode, recursive)?;
|
2018-09-18 18:53:16 +00:00
|
|
|
Ok(empty_buf())
|
2018-09-22 08:42:07 +00:00
|
|
|
})
|
2018-08-26 06:26:30 +00:00
|
|
|
}
|
|
|
|
|
2018-10-26 20:01:45 +00:00
|
|
|
fn op_chmod(
|
2019-04-09 17:11:25 +00:00
|
|
|
state: &ThreadSafeState,
|
2019-01-14 06:30:38 +00:00
|
|
|
base: &msg::Base<'_>,
|
2019-04-28 19:31:10 +00:00
|
|
|
data: Option<PinnedBuf>,
|
2019-06-18 01:02:08 +00:00
|
|
|
) -> CliOpResult {
|
2019-04-28 19:31:10 +00:00
|
|
|
assert!(data.is_none());
|
2018-10-26 20:01:45 +00:00
|
|
|
let inner = base.inner_as_chmod().unwrap();
|
|
|
|
let _mode = inner.mode();
|
2019-06-18 01:02:08 +00:00
|
|
|
let (path, path_) = resolve_path(inner.path().unwrap())?;
|
2018-10-26 20:01:45 +00:00
|
|
|
|
2019-06-18 01:02:08 +00:00
|
|
|
state.check_write(&path_)?;
|
2018-10-26 20:01:45 +00:00
|
|
|
|
2018-12-11 13:36:34 +00:00
|
|
|
blocking(base.sync(), move || {
|
2019-05-08 23:20:30 +00:00
|
|
|
debug!("op_chmod {}", &path_);
|
2018-10-26 20:01:45 +00:00
|
|
|
// Still check file/dir exists on windows
|
|
|
|
let _metadata = fs::metadata(&path)?;
|
|
|
|
#[cfg(any(unix))]
|
|
|
|
{
|
|
|
|
let mut permissions = _metadata.permissions();
|
|
|
|
permissions.set_mode(_mode);
|
|
|
|
fs::set_permissions(&path, permissions)?;
|
|
|
|
}
|
|
|
|
Ok(empty_buf())
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
2019-05-08 01:58:58 +00:00
|
|
|
fn op_chown(
|
|
|
|
state: &ThreadSafeState,
|
|
|
|
base: &msg::Base<'_>,
|
|
|
|
data: Option<PinnedBuf>,
|
2019-06-18 01:02:08 +00:00
|
|
|
) -> CliOpResult {
|
2019-05-08 01:58:58 +00:00
|
|
|
assert!(data.is_none());
|
|
|
|
let inner = base.inner_as_chown().unwrap();
|
|
|
|
let path = String::from(inner.path().unwrap());
|
|
|
|
let uid = inner.uid();
|
|
|
|
let gid = inner.gid();
|
|
|
|
|
2019-06-18 01:02:08 +00:00
|
|
|
state.check_write(&path)?;
|
2019-05-08 01:58:58 +00:00
|
|
|
|
|
|
|
blocking(base.sync(), move || {
|
|
|
|
debug!("op_chown {}", &path);
|
|
|
|
match deno_fs::chown(&path, uid, gid) {
|
|
|
|
Ok(_) => Ok(empty_buf()),
|
|
|
|
Err(e) => Err(e),
|
|
|
|
}
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
2018-10-04 00:48:02 +00:00
|
|
|
fn op_open(
|
2019-04-09 17:11:25 +00:00
|
|
|
state: &ThreadSafeState,
|
2019-01-14 06:30:38 +00:00
|
|
|
base: &msg::Base<'_>,
|
2019-04-28 19:31:10 +00:00
|
|
|
data: Option<PinnedBuf>,
|
2019-06-18 01:02:08 +00:00
|
|
|
) -> CliOpResult {
|
2019-04-28 19:31:10 +00:00
|
|
|
assert!(data.is_none());
|
2019-06-14 17:58:20 +00:00
|
|
|
let cmd_id = base.cmd_id();
|
2018-10-04 01:12:23 +00:00
|
|
|
let inner = base.inner_as_open().unwrap();
|
2019-06-18 01:02:08 +00:00
|
|
|
let (filename, filename_) = resolve_path(inner.filename().unwrap())?;
|
2018-12-12 17:05:58 +00:00
|
|
|
let mode = inner.mode().unwrap();
|
2018-09-27 04:56:39 +00:00
|
|
|
|
2018-12-12 17:05:58 +00:00
|
|
|
let mut open_options = tokio::fs::OpenOptions::new();
|
|
|
|
|
|
|
|
match mode {
|
|
|
|
"r" => {
|
|
|
|
open_options.read(true);
|
|
|
|
}
|
|
|
|
"r+" => {
|
|
|
|
open_options.read(true).write(true);
|
|
|
|
}
|
|
|
|
"w" => {
|
|
|
|
open_options.create(true).write(true).truncate(true);
|
|
|
|
}
|
|
|
|
"w+" => {
|
|
|
|
open_options
|
|
|
|
.read(true)
|
|
|
|
.create(true)
|
|
|
|
.write(true)
|
|
|
|
.truncate(true);
|
|
|
|
}
|
|
|
|
"a" => {
|
|
|
|
open_options.create(true).append(true);
|
|
|
|
}
|
|
|
|
"a+" => {
|
|
|
|
open_options.read(true).create(true).append(true);
|
|
|
|
}
|
|
|
|
"x" => {
|
|
|
|
open_options.create_new(true).write(true);
|
|
|
|
}
|
|
|
|
"x+" => {
|
|
|
|
open_options.create_new(true).read(true).write(true);
|
|
|
|
}
|
|
|
|
&_ => {
|
|
|
|
panic!("Unknown file open mode.");
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-02-08 20:59:38 +00:00
|
|
|
match mode {
|
|
|
|
"r" => {
|
2019-06-18 01:02:08 +00:00
|
|
|
state.check_read(&filename_)?;
|
2019-02-08 20:59:38 +00:00
|
|
|
}
|
|
|
|
"w" | "a" | "x" => {
|
2019-06-18 01:02:08 +00:00
|
|
|
state.check_write(&filename_)?;
|
2019-02-08 20:59:38 +00:00
|
|
|
}
|
|
|
|
&_ => {
|
2019-06-18 01:02:08 +00:00
|
|
|
state.check_read(&filename_)?;
|
|
|
|
state.check_write(&filename_)?;
|
2018-12-14 08:29:17 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-12-12 17:05:58 +00:00
|
|
|
let op = open_options
|
|
|
|
.open(filename)
|
2018-11-04 14:04:24 +00:00
|
|
|
.map_err(DenoError::from)
|
2019-06-18 01:02:08 +00:00
|
|
|
.and_then(move |fs_file| {
|
2018-10-01 23:37:18 +00:00
|
|
|
let resource = resources::add_fs_file(fs_file);
|
2018-09-27 04:56:39 +00:00
|
|
|
let builder = &mut FlatBufferBuilder::new();
|
2018-11-04 14:04:24 +00:00
|
|
|
let inner =
|
|
|
|
msg::OpenRes::create(builder, &msg::OpenResArgs { rid: resource.rid });
|
2018-09-27 04:56:39 +00:00
|
|
|
Ok(serialize_response(
|
2019-06-14 17:58:20 +00:00
|
|
|
cmd_id,
|
2018-09-27 04:56:39 +00:00
|
|
|
builder,
|
|
|
|
msg::BaseArgs {
|
2018-10-04 01:12:23 +00:00
|
|
|
inner: Some(inner.as_union_value()),
|
|
|
|
inner_type: msg::Any::OpenRes,
|
2018-09-27 04:56:39 +00:00
|
|
|
..Default::default()
|
|
|
|
},
|
|
|
|
))
|
|
|
|
});
|
2019-06-18 01:02:08 +00:00
|
|
|
if base.sync() {
|
|
|
|
let buf = op.wait()?;
|
|
|
|
Ok(Op::Sync(buf))
|
|
|
|
} else {
|
|
|
|
Ok(Op::Async(Box::new(op)))
|
|
|
|
}
|
2018-09-27 04:56:39 +00:00
|
|
|
}
|
|
|
|
|
2018-10-04 00:48:02 +00:00
|
|
|
fn op_close(
|
2019-04-09 17:11:25 +00:00
|
|
|
_state: &ThreadSafeState,
|
2019-01-14 06:30:38 +00:00
|
|
|
base: &msg::Base<'_>,
|
2019-04-28 19:31:10 +00:00
|
|
|
data: Option<PinnedBuf>,
|
2019-06-18 01:02:08 +00:00
|
|
|
) -> CliOpResult {
|
2019-04-28 19:31:10 +00:00
|
|
|
assert!(data.is_none());
|
2018-10-04 01:41:59 +00:00
|
|
|
let inner = base.inner_as_close().unwrap();
|
|
|
|
let rid = inner.rid();
|
2018-10-04 03:58:29 +00:00
|
|
|
match resources::lookup(rid) {
|
2019-06-18 01:02:08 +00:00
|
|
|
None => Err(errors::bad_resource()),
|
2019-01-09 17:59:46 +00:00
|
|
|
Some(resource) => {
|
2018-10-04 03:58:29 +00:00
|
|
|
resource.close();
|
2019-06-18 01:02:08 +00:00
|
|
|
ok_buf(empty_buf())
|
2018-10-04 03:58:29 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-04-22 01:26:56 +00:00
|
|
|
fn op_kill(
|
|
|
|
_state: &ThreadSafeState,
|
|
|
|
base: &msg::Base<'_>,
|
2019-04-28 19:31:10 +00:00
|
|
|
data: Option<PinnedBuf>,
|
2019-06-18 01:02:08 +00:00
|
|
|
) -> CliOpResult {
|
2019-04-28 19:31:10 +00:00
|
|
|
assert!(data.is_none());
|
2019-04-22 01:26:56 +00:00
|
|
|
let inner = base.inner_as_kill().unwrap();
|
|
|
|
let pid = inner.pid();
|
|
|
|
let signo = inner.signo();
|
2019-06-18 01:02:08 +00:00
|
|
|
kill(pid, signo)?;
|
|
|
|
ok_buf(empty_buf())
|
2019-04-22 01:26:56 +00:00
|
|
|
}
|
|
|
|
|
2018-10-05 16:16:24 +00:00
|
|
|
fn op_shutdown(
|
2019-04-09 17:11:25 +00:00
|
|
|
_state: &ThreadSafeState,
|
2019-01-14 06:30:38 +00:00
|
|
|
base: &msg::Base<'_>,
|
2019-04-28 19:31:10 +00:00
|
|
|
data: Option<PinnedBuf>,
|
2019-06-18 01:02:08 +00:00
|
|
|
) -> CliOpResult {
|
2019-04-28 19:31:10 +00:00
|
|
|
assert!(data.is_none());
|
2018-10-05 16:16:24 +00:00
|
|
|
let inner = base.inner_as_shutdown().unwrap();
|
|
|
|
let rid = inner.rid();
|
|
|
|
let how = inner.how();
|
|
|
|
match resources::lookup(rid) {
|
2019-06-18 01:02:08 +00:00
|
|
|
None => Err(errors::bad_resource()),
|
2018-10-05 16:16:24 +00:00
|
|
|
Some(mut resource) => {
|
|
|
|
let shutdown_mode = match how {
|
|
|
|
0 => Shutdown::Read,
|
|
|
|
1 => Shutdown::Write,
|
|
|
|
_ => unimplemented!(),
|
|
|
|
};
|
2018-12-11 13:36:34 +00:00
|
|
|
blocking(base.sync(), move || {
|
2018-10-05 16:16:24 +00:00
|
|
|
// Use UFCS for disambiguation
|
|
|
|
Resource::shutdown(&mut resource, shutdown_mode)?;
|
|
|
|
Ok(empty_buf())
|
|
|
|
})
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-10-04 00:48:02 +00:00
|
|
|
fn op_read(
|
2019-04-09 17:11:25 +00:00
|
|
|
_state: &ThreadSafeState,
|
2019-01-14 06:30:38 +00:00
|
|
|
base: &msg::Base<'_>,
|
2019-04-28 19:31:10 +00:00
|
|
|
data: Option<PinnedBuf>,
|
2019-06-18 01:02:08 +00:00
|
|
|
) -> CliOpResult {
|
2019-06-14 17:58:20 +00:00
|
|
|
let cmd_id = base.cmd_id();
|
2018-10-04 01:12:23 +00:00
|
|
|
let inner = base.inner_as_read().unwrap();
|
|
|
|
let rid = inner.rid();
|
2018-09-27 04:56:39 +00:00
|
|
|
|
2018-10-01 23:37:18 +00:00
|
|
|
match resources::lookup(rid) {
|
2019-06-18 01:02:08 +00:00
|
|
|
None => Err(errors::bad_resource()),
|
2018-10-01 23:43:27 +00:00
|
|
|
Some(resource) => {
|
2019-04-28 19:31:10 +00:00
|
|
|
let op = tokio::io::read(resource, data.unwrap())
|
2018-11-04 14:04:24 +00:00
|
|
|
.map_err(DenoError::from)
|
2018-10-01 23:43:27 +00:00
|
|
|
.and_then(move |(_resource, _buf, nread)| {
|
|
|
|
let builder = &mut FlatBufferBuilder::new();
|
2018-10-04 01:12:23 +00:00
|
|
|
let inner = msg::ReadRes::create(
|
2018-10-01 23:43:27 +00:00
|
|
|
builder,
|
|
|
|
&msg::ReadResArgs {
|
|
|
|
nread: nread as u32,
|
|
|
|
eof: nread == 0,
|
|
|
|
},
|
|
|
|
);
|
|
|
|
Ok(serialize_response(
|
2019-06-14 17:58:20 +00:00
|
|
|
cmd_id,
|
2018-10-01 23:43:27 +00:00
|
|
|
builder,
|
|
|
|
msg::BaseArgs {
|
2018-10-04 01:12:23 +00:00
|
|
|
inner: Some(inner.as_union_value()),
|
|
|
|
inner_type: msg::Any::ReadRes,
|
2018-10-01 23:43:27 +00:00
|
|
|
..Default::default()
|
|
|
|
},
|
|
|
|
))
|
|
|
|
});
|
2019-06-18 01:02:08 +00:00
|
|
|
if base.sync() {
|
|
|
|
let buf = op.wait()?;
|
|
|
|
Ok(Op::Sync(buf))
|
|
|
|
} else {
|
|
|
|
Ok(Op::Async(Box::new(op)))
|
|
|
|
}
|
2018-09-27 04:56:39 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-10-04 00:48:02 +00:00
|
|
|
fn op_write(
|
2019-04-09 17:11:25 +00:00
|
|
|
_state: &ThreadSafeState,
|
2019-01-14 06:30:38 +00:00
|
|
|
base: &msg::Base<'_>,
|
2019-04-28 19:31:10 +00:00
|
|
|
data: Option<PinnedBuf>,
|
2019-06-18 01:02:08 +00:00
|
|
|
) -> CliOpResult {
|
2019-06-14 17:58:20 +00:00
|
|
|
let cmd_id = base.cmd_id();
|
2018-10-04 01:12:23 +00:00
|
|
|
let inner = base.inner_as_write().unwrap();
|
|
|
|
let rid = inner.rid();
|
2018-09-27 04:56:39 +00:00
|
|
|
|
2018-10-01 23:37:18 +00:00
|
|
|
match resources::lookup(rid) {
|
2019-06-18 01:02:08 +00:00
|
|
|
None => Err(errors::bad_resource()),
|
2018-10-01 23:43:27 +00:00
|
|
|
Some(resource) => {
|
2019-04-28 19:31:10 +00:00
|
|
|
let op = tokio_write::write(resource, data.unwrap())
|
2018-11-04 14:04:24 +00:00
|
|
|
.map_err(DenoError::from)
|
2018-10-19 20:10:25 +00:00
|
|
|
.and_then(move |(_resource, _buf, nwritten)| {
|
2018-10-01 23:43:27 +00:00
|
|
|
let builder = &mut FlatBufferBuilder::new();
|
2018-10-04 01:12:23 +00:00
|
|
|
let inner = msg::WriteRes::create(
|
2018-10-01 23:43:27 +00:00
|
|
|
builder,
|
|
|
|
&msg::WriteResArgs {
|
2018-10-19 20:10:25 +00:00
|
|
|
nbyte: nwritten as u32,
|
2018-10-01 23:43:27 +00:00
|
|
|
},
|
|
|
|
);
|
|
|
|
Ok(serialize_response(
|
2019-06-14 17:58:20 +00:00
|
|
|
cmd_id,
|
2018-10-01 23:43:27 +00:00
|
|
|
builder,
|
|
|
|
msg::BaseArgs {
|
2018-10-04 01:12:23 +00:00
|
|
|
inner: Some(inner.as_union_value()),
|
|
|
|
inner_type: msg::Any::WriteRes,
|
2018-10-01 23:43:27 +00:00
|
|
|
..Default::default()
|
|
|
|
},
|
|
|
|
))
|
|
|
|
});
|
2019-06-18 01:02:08 +00:00
|
|
|
if base.sync() {
|
|
|
|
let buf = op.wait()?;
|
|
|
|
Ok(Op::Sync(buf))
|
|
|
|
} else {
|
|
|
|
Ok(Op::Async(Box::new(op)))
|
|
|
|
}
|
2018-09-27 04:56:39 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-02-18 23:26:41 +00:00
|
|
|
fn op_seek(
|
2019-04-09 17:11:25 +00:00
|
|
|
_state: &ThreadSafeState,
|
2019-02-18 23:26:41 +00:00
|
|
|
base: &msg::Base<'_>,
|
2019-04-28 19:31:10 +00:00
|
|
|
data: Option<PinnedBuf>,
|
2019-06-18 01:02:08 +00:00
|
|
|
) -> CliOpResult {
|
2019-04-28 19:31:10 +00:00
|
|
|
assert!(data.is_none());
|
2019-02-18 23:26:41 +00:00
|
|
|
let inner = base.inner_as_seek().unwrap();
|
|
|
|
let rid = inner.rid();
|
|
|
|
let offset = inner.offset();
|
|
|
|
let whence = inner.whence();
|
|
|
|
|
|
|
|
match resources::lookup(rid) {
|
2019-06-18 01:02:08 +00:00
|
|
|
None => Err(errors::bad_resource()),
|
2019-02-18 23:26:41 +00:00
|
|
|
Some(resource) => {
|
|
|
|
let op = resources::seek(resource, offset, whence)
|
|
|
|
.and_then(move |_| Ok(empty_buf()));
|
2019-06-18 01:02:08 +00:00
|
|
|
if base.sync() {
|
|
|
|
let buf = op.wait()?;
|
|
|
|
Ok(Op::Sync(buf))
|
|
|
|
} else {
|
|
|
|
Ok(Op::Async(Box::new(op)))
|
|
|
|
}
|
2019-02-18 23:26:41 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-10-04 00:48:02 +00:00
|
|
|
fn op_remove(
|
2019-04-09 17:11:25 +00:00
|
|
|
state: &ThreadSafeState,
|
2019-01-14 06:30:38 +00:00
|
|
|
base: &msg::Base<'_>,
|
2019-04-28 19:31:10 +00:00
|
|
|
data: Option<PinnedBuf>,
|
2019-06-18 01:02:08 +00:00
|
|
|
) -> CliOpResult {
|
2019-04-28 19:31:10 +00:00
|
|
|
assert!(data.is_none());
|
2018-10-04 01:12:23 +00:00
|
|
|
let inner = base.inner_as_remove().unwrap();
|
2019-06-18 01:02:08 +00:00
|
|
|
let (path, path_) = resolve_path(inner.path().unwrap())?;
|
2018-10-04 01:12:23 +00:00
|
|
|
let recursive = inner.recursive();
|
2018-10-27 13:11:39 +00:00
|
|
|
|
2019-06-18 01:02:08 +00:00
|
|
|
state.check_write(&path_)?;
|
2018-10-27 13:11:39 +00:00
|
|
|
|
2018-12-11 13:36:34 +00:00
|
|
|
blocking(base.sync(), move || {
|
2018-10-04 00:48:02 +00:00
|
|
|
debug!("op_remove {}", path.display());
|
2018-09-22 08:42:07 +00:00
|
|
|
let metadata = fs::metadata(&path)?;
|
2018-09-11 03:40:03 +00:00
|
|
|
if metadata.is_file() {
|
2018-09-22 08:42:07 +00:00
|
|
|
fs::remove_file(&path)?;
|
2018-11-04 14:04:24 +00:00
|
|
|
} else if recursive {
|
|
|
|
remove_dir_all(&path)?;
|
2018-09-11 03:40:03 +00:00
|
|
|
} else {
|
2018-11-04 14:04:24 +00:00
|
|
|
fs::remove_dir(&path)?;
|
2018-09-11 03:40:03 +00:00
|
|
|
}
|
2018-09-18 18:53:16 +00:00
|
|
|
Ok(empty_buf())
|
2018-09-22 08:42:07 +00:00
|
|
|
})
|
2018-09-11 03:40:03 +00:00
|
|
|
}
|
|
|
|
|
2018-10-04 00:48:02 +00:00
|
|
|
fn op_copy_file(
|
2019-04-09 17:11:25 +00:00
|
|
|
state: &ThreadSafeState,
|
2019-01-14 06:30:38 +00:00
|
|
|
base: &msg::Base<'_>,
|
2019-04-28 19:31:10 +00:00
|
|
|
data: Option<PinnedBuf>,
|
2019-06-18 01:02:08 +00:00
|
|
|
) -> CliOpResult {
|
2019-04-28 19:31:10 +00:00
|
|
|
assert!(data.is_none());
|
2018-10-04 01:12:23 +00:00
|
|
|
let inner = base.inner_as_copy_file().unwrap();
|
2019-06-18 01:02:08 +00:00
|
|
|
let (from, from_) = resolve_path(inner.from().unwrap())?;
|
|
|
|
let (to, to_) = resolve_path(inner.to().unwrap())?;
|
2018-09-30 22:06:41 +00:00
|
|
|
|
2019-06-18 01:02:08 +00:00
|
|
|
state.check_read(&from_)?;
|
|
|
|
state.check_write(&to_)?;
|
2018-09-30 22:06:41 +00:00
|
|
|
|
2018-10-04 00:48:02 +00:00
|
|
|
debug!("op_copy_file {} {}", from.display(), to.display());
|
2018-12-11 13:36:34 +00:00
|
|
|
blocking(base.sync(), move || {
|
2018-10-17 05:40:03 +00: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(errors::new(
|
2018-10-17 17:04:28 +00:00
|
|
|
ErrorKind::NotFound,
|
|
|
|
"File not found".to_string(),
|
2018-10-17 05:40:03 +00:00
|
|
|
));
|
|
|
|
}
|
|
|
|
|
2018-09-30 22:06:41 +00:00
|
|
|
fs::copy(&from, &to)?;
|
|
|
|
Ok(empty_buf())
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
2018-08-29 13:22:25 +00:00
|
|
|
macro_rules! to_seconds {
|
|
|
|
($time:expr) => {{
|
|
|
|
// Unwrap is safe here as if the file is before the unix epoch
|
|
|
|
// something is very wrong.
|
2018-08-30 17:54:56 +00:00
|
|
|
$time
|
|
|
|
.and_then(|t| Ok(t.duration_since(UNIX_EPOCH).unwrap().as_secs()))
|
|
|
|
.unwrap_or(0)
|
|
|
|
}};
|
2018-08-29 13:22:25 +00:00
|
|
|
}
|
|
|
|
|
2018-09-12 14:28:48 +00:00
|
|
|
#[cfg(any(unix))]
|
2018-11-04 14:04:24 +00:00
|
|
|
fn get_mode(perm: &fs::Permissions) -> u32 {
|
2018-09-17 23:53:55 +00:00
|
|
|
perm.mode()
|
2018-09-12 14:28:48 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
#[cfg(not(any(unix)))]
|
2018-11-04 14:04:24 +00:00
|
|
|
fn get_mode(_perm: &fs::Permissions) -> u32 {
|
2018-09-17 23:53:55 +00:00
|
|
|
0
|
2018-09-12 14:28:48 +00:00
|
|
|
}
|
|
|
|
|
2018-10-13 20:03:27 +00:00
|
|
|
fn op_cwd(
|
2019-04-09 17:11:25 +00:00
|
|
|
_state: &ThreadSafeState,
|
2019-06-14 17:58:20 +00:00
|
|
|
base: &msg::Base<'_>,
|
2019-04-28 19:31:10 +00:00
|
|
|
data: Option<PinnedBuf>,
|
2019-06-18 01:02:08 +00:00
|
|
|
) -> CliOpResult {
|
2019-04-28 19:31:10 +00:00
|
|
|
assert!(data.is_none());
|
2019-06-14 17:58:20 +00:00
|
|
|
let cmd_id = base.cmd_id();
|
2019-06-18 01:02:08 +00:00
|
|
|
let path = std::env::current_dir()?;
|
|
|
|
let builder = &mut FlatBufferBuilder::new();
|
|
|
|
let cwd =
|
|
|
|
builder.create_string(&path.into_os_string().into_string().unwrap());
|
|
|
|
let inner = msg::CwdRes::create(builder, &msg::CwdResArgs { cwd: Some(cwd) });
|
|
|
|
let response_buf = serialize_response(
|
|
|
|
cmd_id,
|
|
|
|
builder,
|
|
|
|
msg::BaseArgs {
|
|
|
|
inner: Some(inner.as_union_value()),
|
|
|
|
inner_type: msg::Any::CwdRes,
|
|
|
|
..Default::default()
|
|
|
|
},
|
|
|
|
);
|
|
|
|
ok_buf(response_buf)
|
2018-10-13 20:03:27 +00:00
|
|
|
}
|
|
|
|
|
2018-10-04 00:48:02 +00:00
|
|
|
fn op_stat(
|
2019-04-09 17:11:25 +00:00
|
|
|
state: &ThreadSafeState,
|
2019-01-14 06:30:38 +00:00
|
|
|
base: &msg::Base<'_>,
|
2019-04-28 19:31:10 +00:00
|
|
|
data: Option<PinnedBuf>,
|
2019-06-18 01:02:08 +00:00
|
|
|
) -> CliOpResult {
|
2019-04-28 19:31:10 +00:00
|
|
|
assert!(data.is_none());
|
2018-10-04 01:12:23 +00:00
|
|
|
let inner = base.inner_as_stat().unwrap();
|
2019-06-14 17:58:20 +00:00
|
|
|
let cmd_id = base.cmd_id();
|
2019-06-18 01:02:08 +00:00
|
|
|
let (filename, filename_) = resolve_path(inner.filename().unwrap())?;
|
2018-10-04 01:12:23 +00:00
|
|
|
let lstat = inner.lstat();
|
2018-09-02 06:33:24 +00:00
|
|
|
|
2019-06-18 01:02:08 +00:00
|
|
|
state.check_read(&filename_)?;
|
2019-02-08 20:59:38 +00:00
|
|
|
|
2018-12-11 13:36:34 +00:00
|
|
|
blocking(base.sync(), move || {
|
2018-09-06 02:13:36 +00:00
|
|
|
let builder = &mut FlatBufferBuilder::new();
|
2018-10-04 00:48:02 +00:00
|
|
|
debug!("op_stat {} {}", filename.display(), lstat);
|
2018-09-06 02:13:36 +00:00
|
|
|
let metadata = if lstat {
|
2018-09-22 08:42:07 +00:00
|
|
|
fs::symlink_metadata(&filename)?
|
2018-09-06 02:13:36 +00:00
|
|
|
} else {
|
2018-09-22 08:42:07 +00:00
|
|
|
fs::metadata(&filename)?
|
2018-09-06 02:13:36 +00:00
|
|
|
};
|
|
|
|
|
2018-10-04 01:12:23 +00:00
|
|
|
let inner = msg::StatRes::create(
|
2018-09-06 02:13:36 +00:00
|
|
|
builder,
|
2018-09-11 19:38:53 +00:00
|
|
|
&msg::StatResArgs {
|
2018-09-06 02:13:36 +00:00
|
|
|
is_file: metadata.is_file(),
|
|
|
|
is_symlink: metadata.file_type().is_symlink(),
|
|
|
|
len: metadata.len(),
|
|
|
|
modified: to_seconds!(metadata.modified()),
|
|
|
|
accessed: to_seconds!(metadata.accessed()),
|
|
|
|
created: to_seconds!(metadata.created()),
|
2018-11-04 14:04:24 +00:00
|
|
|
mode: get_mode(&metadata.permissions()),
|
2018-09-17 23:53:55 +00:00
|
|
|
has_mode: cfg!(target_family = "unix"),
|
2018-09-06 02:13:36 +00:00
|
|
|
..Default::default()
|
|
|
|
},
|
|
|
|
);
|
2018-08-29 13:22:25 +00:00
|
|
|
|
2018-09-09 23:48:10 +00:00
|
|
|
Ok(serialize_response(
|
2019-06-14 17:58:20 +00:00
|
|
|
cmd_id,
|
2018-09-06 02:13:36 +00:00
|
|
|
builder,
|
|
|
|
msg::BaseArgs {
|
2018-10-04 01:12:23 +00:00
|
|
|
inner: Some(inner.as_union_value()),
|
|
|
|
inner_type: msg::Any::StatRes,
|
2018-09-06 02:13:36 +00:00
|
|
|
..Default::default()
|
|
|
|
},
|
|
|
|
))
|
2018-09-22 08:42:07 +00:00
|
|
|
})
|
2018-08-29 13:22:25 +00:00
|
|
|
}
|
|
|
|
|
2018-10-04 00:48:02 +00:00
|
|
|
fn op_read_dir(
|
2019-04-09 17:11:25 +00:00
|
|
|
state: &ThreadSafeState,
|
2019-01-14 06:30:38 +00:00
|
|
|
base: &msg::Base<'_>,
|
2019-04-28 19:31:10 +00:00
|
|
|
data: Option<PinnedBuf>,
|
2019-06-18 01:02:08 +00:00
|
|
|
) -> CliOpResult {
|
2019-04-28 19:31:10 +00:00
|
|
|
assert!(data.is_none());
|
2018-10-04 01:12:23 +00:00
|
|
|
let inner = base.inner_as_read_dir().unwrap();
|
2019-06-14 17:58:20 +00:00
|
|
|
let cmd_id = base.cmd_id();
|
2019-06-18 01:02:08 +00:00
|
|
|
let (path, path_) = resolve_path(inner.path().unwrap())?;
|
2018-10-03 21:56:56 +00:00
|
|
|
|
2019-06-18 01:02:08 +00:00
|
|
|
state.check_read(&path_)?;
|
2019-02-08 20:59:38 +00:00
|
|
|
|
2019-06-18 01:02:08 +00:00
|
|
|
blocking(base.sync(), move || {
|
2019-05-08 23:20:30 +00:00
|
|
|
debug!("op_read_dir {}", path.display());
|
2018-10-03 21:56:56 +00:00
|
|
|
let builder = &mut FlatBufferBuilder::new();
|
2019-05-08 23:20:30 +00:00
|
|
|
let entries: Vec<_> = fs::read_dir(path)?
|
2018-10-03 21:56:56 +00:00
|
|
|
.map(|entry| {
|
|
|
|
let entry = entry.unwrap();
|
|
|
|
let metadata = entry.metadata().unwrap();
|
|
|
|
let file_type = metadata.file_type();
|
|
|
|
let name = builder.create_string(entry.file_name().to_str().unwrap());
|
|
|
|
|
|
|
|
msg::StatRes::create(
|
|
|
|
builder,
|
|
|
|
&msg::StatResArgs {
|
|
|
|
is_file: file_type.is_file(),
|
|
|
|
is_symlink: file_type.is_symlink(),
|
|
|
|
len: metadata.len(),
|
|
|
|
modified: to_seconds!(metadata.modified()),
|
|
|
|
accessed: to_seconds!(metadata.accessed()),
|
|
|
|
created: to_seconds!(metadata.created()),
|
|
|
|
name: Some(name),
|
2018-12-12 07:31:18 +00:00
|
|
|
mode: get_mode(&metadata.permissions()),
|
|
|
|
has_mode: cfg!(target_family = "unix"),
|
2018-10-03 21:56:56 +00:00
|
|
|
},
|
|
|
|
)
|
2018-10-15 20:46:42 +00:00
|
|
|
}).collect();
|
2018-10-03 21:56:56 +00:00
|
|
|
|
|
|
|
let entries = builder.create_vector(&entries);
|
2018-10-04 01:12:23 +00:00
|
|
|
let inner = msg::ReadDirRes::create(
|
2018-10-03 21:56:56 +00:00
|
|
|
builder,
|
|
|
|
&msg::ReadDirResArgs {
|
|
|
|
entries: Some(entries),
|
|
|
|
},
|
|
|
|
);
|
|
|
|
Ok(serialize_response(
|
2019-06-14 17:58:20 +00:00
|
|
|
cmd_id,
|
2018-10-03 21:56:56 +00:00
|
|
|
builder,
|
|
|
|
msg::BaseArgs {
|
2018-10-04 01:12:23 +00:00
|
|
|
inner: Some(inner.as_union_value()),
|
|
|
|
inner_type: msg::Any::ReadDirRes,
|
2018-10-03 21:56:56 +00:00
|
|
|
..Default::default()
|
|
|
|
},
|
|
|
|
))
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
2018-10-04 00:48:02 +00:00
|
|
|
fn op_rename(
|
2019-04-09 17:11:25 +00:00
|
|
|
state: &ThreadSafeState,
|
2019-01-14 06:30:38 +00:00
|
|
|
base: &msg::Base<'_>,
|
2019-04-28 19:31:10 +00:00
|
|
|
data: Option<PinnedBuf>,
|
2019-06-18 01:02:08 +00:00
|
|
|
) -> CliOpResult {
|
2019-04-28 19:31:10 +00:00
|
|
|
assert!(data.is_none());
|
2018-10-04 01:12:23 +00:00
|
|
|
let inner = base.inner_as_rename().unwrap();
|
2019-06-18 01:02:08 +00:00
|
|
|
let (oldpath, _) = resolve_path(inner.oldpath().unwrap())?;
|
|
|
|
let (newpath, newpath_) = resolve_path(inner.newpath().unwrap())?;
|
2019-06-14 03:43:54 +00:00
|
|
|
|
2019-06-18 01:02:08 +00:00
|
|
|
state.check_write(&newpath_)?;
|
|
|
|
|
|
|
|
blocking(base.sync(), move || {
|
2018-10-04 00:48:02 +00:00
|
|
|
debug!("op_rename {} {}", oldpath.display(), newpath.display());
|
2018-09-22 08:42:07 +00:00
|
|
|
fs::rename(&oldpath, &newpath)?;
|
2018-09-18 18:53:16 +00:00
|
|
|
Ok(empty_buf())
|
2018-09-22 08:42:07 +00:00
|
|
|
})
|
2018-09-04 00:22:30 +00:00
|
|
|
}
|
2018-09-19 04:38:24 +00:00
|
|
|
|
2019-04-08 13:11:32 +00:00
|
|
|
fn op_link(
|
2019-04-09 17:11:25 +00:00
|
|
|
state: &ThreadSafeState,
|
2019-04-08 13:11:32 +00:00
|
|
|
base: &msg::Base<'_>,
|
2019-04-28 19:31:10 +00:00
|
|
|
data: Option<PinnedBuf>,
|
2019-06-18 01:02:08 +00:00
|
|
|
) -> CliOpResult {
|
2019-04-28 19:31:10 +00:00
|
|
|
assert!(data.is_none());
|
2019-04-08 13:11:32 +00:00
|
|
|
let inner = base.inner_as_link().unwrap();
|
2019-06-18 01:02:08 +00:00
|
|
|
let (oldname, _) = resolve_path(inner.oldname().unwrap())?;
|
|
|
|
let (newname, newname_) = resolve_path(inner.newname().unwrap())?;
|
2019-04-08 13:11:32 +00:00
|
|
|
|
2019-06-18 01:02:08 +00:00
|
|
|
state.check_write(&newname_)?;
|
2019-04-08 13:11:32 +00:00
|
|
|
|
2019-06-18 01:02:08 +00:00
|
|
|
blocking(base.sync(), move || {
|
2019-04-08 13:11:32 +00:00
|
|
|
debug!("op_link {} {}", oldname.display(), newname.display());
|
|
|
|
std::fs::hard_link(&oldname, &newname)?;
|
|
|
|
Ok(empty_buf())
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
2018-10-04 00:48:02 +00:00
|
|
|
fn op_symlink(
|
2019-04-09 17:11:25 +00:00
|
|
|
state: &ThreadSafeState,
|
2019-01-14 06:30:38 +00:00
|
|
|
base: &msg::Base<'_>,
|
2019-04-28 19:31:10 +00:00
|
|
|
data: Option<PinnedBuf>,
|
2019-06-18 01:02:08 +00:00
|
|
|
) -> CliOpResult {
|
2019-04-28 19:31:10 +00:00
|
|
|
assert!(data.is_none());
|
2018-10-27 13:11:39 +00:00
|
|
|
let inner = base.inner_as_symlink().unwrap();
|
2019-06-18 01:02:08 +00:00
|
|
|
let (oldname, _) = resolve_path(inner.oldname().unwrap())?;
|
|
|
|
let (newname, newname_) = resolve_path(inner.newname().unwrap())?;
|
2018-10-27 13:11:39 +00:00
|
|
|
|
2019-06-18 01:02:08 +00:00
|
|
|
state.check_write(&newname_)?;
|
2018-09-19 04:38:24 +00:00
|
|
|
// TODO Use type for Windows.
|
|
|
|
if cfg!(windows) {
|
2019-06-18 01:02:08 +00:00
|
|
|
return Err(errors::new(ErrorKind::Other, "Not implemented".to_string()));
|
2018-09-19 04:38:24 +00:00
|
|
|
}
|
2019-06-18 01:02:08 +00:00
|
|
|
blocking(base.sync(), move || {
|
2018-10-04 00:48:02 +00:00
|
|
|
debug!("op_symlink {} {}", oldname.display(), newname.display());
|
2018-09-22 08:42:07 +00:00
|
|
|
#[cfg(any(unix))]
|
|
|
|
std::os::unix::fs::symlink(&oldname, &newname)?;
|
|
|
|
Ok(empty_buf())
|
|
|
|
})
|
2018-09-19 04:38:24 +00:00
|
|
|
}
|
2018-09-25 04:20:49 +00:00
|
|
|
|
2018-10-04 00:48:02 +00:00
|
|
|
fn op_read_link(
|
2019-04-09 17:11:25 +00:00
|
|
|
state: &ThreadSafeState,
|
2019-01-14 06:30:38 +00:00
|
|
|
base: &msg::Base<'_>,
|
2019-04-28 19:31:10 +00:00
|
|
|
data: Option<PinnedBuf>,
|
2019-06-18 01:02:08 +00:00
|
|
|
) -> CliOpResult {
|
2019-04-28 19:31:10 +00:00
|
|
|
assert!(data.is_none());
|
2018-10-04 01:12:23 +00:00
|
|
|
let inner = base.inner_as_readlink().unwrap();
|
2019-06-14 17:58:20 +00:00
|
|
|
let cmd_id = base.cmd_id();
|
2019-06-18 01:02:08 +00:00
|
|
|
let (name, name_) = resolve_path(inner.name().unwrap())?;
|
2019-02-08 20:59:38 +00:00
|
|
|
|
2019-06-18 01:02:08 +00:00
|
|
|
state.check_read(&name_)?;
|
2019-06-14 03:43:54 +00:00
|
|
|
|
2019-06-18 01:02:08 +00:00
|
|
|
blocking(base.sync(), move || {
|
2018-10-04 00:48:02 +00:00
|
|
|
debug!("op_read_link {}", name.display());
|
2018-09-22 08:42:07 +00:00
|
|
|
let path = fs::read_link(&name)?;
|
2018-09-25 04:20:49 +00:00
|
|
|
let builder = &mut FlatBufferBuilder::new();
|
|
|
|
let path_off = builder.create_string(path.to_str().unwrap());
|
2018-10-04 01:12:23 +00:00
|
|
|
let inner = msg::ReadlinkRes::create(
|
2018-09-25 04:20:49 +00:00
|
|
|
builder,
|
|
|
|
&msg::ReadlinkResArgs {
|
|
|
|
path: Some(path_off),
|
|
|
|
},
|
|
|
|
);
|
|
|
|
Ok(serialize_response(
|
2019-06-14 17:58:20 +00:00
|
|
|
cmd_id,
|
2018-09-25 04:20:49 +00:00
|
|
|
builder,
|
|
|
|
msg::BaseArgs {
|
2018-10-04 01:12:23 +00:00
|
|
|
inner: Some(inner.as_union_value()),
|
|
|
|
inner_type: msg::Any::ReadlinkRes,
|
2018-09-25 04:20:49 +00:00
|
|
|
..Default::default()
|
|
|
|
},
|
|
|
|
))
|
2018-09-22 08:42:07 +00:00
|
|
|
})
|
2018-09-25 04:20:49 +00:00
|
|
|
}
|
2018-09-30 19:06:20 +00:00
|
|
|
|
2018-11-05 17:55:59 +00:00
|
|
|
fn op_repl_start(
|
2019-04-09 17:11:25 +00:00
|
|
|
state: &ThreadSafeState,
|
2019-01-14 06:30:38 +00:00
|
|
|
base: &msg::Base<'_>,
|
2019-04-28 19:31:10 +00:00
|
|
|
data: Option<PinnedBuf>,
|
2019-06-18 01:02:08 +00:00
|
|
|
) -> CliOpResult {
|
2019-04-28 19:31:10 +00:00
|
|
|
assert!(data.is_none());
|
2018-11-05 17:55:59 +00:00
|
|
|
let inner = base.inner_as_repl_start().unwrap();
|
2019-06-14 17:58:20 +00:00
|
|
|
let cmd_id = base.cmd_id();
|
2018-11-05 17:55:59 +00:00
|
|
|
let history_file = String::from(inner.history_file().unwrap());
|
|
|
|
|
|
|
|
debug!("op_repl_start {}", history_file);
|
2019-04-08 21:10:00 +00:00
|
|
|
let history_path = repl::history_path(&state.dir, &history_file);
|
2018-11-05 17:55:59 +00:00
|
|
|
let repl = repl::Repl::new(history_path);
|
|
|
|
let resource = resources::add_repl(repl);
|
|
|
|
|
|
|
|
let builder = &mut FlatBufferBuilder::new();
|
|
|
|
let inner = msg::ReplStartRes::create(
|
|
|
|
builder,
|
|
|
|
&msg::ReplStartResArgs { rid: resource.rid },
|
|
|
|
);
|
2019-06-18 01:02:08 +00:00
|
|
|
ok_buf(serialize_response(
|
2019-06-14 17:58:20 +00:00
|
|
|
cmd_id,
|
2018-11-05 17:55:59 +00:00
|
|
|
builder,
|
|
|
|
msg::BaseArgs {
|
|
|
|
inner: Some(inner.as_union_value()),
|
|
|
|
inner_type: msg::Any::ReplStartRes,
|
|
|
|
..Default::default()
|
|
|
|
},
|
|
|
|
))
|
|
|
|
}
|
|
|
|
|
|
|
|
fn op_repl_readline(
|
2019-04-09 17:11:25 +00:00
|
|
|
_state: &ThreadSafeState,
|
2019-01-14 06:30:38 +00:00
|
|
|
base: &msg::Base<'_>,
|
2019-04-28 19:31:10 +00:00
|
|
|
data: Option<PinnedBuf>,
|
2019-06-18 01:02:08 +00:00
|
|
|
) -> CliOpResult {
|
2019-04-28 19:31:10 +00:00
|
|
|
assert!(data.is_none());
|
2018-11-05 17:55:59 +00:00
|
|
|
let inner = base.inner_as_repl_readline().unwrap();
|
2019-06-14 17:58:20 +00:00
|
|
|
let cmd_id = base.cmd_id();
|
2018-11-05 17:55:59 +00:00
|
|
|
let rid = inner.rid();
|
|
|
|
let prompt = inner.prompt().unwrap().to_owned();
|
|
|
|
debug!("op_repl_readline {} {}", rid, prompt);
|
|
|
|
|
2019-06-18 01:02:08 +00:00
|
|
|
blocking(base.sync(), move || {
|
2019-02-09 21:55:40 +00:00
|
|
|
let repl = resources::get_repl(rid)?;
|
|
|
|
let line = repl.lock().unwrap().readline(&prompt)?;
|
2018-11-05 17:55:59 +00:00
|
|
|
|
|
|
|
let builder = &mut FlatBufferBuilder::new();
|
|
|
|
let line_off = builder.create_string(&line);
|
|
|
|
let inner = msg::ReplReadlineRes::create(
|
|
|
|
builder,
|
|
|
|
&msg::ReplReadlineResArgs {
|
|
|
|
line: Some(line_off),
|
|
|
|
},
|
|
|
|
);
|
|
|
|
Ok(serialize_response(
|
2019-06-14 17:58:20 +00:00
|
|
|
cmd_id,
|
2018-11-05 17:55:59 +00:00
|
|
|
builder,
|
|
|
|
msg::BaseArgs {
|
|
|
|
inner: Some(inner.as_union_value()),
|
|
|
|
inner_type: msg::Any::ReplReadlineRes,
|
|
|
|
..Default::default()
|
|
|
|
},
|
|
|
|
))
|
2018-11-28 09:07:22 +00:00
|
|
|
})
|
2018-11-05 17:55:59 +00:00
|
|
|
}
|
|
|
|
|
2018-10-04 00:48:02 +00:00
|
|
|
fn op_truncate(
|
2019-04-09 17:11:25 +00:00
|
|
|
state: &ThreadSafeState,
|
2019-01-14 06:30:38 +00:00
|
|
|
base: &msg::Base<'_>,
|
2019-04-28 19:31:10 +00:00
|
|
|
data: Option<PinnedBuf>,
|
2019-06-18 01:02:08 +00:00
|
|
|
) -> CliOpResult {
|
2019-04-28 19:31:10 +00:00
|
|
|
assert!(data.is_none());
|
2018-09-30 19:06:20 +00:00
|
|
|
|
2018-10-04 01:12:23 +00:00
|
|
|
let inner = base.inner_as_truncate().unwrap();
|
2019-06-18 01:02:08 +00:00
|
|
|
let (filename, filename_) = resolve_path(inner.name().unwrap())?;
|
2018-10-04 01:12:23 +00:00
|
|
|
let len = inner.len();
|
2018-10-27 13:11:39 +00:00
|
|
|
|
2019-06-18 01:02:08 +00:00
|
|
|
state.check_write(&filename_)?;
|
2018-10-27 13:11:39 +00:00
|
|
|
|
2018-12-11 13:36:34 +00:00
|
|
|
blocking(base.sync(), move || {
|
2019-05-08 23:20:30 +00:00
|
|
|
debug!("op_truncate {} {}", filename_, len);
|
2018-09-30 19:06:20 +00:00
|
|
|
let f = fs::OpenOptions::new().write(true).open(&filename)?;
|
2018-11-04 14:04:24 +00:00
|
|
|
f.set_len(u64::from(len))?;
|
2018-09-30 19:06:20 +00:00
|
|
|
Ok(empty_buf())
|
|
|
|
})
|
|
|
|
}
|
2018-10-04 03:58:29 +00:00
|
|
|
|
2019-05-01 09:08:12 +00:00
|
|
|
fn op_utime(
|
|
|
|
state: &ThreadSafeState,
|
|
|
|
base: &msg::Base<'_>,
|
2019-04-28 19:31:10 +00:00
|
|
|
data: Option<PinnedBuf>,
|
2019-06-18 01:02:08 +00:00
|
|
|
) -> CliOpResult {
|
2019-04-28 19:31:10 +00:00
|
|
|
assert!(data.is_none());
|
2019-05-01 09:08:12 +00:00
|
|
|
|
|
|
|
let inner = base.inner_as_utime().unwrap();
|
|
|
|
let filename = String::from(inner.filename().unwrap());
|
|
|
|
let atime = inner.atime();
|
|
|
|
let mtime = inner.mtime();
|
|
|
|
|
2019-06-18 01:02:08 +00:00
|
|
|
state.check_write(&filename)?;
|
2019-05-01 09:08:12 +00:00
|
|
|
|
|
|
|
blocking(base.sync(), move || {
|
|
|
|
debug!("op_utimes {} {} {}", filename, atime, mtime);
|
|
|
|
utime::set_file_times(filename, atime, mtime)?;
|
|
|
|
Ok(empty_buf())
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
2018-10-04 00:48:02 +00:00
|
|
|
fn op_listen(
|
2019-04-09 17:11:25 +00:00
|
|
|
state: &ThreadSafeState,
|
2019-01-14 06:30:38 +00:00
|
|
|
base: &msg::Base<'_>,
|
2019-04-28 19:31:10 +00:00
|
|
|
data: Option<PinnedBuf>,
|
2019-06-18 01:02:08 +00:00
|
|
|
) -> CliOpResult {
|
2019-04-28 19:31:10 +00:00
|
|
|
assert!(data.is_none());
|
2019-06-14 17:58:20 +00:00
|
|
|
let cmd_id = base.cmd_id();
|
2018-10-04 01:41:59 +00:00
|
|
|
let inner = base.inner_as_listen().unwrap();
|
|
|
|
let network = inner.network().unwrap();
|
2018-10-04 03:58:29 +00:00
|
|
|
assert_eq!(network, "tcp");
|
2018-10-04 01:41:59 +00:00
|
|
|
let address = inner.address().unwrap();
|
2018-10-04 03:58:29 +00:00
|
|
|
|
2019-06-18 01:02:08 +00:00
|
|
|
state.check_net(&address)?;
|
2019-05-23 20:47:55 +00:00
|
|
|
|
2019-06-18 01:02:08 +00:00
|
|
|
let addr = resolve_addr(address).wait()?;
|
|
|
|
let listener = TcpListener::bind(&addr)?;
|
|
|
|
let resource = resources::add_tcp_listener(listener);
|
2018-10-04 03:58:29 +00:00
|
|
|
|
2019-06-18 01:02:08 +00:00
|
|
|
let builder = &mut FlatBufferBuilder::new();
|
|
|
|
let inner =
|
|
|
|
msg::ListenRes::create(builder, &msg::ListenResArgs { rid: resource.rid });
|
|
|
|
let response_buf = serialize_response(
|
|
|
|
cmd_id,
|
|
|
|
builder,
|
|
|
|
msg::BaseArgs {
|
|
|
|
inner: Some(inner.as_union_value()),
|
|
|
|
inner_type: msg::Any::ListenRes,
|
|
|
|
..Default::default()
|
|
|
|
},
|
|
|
|
);
|
|
|
|
ok_buf(response_buf)
|
2018-10-04 03:58:29 +00:00
|
|
|
}
|
|
|
|
|
2019-06-18 01:02:08 +00:00
|
|
|
fn new_conn(cmd_id: u32, tcp_stream: TcpStream) -> DenoResult<Buf> {
|
2018-10-04 03:58:29 +00:00
|
|
|
let tcp_stream_resource = resources::add_tcp_stream(tcp_stream);
|
|
|
|
// TODO forward socket_addr to client.
|
|
|
|
|
|
|
|
let builder = &mut FlatBufferBuilder::new();
|
2018-10-04 01:41:59 +00:00
|
|
|
let inner = msg::NewConn::create(
|
2018-10-04 03:58:29 +00:00
|
|
|
builder,
|
|
|
|
&msg::NewConnArgs {
|
|
|
|
rid: tcp_stream_resource.rid,
|
|
|
|
..Default::default()
|
|
|
|
},
|
|
|
|
);
|
|
|
|
Ok(serialize_response(
|
2019-06-14 17:58:20 +00:00
|
|
|
cmd_id,
|
2018-10-04 03:58:29 +00:00
|
|
|
builder,
|
|
|
|
msg::BaseArgs {
|
2018-10-04 01:41:59 +00:00
|
|
|
inner: Some(inner.as_union_value()),
|
|
|
|
inner_type: msg::Any::NewConn,
|
2018-10-04 03:58:29 +00:00
|
|
|
..Default::default()
|
|
|
|
},
|
|
|
|
))
|
|
|
|
}
|
|
|
|
|
2018-10-04 00:48:02 +00:00
|
|
|
fn op_accept(
|
2019-05-23 20:47:55 +00:00
|
|
|
_state: &ThreadSafeState,
|
2019-01-14 06:30:38 +00:00
|
|
|
base: &msg::Base<'_>,
|
2019-04-28 19:31:10 +00:00
|
|
|
data: Option<PinnedBuf>,
|
2019-06-18 01:02:08 +00:00
|
|
|
) -> CliOpResult {
|
2019-04-28 19:31:10 +00:00
|
|
|
assert!(data.is_none());
|
2019-06-14 17:58:20 +00:00
|
|
|
let cmd_id = base.cmd_id();
|
2018-10-04 01:41:59 +00:00
|
|
|
let inner = base.inner_as_accept().unwrap();
|
|
|
|
let server_rid = inner.rid();
|
2018-10-04 03:58:29 +00:00
|
|
|
|
|
|
|
match resources::lookup(server_rid) {
|
2019-06-18 01:02:08 +00:00
|
|
|
None => Err(errors::bad_resource()),
|
2018-10-04 03:58:29 +00:00
|
|
|
Some(server_resource) => {
|
2019-03-18 23:06:48 +00:00
|
|
|
let op = tokio_util::accept(server_resource)
|
2018-11-04 14:04:24 +00:00
|
|
|
.map_err(DenoError::from)
|
2019-06-14 17:58:20 +00:00
|
|
|
.and_then(move |(tcp_stream, _socket_addr)| {
|
|
|
|
new_conn(cmd_id, tcp_stream)
|
|
|
|
});
|
2019-06-18 01:02:08 +00:00
|
|
|
if base.sync() {
|
|
|
|
let buf = op.wait()?;
|
|
|
|
Ok(Op::Sync(buf))
|
|
|
|
} else {
|
|
|
|
Ok(Op::Async(Box::new(op)))
|
|
|
|
}
|
2018-10-04 03:58:29 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-10-04 00:48:02 +00:00
|
|
|
fn op_dial(
|
2019-04-09 17:11:25 +00:00
|
|
|
state: &ThreadSafeState,
|
2019-01-14 06:30:38 +00:00
|
|
|
base: &msg::Base<'_>,
|
2019-04-28 19:31:10 +00:00
|
|
|
data: Option<PinnedBuf>,
|
2019-06-18 01:02:08 +00:00
|
|
|
) -> CliOpResult {
|
2019-04-28 19:31:10 +00:00
|
|
|
assert!(data.is_none());
|
2019-06-14 17:58:20 +00:00
|
|
|
let cmd_id = base.cmd_id();
|
2018-10-04 01:41:59 +00:00
|
|
|
let inner = base.inner_as_dial().unwrap();
|
|
|
|
let network = inner.network().unwrap();
|
2019-01-14 03:14:59 +00:00
|
|
|
assert_eq!(network, "tcp"); // TODO Support others.
|
2018-10-04 01:41:59 +00:00
|
|
|
let address = inner.address().unwrap();
|
2018-10-04 03:58:29 +00:00
|
|
|
|
2019-06-18 01:02:08 +00:00
|
|
|
state.check_net(&address)?;
|
2019-05-23 20:47:55 +00:00
|
|
|
|
2019-01-14 03:14:59 +00:00
|
|
|
let op =
|
|
|
|
resolve_addr(address)
|
|
|
|
.map_err(DenoError::from)
|
|
|
|
.and_then(move |addr| {
|
|
|
|
TcpStream::connect(&addr)
|
|
|
|
.map_err(DenoError::from)
|
2019-06-14 17:58:20 +00:00
|
|
|
.and_then(move |tcp_stream| new_conn(cmd_id, tcp_stream))
|
2019-01-14 03:14:59 +00:00
|
|
|
});
|
2019-06-18 01:02:08 +00:00
|
|
|
if base.sync() {
|
|
|
|
let buf = op.wait()?;
|
|
|
|
Ok(Op::Sync(buf))
|
|
|
|
} else {
|
|
|
|
Ok(Op::Async(Box::new(op)))
|
|
|
|
}
|
2018-10-04 03:58:29 +00:00
|
|
|
}
|
2018-10-05 17:21:15 +00:00
|
|
|
|
|
|
|
fn op_metrics(
|
2019-04-09 17:11:25 +00:00
|
|
|
state: &ThreadSafeState,
|
2019-06-14 17:58:20 +00:00
|
|
|
base: &msg::Base<'_>,
|
2019-04-28 19:31:10 +00:00
|
|
|
data: Option<PinnedBuf>,
|
2019-06-18 01:02:08 +00:00
|
|
|
) -> CliOpResult {
|
2019-04-28 19:31:10 +00:00
|
|
|
assert!(data.is_none());
|
2019-06-14 17:58:20 +00:00
|
|
|
let cmd_id = base.cmd_id();
|
2018-10-05 17:21:15 +00:00
|
|
|
|
|
|
|
let builder = &mut FlatBufferBuilder::new();
|
|
|
|
let inner = msg::MetricsRes::create(
|
|
|
|
builder,
|
2019-04-08 21:10:00 +00:00
|
|
|
&msg::MetricsResArgs::from(&state.metrics),
|
2018-10-05 17:21:15 +00:00
|
|
|
);
|
2019-06-18 01:02:08 +00:00
|
|
|
ok_buf(serialize_response(
|
2019-06-14 17:58:20 +00:00
|
|
|
cmd_id,
|
2018-10-05 17:21:15 +00:00
|
|
|
builder,
|
|
|
|
msg::BaseArgs {
|
|
|
|
inner: Some(inner.as_union_value()),
|
|
|
|
inner_type: msg::Any::MetricsRes,
|
|
|
|
..Default::default()
|
|
|
|
},
|
|
|
|
))
|
|
|
|
}
|
2018-10-30 19:58:55 +00:00
|
|
|
|
|
|
|
fn op_resources(
|
2019-04-09 17:11:25 +00:00
|
|
|
_state: &ThreadSafeState,
|
2019-06-14 17:58:20 +00:00
|
|
|
base: &msg::Base<'_>,
|
2019-04-28 19:31:10 +00:00
|
|
|
data: Option<PinnedBuf>,
|
2019-06-18 01:02:08 +00:00
|
|
|
) -> CliOpResult {
|
2019-04-28 19:31:10 +00:00
|
|
|
assert!(data.is_none());
|
2019-06-14 17:58:20 +00:00
|
|
|
let cmd_id = base.cmd_id();
|
2018-10-30 19:58:55 +00:00
|
|
|
|
|
|
|
let builder = &mut FlatBufferBuilder::new();
|
|
|
|
let serialized_resources = table_entries();
|
|
|
|
|
|
|
|
let res: Vec<_> = serialized_resources
|
|
|
|
.iter()
|
|
|
|
.map(|(key, value)| {
|
|
|
|
let repr = builder.create_string(value);
|
|
|
|
|
|
|
|
msg::Resource::create(
|
|
|
|
builder,
|
|
|
|
&msg::ResourceArgs {
|
2018-11-04 14:04:24 +00:00
|
|
|
rid: *key,
|
2018-10-30 19:58:55 +00:00
|
|
|
repr: Some(repr),
|
|
|
|
},
|
|
|
|
)
|
|
|
|
}).collect();
|
|
|
|
|
|
|
|
let resources = builder.create_vector(&res);
|
|
|
|
let inner = msg::ResourcesRes::create(
|
|
|
|
builder,
|
|
|
|
&msg::ResourcesResArgs {
|
|
|
|
resources: Some(resources),
|
|
|
|
},
|
|
|
|
);
|
|
|
|
|
2019-06-18 01:02:08 +00:00
|
|
|
ok_buf(serialize_response(
|
2019-06-14 17:58:20 +00:00
|
|
|
cmd_id,
|
2018-10-30 19:58:55 +00:00
|
|
|
builder,
|
|
|
|
msg::BaseArgs {
|
|
|
|
inner: Some(inner.as_union_value()),
|
|
|
|
inner_type: msg::Any::ResourcesRes,
|
|
|
|
..Default::default()
|
|
|
|
},
|
|
|
|
))
|
|
|
|
}
|
2018-11-16 04:07:40 +00:00
|
|
|
|
|
|
|
fn subprocess_stdio_map(v: msg::ProcessStdio) -> std::process::Stdio {
|
|
|
|
match v {
|
|
|
|
msg::ProcessStdio::Inherit => std::process::Stdio::inherit(),
|
|
|
|
msg::ProcessStdio::Piped => std::process::Stdio::piped(),
|
|
|
|
msg::ProcessStdio::Null => std::process::Stdio::null(),
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-03-20 00:55:59 +00:00
|
|
|
fn op_run(
|
2019-04-09 17:11:25 +00:00
|
|
|
state: &ThreadSafeState,
|
2019-03-20 00:55:59 +00:00
|
|
|
base: &msg::Base<'_>,
|
2019-04-28 19:31:10 +00:00
|
|
|
data: Option<PinnedBuf>,
|
2019-06-18 01:02:08 +00:00
|
|
|
) -> CliOpResult {
|
|
|
|
if !base.sync() {
|
|
|
|
return Err(errors::no_async_support());
|
|
|
|
}
|
2019-06-14 17:58:20 +00:00
|
|
|
let cmd_id = base.cmd_id();
|
2018-11-16 04:07:40 +00:00
|
|
|
|
2019-06-18 01:02:08 +00:00
|
|
|
state.check_run()?;
|
2019-06-14 03:43:54 +00:00
|
|
|
|
2019-04-28 19:31:10 +00:00
|
|
|
assert!(data.is_none());
|
2018-11-16 04:07:40 +00:00
|
|
|
let inner = base.inner_as_run().unwrap();
|
|
|
|
let args = inner.args().unwrap();
|
2019-02-15 15:37:04 +00:00
|
|
|
let env = inner.env().unwrap();
|
2018-11-16 04:07:40 +00:00
|
|
|
let cwd = inner.cwd();
|
|
|
|
|
2018-11-30 08:30:49 +00:00
|
|
|
let mut c = Command::new(args.get(0));
|
2018-11-16 04:07:40 +00:00
|
|
|
(1..args.len()).for_each(|i| {
|
|
|
|
let arg = args.get(i);
|
2018-11-30 08:30:49 +00:00
|
|
|
c.arg(arg);
|
2018-11-16 04:07:40 +00:00
|
|
|
});
|
2018-11-30 08:30:49 +00:00
|
|
|
cwd.map(|d| c.current_dir(d));
|
2019-02-15 15:37:04 +00:00
|
|
|
(0..env.len()).for_each(|i| {
|
|
|
|
let entry = env.get(i);
|
|
|
|
c.env(entry.key().unwrap(), entry.value().unwrap());
|
|
|
|
});
|
2018-11-16 04:07:40 +00:00
|
|
|
|
2018-11-30 08:30:49 +00:00
|
|
|
c.stdin(subprocess_stdio_map(inner.stdin()));
|
|
|
|
c.stdout(subprocess_stdio_map(inner.stdout()));
|
|
|
|
c.stderr(subprocess_stdio_map(inner.stderr()));
|
2018-11-16 04:07:40 +00:00
|
|
|
|
|
|
|
// Spawn the command.
|
2019-06-18 01:02:08 +00:00
|
|
|
let child = c.spawn_async().map_err(DenoError::from)?;
|
2018-11-16 04:07:40 +00:00
|
|
|
|
|
|
|
let pid = child.id();
|
|
|
|
let resources = resources::add_child(child);
|
|
|
|
|
|
|
|
let mut res_args = msg::RunResArgs {
|
|
|
|
rid: resources.child_rid,
|
|
|
|
pid,
|
|
|
|
..Default::default()
|
|
|
|
};
|
|
|
|
|
|
|
|
if let Some(stdin_rid) = resources.stdin_rid {
|
|
|
|
res_args.stdin_rid = stdin_rid;
|
|
|
|
}
|
|
|
|
if let Some(stdout_rid) = resources.stdout_rid {
|
|
|
|
res_args.stdout_rid = stdout_rid;
|
|
|
|
}
|
|
|
|
if let Some(stderr_rid) = resources.stderr_rid {
|
|
|
|
res_args.stderr_rid = stderr_rid;
|
|
|
|
}
|
|
|
|
|
|
|
|
let builder = &mut FlatBufferBuilder::new();
|
|
|
|
let inner = msg::RunRes::create(builder, &res_args);
|
2019-06-18 01:02:08 +00:00
|
|
|
Ok(Op::Sync(serialize_response(
|
2019-06-14 17:58:20 +00:00
|
|
|
cmd_id,
|
2018-11-16 04:07:40 +00:00
|
|
|
builder,
|
|
|
|
msg::BaseArgs {
|
|
|
|
inner: Some(inner.as_union_value()),
|
|
|
|
inner_type: msg::Any::RunRes,
|
|
|
|
..Default::default()
|
|
|
|
},
|
2019-06-18 01:02:08 +00:00
|
|
|
)))
|
2018-11-16 04:07:40 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
fn op_run_status(
|
2019-04-09 17:11:25 +00:00
|
|
|
state: &ThreadSafeState,
|
2019-01-14 06:30:38 +00:00
|
|
|
base: &msg::Base<'_>,
|
2019-04-28 19:31:10 +00:00
|
|
|
data: Option<PinnedBuf>,
|
2019-06-18 01:02:08 +00:00
|
|
|
) -> CliOpResult {
|
2019-04-28 19:31:10 +00:00
|
|
|
assert!(data.is_none());
|
2019-06-14 17:58:20 +00:00
|
|
|
let cmd_id = base.cmd_id();
|
2018-11-16 04:07:40 +00:00
|
|
|
let inner = base.inner_as_run_status().unwrap();
|
|
|
|
let rid = inner.rid();
|
|
|
|
|
2019-06-18 01:02:08 +00:00
|
|
|
state.check_run()?;
|
2018-11-16 04:07:40 +00:00
|
|
|
|
2019-06-18 01:02:08 +00:00
|
|
|
let future = resources::child_status(rid)?;
|
2018-11-16 04:07:40 +00:00
|
|
|
|
|
|
|
let future = future.and_then(move |run_status| {
|
|
|
|
let code = run_status.code();
|
|
|
|
|
|
|
|
#[cfg(unix)]
|
|
|
|
let signal = run_status.signal();
|
|
|
|
#[cfg(not(unix))]
|
|
|
|
let signal = None;
|
|
|
|
|
|
|
|
code
|
|
|
|
.or(signal)
|
|
|
|
.expect("Should have either an exit code or a signal.");
|
|
|
|
let got_signal = signal.is_some();
|
|
|
|
|
|
|
|
let builder = &mut FlatBufferBuilder::new();
|
|
|
|
let inner = msg::RunStatusRes::create(
|
|
|
|
builder,
|
|
|
|
&msg::RunStatusResArgs {
|
|
|
|
got_signal,
|
|
|
|
exit_code: code.unwrap_or(-1),
|
|
|
|
exit_signal: signal.unwrap_or(-1),
|
|
|
|
},
|
|
|
|
);
|
|
|
|
Ok(serialize_response(
|
2019-06-14 17:58:20 +00:00
|
|
|
cmd_id,
|
2018-11-16 04:07:40 +00:00
|
|
|
builder,
|
|
|
|
msg::BaseArgs {
|
|
|
|
inner: Some(inner.as_union_value()),
|
|
|
|
inner_type: msg::Any::RunStatusRes,
|
|
|
|
..Default::default()
|
|
|
|
},
|
|
|
|
))
|
|
|
|
});
|
2019-06-18 01:02:08 +00:00
|
|
|
if base.sync() {
|
|
|
|
let buf = future.wait()?;
|
|
|
|
Ok(Op::Sync(buf))
|
|
|
|
} else {
|
|
|
|
Ok(Op::Async(Box::new(future)))
|
|
|
|
}
|
2018-11-16 04:07:40 +00:00
|
|
|
}
|
2019-01-08 19:44:06 +00:00
|
|
|
|
|
|
|
struct GetMessageFuture {
|
2019-04-09 17:11:25 +00:00
|
|
|
pub state: ThreadSafeState,
|
2019-01-08 19:44:06 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
impl Future for GetMessageFuture {
|
|
|
|
type Item = Option<Buf>;
|
|
|
|
type Error = ();
|
|
|
|
|
|
|
|
fn poll(&mut self) -> Result<Async<Self::Item>, Self::Error> {
|
2019-04-08 21:10:00 +00:00
|
|
|
let mut wc = self.state.worker_channels.lock().unwrap();
|
|
|
|
wc.1
|
|
|
|
.poll()
|
|
|
|
.map_err(|err| panic!("worker_channel recv err {:?}", err))
|
2019-01-08 19:44:06 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-04-01 19:09:59 +00:00
|
|
|
/// Get message from host as guest worker
|
2019-01-08 19:44:06 +00:00
|
|
|
fn op_worker_get_message(
|
2019-04-09 17:11:25 +00:00
|
|
|
state: &ThreadSafeState,
|
2019-01-14 06:30:38 +00:00
|
|
|
base: &msg::Base<'_>,
|
2019-04-28 19:31:10 +00:00
|
|
|
data: Option<PinnedBuf>,
|
2019-06-18 01:02:08 +00:00
|
|
|
) -> CliOpResult {
|
|
|
|
if base.sync() {
|
|
|
|
return Err(errors::no_sync_support());
|
|
|
|
}
|
2019-04-28 19:31:10 +00:00
|
|
|
assert!(data.is_none());
|
2019-06-14 17:58:20 +00:00
|
|
|
let cmd_id = base.cmd_id();
|
2019-01-08 19:44:06 +00:00
|
|
|
|
|
|
|
let op = GetMessageFuture {
|
2019-04-08 21:10:00 +00:00
|
|
|
state: state.clone(),
|
2019-01-08 19:44:06 +00:00
|
|
|
};
|
|
|
|
let op = op.map_err(move |_| -> DenoError { unimplemented!() });
|
|
|
|
let op = op.and_then(move |maybe_buf| -> DenoResult<Buf> {
|
|
|
|
debug!("op_worker_get_message");
|
|
|
|
let builder = &mut FlatBufferBuilder::new();
|
|
|
|
|
|
|
|
let data = maybe_buf.as_ref().map(|buf| builder.create_vector(buf));
|
|
|
|
let inner = msg::WorkerGetMessageRes::create(
|
|
|
|
builder,
|
|
|
|
&msg::WorkerGetMessageResArgs { data },
|
|
|
|
);
|
|
|
|
Ok(serialize_response(
|
2019-06-14 17:58:20 +00:00
|
|
|
cmd_id,
|
2019-01-08 19:44:06 +00:00
|
|
|
builder,
|
|
|
|
msg::BaseArgs {
|
|
|
|
inner: Some(inner.as_union_value()),
|
|
|
|
inner_type: msg::Any::WorkerGetMessageRes,
|
|
|
|
..Default::default()
|
|
|
|
},
|
|
|
|
))
|
|
|
|
});
|
2019-06-18 01:02:08 +00:00
|
|
|
Ok(Op::Async(Box::new(op)))
|
2019-01-08 19:44:06 +00:00
|
|
|
}
|
|
|
|
|
2019-04-01 19:09:59 +00:00
|
|
|
/// Post message to host as guest worker
|
2019-01-08 19:44:06 +00:00
|
|
|
fn op_worker_post_message(
|
2019-04-09 17:11:25 +00:00
|
|
|
state: &ThreadSafeState,
|
2019-06-14 17:58:20 +00:00
|
|
|
base: &msg::Base<'_>,
|
2019-04-28 19:31:10 +00:00
|
|
|
data: Option<PinnedBuf>,
|
2019-06-18 01:02:08 +00:00
|
|
|
) -> CliOpResult {
|
2019-06-14 17:58:20 +00:00
|
|
|
let cmd_id = base.cmd_id();
|
2019-04-28 19:31:10 +00:00
|
|
|
let d = Vec::from(data.unwrap().as_ref()).into_boxed_slice();
|
2019-01-08 19:44:06 +00:00
|
|
|
|
2019-04-08 21:10:00 +00:00
|
|
|
let tx = {
|
|
|
|
let wc = state.worker_channels.lock().unwrap();
|
|
|
|
wc.0.clone()
|
2019-01-08 19:44:06 +00:00
|
|
|
};
|
2019-06-18 01:02:08 +00:00
|
|
|
tx.send(d)
|
|
|
|
.wait()
|
|
|
|
.map_err(|e| errors::new(ErrorKind::Other, e.to_string()))?;
|
|
|
|
let builder = &mut FlatBufferBuilder::new();
|
2019-01-08 19:44:06 +00:00
|
|
|
|
2019-06-18 01:02:08 +00:00
|
|
|
ok_buf(serialize_response(
|
|
|
|
cmd_id,
|
|
|
|
builder,
|
|
|
|
msg::BaseArgs {
|
|
|
|
..Default::default()
|
|
|
|
},
|
|
|
|
))
|
2019-01-08 19:44:06 +00:00
|
|
|
}
|
2019-04-01 19:09:59 +00:00
|
|
|
|
|
|
|
/// Create worker as the host
|
|
|
|
fn op_create_worker(
|
2019-04-09 17:11:25 +00:00
|
|
|
state: &ThreadSafeState,
|
2019-04-01 19:09:59 +00:00
|
|
|
base: &msg::Base<'_>,
|
2019-04-28 19:31:10 +00:00
|
|
|
data: Option<PinnedBuf>,
|
2019-06-18 01:02:08 +00:00
|
|
|
) -> CliOpResult {
|
2019-04-28 19:31:10 +00:00
|
|
|
assert!(data.is_none());
|
2019-06-14 17:58:20 +00:00
|
|
|
let cmd_id = base.cmd_id();
|
2019-04-01 19:09:59 +00:00
|
|
|
let inner = base.inner_as_create_worker().unwrap();
|
|
|
|
let specifier = inner.specifier().unwrap();
|
|
|
|
|
2019-06-08 08:08:00 +00:00
|
|
|
let parent_state = state.clone();
|
2019-04-08 21:10:00 +00:00
|
|
|
|
2019-06-08 08:08:00 +00:00
|
|
|
let child_state = ThreadSafeState::new(
|
|
|
|
parent_state.flags.clone(),
|
|
|
|
parent_state.argv.clone(),
|
|
|
|
op_selector_std,
|
|
|
|
parent_state.progress.clone(),
|
|
|
|
);
|
|
|
|
let rid = child_state.resource.rid;
|
|
|
|
let name = format!("USER-WORKER-{}", specifier);
|
|
|
|
|
|
|
|
let mut worker =
|
|
|
|
Worker::new(name, startup_data::deno_isolate_init(), child_state);
|
|
|
|
js_check(worker.execute("denoMain()"));
|
|
|
|
js_check(worker.execute("workerMain()"));
|
|
|
|
|
2019-06-18 01:02:08 +00:00
|
|
|
let module_specifier = ModuleSpecifier::resolve_root(specifier)?;
|
2019-06-14 03:43:54 +00:00
|
|
|
|
2019-06-18 01:02:08 +00:00
|
|
|
let op = worker
|
|
|
|
.execute_mod_async(&module_specifier, false)
|
|
|
|
.and_then(move |()| {
|
|
|
|
let mut workers_tl = parent_state.workers.lock().unwrap();
|
|
|
|
workers_tl.insert(rid, worker.shared());
|
|
|
|
let builder = &mut FlatBufferBuilder::new();
|
|
|
|
let msg_inner = msg::CreateWorkerRes::create(
|
|
|
|
builder,
|
|
|
|
&msg::CreateWorkerResArgs { rid },
|
|
|
|
);
|
|
|
|
Ok(serialize_response(
|
|
|
|
cmd_id,
|
|
|
|
builder,
|
|
|
|
msg::BaseArgs {
|
|
|
|
inner: Some(msg_inner.as_union_value()),
|
|
|
|
inner_type: msg::Any::CreateWorkerRes,
|
|
|
|
..Default::default()
|
|
|
|
},
|
|
|
|
))
|
|
|
|
}).map_err(|err| match err {
|
|
|
|
errors::RustOrJsError::Js(_) => errors::worker_init_failed(),
|
|
|
|
errors::RustOrJsError::Rust(err) => err,
|
|
|
|
});
|
|
|
|
|
|
|
|
let result = op.wait()?;
|
|
|
|
Ok(Op::Sync(result))
|
2019-04-01 19:09:59 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/// Return when the worker closes
|
|
|
|
fn op_host_get_worker_closed(
|
2019-04-09 17:11:25 +00:00
|
|
|
state: &ThreadSafeState,
|
2019-04-01 19:09:59 +00:00
|
|
|
base: &msg::Base<'_>,
|
2019-04-28 19:31:10 +00:00
|
|
|
data: Option<PinnedBuf>,
|
2019-06-18 01:02:08 +00:00
|
|
|
) -> CliOpResult {
|
|
|
|
if base.sync() {
|
|
|
|
return Err(errors::no_sync_support());
|
|
|
|
}
|
2019-04-28 19:31:10 +00:00
|
|
|
assert!(data.is_none());
|
2019-06-14 17:58:20 +00:00
|
|
|
let cmd_id = base.cmd_id();
|
2019-04-01 19:09:59 +00:00
|
|
|
let inner = base.inner_as_host_get_worker_closed().unwrap();
|
|
|
|
let rid = inner.rid();
|
2019-04-08 21:10:00 +00:00
|
|
|
let state = state.clone();
|
2019-04-01 19:09:59 +00:00
|
|
|
|
|
|
|
let shared_worker_future = {
|
|
|
|
let workers_tl = state.workers.lock().unwrap();
|
|
|
|
let worker = workers_tl.get(&rid).unwrap();
|
|
|
|
worker.clone()
|
|
|
|
};
|
|
|
|
|
2019-06-18 01:02:08 +00:00
|
|
|
let op = Box::new(shared_worker_future.then(move |_result| {
|
2019-04-01 19:09:59 +00:00
|
|
|
let builder = &mut FlatBufferBuilder::new();
|
|
|
|
|
|
|
|
Ok(serialize_response(
|
2019-06-14 17:58:20 +00:00
|
|
|
cmd_id,
|
2019-04-01 19:09:59 +00:00
|
|
|
builder,
|
|
|
|
msg::BaseArgs {
|
|
|
|
..Default::default()
|
|
|
|
},
|
|
|
|
))
|
2019-06-18 01:02:08 +00:00
|
|
|
}));
|
|
|
|
Ok(Op::Async(Box::new(op)))
|
2019-04-01 19:09:59 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/// Get message from guest worker as host
|
|
|
|
fn op_host_get_message(
|
2019-04-09 17:11:25 +00:00
|
|
|
_state: &ThreadSafeState,
|
2019-04-01 19:09:59 +00:00
|
|
|
base: &msg::Base<'_>,
|
2019-04-28 19:31:10 +00:00
|
|
|
data: Option<PinnedBuf>,
|
2019-06-18 01:02:08 +00:00
|
|
|
) -> CliOpResult {
|
|
|
|
if base.sync() {
|
|
|
|
return Err(errors::no_sync_support());
|
|
|
|
}
|
2019-04-28 19:31:10 +00:00
|
|
|
assert!(data.is_none());
|
2019-06-14 17:58:20 +00:00
|
|
|
let cmd_id = base.cmd_id();
|
2019-04-01 19:09:59 +00:00
|
|
|
let inner = base.inner_as_host_get_message().unwrap();
|
|
|
|
let rid = inner.rid();
|
|
|
|
|
|
|
|
let op = resources::get_message_from_worker(rid);
|
|
|
|
let op = op.map_err(move |_| -> DenoError { unimplemented!() });
|
|
|
|
let op = op.and_then(move |maybe_buf| -> DenoResult<Buf> {
|
|
|
|
let builder = &mut FlatBufferBuilder::new();
|
|
|
|
|
|
|
|
let data = maybe_buf.as_ref().map(|buf| builder.create_vector(buf));
|
|
|
|
let msg_inner = msg::HostGetMessageRes::create(
|
|
|
|
builder,
|
|
|
|
&msg::HostGetMessageResArgs { data },
|
|
|
|
);
|
|
|
|
Ok(serialize_response(
|
2019-06-14 17:58:20 +00:00
|
|
|
cmd_id,
|
2019-04-01 19:09:59 +00:00
|
|
|
builder,
|
|
|
|
msg::BaseArgs {
|
|
|
|
inner: Some(msg_inner.as_union_value()),
|
|
|
|
inner_type: msg::Any::HostGetMessageRes,
|
|
|
|
..Default::default()
|
|
|
|
},
|
|
|
|
))
|
|
|
|
});
|
2019-06-18 01:02:08 +00:00
|
|
|
Ok(Op::Async(Box::new(op)))
|
2019-04-01 19:09:59 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/// Post message to guest worker as host
|
|
|
|
fn op_host_post_message(
|
2019-04-09 17:11:25 +00:00
|
|
|
_state: &ThreadSafeState,
|
2019-04-01 19:09:59 +00:00
|
|
|
base: &msg::Base<'_>,
|
2019-04-28 19:31:10 +00:00
|
|
|
data: Option<PinnedBuf>,
|
2019-06-18 01:02:08 +00:00
|
|
|
) -> CliOpResult {
|
2019-06-14 17:58:20 +00:00
|
|
|
let cmd_id = base.cmd_id();
|
2019-04-01 19:09:59 +00:00
|
|
|
let inner = base.inner_as_host_post_message().unwrap();
|
|
|
|
let rid = inner.rid();
|
|
|
|
|
2019-04-28 19:31:10 +00:00
|
|
|
let d = Vec::from(data.unwrap().as_ref()).into_boxed_slice();
|
2019-04-01 19:09:59 +00:00
|
|
|
|
2019-06-18 01:02:08 +00:00
|
|
|
resources::post_message_to_worker(rid, d)
|
|
|
|
.wait()
|
|
|
|
.map_err(|e| errors::new(ErrorKind::Other, e.to_string()))?;
|
|
|
|
let builder = &mut FlatBufferBuilder::new();
|
2019-04-01 19:09:59 +00:00
|
|
|
|
2019-06-18 01:02:08 +00:00
|
|
|
ok_buf(serialize_response(
|
|
|
|
cmd_id,
|
|
|
|
builder,
|
|
|
|
msg::BaseArgs {
|
|
|
|
..Default::default()
|
|
|
|
},
|
|
|
|
))
|
2019-04-01 19:09:59 +00:00
|
|
|
}
|
2019-05-17 18:03:01 +00:00
|
|
|
|
|
|
|
fn op_get_random_values(
|
2019-06-11 14:34:39 +00:00
|
|
|
state: &ThreadSafeState,
|
2019-05-17 18:03:01 +00:00
|
|
|
_base: &msg::Base<'_>,
|
|
|
|
data: Option<PinnedBuf>,
|
2019-06-18 01:02:08 +00:00
|
|
|
) -> CliOpResult {
|
2019-06-11 14:34:39 +00:00
|
|
|
if let Some(ref seeded_rng) = state.seeded_rng {
|
|
|
|
let mut rng = seeded_rng.lock().unwrap();
|
|
|
|
rng.fill(&mut data.unwrap()[..]);
|
|
|
|
} else {
|
|
|
|
let mut rng = thread_rng();
|
|
|
|
rng.fill(&mut data.unwrap()[..]);
|
|
|
|
}
|
|
|
|
|
2019-06-18 01:02:08 +00:00
|
|
|
ok_buf(empty_buf())
|
2019-05-17 18:03:01 +00:00
|
|
|
}
|