Skip to content

Commit 46108f8

Browse files
aduh95danielleadams
authored andcommitted
fs: fix error codes for fs.cp
The context passed into this error must have `.code`, `.syscall` and `.message`. Fixes: #41104 PR-URL: #41106 Reviewed-By: Rich Trott <[email protected]> Reviewed-By: Evan Lucas <[email protected]> Reviewed-By: Ruben Bridgewater <[email protected]> Reviewed-By: Luigi Pinca <[email protected]>
1 parent e25671c commit 46108f8

File tree

3 files changed

+27
-1
lines changed

3 files changed

+27
-1
lines changed

lib/.eslintrc.yaml

+2
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,8 @@ rules:
2525
message: "Please use `require('internal/errors').hideStackFrames()` instead."
2626
- selector: "AssignmentExpression:matches([left.name='prepareStackTrace'], [left.property.name='prepareStackTrace'])"
2727
message: "Use 'overrideStackTrace' from 'lib/internal/errors.js' instead of 'Error.prepareStackTrace'."
28+
- selector: "ThrowStatement > NewExpression[callee.name=/^ERR_[A-Z_]+$/] > ObjectExpression:first-child:not(:has([key.name='message']):has([key.name='code']):has([key.name='syscall']))"
29+
message: "The context passed into SystemError constructor must have .code, .syscall and .message."
2830
no-restricted-globals:
2931
- error
3032
- name: AbortController

lib/internal/fs/cp/cp-sync.js

+12
Original file line numberDiff line numberDiff line change
@@ -70,6 +70,7 @@ function checkPathsSync(src, dest, opts) {
7070
path: dest,
7171
syscall: 'cp',
7272
errno: EINVAL,
73+
code: 'EINVAL',
7374
});
7475
}
7576
if (srcStat.isDirectory() && !destStat.isDirectory()) {
@@ -79,6 +80,7 @@ function checkPathsSync(src, dest, opts) {
7980
path: dest,
8081
syscall: 'cp',
8182
errno: EISDIR,
83+
code: 'EISDIR',
8284
});
8385
}
8486
if (!srcStat.isDirectory() && destStat.isDirectory()) {
@@ -88,6 +90,7 @@ function checkPathsSync(src, dest, opts) {
8890
path: dest,
8991
syscall: 'cp',
9092
errno: ENOTDIR,
93+
code: 'ENOTDIR',
9194
});
9295
}
9396
}
@@ -98,6 +101,7 @@ function checkPathsSync(src, dest, opts) {
98101
path: dest,
99102
syscall: 'cp',
100103
errno: EINVAL,
104+
code: 'EINVAL',
101105
});
102106
}
103107
return { srcStat, destStat };
@@ -135,6 +139,7 @@ function checkParentPathsSync(src, srcStat, dest) {
135139
path: dest,
136140
syscall: 'cp',
137141
errno: EINVAL,
142+
code: 'EINVAL',
138143
});
139144
}
140145
return checkParentPathsSync(src, srcStat, destParent);
@@ -170,6 +175,7 @@ function getStats(destStat, src, dest, opts) {
170175
path: src,
171176
syscall: 'cp',
172177
errno: EINVAL,
178+
code: 'EISDIR',
173179
});
174180
} else if (srcStat.isFile() ||
175181
srcStat.isCharacterDevice() ||
@@ -183,20 +189,23 @@ function getStats(destStat, src, dest, opts) {
183189
path: dest,
184190
syscall: 'cp',
185191
errno: EINVAL,
192+
code: 'EINVAL',
186193
});
187194
} else if (srcStat.isFIFO()) {
188195
throw new ERR_FS_CP_FIFO_PIPE({
189196
message: `cannot copy a FIFO pipe: ${dest}`,
190197
path: dest,
191198
syscall: 'cp',
192199
errno: EINVAL,
200+
code: 'EINVAL',
193201
});
194202
}
195203
throw new ERR_FS_CP_UNKNOWN({
196204
message: `cannot copy an unknown file type: ${dest}`,
197205
path: dest,
198206
syscall: 'cp',
199207
errno: EINVAL,
208+
code: 'EINVAL',
200209
});
201210
}
202211

@@ -215,6 +224,7 @@ function mayCopyFile(srcStat, src, dest, opts) {
215224
path: dest,
216225
syscall: 'cp',
217226
errno: EEXIST,
227+
code: 'EEXIST',
218228
});
219229
}
220230
}
@@ -305,6 +315,7 @@ function onLink(destStat, src, dest) {
305315
path: dest,
306316
syscall: 'cp',
307317
errno: EINVAL,
318+
code: 'EINVAL',
308319
});
309320
}
310321
// Prevent copy if src is a subdir of dest since unlinking
@@ -316,6 +327,7 @@ function onLink(destStat, src, dest) {
316327
path: dest,
317328
syscall: 'cp',
318329
errno: EINVAL,
330+
code: 'EINVAL',
319331
});
320332
}
321333
return copyLink(resolvedSrc, dest);

lib/internal/fs/cp/cp.js

+13-1
Original file line numberDiff line numberDiff line change
@@ -82,6 +82,7 @@ async function checkPaths(src, dest, opts) {
8282
path: dest,
8383
syscall: 'cp',
8484
errno: EINVAL,
85+
code: 'EINVAL',
8586
});
8687
}
8788
if (srcStat.isDirectory() && !destStat.isDirectory()) {
@@ -91,6 +92,7 @@ async function checkPaths(src, dest, opts) {
9192
path: dest,
9293
syscall: 'cp',
9394
errno: EISDIR,
95+
code: 'EISDIR',
9496
});
9597
}
9698
if (!srcStat.isDirectory() && destStat.isDirectory()) {
@@ -100,6 +102,7 @@ async function checkPaths(src, dest, opts) {
100102
path: dest,
101103
syscall: 'cp',
102104
errno: ENOTDIR,
105+
code: 'ENOTDIR',
103106
});
104107
}
105108
}
@@ -110,6 +113,7 @@ async function checkPaths(src, dest, opts) {
110113
path: dest,
111114
syscall: 'cp',
112115
errno: EINVAL,
116+
code: 'EINVAL',
113117
});
114118
}
115119
return { srcStat, destStat };
@@ -171,6 +175,7 @@ async function checkParentPaths(src, srcStat, dest) {
171175
path: dest,
172176
syscall: 'cp',
173177
errno: EINVAL,
178+
code: 'EINVAL',
174179
});
175180
}
176181
return checkParentPaths(src, srcStat, destParent);
@@ -209,7 +214,8 @@ async function getStatsForCopy(destStat, src, dest, opts) {
209214
message: `${src} is a directory (not copied)`,
210215
path: src,
211216
syscall: 'cp',
212-
errno: EINVAL,
217+
errno: EISDIR,
218+
code: 'EISDIR',
213219
});
214220
} else if (srcStat.isFile() ||
215221
srcStat.isCharacterDevice() ||
@@ -223,20 +229,23 @@ async function getStatsForCopy(destStat, src, dest, opts) {
223229
path: dest,
224230
syscall: 'cp',
225231
errno: EINVAL,
232+
code: 'EINVAL',
226233
});
227234
} else if (srcStat.isFIFO()) {
228235
throw new ERR_FS_CP_FIFO_PIPE({
229236
message: `cannot copy a FIFO pipe: ${dest}`,
230237
path: dest,
231238
syscall: 'cp',
232239
errno: EINVAL,
240+
code: 'EINVAL',
233241
});
234242
}
235243
throw new ERR_FS_CP_UNKNOWN({
236244
message: `cannot copy an unknown file type: ${dest}`,
237245
path: dest,
238246
syscall: 'cp',
239247
errno: EINVAL,
248+
code: 'EINVAL',
240249
});
241250
}
242251

@@ -255,6 +264,7 @@ async function mayCopyFile(srcStat, src, dest, opts) {
255264
path: dest,
256265
syscall: 'cp',
257266
errno: EEXIST,
267+
code: 'EEXIST',
258268
});
259269
}
260270
}
@@ -355,6 +365,7 @@ async function onLink(destStat, src, dest) {
355365
path: dest,
356366
syscall: 'cp',
357367
errno: EINVAL,
368+
code: 'EINVAL',
358369
});
359370
}
360371
// Do not copy if src is a subdir of dest since unlinking
@@ -367,6 +378,7 @@ async function onLink(destStat, src, dest) {
367378
path: dest,
368379
syscall: 'cp',
369380
errno: EINVAL,
381+
code: 'EINVAL',
370382
});
371383
}
372384
return copyLink(resolvedSrc, dest);

0 commit comments

Comments
 (0)