Skip to content

Commit

Permalink
fs: fix error codes for fs.cp
Browse files Browse the repository at this point in the history
The context passed into this error must have `.code`, `.syscall` and
`.message`.

Fixes: nodejs#41104
  • Loading branch information
aduh95 committed Dec 7, 2021
1 parent c802c4b commit 59c80a2
Show file tree
Hide file tree
Showing 2 changed files with 25 additions and 1 deletion.
12 changes: 12 additions & 0 deletions lib/internal/fs/cp/cp-sync.js
Original file line number Diff line number Diff line change
Expand Up @@ -70,6 +70,7 @@ function checkPathsSync(src, dest, opts) {
path: dest,
syscall: 'cp',
errno: EINVAL,
code: 'EINVAL',
});
}
if (srcStat.isDirectory() && !destStat.isDirectory()) {
Expand All @@ -79,6 +80,7 @@ function checkPathsSync(src, dest, opts) {
path: dest,
syscall: 'cp',
errno: EISDIR,
code: 'EISDIR',
});
}
if (!srcStat.isDirectory() && destStat.isDirectory()) {
Expand All @@ -88,6 +90,7 @@ function checkPathsSync(src, dest, opts) {
path: dest,
syscall: 'cp',
errno: ENOTDIR,
code: 'ENOTDIR',
});
}
}
Expand All @@ -98,6 +101,7 @@ function checkPathsSync(src, dest, opts) {
path: dest,
syscall: 'cp',
errno: EINVAL,
code: 'EINVAL',
});
}
return { srcStat, destStat };
Expand Down Expand Up @@ -135,6 +139,7 @@ function checkParentPathsSync(src, srcStat, dest) {
path: dest,
syscall: 'cp',
errno: EINVAL,
code: 'EINVAL',
});
}
return checkParentPathsSync(src, srcStat, destParent);
Expand Down Expand Up @@ -170,6 +175,7 @@ function getStats(destStat, src, dest, opts) {
path: src,
syscall: 'cp',
errno: EINVAL,
code: 'EISDIR',
});
} else if (srcStat.isFile() ||
srcStat.isCharacterDevice() ||
Expand All @@ -183,20 +189,23 @@ function getStats(destStat, src, dest, opts) {
path: dest,
syscall: 'cp',
errno: EINVAL,
code: 'EINVAL',
});
} else if (srcStat.isFIFO()) {
throw new ERR_FS_CP_FIFO_PIPE({
message: `cannot copy a FIFO pipe: ${dest}`,
path: dest,
syscall: 'cp',
errno: EINVAL,
code: 'EINVAL',
});
}
throw new ERR_FS_CP_UNKNOWN({
message: `cannot copy an unknown file type: ${dest}`,
path: dest,
syscall: 'cp',
errno: EINVAL,
code: 'EINVAL',
});
}

Expand All @@ -215,6 +224,7 @@ function mayCopyFile(srcStat, src, dest, opts) {
path: dest,
syscall: 'cp',
errno: EEXIST,
code: 'EEXIST',
});
}
}
Expand Down Expand Up @@ -305,6 +315,7 @@ function onLink(destStat, src, dest) {
path: dest,
syscall: 'cp',
errno: EINVAL,
code: 'EINVAL',
});
}
// Prevent copy if src is a subdir of dest since unlinking
Expand All @@ -316,6 +327,7 @@ function onLink(destStat, src, dest) {
path: dest,
syscall: 'cp',
errno: EINVAL,
code: 'EINVAL',
});
}
return copyLink(resolvedSrc, dest);
Expand Down
14 changes: 13 additions & 1 deletion lib/internal/fs/cp/cp.js
Original file line number Diff line number Diff line change
Expand Up @@ -82,6 +82,7 @@ async function checkPaths(src, dest, opts) {
path: dest,
syscall: 'cp',
errno: EINVAL,
code: 'EINVAL',
});
}
if (srcStat.isDirectory() && !destStat.isDirectory()) {
Expand All @@ -91,6 +92,7 @@ async function checkPaths(src, dest, opts) {
path: dest,
syscall: 'cp',
errno: EISDIR,
code: 'EISDIR',
});
}
if (!srcStat.isDirectory() && destStat.isDirectory()) {
Expand All @@ -100,6 +102,7 @@ async function checkPaths(src, dest, opts) {
path: dest,
syscall: 'cp',
errno: ENOTDIR,
code: 'ENOTDIR',
});
}
}
Expand All @@ -110,6 +113,7 @@ async function checkPaths(src, dest, opts) {
path: dest,
syscall: 'cp',
errno: EINVAL,
code: 'EINVAL',
});
}
return { srcStat, destStat };
Expand Down Expand Up @@ -171,6 +175,7 @@ async function checkParentPaths(src, srcStat, dest) {
path: dest,
syscall: 'cp',
errno: EINVAL,
code: 'EINVAL',
});
}
return checkParentPaths(src, srcStat, destParent);
Expand Down Expand Up @@ -209,7 +214,8 @@ async function getStatsForCopy(destStat, src, dest, opts) {
message: `${src} is a directory (not copied)`,
path: src,
syscall: 'cp',
errno: EINVAL,
errno: EISDIR,
code: 'EISDIR',
});
} else if (srcStat.isFile() ||
srcStat.isCharacterDevice() ||
Expand All @@ -223,20 +229,23 @@ async function getStatsForCopy(destStat, src, dest, opts) {
path: dest,
syscall: 'cp',
errno: EINVAL,
code: 'EINVAL',
});
} else if (srcStat.isFIFO()) {
throw new ERR_FS_CP_FIFO_PIPE({
message: `cannot copy a FIFO pipe: ${dest}`,
path: dest,
syscall: 'cp',
errno: EINVAL,
code: 'EINVAL',
});
}
throw new ERR_FS_CP_UNKNOWN({
message: `cannot copy an unknown file type: ${dest}`,
path: dest,
syscall: 'cp',
errno: EINVAL,
code: 'EINVAL',
});
}

Expand All @@ -255,6 +264,7 @@ async function mayCopyFile(srcStat, src, dest, opts) {
path: dest,
syscall: 'cp',
errno: EEXIST,
code: 'EEXIST',
});
}
}
Expand Down Expand Up @@ -355,6 +365,7 @@ async function onLink(destStat, src, dest) {
path: dest,
syscall: 'cp',
errno: EINVAL,
code: 'EINVAL',
});
}
// Do not copy if src is a subdir of dest since unlinking
Expand All @@ -367,6 +378,7 @@ async function onLink(destStat, src, dest) {
path: dest,
syscall: 'cp',
errno: EINVAL,
code: 'EINVAL',
});
}
return copyLink(resolvedSrc, dest);
Expand Down

0 comments on commit 59c80a2

Please sign in to comment.