2018-09-11 15:38:53 -04:00
|
|
|
// 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 { assert } from "./util";
|
|
|
|
|
|
|
|
/**
|
|
|
|
* A FileInfo describes a file and is returned by `stat`, `lstat`,
|
|
|
|
* `statSync`, `lstatSync`.
|
|
|
|
*/
|
2018-09-17 15:00:57 -04:00
|
|
|
export interface FileInfo {
|
|
|
|
readonly _isFile: boolean;
|
|
|
|
readonly _isSymlink: boolean;
|
2018-09-11 15:38:53 -04:00
|
|
|
/** The size of the file, in bytes. */
|
|
|
|
len: number;
|
|
|
|
/**
|
|
|
|
* The last modification time of the file. This corresponds to the `mtime`
|
|
|
|
* field from `stat` on Unix and `ftLastWriteTime` on Windows. This may not
|
|
|
|
* be available on all platforms.
|
|
|
|
*/
|
|
|
|
modified: number | null;
|
|
|
|
/**
|
|
|
|
* The last access time of the file. This corresponds to the `atime`
|
|
|
|
* field from `stat` on Unix and `ftLastAccessTime` on Windows. This may not
|
|
|
|
* be available on all platforms.
|
|
|
|
*/
|
|
|
|
accessed: number | null;
|
|
|
|
/**
|
|
|
|
* The last access time of the file. This corresponds to the `birthtime`
|
|
|
|
* field from `stat` on Unix and `ftCreationTime` on Windows. This may not
|
|
|
|
* be available on all platforms.
|
|
|
|
*/
|
|
|
|
created: number | null;
|
2018-09-12 10:28:48 -04:00
|
|
|
/**
|
|
|
|
* The underlying raw st_mode bits that contain the standard Unix permissions
|
|
|
|
* for this file/directory. TODO Match behavior with Go on windows for mode.
|
|
|
|
*/
|
|
|
|
mode: number | null;
|
2018-09-11 15:38:53 -04:00
|
|
|
|
2018-09-17 15:00:57 -04:00
|
|
|
/**
|
|
|
|
* Returns whether this is info for a regular file. This result is mutually
|
|
|
|
* exclusive to `FileInfo.isDirectory` and `FileInfo.isSymlink`.
|
|
|
|
*/
|
|
|
|
isFile(): boolean;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Returns whether this is info for a regular directory. This result is
|
|
|
|
* mutually exclusive to `FileInfo.isFile` and `FileInfo.isSymlink`.
|
|
|
|
*/
|
|
|
|
isDirectory(): boolean;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Returns whether this is info for a symlink. This result is
|
|
|
|
* mutually exclusive to `FileInfo.isFile` and `FileInfo.isDirectory`.
|
|
|
|
*/
|
|
|
|
isSymlink(): boolean;
|
|
|
|
}
|
|
|
|
|
|
|
|
class FileInfoImpl implements FileInfo {
|
|
|
|
readonly _isFile: boolean;
|
|
|
|
readonly _isSymlink: boolean;
|
|
|
|
len: number;
|
|
|
|
modified: number | null;
|
|
|
|
accessed: number | null;
|
|
|
|
created: number | null;
|
|
|
|
mode: number | null;
|
|
|
|
|
2018-09-11 15:38:53 -04:00
|
|
|
/* @internal */
|
|
|
|
constructor(private _msg: fbs.StatRes) {
|
|
|
|
const modified = this._msg.modified().toFloat64();
|
|
|
|
const accessed = this._msg.accessed().toFloat64();
|
|
|
|
const created = this._msg.created().toFloat64();
|
2018-09-17 19:53:55 -04:00
|
|
|
const hasMode = this._msg.hasMode();
|
2018-09-12 10:28:48 -04:00
|
|
|
const mode = this._msg.mode(); // negative for invalid mode (Windows)
|
2018-09-17 23:58:54 -04:00
|
|
|
|
2018-09-11 15:38:53 -04:00
|
|
|
this._isFile = this._msg.isFile();
|
|
|
|
this._isSymlink = this._msg.isSymlink();
|
|
|
|
this.len = this._msg.len().toFloat64();
|
|
|
|
this.modified = modified ? modified : null;
|
|
|
|
this.accessed = accessed ? accessed : null;
|
|
|
|
this.created = created ? created : null;
|
2018-09-17 19:53:55 -04:00
|
|
|
// null on Windows
|
|
|
|
this.mode = hasMode ? mode : null;
|
2018-09-11 15:38:53 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
isFile() {
|
|
|
|
return this._isFile;
|
|
|
|
}
|
|
|
|
|
|
|
|
isDirectory() {
|
|
|
|
return !this._isFile && !this._isSymlink;
|
|
|
|
}
|
|
|
|
|
|
|
|
isSymlink() {
|
|
|
|
return this._isSymlink;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Queries the file system for information on the path provided.
|
|
|
|
* If the given path is a symlink information about the symlink will
|
|
|
|
* be returned.
|
|
|
|
*
|
|
|
|
* import { lstat } from "deno";
|
2018-09-15 10:04:46 -04:00
|
|
|
* const fileInfo = await lstat("hello.txt");
|
2018-09-11 15:38:53 -04:00
|
|
|
* assert(fileInfo.isFile());
|
|
|
|
*/
|
|
|
|
export async function lstat(filename: string): Promise<FileInfo> {
|
|
|
|
return res(await dispatch.sendAsync(...req(filename, true)));
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Queries the file system for information on the path provided synchronously.
|
|
|
|
* If the given path is a symlink information about the symlink will
|
|
|
|
* be returned.
|
|
|
|
*
|
|
|
|
* import { lstatSync } from "deno";
|
2018-09-15 10:04:46 -04:00
|
|
|
* const fileInfo = lstatSync("hello.txt");
|
2018-09-11 15:38:53 -04:00
|
|
|
* assert(fileInfo.isFile());
|
|
|
|
*/
|
|
|
|
export function lstatSync(filename: string): FileInfo {
|
|
|
|
return res(dispatch.sendSync(...req(filename, true)));
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Queries the file system for information on the path provided.
|
|
|
|
* `stat` Will always follow symlinks.
|
|
|
|
*
|
|
|
|
* import { stat } from "deno";
|
2018-09-15 10:04:46 -04:00
|
|
|
* const fileInfo = await stat("hello.txt");
|
2018-09-11 15:38:53 -04:00
|
|
|
* assert(fileInfo.isFile());
|
|
|
|
*/
|
|
|
|
export async function stat(filename: string): Promise<FileInfo> {
|
|
|
|
return res(await dispatch.sendAsync(...req(filename, false)));
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Queries the file system for information on the path provided synchronously.
|
|
|
|
* `statSync` Will always follow symlinks.
|
|
|
|
*
|
|
|
|
* import { statSync } from "deno";
|
2018-09-15 10:04:46 -04:00
|
|
|
* const fileInfo = statSync("hello.txt");
|
2018-09-11 15:38:53 -04:00
|
|
|
* assert(fileInfo.isFile());
|
|
|
|
*/
|
|
|
|
export function statSync(filename: string): FileInfo {
|
|
|
|
return res(dispatch.sendSync(...req(filename, false)));
|
|
|
|
}
|
|
|
|
|
|
|
|
function req(
|
|
|
|
filename: string,
|
|
|
|
lstat: boolean
|
|
|
|
): [flatbuffers.Builder, fbs.Any, flatbuffers.Offset] {
|
|
|
|
const builder = new flatbuffers.Builder();
|
|
|
|
const filename_ = builder.createString(filename);
|
|
|
|
fbs.Stat.startStat(builder);
|
|
|
|
fbs.Stat.addFilename(builder, filename_);
|
|
|
|
fbs.Stat.addLstat(builder, lstat);
|
|
|
|
const msg = fbs.Stat.endStat(builder);
|
|
|
|
return [builder, fbs.Any.Stat, msg];
|
|
|
|
}
|
|
|
|
|
|
|
|
function res(baseRes: null | fbs.Base): FileInfo {
|
|
|
|
assert(baseRes != null);
|
|
|
|
assert(fbs.Any.StatRes === baseRes!.msgType());
|
|
|
|
const res = new fbs.StatRes();
|
|
|
|
assert(baseRes!.msg(res) != null);
|
2018-09-17 15:00:57 -04:00
|
|
|
return new FileInfoImpl(res);
|
2018-09-11 15:38:53 -04:00
|
|
|
}
|