Skip to content

Commit 10a71b9

Browse files
committed
lib: avoid excluding symlinks in recursive fs.readdir with filetypes
Fixes: nodejs#52663 Signed-off-by: Juan José Arboleda <[email protected]>
1 parent 6af5c4e commit 10a71b9

File tree

2 files changed

+35
-1
lines changed

2 files changed

+35
-1
lines changed

lib/fs.js

+4-1
Original file line numberDiff line numberDiff line change
@@ -1401,9 +1401,12 @@ function readdirSyncRecursive(basePath, options) {
14011401
// of the first array within the result.
14021402
const length = readdirResult[0].length;
14031403
for (let i = 0; i < length; i++) {
1404+
// Avoid excluding symlinks, as they are not directories.
1405+
// Refs: https://github.com/nodejs/node/issues/52663
1406+
const stat = binding.internalModuleStat(binding, pathModule.join(path, readdirResult[0][i]));
14041407
const dirent = getDirent(path, readdirResult[0][i], readdirResult[1][i]);
14051408
ArrayPrototypePush(readdirResults, dirent);
1406-
if (dirent.isDirectory()) {
1409+
if (dirent.isDirectory() || stat === 1) {
14071410
ArrayPrototypePush(pathsQueue, pathModule.join(dirent.parentPath, dirent.name));
14081411
}
14091412
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
'use strict';
2+
3+
// Refs: https://github.com/nodejs/node/issues/52663
4+
const common = require('../common');
5+
const assert = require('node:assert');
6+
const fs = require('node:fs');
7+
const path = require('node:path');
8+
9+
if (!common.canCreateSymLink())
10+
common.skip('insufficient privileges');
11+
12+
const tmpdir = require('../common/tmpdir');
13+
const readdirDir = tmpdir.path;
14+
// clean up the tmpdir
15+
tmpdir.refresh();
16+
17+
// a/file
18+
const a = path.join(readdirDir, 'a');
19+
fs.mkdirSync(a);
20+
fs.writeFileSync(path.join(a, 'file'), 'irrelevant');
21+
22+
// a/b
23+
// b -> a
24+
const b = path.join(readdirDir, 'a', 'b');
25+
fs.symlinkSync(a, b, 'dir');
26+
27+
// Just check that the number of entries are the same
28+
assert.strictEqual(
29+
fs.readdirSync(a, { withFileTypes: true }).length,
30+
fs.readdirSync(a, { withFileTypes: false }).length
31+
);

0 commit comments

Comments
 (0)