summaryrefslogtreecommitdiff
path: root/deps/npm/node_modules/write-file-atomic/index.js
diff options
context:
space:
mode:
Diffstat (limited to 'deps/npm/node_modules/write-file-atomic/index.js')
-rw-r--r--deps/npm/node_modules/write-file-atomic/index.js42
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