-
Notifications
You must be signed in to change notification settings - Fork 1.8k
Expand file tree
/
Copy pathPortableFlow.cpp
More file actions
373 lines (319 loc) · 16.5 KB
/
Copy pathPortableFlow.cpp
File metadata and controls
373 lines (319 loc) · 16.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
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
// Copyright (c) Microsoft Corporation.
// Licensed under the MIT License.
#include "pch.h"
#include "PortableFlow.h"
#include "PortableInstaller.h"
#include "WorkflowBase.h"
#include <map>
#include <winget/Filesystem.h>
#include <winget/PortableFileEntry.h>
#include <winget/PortableIndex.h>
using namespace AppInstaller::Manifest;
using namespace AppInstaller::Repository;
using namespace AppInstaller::Utility;
using namespace AppInstaller::CLI::Portable;
using namespace AppInstaller::Portable;
using namespace AppInstaller::Repository::Microsoft;
namespace AppInstaller::CLI::Workflow
{
namespace
{
constexpr std::string_view s_DefaultSource = "*DefaultSource"sv;
std::string GetPortableProductCode(Execution::Context& context)
{
const std::string& packageId = context.Get<Execution::Data::Manifest>().Id;
std::string source;
if (context.Contains(Execution::Data::PackageVersion))
{
source = context.Get<Execution::Data::PackageVersion>()->GetSource().GetIdentifier();
}
else
{
source = s_DefaultSource;
}
return MakeSuitablePathPart(packageId + "_" + source);
}
void EnsureValidArgsForPortableInstall(Execution::Context& context)
{
std::string_view renameArg = context.Args.GetArg(Execution::Args::Type::Rename);
try
{
if (MakeSuitablePathPart(renameArg) != renameArg)
{
context.Reporter.Error() << Resource::String::ReservedFilenameError << std::endl;
AICLI_TERMINATE_CONTEXT(APPINSTALLER_CLI_ERROR_INVALID_CL_ARGUMENTS);
}
}
catch (...)
{
context.Reporter.Error() << Resource::String::ReservedFilenameError << std::endl;
AICLI_TERMINATE_CONTEXT(APPINSTALLER_CLI_ERROR_INVALID_CL_ARGUMENTS);
}
}
void EnsureVolumeSupportsReparsePoints(Execution::Context& context)
{
Manifest::ScopeEnum scope = ConvertToScopeEnum(context.Args.GetArg(Execution::Args::Type::InstallScope));
const std::filesystem::path& symlinkDirectory = GetPortableLinksLocation(scope);
if (!AppInstaller::Filesystem::SupportsReparsePoints(symlinkDirectory))
{
context.Reporter.Error() << Resource::String::ReparsePointsNotSupportedError << std::endl;
AICLI_TERMINATE_CONTEXT(APPINSTALLER_CLI_ERROR_PORTABLE_REPARSE_POINT_NOT_SUPPORTED);
}
}
}
void VerifyPackageAndSourceMatch(Execution::Context& context)
{
const std::string& packageIdentifier = context.Get<Execution::Data::Manifest>().Id;
std::string sourceIdentifier;
if (context.Contains(Execution::Data::PackageVersion))
{
sourceIdentifier = context.Get<Execution::Data::PackageVersion>()->GetSource().GetIdentifier();
}
else
{
sourceIdentifier = s_DefaultSource;
}
PortableInstaller& portableInstaller = context.Get<Execution::Data::PortableInstaller>();
if (portableInstaller.ARPEntryExists())
{
if (packageIdentifier != portableInstaller.WinGetPackageIdentifier || sourceIdentifier != portableInstaller.WinGetSourceIdentifier)
{
if (!context.Args.Contains(Execution::Args::Type::Force))
{
AICLI_LOG(CLI, Error, << "Registry match failed, skipping write to uninstall registry");
context.Reporter.Error() << Resource::String::PortablePackageAlreadyExists << std::endl;
AICLI_TERMINATE_CONTEXT(APPINSTALLER_CLI_ERROR_PORTABLE_PACKAGE_ALREADY_EXISTS);
}
else
{
AICLI_LOG(CLI, Info, << "Overriding registry match check...");
context.Reporter.Warn() << Resource::String::PortableRegistryCollisionOverridden << std::endl;
}
}
}
portableInstaller.WinGetPackageIdentifier = packageIdentifier;
portableInstaller.WinGetSourceIdentifier = sourceIdentifier;
}
void InitializePortableInstaller(Execution::Context& context)
{
Manifest::ScopeEnum scope = Manifest::ScopeEnum::Unknown;
bool isUpdate = WI_IsFlagSet(context.GetFlags(), Execution::ContextFlag::InstallerExecutionUseUpdate);
std::shared_ptr<Repository::IPackageVersion> installedVersion;
if (context.Contains(Execution::Data::InstalledPackageVersion))
{
installedVersion = context.Get<Execution::Data::InstalledPackageVersion>();
}
if (isUpdate && installedVersion)
{
IPackageVersion::Metadata installationMetadata = installedVersion->GetMetadata();
auto installerScopeItr = installationMetadata.find(Repository::PackageVersionMetadata::InstalledScope);
if (installerScopeItr != installationMetadata.end())
{
scope = Manifest::ConvertToScopeEnum(installerScopeItr->second);
}
}
else
{
if (context.Args.Contains(Execution::Args::Type::InstallScope))
{
scope = Manifest::ConvertToScopeEnum(context.Args.GetArg(Execution::Args::Type::InstallScope));
}
else
{
Manifest::ScopeEnum requiredScope = Settings::User().Get<Settings::Setting::InstallScopeRequirement>();
Manifest::ScopeEnum preferredScope = Settings::User().Get<Settings::Setting::InstallScopePreference>();
scope = requiredScope != Manifest::ScopeEnum::Unknown ? requiredScope : preferredScope;
}
}
const auto& installer = context.Get<Execution::Data::Installer>().value();
Utility::Architecture arch = installer.Arch;
const std::string& productCode = GetPortableProductCode(context);
PortableInstaller portableInstaller = PortableInstaller(scope, arch, productCode);
portableInstaller.IsUpdate = isUpdate;
if (IsArchiveType(installer.BaseInstallerType) && installer.ArchiveBinariesDependOnPath)
{
portableInstaller.BinariesDependOnPath = true;
}
// Set target install directory
std::string_view locationArg = context.Args.GetArg(Execution::Args::Type::InstallLocation);
std::filesystem::path targetInstallDirectory;
if (!locationArg.empty())
{
targetInstallDirectory = std::filesystem::path{ ConvertToUTF16(locationArg) };
}
else
{
targetInstallDirectory = GetPortableInstallRoot(scope, arch);
targetInstallDirectory /= ConvertToUTF16(productCode);
}
portableInstaller.TargetInstallLocation = targetInstallDirectory;
portableInstaller.SetAppsAndFeaturesMetadata(context.Get<Execution::Data::Manifest>(), installer.AppsAndFeaturesEntries);
context.Add<Execution::Data::PortableInstaller>(std::move(portableInstaller));
}
std::vector<PortableFileEntry> GetDesiredStateForPortableInstall(Execution::Context& context)
{
std::filesystem::path& installerPath = context.Get<Execution::Data::InstallerPath>();
PortableInstaller& portableInstaller = context.Get<Execution::Data::PortableInstaller>();
std::vector<PortableFileEntry> entries;
const std::filesystem::path& targetInstallDirectory = portableInstaller.TargetInstallLocation;
const std::filesystem::path& symlinkDirectory = GetPortableLinksLocation(portableInstaller.GetScope());
// InstallerPath will point to a directory if it is extracted from an archive.
if (std::filesystem::is_directory(installerPath))
{
portableInstaller.RecordToIndex = true;
// Build a map of installed target path → SHA256 as file entries are created,
// so nested installer files that need hardlinks can reuse the hash without re-reading from disk.
std::map<std::filesystem::path, std::string> fileHashes;
for (const auto& entry : std::filesystem::directory_iterator(installerPath))
{
std::filesystem::path entryPath = entry.path();
std::filesystem::path relativePath = std::filesystem::relative(entryPath, entryPath.parent_path());
std::filesystem::path targetPath = targetInstallDirectory / relativePath;
if (std::filesystem::is_directory(entryPath))
{
entries.emplace_back(PortableFileEntry::CreateDirectoryEntry(entryPath, targetPath));
}
else
{
const PortableFileEntry& fileEntry = entries.emplace_back(PortableFileEntry::CreateFileEntry(entryPath, targetPath, {}));
fileHashes[std::filesystem::weakly_canonical(targetPath)] = fileEntry.SHA256;
}
}
const std::vector<Manifest::NestedInstallerFile>& nestedInstallerFiles = context.Get<Execution::Data::Installer>()->NestedInstallerFiles;
for (const auto& nestedInstallerFile : nestedInstallerFiles)
{
const std::filesystem::path& relativeFilePath = ConvertToUTF16(nestedInstallerFile.RelativeFilePath);
const std::filesystem::path& targetPath = targetInstallDirectory / relativeFilePath;
std::filesystem::path originalFilename = targetPath.filename();
std::filesystem::path commandAlias;
if (nestedInstallerFile.PortableCommandAlias.empty())
{
commandAlias = originalFilename;
}
else
{
commandAlias = ConvertToUTF16(nestedInstallerFile.PortableCommandAlias);
}
Filesystem::AppendExtension(commandAlias, ".exe");
// If alias differs from original filename, create hardlink
// Hardlink will be placed in the same directory as the original file to avoid pathing issues and same-volume restrictions
if (commandAlias != originalFilename)
{
std::filesystem::path hardlinkPath = targetPath.parent_path() / commandAlias;
auto it = fileHashes.find(std::filesystem::weakly_canonical(targetPath));
std::string sha256 = (it != fileHashes.end()) ? it->second : std::string{};
entries.emplace_back(PortableFileEntry::CreateHardlinkEntry(hardlinkPath, targetPath, sha256));
}
entries.emplace_back(PortableFileEntry::CreateSymlinkEntry(symlinkDirectory / commandAlias, targetPath));
}
}
else
{
// Non-archive portable case: single executable file
std::string_view renameArg = context.Args.GetArg(Execution::Args::Type::Rename);
const std::vector<string_t>& commands = context.Get<Execution::Data::Installer>()->Commands;
std::filesystem::path originalFilename = installerPath.filename();
std::filesystem::path commandAlias = originalFilename;
// Determine the command alias from rename arg, commands, or use original filename
if (!commands.empty())
{
commandAlias = ConvertToUTF16(commands[0]);
}
if (!renameArg.empty())
{
commandAlias = ConvertToUTF16(renameArg);
}
Filesystem::AppendExtension(commandAlias, ".exe");
// Target path for the original file (keeps its original name)
const std::filesystem::path& targetFullPath = targetInstallDirectory / originalFilename;
// Create file entry for original (with original name) - this computes SHA256
std::string fileSha256 = Utility::SHA256::ConvertToString(Utility::SHA256::ComputeHashFromFile(installerPath));
entries.emplace_back(PortableFileEntry::CreateFileEntry(installerPath, targetFullPath, fileSha256));
// If alias differs from original filename, create hardlink
if (commandAlias != originalFilename)
{
std::filesystem::path hardlinkPath = targetInstallDirectory / commandAlias;
entries.emplace_back(PortableFileEntry::CreateHardlinkEntry(hardlinkPath, targetFullPath, fileSha256));
}
entries.emplace_back(PortableFileEntry::CreateSymlinkEntry(symlinkDirectory / commandAlias, targetFullPath));
}
return entries;
}
void PortableInstallImpl(Execution::Context& context)
{
OperationType installType = WI_IsFlagSet(context.GetFlags(), Execution::ContextFlag::InstallerExecutionUseUpdate) ? OperationType::Upgrade : OperationType::Install;
PortableInstaller& portableInstaller = context.Get<Execution::Data::PortableInstaller>();
try
{
context.Reporter.Info() << Resource::String::InstallFlowStartingPackageInstall << std::endl;
std::vector<AppInstaller::Portable::PortableFileEntry> desiredState = GetDesiredStateForPortableInstall(context);
portableInstaller.SetDesiredState(desiredState);
if (!portableInstaller.VerifyExpectedState())
{
if (context.Args.Contains(Execution::Args::Type::Force))
{
context.Reporter.Warn() << Resource::String::PortableHashMismatchOverridden << std::endl;
}
else
{
context.Reporter.Warn() << Resource::String::PortableHashMismatchOverrideRequired << std::endl;
AICLI_TERMINATE_CONTEXT(APPINSTALLER_CLI_ERROR_PORTABLE_UNINSTALL_FAILED);
}
}
portableInstaller.Install(installType);
context.Add<Execution::Data::CorrelatedAppsAndFeaturesEntries>({ portableInstaller.GetAppsAndFeaturesEntry() });
context.Add<Execution::Data::OperationReturnCode>(ERROR_SUCCESS);
context.Reporter.Warn() << portableInstaller.GetOutputMessage();
}
catch (...)
{
context.Add<Execution::Data::OperationReturnCode>(Workflow::HandleException(context, std::current_exception()));
if (!portableInstaller.IsUpdate)
{
context.Reporter.Warn() << Resource::String::PortableInstallFailed << std::endl;
portableInstaller.PrepareForCleanUp();
; portableInstaller.Uninstall();
AICLI_TERMINATE_CONTEXT(APPINSTALLER_CLI_ERROR_PORTABLE_UNINSTALL_FAILED);
}
}
}
void PortableUninstallImpl(Execution::Context& context)
{
PortableInstaller& portableInstaller = context.Get<Execution::Data::PortableInstaller>();
try
{
context.Reporter.Info() << Resource::String::UninstallFlowStartingPackageUninstall << std::endl;
if (!portableInstaller.VerifyExpectedState())
{
if (context.Args.Contains(Execution::Args::Type::Force))
{
context.Reporter.Warn() << Resource::String::PortableHashMismatchOverridden << std::endl;
}
else
{
context.Reporter.Warn() << Resource::String::PortableHashMismatchOverrideRequired << std::endl;
AICLI_TERMINATE_CONTEXT(APPINSTALLER_CLI_ERROR_PORTABLE_UNINSTALL_FAILED);
}
}
portableInstaller.Purge = context.Args.Contains(Execution::Args::Type::Purge) ||
(!portableInstaller.IsUpdate && Settings::User().Get<Settings::Setting::UninstallPurgePortablePackage>() && !context.Args.Contains(Execution::Args::Type::Preserve));
portableInstaller.Uninstall();
context.Add<Execution::Data::OperationReturnCode>(ERROR_SUCCESS);
context.Reporter.Warn() << portableInstaller.GetOutputMessage();
}
catch (...)
{
context.Add<Execution::Data::OperationReturnCode>(Workflow::HandleException(context, std::current_exception()));
}
}
void EnsureSupportForPortableInstall(Execution::Context& context)
{
auto installerType = context.Get<Execution::Data::Installer>().value().EffectiveInstallerType();
if (installerType == InstallerTypeEnum::Portable)
{
context <<
EnsureValidArgsForPortableInstall <<
EnsureVolumeSupportsReparsePoints;
}
}
}