1 // this keeps a queue of opened file descriptors, and will make
2 // fs operations wait until some have closed before trying to open more.
4 var fs = exports = module.exports = {}
5 fs._originalFs = require("fs")
7 Object.getOwnPropertyNames(fs._originalFs).forEach(function(prop) {
8 var desc = Object.getOwnPropertyDescriptor(fs._originalFs, prop)
9 Object.defineProperty(fs, prop, desc)
13 , constants = require("constants")
20 // prevent EMFILE errors
21 function OpenReq (path, flags, mode, cb) {
30 fs.open = gracefulOpen
32 function gracefulOpen (path, flags, mode, cb) {
33 if (typeof mode === "function") cb = mode, mode = null
34 if (typeof cb !== "function") cb = noop
36 if (fs._curOpen >= fs.MAX_OPEN) {
37 queue.push(new OpenReq(path, flags, mode, cb))
41 open(path, flags, mode, function (er, fd) {
42 if (er && er.code === "EMFILE" && fs._curOpen > fs.MIN_MAX_OPEN) {
43 // that was too many. reduce max, get back in queue.
44 // this should only happen once in a great while, and only
45 // if the ulimit -n is set lower than 1024.
46 fs.MAX_OPEN = fs._curOpen - 1
47 return fs.open(path, flags, mode, cb)
53 function open (path, flags, mode, cb) {
56 fs._originalFs.open.call(fs, path, flags, mode, function (er, fd) {
62 fs.openSync = function (path, flags, mode) {
64 ret = fs._originalFs.openSync.call(fs, path, flags, mode)
75 while (fs._curOpen < fs.MAX_OPEN) {
76 var req = queue.shift()
78 switch (req.constructor.name) {
80 open(req.path, req.flags || "r", req.mode || 0777, req.cb)
83 readdir(req.path, req.cb)
86 readFile(req.path, req.options, req.cb)
89 writeFile(req.path, req.data, req.options, req.cb)
92 throw new Error('Unknown req type: ' + req.constructor.name)
97 fs.close = function (fd, cb) {
99 fs._originalFs.close.call(fs, fd, function (er) {
105 fs.closeSync = function (fd) {
107 return fs._originalFs.closeSync.call(fs, fd)
114 // readdir takes a fd as well.
115 // however, the sync version closes it right away, so
116 // there's no need to wrap.
117 // It would be nice to catch when it throws an EMFILE,
118 // but that's relatively rare anyway.
120 fs.readdir = gracefulReaddir
122 function gracefulReaddir (path, cb) {
123 if (fs._curOpen >= fs.MAX_OPEN) {
124 queue.push(new ReaddirReq(path, cb))
129 readdir(path, function (er, files) {
130 if (er && er.code === "EMFILE" && fs._curOpen > fs.MIN_MAX_OPEN) {
131 fs.MAX_OPEN = fs._curOpen - 1
132 return fs.readdir(path, cb)
138 function readdir (path, cb) {
141 fs._originalFs.readdir.call(fs, path, function (er, files) {
147 function ReaddirReq (path, cb) {
153 fs.readFile = gracefulReadFile
155 function gracefulReadFile(path, options, cb) {
156 if (typeof options === "function") cb = options, options = null
157 if (typeof cb !== "function") cb = noop
159 if (fs._curOpen >= fs.MAX_OPEN) {
160 queue.push(new ReadFileReq(path, options, cb))
165 readFile(path, options, function (er, data) {
166 if (er && er.code === "EMFILE" && fs._curOpen > fs.MIN_MAX_OPEN) {
167 fs.MAX_OPEN = fs._curOpen - 1
168 return fs.readFile(path, options, cb)
174 function readFile (path, options, cb) {
177 fs._originalFs.readFile.call(fs, path, options, function (er, data) {
183 function ReadFileReq (path, options, cb) {
185 this.options = options
192 fs.writeFile = gracefulWriteFile
194 function gracefulWriteFile(path, data, options, cb) {
195 if (typeof options === "function") cb = options, options = null
196 if (typeof cb !== "function") cb = noop
198 if (fs._curOpen >= fs.MAX_OPEN) {
199 queue.push(new WriteFileReq(path, data, options, cb))
204 writeFile(path, data, options, function (er) {
205 if (er && er.code === "EMFILE" && fs._curOpen > fs.MIN_MAX_OPEN) {
206 fs.MAX_OPEN = fs._curOpen - 1
207 return fs.writeFile(path, data, options, cb)
213 function writeFile (path, data, options, cb) {
216 fs._originalFs.writeFile.call(fs, path, data, options, function (er) {
222 function WriteFileReq (path, data, options, cb) {
225 this.options = options
230 // (re-)implement some things that are known busted or missing.
232 var constants = require("constants")
234 // lchmod, broken prior to 0.6.2
235 // back-port the fix here.
236 if (constants.hasOwnProperty('O_SYMLINK') &&
237 process.version.match(/^v0\.6\.[0-2]|^v0\.5\./)) {
238 fs.lchmod = function (path, mode, callback) {
239 callback = callback || noop
241 , constants.O_WRONLY | constants.O_SYMLINK
243 , function (err, fd) {
248 // prefer to return the chmod error, if one occurs,
249 // but still try to close, and report closing errors if they occur.
250 fs.fchmod(fd, mode, function (err) {
251 fs.close(fd, function(err2) {
252 callback(err || err2)
258 fs.lchmodSync = function (path, mode) {
259 var fd = fs.openSync(path, constants.O_WRONLY | constants.O_SYMLINK, mode)
261 // prefer to return the chmod error, if one occurs,
262 // but still try to close, and report closing errors if they occur.
265 var ret = fs.fchmodSync(fd, mode)
274 if (err || err2) throw (err || err2)
280 // lutimes implementation, or no-op
282 if (constants.hasOwnProperty("O_SYMLINK")) {
283 fs.lutimes = function (path, at, mt, cb) {
284 fs.open(path, constants.O_SYMLINK, function (er, fd) {
286 if (er) return cb(er)
287 fs.futimes(fd, at, mt, function (er) {
288 fs.close(fd, function (er2) {
295 fs.lutimesSync = function (path, at, mt) {
296 var fd = fs.openSync(path, constants.O_SYMLINK)
302 var ret = fs.futimesSync(fd, at, mt)
311 if (err || err2) throw (err || err2)
315 } else if (fs.utimensat && constants.hasOwnProperty("AT_SYMLINK_NOFOLLOW")) {
316 // maybe utimensat will be bound soonish?
317 fs.lutimes = function (path, at, mt, cb) {
318 fs.utimensat(path, at, mt, constants.AT_SYMLINK_NOFOLLOW, cb)
321 fs.lutimesSync = function (path, at, mt) {
322 return fs.utimensatSync(path, at, mt, constants.AT_SYMLINK_NOFOLLOW)
326 fs.lutimes = function (_a, _b, _c, cb) { process.nextTick(cb) }
327 fs.lutimesSync = function () {}
332 // https://github.com/isaacs/node-graceful-fs/issues/4
333 // Chown should not fail on einval or eperm if non-root.
335 fs.chown = chownFix(fs.chown)
336 fs.fchown = chownFix(fs.fchown)
337 fs.lchown = chownFix(fs.lchown)
339 fs.chownSync = chownFixSync(fs.chownSync)
340 fs.fchownSync = chownFixSync(fs.fchownSync)
341 fs.lchownSync = chownFixSync(fs.lchownSync)
343 function chownFix (orig) {
344 if (!orig) return orig
345 return function (target, uid, gid, cb) {
346 return orig.call(fs, target, uid, gid, function (er, res) {
347 if (chownErOk(er)) er = null
353 function chownFixSync (orig) {
354 if (!orig) return orig
355 return function (target, uid, gid) {
357 return orig.call(fs, target, uid, gid)
359 if (!chownErOk(er)) throw er
364 function chownErOk (er) {
365 // if there's no getuid, or if getuid() is something other than 0,
366 // and the error is EINVAL or EPERM, then just ignore it.
367 // This specific case is a silent failure in cp, install, tar,
368 // and most other unix tools that manage permissions.
369 // When running as root, or if other types of errors are encountered,
371 if (!er || (!process.getuid || process.getuid() !== 0)
372 && (er.code === "EINVAL" || er.code === "EPERM")) return true
376 // if lchmod/lchown do not exist, then make them no-ops
378 fs.lchmod = function (path, mode, cb) {
381 fs.lchmodSync = function () {}
384 fs.lchown = function (path, uid, gid, cb) {
387 fs.lchownSync = function () {}
392 // on Windows, A/V software can lock the directory, causing this
393 // to fail with an EACCES or EPERM if the directory contains newly
394 // created files. Try again on failure, for up to 1 second.
395 if (process.platform === "win32") {
396 var rename_ = fs.rename
397 fs.rename = function rename (from, to, cb) {
398 var start = Date.now()
399 rename_(from, to, function CB (er) {
401 && (er.code === "EACCES" || er.code === "EPERM")
402 && Date.now() - start < 1000) {
403 return rename_(from, to, CB)
411 // if read() returns EAGAIN, then just try it again.
413 fs.read = function (fd, buffer, offset, length, position, callback_) {
415 if (callback_ && typeof callback_ === 'function') {
417 callback = function (er, _, __) {
418 if (er && er.code === 'EAGAIN' && eagCounter < 10) {
420 return read.call(fs, fd, buffer, offset, length, position, callback)
422 callback_.apply(this, arguments)
425 return read.call(fs, fd, buffer, offset, length, position, callback)
428 var readSync = fs.readSync
429 fs.readSync = function (fd, buffer, offset, length, position) {
433 return readSync.call(fs, fd, buffer, offset, length, position)
435 if (er.code === 'EAGAIN' && eagCounter < 10) {