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: #41104

PR-URL: #41106
Reviewed-By: Rich Trott <rtrott@gmail.com>
Reviewed-By: Evan Lucas <evanlucas@me.com>
Reviewed-By: Ruben Bridgewater <ruben@bridgewater.de>
Reviewed-By: Luigi Pinca <luigipinca@gmail.com>
  • Loading branch information
aduh95 authored and danielleadams committed Dec 13, 2021
1 parent e25671c commit 46108f8
Show file tree
Hide file tree
Showing 3 changed files with 27 additions and 1 deletion.
2 changes: 2 additions & 0 deletions lib/.eslintrc.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,8 @@ rules:
message: "Please use `require('internal/errors').hideStackFrames()` instead."
- selector: "AssignmentExpression:matches([left.name='prepareStackTrace'], [left.property.name='prepareStackTrace'])"
message: "Use 'overrideStackTrace' from 'lib/internal/errors.js' instead of 'Error.prepareStackTrace'."
- selector: "ThrowStatement > NewExpression[callee.name=/^ERR_[A-Z_]+$/] > ObjectExpression:first-child:not(:has([key.name='message']):has([key.name='code']):has([key.name='syscall']))"
message: "The context passed into SystemError constructor must have .code, .syscall and .message."
no-restricted-globals:
- error
- name: AbortController
Expand Down
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 46108f8

Please sign in to comment.