diff options
author | Kat Marchán <kzm@sykosomatic.org> | 2017-01-26 17:21:26 +0100 |
---|---|---|
committer | Jeremiah Senkpiel <fishrock123@rocketmail.com> | 2017-01-30 14:47:24 -0500 |
commit | afb7c1bac87d943a9ca6422c7100e0e3fe291740 (patch) | |
tree | 1ce1f8214473a200de9de52989c1ec8aad8ddf89 /deps/npm/node_modules/write-file-atomic/index.js | |
parent | 5de3cf099cd01c84d1809dab90c041b76aa58d8e (diff) | |
download | node-new-afb7c1bac87d943a9ca6422c7100e0e3fe291740.tar.gz |
deps: upgrade npm to 4.1.2
PR-URL: https://github.com/nodejs/node/pull/11020
Reviewed-By: Jeremiah Senkpiel <fishrock123@rocketmail.com>
Diffstat (limited to 'deps/npm/node_modules/write-file-atomic/index.js')
-rw-r--r-- | deps/npm/node_modules/write-file-atomic/index.js | 42 |
1 files changed, 28 insertions, 14 deletions
diff --git a/deps/npm/node_modules/write-file-atomic/index.js b/deps/npm/node_modules/write-file-atomic/index.js index 519aacad98..7939828e53 100644 --- a/deps/npm/node_modules/write-file-atomic/index.js +++ b/deps/npm/node_modules/write-file-atomic/index.js @@ -1,38 +1,48 @@ 'use strict' +module.exports = writeFile +module.exports.sync = writeFileSync +module.exports._getTmpname = getTmpname // for testing + var fs = require('graceful-fs') var chain = require('slide').chain var MurmurHash3 = require('imurmurhash') var extend = Object.assign || require('util')._extend -function murmurhex () { - var hash = new MurmurHash3() - for (var ii = 0; ii < arguments.length; ++ii) hash.hash('' + arguments[ii]) - return hash.result() -} var invocations = 0 -var getTmpname = function (filename) { - return filename + '.' + murmurhex(__filename, process.pid, ++invocations) +function getTmpname (filename) { + return filename + '.' + + MurmurHash3(__filename) + .hash(String(process.pid)) + .hash(String(++invocations)) + .result() } -module.exports = function writeFile (filename, data, options, callback) { +function writeFile (filename, data, options, callback) { if (options instanceof Function) { callback = options options = null } if (!options) options = {} + fs.realpath(filename, function (_, realname) { + _writeFile(realname || filename, data, options, callback) + }) +} +function _writeFile (filename, data, options, callback) { var tmpfile = getTmpname(filename) - if (options.mode && options.chmod) { + if (options.mode && options.chown) { return thenWriteFile() } else { // Either mode or chown is not explicitly set // Default behavior is to copy it from original file return fs.stat(filename, function (err, stats) { + if (err || !stats) return thenWriteFile() + options = extend({}, options) - if (!err && stats && !options.mode) { + if (!options.mode) { options.mode = stats.mode } - if (!err && stats && !options.chown && process.getuid) { + if (!options.chown && process.getuid) { options.chown = { uid: stats.uid, gid: stats.gid } } return thenWriteFile() @@ -52,17 +62,21 @@ module.exports = function writeFile (filename, data, options, callback) { } } -module.exports.sync = function writeFileSync (filename, data, options) { +function writeFileSync (filename, data, options) { if (!options) options = {} + try { + filename = fs.realpathSync(filename) + } catch (ex) { + // it's ok, it'll happen on a not yet existing file + } var tmpfile = getTmpname(filename) try { - if (!options.mode || !options.chmod) { + if (!options.mode || !options.chown) { // Either mode or chown is not explicitly set // Default behavior is to copy it from original file try { var stats = fs.statSync(filename) - options = extend({}, options) if (!options.mode) { options.mode = stats.mode |