From 3b21d328653f7a472b0f77ef1cc0c85766566b96 Mon Sep 17 00:00:00 2001 From: Silent Date: Sun, 6 Oct 2019 21:37:51 +0200 Subject: [PATCH] Remove MAX_PATH limit from: - GetTempFilenameForAtomicWrite - SetUserDirectory --- Source/Core/Common/FileUtil.cpp | 16 ++++++------ Source/Core/Common/FileUtil.h | 2 +- Source/Core/UICommon/UICommon.cpp | 41 ++++++++++++++++++++----------- 3 files changed, 36 insertions(+), 23 deletions(-) diff --git a/Source/Core/Common/FileUtil.cpp b/Source/Core/Common/FileUtil.cpp index 75118bffac..814d891c22 100644 --- a/Source/Core/Common/FileUtil.cpp +++ b/Source/Core/Common/FileUtil.cpp @@ -639,19 +639,21 @@ std::string CreateTempDir() #endif } -std::string GetTempFilenameForAtomicWrite(const std::string& path) +std::string GetTempFilenameForAtomicWrite(std::string path) { - std::string abs = path; #ifdef _WIN32 - TCHAR absbuf[MAX_PATH]; - if (_tfullpath(absbuf, UTF8ToTStr(path).c_str(), MAX_PATH) != nullptr) - abs = TStrToUTF8(absbuf); + std::unique_ptr absbuf{ + _tfullpath(nullptr, UTF8ToTStr(path).c_str(), 0), std::free}; + if (absbuf != nullptr) + { + path = TStrToUTF8(absbuf.get()); + } #else char absbuf[PATH_MAX]; if (realpath(path.c_str(), absbuf) != nullptr) - abs = absbuf; + path = absbuf; #endif - return abs + ".xxx"; + return std::move(path) + ".xxx"; } #if defined(__APPLE__) diff --git a/Source/Core/Common/FileUtil.h b/Source/Core/Common/FileUtil.h index 57e645c285..ff574dee5a 100644 --- a/Source/Core/Common/FileUtil.h +++ b/Source/Core/Common/FileUtil.h @@ -172,7 +172,7 @@ bool SetCurrentDir(const std::string& directory); std::string CreateTempDir(); // Get a filename that can hopefully be atomically renamed to the given path. -std::string GetTempFilenameForAtomicWrite(const std::string& path); +std::string GetTempFilenameForAtomicWrite(std::string path); // Gets a set user directory path // Don't call prior to setting the base user directory diff --git a/Source/Core/UICommon/UICommon.cpp b/Source/Core/UICommon/UICommon.cpp index 935d07b043..878181bd9d 100644 --- a/Source/Core/UICommon/UICommon.cpp +++ b/Source/Core/UICommon/UICommon.cpp @@ -211,46 +211,57 @@ void SetUserDirectory(const std::string& custom_path) // -> Use GetExeDirectory()\User // Check our registry keys + // TODO: Maybe use WIL when it's available? HKEY hkey; DWORD local = 0; - TCHAR configPath[MAX_PATH] = {0}; + std::unique_ptr configPath; if (RegOpenKeyEx(HKEY_CURRENT_USER, TEXT("Software\\Dolphin Emulator"), 0, KEY_QUERY_VALUE, &hkey) == ERROR_SUCCESS) { DWORD size = 4; if (RegQueryValueEx(hkey, TEXT("LocalUserConfig"), nullptr, nullptr, reinterpret_cast(&local), &size) != ERROR_SUCCESS) + { local = 0; + } + + size = 0; + RegQueryValueEx(hkey, TEXT("UserConfigPath"), nullptr, nullptr, nullptr, &size); + configPath = std::make_unique(size / sizeof(TCHAR)); + if (RegQueryValueEx(hkey, TEXT("UserConfigPath"), nullptr, nullptr, + reinterpret_cast(configPath.get()), &size) != ERROR_SUCCESS) + { + configPath.reset(); + } - size = MAX_PATH; - if (RegQueryValueEx(hkey, TEXT("UserConfigPath"), nullptr, nullptr, (LPBYTE)configPath, - &size) != ERROR_SUCCESS) - configPath[0] = 0; RegCloseKey(hkey); } - local = local || File::Exists(File::GetExeDirectory() + DIR_SEP "portable.txt"); + local = local != 0 || File::Exists(File::GetExeDirectory() + DIR_SEP "portable.txt"); - // Get Program Files path in case we need it. - TCHAR my_documents[MAX_PATH]; - bool my_documents_found = SUCCEEDED( - SHGetFolderPath(nullptr, CSIDL_MYDOCUMENTS, nullptr, SHGFP_TYPE_CURRENT, my_documents)); + // Get Documents path in case we need it. + // TODO: Maybe use WIL when it's available? + PWSTR my_documents = nullptr; + bool my_documents_found = + SUCCEEDED(SHGetKnownFolderPath(FOLDERID_Documents, KF_FLAG_DEFAULT, nullptr, &my_documents)); if (local) // Case 1-2 user_path = File::GetExeDirectory() + DIR_SEP USERDATA_DIR DIR_SEP; - else if (configPath[0]) // Case 3 - user_path = TStrToUTF8(configPath); + else if (configPath) // Case 3 + user_path = TStrToUTF8(configPath.get()); else if (my_documents_found) // Case 4 user_path = TStrToUTF8(my_documents) + DIR_SEP "Dolphin Emulator" DIR_SEP; else // Case 5 user_path = File::GetExeDirectory() + DIR_SEP USERDATA_DIR DIR_SEP; + CoTaskMemFree(my_documents); + // Prettify the path: it will be displayed in some places, we don't want a mix // of \ and /. - user_path = ReplaceAll(user_path, "\\", DIR_SEP); + user_path = ReplaceAll(std::move(user_path), "\\", DIR_SEP); // Make sure it ends in DIR_SEP. - if (*user_path.rbegin() != DIR_SEP_CHR) + if (user_path.back() != DIR_SEP_CHR) user_path += DIR_SEP; #else @@ -325,7 +336,7 @@ void SetUserDirectory(const std::string& custom_path) #endif } #endif - File::SetUserPath(D_USER_IDX, user_path); + File::SetUserPath(D_USER_IDX, std::move(user_path)); } void SaveWiimoteSources()