MCPcopy Index your code
hub / github.com/nodejs/node / onLink

Function onLink

lib/internal/fs/cp/cp.js:334–384  ·  view source on GitHub ↗
(destStat, src, dest, opts)

Source from the content-addressed store, hash-verified

332}
333
334async 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
386async function copyLink(resolvedSrc, dest) {
387 await unlink(dest);

Callers 1

getStatsForCopyFunction · 0.70

Calls 9

isAbsoluteFunction · 0.85
dirnameFunction · 0.85
isSrcSubdirFunction · 0.85
copyLinkFunction · 0.70
readlinkFunction · 0.50
resolveFunction · 0.50
symlinkFunction · 0.50
statFunction · 0.50
isDirectoryMethod · 0.45

Tested by

no test coverage detected

Used in the wild real call sites across dependent graphs

searching dependent graphs…