From a7c9daa4972b31b2af777b49d9e0c0b8fda69d62 Mon Sep 17 00:00:00 2001 From: Colin Ihrig Date: Sun, 29 Jan 2023 12:43:20 -0500 Subject: [PATCH] fs: add statfs() functions This commit adds statfs() and statfsSync() to the fs module, and statfs() to the fsPromises module. Co-authored-by: cjihrig Fixes: /~https://github.com/nodejs/node/issues/10745 Refs: /~https://github.com/nodejs/node/pull/31351 PR-URL: /~https://github.com/nodejs/node/pull/46358 Reviewed-By: Matteo Collina Reviewed-By: James M Snell --- doc/api/fs.md | 159 ++++++++++++++++++++++++++++++ lib/fs.js | 30 ++++++ lib/internal/fs/promises.js | 9 ++ lib/internal/fs/utils.js | 19 ++++ src/node_file-inl.h | 61 +++++++++--- src/node_file.cc | 76 +++++++++++++- src/node_file.h | 29 ++++++ test/parallel/test-fs-promises.js | 26 +++++ test/parallel/test-fs-statfs.js | 59 +++++++++++ tools/doc/type-parser.mjs | 1 + 10 files changed, 457 insertions(+), 12 deletions(-) create mode 100644 test/parallel/test-fs-statfs.js diff --git a/doc/api/fs.md b/doc/api/fs.md index 6c7099b265f454..ee67a125b43b66 100644 --- a/doc/api/fs.md +++ b/doc/api/fs.md @@ -1531,6 +1531,19 @@ changes: * Returns: {Promise} Fulfills with the {fs.Stats} object for the given `path`. +### `fsPromises.statfs(path[, options])` + + + +* `path` {string|Buffer|URL} +* `options` {Object} + * `bigint` {boolean} Whether the numeric values in the returned + {fs.StatFs} object should be `bigint`. **Default:** `false`. +* Returns: {Promise} Fulfills with the {fs.StatFs} object for the + given `path`. + ### `fsPromises.symlink(target, path[, type])` + +* `path` {string|Buffer|URL} +* `options` {Object} + * `bigint` {boolean} Whether the numeric values in the returned + {fs.StatFs} object should be `bigint`. **Default:** `false`. +* `callback` {Function} + * `err` {Error} + * `stats` {fs.StatFs} + +Asynchronous statfs(2). Returns information about the mounted file system which +contains `path`. The callback gets two arguments `(err, stats)` where `stats` +is an {fs.StatFs} object. + +In case of an error, the `err.code` will be one of [Common System Errors][]. + ### `fs.symlink(target, path[, type], callback)` + +* `path` {string|Buffer|URL} +* `options` {Object} + * `bigint` {boolean} Whether the numeric values in the returned + {fs.StatFs} object should be `bigint`. **Default:** `false`. +* Returns: {fs.StatFs} + +Synchronous statfs(2). Returns information about the mounted file system which +contains `path`. + +In case of an error, the `err.code` will be one of [Common System Errors][]. + ### `fs.symlinkSync(target, path[, type])` + +Provides information about a mounted file system. + +Objects returned from [`fs.statfs()`][] and its synchronous counterpart are of +this type. If `bigint` in the `options` passed to those methods is `true`, the +numeric values will be `bigint` instead of `number`. + +```console +StatFs { + type: 1397114950, + bsize: 4096, + blocks: 121938943, + bfree: 61058895, + bavail: 61058895, + files: 999, + ffree: 1000000 +} +``` + +`bigint` version: + +```console +StatFs { + type: 1397114950n, + bsize: 4096n, + blocks: 121938943n, + bfree: 61058895n, + bavail: 61058895n, + files: 999n, + ffree: 1000000n +} +``` + +#### `statfs.bavail` + + + +* {number|bigint} + +Free blocks available to unprivileged users. + +#### `statfs.bfree` + + + +* {number|bigint} + +Free blocks in file system. + +#### `statfs.blocks` + + + +* {number|bigint} + +Total data blocks in file system. + +#### `statfs.bsize` + + + +* {number|bigint} + +Optimal transfer block size. + +#### `statfs.ffree` + + + +* {number|bigint} + +Free file nodes in file system. + +#### `statfs.files` + + + +* {number|bigint} + +Total file nodes in file system. + +#### `statfs.type` + + + +* {number|bigint} + +Type of file system. + ### Class: `fs.WriteStream`