2020-01-02 15:13:47 -05:00
|
|
|
// Copyright 2018-2020 the Deno authors. All rights reserved. MIT license.
|
2019-09-02 17:07:11 -04:00
|
|
|
import { StatResponse } from "./stat.ts";
|
2020-01-16 09:46:32 -05:00
|
|
|
import { build } from "./build.ts";
|
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`,
|
2020-03-02 10:19:42 -05:00
|
|
|
* `statSync`, `lstatSync`. A list of FileInfo is returned by `readDir`,
|
|
|
|
* `readDirSync`. */
|
2018-10-03 17:56:56 -04:00
|
|
|
export interface FileInfo {
|
|
|
|
/** 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`
|
2020-03-02 10:19:42 -05:00
|
|
|
* field from `stat` on Linux/Mac OS and `ftLastWriteTime` on Windows. This
|
|
|
|
* may not be available on all platforms. */
|
2018-10-03 17:56:56 -04:00
|
|
|
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
|
2020-03-02 10:19:42 -05:00
|
|
|
* be available on all platforms. */
|
2018-10-03 17:56:56 -04:00
|
|
|
accessed: number | null;
|
2018-10-14 16:29:50 -04:00
|
|
|
/** The last access time of the file. This corresponds to the `birthtime`
|
2020-03-02 10:19:42 -05:00
|
|
|
* field from `stat` on Mac/BSD and `ftCreationTime` on Windows. This may not
|
|
|
|
* be available on all platforms. */
|
2018-10-03 17:56:56 -04:00
|
|
|
created: number | null;
|
2020-01-16 09:46:32 -05:00
|
|
|
/** The file or directory name. */
|
|
|
|
name: string | null;
|
2020-03-02 10:19:42 -05:00
|
|
|
/** ID of the device containing the file.
|
|
|
|
*
|
|
|
|
* _Linux/Mac OS only._ */
|
2020-01-16 09:46:32 -05:00
|
|
|
dev: number | null;
|
2020-03-02 10:19:42 -05:00
|
|
|
/** Inode number.
|
|
|
|
*
|
|
|
|
* _Linux/Mac OS only._ */
|
2020-01-16 09:46:32 -05:00
|
|
|
ino: 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;
|
2020-03-02 10:19:42 -05:00
|
|
|
/** Number of hard links pointing to this file.
|
|
|
|
*
|
|
|
|
* _Linux/Mac OS only._ */
|
2020-01-16 09:46:32 -05:00
|
|
|
nlink: number | null;
|
2020-03-02 10:19:42 -05:00
|
|
|
/** User ID of the owner of this file.
|
|
|
|
*
|
|
|
|
* _Linux/Mac OS only._ */
|
2020-01-16 09:46:32 -05:00
|
|
|
uid: number | null;
|
2020-03-02 10:19:42 -05:00
|
|
|
/** User ID of the owner of this file.
|
|
|
|
*
|
|
|
|
* _Linux/Mac OS only._ */
|
2020-01-16 09:46:32 -05:00
|
|
|
gid: number | null;
|
2020-03-02 10:19:42 -05:00
|
|
|
/** Device ID of this file.
|
|
|
|
*
|
|
|
|
* _Linux/Mac OS only._ */
|
2020-01-16 09:46:32 -05:00
|
|
|
rdev: number | null;
|
2020-03-02 10:19:42 -05:00
|
|
|
/** Blocksize for filesystem I/O.
|
|
|
|
*
|
|
|
|
* _Linux/Mac OS only._ */
|
2020-01-16 09:46:32 -05:00
|
|
|
blksize: number | null;
|
2020-03-02 10:19:42 -05:00
|
|
|
/** Number of blocks allocated to the file, in 512-byte units.
|
|
|
|
*
|
|
|
|
* _Linux/Mac OS only._ */
|
2020-01-16 09:46:32 -05:00
|
|
|
blocks: number | null;
|
2018-10-14 16:29:50 -04:00
|
|
|
/** Returns whether this is info for a regular file. This result is mutually
|
2020-03-02 10:19:42 -05:00
|
|
|
* exclusive to `FileInfo.isDirectory` and `FileInfo.isSymlink`. */
|
2018-10-03 17:56:56 -04:00
|
|
|
isFile(): boolean;
|
2018-10-14 16:29:50 -04:00
|
|
|
/** Returns whether this is info for a regular directory. This result is
|
2020-03-02 10:19:42 -05:00
|
|
|
* mutually exclusive to `FileInfo.isFile` and `FileInfo.isSymlink`. */
|
2018-10-03 17:56:56 -04:00
|
|
|
isDirectory(): boolean;
|
2018-10-14 16:29:50 -04:00
|
|
|
/** Returns whether this is info for a symlink. This result is
|
2020-03-02 10:19:42 -05:00
|
|
|
* mutually exclusive to `FileInfo.isFile` and `FileInfo.isDirectory`. */
|
2018-10-03 17:56:56 -04:00
|
|
|
isSymlink(): boolean;
|
|
|
|
}
|
|
|
|
|
2018-10-07 19:33:30 -04:00
|
|
|
// @internal
|
2018-10-03 17:56:56 -04:00
|
|
|
export class FileInfoImpl implements FileInfo {
|
2018-10-22 15:16:15 -04:00
|
|
|
private readonly _isFile: boolean;
|
|
|
|
private readonly _isSymlink: boolean;
|
2018-10-03 17:56:56 -04:00
|
|
|
len: number;
|
|
|
|
modified: number | null;
|
|
|
|
accessed: number | null;
|
|
|
|
created: number | null;
|
|
|
|
name: string | null;
|
|
|
|
|
2020-01-16 09:46:32 -05:00
|
|
|
dev: number | null;
|
|
|
|
ino: number | null;
|
|
|
|
mode: number | null;
|
|
|
|
nlink: number | null;
|
|
|
|
uid: number | null;
|
|
|
|
gid: number | null;
|
|
|
|
rdev: number | null;
|
|
|
|
blksize: number | null;
|
|
|
|
blocks: number | null;
|
|
|
|
|
2018-10-03 17:56:56 -04:00
|
|
|
/* @internal */
|
2019-08-26 10:18:42 -04:00
|
|
|
constructor(private _res: StatResponse) {
|
2020-01-16 09:46:32 -05:00
|
|
|
const isUnix = build.os === "mac" || build.os === "linux";
|
2019-08-26 10:18:42 -04:00
|
|
|
const modified = this._res.modified;
|
|
|
|
const accessed = this._res.accessed;
|
|
|
|
const created = this._res.created;
|
|
|
|
const name = this._res.name;
|
2020-01-16 09:46:32 -05:00
|
|
|
// Unix only
|
|
|
|
const {
|
|
|
|
dev,
|
|
|
|
ino,
|
|
|
|
mode,
|
|
|
|
nlink,
|
|
|
|
uid,
|
|
|
|
gid,
|
|
|
|
rdev,
|
|
|
|
blksize,
|
|
|
|
blocks
|
|
|
|
} = this._res;
|
2018-10-03 17:56:56 -04:00
|
|
|
|
2019-08-26 10:18:42 -04:00
|
|
|
this._isFile = this._res.isFile;
|
|
|
|
this._isSymlink = this._res.isSymlink;
|
|
|
|
this.len = this._res.len;
|
2018-10-03 17:56:56 -04:00
|
|
|
this.modified = modified ? modified : null;
|
|
|
|
this.accessed = accessed ? accessed : null;
|
|
|
|
this.created = created ? created : null;
|
|
|
|
this.name = name ? name : null;
|
2020-01-16 09:46:32 -05:00
|
|
|
// Only non-null if on Unix
|
|
|
|
this.dev = isUnix ? dev : null;
|
|
|
|
this.ino = isUnix ? ino : null;
|
|
|
|
this.mode = isUnix ? mode : null;
|
|
|
|
this.nlink = isUnix ? nlink : null;
|
|
|
|
this.uid = isUnix ? uid : null;
|
|
|
|
this.gid = isUnix ? gid : null;
|
|
|
|
this.rdev = isUnix ? rdev : null;
|
|
|
|
this.blksize = isUnix ? blksize : null;
|
|
|
|
this.blocks = isUnix ? blocks : null;
|
2018-10-03 17:56:56 -04:00
|
|
|
}
|
|
|
|
|
2019-03-09 12:30:38 -05:00
|
|
|
isFile(): boolean {
|
2018-10-03 17:56:56 -04:00
|
|
|
return this._isFile;
|
|
|
|
}
|
|
|
|
|
2019-03-09 12:30:38 -05:00
|
|
|
isDirectory(): boolean {
|
2018-10-03 17:56:56 -04:00
|
|
|
return !this._isFile && !this._isSymlink;
|
|
|
|
}
|
|
|
|
|
2019-03-09 12:30:38 -05:00
|
|
|
isSymlink(): boolean {
|
2018-10-03 17:56:56 -04:00
|
|
|
return this._isSymlink;
|
|
|
|
}
|
|
|
|
}
|