Repository: nodejs/node
Pull request: fs: fix rmSync error code

Return the correct error code, when a directory_not_empty error occurred.

Fixes: https://github.com/nodejs/node/issues/57095

<!--
Before submitting a pull request, please read:

- the CONTRIBUTING guide at https://github.com/nodejs/node/blob/HEAD/CONTRIBUTING.md
- the commit message formatting guidelines at
  https://github.com/nodejs/node/blob/HEAD/doc/contributing/pull-requests.md#commit-message-guidelines

For code changes:
1. Include tests for any bug fixes or new features.
2. Update documentation if relevant.
3. Ensure that `make -j4 test` (UNIX), or `vcbuild test` (Windows) passes.

If you believe this PR should be highlighted in the Node.js CHANGELOG
please add the `notable-change` label.

Developer's Certificate of Origin 1.1

By making a contribution to this project, I certify that:

(a) The contribution was created in whole or in part by me and I
    have the right to submit it under the open source license
    indicated in the file; or

(b) The contribution is based upon previous work that, to the best
    of my knowledge, is covered under an appropriate open source
    license and I have the right under that license to submit that
    work with modifications, whether created in whole or in part
    by me, under the same open source license (unless I am
    permitted to submit under a different license), as indicated
    in the file; or

(c) The contribution was provided directly to me by some other
    person who certified (a), (b) or (c) and I have not modified
    it.

(d) I understand and agree that this project and the contribution
    are public and that a record of the contribution (including all
    personal information I submit with it, including my sign-off) is
    maintained indefinitely and may be redistributed consistent with
    this project or the open source license(s) involved.
-->


Changed files:
filename: src/node_file.cc
patch: @@ -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);

filename: test/parallel/test-fs-rm.js
patch: @@ -481,12 +481,20 @@ 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) {
+    function makeDirectoryReadOnly(dir, allowExecute) {
       let accessErrorCode = 'EACCES';
+      if (common.isMacOS && allowExecute) {
+        accessErrorCode = 'ENOTEMPTY';
+      }
       if (common.isWindows) {
         accessErrorCode = 'EPERM';
-        execSync(`icacls ${dir} /deny "everyone:(OI)(CI)(DE,DC)"`);
+        const permissions = ['DE', 'DC'];
+        if (!allowExecute) {
+          permissions.push('X');
+        }
+        execSync(`icacls ${dir} /deny "everyone:(OI)(CI)(${permissions.join(',')})"`);
       } else {
+        const mode = allowExecute ? 0o555 : 0o444;
         fs.chmodSync(dir, mode);
       }
       return accessErrorCode;
@@ -510,7 +518,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 }));
         }, {
@@ -532,7 +540,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 }));