-
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: bail on permission error in recursive directory creation
When creating directories recursively, the logic should bail immediately on UV_EACCES and bubble the error to the user. PR-URL: #31505 Fixes: #31481 Reviewed-By: Anna Henningsen <anna@addaleax.net> Reviewed-By: Rich Trott <rtrott@gmail.com>
- Loading branch information
1 parent
a2b7006
commit 8669ecc
Showing
2 changed files
with
84 additions
and
7 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,71 @@ | ||
'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 { execSync } = require('child_process'); | ||
const fs = require('fs'); | ||
const path = require('path'); | ||
|
||
let n = 0; | ||
|
||
function makeDirectoryReadOnly(dir) { | ||
let accessErrorCode = 'EACCES'; | ||
if (common.isWindows) { | ||
accessErrorCode = 'EPERM'; | ||
execSync(`icacls ${dir} /inheritance:r`); | ||
execSync(`icacls ${dir} /deny "everyone":W`); | ||
} else { | ||
fs.chmodSync(dir, '444'); | ||
} | ||
return accessErrorCode; | ||
} | ||
|
||
function makeDirectoryWritable(dir) { | ||
if (common.isWindows) { | ||
execSync(`icacls ${dir} /grant "everyone":W`); | ||
} | ||
} | ||
|
||
// Synchronous API should return an EACCESS error with path populated. | ||
{ | ||
const dir = path.join(tmpdir.path, `mkdirp_${n++}`); | ||
fs.mkdirSync(dir); | ||
const codeExpected = makeDirectoryReadOnly(dir); | ||
let err = null; | ||
try { | ||
fs.mkdirSync(path.join(dir, '/foo'), { recursive: true }); | ||
} catch (_err) { | ||
err = _err; | ||
} | ||
makeDirectoryWritable(dir); | ||
assert(err); | ||
assert.strictEqual(err.code, codeExpected); | ||
assert(err.path); | ||
} | ||
|
||
// Asynchronous API should return an EACCESS error with path populated. | ||
{ | ||
const dir = path.join(tmpdir.path, `mkdirp_${n++}`); | ||
fs.mkdirSync(dir); | ||
const codeExpected = makeDirectoryReadOnly(dir); | ||
fs.mkdir(path.join(dir, '/bar'), { recursive: true }, (err) => { | ||
makeDirectoryWritable(dir); | ||
assert(err); | ||
assert.strictEqual(err.code, codeExpected); | ||
assert(err.path); | ||
}); | ||
} |