Skip to content

Commit 76affb6

Browse files
cjihrigtargos
authored andcommitted
fs: only operate on buffers in rimraf
PR-URL: #30569 Reviewed-By: Jiawen Geng <[email protected]> Reviewed-By: Anna Henningsen <[email protected]> Reviewed-By: Rich Trott <[email protected]> Reviewed-By: Ben Coe <[email protected]> Reviewed-By: Ruben Bridgewater <[email protected]>
1 parent ffc910f commit 76affb6

File tree

1 file changed

+16
-5
lines changed

1 file changed

+16
-5
lines changed

lib/internal/fs/rimraf.js

+16-5
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@ const {
1111
Set,
1212
} = primordials;
1313

14+
const { Buffer } = require('buffer');
1415
const {
1516
chmod,
1617
chmodSync,
@@ -25,7 +26,7 @@ const {
2526
unlink,
2627
unlinkSync
2728
} = require('fs');
28-
const { join } = require('path');
29+
const { sep } = require('path');
2930
const { setTimeout } = require('timers');
3031
const { sleep } = require('internal/util');
3132
const notEmptyErrorCodes = new Set(['ENOTEMPTY', 'EEXIST', 'EPERM']);
@@ -34,6 +35,8 @@ const retryErrorCodes = new Set(
3435
const isWindows = process.platform === 'win32';
3536
const epermHandler = isWindows ? fixWinEPERM : _rmdir;
3637
const epermHandlerSync = isWindows ? fixWinEPERMSync : _rmdirSync;
38+
const readdirEncoding = 'buffer';
39+
const separator = Buffer.from(sep);
3740

3841

3942
function rimraf(path, options, callback) {
@@ -122,7 +125,9 @@ function _rmdir(path, options, originalErr, callback) {
122125

123126

124127
function _rmchildren(path, options, callback) {
125-
readdir(path, (err, files) => {
128+
const pathBuf = Buffer.from(path);
129+
130+
readdir(pathBuf, readdirEncoding, (err, files) => {
126131
if (err)
127132
return callback(err);
128133

@@ -134,7 +139,9 @@ function _rmchildren(path, options, callback) {
134139
let done = false;
135140

136141
files.forEach((child) => {
137-
rimraf(join(path, child), options, (err) => {
142+
const childPath = Buffer.concat([pathBuf, separator, child]);
143+
144+
rimraf(childPath, options, (err) => {
138145
if (done)
139146
return;
140147

@@ -211,8 +218,12 @@ function _rmdirSync(path, options, originalErr) {
211218
// original removal. Windows has a habit of not closing handles promptly
212219
// when files are deleted, resulting in spurious ENOTEMPTY failures. Work
213220
// around that issue by retrying on Windows.
214-
readdirSync(path).forEach((child) => {
215-
rimrafSync(join(path, child), options);
221+
const pathBuf = Buffer.from(path);
222+
223+
readdirSync(pathBuf, readdirEncoding).forEach((child) => {
224+
const childPath = Buffer.concat([pathBuf, separator, child]);
225+
226+
rimrafSync(childPath, options);
216227
});
217228

218229
const tries = options.maxRetries + 1;

0 commit comments

Comments
 (0)