Update search code with more concise Exit* macros and remove dead code.
Sean Hall committed
Feb 23, 2022 at 15:14 UTC
b30c6891216770adb9c46c9904c681dc3ccb1011
3 files changed
+42
-166
src/burn/engine/search.cpp
+42
-117
@@ -41,10 +41,6 @@ static HRESULT MsiProductSearch(
41
__in BURN_SEARCH* pSearch,
42
__in BURN_VARIABLES* pVariables
43
);
44
-static HRESULT MsiFeatureSearch(
45
- __in BURN_SEARCH* pSearch,
46
- __in BURN_VARIABLES* pVariables
47
- );
44
static HRESULT PerformExtensionSearch(
45
__in BURN_SEARCH* pSearch
46
);
@@ -67,6 +63,7 @@ extern "C" HRESULT SearchesParseFromXml(
63
IXMLDOMNode* pixnNode = NULL;
64
DWORD cNodes = 0;
65
BSTR bstrNodeName = NULL;
66
+ BOOL fXmlFound = FALSE;
67
LPWSTR scz = NULL;
68
BURN_VARIANT_TYPE valueType = BURN_VARIANT_TYPE_NONE;
69
@@ -76,7 +73,7 @@ extern "C" HRESULT SearchesParseFromXml(
73
74
// get search node count
75
hr = pixnNodes->get_length((long*)&cNodes);
79
- ExitOnFailure(hr, "Failed to get search node count.");
76
+ ExitOnRootFailure(hr, "Failed to get search node count.");
77
78
if (!cNodes)
79
{
@@ -99,18 +96,15 @@ extern "C" HRESULT SearchesParseFromXml(
96
97
// @Id
98
hr = XmlGetAttributeEx(pixnNode, L"Id", &pSearch->sczKey);
102
- ExitOnFailure(hr, "Failed to get @Id.");
99
+ ExitOnRequiredXmlQueryFailure(hr, "Failed to get @Id.");
100
101
// @Variable
102
hr = XmlGetAttributeEx(pixnNode, L"Variable", &pSearch->sczVariable);
106
- ExitOnFailure(hr, "Failed to get @Variable.");
103
+ ExitOnRequiredXmlQueryFailure(hr, "Failed to get @Variable.");
104
105
// @Condition
106
hr = XmlGetAttributeEx(pixnNode, L"Condition", &pSearch->sczCondition);
110
- if (E_NOTFOUND != hr)
111
- {
112
- ExitOnFailure(hr, "Failed to get @Condition.");
113
- }
107
+ ExitOnOptionalXmlQueryFailure(hr, fXmlFound, "Failed to get @Condition.");
108
109
// read type specific attributes
110
if (CSTR_EQUAL == ::CompareStringW(LOCALE_INVARIANT, 0, bstrNodeName, -1, L"DirectorySearch", -1))
@@ -119,11 +113,11 @@ extern "C" HRESULT SearchesParseFromXml(
113
114
// @Path
115
hr = XmlGetAttributeEx(pixnNode, L"Path", &pSearch->DirectorySearch.sczPath);
122
- ExitOnFailure(hr, "Failed to get @Path.");
116
+ ExitOnRequiredXmlQueryFailure(hr, "Failed to get @Path.");
117
118
// @Type
119
hr = XmlGetAttributeEx(pixnNode, L"Type", &scz);
126
- ExitOnFailure(hr, "Failed to get @Type.");
120
+ ExitOnRequiredXmlQueryFailure(hr, "Failed to get @Type.");
121
122
if (CSTR_EQUAL == ::CompareStringW(LOCALE_INVARIANT, 0, scz, -1, L"exists", -1))
123
{
@@ -135,8 +129,7 @@ extern "C" HRESULT SearchesParseFromXml(
129
}
130
else
131
{
138
- hr = E_INVALIDARG;
139
- ExitOnFailure(hr, "Invalid value for @Type: %ls", scz);
132
+ ExitWithRootFailure(hr, E_INVALIDARG, "Invalid value for @Type: %ls", scz);
133
}
134
}
135
else if (CSTR_EQUAL == ::CompareStringW(LOCALE_INVARIANT, 0, bstrNodeName, -1, L"FileSearch", -1))
@@ -145,11 +138,11 @@ extern "C" HRESULT SearchesParseFromXml(
138
139
// @Path
140
hr = XmlGetAttributeEx(pixnNode, L"Path", &pSearch->FileSearch.sczPath);
148
- ExitOnFailure(hr, "Failed to get @Path.");
141
+ ExitOnRequiredXmlQueryFailure(hr, "Failed to get @Path.");
142
143
// @Type
144
hr = XmlGetAttributeEx(pixnNode, L"Type", &scz);
152
- ExitOnFailure(hr, "Failed to get @Type.");
145
+ ExitOnRequiredXmlQueryFailure(hr, "Failed to get @Type.");
146
147
if (CSTR_EQUAL == ::CompareStringW(LOCALE_INVARIANT, 0, scz, -1, L"exists", -1))
148
{
@@ -165,8 +158,7 @@ extern "C" HRESULT SearchesParseFromXml(
158
}
159
else
160
{
168
- hr = E_INVALIDARG;
169
- ExitOnFailure(hr, "Invalid value for @Type: %ls", scz);
161
+ ExitWithRootFailure(hr, E_INVALIDARG, "Invalid value for @Type: %ls", scz);
162
}
163
}
164
else if (CSTR_EQUAL == ::CompareStringW(LOCALE_INVARIANT, 0, bstrNodeName, -1, L"RegistrySearch", -1))
@@ -175,7 +167,7 @@ extern "C" HRESULT SearchesParseFromXml(
167
168
// @Root
169
hr = XmlGetAttributeEx(pixnNode, L"Root", &scz);
178
- ExitOnFailure(hr, "Failed to get @Root.");
170
+ ExitOnRequiredXmlQueryFailure(hr, "Failed to get @Root.");
171
172
if (CSTR_EQUAL == ::CompareStringW(LOCALE_INVARIANT, 0, scz, -1, L"HKCR", -1))
173
{
@@ -195,30 +187,23 @@ extern "C" HRESULT SearchesParseFromXml(
187
}
188
else
189
{
198
- hr = E_INVALIDARG;
199
- ExitOnFailure(hr, "Invalid value for @Root: %ls", scz);
190
+ ExitWithRootFailure(hr, E_INVALIDARG, "Invalid value for @Root: %ls", scz);
191
}
192
193
// @Key
194
hr = XmlGetAttributeEx(pixnNode, L"Key", &pSearch->RegistrySearch.sczKey);
204
- ExitOnFailure(hr, "Failed to get Key attribute.");
195
+ ExitOnRequiredXmlQueryFailure(hr, "Failed to get Key attribute.");
196
197
// @Value
198
hr = XmlGetAttributeEx(pixnNode, L"Value", &pSearch->RegistrySearch.sczValue);
208
- if (E_NOTFOUND != hr)
209
- {
210
- ExitOnFailure(hr, "Failed to get Value attribute.");
211
- }
199
+ ExitOnOptionalXmlQueryFailure(hr, fXmlFound, "Failed to get Value attribute.");
200
201
// @Type
202
hr = XmlGetAttributeEx(pixnNode, L"Type", &scz);
215
- ExitOnFailure(hr, "Failed to get @Type.");
203
+ ExitOnRequiredXmlQueryFailure(hr, "Failed to get @Type.");
204
205
hr = XmlGetYesNoAttribute(pixnNode, L"Win64", &pSearch->RegistrySearch.fWin64);
218
- if (E_NOTFOUND != hr)
219
- {
220
- ExitOnFailure(hr, "Failed to get Win64 attribute.");
221
- }
206
+ ExitOnOptionalXmlQueryFailure(hr, fXmlFound, "Failed to get Win64 attribute.");
207
208
if (CSTR_EQUAL == ::CompareStringW(LOCALE_INVARIANT, 0, scz, -1, L"exists", -1))
209
{
@@ -230,14 +215,11 @@ extern "C" HRESULT SearchesParseFromXml(
215
216
// @ExpandEnvironment
217
hr = XmlGetYesNoAttribute(pixnNode, L"ExpandEnvironment", &pSearch->RegistrySearch.fExpandEnvironment);
233
- if (E_NOTFOUND != hr)
234
- {
235
- ExitOnFailure(hr, "Failed to get @ExpandEnvironment.");
236
- }
218
+ ExitOnOptionalXmlQueryFailure(hr, fXmlFound, "Failed to get @ExpandEnvironment.");
219
220
// @VariableType
221
hr = XmlGetAttributeEx(pixnNode, L"VariableType", &scz);
240
- ExitOnFailure(hr, "Failed to get @VariableType.");
222
+ ExitOnRequiredXmlQueryFailure(hr, "Failed to get @VariableType.");
223
224
if (CSTR_EQUAL == ::CompareStringW(LOCALE_INVARIANT, 0, scz, -1, L"formatted", -1))
225
{
@@ -257,14 +239,12 @@ extern "C" HRESULT SearchesParseFromXml(
239
}
240
else
241
{
260
- hr = E_INVALIDARG;
261
- ExitOnFailure(hr, "Invalid value for @VariableType: %ls", scz);
242
+ ExitWithRootFailure(hr, E_INVALIDARG, "Invalid value for @VariableType: %ls", scz);
243
}
244
}
245
else
246
{
266
- hr = E_INVALIDARG;
267
- ExitOnFailure(hr, "Invalid value for @Type: %ls", scz);
247
+ ExitWithRootFailure(hr, E_INVALIDARG, "Invalid value for @Type: %ls", scz);
248
}
249
}
250
else if (CSTR_EQUAL == ::CompareStringW(LOCALE_INVARIANT, 0, bstrNodeName, -1, L"MsiComponentSearch", -1))
@@ -273,18 +253,15 @@ extern "C" HRESULT SearchesParseFromXml(
253
254
// @ProductCode
255
hr = XmlGetAttributeEx(pixnNode, L"ProductCode", &pSearch->MsiComponentSearch.sczProductCode);
276
- if (E_NOTFOUND != hr)
277
- {
278
- ExitOnFailure(hr, "Failed to get @ProductCode.");
279
- }
256
+ ExitOnOptionalXmlQueryFailure(hr, fXmlFound, "Failed to get @ProductCode.");
257
258
// @ComponentId
259
hr = XmlGetAttributeEx(pixnNode, L"ComponentId", &pSearch->MsiComponentSearch.sczComponentId);
283
- ExitOnFailure(hr, "Failed to get @ComponentId.");
260
+ ExitOnRequiredXmlQueryFailure(hr, "Failed to get @ComponentId.");
261
262
// @Type
263
hr = XmlGetAttributeEx(pixnNode, L"Type", &scz);
287
- ExitOnFailure(hr, "Failed to get @Type.");
264
+ ExitOnRequiredXmlQueryFailure(hr, "Failed to get @Type.");
265
266
if (CSTR_EQUAL == ::CompareStringW(LOCALE_INVARIANT, 0, scz, -1, L"keyPath", -1))
267
{
@@ -300,8 +277,7 @@ extern "C" HRESULT SearchesParseFromXml(
277
}
278
else
279
{
303
- hr = E_INVALIDARG;
304
- ExitOnFailure(hr, "Invalid value for @Type: %ls", scz);
280
+ ExitWithRootFailure(hr, E_INVALIDARG, "Invalid value for @Type: %ls", scz);
281
}
282
}
283
else if (CSTR_EQUAL == ::CompareStringW(LOCALE_INVARIANT, 0, bstrNodeName, -1, L"MsiProductSearch", -1))
@@ -311,18 +287,20 @@ extern "C" HRESULT SearchesParseFromXml(
287
288
// @ProductCode (if we don't find a product code then look for an upgrade code)
289
hr = XmlGetAttributeEx(pixnNode, L"ProductCode", &pSearch->MsiProductSearch.sczGuid);
314
- if (E_NOTFOUND != hr)
290
+ ExitOnOptionalXmlQueryFailure(hr, fXmlFound, "Failed to get @ProductCode.");
291
+
292
+ if (fXmlFound)
293
{
316
- ExitOnFailure(hr, "Failed to get @ProductCode.");
294
pSearch->MsiProductSearch.GuidType = BURN_MSI_PRODUCT_SEARCH_GUID_TYPE_PRODUCTCODE;
295
}
296
else
297
{
298
// @UpgradeCode
299
hr = XmlGetAttributeEx(pixnNode, L"UpgradeCode", &pSearch->MsiProductSearch.sczGuid);
323
- if (E_NOTFOUND != hr)
300
+ ExitOnOptionalXmlQueryFailure(hr, fXmlFound, "Failed to get @UpgradeCode.");
301
+
302
+ if (fXmlFound)
303
{
325
- ExitOnFailure(hr, "Failed to get @UpgradeCode.");
304
pSearch->MsiProductSearch.GuidType = BURN_MSI_PRODUCT_SEARCH_GUID_TYPE_UPGRADECODE;
305
}
306
}
@@ -330,13 +308,12 @@ extern "C" HRESULT SearchesParseFromXml(
308
// make sure we found either a product or upgrade code
309
if (BURN_MSI_PRODUCT_SEARCH_GUID_TYPE_NONE == pSearch->MsiProductSearch.GuidType)
310
{
333
- hr = E_NOTFOUND;
334
- ExitOnFailure(hr, "Failed to get @ProductCode or @UpgradeCode.");
311
+ ExitWithRootFailure(hr, E_NOTFOUND, "Failed to get @ProductCode or @UpgradeCode.");
312
}
313
314
// @Type
315
hr = XmlGetAttributeEx(pixnNode, L"Type", &scz);
339
- ExitOnFailure(hr, "Failed to get @Type.");
316
+ ExitOnRequiredXmlQueryFailure(hr, "Failed to get @Type.");
317
318
if (CSTR_EQUAL == ::CompareStringW(LOCALE_INVARIANT, 0, scz, -1, L"version", -1))
319
{
@@ -356,34 +333,7 @@ extern "C" HRESULT SearchesParseFromXml(
333
}
334
else
335
{
359
- hr = E_INVALIDARG;
360
- ExitOnFailure(hr, "Invalid value for @Type: %ls", scz);
361
- }
362
- }
363
- else if (CSTR_EQUAL == ::CompareStringW(LOCALE_INVARIANT, 0, bstrNodeName, -1, L"MsiFeatureSearch", -1))
364
- {
365
- pSearch->Type = BURN_SEARCH_TYPE_MSI_FEATURE;
366
-
367
- // @ProductCode
368
- hr = XmlGetAttributeEx(pixnNode, L"ProductCode", &pSearch->MsiFeatureSearch.sczProductCode);
369
- ExitOnFailure(hr, "Failed to get @ProductCode.");
370
-
371
- // @FeatureId
372
- hr = XmlGetAttributeEx(pixnNode, L"FeatureId", &pSearch->MsiFeatureSearch.sczFeatureId);
373
- ExitOnFailure(hr, "Failed to get @FeatureId.");
374
-
375
- // @Type
376
- hr = XmlGetAttributeEx(pixnNode, L"Type", &scz);
377
- ExitOnFailure(hr, "Failed to get @Type.");
378
-
379
- if (CSTR_EQUAL == ::CompareStringW(LOCALE_INVARIANT, 0, scz, -1, L"state", -1))
380
- {
381
- pSearch->MsiFeatureSearch.Type = BURN_MSI_FEATURE_SEARCH_TYPE_STATE;
382
- }
383
- else
384
- {
385
- hr = E_INVALIDARG;
386
- ExitOnFailure(hr, "Invalid value for @Type: %ls", scz);
336
+ ExitWithRootFailure(hr, E_INVALIDARG, "Invalid value for @Type: %ls", scz);
337
}
338
}
339
else if (CSTR_EQUAL == ::CompareStringW(LOCALE_INVARIANT, 0, bstrNodeName, -1, L"ExtensionSearch", -1))
@@ -392,10 +342,10 @@ extern "C" HRESULT SearchesParseFromXml(
342
343
// @ExtensionId
344
hr = XmlGetAttributeEx(pixnNode, L"ExtensionId", &scz);
395
- ExitOnFailure(hr, "Failed to get @ExtensionId.");
345
+ ExitOnRequiredXmlQueryFailure(hr, "Failed to get @ExtensionId.");
346
347
hr = BurnExtensionFindById(pBurnExtensions, scz, &pSearch->ExtensionSearch.pExtension);
398
- ExitOnFailure(hr, "Failed to find extension '%ls' for search '%ls'", scz, pSearch->sczKey);
348
+ ExitOnRootFailure(hr, "Failed to find extension '%ls' for search '%ls'", scz, pSearch->sczKey);
349
}
350
else if (CSTR_EQUAL == ::CompareStringW(LOCALE_INVARIANT, 0, bstrNodeName, -1, L"SetVariable", -1))
351
{
@@ -403,16 +353,16 @@ extern "C" HRESULT SearchesParseFromXml(
353
354
// @Value
355
hr = XmlGetAttributeEx(pixnNode, L"Value", &scz);
406
- if (E_NOTFOUND != hr)
407
- {
408
- ExitOnFailure(hr, "Failed to get @Value.");
356
+ ExitOnOptionalXmlQueryFailure(hr, fXmlFound, "Failed to get @Value.");
357
358
+ if (fXmlFound)
359
+ {
360
hr = BVariantSetString(&pSearch->SetVariable.value, scz, 0, FALSE);
361
ExitOnFailure(hr, "Failed to set variant value.");
362
363
// @Type
364
hr = XmlGetAttributeEx(pixnNode, L"Type", &scz);
415
- ExitOnFailure(hr, "Failed to get @Type.");
365
+ ExitOnRequiredXmlQueryFailure(hr, "Failed to get @Type.");
366
367
if (CSTR_EQUAL == ::CompareStringW(LOCALE_INVARIANT, 0, scz, -1, L"formatted", -1))
368
{
@@ -432,8 +382,7 @@ extern "C" HRESULT SearchesParseFromXml(
382
}
383
else
384
{
435
- hr = E_INVALIDARG;
436
- ExitOnFailure(hr, "Invalid value for @Type: %ls", scz);
385
+ ExitWithRootFailure(hr, E_INVALIDARG, "Invalid value for @Type: %ls", scz);
386
}
387
}
388
else
@@ -447,8 +396,7 @@ extern "C" HRESULT SearchesParseFromXml(
396
}
397
else
398
{
450
- hr = E_UNEXPECTED;
451
- ExitOnFailure(hr, "Unexpected element name: %ls", bstrNodeName);
399
+ ExitWithRootFailure(hr, E_UNEXPECTED, "Unexpected element name: %ls", bstrNodeName);
400
}
401
402
// prepare next iteration
@@ -546,9 +494,6 @@ extern "C" HRESULT SearchesExecute(
494
case BURN_SEARCH_TYPE_MSI_PRODUCT:
495
hr = MsiProductSearch(pSearch, pVariables);
496
break;
549
- case BURN_SEARCH_TYPE_MSI_FEATURE:
550
- hr = MsiFeatureSearch(pSearch, pVariables);
551
- break;
497
case BURN_SEARCH_TYPE_EXTENSION:
498
hr = PerformExtensionSearch(pSearch);
499
break;
@@ -605,10 +550,6 @@ extern "C" void SearchesUninitialize(
550
case BURN_SEARCH_TYPE_MSI_PRODUCT:
551
ReleaseStr(pSearch->MsiProductSearch.sczGuid);
552
break;
608
- case BURN_SEARCH_TYPE_MSI_FEATURE:
609
- ReleaseStr(pSearch->MsiFeatureSearch.sczProductCode);
610
- ReleaseStr(pSearch->MsiFeatureSearch.sczFeatureId);
611
- break;
553
case BURN_SEARCH_TYPE_SET_VARIABLE:
554
BVariantUninitialize(&pSearch->SetVariable.value);
555
break;
@@ -1249,22 +1190,6 @@ LExit:
1190
return hr;
1191
}
1192
1252
-static HRESULT MsiFeatureSearch(
1253
- __in BURN_SEARCH* pSearch,
1254
- __in BURN_VARIABLES* /*pVariables*/
1255
- )
1256
-{
1257
- HRESULT hr = E_NOTIMPL;
1258
-
1259
-//LExit:
1260
- if (FAILED(hr))
1261
- {
1262
- LogStringLine(REPORT_STANDARD, "MsiFeatureSearch failed: ID '%ls', HRESULT 0x%x", pSearch->sczKey, hr);
1263
- }
1264
-
1265
- return hr;
1266
-}
1267
-
1193
static HRESULT PerformExtensionSearch(
1194
__in BURN_SEARCH* pSearch
1195
)
src/burn/engine/search.h
-13
@@ -17,7 +17,6 @@ enum BURN_SEARCH_TYPE
17
BURN_SEARCH_TYPE_REGISTRY,
18
BURN_SEARCH_TYPE_MSI_COMPONENT,
19
BURN_SEARCH_TYPE_MSI_PRODUCT,
20
- BURN_SEARCH_TYPE_MSI_FEATURE,
20
BURN_SEARCH_TYPE_EXTENSION,
21
BURN_SEARCH_TYPE_SET_VARIABLE,
22
};
@@ -68,12 +67,6 @@ enum BURN_MSI_PRODUCT_SEARCH_GUID_TYPE
67
BURN_MSI_PRODUCT_SEARCH_GUID_TYPE_UPGRADECODE
68
};
69
71
-enum BURN_MSI_FEATURE_SEARCH_TYPE
72
-{
73
- BURN_MSI_FEATURE_SEARCH_TYPE_NONE,
74
- BURN_MSI_FEATURE_SEARCH_TYPE_STATE,
75
-};
76
-
70
71
// structs
72
@@ -119,12 +112,6 @@ typedef struct _BURN_SEARCH
112
LPWSTR sczGuid;
113
} MsiProductSearch;
114
struct
122
- {
123
- BURN_MSI_FEATURE_SEARCH_TYPE Type;
124
- LPWSTR sczProductCode;
125
- LPWSTR sczFeatureId;
126
- } MsiFeatureSearch;
127
- struct
115
{
116
BURN_EXTENSION* pExtension;
117
} ExtensionSearch;
src/burn/test/BurnUnitTest/SearchTest.cpp
-36
@@ -426,42 +426,6 @@ namespace Bootstrapper
426
}
427
}
428
429
- [Fact]
430
- void MsiFeatureSearchTest()
431
- {
432
- HRESULT hr = S_OK;
433
- IXMLDOMElement* pixeBundle = NULL;
434
- BURN_VARIABLES variables = { };
435
- BURN_SEARCHES searches = { };
436
- BURN_EXTENSIONS burnExtensions = { };
437
- try
438
- {
439
- LPCWSTR wzDocument =
440
- L"<Bundle>"
441
- L" <MsiFeatureSearch Id='Search1' Type='state' ProductCode='{BAD00000-0000-0000-0000-000000000000}' FeatureId='' Variable='Variable1' />"
442
- L"</Bundle>";
443
-
444
- hr = VariableInitialize(&variables);
445
- TestThrowOnFailure(hr, L"Failed to initialize variables.");
446
-
447
- // load XML document
448
- LoadBundleXmlHelper(wzDocument, &pixeBundle);
449
-
450
- hr = SearchesParseFromXml(&searches, &burnExtensions, pixeBundle);
451
- TestThrowOnFailure(hr, L"Failed to parse searches from XML.");
452
-
453
- // execute searches
454
- hr = SearchesExecute(&searches, &variables);
455
- TestThrowOnFailure(hr, L"Failed to execute searches.");
456
- }
457
- finally
458
- {
459
- ReleaseObject(pixeBundle);
460
- VariablesUninitialize(&variables);
461
- SearchesUninitialize(&searches);
462
- }
463
- }
464
-
429
[Fact]
430
void ConditionalSearchTest()
431
{