| FazBrowse GitHub Viewer | Trending | | Home |
| Tools: [Download Repo ZIP] [Original HTTPS Page] |
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
@@ -3111,9 +3111,12 @@ static void CpSyncCheckPaths(const FunctionCallbackInfo<Value>& args) { | |||
| 3111 | 3111 | } | |
| 3112 | 3112 | ||
| 3113 | 3113 | std::u8string dest_path_str = dest_path.u8string(); | |
| 3114 | - | ||
| 3114 | + std::u8string src_path_str = src_path.u8string(); | ||
| 3115 | + if (!src_path_str.ends_with(std::filesystem::path::preferred_separator)) { | ||
| 3116 | + src_path_str += std::filesystem::path::preferred_separator; | ||
| 3117 | + } | ||
| 3115 | 3118 | // Check if dest_path is a subdirectory of src_path. | |
| 3116 | - if (src_is_dir && dest_path_str.starts_with(src_path.u8string())) { | ||
| 3119 | + if (src_is_dir && dest_path_str.starts_with(src_path_str)) { | ||
| 3117 | 3120 | std::u8string message = u8"Cannot copy " + src_path.u8string() + | |
| 3118 | 3121 | u8" to a subdirectory of self " + | |
| 3119 | 3122 | dest_path.u8string(); | |
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
@@ -24,8 +24,8 @@ import tmpdir from '../common/tmpdir.js'; | |||
| 24 | 24 | tmpdir.refresh(); | |
| 25 | 25 | ||
| 26 | 26 | let dirc = 0; | |
| 27 | - function nextdir() { | ||
| 28 | - return tmpdir.resolve(`copy_${++dirc}`); | ||
| 27 | + function nextdir(dirname) { | ||
| 28 | + return tmpdir.resolve(dirname || `copy_${++dirc}`); | ||
| 29 | 29 | } | |
| 30 | 30 | ||
| 31 | 31 | // Synchronous implementation of copy. | |
@@ -320,6 +320,45 @@ function nextdir() { | |||
| 320 | 320 | ); | |
| 321 | 321 | } | |
| 322 | 322 | ||
| 323 | + // It must not throw error if attempt is made to copy to dest | ||
| 324 | + // directory with same prefix as src directory | ||
| 325 | + // regression test for https://github.com/nodejs/node/issues/54285 | ||
| 326 | + { | ||
| 327 | + const src = nextdir('prefix'); | ||
| 328 | + const dest = nextdir('prefix-a'); | ||
| 329 | + mkdirSync(src); | ||
| 330 | + mkdirSync(dest); | ||
| 331 | + cpSync(src, dest, mustNotMutateObjectDeep({ recursive: true })); | ||
| 332 | + } | ||
| 333 | + | ||
| 334 | + // It must not throw error if attempt is made to copy to dest | ||
| 335 | + // directory if the parent of dest has same prefix as src directory | ||
| 336 | + // regression test for https://github.com/nodejs/node/issues/54285 | ||
| 337 | + { | ||
| 338 | + const src = nextdir('aa'); | ||
| 339 | + const destParent = nextdir('aaa'); | ||
| 340 | + const dest = nextdir('aaa/aabb'); | ||
| 341 | + mkdirSync(src); | ||
| 342 | + mkdirSync(destParent); | ||
| 343 | + mkdirSync(dest); | ||
| 344 | + cpSync(src, dest, mustNotMutateObjectDeep({ recursive: true })); | ||
| 345 | + } | ||
| 346 | + | ||
| 347 | + // It throws error if attempt is made to copy src to dest | ||
| 348 | + // when src is parent directory of the parent of dest | ||
| 349 | + { | ||
| 350 | + const src = nextdir('a'); | ||
| 351 | + const destParent = nextdir('a/b'); | ||
| 352 | + const dest = nextdir('a/b/c'); | ||
| 353 | + mkdirSync(src); | ||
| 354 | + mkdirSync(destParent); | ||
| 355 | + mkdirSync(dest); | ||
| 356 | + assert.throws( | ||
| 357 | + () => cpSync(src, dest, mustNotMutateObjectDeep({ recursive: true })), | ||
| 358 | + { code: 'ERR_FS_CP_EINVAL' }, | ||
| 359 | + ); | ||
| 360 | + } | ||
| 361 | + | ||
| 323 | 362 | // It throws error if attempt is made to copy to subdirectory of self. | |
| 324 | 363 | { | |
| 325 | 364 | const src = './test/fixtures/copy/kitchen-sink'; | |
| Back | FazBrowse Home | New Git URL |
0 commit comments