rimraf.js 6.9 KB

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