forked from microsoft/winget-cli
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathCompositeSource.cpp
More file actions
1159 lines (984 loc) · 50.3 KB
/
Copy pathCompositeSource.cpp
File metadata and controls
1159 lines (984 loc) · 50.3 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
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
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
// Copyright (c) Microsoft Corporation.
// Licensed under the MIT License.
#include "pch.h"
#include "CompositeSource.h"
namespace AppInstaller::Repository
{
using namespace std::string_view_literals;
namespace
{
Utility::VersionAndChannel GetVACFromVersion(IPackageVersion* packageVersion)
{
return {
Utility::Version(packageVersion->GetProperty(PackageVersionProperty::Version)),
Utility::Channel(packageVersion->GetProperty(PackageVersionProperty::Channel))
};
}
// Returns true for fields that provide a strong match; one that is not based on a heuristic.
bool IsStrongMatchField(PackageMatchField field)
{
switch (field)
{
case AppInstaller::Repository::PackageMatchField::PackageFamilyName:
case AppInstaller::Repository::PackageMatchField::ProductCode:
return true;
}
return false;
}
// Move returns if there is only one package in the matches that is strong; otherwise returns an empty value.
std::shared_ptr<IPackage> FindOnlyStrongMatchFieldResult(std::vector<ResultMatch>& matches)
{
std::shared_ptr<IPackage> result;
for (auto&& match : matches)
{
AICLI_LOG(Repo, Info, << " Checking match with package id: " << match.Package->GetProperty(PackageProperty::Id));
if (IsStrongMatchField(match.MatchCriteria.Field))
{
if (!result)
{
result = std::move(match.Package);
}
else
{
AICLI_LOG(Repo, Info, << " Found multiple packages with strong match fields");
result.reset();
break;
}
}
}
return result;
}
// Gets a single matching package from the results
template <typename MultipleIntro, typename Indeterminate>
std::shared_ptr<IPackage> GetMatchingPackage(std::vector<ResultMatch>& matches, MultipleIntro&& multipleIntro, Indeterminate&& indeterminate)
{
if (matches.empty())
{
return {};
}
else if (matches.size() == 1)
{
return std::move(matches[0].Package);
}
else
{
multipleIntro();
auto result = FindOnlyStrongMatchFieldResult(matches);
if (!result)
{
indeterminate();
}
return result;
}
}
// For a given package from a tracking catalog, get the latest write time.
// Look at all versions rather than just the latest to account for the potential of downgrading.
std::chrono::system_clock::time_point GetLatestTrackingPackageWriteTime(const std::shared_ptr<IPackage>& trackingPackage)
{
std::chrono::system_clock::time_point result{};
for (const auto& key : trackingPackage->GetAvailableVersionKeys())
{
auto version = trackingPackage->GetAvailableVersion(key);
if (version)
{
auto metadata = version->GetMetadata();
auto itr = metadata.find(PackageVersionMetadata::TrackingWriteTime);
if (itr != metadata.end())
{
std::int64_t unixEpoch = 0;
try
{
unixEpoch = std::stoll(itr->second);
}
CATCH_LOG();
std::chrono::system_clock::time_point versionTime = Utility::ConvertUnixEpochToSystemClock(unixEpoch);
if (versionTime > result)
{
result = versionTime;
}
}
}
}
return result;
}
// TODO: Note: Currently this function assumes the all versions in the available package is from one source.
// If one day we start adding support for available package from multiple sources, this function needs to be revisited.
std::string GetMappedInstalledVersion(const std::string& installedVersion, const std::shared_ptr<IPackage>& availablePackage)
{
// Stores raw versions value strings to run a preliminary check whether version mapping is needed.
std::vector<std::tuple<std::string, std::string, std::string>> rawVersionValues;
auto versionKeys = availablePackage->GetAvailableVersionKeys();
bool shouldTryPerformMapping = false;
for (auto const& versionKey : versionKeys)
{
auto availableVersion = availablePackage->GetAvailableVersion(versionKey);
std::string arpMinVersion = availableVersion->GetProperty(PackageVersionProperty::ArpMinVersion);
std::string arpMaxVersion = availableVersion->GetProperty(PackageVersionProperty::ArpMaxVersion);
if (!arpMinVersion.empty() && !arpMaxVersion.empty())
{
std::string manifestVersion = versionKey.Version;
if (!shouldTryPerformMapping && (arpMinVersion != manifestVersion || arpMaxVersion != manifestVersion))
{
shouldTryPerformMapping = true;
}
rawVersionValues.emplace_back(std::make_tuple(std::move(manifestVersion), std::move(arpMinVersion), std::move(arpMaxVersion)));
}
}
if (!shouldTryPerformMapping)
{
return installedVersion;
}
// Construct a map between manifest version and arp version range. The map is ordered in descending by package version.
std::vector<std::pair<Utility::Version, Utility::VersionRange>> arpVersionMap;
for (auto& tuple : rawVersionValues)
{
auto&& [manifestVersion, arpMinVersion, arpMaxVersion] = std::move(tuple);
Utility::VersionRange arpVersionRange{ Utility::Version(std::move(arpMinVersion)), Utility::Version(std::move(arpMaxVersion)) };
Utility::Version manifestVer{ std::move(manifestVersion) };
// Skip mapping to unknown version
if (!manifestVer.IsUnknown())
{
arpVersionMap.emplace_back(std::make_pair(std::move(manifestVer), std::move(arpVersionRange)));
}
}
// Go through the arp version map and determine what mapping should be performed.
// shouldPerformMapping is true when at least 1 arp version range is different from the package version.
bool shouldPerformMapping = false;
bool isArpVersionRangeInDescendingOrder = true;
const Utility::VersionRange* previousVersionRange = nullptr;
for (auto const& pair : arpVersionMap)
{
// If arp version range is not same as package version, should perform mapping
// This check is still needed to account for 1.0 == 1.0.0 cases
if (!shouldPerformMapping && !pair.second.IsSameAsSingleVersion(pair.first))
{
shouldPerformMapping = true;
}
if (!previousVersionRange)
{
// This is the first non empty arp version range
previousVersionRange = &pair.second;
}
else if (isArpVersionRangeInDescendingOrder)
{
// The arp version range should be less than previous range
if (pair.second < *previousVersionRange)
{
previousVersionRange = &pair.second;
}
else
{
isArpVersionRangeInDescendingOrder = false;
}
}
}
// Now perform arp version mapping
if (shouldPerformMapping)
{
Utility::Version installed{ installedVersion };
for (auto const& pair : arpVersionMap)
{
// If the installed version is in the arp version range
if (pair.second.ContainsVersion(installed))
{
return pair.first.ToString();
}
}
// At this point, no mapping found. Perform approximate mapping if applicable.
// We'll start from end of the vector because we try to find closest less than version if possible.
if (isArpVersionRangeInDescendingOrder)
{
const Utility::Version* lastGreaterThanVersion = nullptr;
auto it = arpVersionMap.rbegin();
while (it != arpVersionMap.rend())
{
const auto& pair = *it;
if (installed < pair.second.GetMinVersion())
{
return Utility::Version{ pair.first, Utility::Version::ApproximateComparator::LessThan }.ToString();
}
else
{
lastGreaterThanVersion = &pair.first;
}
it++;
}
// No approximate less than version found, approximate greater than version will be returned.
if (lastGreaterThanVersion)
{
return Utility::Version{ *lastGreaterThanVersion, Utility::Version::ApproximateComparator::GreaterThan }.ToString();
}
}
}
// return the input installed version if no mapping is performed or found.
return installedVersion;
}
// A composite package installed version that allows us to override the source or the version.
struct CompositeInstalledVersion : public IPackageVersion
{
CompositeInstalledVersion(std::shared_ptr<IPackageVersion> baseInstalledVersion, Source trackingSource, std::string overrideVersion = {}) :
m_baseInstalledVersion(std::move(baseInstalledVersion)), m_trackingSource(std::move(trackingSource)), m_overrideVersion(std::move(overrideVersion))
{}
Utility::LocIndString GetProperty(PackageVersionProperty property) const override
{
// If there is an override version, use it.
if (property == PackageVersionProperty::Version && !m_overrideVersion.empty())
{
return Utility::LocIndString{ m_overrideVersion };
}
return m_baseInstalledVersion->GetProperty(property);
}
std::vector<Utility::LocIndString> GetMultiProperty(PackageVersionMultiProperty property) const override
{
return m_baseInstalledVersion->GetMultiProperty(property);
}
Manifest::Manifest GetManifest() override
{
return m_baseInstalledVersion->GetManifest();
}
Source GetSource() const override
{
// If there is a tracking source, use it instead to indicate that it came from there.
if (m_trackingSource)
{
return m_trackingSource;
}
return m_baseInstalledVersion->GetSource();
}
Metadata GetMetadata() const override
{
return m_baseInstalledVersion->GetMetadata();
}
private:
std::shared_ptr<IPackageVersion> m_baseInstalledVersion;
Source m_trackingSource;
std::string m_overrideVersion;
};
// A composite package for the CompositeSource.
struct CompositePackage : public IPackage
{
CompositePackage(std::shared_ptr<IPackage> installedPackage, std::shared_ptr<IPackage> availablePackage = {}) :
m_installedPackage(std::move(installedPackage)), m_availablePackage(std::move(availablePackage))
{
// Grab the installed version's channel to allow for filtering in calls to get available info.
if (m_installedPackage)
{
auto installedVersion = m_installedPackage->GetInstalledVersion();
if (installedVersion)
{
m_installedChannel = installedVersion->GetProperty(PackageVersionProperty::Channel);
}
}
TrySetOverrideInstalledVersion();
}
Utility::LocIndString GetProperty(PackageProperty property) const override
{
std::shared_ptr<IPackageVersion> truth = GetLatestAvailableVersion();
if (!truth && m_trackingPackage)
{
truth = m_trackingPackage->GetLatestAvailableVersion();
}
if (!truth)
{
truth = GetInstalledVersion();
}
switch (property)
{
case PackageProperty::Id:
return truth->GetProperty(PackageVersionProperty::Id);
case PackageProperty::Name:
return truth->GetProperty(PackageVersionProperty::Name);
default:
THROW_HR(E_UNEXPECTED);
}
}
std::shared_ptr<IPackageVersion> GetInstalledVersion() const override
{
if (m_installedPackage)
{
auto installedVersion = m_installedPackage->GetInstalledVersion();
if (installedVersion)
{
return std::make_shared<CompositeInstalledVersion>(std::move(installedVersion), m_trackingSource, m_overrideInstalledVersion);
}
}
return {};
}
std::vector<PackageVersionKey> GetAvailableVersionKeys() const override
{
if (m_availablePackage)
{
std::vector<PackageVersionKey> result = m_availablePackage->GetAvailableVersionKeys();
std::string_view channel = m_installedChannel;
// Remove all elements whose channel does not match the installed package.
result.erase(
std::remove_if(result.begin(), result.end(), [&](const PackageVersionKey& pvk) { return !Utility::ICUCaseInsensitiveEquals(pvk.Channel, channel); }),
result.end());
return result;
}
return {};
}
std::shared_ptr<IPackageVersion> GetLatestAvailableVersion() const override
{
return GetAvailableVersion({ "", "", m_installedChannel.get() });
}
std::shared_ptr<IPackageVersion> GetAvailableVersion(const PackageVersionKey& versionKey) const override
{
if (m_availablePackage)
{
return m_availablePackage->GetAvailableVersion(versionKey);
}
return {};
}
bool IsUpdateAvailable() const override
{
auto installed = GetInstalledVersion();
if (!installed)
{
return false;
}
auto latest = GetLatestAvailableVersion();
return (latest && (GetVACFromVersion(installed.get()).IsUpdatedBy(GetVACFromVersion(latest.get()))));
}
bool IsSame(const IPackage* other) const override
{
const CompositePackage* otherComposite = dynamic_cast<const CompositePackage*>(other);
if (!otherComposite ||
static_cast<bool>(m_installedPackage) != static_cast<bool>(otherComposite->m_installedPackage) ||
(m_installedPackage && !m_installedPackage->IsSame(otherComposite->m_installedPackage.get())) ||
static_cast<bool>(m_availablePackage) != static_cast<bool>(otherComposite->m_availablePackage) ||
(m_availablePackage && !m_availablePackage->IsSame(otherComposite->m_availablePackage.get())))
{
return false;
}
return true;
}
const std::shared_ptr<IPackage>& GetInstalledPackage()
{
return m_installedPackage;
}
const std::shared_ptr<IPackage>& GetAvailablePackage()
{
return m_availablePackage;
}
const std::shared_ptr<IPackage>& GetTrackingPackage()
{
return m_trackingPackage;
}
void SetAvailablePackage(std::shared_ptr<IPackage> availablePackage)
{
m_availablePackage = std::move(availablePackage);
TrySetOverrideInstalledVersion();
}
void SetTracking(Source trackingSource, std::shared_ptr<IPackage> trackingPackage)
{
m_trackingSource = std::move(trackingSource);
m_trackingPackage = std::move(trackingPackage);
}
private:
void TrySetOverrideInstalledVersion()
{
if (m_installedPackage && m_availablePackage)
{
auto installedVersion = m_installedPackage->GetInstalledVersion();
if (installedVersion)
{
auto installedType = Manifest::ConvertToInstallerTypeEnum(installedVersion->GetMetadata()[PackageVersionMetadata::InstalledType]);
if (Manifest::DoesInstallerTypeSupportArpVersionRange(installedType))
{
m_overrideInstalledVersion = GetMappedInstalledVersion(installedVersion->GetProperty(PackageVersionProperty::Version), m_availablePackage);
}
}
}
}
std::shared_ptr<IPackage> m_installedPackage;
Utility::LocIndString m_installedChannel;
std::shared_ptr<IPackage> m_availablePackage;
Source m_trackingSource;
std::shared_ptr<IPackage> m_trackingPackage;
std::string m_overrideInstalledVersion;
};
// The comparator compares the ResultMatch by MatchType first, then Field in a predefined order.
struct ResultMatchComparator
{
template <typename U, typename V>
bool operator() (
const U& match1,
const V& match2)
{
if (match1.MatchCriteria.Type != match2.MatchCriteria.Type)
{
return match1.MatchCriteria.Type < match2.MatchCriteria.Type;
}
if (match1.MatchCriteria.Field != match2.MatchCriteria.Field)
{
return match1.MatchCriteria.Field < match2.MatchCriteria.Field;
}
return false;
}
};
template <typename T>
void SortResultMatches(std::vector<T>& matches)
{
std::stable_sort(matches.begin(), matches.end(), ResultMatchComparator());
}
// A copy of the standard match that holds a CompositePackage instead.
struct CompositeResultMatch
{
std::shared_ptr<CompositePackage> Package;
PackageMatchFilter MatchCriteria;
CompositeResultMatch(std::shared_ptr<CompositePackage> p, PackageMatchFilter f) : Package(std::move(p)), MatchCriteria(std::move(f)) {}
};
// Stores data to enable correlation between installed and available packages.
struct CompositeResult
{
// A system reference string.
struct SystemReferenceString
{
SystemReferenceString(PackageMatchField field, Utility::LocIndString string) :
Field(field), String1(string) {}
SystemReferenceString(PackageMatchField field, Utility::LocIndString string1, Utility::LocIndString string2) :
Field(field), String1(string1), String2(string2) {}
bool operator<(const SystemReferenceString& other) const
{
if (Field != other.Field)
{
return Field < other.Field;
}
if (String1 != other.String1)
{
return String1 < other.String1;
}
return String2 < other.String2;
}
bool operator==(const SystemReferenceString& other) const
{
return Field == other.Field && String1 == other.String1 && String2 == other.String2;
}
void AddToFilters(std::vector<PackageMatchFilter>& filters) const
{
switch (Field)
{
case PackageMatchField::NormalizedNameAndPublisher:
filters.emplace_back(PackageMatchFilter(Field, MatchType::Exact, String1.get(), String2.get()));
break;
default:
filters.emplace_back(PackageMatchFilter(Field, MatchType::Exact, String1.get()));
}
}
private:
PackageMatchField Field;
Utility::LocIndString String1;
Utility::LocIndString String2;
};
// Data relevant to correlation for a package.
struct PackageData
{
std::set<SystemReferenceString> SystemReferenceStrings;
void AddIfNotPresent(SystemReferenceString&& srs)
{
if (SystemReferenceStrings.find(srs) == SystemReferenceStrings.end())
{
SystemReferenceStrings.emplace(std::move(srs));
}
}
SearchRequest CreateInclusionsSearchRequest() const
{
SearchRequest result;
for (const auto& srs : SystemReferenceStrings)
{
srs.AddToFilters(result.Inclusions);
}
return result;
}
};
// For a given package version, prepares the results for it.
PackageData GetSystemReferenceStrings(IPackageVersion* version)
{
PackageData result;
AddSystemReferenceStrings(version, result);
return result;
}
// Check for a package already in the result that should have been correlated already.
// If we find one, see if we should upgrade it's match criteria.
// If we don't, return package data for further use.
std::optional<PackageData> CheckForExistingResultFromAvailablePackageMatch(const ResultMatch& availableMatch)
{
for (auto& match : Matches)
{
const std::shared_ptr<IPackage>& availablePackage = match.Package->GetAvailablePackage();
if (availablePackage && availablePackage->IsSame(availableMatch.Package.get()))
{
if (ResultMatchComparator{}(availableMatch, match))
{
match.MatchCriteria = availableMatch.MatchCriteria;
}
return {};
}
}
PackageData result;
for (auto const& versionKey : availableMatch.Package->GetAvailableVersionKeys())
{
auto packageVersion = availableMatch.Package->GetAvailableVersion(versionKey);
AddSystemReferenceStrings(packageVersion.get(), result);
}
return result;
}
// Check for a package already in the result that should have been correlated already.
// If we find one, see if we should upgrade it's match criteria.
// If we don't, return package data for further use.
std::optional<PackageData> CheckForExistingResultFromTrackingPackageMatch(const ResultMatch& trackingMatch)
{
for (auto& match : Matches)
{
const std::shared_ptr<IPackage>& trackingPackage = match.Package->GetTrackingPackage();
if (trackingPackage && trackingPackage->IsSame(trackingMatch.Package.get()))
{
if (ResultMatchComparator{}(trackingMatch, match))
{
match.MatchCriteria = trackingMatch.MatchCriteria;
}
return {};
}
}
PackageData result;
for (auto const& versionKey : trackingMatch.Package->GetAvailableVersionKeys())
{
auto packageVersion = trackingMatch.Package->GetAvailableVersion(versionKey);
AddSystemReferenceStrings(packageVersion.get(), result);
}
return result;
}
// Determines if the results contain the given installed package.
bool ContainsInstalledPackage(const IPackage* installedPackage)
{
for (auto& match : Matches)
{
const std::shared_ptr<IPackage>& matchPackage = match.Package->GetInstalledPackage();
if (matchPackage && matchPackage->IsSame(installedPackage))
{
return true;
}
}
return false;
}
// Destructively converts the result to the standard variant.
operator SearchResult() &&
{
SearchResult result;
result.Matches.reserve(Matches.size());
for (auto& match : Matches)
{
result.Matches.emplace_back(std::move(match.Package), std::move(match.MatchCriteria));
}
result.Truncated = Truncated;
result.Failures = std::move(Failures);
return result;
}
bool AddFailureIfSourceNotPresent(SearchResult::Failure&& failure)
{
auto itr = std::find_if(Failures.begin(), Failures.end(),
[&failure](const SearchResult::Failure& present) {
return present.SourceName == failure.SourceName;
});
if (itr == Failures.end())
{
Failures.emplace_back(std::move(failure));
return true;
}
return false;
}
SearchResult SearchAndHandleFailures(const Source& source, const SearchRequest& request)
{
SearchResult result;
try
{
result = source.Search(request);
}
catch (...)
{
if (AddFailureIfSourceNotPresent({ source.GetDetails().Name, std::current_exception() }))
{
LOG_CAUGHT_EXCEPTION();
AICLI_LOG(Repo, Warning, << "Failed to search source for correlation: " << source.GetDetails().Name);
}
}
// Move failures into the result
for (SearchResult::Failure& failure : result.Failures)
{
AddFailureIfSourceNotPresent(std::move(failure));
}
return result;
}
std::vector<CompositeResultMatch> Matches;
bool Truncated = false;
std::vector<SearchResult::Failure> Failures;
private:
void AddSystemReferenceStrings(IPackageVersion* version, PackageData& data)
{
GetSystemReferenceStrings(
version,
PackageVersionMultiProperty::PackageFamilyName,
PackageMatchField::PackageFamilyName,
data);
GetSystemReferenceStrings(
version,
PackageVersionMultiProperty::ProductCode,
PackageMatchField::ProductCode,
data);
GetNameAndPublisher(
version,
data);
}
void GetSystemReferenceStrings(
IPackageVersion* installedVersion,
PackageVersionMultiProperty prop,
PackageMatchField field,
PackageData& data)
{
for (auto&& string : installedVersion->GetMultiProperty(prop))
{
data.AddIfNotPresent(SystemReferenceString{ field, std::move(string) });
}
}
void GetNameAndPublisher(
IPackageVersion* installedVersion,
PackageData& data)
{
// Unfortunately the names and publishers are unique and not tied to each other strictly, so we need
// to go broad on the matches. Future work can hopefully make name and publisher operate more as a unit,
// but for now we have to search for the cartesian of these...
auto names = installedVersion->GetMultiProperty(PackageVersionMultiProperty::Name);
auto publishers = installedVersion->GetMultiProperty(PackageVersionMultiProperty::Publisher);
for (size_t i = 0; i < names.size(); ++i)
{
for (size_t j = 0; j < publishers.size(); ++j)
{
data.AddIfNotPresent(SystemReferenceString{
PackageMatchField::NormalizedNameAndPublisher,
names[i],
publishers[j] });
}
}
}
};
std::shared_ptr<IPackage> GetTrackedPackageFromAvailableSource(CompositeResult& result, const Source& source, const Utility::LocIndString& identifier)
{
SearchRequest directRequest;
directRequest.Filters.emplace_back(PackageMatchField::Id, MatchType::CaseInsensitive, identifier.get());
SearchResult directResult = result.SearchAndHandleFailures(source, directRequest);
if (directResult.Matches.empty())
{
AICLI_LOG(Repo, Warning, << "Did not find Id [" << identifier << "] in tracked source: " << source.GetDetails().Name);
}
else if (directResult.Matches.size() == 1)
{
return std::move(directResult.Matches[0].Package);
}
else
{
AICLI_LOG(Repo, Warning, << "Found multiple results for Id [" << identifier << "] in tracked source: " << source.GetDetails().Name);
}
return {};
}
}
CompositeSource::CompositeSource(std::string identifier)
{
m_details.Identifier = std::move(identifier);
}
const SourceDetails& CompositeSource::GetDetails() const
{
return m_details;
}
const std::string& CompositeSource::GetIdentifier() const
{
return m_details.Identifier;
}
// The composite search needs to take several steps to get results, and due to the
// potential for different information spread across multiple sources, base searches
// need to be performed in both installed and available.
//
// If an installed source is present, then the searches should only return packages
// that are installed. This means that the base searches against available sources
// will only return results where a match is found in the installed source.
SearchResult CompositeSource::Search(const SearchRequest& request) const
{
if (m_installedSource)
{
return SearchInstalled(request);
}
else
{
return SearchAvailable(request);
}
}
void CompositeSource::AddAvailableSource(const Source& source)
{
m_availableSources.emplace_back(source);
}
void CompositeSource::SetInstalledSource(Source source, CompositeSearchBehavior searchBehavior)
{
m_installedSource = std::move(source);
m_searchBehavior = searchBehavior;
}
// An installed search first finds all installed packages that match the request, then correlates with available sources.
// Next the search is performed against the available sources and correlated with the installed source. A result will only
// be added if there exists an installed package that was not found by the initial search.
// This allows for search terms to find installed packages by their available metadata, as well as the local values.
//
// Search flow:
// Installed :: Search incoming request
// For each result
// For each available source
// Tracking :: Search system references
// If tracking found
// Available :: Search tracking ID
// If no available, for each available source
// Available :: Search system references
//
// For each available source
// Tracking :: Search incoming request
// For each result
// Installed :: Search system references
// If found
// Available :: Search tracking ID
// Available :: Search incoming request
// For each result
// Installed :: Search system references
SearchResult CompositeSource::SearchInstalled(const SearchRequest& request) const
{
CompositeResult result;
// If the search behavior is for AllPackages or Installed then the result can contain packages that are
// only in the Installed source, but do not have an AvailableVersion.
if (m_searchBehavior == CompositeSearchBehavior::AllPackages || m_searchBehavior == CompositeSearchBehavior::Installed)
{
// Search installed source (allow exceptions out as we own the installed source)
SearchResult installedResult = m_installedSource.Search(request);
result.Truncated = installedResult.Truncated;
for (auto&& match : installedResult.Matches)
{
if (!match.Package)
{
// Ensure that the crash from installedVersion below is not from the actual package being null.
AICLI_LOG(Repo, Warning, << "CompositeSource: The match of the package (matched on " <<
ToString(match.MatchCriteria.Field) << " => '" << match.MatchCriteria.Value <<
"') was null and is being dropped from the results.");
continue;
}
auto compositePackage = std::make_shared<CompositePackage>(match.Package);
auto installedVersion = compositePackage->GetInstalledVersion();
if (!installedVersion)
{
// One would think that the installed version coming directly from our own installed source
// would never be null, but it is sometimes. Rather than making users suffer through crashes
// that break their entire experience, lets log a few things and then ignore this match.
AICLI_LOG(Repo, Warning, << "CompositeSource: The installed version of the package '" <<
match.Package->GetProperty(PackageProperty::Id) << "' was null and is being dropped from the results.");
continue;
}
auto installedPackageData = result.GetSystemReferenceStrings(installedVersion.get());
// Create a search request to run against all available sources
if (!installedPackageData.SystemReferenceStrings.empty())
{
SearchRequest systemReferenceSearch = installedPackageData.CreateInclusionsSearchRequest();
Source trackedSource;
std::shared_ptr<IPackage> trackingPackage;
std::chrono::system_clock::time_point trackingPackageTime;
std::shared_ptr<IPackage> availablePackage;
// Check the tracking catalog first to see if there is a correlation there.
// TODO: When the issue with support for multiple available packages is fixed, this should move into
// the below available sources loop as we will check all sources at that point.
for (const auto& source : m_availableSources)
{
auto trackingCatalog = source.GetTrackingCatalog();
SearchResult trackingResult = trackingCatalog.Search(systemReferenceSearch);
std::shared_ptr<IPackage> candidatePackage = GetMatchingPackage(trackingResult.Matches,
[&]() {
AICLI_LOG(Repo, Info,
<< "Found multiple matches for installed package [" << installedVersion->GetProperty(PackageVersionProperty::Id) <<
"] in tracking catalog for source [" << source.GetIdentifier() << "] when searching for [" << systemReferenceSearch.ToString() << "]");
}, [&] {
AICLI_LOG(Repo, Warning, << " Appropriate tracking package could not be determined");
});
// Determine the candidate package with the latest install time
if (candidatePackage)
{
std::chrono::system_clock::time_point candidateTime = GetLatestTrackingPackageWriteTime(candidatePackage);
if (!trackingPackage || candidateTime > trackingPackageTime)
{
trackedSource = source;
trackingPackage = std::move(candidatePackage);
trackingPackageTime = candidateTime;
}
}
}
// Directly search for the available package from tracking information.
if (trackingPackage)
{
availablePackage = GetTrackedPackageFromAvailableSource(result, trackedSource, trackingPackage->GetProperty(PackageProperty::Id));
}
if (!availablePackage)
{
// Search sources and add to result
for (const auto& source : m_availableSources)
{
// Do not attempt to correlate local packages against this source
if (!source.GetDetails().SupportInstalledSearchCorrelation)
{
continue;
}
SearchResult availableResult = result.SearchAndHandleFailures(source, systemReferenceSearch);
if (availableResult.Matches.empty())
{
continue;
}
availablePackage = GetMatchingPackage(availableResult.Matches,
[&]() {
AICLI_LOG(Repo, Info,
<< "Found multiple matches for installed package [" << installedVersion->GetProperty(PackageVersionProperty::Id) <<
"] in source [" << source.GetIdentifier() << "] when searching for [" << systemReferenceSearch.ToString() << "]");
}, [&] {
AICLI_LOG(Repo, Warning, << " Appropriate available package could not be determined");
});
// We found some matching packages here, don't keep going
break;
}
}
compositePackage->SetAvailablePackage(std::move(availablePackage));
compositePackage->SetTracking(std::move(trackedSource), std::move(trackingPackage));
}
// Move the installed result into the composite result
result.Matches.emplace_back(std::move(compositePackage), std::move(match.MatchCriteria));
}
// Optimization for the "everything installed" case, no need to allow for reverse correlations