2 var polyfills = require('./polyfills.js')
3 var legacy = require('./legacy-streams.js')
6 var util = require('util')
12 debug = util.debuglog('gfs4')
13 else if (/\bgfs4\b/i.test(process.env.NODE_DEBUG || ''))
15 var m = util.format.apply(util, arguments)
16 m = 'GFS4: ' + m.split(/\n/).join('\nGFS4: ')
20 if (/\bgfs4\b/i.test(process.env.NODE_DEBUG || '')) {
21 process.on('exit', function() {
23 require('assert').equal(queue.length, 0)
27 module.exports = patch(require('./fs.js'))
28 if (process.env.TEST_GRACEFUL_FS_GLOBAL_PATCH) {
29 module.exports = patch(fs)
32 // Always patch fs.close/closeSync, because we want to
33 // retry() whenever a close happens *anywhere* in the program.
34 // This is essential when multiple graceful-fs instances are
35 // in play at the same time.
36 module.exports.close =
37 fs.close = (function (fs$close) { return function (fd, cb) {
38 return fs$close.call(fs, fd, function (err) {
42 if (typeof cb === 'function')
43 cb.apply(this, arguments)
47 module.exports.closeSync =
48 fs.closeSync = (function (fs$closeSync) { return function (fd) {
49 // Note that graceful-fs also retries when fs.closeSync() fails.
50 // Looks like a bug to me, although it's probably a harmless one.
51 var rval = fs$closeSync.apply(fs, arguments)
57 // Everything that references the open() function needs to be in here
59 fs.gracefulify = patch
60 fs.FileReadStream = ReadStream; // Legacy name.
61 fs.FileWriteStream = WriteStream; // Legacy name.
62 fs.createReadStream = createReadStream
63 fs.createWriteStream = createWriteStream
64 var fs$readFile = fs.readFile
65 fs.readFile = readFile
66 function readFile (path, options, cb) {
67 if (typeof options === 'function')
68 cb = options, options = null
70 return go$readFile(path, options, cb)
72 function go$readFile (path, options, cb) {
73 return fs$readFile(path, options, function (err) {
74 if (err && (err.code === 'EMFILE' || err.code === 'ENFILE'))
75 enqueue([go$readFile, [path, options, cb]])
77 if (typeof cb === 'function')
78 cb.apply(this, arguments)
85 var fs$writeFile = fs.writeFile
86 fs.writeFile = writeFile
87 function writeFile (path, data, options, cb) {
88 if (typeof options === 'function')
89 cb = options, options = null
91 return go$writeFile(path, data, options, cb)
93 function go$writeFile (path, data, options, cb) {
94 return fs$writeFile(path, data, options, function (err) {
95 if (err && (err.code === 'EMFILE' || err.code === 'ENFILE'))
96 enqueue([go$writeFile, [path, data, options, cb]])
98 if (typeof cb === 'function')
99 cb.apply(this, arguments)
106 var fs$appendFile = fs.appendFile
108 fs.appendFile = appendFile
109 function appendFile (path, data, options, cb) {
110 if (typeof options === 'function')
111 cb = options, options = null
113 return go$appendFile(path, data, options, cb)
115 function go$appendFile (path, data, options, cb) {
116 return fs$appendFile(path, data, options, function (err) {
117 if (err && (err.code === 'EMFILE' || err.code === 'ENFILE'))
118 enqueue([go$appendFile, [path, data, options, cb]])
120 if (typeof cb === 'function')
121 cb.apply(this, arguments)
128 var fs$readdir = fs.readdir
130 function readdir (path, options, cb) {
132 if (typeof options !== 'function') {
137 args.push(go$readdir$cb)
139 return go$readdir(args)
141 function go$readdir$cb (err, files) {
142 if (files && files.sort)
145 if (err && (err.code === 'EMFILE' || err.code === 'ENFILE'))
146 enqueue([go$readdir, [args]])
148 if (typeof cb === 'function')
149 cb.apply(this, arguments)
155 function go$readdir (args) {
156 return fs$readdir.apply(fs, args)
159 if (process.version.substr(0, 4) === 'v0.8') {
160 var legStreams = legacy(fs)
161 ReadStream = legStreams.ReadStream
162 WriteStream = legStreams.WriteStream
165 var fs$ReadStream = fs.ReadStream
166 ReadStream.prototype = Object.create(fs$ReadStream.prototype)
167 ReadStream.prototype.open = ReadStream$open
169 var fs$WriteStream = fs.WriteStream
170 WriteStream.prototype = Object.create(fs$WriteStream.prototype)
171 WriteStream.prototype.open = WriteStream$open
173 fs.ReadStream = ReadStream
174 fs.WriteStream = WriteStream
176 function ReadStream (path, options) {
177 if (this instanceof ReadStream)
178 return fs$ReadStream.apply(this, arguments), this
180 return ReadStream.apply(Object.create(ReadStream.prototype), arguments)
183 function ReadStream$open () {
185 open(that.path, that.flags, that.mode, function (err, fd) {
190 that.emit('error', err)
193 that.emit('open', fd)
199 function WriteStream (path, options) {
200 if (this instanceof WriteStream)
201 return fs$WriteStream.apply(this, arguments), this
203 return WriteStream.apply(Object.create(WriteStream.prototype), arguments)
206 function WriteStream$open () {
208 open(that.path, that.flags, that.mode, function (err, fd) {
211 that.emit('error', err)
214 that.emit('open', fd)
219 function createReadStream (path, options) {
220 return new ReadStream(path, options)
223 function createWriteStream (path, options) {
224 return new WriteStream(path, options)
227 var fs$open = fs.open
229 function open (path, flags, mode, cb) {
230 if (typeof mode === 'function')
231 cb = mode, mode = null
233 return go$open(path, flags, mode, cb)
235 function go$open (path, flags, mode, cb) {
236 return fs$open(path, flags, mode, function (err, fd) {
237 if (err && (err.code === 'EMFILE' || err.code === 'ENFILE'))
238 enqueue([go$open, [path, flags, mode, cb]])
240 if (typeof cb === 'function')
241 cb.apply(this, arguments)
251 function enqueue (elem) {
252 debug('ENQUEUE', elem[0].name, elem[1])
257 var elem = queue.shift()
259 debug('RETRY', elem[0].name, elem[1])
260 elem[0].apply(null, elem[1])