Skip to content

Commit

Permalink
fs: fix rmsync error swallowing
Browse files Browse the repository at this point in the history
fix rmsync swallowing errors instead of throwing them.

fixes: #38683
fixes: #34580

PR-URL: #38684
Fixes: #38683
Fixes: #34580
Reviewed-By: Antoine du Hamel <[email protected]>
Reviewed-By: Rich Trott <[email protected]>
Reviewed-By: Darshan Sen <[email protected]>
Reviewed-By: James M Snell <[email protected]>
  • Loading branch information
Linkgoron authored and jasnell committed May 25, 2021
1 parent fa7cdd6 commit f9447b7
Show file tree
Hide file tree
Showing 9 changed files with 286 additions and 154 deletions.
17 changes: 15 additions & 2 deletions lib/internal/fs/rimraf.js
Original file line number Diff line number Diff line change
Expand Up @@ -219,6 +219,11 @@ function _unlinkSync(path, options) {
i < tries &&
options.retryDelay > 0) {
sleep(i * options.retryDelay);
} else if (err.code === 'ENOENT') {
// The file is already gone.
return;
} else if (i === tries) {
throw err;
}
}
}
Expand All @@ -231,8 +236,9 @@ function _rmdirSync(path, options, originalErr) {
} catch (err) {
if (err.code === 'ENOENT')
return;
if (err.code === 'ENOTDIR')
throw originalErr;
if (err.code === 'ENOTDIR') {
throw originalErr || err;
}

if (notEmptyErrorCodes.has(err.code)) {
// Removing failed. Try removing all children and then retrying the
Expand All @@ -259,10 +265,17 @@ function _rmdirSync(path, options, originalErr) {
i < tries &&
options.retryDelay > 0) {
sleep(i * options.retryDelay);
} else if (err.code === 'ENOENT') {
// The file is already gone.
return;
} else if (i === tries) {
throw err;
}
}
}
}

throw originalErr || err;
}
}

Expand Down
5 changes: 2 additions & 3 deletions test/parallel/test-fs-mkdir-recursive-eaccess.js
Original file line number Diff line number Diff line change
Expand Up @@ -26,8 +26,7 @@ function makeDirectoryReadOnly(dir) {
let accessErrorCode = 'EACCES';
if (common.isWindows) {
accessErrorCode = 'EPERM';
execSync(`icacls ${dir} /inheritance:r`);
execSync(`icacls ${dir} /deny "everyone":W`);
execSync(`icacls ${dir} /deny "everyone:(OI)(CI)(DE,DC,AD,WD)"`);
} else {
fs.chmodSync(dir, '444');
}
Expand All @@ -36,7 +35,7 @@ function makeDirectoryReadOnly(dir) {

function makeDirectoryWritable(dir) {
if (common.isWindows) {
execSync(`icacls ${dir} /grant "everyone":W`);
execSync(`icacls ${dir} /remove:d "everyone"`);
}
}

Expand Down
21 changes: 14 additions & 7 deletions test/parallel/test-fs-open-no-close.js
Original file line number Diff line number Diff line change
Expand Up @@ -15,10 +15,17 @@ const debuglog = (arg) => {
const tmpdir = require('../common/tmpdir');
tmpdir.refresh();

{
fs.open(`${tmpdir.path}/dummy`, 'wx+', common.mustCall((err, fd) => {
debuglog('fs open() callback');
assert.ifError(err);
}));
debuglog('waiting for callback');
}
let openFd;

fs.open(`${tmpdir.path}/dummy`, 'wx+', common.mustCall((err, fd) => {
debuglog('fs open() callback');
assert.ifError(err);
openFd = fd;
}));
debuglog('waiting for callback');

process.on('beforeExit', common.mustCall(() => {
if (openFd) {
fs.closeSync(openFd);
}
}));
22 changes: 13 additions & 9 deletions test/parallel/test-fs-promises-file-handle-read-worker.js
Original file line number Diff line number Diff line change
Expand Up @@ -19,16 +19,20 @@ if (isMainThread || !workerData) {
transferList: [handle]
});
});
fs.promises.open(file, 'r').then((handle) => {
fs.createReadStream(null, { fd: handle });
assert.throws(() => {
new Worker(__filename, {
workerData: { handle },
transferList: [handle]
fs.promises.open(file, 'r').then(async (handle) => {
try {
fs.createReadStream(null, { fd: handle });
assert.throws(() => {
new Worker(__filename, {
workerData: { handle },
transferList: [handle]
});
}, {
code: 25,
});
}, {
code: 25,
});
} finally {
await handle.close();
}
});
} else {
let output = '';
Expand Down
17 changes: 10 additions & 7 deletions test/parallel/test-fs-promises-file-handle-readFile.js
Original file line number Diff line number Diff line change
Expand Up @@ -56,13 +56,16 @@ async function doReadAndCancel() {
{
const filePathForHandle = path.resolve(tmpDir, 'dogs-running.txt');
const fileHandle = await open(filePathForHandle, 'w+');
const buffer = Buffer.from('Dogs running'.repeat(10000), 'utf8');
fs.writeFileSync(filePathForHandle, buffer);
const signal = AbortSignal.abort();
await assert.rejects(readFile(fileHandle, { signal }), {
name: 'AbortError'
});
await fileHandle.close();
try {
const buffer = Buffer.from('Dogs running'.repeat(10000), 'utf8');
fs.writeFileSync(filePathForHandle, buffer);
const signal = AbortSignal.abort();
await assert.rejects(readFile(fileHandle, { signal }), {
name: 'AbortError'
});
} finally {
await fileHandle.close();
}
}

// Signal aborted on first tick
Expand Down
18 changes: 11 additions & 7 deletions test/parallel/test-fs-promises-file-handle-writeFile.js
Original file line number Diff line number Diff line change
Expand Up @@ -30,13 +30,17 @@ async function validateWriteFile() {
async function doWriteAndCancel() {
const filePathForHandle = path.resolve(tmpDir, 'dogs-running.txt');
const fileHandle = await open(filePathForHandle, 'w+');
const buffer = Buffer.from('dogs running'.repeat(512 * 1024), 'utf8');
const controller = new AbortController();
const { signal } = controller;
process.nextTick(() => controller.abort());
await assert.rejects(writeFile(fileHandle, buffer, { signal }), {
name: 'AbortError'
});
try {
const buffer = Buffer.from('dogs running'.repeat(512 * 1024), 'utf8');
const controller = new AbortController();
const { signal } = controller;
process.nextTick(() => controller.abort());
await assert.rejects(writeFile(fileHandle, buffer, { signal }), {
name: 'AbortError'
});
} finally {
await fileHandle.close();
}
}

validateWriteFile()
Expand Down
Loading

0 comments on commit f9447b7

Please sign in to comment.