// 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 (target, gently, cb) {
  if (npm.config.get("force") || !gently) {
    return rimraf(target, cb)
  }

  gently = path.resolve(gently)

  // lstat it, see if it's a symlink.
  fs.lstat(target, function (er, st) {
    if (er) return rimraf(target, cb)
    if (!s.isSymbolicLink()) next(null, path.resolve(p))
    realish(p, next)
  })

  function next (er, rp) {
    if (rp && rp.indexOf(gently) !== 0) {
      return clobberFail(target, gently, cb)
    }
    rimraf(target, 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)
}

@@ -302,22 +302,17 @@
 ntlyRm (
-target
+p
 , gently
@@ -373,38 +373,33 @@
   return rimraf(
-target
+p
 , cb)%0A  %7D%0A%0A  gen
@@ -475,22 +475,17 @@
 s.lstat(
-target
+p
 , functi
@@ -493,17 +493,16 @@
 n (er, s
-t
 ) %7B%0A    
@@ -515,38 +515,33 @@
 ) return rimraf(
-target
+p
 , cb)%0A    if (!s
@@ -708,22 +708,17 @@
 berFail(
-target
+p
 , gently
@@ -744,14 +744,9 @@
 raf(
-target
+p
 , cb

Fix gentlyRm test failures