cache.js: preparation for git module (factor out getCacheStat) #4765

Closed
wants to merge 2 commits into
from
Jump to file or symbol
Failed to load files and symbols.
+208 −61
Split
View
@@ -84,6 +84,7 @@ var mkdir = require("mkdirp")
, which = require("which")
, isGitUrl = require("./utils/is-git-url.js")
, pathIsInside = require("path-is-inside")
+ , getCacheStat = require("npm-cache-dir").getCacheStat
cache.usage = "npm cache add <tarball file>"
+ "\nnpm cache add <folder>"
@@ -547,7 +548,7 @@ function archiveGitRemote (p, u, co, origUrl, cb) {
log.silly("verifyOwnership", "skipping for windows")
resolveHead()
} else {
- getCacheStat(function(er, cs) {
+ getCacheStat(npm.cache, log, function(er, cs) {
if (er) {
log.error("Could not get cache stat")
return cb(er)
@@ -969,65 +970,9 @@ function addLocalTarball (p, name, shasum, cb_) {
})
}
-// to maintain the cache dir's permissions consistently.
-var cacheStat = null
-function getCacheStat (cb) {
- if (cacheStat) return cb(null, cacheStat)
- fs.stat(npm.cache, function (er, st) {
- if (er) return makeCacheDir(cb)
- if (!st.isDirectory()) {
- log.error("getCacheStat", "invalid cache dir %j", npm.cache)
- return cb(er)
- }
- return cb(null, cacheStat = st)
- })
-}
-
-function makeCacheDir (cb) {
- if (!process.getuid) return mkdir(npm.cache, cb)
-
- var uid = +process.getuid()
- , gid = +process.getgid()
-
- if (uid === 0) {
- if (process.env.SUDO_UID) uid = +process.env.SUDO_UID
- if (process.env.SUDO_GID) gid = +process.env.SUDO_GID
- }
- if (uid !== 0 || !process.env.HOME) {
- cacheStat = {uid: uid, gid: gid}
- return mkdir(npm.cache, afterMkdir)
- }
-
- fs.stat(process.env.HOME, function (er, st) {
- if (er) {
- log.error("makeCacheDir", "homeless?")
- return cb(er)
- }
- cacheStat = st
- log.silly("makeCacheDir", "cache dir uid, gid", [st.uid, st.gid])
- return mkdir(npm.cache, afterMkdir)
- })
-
- function afterMkdir (er, made) {
- if (er || !cacheStat || isNaN(cacheStat.uid) || isNaN(cacheStat.gid)) {
- return cb(er, cacheStat)
- }
-
- if (!made) return cb(er, cacheStat)
-
- // ensure that the ownership is correct.
- chownr(made, cacheStat.uid, cacheStat.gid, function (er) {
- return cb(er, cacheStat)
- })
- }
-}
-
-
-
-
function addPlacedTarball (p, name, shasum, cb) {
if (!cb) cb = name, name = ""
- getCacheStat(function (er, cs) {
+ getCacheStat(npm.cache, log, function (er, cs) {
if (er) return cb(er)
return addPlacedTarball_(p, name, cs.uid, cs.gid, shasum, cb)
})
@@ -1188,7 +1133,7 @@ function addLocalDirectory (p, name, shasum, cb) {
, data.version, "package.tgz" )
, placeDirect = path.basename(p) === "package"
, tgz = placeDirect ? placed : tmptgz
- getCacheStat(function (er, cs) {
+ getCacheStat(npm.cache, log, function (er, cs) {
mkdir(path.dirname(tgz), function (er, made) {
if (er) return cb(er)
@@ -1217,7 +1162,7 @@ function addLocalDirectory (p, name, shasum, cb) {
function addTmpTarball (tgz, name, shasum, cb) {
if (!cb) cb = name, name = ""
- getCacheStat(function (er, cs) {
+ getCacheStat(npm.cache, log, function (er, cs) {
if (er) return cb(er)
var contents = path.dirname(tgz)
tar.unpack( tgz, path.resolve(contents, "package")
@@ -1278,7 +1223,7 @@ function lockFileName (u) {
var myLocks = {}
function lock (u, cb) {
// the cache dir needs to exist already for this.
- getCacheStat(function (er, cs) {
+ getCacheStat(npm.cache, log, function (er, cs) {
if (er) return cb(er)
var opts = { stale: npm.config.get("cache-lock-stale")
, retries: npm.config.get("cache-lock-retries")

Some generated files are not rendered by default. Learn more.

Oops, something went wrong.

Some generated files are not rendered by default. Learn more.

Oops, something went wrong.

Some generated files are not rendered by default. Learn more.

Oops, something went wrong.

Some generated files are not rendered by default. Learn more.

Oops, something went wrong.

Some generated files are not rendered by default. Learn more.

Oops, something went wrong.
View
@@ -60,6 +60,7 @@
"mkdirp": "~0.3.5",
"node-gyp": "~0.12.2",
"nopt": "^2.2.0",
+ "npm-cache-dir": "~1.0.0",
"npm-install-checks": "~1.0.0",
"npm-registry-client": "^0.4.4",
"npm-user-validate": "0.0.3",
@@ -113,6 +114,7 @@
"node-gyp",
"nopt",
"normalize-package-data",
+ "npm-cache-dir",
"npm-install-checks",
"npm-registry-client",
"npm-user-validate",