Skip to content

Commit

Permalink
Browse files Browse the repository at this point in the history
fs: only operate on buffers in rimraf
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 BethGriggs committed Feb 6, 2020
1 parent e5437ef commit edc9efa
Showing 1 changed file with 16 additions and 5 deletions.
21 changes: 16 additions & 5 deletions lib/internal/fs/rimraf.js
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 edc9efa

Please sign in to comment.