@@ -3127,6 +3127,55 @@ static void GetFormatOfExtensionlessFile(
3127
3127
return args.GetReturnValue ().Set (EXTENSIONLESS_FORMAT_JAVASCRIPT);
3128
3128
}
3129
3129
3130
+ #ifdef _WIN32
3131
+ std::wstring ConvertToWideString (const std::string& str) {
3132
+ int size_needed = MultiByteToWideChar (
3133
+ CP_UTF8, 0 , &str[0 ], static_cast <int >(str.size ()), nullptr , 0 );
3134
+ std::wstring wstrTo (size_needed, 0 );
3135
+ MultiByteToWideChar (CP_UTF8,
3136
+ 0 ,
3137
+ &str[0 ],
3138
+ static_cast <int >(str.size ()),
3139
+ &wstrTo[0 ],
3140
+ size_needed);
3141
+ return wstrTo;
3142
+ }
3143
+
3144
+ #define BufferValueToPath (str ) \
3145
+ std::filesystem::path (ConvertToWideString(str.ToString()))
3146
+
3147
+ std::string ConvertWideToUTF8(const std::wstring& wstr) {
3148
+ if (wstr.empty ()) return std::string ();
3149
+
3150
+ int size_needed = WideCharToMultiByte (CP_UTF8,
3151
+ 0 ,
3152
+ &wstr[0 ],
3153
+ static_cast <int >(wstr.size ()),
3154
+ nullptr ,
3155
+ 0 ,
3156
+ nullptr ,
3157
+ nullptr );
3158
+ std::string strTo (size_needed, 0 );
3159
+ WideCharToMultiByte (CP_UTF8,
3160
+ 0 ,
3161
+ &wstr[0 ],
3162
+ static_cast <int >(wstr.size ()),
3163
+ &strTo[0 ],
3164
+ size_needed,
3165
+ nullptr ,
3166
+ nullptr );
3167
+ return strTo;
3168
+ }
3169
+
3170
+ #define PathToString (path ) ConvertWideToUTF8(path.wstring());
3171
+
3172
+ #else // _WIN32
3173
+
3174
+ #define BufferValueToPath (str ) std::filesystem::path(str.ToStringView());
3175
+ #define PathToString (path ) path.native();
3176
+
3177
+ #endif // _WIN32
3178
+
3130
3179
static void CpSyncCheckPaths (const FunctionCallbackInfo<Value>& args) {
3131
3180
Environment* env = Environment::GetCurrent (args);
3132
3181
Isolate* isolate = env->isolate ();
@@ -3139,15 +3188,15 @@ static void CpSyncCheckPaths(const FunctionCallbackInfo<Value>& args) {
3139
3188
THROW_IF_INSUFFICIENT_PERMISSIONS (
3140
3189
env, permission::PermissionScope::kFileSystemRead , src.ToStringView ());
3141
3190
3142
- auto src_path = std::filesystem::path (src. ToU8StringView () );
3191
+ auto src_path = BufferValueToPath (src);
3143
3192
3144
3193
BufferValue dest (isolate, args[1 ]);
3145
3194
CHECK_NOT_NULL (*dest);
3146
3195
ToNamespacedPath (env, &dest);
3147
3196
THROW_IF_INSUFFICIENT_PERMISSIONS (
3148
3197
env, permission::PermissionScope::kFileSystemWrite , dest.ToStringView ());
3149
3198
3150
- auto dest_path = std::filesystem::path (dest. ToU8StringView () );
3199
+ auto dest_path = BufferValueToPath (dest);
3151
3200
bool dereference = args[2 ]->IsTrue ();
3152
3201
bool recursive = args[3 ]->IsTrue ();
3153
3202
@@ -3176,47 +3225,41 @@ static void CpSyncCheckPaths(const FunctionCallbackInfo<Value>& args) {
3176
3225
(src_status.type () == std::filesystem::file_type::directory) ||
3177
3226
(dereference && src_status.type () == std::filesystem::file_type::symlink );
3178
3227
3228
+ auto src_path_str = PathToString (src_path);
3229
+ auto dest_path_str = PathToString (dest_path);
3230
+
3179
3231
if (!error_code) {
3180
3232
// Check if src and dest are identical.
3181
3233
if (std::filesystem::equivalent (src_path, dest_path)) {
3182
- std::u8string message =
3183
- u8" src and dest cannot be the same " + dest_path.u8string ();
3184
- return THROW_ERR_FS_CP_EINVAL (
3185
- env, reinterpret_cast <const char *>(message.c_str ()));
3234
+ std::string message = " src and dest cannot be the same %s" ;
3235
+ return THROW_ERR_FS_CP_EINVAL (env, message.c_str (), dest_path_str);
3186
3236
}
3187
3237
3188
3238
const bool dest_is_dir =
3189
3239
dest_status.type () == std::filesystem::file_type::directory;
3190
-
3191
3240
if (src_is_dir && !dest_is_dir) {
3192
- std::u8string message = u8" Cannot overwrite non-directory " +
3193
- src_path.u8string () + u8" with directory " +
3194
- dest_path.u8string ();
3241
+ std::string message =
3242
+ " Cannot overwrite non-directory %s with directory %s" ;
3195
3243
return THROW_ERR_FS_CP_DIR_TO_NON_DIR (
3196
- env, reinterpret_cast < const char *>( message.c_str ()) );
3244
+ env, message.c_str (), src_path_str, dest_path_str );
3197
3245
}
3198
3246
3199
3247
if (!src_is_dir && dest_is_dir) {
3200
- std::u8string message = u8" Cannot overwrite directory " +
3201
- dest_path.u8string () + u8" with non-directory " +
3202
- src_path.u8string ();
3248
+ std::string message =
3249
+ " Cannot overwrite directory %s with non-directory %s" ;
3203
3250
return THROW_ERR_FS_CP_NON_DIR_TO_DIR (
3204
- env, reinterpret_cast < const char *>( message.c_str ()) );
3251
+ env, message.c_str (), dest_path_str, src_path_str );
3205
3252
}
3206
3253
}
3207
3254
3208
- std::u8string dest_path_str = dest_path.u8string ();
3209
- std::u8string src_path_str = src_path.u8string ();
3210
3255
if (!src_path_str.ends_with (std::filesystem::path::preferred_separator)) {
3211
3256
src_path_str += std::filesystem::path::preferred_separator;
3212
3257
}
3213
3258
// Check if dest_path is a subdirectory of src_path.
3214
3259
if (src_is_dir && dest_path_str.starts_with (src_path_str)) {
3215
- std::u8string message = u8" Cannot copy " + src_path.u8string () +
3216
- u8" to a subdirectory of self " +
3217
- dest_path.u8string ();
3260
+ std::string message = " Cannot copy %s to a subdirectory of self %s" ;
3218
3261
return THROW_ERR_FS_CP_EINVAL (
3219
- env, reinterpret_cast < const char *>( message.c_str ()) );
3262
+ env, message.c_str (), src_path_str, dest_path_str );
3220
3263
}
3221
3264
3222
3265
auto dest_parent = dest_path.parent_path ();
@@ -3227,11 +3270,9 @@ static void CpSyncCheckPaths(const FunctionCallbackInfo<Value>& args) {
3227
3270
dest_parent.parent_path () != dest_parent) {
3228
3271
if (std::filesystem::equivalent (
3229
3272
src_path, dest_path.parent_path (), error_code)) {
3230
- std::u8string message = u8" Cannot copy " + src_path.u8string () +
3231
- u8" to a subdirectory of self " +
3232
- dest_path.u8string ();
3273
+ std::string message = " Cannot copy %s to a subdirectory of self %s" ;
3233
3274
return THROW_ERR_FS_CP_EINVAL (
3234
- env, reinterpret_cast < const char *>( message.c_str ()) );
3275
+ env, message.c_str (), src_path_str, dest_path_str );
3235
3276
}
3236
3277
3237
3278
// If equivalent fails, it's highly likely that dest_parent does not exist
@@ -3243,29 +3284,23 @@ static void CpSyncCheckPaths(const FunctionCallbackInfo<Value>& args) {
3243
3284
}
3244
3285
3245
3286
if (src_is_dir && !recursive) {
3246
- std::u8string message =
3247
- u8" Recursive option not enabled, cannot copy a directory: " +
3248
- src_path.u8string ();
3249
- return THROW_ERR_FS_EISDIR (env,
3250
- reinterpret_cast <const char *>(message.c_str ()));
3287
+ std::string message =
3288
+ " Recursive option not enabled, cannot copy a directory: %s" ;
3289
+ return THROW_ERR_FS_EISDIR (env, message.c_str (), src_path_str);
3251
3290
}
3252
3291
3253
3292
switch (src_status.type ()) {
3254
3293
case std::filesystem::file_type::socket: {
3255
- std::u8string message = u8" Cannot copy a socket file: " + dest_path_str;
3256
- return THROW_ERR_FS_CP_SOCKET (
3257
- env, reinterpret_cast <const char *>(message.c_str ()));
3294
+ std::string message = " Cannot copy a socket file: %s" ;
3295
+ return THROW_ERR_FS_CP_SOCKET (env, message.c_str (), dest_path_str);
3258
3296
}
3259
3297
case std::filesystem::file_type::fifo: {
3260
- std::u8string message = u8" Cannot copy a FIFO pipe: " + dest_path_str;
3261
- return THROW_ERR_FS_CP_FIFO_PIPE (
3262
- env, reinterpret_cast <const char *>(message.c_str ()));
3298
+ std::string message = " Cannot copy a FIFO pipe: %s" ;
3299
+ return THROW_ERR_FS_CP_FIFO_PIPE (env, message.c_str (), dest_path_str);
3263
3300
}
3264
3301
case std::filesystem::file_type::unknown: {
3265
- std::u8string message =
3266
- u8" Cannot copy an unknown file type: " + dest_path_str;
3267
- return THROW_ERR_FS_CP_UNKNOWN (
3268
- env, reinterpret_cast <const char *>(message.c_str ()));
3302
+ std::string message = " Cannot copy an unknown file type: %s" ;
3303
+ return THROW_ERR_FS_CP_UNKNOWN (env, message.c_str (), dest_path_str);
3269
3304
}
3270
3305
default :
3271
3306
break ;
0 commit comments