29a70f7260468bd5075fbd2d0376ee94163ccafb
[platform/upstream/coreclr.git] / src / inc / clr / fs / path.h
1 //
2 // Copyright (c) Microsoft. All rights reserved.
3 // Licensed under the MIT license. See LICENSE file in the project root for full license information.
4 //
5 //
6
7 //
8 // This header provides general path-related file system services.
9
10 #ifndef _clr_fs_Path_h_
11 #define _clr_fs_Path_h_
12
13 #include "clrtypes.h"
14 #include "cor.h" // SELECTANY
15
16 #include "strsafe.h"
17
18 #include "clr/str.h"
19
20 #ifndef LONG_FORMAT_PATH_PREFIX
21     #define LONG_FORMAT_PATH_PREFIX W("\\\\?\\")
22 #endif
23
24 namespace clr
25 {
26     namespace fs
27     {
28         // This list taken from ndp/clr/src/bcl/system/io/path.cs
29         SELECTANY WCHAR const g_rgInvalidPathChars[] =
30             { W('"'), W('<'), W('>'), W('|'), W('\0'), (WCHAR)1, (WCHAR)2, (WCHAR)3, (WCHAR)4, (WCHAR)5, (WCHAR)6,
31               (WCHAR)7, (WCHAR)8, (WCHAR)9, (WCHAR)10, (WCHAR)11, (WCHAR)12, (WCHAR)13, (WCHAR)14,
32               (WCHAR)15, (WCHAR)16, (WCHAR)17, (WCHAR)18, (WCHAR)19, (WCHAR)20, (WCHAR)21, (WCHAR)22,
33               (WCHAR)23, (WCHAR)24, (WCHAR)25, (WCHAR)26, (WCHAR)27, (WCHAR)28, (WCHAR)29, (WCHAR)30,
34               (WCHAR)31 };
35
36         class Path
37         {
38         public:
39             //-----------------------------------------------------------------------------------------
40             static inline bool
41             Exists(
42                 LPCWSTR wzPath)
43             {
44                 DWORD attrs = WszGetFileAttributes(wzPath);
45                 return (attrs != INVALID_FILE_ATTRIBUTES);
46             }
47
48             //-----------------------------------------------------------------------------------------
49             // Returns true if wzPath represents a long format path (i.e., prefixed with '\\?\').
50             static inline bool
51             HasLongFormatPrefix(LPCWSTR wzPath)
52             {
53                 _ASSERTE(!clr::str::IsNullOrEmpty(wzPath)); // Must check this first.
54                 return wcscmp(wzPath, LONG_FORMAT_PATH_PREFIX) == 0;
55             }
56
57             //-----------------------------------------------------------------------------------------
58             static inline bool
59             HasUncPrefix(LPCWSTR wzPath)
60             {
61                 _ASSERTE(!clr::str::IsNullOrEmpty(wzPath)); // Must check this first.
62                 return wzPath[0] != W('\0') && wzPath[0] == W('\\')
63                     && wzPath[1] != W('\0') && wzPath[1] == W('\\')
64                     && wzPath[2] != W('\0') && wzPath[2] != W('?');
65             }
66
67             //-----------------------------------------------------------------------------------------
68             static inline bool
69             HasDrivePrefix(LPCWSTR wzPath)
70             {
71                 _ASSERTE(!clr::str::IsNullOrEmpty(wzPath)); // Must check this first.
72                 return wzPath[0] != W('\0')
73                     && wzPath[1] != W('\0') && wzPath[1] == W(':')
74                     && ((wzPath[0] >= W('a') && wzPath[0] <= W('z')) ||
75                         (wzPath[0] >= W('A') && wzPath[0] <= W('Z')));
76             }
77
78             //-----------------------------------------------------------------------------------------
79             // Returns true if wzPath represents a relative path.
80             static inline bool
81             IsRelative(LPCWSTR wzPath)
82             {
83                 _ASSERTE(!clr::str::IsNullOrEmpty(wzPath)); // Must check this first.
84                 return !HasLongFormatPrefix(wzPath)
85                     && !HasUncPrefix(wzPath)
86                     && (!HasDrivePrefix(wzPath) || wzPath[2] != W('\\'));
87             }
88
89             //-----------------------------------------------------------------------------------------
90             // Combines two path parts. wzPathLeft must be a directory path and may be either absolute
91             // or relative. wzPathRight may be a directory or file path and must be relative. The
92             // result is placed in wzBuffer and the number of chars written is placed in pcchBuffer on
93             // success; otherwise an error HRESULT is returned.
94             static HRESULT
95             Combine(LPCWSTR wzPathLeft, LPCWSTR wzPathRight, __out DWORD *pcchBuffer, __out_ecount(*pcchBuffer) LPWSTR wzBuffer)
96             {
97                 STATIC_CONTRACT_NOTHROW;
98
99                 HRESULT hr = S_OK;
100
101                 if (clr::str::IsNullOrEmpty(wzPathLeft) || clr::str::IsNullOrEmpty(wzPathRight) || pcchBuffer == nullptr)
102                     return E_INVALIDARG;
103
104                 LPWSTR wzBuf = wzBuffer;
105                 size_t cchBuf = *pcchBuffer;
106
107                 IfFailRet(StringCchCopyExW(wzBuf, cchBuf, wzPathLeft, &wzBuf, &cchBuf, STRSAFE_NULL_ON_FAILURE));
108                 IfFailRet(StringCchCatExW(wzBuf, cchBuf, wzBuf[-1] == W('\\') ? W("") : W("\\"), &wzBuf, &cchBuf, STRSAFE_NULL_ON_FAILURE));
109                 IfFailRet(StringCchCatExW(wzBuf, cchBuf, wzPathRight, &wzBuf, &cchBuf, STRSAFE_NULL_ON_FAILURE));
110
111                 return S_OK;
112             }
113
114             //-----------------------------------------------------------------------------------------
115             // Checks if the path provided is valid within the specified constraints.
116             // ***NOTE: does not yet check for invalid path characters.
117             static bool
118             IsValid(LPCWSTR wzPath, DWORD cchPath, bool fAllowLongFormat)
119             {
120                 if (clr::str::IsNullOrEmpty(wzPath))
121                     return false;
122
123                 bool fIsLongFormat = HasLongFormatPrefix(wzPath);
124
125                 if (fIsLongFormat && !fAllowLongFormat)
126                     return false;
127
128                 if (!fIsLongFormat && cchPath > _MAX_PATH)
129                     return false;
130
131                 return true;
132             }
133         };
134     }
135 }
136
137 #endif // _clr_fs_Path_h_