forked from microsoft/winget-cli
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathRuntime.cpp
More file actions
238 lines (194 loc) · 7.81 KB
/
Copy pathRuntime.cpp
File metadata and controls
238 lines (194 loc) · 7.81 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
// Copyright (c) Microsoft Corporation.
// Licensed under the MIT License.
#include "pch.h"
#include <binver/version.h>
#include "Public/winget/Runtime.h"
#include "Public/AppInstallerLogging.h"
#include "Public/AppInstallerStrings.h"
namespace AppInstaller::Runtime
{
using namespace Utility;
namespace
{
using namespace std::string_view_literals;
constexpr std::string_view s_PreviewBuildSuffix = "-preview"sv;
// 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;
}
}
bool IsRunningInPackagedContext()
{
static bool result = DoesCurrentProcessHaveIdentity();
return result;
}
LocIndString GetClientVersion()
{
std::ostringstream strstr;
strstr << VERSION_MAJOR << '.' << VERSION_MINOR << '.' << VERSION_BUILD;
if (!IsReleaseBuild())
{
strstr << s_PreviewBuildSuffix;
}
return LocIndString{ strstr.str() };
}
std::wstring GetPackageFamilyName()
{
UINT32 length = 0;
LONG returnValue = ::GetPackageFamilyName(GetCurrentProcess(), &length, nullptr);
if (returnValue == APPMODEL_ERROR_NO_PACKAGE)
{
return {};
}
if (returnValue != ERROR_INSUFFICIENT_BUFFER)
{
THROW_IF_WIN32_ERROR(returnValue);
}
std::wstring result(length, '\0');
returnValue = ::GetPackageFamilyName(GetCurrentProcess(), &length, &result[0]);
THROW_IF_WIN32_ERROR(returnValue);
THROW_HR_IF(E_UNEXPECTED, length == 0);
result.resize(length - 1);
return result;
}
LocIndString GetPackageVersion()
{
using namespace std::string_literals;
if (IsRunningInPackagedContext())
{
auto version = GetPACKAGE_VERSION();
if (!version)
{
// In the extremely unlikely event of a failure, this is merely a sentinel value
// to indicated such. The only other option is to completely prevent execution,
// which seems unnecessary.
return LocIndString{ "error"sv };
}
std::ostringstream strstr;
strstr << GetPackageName() << " v" << version->Major << '.' << version->Minor << '.' << version->Build << '.' << version->Revision;
return LocIndString{ strstr.str() };
}
else
{
// Calling code should avoid calling in when this is the case.
return LocIndString{ "none"sv };
}
}
LocIndString 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 LocIndString{ strstr.str() };
}
std::string GetOSRegion()
{
winrt::Windows::Globalization::GeographicRegion region;
return Utility::ConvertToUTF8(region.CodeTwoLetter());
}
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;
}
bool IsRunningAsAdmin()
{
return wil::test_token_membership(nullptr, SECURITY_NT_AUTHORITY, SECURITY_BUILTIN_DOMAIN_RID, DOMAIN_ALIAS_RID_ADMINS);
}
bool IsRunningAsSystem()
{
return wil::test_token_membership(nullptr, SECURITY_NT_AUTHORITY, SECURITY_LOCAL_SYSTEM_RID);
}
bool IsRunningAsAdminOrSystem()
{
return IsRunningAsAdmin() || IsRunningAsSystem();
}
bool IsRunningWithLimitedToken()
{
return wil::get_token_information<TOKEN_ELEVATION_TYPE>() == TokenElevationTypeLimited;
}
DECLSPEC_NOINLINE bool IsStackAvailable(size_t bytes)
{
// https://devblogs.microsoft.com/oldnewthing/20200610-00/?p=103855
ULONG_PTR low, high;
GetCurrentThreadStackLimits(&low, &high);
auto remaining = reinterpret_cast<ULONG_PTR>(&low) - low;
if (remaining > high - low)
{
// Choosing to return false instead of failing
return false;
}
ULONG guarantee = 0;
SetThreadStackGuarantee(&guarantee);
return remaining >= bytes + guarantee;
}
}