aboutsummaryrefslogtreecommitdiff
path: root/src/burn/engine/relatedbundle.cpp
blob: 938b24d73d3a640f96cf38481565c2c2bec247fa (plain)
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
// Copyright (c) .NET Foundation and contributors. All rights reserved. Licensed under the Microsoft Reciprocal License. See LICENSE.TXT file in the project root for full license information.

#include "precomp.h"

typedef struct _BUNDLE_QUERY_CONTEXT
{
    BURN_REGISTRATION* pRegistration;
    BURN_RELATED_BUNDLES* pRelatedBundles;
} BUNDLE_QUERY_CONTEXT;

// internal function declarations

static __callback int __cdecl CompareRelatedBundlesDetect(
    __in void* pvContext,
    __in const void* pvLeft,
    __in const void* pvRight
    );
static __callback int __cdecl CompareRelatedBundlesPlan(
    __in void* /*pvContext*/,
    __in const void* pvLeft,
    __in const void* pvRight
    );
static BUNDLE_QUERY_CALLBACK_RESULT CALLBACK QueryRelatedBundlesCallback(
    __in const BUNDLE_QUERY_RELATED_BUNDLE_RESULT* pBundle,
    __in_opt LPVOID pvContext
    );
static HRESULT LoadIfRelatedBundle(
    __in const BUNDLE_QUERY_RELATED_BUNDLE_RESULT* pBundle,
    __in BURN_REGISTRATION* pRegistration,
    __in BURN_RELATED_BUNDLES* pRelatedBundles
    );
static HRESULT LoadRelatedBundleFromKey(
    __in_z LPCWSTR wzRelatedBundleCode,
    __in HKEY hkBundleCode,
    __in BOOL fPerMachine,
    __in BOOTSTRAPPER_RELATION_TYPE relationType,
    __in BURN_RELATED_BUNDLE *pRelatedBundle
    );


// function definitions

extern "C" HRESULT RelatedBundlesInitializeForScope(
    __in BOOL fPerMachine,
    __in BURN_REGISTRATION* pRegistration,
    __in BURN_RELATED_BUNDLES* pRelatedBundles
    )
{
    HRESULT hr = S_OK;
    BUNDLE_INSTALL_CONTEXT installContext = fPerMachine ? BUNDLE_INSTALL_CONTEXT_MACHINE : BUNDLE_INSTALL_CONTEXT_USER;
    BUNDLE_QUERY_CONTEXT queryContext = { };

    queryContext.pRegistration = pRegistration;
    queryContext.pRelatedBundles = pRelatedBundles;

    hr = BundleQueryRelatedBundles(
        installContext,
        const_cast<LPCWSTR*>(pRegistration->rgsczDetectCodes),
        pRegistration->cDetectCodes,
        const_cast<LPCWSTR*>(pRegistration->rgsczUpgradeCodes),
        pRegistration->cUpgradeCodes,
        const_cast<LPCWSTR*>(pRegistration->rgsczAddonCodes),
        pRegistration->cAddonCodes,
        const_cast<LPCWSTR*>(pRegistration->rgsczPatchCodes),
        pRegistration->cPatchCodes,
        QueryRelatedBundlesCallback,
        &queryContext);
    ExitOnFailure(hr, "Failed to initialize related bundles for scope.");

LExit:
    return hr;
}

extern "C" void RelatedBundlesUninitialize(
    __in BURN_RELATED_BUNDLES* pRelatedBundles
    )
{
    if (pRelatedBundles->rgRelatedBundles)
    {
        for (DWORD i = 0; i < pRelatedBundles->cRelatedBundles; ++i)
        {
            BURN_PACKAGE* pPackage = &pRelatedBundles->rgRelatedBundles[i].package;

            for (DWORD j = 0; j < pPackage->payloads.cItems; ++j)
            {
                PayloadUninitialize(pPackage->payloads.rgItems[j].pPayload);
            }

            PackageUninitialize(pPackage);
            ReleaseStr(pRelatedBundles->rgRelatedBundles[i].sczTag);
        }

        MemFree(pRelatedBundles->rgRelatedBundles);
    }

    ReleaseMem(pRelatedBundles->rgpPlanSortedRelatedBundles);

    memset(pRelatedBundles, 0, sizeof(BURN_RELATED_BUNDLES));
}


extern "C" HRESULT RelatedBundleFindById(
    __in BURN_RELATED_BUNDLES* pRelatedBundles,
    __in_z LPCWSTR wzId,
    __out BURN_RELATED_BUNDLE** ppRelatedBundle
    )
{
    HRESULT hr = S_OK;
    BURN_RELATED_BUNDLE* pRelatedBundle = NULL;
    BURN_PACKAGE* pPackage = NULL;

    *ppRelatedBundle = NULL;

    for (DWORD i = 0; i < pRelatedBundles->cRelatedBundles; ++i)
    {
        pRelatedBundle = pRelatedBundles->rgRelatedBundles + i;
        pPackage = &pRelatedBundle->package;

        if (CSTR_EQUAL == ::CompareStringW(LOCALE_INVARIANT, 0, pPackage->sczId, -1, wzId, -1))
        {
            *ppRelatedBundle = pRelatedBundle;
            ExitFunction1(hr = S_OK);
        }
    }

    hr = E_NOTFOUND;

LExit:
    return hr;
}

extern "C" void RelatedBundlesSortDetect(
    __in BURN_RELATED_BUNDLES* pRelatedBundles
    )
{
    qsort_s(pRelatedBundles->rgRelatedBundles, pRelatedBundles->cRelatedBundles, sizeof(BURN_RELATED_BUNDLE), CompareRelatedBundlesDetect, NULL);
}

extern "C" void RelatedBundlesSortPlan(
    __in BURN_RELATED_BUNDLES* pRelatedBundles
    )
{
    qsort_s(pRelatedBundles->rgpPlanSortedRelatedBundles, pRelatedBundles->cRelatedBundles, sizeof(BURN_RELATED_BUNDLE*), CompareRelatedBundlesPlan, NULL);
}

extern "C" BOOTSTRAPPER_RELATION_TYPE RelatedBundleConvertRelationType(
    __in BUNDLE_RELATION_TYPE relationType
    )
{
    switch (relationType)
    {
    case BUNDLE_RELATION_DETECT:
        return BOOTSTRAPPER_RELATION_DETECT;
    case BUNDLE_RELATION_UPGRADE:
        return BOOTSTRAPPER_RELATION_UPGRADE;
    case BUNDLE_RELATION_ADDON:
        return BOOTSTRAPPER_RELATION_ADDON;
    case BUNDLE_RELATION_PATCH:
        return BOOTSTRAPPER_RELATION_PATCH;
    case BUNDLE_RELATION_DEPENDENT_ADDON:
        return BOOTSTRAPPER_RELATION_DEPENDENT_ADDON;
    case BUNDLE_RELATION_DEPENDENT_PATCH:
        return BOOTSTRAPPER_RELATION_DEPENDENT_PATCH;
    default:
        AssertSz(BUNDLE_RELATION_NONE == relationType, "Unknown BUNDLE_RELATION_TYPE");
        return BOOTSTRAPPER_RELATION_NONE;
    }
}


// internal helper functions

static __callback int __cdecl CompareRelatedBundlesDetect(
    __in void* /*pvContext*/,
    __in const void* pvLeft,
    __in const void* pvRight
    )
{
    int ret = 0;
    const BURN_RELATED_BUNDLE* pBundleLeft = static_cast<const BURN_RELATED_BUNDLE*>(pvLeft);
    const BURN_RELATED_BUNDLE* pBundleRight = static_cast<const BURN_RELATED_BUNDLE*>(pvRight);

    // Sort by relation type, then version, then bundle code.
    if (pBundleLeft->detectRelationType != pBundleRight->detectRelationType)
    {
        // Upgrade bundles last, everything else according to the enum.
        if (BOOTSTRAPPER_RELATION_UPGRADE == pBundleLeft->detectRelationType)
        {
            ret = 1;
        }
        else if (BOOTSTRAPPER_RELATION_UPGRADE == pBundleRight->detectRelationType)
        {
            ret = -1;
        }
        else if (pBundleLeft->detectRelationType < pBundleRight->detectRelationType)
        {
            ret = -1;
        }
        else
        {
            ret = 1;
        }
    }
    else
    {
        VerCompareParsedVersions(pBundleLeft->pVersion, pBundleRight->pVersion, &ret);
        if (0 == ret)
        {
            ret = ::CompareStringW(LOCALE_INVARIANT, NORM_IGNORECASE, pBundleLeft->package.sczId, -1, pBundleRight->package.sczId, -1) - 2;
        }
    }

    return ret;
}

static __callback int __cdecl CompareRelatedBundlesPlan(
    __in void* /*pvContext*/,
    __in const void* pvLeft,
    __in const void* pvRight
    )
{
    int ret = 0;
    const BURN_RELATED_BUNDLE* pBundleLeft = *reinterpret_cast<BURN_RELATED_BUNDLE**>(const_cast<void*>(pvLeft));
    const BURN_RELATED_BUNDLE* pBundleRight = *reinterpret_cast<BURN_RELATED_BUNDLE**>(const_cast<void*>(pvRight));

    // Sort by relation type, then version, then bundle code.
    if (pBundleLeft->planRelationType != pBundleRight->planRelationType)
    {
        // Upgrade bundles last, everything else according to the enum.
        if (BOOTSTRAPPER_RELATED_BUNDLE_PLAN_TYPE_UPGRADE == pBundleLeft->planRelationType)
        {
            ret = 1;
        }
        else if (BOOTSTRAPPER_RELATED_BUNDLE_PLAN_TYPE_UPGRADE == pBundleRight->planRelationType)
        {
            ret = -1;
        }
        else if (pBundleLeft->planRelationType < pBundleRight->planRelationType)
        {
            ret = -1;
        }
        else
        {
            ret = 1;
        }
    }
    else
    {
        VerCompareParsedVersions(pBundleLeft->pVersion, pBundleRight->pVersion, &ret);
        if (0 == ret)
        {
            ret = ::CompareStringW(LOCALE_INVARIANT, NORM_IGNORECASE, pBundleLeft->package.sczId, -1, pBundleRight->package.sczId, -1) - 2;
        }
    }

    return ret;
}

static BUNDLE_QUERY_CALLBACK_RESULT CALLBACK QueryRelatedBundlesCallback(
    __in const BUNDLE_QUERY_RELATED_BUNDLE_RESULT* pBundle,
    __in_opt LPVOID pvContext
    )
{
    HRESULT hr = S_OK;
    BUNDLE_QUERY_CALLBACK_RESULT result = BUNDLE_QUERY_CALLBACK_RESULT_CONTINUE;
    BUNDLE_QUERY_CONTEXT* pContext = reinterpret_cast<BUNDLE_QUERY_CONTEXT*>(pvContext);

    hr = LoadIfRelatedBundle(pBundle, pContext->pRegistration, pContext->pRelatedBundles);
    ExitOnFailure(hr, "Failed to load related bundle: %ls", pBundle->wzBundleCode);

LExit:
    return result;
}

static HRESULT LoadIfRelatedBundle(
    __in const BUNDLE_QUERY_RELATED_BUNDLE_RESULT* pBundle,
    __in BURN_REGISTRATION* pRegistration,
    __in BURN_RELATED_BUNDLES* pRelatedBundles
    )
{
    HRESULT hr = S_OK;
    BOOL fPerMachine = BUNDLE_INSTALL_CONTEXT_MACHINE == pBundle->installContext;
    BOOTSTRAPPER_RELATION_TYPE relationType = RelatedBundleConvertRelationType(pBundle->relationType);
    BURN_RELATED_BUNDLE* pRelatedBundle = NULL;

    // If we found our bundle code, it's not a related bundle.
    if (CSTR_EQUAL == ::CompareStringW(LOCALE_NEUTRAL, NORM_IGNORECASE, pBundle->wzBundleCode, -1, pRegistration->sczCode, -1))
    {
        ExitFunction1(hr = S_FALSE);
    }

    hr = MemEnsureArraySize(reinterpret_cast<LPVOID*>(&pRelatedBundles->rgRelatedBundles), pRelatedBundles->cRelatedBundles + 1, sizeof(BURN_RELATED_BUNDLE), 5);
    ExitOnFailure(hr, "Failed to ensure there is space for related bundles.");

    pRelatedBundle = pRelatedBundles->rgRelatedBundles + pRelatedBundles->cRelatedBundles;

    hr = LoadRelatedBundleFromKey(pBundle->wzBundleCode, pBundle->hkBundle, fPerMachine, relationType, pRelatedBundle);
    ExitOnFailure(hr, "Failed to initialize package from related bundle code: %ls", pBundle->wzBundleCode);

    hr = DependencyDetectRelatedBundle(pRelatedBundle, pRegistration);
    ExitOnFailure(hr, "Failed to detect dependencies for related bundle.");

    ++pRelatedBundles->cRelatedBundles;

LExit:
    return hr;
}

static HRESULT LoadRelatedBundleFromKey(
    __in_z LPCWSTR wzRelatedBundleCode,
    __in HKEY hkBundleCode,
    __in BOOL fPerMachine,
    __in BOOTSTRAPPER_RELATION_TYPE relationType,
    __in BURN_RELATED_BUNDLE* pRelatedBundle
    )
{
    HRESULT hr = S_OK;
    DWORD64 qwEngineVersion = 0;
    DWORD dwEngineProtocolVersion = 0;
    BOOL fSupportsBurnProtocol = FALSE;
    LPWSTR sczBundleVersion = NULL;
    LPWSTR sczCachePath = NULL;
    BOOL fCached = FALSE;
    DWORD64 qwFileSize = 0;
    BOOL fExists = FALSE;
    BURN_DEPENDENCY_PROVIDER dependencyProvider = { };
    BURN_DEPENDENCY_PROVIDER* pBundleDependencyProvider = NULL;

    // Only support progress from engines that are compatible.
    hr = RegReadNumber(hkBundleCode, BURN_REGISTRATION_REGISTRY_ENGINE_PROTOCOL_VERSION, &dwEngineProtocolVersion);
    if (SUCCEEDED(hr))
    {
        fSupportsBurnProtocol = BURN_PROTOCOL_VERSION == dwEngineProtocolVersion;
    }
    else
    {
        // Rely on version checking (aka: version greater than or equal to last protocol breaking change *and* versions that are older or the same as this engine)
        hr = RegReadVersion(hkBundleCode, BURN_REGISTRATION_REGISTRY_ENGINE_VERSION, &qwEngineVersion);
        if (SUCCEEDED(hr))
        {
            fSupportsBurnProtocol = (FILEMAKEVERSION(3, 6, 2221, 0) <= qwEngineVersion && qwEngineVersion <= FILEMAKEVERSION(rmj, rmm, rup, rpr));
        }

        hr = S_OK;
    }

    hr = RegReadString(hkBundleCode, BURN_REGISTRATION_REGISTRY_BUNDLE_VERSION, &sczBundleVersion);
    ExitOnFailure(hr, "Failed to read version from registry for bundle: %ls", wzRelatedBundleCode);

    hr = VerParseVersion(sczBundleVersion, 0, FALSE, &pRelatedBundle->pVersion);
    ExitOnFailure(hr, "Failed to parse pseudo bundle version: %ls", sczBundleVersion);

    if (pRelatedBundle->pVersion->fInvalid)
    {
        LogId(REPORT_WARNING, MSG_RELATED_PACKAGE_INVALID_VERSION, wzRelatedBundleCode, sczBundleVersion);
    }

    hr = RegReadString(hkBundleCode, BURN_REGISTRATION_REGISTRY_BUNDLE_CACHE_PATH, &sczCachePath);
    ExitOnFailure(hr, "Failed to read cache path from registry for bundle: %ls", wzRelatedBundleCode);

    if (FileExistsEx(sczCachePath, NULL))
    {
        fCached = TRUE;
    }
    else
    {
        LogId(REPORT_STANDARD, MSG_DETECT_RELATED_BUNDLE_NOT_CACHED, wzRelatedBundleCode, sczCachePath);
    }

    pRelatedBundle->fPlannable = fCached;

    hr = RegReadString(hkBundleCode, BURN_REGISTRATION_REGISTRY_BUNDLE_PROVIDER_KEY, &dependencyProvider.sczKey);
    ExitOnPathFailure(hr, fExists, "Failed to read provider key from registry for bundle: %ls", wzRelatedBundleCode);

    if (dependencyProvider.sczKey && *dependencyProvider.sczKey)
    {
        pBundleDependencyProvider = &dependencyProvider;

        dependencyProvider.fImported = TRUE;

        hr = StrAllocString(&dependencyProvider.sczVersion, pRelatedBundle->pVersion->sczVersion, 0);
        ExitOnFailure(hr, "Failed to copy version for bundle: %ls", wzRelatedBundleCode);

        hr = RegReadString(hkBundleCode, BURN_REGISTRATION_REGISTRY_BUNDLE_DISPLAY_NAME, &dependencyProvider.sczDisplayName);
        ExitOnPathFailure(hr, fExists, "Failed to copy display name for bundle: %ls", wzRelatedBundleCode);
    }

    hr = RegReadString(hkBundleCode, BURN_REGISTRATION_REGISTRY_BUNDLE_TAG, &pRelatedBundle->sczTag);
    ExitOnPathFailure(hr, fExists, "Failed to read tag from registry for bundle: %ls", wzRelatedBundleCode);

    pRelatedBundle->detectRelationType = relationType;

    hr = PseudoBundleInitializeRelated(&pRelatedBundle->package, fSupportsBurnProtocol, fPerMachine, wzRelatedBundleCode,
#ifdef DEBUG
                                       pRelatedBundle->detectRelationType,
#endif
                                       fCached, sczCachePath, qwFileSize, pBundleDependencyProvider);
    ExitOnFailure(hr, "Failed to initialize related bundle to represent bundle: %ls", wzRelatedBundleCode);

LExit:
    DependencyUninitializeProvider(&dependencyProvider);
    ReleaseStr(sczCachePath);
    ReleaseStr(sczBundleVersion);

    return hr;
}