-
Notifications
You must be signed in to change notification settings - Fork 7
/
apply.cpp
2899 lines (2458 loc) · 105 KB
/
apply.cpp
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
// 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"
#ifdef DEBUG
#define IgnoreRollbackError(x, f, ...) if (FAILED(x)) { TraceError(x, f, __VA_ARGS__); }
#else
#define IgnoreRollbackError(x, f, ...)
#endif
const DWORD BURN_CACHE_MAX_RECOMMENDED_VERIFY_TRYAGAIN_ATTEMPTS = 2;
enum BURN_CACHE_PROGRESS_TYPE
{
BURN_CACHE_PROGRESS_TYPE_ACQUIRE,
BURN_CACHE_PROGRESS_TYPE_VERIFY,
BURN_CACHE_PROGRESS_TYPE_CONTAINER_OR_PAYLOAD_VERIFY,
BURN_CACHE_PROGRESS_TYPE_EXTRACT,
};
// structs
typedef struct _BURN_CACHE_CONTEXT
{
BURN_USER_EXPERIENCE* pUX;
BURN_VARIABLES* pVariables;
BURN_PAYLOADS* pPayloads;
HANDLE hPipe;
HANDLE hSourceEngineFile;
DWORD64 qwTotalCacheSize;
DWORD64 qwSuccessfulCacheProgress;
LPCWSTR wzLayoutDirectory;
LPWSTR* rgSearchPaths;
DWORD cSearchPaths;
DWORD cSearchPathsMax;
LPWSTR sczLastUsedFolderCandidate;
} BURN_CACHE_CONTEXT;
typedef struct _BURN_CACHE_PROGRESS_CONTEXT
{
BURN_CACHE_CONTEXT* pCacheContext;
BURN_CACHE_PROGRESS_TYPE type;
BURN_CONTAINER* pContainer;
BURN_PACKAGE* pPackage;
BURN_PAYLOAD_GROUP_ITEM* pPayloadGroupItem;
BURN_PAYLOAD* pPayload;
BOOL fCancel;
HRESULT hrError;
} BURN_CACHE_PROGRESS_CONTEXT;
typedef struct _BURN_EXECUTE_CONTEXT
{
BURN_USER_EXPERIENCE* pUX;
BOOL fRollback;
BURN_PACKAGE* pExecutingPackage;
DWORD cExecutedPackages;
DWORD cExecutePackagesTotal;
DWORD* pcOverallProgressTicks;
} BURN_EXECUTE_CONTEXT;
// internal function declarations
static HRESULT WINAPI AuthenticationRequired(
__in LPVOID pData,
__in HINTERNET hUrl,
__in long lHttpCode,
__out BOOL* pfRetrySend,
__out BOOL* pfRetry
);
static void CalculateKeepRegistration(
__in BURN_ENGINE_STATE* pEngineState,
__inout BOOL* pfKeepRegistration
);
static HRESULT ExecuteDependentRegistrationActions(
__in HANDLE hPipe,
__in const BURN_REGISTRATION* pRegistration,
__in_ecount(cActions) const BURN_DEPENDENT_REGISTRATION_ACTION* rgActions,
__in DWORD cActions
);
static HRESULT ApplyCachePackage(
__in BURN_CACHE_CONTEXT* pContext,
__in BURN_PACKAGE* pPackage
);
static HRESULT ApplyExtractContainer(
__in BURN_CACHE_CONTEXT* pContext,
__in BURN_CONTAINER* pContainer
);
static HRESULT ApplyLayoutBundle(
__in BURN_CACHE_CONTEXT* pContext,
__in BURN_PAYLOAD_GROUP* pPayloads,
__in_z LPCWSTR wzExecutableName,
__in_z LPCWSTR wzUnverifiedPath,
__in DWORD64 qwBundleSize
);
static HRESULT ApplyLayoutContainer(
__in BURN_CACHE_CONTEXT* pContext,
__in BURN_CONTAINER* pContainer
);
static HRESULT ApplyProcessPayload(
__in BURN_CACHE_CONTEXT* pContext,
__in_opt BURN_PACKAGE* pPackage,
__in BURN_PAYLOAD_GROUP_ITEM* pPayloadGroupItem
);
static HRESULT ApplyCacheVerifyContainerOrPayload(
__in BURN_CACHE_CONTEXT* pContext,
__in_opt BURN_CONTAINER* pContainer,
__in_opt BURN_PACKAGE* pPackage,
__in_opt BURN_PAYLOAD_GROUP_ITEM* pPayloadGroupItem
);
static HRESULT ExtractContainer(
__in BURN_CACHE_CONTEXT* pContext,
__in BURN_CONTAINER* pContainer
);
static HRESULT LayoutBundle(
__in BURN_CACHE_CONTEXT* pContext,
__in_z LPCWSTR wzExecutableName,
__in_z LPCWSTR wzUnverifiedPath,
__in DWORD64 qwBundleSize
);
static HRESULT ApplyAcquireContainerOrPayload(
__in BURN_CACHE_CONTEXT* pContext,
__in_opt BURN_CONTAINER* pContainer,
__in_opt BURN_PACKAGE* pPackage,
__in_opt BURN_PAYLOAD_GROUP_ITEM* pPayloadGroupItem
);
static HRESULT AcquireContainerOrPayload(
__in BURN_CACHE_PROGRESS_CONTEXT* pProgress,
__out BOOL* pfRetry
);
static HRESULT LayoutOrCacheContainerOrPayload(
__in BURN_CACHE_CONTEXT* pContext,
__in_opt BURN_CONTAINER* pContainer,
__in_opt BURN_PACKAGE* pPackage,
__in_opt BURN_PAYLOAD_GROUP_ITEM* pPayloadGroupItem,
__in DWORD cTryAgainAttempts,
__out BOOL* pfRetry
);
static HRESULT PreparePayloadDestinationPath(
__in_z LPCWSTR wzDestinationPath
);
static HRESULT CopyPayload(
__in BURN_CACHE_PROGRESS_CONTEXT* pProgress,
__in HANDLE hSourceFile,
__in_z LPCWSTR wzSourcePath,
__in_z LPCWSTR wzDestinationPath
);
static HRESULT DownloadPayload(
__in BURN_CACHE_PROGRESS_CONTEXT* pProgress,
__in_z LPCWSTR wzDestinationPath
);
static HRESULT CompleteCacheProgress(
__in BURN_CACHE_PROGRESS_CONTEXT* pContext,
__in DWORD64 qwFileSize
);
static DWORD CALLBACK CacheProgressRoutine(
__in LARGE_INTEGER TotalFileSize,
__in LARGE_INTEGER TotalBytesTransferred,
__in LARGE_INTEGER StreamSize,
__in LARGE_INTEGER StreamBytesTransferred,
__in DWORD dwStreamNumber,
__in DWORD dwCallbackReason,
__in HANDLE hSourceFile,
__in HANDLE hDestinationFile,
__in_opt LPVOID lpData
);
static void DoRollbackCache(
__in BURN_USER_EXPERIENCE* pUX,
__in BURN_PLAN* pPlan,
__in HANDLE hPipe,
__in DWORD dwCheckpoint
);
static HRESULT DoExecuteAction(
__in BURN_ENGINE_STATE* pEngineState,
__in BURN_EXECUTE_ACTION* pExecuteAction,
__in_opt HANDLE hCacheThread,
__in BURN_EXECUTE_CONTEXT* pContext,
__inout BURN_ROLLBACK_BOUNDARY** ppRollbackBoundary,
__inout BURN_EXECUTE_ACTION_CHECKPOINT** ppCheckpoint,
__out BOOL* pfSuspend,
__out BOOTSTRAPPER_APPLY_RESTART* pRestart
);
static HRESULT DoRollbackActions(
__in BURN_ENGINE_STATE* pEngineState,
__in BURN_EXECUTE_CONTEXT* pContext,
__in DWORD dwCheckpoint,
__out BOOTSTRAPPER_APPLY_RESTART* pRestart
);
static HRESULT ExecuteExePackage(
__in BURN_ENGINE_STATE* pEngineState,
__in BURN_EXECUTE_ACTION* pExecuteAction,
__in BURN_EXECUTE_CONTEXT* pContext,
__in BOOL fRollback,
__out BOOL* pfRetry,
__out BOOL* pfSuspend,
__out BOOTSTRAPPER_APPLY_RESTART* pRestart
);
static HRESULT ExecuteMsiPackage(
__in BURN_ENGINE_STATE* pEngineState,
__in BURN_EXECUTE_ACTION* pExecuteAction,
__in BURN_EXECUTE_CONTEXT* pContext,
__in BOOL fInsideMsiTransaction,
__in BOOL fRollback,
__out BOOL* pfRetry,
__out BOOL* pfSuspend,
__out BOOTSTRAPPER_APPLY_RESTART* pRestart
);
static HRESULT ExecuteMspPackage(
__in BURN_ENGINE_STATE* pEngineState,
__in BURN_EXECUTE_ACTION* pExecuteAction,
__in BURN_EXECUTE_CONTEXT* pContext,
__in BOOL fInsideMsiTransaction,
__in BOOL fRollback,
__out BOOL* pfRetry,
__out BOOL* pfSuspend,
__out BOOTSTRAPPER_APPLY_RESTART* pRestart
);
static HRESULT ExecuteMsuPackage(
__in BURN_ENGINE_STATE* pEngineState,
__in BURN_EXECUTE_ACTION* pExecuteAction,
__in BURN_EXECUTE_CONTEXT* pContext,
__in BOOL fRollback,
__in BOOL fStopWusaService,
__out BOOL* pfRetry,
__out BOOL* pfSuspend,
__out BOOTSTRAPPER_APPLY_RESTART* pRestart
);
static HRESULT ExecutePackageProviderAction(
__in BURN_ENGINE_STATE* pEngineState,
__in BURN_EXECUTE_ACTION* pAction,
__in BURN_EXECUTE_CONTEXT* pContext
);
static HRESULT ExecuteDependencyAction(
__in BURN_ENGINE_STATE* pEngineState,
__in BURN_EXECUTE_ACTION* pAction,
__in BURN_EXECUTE_CONTEXT* pContext
);
static HRESULT ExecuteMsiBeginTransaction(
__in BURN_ENGINE_STATE* pEngineState,
__in BURN_ROLLBACK_BOUNDARY* pRollbackBoundary,
__in BURN_EXECUTE_CONTEXT* pContext
);
static HRESULT ExecuteMsiCommitTransaction(
__in BURN_ENGINE_STATE* pEngineState,
__in BURN_ROLLBACK_BOUNDARY* pRollbackBoundary,
__in BURN_EXECUTE_CONTEXT* pContext
);
static HRESULT ExecuteMsiRollbackTransaction(
__in BURN_ENGINE_STATE* pEngineState,
__in BURN_ROLLBACK_BOUNDARY* pRollbackBoundary,
__in BURN_EXECUTE_CONTEXT* pContext
);
static void ResetTransactionRegistrationState(
__in BURN_ENGINE_STATE* pEngineState,
__in BOOL fCommit
);
static HRESULT CleanPackage(
__in HANDLE hElevatedPipe,
__in BURN_PACKAGE* pPackage
);
static int GenericExecuteMessageHandler(
__in GENERIC_EXECUTE_MESSAGE* pMessage,
__in LPVOID pvContext
);
static int MsiExecuteMessageHandler(
__in WIU_MSI_EXECUTE_MESSAGE* pMessage,
__in_opt LPVOID pvContext
);
static HRESULT ReportOverallProgressTicks(
__in BURN_USER_EXPERIENCE* pUX,
__in BOOL fRollback,
__in DWORD cOverallProgressTicksTotal,
__in DWORD cOverallProgressTicks
);
static HRESULT ExecutePackageComplete(
__in BURN_USER_EXPERIENCE* pUX,
__in BURN_VARIABLES* pVariables,
__in BURN_PACKAGE* pPackage,
__in HRESULT hrOverall,
__in HRESULT hrExecute,
__in BOOL fRollback,
__out BOOTSTRAPPER_APPLY_RESTART* pRestart,
__out BOOL* pfRetry,
__out BOOL* pfSuspend
);
// function definitions
extern "C" void ApplyInitialize()
{
// Prevent the system from sleeping.
::SetThreadExecutionState(ES_CONTINUOUS | ES_SYSTEM_REQUIRED);
}
extern "C" void ApplyUninitialize()
{
::SetThreadExecutionState(ES_CONTINUOUS);
}
extern "C" HRESULT ApplySetVariables(
__in BURN_VARIABLES* pVariables
)
{
HRESULT hr = S_OK;
hr = VariableSetString(pVariables, BURN_BUNDLE_FORCED_RESTART_PACKAGE, NULL, TRUE, FALSE);
ExitOnFailure(hr, "Failed to set the bundle forced restart package built-in variable.");
LExit:
return hr;
}
extern "C" void ApplyReset(
__in BURN_USER_EXPERIENCE* pUX,
__in BURN_PACKAGES* pPackages
)
{
UserExperienceExecuteReset(pUX);
for (DWORD i = 0; i < pPackages->cPackages; ++i)
{
BURN_PACKAGE* pPackage = pPackages->rgPackages + i;
pPackage->hrCacheResult = S_OK;
pPackage->transactionRegistrationState = BURN_PACKAGE_REGISTRATION_STATE_UNKNOWN;
}
}
extern "C" HRESULT ApplyLock(
__in BOOL /*fPerMachine*/,
__out HANDLE* phLock
)
{
HRESULT hr = S_OK;
*phLock = NULL;
#if 0 // eventually figure out the correct way to support this. In its current form, embedded bundles (including related bundles) are hosed.
DWORD er = ERROR_SUCCESS;
HANDLE hLock = NULL;
hLock = ::CreateMutexW(NULL, TRUE, fPerMachine ? L"Global\\WixBurnExecutionLock" : L"Local\\WixBurnExecutionLock");
ExitOnNullWithLastError(hLock, hr, "Failed to create lock.");
er = ::GetLastError();
if (ERROR_ALREADY_EXISTS == er)
{
ExitFunction1(hr = HRESULT_FROM_WIN32(ERROR_INSTALL_ALREADY_RUNNING));
}
*phLock = hLock;
hLock = NULL;
LExit:
ReleaseHandle(hLock);
#endif
return hr;
}
extern "C" HRESULT ApplyRegister(
__in BURN_ENGINE_STATE* pEngineState
)
{
HRESULT hr = S_OK;
LPWSTR sczEngineWorkingPath = NULL;
hr = UserExperienceOnRegisterBegin(&pEngineState->userExperience);
ExitOnRootFailure(hr, "BA aborted register begin.");
// If we have a resume mode that suggests the bundle is on the machine.
if (BOOTSTRAPPER_RESUME_TYPE_REBOOT_PENDING < pEngineState->command.resumeType)
{
// resume previous session
if (pEngineState->registration.fPerMachine)
{
hr = ElevationSessionResume(pEngineState->companionConnection.hPipe, pEngineState->registration.sczResumeCommandLine, pEngineState->registration.fDisableResume, &pEngineState->variables);
ExitOnFailure(hr, "Failed to resume registration session in per-machine process.");
}
else
{
hr = RegistrationSessionResume(&pEngineState->registration, &pEngineState->variables);
ExitOnFailure(hr, "Failed to resume registration session.");
}
}
else // need to complete registration on the machine.
{
hr = CacheCalculateBundleWorkingPath(pEngineState->registration.sczId, pEngineState->registration.sczExecutableName, &sczEngineWorkingPath);
ExitOnFailure(hr, "Failed to calculate working path for engine.");
// begin new session
if (pEngineState->registration.fPerMachine)
{
hr = ElevationSessionBegin(pEngineState->companionConnection.hPipe, sczEngineWorkingPath, pEngineState->registration.sczResumeCommandLine, pEngineState->registration.fDisableResume, &pEngineState->variables, pEngineState->plan.dwRegistrationOperations, pEngineState->plan.dependencyRegistrationAction, pEngineState->plan.qwEstimatedSize);
ExitOnFailure(hr, "Failed to begin registration session in per-machine process.");
}
else
{
hr = RegistrationSessionBegin(sczEngineWorkingPath, &pEngineState->registration, &pEngineState->variables, pEngineState->plan.dwRegistrationOperations, pEngineState->plan.dependencyRegistrationAction, pEngineState->plan.qwEstimatedSize);
ExitOnFailure(hr, "Failed to begin registration session.");
}
}
// Apply any registration actions.
HRESULT hrExecuteRegistration = ExecuteDependentRegistrationActions(pEngineState->companionConnection.hPipe, &pEngineState->registration, pEngineState->plan.rgRegistrationActions, pEngineState->plan.cRegistrationActions);
UNREFERENCED_PARAMETER(hrExecuteRegistration);
// Try to save engine state.
hr = CoreSaveEngineState(pEngineState);
if (FAILED(hr))
{
LogErrorId(hr, MSG_STATE_NOT_SAVED);
hr = S_OK;
}
LExit:
UserExperienceOnRegisterComplete(&pEngineState->userExperience, hr);
ReleaseStr(sczEngineWorkingPath);
return hr;
}
extern "C" HRESULT ApplyUnregister(
__in BURN_ENGINE_STATE* pEngineState,
__in BOOL fFailedOrRollback,
__in BOOL fSuspend,
__in BOOTSTRAPPER_APPLY_RESTART restart
)
{
HRESULT hr = S_OK;
BURN_RESUME_MODE resumeMode = BURN_RESUME_MODE_NONE;
BOOL fKeepRegistration = pEngineState->plan.fDisallowRemoval;
CalculateKeepRegistration(pEngineState, &fKeepRegistration);
hr = UserExperienceOnUnregisterBegin(&pEngineState->userExperience, &fKeepRegistration);
ExitOnRootFailure(hr, "BA aborted unregister begin.");
// Calculate the correct resume mode. If a restart has been initiated, that trumps all other
// modes. If the user chose to suspend the install then we'll use that as the resume mode.
// Barring those special cases, if it was determined that we should keep the registration then
// do that, otherwise the resume mode was initialized to none and registration will be removed.
if (BOOTSTRAPPER_APPLY_RESTART_INITIATED == restart)
{
resumeMode = BURN_RESUME_MODE_REBOOT_PENDING;
}
else if (fSuspend)
{
resumeMode = BURN_RESUME_MODE_SUSPEND;
}
else if (fKeepRegistration)
{
resumeMode = BURN_RESUME_MODE_ARP;
}
// If apply failed in any way and we're going to be keeping the bundle registered then
// execute any rollback dependency registration actions.
if (fFailedOrRollback && fKeepRegistration)
{
// Execute any rollback registration actions.
HRESULT hrRegistrationRollback = ExecuteDependentRegistrationActions(pEngineState->companionConnection.hPipe, &pEngineState->registration, pEngineState->plan.rgRollbackRegistrationActions, pEngineState->plan.cRollbackRegistrationActions);
UNREFERENCED_PARAMETER(hrRegistrationRollback);
}
if (pEngineState->registration.fPerMachine)
{
hr = ElevationSessionEnd(pEngineState->companionConnection.hPipe, resumeMode, restart, pEngineState->plan.dependencyRegistrationAction);
ExitOnFailure(hr, "Failed to end session in per-machine process.");
}
else
{
hr = RegistrationSessionEnd(&pEngineState->registration, &pEngineState->variables, &pEngineState->packages, resumeMode, restart, pEngineState->plan.dependencyRegistrationAction);
ExitOnFailure(hr, "Failed to end session in per-user process.");
}
pEngineState->resumeMode = resumeMode;
LExit:
UserExperienceOnUnregisterComplete(&pEngineState->userExperience, hr);
return hr;
}
extern "C" HRESULT ApplyCache(
__in HANDLE hSourceEngineFile,
__in BURN_USER_EXPERIENCE* pUX,
__in BURN_VARIABLES* pVariables,
__in BURN_PLAN* pPlan,
__in HANDLE hPipe,
__inout DWORD* pcOverallProgressTicks,
__inout BOOL* pfRollback
)
{
HRESULT hr = S_OK;
DWORD dwCheckpoint = 0;
BURN_CACHE_CONTEXT cacheContext = { };
BURN_PACKAGE* pPackage = NULL;
*pfRollback = FALSE;
hr = UserExperienceOnCacheBegin(pUX);
ExitOnRootFailure(hr, "BA aborted cache.");
cacheContext.hSourceEngineFile = hSourceEngineFile;
cacheContext.pPayloads = pPlan->pPayloads;
cacheContext.pUX = pUX;
cacheContext.pVariables = pVariables;
cacheContext.qwTotalCacheSize = pPlan->qwCacheSizeTotal;
cacheContext.wzLayoutDirectory = pPlan->sczLayoutDirectory;
hr = MemAllocArray(reinterpret_cast<LPVOID*>(&cacheContext.rgSearchPaths), sizeof(LPWSTR), BURN_CACHE_MAX_SEARCH_PATHS);
ExitOnNull(cacheContext.rgSearchPaths, hr, E_OUTOFMEMORY, "Failed to allocate cache search paths array.");
for (DWORD i = 0; i < pPlan->cCacheActions; ++i)
{
BURN_CACHE_ACTION* pCacheAction = pPlan->rgCacheActions + i;
cacheContext.hPipe = hPipe;
pPackage = NULL;
switch (pCacheAction->type)
{
case BURN_CACHE_ACTION_TYPE_CHECKPOINT:
dwCheckpoint = pCacheAction->checkpoint.dwId;
break;
case BURN_CACHE_ACTION_TYPE_LAYOUT_BUNDLE:
hr = ApplyLayoutBundle(&cacheContext, pCacheAction->bundleLayout.pPayloadGroup, pCacheAction->bundleLayout.sczExecutableName, pCacheAction->bundleLayout.sczUnverifiedPath, pCacheAction->bundleLayout.qwBundleSize);
ExitOnFailure(hr, "Failed cache action: %ls", L"layout bundle");
++(*pcOverallProgressTicks);
hr = ReportOverallProgressTicks(pUX, FALSE, pPlan->cOverallProgressTicksTotal, *pcOverallProgressTicks);
LogExitOnFailure(hr, MSG_USER_CANCELED, "Cancel during cache: %ls", L"layout bundle");
break;
case BURN_CACHE_ACTION_TYPE_PACKAGE:
pPackage = pCacheAction->package.pPackage;
if (!pPackage->fPerMachine && !cacheContext.wzLayoutDirectory)
{
hr = CacheGetCompletedPath(FALSE, pPackage->sczCacheId, &pPackage->sczCacheFolder);
ExitOnFailure(hr, "Failed to get cached path for package with cache id: %ls", pPackage->sczCacheId);
cacheContext.hPipe = INVALID_HANDLE_VALUE;
}
hr = ApplyCachePackage(&cacheContext, pPackage);
ExitOnFailure(hr, "Failed cache action: %ls", L"cache package");
++(*pcOverallProgressTicks);
hr = ReportOverallProgressTicks(pUX, FALSE, pPlan->cOverallProgressTicksTotal, *pcOverallProgressTicks);
LogExitOnFailure(hr, MSG_USER_CANCELED, "Cancel during cache: %ls", L"cache package");
break;
case BURN_CACHE_ACTION_TYPE_CONTAINER:
Assert(pPlan->sczLayoutDirectory);
hr = ApplyLayoutContainer(&cacheContext, pCacheAction->container.pContainer);
ExitOnFailure(hr, "Failed cache action: %ls", L"layout container");
break;
case BURN_CACHE_ACTION_TYPE_SIGNAL_SYNCPOINT:
if (!::SetEvent(pCacheAction->syncpoint.hEvent))
{
ExitWithLastError(hr, "Failed to set syncpoint event.");
}
break;
default:
AssertSz(FALSE, "Unknown cache action.");
break;
}
}
LExit:
if (FAILED(hr))
{
DoRollbackCache(pUX, pPlan, hPipe, dwCheckpoint);
*pfRollback = TRUE;
}
// Clean up any remanents in the cache.
if (INVALID_HANDLE_VALUE != hPipe)
{
ElevationCacheCleanup(hPipe);
}
CacheCleanup(FALSE, pPlan->wzBundleId);
for (DWORD i = 0; i < cacheContext.cSearchPathsMax; ++i)
{
ReleaseNullStr(cacheContext.rgSearchPaths[i]);
}
ReleaseMem(cacheContext.rgSearchPaths);
ReleaseStr(cacheContext.sczLastUsedFolderCandidate);
UserExperienceOnCacheComplete(pUX, hr);
return hr;
}
extern "C" HRESULT ApplyExecute(
__in BURN_ENGINE_STATE* pEngineState,
__in_opt HANDLE hCacheThread,
__inout DWORD* pcOverallProgressTicks,
__out BOOL* pfRollback,
__out BOOL* pfSuspend,
__out BOOTSTRAPPER_APPLY_RESTART* pRestart
)
{
HRESULT hr = S_OK;
HRESULT hrRollback = S_OK;
BURN_EXECUTE_ACTION_CHECKPOINT* pCheckpoint = NULL;
BURN_EXECUTE_CONTEXT context = { };
BURN_ROLLBACK_BOUNDARY* pRollbackBoundary = NULL;
BOOL fSeekNextRollbackBoundary = FALSE;
context.pUX = &pEngineState->userExperience;
context.cExecutePackagesTotal = pEngineState->plan.cExecutePackagesTotal;
context.pcOverallProgressTicks = pcOverallProgressTicks;
*pfRollback = FALSE;
*pfSuspend = FALSE;
// Send execute begin to BA.
hr = UserExperienceOnExecuteBegin(&pEngineState->userExperience, pEngineState->plan.cExecutePackagesTotal);
ExitOnRootFailure(hr, "BA aborted execute begin.");
// Do execute actions.
for (DWORD i = 0; i < pEngineState->plan.cExecuteActions; ++i)
{
BURN_EXECUTE_ACTION* pExecuteAction = &pEngineState->plan.rgExecuteActions[i];
if (pExecuteAction->fDeleted)
{
continue;
}
// If we are seeking the next rollback boundary, skip if this action wasn't it.
if (fSeekNextRollbackBoundary)
{
if (BURN_EXECUTE_ACTION_TYPE_ROLLBACK_BOUNDARY == pExecuteAction->type)
{
continue;
}
else
{
fSeekNextRollbackBoundary = FALSE;
}
}
// Execute the action.
hr = DoExecuteAction(pEngineState, pExecuteAction, hCacheThread, &context, &pRollbackBoundary, &pCheckpoint, pfSuspend, pRestart);
if (*pfSuspend || BOOTSTRAPPER_APPLY_RESTART_INITIATED == *pRestart)
{
if (pCheckpoint && pCheckpoint->pActiveRollbackBoundary && pCheckpoint->pActiveRollbackBoundary->fActiveTransaction)
{
hr = E_INVALIDSTATE;
LogId(REPORT_ERROR, MSG_RESTART_REQUEST_DURING_MSI_TRANSACTION, pCheckpoint->pActiveRollbackBoundary->sczId);
}
else
{
ExitFunction();
}
}
if (FAILED(hr))
{
// If rollback is disabled, keep what we have and always end execution here.
if (pEngineState->plan.fDisableRollback)
{
LogId(REPORT_WARNING, MSG_PLAN_ROLLBACK_DISABLED);
if (pCheckpoint && pCheckpoint->pActiveRollbackBoundary && pCheckpoint->pActiveRollbackBoundary->fActiveTransaction)
{
hrRollback = ExecuteMsiCommitTransaction(pEngineState, pCheckpoint->pActiveRollbackBoundary, &context);
IgnoreRollbackError(hrRollback, "Failed commit transaction from disable rollback");
}
*pfRollback = TRUE;
break;
}
if (pCheckpoint)
{
// If inside a MSI transaction, roll it back.
if (pCheckpoint->pActiveRollbackBoundary && pCheckpoint->pActiveRollbackBoundary->fActiveTransaction)
{
hrRollback = ExecuteMsiRollbackTransaction(pEngineState, pCheckpoint->pActiveRollbackBoundary, &context);
IgnoreRollbackError(hrRollback, "Failed rolling back transaction");
}
// The action failed, roll back to previous rollback boundary.
hrRollback = DoRollbackActions(pEngineState, &context, pCheckpoint->dwId, pRestart);
IgnoreRollbackError(hrRollback, "Failed rollback actions");
}
// If the rollback boundary is vital, end execution here.
if (pRollbackBoundary && pRollbackBoundary->fVital)
{
*pfRollback = TRUE;
break;
}
// Move forward to next rollback boundary.
fSeekNextRollbackBoundary = TRUE;
}
}
LExit:
// Send execute complete to BA.
UserExperienceOnExecuteComplete(&pEngineState->userExperience, hr);
return hr;
}
extern "C" void ApplyClean(
__in BURN_USER_EXPERIENCE* /*pUX*/,
__in BURN_PLAN* pPlan,
__in HANDLE hPipe
)
{
HRESULT hr = S_OK;
for (DWORD i = 0; i < pPlan->cCleanActions; ++i)
{
BURN_CLEAN_ACTION* pCleanAction = pPlan->rgCleanActions + i;
BURN_PACKAGE* pPackage = pCleanAction->pPackage;
hr = CleanPackage(hPipe, pPackage);
}
}
// internal helper functions
static void CalculateKeepRegistration(
__in BURN_ENGINE_STATE* pEngineState,
__inout BOOL* pfKeepRegistration
)
{
LogId(REPORT_STANDARD, MSG_POST_APPLY_CALCULATE_REGISTRATION);
for (DWORD i = 0; i < pEngineState->packages.cPackages; ++i)
{
BURN_PACKAGE* pPackage = pEngineState->packages.rgPackages + i;
if (BURN_PACKAGE_TYPE_MSP == pPackage->type)
{
MspEngineFinalizeInstallRegistrationState(pPackage);
}
LogId(REPORT_STANDARD, MSG_POST_APPLY_PACKAGE, pPackage->sczId, LoggingPackageRegistrationStateToString(pPackage->fCanAffectRegistration, pPackage->installRegistrationState), LoggingPackageRegistrationStateToString(pPackage->fCanAffectRegistration, pPackage->cacheRegistrationState));
if (!pPackage->fCanAffectRegistration)
{
continue;
}
if (BURN_PACKAGE_REGISTRATION_STATE_PRESENT == pPackage->installRegistrationState ||
BURN_PACKAGE_REGISTRATION_STATE_PRESENT == pPackage->cacheRegistrationState)
{
*pfKeepRegistration = TRUE;
}
}
}
static HRESULT ExecuteDependentRegistrationActions(
__in HANDLE hPipe,
__in const BURN_REGISTRATION* pRegistration,
__in_ecount(cActions) const BURN_DEPENDENT_REGISTRATION_ACTION* rgActions,
__in DWORD cActions
)
{
HRESULT hr = S_OK;
for (DWORD iAction = 0; iAction < cActions; ++iAction)
{
const BURN_DEPENDENT_REGISTRATION_ACTION* pAction = rgActions + iAction;
if (pRegistration->fPerMachine)
{
hr = ElevationProcessDependentRegistration(hPipe, pAction);
ExitOnFailure(hr, "Failed to execute dependent registration action.");
}
else
{
hr = DependencyProcessDependentRegistration(pRegistration, pAction);
ExitOnFailure(hr, "Failed to process dependency registration action.");
}
}
LExit:
return hr;
}
static HRESULT ApplyCachePackage(
__in BURN_CACHE_CONTEXT* pContext,
__in BURN_PACKAGE* pPackage
)
{
HRESULT hr = S_OK;
BOOTSTRAPPER_CACHEPACKAGECOMPLETE_ACTION cachePackageCompleteAction = BOOTSTRAPPER_CACHEPACKAGECOMPLETE_ACTION_NONE;
for (;;)
{
hr = UserExperienceOnCachePackageBegin(pContext->pUX, pPackage->sczId, pPackage->payloads.cItems, pPackage->payloads.qwTotalSize);
LogExitOnFailure(hr, MSG_USER_CANCELED, "Cancel during cache: %ls: %ls", L"begin cache package", pPackage->sczId);
for (DWORD i = 0; i < pPackage->payloads.cItems; ++i)
{
BURN_PAYLOAD_GROUP_ITEM* pPayloadGroupItem = pPackage->payloads.rgItems + i;
hr = ApplyProcessPayload(pContext, pPackage, pPayloadGroupItem);
if (FAILED(hr))
{
break;
}
}
pPackage->hrCacheResult = hr;
cachePackageCompleteAction = SUCCEEDED(hr) || pPackage->fVital ? BOOTSTRAPPER_CACHEPACKAGECOMPLETE_ACTION_NONE : BOOTSTRAPPER_CACHEPACKAGECOMPLETE_ACTION_IGNORE;
UserExperienceOnCachePackageComplete(pContext->pUX, pPackage->sczId, hr, &cachePackageCompleteAction);
if (SUCCEEDED(hr))
{
break;
}
if (BOOTSTRAPPER_CACHEPACKAGECOMPLETE_ACTION_RETRY == cachePackageCompleteAction)
{
for (DWORD i = 0; i < pPackage->payloads.cItems; ++i)
{
BURN_PAYLOAD_GROUP_ITEM* pItem = pPackage->payloads.rgItems + i;
if (pItem->fCached)
{
pItem->pPayload->cRemainingInstances += 1;
pItem->fCached = FALSE;
}
if (pItem->qwCommittedCacheProgress)
{
pContext->qwSuccessfulCacheProgress -= pItem->qwCommittedCacheProgress;
pItem->qwCommittedCacheProgress = 0;
}
}
LogErrorId(hr, MSG_APPLY_RETRYING_PACKAGE, pPackage->sczId, NULL, NULL);
continue;
}
else if (BOOTSTRAPPER_CACHEPACKAGECOMPLETE_ACTION_IGNORE == cachePackageCompleteAction && !pPackage->fVital) // ignore non-vital download failures.
{
LogId(REPORT_STANDARD, MSG_APPLY_CONTINUING_NONVITAL_PACKAGE, pPackage->sczId, hr);
hr = S_OK;
}
break;
}
LExit:
return hr;
}
static HRESULT ApplyExtractContainer(
__in BURN_CACHE_CONTEXT* pContext,
__in BURN_CONTAINER* pContainer
)
{
HRESULT hr = S_OK;
if (pContainer->qwCommittedCacheProgress)
{
pContext->qwSuccessfulCacheProgress -= pContainer->qwCommittedCacheProgress;
pContainer->qwCommittedCacheProgress = 0;
}
if (pContainer->qwCommittedExtractProgress)
{
pContext->qwSuccessfulCacheProgress -= pContainer->qwCommittedExtractProgress;
pContainer->qwCommittedExtractProgress = 0;
}
if (!pContainer->fActuallyAttached)
{
hr = ApplyAcquireContainerOrPayload(pContext, pContainer, NULL, NULL);
LogExitOnFailure(hr, MSG_FAILED_ACQUIRE_CONTAINER, "Failed to acquire container: %ls to working path: %ls", pContainer->sczId, pContainer->sczUnverifiedPath);
}
hr = ExtractContainer(pContext, pContainer);
LogExitOnFailure(hr, MSG_FAILED_EXTRACT_CONTAINER, "Failed to extract payloads from container: %ls to working path: %ls", pContainer->sczId, pContainer->sczUnverifiedPath);
if (pContext->sczLastUsedFolderCandidate)
{
// We successfully copied from a source location, set that as the last used source.
CacheSetLastUsedSource(pContext->pVariables, pContext->sczLastUsedFolderCandidate, pContainer->sczFilePath);
}
if (pContainer->qwExtractSizeTotal < pContainer->qwCommittedExtractProgress)
{
AssertSz(FALSE, "Container extracted more than planned.");
pContext->qwSuccessfulCacheProgress -= pContainer->qwCommittedExtractProgress;
pContext->qwSuccessfulCacheProgress += pContainer->qwExtractSizeTotal;
}
else
{
pContext->qwSuccessfulCacheProgress += pContainer->qwExtractSizeTotal - pContainer->qwCommittedExtractProgress;
}
pContainer->qwCommittedExtractProgress = pContainer->qwExtractSizeTotal;
LExit:
ReleaseNullStr(pContext->sczLastUsedFolderCandidate);
return hr;
}
static HRESULT ApplyLayoutBundle(
__in BURN_CACHE_CONTEXT* pContext,
__in BURN_PAYLOAD_GROUP* pPayloads,
__in_z LPCWSTR wzExecutableName,
__in_z LPCWSTR wzUnverifiedPath,
__in DWORD64 qwBundleSize
)
{
HRESULT hr = S_OK;
hr = LayoutBundle(pContext, wzExecutableName, wzUnverifiedPath, qwBundleSize);
ExitOnFailure(hr, "Failed to layout bundle.");
for (DWORD i = 0; i < pPayloads->cItems; ++i)
{
BURN_PAYLOAD_GROUP_ITEM* pPayloadGroupItem = pPayloads->rgItems + i;
hr = ApplyProcessPayload(pContext, NULL, pPayloadGroupItem);
ExitOnFailure(hr, "Failed to layout bundle payload: %ls", pPayloadGroupItem->pPayload->sczKey);
}
LExit:
return hr;
}
static HRESULT ApplyLayoutContainer(
__in BURN_CACHE_CONTEXT* pContext,
__in BURN_CONTAINER* pContainer
)
{
HRESULT hr = S_OK;
DWORD cTryAgainAttempts = 0;
BOOL fRetry = FALSE;
Assert(!pContainer->fAttached);
hr = ApplyCacheVerifyContainerOrPayload(pContext, pContainer, NULL, NULL);
if (SUCCEEDED(hr))
{
ExitFunction();
}
for (;;)
{
fRetry = FALSE;
hr = ApplyAcquireContainerOrPayload(pContext, pContainer, NULL, NULL);
LogExitOnFailure(hr, MSG_FAILED_ACQUIRE_CONTAINER, "Failed to acquire container: %ls to working path: %ls", pContainer->sczId, pContainer->sczUnverifiedPath);
hr = LayoutOrCacheContainerOrPayload(pContext, pContainer, NULL, NULL, cTryAgainAttempts, &fRetry);
if (SUCCEEDED(hr))
{
break;
}
else
{
LogErrorId(hr, MSG_FAILED_LAYOUT_CONTAINER, pContainer->sczId, pContext->wzLayoutDirectory, pContainer->sczUnverifiedPath);
if (!fRetry)
{
ExitFunction();
}
++cTryAgainAttempts;
pContext->qwSuccessfulCacheProgress -= pContainer->qwFileSize;
ReleaseNullStr(pContext->sczLastUsedFolderCandidate);
LogErrorId(hr, MSG_APPLY_RETRYING_CONTAINER, pContainer->sczId, NULL, NULL);
}
}
LExit:
ReleaseNullStr(pContext->sczLastUsedFolderCandidate);
return hr;
}
static HRESULT ApplyProcessPayload(
__in BURN_CACHE_CONTEXT* pContext,
__in_opt BURN_PACKAGE* pPackage,
__in BURN_PAYLOAD_GROUP_ITEM* pPayloadGroupItem
)