rimraf.js 7.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335
  1. module.exports = rimraf
  2. rimraf.sync = rimrafSync
  3. var assert = require("assert")
  4. var path = require("path")
  5. var fs = require("fs")
  6. var glob = require("glob")
  7. var defaultGlobOpts = {
  8. nosort: true,
  9. silent: true
  10. }
  11. // for EMFILE handling
  12. var timeout = 0
  13. var isWindows = (process.platform === "win32")
  14. function defaults (options) {
  15. var methods = [
  16. 'unlink',
  17. 'chmod',
  18. 'stat',
  19. 'lstat',
  20. 'rmdir',
  21. 'readdir'
  22. ]
  23. methods.forEach(function(m) {
  24. options[m] = options[m] || fs[m]
  25. m = m + 'Sync'
  26. options[m] = options[m] || fs[m]
  27. })
  28. options.maxBusyTries = options.maxBusyTries || 3
  29. options.emfileWait = options.emfileWait || 1000
  30. if (options.glob === false) {
  31. options.disableGlob = true
  32. }
  33. options.disableGlob = options.disableGlob || false
  34. options.glob = options.glob || defaultGlobOpts
  35. }
  36. function rimraf (p, options, cb) {
  37. if (typeof options === 'function') {
  38. cb = options
  39. options = {}
  40. }
  41. assert(p, 'rimraf: missing path')
  42. assert.equal(typeof p, 'string', 'rimraf: path should be a string')
  43. assert(options, 'rimraf: missing options')
  44. assert.equal(typeof options, 'object', 'rimraf: options should be object')
  45. assert.equal(typeof cb, 'function', 'rimraf: callback function required')
  46. defaults(options)
  47. var busyTries = 0
  48. var errState = null
  49. var n = 0
  50. if (options.disableGlob || !glob.hasMagic(p))
  51. return afterGlob(null, [p])
  52. fs.lstat(p, function (er, stat) {
  53. if (!er)
  54. return afterGlob(null, [p])
  55. glob(p, options.glob, afterGlob)
  56. })
  57. function next (er) {
  58. errState = errState || er
  59. if (--n === 0)
  60. cb(errState)
  61. }
  62. function afterGlob (er, results) {
  63. if (er)
  64. return cb(er)
  65. n = results.length
  66. if (n === 0)
  67. return cb()
  68. results.forEach(function (p) {
  69. rimraf_(p, options, function CB (er) {
  70. if (er) {
  71. if (isWindows && (er.code === "EBUSY" || er.code === "ENOTEMPTY" || er.code === "EPERM") &&
  72. busyTries < options.maxBusyTries) {
  73. busyTries ++
  74. var time = busyTries * 100
  75. // try again, with the same exact callback as this one.
  76. return setTimeout(function () {
  77. rimraf_(p, options, CB)
  78. }, time)
  79. }
  80. // this one won't happen if graceful-fs is used.
  81. if (er.code === "EMFILE" && timeout < options.emfileWait) {
  82. return setTimeout(function () {
  83. rimraf_(p, options, CB)
  84. }, timeout ++)
  85. }
  86. // already gone
  87. if (er.code === "ENOENT") er = null
  88. }
  89. timeout = 0
  90. next(er)
  91. })
  92. })
  93. }
  94. }
  95. // Two possible strategies.
  96. // 1. Assume it's a file. unlink it, then do the dir stuff on EPERM or EISDIR
  97. // 2. Assume it's a directory. readdir, then do the file stuff on ENOTDIR
  98. //
  99. // Both result in an extra syscall when you guess wrong. However, there
  100. // are likely far more normal files in the world than directories. This
  101. // is based on the assumption that a the average number of files per
  102. // directory is >= 1.
  103. //
  104. // If anyone ever complains about this, then I guess the strategy could
  105. // be made configurable somehow. But until then, YAGNI.
  106. function rimraf_ (p, options, cb) {
  107. assert(p)
  108. assert(options)
  109. assert(typeof cb === 'function')
  110. // sunos lets the root user unlink directories, which is... weird.
  111. // so we have to lstat here and make sure it's not a dir.
  112. options.lstat(p, function (er, st) {
  113. if (er && er.code === "ENOENT")
  114. return cb(null)
  115. if (st && st.isDirectory())
  116. return rmdir(p, options, er, cb)
  117. options.unlink(p, function (er) {
  118. if (er) {
  119. if (er.code === "ENOENT")
  120. return cb(null)
  121. if (er.code === "EPERM")
  122. return (isWindows)
  123. ? fixWinEPERM(p, options, er, cb)
  124. : rmdir(p, options, er, cb)
  125. if (er.code === "EISDIR")
  126. return rmdir(p, options, er, cb)
  127. }
  128. return cb(er)
  129. })
  130. })
  131. }
  132. function fixWinEPERM (p, options, er, cb) {
  133. assert(p)
  134. assert(options)
  135. assert(typeof cb === 'function')
  136. if (er)
  137. assert(er instanceof Error)
  138. options.chmod(p, 666, function (er2) {
  139. if (er2)
  140. cb(er2.code === "ENOENT" ? null : er)
  141. else
  142. options.stat(p, function(er3, stats) {
  143. if (er3)
  144. cb(er3.code === "ENOENT" ? null : er)
  145. else if (stats.isDirectory())
  146. rmdir(p, options, er, cb)
  147. else
  148. options.unlink(p, cb)
  149. })
  150. })
  151. }
  152. function fixWinEPERMSync (p, options, er) {
  153. assert(p)
  154. assert(options)
  155. if (er)
  156. assert(er instanceof Error)
  157. try {
  158. options.chmodSync(p, 666)
  159. } catch (er2) {
  160. if (er2.code === "ENOENT")
  161. return
  162. else
  163. throw er
  164. }
  165. try {
  166. var stats = options.statSync(p)
  167. } catch (er3) {
  168. if (er3.code === "ENOENT")
  169. return
  170. else
  171. throw er
  172. }
  173. if (stats.isDirectory())
  174. rmdirSync(p, options, er)
  175. else
  176. options.unlinkSync(p)
  177. }
  178. function rmdir (p, options, originalEr, cb) {
  179. assert(p)
  180. assert(options)
  181. if (originalEr)
  182. assert(originalEr instanceof Error)
  183. assert(typeof cb === 'function')
  184. // try to rmdir first, and only readdir on ENOTEMPTY or EEXIST (SunOS)
  185. // if we guessed wrong, and it's not a directory, then
  186. // raise the original error.
  187. options.rmdir(p, function (er) {
  188. if (er && (er.code === "ENOTEMPTY" || er.code === "EEXIST" || er.code === "EPERM"))
  189. rmkids(p, options, cb)
  190. else if (er && er.code === "ENOTDIR")
  191. cb(originalEr)
  192. else
  193. cb(er)
  194. })
  195. }
  196. function rmkids(p, options, cb) {
  197. assert(p)
  198. assert(options)
  199. assert(typeof cb === 'function')
  200. options.readdir(p, function (er, files) {
  201. if (er)
  202. return cb(er)
  203. var n = files.length
  204. if (n === 0)
  205. return options.rmdir(p, cb)
  206. var errState
  207. files.forEach(function (f) {
  208. rimraf(path.join(p, f), options, function (er) {
  209. if (errState)
  210. return
  211. if (er)
  212. return cb(errState = er)
  213. if (--n === 0)
  214. options.rmdir(p, cb)
  215. })
  216. })
  217. })
  218. }
  219. // this looks simpler, and is strictly *faster*, but will
  220. // tie up the JavaScript thread and fail on excessively
  221. // deep directory trees.
  222. function rimrafSync (p, options) {
  223. options = options || {}
  224. defaults(options)
  225. assert(p, 'rimraf: missing path')
  226. assert.equal(typeof p, 'string', 'rimraf: path should be a string')
  227. assert(options, 'rimraf: missing options')
  228. assert.equal(typeof options, 'object', 'rimraf: options should be object')
  229. var results
  230. if (options.disableGlob || !glob.hasMagic(p)) {
  231. results = [p]
  232. } else {
  233. try {
  234. fs.lstatSync(p)
  235. results = [p]
  236. } catch (er) {
  237. results = glob.sync(p, options.glob)
  238. }
  239. }
  240. if (!results.length)
  241. return
  242. for (var i = 0; i < results.length; i++) {
  243. var p = results[i]
  244. try {
  245. var st = options.lstatSync(p)
  246. } catch (er) {
  247. if (er.code === "ENOENT")
  248. return
  249. }
  250. try {
  251. // sunos lets the root user unlink directories, which is... weird.
  252. if (st && st.isDirectory())
  253. rmdirSync(p, options, null)
  254. else
  255. options.unlinkSync(p)
  256. } catch (er) {
  257. if (er.code === "ENOENT")
  258. return
  259. if (er.code === "EPERM")
  260. return isWindows ? fixWinEPERMSync(p, options, er) : rmdirSync(p, options, er)
  261. if (er.code !== "EISDIR")
  262. throw er
  263. rmdirSync(p, options, er)
  264. }
  265. }
  266. }
  267. function rmdirSync (p, options, originalEr) {
  268. assert(p)
  269. assert(options)
  270. if (originalEr)
  271. assert(originalEr instanceof Error)
  272. try {
  273. options.rmdirSync(p)
  274. } catch (er) {
  275. if (er.code === "ENOENT")
  276. return
  277. if (er.code === "ENOTDIR")
  278. throw originalEr
  279. if (er.code === "ENOTEMPTY" || er.code === "EEXIST" || er.code === "EPERM")
  280. rmkidsSync(p, options)
  281. }
  282. }
  283. function rmkidsSync (p, options) {
  284. assert(p)
  285. assert(options)
  286. options.readdirSync(p).forEach(function (f) {
  287. rimrafSync(path.join(p, f), options)
  288. })
  289. options.rmdirSync(p, options)
  290. }