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
2 changes: 2 additions & 0 deletions src/node_file.cc
Original file line number Diff line number Diff line change
Expand Up @@ -1874,6 +1874,7 @@ int MKDirpSync(uv_loop_t* loop,
case UV_EACCES:
case UV_ENOSPC:
case UV_ENOTDIR:
case UV_EROFS:
case UV_EPERM: {
return err;
}
Expand Down Expand Up @@ -1954,6 +1955,7 @@ int MKDirpAsync(
case UV_EACCES:
case UV_ENOSPC:
case UV_ENOTDIR:
case UV_EROFS:
case UV_EPERM: {
req_wrap->continuation_data()->Done(err);
break;
Expand Down
60 changes: 60 additions & 0 deletions test/parallel/test-fs-mkdir.js
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
// Flags: --expose-internals
// Copyright Joyent, Inc. and other Node contributors.
//
// Permission is hereby granted, free of charge, to any person obtaining a
Expand All @@ -22,6 +23,8 @@
'use strict';
const common = require('../common');
const assert = require('assert');
const { internalBinding } = require('internal/test/binding');
const child_process = require('child_process');
const fs = require('fs');
const path = require('path');
const { isMainThread } = require('worker_threads');
Expand Down Expand Up @@ -184,6 +187,63 @@ function nextdir() {
}));
}

// `mkdirp` when folder was readonly (EROFS) - mocked via internalBinding.
// Verifies that EROFS is propagated correctly without requiring root/sudo.
{
const fsBinding = internalBinding('fs');
const originalMkdir = fsBinding.mkdir;
fsBinding.mkdir = function(p) {
const err = new Error(`EROFS: read-only file system, mkdir '${p}'`);
err.code = 'EROFS';
err.syscall = 'mkdir';
err.path = p;
throw err;
};
try {
const pathname = path.join(tmpdir.path, nextdir(), nextdir());
assert.throws(
() => { fs.mkdirSync(pathname, { recursive: true }); },
{ code: 'EROFS', message: /EROFS:.*mkdir/, name: 'Error', syscall: 'mkdir' }
);
} finally {
fsBinding.mkdir = originalMkdir;
}
}

// `mkdirp` when folder was readonly.
if (common.isLinux) {
const roTmpfsPath = path.join(tmpdir.path, 'ro-tmpfs');
fs.mkdirSync(roTmpfsPath);

const { status, stderr } = child_process.spawnSync(
'sudo', ['-n', 'mount', '-t', 'tmpfs', '-o', 'ro', 'tmpfs', roTmpfsPath],
{ stdio: 'pipe', encoding: 'utf8' }
);

if (status !== 0) {
console.warn(
'Cannot test EROFS: passwordless sudo required to mount read-only tmpfs. ' +
`Mount failed with status ${status}: ${stderr}`
);
} else {
try {
const pathname = path.join(roTmpfsPath, nextdir());
assert.throws(
() => { fs.mkdirSync(pathname, { recursive: true }); },
{
code: 'EROFS',
message: /EROFS:.*mkdir/,
name: 'Error',
syscall: 'mkdir',
}
);
} finally {
child_process.spawnSync('sudo', ['-n', 'umount', roTmpfsPath]);
}
}
fs.rmdirSync(roTmpfsPath);
}

// `mkdirp` when path is a file.
{
const pathname = tmpdir.resolve(nextdir(), nextdir());
Expand Down
Loading