From 54534dd6067080b83180563e447e3d82260154e2 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?H=C3=BCseyin=20A=C3=A7acak?= Date: Mon, 2 Dec 2024 11:58:01 +0300 Subject: [PATCH 1/4] fs,win: fix readdir for named pipe --- src/node_file.cc | 18 ++++++++++++++++++ test/parallel/test-fs-readdir-pipe.js | 21 +++++++++++++++++++++ 2 files changed, 39 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..cffe42dbbed8ec 100644 --- a/src/node_file.cc +++ b/src/node_file.cc @@ -1986,8 +1986,26 @@ static void ReadDir(const FunctionCallbackInfo& args) { BufferValue path(isolate, args[0]); CHECK_NOT_NULL(*path); +#ifdef _WIN32 + bool slashCheck = false; + if (path.ToStringView().ends_with("/") || + path.ToStringView().ends_with("\\")) { + slashCheck = true; + } +#endif + ToNamespacedPath(env, &path); +#ifdef _WIN32 + if (slashCheck) { + std::string result = path.ToString() + "\\"; + size_t new_length = result.size(); + path.AllocateSufficientStorage(new_length + 1); + path.SetLength(new_length); + memcpy(path.out(), result.c_str(), result.size() + 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..ae207f93e3cb58 --- /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\\'; + +assert.ok(readdirSync(pipe).length >= 0); + +readdir(pipe, common.mustCall((err, files) => { + assert.ok(err == null); + assert.ok(files.length >= 0); +})); From ee8ac1589dc4a928d1e2dd996df52c8ab596639d Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?H=C3=BCseyin=20A=C3=A7acak?= Date: Thu, 5 Dec 2024 12:28:05 +0300 Subject: [PATCH 2/4] fixup! fs,win: fix readdir for named pipe --- src/node_file.cc | 11 +++++++---- 1 file changed, 7 insertions(+), 4 deletions(-) diff --git a/src/node_file.cc b/src/node_file.cc index cffe42dbbed8ec..34a86ef7f140d7 100644 --- a/src/node_file.cc +++ b/src/node_file.cc @@ -1987,6 +1987,10 @@ 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("\\")) { @@ -1998,11 +2002,10 @@ static void ReadDir(const FunctionCallbackInfo& args) { #ifdef _WIN32 if (slashCheck) { - std::string result = path.ToString() + "\\"; - size_t new_length = result.size(); + size_t new_length = path.length() + 1; path.AllocateSufficientStorage(new_length + 1); - path.SetLength(new_length); - memcpy(path.out(), result.c_str(), result.size() + 1); + path.SetLengthAndZeroTerminate(new_length); + path.out()[new_length - 1] = '\\'; } #endif From 66ca939b5027516fdb579c51a1b38d106a43295b 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 09:45:29 +0300 Subject: [PATCH 3/4] Update test/parallel/test-fs-readdir-pipe.js Co-authored-by: Antoine du Hamel --- test/parallel/test-fs-readdir-pipe.js | 5 ++--- 1 file changed, 2 insertions(+), 3 deletions(-) diff --git a/test/parallel/test-fs-readdir-pipe.js b/test/parallel/test-fs-readdir-pipe.js index ae207f93e3cb58..f2bcb0a9c96250 100644 --- a/test/parallel/test-fs-readdir-pipe.js +++ b/test/parallel/test-fs-readdir-pipe.js @@ -15,7 +15,6 @@ const pipe = '\\\\.\\pipe\\'; assert.ok(readdirSync(pipe).length >= 0); -readdir(pipe, common.mustCall((err, files) => { - assert.ok(err == null); - assert.ok(files.length >= 0); +readdir(pipe, common.mustSucceed((files) => { + assert.ok(files.length >= 0, `${files.length} is not greater or equal to 0`); })); From 572569cca80096b9760d5a6cf4de19857c6476f9 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 09:45:36 +0300 Subject: [PATCH 4/4] Update test/parallel/test-fs-readdir-pipe.js Co-authored-by: Antoine du Hamel --- test/parallel/test-fs-readdir-pipe.js | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/test/parallel/test-fs-readdir-pipe.js b/test/parallel/test-fs-readdir-pipe.js index f2bcb0a9c96250..592e7a3d54009f 100644 --- a/test/parallel/test-fs-readdir-pipe.js +++ b/test/parallel/test-fs-readdir-pipe.js @@ -13,7 +13,8 @@ if (!common.isWindows) { const pipe = '\\\\.\\pipe\\'; -assert.ok(readdirSync(pipe).length >= 0); +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`);