fs: fix dereference: false on cpSync

PR-URL: https://github.com/nodejs/node/pull/59681
Reviewed-By: Yagiz Nizipli <yagiz@nizipli.com>
Reviewed-By: Juan José Arboleda <soyjuanarbol@gmail.com>
Reviewed-By: Dario Piotrowicz <dario.piotrowicz@gmail.com>
Reviewed-By: James M Snell <jasnell@gmail.com>
Reviewed-By: Luigi Pinca <luigipinca@gmail.com>
This commit is contained in:
Nicholas Paun 2025-08-31 10:30:53 -07:00 committed by GitHub
parent dddc4a5972
commit fe1a2e33f5
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
3 changed files with 52 additions and 41 deletions

View File

@ -3245,8 +3245,8 @@ static void CpSyncCheckPaths(const FunctionCallbackInfo<Value>& args) {
errorno, dereference ? "stat" : "lstat", nullptr, src.out()); errorno, dereference ? "stat" : "lstat", nullptr, src.out());
} }
auto dest_status = auto dest_status =
dereference ? std::filesystem::symlink_status(dest_path, error_code) dereference ? std::filesystem::status(dest_path, error_code)
: std::filesystem::status(dest_path, error_code); : std::filesystem::symlink_status(dest_path, error_code);
bool dest_exists = !error_code && dest_status.type() != bool dest_exists = !error_code && dest_status.type() !=
std::filesystem::file_type::not_found; std::filesystem::file_type::not_found;

View File

@ -1,39 +0,0 @@
'use strict';
// Refs: https://github.com/nodejs/node/issues/58939
//
// The cpSync function is not correctly handling the `dereference` option.
// In this test, both the cp and cpSync functions are attempting to copy
// a file over a symlinked directory. In the cp case it works fine. In the
// cpSync case it fails with an error.
const common = require('../common');
const {
cp,
cpSync,
mkdirSync,
symlinkSync,
writeFileSync,
} = require('fs');
const {
join,
} = require('path');
const tmpdir = require('../common/tmpdir');
tmpdir.refresh();
const pathA = join(tmpdir.path, 'a');
const pathB = join(tmpdir.path, 'b');
const pathC = join(tmpdir.path, 'c');
const pathD = join(tmpdir.path, 'd');
writeFileSync(pathA, 'file a');
mkdirSync(pathB);
symlinkSync(pathB, pathC, 'dir');
symlinkSync(pathB, pathD, 'dir');
cp(pathA, pathD, { dereference: false }, common.mustSucceed());
cpSync(pathA, pathC, { dereference: false });

View File

@ -0,0 +1,50 @@
'use strict';
// Refs: https://github.com/nodejs/node/issues/58939
//
// In this test, both the cp and cpSync functions are attempting to copy
// a file over a symlinked directory.
const common = require('../common');
const {
cp,
cpSync,
mkdirSync,
symlinkSync,
writeFileSync,
readFileSync,
statSync
} = require('fs');
const {
join,
} = require('path');
const assert = require('assert');
const tmpdir = require('../common/tmpdir');
tmpdir.refresh();
const pathA = join(tmpdir.path, 'a'); // file
const pathB = join(tmpdir.path, 'b'); // directory
const pathC = join(tmpdir.path, 'c'); // c -> b
const pathD = join(tmpdir.path, 'd'); // d -> b
writeFileSync(pathA, 'file a');
mkdirSync(pathB);
symlinkSync(pathB, pathC, 'dir');
symlinkSync(pathB, pathD, 'dir');
cp(pathA, pathD, { dereference: false }, common.mustSucceed(() => {
// The path d is now a file, not a symlink
assert.strictEqual(readFileSync(pathA, 'utf-8'), readFileSync(pathD, 'utf-8'));
assert.ok(statSync(pathA).isFile());
assert.ok(statSync(pathD).isFile());
}));
cpSync(pathA, pathC, { dereference: false });
assert.strictEqual(readFileSync(pathA, 'utf-8'), readFileSync(pathC, 'utf-8'));
assert.ok(statSync(pathA).isFile());
assert.ok(statSync(pathC).isFile());