-
Notifications
You must be signed in to change notification settings - Fork 5.3k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Aaron Power
committed
Sep 13, 2018
1 parent
0ca7301
commit b36b49e
Showing
7 changed files
with
219 additions
and
11 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,55 @@ | ||
// Copyright 2018 the Deno authors. All rights reserved. MIT license. | ||
import * as fbs from "gen/msg_generated"; | ||
import { flatbuffers } from "flatbuffers"; | ||
import * as dispatch from "./dispatch"; | ||
import { FileInfo } from "./stat"; | ||
import { assert } from "./util"; | ||
|
||
/** | ||
* Queries the file system for information on the path provided. | ||
* `stat` Will always follow symlinks. | ||
* | ||
* import { stat } from "deno"; | ||
* const fileInfo = await deno.stat("hello.txt"); | ||
* assert(fileInfo.isFile()); | ||
*/ | ||
export async function readDir(filename: string): Promise<FileInfo[]> { | ||
return res(await dispatch.sendAsync(...req(filename))); | ||
} | ||
|
||
/** | ||
* Queries the file system for information on the path provided synchronously. | ||
* `statSync` Will always follow symlinks. | ||
* | ||
* import { statSync } from "deno"; | ||
* const fileInfo = deno.statSync("hello.txt"); | ||
* assert(fileInfo.isFile()); | ||
*/ | ||
export function readDirSync(filename: string): FileInfo[] { | ||
return res(dispatch.sendSync(...req(filename))); | ||
} | ||
|
||
function req( | ||
filename: string, | ||
): [flatbuffers.Builder, fbs.Any, flatbuffers.Offset] { | ||
const builder = new flatbuffers.Builder(); | ||
const filename_ = builder.createString(filename); | ||
fbs.ReadDir.startReadDir(builder); | ||
fbs.ReadDir.addFilename(builder, filename_); | ||
const msg = fbs.ReadDir.endReadDir(builder); | ||
return [builder, fbs.Any.ReadDir, msg]; | ||
} | ||
|
||
function res(baseRes: null | fbs.Base): FileInfo[] { | ||
assert(baseRes != null); | ||
assert(fbs.Any.ReadDirRes === baseRes!.msgType()); | ||
const res = new fbs.ReadDirRes(); | ||
assert(baseRes!.msg(res) != null); | ||
const fileInfos: FileInfo[] = []; | ||
|
||
for (let i = 0; i < res.entriesLength(); i++) { | ||
fileInfos.push(new FileInfo(res.entries(i)!)); | ||
} | ||
|
||
return fileInfos; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,49 @@ | ||
// Copyright 2018 the Deno authors. All rights reserved. MIT license. | ||
import { test, testPerm, assert, assertEqual } from "./test_util.ts"; | ||
import * as deno from "deno"; | ||
|
||
testPerm({ write: true }, async function readDirSuccess() { | ||
const dirName = deno.makeTempDirSync(); | ||
const enc = new TextEncoder(); | ||
const data = enc.encode("Hello"); | ||
deno.writeFileSync(`${dirName}/test.txt`, data, 0o666); | ||
deno.writeFileSync(`${dirName}/test.rs`, data, 0o666); | ||
|
||
const entries = deno.readDirSync(dirName); | ||
assertEqual(entries.length, 2); | ||
|
||
for (const entry of entries) { | ||
assert(entry.isFile()); | ||
assert(entry.name === "test.txt" || entry.name === "test.rs"); | ||
assert( | ||
entry.path === `${dirName}/test.txt` || | ||
entry.path === `${dirName}/test.rs` | ||
); | ||
} | ||
}); | ||
|
||
test(async function readDirSyncNotADir() { | ||
let caughtError = false; | ||
|
||
try { | ||
const src = deno.readDirSync("Cargo.toml"); | ||
} catch (err) { | ||
caughtError = true; | ||
assertEqual(err.kind, deno.ErrorKind.Other); | ||
} | ||
|
||
assert(caughtError); | ||
}); | ||
|
||
test(async function readDirSyncNotFound() { | ||
let caughtError = false; | ||
|
||
try { | ||
const src = deno.readDirSync("bad_dir_name"); | ||
} catch (err) { | ||
caughtError = true; | ||
assertEqual(err.kind, deno.ErrorKind.NotFound); | ||
} | ||
|
||
assert(caughtError); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters