Skip to content

Commit

Permalink
fs: throw fchmodSync 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 c6acfdb commit 79b1954
Show file tree
Hide file tree
Showing 3 changed files with 35 additions and 7 deletions.
4 changes: 3 additions & 1 deletion lib/fs.js
Original file line number Diff line number Diff line change
Expand Up @@ -1004,7 +1004,9 @@ fs.fchmodSync = function(fd, mode) {
validateUint32(mode, 'mode');
if (mode < 0 || mode > 0o777)
throw new errors.RangeError('ERR_OUT_OF_RANGE', 'mode');
return binding.fchmod(fd, mode);
const ctx = {};
binding.fchmod(fd, mode, undefined, ctx);
handleErrorFromBinding(ctx);
};

if (constants.O_SYMLINK !== undefined) {
Expand Down
18 changes: 12 additions & 6 deletions src/node_file.cc
Original file line number Diff line number Diff line change
Expand Up @@ -1493,18 +1493,24 @@ static void Chmod(const FunctionCallbackInfo<Value>& args) {
static void FChmod(const FunctionCallbackInfo<Value>& args) {
Environment* env = Environment::GetCurrent(args);

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

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

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

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

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

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

common.runWithInvalidFD((fd) => {
fs.fchmod(fd, 0o666, common.mustCall(validateError));

assert.throws(
() => fs.fchmodSync(fd, 0o666),
validateError
);
});
}

0 comments on commit 79b1954

Please sign in to comment.