-
Notifications
You must be signed in to change notification settings - Fork 100
/
Copy pathInstanceSelector.cpp
209 lines (170 loc) · 5.1 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
// <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;
const ci_equal InstanceSelector::s_comparer;
InstanceSelector::InstanceSelector(_In_ const CommandArgs& args, _In_opt_ ISetupHelper* pHelper) :
m_args(args)
{
// Get the ISetupHelper (if implemented) if a version range is specified.
auto version = args.get_Version();
if (!version.empty())
{
m_helper = pHelper;
if (m_helper)
{
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);
}
}
}
}
vector<ISetupInstance*> InstanceSelector::Select(_In_ IEnumSetupInstances* pEnum) const
{
_ASSERT(pEnum);
HRESULT hr = S_OK;
unsigned long celtFetched = 0;
ISetupInstance* pInstances[1] = {};
vector<ISetupInstance*> instances;
do
{
celtFetched = 0;
hr = pEnum->Next(1, pInstances, &celtFetched);
if (SUCCEEDED(hr) && celtFetched)
{
auto pInstance = pInstances[0];
if (IsMatch(pInstance))
{
instances.push_back(pInstance);
}
}
} while (SUCCEEDED(hr) && celtFetched);
return instances;
}
bool InstanceSelector::IsMatch(_In_ ISetupInstance* pInstance) const
{
_ASSERT(pInstance);
HRESULT hr = S_OK;
ISetupInstance2Ptr instance;
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();
}
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))
{
// Should always have a product so no match.
return false;
}
const auto productId = GetId(product);
if (productId.empty())
{
return false;
}
const auto products = m_args.get_Products();
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);
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;
}
}
return all_of(found.begin(), found.end(), [](MapType::const_reference pair) -> bool
{
return pair.second;
});
}
bool InstanceSelector::IsVersionMatch(_In_ ISetupInstance* pInstance) const
{
_ASSERT(pInstance);
// m_helper will be NULL if no version range was specified or the interface was not yet implemented.
if (!m_helper)
{
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);
}