0
0
Fork 0
mirror of https://github.com/denoland/deno.git synced 2024-10-29 08:58:01 -04:00

Add Deno.realpath (#3404)

This commit is contained in:
Kevin (Kun) "Kassimo" Qian 2019-11-26 00:40:57 -08:00 committed by Ry Dahl
parent 658ec2aaf9
commit f88dc4e197
7 changed files with 157 additions and 0 deletions

View file

@ -56,6 +56,7 @@ export { chownSync, chown } from "./chown.ts";
export { utimeSync, utime } from "./utime.ts";
export { removeSync, remove, RemoveOption } from "./remove.ts";
export { renameSync, rename } from "./rename.ts";
export { realpathSync, realpath } from "./realpath.ts";
export { readFileSync, readFile } from "./read_file.ts";
export { readDirSync, readDir } from "./read_dir.ts";
export { copyFileSync, copyFile } from "./copy_file.ts";

View file

@ -55,6 +55,7 @@ export let OP_CHOWN: number;
export let OP_REMOVE: number;
export let OP_COPY_FILE: number;
export let OP_STAT: number;
export let OP_REALPATH: number;
export let OP_READ_DIR: number;
export let OP_RENAME: number;
export let OP_LINK: number;
@ -97,6 +98,7 @@ export function asyncMsgFromRust(opId: number, ui8: Uint8Array): void {
case OP_REMOVE:
case OP_COPY_FILE:
case OP_STAT:
case OP_REALPATH:
case OP_READ_DIR:
case OP_RENAME:
case OP_LINK:

View file

@ -605,6 +605,21 @@ declare namespace Deno {
isSymlink(): boolean;
}
// @url js/realpath.d.ts
/** Returns absolute normalized path with symbolic links resolved
* synchronously.
*
* const realPath = Deno.realpathSync("./some/path");
*/
export function realpathSync(path: string): string;
/** Returns absolute normalized path with symbolic links resolved.
*
* const realPath = await Deno.realpath("./some/path");
*/
export function realpath(path: string): Promise<string>;
// @url js/read_dir.d.ts
/** Reads the directory given by path and returns a list of file info

19
cli/js/realpath.ts Normal file
View file

@ -0,0 +1,19 @@
// Copyright 2018-2019 the Deno authors. All rights reserved. MIT license.
import { sendSync, sendAsync } from "./dispatch_json.ts";
import * as dispatch from "./dispatch.ts";
/** Returns absolute normalized path with symbolic links resolved synchronously.
*
* const realPath = Deno.realpathSync("./some/path");
*/
export function realpathSync(path: string): string {
return sendSync(dispatch.OP_REALPATH, { path });
}
/** Returns absolute normalized path with symbolic links resolved.
*
* const realPath = await Deno.realpath("./some/path");
*/
export async function realpath(path: string): Promise<string> {
return await sendAsync(dispatch.OP_REALPATH, { path });
}

91
cli/js/realpath_test.ts Normal file
View file

@ -0,0 +1,91 @@
// Copyright 2018-2019 the Deno authors. All rights reserved. MIT license.
import { testPerm, assert, assertEquals } from "./test_util.ts";
testPerm({ read: true }, function realpathSyncSuccess(): void {
const incompletePath = "cli/tests/fixture.json";
const realPath = Deno.realpathSync(incompletePath);
assert(realPath.startsWith("/"));
assert(realPath.endsWith(incompletePath));
});
if (Deno.build.os !== "win") {
testPerm({ read: true, write: true }, function realpathSyncSymlink(): void {
const testDir = Deno.makeTempDirSync();
const target = testDir + "/target";
const symlink = testDir + "/symln";
Deno.mkdirSync(target);
Deno.symlinkSync(target, symlink);
const targetPath = Deno.realpathSync(symlink);
assert(targetPath.startsWith("/"));
assert(targetPath.endsWith("/target"));
});
}
testPerm({ read: false }, function realpathSyncPerm(): void {
let caughtError = false;
try {
Deno.realpathSync("some_file");
} catch (e) {
caughtError = true;
assertEquals(e.kind, Deno.ErrorKind.PermissionDenied);
assertEquals(e.name, "PermissionDenied");
}
assert(caughtError);
});
testPerm({ read: true }, function realpathSyncNotFound(): void {
let caughtError = false;
try {
Deno.realpathSync("bad_filename");
} catch (e) {
caughtError = true;
assertEquals(e.kind, Deno.ErrorKind.NotFound);
}
assert(caughtError);
});
testPerm({ read: true }, async function realpathSuccess(): Promise<void> {
const incompletePath = "cli/tests/fixture.json";
const realPath = await Deno.realpath(incompletePath);
assert(realPath.startsWith("/"));
assert(realPath.endsWith(incompletePath));
});
if (Deno.build.os !== "win") {
testPerm(
{ read: true, write: true },
async function realpathSymlink(): Promise<void> {
const testDir = Deno.makeTempDirSync();
const target = testDir + "/target";
const symlink = testDir + "/symln";
Deno.mkdirSync(target);
Deno.symlinkSync(target, symlink);
const targetPath = await Deno.realpath(symlink);
assert(targetPath.startsWith("/"));
assert(targetPath.endsWith("/target"));
}
);
}
testPerm({ read: false }, async function realpathPerm(): Promise<void> {
let caughtError = false;
try {
await Deno.realpath("some_file");
} catch (e) {
caughtError = true;
assertEquals(e.kind, Deno.ErrorKind.PermissionDenied);
assertEquals(e.name, "PermissionDenied");
}
assert(caughtError);
});
testPerm({ read: true }, async function realpathNotFound(): Promise<void> {
let caughtError = false;
try {
await Deno.realpath("bad_filename");
} catch (e) {
caughtError = true;
assertEquals(e.kind, Deno.ErrorKind.NotFound);
}
assert(caughtError);
});

View file

@ -33,6 +33,7 @@ import "./mkdir_test.ts";
import "./net_test.ts";
import "./os_test.ts";
import "./process_test.ts";
import "./realpath_test.ts";
import "./read_dir_test.ts";
import "./read_file_test.ts";
import "./read_link_test.ts";

View file

@ -24,6 +24,7 @@ pub fn init(i: &mut Isolate, s: &ThreadSafeState) {
i.register_op("remove", s.core_op(json_op(s.stateful_op(op_remove))));
i.register_op("copy_file", s.core_op(json_op(s.stateful_op(op_copy_file))));
i.register_op("stat", s.core_op(json_op(s.stateful_op(op_stat))));
i.register_op("realpath", s.core_op(json_op(s.stateful_op(op_realpath))));
i.register_op("read_dir", s.core_op(json_op(s.stateful_op(op_read_dir))));
i.register_op("rename", s.core_op(json_op(s.stateful_op(op_rename))));
i.register_op("link", s.core_op(json_op(s.stateful_op(op_link))));
@ -277,6 +278,33 @@ fn op_stat(
})
}
#[derive(Deserialize)]
#[serde(rename_all = "camelCase")]
struct RealpathArgs {
promise_id: Option<u64>,
path: String,
}
fn op_realpath(
state: &ThreadSafeState,
args: Value,
_zero_copy: Option<PinnedBuf>,
) -> Result<JsonOp, ErrBox> {
let args: RealpathArgs = serde_json::from_value(args)?;
let (_, path_) = deno_fs::resolve_from_cwd(args.path.as_ref())?;
state.check_read(&path_)?;
let path = args.path.clone();
let is_sync = args.promise_id.is_none();
blocking_json(is_sync, move || {
debug!("op_realpath {}", &path);
// corresponds to the realpath on Unix and
// CreateFile and GetFinalPathNameByHandle on Windows
let realpath = fs::canonicalize(&path)?;
let realpath_str = realpath.to_str().unwrap().to_owned().replace("\\", "/");
Ok(json!(realpath_str))
})
}
#[derive(Deserialize)]
#[serde(rename_all = "camelCase")]
struct ReadDirArgs {