Permalink
Browse files

Remove options, check dirs for writableness, too

  • Loading branch information...
1 parent df9982c commit 259f75b6f6c843e427197202cece4ac39d5a520b @isaacs committed Jan 30, 2012
Showing with 25 additions and 33 deletions.
  1. +1 −8 README.md
  2. +24 −25 rimraf.js
View
@@ -4,7 +4,7 @@ Install with `npm install rimraf`, or just drop rimraf.js somewhere.
## API
-`rimraf(f, [options,] callback)`
+`rimraf(f, callback)`
The callback will be called with an error if there is one. Certain
errors are handled for you:
@@ -14,13 +14,6 @@ errors are handled for you:
* `EMFILE` - If too many file descriptors get opened, rimraf will
patiently wait until more become available.
-## Options
-
-The options object is optional. These fields are respected:
-
-* `maxBusyTries` - The number of times to retry a file or folder in the
- event of an `EBUSY` error. The default is 3.
-
## rimraf.sync
View
@@ -16,32 +16,30 @@ var lstat = process.platform === "win32" ? "stat" : "lstat"
// for EMFILE handling
var timeout = 0
- , EMFILE_MAX = 1000
+exports.EMFILE_MAX = 1000
+exports.BUSYTRIES_MAX = 3
-function rimraf (p, opts, cb) {
- if (typeof opts === "function") cb = opts, opts = {}
+function rimraf (p, cb) {
if (!cb) throw new Error("No callback passed to rimraf()")
- if (!opts) opts = {}
var busyTries = 0
- opts.maxBusyTries = opts.maxBusyTries || 3
- rimraf_(p, opts, function CB (er) {
+ rimraf_(p, function CB (er) {
if (er) {
- if (er.code === "EBUSY" && busyTries < opts.maxBusyTries) {
- var time = (opts.maxBusyTries - busyTries) * 100
+ if (er.code === "EBUSY" && busyTries < exports.BUSYTRIES_MAX) {
+ var time = (exports.BUSYTRIES_MAX - busyTries) * 100
busyTries ++
// try again, with the same exact callback as this one.
return setTimeout(function () {
- rimraf_(p, opts, CB)
+ rimraf_(p, CB)
})
}
// this one won't happen if graceful-fs is used.
- if (er.code === "EMFILE" && timeout < EMFILE_MAX) {
+ if (er.code === "EMFILE" && timeout < export.EMFILE_MAX) {
return setTimeout(function () {
- rimraf_(p, opts, CB)
+ rimraf_(p, CB)
}, timeout ++)
}
@@ -54,7 +52,7 @@ function rimraf (p, opts, cb) {
})
}
-function rimraf_ (p, opts, cb) {
+function rimraf_ (p, cb) {
fs[lstat](p, function (er, s) {
if (er) {
// already gone
@@ -63,7 +61,7 @@ function rimraf_ (p, opts, cb) {
return cb(er)
}
- return rm_(p, s, opts, cb)
+ return rm_(p, s, false, cb)
})
}
@@ -90,27 +88,28 @@ function writable (s) {
|| (uid === s.uid && (mode & 0200))
}
-function rm_ (p, s, opts, cb) {
+function rm_ (p, s, didWritableCheck, cb) {
+ if (!didWritableCheck && !writable(s)) {
+ // make file writable
+ // user/group/world, doesn't matter at this point
+ // since it's about to get nuked.
+ return fs.chmod(p, s.mode | 0222, function (er) {
+ if (er) return cb(er)
+ rm_(p, s, true, cb)
+ })
+ }
+
if (!s.isDirectory()) {
- // check if the file is writable
- if (!writable(s)) {
- // make file writable
- // user/group/world, doesn't matter at this point
- // since it's about to get nuked.
- return fs.chmod(p, s.mode | 0222, function (er) {
- if (er) return cb(er)
- fs.unlink(p, cb)
- })
- }
return fs.unlink(p, cb)
}
+
// directory
fs.readdir(p, function (er, files) {
if (er) return cb(er)
asyncForEach(files.map(function (f) {
return path.join(p, f)
}), function (file, cb) {
- rimraf(file, opts, cb)
+ rimraf(file, cb)
}, function (er) {
if (er) return cb(er)
fs.rmdir(p, cb)

0 comments on commit 259f75b

Please sign in to comment.