Skip to content

Commit f5e287b

Browse files
committed
fs: throw errors from fs.fdatasyncSync in JS
PR-URL: #18348 Refs: #18106 Reviewed-By: Michaël Zasso <targos@protonmail.com> Reviewed-By: James M Snell <jasnell@gmail.com>
1 parent b3a7df7 commit f5e287b

File tree

3 files changed

+37
-7
lines changed

3 files changed

+37
-7
lines changed

lib/fs.js

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1029,7 +1029,11 @@ fs.fdatasync = function(fd, callback) {
10291029

10301030
fs.fdatasyncSync = function(fd) {
10311031
validateUint32(fd, 'fd');
1032-
return binding.fdatasync(fd);
1032+
const ctx = {};
1033+
binding.fdatasync(fd, undefined, ctx);
1034+
if (ctx.errno !== undefined) {
1035+
throw new errors.uvException(ctx);
1036+
}
10331037
};
10341038

10351039
fs.fsync = function(fd, callback) {

src/node_file.cc

Lines changed: 11 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -735,16 +735,21 @@ static void FTruncate(const FunctionCallbackInfo<Value>& args) {
735735
static void Fdatasync(const FunctionCallbackInfo<Value>& args) {
736736
Environment* env = Environment::GetCurrent(args);
737737

738-
CHECK(args[0]->IsInt32());
738+
const int argc = args.Length();
739+
CHECK_GE(argc, 2);
739740

740-
int fd = args[0]->Int32Value();
741+
CHECK(args[0]->IsInt32());
742+
const int fd = args[0]->As<Int32>()->Value();
741743

742-
if (args[1]->IsObject()) {
743-
CHECK_EQ(args.Length(), 2);
744+
if (args[1]->IsObject()) { // fdatasync(fd, req)
745+
CHECK_EQ(argc, 2);
744746
AsyncCall(env, args, "fdatasync", UTF8, AfterNoArgs,
745747
uv_fs_fdatasync, fd);
746-
} else {
747-
SYNC_CALL(fdatasync, 0, fd)
748+
} else { // fdatasync(fd, undefined, ctx)
749+
CHECK_EQ(argc, 3);
750+
fs_req_wrap req_wrap;
751+
SyncCall(env, args[2], &req_wrap, "fdatasync",
752+
uv_fs_fdatasync, fd);
748753
}
749754
}
750755

test/parallel/test-fs-error-messages.js

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -482,3 +482,24 @@ function re(literals, ...values) {
482482
validateError
483483
);
484484
}
485+
486+
// fdatasync
487+
{
488+
const validateError = (err) => {
489+
assert.strictEqual(err.message, 'EBADF: bad file descriptor, fdatasync');
490+
assert.strictEqual(err.errno, uv.UV_EBADF);
491+
assert.strictEqual(err.code, 'EBADF');
492+
assert.strictEqual(err.syscall, 'fdatasync');
493+
return true;
494+
};
495+
496+
const fd = fs.openSync(existingFile, 'r');
497+
fs.closeSync(fd);
498+
499+
fs.fdatasync(fd, common.mustCall(validateError));
500+
501+
assert.throws(
502+
() => fs.fdatasyncSync(fd),
503+
validateError
504+
);
505+
}

0 commit comments

Comments
 (0)