Skip to content
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

fs: throw errors from sync branches instead of separate implementations #49913

Merged
merged 2 commits into from
Sep 30, 2023
Merged
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
66 changes: 52 additions & 14 deletions lib/fs.js
Original file line number Diff line number Diff line change
Expand Up @@ -141,7 +141,6 @@ const {
validateObject,
validateString,
} = require('internal/validators');
const syncFs = require('internal/fs/sync');

let truncateWarn = true;
let fs;
Expand Down Expand Up @@ -243,7 +242,10 @@ function access(path, mode, callback) {
* @returns {void}
*/
function accessSync(path, mode) {
syncFs.access(path, mode);
path = getValidatedPath(path);
mode = getValidMode(mode, 'access');

binding.access(pathModule.toNamespacedPath(path), mode);
}

/**
Expand Down Expand Up @@ -285,7 +287,13 @@ ObjectDefineProperty(exists, kCustomPromisifiedSymbol, {
* @returns {boolean}
*/
function existsSync(path) {
return syncFs.exists(path);
try {
path = getValidatedPath(path);
} catch {
return false;
}

return binding.existsSync(pathModule.toNamespacedPath(path));
}

function readFileAfterOpen(err, fd) {
Expand Down Expand Up @@ -438,7 +446,10 @@ function readFileSync(path, options) {
options = getOptions(options, { flag: 'r' });

if (options.encoding === 'utf8' || options.encoding === 'utf-8') {
return syncFs.readFileUtf8(path, options.flag);
if (!isInt32(path)) {
path = pathModule.toNamespacedPath(getValidatedPath(path));
}
return binding.readFileUtf8(path, stringToFlags(options.flag));
}

const isUserFd = isFd(path); // File descriptor ownership
Expand Down Expand Up @@ -516,7 +527,9 @@ function close(fd, callback = defaultCloseCallback) {
* @returns {void}
*/
function closeSync(fd) {
return syncFs.close(fd);
fd = getValidatedFd(fd);

return binding.close(fd);
}

/**
Expand Down Expand Up @@ -562,7 +575,13 @@ function open(path, flags, mode, callback) {
* @returns {number}
*/
function openSync(path, flags, mode) {
return syncFs.open(path, flags, mode);
path = getValidatedPath(path);

return binding.open(
pathModule.toNamespacedPath(path),
stringToFlags(flags),
parseFileMode(mode, 'mode', 0o666),
);
}

/**
Expand Down Expand Up @@ -1665,12 +1684,24 @@ function lstatSync(path, options = { bigint: false, throwIfNoEntry: true }) {
* }} [options]
* @returns {Stats}
*/
function statSync(path, options) {
return syncFs.stat(path, options);
function statSync(path, options = { bigint: false, throwIfNoEntry: true }) {
path = getValidatedPath(path);
const stats = binding.stat(
pathModule.toNamespacedPath(path),
options.bigint,
undefined,
options.throwIfNoEntry,
);
if (stats === undefined) {
return undefined;
}
return getStatsFromBinding(stats);
}

function statfsSync(path, options) {
return syncFs.statfs(path, options);
function statfsSync(path, options = { bigint: false }) {
path = getValidatedPath(path);
const stats = binding.statfs(pathModule.toNamespacedPath(path), options.bigint);
return getStatFsFromBinding(stats);
}

/**
Expand Down Expand Up @@ -1850,7 +1881,8 @@ function unlink(path, callback) {
* @returns {void}
*/
function unlinkSync(path) {
return syncFs.unlink(path);
path = pathModule.toNamespacedPath(getValidatedPath(path));
return binding.unlink(path);
}

/**
Expand Down Expand Up @@ -2650,8 +2682,7 @@ function realpathSync(p, options) {
}
if (linkTarget === null) {
const ctx = { path: base };
binding.stat(baseLong, false, undefined, ctx);
handleErrorFromBinding(ctx);
binding.stat(baseLong, false, undefined, true);
linkTarget = binding.readlink(baseLong, undefined, undefined, ctx);
handleErrorFromBinding(ctx);
}
Expand Down Expand Up @@ -2946,7 +2977,14 @@ function copyFile(src, dest, mode, callback) {
* @returns {void}
*/
function copyFileSync(src, dest, mode) {
syncFs.copyFile(src, dest, mode);
src = getValidatedPath(src, 'src');
dest = getValidatedPath(dest, 'dest');

binding.copyFile(
pathModule.toNamespacedPath(src),
pathModule.toNamespacedPath(dest),
getValidMode(mode, 'copyFile'),
);
}

/**
Expand Down
106 changes: 0 additions & 106 deletions lib/internal/fs/sync.js

This file was deleted.

32 changes: 32 additions & 0 deletions src/node_file-inl.h
Original file line number Diff line number Diff line change
Expand Up @@ -349,6 +349,38 @@ int SyncCall(Environment* env, v8::Local<v8::Value> ctx,
return err;
}

// Similar to SyncCall but throws immediately if there is an error.
template <typename Predicate, typename Func, typename... Args>
int SyncCallAndThrowIf(Predicate should_throw,
Environment* env,
FSReqWrapSync* req_wrap,
Func fn,
Args... args) {
env->PrintSyncTrace();
Copy link
Member

@anonrig anonrig Sep 28, 2023

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

If the lack of this call causes a bug, I recommend adding a test to address your concerns in the description

Copy link
Member Author

@joyeecheung joyeecheung Sep 28, 2023

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We do have a test (test/parallel/test-sync-io-option.js), but it only checks fs.statSync. Not sure if it's worth it to add a test for every single fs methods, I think it's testing under the assumption that fs methods would share this bit of code so if one is okay, others are fine.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I unknowingly broke it. We should eventually add it.

int result = fn(nullptr, &(req_wrap->req), args..., nullptr);
if (should_throw(result)) {
env->ThrowUVException(result,
req_wrap->syscall_p,
nullptr,
req_wrap->path_p,
req_wrap->dest_p);
}
return result;
}

constexpr bool is_uv_error(int result) {
return result < 0;
}

// Similar to SyncCall but throws immediately if there is an error.
template <typename Func, typename... Args>
int SyncCallAndThrowOnError(Environment* env,
FSReqWrapSync* req_wrap,
Func fn,
Args... args) {
return SyncCallAndThrowIf(is_uv_error, env, req_wrap, fn, args...);
}

} // namespace fs
} // namespace node

Expand Down
Loading