index.js 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100
  1. 'use strict'
  2. module.exports = writeFile
  3. module.exports.sync = writeFileSync
  4. module.exports._getTmpname = getTmpname // for testing
  5. var fs = require('graceful-fs')
  6. var chain = require('slide').chain
  7. var MurmurHash3 = require('imurmurhash')
  8. var extend = Object.assign || require('util')._extend
  9. var invocations = 0
  10. function getTmpname (filename) {
  11. return filename + '.' +
  12. MurmurHash3(__filename)
  13. .hash(String(process.pid))
  14. .hash(String(++invocations))
  15. .result()
  16. }
  17. function writeFile (filename, data, options, callback) {
  18. if (options instanceof Function) {
  19. callback = options
  20. options = null
  21. }
  22. if (!options) options = {}
  23. fs.realpath(filename, function (_, realname) {
  24. _writeFile(realname || filename, data, options, callback)
  25. })
  26. }
  27. function _writeFile (filename, data, options, callback) {
  28. var tmpfile = getTmpname(filename)
  29. if (options.mode && options.chown) {
  30. return thenWriteFile()
  31. } else {
  32. // Either mode or chown is not explicitly set
  33. // Default behavior is to copy it from original file
  34. return fs.stat(filename, function (err, stats) {
  35. if (err || !stats) return thenWriteFile()
  36. options = extend({}, options)
  37. if (!options.mode) {
  38. options.mode = stats.mode
  39. }
  40. if (!options.chown && process.getuid) {
  41. options.chown = { uid: stats.uid, gid: stats.gid }
  42. }
  43. return thenWriteFile()
  44. })
  45. }
  46. function thenWriteFile () {
  47. chain([
  48. [fs, fs.writeFile, tmpfile, data, options.encoding || 'utf8'],
  49. options.mode && [fs, fs.chmod, tmpfile, options.mode],
  50. options.chown && [fs, fs.chown, tmpfile, options.chown.uid, options.chown.gid],
  51. [fs, fs.rename, tmpfile, filename]
  52. ], function (err) {
  53. err ? fs.unlink(tmpfile, function () { callback(err) })
  54. : callback()
  55. })
  56. }
  57. }
  58. function writeFileSync (filename, data, options) {
  59. if (!options) options = {}
  60. try {
  61. filename = fs.realpathSync(filename)
  62. } catch (ex) {
  63. // it's ok, it'll happen on a not yet existing file
  64. }
  65. var tmpfile = getTmpname(filename)
  66. try {
  67. if (!options.mode || !options.chown) {
  68. // Either mode or chown is not explicitly set
  69. // Default behavior is to copy it from original file
  70. try {
  71. var stats = fs.statSync(filename)
  72. options = extend({}, options)
  73. if (!options.mode) {
  74. options.mode = stats.mode
  75. }
  76. if (!options.chown && process.getuid) {
  77. options.chown = { uid: stats.uid, gid: stats.gid }
  78. }
  79. } catch (ex) {
  80. // ignore stat errors
  81. }
  82. }
  83. fs.writeFileSync(tmpfile, data, options.encoding || 'utf8')
  84. if (options.chown) fs.chownSync(tmpfile, options.chown.uid, options.chown.gid)
  85. if (options.mode) fs.chmodSync(tmpfile, options.mode)
  86. fs.renameSync(tmpfile, filename)
  87. } catch (err) {
  88. try { fs.unlinkSync(tmpfile) } catch (e) {}
  89. throw err
  90. }
  91. }