From f184a0aca0493fba27b762d1bd8985bc06711635 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?H=C3=BCseyin=20A=C3=A7acak?= <110401522+huseyinacacak-janea@users.noreply.github.com> Date: Tue, 10 Dec 2024 16:31:21 +0300 Subject: [PATCH] fs,win: fix readdir for named pipe MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit PR-URL: https://github.com/nodejs/node/pull/56110 Fixes: https://github.com/nodejs/node/issues/56002 Refs: https://github.com/nodejs/node/pull/55623 Refs: https://github.com/nodejs/node/pull/56088 Reviewed-By: Gerhard Stöbich Reviewed-By: Luigi Pinca --- src/node_file.cc | 21 +++++++++++++++++++++ test/parallel/test-fs-readdir-pipe.js | 21 +++++++++++++++++++++ 2 files changed, 42 insertions(+) create mode 100644 test/parallel/test-fs-readdir-pipe.js diff --git a/src/node_file.cc b/src/node_file.cc index 5a50aacb1b939d..34a86ef7f140d7 100644 --- a/src/node_file.cc +++ b/src/node_file.cc @@ -1986,8 +1986,29 @@ static void ReadDir(const FunctionCallbackInfo& 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; + 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(); diff --git a/test/parallel/test-fs-readdir-pipe.js b/test/parallel/test-fs-readdir-pipe.js new file mode 100644 index 00000000000000..592e7a3d54009f --- /dev/null +++ b/test/parallel/test-fs-readdir-pipe.js @@ -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`); +}));