Skip to content

Commit 437c756

Browse files
committed
fs: throw fs.chmodSync 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 e8ec898 commit 437c756

File tree

2 files changed

+13
-7
lines changed

2 files changed

+13
-7
lines changed

lib/fs.js

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1054,7 +1054,9 @@ fs.chmodSync = function(path, mode) {
10541054
validatePath(path);
10551055
mode = modeNum(mode);
10561056
validateUint32(mode, 'mode');
1057-
return binding.chmod(pathModule.toNamespacedPath(path), mode);
1057+
const ctx = { path };
1058+
binding.chmod(pathModule.toNamespacedPath(path), mode, undefined, ctx);
1059+
handleErrorFromBinding(ctx);
10581060
};
10591061

10601062
if (constants.O_SYMLINK !== undefined) {

src/node_file.cc

Lines changed: 10 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1458,20 +1458,24 @@ static void Read(const FunctionCallbackInfo<Value>& args) {
14581458
static void Chmod(const FunctionCallbackInfo<Value>& args) {
14591459
Environment* env = Environment::GetCurrent(args);
14601460

1461-
CHECK_GE(args.Length(), 2);
1462-
CHECK(args[1]->IsInt32());
1461+
const int argc = args.Length();
1462+
CHECK_GE(argc, 2);
14631463

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

1467-
int mode = static_cast<int>(args[1]->Int32Value());
1467+
CHECK(args[1]->IsInt32());
1468+
int mode = args[1].As<Int32>()->Value();
14681469

14691470
FSReqBase* req_wrap = GetReqWrap(env, args[2]);
1470-
if (req_wrap != nullptr) {
1471+
if (req_wrap != nullptr) { // chmod(path, mode, req)
14711472
AsyncCall(env, req_wrap, args, "chmod", UTF8, AfterNoArgs,
14721473
uv_fs_chmod, *path, mode);
1473-
} else {
1474-
SYNC_CALL(chmod, *path, *path, mode);
1474+
} else { // chmod(path, mode, undefined, ctx)
1475+
CHECK_EQ(argc, 4);
1476+
fs_req_wrap req_wrap;
1477+
SyncCall(env, args[3], &req_wrap, "chmod",
1478+
uv_fs_chmod, *path, mode);
14751479
}
14761480
}
14771481

0 commit comments

Comments
 (0)