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')
5 var assert = require('assert')
7 // fix up some busted stuff, mostly on windows and old nodes
8 require('./polyfills.js')
10 var util = require('util')
16 debug = util.debuglog('gfs')
17 else if (/\bgfs\b/i.test(process.env.NODE_DEBUG || ''))
19 var m = util.format.apply(util, arguments)
20 m = 'GFS: ' + m.split(/\n/).join('\nGFS: ')
24 if (/\bgfs\b/i.test(process.env.NODE_DEBUG || '')) {
25 process.on('exit', function() {
28 assert.equal(queue.length, 0)
33 var originalOpen = fs.open
36 function open(path, flags, mode, cb) {
37 if (typeof mode === "function") cb = mode, mode = null
38 if (typeof cb !== "function") cb = noop
39 new OpenReq(path, flags, mode, cb)
42 function OpenReq(path, flags, mode, cb) {
50 util.inherits(OpenReq, Req)
52 OpenReq.prototype.process = function() {
53 originalOpen.call(fs, this.path, this.flags, this.mode, this.done)
57 OpenReq.prototype.done = function(er, fd) {
58 debug('open done', er, fd)
60 fds['fd' + fd] = this.path
61 Req.prototype.done.call(this, er, fd)
65 var originalReaddir = fs.readdir
68 function readdir(path, cb) {
69 if (typeof cb !== "function") cb = noop
70 new ReaddirReq(path, cb)
73 function ReaddirReq(path, cb) {
79 util.inherits(ReaddirReq, Req)
81 ReaddirReq.prototype.process = function() {
82 originalReaddir.call(fs, this.path, this.done)
85 ReaddirReq.prototype.done = function(er, files) {
86 if (files && files.sort)
88 Req.prototype.done.call(this, er, files)
93 var originalClose = fs.close
96 function close (fd, cb) {
98 if (typeof cb !== "function") cb = noop
100 originalClose.call(fs, fd, function(er) {
107 var originalCloseSync = fs.closeSync
108 fs.closeSync = closeSync
110 function closeSync (fd) {
112 return originalCloseSync(fd)
122 this.done = this.done.bind(this)
127 Req.prototype.done = function (er, result) {
131 var tryAgain = code === "EMFILE" || code === "ENFILE"
132 if (process.platform === "win32")
133 tryAgain = tryAgain || code === "OK"
147 function enqueue(req) {
149 debug('enqueue %d %s', queue.length, req.constructor.name, req)
153 var req = queue.shift()
155 debug('process', req.constructor.name, req)