summaryrefslogtreecommitdiff
path: root/js
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 /js
parent1f7ad17152c03b140c997590c897b89fbfea7cea (diff)
Add Deno.chown (#2292)
Diffstat (limited to 'js')
-rw-r--r--js/chown.ts39
-rw-r--r--js/chown_test.ts145
-rw-r--r--js/deno.ts1
-rw-r--r--js/unit_tests.ts1
4 files changed, 186 insertions, 0 deletions
diff --git a/js/chown.ts b/js/chown.ts
new file mode 100644
index 000000000..5c55fdab0
--- /dev/null
+++ b/js/chown.ts
@@ -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));
+}
diff --git a/js/chown_test.ts b/js/chown_test.ts
new file mode 100644
index 000000000..84106d545
--- /dev/null
+++ b/js/chown_test.ts
@@ -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 });
+ });
+}
diff --git a/js/deno.ts b/js/deno.ts
index 0a923ef1d..3275d9353 100644
--- a/js/deno.ts
+++ b/js/deno.ts
@@ -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";
diff --git a/js/unit_tests.ts b/js/unit_tests.ts
index 1fbd1e3cc..2dd2988f6 100644
--- a/js/unit_tests.ts
+++ b/js/unit_tests.ts
@@ -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";