@@ -3033,6 +3033,55 @@ static void GetFormatOfExtensionlessFile(
3033
3033
return args.GetReturnValue ().Set (EXTENSIONLESS_FORMAT_JAVASCRIPT);
3034
3034
}
3035
3035
3036
+ #ifdef _WIN32
3037
+ std::wstring ConvertToWideString (const std::string& str) {
3038
+ int size_needed = MultiByteToWideChar (
3039
+ CP_UTF8, 0 , &str[0 ], static_cast <int >(str.size ()), nullptr , 0 );
3040
+ std::wstring wstrTo (size_needed, 0 );
3041
+ MultiByteToWideChar (CP_UTF8,
3042
+ 0 ,
3043
+ &str[0 ],
3044
+ static_cast <int >(str.size ()),
3045
+ &wstrTo[0 ],
3046
+ size_needed);
3047
+ return wstrTo;
3048
+ }
3049
+
3050
+ #define BufferValueToPath (str ) \
3051
+ std::filesystem::path (ConvertToWideString(str.ToString()))
3052
+
3053
+ std::string ConvertWideToUTF8(const std::wstring& wstr) {
3054
+ if (wstr.empty ()) return std::string ();
3055
+
3056
+ int size_needed = WideCharToMultiByte (CP_UTF8,
3057
+ 0 ,
3058
+ &wstr[0 ],
3059
+ static_cast <int >(wstr.size ()),
3060
+ nullptr ,
3061
+ 0 ,
3062
+ nullptr ,
3063
+ nullptr );
3064
+ std::string strTo (size_needed, 0 );
3065
+ WideCharToMultiByte (CP_UTF8,
3066
+ 0 ,
3067
+ &wstr[0 ],
3068
+ static_cast <int >(wstr.size ()),
3069
+ &strTo[0 ],
3070
+ size_needed,
3071
+ nullptr ,
3072
+ nullptr );
3073
+ return strTo;
3074
+ }
3075
+
3076
+ #define PathToString (path ) ConvertWideToUTF8(path.wstring());
3077
+
3078
+ #else // _WIN32
3079
+
3080
+ #define BufferValueToPath (str ) std::filesystem::path(str.ToStringView());
3081
+ #define PathToString (path ) path.native();
3082
+
3083
+ #endif // _WIN32
3084
+
3036
3085
static void CpSyncCheckPaths (const FunctionCallbackInfo<Value>& args) {
3037
3086
Environment* env = Environment::GetCurrent (args);
3038
3087
Isolate* isolate = env->isolate ();
@@ -3045,15 +3094,15 @@ static void CpSyncCheckPaths(const FunctionCallbackInfo<Value>& args) {
3045
3094
THROW_IF_INSUFFICIENT_PERMISSIONS (
3046
3095
env, permission::PermissionScope::kFileSystemRead , src.ToStringView ());
3047
3096
3048
- auto src_path = std::filesystem::path (src. ToU8StringView () );
3097
+ auto src_path = BufferValueToPath (src);
3049
3098
3050
3099
BufferValue dest (isolate, args[1 ]);
3051
3100
CHECK_NOT_NULL (*dest);
3052
3101
ToNamespacedPath (env, &dest);
3053
3102
THROW_IF_INSUFFICIENT_PERMISSIONS (
3054
3103
env, permission::PermissionScope::kFileSystemWrite , dest.ToStringView ());
3055
3104
3056
- auto dest_path = std::filesystem::path (dest. ToU8StringView () );
3105
+ auto dest_path = BufferValueToPath (dest);
3057
3106
bool dereference = args[2 ]->IsTrue ();
3058
3107
bool recursive = args[3 ]->IsTrue ();
3059
3108
@@ -3082,47 +3131,41 @@ static void CpSyncCheckPaths(const FunctionCallbackInfo<Value>& args) {
3082
3131
(src_status.type () == std::filesystem::file_type::directory) ||
3083
3132
(dereference && src_status.type () == std::filesystem::file_type::symlink );
3084
3133
3134
+ auto src_path_str = PathToString (src_path);
3135
+ auto dest_path_str = PathToString (dest_path);
3136
+
3085
3137
if (!error_code) {
3086
3138
// Check if src and dest are identical.
3087
3139
if (std::filesystem::equivalent (src_path, dest_path)) {
3088
- std::u8string message =
3089
- u8" src and dest cannot be the same " + dest_path.u8string ();
3090
- return THROW_ERR_FS_CP_EINVAL (
3091
- env, reinterpret_cast <const char *>(message.c_str ()));
3140
+ std::string message = " src and dest cannot be the same %s" ;
3141
+ return THROW_ERR_FS_CP_EINVAL (env, message.c_str (), dest_path_str);
3092
3142
}
3093
3143
3094
3144
const bool dest_is_dir =
3095
3145
dest_status.type () == std::filesystem::file_type::directory;
3096
-
3097
3146
if (src_is_dir && !dest_is_dir) {
3098
- std::u8string message = u8" Cannot overwrite non-directory " +
3099
- src_path.u8string () + u8" with directory " +
3100
- dest_path.u8string ();
3147
+ std::string message =
3148
+ " Cannot overwrite non-directory %s with directory %s" ;
3101
3149
return THROW_ERR_FS_CP_DIR_TO_NON_DIR (
3102
- env, reinterpret_cast < const char *>( message.c_str ()) );
3150
+ env, message.c_str (), src_path_str, dest_path_str );
3103
3151
}
3104
3152
3105
3153
if (!src_is_dir && dest_is_dir) {
3106
- std::u8string message = u8" Cannot overwrite directory " +
3107
- dest_path.u8string () + u8" with non-directory " +
3108
- src_path.u8string ();
3154
+ std::string message =
3155
+ " Cannot overwrite directory %s with non-directory %s" ;
3109
3156
return THROW_ERR_FS_CP_NON_DIR_TO_DIR (
3110
- env, reinterpret_cast < const char *>( message.c_str ()) );
3157
+ env, message.c_str (), dest_path_str, src_path_str );
3111
3158
}
3112
3159
}
3113
3160
3114
- std::u8string dest_path_str = dest_path.u8string ();
3115
- std::u8string src_path_str = src_path.u8string ();
3116
3161
if (!src_path_str.ends_with (std::filesystem::path::preferred_separator)) {
3117
3162
src_path_str += std::filesystem::path::preferred_separator;
3118
3163
}
3119
3164
// Check if dest_path is a subdirectory of src_path.
3120
3165
if (src_is_dir && dest_path_str.starts_with (src_path_str)) {
3121
- std::u8string message = u8" Cannot copy " + src_path.u8string () +
3122
- u8" to a subdirectory of self " +
3123
- dest_path.u8string ();
3166
+ std::string message = " Cannot copy %s to a subdirectory of self %s" ;
3124
3167
return THROW_ERR_FS_CP_EINVAL (
3125
- env, reinterpret_cast < const char *>( message.c_str ()) );
3168
+ env, message.c_str (), src_path_str, dest_path_str );
3126
3169
}
3127
3170
3128
3171
auto dest_parent = dest_path.parent_path ();
@@ -3133,11 +3176,9 @@ static void CpSyncCheckPaths(const FunctionCallbackInfo<Value>& args) {
3133
3176
dest_parent.parent_path () != dest_parent) {
3134
3177
if (std::filesystem::equivalent (
3135
3178
src_path, dest_path.parent_path (), error_code)) {
3136
- std::u8string message = u8" Cannot copy " + src_path.u8string () +
3137
- u8" to a subdirectory of self " +
3138
- dest_path.u8string ();
3179
+ std::string message = " Cannot copy %s to a subdirectory of self %s" ;
3139
3180
return THROW_ERR_FS_CP_EINVAL (
3140
- env, reinterpret_cast < const char *>( message.c_str ()) );
3181
+ env, message.c_str (), src_path_str, dest_path_str );
3141
3182
}
3142
3183
3143
3184
// If equivalent fails, it's highly likely that dest_parent does not exist
@@ -3149,29 +3190,23 @@ static void CpSyncCheckPaths(const FunctionCallbackInfo<Value>& args) {
3149
3190
}
3150
3191
3151
3192
if (src_is_dir && !recursive) {
3152
- std::u8string message =
3153
- u8" Recursive option not enabled, cannot copy a directory: " +
3154
- src_path.u8string ();
3155
- return THROW_ERR_FS_EISDIR (env,
3156
- reinterpret_cast <const char *>(message.c_str ()));
3193
+ std::string message =
3194
+ " Recursive option not enabled, cannot copy a directory: %s" ;
3195
+ return THROW_ERR_FS_EISDIR (env, message.c_str (), src_path_str);
3157
3196
}
3158
3197
3159
3198
switch (src_status.type ()) {
3160
3199
case std::filesystem::file_type::socket: {
3161
- std::u8string message = u8" Cannot copy a socket file: " + dest_path_str;
3162
- return THROW_ERR_FS_CP_SOCKET (
3163
- env, reinterpret_cast <const char *>(message.c_str ()));
3200
+ std::string message = " Cannot copy a socket file: %s" ;
3201
+ return THROW_ERR_FS_CP_SOCKET (env, message.c_str (), dest_path_str);
3164
3202
}
3165
3203
case std::filesystem::file_type::fifo: {
3166
- std::u8string message = u8" Cannot copy a FIFO pipe: " + dest_path_str;
3167
- return THROW_ERR_FS_CP_FIFO_PIPE (
3168
- env, reinterpret_cast <const char *>(message.c_str ()));
3204
+ std::string message = " Cannot copy a FIFO pipe: %s" ;
3205
+ return THROW_ERR_FS_CP_FIFO_PIPE (env, message.c_str (), dest_path_str);
3169
3206
}
3170
3207
case std::filesystem::file_type::unknown: {
3171
- std::u8string message =
3172
- u8" Cannot copy an unknown file type: " + dest_path_str;
3173
- return THROW_ERR_FS_CP_UNKNOWN (
3174
- env, reinterpret_cast <const char *>(message.c_str ()));
3208
+ std::string message = " Cannot copy an unknown file type: %s" ;
3209
+ return THROW_ERR_FS_CP_UNKNOWN (env, message.c_str (), dest_path_str);
3175
3210
}
3176
3211
default :
3177
3212
break ;
0 commit comments