From b941c2754748251520dc5032d11396c9844fad8e Mon Sep 17 00:00:00 2001 From: Sean Hall Date: Fri, 16 Apr 2021 10:18:24 -0500 Subject: Verify file in the cache before trying to acquire it. --- src/engine/cache.cpp | 76 +++++++++++++++++++++++++++++++++++++++++++++++++++- 1 file changed, 75 insertions(+), 1 deletion(-) (limited to 'src/engine/cache.cpp') diff --git a/src/engine/cache.cpp b/src/engine/cache.cpp index 6ddbfb50..6ac313e0 100644 --- a/src/engine/cache.cpp +++ b/src/engine/cache.cpp @@ -57,6 +57,10 @@ static HRESULT TransferWorkingPathToUnverifiedPath( __in_z LPCWSTR wzUnverifiedPayloadPath, __in BOOL fMove ); +static HRESULT VerifyFileAgainstContainer( + __in BURN_CONTAINER* pContainer, + __in_z LPCWSTR wzVerifyPath + ); static HRESULT VerifyFileAgainstPayload( __in BURN_PAYLOAD* pPayload, __in_z LPCWSTR wzVerifyPath @@ -837,7 +841,7 @@ LExit: extern "C" HRESULT CacheCompletePayload( __in BOOL fPerMachine, __in BURN_PAYLOAD* pPayload, - __in_z_opt LPCWSTR wzCacheId, + __in_z LPCWSTR wzCacheId, __in_z LPCWSTR wzWorkingPayloadPath, __in BOOL fMove ) @@ -910,6 +914,44 @@ LExit: return hr; } +extern "C" HRESULT CacheVerifyContainer( + __in BURN_CONTAINER* pContainer, + __in_z LPCWSTR wzCachedDirectory + ) +{ + HRESULT hr = S_OK; + LPWSTR sczCachedPath = NULL; + + hr = PathConcat(wzCachedDirectory, pContainer->sczFilePath, &sczCachedPath); + ExitOnFailure(hr, "Failed to concat complete cached path."); + + hr = VerifyFileAgainstContainer(pContainer, sczCachedPath); + +LExit: + ReleaseStr(sczCachedPath); + + return hr; +} + +extern "C" HRESULT CacheVerifyPayload( + __in BURN_PAYLOAD* pPayload, + __in_z LPCWSTR wzCachedDirectory + ) +{ + HRESULT hr = S_OK; + LPWSTR sczCachedPath = NULL; + + hr = PathConcat(wzCachedDirectory, pPayload->sczFilePath, &sczCachedPath); + ExitOnFailure(hr, "Failed to concat complete cached path."); + + hr = VerifyFileAgainstPayload(pPayload, sczCachedPath); + +LExit: + ReleaseStr(sczCachedPath); + + return hr; +} + extern "C" HRESULT CacheRemoveWorkingFolder( __in_z_opt LPCWSTR wzBundleId ) @@ -1383,6 +1425,38 @@ LExit: return hr; } +static HRESULT VerifyFileAgainstContainer( + __in BURN_CONTAINER* pContainer, + __in_z LPCWSTR wzVerifyPath + ) +{ + HRESULT hr = S_OK; + HANDLE hFile = INVALID_HANDLE_VALUE; + + // Get the container on disk actual hash. + hFile = ::CreateFileW(wzVerifyPath, GENERIC_READ, FILE_SHARE_READ | FILE_SHARE_DELETE, NULL, OPEN_EXISTING, FILE_FLAG_SEQUENTIAL_SCAN, NULL); + if (INVALID_HANDLE_VALUE == hFile) + { + hr = HRESULT_FROM_WIN32(::GetLastError()); + if (E_PATHNOTFOUND == hr || E_FILENOTFOUND == hr) + { + ExitFunction(); // do not log error when the file was not found. + } + ExitOnRootFailure(hr, "Failed to open container at path: %ls", wzVerifyPath); + } + + if (pContainer->pbHash) // the container should have a hash we can use to verify it. + { + hr = VerifyHash(pContainer->pbHash, pContainer->cbHash, pContainer->qwFileSize, wzVerifyPath, hFile); + ExitOnFailure(hr, "Failed to verify hash of container: %ls", pContainer->sczId); + } + +LExit: + ReleaseFileHandle(hFile); + + return hr; +} + static HRESULT VerifyFileAgainstPayload( __in BURN_PAYLOAD* pPayload, __in_z LPCWSTR wzVerifyPath -- cgit v1.2.3-55-g6feb