summaryrefslogtreecommitdiff
path: root/ext/node/polyfills/_fs/_fs_statfs.js
blob: 51da1ed684fd499218d19e075296cdea8d88300e (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
// Copyright 2018-2024 the Deno authors. All rights reserved. MIT license.

import { BigInt } from "ext:deno_node/internal/primordials.mjs";
import { op_node_statfs } from "ext:core/ops";
import { promisify } from "ext:deno_node/internal/util.mjs";

class StatFs {
  type;
  bsize;
  blocks;
  bfree;
  bavail;
  files;
  ffree;
  constructor(type, bsize, blocks, bfree, bavail, files, ffree) {
    this.type = type;
    this.bsize = bsize;
    this.blocks = blocks;
    this.bfree = bfree;
    this.bavail = bavail;
    this.files = files;
    this.ffree = ffree;
  }
}

export function statfs(path, options, callback) {
  if (typeof options === "function") {
    callback = options;
    options = {};
  }
  try {
    const res = statfsSync(path, options);
    callback(null, res);
  } catch (err) {
    callback(err, null);
  }
}

export function statfsSync(path, options) {
  const bigint = typeof options?.bigint === "boolean" ? options.bigint : false;
  const statFs = op_node_statfs(
    path,
    bigint,
  );
  return new StatFs(
    bigint ? BigInt(statFs.type) : statFs.type,
    bigint ? BigInt(statFs.bsize) : statFs.bsize,
    bigint ? BigInt(statFs.blocks) : statFs.blocks,
    bigint ? BigInt(statFs.bfree) : statFs.bfree,
    bigint ? BigInt(statFs.bavail) : statFs.bavail,
    bigint ? BigInt(statFs.files) : statFs.files,
    bigint ? BigInt(statFs.ffree) : statFs.ffree,
  );
}

export const statfsPromise = promisify(statfs);