aboutsummaryrefslogtreecommitdiff
path: root/src/dutil/pathutil.cpp
diff options
context:
space:
mode:
authorSean Hall <r.sean.hall@gmail.com>2021-03-02 14:19:14 -0600
committerSean Hall <r.sean.hall@gmail.com>2021-03-02 15:40:02 -0600
commit10ebf674da5df9224e4eddd3545518434c5b455b (patch)
treeea1f4063edd46e9942eab94dd7adb2f75c6c589e /src/dutil/pathutil.cpp
parent3bbf1347b900ec115a12faf8f46965c9b7649696 (diff)
downloadwix-10ebf674da5df9224e4eddd3545518434c5b455b.tar.gz
wix-10ebf674da5df9224e4eddd3545518434c5b455b.tar.bz2
wix-10ebf674da5df9224e4eddd3545518434c5b455b.zip
Update rest of dutil to use their own source with the Exit* macros.
Fix some CA warnings.
Diffstat (limited to 'src/dutil/pathutil.cpp')
-rw-r--r--src/dutil/pathutil.cpp175
1 files changed, 95 insertions, 80 deletions
diff --git a/src/dutil/pathutil.cpp b/src/dutil/pathutil.cpp
index d8894756..ec338f71 100644
--- a/src/dutil/pathutil.cpp
+++ b/src/dutil/pathutil.cpp
@@ -2,11 +2,26 @@
2 2
3#include "precomp.h" 3#include "precomp.h"
4 4
5
6// Exit macros
7#define PathExitOnLastError(x, s, ...) ExitOnLastErrorSource(DUTIL_SOURCE_PATHUTIL, x, s, __VA_ARGS__)
8#define PathExitOnLastErrorDebugTrace(x, s, ...) ExitOnLastErrorDebugTraceSource(DUTIL_SOURCE_PATHUTIL, x, s, __VA_ARGS__)
9#define PathExitWithLastError(x, s, ...) ExitWithLastErrorSource(DUTIL_SOURCE_PATHUTIL, x, s, __VA_ARGS__)
10#define PathExitOnFailure(x, s, ...) ExitOnFailureSource(DUTIL_SOURCE_PATHUTIL, x, s, __VA_ARGS__)
11#define PathExitOnRootFailure(x, s, ...) ExitOnRootFailureSource(DUTIL_SOURCE_PATHUTIL, x, s, __VA_ARGS__)
12#define PathExitOnFailureDebugTrace(x, s, ...) ExitOnFailureDebugTraceSource(DUTIL_SOURCE_PATHUTIL, x, s, __VA_ARGS__)
13#define PathExitOnNull(p, x, e, s, ...) ExitOnNullSource(DUTIL_SOURCE_PATHUTIL, p, x, e, s, __VA_ARGS__)
14#define PathExitOnNullWithLastError(p, x, s, ...) ExitOnNullWithLastErrorSource(DUTIL_SOURCE_PATHUTIL, p, x, s, __VA_ARGS__)
15#define PathExitOnNullDebugTrace(p, x, e, s, ...) ExitOnNullDebugTraceSource(DUTIL_SOURCE_PATHUTIL, p, x, e, s, __VA_ARGS__)
16#define PathExitOnInvalidHandleWithLastError(p, x, s, ...) ExitOnInvalidHandleWithLastErrorSource(DUTIL_SOURCE_PATHUTIL, p, x, s, __VA_ARGS__)
17#define PathExitOnWin32Error(e, x, s, ...) ExitOnWin32ErrorSource(DUTIL_SOURCE_PATHUTIL, e, x, s, __VA_ARGS__)
18#define PathExitOnGdipFailure(g, x, s, ...) ExitOnGdipFailureSource(DUTIL_SOURCE_PATHUTIL, g, x, s, __VA_ARGS__)
19
5#define PATH_GOOD_ENOUGH 64 20#define PATH_GOOD_ENOUGH 64
6 21
7 22
8DAPI_(HRESULT) PathCommandLineAppend( 23DAPI_(HRESULT) PathCommandLineAppend(
9 __deref_out_z LPWSTR* psczCommandLine, 24 __deref_inout_z LPWSTR* psczCommandLine,
10 __in_z LPCWSTR wzArgument 25 __in_z LPCWSTR wzArgument
11 ) 26 )
12{ 27{
@@ -41,7 +56,7 @@ DAPI_(HRESULT) PathCommandLineAppend(
41 if (fRequiresQuoting) 56 if (fRequiresQuoting)
42 { 57 {
43 hr = StrAlloc(&sczQuotedArg, dwMaxEscapedSize + 3); // plus three for the start and end quote plus null terminator. 58 hr = StrAlloc(&sczQuotedArg, dwMaxEscapedSize + 3); // plus three for the start and end quote plus null terminator.
44 ExitOnFailure(hr, "Failed to allocate argument to be quoted."); 59 PathExitOnFailure(hr, "Failed to allocate argument to be quoted.");
45 60
46 LPCWSTR pwz = wzArgument; 61 LPCWSTR pwz = wzArgument;
47 LPWSTR pwzQuoted = sczQuotedArg; 62 LPWSTR pwzQuoted = sczQuotedArg;
@@ -94,11 +109,11 @@ DAPI_(HRESULT) PathCommandLineAppend(
94 if (*psczCommandLine && **psczCommandLine) 109 if (*psczCommandLine && **psczCommandLine)
95 { 110 {
96 hr = StrAllocConcat(psczCommandLine, L" ", 0); 111 hr = StrAllocConcat(psczCommandLine, L" ", 0);
97 ExitOnFailure(hr, "Failed to append space to command line with existing data."); 112 PathExitOnFailure(hr, "Failed to append space to command line with existing data.");
98 } 113 }
99 114
100 hr = StrAllocConcat(psczCommandLine, sczQuotedArg ? sczQuotedArg : wzArgument, 0); 115 hr = StrAllocConcat(psczCommandLine, sczQuotedArg ? sczQuotedArg : wzArgument, 0);
101 ExitOnFailure(hr, "Failed to copy command line argument."); 116 PathExitOnFailure(hr, "Failed to copy command line argument.");
102 117
103LExit: 118LExit:
104 ReleaseStr(sczQuotedArg); 119 ReleaseStr(sczQuotedArg);
@@ -162,7 +177,7 @@ DAPI_(LPCWSTR) PathExtension(
162 177
163DAPI_(HRESULT) PathGetDirectory( 178DAPI_(HRESULT) PathGetDirectory(
164 __in_z LPCWSTR wzPath, 179 __in_z LPCWSTR wzPath,
165 __out LPWSTR *psczDirectory 180 __out_z LPWSTR *psczDirectory
166 ) 181 )
167{ 182{
168 HRESULT hr = S_OK; 183 HRESULT hr = S_OK;
@@ -193,7 +208,7 @@ DAPI_(HRESULT) PathGetDirectory(
193 } 208 }
194 209
195 hr = StrAllocString(psczDirectory, wzPath, cchDirectory); 210 hr = StrAllocString(psczDirectory, wzPath, cchDirectory);
196 ExitOnFailure(hr, "Failed to copy directory."); 211 PathExitOnFailure(hr, "Failed to copy directory.");
197 212
198LExit: 213LExit:
199 return hr; 214 return hr;
@@ -223,28 +238,28 @@ DAPI_(HRESULT) PathExpand(
223 cchExpandedPath = PATH_GOOD_ENOUGH; 238 cchExpandedPath = PATH_GOOD_ENOUGH;
224 239
225 hr = StrAlloc(&sczExpandedPath, cchExpandedPath); 240 hr = StrAlloc(&sczExpandedPath, cchExpandedPath);
226 ExitOnFailure(hr, "Failed to allocate space for expanded path."); 241 PathExitOnFailure(hr, "Failed to allocate space for expanded path.");
227 242
228 cch = ::ExpandEnvironmentStringsW(wzRelativePath, sczExpandedPath, cchExpandedPath); 243 cch = ::ExpandEnvironmentStringsW(wzRelativePath, sczExpandedPath, cchExpandedPath);
229 if (0 == cch) 244 if (0 == cch)
230 { 245 {
231 ExitWithLastError(hr, "Failed to expand environment variables in string: %ls", wzRelativePath); 246 PathExitWithLastError(hr, "Failed to expand environment variables in string: %ls", wzRelativePath);
232 } 247 }
233 else if (cchExpandedPath < cch) 248 else if (cchExpandedPath < cch)
234 { 249 {
235 cchExpandedPath = cch; 250 cchExpandedPath = cch;
236 hr = StrAlloc(&sczExpandedPath, cchExpandedPath); 251 hr = StrAlloc(&sczExpandedPath, cchExpandedPath);
237 ExitOnFailure(hr, "Failed to re-allocate more space for expanded path."); 252 PathExitOnFailure(hr, "Failed to re-allocate more space for expanded path.");
238 253
239 cch = ::ExpandEnvironmentStringsW(wzRelativePath, sczExpandedPath, cchExpandedPath); 254 cch = ::ExpandEnvironmentStringsW(wzRelativePath, sczExpandedPath, cchExpandedPath);
240 if (0 == cch) 255 if (0 == cch)
241 { 256 {
242 ExitWithLastError(hr, "Failed to expand environment variables in string: %ls", wzRelativePath); 257 PathExitWithLastError(hr, "Failed to expand environment variables in string: %ls", wzRelativePath);
243 } 258 }
244 else if (cchExpandedPath < cch) 259 else if (cchExpandedPath < cch)
245 { 260 {
246 hr = HRESULT_FROM_WIN32(ERROR_INSUFFICIENT_BUFFER); 261 hr = HRESULT_FROM_WIN32(ERROR_INSUFFICIENT_BUFFER);
247 ExitOnRootFailure(hr, "Failed to allocate buffer for expanded path."); 262 PathExitOnRootFailure(hr, "Failed to allocate buffer for expanded path.");
248 } 263 }
249 } 264 }
250 265
@@ -255,10 +270,10 @@ DAPI_(HRESULT) PathExpand(
255 { 270 {
256 hr = S_OK; 271 hr = S_OK;
257 } 272 }
258 ExitOnFailure(hr, "Failed to prefix long path after expanding environment variables."); 273 PathExitOnFailure(hr, "Failed to prefix long path after expanding environment variables.");
259 274
260 hr = StrMaxLength(sczExpandedPath, reinterpret_cast<DWORD_PTR *>(&cchExpandedPath)); 275 hr = StrMaxLength(sczExpandedPath, reinterpret_cast<DWORD_PTR *>(&cchExpandedPath));
261 ExitOnFailure(hr, "Failed to get max length of expanded path."); 276 PathExitOnFailure(hr, "Failed to get max length of expanded path.");
262 } 277 }
263 } 278 }
264 279
@@ -272,35 +287,35 @@ DAPI_(HRESULT) PathExpand(
272 DWORD cchFullPath = PATH_GOOD_ENOUGH < cchExpandedPath ? cchExpandedPath : PATH_GOOD_ENOUGH; 287 DWORD cchFullPath = PATH_GOOD_ENOUGH < cchExpandedPath ? cchExpandedPath : PATH_GOOD_ENOUGH;
273 288
274 hr = StrAlloc(&sczFullPath, cchFullPath); 289 hr = StrAlloc(&sczFullPath, cchFullPath);
275 ExitOnFailure(hr, "Failed to allocate space for full path."); 290 PathExitOnFailure(hr, "Failed to allocate space for full path.");
276 291
277 cch = ::GetFullPathNameW(wzPath, cchFullPath, sczFullPath, &wzFileName); 292 cch = ::GetFullPathNameW(wzPath, cchFullPath, sczFullPath, &wzFileName);
278 if (0 == cch) 293 if (0 == cch)
279 { 294 {
280 ExitWithLastError(hr, "Failed to get full path for string: %ls", wzPath); 295 PathExitWithLastError(hr, "Failed to get full path for string: %ls", wzPath);
281 } 296 }
282 else if (cchFullPath < cch) 297 else if (cchFullPath < cch)
283 { 298 {
284 cchFullPath = cch < MAX_PATH ? cch : cch + 7; // ensure space for "\\?\UNC" prefix if needed 299 cchFullPath = cch < MAX_PATH ? cch : cch + 7; // ensure space for "\\?\UNC" prefix if needed
285 hr = StrAlloc(&sczFullPath, cchFullPath); 300 hr = StrAlloc(&sczFullPath, cchFullPath);
286 ExitOnFailure(hr, "Failed to re-allocate more space for full path."); 301 PathExitOnFailure(hr, "Failed to re-allocate more space for full path.");
287 302
288 cch = ::GetFullPathNameW(wzPath, cchFullPath, sczFullPath, &wzFileName); 303 cch = ::GetFullPathNameW(wzPath, cchFullPath, sczFullPath, &wzFileName);
289 if (0 == cch) 304 if (0 == cch)
290 { 305 {
291 ExitWithLastError(hr, "Failed to get full path for string: %ls", wzPath); 306 PathExitWithLastError(hr, "Failed to get full path for string: %ls", wzPath);
292 } 307 }
293 else if (cchFullPath < cch) 308 else if (cchFullPath < cch)
294 { 309 {
295 hr = HRESULT_FROM_WIN32(ERROR_INSUFFICIENT_BUFFER); 310 hr = HRESULT_FROM_WIN32(ERROR_INSUFFICIENT_BUFFER);
296 ExitOnRootFailure(hr, "Failed to allocate buffer for full path."); 311 PathExitOnRootFailure(hr, "Failed to allocate buffer for full path.");
297 } 312 }
298 } 313 }
299 314
300 if (MAX_PATH < cch) 315 if (MAX_PATH < cch)
301 { 316 {
302 hr = PathPrefix(&sczFullPath); 317 hr = PathPrefix(&sczFullPath);
303 ExitOnFailure(hr, "Failed to prefix long path after expanding."); 318 PathExitOnFailure(hr, "Failed to prefix long path after expanding.");
304 } 319 }
305 } 320 }
306 else 321 else
@@ -310,7 +325,7 @@ DAPI_(HRESULT) PathExpand(
310 } 325 }
311 326
312 hr = StrAllocString(psczFullPath, sczFullPath? sczFullPath : wzRelativePath, 0); 327 hr = StrAllocString(psczFullPath, sczFullPath? sczFullPath : wzRelativePath, 0);
313 ExitOnFailure(hr, "Failed to copy relative path into full path."); 328 PathExitOnFailure(hr, "Failed to copy relative path into full path.");
314 329
315LExit: 330LExit:
316 ReleaseStr(sczFullPath); 331 ReleaseStr(sczFullPath);
@@ -336,7 +351,7 @@ DAPI_(HRESULT) PathPrefix(
336 L'\\' == wzFullPath[2]) // normal path 351 L'\\' == wzFullPath[2]) // normal path
337 { 352 {
338 hr = StrAllocPrefix(psczFullPath, L"\\\\?\\", 4); 353 hr = StrAllocPrefix(psczFullPath, L"\\\\?\\", 4);
339 ExitOnFailure(hr, "Failed to add prefix to file path."); 354 PathExitOnFailure(hr, "Failed to add prefix to file path.");
340 } 355 }
341 else if (L'\\' == wzFullPath[0] && L'\\' == wzFullPath[1]) // UNC 356 else if (L'\\' == wzFullPath[0] && L'\\' == wzFullPath[1]) // UNC
342 { 357 {
@@ -344,18 +359,18 @@ DAPI_(HRESULT) PathPrefix(
344 if (!(L'?' == wzFullPath[2] && L'\\' == wzFullPath[3])) 359 if (!(L'?' == wzFullPath[2] && L'\\' == wzFullPath[3]))
345 { 360 {
346 hr = StrSize(*psczFullPath, &cbFullPath); 361 hr = StrSize(*psczFullPath, &cbFullPath);
347 ExitOnFailure(hr, "Failed to get size of full path."); 362 PathExitOnFailure(hr, "Failed to get size of full path.");
348 363
349 memmove_s(wzFullPath, cbFullPath, wzFullPath + 1, cbFullPath - sizeof(WCHAR)); 364 memmove_s(wzFullPath, cbFullPath, wzFullPath + 1, cbFullPath - sizeof(WCHAR));
350 365
351 hr = StrAllocPrefix(psczFullPath, L"\\\\?\\UNC", 7); 366 hr = StrAllocPrefix(psczFullPath, L"\\\\?\\UNC", 7);
352 ExitOnFailure(hr, "Failed to add prefix to UNC path."); 367 PathExitOnFailure(hr, "Failed to add prefix to UNC path.");
353 } 368 }
354 } 369 }
355 else 370 else
356 { 371 {
357 hr = E_INVALIDARG; 372 hr = E_INVALIDARG;
358 ExitOnFailure(hr, "Invalid path provided to prefix: %ls.", wzFullPath); 373 PathExitOnFailure(hr, "Invalid path provided to prefix: %ls.", wzFullPath);
359 } 374 }
360 375
361LExit: 376LExit:
@@ -372,7 +387,7 @@ DAPI_(HRESULT) PathFixedBackslashTerminate(
372 size_t cchLength = 0; 387 size_t cchLength = 0;
373 388
374 hr = ::StringCchLengthW(wzPath, cchPath, &cchLength); 389 hr = ::StringCchLengthW(wzPath, cchPath, &cchLength);
375 ExitOnFailure(hr, "Failed to get length of path."); 390 PathExitOnFailure(hr, "Failed to get length of path.");
376 391
377 if (cchLength >= cchPath) 392 if (cchLength >= cchPath)
378 { 393 {
@@ -400,15 +415,15 @@ DAPI_(HRESULT) PathBackslashTerminate(
400 size_t cchLength = 0; 415 size_t cchLength = 0;
401 416
402 hr = StrMaxLength(*psczPath, &cchPath); 417 hr = StrMaxLength(*psczPath, &cchPath);
403 ExitOnFailure(hr, "Failed to get size of path string."); 418 PathExitOnFailure(hr, "Failed to get size of path string.");
404 419
405 hr = ::StringCchLengthW(*psczPath, cchPath, &cchLength); 420 hr = ::StringCchLengthW(*psczPath, cchPath, &cchLength);
406 ExitOnFailure(hr, "Failed to get length of path."); 421 PathExitOnFailure(hr, "Failed to get length of path.");
407 422
408 if (L'\\' != (*psczPath)[cchLength - 1]) 423 if (L'\\' != (*psczPath)[cchLength - 1])
409 { 424 {
410 hr = StrAllocConcat(psczPath, L"\\", 1); 425 hr = StrAllocConcat(psczPath, L"\\", 1);
411 ExitOnFailure(hr, "Failed to concat backslash onto string."); 426 PathExitOnFailure(hr, "Failed to concat backslash onto string.");
412 } 427 }
413 428
414LExit: 429LExit:
@@ -427,12 +442,12 @@ DAPI_(HRESULT) PathForCurrentProcess(
427 do 442 do
428 { 443 {
429 hr = StrAlloc(psczFullPath, cch); 444 hr = StrAlloc(psczFullPath, cch);
430 ExitOnFailure(hr, "Failed to allocate string for module path."); 445 PathExitOnFailure(hr, "Failed to allocate string for module path.");
431 446
432 DWORD cchRequired = ::GetModuleFileNameW(hModule, *psczFullPath, cch); 447 DWORD cchRequired = ::GetModuleFileNameW(hModule, *psczFullPath, cch);
433 if (0 == cchRequired) 448 if (0 == cchRequired)
434 { 449 {
435 ExitWithLastError(hr, "Failed to get path for executing process."); 450 PathExitWithLastError(hr, "Failed to get path for executing process.");
436 } 451 }
437 else if (cchRequired == cch) 452 else if (cchRequired == cch)
438 { 453 {
@@ -457,15 +472,15 @@ DAPI_(HRESULT) PathRelativeToModule(
457 ) 472 )
458{ 473{
459 HRESULT hr = PathForCurrentProcess(psczFullPath, hModule); 474 HRESULT hr = PathForCurrentProcess(psczFullPath, hModule);
460 ExitOnFailure(hr, "Failed to get current module path."); 475 PathExitOnFailure(hr, "Failed to get current module path.");
461 476
462 hr = PathGetDirectory(*psczFullPath, psczFullPath); 477 hr = PathGetDirectory(*psczFullPath, psczFullPath);
463 ExitOnFailure(hr, "Failed to get current module directory."); 478 PathExitOnFailure(hr, "Failed to get current module directory.");
464 479
465 if (wzFileName) 480 if (wzFileName)
466 { 481 {
467 hr = PathConcat(*psczFullPath, wzFileName, psczFullPath); 482 hr = PathConcat(*psczFullPath, wzFileName, psczFullPath);
468 ExitOnFailure(hr, "Failed to append filename."); 483 PathExitOnFailure(hr, "Failed to append filename.");
469 } 484 }
470 485
471LExit: 486LExit:
@@ -496,16 +511,16 @@ DAPI_(HRESULT) PathCreateTempFile(
496 if (wzDirectory && *wzDirectory) 511 if (wzDirectory && *wzDirectory)
497 { 512 {
498 hr = StrAllocString(&sczTempPath, wzDirectory, 0); 513 hr = StrAllocString(&sczTempPath, wzDirectory, 0);
499 ExitOnFailure(hr, "Failed to copy temp path."); 514 PathExitOnFailure(hr, "Failed to copy temp path.");
500 } 515 }
501 else 516 else
502 { 517 {
503 hr = StrAlloc(&sczTempPath, cchTempPath); 518 hr = StrAlloc(&sczTempPath, cchTempPath);
504 ExitOnFailure(hr, "Failed to allocate memory for the temp path."); 519 PathExitOnFailure(hr, "Failed to allocate memory for the temp path.");
505 520
506 if (!::GetTempPathW(cchTempPath, sczTempPath)) 521 if (!::GetTempPathW(cchTempPath, sczTempPath))
507 { 522 {
508 ExitWithLastError(hr, "Failed to get temp path."); 523 PathExitWithLastError(hr, "Failed to get temp path.");
509 } 524 }
510 } 525 }
511 526
@@ -514,10 +529,10 @@ DAPI_(HRESULT) PathCreateTempFile(
514 for (DWORD i = 1; i <= dwUniqueCount && INVALID_HANDLE_VALUE == hTempFile; ++i) 529 for (DWORD i = 1; i <= dwUniqueCount && INVALID_HANDLE_VALUE == hTempFile; ++i)
515 { 530 {
516 hr = StrAllocFormatted(&scz, wzFileNameTemplate, i); 531 hr = StrAllocFormatted(&scz, wzFileNameTemplate, i);
517 ExitOnFailure(hr, "Failed to allocate memory for file template."); 532 PathExitOnFailure(hr, "Failed to allocate memory for file template.");
518 533
519 hr = StrAllocFormatted(&sczTempFile, L"%s%s", sczTempPath, scz); 534 hr = StrAllocFormatted(&sczTempFile, L"%s%s", sczTempPath, scz);
520 ExitOnFailure(hr, "Failed to allocate temp file name."); 535 PathExitOnFailure(hr, "Failed to allocate temp file name.");
521 536
522 hTempFile = ::CreateFileW(sczTempFile, GENERIC_WRITE, FILE_SHARE_READ | FILE_SHARE_DELETE, NULL, CREATE_NEW, dwFileAttributes, NULL); 537 hTempFile = ::CreateFileW(sczTempFile, GENERIC_WRITE, FILE_SHARE_READ | FILE_SHARE_DELETE, NULL, CREATE_NEW, dwFileAttributes, NULL);
523 if (INVALID_HANDLE_VALUE == hTempFile) 538 if (INVALID_HANDLE_VALUE == hTempFile)
@@ -528,7 +543,7 @@ DAPI_(HRESULT) PathCreateTempFile(
528 { 543 {
529 hr = S_OK; 544 hr = S_OK;
530 } 545 }
531 ExitOnFailure(hr, "Failed to create file: %ls", sczTempFile); 546 PathExitOnFailure(hr, "Failed to create file: %ls", sczTempFile);
532 } 547 }
533 } 548 }
534 } 549 }
@@ -538,17 +553,17 @@ DAPI_(HRESULT) PathCreateTempFile(
538 if (INVALID_HANDLE_VALUE == hTempFile) 553 if (INVALID_HANDLE_VALUE == hTempFile)
539 { 554 {
540 hr = StrAlloc(&sczTempFile, MAX_PATH); 555 hr = StrAlloc(&sczTempFile, MAX_PATH);
541 ExitOnFailure(hr, "Failed to allocate memory for the temp path"); 556 PathExitOnFailure(hr, "Failed to allocate memory for the temp path");
542 557
543 if (!::GetTempFileNameW(sczTempPath, L"TMP", 0, sczTempFile)) 558 if (!::GetTempFileNameW(sczTempPath, L"TMP", 0, sczTempFile))
544 { 559 {
545 ExitWithLastError(hr, "Failed to create new temp file name."); 560 PathExitWithLastError(hr, "Failed to create new temp file name.");
546 } 561 }
547 562
548 hTempFile = ::CreateFileW(sczTempFile, GENERIC_WRITE, FILE_SHARE_READ | FILE_SHARE_DELETE, NULL, OPEN_EXISTING, dwFileAttributes, NULL); 563 hTempFile = ::CreateFileW(sczTempFile, GENERIC_WRITE, FILE_SHARE_READ | FILE_SHARE_DELETE, NULL, OPEN_EXISTING, dwFileAttributes, NULL);
549 if (INVALID_HANDLE_VALUE == hTempFile) 564 if (INVALID_HANDLE_VALUE == hTempFile)
550 { 565 {
551 ExitWithLastError(hr, "Failed to open new temp file: %ls", sczTempFile); 566 PathExitWithLastError(hr, "Failed to open new temp file: %ls", sczTempFile);
552 } 567 }
553 } 568 }
554 569
@@ -556,7 +571,7 @@ DAPI_(HRESULT) PathCreateTempFile(
556 if (psczTempFile) 571 if (psczTempFile)
557 { 572 {
558 hr = StrAllocString(psczTempFile, sczTempFile, 0); 573 hr = StrAllocString(psczTempFile, sczTempFile, 0);
559 ExitOnFailure(hr, "Failed to copy temp file string."); 574 PathExitOnFailure(hr, "Failed to copy temp file string.");
560 } 575 }
561 576
562 if (phTempFile) 577 if (phTempFile)
@@ -602,24 +617,24 @@ DAPI_(HRESULT) PathCreateTimeBasedTempFile(
602 if (wzDirectory && *wzDirectory) 617 if (wzDirectory && *wzDirectory)
603 { 618 {
604 hr = PathConcat(wzDirectory, wzPrefix, &sczPrefix); 619 hr = PathConcat(wzDirectory, wzPrefix, &sczPrefix);
605 ExitOnFailure(hr, "Failed to combine directory and log prefix."); 620 PathExitOnFailure(hr, "Failed to combine directory and log prefix.");
606 } 621 }
607 else 622 else
608 { 623 {
609 if (!::GetTempPathW(countof(wzTempPath), wzTempPath)) 624 if (!::GetTempPathW(countof(wzTempPath), wzTempPath))
610 { 625 {
611 ExitWithLastError(hr, "Failed to get temp folder."); 626 PathExitWithLastError(hr, "Failed to get temp folder.");
612 } 627 }
613 628
614 hr = PathConcat(wzTempPath, wzPrefix, &sczPrefix); 629 hr = PathConcat(wzTempPath, wzPrefix, &sczPrefix);
615 ExitOnFailure(hr, "Failed to concatenate the temp folder and log prefix."); 630 PathExitOnFailure(hr, "Failed to concatenate the temp folder and log prefix.");
616 } 631 }
617 632
618 hr = PathGetDirectory(sczPrefix, &sczPrefixFolder); 633 hr = PathGetDirectory(sczPrefix, &sczPrefixFolder);
619 if (S_OK == hr) 634 if (S_OK == hr)
620 { 635 {
621 hr = DirEnsureExists(sczPrefixFolder, NULL); 636 hr = DirEnsureExists(sczPrefixFolder, NULL);
622 ExitOnFailure(hr, "Failed to ensure temp file path exists: %ls", sczPrefixFolder); 637 PathExitOnFailure(hr, "Failed to ensure temp file path exists: %ls", sczPrefixFolder);
623 } 638 }
624 639
625 if (!wzPostfix) 640 if (!wzPostfix)
@@ -636,7 +651,7 @@ DAPI_(HRESULT) PathCreateTimeBasedTempFile(
636 651
637 // Log format: pre YYYY MM dd hh mm ss post ext 652 // Log format: pre YYYY MM dd hh mm ss post ext
638 hr = StrAllocFormatted(&sczTempPath, L"%ls_%04u%02u%02u%02u%02u%02u%ls%ls%ls", sczPrefix, time.wYear, time.wMonth, time.wDay, time.wHour, time.wMinute, time.wSecond, wzPostfix, L'.' == *wzExtension ? L"" : L".", wzExtension); 653 hr = StrAllocFormatted(&sczTempPath, L"%ls_%04u%02u%02u%02u%02u%02u%ls%ls%ls", sczPrefix, time.wYear, time.wMonth, time.wDay, time.wHour, time.wMinute, time.wSecond, wzPostfix, L'.' == *wzExtension ? L"" : L".", wzExtension);
639 ExitOnFailure(hr, "failed to allocate memory for the temp path"); 654 PathExitOnFailure(hr, "failed to allocate memory for the temp path");
640 655
641 hTempFile = ::CreateFileW(sczTempPath, GENERIC_WRITE, FILE_SHARE_READ, NULL, CREATE_NEW, FILE_ATTRIBUTE_NORMAL, NULL); 656 hTempFile = ::CreateFileW(sczTempPath, GENERIC_WRITE, FILE_SHARE_READ, NULL, CREATE_NEW, FILE_ATTRIBUTE_NORMAL, NULL);
642 if (INVALID_HANDLE_VALUE == hTempFile) 657 if (INVALID_HANDLE_VALUE == hTempFile)
@@ -655,14 +670,14 @@ DAPI_(HRESULT) PathCreateTimeBasedTempFile(
655 } 670 }
656 671
657 hr = HRESULT_FROM_WIN32(er); 672 hr = HRESULT_FROM_WIN32(er);
658 ExitOnFailureDebugTrace(hr, "Failed to create temp file: %ls", sczTempPath); 673 PathExitOnFailureDebugTrace(hr, "Failed to create temp file: %ls", sczTempPath);
659 } 674 }
660 } while (fRetry); 675 } while (fRetry);
661 676
662 if (psczTempFile) 677 if (psczTempFile)
663 { 678 {
664 hr = StrAllocString(psczTempFile, sczTempPath, 0); 679 hr = StrAllocString(psczTempFile, sczTempPath, 0);
665 ExitOnFailure(hr, "Failed to copy temp path to return."); 680 PathExitOnFailure(hr, "Failed to copy temp path to return.");
666 } 681 }
667 682
668 if (phTempFile) 683 if (phTempFile)
@@ -701,29 +716,29 @@ DAPI_(HRESULT) PathCreateTempDirectory(
701 if (wzDirectory && *wzDirectory) 716 if (wzDirectory && *wzDirectory)
702 { 717 {
703 hr = StrAllocString(&sczTempPath, wzDirectory, 0); 718 hr = StrAllocString(&sczTempPath, wzDirectory, 0);
704 ExitOnFailure(hr, "Failed to copy temp path."); 719 PathExitOnFailure(hr, "Failed to copy temp path.");
705 720
706 hr = PathBackslashTerminate(&sczTempPath); 721 hr = PathBackslashTerminate(&sczTempPath);
707 ExitOnFailure(hr, "Failed to ensure path ends in backslash: %ls", wzDirectory); 722 PathExitOnFailure(hr, "Failed to ensure path ends in backslash: %ls", wzDirectory);
708 } 723 }
709 else 724 else
710 { 725 {
711 hr = StrAlloc(&sczTempPath, cchTempPath); 726 hr = StrAlloc(&sczTempPath, cchTempPath);
712 ExitOnFailure(hr, "Failed to allocate memory for the temp path."); 727 PathExitOnFailure(hr, "Failed to allocate memory for the temp path.");
713 728
714 if (!::GetTempPathW(cchTempPath, sczTempPath)) 729 if (!::GetTempPathW(cchTempPath, sczTempPath))
715 { 730 {
716 ExitWithLastError(hr, "Failed to get temp path."); 731 PathExitWithLastError(hr, "Failed to get temp path.");
717 } 732 }
718 } 733 }
719 734
720 for (DWORD i = 1; i <= dwUniqueCount; ++i) 735 for (DWORD i = 1; i <= dwUniqueCount; ++i)
721 { 736 {
722 hr = StrAllocFormatted(&scz, wzDirectoryNameTemplate, i); 737 hr = StrAllocFormatted(&scz, wzDirectoryNameTemplate, i);
723 ExitOnFailure(hr, "Failed to allocate memory for directory name template."); 738 PathExitOnFailure(hr, "Failed to allocate memory for directory name template.");
724 739
725 hr = StrAllocFormatted(psczTempDirectory, L"%s%s", sczTempPath, scz); 740 hr = StrAllocFormatted(psczTempDirectory, L"%s%s", sczTempPath, scz);
726 ExitOnFailure(hr, "Failed to allocate temp directory name."); 741 PathExitOnFailure(hr, "Failed to allocate temp directory name.");
727 742
728 if (!::CreateDirectoryW(*psczTempDirectory, NULL)) 743 if (!::CreateDirectoryW(*psczTempDirectory, NULL))
729 { 744 {
@@ -750,10 +765,10 @@ DAPI_(HRESULT) PathCreateTempDirectory(
750 break; 765 break;
751 } 766 }
752 } 767 }
753 ExitOnFailure(hr, "Failed to create temp directory."); 768 PathExitOnFailure(hr, "Failed to create temp directory.");
754 769
755 hr = PathBackslashTerminate(psczTempDirectory); 770 hr = PathBackslashTerminate(psczTempDirectory);
756 ExitOnFailure(hr, "Failed to ensure temp directory is backslash terminated."); 771 PathExitOnFailure(hr, "Failed to ensure temp directory is backslash terminated.");
757 772
758LExit: 773LExit:
759 ReleaseStr(scz); 774 ReleaseStr(scz);
@@ -771,13 +786,13 @@ DAPI_(HRESULT) PathGetKnownFolder(
771 HRESULT hr = S_OK; 786 HRESULT hr = S_OK;
772 787
773 hr = StrAlloc(psczKnownFolder, MAX_PATH); 788 hr = StrAlloc(psczKnownFolder, MAX_PATH);
774 ExitOnFailure(hr, "Failed to allocate memory for known folder."); 789 PathExitOnFailure(hr, "Failed to allocate memory for known folder.");
775 790
776 hr = ::SHGetFolderPathW(NULL, csidl, NULL, SHGFP_TYPE_CURRENT, *psczKnownFolder); 791 hr = ::SHGetFolderPathW(NULL, csidl, NULL, SHGFP_TYPE_CURRENT, *psczKnownFolder);
777 ExitOnFailure(hr, "Failed to get known folder path."); 792 PathExitOnFailure(hr, "Failed to get known folder path.");
778 793
779 hr = PathBackslashTerminate(psczKnownFolder); 794 hr = PathBackslashTerminate(psczKnownFolder);
780 ExitOnFailure(hr, "Failed to ensure known folder path is backslash terminated."); 795 PathExitOnFailure(hr, "Failed to ensure known folder path is backslash terminated.");
781 796
782LExit: 797LExit:
783 return hr; 798 return hr;
@@ -804,23 +819,23 @@ DAPI_(HRESULT) PathConcat(
804 if (!wzPath2 || !*wzPath2) 819 if (!wzPath2 || !*wzPath2)
805 { 820 {
806 hr = StrAllocString(psczCombined, wzPath1, 0); 821 hr = StrAllocString(psczCombined, wzPath1, 0);
807 ExitOnFailure(hr, "Failed to copy just path1 to output."); 822 PathExitOnFailure(hr, "Failed to copy just path1 to output.");
808 } 823 }
809 else if (!wzPath1 || !*wzPath1 || PathIsAbsolute(wzPath2)) 824 else if (!wzPath1 || !*wzPath1 || PathIsAbsolute(wzPath2))
810 { 825 {
811 hr = StrAllocString(psczCombined, wzPath2, 0); 826 hr = StrAllocString(psczCombined, wzPath2, 0);
812 ExitOnFailure(hr, "Failed to copy just path2 to output."); 827 PathExitOnFailure(hr, "Failed to copy just path2 to output.");
813 } 828 }
814 else 829 else
815 { 830 {
816 hr = StrAllocString(psczCombined, wzPath1, 0); 831 hr = StrAllocString(psczCombined, wzPath1, 0);
817 ExitOnFailure(hr, "Failed to copy path1 to output."); 832 PathExitOnFailure(hr, "Failed to copy path1 to output.");
818 833
819 hr = PathBackslashTerminate(psczCombined); 834 hr = PathBackslashTerminate(psczCombined);
820 ExitOnFailure(hr, "Failed to backslashify."); 835 PathExitOnFailure(hr, "Failed to backslashify.");
821 836
822 hr = StrAllocConcat(psczCombined, wzPath2, 0); 837 hr = StrAllocConcat(psczCombined, wzPath2, 0);
823 ExitOnFailure(hr, "Failed to append path2 to output."); 838 PathExitOnFailure(hr, "Failed to append path2 to output.");
824 } 839 }
825 840
826LExit: 841LExit:
@@ -839,13 +854,13 @@ DAPI_(HRESULT) PathEnsureQuoted(
839 size_t cchPath = 0; 854 size_t cchPath = 0;
840 855
841 hr = ::StringCchLengthW(*ppszPath, STRSAFE_MAX_CCH, &cchPath); 856 hr = ::StringCchLengthW(*ppszPath, STRSAFE_MAX_CCH, &cchPath);
842 ExitOnFailure(hr, "Failed to get the length of the path."); 857 PathExitOnFailure(hr, "Failed to get the length of the path.");
843 858
844 // Handle simple special cases. 859 // Handle simple special cases.
845 if (0 == cchPath || (1 == cchPath && L'"' == (*ppszPath)[0])) 860 if (0 == cchPath || (1 == cchPath && L'"' == (*ppszPath)[0]))
846 { 861 {
847 hr = StrAllocString(ppszPath, L"\"\"", 2); 862 hr = StrAllocString(ppszPath, L"\"\"", 2);
848 ExitOnFailure(hr, "Failed to allocate a quoted empty string."); 863 PathExitOnFailure(hr, "Failed to allocate a quoted empty string.");
849 864
850 ExitFunction(); 865 ExitFunction();
851 } 866 }
@@ -853,7 +868,7 @@ DAPI_(HRESULT) PathEnsureQuoted(
853 if (L'"' != (*ppszPath)[0]) 868 if (L'"' != (*ppszPath)[0])
854 { 869 {
855 hr = StrAllocPrefix(ppszPath, L"\"", 1); 870 hr = StrAllocPrefix(ppszPath, L"\"", 1);
856 ExitOnFailure(hr, "Failed to allocate an opening quote."); 871 PathExitOnFailure(hr, "Failed to allocate an opening quote.");
857 872
858 // Add a char for the opening quote. 873 // Add a char for the opening quote.
859 ++cchPath; 874 ++cchPath;
@@ -862,7 +877,7 @@ DAPI_(HRESULT) PathEnsureQuoted(
862 if (L'"' != (*ppszPath)[cchPath - 1]) 877 if (L'"' != (*ppszPath)[cchPath - 1])
863 { 878 {
864 hr = StrAllocConcat(ppszPath, L"\"", 1); 879 hr = StrAllocConcat(ppszPath, L"\"", 1);
865 ExitOnFailure(hr, "Failed to allocate a closing quote."); 880 PathExitOnFailure(hr, "Failed to allocate a closing quote.");
866 881
867 // Add a char for the closing quote. 882 // Add a char for the closing quote.
868 ++cchPath; 883 ++cchPath;
@@ -876,7 +891,7 @@ DAPI_(HRESULT) PathEnsureQuoted(
876 (*ppszPath)[cchPath - 1] = L'\\'; 891 (*ppszPath)[cchPath - 1] = L'\\';
877 892
878 hr = StrAllocConcat(ppszPath, L"\"", 1); 893 hr = StrAllocConcat(ppszPath, L"\"", 1);
879 ExitOnFailure(hr, "Failed to allocate another closing quote after the backslash."); 894 PathExitOnFailure(hr, "Failed to allocate another closing quote after the backslash.");
880 } 895 }
881 } 896 }
882 897
@@ -897,10 +912,10 @@ DAPI_(HRESULT) PathCompare(
897 LPWSTR sczPath2 = NULL; 912 LPWSTR sczPath2 = NULL;
898 913
899 hr = PathExpand(&sczPath1, wzPath1, PATH_EXPAND_ENVIRONMENT | PATH_EXPAND_FULLPATH); 914 hr = PathExpand(&sczPath1, wzPath1, PATH_EXPAND_ENVIRONMENT | PATH_EXPAND_FULLPATH);
900 ExitOnFailure(hr, "Failed to expand path1."); 915 PathExitOnFailure(hr, "Failed to expand path1.");
901 916
902 hr = PathExpand(&sczPath2, wzPath2, PATH_EXPAND_ENVIRONMENT | PATH_EXPAND_FULLPATH); 917 hr = PathExpand(&sczPath2, wzPath2, PATH_EXPAND_ENVIRONMENT | PATH_EXPAND_FULLPATH);
903 ExitOnFailure(hr, "Failed to expand path2."); 918 PathExitOnFailure(hr, "Failed to expand path2.");
904 919
905 *pnResult = ::CompareStringW(LOCALE_NEUTRAL, NORM_IGNORECASE, sczPath1, -1, sczPath2, -1); 920 *pnResult = ::CompareStringW(LOCALE_NEUTRAL, NORM_IGNORECASE, sczPath1, -1, sczPath2, -1);
906 921
@@ -922,7 +937,7 @@ DAPI_(HRESULT) PathCompress(
922 hPath = ::CreateFileW(wzPath, GENERIC_READ | GENERIC_WRITE, FILE_SHARE_READ | FILE_SHARE_DELETE, NULL, OPEN_EXISTING, FILE_FLAG_BACKUP_SEMANTICS, NULL); 937 hPath = ::CreateFileW(wzPath, GENERIC_READ | GENERIC_WRITE, FILE_SHARE_READ | FILE_SHARE_DELETE, NULL, OPEN_EXISTING, FILE_FLAG_BACKUP_SEMANTICS, NULL);
923 if (INVALID_HANDLE_VALUE == hPath) 938 if (INVALID_HANDLE_VALUE == hPath)
924 { 939 {
925 ExitWithLastError(hr, "Failed to open path %ls for compression.", wzPath); 940 PathExitWithLastError(hr, "Failed to open path %ls for compression.", wzPath);
926 } 941 }
927 942
928 DWORD dwBytesReturned = 0; 943 DWORD dwBytesReturned = 0;
@@ -933,7 +948,7 @@ DAPI_(HRESULT) PathCompress(
933 DWORD er = ::GetLastError(); 948 DWORD er = ::GetLastError();
934 if (ERROR_INVALID_FUNCTION != er) 949 if (ERROR_INVALID_FUNCTION != er)
935 { 950 {
936 ExitOnWin32Error(er, hr, "Failed to set compression state for path %ls.", wzPath); 951 PathExitOnWin32Error(er, hr, "Failed to set compression state for path %ls.", wzPath);
937 } 952 }
938 } 953 }
939 954
@@ -945,7 +960,7 @@ LExit:
945 960
946DAPI_(HRESULT) PathGetHierarchyArray( 961DAPI_(HRESULT) PathGetHierarchyArray(
947 __in_z LPCWSTR wzPath, 962 __in_z LPCWSTR wzPath,
948 __deref_inout_ecount_opt(*pcStrArray) LPWSTR **prgsczPathArray, 963 __deref_inout_ecount_opt(*pcPathArray) LPWSTR **prgsczPathArray,
949 __inout LPUINT pcPathArray 964 __inout LPUINT pcPathArray
950 ) 965 )
951{ 966{
@@ -975,16 +990,16 @@ DAPI_(HRESULT) PathGetHierarchyArray(
975 Assert(cArraySpacesNeeded >= 1); 990 Assert(cArraySpacesNeeded >= 1);
976 991
977 hr = MemEnsureArraySize(reinterpret_cast<void **>(prgsczPathArray), cArraySpacesNeeded, sizeof(LPWSTR), 0); 992 hr = MemEnsureArraySize(reinterpret_cast<void **>(prgsczPathArray), cArraySpacesNeeded, sizeof(LPWSTR), 0);
978 ExitOnFailure(hr, "Failed to allocate array of size %u for parent directories", cArraySpacesNeeded); 993 PathExitOnFailure(hr, "Failed to allocate array of size %u for parent directories", cArraySpacesNeeded);
979 *pcPathArray = cArraySpacesNeeded; 994 *pcPathArray = cArraySpacesNeeded;
980 995
981 hr = StrAllocString(&sczPathCopy, wzPath, 0); 996 hr = StrAllocString(&sczPathCopy, wzPath, 0);
982 ExitOnFailure(hr, "Failed to allocate copy of original path"); 997 PathExitOnFailure(hr, "Failed to allocate copy of original path");
983 998
984 for (DWORD i = 0; i < cArraySpacesNeeded; ++i) 999 for (DWORD i = 0; i < cArraySpacesNeeded; ++i)
985 { 1000 {
986 hr = StrAllocString((*prgsczPathArray) + cArraySpacesNeeded - 1 - i, sczPathCopy, 0); 1001 hr = StrAllocString((*prgsczPathArray) + cArraySpacesNeeded - 1 - i, sczPathCopy, 0);
987 ExitOnFailure(hr, "Failed to copy path"); 1002 PathExitOnFailure(hr, "Failed to copy path");
988 1003
989 // If it ends in a backslash, it's a directory path, so cut off everything the last backslash before we get the directory portion of the path 1004 // If it ends in a backslash, it's a directory path, so cut off everything the last backslash before we get the directory portion of the path
990 if (wzPath[lstrlenW(sczPathCopy) - 1] == L'\\') 1005 if (wzPath[lstrlenW(sczPathCopy) - 1] == L'\\')
@@ -993,7 +1008,7 @@ DAPI_(HRESULT) PathGetHierarchyArray(
993 } 1008 }
994 1009
995 hr = PathGetDirectory(sczPathCopy, &sczNewPathCopy); 1010 hr = PathGetDirectory(sczPathCopy, &sczNewPathCopy);
996 ExitOnFailure(hr, "Failed to get directory portion of path"); 1011 PathExitOnFailure(hr, "Failed to get directory portion of path");
997 1012
998 ReleaseStr(sczPathCopy); 1013 ReleaseStr(sczPathCopy);
999 sczPathCopy = sczNewPathCopy; 1014 sczPathCopy = sczNewPathCopy;