| 1 |
"use strict"; |
| 2 |
|
| 3 |
const partial = require("es5-ext/function/#/partial") |
| 4 |
, isCallable = require("es5-ext/object/is-callable") |
| 5 |
, isValue = require("es5-ext/object/is-value") |
| 6 |
, deferred = require("deferred") |
| 7 |
, fs = require("fs") |
| 8 |
, pathUtils = require("path") |
| 9 |
, chmod = require("./chmod") |
| 10 |
, lstat = require("./lstat") |
| 11 |
, readdir = require("./readdir") |
| 12 |
, unlink = require("./unlink"); |
| 13 |
|
| 14 |
const original = fs.rmdir, { resolve } = pathUtils, { sep } = pathUtils, empty = {}; |
| 15 |
|
| 16 |
const rmcontent = function (path, options) { |
| 17 |
return readdir(path)( |
| 18 |
function self(files, repeated) { |
| 19 |
return deferred.map(files, name => { |
| 20 |
const filename = path + sep + name; |
| 21 |
let aborted; |
| 22 |
return lstat(filename)( |
| 23 |
stats => { |
| 24 |
if (aborted || options.aborted) return null; |
| 25 |
if (options.recursive && stats.isDirectory()) { |
| 26 |
return rmcontent(filename, options)(null, err2 => { |
| 27 |
options.aborted = true; |
| 28 |
throw err2; |
| 29 |
}); |
| 30 |
} |
| 31 |
if (options.force) { |
| 32 |
return unlink(filename)(null, err2 => { |
| 33 |
if (err2.code === "ENOENT") return; |
| 34 |
aborted = true; |
| 35 |
throw err2; |
| 36 |
}); |
| 37 |
} |
| 38 |
const err = new Error(`ENOTEMPTY rmdir '${ path }'`); |
| 39 |
err.errno = 53; |
| 40 |
err.code = "ENOTEMPTY"; |
| 41 |
err.path = path; |
| 42 |
options.aborted = true; |
| 43 |
throw err; |
| 44 |
}, |
| 45 |
err => { |
| 46 |
if (err.code === "ENOENT") return; |
| 47 |
options.aborted = true; |
| 48 |
throw err; |
| 49 |
} |
| 50 |
); |
| 51 |
})(null, err => { |
| 52 |
if (!options.aborted && !repeated && err.code === "EACCES" && chmod) { |
| 53 |
return chmod(path, 146)(partial.call(self, files, true)); |
| 54 |
} |
| 55 |
throw err; |
| 56 |
}); |
| 57 |
}, |
| 58 |
err => { |
| 59 |
if (err.code === "ENOENT") return; |
| 60 |
throw err; |
| 61 |
} |
| 62 |
)( |
| 63 |
() => { |
| 64 |
if (options.aborted) return null; |
| 65 |
return rmdir(path, empty)(null, err => { |
| 66 |
if (options.aborted) return null; |
| 67 |
if (err.code === "ENOTEMPTY") { |
| 68 |
// Race condition (new files were added to the directory in a meantime) |
| 69 |
return rmcontent(path, options); |
| 70 |
} |
| 71 |
if (err.code === "ENOENT") return null; |
| 72 |
throw err; |
| 73 |
}); |
| 74 |
}, |
| 75 |
err => { |
| 76 |
if (err.code === "ENOENT") return; |
| 77 |
throw err; |
| 78 |
} |
| 79 |
); |
| 80 |
}; |
| 81 |
|
| 82 |
const rmdir = function (path, options) { |
| 83 |
const def = deferred(); |
| 84 |
original(path, err => { |
| 85 |
if (err) { |
| 86 |
if (err.code === "ENOTEMPTY") { |
| 87 |
if (options.recursive || options.force) { |
| 88 |
def.resolve(rmcontent(path, options)); |
| 89 |
return; |
| 90 |
} |
| 91 |
} else if (err.code === "ENOENT") { |
| 92 |
if (options.loose) { |
| 93 |
def.resolve(null); |
| 94 |
return; |
| 95 |
} |
| 96 |
} |
| 97 |
def.reject(err); |
| 98 |
return; |
| 99 |
} |
| 100 |
def.resolve(null); |
| 101 |
}); |
| 102 |
return def.promise; |
| 103 |
}; |
| 104 |
rmdir.returnsPromise = true; |
| 105 |
|
| 106 |
module.exports = exports = function (path, options = {}, cb = null) { |
| 107 |
path = resolve(String(path)); |
| 108 |
|
| 109 |
if (!isValue(cb) && isCallable(options)) { |
| 110 |
cb = options; |
| 111 |
options = {}; |
| 112 |
} else { |
| 113 |
options = Object(options); |
| 114 |
} |
| 115 |
|
| 116 |
return rmdir(path, options).cb(cb); |
| 117 |
}; |
| 118 |
exports.returnsPromise = true; |
| 119 |
exports.rmdir = rmdir; |