diff options
author | Filip Weiss <me@fiws.net> | 2014-09-12 14:43:19 +0400 |
---|---|---|
committer | Forrest L Norvell <forrest@npmjs.com> | 2014-09-19 09:42:28 +0400 |
commit | 17c941a2d583210fe97ed47e2968d94ce9f774ba (patch) | |
tree | b4ec50ca5d84cf93f688d0c10a9cfdeba14ffe1c /lib | |
parent | f2d2190aa365d22378d03afab0da13f95614a583 (diff) |
use writeFileAtomic instead of fs.writeFile fixes #6163
Diffstat (limited to 'lib')
-rw-r--r-- | lib/cache.js | 18 | ||||
-rw-r--r-- | lib/cache/add-local-tarball.js | 3 | ||||
-rw-r--r-- | lib/config.js | 4 | ||||
-rw-r--r-- | lib/install.js | 3 | ||||
-rw-r--r-- | lib/shrinkwrap.js | 3 | ||||
-rw-r--r-- | lib/uninstall.js | 3 | ||||
-rw-r--r-- | lib/utils/tar.js | 3 | ||||
-rw-r--r-- | lib/version.js | 3 |
8 files changed, 17 insertions, 23 deletions
diff --git a/lib/cache.js b/lib/cache.js index 281d6100a..50324f9d5 100644 --- a/lib/cache.js +++ b/lib/cache.js @@ -62,6 +62,7 @@ cache.read = read var npm = require("./npm.js") , fs = require("graceful-fs") + , writeFileAtomic = require("write-file-atomic") , assert = require("assert") , rm = require("./utils/gently-rm.js") , readJson = require("read-package-json") @@ -346,26 +347,13 @@ function afterAdd (cb) { return function (er, data) { var name = data.name var ver = data.version var pj = path.join(npm.cache, name, ver, "package", "package.json") - var tmp = pj + "." + process.pid var done = inflight(pj, cb) if (!done) return undefined - fs.writeFile(tmp, JSON.stringify(data), "utf8", function (er) { - if (er) return done(er) - getStat(function (er, cs) { - if (er) return done(er) - fs.rename(tmp, pj, function (er) { - if (cs.uid && cs.gid) { - fs.chown(pj, cs.uid, cs.gid, function (er) { - return done(er, data) - }) - } else { - done(er, data) - } - }) - }) + writeFileAtomic(pj, JSON.stringify(data), function (er) { + return done(er, data) }) }} diff --git a/lib/cache/add-local-tarball.js b/lib/cache/add-local-tarball.js index f7cd76103..ad8563800 100644 --- a/lib/cache/add-local-tarball.js +++ b/lib/cache/add-local-tarball.js @@ -1,6 +1,7 @@ var mkdir = require("mkdirp") , assert = require("assert") , fs = require("graceful-fs") + , writeFileAtomic = require("write-file-atomic") , readJson = require("read-package-json") , log = require("npmlog") , path = require("path") @@ -118,7 +119,7 @@ function addPlacedTarball_ (p, pkgData, uid, gid, resolvedSum, cb) { if (er) return cb(er) var pj = path.join(folder, "package.json") var json = JSON.stringify(pkgData, null, 2) - fs.writeFile(pj, json, "utf8", function (er) { + writeFileAtomic(pj, json, function (er) { cb(er, pkgData) }) }) diff --git a/lib/config.js b/lib/config.js index 8dc814acd..6a45e8a02 100644 --- a/lib/config.js +++ b/lib/config.js @@ -12,6 +12,7 @@ config.usage = "npm config set <key> <value>" var log = require("npmlog") , npm = require("./npm.js") , fs = require("graceful-fs") + , writeFileAtomic = require("write-file-atomic") , npmconf = require("npmconf") , types = npmconf.defs.types , ini = require("ini") @@ -95,10 +96,9 @@ function edit (cb) { }, [])) .concat([""]) .join(os.EOL) - fs.writeFile + writeFileAtomic ( f , data - , "utf8" , function (er) { if (er) return cb(er) editor(f, { editor: e }, cb) diff --git a/lib/install.js b/lib/install.js index 68d85041f..ad542071d 100644 --- a/lib/install.js +++ b/lib/install.js @@ -73,6 +73,7 @@ var npm = require("./npm.js") , log = require("npmlog") , path = require("path") , fs = require("graceful-fs") + , writeFileAtomic = require("write-file-atomic") , cache = require("./cache.js") , asyncMap = require("slide").asyncMap , chain = require("slide").chain @@ -420,7 +421,7 @@ function save (where, installed, tree, pretty, hasArguments, cb) { data[deps] = sortedObject(data[deps]) data = JSON.stringify(data, null, 2) + "\n" - fs.writeFile(saveTarget, data, function (er) { + writeFileAtomic(saveTarget, data, function (er) { cb(er, installed, tree, pretty) }) }) diff --git a/lib/shrinkwrap.js b/lib/shrinkwrap.js index 5f8261d09..a5783837c 100644 --- a/lib/shrinkwrap.js +++ b/lib/shrinkwrap.js @@ -6,6 +6,7 @@ module.exports = exports = shrinkwrap var npm = require("./npm.js") , log = require("npmlog") , fs = require("fs") + , writeFileAtomic = require("write-file-atomic") , path = require("path") , readJson = require("read-package-json") , sortedObject = require("sorted-object") @@ -70,7 +71,7 @@ function save (pkginfo, silent, cb) { var file = path.resolve(npm.prefix, "npm-shrinkwrap.json") - fs.writeFile(file, swdata, function (er) { + writeFileAtomic(file, swdata, function (er) { if (er) return cb(er) if (silent) return cb(null, pkginfo) console.log("wrote npm-shrinkwrap.json") diff --git a/lib/uninstall.js b/lib/uninstall.js index 42a9a83e0..68869f579 100644 --- a/lib/uninstall.js +++ b/lib/uninstall.js @@ -9,6 +9,7 @@ uninstall.usage = "npm uninstall <name>[@<version> [<name>[@<version>] ...]" uninstall.completion = require("./utils/completion/installed-shallow.js") var fs = require("graceful-fs") + , writeFileAtomic = require("write-file-atomic") , log = require("npmlog") , readJson = require("read-package-json") , path = require("path") @@ -120,7 +121,7 @@ function saver (args, nm, cb_) { } } - fs.writeFile(pj, JSON.stringify(pkg, null, 2) + "\n", function (er) { + writeFileAtomic(pj, JSON.stringify(pkg, null, 2) + "\n", function (er) { return cb_(er, data) }) }) diff --git a/lib/utils/tar.js b/lib/utils/tar.js index 192de7a26..55a87273a 100644 --- a/lib/utils/tar.js +++ b/lib/utils/tar.js @@ -3,6 +3,7 @@ var npm = require("../npm.js") , fs = require("graceful-fs") + , writeFileAtomic = require("write-file-atomic") , path = require("path") , log = require("npmlog") , uidNumber = require("uid-number") @@ -335,7 +336,7 @@ function gunzTarPerm (tarball, target, dMode, fMode, uid, gid, cb_) { log.error("not a package", tarball) return cb(er) } - fs.writeFile(j, JSON.stringify(d) + "\n", cb) + writeFileAtomic(j, JSON.stringify(d) + "\n", cb) }) }) } diff --git a/lib/version.js b/lib/version.js index 5091ab9e2..e23b6855e 100644 --- a/lib/version.js +++ b/lib/version.js @@ -6,6 +6,7 @@ var exec = require("child_process").execFile , semver = require("semver") , path = require("path") , fs = require("graceful-fs") + , writeFileAtomic = require("write-file-atomic") , chain = require("slide").chain , log = require("npmlog") , which = require("which") @@ -111,7 +112,7 @@ function checkGit (data, cb) { } function write (data, cb) { - fs.writeFile( path.join(npm.localPrefix, "package.json") + writeFileAtomic( path.join(npm.localPrefix, "package.json") , new Buffer(JSON.stringify(data, null, 2) + "\n") , cb ) } |