diff options
author | isaacs <i@izs.me> | 2014-09-20 03:24:08 +0400 |
---|---|---|
committer | isaacs <i@izs.me> | 2014-09-20 03:29:10 +0400 |
commit | 6784767fe15e28b44c81a1d4bb1738c642a65d78 (patch) | |
tree | a78de762068367ed584cfb3cd9ccbc53d78a8ba2 /lib | |
parent | 42c872b32cadc0e555638fc78eab3a38a04401d8 (diff) |
Make all write streams atomic
IT IS ALWAYS A #%@^& RACE CONDITION!
Diffstat (limited to 'lib')
-rw-r--r-- | lib/cache/add-local-tarball.js | 11 | ||||
-rw-r--r-- | lib/cache/add-remote-git.js | 3 | ||||
-rw-r--r-- | lib/pack.js | 3 | ||||
-rw-r--r-- | lib/utils/error-handler.js | 3 |
4 files changed, 10 insertions, 10 deletions
diff --git a/lib/cache/add-local-tarball.js b/lib/cache/add-local-tarball.js index ad8563800..10aa97681 100644 --- a/lib/cache/add-local-tarball.js +++ b/lib/cache/add-local-tarball.js @@ -16,6 +16,7 @@ var mkdir = require("mkdirp") , chownr = require("chownr") , inflight = require("inflight") , once = require("once") + , writeStream = require('fs-write-stream-atomic') module.exports = addLocalTarball @@ -59,7 +60,7 @@ function addLocalTarball (p, pkgData, shasum, cb_) { mkdir(path.dirname(tmp), function (er) { if (er) return cb(er) var from = fs.createReadStream(p) - , to = fs.createWriteStream(tmp) + , to = writeStream(tmp, { mode: npm.modes.file }) , errState = null function errHandler (er) { if (errState) return @@ -69,11 +70,7 @@ function addLocalTarball (p, pkgData, shasum, cb_) { to.on("error", errHandler) to.on("close", function () { if (errState) return - log.verbose("chmod", tmp, npm.modes.file.toString(8)) - fs.chmod(tmp, npm.modes.file, function (er) { - if (er) return cb(er) - addTmpTarball(tmp, pkgData, shasum, cb) - }) + addTmpTarball(tmp, pkgData, shasum, cb) }) from.pipe(to) }) @@ -203,7 +200,7 @@ function addTmpTarball_ (tgz, data, shasum, cb) { if (er) return cb(er) var read = fs.createReadStream(tgz) - var write = fs.createWriteStream(target) + var write = writeStream(target, { mode: npm.modes.file }) var fin = cs.uid && cs.gid ? chown : done read.on("error", cb).pipe(write).on("error", cb).on("close", fin) }) diff --git a/lib/cache/add-remote-git.js b/lib/cache/add-remote-git.js index 304d2f3f0..97823daef 100644 --- a/lib/cache/add-remote-git.js +++ b/lib/cache/add-remote-git.js @@ -17,6 +17,7 @@ var mkdir = require("mkdirp") , unlock = locker.unlock , getCacheStat = require("./get-stat.js") , addLocalTarball = require("./add-local-tarball.js") + , writeStream = require('fs-write-stream-atomic') // 1. cacheDir = path.join(cache,'_git-remotes',sha1(u)) @@ -205,7 +206,7 @@ function archiveGitRemote (p, u, co, origUrl, cb) { if (er) return cb(er) var gzip = zlib.createGzip({ level: 9 }) var args = ["archive", co, "--format=tar", "--prefix=package/"] - var out = fs.createWriteStream(tmp) + var out = writeStream(tmp) var env = gitEnv() cb = once(cb) var cp = git.spawn(args, { env: env, cwd: p }) diff --git a/lib/pack.js b/lib/pack.js index f955cb71a..79421f72e 100644 --- a/lib/pack.js +++ b/lib/pack.js @@ -11,6 +11,7 @@ var npm = require("./npm.js") , chain = require("slide").chain , path = require("path") , cwd = process.cwd() + , writeStream = require('fs-write-stream-atomic') pack.usage = "npm pack <pkg>" @@ -53,7 +54,7 @@ function pack_ (pkg, cb) { , data.version , "package.tgz" ) , from = fs.createReadStream(cached) - , to = fs.createWriteStream(fname) + , to = writeStream(fname) , errState = null from.on("error", cb_) diff --git a/lib/utils/error-handler.js b/lib/utils/error-handler.js index 788d3f8cc..842927255 100644 --- a/lib/utils/error-handler.js +++ b/lib/utils/error-handler.js @@ -11,6 +11,7 @@ var cbCalled = false , exitCode = 0 , rollbacks = npm.rollbacks , chain = require("slide").chain + , writeStream = require('fs-write-stream-atomic') process.on("exit", function (code) { @@ -352,7 +353,7 @@ function writeLogFile (cb) { wroteLogFile = true var fs = require("graceful-fs") - , fstr = fs.createWriteStream("npm-debug.log") + , fstr = writeStream("npm-debug.log") , os = require("os") , out = "" |