2018-10-03 17:56:56 -04:00
|
|
|
// Copyright 2018 the Deno authors. All rights reserved. MIT license.
|
2018-10-03 21:18:23 -04:00
|
|
|
import * as msg from "gen/msg_generated";
|
2018-10-03 17:56:56 -04:00
|
|
|
|
2018-10-14 16:29:50 -04:00
|
|
|
/** A FileInfo describes a file and is returned by `stat`, `lstat`,
|
2018-10-03 17:56:56 -04:00
|
|
|
* `statSync`, `lstatSync`.
|
|
|
|
*/
|
|
|
|
export interface FileInfo {
|
|
|
|
readonly _isFile: boolean;
|
|
|
|
readonly _isSymlink: boolean;
|
|
|
|
/** The size of the file, in bytes. */
|
|
|
|
len: number;
|
2018-10-14 16:29:50 -04:00
|
|
|
/** The last modification time of the file. This corresponds to the `mtime`
|
2018-10-03 17:56:56 -04:00
|
|
|
* field from `stat` on Unix and `ftLastWriteTime` on Windows. This may not
|
|
|
|
* be available on all platforms.
|
|
|
|
*/
|
|
|
|
modified: number | null;
|
2018-10-14 16:29:50 -04:00
|
|
|
/** The last access time of the file. This corresponds to the `atime`
|
2018-10-03 17:56:56 -04:00
|
|
|
* field from `stat` on Unix and `ftLastAccessTime` on Windows. This may not
|
|
|
|
* be available on all platforms.
|
|
|
|
*/
|
|
|
|
accessed: number | null;
|
2018-10-14 16:29:50 -04:00
|
|
|
/** The last access time of the file. This corresponds to the `birthtime`
|
2018-10-03 17:56:56 -04:00
|
|
|
* field from `stat` on Unix and `ftCreationTime` on Windows. This may not
|
|
|
|
* be available on all platforms.
|
|
|
|
*/
|
|
|
|
created: number | null;
|
2018-10-14 16:29:50 -04:00
|
|
|
/** The underlying raw st_mode bits that contain the standard Unix permissions
|
2018-10-03 17:56:56 -04:00
|
|
|
* for this file/directory. TODO Match behavior with Go on windows for mode.
|
|
|
|
*/
|
|
|
|
mode: number | null;
|
|
|
|
|
2018-10-14 16:29:50 -04:00
|
|
|
/** Returns the file or directory name. */
|
2018-10-03 17:56:56 -04:00
|
|
|
name: string | null;
|
|
|
|
|
2018-10-14 16:29:50 -04:00
|
|
|
/** Returns the file or directory path. */
|
2018-10-03 17:56:56 -04:00
|
|
|
path: string | null;
|
|
|
|
|
2018-10-14 16:29:50 -04:00
|
|
|
/** Returns whether this is info for a regular file. This result is mutually
|
2018-10-03 17:56:56 -04:00
|
|
|
* exclusive to `FileInfo.isDirectory` and `FileInfo.isSymlink`.
|
|
|
|
*/
|
|
|
|
isFile(): boolean;
|
|
|
|
|
2018-10-14 16:29:50 -04:00
|
|
|
/** Returns whether this is info for a regular directory. This result is
|
2018-10-03 17:56:56 -04:00
|
|
|
* mutually exclusive to `FileInfo.isFile` and `FileInfo.isSymlink`.
|
|
|
|
*/
|
|
|
|
isDirectory(): boolean;
|
|
|
|
|
2018-10-14 16:29:50 -04:00
|
|
|
/** Returns whether this is info for a symlink. This result is
|
2018-10-03 17:56:56 -04:00
|
|
|
* mutually exclusive to `FileInfo.isFile` and `FileInfo.isDirectory`.
|
|
|
|
*/
|
|
|
|
isSymlink(): boolean;
|
|
|
|
}
|
|
|
|
|
2018-10-07 19:33:30 -04:00
|
|
|
// @internal
|
2018-10-03 17:56:56 -04:00
|
|
|
export class FileInfoImpl implements FileInfo {
|
|
|
|
readonly _isFile: boolean;
|
|
|
|
readonly _isSymlink: boolean;
|
|
|
|
len: number;
|
|
|
|
modified: number | null;
|
|
|
|
accessed: number | null;
|
|
|
|
created: number | null;
|
|
|
|
mode: number | null;
|
|
|
|
name: string | null;
|
|
|
|
path: string | null;
|
|
|
|
|
|
|
|
/* @internal */
|
2018-10-03 21:18:23 -04:00
|
|
|
constructor(private _inner: msg.StatRes) {
|
2018-10-03 21:12:23 -04:00
|
|
|
const modified = this._inner.modified().toFloat64();
|
|
|
|
const accessed = this._inner.accessed().toFloat64();
|
|
|
|
const created = this._inner.created().toFloat64();
|
|
|
|
const hasMode = this._inner.hasMode();
|
|
|
|
const mode = this._inner.mode(); // negative for invalid mode (Windows)
|
|
|
|
const name = this._inner.name();
|
|
|
|
const path = this._inner.path();
|
2018-10-03 17:56:56 -04:00
|
|
|
|
2018-10-03 21:12:23 -04:00
|
|
|
this._isFile = this._inner.isFile();
|
|
|
|
this._isSymlink = this._inner.isSymlink();
|
|
|
|
this.len = this._inner.len().toFloat64();
|
2018-10-03 17:56:56 -04:00
|
|
|
this.modified = modified ? modified : null;
|
|
|
|
this.accessed = accessed ? accessed : null;
|
|
|
|
this.created = created ? created : null;
|
|
|
|
// null on Windows
|
|
|
|
this.mode = hasMode ? mode : null;
|
|
|
|
this.name = name ? name : null;
|
|
|
|
this.path = path ? path : null;
|
|
|
|
}
|
|
|
|
|
|
|
|
isFile() {
|
|
|
|
return this._isFile;
|
|
|
|
}
|
|
|
|
|
|
|
|
isDirectory() {
|
|
|
|
return !this._isFile && !this._isSymlink;
|
|
|
|
}
|
|
|
|
|
|
|
|
isSymlink() {
|
|
|
|
return this._isSymlink;
|
|
|
|
}
|
|
|
|
}
|