From 240b663ad5fc94ed6d19c966b5c9105a176ecf40 Mon Sep 17 00:00:00 2001 From: Sean Hall Date: Mon, 8 Aug 2022 18:02:15 -0500 Subject: Skip logging errors in some places when they are due to missing files or registry keys or values. Related to 6696 --- src/libs/dutil/WixToolset.DUtil/butil.cpp | 127 ++++++++-------------- src/libs/dutil/WixToolset.DUtil/deputil.cpp | 145 +++++++++++++------------- src/libs/dutil/WixToolset.DUtil/dirutil.cpp | 42 +++++--- src/libs/dutil/WixToolset.DUtil/file2utl.cpp | 31 +++--- src/libs/dutil/WixToolset.DUtil/fileutil.cpp | 12 ++- src/libs/dutil/WixToolset.DUtil/inc/dutil.h | 4 + src/libs/dutil/WixToolset.DUtil/inc/logutil.h | 2 + src/libs/dutil/WixToolset.DUtil/monutil.cpp | 14 +-- src/libs/dutil/WixToolset.DUtil/osutil.cpp | 16 +-- src/libs/dutil/WixToolset.DUtil/path3utl.cpp | 14 ++- src/libs/dutil/WixToolset.DUtil/polcutil.cpp | 48 ++++++--- src/libs/dutil/WixToolset.DUtil/regutil.cpp | 49 ++++++--- src/libs/dutil/WixToolset.DUtil/wiutil.cpp | 15 ++- 13 files changed, 286 insertions(+), 233 deletions(-) (limited to 'src/libs') diff --git a/src/libs/dutil/WixToolset.DUtil/butil.cpp b/src/libs/dutil/WixToolset.DUtil/butil.cpp index 175903ad..47f206ea 100644 --- a/src/libs/dutil/WixToolset.DUtil/butil.cpp +++ b/src/libs/dutil/WixToolset.DUtil/butil.cpp @@ -15,6 +15,7 @@ #define ButilExitOnNullDebugTrace(p, x, e, s, ...) ExitOnNullDebugTraceSource(DUTIL_SOURCE_BUTIL, p, x, e, s, __VA_ARGS__) #define ButilExitOnInvalidHandleWithLastError(p, x, s, ...) ExitOnInvalidHandleWithLastErrorSource(DUTIL_SOURCE_BUTIL, p, x, s, __VA_ARGS__) #define ButilExitOnWin32Error(e, x, s, ...) ExitOnWin32ErrorSource(DUTIL_SOURCE_BUTIL, e, x, s, __VA_ARGS__) +#define ButilExitOnPathFailure(x, b, s, ...) ExitOnPathFailureSource(DUTIL_SOURCE_BUTIL, x, b, s, __VA_ARGS__) // constants // From engine/registration.h @@ -82,19 +83,6 @@ static HRESULT LocateAndQueryBundleValue( __inout DWORD* pdwType, __out INTERNAL_BUNDLE_STATUS* pStatus ); - -/******************************************************************** -OpenBundleKey - Opens the bundle uninstallation key for a given bundle - -NOTE: caller is responsible for closing key -********************************************************************/ -static HRESULT OpenBundleKey( - __in_z LPCWSTR wzBundleId, - __in BUNDLE_INSTALL_CONTEXT context, - __in_opt LPCWSTR wzSubKey, - __in REG_KEY_BITNESS kbKeyBitness, - __inout HKEY* phKey - ); static HRESULT CopyStringToBuffer( __in_z LPWSTR wzValue, __in_z_opt LPWSTR wzBuffer, @@ -389,12 +377,12 @@ DAPI_(HRESULT) BundleQueryRelatedBundles( queryContext.regBitness = REG_KEY_32BIT; hr = QueryRelatedBundlesForScopeAndBitness(&queryContext); - ExitOnFailure(hr, "Failed to query 32-bit related bundles."); + ButilExitOnFailure(hr, "Failed to query 32-bit related bundles."); queryContext.regBitness = REG_KEY_64BIT; hr = QueryRelatedBundlesForScopeAndBitness(&queryContext); - ExitOnFailure(hr, "Failed to query 64-bit related bundles."); + ButilExitOnFailure(hr, "Failed to query 64-bit related bundles."); LExit: return hr; @@ -407,15 +395,17 @@ static HRESULT QueryRelatedBundlesForScopeAndBitness( HRESULT hr = S_OK; HKEY hkRoot = BUNDLE_INSTALL_CONTEXT_USER == pQueryContext->installContext ? HKEY_CURRENT_USER : HKEY_LOCAL_MACHINE; HKEY hkUninstallKey = NULL; + BOOL fExists = FALSE; LPWSTR sczRelatedBundleId = NULL; BUNDLE_QUERY_CALLBACK_RESULT result = BUNDLE_QUERY_CALLBACK_RESULT_CONTINUE; hr = RegOpenEx(hkRoot, BUNDLE_REGISTRATION_REGISTRY_UNINSTALL_KEY, KEY_READ, pQueryContext->regBitness, &hkUninstallKey); - if (HRESULT_FROM_WIN32(ERROR_PATH_NOT_FOUND) == hr || HRESULT_FROM_WIN32(ERROR_FILE_NOT_FOUND) == hr) + ButilExitOnPathFailure(hr, fExists, "Failed to open uninstall registry key."); + + if (!fExists) { ExitFunction1(hr = S_OK); } - ExitOnFailure(hr, "Failed to open uninstall registry key."); for (DWORD dwIndex = 0; /* exit via break below */; ++dwIndex) { @@ -425,7 +415,7 @@ static HRESULT QueryRelatedBundlesForScopeAndBitness( hr = S_OK; break; } - ExitOnFailure(hr, "Failed to enumerate uninstall key for related bundles."); + ButilExitOnFailure(hr, "Failed to enumerate uninstall key for related bundles."); // Ignore failures here since we'll often find products that aren't actually // related bundles (or even bundles at all). @@ -456,7 +446,7 @@ static HRESULT QueryPotentialRelatedBundle( BUNDLE_QUERY_RELATED_BUNDLE_RESULT bundle = { }; hr = RegOpenEx(hkUninstallKey, wzRelatedBundleId, KEY_READ, pQueryContext->regBitness, &hkBundleId); - ExitOnFailure(hr, "Failed to open uninstall key for potential related bundle: %ls", wzRelatedBundleId); + ButilExitOnFailure(hr, "Failed to open uninstall key for potential related bundle: %ls", wzRelatedBundleId); hr = DetermineRelationType(pQueryContext, hkBundleId, &relationType); if (FAILED(hr)) @@ -506,7 +496,7 @@ static HRESULT DetermineRelationType( TraceError(hr, "Failed to read upgrade codes as REG_MULTI_SZ. Trying again as REG_SZ in case of older bundles."); rgsczUpgradeCodes = reinterpret_cast(MemAlloc(sizeof(LPWSTR), TRUE)); - ExitOnNull(rgsczUpgradeCodes, hr, E_OUTOFMEMORY, "Failed to allocate list for a single upgrade code from older bundle."); + ButilExitOnNull(rgsczUpgradeCodes, hr, E_OUTOFMEMORY, "Failed to allocate list for a single upgrade code from older bundle."); hr = RegReadString(hkBundleId, BUNDLE_REGISTRATION_REGISTRY_BUNDLE_UPGRADE_CODE, &rgsczUpgradeCodes[0]); if (SUCCEEDED(hr)) @@ -519,7 +509,7 @@ static HRESULT DetermineRelationType( if (SUCCEEDED(hr)) { hr = DictCreateStringListFromArray(&sdUpgradeCodes, rgsczUpgradeCodes, cUpgradeCodes, DICT_FLAG_CASEINSENSITIVE); - ExitOnFailure(hr, "Failed to create string dictionary for %hs.", "upgrade codes"); + ButilExitOnFailure(hr, "Failed to create string dictionary for %hs.", "upgrade codes"); // Upgrade relationship: when their upgrade codes match our upgrade codes. hr = DictCompareStringListToArray(sdUpgradeCodes, const_cast(pQueryContext->rgwzUpgradeCodes), pQueryContext->cUpgradeCodes); @@ -529,7 +519,7 @@ static HRESULT DetermineRelationType( } else { - ExitOnFailure(hr, "Failed to do array search for upgrade code match."); + ButilExitOnFailure(hr, "Failed to do array search for upgrade code match."); *pRelationType = BUNDLE_RELATION_UPGRADE; ExitFunction(); @@ -543,7 +533,7 @@ static HRESULT DetermineRelationType( } else { - ExitOnFailure(hr, "Failed to do array search for detect code match."); + ButilExitOnFailure(hr, "Failed to do array search for detect code match."); *pRelationType = BUNDLE_RELATION_DETECT; ExitFunction(); @@ -557,7 +547,7 @@ static HRESULT DetermineRelationType( } else { - ExitOnFailure(hr, "Failed to do array search for addon code match."); + ButilExitOnFailure(hr, "Failed to do array search for addon code match."); *pRelationType = BUNDLE_RELATION_DEPENDENT_ADDON; ExitFunction(); @@ -571,7 +561,7 @@ static HRESULT DetermineRelationType( } else { - ExitOnFailure(hr, "Failed to do array search for patch code match."); + ButilExitOnFailure(hr, "Failed to do array search for patch code match."); *pRelationType = BUNDLE_RELATION_DEPENDENT_PATCH; ExitFunction(); @@ -586,7 +576,7 @@ static HRESULT DetermineRelationType( if (SUCCEEDED(hr)) { hr = DictCreateStringListFromArray(&sdAddonCodes, rgsczAddonCodes, cAddonCodes, DICT_FLAG_CASEINSENSITIVE); - ExitOnFailure(hr, "Failed to create string dictionary for %hs.", "addon codes"); + ButilExitOnFailure(hr, "Failed to create string dictionary for %hs.", "addon codes"); // Addon relationship: when their addon codes match our detect codes. hr = DictCompareStringListToArray(sdAddonCodes, const_cast(pQueryContext->rgwzDetectCodes), pQueryContext->cDetectCodes); @@ -596,7 +586,7 @@ static HRESULT DetermineRelationType( } else { - ExitOnFailure(hr, "Failed to do array search for addon code match."); + ButilExitOnFailure(hr, "Failed to do array search for addon code match."); *pRelationType = BUNDLE_RELATION_ADDON; ExitFunction(); @@ -610,7 +600,7 @@ static HRESULT DetermineRelationType( } else { - ExitOnFailure(hr, "Failed to do array search for addon code match."); + ButilExitOnFailure(hr, "Failed to do array search for addon code match."); *pRelationType = BUNDLE_RELATION_ADDON; ExitFunction(); @@ -625,7 +615,7 @@ static HRESULT DetermineRelationType( if (SUCCEEDED(hr)) { hr = DictCreateStringListFromArray(&sdPatchCodes, rgsczPatchCodes, cPatchCodes, DICT_FLAG_CASEINSENSITIVE); - ExitOnFailure(hr, "Failed to create string dictionary for %hs.", "patch codes"); + ButilExitOnFailure(hr, "Failed to create string dictionary for %hs.", "patch codes"); // Patch relationship: when their patch codes match our detect codes. hr = DictCompareStringListToArray(sdPatchCodes, const_cast(pQueryContext->rgwzDetectCodes), pQueryContext->cDetectCodes); @@ -635,7 +625,7 @@ static HRESULT DetermineRelationType( } else { - ExitOnFailure(hr, "Failed to do array search for patch code match."); + ButilExitOnFailure(hr, "Failed to do array search for patch code match."); *pRelationType = BUNDLE_RELATION_PATCH; ExitFunction(); @@ -649,7 +639,7 @@ static HRESULT DetermineRelationType( } else { - ExitOnFailure(hr, "Failed to do array search for patch code match."); + ButilExitOnFailure(hr, "Failed to do array search for patch code match."); *pRelationType = BUNDLE_RELATION_PATCH; ExitFunction(); @@ -664,7 +654,7 @@ static HRESULT DetermineRelationType( if (SUCCEEDED(hr)) { hr = DictCreateStringListFromArray(&sdDetectCodes, rgsczDetectCodes, cDetectCodes, DICT_FLAG_CASEINSENSITIVE); - ExitOnFailure(hr, "Failed to create string dictionary for %hs.", "detect codes"); + ButilExitOnFailure(hr, "Failed to create string dictionary for %hs.", "detect codes"); // Detect relationship: when their detect codes match our detect codes. hr = DictCompareStringListToArray(sdDetectCodes, const_cast(pQueryContext->rgwzDetectCodes), pQueryContext->cDetectCodes); @@ -674,7 +664,7 @@ static HRESULT DetermineRelationType( } else { - ExitOnFailure(hr, "Failed to do array search for detect code match."); + ButilExitOnFailure(hr, "Failed to do array search for detect code match."); *pRelationType = BUNDLE_RELATION_DETECT; ExitFunction(); @@ -688,7 +678,7 @@ static HRESULT DetermineRelationType( } else { - ExitOnFailure(hr, "Failed to do array search for addon code match."); + ButilExitOnFailure(hr, "Failed to do array search for addon code match."); *pRelationType = BUNDLE_RELATION_DEPENDENT_ADDON; ExitFunction(); @@ -702,7 +692,7 @@ static HRESULT DetermineRelationType( } else { - ExitOnFailure(hr, "Failed to do array search for patch code match."); + ButilExitOnFailure(hr, "Failed to do array search for patch code match."); *pRelationType = BUNDLE_RELATION_DEPENDENT_PATCH; ExitFunction(); @@ -740,54 +730,11 @@ static HRESULT LocateAndQueryBundleValue( ) { HRESULT hr = S_OK; + LPWSTR sczKeypath = NULL; + BOOL fExists = TRUE; *pStatus = INTERNAL_BUNDLE_STATUS_SUCCESS; - if (FAILED(hr = OpenBundleKey(wzBundleId, BUNDLE_INSTALL_CONTEXT_MACHINE, wzSubKey, REG_KEY_32BIT, phKey)) && - FAILED(hr = OpenBundleKey(wzBundleId, BUNDLE_INSTALL_CONTEXT_MACHINE, wzSubKey, REG_KEY_64BIT, phKey)) && - FAILED(hr = OpenBundleKey(wzBundleId, BUNDLE_INSTALL_CONTEXT_USER, wzSubKey, REG_KEY_DEFAULT, phKey))) - { - if (E_FILENOTFOUND == hr) - { - *pStatus = INTERNAL_BUNDLE_STATUS_UNKNOWN_BUNDLE; - ExitFunction1(hr = S_OK); - } - - ButilExitOnFailure(hr, "Failed to open bundle key."); - } - - // If the bundle doesn't have the value defined, return ERROR_UNKNOWN_PROPERTY - hr = RegGetType(*phKey, wzValueName, pdwType); - if (FAILED(hr)) - { - if (E_FILENOTFOUND == hr) - { - *pStatus = INTERNAL_BUNDLE_STATUS_UNKNOWN_PROPERTY; - ExitFunction1(hr = S_OK); - } - - ButilExitOnFailure(hr, "Failed to read bundle value."); - } - -LExit: - return hr; -} - -static HRESULT OpenBundleKey( - __in_z LPCWSTR wzBundleId, - __in BUNDLE_INSTALL_CONTEXT context, - __in_opt LPCWSTR wzSubKey, - __in REG_KEY_BITNESS kbKeyBitness, - __inout HKEY* phKey - ) -{ - Assert(phKey && wzBundleId); - AssertSz(NULL == *phKey, "*key should be null"); - - HRESULT hr = S_OK; - HKEY hkRoot = BUNDLE_INSTALL_CONTEXT_USER == context ? HKEY_CURRENT_USER : HKEY_LOCAL_MACHINE; - LPWSTR sczKeypath = NULL; - if (wzSubKey) { hr = StrAllocFormatted(&sczKeypath, L"%ls\\%ls\\%ls", BUNDLE_REGISTRATION_REGISTRY_UNINSTALL_KEY, wzBundleId, wzSubKey); @@ -798,8 +745,24 @@ static HRESULT OpenBundleKey( } ButilExitOnFailure(hr, "Failed to allocate bundle uninstall key path."); - hr = RegOpenEx(hkRoot, sczKeypath, KEY_READ, kbKeyBitness, phKey); - ButilExitOnFailure(hr, "Failed to open bundle uninstall key path."); + if (FAILED(hr = RegOpenEx(HKEY_LOCAL_MACHINE, sczKeypath, KEY_READ, REG_KEY_32BIT, phKey)) && + FAILED(hr = RegOpenEx(HKEY_LOCAL_MACHINE, sczKeypath, KEY_READ, REG_KEY_64BIT, phKey)) && + FAILED(hr = RegOpenEx(HKEY_CURRENT_USER, sczKeypath, KEY_READ, REG_KEY_DEFAULT, phKey))) + { + ButilExitOnPathFailure(hr, fExists, "Failed to open bundle key."); + + *pStatus = INTERNAL_BUNDLE_STATUS_UNKNOWN_BUNDLE; + ExitFunction1(hr = S_OK); + } + + hr = RegGetType(*phKey, wzValueName, pdwType); + ButilExitOnPathFailure(hr, fExists, "Failed to read bundle value."); + + if (!fExists) + { + *pStatus = INTERNAL_BUNDLE_STATUS_UNKNOWN_PROPERTY; + ExitFunction1(hr = S_OK); + } LExit: ReleaseStr(sczKeypath); diff --git a/src/libs/dutil/WixToolset.DUtil/deputil.cpp b/src/libs/dutil/WixToolset.DUtil/deputil.cpp index 4de85199..f92c7d18 100644 --- a/src/libs/dutil/WixToolset.DUtil/deputil.cpp +++ b/src/libs/dutil/WixToolset.DUtil/deputil.cpp @@ -16,6 +16,7 @@ #define DepExitOnInvalidHandleWithLastError(p, x, s, ...) ExitOnInvalidHandleWithLastErrorSource(DUTIL_SOURCE_DEPUTIL, p, x, s, __VA_ARGS__) #define DepExitOnWin32Error(e, x, s, ...) ExitOnWin32ErrorSource(DUTIL_SOURCE_DEPUTIL, e, x, s, __VA_ARGS__) #define DepExitOnGdipFailure(g, x, s, ...) ExitOnGdipFailureSource(DUTIL_SOURCE_DEPUTIL, g, x, s, __VA_ARGS__) +#define DepExitOnPathFailure(x, b, s, ...) ExitOnPathFailureSource(DUTIL_SOURCE_DEPUTIL, x, b, s, __VA_ARGS__) #define ARRAY_GROWTH_SIZE 5 @@ -54,6 +55,7 @@ DAPI_(HRESULT) DepGetProviderInformation( HRESULT hr = S_OK; LPWSTR sczKey = NULL; HKEY hkKey = NULL; + BOOL fExists = FALSE; // Format the provider dependency registry key. hr = AllocDependencyKeyName(wzProviderKey, &sczKey); @@ -61,43 +63,32 @@ DAPI_(HRESULT) DepGetProviderInformation( // Try to open the dependency key. hr = RegOpen(hkHive, sczKey, KEY_READ, &hkKey); - if (E_FILENOTFOUND == hr) + DepExitOnPathFailure(hr, fExists, "Failed to open the registry key for the dependency \"%ls\".", wzProviderKey); + + if (!fExists) { ExitFunction1(hr = E_NOTFOUND); } - DepExitOnFailure(hr, "Failed to open the registry key for the dependency \"%ls\".", wzProviderKey); // Get the Id if requested and available. if (psczId) { hr = RegReadString(hkKey, NULL, psczId); - if (E_FILENOTFOUND == hr) - { - hr = S_OK; - } - DepExitOnFailure(hr, "Failed to get the id for the dependency \"%ls\".", wzProviderKey); + DepExitOnPathFailure(hr, fExists, "Failed to get the id for the dependency \"%ls\".", wzProviderKey); } // Get the DisplayName if requested and available. if (psczName) { hr = RegReadString(hkKey, vcszDisplayNameValue, psczName); - if (E_FILENOTFOUND == hr) - { - hr = S_OK; - } - DepExitOnFailure(hr, "Failed to get the name for the dependency \"%ls\".", wzProviderKey); + DepExitOnPathFailure(hr, fExists, "Failed to get the name for the dependency \"%ls\".", wzProviderKey); } // Get the Version if requested and available. if (psczVersion) { hr = RegReadString(hkKey, vcszVersionValue, psczVersion); - if (E_FILENOTFOUND == hr) - { - hr = S_OK; - } - DepExitOnFailure(hr, "Failed to get the version for the dependency \"%ls\".", wzProviderKey); + DepExitOnPathFailure(hr, fExists, "Failed to get the version for the dependency \"%ls\".", wzProviderKey); } LExit: @@ -121,6 +112,7 @@ DAPI_(HRESULT) DepCheckDependency( HRESULT hr = S_OK; LPWSTR sczKey = NULL; HKEY hkKey = NULL; + BOOL fExists = FALSE; VERUTIL_VERSION* pVersion = NULL; VERUTIL_VERSION* pMinVersion = NULL; VERUTIL_VERSION* pMaxVersion = NULL; @@ -134,20 +126,17 @@ DAPI_(HRESULT) DepCheckDependency( // Try to open the key. If that fails, add the missing dependency key to the dependency array if it doesn't already exist. hr = RegOpen(hkHive, sczKey, KEY_READ, &hkKey); - if (E_FILENOTFOUND != hr) - { - DepExitOnFailure(hr, "Failed to open the registry key for dependency \"%ls\".", wzProviderKey); + DepExitOnPathFailure(hr, fExists, "Failed to open the registry key for dependency \"%ls\".", wzProviderKey); + if (fExists) + { // If there are no registry values, consider the key orphaned and treat it as missing. hr = RegReadWixVersion(hkKey, vcszVersionValue, &pVersion); - if (E_FILENOTFOUND != hr) - { - DepExitOnFailure(hr, "Failed to read the %ls registry value for dependency \"%ls\".", vcszVersionValue, wzProviderKey); - } + DepExitOnPathFailure(hr, fExists, "Failed to read the %ls registry value for dependency \"%ls\".", vcszVersionValue, wzProviderKey); } // If the key was not found or the Version value was not found, add the missing dependency key to the dependency array. - if (E_FILENOTFOUND == hr) + if (!fExists) { hr = DictKeyExists(sdDependencies, wzProviderKey); if (E_NOTFOUND != hr) @@ -190,7 +179,7 @@ DAPI_(HRESULT) DepCheckDependency( else { hr = RegReadString(hkKey, vcszDisplayNameValue, &sczName); - DepExitOnFailure(hr, "Failed to get the display name of the older dependency \"%ls\".", wzProviderKey); + DepExitOnPathFailure(hr, fExists, "Failed to get the display name of the older dependency \"%ls\".", wzProviderKey); hr = DepDependencyArrayAlloc(prgDependencies, pcDependencies, wzProviderKey, sczName); DepExitOnFailure(hr, "Failed to add the older dependency \"%ls\" to the dependencies array.", wzProviderKey); @@ -228,7 +217,7 @@ DAPI_(HRESULT) DepCheckDependency( else { hr = RegReadString(hkKey, vcszDisplayNameValue, &sczName); - DepExitOnFailure(hr, "Failed to get the display name of the newer dependency \"%ls\".", wzProviderKey); + DepExitOnPathFailure(hr, fExists, "Failed to get the display name of the newer dependency \"%ls\".", wzProviderKey); hr = DepDependencyArrayAlloc(prgDependencies, pcDependencies, wzProviderKey, sczName); DepExitOnFailure(hr, "Failed to add the newer dependency \"%ls\" to the dependencies array.", wzProviderKey); @@ -267,6 +256,7 @@ DAPI_(HRESULT) DepCheckDependents( LPWSTR sczKey = NULL; HKEY hkProviderKey = NULL; HKEY hkDependentsKey = NULL; + BOOL fExists = FALSE; LPWSTR sczDependentKey = NULL; LPWSTR sczDependentName = NULL; BOOL fIgnore = FALSE; @@ -277,15 +267,18 @@ DAPI_(HRESULT) DepCheckDependents( // Try to open the key. If that fails, the dependency information is corrupt. hr = RegOpen(hkHive, sczKey, KEY_READ, &hkProviderKey); - DepExitOnFailure(hr, "Failed to open the registry key \"%ls\". The dependency store is corrupt.", sczKey); + DepExitOnPathFailure(hr, fExists, "Failed to open the registry key \"%ls\". The dependency store is corrupt.", sczKey); - // Try to open the dependencies key. If that does not exist, there are no dependents. - hr = RegOpen(hkProviderKey, vsczRegistryDependents, KEY_READ, &hkDependentsKey); - if (E_FILENOTFOUND != hr) + if (!fExists) { - DepExitOnFailure(hr, "Failed to open the registry key for dependents of \"%ls\".", wzProviderKey); + ExitFunction1(hr = S_OK); } - else + + // Try to open the dependencies key. If that does not exist, there are no dependents. + hr = RegOpen(hkProviderKey, vsczRegistryDependents, KEY_READ, &hkDependentsKey); + DepExitOnPathFailure(hr, fExists, "Failed to open the registry key for dependents of \"%ls\".", wzProviderKey); + + if (!fExists) { ExitFunction1(hr = S_OK); } @@ -399,15 +392,18 @@ DAPI_(HRESULT) DepDependentExists( HRESULT hr = S_OK; LPWSTR sczDependentKey = NULL; HKEY hkDependentKey = NULL; + BOOL fExists = FALSE; // Format the provider dependents registry key. hr = StrAllocFormatted(&sczDependentKey, L"%ls%ls\\%ls\\%ls", vsczRegistryRoot, wzDependencyProviderKey, vsczRegistryDependents, wzProviderKey); DepExitOnFailure(hr, "Failed to format registry key to dependent."); hr = RegOpen(hkHive, sczDependentKey, KEY_READ, &hkDependentKey); - if (E_FILENOTFOUND != hr) + DepExitOnPathFailure(hr, fExists, "Failed to open the dependent registry key at: \"%ls\".", sczDependentKey); + + if (!fExists) { - DepExitOnFailure(hr, "Failed to open the dependent registry key at: \"%ls\".", sczDependentKey); + hr = E_FILENOTFOUND; } LExit: @@ -480,6 +476,7 @@ DAPI_(HRESULT) DepUnregisterDependency( HRESULT hr = S_OK; LPWSTR sczKey = NULL; HKEY hkKey = NULL; + BOOL fExists = FALSE; // Format the provider dependency registry key. hr = AllocDependencyKeyName(wzProviderKey, &sczKey); @@ -487,9 +484,11 @@ DAPI_(HRESULT) DepUnregisterDependency( // Delete the entire key including all sub-keys. hr = RegDelete(hkHive, sczKey, REG_KEY_DEFAULT, TRUE); - if (E_FILENOTFOUND != hr) + DepExitOnPathFailure(hr, fExists, "Failed to delete the key \"%ls\".", sczKey); + + if (!fExists) { - DepExitOnFailure(hr, "Failed to delete the key \"%ls\".", sczKey); + hr = E_FILENOTFOUND; } LExit: @@ -506,6 +505,7 @@ DAPI_(HRESULT) DepUnregisterDependent( ) { HRESULT hr = S_OK; + BOOL fExists = FALSE; HKEY hkRegistryRoot = NULL; HKEY hkDependencyProviderKey = NULL; HKEY hkRegistryDependents = NULL; @@ -514,40 +514,39 @@ DAPI_(HRESULT) DepUnregisterDependent( // Open the root key. We may delete the wzDependencyProviderKey during clean up. hr = RegOpen(hkHive, vsczRegistryRoot, KEY_READ, &hkRegistryRoot); - if (E_FILENOTFOUND != hr) - { - DepExitOnFailure(hr, "Failed to open root registry key \"%ls\".", vsczRegistryRoot); - } - else + DepExitOnPathFailure(hr, fExists, "Failed to open root registry key \"%ls\".", vsczRegistryRoot); + + if (!fExists) { - ExitFunction(); + ExitFunction1(hr = E_FILENOTFOUND); } // Try to open the dependency key. If that does not exist, simply return. hr = RegOpen(hkRegistryRoot, wzDependencyProviderKey, KEY_READ, &hkDependencyProviderKey); - if (E_FILENOTFOUND != hr) - { - DepExitOnFailure(hr, "Failed to open the registry key for the dependency \"%ls\".", wzDependencyProviderKey); - } - else + DepExitOnPathFailure(hr, fExists, "Failed to open the registry key for the dependency \"%ls\".", wzDependencyProviderKey); + + if (!fExists) { - ExitFunction(); + ExitFunction1(hr = E_FILENOTFOUND); } // Try to open the dependents subkey to enumerate. hr = RegOpen(hkDependencyProviderKey, vsczRegistryDependents, KEY_READ, &hkRegistryDependents); - if (E_FILENOTFOUND != hr) - { - DepExitOnFailure(hr, "Failed to open the dependents subkey under the dependency \"%ls\".", wzDependencyProviderKey); - } - else + DepExitOnPathFailure(hr, fExists, "Failed to open the dependents subkey under the dependency \"%ls\".", wzDependencyProviderKey); + + if (!fExists) { - ExitFunction(); + ExitFunction1(hr = E_FILENOTFOUND); } // Delete the wzProviderKey dependent sub-key. hr = RegDelete(hkRegistryDependents, wzProviderKey, REG_KEY_DEFAULT, TRUE); - DepExitOnFailure(hr, "Failed to delete the dependent \"%ls\" under the dependency \"%ls\".", wzProviderKey, wzDependencyProviderKey); + DepExitOnPathFailure(hr, fExists, "Failed to delete the dependent \"%ls\" under the dependency \"%ls\".", wzProviderKey, wzDependencyProviderKey); + + if (!fExists) + { + ExitFunction1(hr = E_FILENOTFOUND); + } // If there are no remaining dependents, delete the Dependents subkey. hr = RegQueryKey(hkRegistryDependents, &cSubKeys, NULL); @@ -563,7 +562,12 @@ DAPI_(HRESULT) DepUnregisterDependent( // Fail if there are any subkeys since we just checked. hr = RegDelete(hkDependencyProviderKey, vsczRegistryDependents, REG_KEY_DEFAULT, FALSE); - DepExitOnFailure(hr, "Failed to delete the dependents subkey under the dependency \"%ls\".", wzDependencyProviderKey); + DepExitOnPathFailure(hr, fExists, "Failed to delete the dependents subkey under the dependency \"%ls\".", wzDependencyProviderKey); + + if (!fExists) + { + ExitFunction1(hr = E_FILENOTFOUND); + } // If there are no values, delete the provider dependency key. hr = RegQueryKey(hkDependencyProviderKey, NULL, &cValues); @@ -576,7 +580,12 @@ DAPI_(HRESULT) DepUnregisterDependent( // Fail if there are any subkeys since we just checked. hr = RegDelete(hkRegistryRoot, wzDependencyProviderKey, REG_KEY_DEFAULT, FALSE); - DepExitOnFailure(hr, "Failed to delete the dependency \"%ls\".", wzDependencyProviderKey); + DepExitOnPathFailure(hr, fExists, "Failed to delete the dependency \"%ls\".", wzDependencyProviderKey); + + if (!fExists) + { + ExitFunction1(hr = E_FILENOTFOUND); + } } LExit: @@ -685,6 +694,7 @@ static HRESULT GetDependencyNameFromKey( HRESULT hr = S_OK; LPWSTR sczKey = NULL; HKEY hkKey = NULL; + BOOL fExists = FALSE; // Format the provider dependency registry key. hr = AllocDependencyKeyName(wzProviderKey, &sczKey); @@ -692,25 +702,16 @@ static HRESULT GetDependencyNameFromKey( // Try to open the dependency key. hr = RegOpen(hkHive, sczKey, KEY_READ, &hkKey); - if (E_FILENOTFOUND != hr) - { - DepExitOnFailure(hr, "Failed to open the registry key for the dependency \"%ls\".", wzProviderKey); - } - else + DepExitOnPathFailure(hr, fExists, "Failed to open the registry key for the dependency \"%ls\".", wzProviderKey); + + if (!fExists) { - ExitFunction1(hr = S_OK); + ExitFunction(); } // Get the DisplayName if available. hr = RegReadString(hkKey, vcszDisplayNameValue, psczName); - if (E_FILENOTFOUND != hr) - { - DepExitOnFailure(hr, "Failed to get the dependency name for the dependency \"%ls\".", wzProviderKey); - } - else - { - ExitFunction1(hr = S_OK); - } + DepExitOnPathFailure(hr, fExists, "Failed to get the dependency name for the dependency \"%ls\".", wzProviderKey); LExit: ReleaseRegKey(hkKey); diff --git a/src/libs/dutil/WixToolset.DUtil/dirutil.cpp b/src/libs/dutil/WixToolset.DUtil/dirutil.cpp index 2c02225d..8c7ef461 100644 --- a/src/libs/dutil/WixToolset.DUtil/dirutil.cpp +++ b/src/libs/dutil/WixToolset.DUtil/dirutil.cpp @@ -17,6 +17,8 @@ #define DirExitOnInvalidHandleWithLastError(p, x, s, ...) ExitOnInvalidHandleWithLastErrorSource(DUTIL_SOURCE_DIRUTIL, p, x, s, __VA_ARGS__) #define DirExitOnWin32Error(e, x, s, ...) ExitOnWin32ErrorSource(DUTIL_SOURCE_DIRUTIL, e, x, s, __VA_ARGS__) #define DirExitOnGdipFailure(g, x, s, ...) ExitOnGdipFailureSource(DUTIL_SOURCE_DIRUTIL, g, x, s, __VA_ARGS__) +#define DirExitOnPathFailure(x, b, s, ...) ExitOnPathFailureSource(DUTIL_SOURCE_DIRUTIL, x, b, s, __VA_ARGS__) +#define DirExitWithPathLastError(x, s, ...) ExitWithPathLastErrorSource(DUTIL_SOURCE_DIRUTIL, x, s, __VA_ARGS__) /******************************************************************* @@ -184,13 +186,9 @@ extern "C" HRESULT DAPI DirEnsureDeleteEx( if (-1 == (dwAttrib = ::GetFileAttributesW(wzPath))) { - er = ::GetLastError(); - if (ERROR_FILE_NOT_FOUND == er) // change "file not found" to "path not found" since we were looking for a directory. - { - er = ERROR_PATH_NOT_FOUND; - } - hr = HRESULT_FROM_WIN32(er); - DirExitOnRootFailure(hr, "Failed to get attributes for path: %ls", wzPath); + DirExitWithPathLastError(hr, "Failed to get attributes for path: %ls", wzPath); + + ExitFunction1(hr = E_PATHNOTFOUND); } if (dwAttrib & FILE_ATTRIBUTE_DIRECTORY) @@ -199,7 +197,9 @@ extern "C" HRESULT DAPI DirEnsureDeleteEx( { if (!::SetFileAttributesW(wzPath, FILE_ATTRIBUTE_NORMAL)) { - DirExitWithLastError(hr, "Failed to remove read-only attribute from path: %ls", wzPath); + DirExitWithPathLastError(hr, "Failed to remove read-only attribute from path: %ls", wzPath); + + ExitFunction1(hr = E_PATHNOTFOUND); } } @@ -245,9 +245,12 @@ extern "C" HRESULT DAPI DirEnsureDeleteEx( hr = DirEnsureDeleteEx(sczDelete, dwFlags); // recursive call if (FAILED(hr)) { - // if we failed to delete a subdirectory, keep trying to finish any remaining files - ExitTraceSource(DUTIL_SOURCE_DIRUTIL, hr, "Failed to delete subdirectory; continuing: %ls", sczDelete); - hr = S_OK; + // if we failed to delete a subdirectory, keep trying to finish any remaining files + if (E_PATHNOTFOUND != hr) + { + ExitTraceSource(DUTIL_SOURCE_DIRUTIL, hr, "Failed to delete subdirectory; continuing: %ls", sczDelete); + } + hr = S_OK; } } else if (fDeleteFiles) // this is a file, just delete it @@ -256,7 +259,8 @@ extern "C" HRESULT DAPI DirEnsureDeleteEx( { if (!::SetFileAttributesW(sczDelete, FILE_ATTRIBUTE_NORMAL)) { - DirExitWithLastError(hr, "Failed to remove attributes from file: %ls", sczDelete); + DirExitWithPathLastError(hr, "Failed to remove attributes from file: %ls", sczDelete); + continue; } } @@ -280,7 +284,7 @@ extern "C" HRESULT DAPI DirEnsureDeleteEx( } else { - DirExitWithLastError(hr, "Failed to delete file: %ls", sczDelete); + DirExitWithPathLastError(hr, "Failed to delete file: %ls", sczDelete); } } } @@ -308,13 +312,21 @@ extern "C" HRESULT DAPI DirEnsureDeleteEx( } } + if (E_PATHNOTFOUND == hr || E_FILENOTFOUND == hr) + { + ExitFunction1(hr = E_PATHNOTFOUND); + } + else if (HRESULT_FROM_WIN32(ERROR_DIR_NOT_EMPTY) == hr && !fDeleteFiles && !fRecurse) + { + ExitFunction(); + } + DirExitOnRootFailure(hr, "Failed to remove directory: %ls", wzPath); } } else { - hr = E_UNEXPECTED; - DirExitOnFailure(hr, "Directory delete cannot delete file: %ls", wzPath); + DirExitWithRootFailure(hr, E_UNEXPECTED, "Directory delete cannot delete file: %ls", wzPath); } Assert(S_OK == hr); diff --git a/src/libs/dutil/WixToolset.DUtil/file2utl.cpp b/src/libs/dutil/WixToolset.DUtil/file2utl.cpp index 88f8377c..3ab47f59 100644 --- a/src/libs/dutil/WixToolset.DUtil/file2utl.cpp +++ b/src/libs/dutil/WixToolset.DUtil/file2utl.cpp @@ -16,6 +16,7 @@ #define FileExitOnInvalidHandleWithLastError(p, x, s, ...) ExitOnInvalidHandleWithLastErrorSource(DUTIL_SOURCE_FILEUTIL, p, x, s, __VA_ARGS__) #define FileExitOnWin32Error(e, x, s, ...) ExitOnWin32ErrorSource(DUTIL_SOURCE_FILEUTIL, e, x, s, __VA_ARGS__) #define FileExitOnGdipFailure(g, x, s, ...) ExitOnGdipFailureSource(DUTIL_SOURCE_FILEUTIL, g, x, s, __VA_ARGS__) +#define FileExitOnPathFailure(x, b, s, ...) ExitOnPathFailureSource(DUTIL_SOURCE_FILEUTIL, x, b, s, __VA_ARGS__) // constants @@ -35,6 +36,7 @@ extern "C" BOOL DAPI FileExistsAfterRestart( { HRESULT hr = S_OK; BOOL fExists = FALSE; + BOOL fRegExists = FALSE; HKEY hkPendingFileRename = NULL; LPWSTR* rgsczRenames = NULL; DWORD cRenames = 0; @@ -44,18 +46,20 @@ extern "C" BOOL DAPI FileExistsAfterRestart( if (fExists) { hr = RegOpen(HKEY_LOCAL_MACHINE, REGISTRY_PENDING_FILE_RENAME_KEY, KEY_QUERY_VALUE, &hkPendingFileRename); - if (E_FILENOTFOUND == hr) + FileExitOnPathFailure(hr, fRegExists, "Failed to open pending file rename registry key."); + + if (!fRegExists) { - ExitFunction1(hr = S_OK); + ExitFunction(); } - FileExitOnFailure(hr, "Failed to open pending file rename registry key."); hr = RegReadStringArray(hkPendingFileRename, REGISTRY_PENDING_FILE_RENAME_VALUE, &rgsczRenames, &cRenames); - if (E_FILENOTFOUND == hr) + FileExitOnPathFailure(hr, fRegExists, "Failed to read pending file renames."); + + if (!fRegExists) { - ExitFunction1(hr = S_OK); + ExitFunction(); } - FileExitOnFailure(hr, "Failed to read pending file renames."); // The pending file renames array is pairs of source and target paths. We only care // about checking the source paths so skip the target paths (i += 2). @@ -95,6 +99,7 @@ extern "C" HRESULT DAPI FileRemoveFromPendingRename( { HRESULT hr = S_OK; HKEY hkPendingFileRename = NULL; + BOOL fExists = FALSE; LPWSTR* rgsczRenames = NULL; DWORD cRenames = 0; BOOL fPathEqual = FALSE; @@ -102,18 +107,20 @@ extern "C" HRESULT DAPI FileRemoveFromPendingRename( DWORD cNewRenames = 0; hr = RegOpen(HKEY_LOCAL_MACHINE, REGISTRY_PENDING_FILE_RENAME_KEY, KEY_QUERY_VALUE | KEY_SET_VALUE, &hkPendingFileRename); - if (E_FILENOTFOUND == hr) + FileExitOnPathFailure(hr, fExists, "Failed to open pending file rename registry key."); + + if (!fExists) { - ExitFunction1(hr = S_OK); + ExitFunction(); } - FileExitOnFailure(hr, "Failed to open pending file rename registry key."); hr = RegReadStringArray(hkPendingFileRename, REGISTRY_PENDING_FILE_RENAME_VALUE, &rgsczRenames, &cRenames); - if (E_FILENOTFOUND == hr) + FileExitOnPathFailure(hr, fExists, "Failed to read pending file renames."); + + if (!fExists) { - ExitFunction1(hr = S_OK); + ExitFunction(); } - FileExitOnFailure(hr, "Failed to read pending file renames."); // The pending file renames array is pairs of source and target paths. We only care // about checking the source paths so skip the target paths (i += 2). diff --git a/src/libs/dutil/WixToolset.DUtil/fileutil.cpp b/src/libs/dutil/WixToolset.DUtil/fileutil.cpp index ac407916..a9369ea1 100644 --- a/src/libs/dutil/WixToolset.DUtil/fileutil.cpp +++ b/src/libs/dutil/WixToolset.DUtil/fileutil.cpp @@ -443,6 +443,7 @@ extern "C" HRESULT DAPI FileSize( ) { HRESULT hr = S_OK; + DWORD er = ERROR_SUCCESS; HANDLE hFile = INVALID_HANDLE_VALUE; FileExitOnNull(pwzFileName, hr, E_INVALIDARG, "Attempted to check filename, but no filename was provided"); @@ -450,6 +451,11 @@ extern "C" HRESULT DAPI FileSize( hFile = ::CreateFileW(pwzFileName, FILE_READ_ATTRIBUTES, FILE_SHARE_READ, NULL, OPEN_EXISTING, FILE_ATTRIBUTE_NORMAL, 0); if (INVALID_HANDLE_VALUE == hFile) { + er = ::GetLastError(); + if (ERROR_PATH_NOT_FOUND == er || ERROR_FILE_NOT_FOUND == er) + { + ExitFunction1(hr = HRESULT_FROM_WIN32(er)); + } FileExitWithLastError(hr, "Failed to open file %ls while checking file size", pwzFileName); } @@ -617,11 +623,11 @@ extern "C" HRESULT DAPI FileReadPartialEx( if (INVALID_HANDLE_VALUE == hFile) { er = ::GetLastError(); - if (E_FILENOTFOUND == HRESULT_FROM_WIN32(er)) + if (ERROR_PATH_NOT_FOUND == er || ERROR_FILE_NOT_FOUND == er) { - ExitFunction1(hr = E_FILENOTFOUND); + ExitFunction1(hr = HRESULT_FROM_WIN32(er)); } - FileExitOnWin32Error(er, hr, "Failed to open file: %ls", wzSrcPath); + FileExitWithLastError(hr, "Failed to open file: %ls", wzSrcPath); } if (!::GetFileSizeEx(hFile, &liFileSize)) diff --git a/src/libs/dutil/WixToolset.DUtil/inc/dutil.h b/src/libs/dutil/WixToolset.DUtil/inc/dutil.h index 2db64812..4a172f46 100644 --- a/src/libs/dutil/WixToolset.DUtil/inc/dutil.h +++ b/src/libs/dutil/WixToolset.DUtil/inc/dutil.h @@ -131,6 +131,8 @@ void DAPI Dutil_RootFailure(__in_z LPCSTR szFile, __in int iLine, __in HRESULT h #define ExitOnOptionalXmlQueryFailureSource(d, x, b, s, ...) { { if (S_FALSE == x || E_NOTFOUND == x) { b = FALSE; x = S_OK; } else { b = SUCCEEDED(x); } }; ExitOnRootFailureSource(d, x, s, __VA_ARGS__); } #define ExitOnRequiredXmlQueryFailureSource(d, x, s, ...) { if (S_FALSE == x) { x = E_NOTFOUND; } ExitOnRootFailureSource(d, x, s, __VA_ARGS__); } #define ExitOnWaitObjectFailureSource(d, x, b, s, ...) { { if (HRESULT_FROM_WIN32(WAIT_TIMEOUT) == x) { b = TRUE; x = S_OK; } else { b = FALSE; } }; ExitOnFailureSource(d, x, s, __VA_ARGS__); } +#define ExitOnPathFailureSource(d, x, b, s, ...) { { if (E_PATHNOTFOUND == x || E_FILENOTFOUND == x) { b = FALSE; x = S_OK; } else { b = SUCCEEDED(x); } }; ExitOnFailureSource(d, x, s, __VA_ARGS__); } +#define ExitWithPathLastErrorSource(d, x, s, ...) { DWORD Dutil_er = ::GetLastError(); x = HRESULT_FROM_WIN32(Dutil_er); if (!FAILED(x)) { x = E_FAIL; } else if (E_PATHNOTFOUND == x || E_FILENOTFOUND == x) { x = S_OK; } ExitOnRootFailureSource(d, x, s, __VA_ARGS__); } #define ExitOnLastError(x, s, ...) ExitOnLastErrorSource(DUTIL_SOURCE_DEFAULT, x, s, __VA_ARGS__) #define ExitOnLastErrorDebugTrace(x, s, ...) ExitOnLastErrorDebugTraceSource(DUTIL_SOURCE_DEFAULT, x, s, __VA_ARGS__) @@ -147,6 +149,8 @@ void DAPI Dutil_RootFailure(__in_z LPCSTR szFile, __in int iLine, __in HRESULT h #define ExitOnOptionalXmlQueryFailure(x, b, s, ...) ExitOnOptionalXmlQueryFailureSource(DUTIL_SOURCE_DEFAULT, x, b, s, __VA_ARGS__) #define ExitOnRequiredXmlQueryFailure(x, s, ...) ExitOnRequiredXmlQueryFailureSource(DUTIL_SOURCE_DEFAULT, x, s, __VA_ARGS__) #define ExitOnWaitObjectFailure(x, b, s, ...) ExitOnWaitObjectFailureSource(DUTIL_SOURCE_DEFAULT, x, b, s, __VA_ARGS__) +#define ExitOnPathFailure(x, b, s, ...) ExitOnPathFailureSource(DUTIL_SOURCE_DEFAULT, x, b, s, __VA_ARGS__) +#define ExitWithPathLastError(x, s, ...) ExitWithPathLastErrorSource(DUTIL_SOURCE_DEFAULT, x, s, __VA_ARGS__) // release macros #define ReleaseObject(x) if (x) { x->Release(); } diff --git a/src/libs/dutil/WixToolset.DUtil/inc/logutil.h b/src/libs/dutil/WixToolset.DUtil/inc/logutil.h index 1636e297..8f38e0ce 100644 --- a/src/libs/dutil/WixToolset.DUtil/inc/logutil.h +++ b/src/libs/dutil/WixToolset.DUtil/inc/logutil.h @@ -8,9 +8,11 @@ extern "C" { #define LogExitOnFailureSource(d, x, i, f, ...) if (FAILED(x)) { LogErrorId(x, i, __VA_ARGS__); ExitTraceSource(d, x, f, __VA_ARGS__); goto LExit; } #define LogExitOnRootFailureSource(d, x, i, f, ...) if (FAILED(x)) { LogErrorId(x, i, __VA_ARGS__); Dutil_RootFailure(__FILE__, __LINE__, x); ExitTraceSource(d, x, f, __VA_ARGS__); goto LExit; } +#define LogExitWithRootFailureSource(d, x, e, i, f, ...) { x = FAILED(e) ? e : E_FAIL; LogErrorId(x, i, __VA_ARGS__); Dutil_RootFailure(__FILE__, __LINE__, x); ExitTraceSource(d, x, f, __VA_ARGS__); goto LExit; } #define LogExitOnFailure(x, i, f, ...) LogExitOnFailureSource(DUTIL_SOURCE_DEFAULT, x, i, f, __VA_ARGS__) #define LogExitOnRootFailure(x, i, f, ...) LogExitOnRootFailureSource(DUTIL_SOURCE_DEFAULT, x, i, f, __VA_ARGS__) +#define LogExitWithRootFailure(x, e, i, f, ...) LogExitWithRootFailureSource(DUTIL_SOURCE_DEFAULT, x, e, i, f, __VA_ARGS__) typedef HRESULT (DAPI *PFN_LOGSTRINGWORKRAW)( __in_z LPCSTR szString, diff --git a/src/libs/dutil/WixToolset.DUtil/monutil.cpp b/src/libs/dutil/WixToolset.DUtil/monutil.cpp index 10954164..d7bcfa52 100644 --- a/src/libs/dutil/WixToolset.DUtil/monutil.cpp +++ b/src/libs/dutil/WixToolset.DUtil/monutil.cpp @@ -17,6 +17,7 @@ #define MonExitOnWin32Error(e, x, s, ...) ExitOnWin32ErrorSource(DUTIL_SOURCE_MONUTIL, e, x, s, __VA_ARGS__) #define MonExitOnGdipFailure(g, x, s, ...) ExitOnGdipFailureSource(DUTIL_SOURCE_MONUTIL, g, x, s, __VA_ARGS__) #define MonExitOnWaitObjectFailure(x, b, s, ...) ExitOnWaitObjectFailureSource(DUTIL_SOURCE_MONUTIL, x, b, s, __VA_ARGS__) +#define MonExitOnPathFailure(x, b, s, ...) ExitOnPathFailureSource(DUTIL_SOURCE_MONUTIL, x, b, s, __VA_ARGS__) const int MON_THREAD_GROWTH = 5; const int MON_ARRAY_GROWTH = 40; @@ -982,6 +983,7 @@ static HRESULT InitiateWait( DWORD dwIndex = 0; HKEY hk = NULL; HANDLE hTemp = INVALID_HANDLE_VALUE; + BOOL fExists = FALSE; if (pRequest->hNotify) { @@ -1025,11 +1027,12 @@ static HRESULT InitiateWait( case MON_REGKEY: ReleaseRegKey(pRequest->regkey.hkSubKey); hr = RegOpen(pRequest->regkey.hkRoot, pRequest->rgsczPathHierarchy[dwIndex], KEY_NOTIFY | GetRegKeyBitness(pRequest), &pRequest->regkey.hkSubKey); - if (E_FILENOTFOUND == hr || E_PATHNOTFOUND == hr) + MonExitOnPathFailure(hr, fExists, "Failed to open regkey %ls", pRequest->rgsczPathHierarchy[dwIndex]); + + if (!fExists) { continue; } - MonExitOnFailure(hr, "Failed to open regkey %ls", pRequest->rgsczPathHierarchy[dwIndex]); er = ::RegNotifyChangeKeyValue(pRequest->regkey.hkSubKey, GetRecursiveFlag(pRequest, dwIndex), REG_NOTIFY_CHANGE_NAME | REG_NOTIFY_CHANGE_LAST_SET | REG_NOTIFY_CHANGE_SECURITY, *pHandle, TRUE); ReleaseRegKey(hk); @@ -1038,12 +1041,9 @@ static HRESULT InitiateWait( { continue; } - else - { - MonExitOnWin32Error(er, hr, "Failed to wait on subkey %ls", pRequest->rgsczPathHierarchy[dwIndex]); + MonExitOnFailure(hr, "Failed to wait on subkey %ls", pRequest->rgsczPathHierarchy[dwIndex]); - fHandleFound = TRUE; - } + fHandleFound = TRUE; break; default: diff --git a/src/libs/dutil/WixToolset.DUtil/osutil.cpp b/src/libs/dutil/WixToolset.DUtil/osutil.cpp index 880ec3ea..4798dc58 100644 --- a/src/libs/dutil/WixToolset.DUtil/osutil.cpp +++ b/src/libs/dutil/WixToolset.DUtil/osutil.cpp @@ -16,6 +16,7 @@ #define OsExitOnInvalidHandleWithLastError(p, x, s, ...) ExitOnInvalidHandleWithLastErrorSource(DUTIL_SOURCE_OSUTIL, p, x, s, __VA_ARGS__) #define OsExitOnWin32Error(e, x, s, ...) ExitOnWin32ErrorSource(DUTIL_SOURCE_OSUTIL, e, x, s, __VA_ARGS__) #define OsExitOnGdipFailure(g, x, s, ...) ExitOnGdipFailureSource(DUTIL_SOURCE_OSUTIL, g, x, s, __VA_ARGS__) +#define OsExitOnPathFailure(x, b, s, ...) ExitOnPathFailureSource(DUTIL_SOURCE_OSUTIL, x, b, s, __VA_ARGS__) typedef NTSTATUS(NTAPI* PFN_RTL_GET_VERSION)(_Out_ PRTL_OSVERSIONINFOEXW lpVersionInformation); @@ -186,23 +187,26 @@ extern "C" HRESULT DAPI OsIsUacEnabled( { HRESULT hr = S_OK; HKEY hk = NULL; + BOOL fExists = FALSE; DWORD dwUacEnabled = 0; *pfUacEnabled = FALSE; // assume UAC not enabled. hr = RegOpen(HKEY_LOCAL_MACHINE, L"SOFTWARE\\Microsoft\\Windows\\CurrentVersion\\Policies\\System", KEY_READ, &hk); - if (E_FILENOTFOUND == hr) + OsExitOnPathFailure(hr, fExists, "Failed to open system policy key to detect UAC."); + + if (!fExists) { - ExitFunction1(hr = S_OK); + ExitFunction(); } - OsExitOnFailure(hr, "Failed to open system policy key to detect UAC."); hr = RegReadNumber(hk, L"EnableLUA", &dwUacEnabled); - if (E_FILENOTFOUND == hr) + OsExitOnPathFailure(hr, fExists, "Failed to read registry value to detect UAC."); + + if (!fExists) { - ExitFunction1(hr = S_OK); + ExitFunction(); } - OsExitOnFailure(hr, "Failed to read registry value to detect UAC."); *pfUacEnabled = (0 != dwUacEnabled); diff --git a/src/libs/dutil/WixToolset.DUtil/path3utl.cpp b/src/libs/dutil/WixToolset.DUtil/path3utl.cpp index cb6ce6d5..844cbfbb 100644 --- a/src/libs/dutil/WixToolset.DUtil/path3utl.cpp +++ b/src/libs/dutil/WixToolset.DUtil/path3utl.cpp @@ -17,6 +17,7 @@ #define PathExitOnInvalidHandleWithLastError(p, x, s, ...) ExitOnInvalidHandleWithLastErrorSource(DUTIL_SOURCE_PATHUTIL, p, x, s, __VA_ARGS__) #define PathExitOnWin32Error(e, x, s, ...) ExitOnWin32ErrorSource(DUTIL_SOURCE_PATHUTIL, e, x, s, __VA_ARGS__) #define PathExitOnGdipFailure(g, x, s, ...) ExitOnGdipFailureSource(DUTIL_SOURCE_PATHUTIL, g, x, s, __VA_ARGS__) +#define PathExitOnPathFailure(x, b, s, ...) ExitOnPathFailureSource(DUTIL_SOURCE_PATHUTIL, x, b, s, __VA_ARGS__) static HRESULT GetTempPathFromSystemEnvironmentVariable( __in HKEY hKey, @@ -32,6 +33,7 @@ DAPI_(HRESULT) PathGetSystemTempPaths( HRESULT hr = S_OK; HMODULE hModule = NULL; BOOL fSystem = FALSE; + BOOL fExists = FALSE; HKEY hKey = NULL; LPWSTR sczTemp = NULL; @@ -61,10 +63,10 @@ DAPI_(HRESULT) PathGetSystemTempPaths( // There is no documented API to get system environment variables, so read them from the registry. hr = RegOpen(HKEY_LOCAL_MACHINE, L"System\\CurrentControlSet\\Control\\Session Manager\\Environment", KEY_READ, &hKey); - if (E_FILENOTFOUND != hr) - { - PathExitOnFailure(hr, "Failed to open system environment registry key."); + PathExitOnPathFailure(hr, fExists, "Failed to open system environment registry key."); + if (fExists) + { hr = GetTempPathFromSystemEnvironmentVariable(hKey, L"TMP", &sczTemp); PathExitOnFailure(hr, "Failed to get temp path from system TMP."); @@ -118,14 +120,16 @@ static HRESULT GetTempPathFromSystemEnvironmentVariable( HRESULT hr = S_OK; LPWSTR sczValue = NULL; BOOL fNeedsExpansion = FALSE; + BOOL fExists = FALSE; // Read the value unexpanded so that it can be expanded with system environment variables. hr = RegReadUnexpandedString(hKey, wzName, &fNeedsExpansion, &sczValue); - if (E_FILENOTFOUND == hr) + PathExitOnPathFailure(hr, fExists, "Failed to get system '%ls' value.", wzName); + + if (!fExists) { ExitFunction1(hr = S_FALSE); } - PathExitOnFailure(hr, "Failed to get system '%ls' value.", wzName); if (fNeedsExpansion) { diff --git a/src/libs/dutil/WixToolset.DUtil/polcutil.cpp b/src/libs/dutil/WixToolset.DUtil/polcutil.cpp index c2247bc1..a56c380b 100644 --- a/src/libs/dutil/WixToolset.DUtil/polcutil.cpp +++ b/src/libs/dutil/WixToolset.DUtil/polcutil.cpp @@ -16,6 +16,7 @@ #define PolcExitOnInvalidHandleWithLastError(p, x, s, ...) ExitOnInvalidHandleWithLastErrorSource(DUTIL_SOURCE_POLCUTIL, p, x, s, __VA_ARGS__) #define PolcExitOnWin32Error(e, x, s, ...) ExitOnWin32ErrorSource(DUTIL_SOURCE_POLCUTIL, e, x, s, __VA_ARGS__) #define PolcExitOnGdipFailure(g, x, s, ...) ExitOnGdipFailureSource(DUTIL_SOURCE_POLCUTIL, g, x, s, __VA_ARGS__) +#define PolcExitOnPathFailure(x, b, s, ...) ExitOnPathFailureSource(DUTIL_SOURCE_POLCUTIL, x, b, s, __VA_ARGS__) const LPCWSTR REGISTRY_POLICIES_KEY = L"SOFTWARE\\Policies\\"; @@ -34,25 +35,28 @@ extern "C" HRESULT DAPI PolcReadNumber( { HRESULT hr = S_OK; HKEY hk = NULL; + BOOL fExists = FALSE; hr = OpenPolicyKey(wzPolicyPath, &hk); - if (E_FILENOTFOUND == hr || E_PATHNOTFOUND == hr) + PolcExitOnFailure(hr, "Failed to open policy key: %ls", wzPolicyPath); + + if (!hk) { ExitFunction1(hr = S_FALSE); } - PolcExitOnFailure(hr, "Failed to open policy key: %ls", wzPolicyPath); hr = RegReadNumber(hk, wzPolicyName, pdw); - if (E_FILENOTFOUND == hr || E_PATHNOTFOUND == hr) + PolcExitOnPathFailure(hr, fExists, "Failed to open policy key: %ls, name: %ls", wzPolicyPath, wzPolicyName); + + if (!fExists) { ExitFunction1(hr = S_FALSE); } - PolcExitOnFailure(hr, "Failed to open policy key: %ls, name: %ls", wzPolicyPath, wzPolicyName); LExit: ReleaseRegKey(hk); - if (S_FALSE == hr || FAILED(hr)) + if (!fExists) { *pdw = dwDefault; } @@ -69,25 +73,28 @@ extern "C" HRESULT DAPI PolcReadString( { HRESULT hr = S_OK; HKEY hk = NULL; + BOOL fExists = FALSE; hr = OpenPolicyKey(wzPolicyPath, &hk); - if (E_FILENOTFOUND == hr || E_PATHNOTFOUND == hr) + PolcExitOnFailure(hr, "Failed to open policy key: %ls", wzPolicyPath); + + if (!hk) { ExitFunction1(hr = S_FALSE); } - PolcExitOnFailure(hr, "Failed to open policy key: %ls", wzPolicyPath); hr = RegReadString(hk, wzPolicyName, pscz); - if (E_FILENOTFOUND == hr || E_PATHNOTFOUND == hr) + PolcExitOnPathFailure(hr, fExists, "Failed to open policy key: %ls, name: %ls", wzPolicyPath, wzPolicyName); + + if (!fExists) { ExitFunction1(hr = S_FALSE); } - PolcExitOnFailure(hr, "Failed to open policy key: %ls, name: %ls", wzPolicyPath, wzPolicyName); LExit: ReleaseRegKey(hk); - if (S_FALSE == hr || FAILED(hr)) + if (!fExists) { if (NULL == wzDefault) { @@ -112,25 +119,28 @@ extern "C" HRESULT DAPI PolcReadUnexpandedString( { HRESULT hr = S_OK; HKEY hk = NULL; + BOOL fExists = FALSE; hr = OpenPolicyKey(wzPolicyPath, &hk); - if (E_FILENOTFOUND == hr || E_PATHNOTFOUND == hr) + PolcExitOnFailure(hr, "Failed to open policy key: %ls", wzPolicyPath); + + if (!hk) { ExitFunction1(hr = S_FALSE); } - PolcExitOnFailure(hr, "Failed to open policy key: %ls", wzPolicyPath); hr = RegReadUnexpandedString(hk, wzPolicyName, pfNeedsExpansion, pscz); - if (E_FILENOTFOUND == hr || E_PATHNOTFOUND == hr) + PolcExitOnPathFailure(hr, fExists, "Failed to open policy key: %ls, name: %ls", wzPolicyPath, wzPolicyName); + + if (!fExists) { ExitFunction1(hr = S_FALSE); } - PolcExitOnFailure(hr, "Failed to open policy key: %ls, name: %ls", wzPolicyPath, wzPolicyName); LExit: ReleaseRegKey(hk); - if (S_FALSE == hr || FAILED(hr)) + if (!fExists) { if (NULL == wzDefault) { @@ -155,12 +165,18 @@ static HRESULT OpenPolicyKey( { HRESULT hr = S_OK; LPWSTR sczPath = NULL; + BOOL fExists = FALSE; hr = PathConcat(REGISTRY_POLICIES_KEY, wzPolicyPath, &sczPath); PolcExitOnFailure(hr, "Failed to combine logging path with root path."); hr = RegOpen(HKEY_LOCAL_MACHINE, sczPath, KEY_READ, phk); - PolcExitOnFailure(hr, "Failed to open policy registry key."); + PolcExitOnPathFailure(hr, fExists, "Failed to open policy registry key."); + + if (!fExists) + { + ReleaseRegKey(*phk); + } LExit: ReleaseStr(sczPath); diff --git a/src/libs/dutil/WixToolset.DUtil/regutil.cpp b/src/libs/dutil/WixToolset.DUtil/regutil.cpp index 9a1b9ced..744e7a3f 100644 --- a/src/libs/dutil/WixToolset.DUtil/regutil.cpp +++ b/src/libs/dutil/WixToolset.DUtil/regutil.cpp @@ -17,6 +17,7 @@ #define RegExitOnInvalidHandleWithLastError(p, x, s, ...) ExitOnInvalidHandleWithLastErrorSource(DUTIL_SOURCE_REGUTIL, p, x, s, __VA_ARGS__) #define RegExitOnWin32Error(e, x, s, ...) ExitOnWin32ErrorSource(DUTIL_SOURCE_REGUTIL, e, x, s, __VA_ARGS__) #define RegExitOnGdipFailure(g, x, s, ...) ExitOnGdipFailureSource(DUTIL_SOURCE_REGUTIL, g, x, s, __VA_ARGS__) +#define RegExitOnPathFailure(x, b, s, ...) ExitOnPathFailureSource(DUTIL_SOURCE_REGUTIL, x, b, s, __VA_ARGS__) static PFN_REGCREATEKEYEXW vpfnRegCreateKeyExW = ::RegCreateKeyExW; static PFN_REGOPENKEYEXW vpfnRegOpenKeyExW = ::RegOpenKeyExW; @@ -179,7 +180,7 @@ DAPI_(HRESULT) RegOpen( __in_z LPCWSTR wzSubKey, __in DWORD dwAccess, __out HKEY* phk -) + ) { return RegOpenEx(hkRoot, wzSubKey, dwAccess, REG_KEY_DEFAULT, phk); } @@ -191,18 +192,18 @@ DAPI_(HRESULT) RegOpenEx( __in DWORD dwAccess, __in REG_KEY_BITNESS kbKeyBitness, __out HKEY* phk -) + ) { HRESULT hr = S_OK; DWORD er = ERROR_SUCCESS; REGSAM samDesired = RegTranslateKeyBitness(kbKeyBitness); er = vpfnRegOpenKeyExW(hkRoot, wzSubKey, 0, dwAccess | samDesired, phk); - if (E_FILENOTFOUND == HRESULT_FROM_WIN32(er)) + if (ERROR_PATH_NOT_FOUND == er || ERROR_FILE_NOT_FOUND == er) { - ExitFunction1(hr = E_FILENOTFOUND); + ExitFunction1(hr = HRESULT_FROM_WIN32(er)); } - RegExitOnWin32Error(er, hr, "Failed to open registry key."); + RegExitOnWin32Error(er, hr, "Failed to open registry key, root: %x, subkey: %ls.", hkRoot, wzSubKey); LExit: return hr; @@ -221,6 +222,7 @@ DAPI_(HRESULT) RegDelete( LPWSTR pszEnumeratedSubKey = NULL; LPWSTR pszRecursiveSubKey = NULL; HKEY hkKey = NULL; + BOOL fExists = FALSE; if (!vfRegInitialized && REG_KEY_DEFAULT != kbKeyBitness) { @@ -231,9 +233,9 @@ DAPI_(HRESULT) RegDelete( if (fDeleteTree) { hr = RegOpenEx(hkRoot, wzSubKey, KEY_READ, kbKeyBitness, &hkKey); - if (E_FILENOTFOUND == hr) + if (E_PATHNOTFOUND == hr || E_FILENOTFOUND == hr) { - ExitFunction1(hr = S_OK); + ExitFunction(); } RegExitOnFailure(hr, "Failed to open this key for enumerating subkeys: %ls", wzSubKey); @@ -246,28 +248,31 @@ DAPI_(HRESULT) RegDelete( RegExitOnFailure(hr, "Failed to concatenate paths while recursively deleting subkeys. Path1: %ls, Path2: %ls", wzSubKey, pszEnumeratedSubKey); hr = RegDelete(hkRoot, pszRecursiveSubKey, kbKeyBitness, fDeleteTree); - RegExitOnFailure(hr, "Failed to recursively delete subkey: %ls", pszRecursiveSubKey); + RegExitOnPathFailure(hr, fExists, "Failed to recursively delete subkey: %ls", pszRecursiveSubKey); } hr = S_OK; + + // Release the handle to make sure it's deleted immediately. + ReleaseRegKey(hkKey); } if (NULL != vpfnRegDeleteKeyExW) { REGSAM samDesired = RegTranslateKeyBitness(kbKeyBitness); er = vpfnRegDeleteKeyExW(hkRoot, wzSubKey, samDesired, 0); - if (E_FILENOTFOUND == HRESULT_FROM_WIN32(er)) + if (ERROR_PATH_NOT_FOUND == er || ERROR_FILE_NOT_FOUND == er) { - ExitFunction1(hr = E_FILENOTFOUND); + ExitFunction1(hr = HRESULT_FROM_WIN32(er)); } RegExitOnWin32Error(er, hr, "Failed to delete registry key (ex)."); } else { er = vpfnRegDeleteKeyW(hkRoot, wzSubKey); - if (E_FILENOTFOUND == HRESULT_FROM_WIN32(er)) + if (ERROR_PATH_NOT_FOUND == er || ERROR_FILE_NOT_FOUND == er) { - ExitFunction1(hr = E_FILENOTFOUND); + ExitFunction1(hr = HRESULT_FROM_WIN32(er)); } RegExitOnWin32Error(er, hr, "Failed to delete registry key."); } @@ -772,7 +777,7 @@ DAPI_(HRESULT) RegReadNumber( DWORD cb = sizeof(DWORD); er = vpfnRegQueryValueExW(hk, wzName, NULL, &dwType, reinterpret_cast(pdwValue), &cb); - if (E_FILENOTFOUND == HRESULT_FROM_WIN32(er)) + if (ERROR_FILE_NOT_FOUND == er) { ExitFunction1(hr = E_FILENOTFOUND); } @@ -801,7 +806,7 @@ DAPI_(HRESULT) RegReadQword( DWORD cb = sizeof(DWORD64); er = vpfnRegQueryValueExW(hk, wzName, NULL, &dwType, reinterpret_cast(pqwValue), &cb); - if (E_FILENOTFOUND == HRESULT_FROM_WIN32(er)) + if (ERROR_FILE_NOT_FOUND == er) { ExitFunction1(hr = E_FILENOTFOUND); } @@ -1015,9 +1020,17 @@ DAPI_(HRESULT) RegKeyReadNumber( HKEY hkKey = NULL; hr = RegOpenEx(hk, wzSubKey, KEY_READ, kbKeyBitness, &hkKey); + if (E_PATHNOTFOUND == hr || E_FILENOTFOUND == hr) + { + ExitFunction(); + } RegExitOnFailure(hr, "Failed to open key: %ls", wzSubKey); hr = RegReadNumber(hkKey, wzName, pdwValue); + if (E_FILENOTFOUND == hr) + { + ExitFunction(); + } RegExitOnFailure(hr, "Failed to read value: %ls/@%ls", wzSubKey, wzName); LExit: @@ -1038,9 +1051,17 @@ DAPI_(BOOL) RegValueExists( DWORD dwType = 0; hr = RegOpenEx(hk, wzSubKey, KEY_READ, kbKeyBitness, &hkKey); + if (E_PATHNOTFOUND == hr || E_FILENOTFOUND == hr) + { + ExitFunction(); + } RegExitOnFailure(hr, "Failed to open key: %ls", wzSubKey); hr = RegGetType(hkKey, wzName, &dwType); + if (E_FILENOTFOUND == hr) + { + ExitFunction(); + } RegExitOnFailure(hr, "Failed to read value type: %ls/@%ls", wzSubKey, wzName); LExit: diff --git a/src/libs/dutil/WixToolset.DUtil/wiutil.cpp b/src/libs/dutil/WixToolset.DUtil/wiutil.cpp index da7cffe7..5f81cf3a 100644 --- a/src/libs/dutil/WixToolset.DUtil/wiutil.cpp +++ b/src/libs/dutil/WixToolset.DUtil/wiutil.cpp @@ -427,6 +427,11 @@ extern "C" HRESULT DAPI WiuGetProductInfo( er = vpfnMsiGetProductInfoW(wzProductCode, wzProperty, *psczValue, &cch); } + + if (ERROR_UNKNOWN_PRODUCT == er || ERROR_UNKNOWN_PROPERTY == er) + { + ExitFunction1(hr = HRESULT_FROM_WIN32(er)); + } WiuExitOnWin32Error(er, hr, "Failed to get product info."); LExit: @@ -449,7 +454,10 @@ extern "C" HRESULT DAPI WiuGetProductInfoEx( if (!vpfnMsiGetProductInfoExW) { hr = WiuGetProductInfo(wzProductCode, wzProperty, psczValue); - WiuExitOnFailure(hr, "Failed to get product info when extended info was not available."); + if (HRESULT_FROM_WIN32(ERROR_UNKNOWN_PRODUCT) != hr && HRESULT_FROM_WIN32(ERROR_UNKNOWN_PROPERTY) != hr) + { + WiuExitOnFailure(hr, "Failed to get product info when extended info was not available."); + } ExitFunction(); } @@ -466,6 +474,11 @@ extern "C" HRESULT DAPI WiuGetProductInfoEx( er = vpfnMsiGetProductInfoExW(wzProductCode, wzUserSid, dwContext, wzProperty, *psczValue, &cch); } + + if (ERROR_UNKNOWN_PRODUCT == er || ERROR_UNKNOWN_PROPERTY == er) + { + ExitFunction1(hr = HRESULT_FROM_WIN32(er)); + } WiuExitOnWin32Error(er, hr, "Failed to get extended product info."); LExit: -- cgit v1.2.3-55-g6feb