Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fs: make recursive readdir algorithms iterative #47650

Merged
merged 6 commits into from
May 10, 2023
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
88 changes: 58 additions & 30 deletions lib/fs.js
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,9 @@

const {
ArrayPrototypePush,
ArrayPrototypeShift,
Ethan-Arrowood marked this conversation as resolved.
Show resolved Hide resolved
BigIntPrototypeToString,
Boolean,
MathMax,
Number,
ObjectDefineProperties,
Expand Down Expand Up @@ -1404,34 +1406,60 @@ function mkdirSync(path, options) {
}
}

// TODO(Ethan-Arrowood): Make this iterative too
function readdirSyncRecursive(path, origPath, options) {
nullCheck(path, 'path', true);
const ctx = { path };
const result = binding.readdir(pathModule.toNamespacedPath(path),
options.encoding, !!options.withFileTypes, undefined, ctx);
handleErrorFromBinding(ctx);
return options.withFileTypes ?
getDirents(path, result).flatMap((dirent) => {
return [
dirent,
...(dirent.isDirectory() ?
readdirSyncRecursive(
pathModule.join(path, dirent.name),
origPath,
options,
) : []),
];
}) :
result.flatMap((ent) => {
const innerPath = pathModule.join(path, ent);
const relativePath = pathModule.relative(origPath, innerPath);
const stat = binding.internalModuleStat(innerPath);
return [
relativePath,
...(stat === 1 ? readdirSyncRecursive(innerPath, origPath, options) : []),
];
});
/**
* An iterative 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` after a `nullCheck`.
* @param {string} basePath
* @param {{ encoding: string, withFileTypes: boolean }} options
* @returns {string[] | Dirent[]}
*/
function readdirSyncRecursive(basePath, options) {
nullCheck(basePath, 'path', true);

const readdirResults = [];
const pathsQueue = [basePath];

function read(path) {
const ctx = { path };
const readdirResult = binding.readdir(
pathModule.toNamespacedPath(path),
options.encoding,
Boolean(options.withFileTypes),
undefined,
ctx,
);
Ethan-Arrowood marked this conversation as resolved.
Show resolved Hide resolved
handleErrorFromBinding(ctx);

if (options.withFileTypes) {
const dirents = getDirents(path, readdirResult);
for (let i = 0; i < dirents.length; i++) {
const dirent = dirents[i];
ArrayPrototypePush(readdirResults, dirent);
if (dirent.isDirectory()) {
ArrayPrototypePush(pathsQueue, pathModule.join(dirent.path, dirent.name));
}
}
Ethan-Arrowood marked this conversation as resolved.
Show resolved Hide resolved
} else {
for (let i = 0; i < readdirResult.length; i++) {
const resultPath = pathModule.join(path, readdirResult[i]);
const relativeResultPath = pathModule.relative(basePath, resultPath);
const stat = binding.internalModuleStat(resultPath);
ArrayPrototypePush(readdirResults, relativeResultPath);
// 1 indicates directory
if (stat === 1) {
Ethan-Arrowood marked this conversation as resolved.
Show resolved Hide resolved
ArrayPrototypePush(pathsQueue, resultPath);
}
}
}
}

while (pathsQueue.length > 0) {
const path = ArrayPrototypeShift(pathsQueue);
read(path);
}
Ethan-Arrowood marked this conversation as resolved.
Show resolved Hide resolved

return readdirResults;
}

/**
Expand All @@ -1456,7 +1484,7 @@ function readdir(path, options, callback) {
}

if (options.recursive) {
callback(null, readdirSyncRecursive(path, path, options));
callback(null, readdirSyncRecursive(path, options));
return;
}

Expand Down Expand Up @@ -1494,7 +1522,7 @@ function readdirSync(path, options) {
}

if (options.recursive) {
return readdirSyncRecursive(path, path, options);
return readdirSyncRecursive(path, options);
}

const ctx = { path };
Expand Down
2 changes: 0 additions & 2 deletions lib/internal/fs/dir.js
Original file line number Diff line number Diff line change
Expand Up @@ -160,8 +160,6 @@ class Dir {
}
}

// TODO(Ethan-Arrowood): Review this implementation. Make it iterative.
// Can we better leverage the `kDirOperationQueue`?
readSyncRecursive(dirent) {
const ctx = { path: dirent.path };
const handle = dirBinding.opendir(
Expand Down