-
Notifications
You must be signed in to change notification settings - Fork 1.5k
/
Copy pathRuntime.cpp
426 lines (352 loc) · 14 KB
/
Runtime.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
// Copyright (c) Microsoft Corporation.
// Licensed under the MIT License.
#include "pch.h"
#include <binver/version.h>
#include "Public/AppInstallerRuntime.h"
#include "Public/AppInstallerStrings.h"
#define AICLI_DEFAULT_TEMP_DIRECTORY "WinGet"
#define WINGET_DEFAULT_LOG_DIRECTORY "DiagOutputDir"
namespace AppInstaller::Runtime
{
namespace
{
using namespace std::string_view_literals;
constexpr std::string_view s_AppDataDir_Settings = "Settings";
constexpr std::string_view s_AppDataDir_State = "State";
// Gets a boolean indicating whether the current process has identity.
bool DoesCurrentProcessHaveIdentity()
{
UINT32 length = 0;
LONG result = GetPackageFamilyName(GetCurrentProcess(), &length, nullptr);
return (result != APPMODEL_ERROR_NO_PACKAGE);
}
std::unique_ptr<byte[]> GetPACKAGE_ID()
{
UINT32 bufferLength = 0;
LONG gcpiResult = GetCurrentPackageId(&bufferLength, nullptr);
THROW_HR_IF(E_UNEXPECTED, gcpiResult != ERROR_INSUFFICIENT_BUFFER);
std::unique_ptr<byte[]> buffer = std::make_unique<byte[]>(bufferLength);
gcpiResult = GetCurrentPackageId(&bufferLength, buffer.get());
if (FAILED_WIN32_LOG(gcpiResult))
{
return {};
}
return buffer;
}
// Gets the package name; only succeeds if running in a packaged context.
std::string GetPackageName()
{
std::unique_ptr<byte[]> buffer = GetPACKAGE_ID();
if (!buffer)
{
return {};
}
PACKAGE_ID* packageId = reinterpret_cast<PACKAGE_ID*>(buffer.get());
return Utility::ConvertToUTF8(packageId->name);
}
// Gets the package version; only succeeds if running in a packaged context.
std::optional<PACKAGE_VERSION> GetPACKAGE_VERSION()
{
std::unique_ptr<byte[]> buffer = GetPACKAGE_ID();
if (!buffer)
{
return {};
}
PACKAGE_ID* packageId = reinterpret_cast<PACKAGE_ID*>(buffer.get());
return packageId->version;
}
#ifndef AICLI_DISABLE_TEST_HOOKS
static std::filesystem::path s_Settings_TestHook_ForcedContainerPrepend;
#endif
void ValidateSettingNamePath(std::filesystem::path& name)
{
THROW_HR_IF(E_INVALIDARG, !name.has_relative_path());
THROW_HR_IF(E_INVALIDARG, name.has_root_path());
THROW_HR_IF(E_INVALIDARG, !name.has_filename());
}
// Gets the container within LocalSettings for the given path.
auto GetLocalSettingsContainerForPath(const std::filesystem::path& name)
{
auto result = winrt::Windows::Storage::ApplicationData::Current().LocalSettings();
std::filesystem::path pathToUse;
#ifndef AICLI_DISABLE_TEST_HOOKS
if (!s_Settings_TestHook_ForcedContainerPrepend.empty())
{
pathToUse = s_Settings_TestHook_ForcedContainerPrepend;
pathToUse /= name;
}
else
#endif
{
pathToUse = name;
}
for (const auto& part : pathToUse.parent_path())
{
auto partHstring = winrt::to_hstring(part.c_str());
result = result.CreateContainer(partHstring, winrt::Windows::Storage::ApplicationDataCreateDisposition::Always);
}
return result;
}
// Gets the path to the appdata root.
// *Only used by non packaged version!*
std::filesystem::path GetPathToAppDataRoot()
{
THROW_HR_IF(E_NOT_VALID_STATE, IsRunningInPackagedContext());
DWORD charCount = ExpandEnvironmentStringsW(L"%LOCALAPPDATA%", nullptr, 0);
THROW_LAST_ERROR_IF(charCount == 0);
std::wstring localAppDataPath(charCount + 1, L'\0');
charCount = ExpandEnvironmentStringsW(L"%LOCALAPPDATA%", &localAppDataPath[0], charCount + 1);
THROW_LAST_ERROR_IF(charCount == 0);
localAppDataPath.resize(charCount - 1);
std::filesystem::path result = localAppDataPath;
result /= "Microsoft/WinGet";
#ifndef AICLI_DISABLE_TEST_HOOKS
if (!s_Settings_TestHook_ForcedContainerPrepend.empty())
{
result /= s_Settings_TestHook_ForcedContainerPrepend;
}
#endif
return result;
}
// Gets the path to the app data relative directory.
// Creates the directory if it does not already exist.
std::filesystem::path GetPathToAppDataDir(const std::filesystem::path& relative)
{
THROW_HR_IF(E_INVALIDARG, !relative.has_relative_path());
THROW_HR_IF(E_INVALIDARG, relative.has_root_path());
THROW_HR_IF(E_INVALIDARG, !relative.has_filename());
std::filesystem::path result = GetPathToAppDataRoot();
result /= relative;
if (std::filesystem::exists(result))
{
if (!std::filesystem::is_directory(result))
{
// STATUS_NOT_A_DIRECTORY: A requested opened file is not a directory.
THROW_NTSTATUS_MSG(0xC0000103, "AppData location is not a directory");
}
}
else
{
std::filesystem::create_directories(result);
}
return result;
}
// Gets the path to the settings directory for the given setting.
// Creates the directory if it does not already exist.
std::filesystem::path GetPathToSettings(const std::filesystem::path& name)
{
std::filesystem::path result = GetPathToAppDataDir(s_AppDataDir_Settings);
if (name.has_parent_path())
{
result /= name.parent_path();
std::filesystem::create_directories(result);
}
return result;
}
}
bool IsRunningInPackagedContext()
{
static bool result = DoesCurrentProcessHaveIdentity();
return result;
}
std::string GetClientVersion()
{
using namespace std::string_literals;
if (IsRunningInPackagedContext())
{
auto version = GetPACKAGE_VERSION();
if (!version)
{
return "error"s;
}
std::ostringstream strstr;
strstr << VERSION_MAJOR << '.' << VERSION_MINOR << '.' << version->Build;
return strstr.str();
}
else
{
std::ostringstream strstr;
strstr << VERSION_MAJOR << '.' << VERSION_MINOR << '.' << VERSION_BUILD;
return strstr.str();
}
}
std::string GetPackageVersion()
{
using namespace std::string_literals;
if (IsRunningInPackagedContext())
{
auto version = GetPACKAGE_VERSION();
if (!version)
{
return "error"s;
}
std::ostringstream strstr;
strstr << GetPackageName() << " v" << version->Major << '.' << version->Minor << '.' << version->Build << '.' << version->Revision;
return strstr.str();
}
else
{
return "none";
}
}
std::string GetOSVersion()
{
winrt::Windows::System::Profile::AnalyticsInfo analyticsInfo{};
auto versionInfo = analyticsInfo.VersionInfo();
uint64_t version = std::stoull(Utility::ConvertToUTF8(versionInfo.DeviceFamilyVersion()));
uint16_t parts[4];
for (size_t i = 0; i < ARRAYSIZE(parts); ++i)
{
parts[i] = version & 0xFFFF;
version = version >> 16;
}
std::ostringstream strstr;
strstr << Utility::ConvertToUTF8(versionInfo.DeviceFamily()) << " v" << parts[3] << '.' << parts[2] << '.' << parts[1] << '.' << parts[0];
return strstr.str();
}
std::filesystem::path GetPathToTemp()
{
std::filesystem::path result;
if (IsRunningInPackagedContext())
{
result.assign(winrt::Windows::Storage::ApplicationData::Current().TemporaryFolder().Path().c_str());
}
else
{
wchar_t tempPath[MAX_PATH + 1];
DWORD tempChars = GetTempPathW(ARRAYSIZE(tempPath), tempPath);
result.assign(std::wstring_view{ tempPath, static_cast<size_t>(tempChars) });
}
result /= AICLI_DEFAULT_TEMP_DIRECTORY;
std::filesystem::create_directories(result);
return result;
}
std::filesystem::path GetPathToLocalState()
{
if (IsRunningInPackagedContext())
{
std::filesystem::path result = winrt::Windows::Storage::ApplicationData::Current().LocalFolder().Path().c_str();
#ifndef AICLI_DISABLE_TEST_HOOKS
if (!s_Settings_TestHook_ForcedContainerPrepend.empty())
{
result /= s_Settings_TestHook_ForcedContainerPrepend;
}
#endif
return result;
}
else
{
return GetPathToAppDataDir(s_AppDataDir_State);
}
}
std::filesystem::path GetPathToDefaultLogLocation()
{
if (IsRunningInPackagedContext())
{
// To enable UIF collection through Feedback hub, we must put our logs here.
auto result = GetPathToLocalState() / WINGET_DEFAULT_LOG_DIRECTORY;
std::filesystem::create_directories(result);
return result;
}
else
{
return GetPathToTemp();
}
}
std::unique_ptr<std::istream> GetSettingStream(std::filesystem::path name)
{
ValidateSettingNamePath(name);
if (IsRunningInPackagedContext())
{
auto container = GetLocalSettingsContainerForPath(name);
auto filenameHstring = winrt::to_hstring(name.filename().c_str());
auto settingsValues = container.Values();
if (settingsValues.HasKey(filenameHstring))
{
auto value = winrt::unbox_value<winrt::hstring>(settingsValues.Lookup(filenameHstring));
return std::make_unique<std::istringstream>(Utility::ConvertToUTF8(value.c_str()));
}
else
{
return {};
}
}
else
{
auto settingFileName = GetPathToSettings(name);
settingFileName /= name.filename();
if (std::filesystem::exists(settingFileName))
{
return std::make_unique<std::ifstream>(settingFileName);
}
else
{
return {};
}
}
}
void SetSetting(std::filesystem::path name, std::string_view value)
{
ValidateSettingNamePath(name);
if (IsRunningInPackagedContext())
{
GetLocalSettingsContainerForPath(name).Values().
Insert(winrt::to_hstring(name.filename().c_str()), winrt::box_value(winrt::to_hstring(value)));
}
else
{
auto settingFileName = GetPathToSettings(name);
settingFileName /= name.filename();
std::ofstream stream(settingFileName, std::ios_base::out | std::ios_base::binary | std::ios_base::trunc);
stream << value << std::flush;
}
}
void RemoveSetting(std::filesystem::path name)
{
ValidateSettingNamePath(name);
if (IsRunningInPackagedContext())
{
GetLocalSettingsContainerForPath(name).Values().Remove(winrt::to_hstring(name.filename().c_str()));
}
else
{
auto settingFileName = GetPathToSettings(name);
settingFileName /= name.filename();
std::filesystem::remove(settingFileName);
}
}
bool IsCurrentOSVersionGreaterThanOrEqual(const Utility::Version& version)
{
DWORD versionParts[3] = {};
for (size_t i = 0; i < ARRAYSIZE(versionParts) && i < version.GetParts().size(); ++i)
{
versionParts[i] = static_cast<DWORD>(std::min(static_cast<decltype(version.GetParts()[i].Integer)>(std::numeric_limits<DWORD>::max()), version.GetParts()[i].Integer));
}
OSVERSIONINFOEXW osVersionInfo{};
osVersionInfo.dwOSVersionInfoSize = sizeof(osVersionInfo);
osVersionInfo.dwMajorVersion = versionParts[0];
osVersionInfo.dwMinorVersion = versionParts[1];
osVersionInfo.dwBuildNumber = versionParts[2];
osVersionInfo.wServicePackMajor = 0;
osVersionInfo.wServicePackMinor = 0;
DWORD mask = VER_MAJORVERSION | VER_MINORVERSION | VER_BUILDNUMBER | VER_SERVICEPACKMAJOR | VER_SERVICEPACKMINOR;
DWORDLONG conditions = 0;
VER_SET_CONDITION(conditions, VER_MAJORVERSION, VER_GREATER_EQUAL);
VER_SET_CONDITION(conditions, VER_MINORVERSION, VER_GREATER_EQUAL);
VER_SET_CONDITION(conditions, VER_BUILDNUMBER, VER_GREATER_EQUAL);
VER_SET_CONDITION(conditions, VER_SERVICEPACKMAJOR, VER_GREATER_EQUAL);
VER_SET_CONDITION(conditions, VER_SERVICEPACKMINOR, VER_GREATER_EQUAL);
BOOL result = VerifyVersionInfoW(&osVersionInfo, mask, conditions);
if (!result)
{
THROW_LAST_ERROR_IF(GetLastError() != ERROR_OLD_WIN_VERSION);
}
return !!result;
}
#ifndef AICLI_DISABLE_TEST_HOOKS
void TestHook_ForceContainerPrepend(const std::filesystem::path& prepend)
{
s_Settings_TestHook_ForcedContainerPrepend = prepend;
}
#endif
}