Skip to content

Commit

Permalink
fix: let filesystem api to handle polymorphism
Browse files Browse the repository at this point in the history
  • Loading branch information
jazelly committed Sep 1, 2024
1 parent a09e59f commit 8b8baf5
Show file tree
Hide file tree
Showing 2 changed files with 28 additions and 21 deletions.
31 changes: 28 additions & 3 deletions src/node_file.cc
Original file line number Diff line number Diff line change
Expand Up @@ -3130,15 +3130,40 @@ static void CpSyncCheckPaths(const FunctionCallbackInfo<Value>& 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<int>(src.length()), nullptr, 0);
std::wstring src_wstr(src_size_needed, 0);
MultiByteToWideChar(CP_UTF8,
0,
src.out(),
static_cast<int>(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<int>(dest.length()), nullptr, 0);
std::wstring dest_wstr(dest_size_needed, 0);
MultiByteToWideChar(CP_UTF8,
0,
dest.out(),
static_cast<int>(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();

Expand Down
18 changes: 0 additions & 18 deletions src/util.h
Original file line number Diff line number Diff line change
Expand Up @@ -126,10 +126,6 @@ void DumpJavaScriptBacktrace(FILE* fp);
#define ABORT_NO_BACKTRACE() abort()
#endif

#ifdef _WIN32
#include <windows.h>
#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
Expand Down Expand Up @@ -566,20 +562,6 @@ class BufferValue : public MaybeStackBuffer<char> {
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<int>(length()), nullptr, 0);
std::wstring wstrTo(length(), 0);
MultiByteToWideChar(
CP_UTF8, 0, out(), static_cast<int>(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) \
Expand Down

0 comments on commit 8b8baf5

Please sign in to comment.