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

Add Deno.chown (#2292)

This commit is contained in:
Yingbo (Max) Wang 2019-05-07 18:58:58 -07:00 committed by Ryan Dahl
parent 1f7ad17152
commit ec9080f34c
9 changed files with 246 additions and 0 deletions

View file

@ -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",

View file

@ -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()),
},

View file

@ -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())
}

View file

@ -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;

View file

@ -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<'_>,

39
js/chown.ts Normal file
View file

@ -0,0 +1,39 @@
// Copyright 2018-2019 the Deno authors. All rights reserved. MIT license.
import * as flatbuffers from "./flatbuffers";
import * as msg from "gen/cli/msg_generated";
import * as dispatch from "./dispatch";
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];
}
/**
* Change owner of a regular file or directory synchronously. Unix only at the moment.
* @param path path to the file
* @param uid user id of the new owner
* @param gid group id of the new owner
*/
export function chownSync(path: string, uid: number, gid: number): void {
dispatch.sendSync(...req(path, uid, gid));
}
/**
* Change owner of a regular file or directory asynchronously. Unix only at the moment.
* @param path path to the file
* @param uid user id of the new owner
* @param gid group id of the new owner
*/
export async function chown(
path: string,
uid: number,
gid: number
): Promise<void> {
await dispatch.sendAsync(...req(path, uid, gid));
}

145
js/chown_test.ts Normal file
View file

@ -0,0 +1,145 @@
// Copyright 2018-2019 the Deno authors. All rights reserved. MIT license.
import { testPerm, assertEquals } from "./test_util.ts";
// chown on Windows is noop for now, so ignore its testing on Windows
if (Deno.build.os !== "win") {
async function getUidAndGid(): Promise<{ uid: number; gid: number }> {
// get the user ID and group ID of the current process
const uidProc = Deno.run({
stdout: "piped",
args: ["python", "-c", "import os; print(os.getuid())"]
});
const gidProc = Deno.run({
stdout: "piped",
args: ["python", "-c", "import os; print(os.getgid())"]
});
assertEquals((await uidProc.status()).code, 0);
assertEquals((await gidProc.status()).code, 0);
const uid = parseInt(
new TextDecoder("utf-8").decode(await uidProc.output())
);
const gid = parseInt(
new TextDecoder("utf-8").decode(await gidProc.output())
);
return { uid, gid };
}
testPerm({}, async function chownNoWritePermission(): Promise<void> {
const filePath = "chown_test_file.txt";
try {
await Deno.chown(filePath, 1000, 1000);
} catch (e) {
assertEquals(e.kind, Deno.ErrorKind.PermissionDenied);
assertEquals(e.name, "PermissionDenied");
}
});
testPerm(
{ run: true, write: true },
async function chownSyncFileNotExist(): Promise<void> {
const { uid, gid } = await getUidAndGid();
const filePath = Deno.makeTempDirSync() + "/chown_test_file.txt";
try {
Deno.chownSync(filePath, uid, gid);
} catch (e) {
assertEquals(e.kind, Deno.ErrorKind.NotFound);
assertEquals(e.name, "NotFound");
}
}
);
testPerm(
{ run: true, write: true },
async function chownFileNotExist(): Promise<void> {
const { uid, gid } = await getUidAndGid();
const filePath = (await Deno.makeTempDir()) + "/chown_test_file.txt";
try {
await Deno.chown(filePath, uid, gid);
} catch (e) {
assertEquals(e.kind, Deno.ErrorKind.NotFound);
assertEquals(e.name, "NotFound");
}
}
);
testPerm({ write: true }, function chownSyncPermissionDenied(): void {
const enc = new TextEncoder();
const dirPath = Deno.makeTempDirSync();
const filePath = dirPath + "/chown_test_file.txt";
const fileData = enc.encode("Hello");
Deno.writeFileSync(filePath, fileData);
try {
// try changing the file's owner to root
Deno.chownSync(filePath, 0, 0);
} catch (e) {
assertEquals(e.kind, Deno.ErrorKind.PermissionDenied);
assertEquals(e.name, "PermissionDenied");
}
Deno.removeSync(dirPath, { recursive: true });
});
testPerm({ write: true }, async function chownPermissionDenied(): Promise<
void
> {
const enc = new TextEncoder();
const dirPath = await Deno.makeTempDir();
const filePath = dirPath + "/chown_test_file.txt";
const fileData = enc.encode("Hello");
await Deno.writeFile(filePath, fileData);
try {
// try changing the file's owner to root
await Deno.chown(filePath, 0, 0);
} catch (e) {
assertEquals(e.kind, Deno.ErrorKind.PermissionDenied);
assertEquals(e.name, "PermissionDenied");
}
await Deno.remove(dirPath, { recursive: true });
});
testPerm(
{ run: true, write: true },
async function chownSyncSucceed(): Promise<void> {
// TODO: when a file's owner is actually being changed,
// chown only succeeds if run under priviledged user (root)
// The test script has no such priviledge, so need to find a better way to test this case
const { uid, gid } = await getUidAndGid();
const enc = new TextEncoder();
const dirPath = Deno.makeTempDirSync();
const filePath = dirPath + "/chown_test_file.txt";
const fileData = enc.encode("Hello");
Deno.writeFileSync(filePath, fileData);
// the test script creates this file with the same uid and gid,
// here chown is a noop so it succeeds under non-priviledged user
Deno.chownSync(filePath, uid, gid);
Deno.removeSync(dirPath, { recursive: true });
}
);
testPerm({ run: true, write: true }, async function chownSucceed(): Promise<
void
> {
// TODO: same as chownSyncSucceed
const { uid, gid } = await getUidAndGid();
const enc = new TextEncoder();
const dirPath = await Deno.makeTempDir();
const filePath = dirPath + "/chown_test_file.txt";
const fileData = enc.encode("Hello");
await Deno.writeFile(filePath, fileData);
// the test script creates this file with the same uid and gid,
// here chown is a noop so it succeeds under non-priviledged user
await Deno.chown(filePath, uid, gid);
Deno.removeSync(dirPath, { recursive: true });
});
}

View file

@ -46,6 +46,7 @@ export {
MakeTempDirOptions
} from "./make_temp_dir";
export { chmodSync, chmod } from "./chmod";
export { chownSync, chown } from "./chown";
export { utimeSync, utime } from "./utime";
export { removeSync, remove, RemoveOption } from "./remove";
export { renameSync, rename } from "./rename";

View file

@ -8,6 +8,7 @@ import "./body_test.ts";
import "./buffer_test.ts";
import "./build_test.ts";
import "./chmod_test.ts";
import "./chown_test.ts";
import "./console_test.ts";
import "./copy_file_test.ts";
import "./custom_event_test.ts";