From 939a1f2653a6e84db24cee377a2a64e6991cc825 Mon Sep 17 00:00:00 2001 From: Darshan Sen Date: Mon, 31 May 2021 14:22:14 +0530 Subject: [PATCH] chmod first and then existsSync --- test/parallel/test-fs-rm.js | 192 ++++++++++++++++-------------------- 1 file changed, 84 insertions(+), 108 deletions(-) diff --git a/test/parallel/test-fs-rm.js b/test/parallel/test-fs-rm.js index 3bc16bb0086e45..1d21a65d50f63c 100644 --- a/test/parallel/test-fs-rm.js +++ b/test/parallel/test-fs-rm.js @@ -300,35 +300,29 @@ function removeAsync(dir) { fs.rm(filePath, { force: true }, common.mustCall((err) => { try { - let isValidState = true; - - // Here, existsSync() will not work on POSIX as the file we are - // checking for lies in a read-only directory. - const exists = fs.readdirSync(dirname).includes('text.txt'); - - if (common.isWindows && !(exists === false && err === null)) { - // Since there is no concept of read-only folders on Windows, the - // unlink syscall can easily get access to the files under the - // folder being removed without an EACCES and remove them. - isValidState = false; - } else if (!(exists === true && err?.code === 'EACCES')) { - isValidState = false; - } - - if (!isValidState) { - throw err; - } - } finally { - try { - if (fs.existsSync(dirname)) { - fs.chmodSync(dirname, 0o777); - } - - if (fs.existsSync(filePath)) { - fs.chmodSync(filePath, 0o777); - } - } catch { - } + fs.chmodSync(dirname, 0o777); + } catch { + } + + try { + fs.chmodSync(filePath, 0o777); + } catch { + } + + let isValidState = true; + const exists = fs.existsSync(filePath); + + if (common.isWindows && !(exists === false && err === null)) { + // Since there is no concept of read-only folders on Windows, the + // unlink syscall can easily get access to the files under the folder + // being removed without an EACCES and remove them. + isValidState = false; + } else if (!(exists === true && err?.code === 'EACCES')) { + isValidState = false; + } + + if (!isValidState) { + throw err; } })); } @@ -354,35 +348,29 @@ function removeAsync(dir) { } try { - let isValidState = true; + fs.chmodSync(dirname, 0o777); + } catch { + } - // Here, existsSync() will not work on POSIX as the file we are checking - // for lies in a read-only directory. - const exists = fs.readdirSync(dirname).includes('text.txt'); + try { + fs.chmodSync(filePath, 0o777); + } catch { + } - if (common.isWindows && !(exists === false && err === null)) { - // Since there is no concept of read-only folders on Windows, the - // unlink syscall can easily get access to the files under the folder - // being removed without an EACCES and remove them. - isValidState = false; - } else if (!(exists === true && err?.code === 'EACCES')) { - isValidState = false; - } + let isValidState = true; + const exists = fs.existsSync(filePath); - if (!isValidState) { - throw err; - } - } finally { - try { - if (fs.existsSync(dirname)) { - fs.chmodSync(dirname, 0o777); - } + if (common.isWindows && !(exists === false && err === null)) { + // Since there is no concept of read-only folders on Windows, the unlink + // syscall can easily get access to the files under the folder being + // removed without an EACCES and remove them. + isValidState = false; + } else if (!(exists === true && err?.code === 'EACCES')) { + isValidState = false; + } - if (fs.existsSync(filePath)) { - fs.chmodSync(filePath, 0o777); - } - } catch { - } + if (!isValidState) { + throw err; } } @@ -410,34 +398,28 @@ function removeAsync(dir) { } try { - let isValidState = true; + fs.chmodSync(middle, 0o777); + } catch { + } - // Here, existsSync() will not work on POSIX as the file we are checking - // for lies in a read-only directory. - const exists = fs.readdirSync(middle).includes('leaf'); + try { + fs.chmodSync(leaf, 0o777); + } catch { + } - if (common.isWindows && !(exists === false && err === null)) { - // Since there is no concept of read-only folders on Windows, the - // unlink syscall can easily get access to the files under the folder - // being removed without an EACCES and remove them. - isValidState = false; - } else if (!(exists === true && err?.code === 'EACCES')) { - isValidState = false; - } - if (!isValidState) { - throw err; - } - } finally { - try { - if (fs.existsSync(middle)) { - fs.chmodSync(middle, 0o777); - } + let isValidState = true; + const exists = fs.existsSync(root); - if (fs.existsSync(leaf)) { - fs.chmodSync(leaf, 0o777); - } - } catch { - } + if (common.isWindows && !(exists === false && err === null)) { + // Since there is no concept of read-only folders on Windows, the unlink + // syscall can easily get access to the files under the folder being + // removed without an EACCES and remove them. + isValidState = false; + } else if (!(exists === true && err?.code === 'EACCES')) { + isValidState = false; + } + if (!isValidState) { + throw err; } } @@ -458,35 +440,29 @@ function removeAsync(dir) { fs.rm(root, { recursive: true }, common.mustCall((err) => { try { - let isValidState = true; - - // Here, existsSync() will not work on POSIX as the file we are - // checking for lies in a read-only directory. - const exists = fs.readdirSync(middle).includes('leaf'); - - if (common.isWindows && !(exists === false && err === null)) { - // Since there is no concept of read-only folders on Windows, the - // unlink syscall can easily get access to the files under the - // folder being removed without an EACCES and remove them. - isValidState = false; - } else if (!(exists === true && err?.code === 'EACCES')) { - isValidState = false; - } - - if (!isValidState) { - throw err; - } - } finally { - try { - if (fs.existsSync(middle)) { - fs.chmodSync(middle, 0o777); - } - - if (fs.existsSync(leaf)) { - fs.chmodSync(leaf, 0o777); - } - } catch { - } + fs.chmodSync(middle, 0o777); + } catch { + } + + try { + fs.chmodSync(leaf, 0o777); + } catch { + } + + let isValidState = true; + const exists = fs.existsSync(root); + + if (common.isWindows && !(exists === false && err === null)) { + // Since there is no concept of read-only folders on Windows, the + // unlink syscall can easily get access to the files under the folder + // being removed without an EACCES and remove them. + isValidState = false; + } else if (!(exists === true && err?.code === 'EACCES')) { + isValidState = false; + } + + if (!isValidState) { + throw err; } })); }