Skip to content

Commit 8fb5a6c

Browse files
committed
fs: throw fs.chownSync errors in JS land
PR-URL: #18871 Refs: #18106 Reviewed-By: Michaël Zasso <targos@protonmail.com> Reviewed-By: James M Snell <jasnell@gmail.com> Reviewed-By: Ruben Bridgewater <ruben@bridgewater.de>
1 parent 437c756 commit 8fb5a6c

File tree

3 files changed

+40
-10
lines changed

3 files changed

+40
-10
lines changed

lib/fs.js

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1124,7 +1124,9 @@ fs.chownSync = function(path, uid, gid) {
11241124
validatePath(path);
11251125
validateUint32(uid, 'uid');
11261126
validateUint32(gid, 'gid');
1127-
return binding.chown(pathModule.toNamespacedPath(path), uid, gid);
1127+
const ctx = { path };
1128+
binding.chown(pathModule.toNamespacedPath(path), uid, gid, undefined, ctx);
1129+
handleErrorFromBinding(ctx);
11281130
};
11291131

11301132
// exported for unit tests, not for public consumption

src/node_file.cc

Lines changed: 14 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -102,6 +102,7 @@ using v8::ObjectTemplate;
102102
using v8::Promise;
103103
using v8::String;
104104
using v8::Symbol;
105+
using v8::Uint32;
105106
using v8::Undefined;
106107
using v8::Value;
107108

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

1511-
int len = args.Length();
1512-
CHECK_GE(len, 3);
1513-
CHECK(args[1]->IsUint32());
1514-
CHECK(args[2]->IsUint32());
1512+
const int argc = args.Length();
1513+
CHECK_GE(argc, 3);
15151514

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

1519-
uv_uid_t uid = static_cast<uv_uid_t>(args[1]->Uint32Value());
1520-
uv_gid_t gid = static_cast<uv_gid_t>(args[2]->Uint32Value());
1518+
CHECK(args[1]->IsUint32());
1519+
const uv_uid_t uid = static_cast<uv_uid_t>(args[1].As<Uint32>()->Value());
1520+
1521+
CHECK(args[2]->IsUint32());
1522+
const uv_gid_t gid = static_cast<uv_gid_t>(args[2].As<Uint32>()->Value());
15211523

15221524
FSReqBase* req_wrap = GetReqWrap(env, args[3]);
1523-
if (req_wrap != nullptr) {
1525+
if (req_wrap != nullptr) { // chown(path, uid, gid, req)
15241526
AsyncCall(env, req_wrap, args, "chown", UTF8, AfterNoArgs,
15251527
uv_fs_chown, *path, uid, gid);
1526-
} else {
1527-
SYNC_CALL(chown, *path, *path, uid, gid);
1528+
} else { // chown(path, uid, gid, undefined, ctx)
1529+
CHECK_EQ(argc, 5);
1530+
fs_req_wrap req_wrap;
1531+
SyncCall(env, args[4], &req_wrap, "chown",
1532+
uv_fs_chown, *path, uid, gid);
15281533
}
15291534
}
15301535

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

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -562,3 +562,26 @@ function re(literals, ...values) {
562562
);
563563
});
564564
}
565+
566+
// chown
567+
if (!common.isWindows) {
568+
const validateError = (err) => {
569+
assert.strictEqual(nonexistentFile, err.path);
570+
assert.strictEqual(
571+
err.message,
572+
`ENOENT: no such file or directory, chown '${nonexistentFile}'`);
573+
assert.strictEqual(err.errno, uv.UV_ENOENT);
574+
assert.strictEqual(err.code, 'ENOENT');
575+
assert.strictEqual(err.syscall, 'chown');
576+
return true;
577+
};
578+
579+
fs.chown(nonexistentFile, process.getuid(), process.getgid(),
580+
common.mustCall(validateError));
581+
582+
assert.throws(
583+
() => fs.chownSync(nonexistentFile,
584+
process.getuid(), process.getgid()),
585+
validateError
586+
);
587+
}

0 commit comments

Comments
 (0)