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 <[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]>
  • Loading branch information
cjihrig authored and targos committed Jan 14, 2020
1 parent ffc910f commit 76affb6
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 @@ -11,6 +11,7 @@ const {
Set,
} = primordials;

const { Buffer } = require('buffer');
const {
chmod,
chmodSync,
Expand All @@ -25,7 +26,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 @@ -34,6 +35,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 @@ -122,7 +125,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 @@ -134,7 +139,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 @@ -211,8 +218,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 76affb6

Please sign in to comment.