Skip to content

Commit 17d844c

Browse files
committed
Merged revision(s) 22344 from trunk/OpenMPT:
[Imp] Add long file path support to TempFileGuard and various mpt::native_fs functions. ........ git-svn-id: https://source.openmpt.org/svn/openmpt/branches/OpenMPT-1.31@22374 56274372-70c3-4bfc-bfc3-4c3a0b034d27
1 parent d6be97c commit 17d844c

File tree

2 files changed

+27
-24
lines changed

2 files changed

+27
-24
lines changed

common/mptFileTemporary.cpp

+2-2
Original file line numberDiff line numberDiff line change
@@ -68,7 +68,7 @@ TempFileGuard::~TempFileGuard()
6868
{
6969
if(!filename.empty())
7070
{
71-
DeleteFile(filename.AsNative().c_str());
71+
DeleteFile(mpt::support_long_path(filename.AsNative()).c_str());
7272
}
7373
}
7474

@@ -80,7 +80,7 @@ TempDirGuard::TempDirGuard(const mpt::TemporaryPathname &pathname)
8080
{
8181
return;
8282
}
83-
if(::CreateDirectory(dirname.AsNative().c_str(), NULL) == 0)
83+
if(::CreateDirectory(mpt::support_long_path(dirname.AsNative()).c_str(), NULL) == 0)
8484
{ // fail
8585
dirname = mpt::PathString();
8686
}

src/mpt/fs/fs.hpp

+25-22
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@
88
#include "mpt/base/detect.hpp"
99
#include "mpt/base/namespace.hpp"
1010
#include "mpt/path/native_path.hpp"
11+
#include "mpt/path/os_path_long.hpp"
1112

1213
#if MPT_OS_WINDOWS
1314
#include <vector>
@@ -42,37 +43,38 @@ class fs<mpt::native_path> {
4243

4344
// Verify if this path represents a valid directory on the file system.
4445
bool is_directory(const mpt::native_path & path) {
45-
// Using PathIsDirectoryW here instead would increase libopenmpt dependencies by shlwapi.dll.
46+
// Using PathIsDirectoryW here instead would increase libopenmpt dependencies by shlwapi.dll (and it would only work with paths up to MAX_PATH).
4647
// GetFileAttributesW also does the job just fine.
47-
#if MPT_OS_WINDOWS_WINRT
48-
WIN32_FILE_ATTRIBUTE_DATA data = {};
49-
if (::GetFileAttributesExW(path.AsNative().c_str(), GetFileExInfoStandard, &data) == 0) {
50-
return false;
51-
}
52-
DWORD dwAttrib = data.dwFileAttributes;
53-
#else // !MPT_OS_WINDOWS_WINRT
54-
DWORD dwAttrib = ::GetFileAttributes(path.AsNative().c_str());
55-
#endif // MPT_OS_WINDOWS_WINRT
48+
DWORD dwAttrib = get_file_attributes(path);
5649
return ((dwAttrib != INVALID_FILE_ATTRIBUTES) && (dwAttrib & FILE_ATTRIBUTE_DIRECTORY));
5750
}
5851

5952
// Verify if this path exists and is a file on the file system.
6053
bool is_file(const mpt::native_path & path) {
54+
DWORD dwAttrib = get_file_attributes(path);
55+
return ((dwAttrib != INVALID_FILE_ATTRIBUTES) && !(dwAttrib & FILE_ATTRIBUTE_DIRECTORY));
56+
}
57+
58+
// Verify that a path exists (no matter what type)
59+
bool exists(const mpt::native_path & path) {
60+
DWORD dwAttrib = get_file_attributes(path);
61+
return (dwAttrib != INVALID_FILE_ATTRIBUTES);
62+
}
63+
64+
65+
66+
private:
67+
68+
DWORD get_file_attributes(const mpt::native_path & path) {
6169
#if MPT_OS_WINDOWS_WINRT
6270
WIN32_FILE_ATTRIBUTE_DATA data = {};
6371
if (::GetFileAttributesExW(path.AsNative().c_str(), GetFileExInfoStandard, &data) == 0) {
64-
return false;
72+
return INVALID_FILE_ATTRIBUTES;
6573
}
66-
DWORD dwAttrib = data.dwFileAttributes;
74+
return data.dwFileAttributes;
6775
#else // !MPT_OS_WINDOWS_WINRT
68-
DWORD dwAttrib = ::GetFileAttributes(path.AsNative().c_str());
76+
return ::GetFileAttributes(path.AsNative().c_str());
6977
#endif // MPT_OS_WINDOWS_WINRT
70-
return ((dwAttrib != INVALID_FILE_ATTRIBUTES) && !(dwAttrib & FILE_ATTRIBUTE_DIRECTORY));
71-
}
72-
73-
// Verify that a path exists (no matter what type)
74-
bool exists(const mpt::native_path & path) {
75-
return ::PathFileExists(path.AsNative().c_str()) != FALSE;
7678
}
7779

7880

@@ -81,12 +83,13 @@ class fs<mpt::native_path> {
8183

8284
#if !(MPT_WINRT_BEFORE(MPT_WIN_10))
8385
mpt::native_path absolute(const mpt::native_path & path) {
84-
DWORD size = ::GetFullPathName(path.AsNative().c_str(), 0, nullptr, nullptr);
86+
const auto long_path = mpt::support_long_path(path.AsNative());
87+
DWORD size = ::GetFullPathName(long_path.c_str(), 0, nullptr, nullptr);
8588
if (size == 0) {
8689
return path;
8790
}
8891
std::vector<TCHAR> fullPathName(size, TEXT('\0'));
89-
if (::GetFullPathName(path.AsNative().c_str(), size, fullPathName.data(), nullptr) == 0) {
92+
if (::GetFullPathName(long_path.c_str(), size, fullPathName.data(), nullptr) == 0) {
9093
return path;
9194
}
9295
return mpt::native_path::FromNative(fullPathName.data());
@@ -116,7 +119,7 @@ class fs<mpt::native_path> {
116119
path = path.WithTrailingSlash();
117120
HANDLE hFind = NULL;
118121
WIN32_FIND_DATA wfd = {};
119-
hFind = ::FindFirstFile((path + MPT_NATIVE_PATH("*.*")).AsNative().c_str(), &wfd);
122+
hFind = ::FindFirstFile(mpt::support_long_path((path + MPT_NATIVE_PATH("*.*")).AsNative()).c_str(), &wfd);
120123
if (hFind != NULL && hFind != INVALID_HANDLE_VALUE) {
121124
do {
122125
mpt::native_path filename = mpt::native_path::FromNative(wfd.cFileName);

0 commit comments

Comments
 (0)