Skip to content

Commit

Permalink
fs: throw errors from fs.renameSync in JS
Browse files Browse the repository at this point in the history
PR-URL: nodejs#18348
Refs: nodejs#18106
Reviewed-By: Michaël Zasso <targos@protonmail.com>
Reviewed-By: James M Snell <jasnell@gmail.com>
  • Loading branch information
joyeecheung authored and MayaLekova committed May 8, 2018
1 parent c8deefe commit 05d6940
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 7 deletions.
8 changes: 6 additions & 2 deletions lib/fs.js
Original file line number Diff line number Diff line change
Expand Up @@ -924,8 +924,12 @@ fs.renameSync = function(oldPath, newPath) {
nullCheck(newPath);
validatePath(oldPath, 'oldPath');
validatePath(newPath, 'newPath');
return binding.rename(pathModule.toNamespacedPath(oldPath),
pathModule.toNamespacedPath(newPath));
const ctx = { path: oldPath, dest: newPath };
binding.rename(pathModule.toNamespacedPath(oldPath),
pathModule.toNamespacedPath(newPath), undefined, ctx);
if (ctx.errno !== undefined) {
throw new errors.uvException(ctx);
}
};

fs.truncate = function(path, len, callback) {
Expand Down
14 changes: 9 additions & 5 deletions src/node_file.cc
Original file line number Diff line number Diff line change
Expand Up @@ -688,19 +688,23 @@ static void ReadLink(const FunctionCallbackInfo<Value>& args) {
static void Rename(const FunctionCallbackInfo<Value>& args) {
Environment* env = Environment::GetCurrent(args);

CHECK_GE(args.Length(), 2);
int argc = args.Length();
CHECK_GE(argc, 3);

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

if (args[2]->IsObject()) {
CHECK_EQ(args.Length(), 3);
if (args[2]->IsObject()) { // rename(old_path, new_path, req)
CHECK_EQ(argc, 3);
AsyncDestCall(env, args, "rename", *new_path, new_path.length(),
UTF8, AfterNoArgs, uv_fs_rename, *old_path, *new_path);
} else {
SYNC_DEST_CALL(rename, *old_path, *new_path, *old_path, *new_path)
} else { // rename(old_path, new_path, undefined, ctx)
CHECK_EQ(argc, 4);
fs_req_wrap req_wrap;
SyncCall(env, args[3], &req_wrap, "rename",
uv_fs_rename, *old_path, *new_path);
}
}

Expand Down

0 comments on commit 05d6940

Please sign in to comment.