node/deps/npm/node_modules/graceful-fs/graceful-fs.js

160 lines
3.0 KiB
JavaScript
Raw Normal View History

2013-07-12 23:55:57 +08:00
// Monkey-patching the fs module.
// It's ugly, but there is simply no other way to do this.
var fs = module.exports = require('fs')
2011-12-14 10:53:02 +08:00
2013-07-12 23:55:57 +08:00
var assert = require('assert')
2011-12-14 10:53:02 +08:00
2013-07-12 23:55:57 +08:00
// fix up some busted stuff, mostly on windows and old nodes
require('./polyfills.js')
2011-12-14 10:53:02 +08:00
2013-07-12 23:55:57 +08:00
// The EMFILE enqueuing stuff
2011-12-14 10:53:02 +08:00
2013-07-12 23:55:57 +08:00
var util = require('util')
2011-11-23 08:56:59 +08:00
function noop () {}
2013-07-12 23:55:57 +08:00
var debug = noop
var util = require('util')
if (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)
2011-11-23 08:56:59 +08:00
}
2013-07-12 23:55:57 +08:00
if (/\bgfs\b/i.test(process.env.NODE_DEBUG || '')) {
process.on('exit', function() {
debug('fds', fds)
debug(queue)
assert.equal(queue.length, 0)
2011-11-23 08:56:59 +08:00
})
}
2013-07-12 23:55:57 +08:00
var originalOpen = fs.open
fs.open = open
2011-11-23 08:56:59 +08:00
2013-07-12 23:55:57 +08:00
function 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)
2011-11-23 08:56:59 +08:00
}
2013-07-12 23:55:57 +08:00
function OpenReq(path, flags, mode, cb) {
this.path = path
this.flags = flags
this.mode = mode
this.cb = cb
Req.call(this)
2011-11-23 08:56:59 +08:00
}
2013-07-12 23:55:57 +08:00
util.inherits(OpenReq, Req)
2013-06-13 01:59:44 +08:00
2013-07-12 23:55:57 +08:00
OpenReq.prototype.process = function() {
originalOpen.call(fs, this.path, this.flags, this.mode, this.done)
}
2013-06-13 01:59:44 +08:00
2013-07-12 23:55:57 +08:00
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)
}
2013-06-13 01:59:44 +08:00
2013-07-12 23:55:57 +08:00
var originalReaddir = fs.readdir
fs.readdir = readdir
2013-06-13 01:59:44 +08:00
2013-07-12 23:55:57 +08:00
function readdir(path, cb) {
if (typeof cb !== "function") cb = noop
new ReaddirReq(path, cb)
2013-06-13 01:59:44 +08:00
}
2013-07-12 23:55:57 +08:00
function ReaddirReq(path, cb) {
2013-06-13 01:59:44 +08:00
this.path = path
this.cb = cb
2013-07-12 23:55:57 +08:00
Req.call(this)
2011-11-23 08:56:59 +08:00
}
2013-07-12 23:55:57 +08:00
util.inherits(ReaddirReq, Req)
2013-07-12 23:55:57 +08:00
ReaddirReq.prototype.process = function() {
originalReaddir.call(fs, this.path, this.done)
2011-11-23 08:56:59 +08:00
}
2013-07-12 23:55:57 +08:00
ReaddirReq.prototype.done = function(er, files) {
Req.prototype.done.call(this, er, files)
onclose()
}
2013-07-12 23:55:57 +08:00
var originalClose = fs.close
fs.close = close
2013-07-12 23:55:57 +08:00
function 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)
})
}
2013-07-12 23:55:57 +08:00
var originalCloseSync = fs.closeSync
fs.closeSync = closeSync
2013-07-12 23:55:57 +08:00
function closeSync (fd) {
try {
return originalCloseSync(fd)
} finally {
onclose()
2012-07-14 02:40:38 +08:00
}
}
2013-07-12 23:55:57 +08:00
// Req class
function Req () {
// start processing
this.done = this.done.bind(this)
this.failures = 0
this.process()
}
2012-07-14 02:40:38 +08:00
2013-07-12 23:55:57 +08:00
Req.prototype.done = function (er, result) {
2013-09-08 03:31:04 +08:00
var tryAgain = false
if (er) {
var code = er.code
var tryAgain = code === "EMFILE"
if (process.platform === "win32")
tryAgain = tryAgain || code === "OK"
}
if (tryAgain) {
2013-07-12 23:55:57 +08:00
this.failures ++
enqueue(this)
} else {
var cb = this.cb
cb(er, result)
}
}
2012-09-25 23:28:55 +08:00
2013-07-12 23:55:57 +08:00
var queue = []
2012-09-25 23:28:55 +08:00
2013-07-12 23:55:57 +08:00
function enqueue(req) {
queue.push(req)
debug('enqueue %d %s', queue.length, req.constructor.name, req)
2012-09-25 23:28:55 +08:00
}
2013-07-12 23:55:57 +08:00
function onclose() {
var req = queue.shift()
if (req) {
debug('process', req.constructor.name, req)
req.process()
2012-09-25 23:28:55 +08:00
}
}