Debian Codegraph @

src/mpt/path/os_path_long.hpp

raw
1 /* SPDX-License-Identifier: BSL-1.0 OR BSD-3-Clause */
2
3 #ifndef MPT_PATH_OS_PATH_LONG_HPP
4 #define MPT_PATH_OS_PATH_LONG_HPP
5
6
7
8 #include "mpt/base/detect.hpp"
9 #include "mpt/base/namespace.hpp"
10 #include "mpt/path/os_path.hpp"
11 #include "mpt/string/types.hpp"
12 #include "mpt/string_transcode/transcode.hpp"
13
14 #if MPT_OS_WINDOWS
15 #include <vector>
16 #endif // MPT_OS_WINDOWS
17
18 #if MPT_OS_WINDOWS
19 #include <windows.h>
20 #endif // MPT_OS_WINDOWS
21
22
23
24 namespace mpt {
25 inline namespace MPT_INLINE_NS {
26
27
28
29 inline mpt::os_path support_long_path(const mpt::os_path & path) {
30 #if MPT_OS_WINDOWS
31 #if !MPT_OS_WINDOWS_WINRT
32 #define MPT_PATH_OS_PATH_USE_WINDOWS_LONG_PATH_PREFIX
33 #else // MPT_OS_WINDOWS_WINRT
34 // For WinRT on Windows 8, there is no official wy to determine an absolute path.
35 #if MPT_WINRT_AT_LEAST(MPT_WIN_10)
36 #define MPT_PATH_OS_PATH_USE_WINDOWS_LONG_PATH_PREFIX
37 #endif // Windows >= 10
38 #endif // !MPT_OS_WINDOWS_WINRT
39 #endif // MPT_OS_WINDOWS
40 #if defined(MPT_PATH_OS_PATH_USE_WINDOWS_LONG_PATH_PREFIX)
41 if (path.length() < MAX_PATH) {
42 // path is short enough
43 return path;
44 }
45 if (path.substr(0, 4) == MPT_OS_PATH_LITERAL("\\\\?\\")) {
46 // path is already in prefixed form
47 return path;
48 }
49 mpt::os_path absolute_path = path;
50 DWORD size = GetFullPathName(path.c_str(), 0, nullptr, nullptr);
51 if (size != 0) {
52 std::vector<TCHAR> fullPathName(size, TEXT('\0'));
53 if (GetFullPathName(path.c_str(), size, fullPathName.data(), nullptr) != 0) {
54 absolute_path = fullPathName.data();
55 }
56 }
57 if (absolute_path.substr(0, 2) == MPT_OS_PATH_LITERAL("\\\\")) {
58 // Path is a network share: \\server\foo.bar -> \\?\UNC\server\foo.bar
59 return MPT_OS_PATH_LITERAL("\\\\?\\UNC") + absolute_path.substr(1);
60 } else {
61 // Regular file: C:\foo.bar -> \\?\C:\foo.bar
62 return MPT_OS_PATH_LITERAL("\\\\?\\") + absolute_path;
63 }
64 #else
65 return path;
66 #endif
67 }
68
69
70
71 } // namespace MPT_INLINE_NS
72 } // namespace mpt
73
74
75
76 #endif // MPT_PATH_OS_PATH_LONG_HPP