graceful-fs.js 7.4 KB

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