Skip to content

Commit

Permalink
fix permission errors are swallowed by fs.emptyDir (denoland#3501)
Browse files Browse the repository at this point in the history
  • Loading branch information
axetroy authored and ry committed Dec 18, 2019
1 parent 3115781 commit bb24fb7
Show file tree
Hide file tree
Showing 4 changed files with 172 additions and 23 deletions.
65 changes: 42 additions & 23 deletions std/fs/empty_dir.ts
Original file line number Diff line number Diff line change
@@ -1,25 +1,39 @@
// Copyright 2018-2019 the Deno authors. All rights reserved. MIT license.
import { join } from "../path/mod.ts";
const {
readDir,
readDirSync,
mkdir,
mkdirSync,
remove,
removeSync,
ErrorKind
} = Deno;
/**
* Ensures that a directory is empty.
* Deletes directory contents if the directory is not empty.
* If the directory does not exist, it is created.
* The directory itself is not deleted.
* Requires the `--allow-read` and `--alow-write` flag.
*/
export async function emptyDir(dir: string): Promise<void> {
let items: Deno.FileInfo[] = [];
try {
items = await Deno.readDir(dir);
} catch {
// if not exist. then create it
await Deno.mkdir(dir, true);
return;
}
while (items.length) {
const item = items.shift();
if (item && item.name) {
const fn = dir + "/" + item.name;
await Deno.remove(fn, { recursive: true });
const items = await readDir(dir);

while (items.length) {
const item = items.shift();
if (item && item.name) {
const filepath = join(dir, item.name);
await remove(filepath, { recursive: true });
}
}
} catch (err) {
if ((err as Deno.DenoError<Deno.ErrorKind>).kind !== ErrorKind.NotFound) {
throw err;
}

// if not exist. then create it
await mkdir(dir, true);
}
}

Expand All @@ -28,21 +42,26 @@ export async function emptyDir(dir: string): Promise<void> {
* Deletes directory contents if the directory is not empty.
* If the directory does not exist, it is created.
* The directory itself is not deleted.
* Requires the `--allow-read` and `--alow-write` flag.
*/
export function emptyDirSync(dir: string): void {
let items: Deno.FileInfo[] = [];
try {
items = Deno.readDirSync(dir);
} catch {
const items = readDirSync(dir);

// if directory already exist. then remove it's child item.
while (items.length) {
const item = items.shift();
if (item && item.name) {
const filepath = join(dir, item.name);
removeSync(filepath, { recursive: true });
}
}
} catch (err) {
if ((err as Deno.DenoError<Deno.ErrorKind>).kind !== ErrorKind.NotFound) {
throw err;
}
// if not exist. then create it
Deno.mkdirSync(dir, true);
mkdirSync(dir, true);
return;
}
while (items.length) {
const item = items.shift();
if (item && item.name) {
const fn = dir + "/" + item.name;
Deno.removeSync(fn, { recursive: true });
}
}
}
113 changes: 113 additions & 0 deletions std/fs/empty_dir_test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -123,3 +123,116 @@ test(function emptyDirSyncIfItExist(): void {
Deno.removeSync(testDir, { recursive: true });
}
});

test(async function emptyDirPermission(): Promise<void> {
interface Scenes {
read: boolean; // --allow-read
write: boolean; // --allow-write
async: boolean;
output: string;
}

const testfolder = path.join(testdataDir, "testfolder");

await Deno.mkdir(testfolder);

await Deno.writeFile(
path.join(testfolder, "child.txt"),
new TextEncoder().encode("hello world")
);

const scenes: Scenes[] = [
// 1
{
read: false,
write: false,
async: true,
output: "run again with the --allow-read flag"
},
{
read: false,
write: false,
async: false,
output: "run again with the --allow-read flag"
},
// 2
{
read: true,
write: false,
async: true,
output: "run again with the --allow-write flag"
},
{
read: true,
write: false,
async: false,
output: "run again with the --allow-write flag"
},
// 3
{
read: false,
write: true,
async: true,
output: "run again with the --allow-read flag"
},
{
read: false,
write: true,
async: false,
output: "run again with the --allow-read flag"
},
// 4
{
read: true,
write: true,
async: true,
output: "success"
},
{
read: true,
write: true,
async: false,
output: "success"
}
];

try {
for (const s of scenes) {
console.log(
`test ${s.async ? "emptyDir" : "emptyDirSync"}("testdata/testfolder") ${
s.read ? "with" : "without"
} --allow-read & ${s.write ? "with" : "without"} --allow-write`
);

const args = [Deno.execPath(), "run"];

if (s.read) {
args.push("--allow-read");
}

if (s.write) {
args.push("--allow-write");
}

args.push(
path.join(testdataDir, s.async ? "empty_dir.ts" : "empty_dir_sync.ts")
);
args.push("testfolder");

const { stdout } = Deno.run({
stdout: "piped",
cwd: testdataDir,
args: args
});

const output = await Deno.readAll(stdout);

assertEquals(new TextDecoder().decode(output), s.output);
}
} catch (err) {
await Deno.remove(testfolder, { recursive: true });
throw err;
}

// done
});
9 changes: 9 additions & 0 deletions std/fs/testdata/empty_dir.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
import { emptyDir } from "../empty_dir.ts";

emptyDir(Deno.args[1])
.then(() => {
Deno.stdout.write(new TextEncoder().encode("success"))
})
.catch((err) => {
Deno.stdout.write(new TextEncoder().encode(err.message))
})
8 changes: 8 additions & 0 deletions std/fs/testdata/empty_dir_sync.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
import { emptyDirSync } from "../empty_dir.ts";

try {
emptyDirSync(Deno.args[1])
Deno.stdout.write(new TextEncoder().encode("success"))
} catch (err) {
Deno.stdout.write(new TextEncoder().encode(err.message))
}

0 comments on commit bb24fb7

Please sign in to comment.