From b30c6891216770adb9c46c9904c681dc3ccb1011 Mon Sep 17 00:00:00 2001 From: Sean Hall Date: Wed, 23 Feb 2022 15:14:22 -0600 Subject: Update search code with more concise Exit* macros and remove dead code. --- src/burn/engine/search.cpp | 159 ++++++++---------------------- src/burn/engine/search.h | 13 --- src/burn/test/BurnUnitTest/SearchTest.cpp | 36 ------- 3 files changed, 42 insertions(+), 166 deletions(-) (limited to 'src/burn') diff --git a/src/burn/engine/search.cpp b/src/burn/engine/search.cpp index 4505e1a2..46dd2395 100644 --- a/src/burn/engine/search.cpp +++ b/src/burn/engine/search.cpp @@ -41,10 +41,6 @@ static HRESULT MsiProductSearch( __in BURN_SEARCH* pSearch, __in BURN_VARIABLES* pVariables ); -static HRESULT MsiFeatureSearch( - __in BURN_SEARCH* pSearch, - __in BURN_VARIABLES* pVariables - ); static HRESULT PerformExtensionSearch( __in BURN_SEARCH* pSearch ); @@ -67,6 +63,7 @@ extern "C" HRESULT SearchesParseFromXml( IXMLDOMNode* pixnNode = NULL; DWORD cNodes = 0; BSTR bstrNodeName = NULL; + BOOL fXmlFound = FALSE; LPWSTR scz = NULL; BURN_VARIANT_TYPE valueType = BURN_VARIANT_TYPE_NONE; @@ -76,7 +73,7 @@ extern "C" HRESULT SearchesParseFromXml( // get search node count hr = pixnNodes->get_length((long*)&cNodes); - ExitOnFailure(hr, "Failed to get search node count."); + ExitOnRootFailure(hr, "Failed to get search node count."); if (!cNodes) { @@ -99,18 +96,15 @@ extern "C" HRESULT SearchesParseFromXml( // @Id hr = XmlGetAttributeEx(pixnNode, L"Id", &pSearch->sczKey); - ExitOnFailure(hr, "Failed to get @Id."); + ExitOnRequiredXmlQueryFailure(hr, "Failed to get @Id."); // @Variable hr = XmlGetAttributeEx(pixnNode, L"Variable", &pSearch->sczVariable); - ExitOnFailure(hr, "Failed to get @Variable."); + ExitOnRequiredXmlQueryFailure(hr, "Failed to get @Variable."); // @Condition hr = XmlGetAttributeEx(pixnNode, L"Condition", &pSearch->sczCondition); - if (E_NOTFOUND != hr) - { - ExitOnFailure(hr, "Failed to get @Condition."); - } + ExitOnOptionalXmlQueryFailure(hr, fXmlFound, "Failed to get @Condition."); // read type specific attributes if (CSTR_EQUAL == ::CompareStringW(LOCALE_INVARIANT, 0, bstrNodeName, -1, L"DirectorySearch", -1)) @@ -119,11 +113,11 @@ extern "C" HRESULT SearchesParseFromXml( // @Path hr = XmlGetAttributeEx(pixnNode, L"Path", &pSearch->DirectorySearch.sczPath); - ExitOnFailure(hr, "Failed to get @Path."); + ExitOnRequiredXmlQueryFailure(hr, "Failed to get @Path."); // @Type hr = XmlGetAttributeEx(pixnNode, L"Type", &scz); - ExitOnFailure(hr, "Failed to get @Type."); + ExitOnRequiredXmlQueryFailure(hr, "Failed to get @Type."); if (CSTR_EQUAL == ::CompareStringW(LOCALE_INVARIANT, 0, scz, -1, L"exists", -1)) { @@ -135,8 +129,7 @@ extern "C" HRESULT SearchesParseFromXml( } else { - hr = E_INVALIDARG; - ExitOnFailure(hr, "Invalid value for @Type: %ls", scz); + ExitWithRootFailure(hr, E_INVALIDARG, "Invalid value for @Type: %ls", scz); } } else if (CSTR_EQUAL == ::CompareStringW(LOCALE_INVARIANT, 0, bstrNodeName, -1, L"FileSearch", -1)) @@ -145,11 +138,11 @@ extern "C" HRESULT SearchesParseFromXml( // @Path hr = XmlGetAttributeEx(pixnNode, L"Path", &pSearch->FileSearch.sczPath); - ExitOnFailure(hr, "Failed to get @Path."); + ExitOnRequiredXmlQueryFailure(hr, "Failed to get @Path."); // @Type hr = XmlGetAttributeEx(pixnNode, L"Type", &scz); - ExitOnFailure(hr, "Failed to get @Type."); + ExitOnRequiredXmlQueryFailure(hr, "Failed to get @Type."); if (CSTR_EQUAL == ::CompareStringW(LOCALE_INVARIANT, 0, scz, -1, L"exists", -1)) { @@ -165,8 +158,7 @@ extern "C" HRESULT SearchesParseFromXml( } else { - hr = E_INVALIDARG; - ExitOnFailure(hr, "Invalid value for @Type: %ls", scz); + ExitWithRootFailure(hr, E_INVALIDARG, "Invalid value for @Type: %ls", scz); } } else if (CSTR_EQUAL == ::CompareStringW(LOCALE_INVARIANT, 0, bstrNodeName, -1, L"RegistrySearch", -1)) @@ -175,7 +167,7 @@ extern "C" HRESULT SearchesParseFromXml( // @Root hr = XmlGetAttributeEx(pixnNode, L"Root", &scz); - ExitOnFailure(hr, "Failed to get @Root."); + ExitOnRequiredXmlQueryFailure(hr, "Failed to get @Root."); if (CSTR_EQUAL == ::CompareStringW(LOCALE_INVARIANT, 0, scz, -1, L"HKCR", -1)) { @@ -195,30 +187,23 @@ extern "C" HRESULT SearchesParseFromXml( } else { - hr = E_INVALIDARG; - ExitOnFailure(hr, "Invalid value for @Root: %ls", scz); + ExitWithRootFailure(hr, E_INVALIDARG, "Invalid value for @Root: %ls", scz); } // @Key hr = XmlGetAttributeEx(pixnNode, L"Key", &pSearch->RegistrySearch.sczKey); - ExitOnFailure(hr, "Failed to get Key attribute."); + ExitOnRequiredXmlQueryFailure(hr, "Failed to get Key attribute."); // @Value hr = XmlGetAttributeEx(pixnNode, L"Value", &pSearch->RegistrySearch.sczValue); - if (E_NOTFOUND != hr) - { - ExitOnFailure(hr, "Failed to get Value attribute."); - } + ExitOnOptionalXmlQueryFailure(hr, fXmlFound, "Failed to get Value attribute."); // @Type hr = XmlGetAttributeEx(pixnNode, L"Type", &scz); - ExitOnFailure(hr, "Failed to get @Type."); + ExitOnRequiredXmlQueryFailure(hr, "Failed to get @Type."); hr = XmlGetYesNoAttribute(pixnNode, L"Win64", &pSearch->RegistrySearch.fWin64); - if (E_NOTFOUND != hr) - { - ExitOnFailure(hr, "Failed to get Win64 attribute."); - } + ExitOnOptionalXmlQueryFailure(hr, fXmlFound, "Failed to get Win64 attribute."); if (CSTR_EQUAL == ::CompareStringW(LOCALE_INVARIANT, 0, scz, -1, L"exists", -1)) { @@ -230,14 +215,11 @@ extern "C" HRESULT SearchesParseFromXml( // @ExpandEnvironment hr = XmlGetYesNoAttribute(pixnNode, L"ExpandEnvironment", &pSearch->RegistrySearch.fExpandEnvironment); - if (E_NOTFOUND != hr) - { - ExitOnFailure(hr, "Failed to get @ExpandEnvironment."); - } + ExitOnOptionalXmlQueryFailure(hr, fXmlFound, "Failed to get @ExpandEnvironment."); // @VariableType hr = XmlGetAttributeEx(pixnNode, L"VariableType", &scz); - ExitOnFailure(hr, "Failed to get @VariableType."); + ExitOnRequiredXmlQueryFailure(hr, "Failed to get @VariableType."); if (CSTR_EQUAL == ::CompareStringW(LOCALE_INVARIANT, 0, scz, -1, L"formatted", -1)) { @@ -257,14 +239,12 @@ extern "C" HRESULT SearchesParseFromXml( } else { - hr = E_INVALIDARG; - ExitOnFailure(hr, "Invalid value for @VariableType: %ls", scz); + ExitWithRootFailure(hr, E_INVALIDARG, "Invalid value for @VariableType: %ls", scz); } } else { - hr = E_INVALIDARG; - ExitOnFailure(hr, "Invalid value for @Type: %ls", scz); + ExitWithRootFailure(hr, E_INVALIDARG, "Invalid value for @Type: %ls", scz); } } else if (CSTR_EQUAL == ::CompareStringW(LOCALE_INVARIANT, 0, bstrNodeName, -1, L"MsiComponentSearch", -1)) @@ -273,18 +253,15 @@ extern "C" HRESULT SearchesParseFromXml( // @ProductCode hr = XmlGetAttributeEx(pixnNode, L"ProductCode", &pSearch->MsiComponentSearch.sczProductCode); - if (E_NOTFOUND != hr) - { - ExitOnFailure(hr, "Failed to get @ProductCode."); - } + ExitOnOptionalXmlQueryFailure(hr, fXmlFound, "Failed to get @ProductCode."); // @ComponentId hr = XmlGetAttributeEx(pixnNode, L"ComponentId", &pSearch->MsiComponentSearch.sczComponentId); - ExitOnFailure(hr, "Failed to get @ComponentId."); + ExitOnRequiredXmlQueryFailure(hr, "Failed to get @ComponentId."); // @Type hr = XmlGetAttributeEx(pixnNode, L"Type", &scz); - ExitOnFailure(hr, "Failed to get @Type."); + ExitOnRequiredXmlQueryFailure(hr, "Failed to get @Type."); if (CSTR_EQUAL == ::CompareStringW(LOCALE_INVARIANT, 0, scz, -1, L"keyPath", -1)) { @@ -300,8 +277,7 @@ extern "C" HRESULT SearchesParseFromXml( } else { - hr = E_INVALIDARG; - ExitOnFailure(hr, "Invalid value for @Type: %ls", scz); + ExitWithRootFailure(hr, E_INVALIDARG, "Invalid value for @Type: %ls", scz); } } else if (CSTR_EQUAL == ::CompareStringW(LOCALE_INVARIANT, 0, bstrNodeName, -1, L"MsiProductSearch", -1)) @@ -311,18 +287,20 @@ extern "C" HRESULT SearchesParseFromXml( // @ProductCode (if we don't find a product code then look for an upgrade code) hr = XmlGetAttributeEx(pixnNode, L"ProductCode", &pSearch->MsiProductSearch.sczGuid); - if (E_NOTFOUND != hr) + ExitOnOptionalXmlQueryFailure(hr, fXmlFound, "Failed to get @ProductCode."); + + if (fXmlFound) { - ExitOnFailure(hr, "Failed to get @ProductCode."); pSearch->MsiProductSearch.GuidType = BURN_MSI_PRODUCT_SEARCH_GUID_TYPE_PRODUCTCODE; } else { // @UpgradeCode hr = XmlGetAttributeEx(pixnNode, L"UpgradeCode", &pSearch->MsiProductSearch.sczGuid); - if (E_NOTFOUND != hr) + ExitOnOptionalXmlQueryFailure(hr, fXmlFound, "Failed to get @UpgradeCode."); + + if (fXmlFound) { - ExitOnFailure(hr, "Failed to get @UpgradeCode."); pSearch->MsiProductSearch.GuidType = BURN_MSI_PRODUCT_SEARCH_GUID_TYPE_UPGRADECODE; } } @@ -330,13 +308,12 @@ extern "C" HRESULT SearchesParseFromXml( // make sure we found either a product or upgrade code if (BURN_MSI_PRODUCT_SEARCH_GUID_TYPE_NONE == pSearch->MsiProductSearch.GuidType) { - hr = E_NOTFOUND; - ExitOnFailure(hr, "Failed to get @ProductCode or @UpgradeCode."); + ExitWithRootFailure(hr, E_NOTFOUND, "Failed to get @ProductCode or @UpgradeCode."); } // @Type hr = XmlGetAttributeEx(pixnNode, L"Type", &scz); - ExitOnFailure(hr, "Failed to get @Type."); + ExitOnRequiredXmlQueryFailure(hr, "Failed to get @Type."); if (CSTR_EQUAL == ::CompareStringW(LOCALE_INVARIANT, 0, scz, -1, L"version", -1)) { @@ -356,34 +333,7 @@ extern "C" HRESULT SearchesParseFromXml( } else { - hr = E_INVALIDARG; - ExitOnFailure(hr, "Invalid value for @Type: %ls", scz); - } - } - else if (CSTR_EQUAL == ::CompareStringW(LOCALE_INVARIANT, 0, bstrNodeName, -1, L"MsiFeatureSearch", -1)) - { - pSearch->Type = BURN_SEARCH_TYPE_MSI_FEATURE; - - // @ProductCode - hr = XmlGetAttributeEx(pixnNode, L"ProductCode", &pSearch->MsiFeatureSearch.sczProductCode); - ExitOnFailure(hr, "Failed to get @ProductCode."); - - // @FeatureId - hr = XmlGetAttributeEx(pixnNode, L"FeatureId", &pSearch->MsiFeatureSearch.sczFeatureId); - ExitOnFailure(hr, "Failed to get @FeatureId."); - - // @Type - hr = XmlGetAttributeEx(pixnNode, L"Type", &scz); - ExitOnFailure(hr, "Failed to get @Type."); - - if (CSTR_EQUAL == ::CompareStringW(LOCALE_INVARIANT, 0, scz, -1, L"state", -1)) - { - pSearch->MsiFeatureSearch.Type = BURN_MSI_FEATURE_SEARCH_TYPE_STATE; - } - else - { - hr = E_INVALIDARG; - ExitOnFailure(hr, "Invalid value for @Type: %ls", scz); + ExitWithRootFailure(hr, E_INVALIDARG, "Invalid value for @Type: %ls", scz); } } else if (CSTR_EQUAL == ::CompareStringW(LOCALE_INVARIANT, 0, bstrNodeName, -1, L"ExtensionSearch", -1)) @@ -392,10 +342,10 @@ extern "C" HRESULT SearchesParseFromXml( // @ExtensionId hr = XmlGetAttributeEx(pixnNode, L"ExtensionId", &scz); - ExitOnFailure(hr, "Failed to get @ExtensionId."); + ExitOnRequiredXmlQueryFailure(hr, "Failed to get @ExtensionId."); hr = BurnExtensionFindById(pBurnExtensions, scz, &pSearch->ExtensionSearch.pExtension); - ExitOnFailure(hr, "Failed to find extension '%ls' for search '%ls'", scz, pSearch->sczKey); + ExitOnRootFailure(hr, "Failed to find extension '%ls' for search '%ls'", scz, pSearch->sczKey); } else if (CSTR_EQUAL == ::CompareStringW(LOCALE_INVARIANT, 0, bstrNodeName, -1, L"SetVariable", -1)) { @@ -403,16 +353,16 @@ extern "C" HRESULT SearchesParseFromXml( // @Value hr = XmlGetAttributeEx(pixnNode, L"Value", &scz); - if (E_NOTFOUND != hr) - { - ExitOnFailure(hr, "Failed to get @Value."); + ExitOnOptionalXmlQueryFailure(hr, fXmlFound, "Failed to get @Value."); + if (fXmlFound) + { hr = BVariantSetString(&pSearch->SetVariable.value, scz, 0, FALSE); ExitOnFailure(hr, "Failed to set variant value."); // @Type hr = XmlGetAttributeEx(pixnNode, L"Type", &scz); - ExitOnFailure(hr, "Failed to get @Type."); + ExitOnRequiredXmlQueryFailure(hr, "Failed to get @Type."); if (CSTR_EQUAL == ::CompareStringW(LOCALE_INVARIANT, 0, scz, -1, L"formatted", -1)) { @@ -432,8 +382,7 @@ extern "C" HRESULT SearchesParseFromXml( } else { - hr = E_INVALIDARG; - ExitOnFailure(hr, "Invalid value for @Type: %ls", scz); + ExitWithRootFailure(hr, E_INVALIDARG, "Invalid value for @Type: %ls", scz); } } else @@ -447,8 +396,7 @@ extern "C" HRESULT SearchesParseFromXml( } else { - hr = E_UNEXPECTED; - ExitOnFailure(hr, "Unexpected element name: %ls", bstrNodeName); + ExitWithRootFailure(hr, E_UNEXPECTED, "Unexpected element name: %ls", bstrNodeName); } // prepare next iteration @@ -546,9 +494,6 @@ extern "C" HRESULT SearchesExecute( case BURN_SEARCH_TYPE_MSI_PRODUCT: hr = MsiProductSearch(pSearch, pVariables); break; - case BURN_SEARCH_TYPE_MSI_FEATURE: - hr = MsiFeatureSearch(pSearch, pVariables); - break; case BURN_SEARCH_TYPE_EXTENSION: hr = PerformExtensionSearch(pSearch); break; @@ -605,10 +550,6 @@ extern "C" void SearchesUninitialize( case BURN_SEARCH_TYPE_MSI_PRODUCT: ReleaseStr(pSearch->MsiProductSearch.sczGuid); break; - case BURN_SEARCH_TYPE_MSI_FEATURE: - ReleaseStr(pSearch->MsiFeatureSearch.sczProductCode); - ReleaseStr(pSearch->MsiFeatureSearch.sczFeatureId); - break; case BURN_SEARCH_TYPE_SET_VARIABLE: BVariantUninitialize(&pSearch->SetVariable.value); break; @@ -1249,22 +1190,6 @@ LExit: return hr; } -static HRESULT MsiFeatureSearch( - __in BURN_SEARCH* pSearch, - __in BURN_VARIABLES* /*pVariables*/ - ) -{ - HRESULT hr = E_NOTIMPL; - -//LExit: - if (FAILED(hr)) - { - LogStringLine(REPORT_STANDARD, "MsiFeatureSearch failed: ID '%ls', HRESULT 0x%x", pSearch->sczKey, hr); - } - - return hr; -} - static HRESULT PerformExtensionSearch( __in BURN_SEARCH* pSearch ) diff --git a/src/burn/engine/search.h b/src/burn/engine/search.h index c699c97c..6397d2a3 100644 --- a/src/burn/engine/search.h +++ b/src/burn/engine/search.h @@ -17,7 +17,6 @@ enum BURN_SEARCH_TYPE BURN_SEARCH_TYPE_REGISTRY, BURN_SEARCH_TYPE_MSI_COMPONENT, BURN_SEARCH_TYPE_MSI_PRODUCT, - BURN_SEARCH_TYPE_MSI_FEATURE, BURN_SEARCH_TYPE_EXTENSION, BURN_SEARCH_TYPE_SET_VARIABLE, }; @@ -68,12 +67,6 @@ enum BURN_MSI_PRODUCT_SEARCH_GUID_TYPE BURN_MSI_PRODUCT_SEARCH_GUID_TYPE_UPGRADECODE }; -enum BURN_MSI_FEATURE_SEARCH_TYPE -{ - BURN_MSI_FEATURE_SEARCH_TYPE_NONE, - BURN_MSI_FEATURE_SEARCH_TYPE_STATE, -}; - // structs @@ -119,12 +112,6 @@ typedef struct _BURN_SEARCH LPWSTR sczGuid; } MsiProductSearch; struct - { - BURN_MSI_FEATURE_SEARCH_TYPE Type; - LPWSTR sczProductCode; - LPWSTR sczFeatureId; - } MsiFeatureSearch; - struct { BURN_EXTENSION* pExtension; } ExtensionSearch; diff --git a/src/burn/test/BurnUnitTest/SearchTest.cpp b/src/burn/test/BurnUnitTest/SearchTest.cpp index eca01f5f..de38f2d8 100644 --- a/src/burn/test/BurnUnitTest/SearchTest.cpp +++ b/src/burn/test/BurnUnitTest/SearchTest.cpp @@ -426,42 +426,6 @@ namespace Bootstrapper } } - [Fact] - void MsiFeatureSearchTest() - { - HRESULT hr = S_OK; - IXMLDOMElement* pixeBundle = NULL; - BURN_VARIABLES variables = { }; - BURN_SEARCHES searches = { }; - BURN_EXTENSIONS burnExtensions = { }; - try - { - LPCWSTR wzDocument = - L"" - L" " - L""; - - hr = VariableInitialize(&variables); - TestThrowOnFailure(hr, L"Failed to initialize variables."); - - // load XML document - LoadBundleXmlHelper(wzDocument, &pixeBundle); - - hr = SearchesParseFromXml(&searches, &burnExtensions, pixeBundle); - TestThrowOnFailure(hr, L"Failed to parse searches from XML."); - - // execute searches - hr = SearchesExecute(&searches, &variables); - TestThrowOnFailure(hr, L"Failed to execute searches."); - } - finally - { - ReleaseObject(pixeBundle); - VariablesUninitialize(&variables); - SearchesUninitialize(&searches); - } - } - [Fact] void ConditionalSearchTest() { -- cgit v1.2.3-55-g6feb