index.js 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129
  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. [writeFileAsync, tmpfile, data, options.mode, options.encoding || 'utf8'],
  49. options.chown && [fs, fs.chown, tmpfile, options.chown.uid, options.chown.gid],
  50. options.mode && [fs, fs.chmod, tmpfile, options.mode],
  51. [fs, fs.rename, tmpfile, filename]
  52. ], function (err) {
  53. err ? fs.unlink(tmpfile, function () { callback(err) })
  54. : callback()
  55. })
  56. }
  57. // doing this instead of `fs.writeFile` in order to get the ability to
  58. // call `fsync`.
  59. function writeFileAsync (file, data, mode, encoding, cb) {
  60. fs.open(file, 'w', options.mode, function (err, fd) {
  61. if (err) return cb(err)
  62. if (Buffer.isBuffer(data)) {
  63. return fs.write(fd, data, 0, data.length, 0, syncAndClose)
  64. } else if (data != null) {
  65. return fs.write(fd, String(data), 0, String(encoding), syncAndClose)
  66. } else {
  67. return syncAndClose()
  68. }
  69. function syncAndClose (err) {
  70. if (err) return cb(err)
  71. fs.fsync(fd, function (err) {
  72. if (err) return cb(err)
  73. fs.close(fd, cb)
  74. })
  75. }
  76. })
  77. }
  78. }
  79. function writeFileSync (filename, data, options) {
  80. if (!options) options = {}
  81. try {
  82. filename = fs.realpathSync(filename)
  83. } catch (ex) {
  84. // it's ok, it'll happen on a not yet existing file
  85. }
  86. var tmpfile = getTmpname(filename)
  87. try {
  88. if (!options.mode || !options.chown) {
  89. // Either mode or chown is not explicitly set
  90. // Default behavior is to copy it from original file
  91. try {
  92. var stats = fs.statSync(filename)
  93. options = extend({}, options)
  94. if (!options.mode) {
  95. options.mode = stats.mode
  96. }
  97. if (!options.chown && process.getuid) {
  98. options.chown = { uid: stats.uid, gid: stats.gid }
  99. }
  100. } catch (ex) {
  101. // ignore stat errors
  102. }
  103. }
  104. var fd = fs.openSync(tmpfile, 'w', options.mode)
  105. if (Buffer.isBuffer(data)) {
  106. fs.writeSync(fd, data, 0, data.length, 0)
  107. } else if (data != null) {
  108. fs.writeSync(fd, String(data), 0, String(options.encoding || 'utf8'))
  109. }
  110. fs.fsyncSync(fd)
  111. fs.closeSync(fd)
  112. if (options.chown) fs.chownSync(tmpfile, options.chown.uid, options.chown.gid)
  113. if (options.mode) fs.chmodSync(tmpfile, options.mode)
  114. fs.renameSync(tmpfile, filename)
  115. } catch (err) {
  116. try { fs.unlinkSync(tmpfile) } catch (e) {}
  117. throw err
  118. }
  119. }