Skip to content

Commit

Permalink
fs: translate error code properly in cpSync
Browse files Browse the repository at this point in the history
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>
  • Loading branch information
jazelly and jakecastelli committed Sep 12, 2024
1 parent 9db6327 commit be5dfdd
Show file tree
Hide file tree
Showing 2 changed files with 25 additions and 1 deletion.
6 changes: 5 additions & 1 deletion src/node_file.cc
Original file line number Diff line number Diff line change
Expand Up @@ -3147,7 +3147,11 @@ static void CpSyncCheckPaths(const FunctionCallbackInfo<Value>& args) {
? std::filesystem::symlink_status(src_path, error_code)
: std::filesystem::status(src_path, error_code);
if (error_code) {
return env->ThrowUVException(EEXIST, "lstat", nullptr, src.out());
return env->ThrowUVException(
error_code.value() > 0 ? -error_code.value() : error_code.value(),
dereference ? "stat" : "lstat",
nullptr,
src.out());
}
auto dest_status =
dereference ? std::filesystem::symlink_status(dest_path, error_code)
Expand Down
20 changes: 20 additions & 0 deletions test/parallel/test-fs-cp.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -419,6 +419,26 @@ if (!isWindows) {
);
}

// It throws error if attempt is made to copy a file with name too long.
{
const src = 'a'.repeat(5000);
const dest = nextdir();
assert.throws(
() => cpSync(src, dest, mustNotMutateObjectDeep()),
{ code: 'ENAMETOOLONG' }
);
}

// It throws error if attempt is made to copy a dir not exist.
{
const src = nextdir();
const dest = nextdir();
assert.throws(
() => cpSync(src, dest, mustNotMutateObjectDeep({ recursive: true })),
{ code: 'ENOENT' }
);
}

// It makes file writeable when updating timestamp, if not writeable.
{
const src = nextdir();
Expand Down

0 comments on commit be5dfdd

Please sign in to comment.