1
0
Fork 0
mirror of https://github.com/denoland/deno.git synced 2024-11-30 16:40:57 -05:00

chmod should throw on Windows (#4446)

This commit is contained in:
dubiousjim 2020-03-20 16:03:04 -04:00 committed by GitHub
parent b22f48970f
commit 77a44163fb
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
4 changed files with 23 additions and 23 deletions

View file

@ -950,7 +950,7 @@ declare namespace Deno {
* *
* For a full description, see [chmod](#chmod) * For a full description, see [chmod](#chmod)
* *
* NOTE: This API currently has no effect on Windows * NOTE: This API currently throws on Windows
* *
* Requires `allow-write` permission. */ * Requires `allow-write` permission. */
export function chmodSync(path: string, mode: number): void; export function chmodSync(path: string, mode: number): void;
@ -978,7 +978,7 @@ declare namespace Deno {
* | 1 | execute only | * | 1 | execute only |
* | 0 | no permission | * | 0 | no permission |
* *
* NOTE: This API currently has no effect on Windows * NOTE: This API currently throws on Windows
* *
* Requires `allow-write` permission. */ * Requires `allow-write` permission. */
export function chmod(path: string, mode: number): Promise<void>; export function chmod(path: string, mode: number): Promise<void>;

View file

@ -1,10 +1,8 @@
// Copyright 2018-2020 the Deno authors. All rights reserved. MIT license. // Copyright 2018-2020 the Deno authors. All rights reserved. MIT license.
import { unitTest, assert, assertEquals } from "./test_util.ts"; import { unitTest, assert, assertEquals } from "./test_util.ts";
const isNotWindows = Deno.build.os !== "win";
unitTest( unitTest(
{ perms: { read: true, write: true } }, { ignore: Deno.build.os === "win", perms: { read: true, write: true } },
function chmodSyncSuccess(): void { function chmodSyncSuccess(): void {
const enc = new TextEncoder(); const enc = new TextEncoder();
const data = enc.encode("Hello"); const data = enc.encode("Hello");
@ -12,16 +10,12 @@ unitTest(
const filename = tempDir + "/test.txt"; const filename = tempDir + "/test.txt";
Deno.writeFileSync(filename, data, { mode: 0o666 }); Deno.writeFileSync(filename, data, { mode: 0o666 });
// On windows no effect, but should not crash
Deno.chmodSync(filename, 0o777); Deno.chmodSync(filename, 0o777);
// Check success when not on windows
if (isNotWindows) {
const fileInfo = Deno.statSync(filename); const fileInfo = Deno.statSync(filename);
assert(fileInfo.mode); assert(fileInfo.mode);
assertEquals(fileInfo.mode & 0o777, 0o777); assertEquals(fileInfo.mode & 0o777, 0o777);
} }
}
); );
// Check symlink when not on windows // Check symlink when not on windows
@ -79,7 +73,7 @@ unitTest({ perms: { write: false } }, function chmodSyncPerm(): void {
}); });
unitTest( unitTest(
{ perms: { read: true, write: true } }, { ignore: Deno.build.os === "win", perms: { read: true, write: true } },
async function chmodSuccess(): Promise<void> { async function chmodSuccess(): Promise<void> {
const enc = new TextEncoder(); const enc = new TextEncoder();
const data = enc.encode("Hello"); const data = enc.encode("Hello");
@ -87,16 +81,12 @@ unitTest(
const filename = tempDir + "/test.txt"; const filename = tempDir + "/test.txt";
Deno.writeFileSync(filename, data, { mode: 0o666 }); Deno.writeFileSync(filename, data, { mode: 0o666 });
// On windows no effect, but should not crash
await Deno.chmod(filename, 0o777); await Deno.chmod(filename, 0o777);
// Check success when not on windows
if (isNotWindows) {
const fileInfo = Deno.statSync(filename); const fileInfo = Deno.statSync(filename);
assert(fileInfo.mode); assert(fileInfo.mode);
assertEquals(fileInfo.mode & 0o777, 0o777); assertEquals(fileInfo.mode & 0o777, 0o777);
} }
}
); );
// Check symlink when not on windows // Check symlink when not on windows

View file

@ -3,6 +3,7 @@ import { stat, statSync } from "./ops/fs/stat.ts";
import { open, openSync } from "./files.ts"; import { open, openSync } from "./files.ts";
import { chmod, chmodSync } from "./ops/fs/chmod.ts"; import { chmod, chmodSync } from "./ops/fs/chmod.ts";
import { writeAll, writeAllSync } from "./buffer.ts"; import { writeAll, writeAllSync } from "./buffer.ts";
import { build } from "./build.ts";
export interface WriteFileOptions { export interface WriteFileOptions {
append?: boolean; append?: boolean;
@ -26,7 +27,11 @@ export function writeFileSync(
const openMode = !!options.append ? "a" : "w"; const openMode = !!options.append ? "a" : "w";
const file = openSync(path, openMode); const file = openSync(path, openMode);
if (options.mode !== undefined && options.mode !== null) { if (
options.mode !== undefined &&
options.mode !== null &&
build.os !== "win"
) {
chmodSync(path, options.mode); chmodSync(path, options.mode);
} }
@ -50,7 +55,11 @@ export async function writeFile(
const openMode = !!options.append ? "a" : "w"; const openMode = !!options.append ? "a" : "w";
const file = await open(path, openMode); const file = await open(path, openMode);
if (options.mode !== undefined && options.mode !== null) { if (
options.mode !== undefined &&
options.mode !== null &&
build.os !== "win"
) {
await chmod(path, options.mode); await chmod(path, options.mode);
} }

View file

@ -353,14 +353,15 @@ fn op_chmod(
use std::os::unix::fs::PermissionsExt; use std::os::unix::fs::PermissionsExt;
let permissions = PermissionsExt::from_mode(mode); let permissions = PermissionsExt::from_mode(mode);
std::fs::set_permissions(&path, permissions)?; std::fs::set_permissions(&path, permissions)?;
Ok(json!({}))
} }
// TODO Implement chmod for Windows (#4357) // TODO Implement chmod for Windows (#4357)
#[cfg(not(unix))] #[cfg(not(unix))]
{ {
// Still check file/dir exists on Windows // Still check file/dir exists on Windows
let _metadata = std::fs::metadata(&path)?; let _metadata = std::fs::metadata(&path)?;
return Err(OpError::not_implemented());
} }
Ok(json!({}))
}) })
} }