diff options
Diffstat (limited to 'deps/npm/lib/cache.js')
-rw-r--r-- | deps/npm/lib/cache.js | 125 |
1 files changed, 62 insertions, 63 deletions
diff --git a/deps/npm/lib/cache.js b/deps/npm/lib/cache.js index b62e82dd14..3dc1fb3a0a 100644 --- a/deps/npm/lib/cache.js +++ b/deps/npm/lib/cache.js @@ -3,10 +3,11 @@ /* adding a folder: 1. tar into tmp/random/package.tgz -2. untar into tmp/random/contents/package, stripping one dir piece -3. tar tmp/random/contents/package to cache/n/v/package.tgz -4. untar cache/n/v/package.tgz into cache/n/v/package -5. rm tmp/random +2. untar into tmp/random/contents/{blah} +3. rename {blah} to "package" +4. tar tmp/random/contents/package to cache/n/v/package.tgz +5. untar cache/n/v/package.tgz into cache/n/v/package +6. rm tmp/random Adding a url: 1. fetch to tmp/random/package.tgz @@ -31,7 +32,7 @@ exports.read = read exports.clean = clean exports.unpack = unpack -var mkdir = require("mkdirp") +var mkdir = require("./utils/mkdir-p.js") , exec = require("./utils/exec.js") , fetch = require("./utils/fetch.js") , npm = require("./npm.js") @@ -49,7 +50,6 @@ var mkdir = require("mkdirp") , tar = require("./utils/tar.js") , fileCompletion = require("./utils/completion/file-completion.js") , url = require("url") - , chownr = require("chownr") cache.usage = "npm cache add <tarball file>" + "\nnpm cache add <folder>" @@ -95,10 +95,12 @@ function cache (args, cb) { // if the pkg and ver are in the cache, then // just do a readJson and return. // if they're not, then fetch them from the registry. +var cacheSeen = {} function read (name, ver, forceBypass, cb) { if (typeof cb !== "function") cb = forceBypass, forceBypass = true var jsonFile = path.join(npm.cache, name, ver, "package", "package.json") function c (er, data) { + if (!er) cacheSeen[data._id] = data if (data) deprCheck(data) return cb(er, data) } @@ -108,6 +110,10 @@ function read (name, ver, forceBypass, cb) { return addNamed(name, ver, c) } + if (name+"@"+ver in cacheSeen) { + return cb(null, cacheSeen[name+"@"+ver]) + } + readJson(jsonFile, function (er, data) { if (er) return addNamed(name, ver, c) deprCheck(data) @@ -120,13 +126,9 @@ function ls (args, cb) { output = output || require("./utils/output.js") args = args.join("/").split("@").join("/") if (args.substr(-1) === "/") args = args.substr(0, args.length - 1) - var prefix = npm.config.get("cache") - if (0 === prefix.indexOf(process.env.HOME)) { - prefix = "~" + prefix.substr(process.env.HOME.length) - } ls_(args, npm.config.get("depth"), function(er, files) { output.write(files.map(function (f) { - return path.join(prefix, f) + return path.join("~/.npm", f) }).join("\n").trim(), function (er) { return cb(er, files) }) @@ -210,7 +212,7 @@ function add (args, cb) { // see if the spec is a url // otherwise, treat as name@version - var p = url.parse(spec) || {} + var p = url.parse(spec.replace(/^git\+/, "git")) || {} log.verbose(p, "parsed url") // it could be that we got name@http://blah @@ -228,11 +230,11 @@ function add (args, cb) { case "https:": return addRemoteTarball(spec, null, name, cb) case "git:": - case "git+http:": - case "git+https:": - case "git+rsync:": - case "git+ftp:": - case "git+ssh:": + case "githttp:": + case "githttps:": + case "gitrsync:": + case "gitftp:": + case "gitssh:": //p.protocol = p.protocol.replace(/^git([^:])/, "$1") return addRemoteGit(spec, p, name, cb) default: @@ -634,7 +636,7 @@ function getCacheStat (cb) { } function makeCacheDir (cb) { - if (!process.getuid) return mkdir(npm.cache, cb) + if (!process.getuid) return mkdir(npm.cache, npm.modes.exec, cb) var uid = +process.getuid() , gid = +process.getgid() @@ -645,28 +647,18 @@ function makeCacheDir (cb) { } if (uid !== 0 || !process.env.HOME) { cacheStat = {uid: uid, gid: gid} - return mkdir(npm.cache, afterMkdir) + return mkdir(npm.cache, npm.modes.exec, uid, gid, function (er) { + return cb(er, cacheStat) + }) } - fs.stat(process.env.HOME, function (er, st) { if (er) return log.er(cb, "homeless?")(er) cacheStat = st log.silly([st.uid, st.gid], "uid, gid for cache dir") - 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 mkdir(npm.cache, npm.modes.exec, st.uid, st.gid, function (er) { return cb(er, cacheStat) }) - } + }) } @@ -744,20 +736,9 @@ function addLocalDirectory (p, name, cb) { , tgz = placeDirect ? placed : tmptgz , doFancyCrap = p.indexOf(npm.tmp) !== 0 && p.indexOf(npm.cache) !== 0 - getCacheStat(function (er, cs) { - mkdir(path.dirname(tgz), function (er, made) { - if (er) return cb(er) - tar.pack(tgz, p, data, doFancyCrap, function (er) { - if (er) return log.er(cb,"couldn't pack "+p+ " to "+tgz)(er) - - if (er || !cs || isNaN(cs.uid) || isNaN(cs.gid)) return cb() - - chownr(made || tgz, cs.uid, cs.gid, function (er) { - if (er) return cb(er) - addLocalTarball(tgz, name, cb) - }) - }) - }) + tar.pack(tgz, p, data, doFancyCrap, function (er) { + if (er) return log.er(cb,"couldn't pack "+p+ " to "+tgz)(er) + addLocalTarball(tgz, name, cb) }) }) } @@ -766,15 +747,36 @@ function addTmpTarball (tgz, name, cb) { if (!cb) cb = name, name = "" getCacheStat(function (er, cs) { if (er) return cb(er) - var contents = path.dirname(tgz) - tar.unpack( tgz, path.resolve(contents, "package") - , null, null - , cs.uid, cs.gid - , function (er) { - if (er) { - return cb(er) + return addTmpTarball_(tgz, name, cs.uid, cs.gid, cb) + }) +} + +function addTmpTarball_ (tgz, name, uid, gid, cb) { + var contents = path.resolve(path.dirname(tgz)) // , "contents") + tar.unpack( tgz, path.resolve(contents, "package") + , null, null + , uid, gid + , function (er) { + if (er) return log.er(cb, "couldn't unpack "+tgz+" to "+contents)(er) + fs.readdir(contents, function (er, folder) { + if (er) return log.er(cb, "couldn't readdir "+contents)(er) + log.verbose(folder, "tarball contents") + if (folder.length > 1) { + folder = folder.filter(function (f) { + return !f.match(/^\.|^tmp\.tgz$/) + }) + } + if (folder.length > 1) { + log.warn(folder.slice(1).join("\n") + ,"extra junk in folder, ignoring") } - addLocalDirectory(path.resolve(contents, "package"), name, cb) + if (!folder.length) return cb(new Error("Empty package tarball")) + folder = path.join(contents, folder[0]) + var newName = path.join(contents, "package") + fs.rename(folder, newName, function (er) { + if (er) return log.er(cb, "couldn't rename "+folder+" to package")(er) + addLocalDirectory(newName, name, cb) + }) }) }) } @@ -790,14 +792,11 @@ function unpack (pkg, ver, unpackTarget, dMode, fMode, uid, gid, cb) { log.error("Could not read data for "+pkg+"@"+ver) return cb(er) } - npm.commands.unbuild([unpackTarget], function (er) { - if (er) return cb(er) - tar.unpack( path.join(npm.cache, pkg, ver, "package.tgz") - , unpackTarget - , dMode, fMode - , uid, gid - , cb ) - }) + tar.unpack( path.join(npm.cache, pkg, ver, "package.tgz") + , unpackTarget + , dMode, fMode + , uid, gid + , cb ) }) } |