-
Notifications
You must be signed in to change notification settings - Fork 576
/
_fs_chmod.ts
40 lines (33 loc) · 1.19 KB
/
_fs_chmod.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
// Copyright 2018-2021 the Deno authors. All rights reserved. MIT license.
import type { CallbackWithError } from "./_fs_common.ts";
import { fromFileUrl } from "../path.ts";
const allowedModes = /^[0-7]{3}/;
/**
* TODO: Also accept 'path' parameter as a Node polyfill Buffer type once these
* are implemented. See https://github.com/denoland/deno/issues/3403
*/
export function chmod(
path: string | URL,
mode: string | number,
callback: CallbackWithError,
): void {
path = path instanceof URL ? fromFileUrl(path) : path;
Deno.chmod(path, getResolvedMode(mode)).then(() => callback(null), callback);
}
/**
* TODO: Also accept 'path' parameter as a Node polyfill Buffer type once these
* are implemented. See https://github.com/denoland/deno/issues/3403
*/
export function chmodSync(path: string | URL, mode: string | number): void {
path = path instanceof URL ? fromFileUrl(path) : path;
Deno.chmodSync(path, getResolvedMode(mode));
}
function getResolvedMode(mode: string | number): number {
if (typeof mode === "number") {
return mode;
}
if (typeof mode === "string" && !allowedModes.test(mode)) {
throw new Error("Unrecognized mode: " + mode);
}
return parseInt(mode, 8);
}