diff options
author | Forrest L Norvell <forrest@npmjs.com> | 2014-09-27 04:04:43 +0400 |
---|---|---|
committer | Forrest L Norvell <forrest@npmjs.com> | 2014-09-27 04:04:43 +0400 |
commit | 563225d813ea4c12f46d4f7821ac7f76ba8ee2d6 (patch) | |
tree | c49affc584961a177252f43d8c644b81f68fb4fb | |
parent | c7f30e4550fea882d31fcd4a55b681cd30713c44 (diff) |
clean up locking; prefix lockfile with "."
-rw-r--r-- | lib/install.js | 47 | ||||
-rw-r--r-- | lib/utils/locker.js | 60 |
2 files changed, 33 insertions, 74 deletions
diff --git a/lib/install.js b/lib/install.js index fe36ee017..0a4409aea 100644 --- a/lib/install.js +++ b/lib/install.js @@ -73,7 +73,6 @@ var npm = require("./npm.js") , log = require("npmlog") , path = require("path") , fs = require("graceful-fs") - , crypto = require("crypto") , writeFileAtomic = require("write-file-atomic") , cache = require("./cache.js") , asyncMap = require("slide").asyncMap @@ -86,7 +85,9 @@ var npm = require("./npm.js") , sortedObject = require("sorted-object") , mapToRegistry = require("./utils/map-to-registry.js") , npa = require("npm-package-arg") - , lockfile = require("lockfile") + , locker = require("./utils/locker.js") + , lock = locker.lock + , unlock = locker.unlock function install (args, cb_) { var hasArguments = !!args.length @@ -1049,45 +1050,3 @@ function prepareForInstallMany (packageData, depsKey, bundled, wrap, family) { return t }) } - -// copied from lib/util/locker.js because I don't feel like dealing with -// that module's desire to run getCacheStat -var installLocks = {} -function lockFileName (base, name) { - var c = name.replace(/[^a-zA-Z0-9]+/g, "-").replace(/^-+|-+$/g, "") - , h = crypto.createHash("sha1").update(name).digest("hex") - var generated = h.substr(0, 8) + "-" + c.substr(-32) - log.silly("lockFile", generated, name) - return path.resolve(base, generated + ".lock") -} - -function lock (base, name, cb) { - mkdir(base, function (er) { - if (er) return cb(er) - - var opts = { stale: npm.config.get("cache-lock-stale") - , retries: npm.config.get("cache-lock-retries") - , wait: npm.config.get("cache-lock-wait") } - var lf = lockFileName(base, name) - log.verbose("lock", name, lf) - lockfile.lock(lf, opts, function(er) { - if (!er) installLocks[lf] = true - cb(er) - }) - }) -} - -function unlock (base, name, cb) { - var lf = lockFileName(base, name) - , locked = installLocks[lf] - if (locked === false) { - return process.nextTick(cb) - } - else if (locked === true) { - installLocks[lf] = false - lockfile.unlock(lf, cb) - } - else { - throw new Error("Attempt to unlock " + name + ", which hasn't been locked") - } -} diff --git a/lib/utils/locker.js b/lib/utils/locker.js index 343ebce35..465c50e72 100644 --- a/lib/utils/locker.js +++ b/lib/utils/locker.js @@ -2,51 +2,51 @@ var crypto = require("crypto") var path = require("path") var npm = require("../npm.js") -var lockFile = require("lockfile") +var lockfile = require("lockfile") var log = require("npmlog") -var getCacheStat = require("../cache/get-stat.js") +var mkdirp = require("mkdirp") -function lockFileName (u) { - var c = u.replace(/[^a-zA-Z0-9]+/g, "-").replace(/^-+|-+$/g, "") - , h = crypto.createHash("sha1").update(u).digest("hex") - h = h.substr(0, 8) - c = c.substr(-32) - log.silly("lockFile", h + "-" + c, u) - return path.resolve(npm.config.get("cache"), h + "-" + c + ".lock") +var installLocks = {} +function lockFileName (base, name) { + var c = name.replace(/[^a-zA-Z0-9]+/g, "-").replace(/^-+|-+$/g, "") + , h = crypto.createHash("sha1").update(name).digest("hex") + var generated = "." + h.substr(0, 8) + "-" + c.substr(-32) + log.silly("lockFile", generated, name) + return path.resolve(base, generated + ".lock") } -var myLocks = {} -function lock (u, cb) { - // the cache dir needs to exist already for this. - getCacheStat(function (er) { +function lock (base, name, cb) { + mkdirp(base, function (er) { if (er) return cb(er) - var opts = { stale: npm.config.get("cache-lock-stale") + + var opts = { stale: npm.config.get("cache-lock-stale") , retries: npm.config.get("cache-lock-retries") - , wait: npm.config.get("cache-lock-wait") } - var lf = lockFileName(u) - log.verbose("lock", u, lf) - lockFile.lock(lf, opts, function(er) { - if (!er) myLocks[lf] = true + , wait: npm.config.get("cache-lock-wait") } + var lf = lockFileName(base, name) + log.verbose("lock", name, lf) + lockfile.lock(lf, opts, function(er) { + if (!er) installLocks[lf] = true cb(er) }) }) } -function unlock (u, cb) { - var lf = lockFileName(u) - , locked = myLocks[lf] +function unlock (base, name, cb) { + var lf = lockFileName(base, name) + , locked = installLocks[lf] if (locked === false) { return process.nextTick(cb) - } else if (locked === true) { - myLocks[lf] = false - lockFile.unlock(lockFileName(u), cb) - } else { - throw new Error("Attempt to unlock " + u + ", which hasn't been locked") + } + else if (locked === true) { + installLocks[lf] = false + lockfile.unlock(lf, cb) + } + else { + throw new Error("Attempt to unlock " + name + ", which hasn't been locked") } } module.exports = { - lock: lock, - unlock: unlock, - _lockFileName: lockFileName + lock : lock, + unlock : unlock } |