@@ -16,27 +16,19 @@ const { Buffer } = require('buffer');
1616const fs = require ( 'fs' ) ;
1717const {
1818 chmod,
19- chmodSync,
2019 lstat,
21- lstatSync,
2220 readdir,
23- readdirSync,
2421 rmdir,
25- rmdirSync,
2622 stat,
27- statSync,
2823 unlink,
29- unlinkSync,
3024} = fs ;
3125const { sep } = require ( 'path' ) ;
3226const { setTimeout } = require ( 'timers' ) ;
33- const { sleep } = require ( 'internal/util' ) ;
3427const notEmptyErrorCodes = new SafeSet ( [ 'ENOTEMPTY' , 'EEXIST' , 'EPERM' ] ) ;
3528const retryErrorCodes = new SafeSet (
3629 [ 'EBUSY' , 'EMFILE' , 'ENFILE' , 'ENOTEMPTY' , 'EPERM' ] ) ;
3730const isWindows = process . platform === 'win32' ;
3831const epermHandler = isWindows ? fixWinEPERM : _rmdir ;
39- const epermHandlerSync = isWindows ? fixWinEPERMSync : _rmdirSync ;
4032const readdirEncoding = 'buffer' ;
4133const separator = Buffer . from ( sep ) ;
4234
@@ -173,138 +165,4 @@ function rimrafPromises(path, options) {
173165}
174166
175167
176- function rimrafSync ( path , options ) {
177- let stats ;
178-
179- try {
180- stats = lstatSync ( path ) ;
181- } catch ( err ) {
182- if ( err . code === 'ENOENT' )
183- return ;
184-
185- // Windows can EPERM on stat.
186- if ( isWindows && err . code === 'EPERM' )
187- fixWinEPERMSync ( path , options , err ) ;
188- }
189-
190- try {
191- // SunOS lets the root user unlink directories.
192- if ( stats ?. isDirectory ( ) )
193- _rmdirSync ( path , options , null ) ;
194- else
195- _unlinkSync ( path , options ) ;
196- } catch ( err ) {
197- if ( err . code === 'ENOENT' )
198- return ;
199- if ( err . code === 'EPERM' )
200- return epermHandlerSync ( path , options , err ) ;
201- if ( err . code !== 'EISDIR' )
202- throw err ;
203-
204- _rmdirSync ( path , options , err ) ;
205- }
206- }
207-
208-
209- function _unlinkSync ( path , options ) {
210- const tries = options . maxRetries + 1 ;
211-
212- for ( let i = 1 ; i <= tries ; i ++ ) {
213- try {
214- return unlinkSync ( path ) ;
215- } catch ( err ) {
216- // Only sleep if this is not the last try, and the delay is greater
217- // than zero, and an error was encountered that warrants a retry.
218- if ( retryErrorCodes . has ( err . code ) &&
219- i < tries &&
220- options . retryDelay > 0 ) {
221- sleep ( i * options . retryDelay ) ;
222- } else if ( err . code === 'ENOENT' ) {
223- // The file is already gone.
224- return ;
225- } else if ( i === tries ) {
226- throw err ;
227- }
228- }
229- }
230- }
231-
232-
233- function _rmdirSync ( path , options , originalErr ) {
234- try {
235- rmdirSync ( path ) ;
236- } catch ( err ) {
237- if ( err . code === 'ENOENT' )
238- return ;
239- if ( err . code === 'ENOTDIR' ) {
240- throw originalErr || err ;
241- }
242-
243- if ( notEmptyErrorCodes . has ( err . code ) ) {
244- // Removing failed. Try removing all children and then retrying the
245- // original removal. Windows has a habit of not closing handles promptly
246- // when files are deleted, resulting in spurious ENOTEMPTY failures. Work
247- // around that issue by retrying on Windows.
248- const pathBuf = Buffer . from ( path ) ;
249-
250- ArrayPrototypeForEach ( readdirSync ( pathBuf , readdirEncoding ) , ( child ) => {
251- const childPath = Buffer . concat ( [ pathBuf , separator , child ] ) ;
252-
253- rimrafSync ( childPath , options ) ;
254- } ) ;
255-
256- const tries = options . maxRetries + 1 ;
257-
258- for ( let i = 1 ; i <= tries ; i ++ ) {
259- try {
260- return fs . rmdirSync ( path ) ;
261- } catch ( err ) {
262- // Only sleep if this is not the last try, and the delay is greater
263- // than zero, and an error was encountered that warrants a retry.
264- if ( retryErrorCodes . has ( err . code ) &&
265- i < tries &&
266- options . retryDelay > 0 ) {
267- sleep ( i * options . retryDelay ) ;
268- } else if ( err . code === 'ENOENT' ) {
269- // The file is already gone.
270- return ;
271- } else if ( i === tries ) {
272- throw err ;
273- }
274- }
275- }
276- }
277-
278- throw originalErr || err ;
279- }
280- }
281-
282-
283- function fixWinEPERMSync ( path , options , originalErr ) {
284- try {
285- chmodSync ( path , 0o666 ) ;
286- } catch ( err ) {
287- if ( err . code === 'ENOENT' )
288- return ;
289-
290- throw originalErr ;
291- }
292-
293- let stats ;
294-
295- try {
296- stats = statSync ( path , { throwIfNoEntry : false } ) ;
297- } catch {
298- throw originalErr ;
299- }
300-
301- if ( stats === undefined ) return ;
302-
303- if ( stats . isDirectory ( ) )
304- _rmdirSync ( path , options , originalErr ) ;
305- else
306- _unlinkSync ( path , options ) ;
307- }
308-
309-
310- module . exports = { rimraf, rimrafPromises, rimrafSync } ;
168+ module . exports = { rimraf, rimrafPromises } ;
0 commit comments