fs: throw errors from fs.unlinkSync in JS

PR-URL: https://github.com/nodejs/node/pull/18348
Refs: https://github.com/nodejs/node/issues/18106
Reviewed-By: Michaël Zasso <targos@protonmail.com>
Reviewed-By: James M Snell <jasnell@gmail.com>
This commit is contained in:
Joyee Cheung 2018-01-24 22:18:37 +08:00
parent eca93e631f
commit 776f6cdfc4
No known key found for this signature in database
GPG Key ID: F586868AAD831D0C
2 changed files with 14 additions and 6 deletions

View File

@ -1328,7 +1328,11 @@ fs.unlinkSync = function(path) {
handleError((path = getPathFromURL(path))); handleError((path = getPathFromURL(path)));
nullCheck(path); nullCheck(path);
validatePath(path); validatePath(path);
return binding.unlink(pathModule.toNamespacedPath(path)); const ctx = { path };
binding.unlink(pathModule.toNamespacedPath(path), undefined, ctx);
if (ctx.errno !== undefined) {
throw new errors.uvException(ctx);
}
}; };
fs.fchmod = function(fd, mode, callback) { fs.fchmod = function(fd, mode, callback) {

View File

@ -777,17 +777,21 @@ static void Fsync(const FunctionCallbackInfo<Value>& args) {
static void Unlink(const FunctionCallbackInfo<Value>& args) { static void Unlink(const FunctionCallbackInfo<Value>& args) {
Environment* env = Environment::GetCurrent(args); Environment* env = Environment::GetCurrent(args);
CHECK_GE(args.Length(), 1); const int argc = args.Length();
CHECK_GE(argc, 2);
BufferValue path(env->isolate(), args[0]); BufferValue path(env->isolate(), args[0]);
CHECK_NE(*path, nullptr); CHECK_NE(*path, nullptr);
if (args[1]->IsObject()) { if (args[1]->IsObject()) { // unlink(fd, req)
CHECK_EQ(args.Length(), 2); CHECK_EQ(argc, 2);
AsyncCall(env, args, "unlink", UTF8, AfterNoArgs, AsyncCall(env, args, "unlink", UTF8, AfterNoArgs,
uv_fs_unlink, *path); uv_fs_unlink, *path);
} else { } else { // unlink(fd, undefined, ctx)
SYNC_CALL(unlink, *path, *path) CHECK_EQ(argc, 3);
fs_req_wrap req_wrap;
SyncCall(env, args[2], &req_wrap, "unlink",
uv_fs_unlink, *path);
} }
} }