2020-03-14 17:46:39 -04:00
|
|
|
// Copyright 2018-2020 the Deno authors. All rights reserved. MIT license.
|
|
|
|
|
2020-07-08 05:26:39 -04:00
|
|
|
import type { CallbackWithError } from "./_fs_common.ts";
|
2020-05-03 15:14:38 -04:00
|
|
|
import { fromFileUrl } from "../path.ts";
|
2020-03-14 17:46:39 -04:00
|
|
|
|
|
|
|
const allowedModes = /^[0-7]{3}/;
|
|
|
|
|
|
|
|
/**
|
2020-05-03 15:14:38 -04:00
|
|
|
* TODO: Also accept 'path' parameter as a Node polyfill Buffer type once these
|
2020-03-14 17:46:39 -04:00
|
|
|
* are implemented. See https://github.com/denoland/deno/issues/3403
|
|
|
|
*/
|
|
|
|
export function chmod(
|
2020-05-03 15:14:38 -04:00
|
|
|
path: string | URL,
|
2020-03-14 17:46:39 -04:00
|
|
|
mode: string | number,
|
2020-07-14 15:24:17 -04:00
|
|
|
callback: CallbackWithError,
|
2020-03-14 17:46:39 -04:00
|
|
|
): void {
|
2020-05-03 15:14:38 -04:00
|
|
|
path = path instanceof URL ? fromFileUrl(path) : path;
|
|
|
|
|
2020-04-20 05:29:37 -04:00
|
|
|
Deno.chmod(path, getResolvedMode(mode))
|
|
|
|
.then(() => callback())
|
|
|
|
.catch(callback);
|
2020-03-14 17:46:39 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2020-05-03 15:14:38 -04:00
|
|
|
* TODO: Also accept 'path' parameter as a Node polyfill Buffer type once these
|
2020-03-14 17:46:39 -04:00
|
|
|
* are implemented. See https://github.com/denoland/deno/issues/3403
|
|
|
|
*/
|
2020-05-03 15:14:38 -04:00
|
|
|
export function chmodSync(path: string | URL, mode: string | number): void {
|
|
|
|
path = path instanceof URL ? fromFileUrl(path) : path;
|
2020-03-14 17:46:39 -04:00
|
|
|
Deno.chmodSync(path, getResolvedMode(mode));
|
|
|
|
}
|
|
|
|
|
|
|
|
function getResolvedMode(mode: string | number): number {
|
|
|
|
if (typeof mode === "number") {
|
|
|
|
return mode;
|
|
|
|
}
|
|
|
|
|
2020-04-29 21:36:44 -04:00
|
|
|
if (typeof mode === "string" && !allowedModes.test(mode)) {
|
|
|
|
throw new Error("Unrecognized mode: " + mode);
|
2020-03-14 17:46:39 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
return parseInt(mode, 8);
|
|
|
|
}
|