Skip to content

Commit f9447b7

Browse files
Linkgoronjasnell
authored andcommitted
fs: fix rmsync error swallowing
fix rmsync swallowing errors instead of throwing them. fixes: #38683fixes: #34580 PR-URL: #38684Fixes: #38683Fixes: #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

Lines changed: 286 additions & 154 deletions

‎lib/internal/fs/rimraf.js‎

Lines changed: 15 additions & 2 deletions
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+
}elseif(err.code==='ENOENT'){
223+
// The file is already gone.
224+
return;
225+
}elseif(i===tries){
226+
throwerr;
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-
throworiginalErr;
239+
if(err.code==='ENOTDIR'){
240+
throworiginalErr||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+
}elseif(err.code==='ENOENT'){
269+
// The file is already gone.
270+
return;
271+
}elseif(i===tries){
272+
throwerr;
262273
}
263274
}
264275
}
265276
}
277+
278+
throworiginalErr||err;
266279
}
267280
}
268281

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

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -26,8 +26,7 @@ function makeDirectoryReadOnly(dir) {
2626
letaccessErrorCode='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
functionmakeDirectoryWritable(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‎

Lines changed: 14 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -15,10 +15,17 @@ const debuglog = (arg) => {
1515
consttmpdir=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+
letopenFd;
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‎

Lines changed: 13 additions & 9 deletions
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-
newWorker(__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+
newWorker(__filename,{
27+
workerData: { handle },
28+
transferList: [handle]
29+
});
30+
},{
31+
code: 25,
2832
});
29-
},{
30-
code: 25,
31-
});
33+
}finally{
34+
awaithandle.close();
35+
}
3236
});
3337
}else{
3438
letoutput='';

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

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

6871
// Signal aborted on first tick

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

Lines changed: 11 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -30,13 +30,17 @@ async function validateWriteFile() {
3030
asyncfunctiondoWriteAndCancel(){
3131
constfilePathForHandle=path.resolve(tmpDir,'dogs-running.txt');
3232
constfileHandle=awaitopen(filePathForHandle,'w+');
33-
constbuffer=Buffer.from('dogs running'.repeat(512*1024),'utf8');
34-
constcontroller=newAbortController();
35-
const{ signal }=controller;
36-
process.nextTick(()=>controller.abort());
37-
awaitassert.rejects(writeFile(fileHandle,buffer,{ signal }),{
38-
name: 'AbortError'
39-
});
33+
try{
34+
constbuffer=Buffer.from('dogs running'.repeat(512*1024),'utf8');
35+
constcontroller=newAbortController();
36+
const{ signal }=controller;
37+
process.nextTick(()=>controller.abort());
38+
awaitassert.rejects(writeFile(fileHandle,buffer,{ signal }),{
39+
name: 'AbortError'
40+
});
41+
}finally{
42+
awaitfileHandle.close();
43+
}
4044
}
4145

4246
validateWriteFile()

0 commit comments

Comments
 (0)