From 8b8baf5ca92c137d6afd1512508f077fc307f3bd Mon Sep 17 00:00:00 2001 From: jazelly Date: Sun, 1 Sep 2024 10:09:05 +0930 Subject: [PATCH] fix: let filesystem api to handle polymorphism --- src/node_file.cc | 31 ++++++++++++++++++++++++++++--- src/util.h | 18 ------------------ 2 files changed, 28 insertions(+), 21 deletions(-) diff --git a/src/node_file.cc b/src/node_file.cc index acf0ed2d8e83bf..ed23f0f17018bf 100644 --- a/src/node_file.cc +++ b/src/node_file.cc @@ -3130,15 +3130,40 @@ static void CpSyncCheckPaths(const FunctionCallbackInfo& args) { ToNamespacedPath(env, &src); THROW_IF_INSUFFICIENT_PERMISSIONS( env, permission::PermissionScope::kFileSystemRead, src.ToStringView()); - auto src_path = std::filesystem::path(src.ToWString()); +#ifdef _WIN32 + auto src_size_needed = MultiByteToWideChar( + CP_UTF8, 0, src.out(), static_cast(src.length()), nullptr, 0); + std::wstring src_wstr(src_size_needed, 0); + MultiByteToWideChar(CP_UTF8, + 0, + src.out(), + static_cast(src.length()), + &src_wstr[0], + src_size_needed); + auto src_path = std::filesystem::path(src_wstr); +#else + auto src_path = std::filesystem::path(src.ToStringView()); +#endif BufferValue dest(isolate, args[1]); CHECK_NOT_NULL(*dest); ToNamespacedPath(env, &dest); THROW_IF_INSUFFICIENT_PERMISSIONS( env, permission::PermissionScope::kFileSystemWrite, dest.ToStringView()); - auto dest_path = std::filesystem::path(dest.ToWString()); - +#ifdef _WIN32 + auto dest_size_needed = MultiByteToWideChar( + CP_UTF8, 0, dest.out(), static_cast(dest.length()), nullptr, 0); + std::wstring dest_wstr(dest_size_needed, 0); + MultiByteToWideChar(CP_UTF8, + 0, + dest.out(), + static_cast(dest.length()), + &dest_wstr[0], + dest_size_needed); + auto dest_path = std::filesystem::path(dest_wstr); +#else + auto dest_path = std::filesystem::path(dest.ToStringView()); +#endif bool dereference = args[2]->IsTrue(); bool recursive = args[3]->IsTrue(); diff --git a/src/util.h b/src/util.h index 66d951e0336de5..a6da8720c499df 100644 --- a/src/util.h +++ b/src/util.h @@ -126,10 +126,6 @@ void DumpJavaScriptBacktrace(FILE* fp); #define ABORT_NO_BACKTRACE() abort() #endif -#ifdef _WIN32 -#include -#endif - // Caller of this macro must not be marked as [[noreturn]]. Printing of // backtraces may not work correctly in [[noreturn]] functions because // when generating code for them the compiler can choose not to @@ -566,20 +562,6 @@ class BufferValue : public MaybeStackBuffer { inline std::string_view ToStringView() const { return std::string_view(out(), length()); } - inline std::wstring ToWString() const { -#ifdef _WIN32 - auto size_needed = MultiByteToWideChar( - CP_UTF8, 0, out(), static_cast(length()), nullptr, 0); - std::wstring wstrTo(length(), 0); - MultiByteToWideChar( - CP_UTF8, 0, out(), static_cast(length()), &wstrTo[0], size_needed); -#else - auto size_needed = std::mbstowcs(nullptr, out(), 0); - std::wstring wstrTo(size_needed, L'\0'); - std::mbstowcs(&wstrTo[0], out(), length()); -#endif - return wstrTo; - } }; #define SPREAD_BUFFER_ARG(val, name) \