forked from microsoft/winget-cli
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathDeployment.cpp
More file actions
208 lines (173 loc) · 8.07 KB
/
Copy pathDeployment.cpp
File metadata and controls
208 lines (173 loc) · 8.07 KB
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
// Copyright (c) Microsoft Corporation.
// Licensed under the MIT License.
#include "pch.h"
#include "Public/AppInstallerDeployment.h"
#include "Public/AppInstallerLogging.h"
#include "Public/AppInstallerMsixInfo.h"
#include "Public/AppInstallerRuntime.h"
#include "Public/AppInstallerStrings.h"
namespace AppInstaller::Deployment
{
using namespace winrt::Windows::Foundation;
using namespace winrt::Windows::Management::Deployment;
namespace
{
size_t GetDeploymentOperationId()
{
static std::atomic_size_t s_deploymentId = 0;
return s_deploymentId.fetch_add(1);
}
HRESULT WaitForDeployment(
IAsyncOperationWithProgress<DeploymentResult, DeploymentProgress>& deployOperation,
size_t id,
IProgressCallback& callback,
bool throwOnError = true)
{
AICLI_LOG(Core, Info, << "Begin waiting for operation #" << id);
AsyncOperationProgressHandler<DeploymentResult, DeploymentProgress> progressCallback(
[&callback](const IAsyncOperationWithProgress<DeploymentResult, DeploymentProgress>&, DeploymentProgress progress)
{
callback.OnProgress(progress.percentage, 100, ProgressType::Percent);
}
);
// Set progress callback.
deployOperation.Progress(progressCallback);
auto removeCancel = callback.SetCancellationFunction([&]() { deployOperation.Cancel(); });
AICLI_LOG(Core, Info, << "Begin blocking for operation #" << id);
auto deployResult = deployOperation.get();
if (!SUCCEEDED(deployResult.ExtendedErrorCode()))
{
AICLI_LOG(Core, Error, << "Deployment operation #" << id << ": " << Utility::ConvertToUTF8(deployResult.ErrorText()));
// Note that while the format string is char*, it gets converted to wchar before being used.
if (throwOnError)
{
THROW_HR_MSG(deployResult.ExtendedErrorCode(), "Operation failed: %ws", deployResult.ErrorText().c_str());
}
else
{
// Simple return because this path is generally used for recovery cases
return deployResult.ExtendedErrorCode();
}
}
else
{
AICLI_LOG(Core, Info, << "Successfully completed #" << id);
}
return S_OK;
}
}
void AddPackage(
const winrt::Windows::Foundation::Uri& uri,
winrt::Windows::Management::Deployment::DeploymentOptions options,
bool skipSmartScreen,
IProgressCallback& callback)
{
size_t id = GetDeploymentOperationId();
AICLI_LOG(Core, Info, << "Starting AddPackage operation #" << id << ": " << Utility::ConvertToUTF8(uri.AbsoluteUri().c_str()) << " SkipSmartScreen: " << skipSmartScreen);
PackageManager packageManager;
IAsyncOperationWithProgress<DeploymentResult, DeploymentProgress> deployOperation;
if (skipSmartScreen)
{
deployOperation = packageManager.AddPackageAsync(
uri,
nullptr, /*dependencyPackageUris*/
options,
nullptr, /*targetVolume*/
nullptr, /*optionalAndRelatedPackageFamilyNames*/
nullptr, /*optionalPackageUris*/
nullptr /*relatedPackageUris*/);
}
else
{
deployOperation = packageManager.RequestAddPackageAsync(
uri,
nullptr, /*dependencyPackageUris*/
options,
nullptr, /*targetVolume*/
nullptr, /*optionalAndRelatedPackageFamilyNames*/
nullptr /*relatedPackageUris*/);
}
WaitForDeployment(deployOperation, id, callback);
}
bool AddPackageWithDeferredFallback(
const std::string& uri,
bool skipSmartScreen,
IProgressCallback& callback)
{
PackageManager packageManager;
// In the event of a failure we want to ensure that the package is not left on the system.
Msix::MsixInfo packageInfo{ uri };
std::wstring packageFullName = packageInfo.GetPackageFullNameWide();
auto removePackage = wil::scope_exit([&]() {
try
{
RemovePackage(Utility::ConvertToUTF8(packageFullName), callback);
}
CATCH_LOG();
});
Uri uriObject(Utility::ConvertToUTF16(uri));
if (!skipSmartScreen)
{
// The only way to get SmartScreen is to use RequestAddPackageAsync, so we will have to start with that.
size_t id = GetDeploymentOperationId();
AICLI_LOG(Core, Info, << "Starting RequestAddPackageAsync operation #" << id << ": " << uri);
DeploymentOptions options = DeploymentOptions::None;
// Optimization to keep files if the package is in use. Only available in a newer OS per:
// https://docs.microsoft.com/en-us/uwp/api/Windows.Management.Deployment.DeploymentOptions
if (Runtime::IsCurrentOSVersionGreaterThanOrEqual(Utility::Version{ "10.0.18362.0" }))
{
options = DeploymentOptions::RetainFilesOnFailure;
}
IAsyncOperationWithProgress<DeploymentResult, DeploymentProgress> deployOperation = packageManager.RequestAddPackageAsync(
uriObject,
nullptr, /*dependencyPackageUris*/
options,
nullptr, /*targetVolume*/
nullptr, /*optionalAndRelatedPackageFamilyNames*/
nullptr /*relatedPackageUris*/);
HRESULT hr = WaitForDeployment(deployOperation, id, callback, false);
if (SUCCEEDED(hr))
{
removePackage.release();
return false;
}
THROW_HR_IF(hr, FAILED(hr) && hr != HRESULT_FROM_WIN32(ERROR_PACKAGES_IN_USE));
}
// If we are skipping SmartScreen or the package was in use, stage then register the package.
{
size_t id = GetDeploymentOperationId();
AICLI_LOG(Core, Info, << "Starting StagePackageAsync operation #" << id << ": " << uri);
IAsyncOperationWithProgress<DeploymentResult, DeploymentProgress> stageOperation = packageManager.StagePackageAsync(uriObject, nullptr);
WaitForDeployment(stageOperation, id, callback);
}
bool registrationDeferred = false;
{
size_t id = GetDeploymentOperationId();
AICLI_LOG(Core, Info, << "Starting RegisterPackageByFullNameAsync operation #" << id << ": " << Utility::ConvertToUTF8(packageFullName));
IAsyncOperationWithProgress<DeploymentResult, DeploymentProgress> registerOperation =
packageManager.RegisterPackageByFullNameAsync(packageFullName, nullptr, DeploymentOptions::None);
HRESULT hr = WaitForDeployment(registerOperation, id, callback, false);
if (hr == HRESULT_FROM_WIN32(ERROR_PACKAGES_IN_USE))
{
registrationDeferred = true;
}
else
{
THROW_IF_FAILED(hr);
}
}
removePackage.release();
return registrationDeferred;
}
void RemovePackage(
std::string_view packageFullName,
IProgressCallback& callback)
{
size_t id = GetDeploymentOperationId();
AICLI_LOG(Core, Info, << "Starting RemovePackage operation #" << id << ": " << packageFullName);
PackageManager packageManager;
winrt::hstring fullName = Utility::ConvertToUTF16(packageFullName).c_str();
auto deployOperation = packageManager.RemovePackageAsync(fullName, RemovalOptions::None);
WaitForDeployment(deployOperation, id, callback);
}
}