2018-07-23 18:46:30 +00:00
|
|
|
// Copyright 2018 the Deno authors. All rights reserved. MIT license.
|
2018-09-06 02:13:36 +00:00
|
|
|
use errors::DenoError;
|
2018-08-16 03:36:48 +00:00
|
|
|
use errors::DenoResult;
|
|
|
|
use flatbuffers::FlatBufferBuilder;
|
2018-07-26 21:54:22 +00:00
|
|
|
use from_c;
|
2018-08-26 09:51:25 +00:00
|
|
|
use fs as deno_fs;
|
2018-08-09 19:17:08 +00:00
|
|
|
use futures;
|
|
|
|
use futures::sync::oneshot;
|
2018-08-16 00:57:36 +00:00
|
|
|
use hyper;
|
|
|
|
use hyper::rt::{Future, Stream};
|
|
|
|
use hyper::Client;
|
2018-09-04 22:56:12 +00:00
|
|
|
use libdeno;
|
|
|
|
use libdeno::{deno_buf, DenoC};
|
2018-09-09 23:21:22 +00:00
|
|
|
use msg;
|
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-08-26 09:51:25 +00:00
|
|
|
use std::fs;
|
2018-09-12 14:28:48 +00:00
|
|
|
#[cfg(any(unix))]
|
|
|
|
use std::os::unix::fs::PermissionsExt;
|
2018-08-09 17:24:30 +00:00
|
|
|
use std::path::Path;
|
2018-08-30 17:54:56 +00:00
|
|
|
use std::time::UNIX_EPOCH;
|
2018-08-16 03:36:48 +00:00
|
|
|
use std::time::{Duration, Instant};
|
2018-09-06 02:13:36 +00:00
|
|
|
use tokio::timer::Delay;
|
2018-08-16 03:36:48 +00:00
|
|
|
|
2018-09-06 02:13:36 +00:00
|
|
|
// Buf represents a byte array returned from a "Op".
|
|
|
|
// The message might be empty (which will be translated into a null object on
|
|
|
|
// the javascript side) or it is a heap allocated opaque sequence of bytes.
|
|
|
|
// Usually a flatbuffer message.
|
|
|
|
type Buf = Option<Box<[u8]>>;
|
|
|
|
|
|
|
|
// JS promises in Deno map onto a specific Future
|
|
|
|
// which yields either a DenoError or a byte array.
|
|
|
|
type Op = Future<Item = Buf, Error = DenoError>;
|
|
|
|
|
|
|
|
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.
|
|
|
|
type Handler = fn(d: *const DenoC, base: &msg::Base) -> Box<Op>;
|
2018-07-06 15:27:36 +00:00
|
|
|
|
2018-08-13 23:55:10 +00:00
|
|
|
pub extern "C" fn msg_from_js(d: *const DenoC, buf: deno_buf) {
|
|
|
|
let bytes = unsafe { std::slice::from_raw_parts(buf.data_ptr, buf.data_len) };
|
2018-08-17 19:11:28 +00:00
|
|
|
let base = msg::get_root_as_base(bytes);
|
2018-08-13 23:55:10 +00:00
|
|
|
let msg_type = base.msg_type();
|
2018-09-06 02:13:36 +00:00
|
|
|
let cmd_id = base.cmd_id();
|
2018-09-02 06:33:24 +00:00
|
|
|
let handler: Handler = match msg_type {
|
|
|
|
msg::Any::Start => handle_start,
|
|
|
|
msg::Any::CodeFetch => handle_code_fetch,
|
|
|
|
msg::Any::CodeCache => handle_code_cache,
|
|
|
|
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 09:48:36 +00:00
|
|
|
msg::Any::Mkdir => handle_mkdir,
|
2018-09-11 03:40:03 +00:00
|
|
|
msg::Any::Remove => handle_remove,
|
2018-09-10 00:25:43 +00:00
|
|
|
msg::Any::ReadFile => handle_read_file,
|
2018-09-04 00:22:30 +00:00
|
|
|
msg::Any::RenameSync => handle_rename_sync,
|
2018-09-02 06:33:24 +00:00
|
|
|
msg::Any::SetEnv => handle_set_env,
|
2018-09-11 19:38:53 +00:00
|
|
|
msg::Any::Stat => handle_stat,
|
2018-09-11 16:00:57 +00:00
|
|
|
msg::Any::WriteFile => handle_write_file,
|
2018-09-02 06:33:24 +00:00
|
|
|
msg::Any::Exit => handle_exit,
|
2018-08-16 03:36:48 +00:00
|
|
|
_ => panic!(format!(
|
|
|
|
"Unhandled message {}",
|
|
|
|
msg::enum_name_any(msg_type)
|
|
|
|
)),
|
|
|
|
};
|
|
|
|
|
2018-09-06 02:13:36 +00:00
|
|
|
let future = handler(d, &base);
|
|
|
|
let future = future.or_else(move |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));
|
2018-09-09 23:48:10 +00:00
|
|
|
Ok(serialize_response(
|
2018-09-06 02:13:36 +00:00
|
|
|
cmd_id,
|
|
|
|
builder,
|
|
|
|
msg::BaseArgs {
|
|
|
|
error: Some(errmsg_offset),
|
|
|
|
error_kind: err.kind(),
|
|
|
|
..Default::default()
|
|
|
|
},
|
|
|
|
))
|
|
|
|
});
|
|
|
|
|
|
|
|
let deno = from_c(d);
|
|
|
|
if base.sync() {
|
|
|
|
// Execute future synchronously.
|
|
|
|
// println!("sync handler {}", msg::enum_name_any(msg_type));
|
|
|
|
let maybe_box_u8 = future.wait().unwrap();
|
|
|
|
match maybe_box_u8 {
|
|
|
|
None => {}
|
|
|
|
Some(box_u8) => {
|
|
|
|
let buf = deno_buf_from(box_u8);
|
|
|
|
// Set the synchronous response, the value returned from deno.send().
|
|
|
|
unsafe { libdeno::deno_set_response(d, buf) }
|
|
|
|
}
|
2018-08-13 23:55:10 +00:00
|
|
|
}
|
2018-09-06 02:13:36 +00:00
|
|
|
} else {
|
|
|
|
// Execute future asynchornously.
|
|
|
|
let future = future.and_then(move |maybe_box_u8| {
|
|
|
|
let buf = match maybe_box_u8 {
|
|
|
|
Some(box_u8) => deno_buf_from(box_u8),
|
2018-09-10 18:12:52 +00:00
|
|
|
None => {
|
|
|
|
// async RPCs that return None still need to
|
|
|
|
// send a message back to signal completion.
|
|
|
|
let builder = &mut FlatBufferBuilder::new();
|
|
|
|
deno_buf_from(
|
|
|
|
serialize_response(
|
|
|
|
cmd_id,
|
|
|
|
builder,
|
|
|
|
msg::BaseArgs {
|
|
|
|
..Default::default()
|
|
|
|
},
|
|
|
|
).unwrap(),
|
|
|
|
)
|
|
|
|
}
|
2018-09-06 02:13:36 +00:00
|
|
|
};
|
|
|
|
// TODO(ry) make this thread safe.
|
|
|
|
unsafe { libdeno::deno_send(d, buf) };
|
|
|
|
Ok(())
|
|
|
|
});
|
|
|
|
deno.rt.spawn(future);
|
|
|
|
}
|
|
|
|
}
|
2018-08-16 03:36:48 +00:00
|
|
|
|
2018-09-06 02:13:36 +00:00
|
|
|
fn deno_buf_from(x: Box<[u8]>) -> deno_buf {
|
|
|
|
let len = x.len();
|
|
|
|
let ptr = Box::into_raw(x);
|
|
|
|
deno_buf {
|
|
|
|
alloc_ptr: 0 as *mut u8,
|
|
|
|
alloc_len: 0,
|
|
|
|
data_ptr: ptr as *mut u8,
|
|
|
|
data_len: len,
|
2018-08-13 23:55:10 +00:00
|
|
|
}
|
2018-07-13 07:24:07 +00:00
|
|
|
}
|
|
|
|
|
2018-09-06 02:13:36 +00:00
|
|
|
fn permission_denied() -> DenoError {
|
|
|
|
DenoError::from(std::io::Error::new(
|
|
|
|
std::io::ErrorKind::PermissionDenied,
|
|
|
|
"permission denied",
|
|
|
|
))
|
|
|
|
}
|
|
|
|
|
|
|
|
fn handle_exit(_d: *const DenoC, base: &msg::Base) -> Box<Op> {
|
2018-09-02 06:33:24 +00:00
|
|
|
let msg = base.msg_as_exit().unwrap();
|
|
|
|
std::process::exit(msg.code())
|
|
|
|
}
|
|
|
|
|
2018-09-06 02:13:36 +00:00
|
|
|
fn handle_start(d: *const DenoC, base: &msg::Base) -> Box<Op> {
|
2018-08-16 03:36:48 +00:00
|
|
|
let deno = from_c(d);
|
2018-09-06 02:13:36 +00:00
|
|
|
let mut builder = FlatBufferBuilder::new();
|
2018-08-13 23:55:10 +00:00
|
|
|
|
|
|
|
let argv = deno.argv.iter().map(|s| s.as_str()).collect::<Vec<_>>();
|
|
|
|
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
|
|
|
|
2018-08-17 19:11:28 +00:00
|
|
|
let msg = 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),
|
|
|
|
argv: Some(argv_off),
|
2018-08-17 20:34:30 +00:00
|
|
|
debug_flag: deno.flags.log_debug,
|
2018-08-13 23:55:10 +00:00
|
|
|
..Default::default()
|
|
|
|
},
|
|
|
|
);
|
|
|
|
|
2018-09-09 23:48:10 +00:00
|
|
|
ok_future(serialize_response(
|
2018-09-06 02:13:36 +00:00
|
|
|
base.cmd_id(),
|
|
|
|
&mut builder,
|
|
|
|
msg::BaseArgs {
|
2018-08-13 23:55:10 +00:00
|
|
|
msg_type: msg::Any::StartRes,
|
2018-09-02 06:33:24 +00:00
|
|
|
msg: Some(msg.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
|
|
|
}
|
|
|
|
|
2018-09-09 23:48:10 +00:00
|
|
|
fn serialize_response(
|
2018-09-06 02:13:36 +00:00
|
|
|
cmd_id: u32,
|
2018-08-16 03:36:48 +00:00
|
|
|
builder: &mut FlatBufferBuilder,
|
2018-09-06 02:13:36 +00:00
|
|
|
mut args: msg::BaseArgs,
|
|
|
|
) -> Buf {
|
|
|
|
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-09-06 02:13:36 +00:00
|
|
|
let vec = data.to_vec();
|
|
|
|
Some(vec.into_boxed_slice())
|
2018-08-09 19:17:08 +00:00
|
|
|
}
|
|
|
|
|
2018-09-06 02:13:36 +00: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 19:17:08 +00:00
|
|
|
}
|
|
|
|
|
2018-08-02 23:13:02 +00:00
|
|
|
// https://github.com/denoland/deno/blob/golang/os.go#L100-L154
|
2018-09-06 02:13:36 +00:00
|
|
|
fn handle_code_fetch(d: *const DenoC, base: &msg::Base) -> Box<Op> {
|
2018-09-02 06:33:24 +00:00
|
|
|
let msg = base.msg_as_code_fetch().unwrap();
|
2018-09-06 02:13:36 +00:00
|
|
|
let cmd_id = base.cmd_id();
|
2018-09-02 06:33:24 +00:00
|
|
|
let module_specifier = msg.module_specifier().unwrap();
|
|
|
|
let containing_file = msg.containing_file().unwrap();
|
2018-07-26 21:54:22 +00:00
|
|
|
let deno = from_c(d);
|
2018-07-20 01:14:32 +00:00
|
|
|
|
2018-09-06 02:13:36 +00:00
|
|
|
assert_eq!(deno.dir.root.join("gen"), deno.dir.gen, "Sanity check");
|
2018-07-06 15:27:36 +00:00
|
|
|
|
2018-09-06 02:13:36 +00:00
|
|
|
Box::new(futures::future::result(|| -> OpResult {
|
|
|
|
let builder = &mut FlatBufferBuilder::new();
|
|
|
|
let out = deno.dir.code_fetch(module_specifier, containing_file)?;
|
|
|
|
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-16 03:36:48 +00:00
|
|
|
..Default::default()
|
2018-09-06 02:13:36 +00: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 23:48:10 +00:00
|
|
|
Ok(serialize_response(
|
2018-09-06 02:13:36 +00:00
|
|
|
cmd_id,
|
|
|
|
builder,
|
|
|
|
msg::BaseArgs {
|
|
|
|
msg: Some(msg.as_union_value()),
|
|
|
|
msg_type: msg::Any::CodeFetchRes,
|
|
|
|
..Default::default()
|
|
|
|
},
|
|
|
|
))
|
|
|
|
}()))
|
2018-07-13 07:24:07 +00:00
|
|
|
}
|
|
|
|
|
2018-08-02 23:13:02 +00:00
|
|
|
// https://github.com/denoland/deno/blob/golang/os.go#L156-L169
|
2018-09-06 02:13:36 +00:00
|
|
|
fn handle_code_cache(d: *const DenoC, base: &msg::Base) -> Box<Op> {
|
2018-09-02 06:33:24 +00: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-06 02:13:36 +00:00
|
|
|
Box::new(futures::future::result(|| -> OpResult {
|
|
|
|
let deno = from_c(d);
|
|
|
|
deno.dir.code_cache(filename, source_code, output_code)?;
|
|
|
|
Ok(None)
|
|
|
|
}()))
|
2018-07-06 15:27:36 +00:00
|
|
|
}
|
2018-08-09 19:17:08 +00:00
|
|
|
|
2018-09-06 02:13:36 +00:00
|
|
|
fn handle_set_env(d: *const DenoC, base: &msg::Base) -> Box<Op> {
|
2018-09-02 06:33:24 +00:00
|
|
|
let msg = base.msg_as_set_env().unwrap();
|
|
|
|
let key = msg.key().unwrap();
|
|
|
|
let value = msg.value().unwrap();
|
|
|
|
|
2018-08-31 11:51:12 +00:00
|
|
|
let deno = from_c(d);
|
|
|
|
if !deno.flags.allow_env {
|
2018-09-06 02:13:36 +00:00
|
|
|
return odd_future(permission_denied());
|
2018-08-31 11:51:12 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
std::env::set_var(key, value);
|
2018-09-06 02:13:36 +00:00
|
|
|
ok_future(None)
|
2018-08-31 11:51:12 +00:00
|
|
|
}
|
|
|
|
|
2018-09-06 02:13:36 +00:00
|
|
|
fn handle_env(d: *const DenoC, base: &msg::Base) -> Box<Op> {
|
2018-08-31 11:51:12 +00:00
|
|
|
let deno = from_c(d);
|
2018-09-06 02:13:36 +00:00
|
|
|
let cmd_id = base.cmd_id();
|
2018-08-31 11:51:12 +00:00
|
|
|
if !deno.flags.allow_env {
|
2018-09-06 02:13:36 +00:00
|
|
|
return odd_future(permission_denied());
|
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()
|
|
|
|
.map(|(key, value)| {
|
|
|
|
let key = builder.create_string(&key);
|
|
|
|
let value = builder.create_string(&value);
|
2018-08-31 11:51:12 +00:00
|
|
|
|
2018-09-02 05:59:16 +00:00
|
|
|
msg::EnvPair::create(
|
|
|
|
builder,
|
|
|
|
&msg::EnvPairArgs {
|
|
|
|
key: Some(key),
|
|
|
|
value: Some(value),
|
|
|
|
..Default::default()
|
|
|
|
},
|
|
|
|
)
|
2018-08-31 11:51:12 +00:00
|
|
|
})
|
2018-09-02 05:59:16 +00:00
|
|
|
.collect();
|
2018-09-02 05:58:23 +00:00
|
|
|
let tables = builder.create_vector(&vars);
|
2018-08-31 11:51:12 +00:00
|
|
|
let msg = msg::EnvironRes::create(
|
|
|
|
builder,
|
|
|
|
&msg::EnvironResArgs {
|
|
|
|
map: Some(tables),
|
|
|
|
..Default::default()
|
|
|
|
},
|
|
|
|
);
|
2018-09-09 23:48:10 +00:00
|
|
|
ok_future(serialize_response(
|
2018-09-06 02:13:36 +00:00
|
|
|
cmd_id,
|
2018-08-31 11:51:12 +00:00
|
|
|
builder,
|
2018-09-06 02:13:36 +00:00
|
|
|
msg::BaseArgs {
|
2018-09-02 05:58:23 +00:00
|
|
|
msg: Some(msg.as_union_value()),
|
2018-08-31 11:51:12 +00:00
|
|
|
msg_type: msg::Any::EnvironRes,
|
|
|
|
..Default::default()
|
|
|
|
},
|
|
|
|
))
|
|
|
|
}
|
|
|
|
|
2018-09-06 02:13:36 +00:00
|
|
|
fn handle_fetch_req(d: *const DenoC, base: &msg::Base) -> Box<Op> {
|
2018-09-02 06:33:24 +00:00
|
|
|
let msg = base.msg_as_fetch_req().unwrap();
|
2018-09-06 02:13:36 +00:00
|
|
|
let cmd_id = base.cmd_id();
|
2018-09-02 06:33:24 +00:00
|
|
|
let id = msg.id();
|
|
|
|
let url = msg.url().unwrap();
|
2018-09-06 02:13:36 +00:00
|
|
|
let deno = from_c(d);
|
|
|
|
|
|
|
|
if !deno.flags.allow_net {
|
|
|
|
return odd_future(permission_denied());
|
|
|
|
}
|
|
|
|
|
2018-08-16 00:57:36 +00:00
|
|
|
let url = url.parse::<hyper::Uri>().unwrap();
|
|
|
|
let client = Client::new();
|
|
|
|
|
2018-09-06 02:13:36 +00:00
|
|
|
let future = client.get(url).and_then(|res| {
|
|
|
|
let status = res.status().as_u16() as i32;
|
|
|
|
// TODO Handle streaming body.
|
|
|
|
res.into_body().concat2().map(move |body| (status, body))
|
|
|
|
});
|
|
|
|
|
|
|
|
let future = future.map_err(|err| -> DenoError { err.into() }).and_then(
|
|
|
|
move |(status, body)| {
|
|
|
|
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());
|
|
|
|
let msg = msg::FetchRes::create(
|
|
|
|
builder,
|
|
|
|
&msg::FetchResArgs {
|
|
|
|
id,
|
|
|
|
status,
|
|
|
|
body: Some(body_off),
|
|
|
|
..Default::default()
|
|
|
|
},
|
|
|
|
);
|
2018-09-09 23:48:10 +00:00
|
|
|
Ok(serialize_response(
|
2018-09-06 02:13:36 +00:00
|
|
|
cmd_id,
|
|
|
|
builder,
|
|
|
|
msg::BaseArgs {
|
|
|
|
msg: Some(msg.as_union_value()),
|
|
|
|
msg_type: msg::Any::FetchRes,
|
|
|
|
..Default::default()
|
|
|
|
},
|
|
|
|
))
|
|
|
|
},
|
2018-08-16 00:57:36 +00:00
|
|
|
);
|
2018-09-06 02:13:36 +00:00
|
|
|
Box::new(future)
|
2018-08-16 00:57:36 +00:00
|
|
|
}
|
|
|
|
|
2018-08-09 19:17:08 +00: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(())
|
|
|
|
})
|
|
|
|
.select(cancel_rx)
|
|
|
|
.map(|_| ())
|
|
|
|
.map_err(|_| ());
|
|
|
|
|
|
|
|
(delay_task, cancel_tx)
|
|
|
|
}
|
|
|
|
|
2018-09-06 02:13:36 +00:00
|
|
|
fn handle_make_temp_dir(d: *const DenoC, base: &msg::Base) -> Box<Op> {
|
|
|
|
let base = Box::new(*base);
|
2018-09-02 06:33:24 +00:00
|
|
|
let msg = base.msg_as_make_temp_dir().unwrap();
|
2018-09-06 02:13:36 +00:00
|
|
|
let cmd_id = base.cmd_id();
|
2018-09-02 06:33:24 +00:00
|
|
|
let dir = msg.dir();
|
|
|
|
let prefix = msg.prefix();
|
|
|
|
let suffix = msg.suffix();
|
2018-09-06 02:13:36 +00:00
|
|
|
|
2018-08-23 22:36:45 +00:00
|
|
|
let deno = from_c(d);
|
|
|
|
if !deno.flags.allow_write {
|
2018-09-06 02:13:36 +00:00
|
|
|
return Box::new(futures::future::err(permission_denied()));
|
2018-08-23 22:36:45 +00:00
|
|
|
}
|
2018-09-06 02:13:36 +00:00
|
|
|
// TODO Use blocking() here.
|
|
|
|
Box::new(futures::future::result(|| -> OpResult {
|
|
|
|
// TODO(piscisaureus): use byte vector for paths, not a string.
|
|
|
|
// See https://github.com/denoland/deno/issues/627.
|
|
|
|
// We can't assume that paths are always valid utf8 strings.
|
|
|
|
let path = deno_fs::make_temp_dir(dir.map(Path::new), prefix, suffix)?;
|
|
|
|
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 23:48:10 +00:00
|
|
|
Ok(serialize_response(
|
2018-09-06 02:13:36 +00:00
|
|
|
cmd_id,
|
|
|
|
builder,
|
|
|
|
msg::BaseArgs {
|
|
|
|
msg: Some(msg.as_union_value()),
|
|
|
|
msg_type: msg::Any::MakeTempDirRes,
|
|
|
|
..Default::default()
|
|
|
|
},
|
|
|
|
))
|
|
|
|
}()))
|
2018-08-23 22:36:45 +00:00
|
|
|
}
|
|
|
|
|
2018-09-10 09:48:36 +00:00
|
|
|
fn handle_mkdir(d: *const DenoC, base: &msg::Base) -> Box<Op> {
|
|
|
|
let msg = base.msg_as_mkdir().unwrap();
|
2018-08-26 06:26:30 +00:00
|
|
|
// TODO let mode = msg.mode();
|
2018-09-10 09:48:36 +00:00
|
|
|
let path = msg.path().unwrap();
|
2018-08-26 06:26:30 +00:00
|
|
|
let deno = from_c(d);
|
2018-09-06 02:13:36 +00:00
|
|
|
if !deno.flags.allow_write {
|
|
|
|
return odd_future(permission_denied());
|
|
|
|
}
|
2018-09-10 09:48:36 +00:00
|
|
|
// TODO Use tokio_threadpool.
|
2018-09-06 02:13:36 +00:00
|
|
|
Box::new(futures::future::result(|| -> OpResult {
|
2018-09-10 09:48:36 +00:00
|
|
|
debug!("handle_mkdir {}", path);
|
2018-08-26 06:26:30 +00:00
|
|
|
// TODO(ry) Use mode.
|
|
|
|
deno_fs::mkdir(Path::new(path))?;
|
2018-09-06 02:13:36 +00:00
|
|
|
Ok(None)
|
|
|
|
}()))
|
2018-08-26 06:26:30 +00:00
|
|
|
}
|
|
|
|
|
2018-09-11 03:40:03 +00:00
|
|
|
fn handle_remove(d: *const DenoC, base: &msg::Base) -> Box<Op> {
|
|
|
|
let msg = base.msg_as_remove().unwrap();
|
|
|
|
let path = msg.path().unwrap();
|
|
|
|
let recursive = msg.recursive();
|
|
|
|
let deno = from_c(d);
|
|
|
|
if !deno.flags.allow_write {
|
|
|
|
return odd_future(permission_denied());
|
|
|
|
}
|
|
|
|
// TODO Use tokio_threadpool.
|
|
|
|
Box::new(futures::future::result(|| -> OpResult {
|
|
|
|
debug!("handle_remove {}", path);
|
|
|
|
let path_ = Path::new(&path);
|
|
|
|
let metadata = fs::metadata(&path_)?;
|
|
|
|
if metadata.is_file() {
|
|
|
|
fs::remove_file(&path_)?;
|
|
|
|
} else {
|
|
|
|
if recursive {
|
|
|
|
remove_dir_all(&path_)?;
|
|
|
|
} else {
|
|
|
|
fs::remove_dir(&path_)?;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
Ok(None)
|
|
|
|
}()))
|
|
|
|
}
|
|
|
|
|
2018-08-09 17:24:30 +00:00
|
|
|
// Prototype https://github.com/denoland/deno/blob/golang/os.go#L171-L184
|
2018-09-10 00:25:43 +00:00
|
|
|
fn handle_read_file(_d: *const DenoC, base: &msg::Base) -> Box<Op> {
|
|
|
|
let msg = base.msg_as_read_file().unwrap();
|
2018-09-06 02:13:36 +00:00
|
|
|
let cmd_id = base.cmd_id();
|
|
|
|
let filename = String::from(msg.filename().unwrap());
|
|
|
|
Box::new(futures::future::result(|| -> OpResult {
|
2018-09-10 00:25:43 +00:00
|
|
|
debug!("handle_read_file {}", filename);
|
2018-09-06 02:13:36 +00:00
|
|
|
let vec = fs::read(Path::new(&filename))?;
|
|
|
|
// 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-10 00:25:43 +00:00
|
|
|
let msg = msg::ReadFileRes::create(
|
2018-09-06 02:13:36 +00:00
|
|
|
builder,
|
2018-09-10 00:25:43 +00:00
|
|
|
&msg::ReadFileResArgs {
|
2018-09-06 02:13:36 +00:00
|
|
|
data: Some(data_off),
|
|
|
|
..Default::default()
|
|
|
|
},
|
|
|
|
);
|
2018-09-09 23:48:10 +00:00
|
|
|
Ok(serialize_response(
|
2018-09-06 02:13:36 +00:00
|
|
|
cmd_id,
|
|
|
|
builder,
|
|
|
|
msg::BaseArgs {
|
|
|
|
msg: Some(msg.as_union_value()),
|
2018-09-10 00:25:43 +00:00
|
|
|
msg_type: msg::Any::ReadFileRes,
|
2018-09-06 02:13:36 +00:00
|
|
|
..Default::default()
|
|
|
|
},
|
|
|
|
))
|
|
|
|
}()))
|
2018-08-09 17:24:30 +00:00
|
|
|
}
|
|
|
|
|
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))]
|
|
|
|
fn get_mode(perm: fs::Permissions) -> i32 {
|
|
|
|
(perm.mode() as i32)
|
|
|
|
}
|
|
|
|
|
|
|
|
#[cfg(not(any(unix)))]
|
|
|
|
fn get_mode(_perm: fs::Permissions) -> i32 {
|
|
|
|
-1
|
|
|
|
}
|
|
|
|
|
2018-09-11 19:38:53 +00:00
|
|
|
fn handle_stat(_d: *const DenoC, base: &msg::Base) -> Box<Op> {
|
|
|
|
let msg = base.msg_as_stat().unwrap();
|
2018-09-06 02:13:36 +00:00
|
|
|
let cmd_id = base.cmd_id();
|
|
|
|
let filename = String::from(msg.filename().unwrap());
|
2018-09-02 06:33:24 +00:00
|
|
|
let lstat = msg.lstat();
|
|
|
|
|
2018-09-06 02:13:36 +00:00
|
|
|
Box::new(futures::future::result(|| -> OpResult {
|
|
|
|
let builder = &mut FlatBufferBuilder::new();
|
2018-09-11 19:38:53 +00:00
|
|
|
debug!("handle_stat {} {}", filename, lstat);
|
2018-09-06 02:13:36 +00:00
|
|
|
let path = Path::new(&filename);
|
|
|
|
let metadata = if lstat {
|
|
|
|
fs::symlink_metadata(path)?
|
|
|
|
} else {
|
|
|
|
fs::metadata(path)?
|
|
|
|
};
|
|
|
|
|
2018-09-11 19:38:53 +00:00
|
|
|
let msg = 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-09-12 14:28:48 +00:00
|
|
|
mode: get_mode(metadata.permissions()),
|
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(
|
2018-09-06 02:13:36 +00:00
|
|
|
cmd_id,
|
|
|
|
builder,
|
|
|
|
msg::BaseArgs {
|
|
|
|
msg: Some(msg.as_union_value()),
|
2018-09-11 19:38:53 +00:00
|
|
|
msg_type: msg::Any::StatRes,
|
2018-09-06 02:13:36 +00:00
|
|
|
..Default::default()
|
|
|
|
},
|
|
|
|
))
|
|
|
|
}()))
|
2018-08-29 13:22:25 +00:00
|
|
|
}
|
|
|
|
|
2018-09-11 16:00:57 +00:00
|
|
|
fn handle_write_file(d: *const DenoC, base: &msg::Base) -> Box<Op> {
|
|
|
|
let msg = base.msg_as_write_file().unwrap();
|
2018-09-06 02:13:36 +00:00
|
|
|
let filename = String::from(msg.filename().unwrap());
|
2018-09-02 06:33:24 +00:00
|
|
|
let data = msg.data().unwrap();
|
2018-09-11 16:00:57 +00:00
|
|
|
let perm = msg.perm();
|
2018-08-22 17:19:32 +00:00
|
|
|
let deno = from_c(d);
|
2018-09-02 06:33:24 +00:00
|
|
|
|
2018-09-11 16:00:57 +00:00
|
|
|
debug!("handle_write_file {}", filename);
|
2018-09-06 02:13:36 +00:00
|
|
|
Box::new(futures::future::result(|| -> OpResult {
|
|
|
|
if !deno.flags.allow_write {
|
|
|
|
Err(permission_denied())
|
|
|
|
} else {
|
2018-09-11 16:00:57 +00:00
|
|
|
deno_fs::write_file(Path::new(&filename), data, perm)?;
|
2018-09-06 02:13:36 +00:00
|
|
|
Ok(None)
|
|
|
|
}
|
|
|
|
}()))
|
2018-08-22 17:19:32 +00:00
|
|
|
}
|
|
|
|
|
2018-08-09 19:17:08 +00:00
|
|
|
// TODO(ry) Use Deno instead of DenoC as first arg.
|
|
|
|
fn remove_timer(d: *const DenoC, timer_id: u32) {
|
|
|
|
let deno = from_c(d);
|
2018-09-06 02:13:36 +00:00
|
|
|
assert!(deno.timers.contains_key(&timer_id));
|
2018-08-09 19:17:08 +00:00
|
|
|
deno.timers.remove(&timer_id);
|
2018-09-06 02:13:36 +00:00
|
|
|
assert!(!deno.timers.contains_key(&timer_id));
|
2018-08-09 19:17:08 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// Prototype: https://github.com/ry/deno/blob/golang/timers.go#L25-L39
|
2018-09-06 02:13:36 +00:00
|
|
|
fn handle_timer_start(d: *const DenoC, base: &msg::Base) -> Box<Op> {
|
2018-08-09 19:17:08 +00:00
|
|
|
debug!("handle_timer_start");
|
2018-09-02 06:33:24 +00:00
|
|
|
let msg = base.msg_as_timer_start().unwrap();
|
2018-09-06 02:13:36 +00:00
|
|
|
let cmd_id = base.cmd_id();
|
2018-09-02 06:33:24 +00:00
|
|
|
let timer_id = msg.id();
|
|
|
|
let delay = msg.delay();
|
2018-08-09 19:17:08 +00:00
|
|
|
let deno = from_c(d);
|
|
|
|
|
2018-09-06 02:13:36 +00:00
|
|
|
let future = {
|
2018-08-09 19:17:08 +00:00
|
|
|
let (delay_task, cancel_delay) = set_timeout(
|
|
|
|
move || {
|
|
|
|
remove_timer(d, timer_id);
|
|
|
|
},
|
|
|
|
delay,
|
|
|
|
);
|
|
|
|
deno.timers.insert(timer_id, cancel_delay);
|
2018-09-06 02:13:36 +00:00
|
|
|
delay_task
|
|
|
|
};
|
|
|
|
Box::new(future.then(move |result| {
|
|
|
|
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 23:48:10 +00:00
|
|
|
Ok(serialize_response(
|
2018-09-06 02:13:36 +00:00
|
|
|
cmd_id,
|
|
|
|
builder,
|
|
|
|
msg::BaseArgs {
|
|
|
|
msg: Some(msg.as_union_value()),
|
|
|
|
msg_type: msg::Any::TimerReady,
|
|
|
|
..Default::default()
|
|
|
|
},
|
|
|
|
))
|
|
|
|
}))
|
2018-08-09 19:17:08 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// Prototype: https://github.com/ry/deno/blob/golang/timers.go#L40-L43
|
2018-09-06 02:13:36 +00:00
|
|
|
fn handle_timer_clear(d: *const DenoC, base: &msg::Base) -> Box<Op> {
|
2018-09-02 06:33:24 +00:00
|
|
|
let msg = base.msg_as_timer_clear().unwrap();
|
2018-08-09 19:17:08 +00:00
|
|
|
debug!("handle_timer_clear");
|
2018-09-02 06:33:24 +00:00
|
|
|
remove_timer(d, msg.id());
|
2018-09-06 02:13:36 +00:00
|
|
|
ok_future(None)
|
2018-08-09 19:17:08 +00:00
|
|
|
}
|
2018-09-04 00:22:30 +00:00
|
|
|
|
2018-09-06 02:13:36 +00:00
|
|
|
fn handle_rename_sync(d: *const DenoC, base: &msg::Base) -> Box<Op> {
|
2018-09-04 00:22:30 +00:00
|
|
|
let deno = from_c(d);
|
|
|
|
if !deno.flags.allow_write {
|
2018-09-06 02:13:36 +00:00
|
|
|
return Box::new(futures::future::err(permission_denied()));
|
|
|
|
};
|
|
|
|
let msg = base.msg_as_rename_sync().unwrap();
|
|
|
|
let oldpath = String::from(msg.oldpath().unwrap());
|
|
|
|
let newpath = String::from(msg.newpath().unwrap());
|
|
|
|
// TODO use blocking()
|
|
|
|
Box::new(futures::future::result(|| -> OpResult {
|
|
|
|
debug!("handle_rename {} {}", oldpath, newpath);
|
|
|
|
fs::rename(Path::new(&oldpath), Path::new(&newpath))?;
|
|
|
|
Ok(None)
|
|
|
|
}()))
|
2018-09-04 00:22:30 +00:00
|
|
|
}
|