Skip to content

Commit 3195da9

Browse files
joyeecheungMayaLekova
authored andcommitted
fs: throw errors from fs.fsyncSync in JS
PR-URL: nodejs#18348 Refs: nodejs#18106 Reviewed-By: Michaël Zasso <[email protected]> Reviewed-By: James M Snell <[email protected]>
1 parent d5196d3 commit 3195da9

File tree

3 files changed

+38
-8
lines changed

3 files changed

+38
-8
lines changed

lib/fs.js

+5-1
Original file line numberDiff line numberDiff line change
@@ -1045,7 +1045,11 @@ fs.fsync = function(fd, callback) {
10451045

10461046
fs.fsyncSync = function(fd) {
10471047
validateUint32(fd, 'fd');
1048-
return binding.fsync(fd);
1048+
const ctx = {};
1049+
binding.fsync(fd, undefined, ctx);
1050+
if (ctx.errno !== undefined) {
1051+
throw new errors.uvException(ctx);
1052+
}
10491053
};
10501054

10511055
fs.mkdir = function(path, mode, callback) {

src/node_file.cc

+12-7
Original file line numberDiff line numberDiff line change
@@ -739,7 +739,7 @@ static void Fdatasync(const FunctionCallbackInfo<Value>& args) {
739739
CHECK_GE(argc, 2);
740740

741741
CHECK(args[0]->IsInt32());
742-
const int fd = args[0]->As<Int32>()->Value();
742+
const int fd = args[0].As<Int32>()->Value();
743743

744744
if (args[1]->IsObject()) { // fdatasync(fd, req)
745745
CHECK_EQ(argc, 2);
@@ -756,16 +756,21 @@ static void Fdatasync(const FunctionCallbackInfo<Value>& args) {
756756
static void Fsync(const FunctionCallbackInfo<Value>& args) {
757757
Environment* env = Environment::GetCurrent(args);
758758

759-
CHECK(args[0]->IsInt32());
759+
const int argc = args.Length();
760+
CHECK_GE(argc, 2);
760761

761-
int fd = args[0]->Int32Value();
762+
CHECK(args[0]->IsInt32());
763+
const int fd = args[0].As<Int32>()->Value();
762764

763-
if (args[1]->IsObject()) {
764-
CHECK_EQ(args.Length(), 2);
765+
if (args[1]->IsObject()) { // fsync(fd, req)
766+
CHECK_EQ(argc, 2);
765767
AsyncCall(env, args, "fsync", UTF8, AfterNoArgs,
766768
uv_fs_fsync, fd);
767-
} else {
768-
SYNC_CALL(fsync, 0, fd)
769+
} else { // fsync(fd, undefined, ctx)
770+
CHECK_EQ(argc, 3);
771+
fs_req_wrap req_wrap;
772+
SyncCall(env, args[2], &req_wrap, "fsync",
773+
uv_fs_fsync, fd);
769774
}
770775
}
771776

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

+21
Original file line numberDiff line numberDiff line change
@@ -503,3 +503,24 @@ function re(literals, ...values) {
503503
validateError
504504
);
505505
}
506+
507+
// fsync
508+
{
509+
const validateError = (err) => {
510+
assert.strictEqual(err.message, 'EBADF: bad file descriptor, fsync');
511+
assert.strictEqual(err.errno, uv.UV_EBADF);
512+
assert.strictEqual(err.code, 'EBADF');
513+
assert.strictEqual(err.syscall, 'fsync');
514+
return true;
515+
};
516+
517+
const fd = fs.openSync(existingFile, 'r');
518+
fs.closeSync(fd);
519+
520+
fs.fsync(fd, common.mustCall(validateError));
521+
522+
assert.throws(
523+
() => fs.fsyncSync(fd),
524+
validateError
525+
);
526+
}

0 commit comments

Comments
 (0)