Skip to content

Commit 49bc013

Browse files
committed
fs: fix incorrect dereferencing of src arg in cpSync
1 parent 746c3c2 commit 49bc013

File tree

3 files changed

+48
-2
lines changed

3 files changed

+48
-2
lines changed

src/node_file.cc

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3210,8 +3210,8 @@ static void CpSyncCheckPaths(const FunctionCallbackInfo<Value>& args) {
32103210

32113211
std::error_code error_code;
32123212
auto src_status = dereference
3213-
? std::filesystem::symlink_status(src_path, error_code)
3214-
: std::filesystem::status(src_path, error_code);
3213+
? std::filesystem::status(src_path, error_code)
3214+
: std::filesystem::symlink_status(src_path, error_code);
32153215

32163216
if (error_code) {
32173217
#ifdef _WIN32
Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
'use strict';
2+
3+
// Refs: https://github.com/nodejs/node/issues/58939
4+
//
5+
// In this test, both the cp and cpSync functions are attempting to copy
6+
// a file over a symlinked directory.
7+
8+
const common = require('../common');
9+
10+
const {
11+
cp,
12+
cpSync,
13+
mkdirSync,
14+
symlinkSync,
15+
writeFileSync,
16+
readFileSync,
17+
statSync,
18+
lstatSync,
19+
} = require('fs');
20+
21+
const {
22+
join,
23+
} = require('path');
24+
25+
const assert = require('assert');
26+
27+
const tmpdir = require('../common/tmpdir');
28+
tmpdir.refresh();
29+
30+
const pathA = join(tmpdir.path, 'a');
31+
const pathB = join(tmpdir.path, 'b');
32+
const pathC = join(tmpdir.path, 'c');
33+
const pathD = join(tmpdir.path, 'd');
34+
const pathDC = join(pathD, 'c');
35+
36+
writeFileSync(pathA, 'file a');
37+
assert.ok(statSync(pathA).isFile());
38+
symlinkSync(pathA, pathC); // c -> a
39+
assert.ok(lstatSync(pathC).isSymbolicLink());
40+
assert.ok(statSync(pathC).isFile());
41+
42+
cpSync(pathC, pathD, {dereference: true});
43+
// d/c -> a
44+
// d/c
45+
assert.ok(statSync(pathD).isFile());
46+
assert.strictEqual(readFileSync(pathD, 'utf8'), 'file a');

0 commit comments

Comments
 (0)