Skip to content

Commit

Permalink
port cli/ops/fs.rs to json ops, part 1
Browse files Browse the repository at this point in the history
  • Loading branch information
bartlomieju committed Aug 24, 2019
1 parent 137f337 commit f3c7834
Show file tree
Hide file tree
Showing 9 changed files with 107 additions and 121 deletions.
25 changes: 0 additions & 25 deletions cli/msg.fbs
Original file line number Diff line number Diff line change
@@ -1,14 +1,10 @@
union Any {
Chdir,
Chmod,
Chown,
CopyFile,
Cwd,
CwdRes,
Link,
MakeTempDir,
MakeTempDirRes,
Mkdir,
Read,
ReadDir,
ReadDirRes,
Expand Down Expand Up @@ -122,10 +118,6 @@ table FormatErrorRes {
error: string;
}

table Chdir {
directory: string;
}

table KeyValue {
key: string;
value: string;
Expand All @@ -141,23 +133,6 @@ table MakeTempDirRes {
path: string;
}

table Mkdir {
path: string;
recursive: bool;
mode: uint; // Specified by https://godoc.org/os#FileMode
}

table Chmod {
path: string;
mode: uint; // Specified by https://godoc.org/os#FileMode
}

table Chown {
path: string;
uid: uint;
gid: uint; // Specified by https://godoc.org/os#Chown
}

table Remove {
path: string;
recursive: bool;
Expand Down
9 changes: 2 additions & 7 deletions cli/ops/dispatch_flatbuffers.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,9 +8,8 @@ use hyper::rt::Future;

use super::files::{op_read, op_write};
use super::fs::{
op_chdir, op_chmod, op_chown, op_copy_file, op_cwd, op_link,
op_make_temp_dir, op_mkdir, op_read_dir, op_read_link, op_remove, op_rename,
op_stat, op_symlink, op_truncate,
op_copy_file, op_cwd, op_link, op_make_temp_dir, op_read_dir, op_read_link,
op_remove, op_rename, op_stat, op_symlink, op_truncate,
};

type CliDispatchFn = fn(
Expand Down Expand Up @@ -125,14 +124,10 @@ pub fn serialize_response(
/// Standard ops set for most isolates
pub fn op_selector_std(inner_type: msg::Any) -> Option<CliDispatchFn> {
match inner_type {
msg::Any::Chdir => Some(op_chdir),
msg::Any::Chmod => Some(op_chmod),
msg::Any::Chown => Some(op_chown),
msg::Any::CopyFile => Some(op_copy_file),
msg::Any::Cwd => Some(op_cwd),
msg::Any::Link => Some(op_link),
msg::Any::MakeTempDir => Some(op_make_temp_dir),
msg::Any::Mkdir => Some(op_mkdir),
msg::Any::Read => Some(op_read),
msg::Any::ReadDir => Some(op_read_dir),
msg::Any::Readlink => Some(op_read_link),
Expand Down
107 changes: 65 additions & 42 deletions cli/ops/fs.rs
Original file line number Diff line number Diff line change
Expand Up @@ -18,81 +18,104 @@ use std::time::UNIX_EPOCH;
#[cfg(unix)]
use std::os::unix::fs::PermissionsExt;

#[derive(Deserialize)]
struct ChdirArgs {
directory: String,
}

pub fn op_chdir(
_state: &ThreadSafeState,
base: &msg::Base<'_>,
data: Option<PinnedBuf>,
) -> CliOpResult {
assert!(data.is_none());
let inner = base.inner_as_chdir().unwrap();
let directory = inner.directory().unwrap();
std::env::set_current_dir(&directory)?;
ok_buf(empty_buf())
args: Value,
_zero_copy: Option<PinnedBuf>,
) -> Result<JsonOp, ErrBox> {
let args: ChdirArgs = serde_json::from_value(args)?;
std::env::set_current_dir(&args.directory)?;
Ok(JsonOp::Sync(json!({})))
}

#[derive(Deserialize)]
#[serde(rename_all = "camelCase")]
struct MkdirArgs {
promise_id: Option<u64>,
path: String,
recursive: bool,
mode: u32,
}

pub fn op_mkdir(
state: &ThreadSafeState,
base: &msg::Base<'_>,
data: Option<PinnedBuf>,
) -> CliOpResult {
assert!(data.is_none());
let inner = base.inner_as_mkdir().unwrap();
let (path, path_) = deno_fs::resolve_from_cwd(inner.path().unwrap())?;
let recursive = inner.recursive();
let mode = inner.mode();
args: Value,
_zero_copy: Option<PinnedBuf>,
) -> Result<JsonOp, ErrBox> {
let args: MkdirArgs = serde_json::from_value(args)?;
let (path, path_) = deno_fs::resolve_from_cwd(args.path.as_ref())?;

state.check_write(&path_)?;

blocking(base.sync(), move || {
let is_sync = args.promise_id.is_none();
blocking_json(is_sync, move || {
debug!("op_mkdir {}", path_);
deno_fs::mkdir(&path, mode, recursive)?;
Ok(empty_buf())
deno_fs::mkdir(&path, args.mode, args.recursive)?;
Ok(json!({}))
})
}

#[derive(Deserialize)]
#[serde(rename_all = "camelCase")]
struct ChmodArgs {
promise_id: Option<u64>,
path: String,
mode: u32,
}

pub fn op_chmod(
state: &ThreadSafeState,
base: &msg::Base<'_>,
data: Option<PinnedBuf>,
) -> CliOpResult {
assert!(data.is_none());
let inner = base.inner_as_chmod().unwrap();
let _mode = inner.mode();
let (path, path_) = deno_fs::resolve_from_cwd(inner.path().unwrap())?;
args: Value,
_zero_copy: Option<PinnedBuf>,
) -> Result<JsonOp, ErrBox> {
let args: ChmodArgs = serde_json::from_value(args)?;
let (path, path_) = deno_fs::resolve_from_cwd(args.path.as_ref())?;

state.check_write(&path_)?;

blocking(base.sync(), move || {
let is_sync = args.promise_id.is_none();
blocking_json(is_sync, move || {
debug!("op_chmod {}", &path_);
// Still check file/dir exists on windows
let _metadata = fs::metadata(&path)?;
#[cfg(any(unix))]
{
let mut permissions = _metadata.permissions();
permissions.set_mode(_mode);
permissions.set_mode(args.mode);
fs::set_permissions(&path, permissions)?;
}
Ok(empty_buf())
Ok(json!({}))
})
}

#[derive(Deserialize)]
#[serde(rename_all = "camelCase")]
struct ChownArgs {
promise_id: Option<u64>,
path: String,
uid: u32,
gid: u32,
}

pub fn op_chown(
state: &ThreadSafeState,
base: &msg::Base<'_>,
data: Option<PinnedBuf>,
) -> CliOpResult {
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();
args: Value,
_zero_copy: Option<PinnedBuf>,
) -> Result<JsonOp, ErrBox> {
let args: ChownArgs = serde_json::from_value(args)?;

state.check_write(&path)?;
state.check_write(&args.path)?;

blocking(base.sync(), move || {
debug!("op_chown {}", &path);
match deno_fs::chown(&path, uid, gid) {
Ok(_) => Ok(empty_buf()),
let is_sync = args.promise_id.is_none();
blocking_json(is_sync, move || {
debug!("op_chown {}", &args.path);
match deno_fs::chown(args.path.as_ref(), args.uid, args.gid) {
Ok(_) => Ok(json!({})),
Err(e) => Err(e),
}
})
Expand Down
18 changes: 17 additions & 1 deletion cli/ops/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ mod workers;

// Warning! These values are duplicated in the TypeScript code (js/dispatch.ts),
// update with care.
pub const OP_FLATBUFFER: OpId = 44;
pub const OP_FLATBUFFER: OpId = 100;
pub const OP_READ: OpId = 1;
pub const OP_WRITE: OpId = 2;
pub const OP_EXIT: OpId = 3;
Expand Down Expand Up @@ -68,6 +68,10 @@ pub const OP_WORKER_GET_MESSAGE: OpId = 38;
pub const OP_RUN: OpId = 39;
pub const OP_RUN_STATUS: OpId = 40;
pub const OP_KILL: OpId = 41;
pub const OP_CHDIR: OpId = 42;
pub const OP_MKDIR: OpId = 43;
pub const OP_CHMOD: OpId = 44;
pub const OP_CHOWN: OpId = 45;

pub fn dispatch(
state: &ThreadSafeState,
Expand Down Expand Up @@ -242,6 +246,18 @@ pub fn dispatch(
OP_KILL => {
dispatch_json::dispatch(process::op_kill, state, control, zero_copy)
}
OP_CHDIR => {
dispatch_json::dispatch(fs::op_chdir, state, control, zero_copy)
}
OP_MKDIR => {
dispatch_json::dispatch(fs::op_mkdir, state, control, zero_copy)
}
OP_CHMOD => {
dispatch_json::dispatch(fs::op_chmod, state, control, zero_copy)
}
OP_CHOWN => {
dispatch_json::dispatch(fs::op_chown, state, control, zero_copy)
}
OP_FLATBUFFER => dispatch_flatbuffers::dispatch(state, control, zero_copy),
_ => panic!("bad op_id"),
};
Expand Down
17 changes: 4 additions & 13 deletions js/chmod.ts
Original file line number Diff line number Diff line change
@@ -1,29 +1,20 @@
// Copyright 2018-2019 the Deno authors. All rights reserved. MIT license.
import { sendSync, sendAsync, msg, flatbuffers } from "./dispatch_flatbuffers";

function req(
path: string,
mode: number
): [flatbuffers.Builder, msg.Any, flatbuffers.Offset] {
const builder = flatbuffers.createBuilder();
const path_ = builder.createString(path);
const inner = msg.Chmod.createChmod(builder, path_, mode);
return [builder, msg.Any.Chmod, inner];
}
import { sendSync, sendAsync } from "./dispatch_json";
import * as dispatch from "./dispatch";

/** Changes the permission of a specific file/directory of specified path
* synchronously.
*
* Deno.chmodSync("/path/to/file", 0o666);
*/
export function chmodSync(path: string, mode: number): void {
sendSync(...req(path, mode));
sendSync(dispatch.OP_CHMOD, { path, mode });
}

/** Changes the permission of a specific file/directory of specified path.
*
* await Deno.chmod("/path/to/file", 0o666);
*/
export async function chmod(path: string, mode: number): Promise<void> {
await sendAsync(...req(path, mode));
await sendAsync(dispatch.OP_CHMOD, { path, mode });
}
18 changes: 4 additions & 14 deletions js/chown.ts
Original file line number Diff line number Diff line change
@@ -1,16 +1,6 @@
// Copyright 2018-2019 the Deno authors. All rights reserved. MIT license.
import { sendSync, sendAsync, msg, flatbuffers } from "./dispatch_flatbuffers";

function req(
path: string,
uid: number,
gid: number
): [flatbuffers.Builder, msg.Any, flatbuffers.Offset] {
const builder = flatbuffers.createBuilder();
const path_ = builder.createString(path);
const inner = msg.Chown.createChown(builder, path_, uid, gid);
return [builder, msg.Any.Chown, inner];
}
import { sendSync, sendAsync } from "./dispatch_json";
import * as dispatch from "./dispatch";

/**
* Change owner of a regular file or directory synchronously. Unix only at the moment.
Expand All @@ -19,7 +9,7 @@ function req(
* @param gid group id of the new owner
*/
export function chownSync(path: string, uid: number, gid: number): void {
sendSync(...req(path, uid, gid));
sendSync(dispatch.OP_CHOWN, { path, uid, gid });
}

/**
Expand All @@ -33,5 +23,5 @@ export async function chown(
uid: number,
gid: number
): Promise<void> {
await sendAsync(...req(path, uid, gid));
await sendAsync(dispatch.OP_CHOWN, { path, uid, gid });
}
7 changes: 3 additions & 4 deletions js/dir.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
// Copyright 2018-2019 the Deno authors. All rights reserved. MIT license.
import { assert } from "./util";
import { sendSync, flatbuffers, msg } from "./dispatch_flatbuffers";
import { sendSync as sendSyncJson } from "./dispatch_json";
import * as dispatch from "./dispatch";

/**
* `cwd()` Return a string representing the current working directory.
Expand All @@ -26,8 +28,5 @@ export function cwd(): string {
* throws `NotFound` exception if directory not available
*/
export function chdir(directory: string): void {
const builder = flatbuffers.createBuilder();
const directory_ = builder.createString(directory);
const inner = msg.Chdir.createChdir(builder, directory_);
sendSync(builder, msg.Any.Chdir, inner);
sendSyncJson(dispatch.OP_CHDIR, { directory });
}
9 changes: 8 additions & 1 deletion js/dispatch.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ import * as flatbuffers from "./dispatch_flatbuffers";
import * as json from "./dispatch_json";

// These consts are shared with Rust. Update with care.
export const OP_FLATBUFFER = 44;
export const OP_FLATBUFFER = 100;
export const OP_READ = 1;
export const OP_WRITE = 2;
export const OP_EXIT = 3;
Expand Down Expand Up @@ -46,6 +46,10 @@ export const OP_WORKER_GET_MESSAGE = 38;
export const OP_RUN = 39;
export const OP_RUN_STATUS = 40;
export const OP_KILL = 41;
export const OP_CHDIR = 42;
export const OP_MKDIR = 43;
export const OP_CHMOD = 44;
export const OP_CHOWN = 45;

export function asyncMsgFromRust(opId: number, ui8: Uint8Array): void {
switch (opId) {
Expand Down Expand Up @@ -73,6 +77,9 @@ export function asyncMsgFromRust(opId: number, ui8: Uint8Array): void {
case OP_HOST_GET_MESSAGE:
case OP_WORKER_GET_MESSAGE:
case OP_RUN_STATUS:
case OP_MKDIR:
case OP_CHMOD:
case OP_CHOWN:
json.asyncMsgFromRust(opId, ui8);
break;
default:
Expand Down
Loading

0 comments on commit f3c7834

Please sign in to comment.