1 module.exports = rimraf
2 rimraf.sync = rimrafSync
4 var assert = require("assert")
5 var path = require("path")
7 var glob = require("glob")
16 // for EMFILE handling
19 var isWindows = (process.platform === "win32")
21 function defaults (options) {
30 methods.forEach(function(m) {
31 options[m] = options[m] || fs[m]
33 options[m] = options[m] || fs[m]
36 options.maxBusyTries = options.maxBusyTries || 3
37 options.emfileWait = options.emfileWait || 1000
40 function rimraf (p, options, cb) {
41 if (typeof options === 'function') {
46 assert(p, 'rimraf: missing path')
47 assert.equal(typeof p, 'string', 'rimraf: path should be a string')
48 assert(options, 'rimraf: missing options')
49 assert.equal(typeof options, 'object', 'rimraf: options should be object')
50 assert.equal(typeof cb, 'function', 'rimraf: callback function required')
58 if (!glob.hasMagic(p))
59 return afterGlob(null, [p])
61 fs.lstat(p, function (er, stat) {
63 return afterGlob(null, [p])
65 glob(p, globOpts, afterGlob)
69 errState = errState || er
74 function afterGlob (er, results) {
82 results.forEach(function (p) {
83 rimraf_(p, options, function CB (er) {
85 if (isWindows && (er.code === "EBUSY" || er.code === "ENOTEMPTY") &&
86 busyTries < options.maxBusyTries) {
88 var time = busyTries * 100
89 // try again, with the same exact callback as this one.
90 return setTimeout(function () {
91 rimraf_(p, options, CB)
95 // this one won't happen if graceful-fs is used.
96 if (er.code === "EMFILE" && timeout < options.emfileWait) {
97 return setTimeout(function () {
98 rimraf_(p, options, CB)
103 if (er.code === "ENOENT") er = null
113 // Two possible strategies.
114 // 1. Assume it's a file. unlink it, then do the dir stuff on EPERM or EISDIR
115 // 2. Assume it's a directory. readdir, then do the file stuff on ENOTDIR
117 // Both result in an extra syscall when you guess wrong. However, there
118 // are likely far more normal files in the world than directories. This
119 // is based on the assumption that a the average number of files per
120 // directory is >= 1.
122 // If anyone ever complains about this, then I guess the strategy could
123 // be made configurable somehow. But until then, YAGNI.
124 function rimraf_ (p, options, cb) {
127 assert(typeof cb === 'function')
129 // sunos lets the root user unlink directories, which is... weird.
130 // so we have to lstat here and make sure it's not a dir.
131 options.lstat(p, function (er, st) {
132 if (er && er.code === "ENOENT")
135 if (st && st.isDirectory())
136 return rmdir(p, options, er, cb)
138 options.unlink(p, function (er) {
140 if (er.code === "ENOENT")
142 if (er.code === "EPERM")
144 ? fixWinEPERM(p, options, er, cb)
145 : rmdir(p, options, er, cb)
146 if (er.code === "EISDIR")
147 return rmdir(p, options, er, cb)
154 function fixWinEPERM (p, options, er, cb) {
157 assert(typeof cb === 'function')
159 assert(er instanceof Error)
161 options.chmod(p, 666, function (er2) {
163 cb(er2.code === "ENOENT" ? null : er)
165 options.stat(p, function(er3, stats) {
167 cb(er3.code === "ENOENT" ? null : er)
168 else if (stats.isDirectory())
169 rmdir(p, options, er, cb)
171 options.unlink(p, cb)
176 function fixWinEPERMSync (p, options, er) {
180 assert(er instanceof Error)
183 options.chmodSync(p, 666)
185 if (er2.code === "ENOENT")
192 var stats = options.statSync(p)
194 if (er3.code === "ENOENT")
200 if (stats.isDirectory())
201 rmdirSync(p, options, er)
203 options.unlinkSync(p)
206 function rmdir (p, options, originalEr, cb) {
210 assert(originalEr instanceof Error)
211 assert(typeof cb === 'function')
213 // try to rmdir first, and only readdir on ENOTEMPTY or EEXIST (SunOS)
214 // if we guessed wrong, and it's not a directory, then
215 // raise the original error.
216 options.rmdir(p, function (er) {
217 if (er && (er.code === "ENOTEMPTY" || er.code === "EEXIST" || er.code === "EPERM"))
218 rmkids(p, options, cb)
219 else if (er && er.code === "ENOTDIR")
226 function rmkids(p, options, cb) {
229 assert(typeof cb === 'function')
231 options.readdir(p, function (er, files) {
236 return options.rmdir(p, cb)
238 files.forEach(function (f) {
239 rimraf(path.join(p, f), options, function (er) {
243 return cb(errState = er)
251 // this looks simpler, and is strictly *faster*, but will
252 // tie up the JavaScript thread and fail on excessively
253 // deep directory trees.
254 function rimrafSync (p, options) {
255 options = options || {}
258 assert(p, 'rimraf: missing path')
259 assert.equal(typeof p, 'string', 'rimraf: path should be a string')
260 assert(options, 'rimraf: missing options')
261 assert.equal(typeof options, 'object', 'rimraf: options should be object')
265 if (!glob.hasMagic(p)) {
272 results = glob.sync(p, globOpts)
279 for (var i = 0; i < results.length; i++) {
283 var st = options.lstatSync(p)
285 if (er.code === "ENOENT")
290 // sunos lets the root user unlink directories, which is... weird.
291 if (st && st.isDirectory())
292 rmdirSync(p, options, null)
294 options.unlinkSync(p)
296 if (er.code === "ENOENT")
298 if (er.code === "EPERM")
299 return isWindows ? fixWinEPERMSync(p, options, er) : rmdirSync(p, options, er)
300 if (er.code !== "EISDIR")
302 rmdirSync(p, options, er)
307 function rmdirSync (p, options, originalEr) {
311 assert(originalEr instanceof Error)
316 if (er.code === "ENOENT")
318 if (er.code === "ENOTDIR")
320 if (er.code === "ENOTEMPTY" || er.code === "EEXIST" || er.code === "EPERM")
321 rmkidsSync(p, options)
325 function rmkidsSync (p, options) {
328 options.readdirSync(p).forEach(function (f) {
329 rimrafSync(path.join(p, f), options)
331 options.rmdirSync(p, options)