Skip to content

Commit 439ea47

Browse files
Ethan Arrowoodtargos
Ethan Arrowood
authored andcommittedMay 2, 2023
fs: add recursive option to readdir and opendir
Adds a naive, linear recursive algorithm for the following methods: readdir, readdirSync, opendir, opendirSync, and the promise based equivalents. Fixes: #34992 PR-URL: #41439 Refs: nodejs/tooling#130 Reviewed-By: Yagiz Nizipli <[email protected]> Reviewed-By: Matteo Collina <[email protected]> Reviewed-By: James M Snell <[email protected]> Reviewed-By: Moshe Atlow <[email protected]>
1 parent 872e670 commit 439ea47

File tree

7 files changed

+659
-31
lines changed

7 files changed

+659
-31
lines changed
 

‎doc/api/fs.md

+35
Original file line numberDiff line numberDiff line change
@@ -1220,6 +1220,9 @@ a colon, Node.js will open a file system stream, as described by
12201220
<!-- YAML
12211221
added: v12.12.0
12221222
changes:
1223+
- version: REPLACEME
1224+
pr-url: https://github.com/nodejs/node/pull/41439
1225+
description: Added `recursive` option.
12231226
- version:
12241227
- v13.1.0
12251228
- v12.16.0
@@ -1233,6 +1236,8 @@ changes:
12331236
* `bufferSize` {number} Number of directory entries that are buffered
12341237
internally when reading from the directory. Higher values lead to better
12351238
performance but higher memory usage. **Default:** `32`
1239+
* `recursive` {boolean} Resolved `Dir` will be an {AsyncIterable}
1240+
containing all sub files and directories. **Default:** `false`
12361241
* Returns: {Promise} Fulfills with an {fs.Dir}.
12371242
12381243
Asynchronously open a directory for iterative scanning. See the POSIX
@@ -1266,6 +1271,9 @@ closed after the iterator exits.
12661271
<!-- YAML
12671272
added: v10.0.0
12681273
changes:
1274+
- version: REPLACEME
1275+
pr-url: https://github.com/nodejs/node/pull/41439
1276+
description: Added `recursive` option.
12691277
- version: v10.11.0
12701278
pr-url: https://github.com/nodejs/node/pull/22020
12711279
description: New option `withFileTypes` was added.
@@ -1275,6 +1283,7 @@ changes:
12751283
* `options` {string|Object}
12761284
* `encoding` {string} **Default:** `'utf8'`
12771285
* `withFileTypes` {boolean} **Default:** `false`
1286+
* `recursive` {boolean} **Default:** `false`
12781287
* Returns: {Promise} Fulfills with an array of the names of the files in
12791288
the directory excluding `'.'` and `'..'`.
12801289
@@ -3402,6 +3411,9 @@ const { openAsBlob } = require('node:fs');
34023411
<!-- YAML
34033412
added: v12.12.0
34043413
changes:
3414+
- version: REPLACEME
3415+
pr-url: https://github.com/nodejs/node/pull/41439
3416+
description: Added `recursive` option.
34053417
- version: v18.0.0
34063418
pr-url: https://github.com/nodejs/node/pull/41678
34073419
description: Passing an invalid callback to the `callback` argument
@@ -3420,6 +3432,7 @@ changes:
34203432
* `bufferSize` {number} Number of directory entries that are buffered
34213433
internally when reading from the directory. Higher values lead to better
34223434
performance but higher memory usage. **Default:** `32`
3435+
* `recursive` {boolean} **Default:** `false`
34233436
* `callback` {Function}
34243437
* `err` {Error}
34253438
* `dir` {fs.Dir}
@@ -3538,6 +3551,9 @@ above values.
35383551
<!-- YAML
35393552
added: v0.1.8
35403553
changes:
3554+
- version: REPLACEME
3555+
pr-url: https://github.com/nodejs/node/pull/41439
3556+
description: Added `recursive` option.
35413557
- version: v18.0.0
35423558
pr-url: https://github.com/nodejs/node/pull/41678
35433559
description: Passing an invalid callback to the `callback` argument
@@ -3567,6 +3583,7 @@ changes:
35673583
* `options` {string|Object}
35683584
* `encoding` {string} **Default:** `'utf8'`
35693585
* `withFileTypes` {boolean} **Default:** `false`
3586+
* `recursive` {boolean} **Default:** `false`
35703587
* `callback` {Function}
35713588
* `err` {Error}
35723589
* `files` {string\[]|Buffer\[]|fs.Dirent\[]}
@@ -5543,6 +5560,9 @@ object with an `encoding` property specifying the character encoding to use.
55435560
<!-- YAML
55445561
added: v12.12.0
55455562
changes:
5563+
- version: REPLACEME
5564+
pr-url: https://github.com/nodejs/node/pull/41439
5565+
description: Added `recursive` option.
55465566
- version:
55475567
- v13.1.0
55485568
- v12.16.0
@@ -5556,6 +5576,7 @@ changes:
55565576
* `bufferSize` {number} Number of directory entries that are buffered
55575577
internally when reading from the directory. Higher values lead to better
55585578
performance but higher memory usage. **Default:** `32`
5579+
* `recursive` {boolean} **Default:** `false`
55595580
* Returns: {fs.Dir}
55605581
55615582
Synchronously open a directory. See opendir(3).
@@ -5599,6 +5620,9 @@ this API: [`fs.open()`][].
55995620
<!-- YAML
56005621
added: v0.1.21
56015622
changes:
5623+
- version: REPLACEME
5624+
pr-url: https://github.com/nodejs/node/pull/41439
5625+
description: Added `recursive` option.
56025626
- version: v10.10.0
56035627
pr-url: https://github.com/nodejs/node/pull/22020
56045628
description: New option `withFileTypes` was added.
@@ -5612,6 +5636,7 @@ changes:
56125636
* `options` {string|Object}
56135637
* `encoding` {string} **Default:** `'utf8'`
56145638
* `withFileTypes` {boolean} **Default:** `false`
5639+
* `recursive` {boolean} **Default:** `false`
56155640
* Returns: {string\[]|Buffer\[]|fs.Dirent\[]}
56165641
56175642
Reads the contents of the directory.
@@ -6465,6 +6490,16 @@ The file name that this {fs.Dirent} object refers to. The type of this
64656490
value is determined by the `options.encoding` passed to [`fs.readdir()`][] or
64666491
[`fs.readdirSync()`][].
64676492
6493+
#### `dirent.path`
6494+
6495+
<!-- YAML
6496+
added: REPLACEME
6497+
-->
6498+
6499+
* {string}
6500+
6501+
The base path that this {fs.Dirent} object refers to.
6502+
64686503
### Class: `fs.FSWatcher`
64696504
64706505
<!-- YAML

‎lib/fs.js

+47
Original file line numberDiff line numberDiff line change
@@ -1404,6 +1404,36 @@ function mkdirSync(path, options) {
14041404
}
14051405
}
14061406

1407+
// TODO(Ethan-Arrowood): Make this iterative too
1408+
function readdirSyncRecursive(path, origPath, options) {
1409+
nullCheck(path, 'path', true);
1410+
const ctx = { path };
1411+
const result = binding.readdir(pathModule.toNamespacedPath(path),
1412+
options.encoding, !!options.withFileTypes, undefined, ctx);
1413+
handleErrorFromBinding(ctx);
1414+
return options.withFileTypes ?
1415+
getDirents(path, result).flatMap((dirent) => {
1416+
return [
1417+
dirent,
1418+
...(dirent.isDirectory() ?
1419+
readdirSyncRecursive(
1420+
pathModule.join(path, dirent.name),
1421+
origPath,
1422+
options,
1423+
) : []),
1424+
];
1425+
}) :
1426+
result.flatMap((ent) => {
1427+
const innerPath = pathModule.join(path, ent);
1428+
const relativePath = pathModule.relative(origPath, innerPath);
1429+
const stat = binding.internalModuleStat(innerPath);
1430+
return [
1431+
relativePath,
1432+
...(stat === 1 ? readdirSyncRecursive(innerPath, origPath, options) : []),
1433+
];
1434+
});
1435+
}
1436+
14071437
/**
14081438
* Reads the contents of a directory.
14091439
* @param {string | Buffer | URL} path
@@ -1421,6 +1451,14 @@ function readdir(path, options, callback) {
14211451
callback = makeCallback(typeof options === 'function' ? options : callback);
14221452
options = getOptions(options);
14231453
path = getValidatedPath(path);
1454+
if (options.recursive != null) {
1455+
validateBoolean(options.recursive, 'options.recursive');
1456+
}
1457+
1458+
if (options.recursive) {
1459+
callback(null, readdirSyncRecursive(path, path, options));
1460+
return;
1461+
}
14241462

14251463
const req = new FSReqCallback();
14261464
if (!options.withFileTypes) {
@@ -1444,12 +1482,21 @@ function readdir(path, options, callback) {
14441482
* @param {string | {
14451483
* encoding?: string;
14461484
* withFileTypes?: boolean;
1485+
* recursive?: boolean;
14471486
* }} [options]
14481487
* @returns {string | Buffer[] | Dirent[]}
14491488
*/
14501489
function readdirSync(path, options) {
14511490
options = getOptions(options);
14521491
path = getValidatedPath(path);
1492+
if (options.recursive != null) {
1493+
validateBoolean(options.recursive, 'options.recursive');
1494+
}
1495+
1496+
if (options.recursive) {
1497+
return readdirSyncRecursive(path, path, options);
1498+
}
1499+
14531500
const ctx = { path };
14541501
const result = binding.readdir(pathModule.toNamespacedPath(path),
14551502
options.encoding, !!options.withFileTypes,

‎lib/internal/fs/dir.js

+77-16
Original file line numberDiff line numberDiff line change
@@ -2,8 +2,7 @@
22

33
const {
44
ArrayPrototypePush,
5-
ArrayPrototypeSlice,
6-
ArrayPrototypeSplice,
5+
ArrayPrototypeShift,
76
FunctionPrototypeBind,
87
ObjectDefineProperty,
98
PromiseReject,
@@ -99,13 +98,21 @@ class Dir {
9998
}
10099

101100
if (this[kDirBufferedEntries].length > 0) {
102-
const { 0: name, 1: type } =
103-
ArrayPrototypeSplice(this[kDirBufferedEntries], 0, 2);
104-
if (maybeSync)
105-
process.nextTick(getDirent, this[kDirPath], name, type, callback);
106-
else
107-
getDirent(this[kDirPath], name, type, callback);
108-
return;
101+
try {
102+
const dirent = ArrayPrototypeShift(this[kDirBufferedEntries]);
103+
104+
if (this[kDirOptions].recursive && dirent.isDirectory()) {
105+
this.readSyncRecursive(dirent);
106+
}
107+
108+
if (maybeSync)
109+
process.nextTick(callback, null, dirent);
110+
else
111+
callback(null, dirent);
112+
return;
113+
} catch (error) {
114+
return callback(error);
115+
}
109116
}
110117

111118
const req = new FSReqCallback();
@@ -120,8 +127,16 @@ class Dir {
120127
return callback(err, result);
121128
}
122129

123-
this[kDirBufferedEntries] = ArrayPrototypeSlice(result, 2);
124-
getDirent(this[kDirPath], result[0], result[1], callback);
130+
try {
131+
this.processReadResult(this[kDirPath], result);
132+
const dirent = ArrayPrototypeShift(this[kDirBufferedEntries]);
133+
if (this[kDirOptions].recursive && dirent.isDirectory()) {
134+
this.readSyncRecursive(dirent);
135+
}
136+
callback(null, dirent);
137+
} catch (error) {
138+
callback(error);
139+
}
125140
};
126141

127142
this[kDirOperationQueue] = [];
@@ -132,6 +147,45 @@ class Dir {
132147
);
133148
}
134149

150+
processReadResult(path, result) {
151+
for (let i = 0; i < result.length; i += 2) {
152+
ArrayPrototypePush(
153+
this[kDirBufferedEntries],
154+
getDirent(
155+
pathModule.join(path, result[i]),
156+
result[i],
157+
result[i + 1],
158+
),
159+
);
160+
}
161+
}
162+
163+
// TODO(Ethan-Arrowood): Review this implementation. Make it iterative.
164+
// Can we better leverage the `kDirOperationQueue`?
165+
readSyncRecursive(dirent) {
166+
const ctx = { path: dirent.path };
167+
const handle = dirBinding.opendir(
168+
pathModule.toNamespacedPath(dirent.path),
169+
this[kDirOptions].encoding,
170+
undefined,
171+
ctx,
172+
);
173+
handleErrorFromBinding(ctx);
174+
const result = handle.read(
175+
this[kDirOptions].encoding,
176+
this[kDirOptions].bufferSize,
177+
undefined,
178+
ctx,
179+
);
180+
181+
if (result) {
182+
this.processReadResult(dirent.path, result);
183+
}
184+
185+
handle.close(undefined, ctx);
186+
handleErrorFromBinding(ctx);
187+
}
188+
135189
readSync() {
136190
if (this[kDirClosed] === true) {
137191
throw new ERR_DIR_CLOSED();
@@ -142,9 +196,11 @@ class Dir {
142196
}
143197

144198
if (this[kDirBufferedEntries].length > 0) {
145-
const { 0: name, 1: type } =
146-
ArrayPrototypeSplice(this[kDirBufferedEntries], 0, 2);
147-
return getDirent(this[kDirPath], name, type);
199+
const dirent = ArrayPrototypeShift(this[kDirBufferedEntries]);
200+
if (this[kDirOptions].recursive && dirent.isDirectory()) {
201+
this.readSyncRecursive(dirent);
202+
}
203+
return dirent;
148204
}
149205

150206
const ctx = { path: this[kDirPath] };
@@ -160,8 +216,13 @@ class Dir {
160216
return result;
161217
}
162218

163-
this[kDirBufferedEntries] = ArrayPrototypeSlice(result, 2);
164-
return getDirent(this[kDirPath], result[0], result[1]);
219+
this.processReadResult(this[kDirPath], result);
220+
221+
const dirent = ArrayPrototypeShift(this[kDirBufferedEntries]);
222+
if (this[kDirOptions].recursive && dirent.isDirectory()) {
223+
this.readSyncRecursive(dirent);
224+
}
225+
return dirent;
165226
}
166227

167228
close(callback) {

‎lib/internal/fs/promises.js

+73-4
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22

33
const {
44
ArrayPrototypePush,
5+
ArrayPrototypePop,
56
Error,
67
MathMax,
78
MathMin,
@@ -770,13 +771,81 @@ async function mkdir(path, options) {
770771
kUsePromises);
771772
}
772773

774+
async function readdirRecursive(originalPath, options) {
775+
const result = [];
776+
const queue = [
777+
[
778+
originalPath,
779+
await binding.readdir(
780+
pathModule.toNamespacedPath(originalPath),
781+
options.encoding,
782+
!!options.withFileTypes,
783+
kUsePromises,
784+
),
785+
],
786+
];
787+
788+
789+
if (options.withFileTypes) {
790+
while (queue.length > 0) {
791+
// If we want to implement BFS make this a `shift` call instead of `pop`
792+
const { 0: path, 1: readdir } = ArrayPrototypePop(queue);
793+
for (const dirent of getDirents(path, readdir)) {
794+
ArrayPrototypePush(result, dirent);
795+
if (dirent.isDirectory()) {
796+
const direntPath = pathModule.join(path, dirent.name);
797+
ArrayPrototypePush(queue, [
798+
direntPath,
799+
await binding.readdir(
800+
direntPath,
801+
options.encoding,
802+
true,
803+
kUsePromises,
804+
),
805+
]);
806+
}
807+
}
808+
}
809+
} else {
810+
while (queue.length > 0) {
811+
const { 0: path, 1: readdir } = ArrayPrototypePop(queue);
812+
for (const ent of readdir) {
813+
const direntPath = pathModule.join(path, ent);
814+
const stat = binding.internalModuleStat(direntPath);
815+
ArrayPrototypePush(
816+
result,
817+
pathModule.relative(originalPath, direntPath),
818+
);
819+
if (stat === 1) {
820+
ArrayPrototypePush(queue, [
821+
direntPath,
822+
await binding.readdir(
823+
pathModule.toNamespacedPath(direntPath),
824+
options.encoding,
825+
false,
826+
kUsePromises,
827+
),
828+
]);
829+
}
830+
}
831+
}
832+
}
833+
834+
return result;
835+
}
836+
773837
async function readdir(path, options) {
774838
options = getOptions(options);
775839
path = getValidatedPath(path);
776-
const result = await binding.readdir(pathModule.toNamespacedPath(path),
777-
options.encoding,
778-
!!options.withFileTypes,
779-
kUsePromises);
840+
if (options.recursive) {
841+
return readdirRecursive(path, options);
842+
}
843+
const result = await binding.readdir(
844+
pathModule.toNamespacedPath(path),
845+
options.encoding,
846+
!!options.withFileTypes,
847+
kUsePromises,
848+
);
780849
return options.withFileTypes ?
781850
getDirectoryEntriesPromise(path, result) :
782851
result;

‎lib/internal/fs/utils.js

+14-11
Original file line numberDiff line numberDiff line change
@@ -161,8 +161,9 @@ function assertEncoding(encoding) {
161161
}
162162

163163
class Dirent {
164-
constructor(name, type) {
164+
constructor(name, type, path) {
165165
this.name = name;
166+
this.path = path;
166167
this[kType] = type;
167168
}
168169

@@ -196,8 +197,8 @@ class Dirent {
196197
}
197198

198199
class DirentFromStats extends Dirent {
199-
constructor(name, stats) {
200-
super(name, null);
200+
constructor(name, stats, path) {
201+
super(name, null, path);
201202
this[kStats] = stats;
202203
}
203204
}
@@ -232,7 +233,7 @@ function join(path, name) {
232233
}
233234

234235
if (typeof path === 'string' && typeof name === 'string') {
235-
return pathModule.join(path, name);
236+
return pathModule.basename(path) === name ? path : pathModule.join(path, name);
236237
}
237238

238239
if (isUint8Array(path) && isUint8Array(name)) {
@@ -267,13 +268,13 @@ function getDirents(path, { 0: names, 1: types }, callback) {
267268
callback(err);
268269
return;
269270
}
270-
names[idx] = new DirentFromStats(name, stats);
271+
names[idx] = new DirentFromStats(name, stats, path);
271272
if (--toFinish === 0) {
272273
callback(null, names);
273274
}
274275
});
275276
} else {
276-
names[i] = new Dirent(names[i], types[i]);
277+
names[i] = new Dirent(names[i], types[i], path);
277278
}
278279
}
279280
if (toFinish === 0) {
@@ -303,16 +304,17 @@ function getDirent(path, name, type, callback) {
303304
callback(err);
304305
return;
305306
}
306-
callback(null, new DirentFromStats(name, stats));
307+
callback(null, new DirentFromStats(name, stats, filepath));
307308
});
308309
} else {
309-
callback(null, new Dirent(name, type));
310+
callback(null, new Dirent(name, type, path));
310311
}
311312
} else if (type === UV_DIRENT_UNKNOWN) {
312-
const stats = lazyLoadFs().lstatSync(join(path, name));
313-
return new DirentFromStats(name, stats);
313+
const filepath = join(path, name);
314+
const stats = lazyLoadFs().lstatSync(filepath);
315+
return new DirentFromStats(name, stats, path);
314316
} else {
315-
return new Dirent(name, type);
317+
return new Dirent(name, type, path);
316318
}
317319
}
318320

@@ -335,6 +337,7 @@ function getOptions(options, defaultOptions = kEmptyObject) {
335337
if (options.signal !== undefined) {
336338
validateAbortSignal(options.signal, 'options.signal');
337339
}
340+
338341
return options;
339342
}
340343

Original file line numberDiff line numberDiff line change
@@ -0,0 +1,220 @@
1+
'use strict';
2+
3+
const common = require('../common');
4+
const assert = require('assert');
5+
const fs = require('fs');
6+
const fsPromises = fs.promises;
7+
const pathModule = require('path');
8+
const tmpdir = require('../common/tmpdir');
9+
10+
const testDir = tmpdir.path;
11+
12+
const fileStructure = [
13+
[ 'a', [ 'foo', 'bar' ] ],
14+
[ 'b', [ 'foo', 'bar' ] ],
15+
[ 'c', [ 'foo', 'bar' ] ],
16+
[ 'd', [ 'foo', 'bar' ] ],
17+
[ 'e', [ 'foo', 'bar' ] ],
18+
[ 'f', [ 'foo', 'bar' ] ],
19+
[ 'g', [ 'foo', 'bar' ] ],
20+
[ 'h', [ 'foo', 'bar' ] ],
21+
[ 'i', [ 'foo', 'bar' ] ],
22+
[ 'j', [ 'foo', 'bar' ] ],
23+
[ 'k', [ 'foo', 'bar' ] ],
24+
[ 'l', [ 'foo', 'bar' ] ],
25+
[ 'm', [ 'foo', 'bar' ] ],
26+
[ 'n', [ 'foo', 'bar' ] ],
27+
[ 'o', [ 'foo', 'bar' ] ],
28+
[ 'p', [ 'foo', 'bar' ] ],
29+
[ 'q', [ 'foo', 'bar' ] ],
30+
[ 'r', [ 'foo', 'bar' ] ],
31+
[ 's', [ 'foo', 'bar' ] ],
32+
[ 't', [ 'foo', 'bar' ] ],
33+
[ 'u', [ 'foo', 'bar' ] ],
34+
[ 'v', [ 'foo', 'bar' ] ],
35+
[ 'w', [ 'foo', 'bar' ] ],
36+
[ 'x', [ 'foo', 'bar' ] ],
37+
[ 'y', [ 'foo', 'bar' ] ],
38+
[ 'z', [ 'foo', 'bar' ] ],
39+
[ 'aa', [ 'foo', 'bar' ] ],
40+
[ 'bb', [ 'foo', 'bar' ] ],
41+
[ 'cc', [ 'foo', 'bar' ] ],
42+
[ 'dd', [ 'foo', 'bar' ] ],
43+
[ 'ee', [ 'foo', 'bar' ] ],
44+
[ 'ff', [ 'foo', 'bar' ] ],
45+
[ 'gg', [ 'foo', 'bar' ] ],
46+
[ 'hh', [ 'foo', 'bar' ] ],
47+
[ 'ii', [ 'foo', 'bar' ] ],
48+
[ 'jj', [ 'foo', 'bar' ] ],
49+
[ 'kk', [ 'foo', 'bar' ] ],
50+
[ 'll', [ 'foo', 'bar' ] ],
51+
[ 'mm', [ 'foo', 'bar' ] ],
52+
[ 'nn', [ 'foo', 'bar' ] ],
53+
[ 'oo', [ 'foo', 'bar' ] ],
54+
[ 'pp', [ 'foo', 'bar' ] ],
55+
[ 'qq', [ 'foo', 'bar' ] ],
56+
[ 'rr', [ 'foo', 'bar' ] ],
57+
[ 'ss', [ 'foo', 'bar' ] ],
58+
[ 'tt', [ 'foo', 'bar' ] ],
59+
[ 'uu', [ 'foo', 'bar' ] ],
60+
[ 'vv', [ 'foo', 'bar' ] ],
61+
[ 'ww', [ 'foo', 'bar' ] ],
62+
[ 'xx', [ 'foo', 'bar' ] ],
63+
[ 'yy', [ 'foo', 'bar' ] ],
64+
[ 'zz', [ 'foo', 'bar' ] ],
65+
[ 'abc', [ ['def', [ 'foo', 'bar' ] ], ['ghi', [ 'foo', 'bar' ] ] ] ],
66+
];
67+
68+
function createFiles(path, fileStructure) {
69+
for (const fileOrDir of fileStructure) {
70+
if (typeof fileOrDir === 'string') {
71+
fs.writeFileSync(pathModule.join(path, fileOrDir), '');
72+
} else {
73+
const dirPath = pathModule.join(path, fileOrDir[0]);
74+
fs.mkdirSync(dirPath);
75+
createFiles(dirPath, fileOrDir[1]);
76+
}
77+
}
78+
}
79+
80+
// Make sure tmp directory is clean
81+
tmpdir.refresh();
82+
83+
createFiles(testDir, fileStructure);
84+
const symlinksRootPath = pathModule.join(testDir, 'symlinks');
85+
const symlinkTargetFile = pathModule.join(symlinksRootPath, 'symlink-target-file');
86+
const symlinkTargetDir = pathModule.join(symlinksRootPath, 'symlink-target-dir');
87+
fs.mkdirSync(symlinksRootPath);
88+
fs.writeFileSync(symlinkTargetFile, '');
89+
fs.mkdirSync(symlinkTargetDir);
90+
fs.symlinkSync(symlinkTargetFile, pathModule.join(symlinksRootPath, 'symlink-src-file'));
91+
fs.symlinkSync(symlinkTargetDir, pathModule.join(symlinksRootPath, 'symlink-src-dir'));
92+
93+
const expected = [
94+
'a', 'a/bar', 'a/foo', 'aa', 'aa/bar', 'aa/foo',
95+
'abc', 'abc/def', 'abc/def/bar', 'abc/def/foo', 'abc/ghi', 'abc/ghi/bar', 'abc/ghi/foo',
96+
'b', 'b/bar', 'b/foo', 'bb', 'bb/bar', 'bb/foo',
97+
'c', 'c/bar', 'c/foo', 'cc', 'cc/bar', 'cc/foo',
98+
'd', 'd/bar', 'd/foo', 'dd', 'dd/bar', 'dd/foo',
99+
'e', 'e/bar', 'e/foo', 'ee', 'ee/bar', 'ee/foo',
100+
'f', 'f/bar', 'f/foo', 'ff', 'ff/bar', 'ff/foo',
101+
'g', 'g/bar', 'g/foo', 'gg', 'gg/bar', 'gg/foo',
102+
'h', 'h/bar', 'h/foo', 'hh', 'hh/bar', 'hh/foo',
103+
'i', 'i/bar', 'i/foo', 'ii', 'ii/bar', 'ii/foo',
104+
'j', 'j/bar', 'j/foo', 'jj', 'jj/bar', 'jj/foo',
105+
'k', 'k/bar', 'k/foo', 'kk', 'kk/bar', 'kk/foo',
106+
'l', 'l/bar', 'l/foo', 'll', 'll/bar', 'll/foo',
107+
'm', 'm/bar', 'm/foo', 'mm', 'mm/bar', 'mm/foo',
108+
'n', 'n/bar', 'n/foo', 'nn', 'nn/bar', 'nn/foo',
109+
'o', 'o/bar', 'o/foo', 'oo', 'oo/bar', 'oo/foo',
110+
'p', 'p/bar', 'p/foo', 'pp', 'pp/bar', 'pp/foo',
111+
'q', 'q/bar', 'q/foo', 'qq', 'qq/bar', 'qq/foo',
112+
'r', 'r/bar', 'r/foo', 'rr', 'rr/bar', 'rr/foo',
113+
's', 's/bar', 's/foo', 'ss', 'ss/bar', 'ss/foo',
114+
'symlinks', 'symlinks/symlink-src-dir', 'symlinks/symlink-src-file',
115+
'symlinks/symlink-target-dir', 'symlinks/symlink-target-file',
116+
't', 't/bar', 't/foo', 'tt', 'tt/bar', 'tt/foo',
117+
'u', 'u/bar', 'u/foo', 'uu', 'uu/bar', 'uu/foo',
118+
'v', 'v/bar', 'v/foo', 'vv', 'vv/bar', 'vv/foo',
119+
'w', 'w/bar', 'w/foo', 'ww', 'ww/bar', 'ww/foo',
120+
'x', 'x/bar', 'x/foo', 'xx', 'xx/bar', 'xx/foo',
121+
'y', 'y/bar', 'y/foo', 'yy', 'yy/bar', 'yy/foo',
122+
'z', 'z/bar', 'z/foo', 'zz', 'zz/bar', 'zz/foo',
123+
];
124+
125+
// Normalize paths once for non POSIX platforms
126+
for (let i = 0; i < expected.length; i++) {
127+
expected[i] = pathModule.normalize(expected[i]);
128+
}
129+
130+
function getDirentPath(dirent) {
131+
return pathModule.relative(testDir, dirent.path);
132+
}
133+
134+
function assertDirents(dirents) {
135+
dirents.sort((a, b) => (getDirentPath(a) < getDirentPath(b) ? -1 : 1));
136+
for (const [i, dirent] of dirents.entries()) {
137+
assert(dirent instanceof fs.Dirent);
138+
assert.strictEqual(getDirentPath(dirent), expected[i]);
139+
}
140+
}
141+
142+
function processDirSync(dir) {
143+
const dirents = [];
144+
let dirent = dir.readSync();
145+
while (dirent !== null) {
146+
dirents.push(dirent);
147+
dirent = dir.readSync();
148+
}
149+
assertDirents(dirents);
150+
}
151+
152+
// Opendir read results sync
153+
154+
{
155+
const dir = fs.opendirSync(testDir, { recursive: true });
156+
processDirSync(dir);
157+
dir.closeSync();
158+
}
159+
160+
{
161+
fs.opendir(testDir, { recursive: true }, common.mustSucceed((dir) => {
162+
processDirSync(dir);
163+
dir.close(common.mustSucceed());
164+
}));
165+
}
166+
167+
// Opendir read result using callback
168+
169+
function processDirCb(dir, cb) {
170+
const acc = [];
171+
172+
function _process(dir, acc, cb) {
173+
dir.read((err, dirent) => {
174+
if (err) {
175+
return cb(err);
176+
}
177+
178+
if (dirent !== null) {
179+
acc.push(dirent);
180+
_process(dir, acc, cb);
181+
} else {
182+
cb(null, acc);
183+
}
184+
});
185+
}
186+
187+
_process(dir, acc, cb);
188+
}
189+
190+
{
191+
const dir = fs.opendirSync(testDir, { recursive: true });
192+
processDirCb(dir, common.mustSucceed((dirents) => {
193+
assertDirents(dirents);
194+
dir.close(common.mustSucceed());
195+
}));
196+
}
197+
198+
{
199+
fs.opendir(testDir, { recursive: true }, common.mustSucceed((dir) => {
200+
processDirCb(dir, common.mustSucceed((dirents) => {
201+
assertDirents(dirents);
202+
dir.close(common.mustSucceed());
203+
}));
204+
}));
205+
}
206+
207+
// Opendir read result using AsyncIterator
208+
209+
{
210+
async function test() {
211+
const dir = await fsPromises.opendir(testDir, { recursive: true });
212+
const dirents = [];
213+
for await (const dirent of dir) {
214+
dirents.push(dirent);
215+
}
216+
assertDirents(dirents);
217+
}
218+
219+
test().then(common.mustCall());
220+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,193 @@
1+
'use strict';
2+
3+
const common = require('../common');
4+
const assert = require('assert');
5+
const fs = require('fs');
6+
const pathModule = require('path');
7+
const tmpdir = require('../common/tmpdir');
8+
9+
const readdirDir = tmpdir.path;
10+
11+
const fileStructure = [
12+
[ 'a', [ 'foo', 'bar' ] ],
13+
[ 'b', [ 'foo', 'bar' ] ],
14+
[ 'c', [ 'foo', 'bar' ] ],
15+
[ 'd', [ 'foo', 'bar' ] ],
16+
[ 'e', [ 'foo', 'bar' ] ],
17+
[ 'f', [ 'foo', 'bar' ] ],
18+
[ 'g', [ 'foo', 'bar' ] ],
19+
[ 'h', [ 'foo', 'bar' ] ],
20+
[ 'i', [ 'foo', 'bar' ] ],
21+
[ 'j', [ 'foo', 'bar' ] ],
22+
[ 'k', [ 'foo', 'bar' ] ],
23+
[ 'l', [ 'foo', 'bar' ] ],
24+
[ 'm', [ 'foo', 'bar' ] ],
25+
[ 'n', [ 'foo', 'bar' ] ],
26+
[ 'o', [ 'foo', 'bar' ] ],
27+
[ 'p', [ 'foo', 'bar' ] ],
28+
[ 'q', [ 'foo', 'bar' ] ],
29+
[ 'r', [ 'foo', 'bar' ] ],
30+
[ 's', [ 'foo', 'bar' ] ],
31+
[ 't', [ 'foo', 'bar' ] ],
32+
[ 'u', [ 'foo', 'bar' ] ],
33+
[ 'v', [ 'foo', 'bar' ] ],
34+
[ 'w', [ 'foo', 'bar' ] ],
35+
[ 'x', [ 'foo', 'bar' ] ],
36+
[ 'y', [ 'foo', 'bar' ] ],
37+
[ 'z', [ 'foo', 'bar' ] ],
38+
[ 'aa', [ 'foo', 'bar' ] ],
39+
[ 'bb', [ 'foo', 'bar' ] ],
40+
[ 'cc', [ 'foo', 'bar' ] ],
41+
[ 'dd', [ 'foo', 'bar' ] ],
42+
[ 'ee', [ 'foo', 'bar' ] ],
43+
[ 'ff', [ 'foo', 'bar' ] ],
44+
[ 'gg', [ 'foo', 'bar' ] ],
45+
[ 'hh', [ 'foo', 'bar' ] ],
46+
[ 'ii', [ 'foo', 'bar' ] ],
47+
[ 'jj', [ 'foo', 'bar' ] ],
48+
[ 'kk', [ 'foo', 'bar' ] ],
49+
[ 'll', [ 'foo', 'bar' ] ],
50+
[ 'mm', [ 'foo', 'bar' ] ],
51+
[ 'nn', [ 'foo', 'bar' ] ],
52+
[ 'oo', [ 'foo', 'bar' ] ],
53+
[ 'pp', [ 'foo', 'bar' ] ],
54+
[ 'qq', [ 'foo', 'bar' ] ],
55+
[ 'rr', [ 'foo', 'bar' ] ],
56+
[ 'ss', [ 'foo', 'bar' ] ],
57+
[ 'tt', [ 'foo', 'bar' ] ],
58+
[ 'uu', [ 'foo', 'bar' ] ],
59+
[ 'vv', [ 'foo', 'bar' ] ],
60+
[ 'ww', [ 'foo', 'bar' ] ],
61+
[ 'xx', [ 'foo', 'bar' ] ],
62+
[ 'yy', [ 'foo', 'bar' ] ],
63+
[ 'zz', [ 'foo', 'bar' ] ],
64+
[ 'abc', [ ['def', [ 'foo', 'bar' ] ], ['ghi', [ 'foo', 'bar' ] ] ] ],
65+
];
66+
67+
function createFiles(path, fileStructure) {
68+
for (const fileOrDir of fileStructure) {
69+
if (typeof fileOrDir === 'string') {
70+
fs.writeFileSync(pathModule.join(path, fileOrDir), '');
71+
} else {
72+
const dirPath = pathModule.join(path, fileOrDir[0]);
73+
fs.mkdirSync(dirPath);
74+
createFiles(dirPath, fileOrDir[1]);
75+
}
76+
}
77+
}
78+
79+
// Make sure tmp directory is clean
80+
tmpdir.refresh();
81+
82+
createFiles(readdirDir, fileStructure);
83+
const symlinksRootPath = pathModule.join(readdirDir, 'symlinks');
84+
const symlinkTargetFile = pathModule.join(symlinksRootPath, 'symlink-target-file');
85+
const symlinkTargetDir = pathModule.join(symlinksRootPath, 'symlink-target-dir');
86+
fs.mkdirSync(symlinksRootPath);
87+
fs.writeFileSync(symlinkTargetFile, '');
88+
fs.mkdirSync(symlinkTargetDir);
89+
fs.symlinkSync(symlinkTargetFile, pathModule.join(symlinksRootPath, 'symlink-src-file'));
90+
fs.symlinkSync(symlinkTargetDir, pathModule.join(symlinksRootPath, 'symlink-src-dir'));
91+
92+
const expected = [
93+
'a', 'a/bar', 'a/foo', 'aa', 'aa/bar', 'aa/foo',
94+
'abc', 'abc/def', 'abc/def/bar', 'abc/def/foo', 'abc/ghi', 'abc/ghi/bar', 'abc/ghi/foo',
95+
'b', 'b/bar', 'b/foo', 'bb', 'bb/bar', 'bb/foo',
96+
'c', 'c/bar', 'c/foo', 'cc', 'cc/bar', 'cc/foo',
97+
'd', 'd/bar', 'd/foo', 'dd', 'dd/bar', 'dd/foo',
98+
'e', 'e/bar', 'e/foo', 'ee', 'ee/bar', 'ee/foo',
99+
'f', 'f/bar', 'f/foo', 'ff', 'ff/bar', 'ff/foo',
100+
'g', 'g/bar', 'g/foo', 'gg', 'gg/bar', 'gg/foo',
101+
'h', 'h/bar', 'h/foo', 'hh', 'hh/bar', 'hh/foo',
102+
'i', 'i/bar', 'i/foo', 'ii', 'ii/bar', 'ii/foo',
103+
'j', 'j/bar', 'j/foo', 'jj', 'jj/bar', 'jj/foo',
104+
'k', 'k/bar', 'k/foo', 'kk', 'kk/bar', 'kk/foo',
105+
'l', 'l/bar', 'l/foo', 'll', 'll/bar', 'll/foo',
106+
'm', 'm/bar', 'm/foo', 'mm', 'mm/bar', 'mm/foo',
107+
'n', 'n/bar', 'n/foo', 'nn', 'nn/bar', 'nn/foo',
108+
'o', 'o/bar', 'o/foo', 'oo', 'oo/bar', 'oo/foo',
109+
'p', 'p/bar', 'p/foo', 'pp', 'pp/bar', 'pp/foo',
110+
'q', 'q/bar', 'q/foo', 'qq', 'qq/bar', 'qq/foo',
111+
'r', 'r/bar', 'r/foo', 'rr', 'rr/bar', 'rr/foo',
112+
's', 's/bar', 's/foo', 'ss', 'ss/bar', 'ss/foo',
113+
'symlinks', 'symlinks/symlink-src-dir', 'symlinks/symlink-src-file',
114+
'symlinks/symlink-target-dir', 'symlinks/symlink-target-file',
115+
't', 't/bar', 't/foo', 'tt', 'tt/bar', 'tt/foo',
116+
'u', 'u/bar', 'u/foo', 'uu', 'uu/bar', 'uu/foo',
117+
'v', 'v/bar', 'v/foo', 'vv', 'vv/bar', 'vv/foo',
118+
'w', 'w/bar', 'w/foo', 'ww', 'ww/bar', 'ww/foo',
119+
'x', 'x/bar', 'x/foo', 'xx', 'xx/bar', 'xx/foo',
120+
'y', 'y/bar', 'y/foo', 'yy', 'yy/bar', 'yy/foo',
121+
'z', 'z/bar', 'z/foo', 'zz', 'zz/bar', 'zz/foo',
122+
];
123+
124+
// Normalize paths once for non POSIX platforms
125+
for (let i = 0; i < expected.length; i++) {
126+
expected[i] = pathModule.normalize(expected[i]);
127+
}
128+
129+
function getDirentPath(dirent) {
130+
return pathModule.relative(readdirDir, pathModule.join(dirent.path, dirent.name));
131+
}
132+
133+
function assertDirents(dirents) {
134+
dirents.sort((a, b) => (getDirentPath(a) < getDirentPath(b) ? -1 : 1));
135+
for (const [i, dirent] of dirents.entries()) {
136+
assert(dirent instanceof fs.Dirent);
137+
assert.strictEqual(getDirentPath(dirent), expected[i]);
138+
}
139+
}
140+
141+
// readdirSync
142+
143+
// readdirSync { recursive }
144+
{
145+
const result = fs.readdirSync(readdirDir, { recursive: true });
146+
assert.deepStrictEqual(result.sort(), expected);
147+
}
148+
149+
// readdirSync { recursive, withFileTypes }
150+
{
151+
const result = fs.readdirSync(readdirDir, { recursive: true, withFileTypes: true });
152+
assertDirents(result);
153+
}
154+
155+
// readdir
156+
157+
// readdir { recursive } callback
158+
{
159+
fs.readdir(readdirDir, { recursive: true },
160+
common.mustSucceed((result) => {
161+
assert.deepStrictEqual(result.sort(), expected);
162+
}));
163+
}
164+
165+
// Readdir { recursive, withFileTypes } callback
166+
{
167+
fs.readdir(readdirDir, { recursive: true, withFileTypes: true },
168+
common.mustSucceed((result) => {
169+
assertDirents(result);
170+
}));
171+
}
172+
173+
// fs.promises.readdir
174+
175+
// fs.promises.readdir { recursive }
176+
{
177+
async function test() {
178+
const result = await fs.promises.readdir(readdirDir, { recursive: true });
179+
assert.deepStrictEqual(result.sort(), expected);
180+
}
181+
182+
test().then(common.mustCall());
183+
}
184+
185+
// fs.promises.readdir { recursive, withFileTypes }
186+
{
187+
async function test() {
188+
const result = await fs.promises.readdir(readdirDir, { recursive: true, withFileTypes: true });
189+
assertDirents(result);
190+
}
191+
192+
test().then(common.mustCall());
193+
}

0 commit comments

Comments
 (0)
Please sign in to comment.