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
341 lines (285 loc) · 13.5 KB
/
Copy pathDeployment.cpp
File metadata and controls
341 lines (285 loc) · 13.5 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
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
// 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(
std::string_view 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.
// No need for proxy as Deployment won't use it anyways.
Msix::MsixInfo packageInfo{ uri };
std::wstring packageFullNameWide = packageInfo.GetPackageFullNameWide();
std::string packageFullName = Utility::ConvertToUTF8(packageFullNameWide);
auto removePackage = wil::scope_exit([&]() {
try
{
ProgressCallback cb;
RemovePackage(packageFullName, RemovalOptions::None, cb);
}
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.
PartialPercentProgressCallback progress{ callback, 100 };
progress.SetRange(0, 95);
{
size_t id = GetDeploymentOperationId();
AICLI_LOG(Core, Info, << "Starting StagePackageAsync operation #" << id << ": " << uri);
IAsyncOperationWithProgress<DeploymentResult, DeploymentProgress> stageOperation = packageManager.StagePackageAsync(uriObject, nullptr);
WaitForDeployment(stageOperation, id, progress);
}
bool registrationDeferred = false;
progress.SetRange(95, 100);
{
size_t id = GetDeploymentOperationId();
AICLI_LOG(Core, Info, << "Starting RegisterPackageByFullNameAsync operation #" << id << ": " << packageFullName);
IAsyncOperationWithProgress<DeploymentResult, DeploymentProgress> registerOperation =
packageManager.RegisterPackageByFullNameAsync(packageFullNameWide, nullptr, DeploymentOptions::None);
HRESULT hr = WaitForDeployment(registerOperation, id, progress, 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,
RemovalOptions options,
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, options);
WaitForDeployment(deployOperation, id, callback);
}
bool AddPackageMachineScope(
std::string_view uri,
IProgressCallback& callback)
{
PackageManager packageManager;
// In the event of a failure we want to ensure that the package is not left on the system.
// No need for proxy as Deployment won't use it anyways.
Msix::MsixInfo packageInfo{ uri };
std::wstring packageFullNameWide = packageInfo.GetPackageFullNameWide();
std::string packageFullName = Utility::ConvertToUTF8(packageFullNameWide);
std::string packageFamilyName = Msix::GetPackageFamilyNameFromFullName(packageFullName);
auto removePackage = wil::scope_exit([&]() {
try
{
ProgressCallback cb;
RemovePackage(packageFullName, RemovalOptions::RemoveForAllUsers, cb);
}
CATCH_LOG();
});
Uri uriObject(Utility::ConvertToUTF16(uri));
PartialPercentProgressCallback progress{ callback, 100 };
// First stage package contents
progress.SetRange(0, 90);
{
size_t id = GetDeploymentOperationId();
AICLI_LOG(Core, Info, << "Starting StagePackageAsync operation #" << id << ": " << uri);
IAsyncOperationWithProgress<DeploymentResult, DeploymentProgress> stageOperation = packageManager.StagePackageAsync(uriObject, nullptr);
WaitForDeployment(stageOperation, id, progress);
}
// Provision for all users
progress.SetRange(90, 95);
{
size_t id = GetDeploymentOperationId();
AICLI_LOG(Core, Info, << "Starting ProvisionPackage operation #" << id << ": " << packageFamilyName);
winrt::hstring familyName = Utility::ConvertToUTF16(packageFamilyName).c_str();
auto deployOperation = packageManager.ProvisionPackageForAllUsersAsync(familyName);
WaitForDeployment(deployOperation, id, progress);
}
// Try registration as best effort, operation is considered successful as long as provisioning is successful.
progress.SetRange(95, 100);
bool registrationDeferred = false;
if (Runtime::IsRunningAsSystem())
{
// Packages cannot be registered under local system, just return registration deferred
registrationDeferred = true;
}
else
{
try
{
size_t id = GetDeploymentOperationId();
AICLI_LOG(Core, Info, << "Starting RegisterPackageByFullNameAsync operation #" << id << ": " << packageFullName);
IAsyncOperationWithProgress<DeploymentResult, DeploymentProgress> registerOperation =
packageManager.RegisterPackageByFullNameAsync(packageFullNameWide, nullptr, DeploymentOptions::None);
WaitForDeployment(registerOperation, id, progress);
}
catch (...)
{
registrationDeferred = true;
}
}
progress.OnProgress(100, 100, ProgressType::Percent);
removePackage.release();
return registrationDeferred;
}
void RemovePackageMachineScope(
std::string_view packageFamilyName,
std::string_view packageFullName,
IProgressCallback& callback)
{
PartialPercentProgressCallback progress{ callback, 100 };
// Deprovision first
progress.SetRange(0, 5);
{
size_t id = GetDeploymentOperationId();
AICLI_LOG(Core, Info, << "Starting DeprovisionPackage operation #" << id << ": " << packageFamilyName);
PackageManager packageManager;
winrt::hstring familyName = Utility::ConvertToUTF16(packageFamilyName).c_str();
auto deployOperation = packageManager.DeprovisionPackageForAllUsersAsync(familyName);
WaitForDeployment(deployOperation, id, progress);
}
// Remove for all users
progress.SetRange(5, 100);
{
RemovePackage(packageFullName, RemovalOptions::RemoveForAllUsers, progress);
}
}
bool IsRegistered(std::string_view packageFamilyName)
{
std::wstring wideFamilyName = Utility::ConvertToUTF16(packageFamilyName);
PackageManager packageManager;
auto packages = packageManager.FindPackagesForUser({}, wideFamilyName);
return packages.begin() != packages.end();
}
void RegisterPackage(
std::string_view packageFamilyName,
IProgressCallback& callback)
{
size_t id = GetDeploymentOperationId();
AICLI_LOG(Core, Info, << "Starting RegisterPackageByFullNameAsync operation #" << id << ": " << packageFamilyName);
PackageManager packageManager;
winrt::hstring packageFamilyNameWide = Utility::ConvertToUTF16(packageFamilyName).c_str();
auto deployOperation = packageManager.RegisterPackageByFamilyNameAsync(packageFamilyNameWide, nullptr, DeploymentOptions::None, nullptr, nullptr);
WaitForDeployment(deployOperation, id, callback);
}
}