2021-02-26 04:58:33 +01:00
|
|
|
const assert = require("assert")
|
|
|
|
const path = require("path")
|
|
|
|
const fs = require("fs")
|
|
|
|
let glob = undefined
|
|
|
|
try {
|
|
|
|
glob = require("glob")
|
|
|
|
} catch (_err) {
|
|
|
|
// treat glob as optional.
|
|
|
|
}
|
2020-08-26 01:57:08 +02:00
|
|
|
|
2021-02-26 04:58:33 +01:00
|
|
|
const defaultGlobOpts = {
|
2020-08-26 01:57:08 +02:00
|
|
|
nosort: true,
|
|
|
|
silent: true
|
|
|
|
}
|
|
|
|
|
|
|
|
// for EMFILE handling
|
2021-02-26 04:58:33 +01:00
|
|
|
let timeout = 0
|
2020-08-26 01:57:08 +02:00
|
|
|
|
2021-02-26 04:58:33 +01:00
|
|
|
const isWindows = (process.platform === "win32")
|
2020-08-26 01:57:08 +02:00
|
|
|
|
2021-02-26 04:58:33 +01:00
|
|
|
const defaults = options => {
|
|
|
|
const methods = [
|
2020-08-26 01:57:08 +02:00
|
|
|
'unlink',
|
|
|
|
'chmod',
|
|
|
|
'stat',
|
|
|
|
'lstat',
|
|
|
|
'rmdir',
|
|
|
|
'readdir'
|
|
|
|
]
|
2021-02-26 04:58:33 +01:00
|
|
|
methods.forEach(m => {
|
2020-08-26 01:57:08 +02:00
|
|
|
options[m] = options[m] || fs[m]
|
|
|
|
m = m + 'Sync'
|
|
|
|
options[m] = options[m] || fs[m]
|
|
|
|
})
|
|
|
|
|
|
|
|
options.maxBusyTries = options.maxBusyTries || 3
|
|
|
|
options.emfileWait = options.emfileWait || 1000
|
|
|
|
if (options.glob === false) {
|
|
|
|
options.disableGlob = true
|
|
|
|
}
|
2021-02-26 04:58:33 +01:00
|
|
|
if (options.disableGlob !== true && glob === undefined) {
|
|
|
|
throw Error('glob dependency not found, set `options.disableGlob = true` if intentional')
|
|
|
|
}
|
2020-08-26 01:57:08 +02:00
|
|
|
options.disableGlob = options.disableGlob || false
|
|
|
|
options.glob = options.glob || defaultGlobOpts
|
|
|
|
}
|
|
|
|
|
2021-02-26 04:58:33 +01:00
|
|
|
const rimraf = (p, options, cb) => {
|
2020-08-26 01:57:08 +02:00
|
|
|
if (typeof options === 'function') {
|
|
|
|
cb = options
|
|
|
|
options = {}
|
|
|
|
}
|
|
|
|
|
|
|
|
assert(p, 'rimraf: missing path')
|
|
|
|
assert.equal(typeof p, 'string', 'rimraf: path should be a string')
|
|
|
|
assert.equal(typeof cb, 'function', 'rimraf: callback function required')
|
|
|
|
assert(options, 'rimraf: invalid options argument provided')
|
|
|
|
assert.equal(typeof options, 'object', 'rimraf: options should be object')
|
|
|
|
|
|
|
|
defaults(options)
|
|
|
|
|
2021-02-26 04:58:33 +01:00
|
|
|
let busyTries = 0
|
|
|
|
let errState = null
|
|
|
|
let n = 0
|
2020-08-26 01:57:08 +02:00
|
|
|
|
2021-02-26 04:58:33 +01:00
|
|
|
const next = (er) => {
|
2020-08-26 01:57:08 +02:00
|
|
|
errState = errState || er
|
|
|
|
if (--n === 0)
|
|
|
|
cb(errState)
|
|
|
|
}
|
|
|
|
|
2021-02-26 04:58:33 +01:00
|
|
|
const afterGlob = (er, results) => {
|
2020-08-26 01:57:08 +02:00
|
|
|
if (er)
|
|
|
|
return cb(er)
|
|
|
|
|
|
|
|
n = results.length
|
|
|
|
if (n === 0)
|
|
|
|
return cb()
|
|
|
|
|
2021-02-26 04:58:33 +01:00
|
|
|
results.forEach(p => {
|
|
|
|
const CB = (er) => {
|
2020-08-26 01:57:08 +02:00
|
|
|
if (er) {
|
|
|
|
if ((er.code === "EBUSY" || er.code === "ENOTEMPTY" || er.code === "EPERM") &&
|
|
|
|
busyTries < options.maxBusyTries) {
|
|
|
|
busyTries ++
|
|
|
|
// try again, with the same exact callback as this one.
|
2021-02-26 04:58:33 +01:00
|
|
|
return setTimeout(() => rimraf_(p, options, CB), busyTries * 100)
|
2020-08-26 01:57:08 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
// this one won't happen if graceful-fs is used.
|
|
|
|
if (er.code === "EMFILE" && timeout < options.emfileWait) {
|
2021-02-26 04:58:33 +01:00
|
|
|
return setTimeout(() => rimraf_(p, options, CB), timeout ++)
|
2020-08-26 01:57:08 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
// already gone
|
|
|
|
if (er.code === "ENOENT") er = null
|
|
|
|
}
|
|
|
|
|
|
|
|
timeout = 0
|
|
|
|
next(er)
|
2021-02-26 04:58:33 +01:00
|
|
|
}
|
|
|
|
rimraf_(p, options, CB)
|
2020-08-26 01:57:08 +02:00
|
|
|
})
|
|
|
|
}
|
2021-02-26 04:58:33 +01:00
|
|
|
|
|
|
|
if (options.disableGlob || !glob.hasMagic(p))
|
|
|
|
return afterGlob(null, [p])
|
|
|
|
|
|
|
|
options.lstat(p, (er, stat) => {
|
|
|
|
if (!er)
|
|
|
|
return afterGlob(null, [p])
|
|
|
|
|
|
|
|
glob(p, options.glob, afterGlob)
|
|
|
|
})
|
|
|
|
|
2020-08-26 01:57:08 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
// Two possible strategies.
|
|
|
|
// 1. Assume it's a file. unlink it, then do the dir stuff on EPERM or EISDIR
|
|
|
|
// 2. Assume it's a directory. readdir, then do the file stuff on ENOTDIR
|
|
|
|
//
|
|
|
|
// Both result in an extra syscall when you guess wrong. However, there
|
|
|
|
// are likely far more normal files in the world than directories. This
|
|
|
|
// is based on the assumption that a the average number of files per
|
|
|
|
// directory is >= 1.
|
|
|
|
//
|
|
|
|
// If anyone ever complains about this, then I guess the strategy could
|
|
|
|
// be made configurable somehow. But until then, YAGNI.
|
2021-02-26 04:58:33 +01:00
|
|
|
const rimraf_ = (p, options, cb) => {
|
2020-08-26 01:57:08 +02:00
|
|
|
assert(p)
|
|
|
|
assert(options)
|
|
|
|
assert(typeof cb === 'function')
|
|
|
|
|
|
|
|
// sunos lets the root user unlink directories, which is... weird.
|
|
|
|
// so we have to lstat here and make sure it's not a dir.
|
2021-02-26 04:58:33 +01:00
|
|
|
options.lstat(p, (er, st) => {
|
2020-08-26 01:57:08 +02:00
|
|
|
if (er && er.code === "ENOENT")
|
|
|
|
return cb(null)
|
|
|
|
|
|
|
|
// Windows can EPERM on stat. Life is suffering.
|
|
|
|
if (er && er.code === "EPERM" && isWindows)
|
|
|
|
fixWinEPERM(p, options, er, cb)
|
|
|
|
|
|
|
|
if (st && st.isDirectory())
|
|
|
|
return rmdir(p, options, er, cb)
|
|
|
|
|
2021-02-26 04:58:33 +01:00
|
|
|
options.unlink(p, er => {
|
2020-08-26 01:57:08 +02:00
|
|
|
if (er) {
|
|
|
|
if (er.code === "ENOENT")
|
|
|
|
return cb(null)
|
|
|
|
if (er.code === "EPERM")
|
|
|
|
return (isWindows)
|
|
|
|
? fixWinEPERM(p, options, er, cb)
|
|
|
|
: rmdir(p, options, er, cb)
|
|
|
|
if (er.code === "EISDIR")
|
|
|
|
return rmdir(p, options, er, cb)
|
|
|
|
}
|
|
|
|
return cb(er)
|
|
|
|
})
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
2021-02-26 04:58:33 +01:00
|
|
|
const fixWinEPERM = (p, options, er, cb) => {
|
2020-08-26 01:57:08 +02:00
|
|
|
assert(p)
|
|
|
|
assert(options)
|
|
|
|
assert(typeof cb === 'function')
|
|
|
|
|
2021-02-26 04:58:33 +01:00
|
|
|
options.chmod(p, 0o666, er2 => {
|
2020-08-26 01:57:08 +02:00
|
|
|
if (er2)
|
|
|
|
cb(er2.code === "ENOENT" ? null : er)
|
|
|
|
else
|
2021-02-26 04:58:33 +01:00
|
|
|
options.stat(p, (er3, stats) => {
|
2020-08-26 01:57:08 +02:00
|
|
|
if (er3)
|
|
|
|
cb(er3.code === "ENOENT" ? null : er)
|
|
|
|
else if (stats.isDirectory())
|
|
|
|
rmdir(p, options, er, cb)
|
|
|
|
else
|
|
|
|
options.unlink(p, cb)
|
|
|
|
})
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
2021-02-26 04:58:33 +01:00
|
|
|
const fixWinEPERMSync = (p, options, er) => {
|
2020-08-26 01:57:08 +02:00
|
|
|
assert(p)
|
|
|
|
assert(options)
|
|
|
|
|
|
|
|
try {
|
2021-02-26 04:58:33 +01:00
|
|
|
options.chmodSync(p, 0o666)
|
2020-08-26 01:57:08 +02:00
|
|
|
} catch (er2) {
|
|
|
|
if (er2.code === "ENOENT")
|
|
|
|
return
|
|
|
|
else
|
|
|
|
throw er
|
|
|
|
}
|
|
|
|
|
2021-02-26 04:58:33 +01:00
|
|
|
let stats
|
2020-08-26 01:57:08 +02:00
|
|
|
try {
|
2021-02-26 04:58:33 +01:00
|
|
|
stats = options.statSync(p)
|
2020-08-26 01:57:08 +02:00
|
|
|
} catch (er3) {
|
|
|
|
if (er3.code === "ENOENT")
|
|
|
|
return
|
|
|
|
else
|
|
|
|
throw er
|
|
|
|
}
|
|
|
|
|
|
|
|
if (stats.isDirectory())
|
|
|
|
rmdirSync(p, options, er)
|
|
|
|
else
|
|
|
|
options.unlinkSync(p)
|
|
|
|
}
|
|
|
|
|
2021-02-26 04:58:33 +01:00
|
|
|
const rmdir = (p, options, originalEr, cb) => {
|
2020-08-26 01:57:08 +02:00
|
|
|
assert(p)
|
|
|
|
assert(options)
|
|
|
|
assert(typeof cb === 'function')
|
|
|
|
|
|
|
|
// try to rmdir first, and only readdir on ENOTEMPTY or EEXIST (SunOS)
|
|
|
|
// if we guessed wrong, and it's not a directory, then
|
|
|
|
// raise the original error.
|
2021-02-26 04:58:33 +01:00
|
|
|
options.rmdir(p, er => {
|
2020-08-26 01:57:08 +02:00
|
|
|
if (er && (er.code === "ENOTEMPTY" || er.code === "EEXIST" || er.code === "EPERM"))
|
|
|
|
rmkids(p, options, cb)
|
|
|
|
else if (er && er.code === "ENOTDIR")
|
|
|
|
cb(originalEr)
|
|
|
|
else
|
|
|
|
cb(er)
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
2021-02-26 04:58:33 +01:00
|
|
|
const rmkids = (p, options, cb) => {
|
2020-08-26 01:57:08 +02:00
|
|
|
assert(p)
|
|
|
|
assert(options)
|
|
|
|
assert(typeof cb === 'function')
|
|
|
|
|
2021-02-26 04:58:33 +01:00
|
|
|
options.readdir(p, (er, files) => {
|
2020-08-26 01:57:08 +02:00
|
|
|
if (er)
|
|
|
|
return cb(er)
|
2021-02-26 04:58:33 +01:00
|
|
|
let n = files.length
|
2020-08-26 01:57:08 +02:00
|
|
|
if (n === 0)
|
|
|
|
return options.rmdir(p, cb)
|
2021-02-26 04:58:33 +01:00
|
|
|
let errState
|
|
|
|
files.forEach(f => {
|
|
|
|
rimraf(path.join(p, f), options, er => {
|
2020-08-26 01:57:08 +02:00
|
|
|
if (errState)
|
|
|
|
return
|
|
|
|
if (er)
|
|
|
|
return cb(errState = er)
|
|
|
|
if (--n === 0)
|
|
|
|
options.rmdir(p, cb)
|
|
|
|
})
|
|
|
|
})
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
|
|
|
// this looks simpler, and is strictly *faster*, but will
|
|
|
|
// tie up the JavaScript thread and fail on excessively
|
|
|
|
// deep directory trees.
|
2021-02-26 04:58:33 +01:00
|
|
|
const rimrafSync = (p, options) => {
|
2020-08-26 01:57:08 +02:00
|
|
|
options = options || {}
|
|
|
|
defaults(options)
|
|
|
|
|
|
|
|
assert(p, 'rimraf: missing path')
|
|
|
|
assert.equal(typeof p, 'string', 'rimraf: path should be a string')
|
|
|
|
assert(options, 'rimraf: missing options')
|
|
|
|
assert.equal(typeof options, 'object', 'rimraf: options should be object')
|
|
|
|
|
2021-02-26 04:58:33 +01:00
|
|
|
let results
|
2020-08-26 01:57:08 +02:00
|
|
|
|
|
|
|
if (options.disableGlob || !glob.hasMagic(p)) {
|
|
|
|
results = [p]
|
|
|
|
} else {
|
|
|
|
try {
|
|
|
|
options.lstatSync(p)
|
|
|
|
results = [p]
|
|
|
|
} catch (er) {
|
|
|
|
results = glob.sync(p, options.glob)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!results.length)
|
|
|
|
return
|
|
|
|
|
2021-02-26 04:58:33 +01:00
|
|
|
for (let i = 0; i < results.length; i++) {
|
|
|
|
const p = results[i]
|
2020-08-26 01:57:08 +02:00
|
|
|
|
2021-02-26 04:58:33 +01:00
|
|
|
let st
|
2020-08-26 01:57:08 +02:00
|
|
|
try {
|
2021-02-26 04:58:33 +01:00
|
|
|
st = options.lstatSync(p)
|
2020-08-26 01:57:08 +02:00
|
|
|
} catch (er) {
|
|
|
|
if (er.code === "ENOENT")
|
|
|
|
return
|
|
|
|
|
|
|
|
// Windows can EPERM on stat. Life is suffering.
|
|
|
|
if (er.code === "EPERM" && isWindows)
|
|
|
|
fixWinEPERMSync(p, options, er)
|
|
|
|
}
|
|
|
|
|
|
|
|
try {
|
|
|
|
// sunos lets the root user unlink directories, which is... weird.
|
|
|
|
if (st && st.isDirectory())
|
|
|
|
rmdirSync(p, options, null)
|
|
|
|
else
|
|
|
|
options.unlinkSync(p)
|
|
|
|
} catch (er) {
|
|
|
|
if (er.code === "ENOENT")
|
|
|
|
return
|
|
|
|
if (er.code === "EPERM")
|
|
|
|
return isWindows ? fixWinEPERMSync(p, options, er) : rmdirSync(p, options, er)
|
|
|
|
if (er.code !== "EISDIR")
|
|
|
|
throw er
|
|
|
|
|
|
|
|
rmdirSync(p, options, er)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-02-26 04:58:33 +01:00
|
|
|
const rmdirSync = (p, options, originalEr) => {
|
2020-08-26 01:57:08 +02:00
|
|
|
assert(p)
|
|
|
|
assert(options)
|
|
|
|
|
|
|
|
try {
|
|
|
|
options.rmdirSync(p)
|
|
|
|
} catch (er) {
|
|
|
|
if (er.code === "ENOENT")
|
|
|
|
return
|
|
|
|
if (er.code === "ENOTDIR")
|
|
|
|
throw originalEr
|
|
|
|
if (er.code === "ENOTEMPTY" || er.code === "EEXIST" || er.code === "EPERM")
|
|
|
|
rmkidsSync(p, options)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-02-26 04:58:33 +01:00
|
|
|
const rmkidsSync = (p, options) => {
|
2020-08-26 01:57:08 +02:00
|
|
|
assert(p)
|
|
|
|
assert(options)
|
2021-02-26 04:58:33 +01:00
|
|
|
options.readdirSync(p).forEach(f => rimrafSync(path.join(p, f), options))
|
2020-08-26 01:57:08 +02:00
|
|
|
|
|
|
|
// We only end up here once we got ENOTEMPTY at least once, and
|
|
|
|
// at this point, we are guaranteed to have removed all the kids.
|
|
|
|
// So, we know that it won't be ENOENT or ENOTDIR or anything else.
|
|
|
|
// try really hard to delete stuff on windows, because it has a
|
|
|
|
// PROFOUNDLY annoying habit of not closing handles promptly when
|
|
|
|
// files are deleted, resulting in spurious ENOTEMPTY errors.
|
2021-02-26 04:58:33 +01:00
|
|
|
const retries = isWindows ? 100 : 1
|
|
|
|
let i = 0
|
2020-08-26 01:57:08 +02:00
|
|
|
do {
|
2021-02-26 04:58:33 +01:00
|
|
|
let threw = true
|
2020-08-26 01:57:08 +02:00
|
|
|
try {
|
2021-02-26 04:58:33 +01:00
|
|
|
const ret = options.rmdirSync(p, options)
|
2020-08-26 01:57:08 +02:00
|
|
|
threw = false
|
|
|
|
return ret
|
|
|
|
} finally {
|
|
|
|
if (++i < retries && threw)
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
} while (true)
|
|
|
|
}
|
2021-02-26 04:58:33 +01:00
|
|
|
|
|
|
|
module.exports = rimraf
|
|
|
|
rimraf.sync = rimrafSync
|