1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
|
'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} /deny "everyone:(OI)(CI)(DE,DC,AD,WD)"`);
} else {
fs.chmodSync(dir, '444');
}
return accessErrorCode;
}
function makeDirectoryWritable(dir) {
if (common.isWindows) {
execSync(`icacls ${dir} /remove:d "everyone"`);
}
}
// 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);
});
}
|