fs: throw errors from fs.readlinkSync 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 07:51:08 +08:00
parent 167e22937c
commit 09da11e5e1
No known key found for this signature in database
GPG Key ID: F586868AAD831D0C
2 changed files with 26 additions and 10 deletions

View File

@ -1163,7 +1163,13 @@ fs.readlinkSync = function(path, options) {
handleError((path = getPathFromURL(path))); handleError((path = getPathFromURL(path)));
nullCheck(path); nullCheck(path);
validatePath(path, 'oldPath'); validatePath(path, 'oldPath');
return binding.readlink(pathModule.toNamespacedPath(path), options.encoding); const ctx = { path };
const result = binding.readlink(pathModule.toNamespacedPath(path),
options.encoding, undefined, ctx);
if (ctx.errno !== undefined) {
throw new errors.uvException(ctx);
}
return result;
}; };
function preprocessSymlinkDestination(path, type, linkPath) { function preprocessSymlinkDestination(path, type, linkPath) {
@ -1982,7 +1988,10 @@ fs.realpathSync = function realpathSync(p, options) {
if (ctx.errno !== undefined) { if (ctx.errno !== undefined) {
throw new errors.uvException(ctx); throw new errors.uvException(ctx);
} }
linkTarget = binding.readlink(baseLong); linkTarget = binding.readlink(baseLong, undefined, undefined, ctx);
if (ctx.errno !== undefined) {
throw new errors.uvException(ctx);
}
} }
resolvedLink = pathModule.resolve(previous, linkTarget); resolvedLink = pathModule.resolve(previous, linkTarget);

View File

@ -648,9 +648,8 @@ static void Link(const FunctionCallbackInfo<Value>& args) {
static void ReadLink(const FunctionCallbackInfo<Value>& args) { static void ReadLink(const FunctionCallbackInfo<Value>& args) {
Environment* env = Environment::GetCurrent(args); Environment* env = Environment::GetCurrent(args);
const int argc = args.Length(); int argc = args.Length();
CHECK_GE(argc, 3);
CHECK_GE(argc, 1);
BufferValue path(env->isolate(), args[0]); BufferValue path(env->isolate(), args[0]);
CHECK_NE(*path, nullptr); CHECK_NE(*path, nullptr);
@ -658,12 +657,18 @@ static void ReadLink(const FunctionCallbackInfo<Value>& args) {
const enum encoding encoding = ParseEncoding(env->isolate(), args[1], UTF8); const enum encoding encoding = ParseEncoding(env->isolate(), args[1], UTF8);
if (args[2]->IsObject()) { // readlink(path, encoding, req) if (args[2]->IsObject()) { // readlink(path, encoding, req)
CHECK_EQ(args.Length(), 3); CHECK_EQ(argc, 3);
AsyncCall(env, args, "readlink", encoding, AfterStringPtr, AsyncCall(env, args, "readlink", encoding, AfterStringPtr,
uv_fs_readlink, *path); uv_fs_readlink, *path);
} else { } else { // readlink(path, encoding, undefined, ctx)
SYNC_CALL(readlink, *path, *path) CHECK_EQ(argc, 4);
const char* link_path = static_cast<const char*>(SYNC_REQ.ptr); fs_req_wrap req_wrap;
int err = SyncCall(env, args[3], &req_wrap, "readlink",
uv_fs_readlink, *path);
if (err) {
return; // syscall failed, no need to continue, error info is in ctx
}
const char* link_path = static_cast<const char*>(req_wrap.req.ptr);
Local<Value> error; Local<Value> error;
MaybeLocal<Value> rc = StringBytes::Encode(env->isolate(), MaybeLocal<Value> rc = StringBytes::Encode(env->isolate(),
@ -671,9 +676,11 @@ static void ReadLink(const FunctionCallbackInfo<Value>& args) {
encoding, encoding,
&error); &error);
if (rc.IsEmpty()) { if (rc.IsEmpty()) {
env->isolate()->ThrowException(error); Local<Object> ctx = args[3].As<Object>();
ctx->Set(env->context(), env->error_string(), error).FromJust();
return; return;
} }
args.GetReturnValue().Set(rc.ToLocalChecked()); args.GetReturnValue().Set(rc.ToLocalChecked());
} }
} }