Skip to content

Commit 5e98de1

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 8e16093 commit 5e98de1

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
@@ -5,6 +5,7 @@
55
// - Bring your own custom fs module is not currently supported.
66
// - Some basic code cleanup.
77
'use strict';
8+
const { Buffer } = require('buffer');
89
const {
910
chmod,
1011
chmodSync,
@@ -19,7 +20,7 @@ const {
1920
unlink,
2021
unlinkSync
2122
} = require('fs');
22-
const { join } = require('path');
23+
const { sep } = require('path');
2324
const { setTimeout } = require('timers');
2425
const { sleep } = require('internal/util');
2526
const notEmptyErrorCodes = new Set(['ENOTEMPTY', 'EEXIST', 'EPERM']);
@@ -28,6 +29,8 @@ const retryErrorCodes = new Set(
2829
const isWindows = process.platform === 'win32';
2930
const epermHandler = isWindows ? fixWinEPERM : _rmdir;
3031
const epermHandlerSync = isWindows ? fixWinEPERMSync : _rmdirSync;
32+
const readdirEncoding = 'buffer';
33+
const separator = Buffer.from(sep);
3134

3235

3336
function rimraf(path, options, callback) {
@@ -116,7 +119,9 @@ function _rmdir(path, options, originalErr, callback) {
116119

117120

118121
function _rmchildren(path, options, callback) {
119-
readdir(path, (err, files) => {
122+
const pathBuf = Buffer.from(path);
123+
124+
readdir(pathBuf, readdirEncoding, (err, files) => {
120125
if (err)
121126
return callback(err);
122127

@@ -128,7 +133,9 @@ function _rmchildren(path, options, callback) {
128133
let done = false;
129134

130135
files.forEach((child) => {
131-
rimraf(join(path, child), options, (err) => {
136+
const childPath = Buffer.concat([pathBuf, separator, child]);
137+
138+
rimraf(childPath, options, (err) => {
132139
if (done)
133140
return;
134141

@@ -205,8 +212,12 @@ function _rmdirSync(path, options, originalErr) {
205212
// original removal. Windows has a habit of not closing handles promptly
206213
// when files are deleted, resulting in spurious ENOTEMPTY failures. Work
207214
// around that issue by retrying on Windows.
208-
readdirSync(path).forEach((child) => {
209-
rimrafSync(join(path, child), options);
215+
const pathBuf = Buffer.from(path);
216+
217+
readdirSync(pathBuf, readdirEncoding).forEach((child) => {
218+
const childPath = Buffer.concat([pathBuf, separator, child]);
219+
220+
rimrafSync(childPath, options);
210221
});
211222

212223
const tries = options.maxRetries + 1;

0 commit comments

Comments
 (0)