Skip to content

Commit f6d72af

Browse files
yohannesjoyahiimjako
authored andcommitted
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: #47098 Refs: #48105 Refs: #62725 Co-authored-by: Alberto Moretti <moretti919@gmail.com> Signed-off-by: Yohannes Desta <yohannesdestagebru10@gmail.com> PR-URL: #67500 Reviewed-By: James M Snell <jasnell@gmail.com> Reviewed-By: Jason Zhang <xzha4350@gmail.com>
1 parent 07fc4c9 commit f6d72af

2 files changed

Lines changed: 74 additions & 2 deletions

File tree

‎src/node_file.cc‎

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1995,7 +1995,8 @@ int MKDirpSync(uv_loop_t* loop,
19951995
case UV_EACCES:
19961996
case UV_ENOSPC:
19971997
case UV_ENOTDIR:
1998-
case UV_EPERM: {
1998+
case UV_EPERM:
1999+
case UV_EROFS: {
19992000
return err;
20002001
}
20012002
case UV_ENOENT: {
@@ -2078,7 +2079,8 @@ int MKDirpAsync(
20782079
case UV_EACCES:
20792080
case UV_ENOSPC:
20802081
case UV_ENOTDIR:
2081-
case UV_EPERM: {
2082+
case UV_EPERM:
2083+
case UV_EROFS: {
20822084
req_wrap->continuation_data()->Done(err);
20832085
break;
20842086
}
Lines changed: 70 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,70 @@
1+
'use strict';
2+
3+
// Test that mkdir with the recursive option reports EROFS when the target is
4+
// on a read-only file system instead of turning it into ENOENT.
5+
// Ref: https://github.com/nodejs/node/issues/47098
6+
7+
const common = require('../common');
8+
9+
if (common.isWindows)
10+
common.skip('no read-only file system to test against on Windows');
11+
12+
const assert = require('assert');
13+
const fs = require('fs');
14+
const path = require('path');
15+
16+
const probe = `node-test-erofs-${process.pid}`;
17+
18+
// Find a mounted read-only file system. Linux lists mount options in
19+
// /proc/self/mounts (for example /sys in a container, or squashfs images);
20+
// on macOS the system volume is read-only. Confirm the candidate with a plain
21+
// mkdir, which must fail with EROFS rather than with a permission error.
22+
function findReadOnlyRoot() {
23+
const candidates = [];
24+
if (common.isLinux) {
25+
let mounts;
26+
try {
27+
mounts = fs.readFileSync('/proc/self/mounts', 'utf8');
28+
} catch {
29+
return null;
30+
}
31+
for (const line of mounts.split('\n')) {
32+
const fields = line.split(' ');
33+
if (fields.length >= 4 && /(^|,)ro(,|$)/.test(fields[3])) {
34+
candidates.push(fields[1].replaceAll('\\040', ' '));
35+
}
36+
}
37+
} else if (common.isMacOS) {
38+
candidates.push('/');
39+
}
40+
41+
for (const dir of candidates) {
42+
try {
43+
fs.mkdirSync(path.join(dir, probe));
44+
} catch (err) {
45+
if (err.code === 'EROFS') return dir;
46+
continue;
47+
}
48+
// Not read-only after all.
49+
fs.rmdirSync(path.join(dir, probe));
50+
}
51+
return null;
52+
}
53+
54+
const root = findReadOnlyRoot();
55+
if (root === null)
56+
common.skip('no read-only file system available');
57+
58+
const target = path.join(root, probe, 'child');
59+
const expected = { code: 'EROFS', syscall: 'mkdir' };
60+
61+
assert.throws(() => fs.mkdirSync(target, { recursive: true }), expected);
62+
63+
fs.mkdir(target, { recursive: true }, common.mustCall((err) => {
64+
assert.strictEqual(err.code, 'EROFS');
65+
assert.strictEqual(err.syscall, 'mkdir');
66+
assert(err.path);
67+
}));
68+
69+
assert.rejects(fs.promises.mkdir(target, { recursive: true }), expected)
70+
.then(common.mustCall());

0 commit comments

Comments
 (0)