2018-07-23 14:46:30 -04:00
|
|
|
// Copyright 2018 the Deno authors. All rights reserved. MIT license.
|
2018-09-18 11:53:16 -07:00
|
|
|
|
2018-09-27 00:56:39 -04:00
|
|
|
use errors;
|
2018-09-05 22:13:36 -04:00
|
|
|
use errors::DenoError;
|
2018-08-15 23:36:48 -04:00
|
|
|
use errors::DenoResult;
|
2018-08-26 10:51:25 +01:00
|
|
|
use fs as deno_fs;
|
2018-09-18 11:53:16 -07:00
|
|
|
use isolate::Buf;
|
|
|
|
use isolate::IsolateState;
|
|
|
|
use isolate::Op;
|
|
|
|
use msg;
|
|
|
|
|
2018-09-27 00:56:39 -04:00
|
|
|
use files;
|
2018-09-18 11:53:16 -07:00
|
|
|
use flatbuffers::FlatBufferBuilder;
|
2018-08-09 12:17:08 -07:00
|
|
|
use futures;
|
2018-09-22 04:42:07 -04:00
|
|
|
use futures::future::poll_fn;
|
2018-08-09 12:17:08 -07:00
|
|
|
use futures::sync::oneshot;
|
2018-09-22 04:42:07 -04:00
|
|
|
use futures::Poll;
|
2018-08-15 20:57:36 -04:00
|
|
|
use hyper;
|
|
|
|
use hyper::rt::{Future, Stream};
|
|
|
|
use hyper::Client;
|
2018-09-10 20:40:03 -07:00
|
|
|
use remove_dir_all::remove_dir_all;
|
2018-08-13 19:55:10 -04:00
|
|
|
use std;
|
2018-08-26 10:51:25 +01:00
|
|
|
use std::fs;
|
2018-09-12 07:28:48 -07:00
|
|
|
#[cfg(any(unix))]
|
|
|
|
use std::os::unix::fs::PermissionsExt;
|
2018-08-09 13:24:30 -04:00
|
|
|
use std::path::Path;
|
2018-09-22 04:42:07 -04:00
|
|
|
use std::path::PathBuf;
|
2018-09-18 11:53:16 -07:00
|
|
|
use std::sync::Arc;
|
2018-08-30 13:54:56 -04:00
|
|
|
use std::time::UNIX_EPOCH;
|
2018-08-15 23:36:48 -04:00
|
|
|
use std::time::{Duration, Instant};
|
2018-09-27 00:56:39 -04:00
|
|
|
use tokio;
|
2018-09-05 22:13:36 -04:00
|
|
|
use tokio::timer::Delay;
|
2018-09-27 00:56:39 -04:00
|
|
|
use tokio_io::AsyncRead;
|
|
|
|
use tokio_io::AsyncWrite;
|
2018-09-22 04:42:07 -04:00
|
|
|
use tokio_threadpool;
|
2018-08-15 23:36:48 -04:00
|
|
|
|
2018-09-05 22:13:36 -04:00
|
|
|
type OpResult = DenoResult<Buf>;
|
|
|
|
|
|
|
|
// TODO Ideally we wouldn't have to box the Op being returned.
|
|
|
|
// The box is just to make it easier to get a prototype refactor working.
|
2018-09-27 17:33:10 -04:00
|
|
|
type Handler =
|
|
|
|
fn(state: Arc<IsolateState>, base: &msg::Base, data: &'static mut [u8])
|
|
|
|
-> Box<Op>;
|
2018-07-06 11:27:36 -04:00
|
|
|
|
2018-09-18 11:53:16 -07:00
|
|
|
// Hopefully Rust optimizes this away.
|
|
|
|
fn empty_buf() -> Buf {
|
|
|
|
Box::new([])
|
|
|
|
}
|
|
|
|
|
2018-09-27 17:33:10 -04:00
|
|
|
pub fn msg_from_js(
|
|
|
|
state: Arc<IsolateState>,
|
|
|
|
control: &[u8],
|
|
|
|
data: &'static mut [u8],
|
|
|
|
) -> (bool, Box<Op>) {
|
|
|
|
let base = msg::get_root_as_base(control);
|
2018-09-18 11:53:16 -07:00
|
|
|
let is_sync = base.sync();
|
2018-08-13 19:55:10 -04:00
|
|
|
let msg_type = base.msg_type();
|
2018-09-05 22:13:36 -04:00
|
|
|
let cmd_id = base.cmd_id();
|
2018-09-02 02:33:24 -04:00
|
|
|
let handler: Handler = match msg_type {
|
|
|
|
msg::Any::Start => handle_start,
|
|
|
|
msg::Any::CodeFetch => handle_code_fetch,
|
|
|
|
msg::Any::CodeCache => handle_code_cache,
|
2018-09-24 22:46:36 -04:00
|
|
|
msg::Any::SetTimeout => handle_set_timeout,
|
2018-09-02 02:33:24 -04:00
|
|
|
msg::Any::Environ => handle_env,
|
|
|
|
msg::Any::FetchReq => handle_fetch_req,
|
|
|
|
msg::Any::TimerStart => handle_timer_start,
|
|
|
|
msg::Any::TimerClear => handle_timer_clear,
|
|
|
|
msg::Any::MakeTempDir => handle_make_temp_dir,
|
2018-09-10 14:18:36 +04:30
|
|
|
msg::Any::Mkdir => handle_mkdir,
|
2018-09-27 00:56:39 -04:00
|
|
|
msg::Any::Open => handle_open,
|
|
|
|
msg::Any::Read => handle_read,
|
|
|
|
msg::Any::Write => handle_write,
|
2018-09-10 20:40:03 -07:00
|
|
|
msg::Any::Remove => handle_remove,
|
2018-09-09 20:25:43 -04:00
|
|
|
msg::Any::ReadFile => handle_read_file,
|
2018-09-12 08:44:58 -07:00
|
|
|
msg::Any::Rename => handle_rename,
|
2018-09-24 21:20:49 -07:00
|
|
|
msg::Any::Readlink => handle_read_link,
|
2018-09-18 21:38:24 -07:00
|
|
|
msg::Any::Symlink => handle_symlink,
|
2018-09-02 02:33:24 -04:00
|
|
|
msg::Any::SetEnv => handle_set_env,
|
2018-09-12 00:08:53 +04:30
|
|
|
msg::Any::Stat => handle_stat,
|
2018-10-01 03:06:20 +08:00
|
|
|
msg::Any::Truncate => handle_truncate,
|
2018-09-11 09:00:57 -07:00
|
|
|
msg::Any::WriteFile => handle_write_file,
|
2018-09-02 02:33:24 -04:00
|
|
|
msg::Any::Exit => handle_exit,
|
2018-09-30 15:06:41 -07:00
|
|
|
msg::Any::CopyFile => handle_copy_file,
|
2018-08-15 23:36:48 -04:00
|
|
|
_ => panic!(format!(
|
|
|
|
"Unhandled message {}",
|
|
|
|
msg::enum_name_any(msg_type)
|
|
|
|
)),
|
|
|
|
};
|
|
|
|
|
2018-09-27 17:33:10 -04:00
|
|
|
let op: Box<Op> = handler(state.clone(), &base, data);
|
2018-09-18 11:53:16 -07:00
|
|
|
let boxed_op = Box::new(
|
|
|
|
op.or_else(move |err: DenoError| -> DenoResult<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 deno_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| -> DenoResult<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 is_sync || buf.len() > 0 {
|
|
|
|
buf
|
|
|
|
} else {
|
|
|
|
let builder = &mut FlatBufferBuilder::new();
|
|
|
|
serialize_response(
|
|
|
|
cmd_id,
|
|
|
|
builder,
|
|
|
|
msg::BaseArgs {
|
|
|
|
..Default::default()
|
|
|
|
},
|
|
|
|
)
|
2018-09-05 22:13:36 -04:00
|
|
|
};
|
2018-09-18 11:53:16 -07:00
|
|
|
Ok(buf)
|
|
|
|
}),
|
|
|
|
);
|
2018-08-15 23:36:48 -04:00
|
|
|
|
2018-09-18 11:53:16 -07:00
|
|
|
debug!(
|
|
|
|
"msg_from_js {} sync {}",
|
|
|
|
msg::enum_name_any(msg_type),
|
|
|
|
base.sync()
|
|
|
|
);
|
|
|
|
return (base.sync(), boxed_op);
|
2018-07-13 03:24:07 -04:00
|
|
|
}
|
|
|
|
|
2018-09-05 22:13:36 -04:00
|
|
|
fn permission_denied() -> DenoError {
|
|
|
|
DenoError::from(std::io::Error::new(
|
|
|
|
std::io::ErrorKind::PermissionDenied,
|
|
|
|
"permission denied",
|
|
|
|
))
|
|
|
|
}
|
|
|
|
|
2018-09-18 21:38:24 -07:00
|
|
|
fn not_implemented() -> DenoError {
|
|
|
|
DenoError::from(std::io::Error::new(
|
|
|
|
std::io::ErrorKind::Other,
|
2018-09-24 19:51:37 -04:00
|
|
|
"Not implemented",
|
2018-09-18 21:38:24 -07:00
|
|
|
))
|
|
|
|
}
|
|
|
|
|
2018-09-27 17:33:10 -04:00
|
|
|
fn handle_exit(
|
|
|
|
_config: Arc<IsolateState>,
|
|
|
|
base: &msg::Base,
|
|
|
|
_data: &'static mut [u8],
|
|
|
|
) -> Box<Op> {
|
2018-09-02 02:33:24 -04:00
|
|
|
let msg = base.msg_as_exit().unwrap();
|
|
|
|
std::process::exit(msg.code())
|
|
|
|
}
|
|
|
|
|
2018-09-27 17:33:10 -04:00
|
|
|
fn handle_start(
|
|
|
|
state: Arc<IsolateState>,
|
|
|
|
base: &msg::Base,
|
|
|
|
data: &'static mut [u8],
|
|
|
|
) -> Box<Op> {
|
|
|
|
assert_eq!(data.len(), 0);
|
2018-09-05 22:13:36 -04:00
|
|
|
let mut builder = FlatBufferBuilder::new();
|
2018-08-13 19:55:10 -04:00
|
|
|
|
2018-09-18 11:53:16 -07:00
|
|
|
let argv = state.argv.iter().map(|s| s.as_str()).collect::<Vec<_>>();
|
2018-08-13 19:55:10 -04:00
|
|
|
let argv_off = builder.create_vector_of_strings(argv.as_slice());
|
|
|
|
|
|
|
|
let cwd_path = std::env::current_dir().unwrap();
|
2018-08-23 03:58:59 -04:00
|
|
|
let cwd_off =
|
2018-08-26 10:51:25 +01:00
|
|
|
builder.create_string(deno_fs::normalize_path(cwd_path.as_ref()).as_ref());
|
2018-08-13 19:55:10 -04:00
|
|
|
|
2018-08-17 15:11:28 -04:00
|
|
|
let msg = msg::StartRes::create(
|
2018-09-05 22:13:36 -04:00
|
|
|
&mut builder,
|
2018-08-13 19:55:10 -04:00
|
|
|
&msg::StartResArgs {
|
|
|
|
cwd: Some(cwd_off),
|
|
|
|
argv: Some(argv_off),
|
2018-09-18 11:53:16 -07:00
|
|
|
debug_flag: state.flags.log_debug,
|
|
|
|
recompile_flag: state.flags.recompile,
|
2018-08-13 19:55:10 -04:00
|
|
|
..Default::default()
|
|
|
|
},
|
|
|
|
);
|
|
|
|
|
2018-09-09 19:48:10 -04:00
|
|
|
ok_future(serialize_response(
|
2018-09-05 22:13:36 -04:00
|
|
|
base.cmd_id(),
|
|
|
|
&mut builder,
|
|
|
|
msg::BaseArgs {
|
2018-08-13 19:55:10 -04:00
|
|
|
msg_type: msg::Any::StartRes,
|
2018-09-02 02:33:24 -04:00
|
|
|
msg: Some(msg.as_union_value()),
|
2018-08-13 19:55:10 -04:00
|
|
|
..Default::default()
|
|
|
|
},
|
2018-08-15 23:36:48 -04:00
|
|
|
))
|
2018-07-23 14:13:12 -04:00
|
|
|
}
|
|
|
|
|
2018-09-09 19:48:10 -04:00
|
|
|
fn serialize_response(
|
2018-09-05 22:13:36 -04:00
|
|
|
cmd_id: u32,
|
2018-08-15 23:36:48 -04:00
|
|
|
builder: &mut FlatBufferBuilder,
|
2018-09-05 22:13:36 -04:00
|
|
|
mut args: msg::BaseArgs,
|
|
|
|
) -> Buf {
|
|
|
|
args.cmd_id = cmd_id;
|
2018-08-17 15:11:28 -04:00
|
|
|
let base = msg::Base::create(builder, &args);
|
|
|
|
msg::finish_base_buffer(builder, base);
|
2018-09-02 01:58:23 -04:00
|
|
|
let data = builder.finished_data();
|
2018-09-09 19:48:10 -04:00
|
|
|
// println!("serialize_response {:x?}", data);
|
2018-09-05 22:13:36 -04:00
|
|
|
let vec = data.to_vec();
|
2018-09-18 11:53:16 -07:00
|
|
|
vec.into_boxed_slice()
|
2018-08-09 12:17:08 -07:00
|
|
|
}
|
|
|
|
|
2018-09-05 22:13:36 -04:00
|
|
|
fn ok_future(buf: Buf) -> Box<Op> {
|
|
|
|
Box::new(futures::future::ok(buf))
|
|
|
|
}
|
|
|
|
|
|
|
|
// Shout out to Earl Sweatshirt.
|
|
|
|
fn odd_future(err: DenoError) -> Box<Op> {
|
|
|
|
Box::new(futures::future::err(err))
|
2018-08-09 12:17:08 -07:00
|
|
|
}
|
|
|
|
|
2018-09-17 17:41:13 -07:00
|
|
|
// https://github.com/denoland/isolate/blob/golang/os.go#L100-L154
|
2018-09-27 17:33:10 -04:00
|
|
|
fn handle_code_fetch(
|
|
|
|
state: Arc<IsolateState>,
|
|
|
|
base: &msg::Base,
|
|
|
|
data: &'static mut [u8],
|
|
|
|
) -> Box<Op> {
|
|
|
|
assert_eq!(data.len(), 0);
|
2018-09-02 02:33:24 -04:00
|
|
|
let msg = base.msg_as_code_fetch().unwrap();
|
2018-09-05 22:13:36 -04:00
|
|
|
let cmd_id = base.cmd_id();
|
2018-09-02 02:33:24 -04:00
|
|
|
let module_specifier = msg.module_specifier().unwrap();
|
|
|
|
let containing_file = msg.containing_file().unwrap();
|
2018-07-19 21:14:32 -04:00
|
|
|
|
2018-09-18 11:53:16 -07:00
|
|
|
assert_eq!(state.dir.root.join("gen"), state.dir.gen, "Sanity check");
|
2018-07-06 11:27:36 -04:00
|
|
|
|
2018-09-05 22:13:36 -04:00
|
|
|
Box::new(futures::future::result(|| -> OpResult {
|
|
|
|
let builder = &mut FlatBufferBuilder::new();
|
2018-09-18 11:53:16 -07:00
|
|
|
let out = state.dir.code_fetch(module_specifier, containing_file)?;
|
2018-09-05 22:13:36 -04:00
|
|
|
let mut msg_args = msg::CodeFetchResArgs {
|
|
|
|
module_name: Some(builder.create_string(&out.module_name)),
|
|
|
|
filename: Some(builder.create_string(&out.filename)),
|
|
|
|
source_code: Some(builder.create_string(&out.source_code)),
|
2018-08-15 23:36:48 -04:00
|
|
|
..Default::default()
|
2018-09-05 22:13:36 -04:00
|
|
|
};
|
|
|
|
match out.maybe_output_code {
|
|
|
|
Some(ref output_code) => {
|
|
|
|
msg_args.output_code = Some(builder.create_string(output_code));
|
|
|
|
}
|
|
|
|
_ => (),
|
|
|
|
};
|
|
|
|
let msg = msg::CodeFetchRes::create(builder, &msg_args);
|
2018-09-09 19:48:10 -04:00
|
|
|
Ok(serialize_response(
|
2018-09-05 22:13:36 -04:00
|
|
|
cmd_id,
|
|
|
|
builder,
|
|
|
|
msg::BaseArgs {
|
|
|
|
msg: Some(msg.as_union_value()),
|
|
|
|
msg_type: msg::Any::CodeFetchRes,
|
|
|
|
..Default::default()
|
|
|
|
},
|
|
|
|
))
|
|
|
|
}()))
|
2018-07-13 03:24:07 -04:00
|
|
|
}
|
|
|
|
|
2018-09-17 17:41:13 -07:00
|
|
|
// https://github.com/denoland/isolate/blob/golang/os.go#L156-L169
|
2018-09-27 17:33:10 -04:00
|
|
|
fn handle_code_cache(
|
|
|
|
state: Arc<IsolateState>,
|
|
|
|
base: &msg::Base,
|
|
|
|
data: &'static mut [u8],
|
|
|
|
) -> Box<Op> {
|
|
|
|
assert_eq!(data.len(), 0);
|
2018-09-02 02:33:24 -04:00
|
|
|
let msg = base.msg_as_code_cache().unwrap();
|
|
|
|
let filename = msg.filename().unwrap();
|
|
|
|
let source_code = msg.source_code().unwrap();
|
|
|
|
let output_code = msg.output_code().unwrap();
|
2018-09-05 22:13:36 -04:00
|
|
|
Box::new(futures::future::result(|| -> OpResult {
|
2018-09-18 11:53:16 -07:00
|
|
|
state.dir.code_cache(filename, source_code, output_code)?;
|
|
|
|
Ok(empty_buf())
|
2018-09-05 22:13:36 -04:00
|
|
|
}()))
|
2018-07-06 11:27:36 -04:00
|
|
|
}
|
2018-08-09 12:17:08 -07:00
|
|
|
|
2018-09-27 17:33:10 -04:00
|
|
|
fn handle_set_timeout(
|
|
|
|
state: Arc<IsolateState>,
|
|
|
|
base: &msg::Base,
|
|
|
|
data: &'static mut [u8],
|
|
|
|
) -> Box<Op> {
|
|
|
|
assert_eq!(data.len(), 0);
|
2018-09-24 22:46:36 -04:00
|
|
|
let msg = base.msg_as_set_timeout().unwrap();
|
|
|
|
let val = msg.timeout() as isize;
|
|
|
|
state
|
|
|
|
.timeout
|
|
|
|
.swap(val, std::sync::atomic::Ordering::Relaxed);
|
|
|
|
ok_future(empty_buf())
|
|
|
|
}
|
|
|
|
|
2018-09-27 17:33:10 -04:00
|
|
|
fn handle_set_env(
|
|
|
|
state: Arc<IsolateState>,
|
|
|
|
base: &msg::Base,
|
|
|
|
data: &'static mut [u8],
|
|
|
|
) -> Box<Op> {
|
|
|
|
assert_eq!(data.len(), 0);
|
2018-09-02 02:33:24 -04:00
|
|
|
let msg = base.msg_as_set_env().unwrap();
|
|
|
|
let key = msg.key().unwrap();
|
|
|
|
let value = msg.value().unwrap();
|
|
|
|
|
2018-09-18 11:53:16 -07:00
|
|
|
if !state.flags.allow_env {
|
2018-09-05 22:13:36 -04:00
|
|
|
return odd_future(permission_denied());
|
2018-08-31 12:51:12 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
std::env::set_var(key, value);
|
2018-09-18 11:53:16 -07:00
|
|
|
ok_future(empty_buf())
|
2018-08-31 12:51:12 +01:00
|
|
|
}
|
|
|
|
|
2018-09-27 17:33:10 -04:00
|
|
|
fn handle_env(
|
|
|
|
state: Arc<IsolateState>,
|
|
|
|
base: &msg::Base,
|
|
|
|
data: &'static mut [u8],
|
|
|
|
) -> Box<Op> {
|
|
|
|
assert_eq!(data.len(), 0);
|
2018-09-05 22:13:36 -04:00
|
|
|
let cmd_id = base.cmd_id();
|
2018-09-18 11:53:16 -07:00
|
|
|
|
|
|
|
if !state.flags.allow_env {
|
2018-09-05 22:13:36 -04:00
|
|
|
return odd_future(permission_denied());
|
2018-08-31 12:51:12 +01:00
|
|
|
}
|
|
|
|
|
2018-09-05 22:13:36 -04:00
|
|
|
let builder = &mut FlatBufferBuilder::new();
|
2018-09-02 01:59:16 -04:00
|
|
|
let vars: Vec<_> = std::env::vars()
|
|
|
|
.map(|(key, value)| {
|
|
|
|
let key = builder.create_string(&key);
|
|
|
|
let value = builder.create_string(&value);
|
2018-08-31 12:51:12 +01:00
|
|
|
|
2018-09-02 01:59:16 -04:00
|
|
|
msg::EnvPair::create(
|
|
|
|
builder,
|
|
|
|
&msg::EnvPairArgs {
|
|
|
|
key: Some(key),
|
|
|
|
value: Some(value),
|
|
|
|
..Default::default()
|
|
|
|
},
|
|
|
|
)
|
2018-09-17 17:41:13 -07:00
|
|
|
}).collect();
|
2018-09-02 01:58:23 -04:00
|
|
|
let tables = builder.create_vector(&vars);
|
2018-08-31 12:51:12 +01:00
|
|
|
let msg = msg::EnvironRes::create(
|
|
|
|
builder,
|
|
|
|
&msg::EnvironResArgs {
|
|
|
|
map: Some(tables),
|
|
|
|
..Default::default()
|
|
|
|
},
|
|
|
|
);
|
2018-09-09 19:48:10 -04:00
|
|
|
ok_future(serialize_response(
|
2018-09-05 22:13:36 -04:00
|
|
|
cmd_id,
|
2018-08-31 12:51:12 +01:00
|
|
|
builder,
|
2018-09-05 22:13:36 -04:00
|
|
|
msg::BaseArgs {
|
2018-09-02 01:58:23 -04:00
|
|
|
msg: Some(msg.as_union_value()),
|
2018-08-31 12:51:12 +01:00
|
|
|
msg_type: msg::Any::EnvironRes,
|
|
|
|
..Default::default()
|
|
|
|
},
|
|
|
|
))
|
|
|
|
}
|
|
|
|
|
2018-09-27 17:33:10 -04:00
|
|
|
fn handle_fetch_req(
|
|
|
|
state: Arc<IsolateState>,
|
|
|
|
base: &msg::Base,
|
|
|
|
data: &'static mut [u8],
|
|
|
|
) -> Box<Op> {
|
|
|
|
assert_eq!(data.len(), 0);
|
2018-09-02 02:33:24 -04:00
|
|
|
let msg = base.msg_as_fetch_req().unwrap();
|
2018-09-05 22:13:36 -04:00
|
|
|
let cmd_id = base.cmd_id();
|
2018-09-02 02:33:24 -04:00
|
|
|
let id = msg.id();
|
|
|
|
let url = msg.url().unwrap();
|
2018-09-05 22:13:36 -04:00
|
|
|
|
2018-09-18 11:53:16 -07:00
|
|
|
if !state.flags.allow_net {
|
2018-09-05 22:13:36 -04:00
|
|
|
return odd_future(permission_denied());
|
|
|
|
}
|
|
|
|
|
2018-08-15 20:57:36 -04:00
|
|
|
let url = url.parse::<hyper::Uri>().unwrap();
|
|
|
|
let client = Client::new();
|
|
|
|
|
2018-09-18 11:53:16 -07:00
|
|
|
debug!("Before fetch {}", url);
|
2018-09-12 23:46:42 +04:30
|
|
|
let future = client.get(url).and_then(move |res| {
|
2018-09-05 22:13:36 -04:00
|
|
|
let status = res.status().as_u16() as i32;
|
2018-09-18 11:53:16 -07:00
|
|
|
debug!("fetch {}", status);
|
2018-09-12 23:46:42 +04:30
|
|
|
|
|
|
|
let headers = {
|
|
|
|
let map = res.headers();
|
|
|
|
let keys = map
|
|
|
|
.keys()
|
|
|
|
.map(|s| s.as_str().to_string())
|
|
|
|
.collect::<Vec<_>>();
|
|
|
|
let values = map
|
|
|
|
.values()
|
|
|
|
.map(|s| s.to_str().unwrap().to_string())
|
|
|
|
.collect::<Vec<_>>();
|
|
|
|
(keys, values)
|
|
|
|
};
|
|
|
|
|
2018-09-05 22:13:36 -04:00
|
|
|
// TODO Handle streaming body.
|
2018-09-12 23:46:42 +04:30
|
|
|
res
|
|
|
|
.into_body()
|
|
|
|
.concat2()
|
|
|
|
.map(move |body| (status, body, headers))
|
2018-09-05 22:13:36 -04:00
|
|
|
});
|
|
|
|
|
|
|
|
let future = future.map_err(|err| -> DenoError { err.into() }).and_then(
|
2018-09-12 23:46:42 +04:30
|
|
|
move |(status, body, headers)| {
|
2018-09-18 11:53:16 -07:00
|
|
|
debug!("fetch body ");
|
2018-09-05 22:13:36 -04:00
|
|
|
let builder = &mut FlatBufferBuilder::new();
|
|
|
|
// Send the first message without a body. This is just to indicate
|
|
|
|
// what status code.
|
|
|
|
let body_off = builder.create_vector(body.as_ref());
|
2018-09-12 23:46:42 +04:30
|
|
|
let header_keys: Vec<&str> = headers.0.iter().map(|s| &**s).collect();
|
|
|
|
let header_keys_off =
|
|
|
|
builder.create_vector_of_strings(header_keys.as_slice());
|
|
|
|
let header_values: Vec<&str> = headers.1.iter().map(|s| &**s).collect();
|
|
|
|
let header_values_off =
|
|
|
|
builder.create_vector_of_strings(header_values.as_slice());
|
|
|
|
|
2018-09-05 22:13:36 -04:00
|
|
|
let msg = msg::FetchRes::create(
|
|
|
|
builder,
|
|
|
|
&msg::FetchResArgs {
|
|
|
|
id,
|
|
|
|
status,
|
|
|
|
body: Some(body_off),
|
2018-09-12 23:46:42 +04:30
|
|
|
header_key: Some(header_keys_off),
|
|
|
|
header_value: Some(header_values_off),
|
2018-09-05 22:13:36 -04:00
|
|
|
..Default::default()
|
|
|
|
},
|
|
|
|
);
|
2018-09-12 23:46:42 +04:30
|
|
|
|
2018-09-09 19:48:10 -04:00
|
|
|
Ok(serialize_response(
|
2018-09-05 22:13:36 -04:00
|
|
|
cmd_id,
|
|
|
|
builder,
|
|
|
|
msg::BaseArgs {
|
|
|
|
msg: Some(msg.as_union_value()),
|
|
|
|
msg_type: msg::Any::FetchRes,
|
|
|
|
..Default::default()
|
|
|
|
},
|
|
|
|
))
|
|
|
|
},
|
2018-08-15 20:57:36 -04:00
|
|
|
);
|
2018-09-05 22:13:36 -04:00
|
|
|
Box::new(future)
|
2018-08-15 20:57:36 -04:00
|
|
|
}
|
|
|
|
|
2018-08-09 12:17:08 -07:00
|
|
|
fn set_timeout<F>(
|
|
|
|
cb: F,
|
|
|
|
delay: u32,
|
|
|
|
) -> (
|
|
|
|
impl Future<Item = (), Error = ()>,
|
|
|
|
futures::sync::oneshot::Sender<()>,
|
|
|
|
)
|
|
|
|
where
|
|
|
|
F: FnOnce() -> (),
|
|
|
|
{
|
|
|
|
let (cancel_tx, cancel_rx) = oneshot::channel::<()>();
|
|
|
|
let when = Instant::now() + Duration::from_millis(delay.into());
|
|
|
|
let delay_task = Delay::new(when)
|
|
|
|
.map_err(|e| panic!("timer failed; err={:?}", e))
|
|
|
|
.and_then(|_| {
|
|
|
|
cb();
|
|
|
|
Ok(())
|
2018-09-17 17:41:13 -07:00
|
|
|
}).select(cancel_rx)
|
2018-08-09 12:17:08 -07:00
|
|
|
.map(|_| ())
|
|
|
|
.map_err(|_| ());
|
|
|
|
|
|
|
|
(delay_task, cancel_tx)
|
|
|
|
}
|
|
|
|
|
2018-09-22 04:42:07 -04: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),
|
|
|
|
Err(_) => panic!("blocking error"),
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// TODO Do not use macro for the blocking function.. We should instead be able
|
|
|
|
// to do this with a normal function, but there seems to some type system
|
|
|
|
// issues. The type of this function should be something like this:
|
|
|
|
// fn blocking<F>(is_sync: bool, f: F) -> Box<Op>
|
|
|
|
// where F: FnOnce() -> DenoResult<Buf>
|
|
|
|
macro_rules! blocking {
|
|
|
|
($is_sync:expr,$fn:expr) => {
|
|
|
|
if $is_sync {
|
|
|
|
// If synchronous, execute the function immediately on the main thread.
|
|
|
|
Box::new(futures::future::result($fn()))
|
|
|
|
} else {
|
|
|
|
// Otherwise dispatch to thread pool.
|
|
|
|
Box::new(poll_fn(move || convert_blocking($fn)))
|
|
|
|
}
|
|
|
|
};
|
|
|
|
}
|
|
|
|
|
2018-09-27 17:33:10 -04:00
|
|
|
fn handle_make_temp_dir(
|
|
|
|
state: Arc<IsolateState>,
|
|
|
|
base: &msg::Base,
|
|
|
|
data: &'static mut [u8],
|
|
|
|
) -> Box<Op> {
|
|
|
|
assert_eq!(data.len(), 0);
|
2018-09-05 22:13:36 -04:00
|
|
|
let base = Box::new(*base);
|
2018-09-02 02:33:24 -04:00
|
|
|
let msg = base.msg_as_make_temp_dir().unwrap();
|
2018-09-05 22:13:36 -04:00
|
|
|
let cmd_id = base.cmd_id();
|
|
|
|
|
2018-09-18 11:53:16 -07:00
|
|
|
if !state.flags.allow_write {
|
2018-09-13 04:17:17 +09:00
|
|
|
return odd_future(permission_denied());
|
2018-08-24 00:36:45 +02:00
|
|
|
}
|
2018-09-22 04:42:07 -04:00
|
|
|
|
|
|
|
let dir = msg.dir().map(PathBuf::from);
|
|
|
|
let prefix = msg.prefix().map(String::from);
|
|
|
|
let suffix = msg.suffix().map(String::from);
|
|
|
|
|
|
|
|
blocking!(base.sync(), || -> OpResult {
|
2018-09-05 22:13:36 -04:00
|
|
|
// TODO(piscisaureus): use byte vector for paths, not a string.
|
2018-09-17 17:41:13 -07:00
|
|
|
// See https://github.com/denoland/isolate/issues/627.
|
2018-09-05 22:13:36 -04:00
|
|
|
// We can't assume that paths are always valid utf8 strings.
|
2018-09-22 04:42:07 -04: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-05 22:13:36 -04:00
|
|
|
let builder = &mut FlatBufferBuilder::new();
|
|
|
|
let path_off = builder.create_string(path.to_str().unwrap());
|
|
|
|
let msg = msg::MakeTempDirRes::create(
|
|
|
|
builder,
|
|
|
|
&msg::MakeTempDirResArgs {
|
|
|
|
path: Some(path_off),
|
|
|
|
..Default::default()
|
|
|
|
},
|
|
|
|
);
|
2018-09-09 19:48:10 -04:00
|
|
|
Ok(serialize_response(
|
2018-09-05 22:13:36 -04:00
|
|
|
cmd_id,
|
|
|
|
builder,
|
|
|
|
msg::BaseArgs {
|
|
|
|
msg: Some(msg.as_union_value()),
|
|
|
|
msg_type: msg::Any::MakeTempDirRes,
|
|
|
|
..Default::default()
|
|
|
|
},
|
|
|
|
))
|
2018-09-22 04:42:07 -04:00
|
|
|
})
|
2018-08-24 00:36:45 +02:00
|
|
|
}
|
|
|
|
|
2018-09-27 17:33:10 -04:00
|
|
|
fn handle_mkdir(
|
|
|
|
state: Arc<IsolateState>,
|
|
|
|
base: &msg::Base,
|
|
|
|
data: &'static mut [u8],
|
|
|
|
) -> Box<Op> {
|
|
|
|
assert_eq!(data.len(), 0);
|
2018-09-10 14:18:36 +04:30
|
|
|
let msg = base.msg_as_mkdir().unwrap();
|
2018-09-14 12:30:43 -07:00
|
|
|
let mode = msg.mode();
|
2018-09-22 04:42:07 -04:00
|
|
|
let path = String::from(msg.path().unwrap());
|
2018-09-18 11:53:16 -07:00
|
|
|
|
|
|
|
if !state.flags.allow_write {
|
2018-09-05 22:13:36 -04:00
|
|
|
return odd_future(permission_denied());
|
|
|
|
}
|
2018-09-22 04:42:07 -04:00
|
|
|
|
|
|
|
blocking!(base.sync(), || {
|
2018-09-10 14:18:36 +04:30
|
|
|
debug!("handle_mkdir {}", path);
|
2018-09-22 04:42:07 -04:00
|
|
|
deno_fs::mkdir(Path::new(&path), mode)?;
|
2018-09-18 11:53:16 -07:00
|
|
|
Ok(empty_buf())
|
2018-09-22 04:42:07 -04:00
|
|
|
})
|
2018-08-26 10:56:30 +04:30
|
|
|
}
|
|
|
|
|
2018-09-27 00:56:39 -04:00
|
|
|
fn handle_open(
|
|
|
|
_state: Arc<IsolateState>,
|
|
|
|
base: &msg::Base,
|
|
|
|
data: &'static mut [u8],
|
|
|
|
) -> Box<Op> {
|
|
|
|
assert_eq!(data.len(), 0);
|
|
|
|
let cmd_id = base.cmd_id();
|
|
|
|
let msg = base.msg_as_open().unwrap();
|
|
|
|
let filename = PathBuf::from(msg.filename().unwrap());
|
|
|
|
// TODO let perm = msg.perm();
|
|
|
|
|
|
|
|
let op = tokio::fs::File::open(filename)
|
|
|
|
.map_err(|err| DenoError::from(err))
|
|
|
|
.and_then(move |fs_file| -> OpResult {
|
|
|
|
let dfile = files::add_fs_file(fs_file);
|
|
|
|
let builder = &mut FlatBufferBuilder::new();
|
|
|
|
let msg = msg::OpenRes::create(
|
|
|
|
builder,
|
|
|
|
&msg::OpenResArgs {
|
|
|
|
fd: dfile.fd,
|
|
|
|
..Default::default()
|
|
|
|
},
|
|
|
|
);
|
|
|
|
Ok(serialize_response(
|
|
|
|
cmd_id,
|
|
|
|
builder,
|
|
|
|
msg::BaseArgs {
|
|
|
|
msg: Some(msg.as_union_value()),
|
|
|
|
msg_type: msg::Any::OpenRes,
|
|
|
|
..Default::default()
|
|
|
|
},
|
|
|
|
))
|
|
|
|
});
|
|
|
|
Box::new(op)
|
|
|
|
}
|
|
|
|
|
|
|
|
fn handle_read(
|
|
|
|
_state: Arc<IsolateState>,
|
|
|
|
base: &msg::Base,
|
|
|
|
data: &'static mut [u8],
|
|
|
|
) -> Box<Op> {
|
|
|
|
let cmd_id = base.cmd_id();
|
|
|
|
let msg = base.msg_as_read().unwrap();
|
|
|
|
let fd = msg.fd();
|
|
|
|
|
|
|
|
match files::lookup(fd) {
|
|
|
|
None => odd_future(errors::new(
|
|
|
|
errors::ErrorKind::BadFileDescriptor,
|
|
|
|
String::from("Bad File Descriptor"),
|
|
|
|
)),
|
|
|
|
Some(mut dfile) => {
|
|
|
|
let op = futures::future::poll_fn(move || {
|
|
|
|
let poll = dfile.poll_read(data);
|
|
|
|
poll
|
|
|
|
}).map_err(|err| DenoError::from(err))
|
|
|
|
.and_then(move |nread: usize| {
|
|
|
|
let builder = &mut FlatBufferBuilder::new();
|
|
|
|
let msg = msg::ReadRes::create(
|
|
|
|
builder,
|
|
|
|
&msg::ReadResArgs {
|
|
|
|
nread: nread as u32,
|
|
|
|
eof: nread == 0,
|
|
|
|
..Default::default()
|
|
|
|
},
|
|
|
|
);
|
|
|
|
Ok(serialize_response(
|
|
|
|
cmd_id,
|
|
|
|
builder,
|
|
|
|
msg::BaseArgs {
|
|
|
|
msg: Some(msg.as_union_value()),
|
|
|
|
msg_type: msg::Any::ReadRes,
|
|
|
|
..Default::default()
|
|
|
|
},
|
|
|
|
))
|
|
|
|
});
|
|
|
|
Box::new(op)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
fn handle_write(
|
|
|
|
_state: Arc<IsolateState>,
|
|
|
|
base: &msg::Base,
|
|
|
|
data: &'static mut [u8],
|
|
|
|
) -> Box<Op> {
|
|
|
|
let cmd_id = base.cmd_id();
|
|
|
|
let msg = base.msg_as_write().unwrap();
|
|
|
|
let fd = msg.fd();
|
|
|
|
|
|
|
|
match files::lookup(fd) {
|
|
|
|
None => odd_future(errors::new(
|
|
|
|
errors::ErrorKind::BadFileDescriptor,
|
|
|
|
String::from("Bad File Descriptor"),
|
|
|
|
)),
|
|
|
|
Some(mut dfile) => {
|
|
|
|
let op = futures::future::poll_fn(move || {
|
|
|
|
let poll = dfile.poll_write(data);
|
|
|
|
poll
|
|
|
|
}).map_err(|err| DenoError::from(err))
|
|
|
|
.and_then(move |bytes_written: usize| {
|
|
|
|
let builder = &mut FlatBufferBuilder::new();
|
|
|
|
let msg = msg::WriteRes::create(
|
|
|
|
builder,
|
|
|
|
&msg::WriteResArgs {
|
|
|
|
nbyte: bytes_written as u32,
|
|
|
|
..Default::default()
|
|
|
|
},
|
|
|
|
);
|
|
|
|
Ok(serialize_response(
|
|
|
|
cmd_id,
|
|
|
|
builder,
|
|
|
|
msg::BaseArgs {
|
|
|
|
msg: Some(msg.as_union_value()),
|
|
|
|
msg_type: msg::Any::WriteRes,
|
|
|
|
..Default::default()
|
|
|
|
},
|
|
|
|
))
|
|
|
|
});
|
|
|
|
Box::new(op)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-09-27 17:33:10 -04:00
|
|
|
fn handle_remove(
|
|
|
|
state: Arc<IsolateState>,
|
|
|
|
base: &msg::Base,
|
|
|
|
data: &'static mut [u8],
|
|
|
|
) -> Box<Op> {
|
|
|
|
assert_eq!(data.len(), 0);
|
2018-09-10 20:40:03 -07:00
|
|
|
let msg = base.msg_as_remove().unwrap();
|
2018-09-22 04:42:07 -04:00
|
|
|
let path = PathBuf::from(msg.path().unwrap());
|
2018-09-10 20:40:03 -07:00
|
|
|
let recursive = msg.recursive();
|
2018-09-18 11:53:16 -07:00
|
|
|
if !state.flags.allow_write {
|
2018-09-10 20:40:03 -07:00
|
|
|
return odd_future(permission_denied());
|
|
|
|
}
|
2018-09-22 04:42:07 -04:00
|
|
|
blocking!(base.sync(), || {
|
|
|
|
debug!("handle_remove {}", path.display());
|
|
|
|
let metadata = fs::metadata(&path)?;
|
2018-09-10 20:40:03 -07:00
|
|
|
if metadata.is_file() {
|
2018-09-22 04:42:07 -04:00
|
|
|
fs::remove_file(&path)?;
|
2018-09-10 20:40:03 -07:00
|
|
|
} else {
|
|
|
|
if recursive {
|
2018-09-22 04:42:07 -04:00
|
|
|
remove_dir_all(&path)?;
|
2018-09-10 20:40:03 -07:00
|
|
|
} else {
|
2018-09-22 04:42:07 -04:00
|
|
|
fs::remove_dir(&path)?;
|
2018-09-10 20:40:03 -07:00
|
|
|
}
|
|
|
|
}
|
2018-09-18 11:53:16 -07:00
|
|
|
Ok(empty_buf())
|
2018-09-22 04:42:07 -04:00
|
|
|
})
|
2018-09-10 20:40:03 -07:00
|
|
|
}
|
|
|
|
|
2018-09-17 17:41:13 -07:00
|
|
|
// Prototype https://github.com/denoland/isolate/blob/golang/os.go#L171-L184
|
2018-09-27 17:33:10 -04:00
|
|
|
fn handle_read_file(
|
|
|
|
_config: Arc<IsolateState>,
|
|
|
|
base: &msg::Base,
|
|
|
|
data: &'static mut [u8],
|
|
|
|
) -> Box<Op> {
|
|
|
|
assert_eq!(data.len(), 0);
|
2018-09-09 20:25:43 -04:00
|
|
|
let msg = base.msg_as_read_file().unwrap();
|
2018-09-05 22:13:36 -04:00
|
|
|
let cmd_id = base.cmd_id();
|
2018-09-22 04:42:07 -04:00
|
|
|
let filename = PathBuf::from(msg.filename().unwrap());
|
|
|
|
debug!("handle_read_file {}", filename.display());
|
|
|
|
blocking!(base.sync(), || {
|
|
|
|
let vec = fs::read(&filename)?;
|
2018-09-05 22:13:36 -04:00
|
|
|
// Build the response message. memcpy data into msg.
|
|
|
|
// TODO(ry) zero-copy.
|
|
|
|
let builder = &mut FlatBufferBuilder::new();
|
|
|
|
let data_off = builder.create_vector(vec.as_slice());
|
2018-09-09 20:25:43 -04:00
|
|
|
let msg = msg::ReadFileRes::create(
|
2018-09-05 22:13:36 -04:00
|
|
|
builder,
|
2018-09-09 20:25:43 -04:00
|
|
|
&msg::ReadFileResArgs {
|
2018-09-05 22:13:36 -04:00
|
|
|
data: Some(data_off),
|
|
|
|
..Default::default()
|
|
|
|
},
|
|
|
|
);
|
2018-09-09 19:48:10 -04:00
|
|
|
Ok(serialize_response(
|
2018-09-05 22:13:36 -04:00
|
|
|
cmd_id,
|
|
|
|
builder,
|
|
|
|
msg::BaseArgs {
|
|
|
|
msg: Some(msg.as_union_value()),
|
2018-09-09 20:25:43 -04:00
|
|
|
msg_type: msg::Any::ReadFileRes,
|
2018-09-05 22:13:36 -04:00
|
|
|
..Default::default()
|
|
|
|
},
|
|
|
|
))
|
2018-09-22 04:42:07 -04:00
|
|
|
})
|
2018-08-09 13:24:30 -04:00
|
|
|
}
|
|
|
|
|
2018-09-30 15:06:41 -07:00
|
|
|
fn handle_copy_file(
|
|
|
|
state: Arc<IsolateState>,
|
|
|
|
base: &msg::Base,
|
|
|
|
data: &'static mut [u8],
|
|
|
|
) -> Box<Op> {
|
|
|
|
assert_eq!(data.len(), 0);
|
|
|
|
let msg = base.msg_as_copy_file().unwrap();
|
|
|
|
let from = PathBuf::from(msg.from().unwrap());
|
|
|
|
let to = PathBuf::from(msg.to().unwrap());
|
|
|
|
|
|
|
|
if !state.flags.allow_write {
|
|
|
|
return odd_future(permission_denied());
|
|
|
|
}
|
|
|
|
|
|
|
|
debug!("handle_copy_file {} {}", from.display(), to.display());
|
|
|
|
blocking!(base.sync(), || {
|
|
|
|
fs::copy(&from, &to)?;
|
|
|
|
Ok(empty_buf())
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
2018-08-29 14:22:25 +01: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 13:54:56 -04:00
|
|
|
$time
|
|
|
|
.and_then(|t| Ok(t.duration_since(UNIX_EPOCH).unwrap().as_secs()))
|
|
|
|
.unwrap_or(0)
|
|
|
|
}};
|
2018-08-29 14:22:25 +01:00
|
|
|
}
|
|
|
|
|
2018-09-12 07:28:48 -07:00
|
|
|
#[cfg(any(unix))]
|
2018-09-17 16:53:55 -07:00
|
|
|
fn get_mode(perm: fs::Permissions) -> u32 {
|
|
|
|
perm.mode()
|
2018-09-12 07:28:48 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
#[cfg(not(any(unix)))]
|
2018-09-17 16:53:55 -07:00
|
|
|
fn get_mode(_perm: fs::Permissions) -> u32 {
|
|
|
|
0
|
2018-09-12 07:28:48 -07:00
|
|
|
}
|
|
|
|
|
2018-09-27 17:33:10 -04:00
|
|
|
fn handle_stat(
|
|
|
|
_config: Arc<IsolateState>,
|
|
|
|
base: &msg::Base,
|
|
|
|
data: &'static mut [u8],
|
|
|
|
) -> Box<Op> {
|
|
|
|
assert_eq!(data.len(), 0);
|
2018-09-12 00:08:53 +04:30
|
|
|
let msg = base.msg_as_stat().unwrap();
|
2018-09-05 22:13:36 -04:00
|
|
|
let cmd_id = base.cmd_id();
|
2018-09-22 04:42:07 -04:00
|
|
|
let filename = PathBuf::from(msg.filename().unwrap());
|
2018-09-02 02:33:24 -04:00
|
|
|
let lstat = msg.lstat();
|
|
|
|
|
2018-09-22 04:42:07 -04:00
|
|
|
blocking!(base.sync(), || {
|
2018-09-05 22:13:36 -04:00
|
|
|
let builder = &mut FlatBufferBuilder::new();
|
2018-09-22 04:42:07 -04:00
|
|
|
debug!("handle_stat {} {}", filename.display(), lstat);
|
2018-09-05 22:13:36 -04:00
|
|
|
let metadata = if lstat {
|
2018-09-22 04:42:07 -04:00
|
|
|
fs::symlink_metadata(&filename)?
|
2018-09-05 22:13:36 -04:00
|
|
|
} else {
|
2018-09-22 04:42:07 -04:00
|
|
|
fs::metadata(&filename)?
|
2018-09-05 22:13:36 -04:00
|
|
|
};
|
|
|
|
|
2018-09-12 00:08:53 +04:30
|
|
|
let msg = msg::StatRes::create(
|
2018-09-05 22:13:36 -04:00
|
|
|
builder,
|
2018-09-12 00:08:53 +04:30
|
|
|
&msg::StatResArgs {
|
2018-09-05 22:13:36 -04: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-09-12 07:28:48 -07:00
|
|
|
mode: get_mode(metadata.permissions()),
|
2018-09-17 16:53:55 -07:00
|
|
|
has_mode: cfg!(target_family = "unix"),
|
2018-09-05 22:13:36 -04:00
|
|
|
..Default::default()
|
|
|
|
},
|
|
|
|
);
|
2018-08-29 14:22:25 +01:00
|
|
|
|
2018-09-09 19:48:10 -04:00
|
|
|
Ok(serialize_response(
|
2018-09-05 22:13:36 -04:00
|
|
|
cmd_id,
|
|
|
|
builder,
|
|
|
|
msg::BaseArgs {
|
|
|
|
msg: Some(msg.as_union_value()),
|
2018-09-12 00:08:53 +04:30
|
|
|
msg_type: msg::Any::StatRes,
|
2018-09-05 22:13:36 -04:00
|
|
|
..Default::default()
|
|
|
|
},
|
|
|
|
))
|
2018-09-22 04:42:07 -04:00
|
|
|
})
|
2018-08-29 14:22:25 +01:00
|
|
|
}
|
|
|
|
|
2018-09-27 17:33:10 -04:00
|
|
|
fn handle_write_file(
|
|
|
|
state: Arc<IsolateState>,
|
|
|
|
base: &msg::Base,
|
|
|
|
data: &'static mut [u8],
|
|
|
|
) -> Box<Op> {
|
2018-09-11 09:00:57 -07:00
|
|
|
let msg = base.msg_as_write_file().unwrap();
|
2018-09-02 02:33:24 -04:00
|
|
|
|
2018-09-18 11:53:16 -07:00
|
|
|
if !state.flags.allow_write {
|
2018-09-13 04:17:17 +09:00
|
|
|
return odd_future(permission_denied());
|
|
|
|
}
|
2018-09-18 11:53:16 -07:00
|
|
|
|
2018-09-22 04:42:07 -04:00
|
|
|
let filename = String::from(msg.filename().unwrap());
|
|
|
|
let perm = msg.perm();
|
|
|
|
|
|
|
|
blocking!(base.sync(), || -> OpResult {
|
2018-09-27 17:33:10 -04:00
|
|
|
debug!("handle_write_file {} {}", filename, data.len());
|
|
|
|
deno_fs::write_file(Path::new(&filename), data, perm)?;
|
2018-09-18 11:53:16 -07:00
|
|
|
Ok(empty_buf())
|
2018-09-22 04:42:07 -04:00
|
|
|
})
|
2018-08-22 13:19:32 -04:00
|
|
|
}
|
|
|
|
|
2018-09-18 11:53:16 -07:00
|
|
|
fn remove_timer(state: Arc<IsolateState>, timer_id: u32) {
|
|
|
|
let mut timers = state.timers.lock().unwrap();
|
|
|
|
timers.remove(&timer_id);
|
2018-08-09 12:17:08 -07:00
|
|
|
}
|
|
|
|
|
2018-09-17 17:41:13 -07:00
|
|
|
// Prototype: https://github.com/ry/isolate/blob/golang/timers.go#L25-L39
|
2018-09-27 17:33:10 -04:00
|
|
|
fn handle_timer_start(
|
|
|
|
state: Arc<IsolateState>,
|
|
|
|
base: &msg::Base,
|
|
|
|
data: &'static mut [u8],
|
|
|
|
) -> Box<Op> {
|
|
|
|
assert_eq!(data.len(), 0);
|
2018-08-09 12:17:08 -07:00
|
|
|
debug!("handle_timer_start");
|
2018-09-02 02:33:24 -04:00
|
|
|
let msg = base.msg_as_timer_start().unwrap();
|
2018-09-05 22:13:36 -04:00
|
|
|
let cmd_id = base.cmd_id();
|
2018-09-02 02:33:24 -04:00
|
|
|
let timer_id = msg.id();
|
|
|
|
let delay = msg.delay();
|
2018-08-09 12:17:08 -07:00
|
|
|
|
2018-09-18 11:53:16 -07:00
|
|
|
let config2 = state.clone();
|
2018-09-05 22:13:36 -04:00
|
|
|
let future = {
|
2018-08-09 12:17:08 -07:00
|
|
|
let (delay_task, cancel_delay) = set_timeout(
|
|
|
|
move || {
|
2018-09-18 11:53:16 -07:00
|
|
|
remove_timer(config2, timer_id);
|
2018-08-09 12:17:08 -07:00
|
|
|
},
|
|
|
|
delay,
|
|
|
|
);
|
2018-09-18 11:53:16 -07:00
|
|
|
let mut timers = state.timers.lock().unwrap();
|
|
|
|
timers.insert(timer_id, cancel_delay);
|
2018-09-05 22:13:36 -04:00
|
|
|
delay_task
|
|
|
|
};
|
2018-09-18 11:53:16 -07:00
|
|
|
let r = Box::new(future.then(move |result| {
|
2018-09-05 22:13:36 -04:00
|
|
|
let builder = &mut FlatBufferBuilder::new();
|
|
|
|
let msg = msg::TimerReady::create(
|
|
|
|
builder,
|
|
|
|
&msg::TimerReadyArgs {
|
|
|
|
id: timer_id,
|
|
|
|
canceled: result.is_err(),
|
|
|
|
..Default::default()
|
|
|
|
},
|
|
|
|
);
|
2018-09-09 19:48:10 -04:00
|
|
|
Ok(serialize_response(
|
2018-09-05 22:13:36 -04:00
|
|
|
cmd_id,
|
|
|
|
builder,
|
|
|
|
msg::BaseArgs {
|
|
|
|
msg: Some(msg.as_union_value()),
|
|
|
|
msg_type: msg::Any::TimerReady,
|
|
|
|
..Default::default()
|
|
|
|
},
|
|
|
|
))
|
2018-09-18 11:53:16 -07:00
|
|
|
}));
|
|
|
|
r
|
2018-08-09 12:17:08 -07:00
|
|
|
}
|
|
|
|
|
2018-09-17 17:41:13 -07:00
|
|
|
// Prototype: https://github.com/ry/isolate/blob/golang/timers.go#L40-L43
|
2018-09-27 17:33:10 -04:00
|
|
|
fn handle_timer_clear(
|
|
|
|
state: Arc<IsolateState>,
|
|
|
|
base: &msg::Base,
|
|
|
|
data: &'static mut [u8],
|
|
|
|
) -> Box<Op> {
|
|
|
|
assert_eq!(data.len(), 0);
|
2018-09-02 02:33:24 -04:00
|
|
|
let msg = base.msg_as_timer_clear().unwrap();
|
2018-08-09 12:17:08 -07:00
|
|
|
debug!("handle_timer_clear");
|
2018-09-18 11:53:16 -07:00
|
|
|
remove_timer(state, msg.id());
|
|
|
|
ok_future(empty_buf())
|
2018-08-09 12:17:08 -07:00
|
|
|
}
|
2018-09-03 17:22:30 -07:00
|
|
|
|
2018-09-27 17:33:10 -04:00
|
|
|
fn handle_rename(
|
|
|
|
state: Arc<IsolateState>,
|
|
|
|
base: &msg::Base,
|
|
|
|
data: &'static mut [u8],
|
|
|
|
) -> Box<Op> {
|
|
|
|
assert_eq!(data.len(), 0);
|
2018-09-18 11:53:16 -07:00
|
|
|
if !state.flags.allow_write {
|
2018-09-12 08:44:58 -07:00
|
|
|
return odd_future(permission_denied());
|
2018-09-18 21:38:24 -07:00
|
|
|
}
|
2018-09-12 08:44:58 -07:00
|
|
|
let msg = base.msg_as_rename().unwrap();
|
2018-09-22 04:42:07 -04:00
|
|
|
let oldpath = PathBuf::from(msg.oldpath().unwrap());
|
|
|
|
let newpath = PathBuf::from(msg.newpath().unwrap());
|
|
|
|
blocking!(base.sync(), || -> OpResult {
|
|
|
|
debug!("handle_rename {} {}", oldpath.display(), newpath.display());
|
|
|
|
fs::rename(&oldpath, &newpath)?;
|
2018-09-18 11:53:16 -07:00
|
|
|
Ok(empty_buf())
|
2018-09-22 04:42:07 -04:00
|
|
|
})
|
2018-09-03 17:22:30 -07:00
|
|
|
}
|
2018-09-18 21:38:24 -07:00
|
|
|
|
2018-09-27 17:33:10 -04:00
|
|
|
fn handle_symlink(
|
|
|
|
state: Arc<IsolateState>,
|
|
|
|
base: &msg::Base,
|
|
|
|
data: &'static mut [u8],
|
|
|
|
) -> Box<Op> {
|
|
|
|
assert_eq!(data.len(), 0);
|
2018-09-18 11:53:16 -07:00
|
|
|
if !state.flags.allow_write {
|
2018-09-18 21:38:24 -07:00
|
|
|
return odd_future(permission_denied());
|
|
|
|
}
|
|
|
|
// TODO Use type for Windows.
|
|
|
|
if cfg!(windows) {
|
|
|
|
return odd_future(not_implemented());
|
|
|
|
}
|
2018-09-22 04:42:07 -04:00
|
|
|
|
|
|
|
let msg = base.msg_as_symlink().unwrap();
|
|
|
|
let oldname = PathBuf::from(msg.oldname().unwrap());
|
|
|
|
let newname = PathBuf::from(msg.newname().unwrap());
|
|
|
|
blocking!(base.sync(), || -> OpResult {
|
|
|
|
debug!("handle_symlink {} {}", oldname.display(), newname.display());
|
|
|
|
#[cfg(any(unix))]
|
|
|
|
std::os::unix::fs::symlink(&oldname, &newname)?;
|
|
|
|
Ok(empty_buf())
|
|
|
|
})
|
2018-09-18 21:38:24 -07:00
|
|
|
}
|
2018-09-24 21:20:49 -07:00
|
|
|
|
2018-09-27 17:33:10 -04:00
|
|
|
fn handle_read_link(
|
|
|
|
_state: Arc<IsolateState>,
|
|
|
|
base: &msg::Base,
|
|
|
|
data: &'static mut [u8],
|
|
|
|
) -> Box<Op> {
|
|
|
|
assert_eq!(data.len(), 0);
|
2018-09-24 21:20:49 -07:00
|
|
|
let msg = base.msg_as_readlink().unwrap();
|
|
|
|
let cmd_id = base.cmd_id();
|
2018-09-22 04:42:07 -04:00
|
|
|
let name = PathBuf::from(msg.name().unwrap());
|
|
|
|
|
|
|
|
blocking!(base.sync(), || -> OpResult {
|
|
|
|
debug!("handle_read_link {}", name.display());
|
|
|
|
let path = fs::read_link(&name)?;
|
2018-09-24 21:20:49 -07:00
|
|
|
let builder = &mut FlatBufferBuilder::new();
|
|
|
|
let path_off = builder.create_string(path.to_str().unwrap());
|
|
|
|
let msg = msg::ReadlinkRes::create(
|
|
|
|
builder,
|
|
|
|
&msg::ReadlinkResArgs {
|
|
|
|
path: Some(path_off),
|
|
|
|
..Default::default()
|
|
|
|
},
|
|
|
|
);
|
|
|
|
Ok(serialize_response(
|
|
|
|
cmd_id,
|
|
|
|
builder,
|
|
|
|
msg::BaseArgs {
|
|
|
|
msg: Some(msg.as_union_value()),
|
|
|
|
msg_type: msg::Any::ReadlinkRes,
|
|
|
|
..Default::default()
|
|
|
|
},
|
|
|
|
))
|
2018-09-22 04:42:07 -04:00
|
|
|
})
|
2018-09-24 21:20:49 -07:00
|
|
|
}
|
2018-10-01 03:06:20 +08:00
|
|
|
|
|
|
|
fn handle_truncate(
|
|
|
|
state: Arc<IsolateState>,
|
|
|
|
base: &msg::Base,
|
|
|
|
data: &'static mut [u8],
|
|
|
|
) -> Box<Op> {
|
|
|
|
assert_eq!(data.len(), 0);
|
|
|
|
|
|
|
|
if !state.flags.allow_write {
|
|
|
|
return odd_future(permission_denied());
|
|
|
|
}
|
|
|
|
|
|
|
|
let msg = base.msg_as_truncate().unwrap();
|
|
|
|
let filename = String::from(msg.name().unwrap());
|
|
|
|
let len = msg.len();
|
|
|
|
blocking!(base.sync(), || {
|
|
|
|
debug!("handle_truncate {} {}", filename, len);
|
|
|
|
let f = fs::OpenOptions::new().write(true).open(&filename)?;
|
|
|
|
f.set_len(len as u64)?;
|
|
|
|
Ok(empty_buf())
|
|
|
|
})
|
|
|
|
}
|