| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| const errnos = require('os').constants.errno |
| const { join } = require('path') |
| const fs = require('../fs.js') |
|
|
| |
| const notEmptyCodes = new Set([ |
| 'ENOTEMPTY', |
| 'EEXIST', |
| 'EPERM', |
| ]) |
|
|
| |
| const retryCodes = new Set([ |
| 'EBUSY', |
| 'EMFILE', |
| 'ENFILE', |
| 'ENOTEMPTY', |
| 'EPERM', |
| ]) |
|
|
| const isWindows = process.platform === 'win32' |
|
|
| const defaultOptions = { |
| retryDelay: 100, |
| maxRetries: 0, |
| recursive: false, |
| force: false, |
| } |
|
|
| |
| |
| class ERR_FS_EISDIR extends Error { |
| constructor (path) { |
| super() |
| this.info = { |
| code: 'EISDIR', |
| message: 'is a directory', |
| path, |
| syscall: 'rm', |
| errno: errnos.EISDIR, |
| } |
| this.name = 'SystemError' |
| this.code = 'ERR_FS_EISDIR' |
| this.errno = errnos.EISDIR |
| this.syscall = 'rm' |
| this.path = path |
| this.message = `Path is a directory: ${this.syscall} returned ` + |
| `${this.info.code} (is a directory) ${path}` |
| } |
|
|
| toString () { |
| return `${this.name} [${this.code}]: ${this.message}` |
| } |
| } |
|
|
| class ENOTDIR extends Error { |
| constructor (path) { |
| super() |
| this.name = 'Error' |
| this.code = 'ENOTDIR' |
| this.errno = errnos.ENOTDIR |
| this.syscall = 'rmdir' |
| this.path = path |
| this.message = `not a directory, ${this.syscall} '${this.path}'` |
| } |
|
|
| toString () { |
| return `${this.name}: ${this.code}: ${this.message}` |
| } |
| } |
|
|
| |
| |
| |
| const rimraf = async (path, options, isTop = false) => { |
| const force = isTop ? options.force : true |
| const stat = await fs.lstat(path) |
| .catch((err) => { |
| |
| if (err.code === 'ENOENT' && force) { |
| return |
| } |
|
|
| if (isWindows && err.code === 'EPERM') { |
| return fixEPERM(path, options, err, isTop) |
| } |
|
|
| throw err |
| }) |
|
|
| |
| |
| |
| if (!stat) { |
| return |
| } |
|
|
| if (stat.isDirectory()) { |
| return rmdir(path, options, null, isTop) |
| } |
|
|
| return fs.unlink(path) |
| .catch((err) => { |
| if (err.code === 'ENOENT' && force) { |
| return |
| } |
|
|
| if (err.code === 'EISDIR') { |
| return rmdir(path, options, err, isTop) |
| } |
|
|
| if (err.code === 'EPERM') { |
| |
| |
| |
| return isWindows |
| ? fixEPERM(path, options, err, isTop) |
| : rmdir(path, options, err, isTop) |
| } |
|
|
| throw err |
| }) |
| } |
|
|
| const fixEPERM = async (path, options, originalErr, isTop) => { |
| const force = isTop ? options.force : true |
| const targetMissing = await fs.chmod(path, 0o666) |
| .catch((err) => { |
| if (err.code === 'ENOENT' && force) { |
| return true |
| } |
|
|
| throw originalErr |
| }) |
|
|
| |
| if (targetMissing) { |
| return |
| } |
|
|
| |
| |
| const stat = await fs.lstat(path) |
| .catch((err) => { |
| if (err.code === 'ENOENT' && force) { |
| return |
| } |
|
|
| throw originalErr |
| }) |
|
|
| if (!stat) { |
| return |
| } |
|
|
| if (stat.isDirectory()) { |
| return rmdir(path, options, originalErr, isTop) |
| } |
|
|
| return fs.unlink(path) |
| } |
|
|
| const rmdir = async (path, options, originalErr, isTop) => { |
| if (!options.recursive && isTop) { |
| throw originalErr || new ERR_FS_EISDIR(path) |
| } |
| const force = isTop ? options.force : true |
|
|
| return fs.rmdir(path) |
| .catch(async (err) => { |
| |
| |
| |
| |
| if (isWindows && err.code === 'ENOENT') { |
| const stillExists = await fs.lstat(path).then(() => true, () => false) |
| if (stillExists) { |
| err = new ENOTDIR(path) |
| } |
| } |
|
|
| |
| if (err.code === 'ENOENT' && force) { |
| return |
| } |
|
|
| |
| |
| |
| if (originalErr && err.code === 'ENOTDIR') { |
| throw originalErr |
| } |
|
|
| |
| if (notEmptyCodes.has(err.code)) { |
| const files = await fs.readdir(path) |
| await Promise.all(files.map((file) => { |
| const target = join(path, file) |
| return rimraf(target, options) |
| })) |
| return fs.rmdir(path) |
| } |
|
|
| throw err |
| }) |
| } |
|
|
| const rm = async (path, opts) => { |
| const options = { ...defaultOptions, ...opts } |
| let retries = 0 |
|
|
| const errHandler = async (err) => { |
| if (retryCodes.has(err.code) && ++retries < options.maxRetries) { |
| const delay = retries * options.retryDelay |
| await promiseTimeout(delay) |
| return rimraf(path, options, true).catch(errHandler) |
| } |
|
|
| throw err |
| } |
|
|
| return rimraf(path, options, true).catch(errHandler) |
| } |
|
|
| const promiseTimeout = (ms) => new Promise((r) => setTimeout(r, ms)) |
|
|
| module.exports = rm |
|
|