Skip to content

Commit 4f40f36

Browse files
jazellyjakecastelli
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>
1 parent 9db6327 commit 4f40f36

File tree

2 files changed

+25
-1
lines changed

2 files changed

+25
-1
lines changed

src/node_file.cc

+5-1
Original file line numberDiff line numberDiff line change
@@ -3147,7 +3147,11 @@ 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+
return env->ThrowUVException(
3151+
error_code.value() > 0 ? -error_code.value() : error_code.value(),
3152+
dereference ? "stat" : "lstat",
3153+
nullptr,
3154+
src.out());
31513155
}
31523156
auto dest_status =
31533157
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 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, mustNotMutateObjectDeep()),
428+
{ code: 'ENAMETOOLONG' }
429+
);
430+
}
431+
432+
// It throws 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)