Skip to content

Commit 041a217

Browse files
Paul Schwabaueraduh95
authored andcommitted
fs: fix rmSync error code
Return the correct error code, when a directory_not_empty error occurred. Fixes: #57095 PR-URL: #57103 Reviewed-By: Jason Zhang <[email protected]> Reviewed-By: Luigi Pinca <[email protected]>
1 parent 98d328a commit 041a217

File tree

2 files changed

+14
-5
lines changed

2 files changed

+14
-5
lines changed

‎src/node_file.cc‎

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1705,7 +1705,8 @@ static void RmSync(const FunctionCallbackInfo<Value>& args){
17051705
return env->ThrowErrnoException(EPERM, "rm", message.c_str(), path_c_str);
17061706
} elseif (error == std::errc::directory_not_empty){
17071707
std::string message = "Directory not empty: " + file_path_str;
1708-
return env->ThrowErrnoException(EACCES, "rm", message.c_str(), path_c_str);
1708+
return env->ThrowErrnoException(
1709+
ENOTEMPTY, "rm", message.c_str(), path_c_str);
17091710
} elseif (error == std::errc::not_a_directory){
17101711
std::string message = "Not a directory: " + file_path_str;
17111712
return env->ThrowErrnoException(ENOTDIR, "rm", message.c_str(), path_c_str);

‎test/parallel/test-fs-rm.js‎

Lines changed: 12 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -481,12 +481,20 @@ if (isGitPresent){
481481
// IBMi has a different access permission mechanism
482482
// This test should not be run as `root`
483483
if(!common.isIBMi&&(common.isWindows||process.getuid()!==0)){
484-
functionmakeDirectoryReadOnly(dir,mode){
484+
functionmakeDirectoryReadOnly(dir,allowExecute){
485485
letaccessErrorCode='EACCES';
486+
if(common.isMacOS&&allowExecute){
487+
accessErrorCode='ENOTEMPTY';
488+
}
486489
if(common.isWindows){
487490
accessErrorCode='EPERM';
488-
execSync(`icacls ${dir} /deny "everyone:(OI)(CI)(DE,DC)"`);
491+
constpermissions=['DE','DC'];
492+
if(!allowExecute){
493+
permissions.push('X');
494+
}
495+
execSync(`icacls ${dir} /deny "everyone:(OI)(CI)(${permissions.join(',')})"`);
489496
}else{
497+
constmode=allowExecute ? 0o555 : 0o444;
490498
fs.chmodSync(dir,mode);
491499
}
492500
returnaccessErrorCode;
@@ -510,7 +518,7 @@ if (isGitPresent){
510518
try{
511519
fs.mkdirSync(dirname,common.mustNotMutateObjectDeep({recursive: true}));
512520
fs.writeFileSync(filePath,'hello');
513-
constcode=makeDirectoryReadOnly(dirname,0o444);
521+
constcode=makeDirectoryReadOnly(dirname,false);
514522
assert.throws(()=>{
515523
fs.rmSync(filePath,common.mustNotMutateObjectDeep({force: true}));
516524
},{
@@ -532,7 +540,7 @@ if (isGitPresent){
532540
fs.mkdirSync(middle);
533541
fs.mkdirSync(path.join(middle,'leaf'));// Make `middle` non-empty
534542
try{
535-
constcode=makeDirectoryReadOnly(middle,0o555);
543+
constcode=makeDirectoryReadOnly(middle,true);
536544
try{
537545
assert.throws(()=>{
538546
fs.rmSync(root,common.mustNotMutateObjectDeep({recursive: true}));

0 commit comments

Comments
(0)