GeezerVison is showing github.com — fs: fix cpSync() abort when dest is a dangling symlink · nodejs/node@942fd1b
File tree
Expand 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 +4-2Lines changed: 4 additions & 2 deletions | Original file line number | Diff line number | Diff line change |
|---|
@@ -4643,8 +4643,10 @@ static void CpSyncCheckPaths(const FunctionCallbackInfo<Value>& args) { |
4643 | 4643 | auto dest_path_str = ConvertPathToUTF8(dest_path); |
4644 | 4644 |
|
4645 | 4645 | if (!error_code) { |
4646 | | - // Check if src and dest are identical. |
4647 | | - if (std::filesystem::equivalent(src_path, dest_path)) { |
| 4646 | + // Check if src and dest are identical. A dangling dest symlink makes |
| 4647 | + // equivalent() fail, which means they are not the same file. |
| 4648 | + std::error_code equivalent_error; |
| 4649 | + if (std::filesystem::equivalent(src_path, dest_path, equivalent_error)) { |
4648 | 4650 | static constexpr const char* message = |
4649 | 4651 | "src and dest cannot be the same %s"; |
4650 | 4652 | return THROW_ERR_FS_CP_EINVAL(env, message, dest_path_str); |
|
Collapse file
Copy file name to clipboard +30Lines changed: 30 additions & 0 deletions | Original file line number | Diff line number | Diff line change |
|---|
|
| 1 | +// This tests that cpSync() replaces a dangling symlink in dest with a file |
| 2 | +// instead of aborting the process. |
| 3 | +import { mustNotMutateObjectDeep } from '../common/index.mjs'; |
| 4 | +import { nextdir } from '../common/fs.js'; |
| 5 | +import assert from 'node:assert'; |
| 6 | +import { |
| 7 | + cpSync, |
| 8 | + existsSync, |
| 9 | + lstatSync, |
| 10 | + mkdirSync, |
| 11 | + readFileSync, |
| 12 | + symlinkSync, |
| 13 | + writeFileSync, |
| 14 | +} from 'node:fs'; |
| 15 | +import { join } from 'node:path'; |
| 16 | +import tmpdir from '../common/tmpdir.js'; |
| 17 | + |
| 18 | +tmpdir.refresh(); |
| 19 | + |
| 20 | +const dir = nextdir(); |
| 21 | +mkdirSync(dir); |
| 22 | +const src = join(dir, 'src'); |
| 23 | +const dest = join(dir, 'dest'); |
| 24 | +writeFileSync(src, 'data'); |
| 25 | +symlinkSync('missing', dest); |
| 26 | + |
| 27 | +cpSync(src, dest, mustNotMutateObjectDeep({ force: true })); |
| 28 | +assert(lstatSync(dest).isFile()); |
| 29 | +assert.strictEqual(readFileSync(dest, 'utf8'), 'data'); |
| 30 | +assert(!existsSync(join(dir, 'missing'))); |