-
Notifications
You must be signed in to change notification settings - Fork 99
/
InstanceSelector.cpp
342 lines (291 loc) · 8.8 KB
/
InstanceSelector.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
// <copyright file="InstanceSelector.cpp" company="Microsoft Corporation">
// Copyright (C) Microsoft Corporation. All rights reserved.
// Licensed under the MIT license. See LICENSE.txt in the project root for license information.
// </copyright>
#include "stdafx.h"
using namespace std;
using std::placeholders::_1;
InstanceSelector::InstanceSelector(_In_ const CommandArgs& args, _In_ ILegacyProvider& provider, _In_opt_ ISetupHelper* pHelper) :
m_args(args),
m_provider(provider),
m_ullMinimumVersion(0),
m_ullMaximumVersion(0)
{
m_helper = pHelper;
if (m_helper)
{
auto version = args.get_Version();
if (!version.empty())
{
auto hr = m_helper->ParseVersionRange(version.c_str(), &m_ullMinimumVersion, &m_ullMaximumVersion);
if (FAILED(hr))
{
// Late evaluation of the -version parameter so throw like any other invalid parameter.
auto message = ResourceManager::FormatString(IDS_E_INVALIDVERSION, version.c_str());
throw win32_error(ERROR_INVALID_PARAMETER, message);
}
}
}
}
bool InstanceSelector::Less(const ISetupInstancePtr& a, const ISetupInstancePtr& b) const
{
static ci_less less;
bstr_t bstrVersionA, bstrVersionB;
ULONGLONG ullVersionA, ullVersionB;
FILETIME ftDateA, ftDateB;
// Compare versions.
auto hrA = a->GetInstallationVersion(bstrVersionA.GetAddress());
auto hrB = b->GetInstallationVersion(bstrVersionB.GetAddress());
if (SUCCEEDED(hrA) && SUCCEEDED(hrB))
{
if (m_helper)
{
hrA = m_helper->ParseVersion(bstrVersionA, &ullVersionA);
hrB = m_helper->ParseVersion(bstrVersionB, &ullVersionB);
if (SUCCEEDED(hrA) && SUCCEEDED(hrB))
{
if (ullVersionA != ullVersionB)
{
return ullVersionA < ullVersionB;
}
}
else
{
// a is less than b if we can't parse version for a but can for b.
return SUCCEEDED(hrB);
}
}
else
{
// If ISetupHelper is not available we have only legacy products, or very early pre-releases of VS2017.
// For version 10.0 and newer either should lexigraphically sort correctly.
return less(wstring(bstrVersionA), wstring(bstrVersionB));
}
}
else
{
// a is less than b if we can't get version for a but can for b.
return SUCCEEDED(hrB);
}
// Compare dates.
hrA = a->GetInstallDate(&ftDateA);
hrB = b->GetInstallDate(&ftDateB);
if (SUCCEEDED(hrA) && SUCCEEDED(hrB))
{
auto result = ::CompareFileTime(&ftDateA, &ftDateB);
if (0 == result)
{
auto message = ResourceManager::GetString(IDS_E_UNEXPECTEDDATE);
throw win32_error(E_UNEXPECTED, message);
}
return 0 > result;
}
else
{
// a is less than b if we can't get date for a but can for b.
return SUCCEEDED(hrB);
}
}
vector<ISetupInstancePtr> InstanceSelector::Select(_In_opt_ IEnumSetupInstances* pEnum) const
{
vector<ISetupInstancePtr> instances;
if (pEnum)
{
HRESULT hr = S_OK;
unsigned long celtFetched = 0;
ISetupInstance* pInstances[1] = {};
do
{
celtFetched = 0;
hr = pEnum->Next(1, pInstances, &celtFetched);
if (SUCCEEDED(hr) && celtFetched)
{
ISetupInstancePtr instance(pInstances[0], false);
if (IsMatch(instance))
{
instances.push_back(instance);
}
}
} while (SUCCEEDED(hr) && celtFetched);
}
if (m_args.get_Legacy() && m_provider.HasLegacyInstances())
{
vector<LPCWSTR> versions =
{
L"14.0",
L"12.0",
L"11.0",
L"10.0",
};
for (const auto version : versions)
{
ISetupInstancePtr instance;
// Currently only support version checks.
if (m_provider.TryGetLegacyInstance(version, &instance) && IsVersionMatch(instance))
{
instances.push_back(instance);
}
}
}
if (1 < instances.size())
{
if (m_args.get_Latest() || m_args.get_Sort())
{
sort(instances.begin(), instances.end(), [&](const ISetupInstancePtr& a, const ISetupInstancePtr& b) -> bool
{
// Reverse the sort with addition of -sort using existing algorithm.
return !Less(a, b);
});
if (m_args.get_Latest())
{
return vector<ISetupInstancePtr>
{
instances.front(),
};
}
}
}
return instances;
}
bool InstanceSelector::IsMatch(_In_ ISetupInstance* pInstance) const
{
_ASSERT(pInstance);
HRESULT hr = S_OK;
ISetupInstance2Ptr instance;
ISetupInstanceCatalogPtr catalog;
hr = pInstance->QueryInterface(&instance);
if (FAILED(hr))
{
// Only VS products were released before ISetupInstance2 was released, so if no workload requirements assume it matches.
return m_args.get_Requires().empty();
}
hr = pInstance->QueryInterface(&catalog);
if (SUCCEEDED(hr))
{
VARIANT_BOOL fPrerelease = VARIANT_FALSE;
hr = catalog->IsPrerelease(&fPrerelease);
if (SUCCEEDED(hr) && fPrerelease && !m_args.get_Prerelease())
{
return false;
}
}
if (!IsProductMatch(instance))
{
return false;
}
if (!IsVersionMatch(instance))
{
return false;
}
if (!IsWorkloadMatch(instance))
{
return false;
}
return true;
}
bool InstanceSelector::IsProductMatch(_In_ ISetupInstance2* pInstance) const
{
_ASSERT(pInstance);
ISetupPackageReferencePtr product;
auto hr = pInstance->GetProduct(&product);
if (FAILED(hr) || !product)
{
// Should always have a product so no match.
return false;
}
// Asterisk on command line will clear the array to find any products.
const auto products = m_args.get_Products();
if (products.empty())
{
return true;
}
const auto productId = GetId(product);
if (productId.empty())
{
return false;
}
const auto ci_equal_productId = bind(ci_equal(), productId, _1);
const auto it = find_if(products.begin(), products.end(), ci_equal_productId);
if (it == products.end())
{
return false;
}
return true;
}
bool InstanceSelector::IsWorkloadMatch(_In_ ISetupInstance2* pInstance) const
{
_ASSERT(pInstance);
const auto requires = m_args.get_Requires();
if (requires.empty())
{
// No workloads required matches every instance.
return true;
}
// Keep track of which requirements we matched.
typedef map<wstring, bool, ci_less> MapType;
MapType found;
for (const auto& require : requires)
{
found.emplace(make_pair(require, false));
}
LPSAFEARRAY psa = NULL;
auto hr = pInstance->GetPackages(&psa);
if (FAILED(hr))
{
return false;
}
SafeArray<ISetupPackageReference*> packages(psa);
for (const auto package : packages.Elements())
{
auto id = GetId(package);
auto it = found.find(id);
if (it != found.end())
{
it->second = true;
}
}
if (m_args.get_RequiresAny())
{
return any_of(found.begin(), found.end(), [](MapType::const_reference pair) -> bool
{
return pair.second;
});
}
return all_of(found.begin(), found.end(), [](MapType::const_reference pair) -> bool
{
return pair.second;
});
}
bool InstanceSelector::IsVersionMatch(_In_ ISetupInstance* pInstance) const
{
_ASSERT(pInstance);
if (!HasVersionRange())
{
return true;
}
bstr_t bstrInstallationVersion;
ULONGLONG ullInstallationVersion = 0;
auto hr = pInstance->GetInstallationVersion(bstrInstallationVersion.GetAddress());
if (FAILED(hr))
{
return false;
}
hr = m_helper->ParseVersion(bstrInstallationVersion, &ullInstallationVersion);
if (FAILED(hr))
{
return false;
}
return m_ullMinimumVersion <= ullInstallationVersion && ullInstallationVersion <= m_ullMaximumVersion;
}
wstring InstanceSelector::GetId(_In_ ISetupPackageReference* pPackageReference)
{
_ASSERT(pPackageReference);
bstr_t bstrId;
auto hr = pPackageReference->GetId(bstrId.GetAddress());
if (FAILED(hr))
{
// Exceptional since Id is required on all package references.
throw win32_error(hr);
}
return wstring(bstrId);
}