1
0
Fork 0
mirror of https://github.com/denoland/deno.git synced 2024-11-23 15:16:54 -05:00
denoland-deno/js/file_info.ts
Ryan Dahl c9614d86c1
Move //libdeno to //core/libdeno (#2015)
Fixes some sed errors introduced in c43cfe.

Unfortunately moving libdeno required splitting build.rs into two parts,
one for cli and one for core.

I've also removed the arm64 build - it's complicating things at this
re-org and we're not even testing it. I need to swing back to it and get
tools/test.py running for it.
2019-03-30 14:45:36 -04:00

97 lines
3.1 KiB
TypeScript

// Copyright 2018-2019 the Deno authors. All rights reserved. MIT license.
import * as msg from "gen/cli/msg_generated";
/** A FileInfo describes a file and is returned by `stat`, `lstat`,
* `statSync`, `lstatSync`.
*/
export interface FileInfo {
/** 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;
/** 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;
/** Returns the file or directory name. */
name: string | null;
/** Returns the file or directory path. */
path: string | null;
/** 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;
}
// @internal
export class FileInfoImpl implements FileInfo {
private readonly _isFile: boolean;
private readonly _isSymlink: boolean;
len: number;
modified: number | null;
accessed: number | null;
created: number | null;
mode: number | null;
name: string | null;
path: string | null;
/* @internal */
constructor(private _inner: msg.StatRes) {
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();
this._isFile = this._inner.isFile();
this._isSymlink = this._inner.isSymlink();
this.len = this._inner.len().toFloat64();
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(): boolean {
return this._isFile;
}
isDirectory(): boolean {
return !this._isFile && !this._isSymlink;
}
isSymlink(): boolean {
return this._isSymlink;
}
}