Skip to content

Commit

Permalink
fs: fix rmSync error code
Browse files Browse the repository at this point in the history
Return the correct error code, when a directory_not_empty error
occurred.

Fixes: nodejs#57095
  • Loading branch information
koplas committed Feb 17, 2025
1 parent f1b951f commit 55719ed
Show file tree
Hide file tree
Showing 2 changed files with 13 additions and 7 deletions.
3 changes: 2 additions & 1 deletion src/node_file.cc
Original file line number Diff line number Diff line change
Expand Up @@ -1705,7 +1705,8 @@ static void RmSync(const FunctionCallbackInfo<Value>& args) {
return env->ThrowErrnoException(EPERM, "rm", message.c_str(), path_c_str);
} else if (error == std::errc::directory_not_empty) {
std::string message = "Directory not empty: " + file_path_str;
return env->ThrowErrnoException(EACCES, "rm", message.c_str(), path_c_str);
return env->ThrowErrnoException(
ENOTEMPTY, "rm", message.c_str(), path_c_str);
} else if (error == std::errc::not_a_directory) {
std::string message = "Not a directory: " + file_path_str;
return env->ThrowErrnoException(ENOTDIR, "rm", message.c_str(), path_c_str);
Expand Down
17 changes: 11 additions & 6 deletions test/parallel/test-fs-rm.js
Original file line number Diff line number Diff line change
Expand Up @@ -481,12 +481,17 @@ if (isGitPresent) {
// IBMi has a different access permission mechanism
// This test should not be run as `root`
if (!common.isIBMi && (common.isWindows || process.getuid() !== 0)) {
function makeDirectoryReadOnly(dir, mode) {
let accessErrorCode = 'EACCES';
function makeDirectoryReadOnly(dir, allowExecute) {
let accessErrorCode = allowExecute ? 'ENOTEMPTY' : 'EACCES';
if (common.isWindows) {
accessErrorCode = 'EPERM';
execSync(`icacls ${dir} /deny "everyone:(OI)(CI)(DE,DC)"`);
const permissions = ['DE', 'DC'];
if (!allowExecute) {
accessErrorCode = 'EPERM';
permissions.push('X')
}
execSync(`icacls ${dir} /deny "everyone:(OI)(CI)(${permissions.join(',')})"`);
} else {
const mode = allowExecute ? 0o555 : 0o444;
fs.chmodSync(dir, mode);
}
return accessErrorCode;
Expand All @@ -510,7 +515,7 @@ if (isGitPresent) {
try {
fs.mkdirSync(dirname, common.mustNotMutateObjectDeep({ recursive: true }));
fs.writeFileSync(filePath, 'hello');
const code = makeDirectoryReadOnly(dirname, 0o444);
const code = makeDirectoryReadOnly(dirname, false);
assert.throws(() => {
fs.rmSync(filePath, common.mustNotMutateObjectDeep({ force: true }));
}, {
Expand All @@ -532,7 +537,7 @@ if (isGitPresent) {
fs.mkdirSync(middle);
fs.mkdirSync(path.join(middle, 'leaf')); // Make `middle` non-empty
try {
const code = makeDirectoryReadOnly(middle, 0o555);
const code = makeDirectoryReadOnly(middle, true);
try {
assert.throws(() => {
fs.rmSync(root, common.mustNotMutateObjectDeep({ recursive: true }));
Expand Down

0 comments on commit 55719ed

Please sign in to comment.