0
0
Fork 0
mirror of https://github.com/denoland/deno.git synced 2025-03-04 01:44:26 -05:00

feat: Deno.chown() make uid, gid args optional (#4612)

This commit is contained in:
dubiousjim 2020-07-06 07:15:13 -04:00 committed by GitHub
parent 79610378d3
commit 6b78729ba8
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
4 changed files with 175 additions and 181 deletions

View file

@ -1080,10 +1080,14 @@ declare namespace Deno {
* Throws Error (not implemented) if executed on Windows * Throws Error (not implemented) if executed on Windows
* *
* @param path path to the file * @param path path to the file
* @param uid user id (UID) of the new owner * @param uid user id (UID) of the new owner, or `null` for no change
* @param gid group id (GID) of the new owner * @param gid group id (GID) of the new owner, or `null` for no change
*/ */
export function chownSync(path: string | URL, uid: number, gid: number): void; export function chownSync(
path: string | URL,
uid: number | null,
gid: number | null
): void;
/** Change owner of a regular file or directory. This functionality /** Change owner of a regular file or directory. This functionality
* is not available on Windows. * is not available on Windows.
@ -1097,13 +1101,13 @@ declare namespace Deno {
* Throws Error (not implemented) if executed on Windows * Throws Error (not implemented) if executed on Windows
* *
* @param path path to the file * @param path path to the file
* @param uid user id (UID) of the new owner * @param uid user id (UID) of the new owner, or `null` for no change
* @param gid group id (GID) of the new owner * @param gid group id (GID) of the new owner, or `null` for no change
*/ */
export function chown( export function chown(
path: string | URL, path: string | URL,
uid: number, uid: number | null,
gid: number gid: number | null
): Promise<void>; ): Promise<void>;
export interface RemoveOptions { export interface RemoveOptions {

View file

@ -2,15 +2,19 @@
import { sendSync, sendAsync } from "../dispatch_json.ts"; import { sendSync, sendAsync } from "../dispatch_json.ts";
import { pathFromURL } from "../../util.ts"; import { pathFromURL } from "../../util.ts";
export function chownSync(path: string | URL, uid: number, gid: number): void { export function chownSync(
path: string | URL,
uid: number | null,
gid: number | null
): void {
path = pathFromURL(path); path = pathFromURL(path);
sendSync("op_chown", { path, uid, gid }); sendSync("op_chown", { path, uid, gid });
} }
export async function chown( export async function chown(
path: string | URL, path: string | URL,
uid: number, uid: number | null,
gid: number gid: number | null
): Promise<void> { ): Promise<void> {
path = pathFromURL(path); path = pathFromURL(path);
await sendAsync("op_chown", { path, uid, gid }); await sendAsync("op_chown", { path, uid, gid });

View file

@ -475,8 +475,8 @@ fn op_chmod(
struct ChownArgs { struct ChownArgs {
promise_id: Option<u64>, promise_id: Option<u64>,
path: String, path: String,
uid: u32, uid: Option<u32>,
gid: u32, gid: Option<u32>,
} }
fn op_chown( fn op_chown(
@ -491,20 +491,18 @@ fn op_chown(
let is_sync = args.promise_id.is_none(); let is_sync = args.promise_id.is_none();
blocking_json(is_sync, move || { blocking_json(is_sync, move || {
debug!("op_chown {} {} {}", path.display(), args.uid, args.gid); debug!("op_chown {} {:?} {:?}", path.display(), args.uid, args.gid,);
#[cfg(unix)] #[cfg(unix)]
{ {
use nix::unistd::{chown, Gid, Uid}; use nix::unistd::{chown, Gid, Uid};
let nix_uid = Uid::from_raw(args.uid); let nix_uid = args.uid.map(Uid::from_raw);
let nix_gid = Gid::from_raw(args.gid); let nix_gid = args.gid.map(Gid::from_raw);
chown(&path, Option::Some(nix_uid), Option::Some(nix_gid))?; chown(&path, nix_uid, nix_gid)?;
Ok(json!({})) Ok(json!({}))
} }
// TODO Implement chown for Windows // TODO Implement chown for Windows
#[cfg(not(unix))] #[cfg(not(unix))]
{ {
// Still check file/dir exists on Windows
let _metadata = std::fs::metadata(&path)?;
Err(OpError::not_implemented()) Err(OpError::not_implemented())
} }
}) })

View file

@ -2,8 +2,8 @@
import { unitTest, assertEquals, assert } from "./test_util.ts"; import { unitTest, assertEquals, assert } from "./test_util.ts";
// chown on Windows is noop for now, so ignore its testing on Windows // chown on Windows is noop for now, so ignore its testing on Windows
if (Deno.build.os !== "windows") {
async function getUidAndGid(): Promise<{ uid: number; gid: number }> { async function getUidAndGid(): Promise<{ uid: number; gid: number }> {
// get the user ID and group ID of the current process // get the user ID and group ID of the current process
const uidProc = Deno.run({ const uidProc = Deno.run({
stdout: "piped", stdout: "piped",
@ -16,29 +16,28 @@ if (Deno.build.os !== "windows") {
assertEquals((await uidProc.status()).code, 0); assertEquals((await uidProc.status()).code, 0);
assertEquals((await gidProc.status()).code, 0); assertEquals((await gidProc.status()).code, 0);
const uid = parseInt( const uid = parseInt(new TextDecoder("utf-8").decode(await uidProc.output()));
new TextDecoder("utf-8").decode(await uidProc.output())
);
uidProc.close(); uidProc.close();
const gid = parseInt( const gid = parseInt(new TextDecoder("utf-8").decode(await gidProc.output()));
new TextDecoder("utf-8").decode(await gidProc.output())
);
gidProc.close(); gidProc.close();
return { uid, gid }; return { uid, gid };
} }
unitTest(async function chownNoWritePermission(): Promise<void> { unitTest(
{ ignore: Deno.build.os == "windows" },
async function chownNoWritePermission(): Promise<void> {
const filePath = "chown_test_file.txt"; const filePath = "chown_test_file.txt";
try { try {
await Deno.chown(filePath, 1000, 1000); await Deno.chown(filePath, 1000, 1000);
} catch (e) { } catch (e) {
assert(e instanceof Deno.errors.PermissionDenied); assert(e instanceof Deno.errors.PermissionDenied);
} }
}); }
);
unitTest( unitTest(
{ perms: { run: true, write: true } }, { perms: { run: true, write: true }, ignore: Deno.build.os == "windows" },
async function chownSyncFileNotExist(): Promise<void> { async function chownSyncFileNotExist(): Promise<void> {
const { uid, gid } = await getUidAndGid(); const { uid, gid } = await getUidAndGid();
const filePath = Deno.makeTempDirSync() + "/chown_test_file.txt"; const filePath = Deno.makeTempDirSync() + "/chown_test_file.txt";
@ -49,10 +48,10 @@ if (Deno.build.os !== "windows") {
assert(e instanceof Deno.errors.NotFound); assert(e instanceof Deno.errors.NotFound);
} }
} }
); );
unitTest( unitTest(
{ perms: { run: true, write: true } }, { perms: { run: true, write: true }, ignore: Deno.build.os == "windows" },
async function chownFileNotExist(): Promise<void> { async function chownFileNotExist(): Promise<void> {
const { uid, gid } = await getUidAndGid(); const { uid, gid } = await getUidAndGid();
const filePath = (await Deno.makeTempDir()) + "/chown_test_file.txt"; const filePath = (await Deno.makeTempDir()) + "/chown_test_file.txt";
@ -63,16 +62,14 @@ if (Deno.build.os !== "windows") {
assert(e instanceof Deno.errors.NotFound); assert(e instanceof Deno.errors.NotFound);
} }
} }
); );
unitTest( unitTest(
{ perms: { write: true } }, { perms: { write: true }, ignore: Deno.build.os == "windows" },
function chownSyncPermissionDenied(): void { function chownSyncPermissionDenied(): void {
const enc = new TextEncoder();
const dirPath = Deno.makeTempDirSync(); const dirPath = Deno.makeTempDirSync();
const filePath = dirPath + "/chown_test_file.txt"; const filePath = dirPath + "/chown_test_file.txt";
const fileData = enc.encode("Hello"); Deno.writeTextFileSync(filePath, "Hello");
Deno.writeFileSync(filePath, fileData);
try { try {
// try changing the file's owner to root // try changing the file's owner to root
@ -82,16 +79,14 @@ if (Deno.build.os !== "windows") {
} }
Deno.removeSync(dirPath, { recursive: true }); Deno.removeSync(dirPath, { recursive: true });
} }
); );
unitTest( unitTest(
{ perms: { write: true } }, { perms: { write: true }, ignore: Deno.build.os == "windows" },
async function chownPermissionDenied(): Promise<void> { async function chownPermissionDenied(): Promise<void> {
const enc = new TextEncoder();
const dirPath = await Deno.makeTempDir(); const dirPath = await Deno.makeTempDir();
const filePath = dirPath + "/chown_test_file.txt"; const filePath = dirPath + "/chown_test_file.txt";
const fileData = enc.encode("Hello"); await Deno.writeTextFile(filePath, "Hello");
await Deno.writeFile(filePath, fileData);
try { try {
// try changing the file's owner to root // try changing the file's owner to root
@ -101,21 +96,19 @@ if (Deno.build.os !== "windows") {
} }
await Deno.remove(dirPath, { recursive: true }); await Deno.remove(dirPath, { recursive: true });
} }
); );
unitTest( unitTest(
{ perms: { run: true, write: true } }, { perms: { run: true, write: true }, ignore: Deno.build.os == "windows" },
async function chownSyncSucceed(): Promise<void> { async function chownSyncSucceed(): Promise<void> {
// TODO: when a file's owner is actually being changed, // TODO: when a file's owner is actually being changed,
// chown only succeeds if run under priviledged user (root) // chown only succeeds if run under priviledged user (root)
// The test script has no such privilege, so need to find a better way to test this case // The test script has no such privilege, so need to find a better way to test this case
const { uid, gid } = await getUidAndGid(); const { uid, gid } = await getUidAndGid();
const enc = new TextEncoder();
const dirPath = Deno.makeTempDirSync(); const dirPath = Deno.makeTempDirSync();
const filePath = dirPath + "/chown_test_file.txt"; const filePath = dirPath + "/chown_test_file.txt";
const fileData = enc.encode("Hello"); Deno.writeTextFileSync(filePath, "Hello");
Deno.writeFileSync(filePath, fileData);
// the test script creates this file with the same uid and gid, // the test script creates this file with the same uid and gid,
// here chown is a noop so it succeeds under non-priviledged user // here chown is a noop so it succeeds under non-priviledged user
@ -123,50 +116,46 @@ if (Deno.build.os !== "windows") {
Deno.removeSync(dirPath, { recursive: true }); Deno.removeSync(dirPath, { recursive: true });
} }
); );
unitTest( unitTest(
{ perms: { run: true, write: true } }, { perms: { run: true, write: true }, ignore: Deno.build.os == "windows" },
async function chownSyncWithUrl(): Promise<void> { async function chownSyncWithUrl(): Promise<void> {
// TODO: same as chownSyncSucceed
const { uid, gid } = await getUidAndGid(); const { uid, gid } = await getUidAndGid();
const enc = new TextEncoder();
const dirPath = Deno.makeTempDirSync(); const dirPath = Deno.makeTempDirSync();
const fileUrl = new URL(`file://${dirPath}/chown_test_file.txt`); const fileUrl = new URL(`file://${dirPath}/chown_test_file.txt`);
const fileData = enc.encode("Hello"); Deno.writeTextFileSync(fileUrl, "Hello");
Deno.writeFileSync(fileUrl, 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(fileUrl, uid, gid); Deno.chownSync(fileUrl, uid, gid);
Deno.removeSync(dirPath, { recursive: true }); Deno.removeSync(dirPath, { recursive: true });
} }
); );
unitTest( unitTest(
{ perms: { run: true, write: true } }, { perms: { run: true, write: true }, ignore: Deno.build.os == "windows" },
async function chownSucceed(): Promise<void> { async function chownSucceed(): Promise<void> {
// TODO: same as chownSyncSucceed
const { uid, gid } = await getUidAndGid(); const { uid, gid } = await getUidAndGid();
const enc = new TextEncoder();
const dirPath = await Deno.makeTempDir(); const dirPath = await Deno.makeTempDir();
const filePath = dirPath + "/chown_test_file.txt"; const filePath = dirPath + "/chown_test_file.txt";
const fileData = enc.encode("Hello"); await Deno.writeTextFile(filePath, "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); await Deno.chown(filePath, uid, gid);
Deno.removeSync(dirPath, { recursive: true }); Deno.removeSync(dirPath, { recursive: true });
} }
); );
unitTest( unitTest(
{ perms: { run: true, write: true } }, { perms: { run: true, write: true }, ignore: Deno.build.os == "windows" },
async function chownUidOnly(): Promise<void> {
const { uid } = await getUidAndGid();
const dirPath = await Deno.makeTempDir();
const filePath = dirPath + "/chown_test_file.txt";
await Deno.writeTextFile(filePath, "Foo");
await Deno.chown(filePath, uid, null);
Deno.removeSync(dirPath, { recursive: true });
}
);
unitTest(
{ perms: { run: true, write: true }, ignore: Deno.build.os == "windows" },
async function chownWithUrl(): Promise<void> { async function chownWithUrl(): Promise<void> {
// TODO: same as chownSyncSucceed // TODO: same as chownSyncSucceed
const { uid, gid } = await getUidAndGid(); const { uid, gid } = await getUidAndGid();
@ -183,5 +172,4 @@ if (Deno.build.os !== "windows") {
Deno.removeSync(dirPath, { recursive: true }); Deno.removeSync(dirPath, { recursive: true });
} }
); );
}