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,win: fix readdir for named pipe #56110

Merged
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
21 changes: 21 additions & 0 deletions src/node_file.cc
Original file line number Diff line number Diff line change
Expand Up @@ -1986,8 +1986,29 @@ static void ReadDir(const FunctionCallbackInfo<Value>& args) {

BufferValue path(isolate, args[0]);
CHECK_NOT_NULL(*path);
#ifdef _WIN32
// On Windows, some API functions accept paths with trailing slashes,
// while others do not. This code checks if the input path ends with
// a slash (either '/' or '\\') and, if so, ensures that the processed
// path also ends with a trailing backslash ('\\').
bool slashCheck = false;
Flarna marked this conversation as resolved.
Show resolved Hide resolved
if (path.ToStringView().ends_with("/") ||
path.ToStringView().ends_with("\\")) {
slashCheck = true;
}
#endif

ToNamespacedPath(env, &path);

#ifdef _WIN32
if (slashCheck) {
size_t new_length = path.length() + 1;
path.AllocateSufficientStorage(new_length + 1);
path.SetLengthAndZeroTerminate(new_length);
path.out()[new_length - 1] = '\\';
}
#endif

const enum encoding encoding = ParseEncoding(isolate, args[1], UTF8);

bool with_types = args[2]->IsTrue();
Expand Down
21 changes: 21 additions & 0 deletions test/parallel/test-fs-readdir-pipe.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
'use strict';

const common = require('../common');
const assert = require('assert');
const { readdir, readdirSync } = require('fs');

if (!common.isWindows) {
common.skip('This test is specific to Windows to test enumerate pipes');
}

// Ref: https://github.com/nodejs/node/issues/56002
// This test is specific to Windows.

const pipe = '\\\\.\\pipe\\';

const { length } = readdirSync(pipe);
assert.ok(length >= 0, `${length} is not greater or equal to 0`);

readdir(pipe, common.mustSucceed((files) => {
assert.ok(files.length >= 0, `${files.length} is not greater or equal to 0`);
}));
Loading