1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
|
// only remove the thing if it's a symlink into a specific folder.
// This is a very common use-case of npm's, but not so common elsewhere.
module.exports = gentlyRm
var rimraf = require("rimraf")
, fs = require("graceful-fs")
, npm = require("../npm.js")
, path = require("path")
function gentlyRm (p, gently, cb) {
if (!cb) cb = gently, gently = null
// never rm the root, prefix, or bin dirs.
// just a safety precaution.
p = path.resolve(p)
if (p === npm.dir ||
p === npm.root ||
p === npm.bin ||
p === npm.prefix ||
p === npm.globalDir ||
p === npm.globalRoot ||
p === npm.globalBin ||
p === npm.globalPrefix) {
return cb(new Error("May not delete: " + p))
}
if (npm.config.get("force") || !gently) {
return rimraf(p, cb)
}
gently = path.resolve(gently)
// lstat it, see if it's a symlink.
fs.lstat(p, function (er, s) {
if (er) return rimraf(p, cb)
if (!s.isSymbolicLink()) next(null, path.resolve(p))
realish(p, next)
})
function next (er, rp) {
if (rp && rp.indexOf(gently) !== 0) {
return clobberFail(p, gently, cb)
}
rimraf(p, cb)
}
}
function realish (p, cb) {
fs.readlink(p, function (er, r) {
if (er) return cb(er)
return cb(null, path.resolve(path.dirname(p), r))
})
}
function clobberFail (p, g, cb) {
var er = new Error("Refusing to delete: "+p+" not in "+g)
er.code = "EEXIST"
er.path = p
return cb(er)
}
|