aboutsummaryrefslogtreecommitdiff
path: root/src/burn/engine/dependency.cpp
blob: f398a0705eecd43a74cd67d73e62fbda45e4ffbc (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275
1276
1277
1278
1279
1280
1281
1282
1283
1284
1285
1286
1287
1288
1289
1290
1291
1292
1293
1294
1295
1296
1297
1298
1299
1300
1301
1302
1303
1304
1305
1306
1307
1308
1309
1310
1311
1312
1313
1314
1315
1316
1317
1318
1319
1320
1321
1322
1323
1324
1325
1326
1327
1328
1329
1330
1331
1332
1333
1334
1335
1336
1337
1338
1339
1340
1341
1342
1343
1344
1345
1346
1347
1348
1349
1350
1351
1352
1353
1354
1355
1356
1357
1358
1359
1360
1361
1362
1363
1364
1365
1366
1367
1368
1369
1370
1371
1372
1373
1374
1375
1376
1377
1378
1379
1380
1381
1382
1383
1384
1385
1386
1387
1388
1389
1390
1391
1392
1393
1394
1395
1396
1397
1398
1399
1400
1401
1402
1403
1404
1405
1406
1407
1408
1409
1410
1411
1412
1413
1414
1415
1416
1417
1418
1419
1420
1421
1422
1423
1424
1425
1426
1427
1428
1429
1430
1431
1432
1433
1434
1435
1436
1437
1438
1439
1440
1441
1442
1443
1444
1445
1446
1447
1448
1449
1450
1451
1452
1453
1454
1455
1456
1457
1458
1459
1460
1461
1462
1463
1464
1465
1466
1467
1468
1469
1470
1471
1472
1473
1474
1475
1476
1477
1478
1479
1480
1481
1482
1483
1484
1485
1486
// Copyright (c) .NET Foundation and contributors. All rights reserved. Licensed under the Microsoft Reciprocal License. See LICENSE.TXT file in the project root for full license information.

#include "precomp.h"

// constants

#define INITIAL_STRINGDICT_SIZE 48
const LPCWSTR vcszIgnoreDependenciesDelim = L";";


// internal function declarations

static HRESULT DetectPackageDependents(
    __in BURN_PACKAGE* pPackage,
    __in const BURN_REGISTRATION* pRegistration
    );

static HRESULT SplitIgnoreDependencies(
    __in_z LPCWSTR wzIgnoreDependencies,
    __deref_inout_ecount_opt(*pcDependencies) DEPENDENCY** prgDependencies,
    __inout LPUINT pcDependencies,
    __out BOOL* pfIgnoreAll
    );

static HRESULT JoinIgnoreDependencies(
    __out_z LPWSTR* psczIgnoreDependencies,
    __in_ecount(cDependencies) const DEPENDENCY* rgDependencies,
    __in UINT cDependencies
    );

static HRESULT GetIgnoredDependents(
    __in const BURN_PACKAGE* pPackage,
    __in const BURN_PLAN* pPlan,
    __deref_inout STRINGDICT_HANDLE* psdIgnoredDependents
    );

static BOOL GetProviderExists(
    __in HKEY hkRoot,
    __in_z LPCWSTR wzProviderKey
    );

static void CalculateDependencyActionStates(
    __in const BURN_PACKAGE* pPackage,
    __out BURN_DEPENDENCY_ACTION* pDependencyExecuteAction,
    __out BURN_DEPENDENCY_ACTION* pDependencyRollbackAction
    );

static HRESULT AddPackageDependencyActions(
    __in_opt DWORD *pdwInsertSequence,
    __in const BURN_PACKAGE* pPackage,
    __in BURN_PLAN* pPlan,
    __in const BURN_DEPENDENCY_ACTION dependencyExecuteAction,
    __in const BURN_DEPENDENCY_ACTION dependencyRollbackAction
    );

static LPCWSTR GetPackageProviderId(
    __in const BURN_PACKAGE* pPackage
    );

static HRESULT RegisterPackageProvider(
    __in const BURN_DEPENDENCY_PROVIDER* pProvider,
    __in LPCWSTR wzPackageId,
    __in LPCWSTR wzPackageProviderId,
    __in HKEY hkRoot,
    __in BOOL fVital
    );

static void UnregisterPackageProvider(
    __in const BURN_DEPENDENCY_PROVIDER* pProvider,
    __in LPCWSTR wzPackageId,
    __in HKEY hkRoot
    );

static HRESULT RegisterPackageProviderDependent(
    __in const BURN_DEPENDENCY_PROVIDER* pProvider,
    __in BOOL fVital,
    __in HKEY hkRoot,
    __in LPCWSTR wzPackageId,
    __in_z LPCWSTR wzDependentProviderKey
    );

static void UnregisterPackageDependency(
    __in BOOL fPerMachine,
    __in const BURN_PACKAGE* pPackage,
    __in_z LPCWSTR wzDependentProviderKey
    );

static void UnregisterPackageProviderDependent(
    __in const BURN_DEPENDENCY_PROVIDER* pProvider,
    __in HKEY hkRoot,
    __in LPCWSTR wzPackageId,
    __in_z LPCWSTR wzDependentProviderKey
    );
static void UnregisterOrphanPackageProviders(
    __in const BURN_PACKAGE* pPackage
    );


// functions

extern "C" void DependencyUninitializeProvider(
    __in BURN_DEPENDENCY_PROVIDER* pProvider
    )
{
    ReleaseStr(pProvider->sczKey);
    ReleaseStr(pProvider->sczVersion);
    ReleaseStr(pProvider->sczDisplayName);
    ReleaseDependencyArray(pProvider->rgDependents, pProvider->cDependents);

    memset(pProvider, 0, sizeof(BURN_DEPENDENCY_PROVIDER));
}

extern "C" HRESULT DependencyParseProvidersFromXml(
    __in BURN_PACKAGE* pPackage,
    __in IXMLDOMNode* pixnPackage
    )
{
    HRESULT hr = S_OK;
    IXMLDOMNodeList* pixnNodes = NULL;
    DWORD cNodes = 0;
    IXMLDOMNode* pixnNode = NULL;

    // Select dependency provider nodes.
    hr = XmlSelectNodes(pixnPackage, L"Provides", &pixnNodes);
    ExitOnFailure(hr, "Failed to select dependency provider nodes.");

    // Get dependency provider node count.
    hr = pixnNodes->get_length((long*)&cNodes);
    ExitOnFailure(hr, "Failed to get the dependency provider node count.");

    if (!cNodes)
    {
        ExitFunction1(hr = S_OK);
    }

    // Allocate memory for dependency provider pointers.
    pPackage->rgDependencyProviders = (BURN_DEPENDENCY_PROVIDER*)MemAlloc(sizeof(BURN_DEPENDENCY_PROVIDER) * cNodes, TRUE);
    ExitOnNull(pPackage->rgDependencyProviders, hr, E_OUTOFMEMORY, "Failed to allocate memory for dependency providers.");

    pPackage->cDependencyProviders = cNodes;

    // Parse dependency provider elements.
    for (DWORD i = 0; i < cNodes; i++)
    {
        BURN_DEPENDENCY_PROVIDER* pDependencyProvider = &pPackage->rgDependencyProviders[i];

        hr = XmlNextElement(pixnNodes, &pixnNode, NULL);
        ExitOnFailure(hr, "Failed to get the next dependency provider node.");

        // @Key
        hr = XmlGetAttributeEx(pixnNode, L"Key", &pDependencyProvider->sczKey);
        ExitOnFailure(hr, "Failed to get the Key attribute.");

        // @Version
        hr = XmlGetAttributeEx(pixnNode, L"Version", &pDependencyProvider->sczVersion);
        if (E_NOTFOUND != hr)
        {
            ExitOnFailure(hr, "Failed to get the Version attribute.");
        }

        // @DisplayName
        hr = XmlGetAttributeEx(pixnNode, L"DisplayName", &pDependencyProvider->sczDisplayName);
        if (E_NOTFOUND != hr)
        {
            ExitOnFailure(hr, "Failed to get the DisplayName attribute.");
        }

        // @Imported
        hr = XmlGetYesNoAttribute(pixnNode, L"Imported", &pDependencyProvider->fImported);
        if (E_NOTFOUND != hr)
        {
            ExitOnFailure(hr, "Failed to get the Imported attribute.");
        }
        else
        {
            pDependencyProvider->fImported = FALSE;
            hr = S_OK;
        }

        // Prepare next iteration.
        ReleaseNullObject(pixnNode);
    }

    hr = S_OK;

LExit:
    ReleaseObject(pixnNode);
    ReleaseObject(pixnNodes);

    return hr;
}

extern "C" HRESULT DependencyInitialize(
    __in BURN_ENGINE_COMMAND* pInternalCommand,
    __in BURN_DEPENDENCIES* pDependencies,
    __in BURN_REGISTRATION* pRegistration
    )
{
    AssertSz(!pDependencies->cIgnoredDependencies, "Dependencies already initalized.");

    HRESULT hr = S_OK;

    // If no parent was specified at all, use the bundle code as the self dependent.
    if (!pInternalCommand->sczActiveParent)
    {
        pDependencies->wzSelfDependent = pRegistration->sczCode;
    }
    else if (*pInternalCommand->sczActiveParent) // if parent was specified use that as the self dependent.
    {
        pDependencies->wzSelfDependent = pInternalCommand->sczActiveParent;
    }
    // else parent:none was used which means we should not register a dependency on ourself.

    pDependencies->wzActiveParent = pInternalCommand->sczActiveParent;

    // The current bundle provider key should always be ignored for dependency checks.
    hr = DepDependencyArrayAlloc(&pDependencies->rgIgnoredDependencies, &pDependencies->cIgnoredDependencies, pRegistration->sczProviderKey, NULL);
    ExitOnFailure(hr, "Failed to add the bundle provider key to the list of dependencies to ignore.");

    // Add the list of dependencies to ignore.
    if (pInternalCommand->sczIgnoreDependencies)
    {
        hr = SplitIgnoreDependencies(pInternalCommand->sczIgnoreDependencies, &pDependencies->rgIgnoredDependencies, &pDependencies->cIgnoredDependencies, &pDependencies->fIgnoreAllDependents);
        ExitOnFailure(hr, "Failed to split the list of dependencies to ignore.");
    }

    pDependencies->fSelfDependent = NULL != pDependencies->wzSelfDependent;
    pDependencies->fActiveParent = NULL != pInternalCommand->sczActiveParent && NULL != *pInternalCommand->sczActiveParent;

LExit:
    return hr;
}

extern "C" void DependencyUninitialize(
    __in BURN_DEPENDENCIES* pDependencies
    )
{
    if (pDependencies->rgIgnoredDependencies)
    {
        ReleaseDependencyArray(pDependencies->rgIgnoredDependencies, pDependencies->cIgnoredDependencies);
    }

    memset(pDependencies, 0, sizeof(BURN_DEPENDENCIES));
}

extern "C" HRESULT DependencyDetectProviderKeyBundleCode(
    __in BURN_REGISTRATION* pRegistration
    )
{
    HRESULT hr = S_OK;

    hr = DepGetProviderInformation(pRegistration->hkRoot, pRegistration->sczProviderKey, &pRegistration->sczDetectedProviderKeyBundleCode, NULL, NULL);
    if (E_NOTFOUND == hr)
    {
        ReleaseNullStr(pRegistration->sczDetectedProviderKeyBundleCode);
        ExitFunction1(hr = S_OK);
    }
    ExitOnFailure(hr, "Failed to get provider key bundle code.");

    // If a bundle code was not explicitly set, default the provider key bundle code to this bundle's provider key.
    if (!pRegistration->sczDetectedProviderKeyBundleCode || !*pRegistration->sczDetectedProviderKeyBundleCode)
    {
        hr = StrAllocString(&pRegistration->sczDetectedProviderKeyBundleCode, pRegistration->sczProviderKey, 0);
        ExitOnFailure(hr, "Failed to initialize provider key bundle code.");
    }
    else if (CSTR_EQUAL != ::CompareStringW(LOCALE_NEUTRAL, NORM_IGNORECASE, pRegistration->sczCode, -1, pRegistration->sczDetectedProviderKeyBundleCode, -1))
    {
        pRegistration->fDetectedForeignProviderKeyBundleCode = TRUE;
        LogId(REPORT_STANDARD, MSG_DETECTED_FOREIGN_BUNDLE_PROVIDER_REGISTRATION, pRegistration->sczProviderKey, pRegistration->sczDetectedProviderKeyBundleCode);
    }

LExit:
    return hr;
}

extern "C" HRESULT DependencyDetectBundle(
    __in BURN_DEPENDENCIES* pDependencies,
    __in BURN_REGISTRATION* pRegistration
    )
{
    HRESULT hr = S_OK;
    BOOL fExists = FALSE;

    hr = DependencyDetectProviderKeyBundleCode(pRegistration);
    ExitOnFailure(hr, "Failed to detect provider key bundle code.");

    hr = DepCheckDependents(pRegistration->hkRoot, pRegistration->sczProviderKey, 0, NULL, &pRegistration->rgDependents, &pRegistration->cDependents);
    ExitOnPathFailure(hr, fExists, "Failed dependents check on bundle.");

    if (pDependencies->fSelfDependent || pDependencies->fActiveParent)
    {
        for (DWORD i = 0; i < pRegistration->cDependents; ++i)
        {
            DEPENDENCY* pDependent = pRegistration->rgDependents + i;

            if (pDependencies->fActiveParent && CSTR_EQUAL == ::CompareStringW(LOCALE_NEUTRAL, NORM_IGNORECASE, pDependencies->wzActiveParent, -1, pDependent->sczKey, -1))
            {
                pRegistration->fParentRegisteredAsDependent = TRUE;
            }

            if (pDependencies->fSelfDependent && CSTR_EQUAL == ::CompareStringW(LOCALE_NEUTRAL, NORM_IGNORECASE, pDependencies->wzSelfDependent, -1, pDependent->sczKey, -1))
            {
                pRegistration->fSelfRegisteredAsDependent = TRUE;
            }
        }
    }

LExit:
    return hr;
}

extern "C" HRESULT DependencyDetectChainPackage(
    __in BURN_PACKAGE* pPackage,
    __in BURN_REGISTRATION* pRegistration
    )
{
    HRESULT hr = S_OK;

    hr = DetectPackageDependents(pPackage, pRegistration);
    ExitOnFailure(hr, "Failed to detect dependents for package '%ls'", pPackage->sczId);

    hr = DependencyDetectCompatibleEntry(pPackage, pRegistration);
    ExitOnFailure(hr, "Failed to detect compatible package for package '%ls'", pPackage->sczId);

LExit:
    return hr;
}

extern "C" HRESULT DependencyDetectRelatedBundle(
    __in BURN_RELATED_BUNDLE* pRelatedBundle,
    __in BURN_REGISTRATION* pRegistration
    )
{
    HRESULT hr = S_OK;
    BURN_PACKAGE* pPackage = &pRelatedBundle->package;

    if (pRelatedBundle->fPlannable)
    {
        hr = DetectPackageDependents(pPackage, pRegistration);
        ExitOnFailure(hr, "Failed to detect dependents for related bundle '%ls'", pPackage->sczId);
    }

LExit:
    return hr;
}

extern "C" HRESULT DependencyPlanInitialize(
    __in BURN_DEPENDENCIES* pDependencies,
    __in BURN_PLAN* pPlan
    )
{
    HRESULT hr = S_OK;

    // TODO: After adding enumeration to STRINGDICT, a single STRINGDICT_HANDLE can be used everywhere.
    for (DWORD i = 0; i < pDependencies->cIgnoredDependencies; ++i)
    {
        DEPENDENCY* pDependency = pDependencies->rgIgnoredDependencies + i;

        hr = DepDependencyArrayAlloc(&pPlan->rgPlannedProviders, &pPlan->cPlannedProviders, pDependency->sczKey, pDependency->sczName);
        ExitOnFailure(hr, "Failed to add the detected provider to the list of dependencies to ignore.");
    }

LExit:
    return hr;
}

extern "C" HRESULT DependencyAllocIgnoreDependencies(
    __in const BURN_PLAN *pPlan,
    __out_z LPWSTR* psczIgnoreDependencies
    )
{
    HRESULT hr = S_OK;

    // Join the list of dependencies to ignore for each related bundle.
    if (0 < pPlan->cPlannedProviders)
    {
        hr = JoinIgnoreDependencies(psczIgnoreDependencies, pPlan->rgPlannedProviders, pPlan->cPlannedProviders);
        ExitOnFailure(hr, "Failed to join the list of dependencies to ignore.");
    }

LExit:
    return hr;
}

extern "C" HRESULT DependencyAddIgnoreDependencies(
    __in STRINGDICT_HANDLE sdIgnoreDependencies,
    __in_z LPCWSTR wzAddIgnoreDependencies
    )
{
    HRESULT hr = S_OK;
    LPWSTR wzContext = NULL;

    // Parse through the semicolon-delimited tokens and add to the array.
    for (LPCWSTR wzToken = ::wcstok_s(const_cast<LPWSTR>(wzAddIgnoreDependencies), vcszIgnoreDependenciesDelim, &wzContext); wzToken; wzToken = ::wcstok_s(NULL, vcszIgnoreDependenciesDelim, &wzContext))
    {
        hr = DictKeyExists(sdIgnoreDependencies, wzToken);
        if (E_NOTFOUND != hr)
        {
            ExitOnFailure(hr, "Failed to check the dictionary of unique dependencies.");
        }
        else
        {
            hr = DictAddKey(sdIgnoreDependencies, wzToken);
            ExitOnFailure(hr, "Failed to add \"%ls\" to the string dictionary.", wzToken);
        }
    }

LExit:
    return hr;
}

extern "C" HRESULT DependencyPlanPackageBegin(
    __in BOOL fPerMachine,
    __in BURN_PACKAGE* pPackage,
    __in BURN_PLAN* pPlan
    )
{
    HRESULT hr = S_OK;
    STRINGDICT_HANDLE sdIgnoredDependents = NULL;
    BURN_DEPENDENCY_ACTION dependencyExecuteAction = BURN_DEPENDENCY_ACTION_NONE;
    BURN_DEPENDENCY_ACTION dependencyRollbackAction = BURN_DEPENDENCY_ACTION_NONE;
    BOOL fDependentBlocksUninstall = FALSE;
    BOOL fAttemptingUninstall = BOOTSTRAPPER_ACTION_STATE_UNINSTALL == pPackage->execute || pPackage->compatiblePackage.fRemove;

    pPackage->dependencyExecute = BURN_DEPENDENCY_ACTION_NONE;
    pPackage->dependencyRollback = BURN_DEPENDENCY_ACTION_NONE;

    // Make sure the package defines at least one provider.
    if (0 == pPackage->cDependencyProviders)
    {
        LogId(REPORT_VERBOSE, MSG_DEPENDENCY_PACKAGE_SKIP_NOPROVIDERS, pPackage->sczId);
        ExitFunction1(hr = S_OK);
    }

    // Make sure the package is in the same scope as the bundle.
    if (fPerMachine != pPackage->fPerMachine)
    {
        LogId(REPORT_STANDARD, MSG_DEPENDENCY_PACKAGE_SKIP_WRONGSCOPE, pPackage->sczId, LoggingPerMachineToString(fPerMachine), LoggingPerMachineToString(pPackage->fPerMachine));
        ExitFunction1(hr = S_OK);
    }

    // Check if any dependents are registered which would prevent the package from being uninstalled.
    // Build up a list of dependents to ignore, including the current bundle.
    hr = GetIgnoredDependents(pPackage, pPlan, &sdIgnoredDependents);
    ExitOnFailure(hr, "Failed to build the list of ignored dependents.");

    // Skip the dependency check if "ALL" was authored for IGNOREDEPENDENCIES.
    hr = DictKeyExists(sdIgnoredDependents, L"ALL");
    if (E_NOTFOUND != hr)
    {
        ExitOnFailure(hr, "Failed to check if \"ALL\" was set in IGNOREDEPENDENCIES.");
    }
    else
    {
        hr = S_OK;

        for (DWORD i = 0; i < pPackage->cDependencyProviders; ++i)
        {
            const BURN_DEPENDENCY_PROVIDER* pProvider = pPackage->rgDependencyProviders + i;

            for (DWORD j = 0; j < pProvider->cDependents; ++j)
            {
                const DEPENDENCY* pDependency = pProvider->rgDependents + j;

                hr = DictKeyExists(sdIgnoredDependents, pDependency->sczKey);
                if (E_NOTFOUND == hr)
                {
                    hr = S_OK;

                    if (!fDependentBlocksUninstall)
                    {
                        fDependentBlocksUninstall = TRUE;

                        LogId(REPORT_STANDARD, MSG_DEPENDENCY_PACKAGE_HASDEPENDENTS, pPackage->sczId);
                    }

                    LogId(REPORT_VERBOSE, MSG_DEPENDENCY_PACKAGE_DEPENDENT, pDependency->sczKey, LoggingStringOrUnknownIfNull(pDependency->sczName));
                }
                ExitOnFailure(hr, "Failed to check the dictionary of ignored dependents.");
            }
        }
    }

    if (BOOTSTRAPPER_ACTION_STATE_UNINSTALL == pPackage->execute)
    {
        pPackage->fDependencyManagerWasHere = fDependentBlocksUninstall;
    }

    // Calculate the dependency actions before the package itself is planned.
    CalculateDependencyActionStates(pPackage, &dependencyExecuteAction, &dependencyRollbackAction);

    // If dependents were found, change the action to not uninstall the package.
    if (fAttemptingUninstall && fDependentBlocksUninstall)
    {
        pPackage->execute = BOOTSTRAPPER_ACTION_STATE_NONE;
        pPackage->rollback = BOOTSTRAPPER_ACTION_STATE_NONE;

        // Assume the compatible package has the same exact providers.
        pPackage->compatiblePackage.fRemove = FALSE;
    }
    else
    {
        // Trust the forward compatible nature of providers - don't uninstall the package during rollback if there were dependents.
        if (fDependentBlocksUninstall && BOOTSTRAPPER_ACTION_STATE_UNINSTALL == pPackage->rollback)
        {
            pPackage->rollback = BOOTSTRAPPER_ACTION_STATE_NONE;
        }

        // Only plan providers when the package is current (not obsolete).
        if (BOOTSTRAPPER_PACKAGE_STATE_OBSOLETE != pPackage->currentState)
        {
            for (DWORD i = 0; i < pPackage->cDependencyProviders; ++i)
            {
                BURN_DEPENDENCY_PROVIDER* pProvider = &pPackage->rgDependencyProviders[i];

                // Only need to handle providers that were authored directly in the bundle.
                if (pProvider->fImported)
                {
                    continue;
                }

                pProvider->providerExecute = dependencyExecuteAction;
                pProvider->providerRollback = dependencyRollbackAction;

                // Don't overwrite providers that we don't own.
                if (pPackage->compatiblePackage.fDetected)
                {
                    if (BURN_DEPENDENCY_ACTION_REGISTER == pProvider->providerExecute)
                    {
                        pProvider->providerExecute = BURN_DEPENDENCY_ACTION_NONE;
                        pProvider->providerRollback = BURN_DEPENDENCY_ACTION_NONE;
                    }

                    if (BURN_DEPENDENCY_ACTION_REGISTER == pProvider->providerRollback)
                    {
                        pProvider->providerRollback = BURN_DEPENDENCY_ACTION_NONE;
                    }
                }

                if (BURN_DEPENDENCY_ACTION_UNREGISTER == pProvider->providerExecute && !pProvider->fExists)
                {
                    pProvider->providerExecute = BURN_DEPENDENCY_ACTION_NONE;
                }

                if (BURN_DEPENDENCY_ACTION_UNREGISTER == pProvider->providerRollback && pProvider->fExists ||
                    BURN_DEPENDENCY_ACTION_REGISTER == pProvider->providerRollback && !pProvider->fExists)
                {
                    pProvider->providerRollback = BURN_DEPENDENCY_ACTION_NONE;
                }

                if (BURN_DEPENDENCY_ACTION_NONE != pProvider->providerExecute)
                {
                    pPackage->fProviderExecute = TRUE;
                }

                if (BURN_DEPENDENCY_ACTION_NONE != pProvider->providerRollback)
                {
                    pPackage->fProviderRollback = TRUE;
                }
            }
        }

        // If the package will be removed, add its providers to the growing list in the plan.
        if (fAttemptingUninstall)
        {
            for (DWORD i = 0; i < pPackage->cDependencyProviders; ++i)
            {
                const BURN_DEPENDENCY_PROVIDER* pProvider = &pPackage->rgDependencyProviders[i];

                hr = DepDependencyArrayAlloc(&pPlan->rgPlannedProviders, &pPlan->cPlannedProviders, pProvider->sczKey, NULL);
                ExitOnFailure(hr, "Failed to add the package provider key \"%ls\" to the planned list.", pProvider->sczKey);
            }
        }
    }

    for (DWORD i = 0; i < pPackage->cDependencyProviders; ++i)
    {
        BURN_DEPENDENCY_PROVIDER* pProvider = &pPackage->rgDependencyProviders[i];

        pProvider->dependentExecute = dependencyExecuteAction;
        pProvider->dependentRollback = dependencyRollbackAction;

        if (BURN_DEPENDENCY_ACTION_REGISTER == pProvider->dependentRollback &&
            BURN_DEPENDENCY_ACTION_UNREGISTER  == pProvider->providerExecute && BURN_DEPENDENCY_ACTION_REGISTER != pProvider->providerRollback)
        {
            pProvider->dependentRollback = BURN_DEPENDENCY_ACTION_NONE;
        }

        if (BURN_DEPENDENCY_ACTION_UNREGISTER == pProvider->dependentExecute && !pProvider->fBundleRegisteredAsDependent)
        {
            pProvider->dependentExecute = BURN_DEPENDENCY_ACTION_NONE;
        }

        if (BURN_DEPENDENCY_ACTION_UNREGISTER == pProvider->dependentRollback && pProvider->fBundleRegisteredAsDependent ||
            BURN_DEPENDENCY_ACTION_REGISTER == pProvider->dependentRollback && !pProvider->fBundleRegisteredAsDependent)
        {
            pProvider->dependentRollback = BURN_DEPENDENCY_ACTION_NONE;
        }

        // The highest aggregate action state found will be returned.
        if (pPackage->dependencyExecute < pProvider->dependentExecute)
        {
            pPackage->dependencyExecute = pProvider->dependentExecute;
        }

        if (pPackage->dependencyRollback < pProvider->dependentRollback)
        {
            pPackage->dependencyRollback = pProvider->dependentRollback;
        }
    }

LExit:
    ReleaseDict(sdIgnoredDependents);

    return hr;
}

extern "C" HRESULT DependencyPlanPackage(
    __in_opt DWORD *pdwInsertSequence,
    __in const BURN_PACKAGE* pPackage,
    __in BURN_PLAN* pPlan
    )
{
    HRESULT hr = S_OK;
    BURN_EXECUTE_ACTION* pAction = NULL;

    // If the dependency execution action is to unregister, add the dependency actions to the plan
    // *before* the provider key is potentially removed.
    if (BURN_DEPENDENCY_ACTION_UNREGISTER == pPackage->dependencyExecute)
    {
        hr = AddPackageDependencyActions(pdwInsertSequence, pPackage, pPlan, pPackage->dependencyExecute, pPackage->dependencyRollback);
        ExitOnFailure(hr, "Failed to plan the dependency actions for package: %ls", pPackage->sczId);
    }

    // Add the provider rollback plan.
    if (pPackage->fProviderRollback)
    {
        hr = PlanAppendRollbackAction(pPlan, &pAction);
        ExitOnFailure(hr, "Failed to append provider rollback action.");

        pAction->type = BURN_EXECUTE_ACTION_TYPE_PACKAGE_PROVIDER;
        pAction->packageProvider.pPackage = const_cast<BURN_PACKAGE*>(pPackage);

        // Put a checkpoint before the execute action so that rollback happens
        // if execute fails.
        hr = PlanExecuteCheckpoint(pPlan);
        ExitOnFailure(hr, "Failed to plan provider checkpoint action.");
    }

    // Add the provider execute plan. This comes after rollback so if something goes wrong
    // rollback will try to clean up after us.
    if (pPackage->fProviderExecute)
    {
        if (NULL != pdwInsertSequence)
        {
            hr = PlanInsertExecuteAction(*pdwInsertSequence, pPlan, &pAction);
            ExitOnFailure(hr, "Failed to insert provider execute action.");

            // Always move the sequence after this dependency action so the provider registration
            // stays in front of the inserted actions.
            ++(*pdwInsertSequence);
        }
        else
        {
            hr = PlanAppendExecuteAction(pPlan, &pAction);
            ExitOnFailure(hr, "Failed to append provider execute action.");
        }

        pAction->type = BURN_EXECUTE_ACTION_TYPE_PACKAGE_PROVIDER;
        pAction->packageProvider.pPackage = const_cast<BURN_PACKAGE*>(pPackage);
    }

LExit:
    return hr;
}

extern "C" HRESULT DependencyPlanPackageComplete(
    __in BURN_PACKAGE* pPackage,
    __in BURN_PLAN* pPlan
    )
{
    HRESULT hr = S_OK;

    // Registration of dependencies happens here, after the package is planned to be
    // installed and all that good stuff.
    if (BURN_DEPENDENCY_ACTION_REGISTER == pPackage->dependencyExecute)
    {
        hr = AddPackageDependencyActions(NULL, pPackage, pPlan, pPackage->dependencyExecute, pPackage->dependencyRollback);
        ExitOnFailure(hr, "Failed to plan the dependency actions for package: %ls", pPackage->sczId);
    }

LExit:
    return hr;
}

extern "C" HRESULT DependencyExecutePackageProviderAction(
    __in const BURN_EXECUTE_ACTION* pAction,
    __in BOOL fRollback
    )
{
    AssertSz(BURN_EXECUTE_ACTION_TYPE_PACKAGE_PROVIDER == pAction->type, "Execute action type not supported by this function.");

    HRESULT hr = S_OK;
    const BURN_PACKAGE* pPackage = pAction->packageProvider.pPackage;
    HKEY hkRoot = pPackage->fPerMachine ? HKEY_LOCAL_MACHINE : HKEY_CURRENT_USER;
    LPCWSTR wzId = GetPackageProviderId(pPackage);

    for (DWORD i = 0; i < pPackage->cDependencyProviders; ++i)
    {
        const BURN_DEPENDENCY_PROVIDER* pProvider = pPackage->rgDependencyProviders + i;
        BURN_DEPENDENCY_ACTION action = fRollback ? pProvider->providerRollback : pProvider->providerExecute;
        HRESULT hrProvider = S_OK;

        // Register or unregister the package provider.
        switch (action)
        {
        case BURN_DEPENDENCY_ACTION_REGISTER:
            hrProvider = RegisterPackageProvider(pProvider, pPackage->sczId, wzId, hkRoot, pPackage->fVital);
            if (SUCCEEDED(hr) && FAILED(hrProvider))
            {
                hr = hrProvider;
            }
            break;
        case BURN_DEPENDENCY_ACTION_UNREGISTER:
            UnregisterPackageProvider(pProvider, pPackage->sczId, hkRoot);
            break;
        }
    }

    if (!pPackage->fVital)
    {
        hr = S_OK;
    }

    return hr;
}

extern "C" HRESULT DependencyExecutePackageDependencyAction(
    __in BOOL fPerMachine,
    __in const BURN_EXECUTE_ACTION* pAction,
    __in BOOL fRollback
    )
{
    AssertSz(BURN_EXECUTE_ACTION_TYPE_PACKAGE_DEPENDENCY == pAction->type, "Execute action type not supported by this function.");

    HRESULT hr = S_OK;
    const BURN_PACKAGE* pPackage = pAction->packageDependency.pPackage;
    HKEY hkRoot = pPackage->fPerMachine ? HKEY_LOCAL_MACHINE : HKEY_CURRENT_USER;

    // Do not register a dependency on a package in a different install context.
    if (fPerMachine != pPackage->fPerMachine)
    {
        LogId(REPORT_STANDARD, MSG_DEPENDENCY_PACKAGE_SKIP_WRONGSCOPE, pPackage->sczId, LoggingPerMachineToString(fPerMachine), LoggingPerMachineToString(pPackage->fPerMachine));
        ExitFunction1(hr = S_OK);
    }

    for (DWORD i = 0; i < pPackage->cDependencyProviders; ++i)
    {
        const BURN_DEPENDENCY_PROVIDER* pProvider = pPackage->rgDependencyProviders + i;
        BURN_DEPENDENCY_ACTION action = fRollback ? pProvider->dependentRollback : pProvider->dependentExecute;
        HRESULT hrProvider = S_OK;

        // Register or unregister the bundle as a dependent of the package dependency provider.
        switch (action)
        {
        case BURN_DEPENDENCY_ACTION_REGISTER:
            hrProvider = RegisterPackageProviderDependent(pProvider, pPackage->fVital, hkRoot, pPackage->sczId, pAction->packageDependency.sczBundleProviderKey);
            if (SUCCEEDED(hr) && FAILED(hrProvider))
            {
                hr = hrProvider;
            }
            break;
        case BURN_DEPENDENCY_ACTION_UNREGISTER:
            UnregisterPackageProviderDependent(pProvider, hkRoot, pPackage->sczId, pAction->packageDependency.sczBundleProviderKey);
            break;
        }
    }

LExit:
    if (!pPackage->fVital)
    {
        hr = S_OK;
    }

    return hr;
}

extern "C" HRESULT DependencyRegisterBundle(
    __in const BURN_REGISTRATION* pRegistration
    )
{
    HRESULT hr = S_OK;

    LogId(REPORT_VERBOSE, MSG_DEPENDENCY_BUNDLE_REGISTER, pRegistration->sczProviderKey, pRegistration->pVersion->sczVersion);

    // Register the bundle provider key.
    hr = DepRegisterDependency(pRegistration->hkRoot, pRegistration->sczProviderKey, pRegistration->pVersion->sczVersion, pRegistration->sczDisplayName, pRegistration->sczCode, 0);
    ExitOnFailure(hr, "Failed to register the bundle dependency provider.");

LExit:
    return hr;
}

extern "C" HRESULT DependencyProcessDependentRegistration(
    __in const BURN_REGISTRATION* pRegistration,
    __in const BURN_DEPENDENT_REGISTRATION_ACTION* pAction
    )
{
    HRESULT hr = S_OK;
    BOOL fDeleted = FALSE;

    switch (pAction->type)
    {
    case BURN_DEPENDENT_REGISTRATION_ACTION_TYPE_REGISTER:
        hr = DepRegisterDependent(pRegistration->hkRoot, pRegistration->sczProviderKey, pAction->sczDependentProviderKey, NULL, NULL, 0);
        ExitOnFailure(hr, "Failed to register dependent: %ls", pAction->sczDependentProviderKey);
        break;

    case BURN_DEPENDENT_REGISTRATION_ACTION_TYPE_UNREGISTER:
        hr = DepUnregisterDependent(pRegistration->hkRoot, pRegistration->sczProviderKey, pAction->sczDependentProviderKey);
        ExitOnPathFailure(hr, fDeleted, "Failed to unregister dependent: %ls", pAction->sczDependentProviderKey);
        break;

    default:
        ExitWithRootFailure(hr, E_INVALIDARG, "Unrecognized registration action type: %d", pAction->type);
    }

LExit:
    return hr;
}

extern "C" void DependencyUnregisterBundle(
    __in const BURN_REGISTRATION* pRegistration,
    __in const BURN_PACKAGES* pPackages
    )
{
    HRESULT hr = S_OK;
    LPCWSTR wzDependentProviderKey = pRegistration->sczCode;

    // If we own the bundle dependency then remove it.
    if (!pRegistration->fDetectedForeignProviderKeyBundleCode)
    {
        // Remove the bundle provider key.
        hr = DepUnregisterDependency(pRegistration->hkRoot, pRegistration->sczProviderKey);
        if (SUCCEEDED(hr) || E_FILENOTFOUND == hr)
        {
            LogId(REPORT_VERBOSE, MSG_DEPENDENCY_BUNDLE_UNREGISTERED, pRegistration->sczProviderKey);
        }
        else
        {
            LogId(REPORT_VERBOSE, MSG_DEPENDENCY_BUNDLE_UNREGISTERED_FAILED, pRegistration->sczProviderKey, hr);
        }
    }

    // Best effort to make sure this bundle is not registered as a dependent for anything.
    for (DWORD i = 0; i < pPackages->cPackages; ++i)
    {
        const BURN_PACKAGE* pPackage = pPackages->rgPackages + i;
        UnregisterPackageDependency(pPackage->fPerMachine, pPackage, wzDependentProviderKey);
    }

    for (DWORD i = 0; i < pRegistration->relatedBundles.cRelatedBundles; ++i)
    {
        const BURN_PACKAGE* pPackage = &pRegistration->relatedBundles.rgRelatedBundles[i].package;
        UnregisterPackageDependency(pPackage->fPerMachine, pPackage, wzDependentProviderKey);
    }

    // Best effort to make sure package providers are removed if unused.
    for (DWORD i = 0; i < pPackages->cPackages; ++i)
    {
        const BURN_PACKAGE* pPackage = pPackages->rgPackages + i;
        UnregisterOrphanPackageProviders(pPackage);
    }
}

extern "C" HRESULT DependencyDetectCompatibleEntry(
    __in BURN_PACKAGE* pPackage,
    __in BURN_REGISTRATION* pRegistration
    )
{
    HRESULT hr = S_OK;
    LPWSTR sczId = NULL;
    LPWSTR sczName = NULL;
    LPWSTR sczVersion = NULL;
    LPCWSTR wzPackageProviderId = GetPackageProviderId(pPackage);
    HKEY hkHive = pRegistration->fPerMachine ? HKEY_LOCAL_MACHINE : HKEY_CURRENT_USER;

    for (DWORD i = 0; i < pPackage->cDependencyProviders; ++i)
    {
        BURN_DEPENDENCY_PROVIDER* pProvider = &pPackage->rgDependencyProviders[i];

        hr = DepGetProviderInformation(hkHive, pProvider->sczKey, &sczId, &sczName, &sczVersion);
        if (E_NOTFOUND == hr)
        {
            hr = S_OK;
            continue;
        }
        ExitOnFailure(hr, "Failed to get provider information for compatible package: %ls", pProvider->sczKey);

        // Make sure the compatible package is not the package itself.
        if (!wzPackageProviderId)
        {
            if (!sczId)
            {
                continue;
            }
        }
        else if (sczId && CSTR_EQUAL == ::CompareStringW(LOCALE_NEUTRAL, NORM_IGNORECASE, wzPackageProviderId, -1, sczId, -1))
        {
            continue;
        }

        pPackage->compatiblePackage.fDetected = TRUE;

        hr = StrAllocString(&pPackage->compatiblePackage.compatibleEntry.sczProviderKey, pProvider->sczKey, 0);
        ExitOnFailure(hr, "Failed to copy provider key for compatible entry.");

        pPackage->compatiblePackage.compatibleEntry.sczId = sczId;
        sczId = NULL;

        pPackage->compatiblePackage.compatibleEntry.sczName = sczName;
        sczName = NULL;

        pPackage->compatiblePackage.compatibleEntry.sczVersion = sczVersion;
        sczVersion = NULL;

        break;
    }

LExit:
    return hr;
}

// internal functions


static HRESULT DetectPackageDependents(
    __in BURN_PACKAGE* pPackage,
    __in const BURN_REGISTRATION* pRegistration
    )
{
    HRESULT hr = S_OK;
    HKEY hkHive = pPackage->fPerMachine ? HKEY_LOCAL_MACHINE : HKEY_CURRENT_USER;
    BOOL fCanIgnorePresence = pPackage->fCanAffectRegistration && 0 < pPackage->cDependencyProviders &&
                              (BURN_PACKAGE_REGISTRATION_STATE_PRESENT == pPackage->cacheRegistrationState || BURN_PACKAGE_REGISTRATION_STATE_PRESENT == pPackage->installRegistrationState);
    BOOL fBundleRegisteredAsDependent = FALSE;

    // There's currently no point in getting the dependents if the scope doesn't match,
    // because they will just get ignored.
    if (pRegistration->fPerMachine != pPackage->fPerMachine)
    {
        ExitFunction();
    }

    for (DWORD i = 0; i < pPackage->cDependencyProviders; ++i)
    {
        BURN_DEPENDENCY_PROVIDER* pProvider = &pPackage->rgDependencyProviders[i];
        BOOL fExists = FALSE;

        hr = DepCheckDependents(hkHive, pProvider->sczKey, 0, NULL, &pProvider->rgDependents, &pProvider->cDependents);
        ExitOnPathFailure(hr, fExists, "Failed dependents check on package provider: %ls", pProvider->sczKey);

        if (0 < pProvider->cDependents || GetProviderExists(hkHive, pProvider->sczKey))
        {
            pProvider->fExists = TRUE;
        }

        for (DWORD iDependent = 0; iDependent < pProvider->cDependents; ++iDependent)
        {
            DEPENDENCY* pDependent = pProvider->rgDependents + iDependent;

            if (CSTR_EQUAL == ::CompareStringW(LOCALE_NEUTRAL, NORM_IGNORECASE, pRegistration->sczCode, -1, pDependent->sczKey, -1))
            {
                pProvider->fBundleRegisteredAsDependent = TRUE;
                fBundleRegisteredAsDependent = TRUE;
                break;
            }
        }
    }

    if (fCanIgnorePresence && !fBundleRegisteredAsDependent)
    {
        if (BURN_PACKAGE_REGISTRATION_STATE_PRESENT == pPackage->cacheRegistrationState)
        {
            pPackage->cacheRegistrationState = BURN_PACKAGE_REGISTRATION_STATE_IGNORED;
        }
        if (BURN_PACKAGE_REGISTRATION_STATE_PRESENT == pPackage->installRegistrationState)
        {
            pPackage->installRegistrationState = BURN_PACKAGE_REGISTRATION_STATE_IGNORED;
        }
        if (BURN_PACKAGE_TYPE_MSP == pPackage->type)
        {
            for (DWORD i = 0; i < pPackage->Msp.cTargetProductCodes; ++i)
            {
                BURN_MSPTARGETPRODUCT* pTargetProduct = pPackage->Msp.rgTargetProducts + i;

                if (BURN_PACKAGE_REGISTRATION_STATE_PRESENT == pTargetProduct->registrationState)
                {
                    pTargetProduct->registrationState = BURN_PACKAGE_REGISTRATION_STATE_IGNORED;
                }
            }
        }
    }

LExit:
    return hr;
}

/********************************************************************
 SplitIgnoreDependencies - Splits a semicolon-delimited
  string into a list of unique dependencies to ignore.

*********************************************************************/
static HRESULT SplitIgnoreDependencies(
    __in_z LPCWSTR wzIgnoreDependencies,
    __deref_inout_ecount_opt(*pcDependencies) DEPENDENCY** prgDependencies,
    __inout LPUINT pcDependencies,
    __out BOOL* pfIgnoreAll
    )
{
    HRESULT hr = S_OK;
    LPWSTR wzContext = NULL;
    STRINGDICT_HANDLE sdIgnoreDependencies = NULL;
    *pfIgnoreAll = FALSE;

    // Create a dictionary to hold unique dependencies.
    hr = DictCreateStringList(&sdIgnoreDependencies, INITIAL_STRINGDICT_SIZE, DICT_FLAG_CASEINSENSITIVE);
    ExitOnFailure(hr, "Failed to create the string dictionary.");

    // Parse through the semicolon-delimited tokens and add to the array.
    for (LPCWSTR wzToken = ::wcstok_s(const_cast<LPWSTR>(wzIgnoreDependencies), vcszIgnoreDependenciesDelim, &wzContext); wzToken; wzToken = ::wcstok_s(NULL, vcszIgnoreDependenciesDelim, &wzContext))
    {
        hr = DictKeyExists(sdIgnoreDependencies, wzToken);
        if (E_NOTFOUND != hr)
        {
            ExitOnFailure(hr, "Failed to check the dictionary of unique dependencies.");
        }
        else
        {
            hr = DepDependencyArrayAlloc(prgDependencies, pcDependencies, wzToken, NULL);
            ExitOnFailure(hr, "Failed to add \"%ls\" to the list of dependencies to ignore.", wzToken);

            hr = DictAddKey(sdIgnoreDependencies, wzToken);
            ExitOnFailure(hr, "Failed to add \"%ls\" to the string dictionary.", wzToken);

            if (!*pfIgnoreAll && CSTR_EQUAL == ::CompareStringW(LOCALE_NEUTRAL, NORM_IGNORECASE, L"ALL", -1, wzToken, -1))
            {
                *pfIgnoreAll = TRUE;
            }
        }
    }

LExit:
    ReleaseDict(sdIgnoreDependencies);

    return hr;
}

/********************************************************************
 JoinIgnoreDependencies - Joins a list of dependencies
  to ignore into a semicolon-delimited string of unique values.

*********************************************************************/
static HRESULT JoinIgnoreDependencies(
    __out_z LPWSTR* psczIgnoreDependencies,
    __in_ecount(cDependencies) const DEPENDENCY* rgDependencies,
    __in UINT cDependencies
    )
{
    HRESULT hr = S_OK;
    STRINGDICT_HANDLE sdIgnoreDependencies = NULL;

    // Make sure we pass back an empty string if there are no dependencies.
    if (0 == cDependencies)
    {
        ExitFunction1(hr = S_OK);
    }

    // Create a dictionary to hold unique dependencies.
    hr = DictCreateStringList(&sdIgnoreDependencies, INITIAL_STRINGDICT_SIZE, DICT_FLAG_CASEINSENSITIVE);
    ExitOnFailure(hr, "Failed to create the string dictionary.");

    for (UINT i = 0; i < cDependencies; ++i)
    {
        const DEPENDENCY* pDependency = &rgDependencies[i];

        hr = DictKeyExists(sdIgnoreDependencies, pDependency->sczKey);
        if (E_NOTFOUND != hr)
        {
            ExitOnFailure(hr, "Failed to check the dictionary of unique dependencies.");
        }
        else
        {
            if (0 < i)
            {
                hr = StrAllocConcat(psczIgnoreDependencies, vcszIgnoreDependenciesDelim, 1);
                ExitOnFailure(hr, "Failed to append the string delimiter.");
            }

            hr = StrAllocConcat(psczIgnoreDependencies, pDependency->sczKey, 0);
            ExitOnFailure(hr, "Failed to append the key \"%ls\".", pDependency->sczKey);

            hr = DictAddKey(sdIgnoreDependencies, pDependency->sczKey);
            ExitOnFailure(hr, "Failed to add \"%ls\" to the string dictionary.", pDependency->sczKey);
        }
    }

LExit:
    ReleaseDict(sdIgnoreDependencies);

    return hr;
}

/********************************************************************
 GetIgnoredDependents - Combines the current bundle's
  provider key, packages' provider keys that are being uninstalled,
  and any ignored dependencies authored for packages into a string
  list to pass to deputil.

*********************************************************************/
static HRESULT GetIgnoredDependents(
    __in const BURN_PACKAGE* pPackage,
    __in const BURN_PLAN* pPlan,
    __deref_inout STRINGDICT_HANDLE* psdIgnoredDependents
    )
{
    HRESULT hr = S_OK;
    LPWSTR sczIgnoreDependencies = NULL;

    // Create the dictionary and add the bundle provider key initially.
    hr = DictCreateStringList(psdIgnoredDependents, INITIAL_STRINGDICT_SIZE, DICT_FLAG_CASEINSENSITIVE);
    ExitOnFailure(hr, "Failed to create the string dictionary.");

    hr = DictAddKey(*psdIgnoredDependents, pPlan->wzBundleProviderKey);
    ExitOnFailure(hr, "Failed to add the bundle provider key \"%ls\" to the list of ignored dependencies.", pPlan->wzBundleProviderKey);

    // Add previously planned package providers to the dictionary.
    for (DWORD i = 0; i < pPlan->cPlannedProviders; ++i)
    {
        const DEPENDENCY* pDependency = &pPlan->rgPlannedProviders[i];

        hr = DictAddKey(*psdIgnoredDependents, pDependency->sczKey);
        ExitOnFailure(hr, "Failed to add the package provider key \"%ls\" to the list of ignored dependencies.", pDependency->sczKey);
    }

    // Get the IGNOREDEPENDENCIES property if defined.
    hr = PackageGetProperty(pPackage, DEPENDENCY_IGNOREDEPENDENCIES, &sczIgnoreDependencies);
    if (E_NOTFOUND != hr)
    {
        ExitOnFailure(hr, "Failed to get the package property: %ls", DEPENDENCY_IGNOREDEPENDENCIES);

        // TODO: this is the raw value of the property, all property values are currently formatted in a different part of planning.
        hr = DependencyAddIgnoreDependencies(*psdIgnoredDependents, sczIgnoreDependencies);
        ExitOnFailure(hr, "Failed to add the authored ignored dependencies to the cumulative list of ignored dependencies.");
    }
    else
    {
        hr = S_OK;
    }

LExit:
    ReleaseStr(sczIgnoreDependencies);

    return hr;
}

/********************************************************************
 GetProviderExists - Gets whether the provider key is registered.

*********************************************************************/
static BOOL GetProviderExists(
    __in HKEY hkRoot,
    __in_z LPCWSTR wzProviderKey
    )
{
    HRESULT hr = DepGetProviderInformation(hkRoot, wzProviderKey, NULL, NULL, NULL);
    return SUCCEEDED(hr);
}

/********************************************************************
 CalculateDependencyActionStates - Calculates the dependency execute and
  rollback actions for a package.

*********************************************************************/
static void CalculateDependencyActionStates(
    __in const BURN_PACKAGE* pPackage,
    __out BURN_DEPENDENCY_ACTION* pDependencyExecuteAction,
    __out BURN_DEPENDENCY_ACTION* pDependencyRollbackAction
    )
{
    switch (pPackage->execute)
    {
    case BOOTSTRAPPER_ACTION_STATE_NONE:
        switch (pPackage->requested)
        {
        case BOOTSTRAPPER_REQUEST_STATE_ABSENT:
            // Unregister if the package is not requested but already not installed.
            *pDependencyExecuteAction = BURN_DEPENDENCY_ACTION_UNREGISTER;
            break;
        case BOOTSTRAPPER_REQUEST_STATE_NONE:
            // Register if a newer, compatible package is already installed.
            switch (pPackage->currentState)
            {
            case BOOTSTRAPPER_PACKAGE_STATE_OBSOLETE: __fallthrough;
            case BOOTSTRAPPER_PACKAGE_STATE_SUPERSEDED:
                *pDependencyExecuteAction = BURN_DEPENDENCY_ACTION_REGISTER;
                break;
            }
            break;
        case BOOTSTRAPPER_REQUEST_STATE_PRESENT: __fallthrough;
        case BOOTSTRAPPER_REQUEST_STATE_REPAIR:
            // Register if the package is requested but already installed.
            *pDependencyExecuteAction = BURN_DEPENDENCY_ACTION_REGISTER;
            break;
        }
        break;
    case BOOTSTRAPPER_ACTION_STATE_UNINSTALL:
        *pDependencyExecuteAction = BURN_DEPENDENCY_ACTION_UNREGISTER;
        break;
    case BOOTSTRAPPER_ACTION_STATE_INSTALL: __fallthrough;
    case BOOTSTRAPPER_ACTION_STATE_MODIFY: __fallthrough;
    case BOOTSTRAPPER_ACTION_STATE_REPAIR: __fallthrough;
    case BOOTSTRAPPER_ACTION_STATE_MINOR_UPGRADE:
        *pDependencyExecuteAction = BURN_DEPENDENCY_ACTION_REGISTER;
        break;
    }

    switch (*pDependencyExecuteAction)
    {
    case BURN_DEPENDENCY_ACTION_REGISTER:
        *pDependencyRollbackAction = BURN_DEPENDENCY_ACTION_UNREGISTER;
        break;
    case BURN_DEPENDENCY_ACTION_UNREGISTER:
        *pDependencyRollbackAction = BURN_DEPENDENCY_ACTION_REGISTER;
        break;
    }
}

/********************************************************************
 AddPackageDependencyActions - Adds the dependency execute and rollback
  actions to the plan.

*********************************************************************/
static HRESULT AddPackageDependencyActions(
    __in_opt DWORD *pdwInsertSequence,
    __in const BURN_PACKAGE* pPackage,
    __in BURN_PLAN* pPlan,
    __in const BURN_DEPENDENCY_ACTION dependencyExecuteAction,
    __in const BURN_DEPENDENCY_ACTION dependencyRollbackAction
    )
{
    HRESULT hr = S_OK;
    BURN_EXECUTE_ACTION* pAction = NULL;

    // Add the rollback plan.
    if (BURN_DEPENDENCY_ACTION_NONE != dependencyRollbackAction)
    {
        hr = PlanAppendRollbackAction(pPlan, &pAction);
        ExitOnFailure(hr, "Failed to append rollback action.");

        pAction->type = BURN_EXECUTE_ACTION_TYPE_PACKAGE_DEPENDENCY;
        pAction->packageDependency.pPackage = const_cast<BURN_PACKAGE*>(pPackage);

        hr = StrAllocString(&pAction->packageDependency.sczBundleProviderKey, pPlan->wzBundleProviderKey, 0);
        ExitOnFailure(hr, "Failed to copy the bundle dependency provider.");

        // Put a checkpoint before the execute action so that rollback happens
        // if execute fails.
        hr = PlanExecuteCheckpoint(pPlan);
        ExitOnFailure(hr, "Failed to plan dependency checkpoint action.");
    }

    // Add the execute plan. This comes after rollback so if something goes wrong
    // rollback will try to clean up after us correctly.
    if (BURN_DEPENDENCY_ACTION_NONE != dependencyExecuteAction)
    {
        if (NULL != pdwInsertSequence)
        {
            hr = PlanInsertExecuteAction(*pdwInsertSequence, pPlan, &pAction);
            ExitOnFailure(hr, "Failed to insert execute action.");

            // Always move the sequence after this dependency action so the dependency registration
            // stays in front of the inserted actions.
            ++(*pdwInsertSequence);
        }
        else
        {
            hr = PlanAppendExecuteAction(pPlan, &pAction);
            ExitOnFailure(hr, "Failed to append execute action.");
        }

        pAction->type = BURN_EXECUTE_ACTION_TYPE_PACKAGE_DEPENDENCY;
        pAction->packageDependency.pPackage = const_cast<BURN_PACKAGE*>(pPackage);

        hr = StrAllocString(&pAction->packageDependency.sczBundleProviderKey, pPlan->wzBundleProviderKey, 0);
        ExitOnFailure(hr, "Failed to copy the bundle dependency provider.");
    }

LExit:
    return hr;
}

static LPCWSTR GetPackageProviderId(
    __in const BURN_PACKAGE* pPackage
    )
{
    LPCWSTR wzId = NULL;

    switch (pPackage->type)
    {
    case BURN_PACKAGE_TYPE_MSI:
        wzId = pPackage->Msi.sczProductCode;
        break;
    case BURN_PACKAGE_TYPE_MSP:
        wzId = pPackage->Msp.sczPatchCode;
        break;
    }

    return wzId;
}

static HRESULT RegisterPackageProvider(
    __in const BURN_DEPENDENCY_PROVIDER* pProvider,
    __in LPCWSTR wzPackageId,
    __in LPCWSTR wzPackageProviderId,
    __in HKEY hkRoot,
    __in BOOL fVital
    )
{
    HRESULT hr = S_OK;

    LogId(REPORT_VERBOSE, MSG_DEPENDENCY_PACKAGE_REGISTER, pProvider->sczKey, pProvider->sczVersion, wzPackageId);

    hr = DepRegisterDependency(hkRoot, pProvider->sczKey, pProvider->sczVersion, pProvider->sczDisplayName, wzPackageProviderId, 0);
    ExitOnFailure(hr, "Failed to register the package dependency provider: %ls", pProvider->sczKey);

LExit:
    if (!fVital)
    {
        hr = S_OK;
    }

    return hr;
}

/********************************************************************
 UnregisterPackageProvider - Removes the dependency provider.

 Note: Does not check for existing dependents before removing the key.
*********************************************************************/
static void UnregisterPackageProvider(
    __in const BURN_DEPENDENCY_PROVIDER* pProvider,
    __in LPCWSTR wzPackageId,
    __in HKEY hkRoot
    )
{
    HRESULT hr = S_OK;

    hr = DepUnregisterDependency(hkRoot, pProvider->sczKey);
    if (SUCCEEDED(hr) || E_FILENOTFOUND == hr)
    {
        LogId(REPORT_VERBOSE, MSG_DEPENDENCY_PACKAGE_UNREGISTERED, pProvider->sczKey, wzPackageId);
    }
    else
    {
        LogId(REPORT_VERBOSE, MSG_DEPENDENCY_PACKAGE_UNREGISTERED_FAILED, pProvider->sczKey, wzPackageId, hr);
    }
}

/********************************************************************
 RegisterPackageProviderDependent - Registers the provider key
  as a dependent of a package provider.

*********************************************************************/
static HRESULT RegisterPackageProviderDependent(
    __in const BURN_DEPENDENCY_PROVIDER* pProvider,
    __in BOOL fVital,
    __in HKEY hkRoot,
    __in LPCWSTR wzPackageId,
    __in_z LPCWSTR wzDependentProviderKey
    )
{
    HRESULT hr = S_OK;
    BOOL fExists = FALSE;

    LogId(REPORT_VERBOSE, MSG_DEPENDENCY_PACKAGE_REGISTER_DEPENDENCY, wzDependentProviderKey, pProvider->sczKey, wzPackageId);

    hr = DepRegisterDependent(hkRoot, pProvider->sczKey, wzDependentProviderKey, NULL, NULL, 0);
    ExitOnPathFailure(hr, fExists, "Failed to register the dependency on package dependency provider: %ls", pProvider->sczKey);

    if (!fExists)
    {
        LogId(REPORT_VERBOSE, MSG_DEPENDENCY_PACKAGE_SKIP_MISSING, pProvider->sczKey, wzPackageId);
    }

LExit:
    if (!fVital)
    {
        hr = S_OK;
    }

    return hr;
}

/********************************************************************
 UnregisterPackageDependency - Unregisters the provider key
  as a dependent of a package.

*********************************************************************/
static void UnregisterPackageDependency(
    __in BOOL fPerMachine,
    __in const BURN_PACKAGE* pPackage,
    __in_z LPCWSTR wzDependentProviderKey
    )
{
    HKEY hkRoot = fPerMachine ? HKEY_LOCAL_MACHINE : HKEY_CURRENT_USER;

    // Should be no registration to remove since we don't write keys across contexts.
    if (fPerMachine != pPackage->fPerMachine)
    {
        LogId(REPORT_STANDARD, MSG_DEPENDENCY_PACKAGE_SKIP_WRONGSCOPE, pPackage->sczId, LoggingPerMachineToString(fPerMachine), LoggingPerMachineToString(pPackage->fPerMachine));
        return;
    }

    // Loop through each package provider and remove the bundle dependency key.
    if (pPackage->rgDependencyProviders)
    {
        for (DWORD i = 0; i < pPackage->cDependencyProviders; ++i)
        {
            const BURN_DEPENDENCY_PROVIDER* pProvider = &pPackage->rgDependencyProviders[i];

            UnregisterPackageProviderDependent(pProvider, hkRoot, pPackage->sczId, wzDependentProviderKey);
        }
    }
}

static void UnregisterPackageProviderDependent(
    __in const BURN_DEPENDENCY_PROVIDER* pProvider,
    __in HKEY hkRoot,
    __in LPCWSTR wzPackageId,
    __in_z LPCWSTR wzDependentProviderKey
    )
{
    HRESULT hr = S_OK;

    hr = DepUnregisterDependent(hkRoot, pProvider->sczKey, wzDependentProviderKey);
    if (SUCCEEDED(hr) || E_FILENOTFOUND == hr)
    {
        LogId(REPORT_VERBOSE, MSG_DEPENDENCY_PACKAGE_UNREGISTERED_DEPENDENCY, wzDependentProviderKey, pProvider->sczKey, wzPackageId);
    }
    else
    {
        LogId(REPORT_VERBOSE, MSG_DEPENDENCY_PACKAGE_UNREGISTERED_DEPENDENCY_FAILED, wzDependentProviderKey, pProvider->sczKey, wzPackageId, hr);
    }
}

static void UnregisterOrphanPackageProviders(
    __in const BURN_PACKAGE* pPackage
    )
{
    HRESULT hr = S_OK;
    DEPENDENCY* rgDependents = NULL;
    UINT cDependents = 0;
    HKEY hkRoot = pPackage->fPerMachine ? HKEY_LOCAL_MACHINE : HKEY_CURRENT_USER;

    for (DWORD i = 0; i < pPackage->cDependencyProviders; ++i)
    {
        const BURN_DEPENDENCY_PROVIDER* pProvider = &pPackage->rgDependencyProviders[i];

        // Skip providers not owned by the bundle.
        if (pProvider->fImported)
        {
            continue;
        }

        hr = DepCheckDependents(hkRoot, pProvider->sczKey, 0, NULL, &rgDependents, &cDependents);
        if (SUCCEEDED(hr) && !cDependents)
        {
            UnregisterPackageProvider(pProvider, pPackage->sczId, hkRoot);
        }

        ReleaseDependencyArray(rgDependents, cDependents);
        rgDependents = NULL;
        cDependents = 0;
    }
}