Skip to content

Commit

Permalink
fs: throw errors from fs.readlinkSync in JS
Browse files Browse the repository at this point in the history
PR-URL: #18348
Refs: #18106
Reviewed-By: Michaël Zasso <targos@protonmail.com>
Reviewed-By: James M Snell <jasnell@gmail.com>
  • Loading branch information
joyeecheung committed Feb 1, 2018
1 parent 167e229 commit 09da11e
Show file tree
Hide file tree
Showing 2 changed files with 26 additions and 10 deletions.
13 changes: 11 additions & 2 deletions lib/fs.js
Original file line number Diff line number Diff line change
Expand Up @@ -1163,7 +1163,13 @@ fs.readlinkSync = function(path, options) {
handleError((path = getPathFromURL(path)));
nullCheck(path);
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) {
Expand Down Expand Up @@ -1982,7 +1988,10 @@ fs.realpathSync = function realpathSync(p, options) {
if (ctx.errno !== undefined) {
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);

Expand Down
23 changes: 15 additions & 8 deletions src/node_file.cc
Original file line number Diff line number Diff line change
Expand Up @@ -648,32 +648,39 @@ static void Link(const FunctionCallbackInfo<Value>& args) {
static void ReadLink(const FunctionCallbackInfo<Value>& args) {
Environment* env = Environment::GetCurrent(args);

const int argc = args.Length();

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

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

const enum encoding encoding = ParseEncoding(env->isolate(), args[1], UTF8);

if (args[2]->IsObject()) { // readlink(path, encoding, req)
CHECK_EQ(args.Length(), 3);
CHECK_EQ(argc, 3);
AsyncCall(env, args, "readlink", encoding, AfterStringPtr,
uv_fs_readlink, *path);
} else {
SYNC_CALL(readlink, *path, *path)
const char* link_path = static_cast<const char*>(SYNC_REQ.ptr);
} else { // readlink(path, encoding, undefined, ctx)
CHECK_EQ(argc, 4);
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;
MaybeLocal<Value> rc = StringBytes::Encode(env->isolate(),
link_path,
encoding,
&error);
if (rc.IsEmpty()) {
env->isolate()->ThrowException(error);
Local<Object> ctx = args[3].As<Object>();
ctx->Set(env->context(), env->error_string(), error).FromJust();
return;
}

args.GetReturnValue().Set(rc.ToLocalChecked());
}
}
Expand Down

0 comments on commit 09da11e

Please sign in to comment.