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: fix bufferSize option for opendir recursive #55744

Merged
merged 1 commit into from
Nov 8, 2024
Merged
Show file tree
Hide file tree
Changes from all 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
12 changes: 8 additions & 4 deletions lib/internal/fs/dir.js
Original file line number Diff line number Diff line change
Expand Up @@ -164,12 +164,16 @@ class Dir {
return;
}

const result = handle.read(
// Fully read the directory and buffer the entries.
// This is a naive solution and for very large sub-directories
// it can even block the event loop. Essentially, `bufferSize` is
// not respected for recursive mode. This is a known limitation.
// Issue to fix: https://github.com/nodejs/node/issues/55764
let result;
while ((result = handle.read(
this.#options.encoding,
this.#options.bufferSize,
);

if (result) {
))) {
this.processReadResult(path, result);
}

Expand Down
19 changes: 19 additions & 0 deletions test/sequential/test-fs-opendir-recursive.js
Original file line number Diff line number Diff line change
Expand Up @@ -132,6 +132,7 @@ function getDirentPath(dirent) {
}

function assertDirents(dirents) {
assert.strictEqual(dirents.length, expected.length);
dirents.sort((a, b) => (getDirentPath(a) < getDirentPath(b) ? -1 : 1));
assert.deepStrictEqual(
dirents.map((dirent) => {
Expand Down Expand Up @@ -221,3 +222,21 @@ function processDirCb(dir, cb) {

test().then(common.mustCall());
}

// Issue https://github.com/nodejs/node/issues/48820 highlights that
// opendir recursive does not properly handle the buffer size option.
// This test asserts that the buffer size option is respected.
{
const dir = fs.opendirSync(testDir, { bufferSize: 1, recursive: true });
processDirSync(dir);
dir.closeSync();
}

{
fs.opendir(testDir, { recursive: true, bufferSize: 1 }, common.mustSucceed((dir) => {
processDirCb(dir, common.mustSucceed((dirents) => {
assertDirents(dirents);
dir.close(common.mustSucceed());
}));
}));
}
Loading