graceful-fs.js 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160
  1. // Monkey-patching the fs module.
  2. // It's ugly, but there is simply no other way to do this.
  3. var fs = module.exports = require('fs')
  4. var assert = require('assert')
  5. // fix up some busted stuff, mostly on windows and old nodes
  6. require('./polyfills.js')
  7. // The EMFILE enqueuing stuff
  8. var util = require('util')
  9. function noop () {}
  10. var debug = noop
  11. if (util.debuglog)
  12. debug = util.debuglog('gfs')
  13. else if (/\bgfs\b/i.test(process.env.NODE_DEBUG || ''))
  14. debug = function() {
  15. var m = util.format.apply(util, arguments)
  16. m = 'GFS: ' + m.split(/\n/).join('\nGFS: ')
  17. console.error(m)
  18. }
  19. if (/\bgfs\b/i.test(process.env.NODE_DEBUG || '')) {
  20. process.on('exit', function() {
  21. debug('fds', fds)
  22. debug(queue)
  23. assert.equal(queue.length, 0)
  24. })
  25. }
  26. var originalOpen = fs.open
  27. fs.open = open
  28. function open(path, flags, mode, cb) {
  29. if (typeof mode === "function") cb = mode, mode = null
  30. if (typeof cb !== "function") cb = noop
  31. new OpenReq(path, flags, mode, cb)
  32. }
  33. function OpenReq(path, flags, mode, cb) {
  34. this.path = path
  35. this.flags = flags
  36. this.mode = mode
  37. this.cb = cb
  38. Req.call(this)
  39. }
  40. util.inherits(OpenReq, Req)
  41. OpenReq.prototype.process = function() {
  42. originalOpen.call(fs, this.path, this.flags, this.mode, this.done)
  43. }
  44. var fds = {}
  45. OpenReq.prototype.done = function(er, fd) {
  46. debug('open done', er, fd)
  47. if (fd)
  48. fds['fd' + fd] = this.path
  49. Req.prototype.done.call(this, er, fd)
  50. }
  51. var originalReaddir = fs.readdir
  52. fs.readdir = readdir
  53. function readdir(path, cb) {
  54. if (typeof cb !== "function") cb = noop
  55. new ReaddirReq(path, cb)
  56. }
  57. function ReaddirReq(path, cb) {
  58. this.path = path
  59. this.cb = cb
  60. Req.call(this)
  61. }
  62. util.inherits(ReaddirReq, Req)
  63. ReaddirReq.prototype.process = function() {
  64. originalReaddir.call(fs, this.path, this.done)
  65. }
  66. ReaddirReq.prototype.done = function(er, files) {
  67. if (files && files.sort)
  68. files = files.sort()
  69. Req.prototype.done.call(this, er, files)
  70. onclose()
  71. }
  72. var originalClose = fs.close
  73. fs.close = close
  74. function close (fd, cb) {
  75. debug('close', fd)
  76. if (typeof cb !== "function") cb = noop
  77. delete fds['fd' + fd]
  78. originalClose.call(fs, fd, function(er) {
  79. onclose()
  80. cb(er)
  81. })
  82. }
  83. var originalCloseSync = fs.closeSync
  84. fs.closeSync = closeSync
  85. function closeSync (fd) {
  86. try {
  87. return originalCloseSync(fd)
  88. } finally {
  89. onclose()
  90. }
  91. }
  92. // Req class
  93. function Req () {
  94. // start processing
  95. this.done = this.done.bind(this)
  96. this.failures = 0
  97. this.process()
  98. }
  99. Req.prototype.done = function (er, result) {
  100. var tryAgain = false
  101. if (er) {
  102. var code = er.code
  103. var tryAgain = code === "EMFILE"
  104. if (process.platform === "win32")
  105. tryAgain = tryAgain || code === "OK"
  106. }
  107. if (tryAgain) {
  108. this.failures ++
  109. enqueue(this)
  110. } else {
  111. var cb = this.cb
  112. cb(er, result)
  113. }
  114. }
  115. var queue = []
  116. function enqueue(req) {
  117. queue.push(req)
  118. debug('enqueue %d %s', queue.length, req.constructor.name, req)
  119. }
  120. function onclose() {
  121. var req = queue.shift()
  122. if (req) {
  123. debug('process', req.constructor.name, req)
  124. req.process()
  125. }
  126. }