graceful-fs.js 3.0 KB

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