Skip to content

Commit

Permalink
fs: fix bufferSize option for opendir recursive
Browse files Browse the repository at this point in the history
The bufferSize option was not respected in recursive mode. This PR
implements a naive solution to fix this issue until a better
implementation can be designed.

Fixes: nodejs#48820
  • Loading branch information
Ethan-Arrowood committed Nov 7, 2024
1 parent 6f12f1e commit 3d1821d
Show file tree
Hide file tree
Showing 2 changed files with 23 additions and 4 deletions.
8 changes: 4 additions & 4 deletions lib/internal/fs/dir.js
Original file line number Diff line number Diff line change
Expand Up @@ -164,12 +164,12 @@ class Dir {
return;
}

const result = handle.read(
// Fully read the directory and buffer the entries.
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());
}));
}));
}

0 comments on commit 3d1821d

Please sign in to comment.