mirror of
https://github.com/nodejs/node.git
synced 2026-11-24 21:50:36 -05:00
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>
This commit is contained in:
1 parent
07fc4c9052
commit
f6d72af926
2 files changed
+74
-2
No files matched your search
+4
-2
@@ -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;
|
||||
}
|
||||
|
||||
@@ -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());
|
||||
Reference in new issue
Block a user