Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 4 additions & 2 deletions src/node_file.cc
Original file line number Diff line number Diff line change
Expand Up @@ -1976,7 +1976,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: {
Expand Down Expand Up @@ -2059,7 +2060,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;
}
Expand Down
70 changes: 70 additions & 0 deletions test/parallel/test-fs-mkdir-recursive-erofs.js
Original file line number Diff line number Diff line change
@@ -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());
Loading