Skip to content

Commit f434191

Browse files
committed
src: use BufferValueToPath for cross-platform path handling
1 parent 3a92727 commit f434191

File tree

1 file changed

+50
-50
lines changed

1 file changed

+50
-50
lines changed

src/node_file.cc

Lines changed: 50 additions & 50 deletions
Original file line numberDiff line numberDiff line change
@@ -1614,6 +1614,55 @@ static void RMDir(const FunctionCallbackInfo<Value>& args) {
16141614
}
16151615
}
16161616

1617+
#ifdef _WIN32
1618+
std::wstring ConvertToWideString(const std::string& str) {
1619+
int size_needed = MultiByteToWideChar(
1620+
CP_UTF8, 0, &str[0], static_cast<int>(str.size()), nullptr, 0);
1621+
std::wstring wstrTo(size_needed, 0);
1622+
MultiByteToWideChar(CP_UTF8,
1623+
0,
1624+
&str[0],
1625+
static_cast<int>(str.size()),
1626+
&wstrTo[0],
1627+
size_needed);
1628+
return wstrTo;
1629+
}
1630+
1631+
#define BufferValueToPath(str) \
1632+
std::filesystem::path(ConvertToWideString(str.ToString()))
1633+
1634+
std::string ConvertWideToUTF8(const std::wstring& wstr) {
1635+
if (wstr.empty()) return std::string();
1636+
1637+
int size_needed = WideCharToMultiByte(CP_UTF8,
1638+
0,
1639+
&wstr[0],
1640+
static_cast<int>(wstr.size()),
1641+
nullptr,
1642+
0,
1643+
nullptr,
1644+
nullptr);
1645+
std::string strTo(size_needed, 0);
1646+
WideCharToMultiByte(CP_UTF8,
1647+
0,
1648+
&wstr[0],
1649+
static_cast<int>(wstr.size()),
1650+
&strTo[0],
1651+
size_needed,
1652+
nullptr,
1653+
nullptr);
1654+
return strTo;
1655+
}
1656+
1657+
#define PathToString(path) ConvertWideToUTF8(path.wstring());
1658+
1659+
#else // _WIN32
1660+
1661+
#define BufferValueToPath(str) std::filesystem::path(str.ToStringView());
1662+
#define PathToString(path) path.native();
1663+
1664+
#endif // _WIN32
1665+
16171666
static void RmSync(const FunctionCallbackInfo<Value>& args) {
16181667
Environment* env = Environment::GetCurrent(args);
16191668
Isolate* isolate = env->isolate();
@@ -1625,7 +1674,7 @@ static void RmSync(const FunctionCallbackInfo<Value>& args) {
16251674
ToNamespacedPath(env, &path);
16261675
THROW_IF_INSUFFICIENT_PERMISSIONS(
16271676
env, permission::PermissionScope::kFileSystemWrite, path.ToStringView());
1628-
auto file_path = std::filesystem::path(path.ToU8StringView());
1677+
auto file_path = BufferValueToPath(path);
16291678
std::error_code error;
16301679
auto file_status = std::filesystem::status(file_path, error);
16311680

@@ -3125,55 +3174,6 @@ static void GetFormatOfExtensionlessFile(
31253174
return args.GetReturnValue().Set(EXTENSIONLESS_FORMAT_JAVASCRIPT);
31263175
}
31273176

3128-
#ifdef _WIN32
3129-
std::wstring ConvertToWideString(const std::string& str) {
3130-
int size_needed = MultiByteToWideChar(
3131-
CP_UTF8, 0, &str[0], static_cast<int>(str.size()), nullptr, 0);
3132-
std::wstring wstrTo(size_needed, 0);
3133-
MultiByteToWideChar(CP_UTF8,
3134-
0,
3135-
&str[0],
3136-
static_cast<int>(str.size()),
3137-
&wstrTo[0],
3138-
size_needed);
3139-
return wstrTo;
3140-
}
3141-
3142-
#define BufferValueToPath(str) \
3143-
std::filesystem::path(ConvertToWideString(str.ToString()))
3144-
3145-
std::string ConvertWideToUTF8(const std::wstring& wstr) {
3146-
if (wstr.empty()) return std::string();
3147-
3148-
int size_needed = WideCharToMultiByte(CP_UTF8,
3149-
0,
3150-
&wstr[0],
3151-
static_cast<int>(wstr.size()),
3152-
nullptr,
3153-
0,
3154-
nullptr,
3155-
nullptr);
3156-
std::string strTo(size_needed, 0);
3157-
WideCharToMultiByte(CP_UTF8,
3158-
0,
3159-
&wstr[0],
3160-
static_cast<int>(wstr.size()),
3161-
&strTo[0],
3162-
size_needed,
3163-
nullptr,
3164-
nullptr);
3165-
return strTo;
3166-
}
3167-
3168-
#define PathToString(path) ConvertWideToUTF8(path.wstring());
3169-
3170-
#else // _WIN32
3171-
3172-
#define BufferValueToPath(str) std::filesystem::path(str.ToStringView());
3173-
#define PathToString(path) path.native();
3174-
3175-
#endif // _WIN32
3176-
31773177
static void CpSyncCheckPaths(const FunctionCallbackInfo<Value>& args) {
31783178
Environment* env = Environment::GetCurrent(args);
31793179
Isolate* isolate = env->isolate();

0 commit comments

Comments
 (0)