aboutsummaryrefslogtreecommitdiff
path: root/src/dutil/dirutil.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/dirutil.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/dirutil.cpp')
-rw-r--r--src/dutil/dirutil.cpp61
1 files changed, 38 insertions, 23 deletions
diff --git a/src/dutil/dirutil.cpp b/src/dutil/dirutil.cpp
index 5e22ee65..81130a8d 100644
--- a/src/dutil/dirutil.cpp
+++ b/src/dutil/dirutil.cpp
@@ -3,6 +3,21 @@
3#include "precomp.h" 3#include "precomp.h"
4 4
5 5
6// Exit macros
7#define DirExitOnLastError(x, s, ...) ExitOnLastErrorSource(DUTIL_SOURCE_DIRUTIL, x, s, __VA_ARGS__)
8#define DirExitOnLastErrorDebugTrace(x, s, ...) ExitOnLastErrorDebugTraceSource(DUTIL_SOURCE_DIRUTIL, x, s, __VA_ARGS__)
9#define DirExitWithLastError(x, s, ...) ExitWithLastErrorSource(DUTIL_SOURCE_DIRUTIL, x, s, __VA_ARGS__)
10#define DirExitOnFailure(x, s, ...) ExitOnFailureSource(DUTIL_SOURCE_DIRUTIL, x, s, __VA_ARGS__)
11#define DirExitOnRootFailure(x, s, ...) ExitOnRootFailureSource(DUTIL_SOURCE_DIRUTIL, x, s, __VA_ARGS__)
12#define DirExitOnFailureDebugTrace(x, s, ...) ExitOnFailureDebugTraceSource(DUTIL_SOURCE_DIRUTIL, x, s, __VA_ARGS__)
13#define DirExitOnNull(p, x, e, s, ...) ExitOnNullSource(DUTIL_SOURCE_DIRUTIL, p, x, e, s, __VA_ARGS__)
14#define DirExitOnNullWithLastError(p, x, s, ...) ExitOnNullWithLastErrorSource(DUTIL_SOURCE_DIRUTIL, p, x, s, __VA_ARGS__)
15#define DirExitOnNullDebugTrace(p, x, e, s, ...) ExitOnNullDebugTraceSource(DUTIL_SOURCE_DIRUTIL, p, x, e, s, __VA_ARGS__)
16#define DirExitOnInvalidHandleWithLastError(p, x, s, ...) ExitOnInvalidHandleWithLastErrorSource(DUTIL_SOURCE_DIRUTIL, p, x, s, __VA_ARGS__)
17#define DirExitOnWin32Error(e, x, s, ...) ExitOnWin32ErrorSource(DUTIL_SOURCE_DIRUTIL, e, x, s, __VA_ARGS__)
18#define DirExitOnGdipFailure(g, x, s, ...) ExitOnGdipFailureSource(DUTIL_SOURCE_DIRUTIL, g, x, s, __VA_ARGS__)
19
20
6/******************************************************************* 21/*******************************************************************
7 DirExists 22 DirExists
8 23
@@ -59,12 +74,12 @@ extern "C" HRESULT DAPI DirCreateTempPath(
59 cch = ::GetTempPathW(countof(wzDir), wzDir); 74 cch = ::GetTempPathW(countof(wzDir), wzDir);
60 if (!cch || cch >= countof(wzDir)) 75 if (!cch || cch >= countof(wzDir))
61 { 76 {
62 ExitWithLastError(hr, "Failed to GetTempPath."); 77 DirExitWithLastError(hr, "Failed to GetTempPath.");
63 } 78 }
64 79
65 if (!::GetTempFileNameW(wzDir, wzPrefix, 0, wzFile)) 80 if (!::GetTempFileNameW(wzDir, wzPrefix, 0, wzFile))
66 { 81 {
67 ExitWithLastError(hr, "Failed to GetTempFileName."); 82 DirExitWithLastError(hr, "Failed to GetTempFileName.");
68 } 83 }
69 84
70 hr = ::StringCchCopyW(wzPath, cchPath, wzFile); 85 hr = ::StringCchCopyW(wzPath, cchPath, wzFile);
@@ -111,12 +126,12 @@ extern "C" HRESULT DAPI DirEnsureExists(
111 } 126 }
112 127
113 // if there is no parent directory fail 128 // if there is no parent directory fail
114 ExitOnNullDebugTrace(pwzLastSlash, hr, HRESULT_FROM_WIN32(ERROR_PATH_NOT_FOUND), "cannot find parent path"); 129 DirExitOnNullDebugTrace(pwzLastSlash, hr, HRESULT_FROM_WIN32(ERROR_PATH_NOT_FOUND), "cannot find parent path");
115 130
116 *pwzLastSlash = L'\0'; // null terminate the parent path 131 *pwzLastSlash = L'\0'; // null terminate the parent path
117 hr = DirEnsureExists(wzPath, psa); // recurse! 132 hr = DirEnsureExists(wzPath, psa); // recurse!
118 *pwzLastSlash = L'\\'; // put the slash back 133 *pwzLastSlash = L'\\'; // put the slash back
119 ExitOnFailureDebugTrace(hr, "failed to create path: %ls", wzPath); 134 DirExitOnFailureDebugTrace(hr, "failed to create path: %ls", wzPath);
120 135
121 // try to create the directory now that all parents are created 136 // try to create the directory now that all parents are created
122 if (!::CreateDirectoryW(wzPath, psa)) 137 if (!::CreateDirectoryW(wzPath, psa))
@@ -197,7 +212,7 @@ extern "C" HRESULT DAPI DirEnsureDeleteEx(
197 er = ERROR_PATH_NOT_FOUND; 212 er = ERROR_PATH_NOT_FOUND;
198 } 213 }
199 hr = HRESULT_FROM_WIN32(er); 214 hr = HRESULT_FROM_WIN32(er);
200 ExitOnRootFailure(hr, "Failed to get attributes for path: %ls", wzPath); 215 DirExitOnRootFailure(hr, "Failed to get attributes for path: %ls", wzPath);
201 } 216 }
202 217
203 if (dwAttrib & FILE_ATTRIBUTE_DIRECTORY) 218 if (dwAttrib & FILE_ATTRIBUTE_DIRECTORY)
@@ -206,7 +221,7 @@ extern "C" HRESULT DAPI DirEnsureDeleteEx(
206 { 221 {
207 if (!::SetFileAttributesW(wzPath, FILE_ATTRIBUTE_NORMAL)) 222 if (!::SetFileAttributesW(wzPath, FILE_ATTRIBUTE_NORMAL))
208 { 223 {
209 ExitWithLastError(hr, "Failed to remove read-only attribute from path: %ls", wzPath); 224 DirExitWithLastError(hr, "Failed to remove read-only attribute from path: %ls", wzPath);
210 } 225 }
211 } 226 }
212 227
@@ -217,18 +232,18 @@ extern "C" HRESULT DAPI DirEnsureDeleteEx(
217 { 232 {
218 if (!::GetTempPathW(countof(wzTempDirectory), wzTempDirectory)) 233 if (!::GetTempPathW(countof(wzTempDirectory), wzTempDirectory))
219 { 234 {
220 ExitWithLastError(hr, "Failed to get temp directory."); 235 DirExitWithLastError(hr, "Failed to get temp directory.");
221 } 236 }
222 } 237 }
223 238
224 // Delete everything in this directory. 239 // Delete everything in this directory.
225 hr = PathConcat(wzPath, L"*.*", &sczDelete); 240 hr = PathConcat(wzPath, L"*.*", &sczDelete);
226 ExitOnFailure(hr, "Failed to concat wild cards to string: %ls", wzPath); 241 DirExitOnFailure(hr, "Failed to concat wild cards to string: %ls", wzPath);
227 242
228 hFind = ::FindFirstFileW(sczDelete, &wfd); 243 hFind = ::FindFirstFileW(sczDelete, &wfd);
229 if (INVALID_HANDLE_VALUE == hFind) 244 if (INVALID_HANDLE_VALUE == hFind)
230 { 245 {
231 ExitWithLastError(hr, "failed to get first file in directory: %ls", wzPath); 246 DirExitWithLastError(hr, "failed to get first file in directory: %ls", wzPath);
232 } 247 }
233 248
234 do 249 do
@@ -243,18 +258,18 @@ extern "C" HRESULT DAPI DirEnsureDeleteEx(
243 wfd.cFileName[MAX_PATH - 1] = L'\0'; 258 wfd.cFileName[MAX_PATH - 1] = L'\0';
244 259
245 hr = PathConcat(wzPath, wfd.cFileName, &sczDelete); 260 hr = PathConcat(wzPath, wfd.cFileName, &sczDelete);
246 ExitOnFailure(hr, "Failed to concat filename '%ls' to directory: %ls", wfd.cFileName, wzPath); 261 DirExitOnFailure(hr, "Failed to concat filename '%ls' to directory: %ls", wfd.cFileName, wzPath);
247 262
248 if (fRecurse && wfd.dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY) 263 if (fRecurse && wfd.dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY)
249 { 264 {
250 hr = PathBackslashTerminate(&sczDelete); 265 hr = PathBackslashTerminate(&sczDelete);
251 ExitOnFailure(hr, "Failed to ensure path is backslash terminated: %ls", sczDelete); 266 DirExitOnFailure(hr, "Failed to ensure path is backslash terminated: %ls", sczDelete);
252 267
253 hr = DirEnsureDeleteEx(sczDelete, dwFlags); // recursive call 268 hr = DirEnsureDeleteEx(sczDelete, dwFlags); // recursive call
254 if (FAILED(hr)) 269 if (FAILED(hr))
255 { 270 {
256 // if we failed to delete a subdirectory, keep trying to finish any remaining files 271 // if we failed to delete a subdirectory, keep trying to finish any remaining files
257 ExitTraceSource(DUTIL_SOURCE_DEFAULT, hr, "Failed to delete subdirectory; continuing: %ls", sczDelete); 272 ExitTraceSource(DUTIL_SOURCE_DIRUTIL, hr, "Failed to delete subdirectory; continuing: %ls", sczDelete);
258 hr = S_OK; 273 hr = S_OK;
259 } 274 }
260 } 275 }
@@ -264,7 +279,7 @@ extern "C" HRESULT DAPI DirEnsureDeleteEx(
264 { 279 {
265 if (!::SetFileAttributesW(sczDelete, FILE_ATTRIBUTE_NORMAL)) 280 if (!::SetFileAttributesW(sczDelete, FILE_ATTRIBUTE_NORMAL))
266 { 281 {
267 ExitWithLastError(hr, "Failed to remove attributes from file: %ls", sczDelete); 282 DirExitWithLastError(hr, "Failed to remove attributes from file: %ls", sczDelete);
268 } 283 }
269 } 284 }
270 285
@@ -274,7 +289,7 @@ extern "C" HRESULT DAPI DirEnsureDeleteEx(
274 { 289 {
275 if (!::GetTempFileNameW(wzTempDirectory, L"DEL", 0, wzTempPath)) 290 if (!::GetTempFileNameW(wzTempDirectory, L"DEL", 0, wzTempPath))
276 { 291 {
277 ExitWithLastError(hr, "Failed to get temp file to move to."); 292 DirExitWithLastError(hr, "Failed to get temp file to move to.");
278 } 293 }
279 294
280 // Try to move the file to the temp directory then schedule for delete, 295 // Try to move the file to the temp directory then schedule for delete,
@@ -290,7 +305,7 @@ extern "C" HRESULT DAPI DirEnsureDeleteEx(
290 } 305 }
291 else 306 else
292 { 307 {
293 ExitWithLastError(hr, "Failed to delete file: %ls", sczDelete); 308 DirExitWithLastError(hr, "Failed to delete file: %ls", sczDelete);
294 } 309 }
295 } 310 }
296 } 311 }
@@ -303,7 +318,7 @@ extern "C" HRESULT DAPI DirEnsureDeleteEx(
303 } 318 }
304 else 319 else
305 { 320 {
306 ExitWithLastError(hr, "Failed while looping through files in directory: %ls", wzPath); 321 DirExitWithLastError(hr, "Failed while looping through files in directory: %ls", wzPath);
307 } 322 }
308 } 323 }
309 324
@@ -318,13 +333,13 @@ extern "C" HRESULT DAPI DirEnsureDeleteEx(
318 } 333 }
319 } 334 }
320 335
321 ExitOnRootFailure(hr, "Failed to remove directory: %ls", wzPath); 336 DirExitOnRootFailure(hr, "Failed to remove directory: %ls", wzPath);
322 } 337 }
323 } 338 }
324 else 339 else
325 { 340 {
326 hr = E_UNEXPECTED; 341 hr = E_UNEXPECTED;
327 ExitOnFailure(hr, "Directory delete cannot delete file: %ls", wzPath); 342 DirExitOnFailure(hr, "Directory delete cannot delete file: %ls", wzPath);
328 } 343 }
329 344
330 Assert(S_OK == hr); 345 Assert(S_OK == hr);
@@ -351,22 +366,22 @@ extern "C" HRESULT DAPI DirGetCurrent(
351 if (psczCurrentDirectory && *psczCurrentDirectory) 366 if (psczCurrentDirectory && *psczCurrentDirectory)
352 { 367 {
353 hr = StrMaxLength(*psczCurrentDirectory, &cch); 368 hr = StrMaxLength(*psczCurrentDirectory, &cch);
354 ExitOnFailure(hr, "Failed to determine size of current directory."); 369 DirExitOnFailure(hr, "Failed to determine size of current directory.");
355 } 370 }
356 371
357 DWORD cchRequired = ::GetCurrentDirectoryW(static_cast<DWORD>(cch), 0 == cch ? NULL : *psczCurrentDirectory); 372 DWORD cchRequired = ::GetCurrentDirectoryW(static_cast<DWORD>(cch), 0 == cch ? NULL : *psczCurrentDirectory);
358 if (0 == cchRequired) 373 if (0 == cchRequired)
359 { 374 {
360 ExitWithLastError(hr, "Failed to get current directory."); 375 DirExitWithLastError(hr, "Failed to get current directory.");
361 } 376 }
362 else if (cch < cchRequired) 377 else if (cch < cchRequired)
363 { 378 {
364 hr = StrAlloc(psczCurrentDirectory, cchRequired); 379 hr = StrAlloc(psczCurrentDirectory, cchRequired);
365 ExitOnFailure(hr, "Failed to allocate string for current directory."); 380 DirExitOnFailure(hr, "Failed to allocate string for current directory.");
366 381
367 if (!::GetCurrentDirectoryW(cchRequired, *psczCurrentDirectory)) 382 if (!::GetCurrentDirectoryW(cchRequired, *psczCurrentDirectory))
368 { 383 {
369 ExitWithLastError(hr, "Failed to get current directory using allocated string."); 384 DirExitWithLastError(hr, "Failed to get current directory using allocated string.");
370 } 385 }
371 } 386 }
372 387
@@ -387,7 +402,7 @@ extern "C" HRESULT DAPI DirSetCurrent(
387 402
388 if (!::SetCurrentDirectoryW(wzDirectory)) 403 if (!::SetCurrentDirectoryW(wzDirectory))
389 { 404 {
390 ExitWithLastError(hr, "Failed to set current directory to: %ls", wzDirectory); 405 DirExitWithLastError(hr, "Failed to set current directory to: %ls", wzDirectory);
391 } 406 }
392 407
393LExit: 408LExit: