GeezerVison is showing github.com — fs: fix readdir recursive sync & callback · nodejs/node@7c758f6
File tree
Expand file treeCollapse file tree
Open diff view settings
Expand file treeCollapse file tree
Open diff view settings
Collapse file
Copy file name to clipboardExpand all lines: lib/fs.js +10-3Lines changed: 10 additions & 3 deletions | Original file line number | Diff line number | Diff line change |
|---|
@@ -1430,14 +1430,21 @@ function readdirSyncRecursive(basePath, options) { |
1430 | 1430 | ); |
1431 | 1431 | handleErrorFromBinding(ctx); |
1432 | 1432 |
|
1433 | | - for (let i = 0; i < readdirResult.length; i++) { |
1434 | | - if (withFileTypes) { |
| 1433 | + if (withFileTypes) { |
| 1434 | + // Calling `readdir` with `withFileTypes=true`, the result is an array of arrays. |
| 1435 | + // The first array is the names, and the second array is the types. |
| 1436 | + // They are guaranteed to be the same length; hence, setting `length` to the length |
| 1437 | + // of the first array within the result. |
| 1438 | + const length = readdirResult[0].length; |
| 1439 | + for (let i = 0; i < length; i++) { |
1435 | 1440 | const dirent = getDirent(path, readdirResult[0][i], readdirResult[1][i]); |
1436 | 1441 | ArrayPrototypePush(readdirResults, dirent); |
1437 | 1442 | if (dirent.isDirectory()) { |
1438 | 1443 | ArrayPrototypePush(pathsQueue, pathModule.join(dirent.path, dirent.name)); |
1439 | 1444 | } |
1440 | | - } else { |
| 1445 | + } |
| 1446 | + } else { |
| 1447 | + for (let i = 0; i < readdirResult.length; i++) { |
1441 | 1448 | const resultPath = pathModule.join(path, readdirResult[i]); |
1442 | 1449 | const relativeResultPath = pathModule.relative(basePath, resultPath); |
1443 | 1450 | const stat = binding.internalModuleStat(resultPath); |
|
Collapse file
Copy file name to clipboardExpand all lines: test/sequential/test-fs-readdir-recursive.js +2Lines changed: 2 additions & 0 deletions | Original file line number | Diff line number | Diff line change |
|---|
@@ -131,9 +131,11 @@ function getDirentPath(dirent) { |
131 | 131 | } |
132 | 132 |
|
133 | 133 | function assertDirents(dirents) { |
| 134 | + assert.strictEqual(dirents.length, expected.length); |
134 | 135 | dirents.sort((a, b) => (getDirentPath(a) < getDirentPath(b) ? -1 : 1)); |
135 | 136 | for (const [i, dirent] of dirents.entries()) { |
136 | 137 | assert(dirent instanceof fs.Dirent); |
| 138 | + assert.notStrictEqual(dirent.name, undefined); |
137 | 139 | assert.strictEqual(getDirentPath(dirent), expected[i]); |
138 | 140 | } |
139 | 141 | } |
|