Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fs: fix error codes for fs.cp #41106

Merged
merged 3 commits into from
Dec 10, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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