123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100 |
- '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
- var invocations = 0
- function getTmpname (filename) {
- return filename + '.' +
- MurmurHash3(__filename)
- .hash(String(process.pid))
- .hash(String(++invocations))
- .result()
- }
- 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.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 (!options.mode) {
- options.mode = stats.mode
- }
- if (!options.chown && process.getuid) {
- options.chown = { uid: stats.uid, gid: stats.gid }
- }
- return thenWriteFile()
- })
- }
- function thenWriteFile () {
- chain([
- [fs, fs.writeFile, tmpfile, data, options.encoding || 'utf8'],
- options.mode && [fs, fs.chmod, tmpfile, options.mode],
- options.chown && [fs, fs.chown, tmpfile, options.chown.uid, options.chown.gid],
- [fs, fs.rename, tmpfile, filename]
- ], function (err) {
- err ? fs.unlink(tmpfile, function () { callback(err) })
- : callback()
- })
- }
- }
- 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.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
- }
- if (!options.chown && process.getuid) {
- options.chown = { uid: stats.uid, gid: stats.gid }
- }
- } catch (ex) {
- // ignore stat errors
- }
- }
- fs.writeFileSync(tmpfile, data, options.encoding || 'utf8')
- if (options.chown) fs.chownSync(tmpfile, options.chown.uid, options.chown.gid)
- if (options.mode) fs.chmodSync(tmpfile, options.mode)
- fs.renameSync(tmpfile, filename)
- } catch (err) {
- try { fs.unlinkSync(tmpfile) } catch (e) {}
- throw err
- }
- }
|