From f88dc4e197f36842b13843dd88da3d74d67578e5 Mon Sep 17 00:00:00 2001 From: "Kevin (Kun) \"Kassimo\" Qian" Date: Tue, 26 Nov 2019 00:40:57 -0800 Subject: Add Deno.realpath (#3404) --- cli/ops/fs.rs | 28 ++++++++++++++++++++++++++++ 1 file changed, 28 insertions(+) (limited to 'cli/ops') diff --git a/cli/ops/fs.rs b/cli/ops/fs.rs index 4d54aaad6..54ac1971b 100644 --- a/cli/ops/fs.rs +++ b/cli/ops/fs.rs @@ -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, + path: String, +} + +fn op_realpath( + state: &ThreadSafeState, + args: Value, + _zero_copy: Option, +) -> Result { + 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 { -- cgit v1.2.3