Skip to content

Commit

Permalink
chmod first and then existsSync
Browse files Browse the repository at this point in the history
  • Loading branch information
RaisinTen committed May 31, 2021
1 parent ee00cb0 commit 939a1f2
Showing 1 changed file with 84 additions and 108 deletions.
192 changes: 84 additions & 108 deletions test/parallel/test-fs-rm.js
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}
}));
}
Expand All @@ -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;
}
}

Expand Down Expand Up @@ -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;
}
}

Expand All @@ -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;
}
}));
}
Expand Down

0 comments on commit 939a1f2

Please sign in to comment.