Skip to content

Commit bb38158

Browse files
jazellyjakecastelli
authored andcommitted
fs: translate error code properly in cpSync
UV error code needs to be negative integer so it can be mapped correctly. The filesystem error are positive integer, so we need to handle it before throwing. Co-authored-by: Jake Yuesong Li <jake.yuesong@gmail.com> PR-URL: nodejs#54906 Reviewed-By: Yagiz Nizipli <yagiz@nizipli.com> Reviewed-By: Luigi Pinca <luigipinca@gmail.com> Reviewed-By: Richard Lau <rlau@redhat.com>
1 parent 9bb73f5 commit bb38158

File tree

2 files changed

+28
-1
lines changed

2 files changed

+28
-1
lines changed

src/node_file.cc

+8-1
Original file line numberDiff line numberDiff line change
@@ -3147,7 +3147,14 @@ static void CpSyncCheckPaths(const FunctionCallbackInfo<Value>& args) {
31473147
? std::filesystem::symlink_status(src_path, error_code)
31483148
: std::filesystem::status(src_path, error_code);
31493149
if (error_code) {
3150-
return env->ThrowUVException(EEXIST, "lstat", nullptr, src.out());
3150+
#ifdef _WIN32
3151+
int errorno = uv_translate_sys_error(error_code.value());
3152+
#else
3153+
int errorno =
3154+
error_code.value() > 0 ? -error_code.value() : error_code.value();
3155+
#endif
3156+
return env->ThrowUVException(
3157+
errorno, dereference ? "stat" : "lstat", nullptr, src.out());
31513158
}
31523159
auto dest_status =
31533160
dereference ? std::filesystem::symlink_status(dest_path, error_code)

test/parallel/test-fs-cp.mjs

+20
Original file line numberDiff line numberDiff line change
@@ -419,6 +419,26 @@ if (!isWindows) {
419419
);
420420
}
421421

422+
// It throws an error when attempting to copy a file with a name that is too long.
423+
{
424+
const src = 'a'.repeat(5000);
425+
const dest = nextdir();
426+
assert.throws(
427+
() => cpSync(src, dest),
428+
{ code: isWindows ? 'ENOENT' : 'ENAMETOOLONG' }
429+
);
430+
}
431+
432+
// It throws an error when attempting to copy a dir that does not exist.
433+
{
434+
const src = nextdir();
435+
const dest = nextdir();
436+
assert.throws(
437+
() => cpSync(src, dest, mustNotMutateObjectDeep({ recursive: true })),
438+
{ code: 'ENOENT' }
439+
);
440+
}
441+
422442
// It makes file writeable when updating timestamp, if not writeable.
423443
{
424444
const src = nextdir();

0 commit comments

Comments
 (0)