Skip to content

Commit

Permalink
fs: only operate on buffers in rimraf
Browse files Browse the repository at this point in the history
PR-URL: #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>
  • Loading branch information
cjihrig authored and targos committed Dec 11, 2019
1 parent 8e16093 commit 5e98de1
Showing 1 changed file with 16 additions and 5 deletions.
21 changes: 16 additions & 5 deletions lib/internal/fs/rimraf.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
// - Bring your own custom fs module is not currently supported.
// - Some basic code cleanup.
'use strict';
const { Buffer } = require('buffer');
const {
chmod,
chmodSync,
Expand All @@ -19,7 +20,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']);
Expand All @@ -28,6 +29,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) {
Expand Down Expand Up @@ -116,7 +119,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);

Expand All @@ -128,7 +133,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;

Expand Down Expand Up @@ -205,8 +212,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;
Expand Down

0 comments on commit 5e98de1

Please sign in to comment.