summaryrefslogtreecommitdiff
path: root/deps/npm/node_modules/graceful-fs
diff options
context:
space:
mode:
authorisaacs <i@izs.me>2011-11-22 16:56:59 -0800
committerisaacs <i@izs.me>2011-11-22 17:03:58 -0800
commit3ab15cde2516564a2bc4fe6246fe447f07752ed4 (patch)
treee858b4c4617fe2f9d1b69e7b2186f6b0674ec93b /deps/npm/node_modules/graceful-fs
parent0ba78d5f36256dacf625e96dc40e4e34bacfdd35 (diff)
downloadnode-new-3ab15cde2516564a2bc4fe6246fe447f07752ed4.tar.gz
Update npm to 1.1.0-alpha-2
Diffstat (limited to 'deps/npm/node_modules/graceful-fs')
-rw-r--r--deps/npm/node_modules/graceful-fs/graceful-fs.js218
-rw-r--r--deps/npm/node_modules/graceful-fs/package.json8
2 files changed, 189 insertions, 37 deletions
diff --git a/deps/npm/node_modules/graceful-fs/graceful-fs.js b/deps/npm/node_modules/graceful-fs/graceful-fs.js
index 08e99b24b0..9b8492ac13 100644
--- a/deps/npm/node_modules/graceful-fs/graceful-fs.js
+++ b/deps/npm/node_modules/graceful-fs/graceful-fs.js
@@ -1,39 +1,189 @@
-// wrapper around the non-sync fs functions to gracefully handle
-// having too many file descriptors open. Note that this is
-// *only* possible because async patterns let one interject timeouts
-// and other cleverness anywhere in the process without disrupting
-// anything else.
+// this keeps a queue of opened file descriptors, and will make
+// fs operations wait until some have closed before trying to open more.
var fs = require("fs")
- , timeout = 0
-
-Object.keys(fs)
- .forEach(function (i) {
- exports[i] = (typeof fs[i] !== "function") ? fs[i]
- : (i.match(/^[A-Z]|^create|Sync$/)) ? function () {
- return fs[i].apply(fs, arguments)
- }
- : graceful(fs[i])
+ , FastList = require("fast-list")
+ , queue = new FastList()
+ , curOpen = 0
+ , constants = require("constants")
+
+exports = module.exports = fs
+
+fs.MAX_OPEN = 256
+
+fs._open = fs.open
+fs._openSync = fs.openSync
+fs._close = fs.close
+fs._closeSync = fs.closeSync
+
+
+// lstat on windows, missing from early 0.5 versions
+if (process.platform === "win32" && !process.binding("fs").lstat) {
+ fs.lstat = fs.stat
+ fs.lstatSync = fs.statSync
+}
+
+// lutimes
+var constants = require("constants")
+if (!fs.lutimes) fs.lutimes = function (path, at, mt, cb) {
+ fs.open(path, constants.O_SYMLINK, function (er, fd) {
+ cb = cb || noop
+ if (er) return cb(er)
+ fs.futimes(fd, at, mt, function (er) {
+ if (er) {
+ fs.close(fd, function () {})
+ return cb(er)
+ }
+ fs.close(fd, cb)
+ })
+ })
+}
+
+if (!fs.lutimesSync) fs.lutimesSync = function (path, at, mt) {
+ var fd = fs.openSync(path, constants.O_SYMLINK)
+ fs.futimesSync(fd, at, mt)
+ fs.closeSync(fd)
+}
+
+
+// prevent EMFILE errors
+function OpenReq (path, flags, mode, cb) {
+ this.path = path
+ this.flags = flags
+ this.mode = mode
+ this.cb = cb
+}
+
+function noop () {}
+
+fs.open = function (path, flags, mode, cb) {
+ if (typeof mode === "function") cb = mode, mode = null
+ if (typeof cb !== "function") cb = noop
+
+ if (curOpen >= fs.MAX_OPEN) {
+ queue.push(new OpenReq(path, flags, mode, cb))
+ setTimeout(flush)
+ return
+ }
+ open(path, flags, mode, cb)
+}
+
+function open (path, flags, mode, cb) {
+ cb = cb || noop
+ curOpen ++
+ fs._open(path, flags, mode, function (er, fd) {
+ if (er) {
+ onclose()
+ }
+
+ cb(er, fd)
+ })
+}
+
+fs.openSync = function (path, flags, mode) {
+ curOpen ++
+ return fs._openSync(path, flags, mode)
+}
+
+function onclose () {
+ curOpen --
+ flush()
+}
+
+function flush () {
+ while (curOpen < fs.MAX_OPEN) {
+ var req = queue.shift()
+ if (!req) break
+ open(req.path, req.flags, req.mode, req.cb)
+ }
+ if (queue.length === 0) return
+}
+
+fs.close = function (fd, cb) {
+ cb = cb || noop
+ fs._close(fd, function (er) {
+ onclose()
+ cb(er)
})
+}
+
+fs.closeSync = function (fd) {
+ onclose()
+ return fs._closeSync(fd)
+}
-if (process.platform === "win32"
- && !process.binding("fs").lstat) {
- exports.lstat = exports.stat
- exports.lstatSync = exports.statSync
-}
-
-function graceful (fn) { return function GRACEFUL () {
- var args = Array.prototype.slice.call(arguments)
- , cb_ = args.pop()
- args.push(cb)
- function cb (er) {
- if (er && er.message.match(/^EMFILE, Too many open files/)) {
- setTimeout(function () {
- GRACEFUL.apply(fs, args)
- }, timeout ++)
- return
+// lchmod, broken prior to 0.6.2
+// back-port the fix here.
+if (constants.hasOwnProperty('O_SYMLINK') &&
+ process.version.match(/^v0\.6\.[0-2]|^v0\.5\./)) {
+ fs.lchmod = function(path, mode, callback) {
+ callback = callback || noop;
+ fs.open(path, constants.O_WRONLY | constants.O_SYMLINK, function(err, fd) {
+ if (err) {
+ callback(err);
+ return;
+ }
+ // prefer to return the chmod error, if one occurs,
+ // but still try to close, and report closing errors if they occur.
+ fs.fchmod(fd, mode, function(err) {
+ fs.close(fd, function(err2) {
+ callback(err || err2);
+ });
+ });
+ });
+ };
+
+ fs.lchmodSync = function(path, mode) {
+ var fd = fs.openSync(path, constants.O_WRONLY | constants.O_SYMLINK);
+
+ // prefer to return the chmod error, if one occurs,
+ // but still try to close, and report closing errors if they occur.
+ var err, err2;
+ try {
+ var ret = fs.fchmodSync(fd, mode);
+ } catch (er) {
+ err = er;
+ }
+ try {
+ fs.closeSync(fd);
+ } catch (er) {
+ err2 = er;
}
- timeout = 0
- cb_.apply(null, arguments)
+ if (err || err2) throw (err || err2);
+ return ret;
+ };
+}
+
+// lutimes, not yet implemented in node
+if (constants.hasOwnProperty('O_SYMLINK') && !fs.lutimes) {
+ fs.lutimes = function (path, atime, mtime, cb) {
+ cb = cb || noop
+ fs.open(path, constants.O_SYMLINK | constants.O_WRONLY, function (er, fd) {
+ if (er) return cb(er)
+ fs.futimes(fd, atime, mtime, function (er) {
+ fs.close(fd, function (er2) {
+ cb(er || er2)
+ })
+ })
+ })
}
- fn.apply(fs, args)
-}}
+
+ fs.lutimesSync = function(path, atime, mtime) {
+ var fd = fs.openSync(path, constants.O_WRONLY | constants.O_SYMLINK)
+
+ // prefer to return the chmod error, if one occurs,
+ // but still try to close, and report closing errors if they occur.
+ var err, err2
+ try {
+ var ret = fs.futimesSync(fd, atime, mtime)
+ } catch (er) {
+ err = er
+ }
+ try {
+ fs.closeSync(fd)
+ } catch (er) {
+ err2 = er
+ }
+ if (err || err2) throw (err || err2)
+ return ret
+ }
+}
diff --git a/deps/npm/node_modules/graceful-fs/package.json b/deps/npm/node_modules/graceful-fs/package.json
index 934829c5cc..3c2f8a6fff 100644
--- a/deps/npm/node_modules/graceful-fs/package.json
+++ b/deps/npm/node_modules/graceful-fs/package.json
@@ -1,8 +1,8 @@
{
"author": "Isaac Z. Schlueter <i@izs.me> (http://blog.izs.me)",
"name": "graceful-fs",
- "description": "fs with incremental backoff on EMFILE",
- "version": "1.0.1",
+ "description": "fs monkey-patching to avoid EMFILE and other problems",
+ "version": "1.1.0",
"repository": {
"type": "git",
"url": "git://github.com/isaacs/node-graceful-fs.git"
@@ -11,6 +11,8 @@
"engines": {
"node": "0.4 || 0.5 || 0.6"
},
- "dependencies": {},
+ "dependencies": {
+ "fast-list": "1"
+ },
"devDependencies": {}
}