Skip to content

Commit 939a1f2

Browse files
committed
chmod first and then existsSync
1 parent ee00cb0 commit 939a1f2

File tree

1 file changed

+84
-108
lines changed

1 file changed

+84
-108
lines changed

test/parallel/test-fs-rm.js

Lines changed: 84 additions & 108 deletions
Original file line numberDiff line numberDiff line change
@@ -300,35 +300,29 @@ function removeAsync(dir) {
300300

301301
fs.rm(filePath, { force: true }, common.mustCall((err) => {
302302
try {
303-
let isValidState = true;
304-
305-
// Here, existsSync() will not work on POSIX as the file we are
306-
// checking for lies in a read-only directory.
307-
const exists = fs.readdirSync(dirname).includes('text.txt');
308-
309-
if (common.isWindows && !(exists === false && err === null)) {
310-
// Since there is no concept of read-only folders on Windows, the
311-
// unlink syscall can easily get access to the files under the
312-
// folder being removed without an EACCES and remove them.
313-
isValidState = false;
314-
} else if (!(exists === true && err?.code === 'EACCES')) {
315-
isValidState = false;
316-
}
317-
318-
if (!isValidState) {
319-
throw err;
320-
}
321-
} finally {
322-
try {
323-
if (fs.existsSync(dirname)) {
324-
fs.chmodSync(dirname, 0o777);
325-
}
326-
327-
if (fs.existsSync(filePath)) {
328-
fs.chmodSync(filePath, 0o777);
329-
}
330-
} catch {
331-
}
303+
fs.chmodSync(dirname, 0o777);
304+
} catch {
305+
}
306+
307+
try {
308+
fs.chmodSync(filePath, 0o777);
309+
} catch {
310+
}
311+
312+
let isValidState = true;
313+
const exists = fs.existsSync(filePath);
314+
315+
if (common.isWindows && !(exists === false && err === null)) {
316+
// Since there is no concept of read-only folders on Windows, the
317+
// unlink syscall can easily get access to the files under the folder
318+
// being removed without an EACCES and remove them.
319+
isValidState = false;
320+
} else if (!(exists === true && err?.code === 'EACCES')) {
321+
isValidState = false;
322+
}
323+
324+
if (!isValidState) {
325+
throw err;
332326
}
333327
}));
334328
}
@@ -354,35 +348,29 @@ function removeAsync(dir) {
354348
}
355349

356350
try {
357-
let isValidState = true;
351+
fs.chmodSync(dirname, 0o777);
352+
} catch {
353+
}
358354

359-
// Here, existsSync() will not work on POSIX as the file we are checking
360-
// for lies in a read-only directory.
361-
const exists = fs.readdirSync(dirname).includes('text.txt');
355+
try {
356+
fs.chmodSync(filePath, 0o777);
357+
} catch {
358+
}
362359

363-
if (common.isWindows && !(exists === false && err === null)) {
364-
// Since there is no concept of read-only folders on Windows, the
365-
// unlink syscall can easily get access to the files under the folder
366-
// being removed without an EACCES and remove them.
367-
isValidState = false;
368-
} else if (!(exists === true && err?.code === 'EACCES')) {
369-
isValidState = false;
370-
}
360+
let isValidState = true;
361+
const exists = fs.existsSync(filePath);
371362

372-
if (!isValidState) {
373-
throw err;
374-
}
375-
} finally {
376-
try {
377-
if (fs.existsSync(dirname)) {
378-
fs.chmodSync(dirname, 0o777);
379-
}
363+
if (common.isWindows && !(exists === false && err === null)) {
364+
// Since there is no concept of read-only folders on Windows, the unlink
365+
// syscall can easily get access to the files under the folder being
366+
// removed without an EACCES and remove them.
367+
isValidState = false;
368+
} else if (!(exists === true && err?.code === 'EACCES')) {
369+
isValidState = false;
370+
}
380371

381-
if (fs.existsSync(filePath)) {
382-
fs.chmodSync(filePath, 0o777);
383-
}
384-
} catch {
385-
}
372+
if (!isValidState) {
373+
throw err;
386374
}
387375
}
388376

@@ -410,34 +398,28 @@ function removeAsync(dir) {
410398
}
411399

412400
try {
413-
let isValidState = true;
401+
fs.chmodSync(middle, 0o777);
402+
} catch {
403+
}
414404

415-
// Here, existsSync() will not work on POSIX as the file we are checking
416-
// for lies in a read-only directory.
417-
const exists = fs.readdirSync(middle).includes('leaf');
405+
try {
406+
fs.chmodSync(leaf, 0o777);
407+
} catch {
408+
}
418409

419-
if (common.isWindows && !(exists === false && err === null)) {
420-
// Since there is no concept of read-only folders on Windows, the
421-
// unlink syscall can easily get access to the files under the folder
422-
// being removed without an EACCES and remove them.
423-
isValidState = false;
424-
} else if (!(exists === true && err?.code === 'EACCES')) {
425-
isValidState = false;
426-
}
427-
if (!isValidState) {
428-
throw err;
429-
}
430-
} finally {
431-
try {
432-
if (fs.existsSync(middle)) {
433-
fs.chmodSync(middle, 0o777);
434-
}
410+
let isValidState = true;
411+
const exists = fs.existsSync(root);
435412

436-
if (fs.existsSync(leaf)) {
437-
fs.chmodSync(leaf, 0o777);
438-
}
439-
} catch {
440-
}
413+
if (common.isWindows && !(exists === false && err === null)) {
414+
// Since there is no concept of read-only folders on Windows, the unlink
415+
// syscall can easily get access to the files under the folder being
416+
// removed without an EACCES and remove them.
417+
isValidState = false;
418+
} else if (!(exists === true && err?.code === 'EACCES')) {
419+
isValidState = false;
420+
}
421+
if (!isValidState) {
422+
throw err;
441423
}
442424
}
443425

@@ -458,35 +440,29 @@ function removeAsync(dir) {
458440

459441
fs.rm(root, { recursive: true }, common.mustCall((err) => {
460442
try {
461-
let isValidState = true;
462-
463-
// Here, existsSync() will not work on POSIX as the file we are
464-
// checking for lies in a read-only directory.
465-
const exists = fs.readdirSync(middle).includes('leaf');
466-
467-
if (common.isWindows && !(exists === false && err === null)) {
468-
// Since there is no concept of read-only folders on Windows, the
469-
// unlink syscall can easily get access to the files under the
470-
// folder being removed without an EACCES and remove them.
471-
isValidState = false;
472-
} else if (!(exists === true && err?.code === 'EACCES')) {
473-
isValidState = false;
474-
}
475-
476-
if (!isValidState) {
477-
throw err;
478-
}
479-
} finally {
480-
try {
481-
if (fs.existsSync(middle)) {
482-
fs.chmodSync(middle, 0o777);
483-
}
484-
485-
if (fs.existsSync(leaf)) {
486-
fs.chmodSync(leaf, 0o777);
487-
}
488-
} catch {
489-
}
443+
fs.chmodSync(middle, 0o777);
444+
} catch {
445+
}
446+
447+
try {
448+
fs.chmodSync(leaf, 0o777);
449+
} catch {
450+
}
451+
452+
let isValidState = true;
453+
const exists = fs.existsSync(root);
454+
455+
if (common.isWindows && !(exists === false && err === null)) {
456+
// Since there is no concept of read-only folders on Windows, the
457+
// unlink syscall can easily get access to the files under the folder
458+
// being removed without an EACCES and remove them.
459+
isValidState = false;
460+
} else if (!(exists === true && err?.code === 'EACCES')) {
461+
isValidState = false;
462+
}
463+
464+
if (!isValidState) {
465+
throw err;
490466
}
491467
}));
492468
}

0 commit comments

Comments
 (0)