update
This commit is contained in:
+2
-2
@@ -8,9 +8,9 @@ function disposer (creatorFn, disposerFn, fn) {
|
||||
.then(
|
||||
// disposer resolved, do something with original fn's promise
|
||||
() => {
|
||||
if (shouldThrow) {
|
||||
if (shouldThrow)
|
||||
throw result
|
||||
}
|
||||
|
||||
return result
|
||||
},
|
||||
// Disposer fn failed, crash process
|
||||
|
||||
+10
-13
@@ -3,7 +3,7 @@
|
||||
const util = require('util')
|
||||
|
||||
const chownr = util.promisify(require('chownr'))
|
||||
const mkdirp = util.promisify(require('mkdirp'))
|
||||
const mkdirp = require('mkdirp')
|
||||
const inflight = require('promise-inflight')
|
||||
const inferOwner = require('infer-owner')
|
||||
|
||||
@@ -49,9 +49,8 @@ function fixOwner (cache, filepath) {
|
||||
const { uid, gid } = owner
|
||||
|
||||
// No need to override if it's already what we used.
|
||||
if (self.uid === uid && self.gid === gid) {
|
||||
if (self.uid === uid && self.gid === gid)
|
||||
return
|
||||
}
|
||||
|
||||
return inflight('fixOwner: fixing ownership on ' + filepath, () =>
|
||||
chownr(
|
||||
@@ -59,9 +58,9 @@ function fixOwner (cache, filepath) {
|
||||
typeof uid === 'number' ? uid : self.uid,
|
||||
typeof gid === 'number' ? gid : self.gid
|
||||
).catch((err) => {
|
||||
if (err.code === 'ENOENT') {
|
||||
if (err.code === 'ENOENT')
|
||||
return null
|
||||
}
|
||||
|
||||
throw err
|
||||
})
|
||||
)
|
||||
@@ -94,9 +93,9 @@ function fixOwnerSync (cache, filepath) {
|
||||
)
|
||||
} catch (err) {
|
||||
// only catch ENOENT, any other error is a problem.
|
||||
if (err.code === 'ENOENT') {
|
||||
if (err.code === 'ENOENT')
|
||||
return null
|
||||
}
|
||||
|
||||
throw err
|
||||
}
|
||||
}
|
||||
@@ -111,14 +110,13 @@ function mkdirfix (cache, p, cb) {
|
||||
return Promise.resolve(inferOwner(cache)).then(() => {
|
||||
return mkdirp(p)
|
||||
.then((made) => {
|
||||
if (made) {
|
||||
if (made)
|
||||
return fixOwner(cache, made).then(() => made)
|
||||
}
|
||||
})
|
||||
.catch((err) => {
|
||||
if (err.code === 'EEXIST') {
|
||||
if (err.code === 'EEXIST')
|
||||
return fixOwner(cache, p).then(() => null)
|
||||
}
|
||||
|
||||
throw err
|
||||
})
|
||||
})
|
||||
@@ -138,8 +136,7 @@ function mkdirfixSync (cache, p) {
|
||||
if (err.code === 'EEXIST') {
|
||||
fixOwnerSync(cache, p)
|
||||
return null
|
||||
} else {
|
||||
} else
|
||||
throw err
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
+22
-11
@@ -1,16 +1,19 @@
|
||||
'use strict'
|
||||
|
||||
const fs = require('graceful-fs')
|
||||
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 move = require('move-concurrently')
|
||||
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'
|
||||
|
||||
// This isn't quite an fs.rename -- the assumption is that
|
||||
// if `dest` already exists, and we get certain errors while
|
||||
// trying to move it, we should just not bother.
|
||||
@@ -23,22 +26,29 @@ function moveFile (src, dest) {
|
||||
return new Promise((resolve, reject) => {
|
||||
fs.link(src, dest, (err) => {
|
||||
if (err) {
|
||||
if (err.code === 'EEXIST' || err.code === 'EBUSY') {
|
||||
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
|
||||
} else if (err.code === 'EPERM' && process.platform === 'win32') {
|
||||
// file handle stayed open even past graceful-fs limits
|
||||
} else {
|
||||
return resolve()
|
||||
} else
|
||||
return reject(err)
|
||||
}
|
||||
}
|
||||
return resolve()
|
||||
} else
|
||||
return resolve()
|
||||
})
|
||||
})
|
||||
.then(() => {
|
||||
// content should never change for any reason, so make it read-only
|
||||
return Promise.all([
|
||||
unlink(src),
|
||||
process.platform !== 'win32' && chmod(dest, '0444')
|
||||
!isWindows && chmod(dest, '0444'),
|
||||
])
|
||||
})
|
||||
.catch(() => {
|
||||
@@ -49,7 +59,8 @@ function moveFile (src, dest) {
|
||||
throw err
|
||||
}
|
||||
// file doesn't already exist! let's try a rename -> copy fallback
|
||||
return move(src, dest, { Promise, fs })
|
||||
// only delete if it successfully copies
|
||||
return move(src, dest)
|
||||
})
|
||||
})
|
||||
})
|
||||
|
||||
+12
-19
@@ -1,26 +1,21 @@
|
||||
'use strict'
|
||||
|
||||
const util = require('util')
|
||||
const fs = require('@npmcli/fs')
|
||||
|
||||
const figgyPudding = require('figgy-pudding')
|
||||
const fixOwner = require('./fix-owner')
|
||||
const path = require('path')
|
||||
const rimraf = util.promisify(require('rimraf'))
|
||||
const uniqueFilename = require('unique-filename')
|
||||
const { disposer } = require('./disposer')
|
||||
|
||||
const TmpOpts = figgyPudding({
|
||||
tmpPrefix: {}
|
||||
})
|
||||
|
||||
module.exports.mkdir = mktmpdir
|
||||
|
||||
function mktmpdir (cache, opts) {
|
||||
opts = TmpOpts(opts)
|
||||
const tmpTarget = uniqueFilename(path.join(cache, 'tmp'), opts.tmpPrefix)
|
||||
return fixOwner.mkdirfix(cache, tmpTarget).then(() => {
|
||||
return tmpTarget
|
||||
})
|
||||
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' })
|
||||
})
|
||||
}
|
||||
|
||||
module.exports.withTmp = withTmp
|
||||
@@ -28,11 +23,9 @@ module.exports.withTmp = withTmp
|
||||
function withTmp (cache, opts, cb) {
|
||||
if (!cb) {
|
||||
cb = opts
|
||||
opts = null
|
||||
opts = {}
|
||||
}
|
||||
opts = TmpOpts(opts)
|
||||
|
||||
return disposer(mktmpdir(cache, opts), rimraf, cb)
|
||||
return fs.withTempDir(path.join(cache, 'tmp'), cb, opts)
|
||||
}
|
||||
|
||||
module.exports.fix = fixtmpdir
|
||||
|
||||
Reference in New Issue
Block a user