Skip to content

Commit

Permalink
fs: throw fs.chownSync errors in JS land
Browse files Browse the repository at this point in the history
PR-URL: #18871
Refs: #18106
Reviewed-By: Michaël Zasso <[email protected]>
Reviewed-By: James M Snell <[email protected]>
Reviewed-By: Ruben Bridgewater <[email protected]>
  • Loading branch information
joyeecheung committed Feb 27, 2018
1 parent 437c756 commit 8fb5a6c
Show file tree
Hide file tree
Showing 3 changed files with 40 additions and 10 deletions.
4 changes: 3 additions & 1 deletion lib/fs.js
Original file line number Diff line number Diff line change
Expand Up @@ -1124,7 +1124,9 @@ fs.chownSync = function(path, uid, gid) {
validatePath(path);
validateUint32(uid, 'uid');
validateUint32(gid, 'gid');
return binding.chown(pathModule.toNamespacedPath(path), uid, gid);
const ctx = { path };
binding.chown(pathModule.toNamespacedPath(path), uid, gid, undefined, ctx);
handleErrorFromBinding(ctx);
};

// exported for unit tests, not for public consumption
Expand Down
23 changes: 14 additions & 9 deletions src/node_file.cc
Original file line number Diff line number Diff line change
Expand Up @@ -102,6 +102,7 @@ using v8::ObjectTemplate;
using v8::Promise;
using v8::String;
using v8::Symbol;
using v8::Uint32;
using v8::Undefined;
using v8::Value;

Expand Down Expand Up @@ -1508,23 +1509,27 @@ static void FChmod(const FunctionCallbackInfo<Value>& args) {
static void Chown(const FunctionCallbackInfo<Value>& args) {
Environment* env = Environment::GetCurrent(args);

int len = args.Length();
CHECK_GE(len, 3);
CHECK(args[1]->IsUint32());
CHECK(args[2]->IsUint32());
const int argc = args.Length();
CHECK_GE(argc, 3);

BufferValue path(env->isolate(), args[0]);
CHECK_NE(*path, nullptr);

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[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) { // chown(path, uid, gid, req)
AsyncCall(env, req_wrap, args, "chown", UTF8, AfterNoArgs,
uv_fs_chown, *path, uid, gid);
} else {
SYNC_CALL(chown, *path, *path, uid, gid);
} else { // chown(path, uid, gid, undefined, ctx)
CHECK_EQ(argc, 5);
fs_req_wrap req_wrap;
SyncCall(env, args[4], &req_wrap, "chown",
uv_fs_chown, *path, uid, gid);
}
}

Expand Down
23 changes: 23 additions & 0 deletions test/parallel/test-fs-error-messages.js
Original file line number Diff line number Diff line change
Expand Up @@ -562,3 +562,26 @@ function re(literals, ...values) {
);
});
}

// chown
if (!common.isWindows) {
const validateError = (err) => {
assert.strictEqual(nonexistentFile, err.path);
assert.strictEqual(
err.message,
`ENOENT: no such file or directory, chown '${nonexistentFile}'`);
assert.strictEqual(err.errno, uv.UV_ENOENT);
assert.strictEqual(err.code, 'ENOENT');
assert.strictEqual(err.syscall, 'chown');
return true;
};

fs.chown(nonexistentFile, process.getuid(), process.getgid(),
common.mustCall(validateError));

assert.throws(
() => fs.chownSync(nonexistentFile,
process.getuid(), process.getgid()),
validateError
);
}

0 comments on commit 8fb5a6c

Please sign in to comment.