The data contained in this repository can be downloaded to your computer using one of several clients.
Please see the documentation of your version control software client for more information.

Please select the desired protocol below to get the URL.

This URL has Read-Only access.

Statistics
| Branch: | Revision:

main_repo / deps / npm / node_modules / rimraf / rimraf.js @ 5aef65a9

History | View | Annotate | Download (3.23 KB)

1
module.exports = rimraf
2
rimraf.sync = rimrafSync
3

    
4
var path = require("path")
5
  , fs
6

    
7
try {
8
  // optional dependency
9
  fs = require("graceful-fs")
10
} catch (er) {
11
  fs = require("fs")
12
}
13

    
14
// for EMFILE handling
15
var timeout = 0
16
exports.EMFILE_MAX = 1000
17
exports.BUSYTRIES_MAX = 3
18

    
19
function rimraf (p, cb) {
20
  if (!cb) throw new Error("No callback passed to rimraf()")
21

    
22
  var busyTries = 0
23
  rimraf_(p, function CB (er) {
24
    if (er) {
25
      if (er.code === "EBUSY" && busyTries < exports.BUSYTRIES_MAX) {
26
        busyTries ++
27
        var time = busyTries * 100
28
        // try again, with the same exact callback as this one.
29
        return setTimeout(function () {
30
          rimraf_(p, CB)
31
        }, time)
32
      }
33

    
34
      // this one won't happen if graceful-fs is used.
35
      if (er.code === "EMFILE" && timeout < exports.EMFILE_MAX) {
36
        return setTimeout(function () {
37
          rimraf_(p, CB)
38
        }, timeout ++)
39
      }
40

    
41
      // already gone
42
      if (er.code === "ENOENT") er = null
43
    }
44

    
45
    timeout = 0
46
    cb(er)
47
  })
48
}
49

    
50
// Two possible strategies.
51
// 1. Assume it's a file.  unlink it, then do the dir stuff on EPERM or EISDIR
52
// 2. Assume it's a directory.  readdir, then do the file stuff on ENOTDIR
53
//
54
// Both result in an extra syscall when you guess wrong.  However, there
55
// are likely far more normal files in the world than directories.  This
56
// is based on the assumption that a the average number of files per
57
// directory is >= 1.
58
//
59
// If anyone ever complains about this, then I guess the strategy could
60
// be made configurable somehow.  But until then, YAGNI.
61
function rimraf_ (p, cb) {
62
  fs.unlink(p, function (er) {
63
    if (er && er.code === "ENOENT")
64
      return cb()
65
    if (er && (er.code === "EPERM" || er.code === "EISDIR"))
66
      return rmdir(p, er, cb)
67
    return cb(er)
68
  })
69
}
70

    
71
function rmdir (p, originalEr, cb) {
72
  // try to rmdir first, and only readdir on ENOTEMPTY or EEXIST (SunOS)
73
  // if we guessed wrong, and it's not a directory, then
74
  // raise the original error.
75
  fs.rmdir(p, function (er) {
76
    if (er && (er.code === "ENOTEMPTY" || er.code === "EEXIST"))
77
      rmkids(p, cb)
78
    else if (er && er.code === "ENOTDIR")
79
      cb(originalEr)
80
    else
81
      cb(er)
82
  })
83
}
84

    
85
function rmkids(p, cb) {
86
  fs.readdir(p, function (er, files) {
87
    if (er)
88
      return cb(er)
89
    var n = files.length
90
    if (n === 0)
91
      return fs.rmdir(p, cb)
92
    var errState
93
    files.forEach(function (f) {
94
      rimraf(path.join(p, f), function (er) {
95
        if (errState)
96
          return
97
        if (er)
98
          return cb(errState = er)
99
        if (--n === 0)
100
          fs.rmdir(p, cb)
101
      })
102
    })
103
  })
104
}
105

    
106
// this looks simpler, and is strictly *faster*, but will
107
// tie up the JavaScript thread and fail on excessively
108
// deep directory trees.
109
function rimrafSync (p) {
110
  try {
111
    fs.unlinkSync(p)
112
  } catch (er) {
113
    if (er.code === "ENOENT")
114
      return
115
    if (er.code !== "EPERM" && er.code !== "EISDIR")
116
      throw er
117
    try {
118
      fs.rmdirSync(p)
119
    } catch (er2) {
120
      if (er2.code === "ENOENT")
121
        return
122
      if (er2.code === "ENOTDIR")
123
        throw er
124
      if (er2.code === "ENOTEMPTY") {
125
        fs.readdirSync(p).forEach(function (f) {
126
          rimrafSync(path.join(p, f))
127
        })
128
        fs.rmdirSync(p)
129
      }
130
    }
131
  }
132
}