From: <sv...@op...> - 2024-11-29 13:49:24
|
Author: sagamusix Date: Fri Nov 29 14:49:16 2024 New Revision: 22344 URL: https://source.openmpt.org/browse/openmpt/?op=revision&rev=22344 Log: [Imp] Add long file path support to TempFileGuard and various mpt::native_fs functions. Modified: trunk/OpenMPT/common/mptFileTemporary.cpp trunk/OpenMPT/src/mpt/fs/fs.hpp Modified: trunk/OpenMPT/common/mptFileTemporary.cpp ============================================================================== --- trunk/OpenMPT/common/mptFileTemporary.cpp Fri Nov 29 12:00:17 2024 (r22343) +++ trunk/OpenMPT/common/mptFileTemporary.cpp Fri Nov 29 14:49:16 2024 (r22344) @@ -68,7 +68,7 @@ { if(!filename.empty()) { - DeleteFile(filename.AsNative().c_str()); + DeleteFile(mpt::support_long_path(filename.AsNative()).c_str()); } } @@ -80,7 +80,7 @@ { return; } - if(::CreateDirectory(dirname.AsNative().c_str(), NULL) == 0) + if(::CreateDirectory(mpt::support_long_path(dirname.AsNative()).c_str(), NULL) == 0) { // fail dirname = mpt::PathString(); } Modified: trunk/OpenMPT/src/mpt/fs/fs.hpp ============================================================================== --- trunk/OpenMPT/src/mpt/fs/fs.hpp Fri Nov 29 12:00:17 2024 (r22343) +++ trunk/OpenMPT/src/mpt/fs/fs.hpp Fri Nov 29 14:49:16 2024 (r22344) @@ -8,6 +8,7 @@ #include "mpt/base/detect.hpp" #include "mpt/base/namespace.hpp" #include "mpt/path/native_path.hpp" +#include "mpt/path/os_path_long.hpp" #if MPT_OS_WINDOWS #include <vector> @@ -42,37 +43,38 @@ // Verify if this path represents a valid directory on the file system. bool is_directory(const mpt::native_path & path) { - // Using PathIsDirectoryW here instead would increase libopenmpt dependencies by shlwapi.dll. + // Using PathIsDirectoryW here instead would increase libopenmpt dependencies by shlwapi.dll (and it would only work with paths up to MAX_PATH). // GetFileAttributesW also does the job just fine. -#if MPT_OS_WINDOWS_WINRT - WIN32_FILE_ATTRIBUTE_DATA data = {}; - if (::GetFileAttributesExW(path.AsNative().c_str(), GetFileExInfoStandard, &data) == 0) { - return false; - } - DWORD dwAttrib = data.dwFileAttributes; -#else // !MPT_OS_WINDOWS_WINRT - DWORD dwAttrib = ::GetFileAttributes(path.AsNative().c_str()); -#endif // MPT_OS_WINDOWS_WINRT + DWORD dwAttrib = get_file_attributes(path); return ((dwAttrib != INVALID_FILE_ATTRIBUTES) && (dwAttrib & FILE_ATTRIBUTE_DIRECTORY)); } // Verify if this path exists and is a file on the file system. bool is_file(const mpt::native_path & path) { + DWORD dwAttrib = get_file_attributes(path); + return ((dwAttrib != INVALID_FILE_ATTRIBUTES) && !(dwAttrib & FILE_ATTRIBUTE_DIRECTORY)); + } + + // Verify that a path exists (no matter what type) + bool exists(const mpt::native_path & path) { + DWORD dwAttrib = get_file_attributes(path); + return (dwAttrib != INVALID_FILE_ATTRIBUTES); + } + + + +private: + + DWORD get_file_attributes(const mpt::native_path & path) { #if MPT_OS_WINDOWS_WINRT WIN32_FILE_ATTRIBUTE_DATA data = {}; if (::GetFileAttributesExW(path.AsNative().c_str(), GetFileExInfoStandard, &data) == 0) { - return false; + return INVALID_FILE_ATTRIBUTES; } - DWORD dwAttrib = data.dwFileAttributes; + return data.dwFileAttributes; #else // !MPT_OS_WINDOWS_WINRT - DWORD dwAttrib = ::GetFileAttributes(path.AsNative().c_str()); + return ::GetFileAttributes(path.AsNative().c_str()); #endif // MPT_OS_WINDOWS_WINRT - return ((dwAttrib != INVALID_FILE_ATTRIBUTES) && !(dwAttrib & FILE_ATTRIBUTE_DIRECTORY)); - } - - // Verify that a path exists (no matter what type) - bool exists(const mpt::native_path & path) { - return ::PathFileExists(path.AsNative().c_str()) != FALSE; } @@ -81,12 +83,13 @@ #if !(MPT_WINRT_BEFORE(MPT_WIN_10)) mpt::native_path absolute(const mpt::native_path & path) { - DWORD size = ::GetFullPathName(path.AsNative().c_str(), 0, nullptr, nullptr); + const auto long_path = mpt::support_long_path(path.AsNative()); + DWORD size = ::GetFullPathName(long_path.c_str(), 0, nullptr, nullptr); if (size == 0) { return path; } std::vector<TCHAR> fullPathName(size, TEXT('\0')); - if (::GetFullPathName(path.AsNative().c_str(), size, fullPathName.data(), nullptr) == 0) { + if (::GetFullPathName(long_path.c_str(), size, fullPathName.data(), nullptr) == 0) { return path; } return mpt::native_path::FromNative(fullPathName.data()); @@ -116,7 +119,7 @@ path = path.WithTrailingSlash(); HANDLE hFind = NULL; WIN32_FIND_DATA wfd = {}; - hFind = ::FindFirstFile((path + MPT_NATIVE_PATH("*.*")).AsNative().c_str(), &wfd); + hFind = ::FindFirstFile(mpt::support_long_path((path + MPT_NATIVE_PATH("*.*")).AsNative()).c_str(), &wfd); if (hFind != NULL && hFind != INVALID_HANDLE_VALUE) { do { mpt::native_path filename = mpt::native_path::FromNative(wfd.cFileName); |