5
5
// - Bring your own custom fs module is not currently supported.
6
6
// - Some basic code cleanup.
7
7
'use strict' ;
8
+ const { Buffer } = require ( 'buffer' ) ;
8
9
const {
9
10
chmod,
10
11
chmodSync,
@@ -19,7 +20,7 @@ const {
19
20
unlink,
20
21
unlinkSync
21
22
} = require ( 'fs' ) ;
22
- const { join } = require ( 'path' ) ;
23
+ const { sep } = require ( 'path' ) ;
23
24
const { setTimeout } = require ( 'timers' ) ;
24
25
const { sleep } = require ( 'internal/util' ) ;
25
26
const notEmptyErrorCodes = new Set ( [ 'ENOTEMPTY' , 'EEXIST' , 'EPERM' ] ) ;
@@ -28,6 +29,8 @@ const retryErrorCodes = new Set(
28
29
const isWindows = process . platform === 'win32' ;
29
30
const epermHandler = isWindows ? fixWinEPERM : _rmdir ;
30
31
const epermHandlerSync = isWindows ? fixWinEPERMSync : _rmdirSync ;
32
+ const readdirEncoding = 'buffer' ;
33
+ const separator = Buffer . from ( sep ) ;
31
34
32
35
33
36
function rimraf ( path , options , callback ) {
@@ -116,7 +119,9 @@ function _rmdir(path, options, originalErr, callback) {
116
119
117
120
118
121
function _rmchildren ( path , options , callback ) {
119
- readdir ( path , ( err , files ) => {
122
+ const pathBuf = Buffer . from ( path ) ;
123
+
124
+ readdir ( pathBuf , readdirEncoding , ( err , files ) => {
120
125
if ( err )
121
126
return callback ( err ) ;
122
127
@@ -128,7 +133,9 @@ function _rmchildren(path, options, callback) {
128
133
let done = false ;
129
134
130
135
files . forEach ( ( child ) => {
131
- rimraf ( join ( path , child ) , options , ( err ) => {
136
+ const childPath = Buffer . concat ( [ pathBuf , separator , child ] ) ;
137
+
138
+ rimraf ( childPath , options , ( err ) => {
132
139
if ( done )
133
140
return ;
134
141
@@ -205,8 +212,12 @@ function _rmdirSync(path, options, originalErr) {
205
212
// original removal. Windows has a habit of not closing handles promptly
206
213
// when files are deleted, resulting in spurious ENOTEMPTY failures. Work
207
214
// 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 ) ;
210
221
} ) ;
211
222
212
223
const tries = options . maxRetries + 1 ;
0 commit comments