Skip to content

[v14.x Backport] lib: add throws option to fs.f/l/statSync #36921

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
28 changes: 28 additions & 0 deletions benchmark/fs/bench-statSync-failure.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
'use strict';

const common = require('../common');
const fs = require('fs');
const path = require('path');

const bench = common.createBenchmark(main, {
n: [1e6],
statSyncType: ['throw', 'noThrow']
});


function main({ n, statSyncType }) {
const arg = path.join(__dirname, 'non.existent');

bench.start();
for (let i = 0; i < n; i++) {
if (statSyncType === 'noThrow') {
fs.statSync(arg, { throwIfNoEntry: false });
} else {
try {
fs.statSync(arg);
} catch {
}
}
}
bench.end(n);
}
14 changes: 14 additions & 0 deletions doc/api/fs.md
Original file line number Diff line number Diff line change
Expand Up @@ -2546,6 +2546,10 @@ not the file that it refers to.
<!-- YAML
added: v0.1.30
changes:
- version: REPLACEME
pr-url: https://github.com/nodejs/node/pull/33716
description: Accepts a `throwIfNoEntry` option to specify whether
an exception should be thrown if the entry does not exist.
- version: v10.5.0
pr-url: https://github.com/nodejs/node/pull/20220
description: Accepts an additional `options` object to specify whether
Expand All @@ -2560,6 +2564,9 @@ changes:
* `options` {Object}
* `bigint` {boolean} Whether the numeric values in the returned
[`fs.Stats`][] object should be `bigint`. **Default:** `false`.
* `throwIfNoEntry` {boolean} Whether an exception will be thrown
if no file system entry exists, rather than returning `undefined`.
**Default:** `true`.
* Returns: {fs.Stats}

Synchronous lstat(2).
Expand Down Expand Up @@ -3751,6 +3758,10 @@ Stats {
<!-- YAML
added: v0.1.21
changes:
- version: REPLACEME
pr-url: https://github.com/nodejs/node/pull/33716
description: Accepts a `throwIfNoEntry` option to specify whether
an exception should be thrown if the entry does not exist.
- version: v10.5.0
pr-url: https://github.com/nodejs/node/pull/20220
description: Accepts an additional `options` object to specify whether
Expand All @@ -3765,6 +3776,9 @@ changes:
* `options` {Object}
* `bigint` {boolean} Whether the numeric values in the returned
[`fs.Stats`][] object should be `bigint`. **Default:** `false`.
* `throwIfNoEntry` {boolean} Whether an exception will be thrown
if no file system entry exists, rather than returning `undefined`.
**Default:** `true`.
* Returns: {fs.Stats}

Synchronous stat(2).
Expand Down
26 changes: 23 additions & 3 deletions lib/fs.js
Original file line number Diff line number Diff line change
Expand Up @@ -71,6 +71,7 @@ const {
ERR_INVALID_CALLBACK,
ERR_FEATURE_UNAVAILABLE_ON_PLATFORM
},
uvErrmapGet,
uvException
} = require('internal/errors');

Expand Down Expand Up @@ -1061,28 +1062,47 @@ function stat(path, options = { bigint: false }, callback) {
binding.stat(pathModule.toNamespacedPath(path), options.bigint, req);
}

function fstatSync(fd, options = { bigint: false }) {
function hasNoEntryError(ctx) {
if (ctx.errno) {
const uvErr = uvErrmapGet(ctx.errno);
return uvErr && uvErr[0] === 'ENOENT';
}

if (ctx.error) {
return ctx.error.code === 'ENOENT';
}

return false;
}

function fstatSync(fd, options = { bigint: false, throwIfNoEntry: true }) {
validateInt32(fd, 'fd', 0);
const ctx = { fd };
const stats = binding.fstat(fd, options.bigint, undefined, ctx);
handleErrorFromBinding(ctx);
return getStatsFromBinding(stats);
}

function lstatSync(path, options = { bigint: false }) {
function lstatSync(path, options = { bigint: false, throwIfNoEntry: true }) {
path = getValidatedPath(path);
const ctx = { path };
const stats = binding.lstat(pathModule.toNamespacedPath(path),
options.bigint, undefined, ctx);
if (options.throwIfNoEntry === false && hasNoEntryError(ctx)) {
return undefined;
}
handleErrorFromBinding(ctx);
return getStatsFromBinding(stats);
}

function statSync(path, options = { bigint: false }) {
function statSync(path, options = { bigint: false, throwIfNoEntry: true }) {
path = getValidatedPath(path);
const ctx = { path };
const stats = binding.stat(pathModule.toNamespacedPath(path),
options.bigint, undefined, ctx);
if (options.throwIfNoEntry === false && hasNoEntryError(ctx)) {
return undefined;
}
handleErrorFromBinding(ctx);
return getStatsFromBinding(stats);
}
Expand Down
27 changes: 27 additions & 0 deletions test/parallel/test-fs-stat-bigint.js
Original file line number Diff line number Diff line change
Expand Up @@ -122,6 +122,33 @@ if (!common.isWindows) {
fs.closeSync(fd);
}

{
assert.throws(
() => fs.statSync('does_not_exist'),
{ code: 'ENOENT' });
assert.strictEqual(
fs.statSync('does_not_exist', { throwIfNoEntry: false }),
undefined);
}

{
assert.throws(
() => fs.lstatSync('does_not_exist'),
{ code: 'ENOENT' });
assert.strictEqual(
fs.lstatSync('does_not_exist', { throwIfNoEntry: false }),
undefined);
}

{
assert.throws(
() => fs.fstatSync(9999),
{ code: 'EBADF' });
assert.throws(
() => fs.fstatSync(9999, { throwIfNoEntry: false }),
{ code: 'EBADF' });
}

const runCallbackTest = (func, arg, done) => {
const startTime = process.hrtime.bigint();
func(arg, { bigint: true }, common.mustCall((err, bigintStats) => {
Expand Down