| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160 | // Monkey-patching the fs module.// It's ugly, but there is simply no other way to do this.var fs = module.exports = require('fs')var assert = require('assert')// fix up some busted stuff, mostly on windows and old nodesrequire('./polyfills.js')// The EMFILE enqueuing stuffvar util = require('util')function noop () {}var debug = noopif (util.debuglog)  debug = util.debuglog('gfs')else if (/\bgfs\b/i.test(process.env.NODE_DEBUG || ''))  debug = function() {    var m = util.format.apply(util, arguments)    m = 'GFS: ' + m.split(/\n/).join('\nGFS: ')    console.error(m)  }if (/\bgfs\b/i.test(process.env.NODE_DEBUG || '')) {  process.on('exit', function() {    debug('fds', fds)    debug(queue)    assert.equal(queue.length, 0)  })}var originalOpen = fs.openfs.open = openfunction open(path, flags, mode, cb) {  if (typeof mode === "function") cb = mode, mode = null  if (typeof cb !== "function") cb = noop  new OpenReq(path, flags, mode, cb)}function OpenReq(path, flags, mode, cb) {  this.path = path  this.flags = flags  this.mode = mode  this.cb = cb  Req.call(this)}util.inherits(OpenReq, Req)OpenReq.prototype.process = function() {  originalOpen.call(fs, this.path, this.flags, this.mode, this.done)}var fds = {}OpenReq.prototype.done = function(er, fd) {  debug('open done', er, fd)  if (fd)    fds['fd' + fd] = this.path  Req.prototype.done.call(this, er, fd)}var originalReaddir = fs.readdirfs.readdir = readdirfunction readdir(path, cb) {  if (typeof cb !== "function") cb = noop  new ReaddirReq(path, cb)}function ReaddirReq(path, cb) {  this.path = path  this.cb = cb  Req.call(this)}util.inherits(ReaddirReq, Req)ReaddirReq.prototype.process = function() {  originalReaddir.call(fs, this.path, this.done)}ReaddirReq.prototype.done = function(er, files) {  if (files && files.sort)    files = files.sort()  Req.prototype.done.call(this, er, files)  onclose()}var originalClose = fs.closefs.close = closefunction close (fd, cb) {  debug('close', fd)  if (typeof cb !== "function") cb = noop  delete fds['fd' + fd]  originalClose.call(fs, fd, function(er) {    onclose()    cb(er)  })}var originalCloseSync = fs.closeSyncfs.closeSync = closeSyncfunction closeSync (fd) {  try {    return originalCloseSync(fd)  } finally {    onclose()  }}// Req classfunction Req () {  // start processing  this.done = this.done.bind(this)  this.failures = 0  this.process()}Req.prototype.done = function (er, result) {  var tryAgain = false  if (er) {    var code = er.code    var tryAgain = code === "EMFILE"    if (process.platform === "win32")      tryAgain = tryAgain || code === "OK"  }  if (tryAgain) {    this.failures ++    enqueue(this)  } else {    var cb = this.cb    cb(er, result)  }}var queue = []function enqueue(req) {  queue.push(req)  debug('enqueue %d %s', queue.length, req.constructor.name, req)}function onclose() {  var req = queue.shift()  if (req) {    debug('process', req.constructor.name, req)    req.process()  }}
 |