-
Notifications
You must be signed in to change notification settings - Fork 30k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
fs: recursion should bail on permission error
When creating directories recursively, the logic should bail immediately on UV_EACCES and bubble the error to the user. Fixes: #31481
- Loading branch information
Showing
2 changed files
with
57 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,50 @@ | ||
'use strict'; | ||
|
||
// Test that mkdir with recursive option returns appropriate error | ||
// when executed on folder it does not have permission to access. | ||
// Ref: https://github.com/nodejs/node/issues/31481 | ||
|
||
const common = require('../common'); | ||
|
||
if (!common.isWindows && process.getuid() === 0) | ||
common.skip('as this test should not be run as `root`'); | ||
|
||
if (common.isIBMi) | ||
common.skip('IBMi has a different access permission mechanism'); | ||
|
||
const tmpdir = require('../common/tmpdir'); | ||
tmpdir.refresh(); | ||
|
||
const assert = require('assert'); | ||
const fs = require('fs'); | ||
const path = require('path'); | ||
|
||
let n = 0; | ||
|
||
// Synchronous API should return an EACCESS error with path populated. | ||
{ | ||
const dir = path.join(tmpdir.path, `mkdirp_${n++}`); | ||
fs.mkdirSync(dir); | ||
fs.chmodSync(dir, '444'); | ||
let err = null; | ||
try { | ||
fs.mkdirSync(path.join(dir, '/foo'), { recursive: true }); | ||
} catch (_err) { | ||
err = _err; | ||
} | ||
assert(err); | ||
assert.strictEqual(err.code, 'EACCES'); | ||
assert(err.path); | ||
} | ||
|
||
// Asynchronous API should return an EACCESS error with path populated. | ||
{ | ||
const dir = path.join(tmpdir.path, `mkdirp_${n++}`); | ||
fs.mkdirSync(dir); | ||
fs.chmodSync(dir, '444'); | ||
fs.mkdir(path.join(dir, '/bar'), { recursive: true }, (err) => { | ||
assert(err); | ||
assert.strictEqual(err.code, 'EACCES'); | ||
assert(err.path); | ||
}); | ||
} |