From 1610728d7c04884c9f68c28e6602418936e02b50 Mon Sep 17 00:00:00 2001 From: Ian Sutherland Date: Sat, 3 Oct 2020 20:20:28 -0600 Subject: [PATCH] fs: add rm method This PR introduces a new method fs.rm that provides the behaviour of rimraf when used with the recursive: true and force: true options. PR-URL: https://github.com/nodejs/node/pull/35494 Reviewed-By: Ben Coe Reviewed-By: Rich Trott Reviewed-By: Ruy Adorno Reviewed-By: Matteo Collina Reviewed-By: Michael Dawson --- doc/api/errors.md | 5 + doc/api/fs.md | 87 +++++- lib/fs.js | 59 +++- lib/internal/errors.js | 1 + lib/internal/fs/promises.js | 9 + lib/internal/fs/utils.js | 97 ++++++- test/common/tmpdir.js | 8 +- test/parallel/test-fs-rm.js | 283 +++++++++++++++++++ test/parallel/test-policy-parse-integrity.js | 2 +- test/pummel/test-policy-integrity.js | 4 +- 10 files changed, 518 insertions(+), 37 deletions(-) create mode 100644 test/parallel/test-fs-rm.js diff --git a/doc/api/errors.md b/doc/api/errors.md index 7e172d58d7fe3a..7e356f1001318a 100644 --- a/doc/api/errors.md +++ b/doc/api/errors.md @@ -928,6 +928,11 @@ added: v14.0.0 Used when a feature that is not available to the current platform which is running Node.js is used. + +### `ERR_FS_EISDIR` + +Path is a directory. + ### `ERR_FS_FILE_TOO_LARGE` diff --git a/doc/api/fs.md b/doc/api/fs.md index 36d3fc10f4df54..db3e41e1ee17e0 100644 --- a/doc/api/fs.md +++ b/doc/api/fs.md @@ -3509,9 +3509,9 @@ changes: * `options` {Object} * `maxRetries` {integer} If an `EBUSY`, `EMFILE`, `ENFILE`, `ENOTEMPTY`, or `EPERM` error is encountered, Node.js will retry the operation with a linear - backoff wait of `retryDelay` ms longer on each try. This option represents - the number of retries. This option is ignored if the `recursive` option is - not `true`. **Default:** `0`. + backoff wait of `retryDelay` milliseconds longer on each try. This option + represents the number of retries. This option is ignored if the `recursive` + option is not `true`. **Default:** `0`. * `recursive` {boolean} If `true`, perform a recursive directory removal. In recursive mode, errors are not reported if `path` does not exist, and operations are retried on failure. **Default:** `false`. @@ -3560,9 +3560,9 @@ changes: * `options` {Object} * `maxRetries` {integer} If an `EBUSY`, `EMFILE`, `ENFILE`, `ENOTEMPTY`, or `EPERM` error is encountered, Node.js will retry the operation with a linear - backoff wait of `retryDelay` ms longer on each try. This option represents - the number of retries. This option is ignored if the `recursive` option is - not `true`. **Default:** `0`. + backoff wait of `retryDelay` milliseconds longer on each try. This option + represents the number of retries. This option is ignored if the `recursive` + option is not `true`. **Default:** `0`. * `recursive` {boolean} If `true`, perform a recursive directory removal. In recursive mode, errors are not reported if `path` does not exist, and operations are retried on failure. **Default:** `false`. @@ -3581,6 +3581,53 @@ that represent files will be deleted. The permissive behavior of the `recursive` option is deprecated, `ENOTDIR` and `ENOENT` will be thrown in the future. +## `fs.rm(path[, options], callback)` + + +* `path` {string|Buffer|URL} +* `options` {Object} + * `force` don't error on nonexistent path + * `maxRetries` {integer} If an `EBUSY`, `EMFILE`, `ENFILE`, `ENOTEMPTY`, or + `EPERM` error is encountered, Node.js will retry the operation with a linear + backoff wait of `retryDelay` milliseconds longer on each try. This option + represents the number of retries. This option is ignored if the `recursive` + option is not `true`. **Default:** `0`. + * `recursive` {boolean} If `true`, perform a recursive removal. In + recursive mode operations are retried on failure. **Default:** `false`. + * `retryDelay` {integer} The amount of time in milliseconds to wait between + retries. This option is ignored if the `recursive` option is not `true`. + **Default:** `100`. +* `callback` {Function} + * `err` {Error} + +Asynchronously removes files and directories (modeled on the standard POSIX `rm` +utility). No arguments other than a possible exception are given to the +completion callback. + +## `fs.rmSync(path[, options])` + + +* `path` {string|Buffer|URL} +* `options` {Object} + * `force` Ignore errors + * `maxRetries` {integer} If an `EBUSY`, `EMFILE`, `ENFILE`, `ENOTEMPTY`, or + `EPERM` error is encountered, Node.js will retry the operation with a linear + backoff wait of `retryDelay` milliseconds longer on each try. This option + represents the number of retries. This option is ignored if the `recursive` + option is not `true`. **Default:** `0`. + * `recursive` {boolean} If `true`, perform a recursive directory removal. In + recursive mode operations are retried on failure. **Default:** `false`. + * `retryDelay` {integer} The amount of time in milliseconds to wait between + retries. This option is ignored if the `recursive` option is not `true`. + **Default:** `100`. + +Synchronously removes files and directories (modeled on the standard POSIX `rm` +utility). Returns `undefined`. + ## `fs.stat(path[, options], callback)` + +* `path` {string|Buffer|URL} +* `options` {Object} + * `force` Ignore errors + * `maxRetries` {integer} If an `EBUSY`, `EMFILE`, `ENFILE`, `ENOTEMPTY`, or + `EPERM` error is encountered, Node.js will retry the operation with a linear + backoff wait of `retryDelay` milliseconds longer on each try. This option + represents the number of retries. This option is ignored if the `recursive` + option is not `true`. **Default:** `0`. + * `recursive` {boolean} If `true`, perform a recursive directory removal. In + recursive mode operations are retried on failure. **Default:** `false`. + * `retryDelay` {integer} The amount of time in milliseconds to wait between + retries. This option is ignored if the `recursive` option is not `true`. + **Default:** `100`. + +Synchronously removes files and directories (modeled on the standard POSIX `rm` +utility). Resolves the `Promise` with no arguments on success. + ### `fsPromises.stat(path[, options])`