diff options
author | dubiousjim <dubiousjim@gmail.com> | 2020-03-16 15:02:41 -0400 |
---|---|---|
committer | GitHub <noreply@github.com> | 2020-03-16 15:02:41 -0400 |
commit | f9557a4ff6b73a4af37e713bb6b2294253c7b230 (patch) | |
tree | 0a4e05e6fb709e1c4ebb4bc21c2b50685049e6f4 /cli/js | |
parent | 8077ade7413db72572b8685c883f078335e0561b (diff) |
Add mode option to open/create (#4289)
Diffstat (limited to 'cli/js')
-rw-r--r-- | cli/js/files.ts | 22 | ||||
-rw-r--r-- | cli/js/lib.deno.ns.d.ts | 18 | ||||
-rw-r--r-- | cli/js/ops/fs/open.ts | 14 | ||||
-rw-r--r-- | cli/js/tests/files_test.ts | 38 |
4 files changed, 73 insertions, 19 deletions
diff --git a/cli/js/files.ts b/cli/js/files.ts index b9da1363f..99ae19879 100644 --- a/cli/js/files.ts +++ b/cli/js/files.ts @@ -22,43 +22,47 @@ import { } from "./ops/fs/open.ts"; export { OpenOptions, OpenMode } from "./ops/fs/open.ts"; -export function openSync(path: string, mode?: OpenOptions): File; -export function openSync(path: string, mode?: OpenMode): File; +export function openSync(path: string, options?: OpenOptions): File; +export function openSync(path: string, openMode?: OpenMode): File; + +/**@internal*/ export function openSync( path: string, modeOrOptions: OpenOptions | OpenMode = "r" ): File { - let mode = undefined; + let openMode = undefined; let options = undefined; if (typeof modeOrOptions === "string") { - mode = modeOrOptions; + openMode = modeOrOptions; } else { checkOpenOptions(modeOrOptions); options = modeOrOptions as OpenOptions; } - const rid = opOpenSync(path, mode as OpenMode, options); + const rid = opOpenSync(path, openMode as OpenMode, options); return new File(rid); } export async function open(path: string, options?: OpenOptions): Promise<File>; -export async function open(path: string, mode?: OpenMode): Promise<File>; +export async function open(path: string, openMode?: OpenMode): Promise<File>; + +/**@internal*/ export async function open( path: string, modeOrOptions: OpenOptions | OpenMode = "r" ): Promise<File> { - let mode = undefined; + let openMode = undefined; let options = undefined; if (typeof modeOrOptions === "string") { - mode = modeOrOptions; + openMode = modeOrOptions; } else { checkOpenOptions(modeOrOptions); options = modeOrOptions as OpenOptions; } - const rid = await opOpen(path, mode as OpenMode, options); + const rid = await opOpen(path, openMode as OpenMode, options); return new File(rid); } diff --git a/cli/js/lib.deno.ns.d.ts b/cli/js/lib.deno.ns.d.ts index e700c286e..6453b4ab8 100644 --- a/cli/js/lib.deno.ns.d.ts +++ b/cli/js/lib.deno.ns.d.ts @@ -515,7 +515,7 @@ declare namespace Deno { * * const file = Deno.openSync("/foo/bar.txt", { read: true, write: true }); * - * Requires `allow-read` and `allow-write` permissions depending on mode. + * Requires `allow-read` and `allow-write` permissions depending on openMode. */ export function openSync(path: string, options?: OpenOptions): File; @@ -523,15 +523,15 @@ declare namespace Deno { * * const file = Deno.openSync("/foo/bar.txt", "r"); * - * Requires `allow-read` and `allow-write` permissions depending on mode. + * Requires `allow-read` and `allow-write` permissions depending on openMode. */ - export function openSync(path: string, mode?: OpenMode): File; + export function openSync(path: string, openMode?: OpenMode): File; /** Open a file and resolve to an instance of the `File` object. * * const file = await Deno.open("/foo/bar.txt", { read: true, write: true }); * - * Requires `allow-read` and `allow-write` permissions depending on mode. + * Requires `allow-read` and `allow-write` permissions depending on openMode. */ export function open(path: string, options?: OpenOptions): Promise<File>; @@ -539,9 +539,9 @@ declare namespace Deno { * * const file = await Deno.open("/foo/bar.txt, "w+"); * - * Requires `allow-read` and `allow-write` permissions depending on mode. + * Requires `allow-read` and `allow-write` permissions depending on openMode. */ - export function open(path: string, mode?: OpenMode): Promise<File>; + export function open(path: string, openMode?: OpenMode): Promise<File>; /** Creates a file if none exists or truncates an existing file and returns * an instance of `Deno.File`. @@ -686,9 +686,13 @@ declare namespace Deno { * access to be used. When createNew is set to `true`, create and truncate * are ignored. */ createNew?: boolean; + /** Permissions to use if creating the file (defaults to `0o666`, before + * the process's umask). + * Ignored on Windows. */ + mode?: number; } - /** A set of string literals which specify the open mode of a file. + /** A set of string literals which specify how to open a file. * * |Value |Description | * |------|--------------------------------------------------------------------------------------------------| diff --git a/cli/js/ops/fs/open.ts b/cli/js/ops/fs/open.ts index 4c9281909..2ac5c715c 100644 --- a/cli/js/ops/fs/open.ts +++ b/cli/js/ops/fs/open.ts @@ -8,26 +8,34 @@ export interface OpenOptions { truncate?: boolean; create?: boolean; createNew?: boolean; + /** Permissions to use if creating the file (defaults to `0o666`, before + * the process's umask). + * It's an error to specify mode without also setting create or createNew to `true`. + * Ignored on Windows. */ + mode?: number; } export type OpenMode = "r" | "r+" | "w" | "w+" | "a" | "a+" | "x" | "x+"; export function openSync( path: string, - mode: OpenMode | undefined, + openMode: OpenMode | undefined, options: OpenOptions | undefined ): number { - return sendSync("op_open", { path, options, mode }); + const mode: number | undefined = options?.mode; + return sendSync("op_open", { path, options, openMode, mode }); } export async function open( path: string, - mode: OpenMode | undefined, + openMode: OpenMode | undefined, options: OpenOptions | undefined ): Promise<number> { + const mode: number | undefined = options?.mode; return sendAsync("op_open", { path, options, + openMode, mode }); } diff --git a/cli/js/tests/files_test.ts b/cli/js/tests/files_test.ts index 29e2812d4..b087d7398 100644 --- a/cli/js/tests/files_test.ts +++ b/cli/js/tests/files_test.ts @@ -75,6 +75,44 @@ unitTest(async function readerToAsyncIterator(): Promise<void> { }); unitTest( + { + perms: { read: true, write: true } + }, + function openSyncMode(): void { + const path = Deno.makeTempDirSync() + "/test_openSync.txt"; + const file = Deno.openSync(path, { + write: true, + createNew: true, + mode: 0o626 + }); + file.close(); + const pathInfo = Deno.statSync(path); + if (Deno.build.os !== "win") { + assertEquals(pathInfo.mode! & 0o777, 0o626 & ~Deno.umask()); + } + } +); + +unitTest( + { + perms: { read: true, write: true } + }, + async function openMode(): Promise<void> { + const path = (await Deno.makeTempDir()) + "/test_open.txt"; + const file = await Deno.open(path, { + write: true, + createNew: true, + mode: 0o626 + }); + file.close(); + const pathInfo = Deno.statSync(path); + if (Deno.build.os !== "win") { + assertEquals(pathInfo.mode! & 0o777, 0o626 & ~Deno.umask()); + } + } +); + +unitTest( { perms: { write: false } }, async function writePermFailure(): Promise<void> { const filename = "tests/hello.txt"; |