Skip to content

Commit

Permalink
fs: fix readdir recursive sync & callback
Browse files Browse the repository at this point in the history
Refs: nodejs/node#48640
PR-URL: nodejs/node#48698
Fixes: nodejs/node#48858
Reviewed-By: Ruben Bridgewater <ruben@bridgewater.de>
Reviewed-By: Benjamin Gruenbaum <benjamingr@gmail.com>
Reviewed-By: Moshe Atlow <moshe@atlow.co.il>
Reviewed-By: Yagiz Nizipli <yagiz@nizipli.com>
  • Loading branch information
sercher committed Apr 25, 2024
1 parent 37d4ec7 commit a45f06b
Show file tree
Hide file tree
Showing 2 changed files with 12 additions and 3 deletions.
13 changes: 10 additions & 3 deletions graal-nodejs/lib/fs.js
Original file line number Diff line number Diff line change
Expand Up @@ -1430,14 +1430,21 @@ function readdirSyncRecursive(basePath, options) {
);
handleErrorFromBinding(ctx);

for (let i = 0; i < readdirResult.length; i++) {
if (withFileTypes) {
if (withFileTypes) {
// 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 length = readdirResult[0].length;
for (let i = 0; i < length; i++) {
const dirent = getDirent(path, readdirResult[0][i], readdirResult[1][i]);
ArrayPrototypePush(readdirResults, dirent);
if (dirent.isDirectory()) {
ArrayPrototypePush(pathsQueue, pathModule.join(dirent.path, dirent.name));
}
} else {
}
} 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);
Expand Down
2 changes: 2 additions & 0 deletions graal-nodejs/test/sequential/test-fs-readdir-recursive.js
Original file line number Diff line number Diff line change
Expand Up @@ -131,9 +131,11 @@ function getDirentPath(dirent) {
}

function assertDirents(dirents) {
assert.strictEqual(dirents.length, expected.length);
dirents.sort((a, b) => (getDirentPath(a) < getDirentPath(b) ? -1 : 1));
for (const [i, dirent] of dirents.entries()) {
assert(dirent instanceof fs.Dirent);
assert.notStrictEqual(dirent.name, undefined);
assert.strictEqual(getDirentPath(dirent), expected[i]);
}
}
Expand Down

0 comments on commit a45f06b

Please sign in to comment.