Skip to content

Commit f9447b7

Browse files
Linkgoronjasnell
authored andcommitted
fs: fix rmsync error swallowing
fix rmsync swallowing errors instead of throwing them. fixes: #38683 fixes: #34580 PR-URL: #38684 Fixes: #38683 Fixes: #34580 Reviewed-By: Antoine du Hamel <duhamelantoine1995@gmail.com> Reviewed-By: Rich Trott <rtrott@gmail.com> Reviewed-By: Darshan Sen <raisinten@gmail.com> Reviewed-By: James M Snell <jasnell@gmail.com>
1 parent fa7cdd6 commit f9447b7

9 files changed

+286
-154
lines changed

lib/internal/fs/rimraf.js

+15-2
Original file line numberDiff line numberDiff line change
@@ -219,6 +219,11 @@ function _unlinkSync(path, options) {
219219
i < tries &&
220220
options.retryDelay > 0) {
221221
sleep(i * options.retryDelay);
222+
} else if (err.code === 'ENOENT') {
223+
// The file is already gone.
224+
return;
225+
} else if (i === tries) {
226+
throw err;
222227
}
223228
}
224229
}
@@ -231,8 +236,9 @@ function _rmdirSync(path, options, originalErr) {
231236
} catch (err) {
232237
if (err.code === 'ENOENT')
233238
return;
234-
if (err.code === 'ENOTDIR')
235-
throw originalErr;
239+
if (err.code === 'ENOTDIR') {
240+
throw originalErr || err;
241+
}
236242

237243
if (notEmptyErrorCodes.has(err.code)) {
238244
// Removing failed. Try removing all children and then retrying the
@@ -259,10 +265,17 @@ function _rmdirSync(path, options, originalErr) {
259265
i < tries &&
260266
options.retryDelay > 0) {
261267
sleep(i * options.retryDelay);
268+
} else if (err.code === 'ENOENT') {
269+
// The file is already gone.
270+
return;
271+
} else if (i === tries) {
272+
throw err;
262273
}
263274
}
264275
}
265276
}
277+
278+
throw originalErr || err;
266279
}
267280
}
268281

test/parallel/test-fs-mkdir-recursive-eaccess.js

+2-3
Original file line numberDiff line numberDiff line change
@@ -26,8 +26,7 @@ function makeDirectoryReadOnly(dir) {
2626
let accessErrorCode = 'EACCES';
2727
if (common.isWindows) {
2828
accessErrorCode = 'EPERM';
29-
execSync(`icacls ${dir} /inheritance:r`);
30-
execSync(`icacls ${dir} /deny "everyone":W`);
29+
execSync(`icacls ${dir} /deny "everyone:(OI)(CI)(DE,DC,AD,WD)"`);
3130
} else {
3231
fs.chmodSync(dir, '444');
3332
}
@@ -36,7 +35,7 @@ function makeDirectoryReadOnly(dir) {
3635

3736
function makeDirectoryWritable(dir) {
3837
if (common.isWindows) {
39-
execSync(`icacls ${dir} /grant "everyone":W`);
38+
execSync(`icacls ${dir} /remove:d "everyone"`);
4039
}
4140
}
4241

test/parallel/test-fs-open-no-close.js

+14-7
Original file line numberDiff line numberDiff line change
@@ -15,10 +15,17 @@ const debuglog = (arg) => {
1515
const tmpdir = require('../common/tmpdir');
1616
tmpdir.refresh();
1717

18-
{
19-
fs.open(`${tmpdir.path}/dummy`, 'wx+', common.mustCall((err, fd) => {
20-
debuglog('fs open() callback');
21-
assert.ifError(err);
22-
}));
23-
debuglog('waiting for callback');
24-
}
18+
let openFd;
19+
20+
fs.open(`${tmpdir.path}/dummy`, 'wx+', common.mustCall((err, fd) => {
21+
debuglog('fs open() callback');
22+
assert.ifError(err);
23+
openFd = fd;
24+
}));
25+
debuglog('waiting for callback');
26+
27+
process.on('beforeExit', common.mustCall(() => {
28+
if (openFd) {
29+
fs.closeSync(openFd);
30+
}
31+
}));

test/parallel/test-fs-promises-file-handle-read-worker.js

+13-9
Original file line numberDiff line numberDiff line change
@@ -19,16 +19,20 @@ if (isMainThread || !workerData) {
1919
transferList: [handle]
2020
});
2121
});
22-
fs.promises.open(file, 'r').then((handle) => {
23-
fs.createReadStream(null, { fd: handle });
24-
assert.throws(() => {
25-
new Worker(__filename, {
26-
workerData: { handle },
27-
transferList: [handle]
22+
fs.promises.open(file, 'r').then(async (handle) => {
23+
try {
24+
fs.createReadStream(null, { fd: handle });
25+
assert.throws(() => {
26+
new Worker(__filename, {
27+
workerData: { handle },
28+
transferList: [handle]
29+
});
30+
}, {
31+
code: 25,
2832
});
29-
}, {
30-
code: 25,
31-
});
33+
} finally {
34+
await handle.close();
35+
}
3236
});
3337
} else {
3438
let output = '';

test/parallel/test-fs-promises-file-handle-readFile.js

+10-7
Original file line numberDiff line numberDiff line change
@@ -56,13 +56,16 @@ async function doReadAndCancel() {
5656
{
5757
const filePathForHandle = path.resolve(tmpDir, 'dogs-running.txt');
5858
const fileHandle = await open(filePathForHandle, 'w+');
59-
const buffer = Buffer.from('Dogs running'.repeat(10000), 'utf8');
60-
fs.writeFileSync(filePathForHandle, buffer);
61-
const signal = AbortSignal.abort();
62-
await assert.rejects(readFile(fileHandle, { signal }), {
63-
name: 'AbortError'
64-
});
65-
await fileHandle.close();
59+
try {
60+
const buffer = Buffer.from('Dogs running'.repeat(10000), 'utf8');
61+
fs.writeFileSync(filePathForHandle, buffer);
62+
const signal = AbortSignal.abort();
63+
await assert.rejects(readFile(fileHandle, { signal }), {
64+
name: 'AbortError'
65+
});
66+
} finally {
67+
await fileHandle.close();
68+
}
6669
}
6770

6871
// Signal aborted on first tick

test/parallel/test-fs-promises-file-handle-writeFile.js

+11-7
Original file line numberDiff line numberDiff line change
@@ -30,13 +30,17 @@ async function validateWriteFile() {
3030
async function doWriteAndCancel() {
3131
const filePathForHandle = path.resolve(tmpDir, 'dogs-running.txt');
3232
const fileHandle = await open(filePathForHandle, 'w+');
33-
const buffer = Buffer.from('dogs running'.repeat(512 * 1024), 'utf8');
34-
const controller = new AbortController();
35-
const { signal } = controller;
36-
process.nextTick(() => controller.abort());
37-
await assert.rejects(writeFile(fileHandle, buffer, { signal }), {
38-
name: 'AbortError'
39-
});
33+
try {
34+
const buffer = Buffer.from('dogs running'.repeat(512 * 1024), 'utf8');
35+
const controller = new AbortController();
36+
const { signal } = controller;
37+
process.nextTick(() => controller.abort());
38+
await assert.rejects(writeFile(fileHandle, buffer, { signal }), {
39+
name: 'AbortError'
40+
});
41+
} finally {
42+
await fileHandle.close();
43+
}
4044
}
4145

4246
validateWriteFile()

0 commit comments

Comments
 (0)