2019-08-14 11:03:02 -04:00
|
|
|
// Copyright 2018-2019 the Deno authors. All rights reserved. MIT license.
|
2019-08-26 08:50:21 -04:00
|
|
|
use super::dispatch_json::{Deserialize, JsonOp, Value};
|
2019-10-28 20:42:44 -04:00
|
|
|
use crate::deno_error::bad_resource;
|
2019-11-07 11:11:15 -05:00
|
|
|
use crate::deno_error::DenoError;
|
|
|
|
use crate::deno_error::ErrorKind;
|
2019-08-14 11:03:02 -04:00
|
|
|
use crate::fs as deno_fs;
|
2019-10-11 14:41:54 -04:00
|
|
|
use crate::ops::json_op;
|
2019-11-13 23:14:48 -05:00
|
|
|
use crate::resources;
|
|
|
|
use crate::resources::CliResource;
|
2019-08-14 11:03:02 -04:00
|
|
|
use crate::state::ThreadSafeState;
|
|
|
|
use deno::*;
|
|
|
|
use futures::Future;
|
2019-11-07 11:11:15 -05:00
|
|
|
use futures::Poll;
|
2019-08-14 11:03:02 -04:00
|
|
|
use std;
|
|
|
|
use std::convert::From;
|
2019-11-07 11:11:15 -05:00
|
|
|
use std::io::SeekFrom;
|
2019-08-14 11:03:02 -04:00
|
|
|
use tokio;
|
|
|
|
|
2019-10-11 14:41:54 -04:00
|
|
|
pub fn init(i: &mut Isolate, s: &ThreadSafeState) {
|
|
|
|
i.register_op("open", s.core_op(json_op(s.stateful_op(op_open))));
|
|
|
|
i.register_op("close", s.core_op(json_op(s.stateful_op(op_close))));
|
|
|
|
i.register_op("seek", s.core_op(json_op(s.stateful_op(op_seek))));
|
|
|
|
}
|
|
|
|
|
2019-08-26 08:50:21 -04:00
|
|
|
#[derive(Deserialize)]
|
|
|
|
#[serde(rename_all = "camelCase")]
|
|
|
|
struct OpenArgs {
|
|
|
|
promise_id: Option<u64>,
|
|
|
|
filename: String,
|
|
|
|
mode: String,
|
|
|
|
}
|
|
|
|
|
2019-10-11 14:41:54 -04:00
|
|
|
fn op_open(
|
2019-08-14 11:03:02 -04:00
|
|
|
state: &ThreadSafeState,
|
2019-08-26 08:50:21 -04:00
|
|
|
args: Value,
|
|
|
|
_zero_copy: Option<PinnedBuf>,
|
|
|
|
) -> Result<JsonOp, ErrBox> {
|
|
|
|
let args: OpenArgs = serde_json::from_value(args)?;
|
|
|
|
let (filename, filename_) = deno_fs::resolve_from_cwd(&args.filename)?;
|
|
|
|
let mode = args.mode.as_ref();
|
2019-11-13 23:14:48 -05:00
|
|
|
|
2019-08-14 11:03:02 -04: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.");
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
match mode {
|
|
|
|
"r" => {
|
|
|
|
state.check_read(&filename_)?;
|
|
|
|
}
|
|
|
|
"w" | "a" | "x" => {
|
|
|
|
state.check_write(&filename_)?;
|
|
|
|
}
|
|
|
|
&_ => {
|
|
|
|
state.check_read(&filename_)?;
|
|
|
|
state.check_write(&filename_)?;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-08-26 08:50:21 -04:00
|
|
|
let is_sync = args.promise_id.is_none();
|
2019-08-14 11:03:02 -04:00
|
|
|
let op = open_options.open(filename).map_err(ErrBox::from).and_then(
|
|
|
|
move |fs_file| {
|
2019-11-13 23:14:48 -05:00
|
|
|
let rid = resources::add_fs_file(fs_file);
|
2019-11-07 11:11:15 -05:00
|
|
|
futures::future::ok(json!(rid))
|
2019-08-14 11:03:02 -04:00
|
|
|
},
|
|
|
|
);
|
2019-08-26 08:50:21 -04:00
|
|
|
|
|
|
|
if is_sync {
|
2019-08-14 11:03:02 -04:00
|
|
|
let buf = op.wait()?;
|
2019-08-26 08:50:21 -04:00
|
|
|
Ok(JsonOp::Sync(buf))
|
2019-08-14 11:03:02 -04:00
|
|
|
} else {
|
2019-08-26 08:50:21 -04:00
|
|
|
Ok(JsonOp::Async(Box::new(op)))
|
2019-08-14 11:03:02 -04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-08-26 08:50:21 -04:00
|
|
|
#[derive(Deserialize)]
|
|
|
|
struct CloseArgs {
|
|
|
|
rid: i32,
|
|
|
|
}
|
|
|
|
|
2019-10-11 14:41:54 -04:00
|
|
|
fn op_close(
|
2019-11-13 23:14:48 -05:00
|
|
|
_state: &ThreadSafeState,
|
2019-08-26 08:50:21 -04:00
|
|
|
args: Value,
|
|
|
|
_zero_copy: Option<PinnedBuf>,
|
|
|
|
) -> Result<JsonOp, ErrBox> {
|
|
|
|
let args: CloseArgs = serde_json::from_value(args)?;
|
|
|
|
|
2019-11-13 23:14:48 -05:00
|
|
|
let mut table = resources::lock_resource_table();
|
2019-10-28 20:42:44 -04:00
|
|
|
table.close(args.rid as u32).ok_or_else(bad_resource)?;
|
2019-09-10 00:59:40 -04:00
|
|
|
Ok(JsonOp::Sync(json!({})))
|
2019-08-14 11:03:02 -04:00
|
|
|
}
|
|
|
|
|
2019-11-13 23:14:48 -05:00
|
|
|
#[derive(Debug)]
|
2019-11-07 11:11:15 -05:00
|
|
|
pub struct SeekFuture {
|
|
|
|
seek_from: SeekFrom,
|
|
|
|
rid: ResourceId,
|
|
|
|
}
|
|
|
|
|
|
|
|
impl Future for SeekFuture {
|
|
|
|
type Item = u64;
|
|
|
|
type Error = ErrBox;
|
|
|
|
|
|
|
|
fn poll(&mut self) -> Poll<Self::Item, Self::Error> {
|
2019-11-13 23:14:48 -05:00
|
|
|
let mut table = resources::lock_resource_table();
|
2019-11-07 11:11:15 -05:00
|
|
|
let resource = table
|
2019-11-13 23:14:48 -05:00
|
|
|
.get_mut::<CliResource>(self.rid)
|
2019-11-07 11:11:15 -05:00
|
|
|
.ok_or_else(bad_resource)?;
|
|
|
|
|
|
|
|
let tokio_file = match resource {
|
2019-11-13 23:14:48 -05:00
|
|
|
CliResource::FsFile(ref mut file) => file,
|
2019-11-07 11:11:15 -05:00
|
|
|
_ => return Err(bad_resource()),
|
|
|
|
};
|
|
|
|
|
|
|
|
tokio_file.poll_seek(self.seek_from).map_err(ErrBox::from)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-08-26 08:50:21 -04:00
|
|
|
#[derive(Deserialize)]
|
|
|
|
#[serde(rename_all = "camelCase")]
|
|
|
|
struct SeekArgs {
|
|
|
|
promise_id: Option<u64>,
|
|
|
|
rid: i32,
|
|
|
|
offset: i32,
|
|
|
|
whence: i32,
|
|
|
|
}
|
|
|
|
|
2019-10-11 14:41:54 -04:00
|
|
|
fn op_seek(
|
2019-11-13 23:14:48 -05:00
|
|
|
_state: &ThreadSafeState,
|
2019-08-26 08:50:21 -04:00
|
|
|
args: Value,
|
|
|
|
_zero_copy: Option<PinnedBuf>,
|
|
|
|
) -> Result<JsonOp, ErrBox> {
|
|
|
|
let args: SeekArgs = serde_json::from_value(args)?;
|
2019-11-07 11:11:15 -05:00
|
|
|
let rid = args.rid as u32;
|
|
|
|
let offset = args.offset;
|
|
|
|
let whence = args.whence as u32;
|
|
|
|
// Translate seek mode to Rust repr.
|
|
|
|
let seek_from = match whence {
|
|
|
|
0 => SeekFrom::Start(offset as u64),
|
|
|
|
1 => SeekFrom::Current(i64::from(offset)),
|
|
|
|
2 => SeekFrom::End(i64::from(offset)),
|
|
|
|
_ => {
|
|
|
|
return Err(ErrBox::from(DenoError::new(
|
|
|
|
ErrorKind::InvalidSeekMode,
|
|
|
|
format!("Invalid seek mode: {}", whence),
|
|
|
|
)));
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2019-11-13 23:14:48 -05:00
|
|
|
let fut = SeekFuture { seek_from, rid };
|
2019-08-14 11:03:02 -04:00
|
|
|
|
2019-11-07 11:11:15 -05:00
|
|
|
let op = fut.and_then(move |_| futures::future::ok(json!({})));
|
2019-09-10 00:59:40 -04:00
|
|
|
if args.promise_id.is_none() {
|
|
|
|
let buf = op.wait()?;
|
|
|
|
Ok(JsonOp::Sync(buf))
|
|
|
|
} else {
|
|
|
|
Ok(JsonOp::Async(Box::new(op)))
|
2019-08-14 11:03:02 -04:00
|
|
|
}
|
|
|
|
}
|