| FazBrowse GitHub Viewer | Trending | | Home |
| Tools: [Download Repo ZIP] [Original HTTPS Page] |
1 parent a31ad37 commit 0ebf839
5 files changed
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
@@ -232,10 +232,8 @@ static std::string GetRelativePath(std::string_view path, | |||
| 232 | 232 | // the paths to wide strings before using std::filesystem::path. | |
| 233 | 233 | // On other platforms, std::filesystem::path can handle UTF-8 directly. | |
| 234 | 234 | #ifdef _WIN32 | |
| 235 | - std::filesystem::path module_path( | ||
| 236 | - ConvertToWideString(std::string(path), CP_UTF8)); | ||
| 237 | - std::filesystem::path base_path( | ||
| 238 | - ConvertToWideString(std::string(base), CP_UTF8)); | ||
| 235 | + std::filesystem::path module_path(ConvertUTF8ToWideString(std::string(path))); | ||
| 236 | + std::filesystem::path base_path(ConvertUTF8ToWideString(std::string(base))); | ||
| 239 | 237 | #else | |
| 240 | 238 | std::filesystem::path module_path(path); | |
| 241 | 239 | std::filesystem::path base_path(base); | |
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
@@ -3184,42 +3184,6 @@ static void GetFormatOfExtensionlessFile( | |||
| 3184 | 3184 | return args.GetReturnValue().Set(EXTENSIONLESS_FORMAT_JAVASCRIPT); | |
| 3185 | 3185 | } | |
| 3186 | 3186 | ||
| 3187 | - #ifdef _WIN32 | ||
| 3188 | - #define BufferValueToPath(str) \ | ||
| 3189 | - std::filesystem::path(ConvertToWideString(str.ToString(), CP_UTF8)) | ||
| 3190 | - | ||
| 3191 | - std::string ConvertWideToUTF8(const std::wstring& wstr) { | ||
| 3192 | - if (wstr.empty()) return std::string(); | ||
| 3193 | - | ||
| 3194 | - int size_needed = WideCharToMultiByte(CP_UTF8, | ||
| 3195 | - 0, | ||
| 3196 | - &wstr[0], | ||
| 3197 | - static_cast<int>(wstr.size()), | ||
| 3198 | - nullptr, | ||
| 3199 | - 0, | ||
| 3200 | - nullptr, | ||
| 3201 | - nullptr); | ||
| 3202 | - std::string strTo(size_needed, 0); | ||
| 3203 | - WideCharToMultiByte(CP_UTF8, | ||
| 3204 | - 0, | ||
| 3205 | - &wstr[0], | ||
| 3206 | - static_cast<int>(wstr.size()), | ||
| 3207 | - &strTo[0], | ||
| 3208 | - size_needed, | ||
| 3209 | - nullptr, | ||
| 3210 | - nullptr); | ||
| 3211 | - return strTo; | ||
| 3212 | - } | ||
| 3213 | - | ||
| 3214 | - #define PathToString(path) ConvertWideToUTF8(path.wstring()); | ||
| 3215 | - | ||
| 3216 | - #else // _WIN32 | ||
| 3217 | - | ||
| 3218 | - #define BufferValueToPath(str) std::filesystem::path(str.ToStringView()); | ||
| 3219 | - #define PathToString(path) path.native(); | ||
| 3220 | - | ||
| 3221 | - #endif // _WIN32 | ||
| 3222 | - | ||
| 3223 | 3187 | static void CpSyncCheckPaths(const FunctionCallbackInfo<Value>& args) { | |
| 3224 | 3188 | Environment* env = Environment::GetCurrent(args); | |
| 3225 | 3189 | Isolate* isolate = env->isolate(); | |
@@ -3232,15 +3196,15 @@ static void CpSyncCheckPaths(const FunctionCallbackInfo<Value>& args) { | |||
| 3232 | 3196 | THROW_IF_INSUFFICIENT_PERMISSIONS( | |
| 3233 | 3197 | env, permission::PermissionScope::kFileSystemRead, src.ToStringView()); | |
| 3234 | 3198 | ||
| 3235 | - auto src_path = BufferValueToPath(src); | ||
| 3199 | + auto src_path = src.ToPath(); | ||
| 3236 | 3200 | ||
| 3237 | 3201 | BufferValue dest(isolate, args[1]); | |
| 3238 | 3202 | CHECK_NOT_NULL(*dest); | |
| 3239 | 3203 | ToNamespacedPath(env, &dest); | |
| 3240 | 3204 | THROW_IF_INSUFFICIENT_PERMISSIONS( | |
| 3241 | 3205 | env, permission::PermissionScope::kFileSystemWrite, dest.ToStringView()); | |
| 3242 | 3206 | ||
| 3243 | - auto dest_path = BufferValueToPath(dest); | ||
| 3207 | + auto dest_path = dest.ToPath(); | ||
| 3244 | 3208 | bool dereference = args[2]->IsTrue(); | |
| 3245 | 3209 | bool recursive = args[3]->IsTrue(); | |
| 3246 | 3210 | ||
@@ -3269,8 +3233,8 @@ static void CpSyncCheckPaths(const FunctionCallbackInfo<Value>& args) { | |||
| 3269 | 3233 | (src_status.type() == std::filesystem::file_type::directory) || | |
| 3270 | 3234 | (dereference && src_status.type() == std::filesystem::file_type::symlink); | |
| 3271 | 3235 | ||
| 3272 | - auto src_path_str = PathToString(src_path); | ||
| 3273 | - auto dest_path_str = PathToString(dest_path); | ||
| 3236 | + auto src_path_str = ConvertPathToUTF8(src_path); | ||
| 3237 | + auto dest_path_str = ConvertPathToUTF8(dest_path); | ||
| 3274 | 3238 | ||
| 3275 | 3239 | if (!error_code) { | |
| 3276 | 3240 | // Check if src and dest are identical. | |
@@ -3365,7 +3329,7 @@ static bool CopyUtimes(const std::filesystem::path& src, | |||
| 3365 | 3329 | uv_fs_t req; | |
| 3366 | 3330 | auto cleanup = OnScopeLeave([&req]() { uv_fs_req_cleanup(&req); }); | |
| 3367 | 3331 | ||
| 3368 | - auto src_path_str = PathToString(src); | ||
| 3332 | + auto src_path_str = ConvertPathToUTF8(src); | ||
| 3369 | 3333 | int result = uv_fs_stat(nullptr, &req, src_path_str.c_str(), nullptr); | |
| 3370 | 3334 | if (is_uv_error(result)) { | |
| 3371 | 3335 | env->ThrowUVException(result, "stat", nullptr, src_path_str.c_str()); | |
@@ -3376,7 +3340,7 @@ static bool CopyUtimes(const std::filesystem::path& src, | |||
| 3376 | 3340 | const double source_atime = s->st_atim.tv_sec + s->st_atim.tv_nsec / 1e9; | |
| 3377 | 3341 | const double source_mtime = s->st_mtim.tv_sec + s->st_mtim.tv_nsec / 1e9; | |
| 3378 | 3342 | ||
| 3379 | - auto dest_file_path_str = PathToString(dest); | ||
| 3343 | + auto dest_file_path_str = ConvertPathToUTF8(dest); | ||
| 3380 | 3344 | int utime_result = uv_fs_utime(nullptr, | |
| 3381 | 3345 | &req, | |
| 3382 | 3346 | dest_file_path_str.c_str(), | |
@@ -3511,7 +3475,7 @@ static void CpSyncCopyDir(const FunctionCallbackInfo<Value>& args) { | |||
| 3511 | 3475 | std::error_code error; | |
| 3512 | 3476 | for (auto dir_entry : std::filesystem::directory_iterator(src)) { | |
| 3513 | 3477 | auto dest_file_path = dest / dir_entry.path().filename(); | |
| 3514 | - auto dest_str = PathToString(dest); | ||
| 3478 | + auto dest_str = ConvertPathToUTF8(dest); | ||
| 3515 | 3479 | ||
| 3516 | 3480 | if (dir_entry.is_symlink()) { | |
| 3517 | 3481 | if (verbatim_symlinks) { | |
@@ -3570,7 +3534,7 @@ static void CpSyncCopyDir(const FunctionCallbackInfo<Value>& args) { | |||
| 3570 | 3534 | } | |
| 3571 | 3535 | } else if (std::filesystem::is_regular_file(dest_file_path)) { | |
| 3572 | 3536 | if (!dereference || (!force && error_on_exist)) { | |
| 3573 | - auto dest_file_path_str = PathToString(dest_file_path); | ||
| 3537 | + auto dest_file_path_str = ConvertPathToUTF8(dest_file_path); | ||
| 3574 | 3538 | env->ThrowStdErrException( | |
| 3575 | 3539 | std::make_error_code(std::errc::file_exists), | |
| 3576 | 3540 | "cp", | |
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
@@ -296,22 +296,24 @@ const BindingData::PackageConfig* BindingData::TraverseParent( | |||
| 296 | 296 | ||
| 297 | 297 | // Stop the search when the process doesn't have permissions | |
| 298 | 298 | // to walk upwards | |
| 299 | - if (is_permissions_enabled && | ||
| 300 | - !env->permission()->is_granted( | ||
| 301 | - env, | ||
| 302 | - permission::PermissionScope::kFileSystemRead, | ||
| 303 | - current_path.generic_string())) [[unlikely]] { | ||
| 304 | - return nullptr; | ||
| 299 | + if (is_permissions_enabled) { | ||
| 300 | + if (!env->permission()->is_granted( | ||
| 301 | + env, | ||
| 302 | + permission::PermissionScope::kFileSystemRead, | ||
| 303 | + ConvertGenericPathToUTF8(current_path))) [[unlikely]] { | ||
| 304 | + return nullptr; | ||
| 305 | + } | ||
| 305 | 306 | } | |
| 306 | 307 | ||
| 307 | 308 | // Check if the path ends with `/node_modules` | |
| 308 | - if (current_path.generic_string().ends_with("/node_modules")) { | ||
| 309 | + if (current_path.filename() == "node_modules") { | ||
| 309 | 310 | return nullptr; | |
| 310 | 311 | } | |
| 311 | 312 | ||
| 312 | 313 | auto package_json_path = current_path / "package.json"; | |
| 314 | + | ||
| 313 | 315 | auto package_json = | |
| 314 | - GetPackageJSON(realm, package_json_path.string(), nullptr); | ||
| 316 | + GetPackageJSON(realm, ConvertPathToUTF8(package_json_path), nullptr); | ||
| 315 | 317 | if (package_json != nullptr) { | |
| 316 | 318 | return package_json; | |
| 317 | 319 | } | |
@@ -333,20 +335,12 @@ void BindingData::GetNearestParentPackageJSONType( | |||
| 333 | 335 | ||
| 334 | 336 | ToNamespacedPath(realm->env(), &path_value); | |
| 335 | 337 | ||
| 336 | - std::string path_value_str = path_value.ToString(); | ||
| 338 | + auto path = path_value.ToPath(); | ||
| 339 | + | ||
| 337 | 340 | if (slashCheck) { | |
| 338 | - path_value_str.push_back(kPathSeparator); | ||
| 341 | + path /= ""; | ||
| 339 | 342 | } | |
| 340 | 343 | ||
| 341 | - std::filesystem::path path; | ||
| 342 | - | ||
| 343 | - #ifdef _WIN32 | ||
| 344 | - std::wstring wide_path = ConvertToWideString(path_value_str, GetACP()); | ||
| 345 | - path = std::filesystem::path(wide_path); | ||
| 346 | - #else | ||
| 347 | - path = std::filesystem::path(path_value_str); | ||
| 348 | - #endif | ||
| 349 | - | ||
| 350 | 344 | auto package_json = TraverseParent(realm, path); | |
| 351 | 345 | ||
| 352 | 346 | if (package_json == nullptr) { | |
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
@@ -718,19 +718,71 @@ inline bool IsWindowsBatchFile(const char* filename) { | |||
| 718 | 718 | return !extension.empty() && (extension == "cmd" || extension == "bat"); | |
| 719 | 719 | } | |
| 720 | 720 | ||
| 721 | - inline std::wstring ConvertToWideString(const std::string& str, | ||
| 722 | - UINT code_page) { | ||
| 721 | + inline std::wstring ConvertUTF8ToWideString(const std::string& str) { | ||
| 723 | 722 | int size_needed = MultiByteToWideChar( | |
| 724 | - code_page, 0, &str[0], static_cast<int>(str.size()), nullptr, 0); | ||
| 723 | + CP_UTF8, 0, &str[0], static_cast<int>(str.size()), nullptr, 0); | ||
| 725 | 724 | std::wstring wstrTo(size_needed, 0); | |
| 726 | - MultiByteToWideChar(code_page, | ||
| 725 | + MultiByteToWideChar(CP_UTF8, | ||
| 727 | 726 | 0, | |
| 728 | 727 | &str[0], | |
| 729 | 728 | static_cast<int>(str.size()), | |
| 730 | 729 | &wstrTo[0], | |
| 731 | 730 | size_needed); | |
| 732 | 731 | return wstrTo; | |
| 733 | 732 | } | |
| 733 | + | ||
| 734 | + std::string ConvertWideStringToUTF8(const std::wstring& wstr) { | ||
| 735 | + if (wstr.empty()) return std::string(); | ||
| 736 | + | ||
| 737 | + int size_needed = WideCharToMultiByte(CP_UTF8, | ||
| 738 | + 0, | ||
| 739 | + &wstr[0], | ||
| 740 | + static_cast<int>(wstr.size()), | ||
| 741 | + nullptr, | ||
| 742 | + 0, | ||
| 743 | + nullptr, | ||
| 744 | + nullptr); | ||
| 745 | + std::string strTo(size_needed, 0); | ||
| 746 | + WideCharToMultiByte(CP_UTF8, | ||
| 747 | + 0, | ||
| 748 | + &wstr[0], | ||
| 749 | + static_cast<int>(wstr.size()), | ||
| 750 | + &strTo[0], | ||
| 751 | + size_needed, | ||
| 752 | + nullptr, | ||
| 753 | + nullptr); | ||
| 754 | + return strTo; | ||
| 755 | + } | ||
| 756 | + | ||
| 757 | + template <typename T, size_t kStackStorageSize> | ||
| 758 | + std::filesystem::path MaybeStackBuffer<T, kStackStorageSize>::ToPath() const { | ||
| 759 | + std::wstring wide_path = ConvertUTF8ToWideString(ToString()); | ||
| 760 | + return std::filesystem::path(wide_path); | ||
| 761 | + } | ||
| 762 | + | ||
| 763 | + std::string ConvertPathToUTF8(const std::filesystem::path& path) { | ||
| 764 | + return ConvertWideStringToUTF8(path.wstring()); | ||
| 765 | + } | ||
| 766 | + | ||
| 767 | + std::string ConvertGenericPathToUTF8(const std::filesystem::path& path) { | ||
| 768 | + return ConvertWideStringToUTF8(path.generic_wstring()); | ||
| 769 | + } | ||
| 770 | + | ||
| 771 | + #else // _WIN32 | ||
| 772 | + | ||
| 773 | + template <typename T, size_t kStackStorageSize> | ||
| 774 | + std::filesystem::path MaybeStackBuffer<T, kStackStorageSize>::ToPath() const { | ||
| 775 | + return std::filesystem::path(ToStringView()); | ||
| 776 | + } | ||
| 777 | + | ||
| 778 | + std::string ConvertPathToUTF8(const std::filesystem::path& path) { | ||
| 779 | + return path.native(); | ||
| 780 | + } | ||
| 781 | + | ||
| 782 | + std::string ConvertGenericPathToUTF8(const std::filesystem::path& path) { | ||
| 783 | + return path.generic_string(); | ||
| 784 | + } | ||
| 785 | + | ||
| 734 | 786 | #endif // _WIN32 | |
| 735 | 787 | ||
| 736 | 788 | inline v8::MaybeLocal<v8::Object> NewDictionaryInstance( | |
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
@@ -507,6 +507,8 @@ class MaybeStackBuffer { | |||
| 507 | 507 | inline std::basic_string_view<T> ToStringView() const { | |
| 508 | 508 | return {out(), length()}; | |
| 509 | 509 | } | |
| 510 | + // This can only be used if the buffer contains path data in UTF8 | ||
| 511 | + inline std::filesystem::path ToPath() const; | ||
| 510 | 512 | ||
| 511 | 513 | private: | |
| 512 | 514 | size_t length_; | |
@@ -1038,9 +1040,15 @@ class JSONOutputStream final : public v8::OutputStream { | |||
| 1038 | 1040 | // Returns true if OS==Windows and filename ends in .bat or .cmd, | |
| 1039 | 1041 | // case insensitive. | |
| 1040 | 1042 | inline bool IsWindowsBatchFile(const char* filename); | |
| 1041 | - inline std::wstring ConvertToWideString(const std::string& str, UINT code_page); | ||
| 1043 | + inline std::wstring ConvertUTF8ToWideString(const std::string& str); | ||
| 1044 | + inline std::string ConvertWideStringToUTF8(const std::wstring& wstr); | ||
| 1045 | + | ||
| 1042 | 1046 | #endif // _WIN32 | |
| 1043 | 1047 | ||
| 1048 | + inline std::filesystem::path ConvertUTF8ToPath(const std::string& str); | ||
| 1049 | + inline std::string ConvertPathToUTF8(const std::filesystem::path& path); | ||
| 1050 | + inline std::string ConvertGenericPathToUTF8(const std::filesystem::path& path); | ||
| 1051 | + | ||
| 1044 | 1052 | // A helper to create a new instance of the dictionary template. | |
| 1045 | 1053 | // Unlike v8::DictionaryTemplate::NewInstance, this method will | |
| 1046 | 1054 | // check that all properties have been set (are not empty MaybeLocals) | |
| Back | FazBrowse Home | New Git URL |
0 commit comments