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, cb) {
131 return go$readdir(path, cb)
133 function go$readdir () {
134 return fs$readdir(path, function (err, files) {
135 if (files && files.sort)
136 files.sort(); // Backwards compatibility with graceful-fs.
138 if (err && (err.code === 'EMFILE' || err.code === 'ENFILE'))
139 enqueue([go$readdir, [path, cb]])
141 if (typeof cb === 'function')
142 cb.apply(this, arguments)
150 if (process.version.substr(0, 4) === 'v0.8') {
151 var legStreams = legacy(fs)
152 ReadStream = legStreams.ReadStream
153 WriteStream = legStreams.WriteStream
156 var fs$ReadStream = fs.ReadStream
157 ReadStream.prototype = Object.create(fs$ReadStream.prototype)
158 ReadStream.prototype.open = ReadStream$open
160 var fs$WriteStream = fs.WriteStream
161 WriteStream.prototype = Object.create(fs$WriteStream.prototype)
162 WriteStream.prototype.open = WriteStream$open
164 fs.ReadStream = ReadStream
165 fs.WriteStream = WriteStream
167 function ReadStream (path, options) {
168 if (this instanceof ReadStream)
169 return fs$ReadStream.apply(this, arguments), this
171 return ReadStream.apply(Object.create(ReadStream.prototype), arguments)
174 function ReadStream$open () {
176 open(that.path, that.flags, that.mode, function (err, fd) {
181 that.emit('error', err)
184 that.emit('open', fd)
190 function WriteStream (path, options) {
191 if (this instanceof WriteStream)
192 return fs$WriteStream.apply(this, arguments), this
194 return WriteStream.apply(Object.create(WriteStream.prototype), arguments)
197 function WriteStream$open () {
199 open(that.path, that.flags, that.mode, function (err, fd) {
202 that.emit('error', err)
205 that.emit('open', fd)
210 function createReadStream (path, options) {
211 return new ReadStream(path, options)
214 function createWriteStream (path, options) {
215 return new WriteStream(path, options)
218 var fs$open = fs.open
220 function open (path, flags, mode, cb) {
221 if (typeof mode === 'function')
222 cb = mode, mode = null
224 return go$open(path, flags, mode, cb)
226 function go$open (path, flags, mode, cb) {
227 return fs$open(path, flags, mode, function (err, fd) {
228 if (err && (err.code === 'EMFILE' || err.code === 'ENFILE'))
229 enqueue([go$open, [path, flags, mode, cb]])
231 if (typeof cb === 'function')
232 cb.apply(this, arguments)
242 function enqueue (elem) {
243 debug('ENQUEUE', elem[0].name, elem[1])
248 var elem = queue.shift()
250 debug('RETRY', elem[0].name, elem[1])
251 elem[0].apply(null, elem[1])