/
correct-mkdir.js
65 lines (58 loc) · 1.77 KB
/
correct-mkdir.js
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
const chownr = require('chownr')
const fs = require('graceful-fs')
const inflight = require('inflight')
const log = require('npmlog')
const mkdirp = require('mkdirp')
const { dirname } = require('path')
// retain ownership of the parent dir
// this matches behavior in cacache to infer the cache ownership
// based on the ownership of the cache folder or it is parent.
module.exports = function correctMkdir (path, cb) {
cb = inflight('correctMkdir: ' + path, cb)
if (!cb) {
return log.verbose('correctMkdir', path, 'correctMkdir already in flight; waiting')
} else {
log.verbose('correctMkdir', path, 'correctMkdir not in flight; initializing')
}
if (!process.getuid) {
log.verbose('makeCacheDir', 'UID & GID are irrelevant on', process.platform)
return mkdirp(path, (er, made) => cb(er, { uid: 0, gid: 0 }))
}
inferOwner(path, (er, owner) => {
if (er) {
log.error('correctMkdir', 'failed to infer path ownership %s', path)
return cb(er)
}
mkdirp(path, (er, made) => {
if (er) {
log.error('correctMkdir', 'failed to make directory %s', path)
return cb(er)
}
chownr(made || path, owner.uid, owner.gid, (er) => cb(er, owner))
})
})
}
const pathToOwner = new Map()
const inferOwner = (path, cb) => {
if (pathToOwner.has(path)) {
return cb(null, pathToOwner.get(path))
}
const parent = dirname(path)
fs.lstat(path, (er, st) => {
if (er && parent !== path) {
inferOwner(parent, (er, owner) => {
if (er) {
return cb(er)
}
pathToOwner.set(path, owner)
return cb(null, owner)
})
} else if (er) {
cb(er)
} else {
const owner = { uid: st.uid, gid: st.gid }
pathToOwner.set(path, owner)
cb(null, owner)
}
})
}