From beb4925c1d3281cb0c722f0243c8bb95a0a8a8f9 Mon Sep 17 00:00:00 2001 From: Jason Zhang Date: Mon, 16 Sep 2024 22:11:55 +0930 Subject: [PATCH] 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 PR-URL: https://github.com/nodejs/node/pull/54906 Reviewed-By: Yagiz Nizipli Reviewed-By: Luigi Pinca Reviewed-By: Richard Lau --- src/node_file.cc | 9 ++++++++- test/parallel/test-fs-cp.mjs | 20 ++++++++++++++++++++ 2 files changed, 28 insertions(+), 1 deletion(-) diff --git a/src/node_file.cc b/src/node_file.cc index a12bb9e1a94ad7..0bb70eb0fcd42d 100644 --- a/src/node_file.cc +++ b/src/node_file.cc @@ -3052,7 +3052,14 @@ static void CpSyncCheckPaths(const FunctionCallbackInfo& 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()); +#ifdef _WIN32 + int errorno = uv_translate_sys_error(error_code.value()); +#else + int errorno = + error_code.value() > 0 ? -error_code.value() : error_code.value(); +#endif + return env->ThrowUVException( + errorno, dereference ? "stat" : "lstat", nullptr, src.out()); } auto dest_status = dereference ? std::filesystem::symlink_status(dest_path, error_code) diff --git a/test/parallel/test-fs-cp.mjs b/test/parallel/test-fs-cp.mjs index c888b5c30e042d..16a52bc12c49f1 100644 --- a/test/parallel/test-fs-cp.mjs +++ b/test/parallel/test-fs-cp.mjs @@ -419,6 +419,26 @@ if (!isWindows) { ); } +// It throws an error when attempting to copy a file with a name that is too long. +{ + const src = 'a'.repeat(5000); + const dest = nextdir(); + assert.throws( + () => cpSync(src, dest), + { code: isWindows ? 'ENOENT' : 'ENAMETOOLONG' } + ); +} + +// It throws an error when attempting to copy a dir that does 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();