GeezerVison is showing github.com — fs: stat src through symlinks in cpSync() when dereference is set · gabylb/node@57a9c49
File treeExpand file treeCollapse file tree Open diff view settings Expand file treeCollapse file tree Open diff view settings Collapse file Copy file name to clipboardExpand all lines: src/node_file.cc +3-5Lines changed: 3 additions & 5 deletions | Original file line number | Diff line number | Diff line change |
|---|
@@ -4567,8 +4567,8 @@ static void CpSyncCheckPaths(const FunctionCallbackInfo<Value>& args) { | 4567 | 4567 |
| 4568 | 4568 | std::error_code error_code; | 4569 | 4569 | auto src_status = dereference | 4570 | | - ? std::filesystem::symlink_status(src_path, error_code) | 4571 | | - : std::filesystem::status(src_path, error_code); | | 4570 | + ? std::filesystem::status(src_path, error_code) | | 4571 | + : std::filesystem::symlink_status(src_path, error_code); | 4572 | 4572 |
| 4573 | 4573 | if (error_code) { | 4574 | 4574 | #ifdef _WIN32 | @@ -4586,9 +4586,7 @@ static void CpSyncCheckPaths(const FunctionCallbackInfo<Value>& args) { | 4586 | 4586 |
| 4587 | 4587 | bool dest_exists = !error_code && dest_status.type() != | 4588 | 4588 | std::filesystem::file_type::not_found; | 4589 | | - bool src_is_dir = | 4590 | | - (src_status.type() == std::filesystem::file_type::directory) || | 4591 | | - (dereference && src_status.type() == std::filesystem::file_type::symlink); | | 4589 | + bool src_is_dir = src_status.type() == std::filesystem::file_type::directory; | 4592 | 4590 |
| 4593 | 4591 | auto src_path_str = ConvertPathToUTF8(src_path); | 4594 | 4592 | auto dest_path_str = ConvertPathToUTF8(dest_path); | |
Collapse file Copy file name to clipboard +42Lines changed: 42 additions & 0 deletions | Original file line number | Diff line number | Diff line change |
|---|
| | 1 | +// This tests that cp() and cpSync() copy the target file of a symlink passed | | 2 | +// as src when dereference is true and recursive is not set. | | 3 | + | | 4 | +import { mustCall, mustNotMutateObjectDeep } from '../common/index.mjs'; | | 5 | +import { nextdir } from '../common/fs.js'; | | 6 | +import assert from 'node:assert'; | | 7 | +import { | | 8 | + cp, | | 9 | + cpSync, | | 10 | + lstatSync, | | 11 | + mkdirSync, | | 12 | + readFileSync, | | 13 | + symlinkSync, | | 14 | + writeFileSync, | | 15 | +} from 'node:fs'; | | 16 | +import { join } from 'node:path'; | | 17 | +import tmpdir from '../common/tmpdir.js'; | | 18 | + | | 19 | +tmpdir.refresh(); | | 20 | + | | 21 | +const src = nextdir(); | | 22 | +mkdirSync(src); | | 23 | +writeFileSync(join(src, 'foo.txt'), 'foo'); | | 24 | +symlinkSync(join(src, 'foo.txt'), join(src, 'link.txt')); | | 25 | + | | 26 | +const opts = mustNotMutateObjectDeep({ dereference: true }); | | 27 | + | | 28 | +{ | | 29 | + const dest = join(nextdir(), 'foo.txt'); | | 30 | + cpSync(join(src, 'link.txt'), dest, opts); | | 31 | + assert(lstatSync(dest).isFile()); | | 32 | + assert.strictEqual(readFileSync(dest, 'utf8'), 'foo'); | | 33 | +} | | 34 | + | | 35 | +{ | | 36 | + const dest = join(nextdir(), 'foo.txt'); | | 37 | + cp(join(src, 'link.txt'), dest, opts, mustCall((err) => { | | 38 | + assert.ifError(err); | | 39 | + assert(lstatSync(dest).isFile()); | | 40 | + assert.strictEqual(readFileSync(dest, 'utf8'), 'foo'); | | 41 | + })); | | 42 | +} |
|