summaryrefslogtreecommitdiff
path: root/deps/npm/node_modules/cacache/lib/util
diff options
context:
space:
mode:
Diffstat (limited to 'deps/npm/node_modules/cacache/lib/util')
-rw-r--r--deps/npm/node_modules/cacache/lib/util/fix-owner.js75
-rw-r--r--deps/npm/node_modules/cacache/lib/util/move-file.js83
-rw-r--r--deps/npm/node_modules/cacache/lib/util/tmp.js12
3 files changed, 76 insertions, 94 deletions
diff --git a/deps/npm/node_modules/cacache/lib/util/fix-owner.js b/deps/npm/node_modules/cacache/lib/util/fix-owner.js
index bc14def4e4..182fcb028f 100644
--- a/deps/npm/node_modules/cacache/lib/util/fix-owner.js
+++ b/deps/npm/node_modules/cacache/lib/util/fix-owner.js
@@ -33,40 +33,38 @@ const getSelf = () => {
module.exports.chownr = fixOwner
-function fixOwner (cache, filepath) {
+async function fixOwner (cache, filepath) {
if (!process.getuid) {
// This platform doesn't need ownership fixing
- return Promise.resolve()
+ return
}
getSelf()
if (self.uid !== 0) {
// almost certainly can't chown anyway
- return Promise.resolve()
+ return
}
- return Promise.resolve(inferOwner(cache)).then((owner) => {
- const { uid, gid } = owner
+ const { uid, gid } = await inferOwner(cache)
- // No need to override if it's already what we used.
- if (self.uid === uid && self.gid === gid) {
- return
- }
+ // No need to override if it's already what we used.
+ if (self.uid === uid && self.gid === gid) {
+ return
+ }
- return inflight('fixOwner: fixing ownership on ' + filepath, () =>
- chownr(
- filepath,
- typeof uid === 'number' ? uid : self.uid,
- typeof gid === 'number' ? gid : self.gid
- ).catch((err) => {
- if (err.code === 'ENOENT') {
- return null
- }
-
- throw err
- })
- )
- })
+ return inflight('fixOwner: fixing ownership on ' + filepath, () =>
+ chownr(
+ filepath,
+ typeof uid === 'number' ? uid : self.uid,
+ typeof gid === 'number' ? gid : self.gid
+ ).catch((err) => {
+ if (err.code === 'ENOENT') {
+ return null
+ }
+
+ throw err
+ })
+ )
}
module.exports.chownr.sync = fixOwnerSync
@@ -105,26 +103,25 @@ function fixOwnerSync (cache, filepath) {
module.exports.mkdirfix = mkdirfix
-function mkdirfix (cache, p, cb) {
+async function mkdirfix (cache, p, cb) {
// we have to infer the owner _before_ making the directory, even though
// we aren't going to use the results, since the cache itself might not
// exist yet. If we mkdirp it, then our current uid/gid will be assumed
// to be correct if it creates the cache folder in the process.
- return Promise.resolve(inferOwner(cache)).then(() => {
- return mkdirp(p)
- .then((made) => {
- if (made) {
- return fixOwner(cache, made).then(() => made)
- }
- })
- .catch((err) => {
- if (err.code === 'EEXIST') {
- return fixOwner(cache, p).then(() => null)
- }
-
- throw err
- })
- })
+ await inferOwner(cache)
+ try {
+ const made = await mkdirp(p)
+ if (made) {
+ await fixOwner(cache, made)
+ return made
+ }
+ } catch (err) {
+ if (err.code === 'EEXIST') {
+ await fixOwner(cache, p)
+ return null
+ }
+ throw err
+ }
}
module.exports.mkdirfix.sync = mkdirfixSync
diff --git a/deps/npm/node_modules/cacache/lib/util/move-file.js b/deps/npm/node_modules/cacache/lib/util/move-file.js
index 3739cea3df..a0b40413cb 100644
--- a/deps/npm/node_modules/cacache/lib/util/move-file.js
+++ b/deps/npm/node_modules/cacache/lib/util/move-file.js
@@ -1,18 +1,13 @@
'use strict'
-const fs = require('fs')
-const util = require('util')
-const chmod = util.promisify(fs.chmod)
-const unlink = util.promisify(fs.unlink)
-const stat = util.promisify(fs.stat)
+const fs = require('@npmcli/fs')
const move = require('@npmcli/move-file')
const pinflight = require('promise-inflight')
module.exports = moveFile
-function moveFile (src, dest) {
- const isWindows = global.__CACACHE_TEST_FAKE_WINDOWS__ ||
- process.platform === 'win32'
+async function moveFile (src, dest) {
+ const isWindows = process.platform === 'win32'
// This isn't quite an fs.rename -- the assumption is that
// if `dest` already exists, and we get certain errors while
@@ -23,47 +18,39 @@ function moveFile (src, dest) {
// content their own way.
//
// Note that, as the name suggests, this strictly only supports file moves.
- return new Promise((resolve, reject) => {
- fs.link(src, dest, (err) => {
- if (err) {
- if (isWindows && err.code === 'EPERM') {
- // XXX This is a really weird way to handle this situation, as it
- // results in the src file being deleted even though the dest
- // might not exist. Since we pretty much always write files to
- // deterministic locations based on content hash, this is likely
- // ok (or at worst, just ends in a future cache miss). But it would
- // be worth investigating at some time in the future if this is
- // really what we want to do here.
- return resolve()
- } else if (err.code === 'EEXIST' || err.code === 'EBUSY') {
- // file already exists, so whatever
- return resolve()
- } else {
- return reject(err)
+ try {
+ await fs.link(src, dest)
+ } catch (err) {
+ if (isWindows && err.code === 'EPERM') {
+ // XXX This is a really weird way to handle this situation, as it
+ // results in the src file being deleted even though the dest
+ // might not exist. Since we pretty much always write files to
+ // deterministic locations based on content hash, this is likely
+ // ok (or at worst, just ends in a future cache miss). But it would
+ // be worth investigating at some time in the future if this is
+ // really what we want to do here.
+ } else if (err.code === 'EEXIST' || err.code === 'EBUSY') {
+ // file already exists, so whatever
+ } else {
+ throw err
+ }
+ }
+ try {
+ await Promise.all([
+ fs.unlink(src),
+ !isWindows && fs.chmod(dest, '0444'),
+ ])
+ } catch (e) {
+ return pinflight('cacache-move-file:' + dest, async () => {
+ await fs.stat(dest).catch((err) => {
+ if (err.code !== 'ENOENT') {
+ // Something else is wrong here. Bail bail bail
+ throw err
}
- } else {
- return resolve()
- }
- })
- })
- .then(() => {
- // content should never change for any reason, so make it read-only
- return Promise.all([
- unlink(src),
- !isWindows && chmod(dest, '0444'),
- ])
- })
- .catch(() => {
- return pinflight('cacache-move-file:' + dest, () => {
- return stat(dest).catch((err) => {
- if (err.code !== 'ENOENT') {
- // Something else is wrong here. Bail bail bail
- throw err
- }
- // file doesn't already exist! let's try a rename -> copy fallback
- // only delete if it successfully copies
- return move(src, dest)
- })
})
+ // file doesn't already exist! let's try a rename -> copy fallback
+ // only delete if it successfully copies
+ return move(src, dest)
})
+ }
}
diff --git a/deps/npm/node_modules/cacache/lib/util/tmp.js b/deps/npm/node_modules/cacache/lib/util/tmp.js
index 0a5a50eba3..b4437cfcbe 100644
--- a/deps/npm/node_modules/cacache/lib/util/tmp.js
+++ b/deps/npm/node_modules/cacache/lib/util/tmp.js
@@ -7,15 +7,13 @@ const path = require('path')
module.exports.mkdir = mktmpdir
-function mktmpdir (cache, opts = {}) {
+async function mktmpdir (cache, opts = {}) {
const { tmpPrefix } = opts
const tmpDir = path.join(cache, 'tmp')
- return fs.mkdir(tmpDir, { recursive: true, owner: 'inherit' })
- .then(() => {
- // do not use path.join(), it drops the trailing / if tmpPrefix is unset
- const target = `${tmpDir}${path.sep}${tmpPrefix || ''}`
- return fs.mkdtemp(target, { owner: 'inherit' })
- })
+ await fs.mkdir(tmpDir, { recursive: true, owner: 'inherit' })
+ // do not use path.join(), it drops the trailing / if tmpPrefix is unset
+ const target = `${tmpDir}${path.sep}${tmpPrefix || ''}`
+ return fs.mkdtemp(target, { owner: 'inherit' })
}
module.exports.withTmp = withTmp