2020-01-02 15:13:47 -05:00
|
|
|
// Copyright 2018-2020 the Deno authors. All rights reserved. MIT license.
|
2019-08-26 08:50:21 -04:00
|
|
|
use super::dispatch_json::{Deserialize, JsonOp, Value};
|
2020-02-26 01:01:24 -05:00
|
|
|
use super::io::{FileMetadata, StreamResource};
|
2019-08-14 11:03:02 -04:00
|
|
|
use crate::fs as deno_fs;
|
2020-02-23 14:51:29 -05:00
|
|
|
use crate::op_error::OpError;
|
2020-02-08 14:34:31 -05:00
|
|
|
use crate::state::State;
|
2020-01-05 11:56:18 -05:00
|
|
|
use deno_core::*;
|
2019-11-16 19:17:47 -05:00
|
|
|
use futures::future::FutureExt;
|
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;
|
2020-01-20 09:45:44 -05:00
|
|
|
use std::path::Path;
|
2019-08-14 11:03:02 -04:00
|
|
|
use tokio;
|
|
|
|
|
2020-02-08 14:34:31 -05:00
|
|
|
pub fn init(i: &mut Isolate, s: &State) {
|
2020-02-25 09:14:27 -05:00
|
|
|
i.register_op("op_open", s.stateful_json_op(op_open));
|
|
|
|
i.register_op("op_close", s.stateful_json_op(op_close));
|
|
|
|
i.register_op("op_seek", s.stateful_json_op(op_seek));
|
2019-10-11 14:41:54 -04:00
|
|
|
}
|
|
|
|
|
2019-08-26 08:50:21 -04:00
|
|
|
#[derive(Deserialize)]
|
|
|
|
#[serde(rename_all = "camelCase")]
|
|
|
|
struct OpenArgs {
|
|
|
|
promise_id: Option<u64>,
|
|
|
|
filename: String,
|
2020-01-21 04:49:42 -05:00
|
|
|
options: Option<OpenOptions>,
|
|
|
|
mode: Option<String>,
|
|
|
|
}
|
|
|
|
|
|
|
|
#[derive(Deserialize, Default, Debug)]
|
|
|
|
#[serde(rename_all = "camelCase")]
|
|
|
|
#[serde(default)]
|
|
|
|
struct OpenOptions {
|
|
|
|
read: bool,
|
|
|
|
write: bool,
|
|
|
|
create: bool,
|
|
|
|
truncate: bool,
|
|
|
|
append: bool,
|
|
|
|
create_new: bool,
|
2019-08-26 08:50:21 -04:00
|
|
|
}
|
|
|
|
|
2019-10-11 14:41:54 -04:00
|
|
|
fn op_open(
|
2020-02-08 14:34:31 -05:00
|
|
|
state: &State,
|
2019-08-26 08:50:21 -04:00
|
|
|
args: Value,
|
2020-01-24 15:10:49 -05:00
|
|
|
_zero_copy: Option<ZeroCopyBuf>,
|
2020-02-23 14:51:29 -05:00
|
|
|
) -> Result<JsonOp, OpError> {
|
2019-08-26 08:50:21 -04:00
|
|
|
let args: OpenArgs = serde_json::from_value(args)?;
|
2020-01-20 09:45:44 -05:00
|
|
|
let filename = deno_fs::resolve_from_cwd(Path::new(&args.filename))?;
|
2019-11-14 12:10:25 -05:00
|
|
|
let state_ = state.clone();
|
2019-08-14 11:03:02 -04:00
|
|
|
let mut open_options = tokio::fs::OpenOptions::new();
|
|
|
|
|
2020-01-21 04:49:42 -05:00
|
|
|
if let Some(options) = args.options {
|
|
|
|
if options.read {
|
2020-01-20 09:45:44 -05:00
|
|
|
state.check_read(&filename)?;
|
2019-08-14 11:03:02 -04:00
|
|
|
}
|
2020-01-21 04:49:42 -05:00
|
|
|
|
|
|
|
if options.write || options.append {
|
2020-01-20 09:45:44 -05:00
|
|
|
state.check_write(&filename)?;
|
2019-08-14 11:03:02 -04:00
|
|
|
}
|
2020-01-21 04:49:42 -05:00
|
|
|
|
|
|
|
open_options
|
|
|
|
.read(options.read)
|
|
|
|
.create(options.create)
|
|
|
|
.write(options.write)
|
|
|
|
.truncate(options.truncate)
|
|
|
|
.append(options.append)
|
|
|
|
.create_new(options.create_new);
|
|
|
|
} else if let Some(mode) = args.mode {
|
|
|
|
let mode = mode.as_ref();
|
|
|
|
match mode {
|
|
|
|
"r" => {
|
|
|
|
state.check_read(&filename)?;
|
|
|
|
}
|
|
|
|
"w" | "a" | "x" => {
|
|
|
|
state.check_write(&filename)?;
|
|
|
|
}
|
|
|
|
&_ => {
|
|
|
|
state.check_read(&filename)?;
|
|
|
|
state.check_write(&filename)?;
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
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);
|
|
|
|
}
|
|
|
|
&_ => {
|
2020-02-23 14:51:29 -05:00
|
|
|
// TODO: this should be type error
|
|
|
|
return Err(OpError::other("Unknown open mode.".to_string()));
|
2020-01-21 04:49:42 -05:00
|
|
|
}
|
2019-08-14 11:03:02 -04:00
|
|
|
}
|
2020-01-21 04:49:42 -05:00
|
|
|
} else {
|
2020-02-23 14:51:29 -05:00
|
|
|
return Err(OpError::other(
|
2020-01-21 04:49:42 -05:00
|
|
|
"Open requires either mode or options.".to_string(),
|
2020-02-23 14:51:29 -05:00
|
|
|
));
|
2020-01-21 04:49:42 -05:00
|
|
|
};
|
2019-08-14 11:03:02 -04:00
|
|
|
|
2019-08-26 08:50:21 -04:00
|
|
|
let is_sync = args.promise_id.is_none();
|
2019-12-30 08:57:17 -05:00
|
|
|
|
|
|
|
let fut = async move {
|
|
|
|
let fs_file = open_options.open(filename).await?;
|
2020-02-08 14:34:31 -05:00
|
|
|
let mut state = state_.borrow_mut();
|
2020-02-26 01:01:24 -05:00
|
|
|
let rid = state.resource_table.add(
|
|
|
|
"fsFile",
|
|
|
|
Box::new(StreamResource::FsFile(fs_file, FileMetadata::default())),
|
|
|
|
);
|
2019-12-30 08:57:17 -05:00
|
|
|
Ok(json!(rid))
|
|
|
|
};
|
2019-08-26 08:50:21 -04:00
|
|
|
|
|
|
|
if is_sync {
|
2019-12-30 08:57:17 -05:00
|
|
|
let buf = futures::executor::block_on(fut)?;
|
2019-08-26 08:50:21 -04:00
|
|
|
Ok(JsonOp::Sync(buf))
|
2019-08-14 11:03:02 -04:00
|
|
|
} else {
|
2020-02-03 18:08:44 -05:00
|
|
|
Ok(JsonOp::Async(fut.boxed_local()))
|
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(
|
2020-02-08 14:34:31 -05:00
|
|
|
state: &State,
|
2019-08-26 08:50:21 -04:00
|
|
|
args: Value,
|
2020-01-24 15:10:49 -05:00
|
|
|
_zero_copy: Option<ZeroCopyBuf>,
|
2020-02-23 14:51:29 -05:00
|
|
|
) -> Result<JsonOp, OpError> {
|
2019-08-26 08:50:21 -04:00
|
|
|
let args: CloseArgs = serde_json::from_value(args)?;
|
|
|
|
|
2020-02-08 14:34:31 -05:00
|
|
|
let mut state = state.borrow_mut();
|
|
|
|
state
|
|
|
|
.resource_table
|
|
|
|
.close(args.rid as u32)
|
2020-03-05 08:30:41 -05:00
|
|
|
.ok_or_else(OpError::bad_resource_id)?;
|
2019-09-10 00:59:40 -04:00
|
|
|
Ok(JsonOp::Sync(json!({})))
|
2019-08-14 11:03:02 -04:00
|
|
|
}
|
|
|
|
|
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(
|
2020-02-08 14:34:31 -05:00
|
|
|
state: &State,
|
2019-08-26 08:50:21 -04:00
|
|
|
args: Value,
|
2020-01-24 15:10:49 -05:00
|
|
|
_zero_copy: Option<ZeroCopyBuf>,
|
2020-02-23 14:51:29 -05:00
|
|
|
) -> Result<JsonOp, OpError> {
|
2019-08-26 08:50:21 -04:00
|
|
|
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)),
|
|
|
|
_ => {
|
2020-02-23 14:51:29 -05:00
|
|
|
return Err(OpError::type_error(format!(
|
|
|
|
"Invalid seek mode: {}",
|
|
|
|
whence
|
2019-11-07 11:11:15 -05:00
|
|
|
)));
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2020-02-08 14:34:31 -05:00
|
|
|
let state = state.borrow();
|
|
|
|
let resource = state
|
|
|
|
.resource_table
|
|
|
|
.get::<StreamResource>(rid)
|
2020-03-05 08:30:41 -05:00
|
|
|
.ok_or_else(OpError::bad_resource_id)?;
|
2019-12-30 08:57:17 -05:00
|
|
|
|
|
|
|
let tokio_file = match resource {
|
2020-02-26 01:01:24 -05:00
|
|
|
StreamResource::FsFile(ref file, _) => file,
|
2020-03-05 08:30:41 -05:00
|
|
|
_ => return Err(OpError::bad_resource_id()),
|
2019-12-30 08:57:17 -05:00
|
|
|
};
|
|
|
|
let mut file = futures::executor::block_on(tokio_file.try_clone())?;
|
|
|
|
|
|
|
|
let fut = async move {
|
2020-03-02 11:44:46 -05:00
|
|
|
let pos = file.seek(seek_from).await?;
|
|
|
|
Ok(json!(pos))
|
2019-11-14 12:10:25 -05:00
|
|
|
};
|
2019-08-14 11:03:02 -04:00
|
|
|
|
2019-09-10 00:59:40 -04:00
|
|
|
if args.promise_id.is_none() {
|
2019-12-30 08:57:17 -05:00
|
|
|
let buf = futures::executor::block_on(fut)?;
|
2019-09-10 00:59:40 -04:00
|
|
|
Ok(JsonOp::Sync(buf))
|
|
|
|
} else {
|
2020-02-03 18:08:44 -05:00
|
|
|
Ok(JsonOp::Async(fut.boxed_local()))
|
2019-08-14 11:03:02 -04:00
|
|
|
}
|
|
|
|
}
|