fs: throw errors from fs.symlinkSync 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 06:53:47 +08:00
parent bf6ce47259
commit 32bf0f6c5b
No known key found for this signature in database
GPG Key ID: F586868AAD831D0C
2 changed files with 23 additions and 7 deletions

View File

@ -1220,6 +1220,7 @@ fs.symlink = function(target, path, type_, callback_) {
const flags = stringToSymlinkType(type);
const req = new FSReqWrap();
req.oncomplete = callback;
binding.symlink(preprocessSymlinkDestination(target, type, path),
pathModule.toNamespacedPath(path), flags, req);
};
@ -1234,8 +1235,19 @@ fs.symlinkSync = function(target, path, type) {
validatePath(target, 'target');
validatePath(path);
const flags = stringToSymlinkType(type);
return binding.symlink(preprocessSymlinkDestination(target, type, path),
pathModule.toNamespacedPath(path), flags);
const ctx = { path: target, dest: path };
binding.symlink(preprocessSymlinkDestination(target, type, path),
pathModule.toNamespacedPath(path), flags, undefined, ctx);
if (ctx.errno !== undefined) {
throw new errors.uvException(ctx);
} else if (ctx.error) {
// TODO(joyeecheung): this is an encoding error usually caused by memory
// problems. We need to figure out proper error code(s) for this.
Error.captureStackTrace(ctx.error);
throw ctx.error;
}
};
fs.link = function(existingPath, newPath, callback) {

View File

@ -598,22 +598,26 @@ static void FStat(const FunctionCallbackInfo<Value>& args) {
static void Symlink(const FunctionCallbackInfo<Value>& args) {
Environment* env = Environment::GetCurrent(args);
CHECK_GE(args.Length(), 3);
int argc = args.Length();
CHECK_GE(argc, 4);
BufferValue target(env->isolate(), args[0]);
CHECK_NE(*target, nullptr);
BufferValue path(env->isolate(), args[1]);
CHECK_NE(*path, nullptr);
CHECK(args[2]->IsUint32());
int flags = args[2]->Uint32Value(env->context()).ToChecked();
CHECK(args[2]->IsInt32());
int flags = args[2].As<Int32>()->Value();
if (args[3]->IsObject()) { // symlink(target, path, flags, req)
CHECK_EQ(args.Length(), 4);
AsyncDestCall(env, args, "symlink", *path, path.length(), UTF8,
AfterNoArgs, uv_fs_symlink, *target, *path, flags);
} else { // symlink(target, path, flags)
SYNC_DEST_CALL(symlink, *target, *path, *target, *path, flags)
} else { // symlink(target, path, flags, undefinec, ctx)
CHECK_EQ(argc, 5);
fs_req_wrap req_wrap;
SyncCall(env, args[4], &req_wrap, "symlink",
uv_fs_symlink, *target, *path, flags);
}
}