summaryrefslogtreecommitdiff
path: root/deps/npm/lib/utils/cache-file.js
blob: 7b3136b2e2db9385609e50efaf89c6d518e09b31 (plain)
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
// XXX use infer-owner or gentle-fs.mkdir here
const npm = require('../npm.js')
const path = require('path')
const chownr = require('chownr')
const writeFileAtomic = require('write-file-atomic')
const mkdirp = require('mkdirp')
const fs = require('graceful-fs')

let cache = null
let cacheUid = null
let cacheGid = null
let needChown = typeof process.getuid === 'function'

const getCacheOwner = () => {
  let st
  try {
    st = fs.lstatSync(cache)
  } catch (er) {
    if (er.code !== 'ENOENT') {
      throw er
    }
    st = fs.lstatSync(path.dirname(cache))
  }

  cacheUid = st.uid
  cacheGid = st.gid

  needChown = st.uid !== process.getuid() ||
    st.gid !== process.getgid()
}

const writeOrAppend = (method, file, data) => {
  if (!cache) {
    cache = npm.config.get('cache')
  }

  // redundant if already absolute, but prevents non-absolute files
  // from being written as if they're part of the cache.
  file = path.resolve(cache, file)

  if (cacheUid === null && needChown) {
    getCacheOwner()
  }

  const dir = path.dirname(file)
  const firstMade = mkdirp.sync(dir)

  if (!needChown) {
    return method(file, data)
  }

  let methodThrew = true
  try {
    method(file, data)
    methodThrew = false
  } finally {
    // always try to leave it in the right ownership state, even on failure
    // let the method error fail it instead of the chownr error, though
    if (!methodThrew) {
      chownr.sync(firstMade || file, cacheUid, cacheGid)
    } else {
      try {
        chownr.sync(firstMade || file, cacheUid, cacheGid)
      } catch (_) {}
    }
  }
}

exports.append = (file, data) => writeOrAppend(fs.appendFileSync, file, data)
exports.write = (file, data) => writeOrAppend(writeFileAtomic.sync, file, data)