Skip to content

Commit

Permalink
fs: throw fchownSync errors in JS
Browse files Browse the repository at this point in the history
PR-URL: #19041
Reviewed-By: Michaël Zasso <targos@protonmail.com>
Reviewed-By: James M Snell <jasnell@gmail.com>
  • Loading branch information
joyeecheung committed Mar 2, 2018
1 parent 79b1954 commit 1650eae
Show file tree
Hide file tree
Showing 3 changed files with 38 additions and 8 deletions.
4 changes: 3 additions & 1 deletion lib/fs.js
Original file line number Diff line number Diff line change
Expand Up @@ -1110,7 +1110,9 @@ fs.fchownSync = function(fd, uid, gid) {
validateUint32(uid, 'uid');
validateUint32(gid, 'gid');

return binding.fchown(fd, uid, gid);
const ctx = {};
binding.fchown(fd, uid, gid, undefined, ctx);
handleErrorFromBinding(ctx);
};

fs.chown = function(path, uid, gid, callback) {
Expand Down
21 changes: 14 additions & 7 deletions src/node_file.cc
Original file line number Diff line number Diff line change
Expand Up @@ -1552,20 +1552,27 @@ static void Chown(const FunctionCallbackInfo<Value>& args) {
static void FChown(const FunctionCallbackInfo<Value>& args) {
Environment* env = Environment::GetCurrent(args);

const int argc = args.Length();
CHECK_GE(argc, 3);

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

CHECK(args[1]->IsUint32());
CHECK(args[2]->IsUint32());
const uv_uid_t uid = static_cast<uv_uid_t>(args[1].As<Uint32>()->Value());

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

FSReqBase* req_wrap = GetReqWrap(env, args[3]);
if (req_wrap != nullptr) {
if (req_wrap != nullptr) { // fchown(fd, uid, gid, req)
AsyncCall(env, req_wrap, args, "fchown", UTF8, AfterNoArgs,
uv_fs_fchown, fd, uid, gid);
} else {
SYNC_CALL(fchown, 0, fd, uid, gid);
} else { // fchown(fd, uid, gid, undefined, ctx)
CHECK_EQ(argc, 5);
fs_req_wrap req_wrap;
SyncCall(env, args[4], &req_wrap, "fchown",
uv_fs_fchown, fd, uid, gid);
}
}

Expand Down
21 changes: 21 additions & 0 deletions test/parallel/test-fs-error-messages.js
Original file line number Diff line number Diff line change
Expand Up @@ -749,3 +749,24 @@ if (!common.isAIX) {
);
});
}

// fchown
if (!common.isWindows) {
const validateError = (err) => {
assert.strictEqual(err.message, 'EBADF: bad file descriptor, fchown');
assert.strictEqual(err.errno, uv.UV_EBADF);
assert.strictEqual(err.code, 'EBADF');
assert.strictEqual(err.syscall, 'fchown');
return true;
};

common.runWithInvalidFD((fd) => {
fs.fchown(fd, process.getuid(), process.getgid(),
common.mustCall(validateError));

assert.throws(
() => fs.fchownSync(fd, process.getuid(), process.getgid()),
validateError
);
});
}

0 comments on commit 1650eae

Please sign in to comment.