rimraf.js 8.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343
  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.equal(typeof cb, 'function', 'rimraf: callback function required')
  44. assert(options, 'rimraf: invalid options argument provided')
  45. assert.equal(typeof options, 'object', 'rimraf: options should be object')
  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. options.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. // Windows can EPERM on stat. Life is suffering.
  116. if (er && er.code === "EPERM" && isWindows)
  117. fixWinEPERM(p, options, er, cb)
  118. if (st && st.isDirectory())
  119. return rmdir(p, options, er, cb)
  120. options.unlink(p, function (er) {
  121. if (er) {
  122. if (er.code === "ENOENT")
  123. return cb(null)
  124. if (er.code === "EPERM")
  125. return (isWindows)
  126. ? fixWinEPERM(p, options, er, cb)
  127. : rmdir(p, options, er, cb)
  128. if (er.code === "EISDIR")
  129. return rmdir(p, options, er, cb)
  130. }
  131. return cb(er)
  132. })
  133. })
  134. }
  135. function fixWinEPERM (p, options, er, cb) {
  136. assert(p)
  137. assert(options)
  138. assert(typeof cb === 'function')
  139. if (er)
  140. assert(er instanceof Error)
  141. options.chmod(p, 666, function (er2) {
  142. if (er2)
  143. cb(er2.code === "ENOENT" ? null : er)
  144. else
  145. options.stat(p, function(er3, stats) {
  146. if (er3)
  147. cb(er3.code === "ENOENT" ? null : er)
  148. else if (stats.isDirectory())
  149. rmdir(p, options, er, cb)
  150. else
  151. options.unlink(p, cb)
  152. })
  153. })
  154. }
  155. function fixWinEPERMSync (p, options, er) {
  156. assert(p)
  157. assert(options)
  158. if (er)
  159. assert(er instanceof Error)
  160. try {
  161. options.chmodSync(p, 666)
  162. } catch (er2) {
  163. if (er2.code === "ENOENT")
  164. return
  165. else
  166. throw er
  167. }
  168. try {
  169. var stats = options.statSync(p)
  170. } catch (er3) {
  171. if (er3.code === "ENOENT")
  172. return
  173. else
  174. throw er
  175. }
  176. if (stats.isDirectory())
  177. rmdirSync(p, options, er)
  178. else
  179. options.unlinkSync(p)
  180. }
  181. function rmdir (p, options, originalEr, cb) {
  182. assert(p)
  183. assert(options)
  184. if (originalEr)
  185. assert(originalEr instanceof Error)
  186. assert(typeof cb === 'function')
  187. // try to rmdir first, and only readdir on ENOTEMPTY or EEXIST (SunOS)
  188. // if we guessed wrong, and it's not a directory, then
  189. // raise the original error.
  190. options.rmdir(p, function (er) {
  191. if (er && (er.code === "ENOTEMPTY" || er.code === "EEXIST" || er.code === "EPERM"))
  192. rmkids(p, options, cb)
  193. else if (er && er.code === "ENOTDIR")
  194. cb(originalEr)
  195. else
  196. cb(er)
  197. })
  198. }
  199. function rmkids(p, options, cb) {
  200. assert(p)
  201. assert(options)
  202. assert(typeof cb === 'function')
  203. options.readdir(p, function (er, files) {
  204. if (er)
  205. return cb(er)
  206. var n = files.length
  207. if (n === 0)
  208. return options.rmdir(p, cb)
  209. var errState
  210. files.forEach(function (f) {
  211. rimraf(path.join(p, f), options, function (er) {
  212. if (errState)
  213. return
  214. if (er)
  215. return cb(errState = er)
  216. if (--n === 0)
  217. options.rmdir(p, cb)
  218. })
  219. })
  220. })
  221. }
  222. // this looks simpler, and is strictly *faster*, but will
  223. // tie up the JavaScript thread and fail on excessively
  224. // deep directory trees.
  225. function rimrafSync (p, options) {
  226. options = options || {}
  227. defaults(options)
  228. assert(p, 'rimraf: missing path')
  229. assert.equal(typeof p, 'string', 'rimraf: path should be a string')
  230. assert(options, 'rimraf: missing options')
  231. assert.equal(typeof options, 'object', 'rimraf: options should be object')
  232. var results
  233. if (options.disableGlob || !glob.hasMagic(p)) {
  234. results = [p]
  235. } else {
  236. try {
  237. options.lstatSync(p)
  238. results = [p]
  239. } catch (er) {
  240. results = glob.sync(p, options.glob)
  241. }
  242. }
  243. if (!results.length)
  244. return
  245. for (var i = 0; i < results.length; i++) {
  246. var p = results[i]
  247. try {
  248. var st = options.lstatSync(p)
  249. } catch (er) {
  250. if (er.code === "ENOENT")
  251. return
  252. // Windows can EPERM on stat. Life is suffering.
  253. if (er.code === "EPERM" && isWindows)
  254. fixWinEPERMSync(p, options, er)
  255. }
  256. try {
  257. // sunos lets the root user unlink directories, which is... weird.
  258. if (st && st.isDirectory())
  259. rmdirSync(p, options, null)
  260. else
  261. options.unlinkSync(p)
  262. } catch (er) {
  263. if (er.code === "ENOENT")
  264. return
  265. if (er.code === "EPERM")
  266. return isWindows ? fixWinEPERMSync(p, options, er) : rmdirSync(p, options, er)
  267. if (er.code !== "EISDIR")
  268. throw er
  269. rmdirSync(p, options, er)
  270. }
  271. }
  272. }
  273. function rmdirSync (p, options, originalEr) {
  274. assert(p)
  275. assert(options)
  276. if (originalEr)
  277. assert(originalEr instanceof Error)
  278. try {
  279. options.rmdirSync(p)
  280. } catch (er) {
  281. if (er.code === "ENOENT")
  282. return
  283. if (er.code === "ENOTDIR")
  284. throw originalEr
  285. if (er.code === "ENOTEMPTY" || er.code === "EEXIST" || er.code === "EPERM")
  286. rmkidsSync(p, options)
  287. }
  288. }
  289. function rmkidsSync (p, options) {
  290. assert(p)
  291. assert(options)
  292. options.readdirSync(p).forEach(function (f) {
  293. rimrafSync(path.join(p, f), options)
  294. })
  295. options.rmdirSync(p, options)
  296. }