rimraf.js 5.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248
  1. module.exports = rimraf
  2. rimraf.sync = rimrafSync
  3. var assert = require("assert")
  4. var path = require("path")
  5. var fs = require("fs")
  6. // for EMFILE handling
  7. var timeout = 0
  8. exports.EMFILE_MAX = 1000
  9. exports.BUSYTRIES_MAX = 3
  10. var isWindows = (process.platform === "win32")
  11. function defaults (options) {
  12. var methods = [
  13. 'unlink',
  14. 'chmod',
  15. 'stat',
  16. 'rmdir',
  17. 'readdir'
  18. ]
  19. methods.forEach(function(m) {
  20. options[m] = options[m] || fs[m]
  21. m = m + 'Sync'
  22. options[m] = options[m] || fs[m]
  23. })
  24. }
  25. function rimraf (p, options, cb) {
  26. if (typeof options === 'function') {
  27. cb = options
  28. options = {}
  29. }
  30. assert(p)
  31. assert(options)
  32. assert(typeof cb === 'function')
  33. defaults(options)
  34. if (!cb) throw new Error("No callback passed to rimraf()")
  35. var busyTries = 0
  36. rimraf_(p, options, function CB (er) {
  37. if (er) {
  38. if (isWindows && (er.code === "EBUSY" || er.code === "ENOTEMPTY") &&
  39. busyTries < exports.BUSYTRIES_MAX) {
  40. busyTries ++
  41. var time = busyTries * 100
  42. // try again, with the same exact callback as this one.
  43. return setTimeout(function () {
  44. rimraf_(p, options, CB)
  45. }, time)
  46. }
  47. // this one won't happen if graceful-fs is used.
  48. if (er.code === "EMFILE" && timeout < exports.EMFILE_MAX) {
  49. return setTimeout(function () {
  50. rimraf_(p, options, CB)
  51. }, timeout ++)
  52. }
  53. // already gone
  54. if (er.code === "ENOENT") er = null
  55. }
  56. timeout = 0
  57. cb(er)
  58. })
  59. }
  60. // Two possible strategies.
  61. // 1. Assume it's a file. unlink it, then do the dir stuff on EPERM or EISDIR
  62. // 2. Assume it's a directory. readdir, then do the file stuff on ENOTDIR
  63. //
  64. // Both result in an extra syscall when you guess wrong. However, there
  65. // are likely far more normal files in the world than directories. This
  66. // is based on the assumption that a the average number of files per
  67. // directory is >= 1.
  68. //
  69. // If anyone ever complains about this, then I guess the strategy could
  70. // be made configurable somehow. But until then, YAGNI.
  71. function rimraf_ (p, options, cb) {
  72. assert(p)
  73. assert(options)
  74. assert(typeof cb === 'function')
  75. options.unlink(p, function (er) {
  76. if (er) {
  77. if (er.code === "ENOENT")
  78. return cb(null)
  79. if (er.code === "EPERM")
  80. return (isWindows)
  81. ? fixWinEPERM(p, options, er, cb)
  82. : rmdir(p, options, er, cb)
  83. if (er.code === "EISDIR")
  84. return rmdir(p, options, er, cb)
  85. }
  86. return cb(er)
  87. })
  88. }
  89. function fixWinEPERM (p, options, er, cb) {
  90. assert(p)
  91. assert(options)
  92. assert(typeof cb === 'function')
  93. if (er)
  94. assert(er instanceof Error)
  95. options.chmod(p, 666, function (er2) {
  96. if (er2)
  97. cb(er2.code === "ENOENT" ? null : er)
  98. else
  99. options.stat(p, function(er3, stats) {
  100. if (er3)
  101. cb(er3.code === "ENOENT" ? null : er)
  102. else if (stats.isDirectory())
  103. rmdir(p, options, er, cb)
  104. else
  105. options.unlink(p, cb)
  106. })
  107. })
  108. }
  109. function fixWinEPERMSync (p, options, er) {
  110. assert(p)
  111. assert(options)
  112. if (er)
  113. assert(er instanceof Error)
  114. try {
  115. options.chmodSync(p, 666)
  116. } catch (er2) {
  117. if (er2.code === "ENOENT")
  118. return
  119. else
  120. throw er
  121. }
  122. try {
  123. var stats = options.statSync(p)
  124. } catch (er3) {
  125. if (er3.code === "ENOENT")
  126. return
  127. else
  128. throw er
  129. }
  130. if (stats.isDirectory())
  131. rmdirSync(p, options, er)
  132. else
  133. options.unlinkSync(p)
  134. }
  135. function rmdir (p, options, originalEr, cb) {
  136. assert(p)
  137. assert(options)
  138. if (originalEr)
  139. assert(originalEr instanceof Error)
  140. assert(typeof cb === 'function')
  141. // try to rmdir first, and only readdir on ENOTEMPTY or EEXIST (SunOS)
  142. // if we guessed wrong, and it's not a directory, then
  143. // raise the original error.
  144. options.rmdir(p, function (er) {
  145. if (er && (er.code === "ENOTEMPTY" || er.code === "EEXIST" || er.code === "EPERM"))
  146. rmkids(p, options, cb)
  147. else if (er && er.code === "ENOTDIR")
  148. cb(originalEr)
  149. else
  150. cb(er)
  151. })
  152. }
  153. function rmkids(p, options, cb) {
  154. assert(p)
  155. assert(options)
  156. assert(typeof cb === 'function')
  157. options.readdir(p, function (er, files) {
  158. if (er)
  159. return cb(er)
  160. var n = files.length
  161. if (n === 0)
  162. return options.rmdir(p, cb)
  163. var errState
  164. files.forEach(function (f) {
  165. rimraf(path.join(p, f), options, function (er) {
  166. if (errState)
  167. return
  168. if (er)
  169. return cb(errState = er)
  170. if (--n === 0)
  171. options.rmdir(p, cb)
  172. })
  173. })
  174. })
  175. }
  176. // this looks simpler, and is strictly *faster*, but will
  177. // tie up the JavaScript thread and fail on excessively
  178. // deep directory trees.
  179. function rimrafSync (p, options) {
  180. options = options || {}
  181. defaults(options)
  182. assert(p)
  183. assert(options)
  184. try {
  185. options.unlinkSync(p)
  186. } catch (er) {
  187. if (er.code === "ENOENT")
  188. return
  189. if (er.code === "EPERM")
  190. return isWindows ? fixWinEPERMSync(p, options, er) : rmdirSync(p, options, er)
  191. if (er.code !== "EISDIR")
  192. throw er
  193. rmdirSync(p, options, er)
  194. }
  195. }
  196. function rmdirSync (p, options, originalEr) {
  197. assert(p)
  198. assert(options)
  199. if (originalEr)
  200. assert(originalEr instanceof Error)
  201. try {
  202. options.rmdirSync(p)
  203. } catch (er) {
  204. if (er.code === "ENOENT")
  205. return
  206. if (er.code === "ENOTDIR")
  207. throw originalEr
  208. if (er.code === "ENOTEMPTY" || er.code === "EEXIST" || er.code === "EPERM")
  209. rmkidsSync(p, options)
  210. }
  211. }
  212. function rmkidsSync (p, options) {
  213. assert(p)
  214. assert(options)
  215. options.readdirSync(p).forEach(function (f) {
  216. rimrafSync(path.join(p, f), options)
  217. })
  218. options.rmdirSync(p, options)
  219. }