Commit a055d9f2ccd for nodejs
commit a055d9f2ccdabc4652b9d8b5720fbab7280928e5
Author: Yohannes Desta <108905193+yohannesjoya@users.noreply.github.com>
Date: Tue Nov 24 15:29:43 2026 +0300
fs: report EROFS from recursive mkdir()
A recursive mkdir() that hits a read-only file system treated EROFS like
any unknown error: it stat()ed the missing path and reported the ENOENT
from that instead. Return EROFS straight away, as mkdir() already does
for EACCES, ENOSPC, ENOTDIR and EPERM.
The test uses a read-only file system that is already mounted (listed
in /proc/self/mounts on Linux, the system volume on macOS) and skips
when there is none, since mounting one needs root.
Fixes: https://github.com/nodejs/node/issues/47098
Refs: https://github.com/nodejs/node/pull/48105
Refs: https://github.com/nodejs/node/pull/62725
Co-authored-by: Alberto Moretti <moretti919@gmail.com>
Signed-off-by: Yohannes Desta <yohannesdestagebru10@gmail.com>
PR-URL: https://github.com/nodejs/node/pull/67500
Reviewed-By: James M Snell <jasnell@gmail.com>
Reviewed-By: Jason Zhang <xzha4350@gmail.com>
diff --git a/src/node_file.cc b/src/node_file.cc
index 5e33d40c776..e9423a378da 100644
--- a/src/node_file.cc
+++ b/src/node_file.cc
@@ -1995,7 +1995,8 @@ int MKDirpSync(uv_loop_t* loop,
case UV_EACCES:
case UV_ENOSPC:
case UV_ENOTDIR:
- case UV_EPERM: {
+ case UV_EPERM:
+ case UV_EROFS: {
return err;
}
case UV_ENOENT: {
@@ -2078,7 +2079,8 @@ int MKDirpAsync(
case UV_EACCES:
case UV_ENOSPC:
case UV_ENOTDIR:
- case UV_EPERM: {
+ case UV_EPERM:
+ case UV_EROFS: {
req_wrap->continuation_data()->Done(err);
break;
}
diff --git a/test/parallel/test-fs-mkdir-recursive-erofs.js b/test/parallel/test-fs-mkdir-recursive-erofs.js
new file mode 100644
index 00000000000..0f874888ffe
--- /dev/null
+++ b/test/parallel/test-fs-mkdir-recursive-erofs.js
@@ -0,0 +1,70 @@
+'use strict';
+
+// Test that mkdir with the recursive option reports EROFS when the target is
+// on a read-only file system instead of turning it into ENOENT.
+// Ref: https://github.com/nodejs/node/issues/47098
+
+const common = require('../common');
+
+if (common.isWindows)
+ common.skip('no read-only file system to test against on Windows');
+
+const assert = require('assert');
+const fs = require('fs');
+const path = require('path');
+
+const probe = `node-test-erofs-${process.pid}`;
+
+// Find a mounted read-only file system. Linux lists mount options in
+// /proc/self/mounts (for example /sys in a container, or squashfs images);
+// on macOS the system volume is read-only. Confirm the candidate with a plain
+// mkdir, which must fail with EROFS rather than with a permission error.
+function findReadOnlyRoot() {
+ const candidates = [];
+ if (common.isLinux) {
+ let mounts;
+ try {
+ mounts = fs.readFileSync('/proc/self/mounts', 'utf8');
+ } catch {
+ return null;
+ }
+ for (const line of mounts.split('\n')) {
+ const fields = line.split(' ');
+ if (fields.length >= 4 && /(^|,)ro(,|$)/.test(fields[3])) {
+ candidates.push(fields[1].replaceAll('\\040', ' '));
+ }
+ }
+ } else if (common.isMacOS) {
+ candidates.push('/');
+ }
+
+ for (const dir of candidates) {
+ try {
+ fs.mkdirSync(path.join(dir, probe));
+ } catch (err) {
+ if (err.code === 'EROFS') return dir;
+ continue;
+ }
+ // Not read-only after all.
+ fs.rmdirSync(path.join(dir, probe));
+ }
+ return null;
+}
+
+const root = findReadOnlyRoot();
+if (root === null)
+ common.skip('no read-only file system available');
+
+const target = path.join(root, probe, 'child');
+const expected = { code: 'EROFS', syscall: 'mkdir' };
+
+assert.throws(() => fs.mkdirSync(target, { recursive: true }), expected);
+
+fs.mkdir(target, { recursive: true }, common.mustCall((err) => {
+ assert.strictEqual(err.code, 'EROFS');
+ assert.strictEqual(err.syscall, 'mkdir');
+ assert(err.path);
+}));
+
+assert.rejects(fs.promises.mkdir(target, { recursive: true }), expected)
+ .then(common.mustCall());