-
Notifications
You must be signed in to change notification settings - Fork 821
Expand file tree
/
Copy pathWinGetPkgOperationHelper.cs
More file actions
431 lines (388 loc) · 15.4 KB
/
WinGetPkgOperationHelper.cs
File metadata and controls
431 lines (388 loc) · 15.4 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
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
using Microsoft.Management.Deployment;
using Microsoft.Win32;
using UniGetUI.Core.Logging;
using UniGetUI.Core.SettingsEngine;
using UniGetUI.Core.Tools;
using UniGetUI.PackageEngine.Classes.Manager.BaseProviders;
using UniGetUI.PackageEngine.Classes.Packages.Classes;
using UniGetUI.PackageEngine.Enums;
using UniGetUI.PackageEngine.Interfaces;
using UniGetUI.PackageEngine.PackageClasses;
using Architecture = UniGetUI.PackageEngine.Enums.Architecture;
using InstallOptions = UniGetUI.PackageEngine.Serializable.InstallOptions;
namespace UniGetUI.PackageEngine.Managers.WingetManager;
internal sealed class WinGetPkgOperationHelper : BasePkgOperationHelper
{
public static string GetIdNamePiece(IPackage package)
{
if (!package.Id.EndsWith("…"))
return $"--id \"{package.Id.TrimEnd('…')}\" --exact";
if (!package.Name.EndsWith("…"))
return $"--name \"{package.Name}\" --exact";
return $"--id \"{package.Id.TrimEnd('…')}\"";
}
public WinGetPkgOperationHelper(WinGet manager)
: base(manager) { }
protected override IReadOnlyList<string> _getOperationParameters(
IPackage package,
InstallOptions options,
OperationType operation
)
{
// Pinget 0.4.x does not accept --accept-source-agreements, --disable-interactivity,
// or --proxy on any verb; --accept-package-agreements, --force, --location, and
// --interactive are accepted on install/uninstall but rejected on upgrade.
bool usePinget =
((WinGet)Manager).SelectedCliToolKind == WinGetCliToolKind.BundledPinget;
List<string> parameters =
[
operation switch
{
OperationType.Install => Manager.Properties.InstallVerb,
OperationType.Update => Manager.Properties.UpdateVerb,
OperationType.Uninstall => Manager.Properties.UninstallVerb,
_ => throw new InvalidDataException("Invalid package operation"),
},
];
parameters.AddRange(GetIdNamePiece(package).Split(" "));
if (!package.Source.IsVirtualManager)
{
parameters.AddRange(["--source", package.Source.Name]);
}
if (!usePinget)
{
parameters.AddRange(["--accept-source-agreements", "--disable-interactivity"]);
}
// package.OverridenInstallationOptions.Scope is meaningless in WinGet packages. Default is unspecified, hence the _ => [].
parameters.AddRange(
(package.OverridenOptions.Scope ?? options.InstallationScope) switch
{
PackageScope.User => ["--scope", "user"],
PackageScope.Machine => ["--scope", "machine"],
_ => [],
}
);
if (
operation is OperationType.Uninstall
&& package.VersionString != "Unknown"
&& package.OverridenOptions.WinGet_SpecifyVersion is not false
)
{
parameters.AddRange(["--version", $"\"{package.VersionString}\""]);
}
else if (operation is OperationType.Install && options.Version != "")
{
parameters.AddRange(["--version", $"\"{options.Version}\""]);
}
if (usePinget && operation is OperationType.Update)
{
// pinget upgrade only supports --silent (no --interactive).
parameters.Add("--silent");
}
else
{
parameters.Add(options.InteractiveInstallation ? "--interactive" : "--silent");
}
if (operation is OperationType.Update)
{
if (package.Name.Contains("64-bit") || package.Id.ToLower().Contains("x64"))
{
options.Architecture = Architecture.x64;
}
else if (package.Name.Contains("32-bit") || package.Id.ToLower().Contains("x86"))
{
options.Architecture = Architecture.x86;
}
parameters.Add("--include-unknown");
if (!usePinget)
{
if (options.CustomInstallLocation != "")
{
if (Settings.Get(Settings.K.WinGetForceLocationOnUpdate))
parameters.AddRange(["--location", $"\"{options.CustomInstallLocation}\""]);
}
else
{
var detectedLocation = TryGetPortableInstallLocation(package);
if (detectedLocation is not null)
parameters.AddRange(["--location", $"\"{detectedLocation}\""]);
}
}
}
else if (operation is OperationType.Install)
{
if (options.CustomInstallLocation != "")
parameters.AddRange(["--location", $"\"{options.CustomInstallLocation}\""]);
}
if (operation is not OperationType.Uninstall)
{
// pinget upgrade does not accept --accept-package-agreements or --force.
if (!(usePinget && operation is OperationType.Update))
{
parameters.AddRange(["--accept-package-agreements", "--force"]);
}
if (options.SkipHashCheck)
parameters.Add("--ignore-security-hash");
parameters.AddRange(
options.Architecture switch
{
Architecture.x86 => ["--architecture", "x86"],
Architecture.x64 => ["--architecture", "x64"],
Architecture.arm64 => ["--architecture", "arm64"],
_ => [],
}
);
}
try
{
var installOptions = NativePackageHandler.GetInstallationOptions(
package,
options,
operation
);
if (
installOptions?.ElevationRequirement
is ElevationRequirement.ElevationRequired
or ElevationRequirement.ElevatesSelf
)
{
Logger.Info(
$"WinGet package {package.Id} requires elevation, forcing administrator rights..."
);
package.OverridenOptions.RunAsAdministrator = true;
}
else if (
installOptions?.ElevationRequirement is ElevationRequirement.ElevationProhibited
)
{
if (CoreTools.IsAdministrator())
throw new UnauthorizedAccessException(
CoreTools.Translate(
"This package cannot be installed from an elevated context."
)
+ CoreTools.Translate(
"Please run UniGetUI as a regular user and try again."
)
);
if (options.RunAsAdministrator)
throw new UnauthorizedAccessException(
CoreTools.Translate(
"This package cannot be installed from an elevated context."
)
+ CoreTools.Translate(
"Please check the installation options for this package and try again"
)
);
package.OverridenOptions.RunAsAdministrator = false;
}
else if (
installOptions?.Scope is PackageInstallerScope.System /* or PackageInstallerScope.Unknown*/
)
{
Logger.Info(
$"WinGet package {package.Id} is installed on a system-wide scope, forcing administrator rights..."
);
package.OverridenOptions.RunAsAdministrator = true;
}
}
catch (Exception ex)
{
if (ex is UnauthorizedAccessException)
throw;
Logger.Error("Recovered from fatal WinGet exception:");
Logger.Error(ex);
}
if (!usePinget)
{
parameters.Add(WinGet.GetProxyArgument());
}
parameters.AddRange(
operation switch
{
OperationType.Update => options.CustomParameters_Update,
OperationType.Uninstall => options.CustomParameters_Uninstall,
_ => options.CustomParameters_Install,
}
);
return parameters;
}
protected override OperationVeredict _getOperationResult(
IPackage package,
OperationType operation,
IReadOnlyList<string> processOutput,
int returnCode
)
{
// See https://github.com/microsoft/winget-cli/blob/master/doc/windows/package-manager/winget/returnCodes.md for reference
uint uintCode = (uint)returnCode;
if (uintCode is 0x8A150109)
{ // TODO: Restart required to finish installation
if (operation is OperationType.Update or OperationType.Install)
MarkUpgradeAsDone(package);
return OperationVeredict.Success;
}
if (uintCode is 0x8A150077 or 0x8A15010C or 0x8A150005)
{ // At some point, the user clicked cancel or Ctrl+C
return OperationVeredict.Canceled;
}
if (
operation is OperationType.Uninstall
&& uintCode is 0x8A150017
&& package.OverridenOptions.WinGet_SpecifyVersion is not false
)
{ // No manifest found matching criteria
package.OverridenOptions.WinGet_SpecifyVersion = false;
return OperationVeredict.AutoRetry;
}
if (uintCode is 0x8A150011)
{ // TODO: Integrity failed
return OperationVeredict.Failure;
}
if (uintCode is 0x8A15002B)
{
//if (Settings.Get(Settings.K.IgnoreUpdatesNotApplicable))
//{
Logger.Warn(
$"Ignoring update {package.Id} as the update is not applicable to the platform, and the user has enabled IgnoreUpdatesNotApplicable"
);
IgnoredUpdatesDatabase.Add(
IgnoredUpdatesDatabase.GetIgnoredIdForPackage(package),
package.VersionString
);
return OperationVeredict.Success;
//}
//return OperationVeredict.Failure;
}
if (uintCode is 0x8A15010D or 0x8A15004F or 0x8A15010E)
{ // Application is already installed
if (operation is OperationType.Update or OperationType.Install)
MarkUpgradeAsDone(package);
return OperationVeredict.Success;
}
if (returnCode is 0)
{ // Operation succeeded
if (operation is OperationType.Update or OperationType.Install)
MarkUpgradeAsDone(package);
return OperationVeredict.Success;
}
if (
uintCode is 0x8A150056
&& package.OverridenOptions.RunAsAdministrator is not false
&& !CoreTools.IsAdministrator()
)
{ // Installer can't run elevated, but this condition hasn't been forced on UniGetUI
package.OverridenOptions.RunAsAdministrator = false;
return OperationVeredict.AutoRetry;
}
if (
(uintCode is 0x8A150019 or 0x80073D28)
&& package.OverridenOptions.RunAsAdministrator is not true
)
{ // Installer needs to run elevated, handle autoelevation
// Code 0x80073D28 was added after https://github.com/Devolutions/UniGetUI/issues/3093
package.OverridenOptions.RunAsAdministrator = true;
return OperationVeredict.AutoRetry;
}
if (
operation is OperationType.Uninstall
&& (uintCode is 0x8A150030)
&& package.OverridenOptions.RunAsAdministrator is not true
)
{ // Sometimes, when uninstalling, error code 0x8A150030 can be caused by missing permissions.
package.OverridenOptions.RunAsAdministrator = true;
return OperationVeredict.AutoRetry;
}
return OperationVeredict.Failure;
}
private static void MarkUpgradeAsDone(IPackage package)
{
var options = InstallOptionsFactory.LoadApplicable(package);
string version;
if (package.IsUpgradable)
version = package.NewVersionString;
else if (options.Version != "")
version = options.Version;
else
version = package.VersionString;
Settings.SetDictionaryItem<string, string>(
Settings.K.WinGetAlreadyUpgradedPackages,
package.Id,
version
);
}
public static bool UpdateAlreadyInstalled(IPackage package)
{
return Settings.GetDictionaryItem<string, string>(
Settings.K.WinGetAlreadyUpgradedPackages,
package.Id
) == package.NewVersionString;
}
public static string GetLastInstalledVersion(string id)
{
var val = Settings.GetDictionaryItem<string, string>(
Settings.K.WinGetAlreadyUpgradedPackages,
id
);
if (val is null || val == "")
val = "Unknown";
return val;
}
/// <summary>
/// For portable WinGet packages, reads the current install location from the Windows registry
/// ARP entry (written by WinGet at install time). Returns null if the package is not portable
/// or the location cannot be determined.
/// </summary>
private static string? TryGetPortableInstallLocation(IPackage package)
{
try
{
foreach (
var hive in new RegistryKey[] { Registry.CurrentUser, Registry.LocalMachine }
)
{
using var uninstallKey = hive.OpenSubKey(
@"Software\Microsoft\Windows\CurrentVersion\Uninstall"
);
if (uninstallKey is null)
continue;
foreach (var name in uninstallKey.GetSubKeyNames())
{
using var entry = uninstallKey.OpenSubKey(name);
if (entry is null)
continue;
if (
entry.GetValue("WinGetPackageIdentifier") is not string pkgId
|| !string.Equals(pkgId, package.Id, StringComparison.OrdinalIgnoreCase)
)
continue;
if (
entry.GetValue("WinGetInstallerType") is not string installerType
|| !string.Equals(
installerType,
"portable",
StringComparison.OrdinalIgnoreCase
)
)
return null;
if (
entry.GetValue("InstallLocation") is string location
&& location.Length > 0
)
{
Logger.Info(
$"Auto-detected portable install location for {package.Id}: {location}"
);
return location;
}
}
}
}
catch (Exception ex)
{
Logger.Warn(
$"Failed to auto-detect portable install location for {package.Id}: {ex.Message}"
);
}
return null;
}
}