Skip to content

Commit

Permalink
Handle EROFS errors
Browse files Browse the repository at this point in the history
A EROFS will be raised when trying to write a dir onto a read-only
filesystem.  However, if the dir already is there, then it should be
treated like an EEXIST (since EROFS can be raised by trying to create a
dir over a mount point, where the mount point is not read-only, but you
still can't just clobber over it).

If the dir doesn't already exist, then the EROFS will be accurately
reported as the reason why it could not be created.

(Copied from 17ee84f)
  • Loading branch information
isaacs committed Jan 24, 2020
1 parent 6b2632d commit e84d327
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 2 deletions.
4 changes: 2 additions & 2 deletions lib/mkdirp-manual.js
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ const mkdirpManual = (path, opts, made) => {
if (er.code === 'ENOENT')
return mkdirpManual(parent, opts)
.then(made => mkdirpManual(path, opts, made))
if (er.code !== 'EEXIST')
if (er.code !== 'EEXIST' && er.code !== 'EROFS')
throw er
return opts.statAsync(path).then(st => {
if (st.isDirectory())
Expand Down Expand Up @@ -50,7 +50,7 @@ const mkdirpManualSync = (path, opts, made) => {
} catch (er) {
if (er.code === 'ENOENT')
return mkdirpManualSync(path, opts, mkdirpManualSync(parent, opts, made))
if (er.code !== 'EEXIST')
if (er.code !== 'EEXIST' && er.code !== 'EROFS')
throw er
try {
if (!opts.statSync(path).isDirectory())
Expand Down
20 changes: 20 additions & 0 deletions test/mkdirp-manual.js
Original file line number Diff line number Diff line change
Expand Up @@ -62,6 +62,26 @@ t.test('mkdirpManual / just calls implementation', t => {
t.end()
})

t.test('read-only file system, still succeed if dir exists', t => {
const dir = t.testdir({ foo: {} })
const opt = {
stat,
statAsync,
statSync,
mkdir,
mkdirAsync: () => Promise.reject(Object.assign(new Error('EROFS'), {
code: 'EROFS',
})),
mkdirSync: () => {
throw Object.assign(new Error('EROFS'), {
code: 'EROFS',
})
},
}
t.equal(mkdirpManualSync(`${dir}/foo`, opt), undefined)
return mkdirpManual(`${dir}/foo`, opt).then(made => t.equal(made, undefined))
})

t.test('recurse and return first dir made', t => {
const dir = t.testdir()
const opt = {
Expand Down

0 comments on commit e84d327

Please sign in to comment.