1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
|
// Copyright (c) .NET Foundation and contributors. All rights reserved. Licensed under the Microsoft Reciprocal License. See LICENSE.TXT file in the project root for full license information.
#include "precomp.h"
// Exit macros
#define PathExitOnLastError(x, s, ...) ExitOnLastErrorSource(DUTIL_SOURCE_PATHUTIL, x, s, __VA_ARGS__)
#define PathExitOnLastErrorDebugTrace(x, s, ...) ExitOnLastErrorDebugTraceSource(DUTIL_SOURCE_PATHUTIL, x, s, __VA_ARGS__)
#define PathExitWithLastError(x, s, ...) ExitWithLastErrorSource(DUTIL_SOURCE_PATHUTIL, x, s, __VA_ARGS__)
#define PathExitOnFailure(x, s, ...) ExitOnFailureSource(DUTIL_SOURCE_PATHUTIL, x, s, __VA_ARGS__)
#define PathExitOnRootFailure(x, s, ...) ExitOnRootFailureSource(DUTIL_SOURCE_PATHUTIL, x, s, __VA_ARGS__)
#define PathExitWithRootFailure(x, e, s, ...) ExitWithRootFailureSource(DUTIL_SOURCE_PATHUTIL, x, e, s, __VA_ARGS__)
#define PathExitOnFailureDebugTrace(x, s, ...) ExitOnFailureDebugTraceSource(DUTIL_SOURCE_PATHUTIL, x, s, __VA_ARGS__)
#define PathExitOnNull(p, x, e, s, ...) ExitOnNullSource(DUTIL_SOURCE_PATHUTIL, p, x, e, s, __VA_ARGS__)
#define PathExitOnNullWithLastError(p, x, s, ...) ExitOnNullWithLastErrorSource(DUTIL_SOURCE_PATHUTIL, p, x, s, __VA_ARGS__)
#define PathExitOnNullDebugTrace(p, x, e, s, ...) ExitOnNullDebugTraceSource(DUTIL_SOURCE_PATHUTIL, p, x, e, s, __VA_ARGS__)
#define PathExitOnInvalidHandleWithLastError(p, x, s, ...) ExitOnInvalidHandleWithLastErrorSource(DUTIL_SOURCE_PATHUTIL, p, x, s, __VA_ARGS__)
#define PathExitOnWin32Error(e, x, s, ...) ExitOnWin32ErrorSource(DUTIL_SOURCE_PATHUTIL, e, x, s, __VA_ARGS__)
#define PathExitOnGdipFailure(g, x, s, ...) ExitOnGdipFailureSource(DUTIL_SOURCE_PATHUTIL, g, x, s, __VA_ARGS__)
DAPI_(HRESULT) PathCanonicalizePath(
__in_z LPCWSTR wzPath,
__deref_out_z LPWSTR* psczCanonicalized
)
{
HRESULT hr = S_OK;
int cch = MAX_PATH + 1;
hr = StrAlloc(psczCanonicalized, cch);
PathExitOnFailure(hr, "Failed to allocate string for the canonicalized path.");
if (::PathCanonicalizeW(*psczCanonicalized, wzPath))
{
hr = S_OK;
}
else
{
ExitFunctionWithLastError(hr);
}
LExit:
return hr;
}
DAPI_(HRESULT) PathCanonicalizeForComparison(
__in_z LPCWSTR wzPath,
__in DWORD dwCanonicalizeFlags,
__deref_out_z LPWSTR* psczCanonicalized
)
{
HRESULT hr = S_OK;
LPWSTR sczNormalizedPath = NULL;
LPCWSTR wzNormalizedPath = NULL;
SIZE_T cchUncRootLength = 0;
BOOL fHasPrefix = FALSE;
hr = StrAllocString(&sczNormalizedPath, wzPath, 0);
PathExitOnFailure(hr, "Failed to allocate string for the normalized path.");
PathFixedNormalizeSlashes(sczNormalizedPath);
wzNormalizedPath = sczNormalizedPath;
if (PATH_CANONICALIZE_KEEP_UNC_ROOT & dwCanonicalizeFlags)
{
BOOL fUNC = FALSE;
LPCWSTR wzPastRoot = PathSkipPastRoot(sczNormalizedPath, NULL, NULL, &fUNC);
if (fUNC)
{
wzNormalizedPath = wzPastRoot;
cchUncRootLength = wzPastRoot - sczNormalizedPath;
}
}
if (*wzNormalizedPath)
{
hr = PathCanonicalizePath(wzNormalizedPath, psczCanonicalized);
PathExitOnFailure(hr, "Failed to canonicalize: %ls", wzNormalizedPath);
}
else
{
Assert(cchUncRootLength);
ReleaseStr(*psczCanonicalized);
*psczCanonicalized = sczNormalizedPath;
sczNormalizedPath = NULL;
cchUncRootLength = 0;
}
if (cchUncRootLength)
{
hr = StrAllocPrefix(psczCanonicalized, sczNormalizedPath, cchUncRootLength);
PathExitOnFailure(hr, "Failed to prefix the UNC root to the canonicalized path.");
}
if (PATH_CANONICALIZE_BACKSLASH_TERMINATE & dwCanonicalizeFlags)
{
hr = PathBackslashTerminate(psczCanonicalized);
PathExitOnFailure(hr, "Failed to backslash terminate the canonicalized path.");
}
if (PATH_CANONICALIZE_APPEND_EXTENDED_PATH_PREFIX & dwCanonicalizeFlags)
{
hr = PathPrefix(psczCanonicalized, 0, PATH_PREFIX_SHORT_PATHS);
PathExitOnFailure(hr, "Failed to ensure the extended path prefix on the canonicalized path.");
}
PathSkipPastRoot(*psczCanonicalized, &fHasPrefix, NULL, NULL);
if (fHasPrefix)
{
// Canonicalize prefix into \\?\.
(*psczCanonicalized)[0] = L'\\';
(*psczCanonicalized)[1] = L'\\';
(*psczCanonicalized)[2] = L'?';
(*psczCanonicalized)[3] = L'\\';
}
LExit:
ReleaseStr(sczNormalizedPath);
return hr;
}
DAPI_(HRESULT) PathConcatRelativeToBase(
__in LPCWSTR wzBase,
__in_opt LPCWSTR wzRelative,
__deref_out_z LPWSTR* psczCombined
)
{
HRESULT hr = S_OK;
LPWSTR sczCanonicalizedRelative = NULL;
if (!wzBase || !*wzBase)
{
PathExitWithRootFailure(hr, E_INVALIDARG, "wzBase is required.");
}
if (PathIsRooted(wzRelative))
{
PathExitWithRootFailure(hr, E_INVALIDARG, "wzRelative cannot be rooted.");
}
hr = StrAllocString(psczCombined, wzBase, 0);
PathExitOnFailure(hr, "Failed to copy base to output.");
if (wzRelative && *wzRelative)
{
hr = PathBackslashTerminate(psczCombined);
PathExitOnFailure(hr, "Failed to backslashify.");
hr = PathCanonicalizeForComparison(wzRelative, 0, &sczCanonicalizedRelative);
PathExitOnFailure(hr, "Failed to canonicalize wzRelative.");
hr = StrAllocConcat(psczCombined, sczCanonicalizedRelative, 0);
PathExitOnFailure(hr, "Failed to append relative to output.");
}
LExit:
ReleaseStr(sczCanonicalizedRelative);
return hr;
}
DAPI_(HRESULT) PathConcatRelativeToFullyQualifiedBase(
__in LPCWSTR wzBase,
__in_opt LPCWSTR wzRelative,
__deref_out_z LPWSTR* psczCombined
)
{
HRESULT hr = S_OK;
if (!PathIsFullyQualified(wzBase))
{
PathExitWithRootFailure(hr, E_INVALIDARG, "wzBase must be fully qualified: %ls.", wzBase);
}
hr = PathConcatRelativeToBase(wzBase, wzRelative, psczCombined);
LExit:
return hr;
}
DAPI_(HRESULT) PathCompareCanonicalized(
__in_z LPCWSTR wzPath1,
__in_z LPCWSTR wzPath2,
__out BOOL* pfEqual
)
{
HRESULT hr = S_OK;
LPWSTR sczCanonicalized1 = NULL;
LPWSTR sczCanonicalized2 = NULL;
DWORD dwDefaultFlags = PATH_CANONICALIZE_APPEND_EXTENDED_PATH_PREFIX | PATH_CANONICALIZE_KEEP_UNC_ROOT;
int nResult = 0;
if (!wzPath1 || !wzPath2)
{
PathExitWithRootFailure(hr, E_INVALIDARG, "Both paths are required.");
}
hr = PathCanonicalizeForComparison(wzPath1, dwDefaultFlags, &sczCanonicalized1);
PathExitOnFailure(hr, "Failed to canonicalize wzPath1.");
hr = PathCanonicalizeForComparison(wzPath2, dwDefaultFlags, &sczCanonicalized2);
PathExitOnFailure(hr, "Failed to canonicalize wzPath2.");
nResult = ::CompareStringW(LOCALE_NEUTRAL, NORM_IGNORECASE, sczCanonicalized1, -1, sczCanonicalized2, -1);
PathExitOnNullWithLastError(nResult, hr, "Failed to compare canonicalized paths.");
*pfEqual = CSTR_EQUAL == nResult;
LExit:
ReleaseStr(sczCanonicalized1);
ReleaseStr(sczCanonicalized2);
return hr;
}
DAPI_(HRESULT) PathDirectoryContainsPath(
__in_z LPCWSTR wzDirectory,
__in_z LPCWSTR wzPath
)
{
HRESULT hr = S_OK;
LPWSTR sczCanonicalizedDirectory = NULL;
LPWSTR sczCanonicalizedPath = NULL;
DWORD dwDefaultFlags = PATH_CANONICALIZE_APPEND_EXTENDED_PATH_PREFIX | PATH_CANONICALIZE_KEEP_UNC_ROOT;
size_t cchDirectory = 0;
if (!wzDirectory || !*wzDirectory)
{
PathExitWithRootFailure(hr, E_INVALIDARG, "wzDirectory is required.");
}
if (!wzPath || !*wzPath)
{
PathExitWithRootFailure(hr, E_INVALIDARG, "wzPath is required.");
}
hr = PathCanonicalizeForComparison(wzDirectory, dwDefaultFlags | PATH_CANONICALIZE_BACKSLASH_TERMINATE, &sczCanonicalizedDirectory);
PathExitOnFailure(hr, "Failed to canonicalize the directory.");
hr = PathCanonicalizeForComparison(wzPath, dwDefaultFlags, &sczCanonicalizedPath);
PathExitOnFailure(hr, "Failed to canonicalize the path.");
if (!PathIsFullyQualified(sczCanonicalizedDirectory))
{
PathExitWithRootFailure(hr, E_INVALIDARG, "wzDirectory must be a fully qualified path.");
}
if (!sczCanonicalizedPath || !*sczCanonicalizedPath)
{
ExitFunction1(hr = S_FALSE);
}
hr = ::StringCchLengthW(sczCanonicalizedDirectory, STRSAFE_MAX_CCH, &cchDirectory);
PathExitOnFailure(hr, "Failed to get length of canonicalized directory.");
if (CSTR_EQUAL != ::CompareStringW(LOCALE_NEUTRAL, NORM_IGNORECASE, sczCanonicalizedDirectory, (DWORD)cchDirectory, sczCanonicalizedPath, (DWORD)cchDirectory))
{
ExitFunction1(hr = S_FALSE);
}
hr = sczCanonicalizedPath[cchDirectory] ? S_OK : S_FALSE;
LExit:
ReleaseStr(sczCanonicalizedPath);
ReleaseStr(sczCanonicalizedDirectory);
return hr;
}
DAPI_(HRESULT) PathSystemWindowsSubdirectory(
__in_z_opt LPCWSTR wzSubdirectory,
__out_z LPWSTR* psczFullPath
)
{
HRESULT hr = S_OK;
WCHAR wzTempPath[MAX_PATH + 1] = { };
DWORD cch = 0;
cch = ::GetSystemWindowsDirectoryW(wzTempPath, countof(wzTempPath));
if (!cch)
{
PathExitWithLastError(hr, "Failed to get Windows directory path.");
}
else if (cch >= countof(wzTempPath))
{
PathExitWithRootFailure(hr, E_INSUFFICIENT_BUFFER, "Windows directory path too long.");
}
if (wzSubdirectory)
{
hr = PathConcatRelativeToBase(wzTempPath, wzSubdirectory, psczFullPath);
PathExitOnFailure(hr, "Failed to concat subdirectory on Windows directory path.");
}
else
{
hr = StrAllocString(psczFullPath, wzTempPath, 0);
PathExitOnFailure(hr, "Failed to copy Windows directory path.");
}
hr = PathBackslashTerminate(psczFullPath);
PathExitOnFailure(hr, "Failed to terminate Windows directory path with backslash.");
LExit:
return hr;
}
|