diff options
author | isaacs <i@izs.me> | 2014-06-11 04:59:29 +0400 |
---|---|---|
committer | isaacs <i@izs.me> | 2014-06-11 04:59:29 +0400 |
commit | 5ceb01b86809f2bcf693a0ab5e18468975572b49 (patch) | |
tree | 339ba789ed84848022f3ecdf410c90f5307121aa /lib | |
parent | 76c03da79fe6dc8e946823b7373b543f81e55724 (diff) |
cache: atomic de-race-ified package.json writing
a. Use a tmp file with the pid, and mv into place
b. Only write it once, use inflight to hop on it if in progress
Diffstat (limited to 'lib')
-rw-r--r-- | lib/cache.js | 14 |
1 files changed, 12 insertions, 2 deletions
diff --git a/lib/cache.js b/lib/cache.js index 9905ea4a7..37bba5a06 100644 --- a/lib/cache.js +++ b/lib/cache.js @@ -71,6 +71,7 @@ var npm = require("./npm.js") , addLocal = require("./cache/add-local.js") , addRemoteTarball = require("./cache/add-remote-tarball.js") , addRemoteGit = require("./cache/add-remote-git.js") + , inflight = require("inflight") cache.usage = "npm cache add <tarball file>" + "\nnpm cache add <folder>" @@ -317,8 +318,17 @@ function afterAdd (arg, cb) { return function (er, data) { var name = data.name var ver = data.version var pj = path.join(npm.cache, name, ver, "package", "package.json") - fs.writeFile(pj, JSON.stringify(data), "utf8", function (er) { - cb(er, data) + var tmp = pj + "." + process.pid + + var done = inflight(pj, cb) + + if (!done) return + + fs.writeFile(tmp, JSON.stringify(data), "utf8", function (er) { + if (er) return done(er) + fs.rename(tmp, pj, function (er) { + done(er, data) + }) }) }} |