-
Notifications
You must be signed in to change notification settings - Fork 1.5k
/
Copy pathInstallFlow.cpp
966 lines (859 loc) · 43 KB
/
InstallFlow.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
// Copyright (c) Microsoft Corporation.
// Licensed under the MIT License.
#include "pch.h"
#include "InstallFlow.h"
#include "DownloadFlow.h"
#include "UninstallFlow.h"
#include "UpdateFlow.h"
#include "ResumeFlow.h"
#include "ShowFlow.h"
#include "Resources.h"
#include "ShellExecuteInstallerHandler.h"
#include "MSStoreInstallerHandler.h"
#include "MsiInstallFlow.h"
#include "ArchiveFlow.h"
#include "PortableFlow.h"
#include "WorkflowBase.h"
#include "DependenciesFlow.h"
#include "PromptFlow.h"
#include "SourceFlow.h"
#include <AppInstallerMsixInfo.h>
#include <AppInstallerDeployment.h>
#include <AppInstallerSynchronization.h>
#include <Argument.h>
#include <Command.h>
#include <winget/ARPCorrelation.h>
#include <winget/Archive.h>
#include <winget/PathVariable.h>
#include <winget/Runtime.h>
using namespace winrt::Windows::Foundation;
using namespace winrt::Windows::Foundation::Collections;
using namespace winrt::Windows::Management::Deployment;
using namespace AppInstaller::CLI::Execution;
using namespace AppInstaller::Manifest;
using namespace AppInstaller::Repository;
using namespace AppInstaller::Registry::Environment;
using namespace AppInstaller::Settings;
using namespace AppInstaller::Utility;
using namespace AppInstaller::Utility::literals;
namespace AppInstaller::CLI::Workflow
{
namespace
{
bool MightWriteToARP(InstallerTypeEnum type)
{
switch (type)
{
case InstallerTypeEnum::Exe:
case InstallerTypeEnum::Burn:
case InstallerTypeEnum::Inno:
case InstallerTypeEnum::Msi:
case InstallerTypeEnum::Nullsoft:
case InstallerTypeEnum::Wix:
return true;
default:
return false;
}
}
bool ShouldUseDirectMSIInstall(InstallerTypeEnum type, bool isSilentInstall)
{
switch (type)
{
case InstallerTypeEnum::Msi:
case InstallerTypeEnum::Wix:
return isSilentInstall || ExperimentalFeature::IsEnabled(ExperimentalFeature::Feature::DirectMSI);
default:
return false;
}
}
bool ShouldErrorForUnsupportedArgument(UnsupportedArgumentEnum arg)
{
switch (arg)
{
case UnsupportedArgumentEnum::Location:
return true;
default:
return false;
}
}
Execution::Args::Type GetUnsupportedArgumentType(UnsupportedArgumentEnum unsupportedArgument)
{
Execution::Args::Type execArg;
switch (unsupportedArgument)
{
case UnsupportedArgumentEnum::Log:
execArg = Execution::Args::Type::Log;
break;
case UnsupportedArgumentEnum::Location:
execArg = Execution::Args::Type::InstallLocation;
break;
default:
THROW_HR(E_UNEXPECTED);
}
return execArg;
}
struct ExpectedReturnCode
{
ExpectedReturnCode(ExpectedReturnCodeEnum installerReturnCode, HRESULT hr, Resource::StringId message) :
InstallerReturnCode(installerReturnCode), HResult(hr), Message(message) {}
static ExpectedReturnCode GetExpectedReturnCode(ExpectedReturnCodeEnum returnCode)
{
switch (returnCode)
{
case ExpectedReturnCodeEnum::PackageInUse:
return ExpectedReturnCode(returnCode, APPINSTALLER_CLI_ERROR_INSTALL_PACKAGE_IN_USE, Resource::String::InstallFlowReturnCodePackageInUse);
case ExpectedReturnCodeEnum::PackageInUseByApplication:
return ExpectedReturnCode(returnCode, APPINSTALLER_CLI_ERROR_INSTALL_PACKAGE_IN_USE_BY_APPLICATION, Resource::String::InstallFlowReturnCodePackageInUseByApplication);
case ExpectedReturnCodeEnum::InstallInProgress:
return ExpectedReturnCode(returnCode, APPINSTALLER_CLI_ERROR_INSTALL_INSTALL_IN_PROGRESS, Resource::String::InstallFlowReturnCodeInstallInProgress);
case ExpectedReturnCodeEnum::FileInUse:
return ExpectedReturnCode(returnCode, APPINSTALLER_CLI_ERROR_INSTALL_FILE_IN_USE, Resource::String::InstallFlowReturnCodeFileInUse);
case ExpectedReturnCodeEnum::MissingDependency:
return ExpectedReturnCode(returnCode, APPINSTALLER_CLI_ERROR_INSTALL_MISSING_DEPENDENCY, Resource::String::InstallFlowReturnCodeMissingDependency);
case ExpectedReturnCodeEnum::DiskFull:
return ExpectedReturnCode(returnCode, APPINSTALLER_CLI_ERROR_INSTALL_DISK_FULL, Resource::String::InstallFlowReturnCodeDiskFull);
case ExpectedReturnCodeEnum::InsufficientMemory:
return ExpectedReturnCode(returnCode, APPINSTALLER_CLI_ERROR_INSTALL_INSUFFICIENT_MEMORY, Resource::String::InstallFlowReturnCodeInsufficientMemory);
case ExpectedReturnCodeEnum::InvalidParameter:
return ExpectedReturnCode(returnCode, APPINSTALLER_CLI_ERROR_INSTALL_INVALID_PARAMETER, Resource::String::InstallFlowReturnCodeInvalidParameter);
case ExpectedReturnCodeEnum::NoNetwork:
return ExpectedReturnCode(returnCode, APPINSTALLER_CLI_ERROR_INSTALL_NO_NETWORK, Resource::String::InstallFlowReturnCodeNoNetwork);
case ExpectedReturnCodeEnum::ContactSupport:
return ExpectedReturnCode(returnCode, APPINSTALLER_CLI_ERROR_INSTALL_CONTACT_SUPPORT, Resource::String::InstallFlowReturnCodeContactSupport);
case ExpectedReturnCodeEnum::RebootRequiredToFinish:
return ExpectedReturnCode(returnCode, APPINSTALLER_CLI_ERROR_INSTALL_REBOOT_REQUIRED_TO_FINISH, Resource::String::InstallFlowReturnCodeRebootRequiredToFinish);
case ExpectedReturnCodeEnum::RebootRequiredForInstall:
return ExpectedReturnCode(returnCode, APPINSTALLER_CLI_ERROR_INSTALL_REBOOT_REQUIRED_FOR_INSTALL, Resource::String::InstallFlowReturnCodeRebootRequiredForInstall);
case ExpectedReturnCodeEnum::RebootInitiated:
return ExpectedReturnCode(returnCode, APPINSTALLER_CLI_ERROR_INSTALL_REBOOT_INITIATED, Resource::String::InstallFlowReturnCodeRebootInitiated);
case ExpectedReturnCodeEnum::CancelledByUser:
return ExpectedReturnCode(returnCode, APPINSTALLER_CLI_ERROR_INSTALL_CANCELLED_BY_USER, Resource::String::InstallFlowReturnCodeCancelledByUser);
case ExpectedReturnCodeEnum::AlreadyInstalled:
return ExpectedReturnCode(returnCode, APPINSTALLER_CLI_ERROR_INSTALL_ALREADY_INSTALLED, Resource::String::InstallFlowReturnCodeAlreadyInstalled);
case ExpectedReturnCodeEnum::Downgrade:
return ExpectedReturnCode(returnCode, APPINSTALLER_CLI_ERROR_INSTALL_DOWNGRADE, Resource::String::InstallFlowReturnCodeDowngrade);
case ExpectedReturnCodeEnum::BlockedByPolicy:
return ExpectedReturnCode(returnCode, APPINSTALLER_CLI_ERROR_INSTALL_BLOCKED_BY_POLICY, Resource::String::InstallFlowReturnCodeBlockedByPolicy);
case ExpectedReturnCodeEnum::SystemNotSupported:
return ExpectedReturnCode(returnCode, APPINSTALLER_CLI_ERROR_INSTALL_SYSTEM_NOT_SUPPORTED, Resource::String::InstallFlowReturnCodeSystemNotSupported);
case ExpectedReturnCodeEnum::Custom:
return ExpectedReturnCode(returnCode, APPINSTALLER_CLI_ERROR_INSTALL_CUSTOM_ERROR, Resource::String::InstallFlowReturnCodeCustomError);
default:
THROW_HR(E_UNEXPECTED);
}
}
ExpectedReturnCodeEnum InstallerReturnCode;
HRESULT HResult;
Resource::StringId Message;
};
}
namespace details
{
// Runs the installer via ShellExecute.
// Required Args: None
// Inputs: Installer, InstallerPath
// Outputs: None
void ShellExecuteInstall(Execution::Context& context)
{
context <<
GetInstallerArgs <<
ShellExecuteInstallImpl <<
ReportInstallerResult("ShellExecute"sv, APPINSTALLER_CLI_ERROR_SHELLEXEC_INSTALL_FAILED);
}
// Runs an MSI installer directly via MSI APIs.
// Required Args: None
// Inputs: Installer, InstallerPath
// Outputs: None
void DirectMSIInstall(Execution::Context& context)
{
context <<
GetInstallerArgs <<
DirectMSIInstallImpl <<
ReportInstallerResult("MsiInstallProduct"sv, APPINSTALLER_CLI_ERROR_MSI_INSTALL_FAILED);
}
// Deploys the MSIX.
// Required Args: None
// Inputs: Manifest?, Installer || InstallerPath
// Outputs: None
void MsixInstall(Execution::Context& context)
{
std::string uri;
Deployment::Options deploymentOptions;
if (context.Contains(Execution::Data::InstallerPath))
{
uri = context.Get<Execution::Data::InstallerPath>().u8string();
}
else
{
uri = context.Get<Execution::Data::Installer>()->Url;
deploymentOptions.ExpectedDigests = context.Get<Execution::Data::MsixDigests>();
}
deploymentOptions.SkipReputationCheck = WI_IsFlagSet(context.GetFlags(), Execution::ContextFlag::InstallerTrusted);
bool isMachineScope = Manifest::ConvertToScopeEnum(context.Args.GetArg(Execution::Args::Type::InstallScope)) == Manifest::ScopeEnum::Machine;
// TODO: There was a bug in deployment api if provision api was called in packaged context.
// Remove this check when the OS bug is fixed and back ported.
if (isMachineScope && Runtime::IsRunningInPackagedContext())
{
context.Reporter.Error() << Resource::String::InstallFlowReturnCodeSystemNotSupported << std::endl;
context.Add<Execution::Data::OperationReturnCode>(static_cast<DWORD>(APPINSTALLER_CLI_ERROR_INSTALL_SYSTEM_NOT_SUPPORTED));
AICLI_LOG(CLI, Error, << "Device wide install for msix type is not supported in packaged context.");
AICLI_TERMINATE_CONTEXT(APPINSTALLER_CLI_ERROR_INSTALL_SYSTEM_NOT_SUPPORTED);
}
context.Reporter.Info() << Resource::String::InstallFlowStartingPackageInstall << std::endl;
bool registrationDeferred = false;
try
{
registrationDeferred = context.Reporter.ExecuteWithProgress([&](IProgressCallback& callback)
{
if (isMachineScope)
{
return Deployment::AddPackageMachineScope(uri, deploymentOptions, callback);
}
else
{
return Deployment::AddPackageWithDeferredFallback(uri, deploymentOptions, callback);
}
});
}
catch (const wil::ResultException& re)
{
context.Add<Execution::Data::OperationReturnCode>(re.GetErrorCode());
context << ReportInstallerResult("MSIX"sv, re.GetErrorCode(), /* isHResult */ true);
return;
}
if (registrationDeferred)
{
context.Reporter.Warn() << Resource::String::InstallFlowRegistrationDeferred << std::endl;
}
else
{
context.Reporter.Info() << Resource::String::InstallFlowInstallSuccess << std::endl;
}
}
// Runs the flow for installing a Portable package.
// Required Args: None
// Inputs: Installer, InstallerPath
// Outputs: None
void PortableInstall(Execution::Context& context)
{
context <<
InitializePortableInstaller <<
VerifyPackageAndSourceMatch <<
PortableInstallImpl <<
ReportInstallerResult("Portable"sv, APPINSTALLER_CLI_ERROR_PORTABLE_INSTALL_FAILED, true);
}
// Runs the flow for installing a package from an archive.
// Required Args: None
// Inputs: Installer, InstallerPath, Manifest
// Outputs: None
void ArchiveInstall(Execution::Context& context)
{
context <<
ScanArchiveFromLocalManifest <<
ExtractFilesFromArchive <<
VerifyAndSetNestedInstaller <<
ExecuteInstallerForType(context.Get<Execution::Data::Installer>().value().NestedInstallerType);
}
}
bool ExemptFromSingleInstallLocking(InstallerTypeEnum type)
{
switch (type)
{
// MSStore installs are always MSIX based; MSIX installs are safe to run in parallel.
case InstallerTypeEnum::Msix:
case InstallerTypeEnum::MSStore:
return true;
default:
return false;
}
}
void EnsureApplicableInstaller(Execution::Context& context)
{
const auto& installer = context.Get<Execution::Data::Installer>();
if (!installer.has_value())
{
context.Reporter.Error() << Resource::String::NoApplicableInstallers << std::endl;
AICLI_TERMINATE_CONTEXT(APPINSTALLER_CLI_ERROR_NO_APPLICABLE_INSTALLER);
}
context <<
EnsureSupportForDownload <<
EnsureSupportForInstall;
}
void CheckForUnsupportedArgs(Execution::Context& context)
{
bool messageDisplayed = false;
const auto& unsupportedArgs = context.Get<Execution::Data::Installer>()->UnsupportedArguments;
for (auto unsupportedArg : unsupportedArgs)
{
const auto& unsupportedArgType = GetUnsupportedArgumentType(unsupportedArg);
if (context.Args.Contains(unsupportedArgType))
{
if (!messageDisplayed)
{
context.Reporter.Warn() << Resource::String::UnsupportedArgument << std::endl;
messageDisplayed = true;
}
const auto& executingCommand = context.GetExecutingCommand();
if (executingCommand != nullptr)
{
const auto& commandArguments = executingCommand->GetArguments();
for (const auto& argument : commandArguments)
{
if (unsupportedArgType == argument.ExecArgType())
{
const auto& usageString = argument.GetUsageString();
if (ShouldErrorForUnsupportedArgument(unsupportedArg))
{
context.Reporter.Error() << usageString << std::endl;
AICLI_TERMINATE_CONTEXT(APPINSTALLER_CLI_ERROR_UNSUPPORTED_ARGUMENT);
}
else
{
context.Reporter.Warn() << usageString << std::endl;
break;
}
}
}
}
}
}
}
void ShowInstallationDisclaimer(Execution::Context& context)
{
auto installerType = context.Get<Execution::Data::Installer>().value().EffectiveInstallerType();
if (installerType == InstallerTypeEnum::MSStore)
{
context.Reporter.Info() << Execution::PromptEmphasis << Resource::String::InstallationDisclaimerMSStore << std::endl;
}
else
{
context.Reporter.Info() <<
Resource::String::InstallationDisclaimer1 << std::endl <<
Resource::String::InstallationDisclaimer2 << std::endl;
}
}
void DisplayInstallationNotes(Execution::Context& context)
{
if (!Settings::User().Get<Settings::Setting::DisableInstallNotes>())
{
const auto& manifest = context.Get<Execution::Data::Manifest>();
auto installationNotes = manifest.CurrentLocalization.Get<AppInstaller::Manifest::Localization::InstallationNotes>();
if (!installationNotes.empty())
{
context.Reporter.Info() << Resource::String::Notes(installationNotes) << std::endl;
}
}
}
void ExecuteInstallerForType::operator()(Execution::Context& context) const
{
bool isUpdate = WI_IsFlagSet(context.GetFlags(), Execution::ContextFlag::InstallerExecutionUseUpdate);
UpdateBehaviorEnum updateBehavior = context.Get<Execution::Data::Installer>().value().UpdateBehavior;
bool doUninstallPrevious = isUpdate && (updateBehavior == UpdateBehaviorEnum::UninstallPrevious || context.Args.Contains(Execution::Args::Type::UninstallPrevious));
Synchronization::CrossProcessInstallLock lock;
if (!ExemptFromSingleInstallLocking(m_installerType))
{
// Acquire install lock; if the operation is cancelled it will return false so we will also return.
if (!context.Reporter.ExecuteWithProgress([&](IProgressCallback& callback)
{
callback.SetProgressMessage(Resource::String::InstallWaitingOnAnother());
return lock.Acquire(callback);
}))
{
AICLI_LOG(CLI, Info, << "Abandoning attempt to acquire install lock due to cancellation");
return;
}
}
switch (m_installerType)
{
case InstallerTypeEnum::Exe:
case InstallerTypeEnum::Burn:
case InstallerTypeEnum::Inno:
case InstallerTypeEnum::Msi:
case InstallerTypeEnum::Nullsoft:
case InstallerTypeEnum::Wix:
if (doUninstallPrevious)
{
context <<
GetUninstallInfo <<
ExecuteUninstaller;
context.ClearFlags(Execution::ContextFlag::InstallerExecutionUseUpdate);
}
if (ShouldUseDirectMSIInstall(m_installerType, context.Args.Contains(Execution::Args::Type::Silent)))
{
context << details::DirectMSIInstall;
}
else
{
context << details::ShellExecuteInstall;
}
break;
case InstallerTypeEnum::Msix:
context << details::MsixInstall;
break;
case InstallerTypeEnum::MSStore:
context <<
EnsureStorePolicySatisfied <<
(isUpdate ? MSStoreUpdate : MSStoreInstall);
break;
case InstallerTypeEnum::Portable:
if (doUninstallPrevious)
{
context <<
GetUninstallInfo <<
ExecuteUninstaller;
context.ClearFlags(Execution::ContextFlag::InstallerExecutionUseUpdate);
}
context << details::PortableInstall;
break;
case InstallerTypeEnum::Zip:
context << details::ArchiveInstall;
break;
default:
THROW_HR(HRESULT_FROM_WIN32(ERROR_NOT_SUPPORTED));
}
}
void EnsureRunningAsAdminForMachineScopeInstall(Execution::Context& context)
{
// Admin is required for machine scope install for installer types like portable, msix and msstore.
auto installerType = context.Get<Execution::Data::Installer>().value().EffectiveInstallerType();
if (Manifest::DoesInstallerTypeRequireAdminForMachineScopeInstall(installerType))
{
Manifest::ScopeEnum scope = ConvertToScopeEnum(context.Args.GetArg(Execution::Args::Type::InstallScope));
if (scope == Manifest::ScopeEnum::Machine)
{
context << Workflow::EnsureRunningAsAdmin;
}
}
}
void ExecuteInstaller(Execution::Context& context)
{
context << Workflow::ExecuteInstallerForType(context.Get<Execution::Data::Installer>().value().BaseInstallerType);
}
void ReportInstallerResult::operator()(Execution::Context& context) const
{
bool isRepair = WI_IsFlagSet(context.GetFlags(), Execution::ContextFlag::InstallerExecutionUseRepair);
DWORD installResult = context.Get<Execution::Data::OperationReturnCode>();
const auto& additionalSuccessCodes = context.Get<Execution::Data::Installer>()->InstallerSuccessCodes;
if (installResult != 0 && (std::find(additionalSuccessCodes.begin(), additionalSuccessCodes.end(), installResult) == additionalSuccessCodes.end()))
{
HRESULT terminationHR = m_hr;
const auto& expectedReturnCodes = context.Get<Execution::Data::Installer>()->ExpectedReturnCodes;
auto expectedReturnCodeItr = expectedReturnCodes.find(installResult);
if (expectedReturnCodeItr != expectedReturnCodes.end() && expectedReturnCodeItr->second.ReturnResponseEnum != ExpectedReturnCodeEnum::Unknown)
{
auto returnCode = ExpectedReturnCode::GetExpectedReturnCode(expectedReturnCodeItr->second.ReturnResponseEnum);
terminationHR = returnCode.HResult;
switch (terminationHR)
{
case APPINSTALLER_CLI_ERROR_INSTALL_REBOOT_REQUIRED_TO_FINISH:
// REBOOT_REQUIRED_TO_FINISH is treated as a success since installation has completed but is pending a reboot.
context.SetFlags(ContextFlag::RebootRequired);
context.Reporter.Warn() << returnCode.Message << std::endl;
terminationHR = S_OK;
break;
case APPINSTALLER_CLI_ERROR_INSTALL_REBOOT_REQUIRED_FOR_INSTALL:
// REBOOT_REQUIRED_FOR_INSTALL is treated as an error since installation has not yet completed.
context.SetFlags(ContextFlag::RebootRequired);
// TODO: Add separate workflow to handle restart registration for resume.
context.SetFlags(ContextFlag::RegisterResume);
break;
}
if (FAILED(terminationHR))
{
context.Reporter.Error() << returnCode.Message << std::endl;
const auto& returnResponseUrl = expectedReturnCodeItr->second.ReturnResponseUrl;
if (!returnResponseUrl.empty())
{
context.Reporter.Error() << Resource::String::RelatedLink << ' ' << returnResponseUrl << std::endl;
}
}
}
if (FAILED(terminationHR))
{
const auto& manifest = context.Get<Execution::Data::Manifest>();
if (isRepair)
{
Logging::Telemetry().LogRepairFailure(manifest.Id, manifest.Version, m_installerType, installResult);
}
else
{
Logging::Telemetry().LogInstallerFailure(manifest.Id, manifest.Version, manifest.Channel, m_installerType, installResult);
}
if (m_isHResult)
{
context.Reporter.Error()
<< Resource::String::InstallerFailedWithCode(Utility::LocIndView{ GetUserPresentableMessage(installResult) })
<< std::endl;
}
else
{
context.Reporter.Error()
<< Resource::String::InstallerFailedWithCode(installResult)
<< std::endl;
}
// Show installer log path if exists
if (context.Contains(Execution::Data::LogPath) && std::filesystem::exists(context.Get<Execution::Data::LogPath>()))
{
auto installerLogPath = Utility::LocIndString{ context.Get<Execution::Data::LogPath>().u8string() };
context.Reporter.Info() << Resource::String::InstallerLogAvailable(installerLogPath) << std::endl;
}
AICLI_TERMINATE_CONTEXT(terminationHR);
}
}
else
{
if (isRepair)
{
context.Reporter.Info() << Resource::String::RepairFlowRepairSuccess << std::endl;
}
else
{
context.Reporter.Info() << Resource::String::InstallFlowInstallSuccess << std::endl;
}
}
}
void ReportIdentityAndInstallationDisclaimer(Execution::Context& context)
{
context <<
Workflow::ReportManifestIdentityWithVersion() <<
Workflow::ShowInstallationDisclaimer;
}
void InstallPackageInstaller(Execution::Context& context)
{
context <<
Workflow::ReportExecutionStage(ExecutionStage::PreExecution) <<
Workflow::SnapshotARPEntries <<
Workflow::ReportExecutionStage(ExecutionStage::Execution) <<
Workflow::ExecuteInstaller <<
Workflow::ReportExecutionStage(ExecutionStage::PostExecution) <<
Workflow::ReportARPChanges <<
Workflow::RecordInstall <<
Workflow::ForceInstalledCacheUpdate <<
Workflow::RemoveInstaller <<
Workflow::DisplayInstallationNotes;
}
void InstallDependencies(Execution::Context& context)
{
if (Settings::User().Get<Settings::Setting::InstallSkipDependencies>() || context.Args.Contains(Execution::Args::Type::SkipDependencies))
{
context.Reporter.Warn() << Resource::String::DependenciesSkippedMessage << std::endl;
return;
}
context <<
Workflow::GetDependenciesFromInstaller <<
Workflow::ReportDependencies(Resource::String::PackageRequiresDependencies) <<
Workflow::EnableWindowsFeaturesDependencies <<
Workflow::ProcessMultiplePackages(Resource::String::PackageRequiresDependencies, APPINSTALLER_CLI_ERROR_INSTALL_DEPENDENCIES, {}, true, true, true, true);
}
void DownloadPackageDependencies(Execution::Context& context)
{
if (Settings::User().Get<Settings::Setting::InstallSkipDependencies>() || context.Args.Contains(Execution::Args::Type::SkipDependencies))
{
context.Reporter.Warn() << Resource::String::DependenciesSkippedMessage << std::endl;
return;
}
context <<
Workflow::GetDependenciesFromInstaller <<
Workflow::ReportDependencies(Resource::String::PackageRequiresDependencies) <<
Workflow::CreateDependencySubContexts(Resource::String::PackageRequiresDependencies) <<
Workflow::ProcessMultiplePackages(Resource::String::PackageRequiresDependencies, APPINSTALLER_CLI_ERROR_DOWNLOAD_DEPENDENCIES, {}, true, true, true, false);
}
void InstallSinglePackage(Execution::Context& context)
{
context <<
Workflow::CheckForUnsupportedArgs <<
Workflow::ReportIdentityAndInstallationDisclaimer <<
Workflow::ShowPromptsForSinglePackage(/* ensureAcceptance */ true) <<
Workflow::CreateDependencySubContexts(Resource::String::PackageRequiresDependencies) <<
Workflow::InstallDependencies <<
Workflow::DownloadInstaller <<
Workflow::InstallPackageInstaller <<
Workflow::RegisterStartupAfterReboot();
}
void EnsureSupportForInstall(Execution::Context& context)
{
if (WI_IsFlagSet(context.GetFlags(), Execution::ContextFlag::InstallerDownloadOnly))
{
return;
}
const auto& installer = context.Get<Execution::Data::Installer>();
// This check is only necessary for the Repair workflow when operating on an installer with RepairBehavior set to Installer.
if (WI_IsFlagSet(context.GetFlags(), Execution::ContextFlag::InstallerExecutionUseRepair))
{
if (installer->RepairBehavior != RepairBehaviorEnum::Installer)
{
return;
}
// At present, the installer repair behavior scenario is restricted to Exe, Inno, Nullsoft, and Burn installer types.
if (!DoesInstallerTypeRequireRepairBehaviorForRepair(installer->EffectiveInstallerType()))
{
return;
}
}
// This installer cannot be run elevated, but we are running elevated.
// Implementation of de-elevation is complex; simply block for now.
if (installer->ElevationRequirement == ElevationRequirementEnum::ElevationProhibited && Runtime::IsRunningAsAdmin())
{
AICLI_LOG(CLI, Error, << "The installer cannot be run from an administrator context.");
context.Reporter.Error() << Resource::String::InstallerProhibitsElevation << std::endl;
AICLI_TERMINATE_CONTEXT(APPINSTALLER_CLI_ERROR_INSTALLER_PROHIBITS_ELEVATION);
}
// This installer cannot be used to upgrade the currently installed application
// Because the upgrade mechanism may be package-specific, simply block.
bool isUpdate = WI_IsFlagSet(context.GetFlags(), Execution::ContextFlag::InstallerExecutionUseUpdate);
UpdateBehaviorEnum updateBehavior = installer->UpdateBehavior;
if (isUpdate && (updateBehavior == UpdateBehaviorEnum::Deny))
{
AICLI_LOG(CLI, Error, << "Manifest specifies update behavior is denied. The attempt will be cancelled.");
context.Reporter.Error() << Resource::String::UpgradeBlockedByManifest << std::endl;
AICLI_TERMINATE_CONTEXT(APPINSTALLER_CLI_ERROR_INSTALL_UPGRADE_NOT_SUPPORTED);
}
context <<
Workflow::EnsureRunningAsAdminForMachineScopeInstall <<
Workflow::EnsureSupportForPortableInstall <<
Workflow::EnsureValidNestedInstallerMetadataForArchiveInstall;
}
void ProcessMultiplePackages::operator()(Execution::Context& context) const
{
if (!context.Contains(Execution::Data::PackageSubContexts))
{
return;
}
bool downloadInstallerOnly = WI_IsFlagSet(context.GetFlags(), Execution::ContextFlag::InstallerDownloadOnly);
// Show all prompts needed for every package before installing anything
context << Workflow::ShowPromptsForMultiplePackages(m_ensurePackageAgreements, downloadInstallerOnly);
if (context.IsTerminated())
{
return;
}
// Report dependencies
if (!m_ignorePackageDependencies)
{
auto& packageSubContexts = context.Get<Execution::Data::PackageSubContexts>();
DependencyList allDependencies;
for (auto& packageContext : packageSubContexts)
{
allDependencies.Add(packageContext->Get<Execution::Data::Installer>().value().Dependencies);
}
if (!allDependencies.Empty())
{
if (downloadInstallerOnly)
{
context.Reporter.Info() << Resource::String::DependenciesFlowDownload << std::endl;
}
else
{
context.Reporter.Info() << Resource::String::DependenciesFlowInstall << std::endl;
}
}
context.Add<Execution::Data::Dependencies>(allDependencies);
context << Workflow::ReportDependencies(m_dependenciesReportMessage);
}
bool allSucceeded = true;
size_t packagesCount = context.Get<Execution::Data::PackageSubContexts>().size();
size_t packagesProgress = 0;
for (auto& packageContext : context.Get<Execution::Data::PackageSubContexts>())
{
packagesProgress++;
context.Reporter.Info() << '(' << packagesProgress << '/' << packagesCount << ") "_liv;
// We want to do best effort to install all packages regardless of previous failures
Execution::Context& currentContext = *packageContext;
auto previousThreadGlobals = currentContext.SetForCurrentThread();
currentContext << Workflow::ReportIdentityAndInstallationDisclaimer;
// Prevent individual exceptions from breaking out of the loop
try
{
// Handle dependencies if requested.
if (!m_ignorePackageDependencies && !downloadInstallerOnly)
{
currentContext <<
Workflow::EnableWindowsFeaturesDependencies <<
Workflow::CreateDependencySubContexts(m_dependenciesReportMessage) <<
Workflow::ProcessMultiplePackages(m_dependenciesReportMessage, APPINSTALLER_CLI_ERROR_INSTALL_DEPENDENCIES, {}, true, true, true, true);
}
currentContext << Workflow::DownloadInstaller;
if (!downloadInstallerOnly)
{
currentContext << Workflow::InstallPackageInstaller;
}
}
catch (...)
{
currentContext.SetTerminationHR(Workflow::HandleException(currentContext, std::current_exception()));
}
if (m_refreshPathVariable)
{
if (RefreshPathVariableForCurrentProcess())
{
AICLI_LOG(CLI, Info, << "Successfully refreshed process PATH environment variable.");
}
else
{
AICLI_LOG(CLI, Warning, << "Failed to refresh process PATH environment variable.");
context.Reporter.Warn() << Resource::String::FailedToRefreshPathWarning << std::endl;
}
}
currentContext.Reporter.Info() << std::endl;
if (currentContext.IsTerminated())
{
if (context.IsTerminated() && context.GetTerminationHR() == E_ABORT)
{
// This means that the subcontext being terminated is due to an overall abort
context.Reporter.Info() << Resource::String::Cancelled << std::endl;
return;
}
if (m_ignorableInstallResults.end() == std::find(m_ignorableInstallResults.begin(), m_ignorableInstallResults.end(), currentContext.GetTerminationHR()))
{
allSucceeded = false;
if (m_stopOnFailure)
{
break;
}
}
}
}
if (!allSucceeded)
{
AICLI_TERMINATE_CONTEXT(m_resultOnFailure);
}
}
void SnapshotARPEntries(Execution::Context& context) try
{
// Ensure that installer type might actually write to ARP, otherwise this is a waste of time
auto installer = context.Get<Execution::Data::Installer>();
if (installer && MightWriteToARP(installer->EffectiveInstallerType()))
{
Repository::Correlation::ARPCorrelationData data;
data.CapturePreInstallSnapshot();
context.Add<Execution::Data::ARPCorrelationData>(std::move(data));
}
}
CATCH_LOG()
void ReportARPChanges(Execution::Context& context) try
{
if (!context.Contains(Execution::Data::ARPCorrelationData))
{
return;
}
// If the installer claims to have a PackageFamilyName, and that family name is currently registered for the user,
// let that be the correlated item and skip any attempt at further ARP correlation.
const auto& installer = context.Get<Execution::Data::Installer>();
if (installer && !installer->PackageFamilyName.empty() && Deployment::IsRegistered(installer->PackageFamilyName))
{
return;
}
const auto& manifest = context.Get<Execution::Data::Manifest>();
auto& arpCorrelationData = context.Get<Execution::Data::ARPCorrelationData>();
arpCorrelationData.CapturePostInstallSnapshot();
auto correlationResult = arpCorrelationData.CorrelateForNewlyInstalled(manifest);
// Store the ARP entry found to match the package to record it in the tracking catalog later
if (correlationResult.Package)
{
std::vector<AppsAndFeaturesEntry> entries;
auto metadata = correlationResult.Package->GetMetadata();
AppsAndFeaturesEntry baseEntry;
// Display name and publisher are also available as multi properties, but
// for ARP there will always be only 0 or 1 values.
baseEntry.DisplayName = correlationResult.Package->GetProperty(PackageVersionProperty::Name).get();
baseEntry.Publisher = correlationResult.Package->GetProperty(PackageVersionProperty::Publisher).get();
baseEntry.DisplayVersion = correlationResult.Package->GetProperty(PackageVersionProperty::Version).get();
baseEntry.InstallerType = Manifest::ConvertToInstallerTypeEnum(metadata[PackageVersionMetadata::InstalledType]);
auto productCodes = correlationResult.Package->GetMultiProperty(PackageVersionMultiProperty::ProductCode);
for (auto&& productCode : productCodes)
{
AppsAndFeaturesEntry entry = baseEntry;
entry.ProductCode = std::move(productCode).get();
entries.push_back(std::move(entry));
}
auto upgradeCodes = correlationResult.Package->GetMultiProperty(PackageVersionMultiProperty::UpgradeCode);
for (auto&& upgradeCode : upgradeCodes)
{
AppsAndFeaturesEntry entry = baseEntry;
entry.UpgradeCode = std::move(upgradeCode).get();
entries.push_back(std::move(entry));
}
context.Add<Data::CorrelatedAppsAndFeaturesEntries>(std::move(entries));
}
// We can only get the source identifier from an active source
std::string sourceIdentifier;
if (context.Contains(Execution::Data::PackageVersion))
{
sourceIdentifier = context.Get<Execution::Data::PackageVersion>()->GetProperty(PackageVersionProperty::SourceIdentifier);
}
IPackageVersion::Metadata arpEntryMetadata;
if (correlationResult.Package)
{
arpEntryMetadata = correlationResult.Package->GetMetadata();
}
Logging::Telemetry().LogSuccessfulInstallARPChange(
sourceIdentifier,
manifest.Id,
manifest.Version,
manifest.Channel,
correlationResult.ChangesToARP,
correlationResult.MatchesInARP,
correlationResult.CountOfIntersectionOfChangesAndMatches,
correlationResult.Package ? static_cast<std::string>(correlationResult.Package->GetProperty(PackageVersionProperty::Name)) : "",
correlationResult.Package ? static_cast<std::string>(correlationResult.Package->GetProperty(PackageVersionProperty::Version)) : "",
correlationResult.Package ? static_cast<std::string>(correlationResult.Package->GetProperty(PackageVersionProperty::Publisher)) : "",
correlationResult.Package ? static_cast<std::string_view>(arpEntryMetadata[PackageVersionMetadata::InstalledLocale]) : ""
);
}
CATCH_LOG();
void RecordInstall(Context& context)
{
// Local manifest installs won't have a package version, and tracking them doesn't provide much
// value currently. If we ever do use our own database as a primary source of packages that we
// maintain, this decision will probably have to be reconsidered.
if (!context.Contains(Data::PackageVersion))
{
return;
}
auto manifest = context.Get<Data::Manifest>();
// If we have determined an ARP entry matches the installed package,
// we set its product code in the manifest we record to ensure we can
// find it in the future.
// Note that this may overwrite existing information.
if (context.Contains(Data::CorrelatedAppsAndFeaturesEntries))
{
// Use a new Installer entry
manifest.Installers.emplace_back();
manifest.Installers.back().AppsAndFeaturesEntries = context.Get<Data::CorrelatedAppsAndFeaturesEntries>();
}
auto trackingCatalog = context.Get<Data::PackageVersion>()->GetSource().GetTrackingCatalog();
auto version = trackingCatalog.RecordInstall(
manifest,
context.Get<Data::Installer>().value(),
WI_IsFlagSet(context.GetFlags(), ContextFlag::InstallerExecutionUseUpdate));
// Record user intent values. Command args takes precedence. Then previous user intent values.
Repository::IPackageVersion::Metadata installedMetadata;
if (context.Contains(Data::InstalledPackageVersion) && context.Get<Execution::Data::InstalledPackageVersion>())
{
installedMetadata = context.Get<Data::InstalledPackageVersion>()->GetMetadata();
}
if (context.Args.Contains(Execution::Args::Type::InstallArchitecture))
{
version.SetMetadata(Repository::PackageVersionMetadata::UserIntentArchitecture, context.Args.GetArg(Execution::Args::Type::InstallArchitecture));
}
else
{
auto itr = installedMetadata.find(Repository::PackageVersionMetadata::UserIntentArchitecture);
if (itr != installedMetadata.end())
{
version.SetMetadata(Repository::PackageVersionMetadata::UserIntentArchitecture, itr->second);
}
}
if (context.Args.Contains(Execution::Args::Type::Locale))
{
version.SetMetadata(Repository::PackageVersionMetadata::UserIntentLocale, context.Args.GetArg(Execution::Args::Type::Locale));
}
else
{
auto itr = installedMetadata.find(Repository::PackageVersionMetadata::UserIntentLocale);
if (itr != installedMetadata.end())
{
version.SetMetadata(Repository::PackageVersionMetadata::UserIntentLocale, itr->second);
}
}
}
}