Permalink
Browse files

Change mode only if the file read-only

  • Loading branch information...
1 parent 8224819 commit 4b4df2fb616456bc0f164d5714d6f97521b5d54b @yosefd yosefd committed Jan 29, 2012
Showing with 13 additions and 9 deletions.
  1. +13 −9 rimraf.js
View
@@ -58,7 +58,6 @@ function rimraf (p, opts, cb) {
function rimraf_ (p, opts, cb) {
fs[lstat](p, function (er, s) {
- // if the stat fails, then assume it's already gone.
if (er) {
// already gone
if (er.code === "ENOENT") return cb()
@@ -72,14 +71,19 @@ function rimraf_ (p, opts, cb) {
})
}
-function rm_ (p, s, opts, cb) {
- if (!s.isDirectory()) {
- // In case the file is read-only, make it read-write.
- return fs.chmod(p, s.mode | 0777, function (er) {
- if (er) return cb(er)
- return fs.unlink(p, cb)
- });
+function rm_ (p, s, opts, cb) {
+ if (!s.isDirectory()) {
+ // check if the file is writable
+ if ((s.mode & 0222) !== 0222) {
+ // make file writable
+ 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) {
@@ -99,7 +103,7 @@ function clobberTest (p, s, opts, cb) {
else realish(p, next)
function next (er, rp) {
- if (er) return rm_(p, s, cb)
+ if (er) return rm_(p, s, opts, cb)
if (rp.indexOf(gently) !== 0) return clobberFail(p, gently, cb)
else return rm_(p, s, opts, cb)
}

0 comments on commit 4b4df2f

Please sign in to comment.