0
0
Fork 0
mirror of https://github.com/denoland/deno.git synced 2025-03-03 09:31:22 -05:00

Implement deno.mkdir()

This commit is contained in:
Sajjad Hashemian 2018-09-10 14:18:36 +04:30 committed by Ryan Dahl
parent e293c204a0
commit c2663e1d82
9 changed files with 77 additions and 48 deletions

View file

@ -195,6 +195,7 @@ run_node("gen_declarations") {
"js/fetch.ts",
"js/global-eval.ts",
"js/globals.ts",
"js/mkdir.ts",
"js/os.ts",
"js/read_file.ts",
"js/text_encoding.ts",
@ -233,6 +234,7 @@ run_node("bundle") {
"js/fetch_types.d.ts",
"js/globals.ts",
"js/main.ts",
"js/mkdir.ts",
"js/os.ts",
"js/plugins.d.ts",
"js/read_file.ts",

View file

@ -6,12 +6,12 @@ export {
exit,
FileInfo,
makeTempDirSync,
mkdirSync,
renameSync,
statSync,
lstatSync,
writeFileSync
} from "./os";
export { mkdirSync, mkdir } from "./mkdir";
export { readFileSync, readFile } from "./read_file";
export { ErrorKind, DenoError } from "./errors";
export { libdeno } from "./libdeno";

37
js/mkdir.ts Normal file
View file

@ -0,0 +1,37 @@
// Copyright 2018 the Deno authors. All rights reserved. MIT license.
import * as fbs from "gen/msg_generated";
import { flatbuffers } from "flatbuffers";
import * as dispatch from "./dispatch";
/**
* Creates a new directory with the specified path and permission synchronously.
*
* import { mkdirSync } from "deno";
* mkdirSync("new_dir");
*/
export function mkdirSync(path: string, mode = 0o777): void {
dispatch.sendSync(...req(path, mode));
}
/**
* Creates a new directory with the specified path and permission.
*
* import { mkdir } from "deno";
* await mkdir("new_dir");
*/
export async function mkdir(path: string, mode = 0o777): Promise<void> {
await dispatch.sendAsync(...req(path, mode));
}
function req(
path: string,
mode: number
): [flatbuffers.Builder, fbs.Any, flatbuffers.Offset] {
const builder = new flatbuffers.Builder();
const path_ = builder.createString(path);
fbs.Mkdir.startMkdir(builder);
fbs.Mkdir.addPath(builder, path_);
fbs.Mkdir.addMode(builder, mode);
const msg = fbs.Mkdir.endMkdir(builder);
return [builder, fbs.Any.Mkdir, msg];
}

28
js/mkdir_test.ts Normal file
View file

@ -0,0 +1,28 @@
// Copyright 2018 the Deno authors. All rights reserved. MIT license.
import { test, testPerm, assert, assertEqual } from "./test_util.ts";
import * as deno from "deno";
testPerm({ write: true }, function mkdirSyncSuccess() {
const path = deno.makeTempDirSync() + "/dir/subdir";
deno.mkdirSync(path);
const pathInfo = deno.statSync(path);
assert(pathInfo.isDirectory());
});
testPerm({ write: false }, function mkdirSyncPerm() {
let err;
try {
deno.mkdirSync("/baddir");
} catch (e) {
err = e;
}
assertEqual(err.kind, deno.ErrorKind.PermissionDenied);
assertEqual(err.name, "PermissionDenied");
});
testPerm({ write: true }, async function mkdirSuccess() {
const path = deno.makeTempDirSync() + "/dir/subdir";
await deno.mkdir(path);
const pathInfo = deno.statSync(path);
assert(pathInfo.isDirectory());
});

View file

@ -107,25 +107,6 @@ export function makeTempDirSync({
return path!;
}
// mkdir creates a new directory with the specified name
// and permission bits (before umask).
export function mkdirSync(path: string, mode = 0o777): void {
/* Ideally we could write:
const res = sendSync({
command: fbs.Command.MKDIR_SYNC,
mkdirSyncPath: path,
mkdirSyncMode: mode,
});
*/
const builder = new flatbuffers.Builder();
const path_ = builder.createString(path);
fbs.MkdirSync.startMkdirSync(builder);
fbs.MkdirSync.addPath(builder, path_);
fbs.MkdirSync.addMode(builder, mode);
const msg = fbs.MkdirSync.endMkdirSync(builder);
sendSync(builder, fbs.Any.MkdirSync, msg);
}
function createEnv(_msg: fbs.EnvironRes): { [index: string]: string } {
const env: { [index: string]: string } = {};

View file

@ -152,25 +152,6 @@ test(function makeTempDirSyncPerm() {
assertEqual(err.name, "PermissionDenied");
});
testPerm({ write: true }, function mkdirSync() {
const path = deno.makeTempDirSync() + "/dir/subdir";
deno.mkdirSync(path);
const pathInfo = deno.statSync(path);
assert(pathInfo.isDirectory());
});
testPerm({ write: false }, function mkdDirSyncPerm() {
let err;
try {
const path = "/baddir";
deno.mkdirSync(path);
} catch (err_) {
err = err_;
}
assertEqual(err.kind, deno.ErrorKind.PermissionDenied);
assertEqual(err.name, "PermissionDenied");
});
testPerm({ write: true }, function renameSync() {
const testDir = deno.makeTempDirSync() + "/test-rename";
const oldpath = testDir + "/oldpath";

View file

@ -6,3 +6,4 @@ import "./console_test.ts";
import "./fetch_test.ts";
import "./os_test.ts";
import "./read_file_test.ts";
import "./mkdir_test.ts";

View file

@ -49,7 +49,7 @@ pub extern "C" fn msg_from_js(d: *const DenoC, buf: deno_buf) {
msg::Any::TimerStart => handle_timer_start,
msg::Any::TimerClear => handle_timer_clear,
msg::Any::MakeTempDir => handle_make_temp_dir,
msg::Any::MkdirSync => handle_mkdir_sync,
msg::Any::Mkdir => handle_mkdir,
msg::Any::ReadFile => handle_read_file,
msg::Any::RenameSync => handle_rename_sync,
msg::Any::SetEnv => handle_set_env,
@ -418,18 +418,17 @@ fn handle_make_temp_dir(d: *const DenoC, base: &msg::Base) -> Box<Op> {
}()))
}
fn handle_mkdir_sync(d: *const DenoC, base: &msg::Base) -> Box<Op> {
let msg = base.msg_as_mkdir_sync().unwrap();
let path = msg.path().unwrap();
fn handle_mkdir(d: *const DenoC, base: &msg::Base) -> Box<Op> {
let msg = base.msg_as_mkdir().unwrap();
// TODO let mode = msg.mode();
let path = msg.path().unwrap();
let deno = from_c(d);
debug!("handle_mkdir_sync {}", path);
if !deno.flags.allow_write {
return odd_future(permission_denied());
}
// TODO(ry) use blocking
// TODO Use tokio_threadpool.
Box::new(futures::future::result(|| -> OpResult {
debug!("handle_mkdir {}", path);
// TODO(ry) Use mode.
deno_fs::mkdir(Path::new(path))?;
Ok(None)

View file

@ -14,7 +14,7 @@ union Any {
FetchRes,
MakeTempDir,
MakeTempDirRes,
MkdirSync,
Mkdir,
ReadFile,
ReadFileRes,
RenameSync,
@ -167,7 +167,7 @@ table MakeTempDirRes {
path: string;
}
table MkdirSync {
table Mkdir {
path: string;
mode: uint;
// mode specified by https://godoc.org/os#FileMode