mirror of
https://github.com/nodejs/node.git
synced 2026-11-25 23:01:06 -05:00
fs: only operate on buffers in rimraf
PR-URL: https://github.com/nodejs/node/pull/30569 Reviewed-By: Jiawen Geng <technicalcute@gmail.com> Reviewed-By: Anna Henningsen <anna@addaleax.net> Reviewed-By: Rich Trott <rtrott@gmail.com> Reviewed-By: Ben Coe <bencoe@gmail.com> Reviewed-By: Ruben Bridgewater <ruben@bridgewater.de>
This commit is contained in:
1 parent
e5437ef355
commit
edc9efa5c8
1 file changed
+16
-5
@@ -11,6 +11,7 @@ const {
|
||||
Set,
|
||||
} = primordials;
|
||||
|
||||
const { Buffer } = require('buffer');
|
||||
const {
|
||||
chmod,
|
||||
chmodSync,
|
||||
@@ -25,7 +26,7 @@ const {
|
||||
unlink,
|
||||
unlinkSync
|
||||
} = require('fs');
|
||||
const { join } = require('path');
|
||||
const { sep } = require('path');
|
||||
const { setTimeout } = require('timers');
|
||||
const { sleep } = require('internal/util');
|
||||
const notEmptyErrorCodes = new Set(['ENOTEMPTY', 'EEXIST', 'EPERM']);
|
||||
@@ -34,6 +35,8 @@ const retryErrorCodes = new Set(
|
||||
const isWindows = process.platform === 'win32';
|
||||
const epermHandler = isWindows ? fixWinEPERM : _rmdir;
|
||||
const epermHandlerSync = isWindows ? fixWinEPERMSync : _rmdirSync;
|
||||
const readdirEncoding = 'buffer';
|
||||
const separator = Buffer.from(sep);
|
||||
|
||||
|
||||
function rimraf(path, options, callback) {
|
||||
@@ -122,7 +125,9 @@ function _rmdir(path, options, originalErr, callback) {
|
||||
|
||||
|
||||
function _rmchildren(path, options, callback) {
|
||||
readdir(path, (err, files) => {
|
||||
const pathBuf = Buffer.from(path);
|
||||
|
||||
readdir(pathBuf, readdirEncoding, (err, files) => {
|
||||
if (err)
|
||||
return callback(err);
|
||||
|
||||
@@ -134,7 +139,9 @@ function _rmchildren(path, options, callback) {
|
||||
let done = false;
|
||||
|
||||
files.forEach((child) => {
|
||||
rimraf(join(path, child), options, (err) => {
|
||||
const childPath = Buffer.concat([pathBuf, separator, child]);
|
||||
|
||||
rimraf(childPath, options, (err) => {
|
||||
if (done)
|
||||
return;
|
||||
|
||||
@@ -211,8 +218,12 @@ function _rmdirSync(path, options, originalErr) {
|
||||
// original removal. Windows has a habit of not closing handles promptly
|
||||
// when files are deleted, resulting in spurious ENOTEMPTY failures. Work
|
||||
// around that issue by retrying on Windows.
|
||||
readdirSync(path).forEach((child) => {
|
||||
rimrafSync(join(path, child), options);
|
||||
const pathBuf = Buffer.from(path);
|
||||
|
||||
readdirSync(pathBuf, readdirEncoding).forEach((child) => {
|
||||
const childPath = Buffer.concat([pathBuf, separator, child]);
|
||||
|
||||
rimrafSync(childPath, options);
|
||||
});
|
||||
|
||||
const tries = options.maxRetries + 1;
|
||||
|
||||
Reference in new issue
Block a user