fs: throw fchownSync errors in JS

PR-URL: https://github.com/nodejs/node/pull/19041
Reviewed-By: Michaël Zasso <targos@protonmail.com>
Reviewed-By: James M Snell <jasnell@gmail.com>
This commit is contained in:
Joyee Cheung 2018-02-28 01:45:17 +08:00
parent 79b195437c
commit 1650eaeac4
No known key found for this signature in database
GPG Key ID: F586868AAD831D0C
3 changed files with 40 additions and 10 deletions

View File

@ -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) {

View File

@ -1552,20 +1552,27 @@ static void Chown(const FunctionCallbackInfo<Value>& args) {
static void FChown(const FunctionCallbackInfo<Value>& args) {
Environment* env = Environment::GetCurrent(args);
CHECK(args[0]->IsInt32());
CHECK(args[1]->IsUint32());
CHECK(args[2]->IsUint32());
const int argc = args.Length();
CHECK_GE(argc, 3);
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[0]->IsInt32());
const int fd = args[0].As<Int32>()->Value();
CHECK(args[1]->IsUint32());
const uv_uid_t uid = static_cast<uv_uid_t>(args[1].As<Uint32>()->Value());
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);
}
}

View File

@ -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
);
});
}