forked from microsoft/winget-cli
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathRepositorySource.cpp
More file actions
725 lines (612 loc) · 27.3 KB
/
Copy pathRepositorySource.cpp
File metadata and controls
725 lines (612 loc) · 27.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
// Copyright (c) Microsoft Corporation.
// Licensed under the MIT License.
#include "pch.h"
#include "ISource.h"
#include "CompositeSource.h"
#include "SourceFactory.h"
#include "SourceList.h"
#include "SourcePolicy.h"
#include "Microsoft/PredefinedInstalledSourceFactory.h"
#include "Microsoft/PredefinedWriteableSourceFactory.h"
#include "Microsoft/PreIndexedPackageSourceFactory.h"
#include "Rest/RestSourceFactory.h"
#include "PackageTrackingCatalogSourceFactory.h"
#ifndef AICLI_DISABLE_TEST_HOOKS
#include "Microsoft/ConfigurableTestSourceFactory.h"
#endif
#include <winget/GroupPolicy.h>
using namespace AppInstaller::Settings;
using namespace std::chrono_literals;
namespace AppInstaller::Repository
{
namespace
{
#ifndef AICLI_DISABLE_TEST_HOOKS
static std::map<std::string, std::function<std::unique_ptr<ISourceFactory>()>> s_Sources_TestHook_SourceFactories;
#endif
std::shared_ptr<ISourceReference> CreateSourceFromDetails(const SourceDetails& details)
{
return ISourceFactory::GetForType(details.Type)->Create(details);
}
template <typename MemberFunc>
bool AddOrUpdateFromDetails(SourceDetails& details, MemberFunc member, IProgressCallback& progress)
{
bool result = false;
auto factory = ISourceFactory::GetForType(details.Type);
// Attempt; if it fails, wait a short time and retry.
try
{
result = (factory.get()->*member)(details, progress);
if (result)
{
details.LastUpdateTime = std::chrono::system_clock::now();
}
return result;
}
CATCH_LOG();
AICLI_LOG(Repo, Info, << "Source add/update failed, waiting a bit and retrying: " << details.Name);
std::this_thread::sleep_for(2s);
// If this one fails, maybe the problem is persistent.
result = (factory.get()->*member)(details, progress);
if (result)
{
details.LastUpdateTime = std::chrono::system_clock::now();
}
return result;
}
bool AddSourceFromDetails(SourceDetails& details, IProgressCallback& progress)
{
return AddOrUpdateFromDetails(details, &ISourceFactory::Add, progress);
}
bool UpdateSourceFromDetails(SourceDetails& details, IProgressCallback& progress)
{
return AddOrUpdateFromDetails(details, &ISourceFactory::Update, progress);
}
bool BackgroundUpdateSourceFromDetails(SourceDetails& details, IProgressCallback& progress)
{
return AddOrUpdateFromDetails(details, &ISourceFactory::BackgroundUpdate, progress);
}
bool RemoveSourceFromDetails(const SourceDetails& details, IProgressCallback& progress)
{
auto factory = ISourceFactory::GetForType(details.Type);
return factory->Remove(details, progress);
}
bool ContainsAvailablePackagesInternal(SourceOrigin origin)
{
return (origin == SourceOrigin::Default || origin == SourceOrigin::GroupPolicy || origin == SourceOrigin::User);
}
// Determines whether (and logs why) a source should be updated before it is opened.
bool ShouldUpdateBeforeOpen(const SourceDetails& details)
{
if (!ContainsAvailablePackagesInternal(details.Origin))
{
return false;
}
constexpr static auto s_ZeroMins = 0min;
auto autoUpdateTime = User().Get<Setting::AutoUpdateTimeInMinutes>();
// A value of zero means no auto update, to get update the source run `winget update`
if (autoUpdateTime != s_ZeroMins)
{
auto autoUpdateTimeMins = std::chrono::minutes(autoUpdateTime);
auto timeSinceLastUpdate = std::chrono::system_clock::now() - details.LastUpdateTime;
if (timeSinceLastUpdate > autoUpdateTimeMins)
{
AICLI_LOG(Repo, Info, << "Source past auto update time [" <<
std::chrono::duration_cast<std::chrono::minutes>(autoUpdateTimeMins).count() << " mins]; it has been at least " <<
std::chrono::duration_cast<std::chrono::minutes>(timeSinceLastUpdate).count() << " mins");
return true;
}
}
return false;
}
SourceDetails GetPredefinedSourceDetails(PredefinedSource source)
{
SourceDetails details;
details.Origin = SourceOrigin::Predefined;
switch (source)
{
case PredefinedSource::Installed:
details.Type = Microsoft::PredefinedInstalledSourceFactory::Type();
details.Arg = Microsoft::PredefinedInstalledSourceFactory::FilterToString(Microsoft::PredefinedInstalledSourceFactory::Filter::None);
return details;
case PredefinedSource::ARP:
details.Type = Microsoft::PredefinedInstalledSourceFactory::Type();
details.Arg = Microsoft::PredefinedInstalledSourceFactory::FilterToString(Microsoft::PredefinedInstalledSourceFactory::Filter::ARP);
return details;
case PredefinedSource::MSIX:
details.Type = Microsoft::PredefinedInstalledSourceFactory::Type();
details.Arg = Microsoft::PredefinedInstalledSourceFactory::FilterToString(Microsoft::PredefinedInstalledSourceFactory::Filter::MSIX);
return details;
case PredefinedSource::Installing:
details.Type = Microsoft::PredefinedWriteableSourceFactory::Type();
// As long as there is only one type this is not particularly needed, but Arg is exposed publicly
// so this is used here for consistency with other predefined sources.
details.Arg = Microsoft::PredefinedWriteableSourceFactory::TypeToString(Microsoft::PredefinedWriteableSourceFactory::WriteableType::Installing);
return details;
}
THROW_HR(E_UNEXPECTED);
}
// Carries the exception from an OpenSource call and presents it back at search time.
struct OpenExceptionProxy : public ISource, std::enable_shared_from_this<OpenExceptionProxy>
{
OpenExceptionProxy(const SourceDetails& details, std::exception_ptr exception) :
m_details(details), m_exception(std::move(exception)) {}
const SourceDetails& GetDetails() const override { return m_details; }
const std::string& GetIdentifier() const override { return m_details.Identifier; }
SearchResult Search(const SearchRequest&) const override
{
SearchResult result;
result.Failures.emplace_back(SearchResult::Failure{ GetDetails().Name, m_exception });
return result;
}
private:
SourceDetails m_details;
std::exception_ptr m_exception;
};
}
std::unique_ptr<ISourceFactory> ISourceFactory::GetForType(std::string_view type)
{
#ifndef AICLI_DISABLE_TEST_HOOKS
// Tests can ensure case matching
auto itr = s_Sources_TestHook_SourceFactories.find(std::string(type));
if (itr != s_Sources_TestHook_SourceFactories.end())
{
return itr->second();
}
if (Utility::CaseInsensitiveEquals(Microsoft::ConfigurableTestSourceFactory::Type(), type))
{
return Microsoft::ConfigurableTestSourceFactory::Create();
}
#endif
// For now, enable an empty type to represent the only one we have.
if (type.empty() ||
Utility::CaseInsensitiveEquals(Microsoft::PreIndexedPackageSourceFactory::Type(), type))
{
return Microsoft::PreIndexedPackageSourceFactory::Create();
}
// Should always come from code, so no need for case insensitivity
else if (Microsoft::PredefinedInstalledSourceFactory::Type() == type)
{
return Microsoft::PredefinedInstalledSourceFactory::Create();
}
// Should always come from code, so no need for case insensitivity
else if (Microsoft::PredefinedWriteableSourceFactory::Type() == type)
{
return Microsoft::PredefinedWriteableSourceFactory::Create();
}
// Should always come from code, so no need for case insensitivity
else if (PackageTrackingCatalogSourceFactory::Type() == type)
{
return PackageTrackingCatalogSourceFactory::Create();
}
else if (Utility::CaseInsensitiveEquals(Rest::RestSourceFactory::Type(), type))
{
return Rest::RestSourceFactory::Create();
}
THROW_HR(APPINSTALLER_CLI_ERROR_INVALID_SOURCE_TYPE);
}
std::string_view ToString(SourceOrigin origin)
{
switch (origin)
{
case SourceOrigin::Default:
return "Default"sv;
case SourceOrigin::User:
return "User"sv;
case SourceOrigin::Predefined:
return "Predefined"sv;
case SourceOrigin::GroupPolicy:
return "GroupPolicy"sv;
case SourceOrigin::Metadata:
return "Metadata"sv;
default:
THROW_HR(E_UNEXPECTED);
}
}
Source::Source() {}
Source::Source(std::string_view name)
{
InitializeSourceReference(name);
}
Source::Source(PredefinedSource source)
{
SourceDetails details = GetPredefinedSourceDetails(source);
m_sourceReferences.emplace_back(CreateSourceFromDetails(details));
}
Source::Source(WellKnownSource source)
{
SourceDetails details = GetWellKnownSourceDetailsInternal(source);
m_sourceReferences.emplace_back(CreateSourceFromDetails(details));
}
Source::Source(std::string_view name, std::string_view arg, std::string_view type)
{
m_isSourceToBeAdded = true;
SourceDetails details;
details.Name = name;
details.Arg = arg;
details.Type = type;
m_sourceReferences.emplace_back(CreateSourceFromDetails(details));
}
Source::Source(const std::vector<Source>& availableSources)
{
std::shared_ptr<CompositeSource> compositeSource = std::make_shared<CompositeSource>("*CompositeSource");
for (const auto& availableSource : availableSources)
{
THROW_HR_IF(E_INVALIDARG, !availableSource.m_source || availableSource.IsComposite());
compositeSource->AddAvailableSource(availableSource.m_source);
}
m_source = compositeSource;
m_isComposite = true;
}
Source::Source(const Source& installedSource, const Source& availableSource, CompositeSearchBehavior searchBehavior)
{
THROW_HR_IF(E_INVALIDARG, !installedSource.m_source || installedSource.m_isComposite || !availableSource.m_source);
std::shared_ptr<CompositeSource> compositeSource = std::dynamic_pointer_cast<CompositeSource>(availableSource.m_source);
if (!compositeSource)
{
compositeSource = std::make_shared<CompositeSource>("*CompositeSource");
compositeSource->AddAvailableSource(availableSource.m_source);
}
compositeSource->SetInstalledSource(installedSource, searchBehavior);
m_source = compositeSource;
m_isComposite = true;
}
Source::Source(std::shared_ptr<ISource> source) : m_source(std::move(source)) {}
Source::operator bool() const
{
return !m_sourceReferences.empty() || m_source != nullptr;
}
void Source::InitializeSourceReference(std::string_view name)
{
SourceList sourceList;
if (name.empty())
{
auto currentSources = sourceList.GetCurrentSourceRefs();
if (currentSources.empty())
{
AICLI_LOG(Repo, Info, << "Default source requested, but no sources configured");
}
else if (currentSources.size() == 1)
{
AICLI_LOG(Repo, Info, << "Default source requested, only 1 source available, using the only source: " << currentSources[0].get().Name);
InitializeSourceReference(currentSources[0].get().Name);
}
else
{
AICLI_LOG(Repo, Info, << "Default source requested, multiple sources available, adding all to source references.");
for (auto& source : currentSources)
{
AICLI_LOG(Repo, Info, << "Adding to source references " << source.get().Name);
m_sourceReferences.emplace_back(CreateSourceFromDetails(source));
}
m_isComposite = true;
}
}
else
{
auto source = sourceList.GetCurrentSource(name);
if (!source)
{
AICLI_LOG(Repo, Info, << "Named source requested, but not found: " << name);
}
else
{
AICLI_LOG(Repo, Info, << "Named source requested, found: " << source->Name);
m_sourceReferences.emplace_back(CreateSourceFromDetails(*source));
}
}
}
std::string Source::GetIdentifier() const
{
if (m_source)
{
return m_source->GetIdentifier();
}
else if (m_sourceReferences.size() == 1)
{
return m_sourceReferences[0]->GetIdentifier();
}
else
{
THROW_HR(HRESULT_FROM_WIN32(ERROR_INVALID_STATE));
}
}
SourceDetails Source::GetDetails() const
{
if (m_source)
{
return m_source->GetDetails();
}
else if (m_sourceReferences.size() == 1)
{
return m_sourceReferences[0]->GetDetails();
}
else
{
THROW_HR(HRESULT_FROM_WIN32(ERROR_INVALID_STATE));
}
}
SourceInformation Source::GetInformation() const
{
if (m_source && !m_isComposite)
{
return m_source->GetInformation();
}
else if (m_sourceReferences.size() == 1)
{
return m_sourceReferences[0]->GetInformation();
}
else
{
THROW_HR(HRESULT_FROM_WIN32(ERROR_INVALID_STATE));
}
}
bool Source::ContainsAvailablePackages() const
{
THROW_HR_IF(HRESULT_FROM_WIN32(ERROR_INVALID_STATE), IsComposite());
return ContainsAvailablePackagesInternal(GetDetails().Origin);
}
bool Source::SetCustomHeader(std::optional<std::string> header)
{
THROW_HR_IF(HRESULT_FROM_WIN32(ERROR_INVALID_STATE), m_sourceReferences.size() != 1);
return m_sourceReferences[0]->SetCustomHeader(header);
}
SearchResult Source::Search(const SearchRequest& request) const
{
THROW_HR_IF(HRESULT_FROM_WIN32(ERROR_INVALID_STATE), !m_source);
return m_source->Search(request);
}
ImplicitAgreementFieldEnum Source::GetAgreementFieldsFromSourceInformation() const
{
ImplicitAgreementFieldEnum result = ImplicitAgreementFieldEnum::None;
auto info = GetInformation();
if (info.RequiredPackageMatchFields.end() != std::find_if(info.RequiredPackageMatchFields.begin(), info.RequiredPackageMatchFields.end(), [&](const auto& field) { return Utility::CaseInsensitiveEquals(field, "market"); }) ||
info.RequiredQueryParameters.end() != std::find_if(info.RequiredQueryParameters.begin(), info.RequiredQueryParameters.end(), [&](const auto& param) { return Utility::CaseInsensitiveEquals(param, "market"); }))
{
WI_SetFlag(result, ImplicitAgreementFieldEnum::Market);
}
return result;
}
bool Source::CheckSourceAgreements() const
{
auto sourceName = GetDetails().Name;
auto agreementFields = GetAgreementFieldsFromSourceInformation();
auto agreementsIdentifier = GetInformation().SourceAgreementsIdentifier;
SourceList sourceList;
return sourceList.CheckSourceAgreements(sourceName, agreementsIdentifier, agreementFields);
}
void Source::SaveAcceptedSourceAgreements() const
{
auto sourceName = GetDetails().Name;
auto agreementFields = GetAgreementFieldsFromSourceInformation();
auto agreementsIdentifier = GetInformation().SourceAgreementsIdentifier;
SourceList sourceList;
return sourceList.SaveAcceptedSourceAgreements(sourceName, agreementsIdentifier, agreementFields);
}
bool Source::IsComposite() const
{
return m_isComposite;
}
std::vector<Source> Source::GetAvailableSources() const
{
THROW_HR_IF(HRESULT_FROM_WIN32(ERROR_INVALID_STATE), !m_source || !m_isComposite);
auto compositeSource = std::dynamic_pointer_cast<CompositeSource>(m_source);
THROW_HR_IF(HRESULT_FROM_WIN32(ERROR_INVALID_STATE), !compositeSource);
return compositeSource->GetAvailableSources();
}
void Source::AddPackageVersion(const Manifest::Manifest& manifest, const std::filesystem::path& relativePath)
{
THROW_HR_IF(HRESULT_FROM_WIN32(ERROR_INVALID_STATE), !m_source);
auto writableSource = std::dynamic_pointer_cast<IMutablePackageSource>(m_source);
THROW_HR_IF(HRESULT_FROM_WIN32(ERROR_INVALID_STATE), !writableSource);
writableSource->AddPackageVersion(manifest, relativePath);
}
void Source::RemovePackageVersion(const Manifest::Manifest& manifest, const std::filesystem::path& relativePath)
{
THROW_HR_IF(HRESULT_FROM_WIN32(ERROR_INVALID_STATE), !m_source);
auto writableSource = std::dynamic_pointer_cast<IMutablePackageSource>(m_source);
THROW_HR_IF(HRESULT_FROM_WIN32(ERROR_INVALID_STATE), !writableSource);
writableSource->RemovePackageVersion(manifest, relativePath);
}
std::vector<SourceDetails> Source::Open(IProgressCallback& progress)
{
THROW_HR_IF(HRESULT_FROM_WIN32(ERROR_INVALID_STATE), m_isSourceToBeAdded || m_sourceReferences.empty());
std::vector<SourceDetails> result;
if (!m_source)
{
SourceList sourceList;
// Check for updates before opening.
for (auto& sourceReference : m_sourceReferences)
{
auto& details = sourceReference->GetDetails();
if (ShouldUpdateBeforeOpen(details))
{
try
{
// TODO: Consider adding a context callback to indicate we are doing the same action
// to avoid the progress bar fill up multiple times.
if (BackgroundUpdateSourceFromDetails(details, progress))
{
auto detailsInternal = sourceList.GetSource(details.Name);
detailsInternal->LastUpdateTime = details.LastUpdateTime;
sourceList.SaveMetadata(*detailsInternal);
}
else
{
AICLI_LOG(Repo, Error, << "Failed to update source: " << details.Name);
result.emplace_back(details);
}
}
catch (...)
{
LOG_CAUGHT_EXCEPTION();
AICLI_LOG(Repo, Warning, << "Failed to update source: " << details.Name);
result.emplace_back(details);
}
}
}
if (m_sourceReferences.size() > 1)
{
AICLI_LOG(Repo, Info, << "Multiple sources available, creating aggregated source.");
auto aggregatedSource = std::make_shared<CompositeSource>("*DefaultSource");
std::vector<std::shared_ptr<OpenExceptionProxy>> openExceptionProxies;
for (auto& sourceReference : m_sourceReferences)
{
AICLI_LOG(Repo, Info, << "Adding to aggregated source: " << sourceReference->GetDetails().Name);
try
{
aggregatedSource->AddAvailableSource(sourceReference->Open(progress));
}
catch (...)
{
LOG_CAUGHT_EXCEPTION();
AICLI_LOG(Repo, Warning, << "Failed to open available source: " << sourceReference->GetDetails().Name);
openExceptionProxies.emplace_back(std::make_shared<OpenExceptionProxy>(sourceReference->GetDetails(), std::current_exception()));
}
}
// If all sources failed to open, then throw an exception that is specific to this case.
THROW_HR_IF(APPINSTALLER_CLI_ERROR_FAILED_TO_OPEN_ALL_SOURCES, !aggregatedSource->HasAvailableSource());
// Place all of the proxies into the source to be searched later
for (auto& proxy : openExceptionProxies)
{
aggregatedSource->AddAvailableSource(Source{ std::move(proxy) });
}
m_source = aggregatedSource;
}
else
{
m_source = m_sourceReferences[0]->Open(progress);
}
}
return result;
}
bool Source::Add(IProgressCallback& progress)
{
THROW_HR_IF(HRESULT_FROM_WIN32(ERROR_INVALID_STATE), !m_isSourceToBeAdded || m_sourceReferences.size() != 1);
auto& sourceDetails = m_sourceReferences[0]->GetDetails();
AICLI_LOG(Repo, Info, << "Adding source: Name[" << sourceDetails.Name << "], Type[" << sourceDetails.Type << "], Arg[" << sourceDetails.Arg << "]");
// Check all sources for the given name.
SourceList sourceList;
auto source = sourceList.GetSource(sourceDetails.Name);
THROW_HR_IF(APPINSTALLER_CLI_ERROR_SOURCE_NAME_ALREADY_EXISTS, source != nullptr && source->Origin != SourceOrigin::Metadata && !source->IsTombstone);
// Check sources allowed by group policy
auto blockingPolicy = GetPolicyBlockingUserSource(sourceDetails.Name, sourceDetails.Type, sourceDetails.Arg, false);
if (blockingPolicy != TogglePolicy::Policy::None)
{
throw GroupPolicyException(blockingPolicy);
}
sourceDetails.LastUpdateTime = Utility::ConvertUnixEpochToSystemClock(0);
sourceDetails.Origin = SourceOrigin::User;
bool result = AddSourceFromDetails(sourceDetails, progress);
if (result)
{
sourceList.AddSource(sourceDetails);
SaveAcceptedSourceAgreements();
m_isSourceToBeAdded = false;
AICLI_LOG(Repo, Info, << "Source created with extra data: " << sourceDetails.Data);
}
return result;
}
std::vector<SourceDetails> Source::Update(IProgressCallback& progress)
{
THROW_HR_IF(HRESULT_FROM_WIN32(ERROR_INVALID_STATE), m_isSourceToBeAdded || m_source || m_sourceReferences.empty());
SourceList sourceList;
std::vector<SourceDetails> result;
for (auto& sourceReference : m_sourceReferences)
{
THROW_HR_IF(HRESULT_FROM_WIN32(ERROR_INVALID_STATE), !ContainsAvailablePackagesInternal(sourceReference->GetDetails().Origin));
auto& details = sourceReference->GetDetails();
AICLI_LOG(Repo, Info, << "Named source to be updated, found: " << details.Name);
try
{
// TODO: Consider adding a context callback to indicate we are doing the same action
// to avoid the progress bar fill up multiple times.
if (UpdateSourceFromDetails(details, progress))
{
auto detailsInternal = sourceList.GetSource(details.Name);
detailsInternal->LastUpdateTime = details.LastUpdateTime;
sourceList.SaveMetadata(*detailsInternal);
}
else
{
AICLI_LOG(Repo, Error, << "Failed to update source: " << details.Name);
result.emplace_back(details);
}
}
catch (...)
{
LOG_CAUGHT_EXCEPTION();
AICLI_LOG(Repo, Error, << "Failed to update source: " << details.Name);
result.emplace_back(details);
}
}
return result;
}
bool Source::Remove(IProgressCallback& progress)
{
THROW_HR_IF(HRESULT_FROM_WIN32(ERROR_INVALID_STATE), m_isSourceToBeAdded || m_sourceReferences.size() != 1 || m_source);
const auto& details = m_sourceReferences[0]->GetDetails();
AICLI_LOG(Repo, Info, << "Named source to be removed, found: " << details.Name << " [" << ToString(details.Origin) << ']');
EnsureSourceIsRemovable(details);
bool result = RemoveSourceFromDetails(details, progress);
if (result)
{
SourceList sourceList;
sourceList.RemoveSource(details);
}
return result;
}
PackageTrackingCatalog Source::GetTrackingCatalog() const
{
if (!m_trackingCatalog)
{
m_trackingCatalog = PackageTrackingCatalog::CreateForSource(*this);
}
return m_trackingCatalog;
}
std::vector<SourceDetails> Source::GetCurrentSources()
{
SourceList sourceList;
std::vector<SourceDetails> result;
for (auto&& source : sourceList.GetCurrentSourceRefs())
{
result.emplace_back(std::move(source));
}
return result;
}
bool Source::DropSource(std::string_view name)
{
if (name.empty())
{
SourceList::RemoveSettingsStreams();
return true;
}
else
{
SourceList sourceList;
auto source = sourceList.GetCurrentSource(name);
if (!source)
{
AICLI_LOG(Repo, Info, << "Named source to be dropped, but not found: " << name);
return false;
}
else
{
AICLI_LOG(Repo, Info, << "Named source to be dropped, found: " << source->Name);
EnsureSourceIsRemovable(*source);
sourceList.RemoveSource(*source);
return true;
}
}
}
#ifndef AICLI_DISABLE_TEST_HOOKS
void TestHook_SetSourceFactoryOverride(const std::string& type, std::function<std::unique_ptr<ISourceFactory>()>&& factory)
{
s_Sources_TestHook_SourceFactories[type] = std::move(factory);
}
void TestHook_ClearSourceFactoryOverrides()
{
s_Sources_TestHook_SourceFactories.clear();
}
#endif
}