Skip to content

Commit 1650eae

Browse files
committed
fs: throw fchownSync errors in JS
PR-URL: #19041 Reviewed-By: Michaël Zasso <targos@protonmail.com> Reviewed-By: James M Snell <jasnell@gmail.com>
1 parent 79b1954 commit 1650eae

File tree

3 files changed

+38
-8
lines changed

3 files changed

+38
-8
lines changed

lib/fs.js

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1110,7 +1110,9 @@ fs.fchownSync = function(fd, uid, gid) {
11101110
validateUint32(uid, 'uid');
11111111
validateUint32(gid, 'gid');
11121112

1113-
return binding.fchown(fd, uid, gid);
1113+
const ctx = {};
1114+
binding.fchown(fd, uid, gid, undefined, ctx);
1115+
handleErrorFromBinding(ctx);
11141116
};
11151117

11161118
fs.chown = function(path, uid, gid, callback) {

src/node_file.cc

Lines changed: 14 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1552,20 +1552,27 @@ static void Chown(const FunctionCallbackInfo<Value>& args) {
15521552
static void FChown(const FunctionCallbackInfo<Value>& args) {
15531553
Environment* env = Environment::GetCurrent(args);
15541554

1555+
const int argc = args.Length();
1556+
CHECK_GE(argc, 3);
1557+
15551558
CHECK(args[0]->IsInt32());
1559+
const int fd = args[0].As<Int32>()->Value();
1560+
15561561
CHECK(args[1]->IsUint32());
1557-
CHECK(args[2]->IsUint32());
1562+
const uv_uid_t uid = static_cast<uv_uid_t>(args[1].As<Uint32>()->Value());
15581563

1559-
int fd = args[0]->Int32Value();
1560-
uv_uid_t uid = static_cast<uv_uid_t>(args[1]->Uint32Value());
1561-
uv_gid_t gid = static_cast<uv_gid_t>(args[2]->Uint32Value());
1564+
CHECK(args[2]->IsUint32());
1565+
const uv_gid_t gid = static_cast<uv_gid_t>(args[2].As<Uint32>()->Value());
15621566

15631567
FSReqBase* req_wrap = GetReqWrap(env, args[3]);
1564-
if (req_wrap != nullptr) {
1568+
if (req_wrap != nullptr) { // fchown(fd, uid, gid, req)
15651569
AsyncCall(env, req_wrap, args, "fchown", UTF8, AfterNoArgs,
15661570
uv_fs_fchown, fd, uid, gid);
1567-
} else {
1568-
SYNC_CALL(fchown, 0, fd, uid, gid);
1571+
} else { // fchown(fd, uid, gid, undefined, ctx)
1572+
CHECK_EQ(argc, 5);
1573+
fs_req_wrap req_wrap;
1574+
SyncCall(env, args[4], &req_wrap, "fchown",
1575+
uv_fs_fchown, fd, uid, gid);
15691576
}
15701577
}
15711578

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

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -749,3 +749,24 @@ if (!common.isAIX) {
749749
);
750750
});
751751
}
752+
753+
// fchown
754+
if (!common.isWindows) {
755+
const validateError = (err) => {
756+
assert.strictEqual(err.message, 'EBADF: bad file descriptor, fchown');
757+
assert.strictEqual(err.errno, uv.UV_EBADF);
758+
assert.strictEqual(err.code, 'EBADF');
759+
assert.strictEqual(err.syscall, 'fchown');
760+
return true;
761+
};
762+
763+
common.runWithInvalidFD((fd) => {
764+
fs.fchown(fd, process.getuid(), process.getgid(),
765+
common.mustCall(validateError));
766+
767+
assert.throws(
768+
() => fs.fchownSync(fd, process.getuid(), process.getgid()),
769+
validateError
770+
);
771+
});
772+
}

0 commit comments

Comments
 (0)