aboutsummaryrefslogtreecommitdiff
path: root/src
diff options
context:
space:
mode:
authorRob Mensching <rob@firegiant.com>2025-02-10 17:38:43 -0800
committerRob Mensching <rob@firegiant.com>2025-02-11 15:49:33 -0800
commit83cb7864064fa6181902c28fc56d26f236881abe (patch)
treeb7e1e58bcf17c8ba161ad68dde3da5fcc6581ae3 /src
parenta797638d231b568b3e53bb2f478c28b6c0d5a1dc (diff)
downloadwix-83cb7864064fa6181902c28fc56d26f236881abe.tar.gz
wix-83cb7864064fa6181902c28fc56d26f236881abe.tar.bz2
wix-83cb7864064fa6181902c28fc56d26f236881abe.zip
Support CreateFile with retries
Diffstat (limited to 'src')
-rw-r--r--src/libs/dutil/WixToolset.DUtil/fileutil.cpp53
-rw-r--r--src/libs/dutil/WixToolset.DUtil/inc/fileutil.h29
2 files changed, 69 insertions, 13 deletions
diff --git a/src/libs/dutil/WixToolset.DUtil/fileutil.cpp b/src/libs/dutil/WixToolset.DUtil/fileutil.cpp
index 31802d29..abe1f6ad 100644
--- a/src/libs/dutil/WixToolset.DUtil/fileutil.cpp
+++ b/src/libs/dutil/WixToolset.DUtil/fileutil.cpp
@@ -32,7 +32,7 @@ __out LPWSTR *ppwzFileNameNoExtension
32) 32)
33{ 33{
34 Assert(wzFileName && *wzFileName); 34 Assert(wzFileName && *wzFileName);
35 35
36 HRESULT hr = S_OK; 36 HRESULT hr = S_OK;
37 size_t cchFileName = 0; 37 size_t cchFileName = 0;
38 LPWSTR pwzFileNameNoExtension = NULL; 38 LPWSTR pwzFileNameNoExtension = NULL;
@@ -90,7 +90,7 @@ extern "C" HRESULT DAPI FileChangeExtension(
90 90
91LExit: 91LExit:
92 ReleaseStr(sczFileName); 92 ReleaseStr(sczFileName);
93 93
94 return hr; 94 return hr;
95} 95}
96 96
@@ -140,7 +140,7 @@ extern "C" HRESULT DAPI FileAddSuffixToBaseName(
140 140
141LExit: 141LExit:
142 ReleaseStr(sczNewFileName); 142 ReleaseStr(sczNewFileName);
143 143
144 return hr; 144 return hr;
145} 145}
146 146
@@ -1340,6 +1340,51 @@ LExit:
1340 1340
1341 1341
1342/******************************************************************* 1342/*******************************************************************
1343 FileCreateWithRetry - create a file with multiple attempts if necessary
1344
1345********************************************************************/
1346extern "C" HRESULT DAPI FileCreateWithRetry(
1347 __in LPCWSTR wzFile,
1348 __in DWORD dwDesiredAccess,
1349 __in DWORD dwShareMode,
1350 __in_opt LPSECURITY_ATTRIBUTES lpSecurityAttributes,
1351 __in DWORD dwCreationDisposition,
1352 __in DWORD dwFlagsAndAttributes,
1353 __in DWORD cRetry,
1354 __in DWORD dwWaitMilliseconds,
1355 __out HANDLE* phFile
1356 )
1357{
1358 HRESULT hr = HRESULT_FROM_WIN32(ERROR_INVALID_HANDLE);
1359
1360 *phFile = INVALID_HANDLE_VALUE;
1361
1362 for (DWORD i = 0; i <= cRetry; ++i)
1363 {
1364 if (0 < i)
1365 {
1366 ::Sleep(dwWaitMilliseconds);
1367 }
1368
1369 *phFile = ::CreateFileW(wzFile, dwDesiredAccess, dwShareMode, lpSecurityAttributes, dwCreationDisposition, dwFlagsAndAttributes, NULL);
1370 if (INVALID_HANDLE_VALUE == *phFile)
1371 {
1372 DWORD er = ::GetLastError();
1373 hr = HRESULT_FROM_WIN32(er ? er : ERROR_INVALID_HANDLE);
1374 }
1375 else
1376 {
1377 hr = S_OK;
1378 break;
1379 }
1380 }
1381 FileExitOnRootFailure(hr, "Failed to create file: %ls after %u retries.", wzFile, cRetry);
1382
1383LExit:
1384 return hr;
1385}
1386
1387/*******************************************************************
1343 FileIsSame 1388 FileIsSame
1344 1389
1345********************************************************************/ 1390********************************************************************/
@@ -1479,7 +1524,7 @@ extern "C" HRESULT DAPI FileResetTime(
1479 1524
1480 hFile = ::CreateFileW(wzFile, FILE_WRITE_ATTRIBUTES | FILE_READ_ATTRIBUTES, FILE_SHARE_WRITE | FILE_SHARE_READ, NULL, OPEN_EXISTING, 0, NULL); 1525 hFile = ::CreateFileW(wzFile, FILE_WRITE_ATTRIBUTES | FILE_READ_ATTRIBUTES, FILE_SHARE_WRITE | FILE_SHARE_READ, NULL, OPEN_EXISTING, 0, NULL);
1481 FileExitOnInvalidHandleWithLastError(hFile, hr, "Failed to open file. File = '%ls'", wzFile); 1526 FileExitOnInvalidHandleWithLastError(hFile, hr, "Failed to open file. File = '%ls'", wzFile);
1482 1527
1483 if (!::GetFileTime(hFile, &ftCreateTime, NULL, NULL)) 1528 if (!::GetFileTime(hFile, &ftCreateTime, NULL, NULL))
1484 { 1529 {
1485 FileExitWithLastError(hr, "Failed to get file time for file. File = '%ls'", wzFile); 1530 FileExitWithLastError(hr, "Failed to get file time for file. File = '%ls'", wzFile);
diff --git a/src/libs/dutil/WixToolset.DUtil/inc/fileutil.h b/src/libs/dutil/WixToolset.DUtil/inc/fileutil.h
index 868312dc..c206fc5c 100644
--- a/src/libs/dutil/WixToolset.DUtil/inc/fileutil.h
+++ b/src/libs/dutil/WixToolset.DUtil/inc/fileutil.h
@@ -49,8 +49,8 @@ HRESULT DAPI FileAddSuffixToBaseName(
49 __out_z LPWSTR* psczNewFileName 49 __out_z LPWSTR* psczNewFileName
50 ); 50 );
51HRESULT DAPI FileVersionFromString( 51HRESULT DAPI FileVersionFromString(
52 __in_z LPCWSTR wzVersion, 52 __in_z LPCWSTR wzVersion,
53 __out DWORD *pdwVerMajor, 53 __out DWORD *pdwVerMajor,
54 __out DWORD* pdwVerMinor 54 __out DWORD* pdwVerMinor
55 ); 55 );
56HRESULT DAPI FileVersionFromStringEx( 56HRESULT DAPI FileVersionFromStringEx(
@@ -73,11 +73,11 @@ HRESULT DAPI FileSize(
73 __out LONGLONG* pllSize 73 __out LONGLONG* pllSize
74 ); 74 );
75HRESULT DAPI FileSizeByHandle( 75HRESULT DAPI FileSizeByHandle(
76 __in HANDLE hFile, 76 __in HANDLE hFile,
77 __out LONGLONG* pllSize 77 __out LONGLONG* pllSize
78 ); 78 );
79BOOL DAPI FileExistsEx( 79BOOL DAPI FileExistsEx(
80 __in_z LPCWSTR wzPath, 80 __in_z LPCWSTR wzPath,
81 __out_opt DWORD *pdwAttributes 81 __out_opt DWORD *pdwAttributes
82 ); 82 );
83BOOL DAPI FileExistsAfterRestart( 83BOOL DAPI FileExistsAfterRestart(
@@ -166,8 +166,8 @@ HRESULT DAPI FileEnsureCopyWithRetry(
166 __in DWORD dwWaitMilliseconds 166 __in DWORD dwWaitMilliseconds
167 ); 167 );
168HRESULT DAPI FileEnsureMove( 168HRESULT DAPI FileEnsureMove(
169 __in_z LPCWSTR wzSource, 169 __in_z LPCWSTR wzSource,
170 __in_z LPCWSTR wzTarget, 170 __in_z LPCWSTR wzTarget,
171 __in BOOL fOverwrite, 171 __in BOOL fOverwrite,
172 __in BOOL fAllowCopy 172 __in BOOL fAllowCopy
173 ); 173 );
@@ -191,9 +191,20 @@ HRESULT DAPI FileCreateTempW(
191 __deref_opt_out_z LPWSTR* ppwzTempFile, 191 __deref_opt_out_z LPWSTR* ppwzTempFile,
192 __out_opt HANDLE* phTempFile 192 __out_opt HANDLE* phTempFile
193 ); 193 );
194HRESULT DAPI FileCreateWithRetry(
195 __in LPCWSTR wzFile,
196 __in DWORD dwDesiredAccess,
197 __in DWORD dwShareMode,
198 __in_opt LPSECURITY_ATTRIBUTES lpSecurityAttributes,
199 __in DWORD dwCreationDisposition,
200 __in DWORD dwFlagsAndAttributes,
201 __in DWORD cRetry,
202 __in DWORD dwWaitMilliseconds,
203 __out HANDLE* phFile
204 );
194HRESULT DAPI FileVersion( 205HRESULT DAPI FileVersion(
195 __in_z LPCWSTR wzFilename, 206 __in_z LPCWSTR wzFilename,
196 __out DWORD *pdwVerMajor, 207 __out DWORD *pdwVerMajor,
197 __out DWORD* pdwVerMinor 208 __out DWORD* pdwVerMinor
198 ); 209 );
199HRESULT DAPI FileIsSame( 210HRESULT DAPI FileIsSame(
@@ -205,7 +216,7 @@ HRESULT DAPI FileEnsureDelete(
205 __in_z LPCWSTR wzFile 216 __in_z LPCWSTR wzFile
206 ); 217 );
207HRESULT DAPI FileGetTime( 218HRESULT DAPI FileGetTime(
208 __in_z LPCWSTR wzFile, 219 __in_z LPCWSTR wzFile,
209 __out_opt LPFILETIME lpCreationTime, 220 __out_opt LPFILETIME lpCreationTime,
210 __out_opt LPFILETIME lpLastAccessTime, 221 __out_opt LPFILETIME lpLastAccessTime,
211 __out_opt LPFILETIME lpLastWriteTime 222 __out_opt LPFILETIME lpLastWriteTime