Skip to content

Commit

Permalink
fixup! lib: fix fs.readdir recursive async
Browse files Browse the repository at this point in the history
  • Loading branch information
RafaelGSS committed Nov 29, 2024
1 parent 982a50e commit 6ca4cf4
Showing 1 changed file with 8 additions and 15 deletions.
23 changes: 8 additions & 15 deletions lib/fs.js
Original file line number Diff line number Diff line change
Expand Up @@ -1370,7 +1370,7 @@ function mkdirSync(path, options) {
}

/*
* An recurssive algorithm for reading the entire contents of the `basePath` directory.
* An recursive algorithm for reading the entire contents of the `basePath` directory.
* This function does not validate `basePath` as a directory. It is passed directly to
* `binding.readdir`.
* @param {string} basePath
Expand Down Expand Up @@ -1429,31 +1429,24 @@ function readdirRecursive(basePath, options, callback) {
read(context.pathsQueue[i++]);
}

function processReaddirResult({ result, currentPath, context }) {
// Calling `readdir` with `withFileTypes=true`, the result is an array of arrays.
// The first array is the names, and the second array is the types.
// They are guaranteed to be the same length; hence, setting `length` to the length
// of the first array within the result.
if (context.withFileTypes) {
handleDirents({ result, currentPath, context });
} else {
handleFilePaths({ result, currentPath, context });
}
}
// Calling `readdir` with `withFileTypes=true`, the result is an array of arrays.
// The first array is the names, and the second array is the types.
// They are guaranteed to be the same length; hence, setting `length` to the length
// of the first array within the result.
const processReaddirResult = (context) => (context.withFileTypes ? handleDirents(context) : handleFilePaths(context));

function handleDirents({ result, currentPath, context }) {
const { 0: names, 1: types } = result;
const length = names.length;
const { length } = names;

for (let i = 0; i < length; i++) {
// Avoid excluding symlinks, as they are not directories.
// Refs: https://github.com/nodejs/node/issues/52663
const fullPath = pathModule.join(currentPath, names[i]);
const stat = binding.internalModuleStat(binding, fullPath);
const dirent = getDirent(currentPath, names[i], types[i]);
ArrayPrototypePush(context.readdirResults, dirent);

if (dirent.isDirectory() || stat === 1) {
if (dirent.isDirectory() || binding.internalModuleStat(binding, fullPath) === 1) {
ArrayPrototypePush(context.pathsQueue, fullPath);
}
}
Expand Down

0 comments on commit 6ca4cf4

Please sign in to comment.