graceful-fs.js 6.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262
  1. var fs = require('fs')
  2. var polyfills = require('./polyfills.js')
  3. var legacy = require('./legacy-streams.js')
  4. var queue = []
  5. var util = require('util')
  6. function noop () {}
  7. var debug = noop
  8. if (util.debuglog)
  9. debug = util.debuglog('gfs4')
  10. else if (/\bgfs4\b/i.test(process.env.NODE_DEBUG || ''))
  11. debug = function() {
  12. var m = util.format.apply(util, arguments)
  13. m = 'GFS4: ' + m.split(/\n/).join('\nGFS4: ')
  14. console.error(m)
  15. }
  16. if (/\bgfs4\b/i.test(process.env.NODE_DEBUG || '')) {
  17. process.on('exit', function() {
  18. debug(queue)
  19. require('assert').equal(queue.length, 0)
  20. })
  21. }
  22. module.exports = patch(require('./fs.js'))
  23. if (process.env.TEST_GRACEFUL_FS_GLOBAL_PATCH) {
  24. module.exports = patch(fs)
  25. }
  26. // Always patch fs.close/closeSync, because we want to
  27. // retry() whenever a close happens *anywhere* in the program.
  28. // This is essential when multiple graceful-fs instances are
  29. // in play at the same time.
  30. module.exports.close =
  31. fs.close = (function (fs$close) { return function (fd, cb) {
  32. return fs$close.call(fs, fd, function (err) {
  33. if (!err)
  34. retry()
  35. if (typeof cb === 'function')
  36. cb.apply(this, arguments)
  37. })
  38. }})(fs.close)
  39. module.exports.closeSync =
  40. fs.closeSync = (function (fs$closeSync) { return function (fd) {
  41. // Note that graceful-fs also retries when fs.closeSync() fails.
  42. // Looks like a bug to me, although it's probably a harmless one.
  43. var rval = fs$closeSync.apply(fs, arguments)
  44. retry()
  45. return rval
  46. }})(fs.closeSync)
  47. function patch (fs) {
  48. // Everything that references the open() function needs to be in here
  49. polyfills(fs)
  50. fs.gracefulify = patch
  51. fs.FileReadStream = ReadStream; // Legacy name.
  52. fs.FileWriteStream = WriteStream; // Legacy name.
  53. fs.createReadStream = createReadStream
  54. fs.createWriteStream = createWriteStream
  55. var fs$readFile = fs.readFile
  56. fs.readFile = readFile
  57. function readFile (path, options, cb) {
  58. if (typeof options === 'function')
  59. cb = options, options = null
  60. return go$readFile(path, options, cb)
  61. function go$readFile (path, options, cb) {
  62. return fs$readFile(path, options, function (err) {
  63. if (err && (err.code === 'EMFILE' || err.code === 'ENFILE'))
  64. enqueue([go$readFile, [path, options, cb]])
  65. else {
  66. if (typeof cb === 'function')
  67. cb.apply(this, arguments)
  68. retry()
  69. }
  70. })
  71. }
  72. }
  73. var fs$writeFile = fs.writeFile
  74. fs.writeFile = writeFile
  75. function writeFile (path, data, options, cb) {
  76. if (typeof options === 'function')
  77. cb = options, options = null
  78. return go$writeFile(path, data, options, cb)
  79. function go$writeFile (path, data, options, cb) {
  80. return fs$writeFile(path, data, options, function (err) {
  81. if (err && (err.code === 'EMFILE' || err.code === 'ENFILE'))
  82. enqueue([go$writeFile, [path, data, options, cb]])
  83. else {
  84. if (typeof cb === 'function')
  85. cb.apply(this, arguments)
  86. retry()
  87. }
  88. })
  89. }
  90. }
  91. var fs$appendFile = fs.appendFile
  92. if (fs$appendFile)
  93. fs.appendFile = appendFile
  94. function appendFile (path, data, options, cb) {
  95. if (typeof options === 'function')
  96. cb = options, options = null
  97. return go$appendFile(path, data, options, cb)
  98. function go$appendFile (path, data, options, cb) {
  99. return fs$appendFile(path, data, options, function (err) {
  100. if (err && (err.code === 'EMFILE' || err.code === 'ENFILE'))
  101. enqueue([go$appendFile, [path, data, options, cb]])
  102. else {
  103. if (typeof cb === 'function')
  104. cb.apply(this, arguments)
  105. retry()
  106. }
  107. })
  108. }
  109. }
  110. var fs$readdir = fs.readdir
  111. fs.readdir = readdir
  112. function readdir (path, options, cb) {
  113. var args = [path]
  114. if (typeof options !== 'function') {
  115. args.push(options)
  116. } else {
  117. cb = options
  118. }
  119. args.push(go$readdir$cb)
  120. return go$readdir(args)
  121. function go$readdir$cb (err, files) {
  122. if (files && files.sort)
  123. files.sort()
  124. if (err && (err.code === 'EMFILE' || err.code === 'ENFILE'))
  125. enqueue([go$readdir, [args]])
  126. else {
  127. if (typeof cb === 'function')
  128. cb.apply(this, arguments)
  129. retry()
  130. }
  131. }
  132. }
  133. function go$readdir (args) {
  134. return fs$readdir.apply(fs, args)
  135. }
  136. if (process.version.substr(0, 4) === 'v0.8') {
  137. var legStreams = legacy(fs)
  138. ReadStream = legStreams.ReadStream
  139. WriteStream = legStreams.WriteStream
  140. }
  141. var fs$ReadStream = fs.ReadStream
  142. ReadStream.prototype = Object.create(fs$ReadStream.prototype)
  143. ReadStream.prototype.open = ReadStream$open
  144. var fs$WriteStream = fs.WriteStream
  145. WriteStream.prototype = Object.create(fs$WriteStream.prototype)
  146. WriteStream.prototype.open = WriteStream$open
  147. fs.ReadStream = ReadStream
  148. fs.WriteStream = WriteStream
  149. function ReadStream (path, options) {
  150. if (this instanceof ReadStream)
  151. return fs$ReadStream.apply(this, arguments), this
  152. else
  153. return ReadStream.apply(Object.create(ReadStream.prototype), arguments)
  154. }
  155. function ReadStream$open () {
  156. var that = this
  157. open(that.path, that.flags, that.mode, function (err, fd) {
  158. if (err) {
  159. if (that.autoClose)
  160. that.destroy()
  161. that.emit('error', err)
  162. } else {
  163. that.fd = fd
  164. that.emit('open', fd)
  165. that.read()
  166. }
  167. })
  168. }
  169. function WriteStream (path, options) {
  170. if (this instanceof WriteStream)
  171. return fs$WriteStream.apply(this, arguments), this
  172. else
  173. return WriteStream.apply(Object.create(WriteStream.prototype), arguments)
  174. }
  175. function WriteStream$open () {
  176. var that = this
  177. open(that.path, that.flags, that.mode, function (err, fd) {
  178. if (err) {
  179. that.destroy()
  180. that.emit('error', err)
  181. } else {
  182. that.fd = fd
  183. that.emit('open', fd)
  184. }
  185. })
  186. }
  187. function createReadStream (path, options) {
  188. return new ReadStream(path, options)
  189. }
  190. function createWriteStream (path, options) {
  191. return new WriteStream(path, options)
  192. }
  193. var fs$open = fs.open
  194. fs.open = open
  195. function open (path, flags, mode, cb) {
  196. if (typeof mode === 'function')
  197. cb = mode, mode = null
  198. return go$open(path, flags, mode, cb)
  199. function go$open (path, flags, mode, cb) {
  200. return fs$open(path, flags, mode, function (err, fd) {
  201. if (err && (err.code === 'EMFILE' || err.code === 'ENFILE'))
  202. enqueue([go$open, [path, flags, mode, cb]])
  203. else {
  204. if (typeof cb === 'function')
  205. cb.apply(this, arguments)
  206. retry()
  207. }
  208. })
  209. }
  210. }
  211. return fs
  212. }
  213. function enqueue (elem) {
  214. debug('ENQUEUE', elem[0].name, elem[1])
  215. queue.push(elem)
  216. }
  217. function retry () {
  218. var elem = queue.shift()
  219. if (elem) {
  220. debug('RETRY', elem[0].name, elem[1])
  221. elem[0].apply(null, elem[1])
  222. }
  223. }