1
0
Fork 0
mirror of https://github.com/denoland/deno.git synced 2024-11-25 15:29:32 -05:00

feat(runtime): add stat and statSync methods to Deno.File (#10107)

This commit is contained in:
Casper Beyer 2021-04-12 19:33:05 +08:00 committed by GitHub
parent 5c2a8cdbdc
commit da9219341f
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
4 changed files with 45 additions and 2 deletions

View file

@ -785,6 +785,8 @@ declare namespace Deno {
readSync(p: Uint8Array): number | null;
seek(offset: number, whence: SeekMode): Promise<number>;
seekSync(offset: number, whence: SeekMode): number;
stat(): Promise<FileInfo>;
statSync(): FileInfo;
close(): void;
}

View file

@ -103,3 +103,35 @@ unitTest(function fileUsingNumberFileName(): void {
unitTest(function fileUsingEmptyStringFileName(): void {
testSecondArgument("", "");
});
unitTest({ perms: { read: true } }, function fileStatSyncSuccess(): void {
const file = Deno.openSync("README.md");
const fileInfo = file.statSync();
assert(fileInfo.isFile);
assert(!fileInfo.isSymlink);
assert(!fileInfo.isDirectory);
assert(fileInfo.size);
assert(fileInfo.atime);
assert(fileInfo.mtime);
// The `birthtime` field is not available on Linux before kernel version 4.11.
assert(fileInfo.birthtime || Deno.build.os === "linux");
file.close();
});
unitTest({ perms: { read: true } }, async function fileStatSuccess(): Promise<
void
> {
const file = await Deno.open("README.md");
const fileInfo = await file.stat();
assert(fileInfo.isFile);
assert(!fileInfo.isSymlink);
assert(!fileInfo.isDirectory);
assert(fileInfo.size);
assert(fileInfo.atime);
assert(fileInfo.mtime);
// The `birthtime` field is not available on Linux before kernel version 4.11.
assert(fileInfo.birthtime || Deno.build.os === "linux");
file.close();
});

View file

@ -390,8 +390,6 @@
removeSync,
renameSync,
rename,
fstatSync,
fstat,
lstat,
lstatSync,
stat,
@ -403,6 +401,8 @@
umask,
link,
linkSync,
fstatSync,
fstat,
futime,
futimeSync,
utime,

View file

@ -4,6 +4,7 @@
((window) => {
const core = window.Deno.core;
const { read, readSync, write, writeSync } = window.__bootstrap.io;
const { fstat, fstatSync } = window.__bootstrap.fs;
const { pathFromURL } = window.__bootstrap.util;
function seekSync(
@ -103,6 +104,14 @@
return seekSync(this.rid, offset, whence);
}
stat() {
return fstat(this.rid);
}
statSync() {
return fstatSync(this.rid);
}
close() {
core.close(this.rid);
}