Skip to content

Commit 4691f3e

Browse files
StefanStojanovicaduh95
authored andcommitted
fs: fix cpSync to handle non-ASCII characters
Fixes: #61878 PR-URL: #61950 Reviewed-By: Yagiz Nizipli <yagiz@nizipli.com> Reviewed-By: Luigi Pinca <luigipinca@gmail.com>
1 parent 3d87eca commit 4691f3e

File tree

2 files changed

+34
-5
lines changed

2 files changed

+34
-5
lines changed

src/node_file.cc

Lines changed: 11 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -3423,15 +3423,18 @@ static void CpSyncOverrideFile(const FunctionCallbackInfo<Value>& args) {
34233423
THROW_IF_INSUFFICIENT_PERMISSIONS(
34243424
env, permission::PermissionScope::kFileSystemWrite, dest.ToStringView());
34253425

3426+
auto src_path = src.ToPath();
3427+
auto dest_path = dest.ToPath();
3428+
34263429
std::error_code error;
34273430

3428-
if (!std::filesystem::remove(*dest, error)) {
3431+
if (!std::filesystem::remove(dest_path, error)) {
34293432
return env->ThrowStdErrException(error, "unlink", *dest);
34303433
}
34313434

34323435
if (mode == 0) {
34333436
// if no mode is specified use the faster std::filesystem API
3434-
if (!std::filesystem::copy_file(*src, *dest, error)) {
3437+
if (!std::filesystem::copy_file(src_path, dest_path, error)) {
34353438
return env->ThrowStdErrException(error, "cp", *dest);
34363439
}
34373440
} else {
@@ -3444,7 +3447,7 @@ static void CpSyncOverrideFile(const FunctionCallbackInfo<Value>& args) {
34443447
}
34453448

34463449
if (preserve_timestamps) {
3447-
CopyUtimes(*src, *dest, env);
3450+
CopyUtimes(src_path, dest_path, env);
34483451
}
34493452
}
34503453

@@ -3487,8 +3490,11 @@ static void CpSyncCopyDir(const FunctionCallbackInfo<Value>& args) {
34873490
bool verbatim_symlinks = args[5]->IsTrue();
34883491
bool preserve_timestamps = args[6]->IsTrue();
34893492

3493+
auto src_path = src.ToPath();
3494+
auto dest_path = dest.ToPath();
3495+
34903496
std::error_code error;
3491-
std::filesystem::create_directories(*dest, error);
3497+
std::filesystem::create_directories(dest_path, error);
34923498
if (error) {
34933499
return env->ThrowStdErrException(error, "cp", *dest);
34943500
}
@@ -3630,7 +3636,7 @@ static void CpSyncCopyDir(const FunctionCallbackInfo<Value>& args) {
36303636
return true;
36313637
};
36323638

3633-
copy_dir_contents(std::filesystem::path(*src), std::filesystem::path(*dest));
3639+
copy_dir_contents(src_path, dest_path);
36343640
}
36353641

36363642
BindingData::FilePathIsFileReturnType BindingData::FilePathIsFile(
Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
// Regression test for https://github.com/nodejs/node/issues/61878
2+
// fs.cpSync should copy files when destination path has UTF characters.
3+
import '../common/index.mjs';
4+
import { cpSync, mkdirSync, readdirSync, readFileSync, writeFileSync } from 'node:fs';
5+
import { join } from 'node:path';
6+
import assert from 'node:assert';
7+
import tmpdir from '../common/tmpdir.js';
8+
9+
tmpdir.refresh();
10+
11+
const src = join(tmpdir.path, 'src');
12+
mkdirSync(join(src, 'subdir'), { recursive: true });
13+
writeFileSync(join(src, 'file1.txt'), 'Hello World');
14+
writeFileSync(join(src, 'subdir', 'nested.txt'), 'Nested File');
15+
16+
const dest = join(tmpdir.path, 'Eyjafjallajökull-Pranckevičius');
17+
cpSync(src, dest, { recursive: true, force: true });
18+
19+
const destFiles = readdirSync(dest);
20+
assert.ok(destFiles.includes('file1.txt'));
21+
assert.strictEqual(readFileSync(join(dest, 'file1.txt'), 'utf8'), 'Hello World');
22+
assert.ok(destFiles.includes('subdir'));
23+
assert.strictEqual(readFileSync(join(dest, 'subdir', 'nested.txt'), 'utf8'), 'Nested File');

0 commit comments

Comments
 (0)