| 1 | var fs = require('fs') |
| 2 | var polyfills = require('./polyfills.js') |
| 3 | var legacy = require('./legacy-streams.js') |
| 4 | var queue = [] |
| 5 | |
| 6 | var util = require('util') |
| 7 | |
| 8 | function noop () {} |
| 9 | |
| 10 | var debug = noop |
| 11 | if (util.debuglog) |
| 12 | debug = util.debuglog('gfs4') |
| 13 | else if (/\bgfs4\b/i.test(process.env.NODE_DEBUG || '')) |
| 14 | debug = function() { |
| 15 | var m = util.format.apply(util, arguments) |
| 16 | m = 'GFS4: ' + m.split(/\n/).join('\nGFS4: ') |
| 17 | console.error(m) |
| 18 | } |
| 19 | |
| 20 | if (/\bgfs4\b/i.test(process.env.NODE_DEBUG || '')) { |
| 21 | process.on('exit', function() { |
| 22 | debug(queue) |
| 23 | require('assert').equal(queue.length, 0) |
| 24 | }) |
| 25 | } |
| 26 | |
| 27 | module.exports = patch(require('./fs.js')) |
| 28 | if (process.env.TEST_GRACEFUL_FS_GLOBAL_PATCH) { |
| 29 | module.exports = patch(fs) |
| 30 | } |
| 31 | |
| 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) { |
| 39 | if (!err) |
| 40 | retry() |
| 41 | |
| 42 | if (typeof cb === 'function') |
| 43 | cb.apply(this, arguments) |
| 44 | }) |
| 45 | }})(fs.close) |
| 46 | |
| 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) |
| 52 | retry() |
| 53 | return rval |
| 54 | }})(fs.closeSync) |
| 55 | |
| 56 | function patch (fs) { |
| 57 | // Everything that references the open() function needs to be in here |
| 58 | polyfills(fs) |
| 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 |
| 69 | |
| 70 | return go$readFile(path, options, cb) |
| 71 | |
| 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]]) |
| 76 | else { |
| 77 | if (typeof cb === 'function') |
| 78 | cb.apply(this, arguments) |
| 79 | retry() |
| 80 | } |
| 81 | }) |
| 82 | } |
| 83 | } |
| 84 | |
| 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 |
| 90 | |
| 91 | return go$writeFile(path, data, options, cb) |
| 92 | |
| 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]]) |
| 97 | else { |
| 98 | if (typeof cb === 'function') |
| 99 | cb.apply(this, arguments) |
| 100 | retry() |
| 101 | } |
| 102 | }) |
| 103 | } |
| 104 | } |
| 105 | |
| 106 | var fs$appendFile = fs.appendFile |
| 107 | if (fs$appendFile) |
| 108 | fs.appendFile = appendFile |
| 109 | function appendFile (path, data, options, cb) { |
| 110 | if (typeof options === 'function') |
| 111 | cb = options, options = null |
| 112 | |
| 113 | return go$appendFile(path, data, options, cb) |
| 114 | |
| 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]]) |
| 119 | else { |
| 120 | if (typeof cb === 'function') |
| 121 | cb.apply(this, arguments) |
| 122 | retry() |
| 123 | } |
| 124 | }) |
| 125 | } |
| 126 | } |
| 127 | |
| 128 | var fs$readdir = fs.readdir |
| 129 | fs.readdir = readdir |
| 130 | function readdir (path, options, cb) { |
| 131 | var args = [path] |
| 132 | if (typeof options !== 'function') { |
| 133 | args.push(options) |
| 134 | } else { |
| 135 | cb = options |
| 136 | } |
| 137 | args.push(go$readdir$cb) |
| 138 | |
| 139 | return go$readdir(args) |
| 140 | |
| 141 | function go$readdir$cb (err, files) { |
| 142 | if (files && files.sort) |
| 143 | files.sort() |
| 144 | |
| 145 | if (err && (err.code === 'EMFILE' || err.code === 'ENFILE')) |
| 146 | enqueue([go$readdir, [args]]) |
| 147 | else { |
| 148 | if (typeof cb === 'function') |
| 149 | cb.apply(this, arguments) |
| 150 | retry() |
| 151 | } |
| 152 | } |
| 153 | } |
| 154 | |
| 155 | function go$readdir (args) { |
| 156 | return fs$readdir.apply(fs, args) |
| 157 | } |
| 158 | |
| 159 | if (process.version.substr(0, 4) === 'v0.8') { |
| 160 | var legStreams = legacy(fs) |
| 161 | ReadStream = legStreams.ReadStream |
| 162 | WriteStream = legStreams.WriteStream |
| 163 | } |
| 164 | |
| 165 | var fs$ReadStream = fs.ReadStream |
| 166 | ReadStream.prototype = Object.create(fs$ReadStream.prototype) |
| 167 | ReadStream.prototype.open = ReadStream$open |
| 168 | |
| 169 | var fs$WriteStream = fs.WriteStream |
| 170 | WriteStream.prototype = Object.create(fs$WriteStream.prototype) |
| 171 | WriteStream.prototype.open = WriteStream$open |
| 172 | |
| 173 | fs.ReadStream = ReadStream |
| 174 | fs.WriteStream = WriteStream |
| 175 | |
| 176 | function ReadStream (path, options) { |
| 177 | if (this instanceof ReadStream) |
| 178 | return fs$ReadStream.apply(this, arguments), this |
| 179 | else |
| 180 | return ReadStream.apply(Object.create(ReadStream.prototype), arguments) |
| 181 | } |
| 182 | |
| 183 | function ReadStream$open () { |
| 184 | var that = this |
| 185 | open(that.path, that.flags, that.mode, function (err, fd) { |
| 186 | if (err) { |
| 187 | if (that.autoClose) |
| 188 | that.destroy() |
| 189 | |
| 190 | that.emit('error', err) |
| 191 | } else { |
| 192 | that.fd = fd |
| 193 | that.emit('open', fd) |
| 194 | that.read() |
| 195 | } |
| 196 | }) |
| 197 | } |
| 198 | |
| 199 | function WriteStream (path, options) { |
| 200 | if (this instanceof WriteStream) |
| 201 | return fs$WriteStream.apply(this, arguments), this |
| 202 | else |
| 203 | return WriteStream.apply(Object.create(WriteStream.prototype), arguments) |
| 204 | } |
| 205 | |
| 206 | function WriteStream$open () { |
| 207 | var that = this |
| 208 | open(that.path, that.flags, that.mode, function (err, fd) { |
| 209 | if (err) { |
| 210 | that.destroy() |
| 211 | that.emit('error', err) |
| 212 | } else { |
| 213 | that.fd = fd |
| 214 | that.emit('open', fd) |
| 215 | } |
| 216 | }) |
| 217 | } |
| 218 | |
| 219 | function createReadStream (path, options) { |
| 220 | return new ReadStream(path, options) |
| 221 | } |
| 222 | |
| 223 | function createWriteStream (path, options) { |
| 224 | return new WriteStream(path, options) |
| 225 | } |
| 226 | |
| 227 | var fs$open = fs.open |
| 228 | fs.open = open |
| 229 | function open (path, flags, mode, cb) { |
| 230 | if (typeof mode === 'function') |
| 231 | cb = mode, mode = null |
| 232 | |
| 233 | return go$open(path, flags, mode, cb) |
| 234 | |
| 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]]) |
| 239 | else { |
| 240 | if (typeof cb === 'function') |
| 241 | cb.apply(this, arguments) |
| 242 | retry() |
| 243 | } |
| 244 | }) |
| 245 | } |
| 246 | } |
| 247 | |
| 248 | return fs |
| 249 | } |
| 250 | |
| 251 | function enqueue (elem) { |
| 252 | debug('ENQUEUE', elem[0].name, elem[1]) |
| 253 | queue.push(elem) |
| 254 | } |
| 255 | |
| 256 | function retry () { |
| 257 | var elem = queue.shift() |
| 258 | if (elem) { |
| 259 | debug('RETRY', elem[0].name, elem[1]) |
| 260 | elem[0].apply(null, elem[1]) |
| 261 | } |
| 262 | } |