source: trip-planner-front/node_modules/cacache/lib/util/fix-owner.js

Last change on this file was 6a3a178, checked in by Ema <ema_spirova@…>, 3 years ago

initial commit

  • Property mode set to 100644
File size: 3.4 KB
Line 
1'use strict'
2
3const util = require('util')
4
5const chownr = util.promisify(require('chownr'))
6const mkdirp = require('mkdirp')
7const inflight = require('promise-inflight')
8const inferOwner = require('infer-owner')
9
10// Memoize getuid()/getgid() calls.
11// patch process.setuid/setgid to invalidate cached value on change
12const self = { uid: null, gid: null }
13const getSelf = () => {
14 if (typeof self.uid !== 'number') {
15 self.uid = process.getuid()
16 const setuid = process.setuid
17 process.setuid = (uid) => {
18 self.uid = null
19 process.setuid = setuid
20 return process.setuid(uid)
21 }
22 }
23 if (typeof self.gid !== 'number') {
24 self.gid = process.getgid()
25 const setgid = process.setgid
26 process.setgid = (gid) => {
27 self.gid = null
28 process.setgid = setgid
29 return process.setgid(gid)
30 }
31 }
32}
33
34module.exports.chownr = fixOwner
35
36function fixOwner (cache, filepath) {
37 if (!process.getuid) {
38 // This platform doesn't need ownership fixing
39 return Promise.resolve()
40 }
41
42 getSelf()
43 if (self.uid !== 0) {
44 // almost certainly can't chown anyway
45 return Promise.resolve()
46 }
47
48 return Promise.resolve(inferOwner(cache)).then((owner) => {
49 const { uid, gid } = owner
50
51 // No need to override if it's already what we used.
52 if (self.uid === uid && self.gid === gid)
53 return
54
55 return inflight('fixOwner: fixing ownership on ' + filepath, () =>
56 chownr(
57 filepath,
58 typeof uid === 'number' ? uid : self.uid,
59 typeof gid === 'number' ? gid : self.gid
60 ).catch((err) => {
61 if (err.code === 'ENOENT')
62 return null
63
64 throw err
65 })
66 )
67 })
68}
69
70module.exports.chownr.sync = fixOwnerSync
71
72function fixOwnerSync (cache, filepath) {
73 if (!process.getuid) {
74 // This platform doesn't need ownership fixing
75 return
76 }
77 const { uid, gid } = inferOwner.sync(cache)
78 getSelf()
79 if (self.uid !== 0) {
80 // almost certainly can't chown anyway
81 return
82 }
83
84 if (self.uid === uid && self.gid === gid) {
85 // No need to override if it's already what we used.
86 return
87 }
88 try {
89 chownr.sync(
90 filepath,
91 typeof uid === 'number' ? uid : self.uid,
92 typeof gid === 'number' ? gid : self.gid
93 )
94 } catch (err) {
95 // only catch ENOENT, any other error is a problem.
96 if (err.code === 'ENOENT')
97 return null
98
99 throw err
100 }
101}
102
103module.exports.mkdirfix = mkdirfix
104
105function mkdirfix (cache, p, cb) {
106 // we have to infer the owner _before_ making the directory, even though
107 // we aren't going to use the results, since the cache itself might not
108 // exist yet. If we mkdirp it, then our current uid/gid will be assumed
109 // to be correct if it creates the cache folder in the process.
110 return Promise.resolve(inferOwner(cache)).then(() => {
111 return mkdirp(p)
112 .then((made) => {
113 if (made)
114 return fixOwner(cache, made).then(() => made)
115 })
116 .catch((err) => {
117 if (err.code === 'EEXIST')
118 return fixOwner(cache, p).then(() => null)
119
120 throw err
121 })
122 })
123}
124
125module.exports.mkdirfix.sync = mkdirfixSync
126
127function mkdirfixSync (cache, p) {
128 try {
129 inferOwner.sync(cache)
130 const made = mkdirp.sync(p)
131 if (made) {
132 fixOwnerSync(cache, made)
133 return made
134 }
135 } catch (err) {
136 if (err.code === 'EEXIST') {
137 fixOwnerSync(cache, p)
138 return null
139 } else
140 throw err
141 }
142}
Note: See TracBrowser for help on using the repository browser.