(destStat, src, dest, opts)
| 332 | } |
| 333 | |
| 334 | async function onLink(destStat, src, dest, opts) { |
| 335 | let resolvedSrc = await readlink(src); |
| 336 | if (!opts.verbatimSymlinks && !isAbsolute(resolvedSrc)) { |
| 337 | resolvedSrc = resolve(dirname(src), resolvedSrc); |
| 338 | } |
| 339 | if (!destStat) { |
| 340 | return symlink(resolvedSrc, dest); |
| 341 | } |
| 342 | let resolvedDest; |
| 343 | try { |
| 344 | resolvedDest = await readlink(dest); |
| 345 | } catch (err) { |
| 346 | // Dest exists and is a regular file or directory, |
| 347 | // Windows may throw UNKNOWN error. If dest already exists, |
| 348 | // fs throws error anyway, so no need to guard against it here. |
| 349 | if (err.code === 'EINVAL' || err.code === 'UNKNOWN') { |
| 350 | return symlink(resolvedSrc, dest); |
| 351 | } |
| 352 | throw err; |
| 353 | } |
| 354 | if (!isAbsolute(resolvedDest)) { |
| 355 | resolvedDest = resolve(dirname(dest), resolvedDest); |
| 356 | } |
| 357 | |
| 358 | const srcIsDir = fsBinding.internalModuleStat(src) === 1; |
| 359 | |
| 360 | if (srcIsDir && isSrcSubdir(resolvedSrc, resolvedDest)) { |
| 361 | throw new ERR_FS_CP_EINVAL({ |
| 362 | message: `cannot copy ${resolvedSrc} to a subdirectory of self ` + |
| 363 | `${resolvedDest}`, |
| 364 | path: dest, |
| 365 | syscall: 'cp', |
| 366 | errno: EINVAL, |
| 367 | code: 'EINVAL', |
| 368 | }); |
| 369 | } |
| 370 | // Do not copy if src is a subdir of dest since unlinking |
| 371 | // dest in this case would result in removing src contents |
| 372 | // and therefore a broken symlink would be created. |
| 373 | const srcStat = await stat(src); |
| 374 | if (srcStat.isDirectory() && isSrcSubdir(resolvedDest, resolvedSrc)) { |
| 375 | throw new ERR_FS_CP_SYMLINK_TO_SUBDIRECTORY({ |
| 376 | message: `cannot overwrite ${resolvedDest} with ${resolvedSrc}`, |
| 377 | path: dest, |
| 378 | syscall: 'cp', |
| 379 | errno: EINVAL, |
| 380 | code: 'EINVAL', |
| 381 | }); |
| 382 | } |
| 383 | return copyLink(resolvedSrc, dest); |
| 384 | } |
| 385 | |
| 386 | async function copyLink(resolvedSrc, dest) { |
| 387 | await unlink(dest); |
no test coverage detected
searching dependent graphs…