summaryrefslogtreecommitdiff
path: root/src/libs/dutil/WixToolset.DUtil/dirutil.cpp
diff options
context:
space:
mode:
Diffstat (limited to 'src/libs/dutil/WixToolset.DUtil/dirutil.cpp')
-rw-r--r--src/libs/dutil/WixToolset.DUtil/dirutil.cpp64
1 files changed, 20 insertions, 44 deletions
diff --git a/src/libs/dutil/WixToolset.DUtil/dirutil.cpp b/src/libs/dutil/WixToolset.DUtil/dirutil.cpp
index 94eab9e7..2c02225d 100644
--- a/src/libs/dutil/WixToolset.DUtil/dirutil.cpp
+++ b/src/libs/dutil/WixToolset.DUtil/dirutil.cpp
@@ -59,34 +59,10 @@ LExit:
59 *******************************************************************/ 59 *******************************************************************/
60extern "C" HRESULT DAPI DirCreateTempPath( 60extern "C" HRESULT DAPI DirCreateTempPath(
61 __in_z LPCWSTR wzPrefix, 61 __in_z LPCWSTR wzPrefix,
62 __out_ecount_z(cchPath) LPWSTR wzPath, 62 __out_opt LPWSTR* psczTempFile
63 __in DWORD cchPath
64 ) 63 )
65{ 64{
66 Assert(wzPrefix); 65 return PathCreateTempFile(NULL, NULL, 0, wzPrefix, 0, psczTempFile, NULL);
67 Assert(wzPath);
68
69 HRESULT hr = S_OK;
70
71 WCHAR wzDir[MAX_PATH];
72 WCHAR wzFile[MAX_PATH];
73 DWORD cch = 0;
74
75 cch = ::GetTempPathW(countof(wzDir), wzDir);
76 if (!cch || cch >= countof(wzDir))
77 {
78 DirExitWithLastError(hr, "Failed to GetTempPath.");
79 }
80
81 if (!::GetTempFileNameW(wzDir, wzPrefix, 0, wzFile))
82 {
83 DirExitWithLastError(hr, "Failed to GetTempFileName.");
84 }
85
86 hr = ::StringCchCopyW(wzPath, cchPath, wzFile);
87
88LExit:
89 return hr;
90} 66}
91 67
92 68
@@ -192,18 +168,19 @@ extern "C" HRESULT DAPI DirEnsureDeleteEx(
192 Assert(wzPath && *wzPath); 168 Assert(wzPath && *wzPath);
193 169
194 HRESULT hr = S_OK; 170 HRESULT hr = S_OK;
195 DWORD er; 171 DWORD er = ERROR_SUCCESS;
196 172
197 DWORD dwAttrib; 173 DWORD dwAttrib = 0;
198 HANDLE hFind = INVALID_HANDLE_VALUE; 174 HANDLE hFind = INVALID_HANDLE_VALUE;
199 LPWSTR sczDelete = NULL; 175 LPWSTR sczDelete = NULL;
200 WIN32_FIND_DATAW wfd; 176 WIN32_FIND_DATAW wfd = { };
201 177
202 BOOL fDeleteFiles = (DIR_DELETE_FILES == (dwFlags & DIR_DELETE_FILES)); 178 BOOL fDeleteFiles = (DIR_DELETE_FILES == (dwFlags & DIR_DELETE_FILES));
203 BOOL fRecurse = (DIR_DELETE_RECURSE == (dwFlags & DIR_DELETE_RECURSE)); 179 BOOL fRecurse = (DIR_DELETE_RECURSE == (dwFlags & DIR_DELETE_RECURSE));
204 BOOL fScheduleDelete = (DIR_DELETE_SCHEDULE == (dwFlags & DIR_DELETE_SCHEDULE)); 180 BOOL fScheduleDelete = (DIR_DELETE_SCHEDULE == (dwFlags & DIR_DELETE_SCHEDULE));
205 WCHAR wzTempDirectory[MAX_PATH] = { }; 181 WCHAR wzSafeFileName[MAX_PATH + 1] = { };
206 WCHAR wzTempPath[MAX_PATH] = { }; 182 LPWSTR sczTempDirectory = NULL;
183 LPWSTR sczTempPath = NULL;
207 184
208 if (-1 == (dwAttrib = ::GetFileAttributesW(wzPath))) 185 if (-1 == (dwAttrib = ::GetFileAttributesW(wzPath)))
209 { 186 {
@@ -231,10 +208,8 @@ extern "C" HRESULT DAPI DirEnsureDeleteEx(
231 { 208 {
232 if (fScheduleDelete) 209 if (fScheduleDelete)
233 { 210 {
234 if (!::GetTempPathW(countof(wzTempDirectory), wzTempDirectory)) 211 hr = PathGetTempPath(&sczTempDirectory, NULL);
235 { 212 DirExitOnFailure(hr, "Failed to get temp directory.");
236 DirExitWithLastError(hr, "Failed to get temp directory.");
237 }
238 } 213 }
239 214
240 // Delete everything in this directory. 215 // Delete everything in this directory.
@@ -256,10 +231,11 @@ extern "C" HRESULT DAPI DirEnsureDeleteEx(
256 } 231 }
257 232
258 // For extra safety and to silence OACR. 233 // For extra safety and to silence OACR.
259 wfd.cFileName[MAX_PATH - 1] = L'\0'; 234 hr = ::StringCchCopyNExW(wzSafeFileName, countof(wzSafeFileName), wfd.cFileName, countof(wfd.cFileName), NULL, NULL, STRSAFE_FILL_BEHIND_NULL | STRSAFE_NULL_ON_FAILURE);
235 DirExitOnFailure(hr, "Failed to ensure file name was null terminated.");
260 236
261 hr = PathConcat(wzPath, wfd.cFileName, &sczDelete); 237 hr = PathConcat(wzPath, wzSafeFileName, &sczDelete);
262 DirExitOnFailure(hr, "Failed to concat filename '%ls' to directory: %ls", wfd.cFileName, wzPath); 238 DirExitOnFailure(hr, "Failed to concat filename '%ls' to directory: %ls", wzSafeFileName, wzPath);
263 239
264 if (fRecurse && wfd.dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY) 240 if (fRecurse && wfd.dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY)
265 { 241 {
@@ -288,16 +264,14 @@ extern "C" HRESULT DAPI DirEnsureDeleteEx(
288 { 264 {
289 if (fScheduleDelete) 265 if (fScheduleDelete)
290 { 266 {
291 if (!::GetTempFileNameW(wzTempDirectory, L"DEL", 0, wzTempPath)) 267 hr = PathGetTempFileName(sczTempDirectory, L"DEL", 0, &sczTempPath);
292 { 268 DirExitOnFailure(hr, "Failed to get temp file to move to.");
293 DirExitWithLastError(hr, "Failed to get temp file to move to.");
294 }
295 269
296 // Try to move the file to the temp directory then schedule for delete, 270 // Try to move the file to the temp directory then schedule for delete,
297 // otherwise just schedule for delete. 271 // otherwise just schedule for delete.
298 if (::MoveFileExW(sczDelete, wzTempPath, MOVEFILE_REPLACE_EXISTING)) 272 if (::MoveFileExW(sczDelete, sczTempPath, MOVEFILE_REPLACE_EXISTING))
299 { 273 {
300 ::MoveFileExW(wzTempPath, NULL, MOVEFILE_DELAY_UNTIL_REBOOT); 274 ::MoveFileExW(sczTempPath, NULL, MOVEFILE_DELAY_UNTIL_REBOOT);
301 } 275 }
302 else 276 else
303 { 277 {
@@ -348,6 +322,8 @@ extern "C" HRESULT DAPI DirEnsureDeleteEx(
348LExit: 322LExit:
349 ReleaseFileFindHandle(hFind); 323 ReleaseFileFindHandle(hFind);
350 ReleaseStr(sczDelete); 324 ReleaseStr(sczDelete);
325 ReleaseStr(sczTempDirectory);
326 ReleaseStr(sczTempPath);
351 327
352 return hr; 328 return hr;
353} 329}