summaryrefslogtreecommitdiff
path: root/cli/js/ops/fs/stat.ts
blob: 6a764a8bb6a38d4370a5a0ab87611b1d37ea187d (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
// Copyright 2018-2020 the Deno authors. All rights reserved. MIT license.
import { sendSync, sendAsync } from "../dispatch_json.ts";
import { FileInfo, FileInfoImpl } from "../../file_info.ts";

/** @internal */
export interface StatResponse {
  isFile: boolean;
  isSymlink: boolean;
  len: number;
  modified: number;
  accessed: number;
  created: number;
  name: string | null;
  // Unix only members
  dev: number;
  ino: number;
  mode: number;
  nlink: number;
  uid: number;
  gid: number;
  rdev: number;
  blksize: number;
  blocks: number;
}

/** Resolves to a `Deno.FileInfo` for the specified `path`. If `path` is a
 * symlink, information for the symlink will be returned.
 *
 *       const fileInfo = await Deno.lstat("hello.txt");
 *       assert(fileInfo.isFile());
 *
 * Requires `allow-read` permission. */
export async function lstat(path: string): Promise<FileInfo> {
  const res = (await sendAsync("op_stat", {
    path,
    lstat: true
  })) as StatResponse;
  return new FileInfoImpl(res);
}

/** Synchronously returns a `Deno.FileInfo` for the specified `path`. If
 * `path` is a symlink, information for the symlink will be returned.
 *
 *       const fileInfo = Deno.lstatSync("hello.txt");
 *       assert(fileInfo.isFile());
 *
 * Requires `allow-read` permission. */
export function lstatSync(path: string): FileInfo {
  const res = sendSync("op_stat", {
    path,
    lstat: true
  }) as StatResponse;
  return new FileInfoImpl(res);
}

/** Resolves to a `Deno.FileInfo` for the specified `path`. Will always
 * follow symlinks.
 *
 *       const fileInfo = await Deno.stat("hello.txt");
 *       assert(fileInfo.isFile());
 *
 * Requires `allow-read` permission. */
export async function stat(path: string): Promise<FileInfo> {
  const res = (await sendAsync("op_stat", {
    path,
    lstat: false
  })) as StatResponse;
  return new FileInfoImpl(res);
}

/** Synchronously returns a `Deno.FileInfo` for the specified `path`. Will
 * always follow symlinks.
 *
 *       const fileInfo = Deno.statSync("hello.txt");
 *       assert(fileInfo.isFile());
 *
 * Requires `allow-read` permission. */
export function statSync(path: string): FileInfo {
  const res = sendSync("op_stat", {
    path,
    lstat: false
  }) as StatResponse;
  return new FileInfoImpl(res);
}