Skip to content

Commit 5583981

Browse files
committed
fs: throw errors from fs.renameSync in JS
PR-URL: #18348 Refs: #18106 Reviewed-By: Michaël Zasso <targos@protonmail.com> Reviewed-By: James M Snell <jasnell@gmail.com>
1 parent 09da11e commit 5583981

File tree

2 files changed

+15
-7
lines changed

2 files changed

+15
-7
lines changed

lib/fs.js

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -924,8 +924,12 @@ fs.renameSync = function(oldPath, newPath) {
924924
nullCheck(newPath);
925925
validatePath(oldPath, 'oldPath');
926926
validatePath(newPath, 'newPath');
927-
return binding.rename(pathModule.toNamespacedPath(oldPath),
928-
pathModule.toNamespacedPath(newPath));
927+
const ctx = { path: oldPath, dest: newPath };
928+
binding.rename(pathModule.toNamespacedPath(oldPath),
929+
pathModule.toNamespacedPath(newPath), undefined, ctx);
930+
if (ctx.errno !== undefined) {
931+
throw new errors.uvException(ctx);
932+
}
929933
};
930934

931935
fs.truncate = function(path, len, callback) {

src/node_file.cc

Lines changed: 9 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -688,19 +688,23 @@ static void ReadLink(const FunctionCallbackInfo<Value>& args) {
688688
static void Rename(const FunctionCallbackInfo<Value>& args) {
689689
Environment* env = Environment::GetCurrent(args);
690690

691-
CHECK_GE(args.Length(), 2);
691+
int argc = args.Length();
692+
CHECK_GE(argc, 3);
692693

693694
BufferValue old_path(env->isolate(), args[0]);
694695
CHECK_NE(*old_path, nullptr);
695696
BufferValue new_path(env->isolate(), args[1]);
696697
CHECK_NE(*new_path, nullptr);
697698

698-
if (args[2]->IsObject()) {
699-
CHECK_EQ(args.Length(), 3);
699+
if (args[2]->IsObject()) { // rename(old_path, new_path, req)
700+
CHECK_EQ(argc, 3);
700701
AsyncDestCall(env, args, "rename", *new_path, new_path.length(),
701702
UTF8, AfterNoArgs, uv_fs_rename, *old_path, *new_path);
702-
} else {
703-
SYNC_DEST_CALL(rename, *old_path, *new_path, *old_path, *new_path)
703+
} else { // rename(old_path, new_path, undefined, ctx)
704+
CHECK_EQ(argc, 4);
705+
fs_req_wrap req_wrap;
706+
SyncCall(env, args[3], &req_wrap, "rename",
707+
uv_fs_rename, *old_path, *new_path);
704708
}
705709
}
706710

0 commit comments

Comments
 (0)