summaryrefslogtreecommitdiff
path: root/cli
diff options
context:
space:
mode:
authorYingbo (Max) Wang <maxwyb@gmail.com>2019-05-07 18:58:58 -0700
committerRyan Dahl <ry@tinyclouds.org>2019-05-07 21:58:57 -0400
commitec9080f34c936d9af56cca68de664954053bf423 (patch)
treeede97b27acebc225cc69da70433b626af63cc0e6 /cli
parent1f7ad17152c03b140c997590c897b89fbfea7cea (diff)
Add Deno.chown (#2292)
Diffstat (limited to 'cli')
-rw-r--r--cli/BUILD.gn1
-rw-r--r--cli/errors.rs6
-rw-r--r--cli/fs.rs21
-rw-r--r--cli/msg.fbs7
-rw-r--r--cli/ops.rs25
5 files changed, 60 insertions, 0 deletions
diff --git a/cli/BUILD.gn b/cli/BUILD.gn
index 195bdfd60..7f7c6e96f 100644
--- a/cli/BUILD.gn
+++ b/cli/BUILD.gn
@@ -54,6 +54,7 @@ ts_sources = [
"../js/buffer.ts",
"../js/build.ts",
"../js/chmod.ts",
+ "../js/chown.ts",
"../js/colors.ts",
"../js/compiler.ts",
"../js/console.ts",
diff --git a/cli/errors.rs b/cli/errors.rs
index 424091584..71c14282b 100644
--- a/cli/errors.rs
+++ b/cli/errors.rs
@@ -186,6 +186,12 @@ impl From<UnixError> for DenoError {
Errno::EINVAL.desc().to_owned(),
),
},
+ UnixError::Sys(Errno::ENOENT) => Self {
+ repr: Repr::Simple(
+ ErrorKind::NotFound,
+ Errno::ENOENT.desc().to_owned(),
+ ),
+ },
UnixError::Sys(err) => Self {
repr: Repr::Simple(ErrorKind::UnixError, err.desc().to_owned()),
},
diff --git a/cli/fs.rs b/cli/fs.rs
index ff0da95e5..73d7701e6 100644
--- a/cli/fs.rs
+++ b/cli/fs.rs
@@ -8,11 +8,15 @@ use std::path::{Path, PathBuf};
use rand;
use rand::Rng;
+#[cfg(unix)]
+use nix::unistd::{chown as unix_chown, Gid, Uid};
#[cfg(any(unix))]
use std::os::unix::fs::DirBuilderExt;
#[cfg(any(unix))]
use std::os::unix::fs::PermissionsExt;
+use crate::errors::DenoResult;
+
pub fn write_file<T: AsRef<[u8]>>(
filename: &Path,
data: T,
@@ -108,3 +112,20 @@ pub fn normalize_path(path: &Path) -> String {
s
}
}
+
+#[cfg(unix)]
+pub fn chown(path: &str, uid: u32, gid: u32) -> DenoResult<()> {
+ use crate::errors::DenoError;
+ let nix_uid = Uid::from_raw(uid);
+ let nix_gid = Gid::from_raw(gid);
+ unix_chown(path, Option::Some(nix_uid), Option::Some(nix_gid))
+ .map_err(DenoError::from)
+}
+
+#[cfg(not(unix))]
+pub fn chown(_path: &str, _uid: u32, _gid: u32) -> DenoResult<()> {
+ // Noop
+ // TODO: implement chown for Windows
+ use crate::errors;
+ Err(errors::op_not_implemented())
+}
diff --git a/cli/msg.fbs b/cli/msg.fbs
index fb8fd9c22..493bf1d68 100644
--- a/cli/msg.fbs
+++ b/cli/msg.fbs
@@ -2,6 +2,7 @@ union Any {
Accept,
Chdir,
Chmod,
+ Chown,
Close,
CompilerConfig,
CompilerConfigRes,
@@ -343,6 +344,12 @@ table Chmod {
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;
diff --git a/cli/ops.rs b/cli/ops.rs
index ab2284110..7b9500ef8 100644
--- a/cli/ops.rs
+++ b/cli/ops.rs
@@ -186,6 +186,7 @@ pub fn op_selector_std(inner_type: msg::Any) -> Option<OpCreator> {
msg::Any::Accept => Some(op_accept),
msg::Any::Chdir => Some(op_chdir),
msg::Any::Chmod => Some(op_chmod),
+ msg::Any::Chown => Some(op_chown),
msg::Any::Close => Some(op_close),
msg::Any::CopyFile => Some(op_copy_file),
msg::Any::Cwd => Some(op_cwd),
@@ -869,6 +870,30 @@ fn op_chmod(
})
}
+fn op_chown(
+ state: &ThreadSafeState,
+ base: &msg::Base<'_>,
+ data: Option<PinnedBuf>,
+) -> Box<OpWithError> {
+ 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();
+
+ if let Err(e) = state.check_write(&path) {
+ return odd_future(e);
+ }
+
+ blocking(base.sync(), move || {
+ debug!("op_chown {}", &path);
+ match deno_fs::chown(&path, uid, gid) {
+ Ok(_) => Ok(empty_buf()),
+ Err(e) => Err(e),
+ }
+ })
+}
+
fn op_open(
state: &ThreadSafeState,
base: &msg::Base<'_>,