Skip to content

Commit 93116dd

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 <[email protected]> PR-URL: #54906 Reviewed-By: Yagiz Nizipli <[email protected]> Reviewed-By: Luigi Pinca <[email protected]> Reviewed-By: Richard Lau <[email protected]>
1 parent 77d162a commit 93116dd

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
@@ -3052,7 +3052,14 @@ static void CpSyncCheckPaths(const FunctionCallbackInfo<Value>& args) {
30523052
? std::filesystem::symlink_status(src_path, error_code)
30533053
: std::filesystem::status(src_path, error_code);
30543054
if (error_code) {
3055-
return env->ThrowUVException(EEXIST, "lstat", nullptr, src.out());
3055+
#ifdef _WIN32
3056+
int errorno = uv_translate_sys_error(error_code.value());
3057+
#else
3058+
int errorno =
3059+
error_code.value() > 0 ? -error_code.value() : error_code.value();
3060+
#endif
3061+
return env->ThrowUVException(
3062+
errorno, dereference ? "stat" : "lstat", nullptr, src.out());
30563063
}
30573064
auto dest_status =
30583065
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)