1 module.exports = rimraf
2 rimraf.sync = rimrafSync
4 var assert = require("assert")
5 var path = require("path")
7 var glob = require("glob")
9 var defaultGlobOpts = {
14 // for EMFILE handling
17 var isWindows = (process.platform === "win32")
19 function defaults (options) {
28 methods.forEach(function(m) {
29 options[m] = options[m] || fs[m]
31 options[m] = options[m] || fs[m]
34 options.maxBusyTries = options.maxBusyTries || 3
35 options.emfileWait = options.emfileWait || 1000
36 if (options.glob === false) {
37 options.disableGlob = true
39 options.disableGlob = options.disableGlob || false
40 options.glob = options.glob || defaultGlobOpts
43 function rimraf (p, options, cb) {
44 if (typeof options === 'function') {
49 assert(p, 'rimraf: missing path')
50 assert.equal(typeof p, 'string', 'rimraf: path should be a string')
51 assert.equal(typeof cb, 'function', 'rimraf: callback function required')
52 assert(options, 'rimraf: invalid options argument provided')
53 assert.equal(typeof options, 'object', 'rimraf: options should be object')
61 if (options.disableGlob || !glob.hasMagic(p))
62 return afterGlob(null, [p])
64 options.lstat(p, function (er, stat) {
66 return afterGlob(null, [p])
68 glob(p, options.glob, afterGlob)
72 errState = errState || er
77 function afterGlob (er, results) {
85 results.forEach(function (p) {
86 rimraf_(p, options, function CB (er) {
88 if (isWindows && (er.code === "EBUSY" || er.code === "ENOTEMPTY" || er.code === "EPERM") &&
89 busyTries < options.maxBusyTries) {
91 var time = busyTries * 100
92 // try again, with the same exact callback as this one.
93 return setTimeout(function () {
94 rimraf_(p, options, CB)
98 // this one won't happen if graceful-fs is used.
99 if (er.code === "EMFILE" && timeout < options.emfileWait) {
100 return setTimeout(function () {
101 rimraf_(p, options, CB)
106 if (er.code === "ENOENT") er = null
116 // Two possible strategies.
117 // 1. Assume it's a file. unlink it, then do the dir stuff on EPERM or EISDIR
118 // 2. Assume it's a directory. readdir, then do the file stuff on ENOTDIR
120 // Both result in an extra syscall when you guess wrong. However, there
121 // are likely far more normal files in the world than directories. This
122 // is based on the assumption that a the average number of files per
123 // directory is >= 1.
125 // If anyone ever complains about this, then I guess the strategy could
126 // be made configurable somehow. But until then, YAGNI.
127 function rimraf_ (p, options, cb) {
130 assert(typeof cb === 'function')
132 // sunos lets the root user unlink directories, which is... weird.
133 // so we have to lstat here and make sure it's not a dir.
134 options.lstat(p, function (er, st) {
135 if (er && er.code === "ENOENT")
138 // Windows can EPERM on stat. Life is suffering.
139 if (er && er.code === "EPERM" && isWindows)
140 fixWinEPERM(p, options, er, cb)
142 if (st && st.isDirectory())
143 return rmdir(p, options, er, cb)
145 options.unlink(p, function (er) {
147 if (er.code === "ENOENT")
149 if (er.code === "EPERM")
151 ? fixWinEPERM(p, options, er, cb)
152 : rmdir(p, options, er, cb)
153 if (er.code === "EISDIR")
154 return rmdir(p, options, er, cb)
161 function fixWinEPERM (p, options, er, cb) {
164 assert(typeof cb === 'function')
166 assert(er instanceof Error)
168 options.chmod(p, 666, function (er2) {
170 cb(er2.code === "ENOENT" ? null : er)
172 options.stat(p, function(er3, stats) {
174 cb(er3.code === "ENOENT" ? null : er)
175 else if (stats.isDirectory())
176 rmdir(p, options, er, cb)
178 options.unlink(p, cb)
183 function fixWinEPERMSync (p, options, er) {
187 assert(er instanceof Error)
190 options.chmodSync(p, 666)
192 if (er2.code === "ENOENT")
199 var stats = options.statSync(p)
201 if (er3.code === "ENOENT")
207 if (stats.isDirectory())
208 rmdirSync(p, options, er)
210 options.unlinkSync(p)
213 function rmdir (p, options, originalEr, cb) {
217 assert(originalEr instanceof Error)
218 assert(typeof cb === 'function')
220 // try to rmdir first, and only readdir on ENOTEMPTY or EEXIST (SunOS)
221 // if we guessed wrong, and it's not a directory, then
222 // raise the original error.
223 options.rmdir(p, function (er) {
224 if (er && (er.code === "ENOTEMPTY" || er.code === "EEXIST" || er.code === "EPERM"))
225 rmkids(p, options, cb)
226 else if (er && er.code === "ENOTDIR")
233 function rmkids(p, options, cb) {
236 assert(typeof cb === 'function')
238 options.readdir(p, function (er, files) {
243 return options.rmdir(p, cb)
245 files.forEach(function (f) {
246 rimraf(path.join(p, f), options, function (er) {
250 return cb(errState = er)
258 // this looks simpler, and is strictly *faster*, but will
259 // tie up the JavaScript thread and fail on excessively
260 // deep directory trees.
261 function rimrafSync (p, options) {
262 options = options || {}
265 assert(p, 'rimraf: missing path')
266 assert.equal(typeof p, 'string', 'rimraf: path should be a string')
267 assert(options, 'rimraf: missing options')
268 assert.equal(typeof options, 'object', 'rimraf: options should be object')
272 if (options.disableGlob || !glob.hasMagic(p)) {
279 results = glob.sync(p, options.glob)
286 for (var i = 0; i < results.length; i++) {
290 var st = options.lstatSync(p)
292 if (er.code === "ENOENT")
295 // Windows can EPERM on stat. Life is suffering.
296 if (er.code === "EPERM" && isWindows)
297 fixWinEPERMSync(p, options, er)
301 // sunos lets the root user unlink directories, which is... weird.
302 if (st && st.isDirectory())
303 rmdirSync(p, options, null)
305 options.unlinkSync(p)
307 if (er.code === "ENOENT")
309 if (er.code === "EPERM")
310 return isWindows ? fixWinEPERMSync(p, options, er) : rmdirSync(p, options, er)
311 if (er.code !== "EISDIR")
313 rmdirSync(p, options, er)
318 function rmdirSync (p, options, originalEr) {
322 assert(originalEr instanceof Error)
327 if (er.code === "ENOENT")
329 if (er.code === "ENOTDIR")
331 if (er.code === "ENOTEMPTY" || er.code === "EEXIST" || er.code === "EPERM")
332 rmkidsSync(p, options)
336 function rmkidsSync (p, options) {
339 options.readdirSync(p).forEach(function (f) {
340 rimrafSync(path.join(p, f), options)
342 options.rmdirSync(p, options)