53
53
#include < system_error>
54
54
#include < thread>
55
55
#include < unordered_map>
56
+ #include < unordered_set>
56
57
#include < utility>
57
58
#include < vector>
58
59
@@ -357,46 +358,76 @@ utils::error::Result<QString> commitDirToRepo(std::vector<GFile *> dirs,
357
358
return commit;
358
359
}
359
360
360
- utils::error::Result<void > updateOstreeRepoConfig (OstreeRepo *repo,
361
- const QString &remoteName ,
362
- const QString &url ,
363
- const QString &parent = " " ) noexcept
361
+ utils::error::Result<void >
362
+ updateOstreeRepoConfig (OstreeRepo *repo ,
363
+ const linglong::api::types::v1::RepoConfigV2 &config ,
364
+ const QString &parent = " " ) noexcept
364
365
{
365
366
LINGLONG_TRACE (" update configuration" );
366
367
367
- g_autoptr (GVariant) options = NULL ;
368
- GVariantBuilder builder;
369
- g_variant_builder_init (&builder, G_VARIANT_TYPE (" a{sv}" ));
370
- g_variant_builder_add (&builder, " {sv}" , " gpg-verify" , g_variant_new_boolean (FALSE ));
371
- // NOTE:
372
- // libcurl 8.2.1 has a http2 bug https://github.com/curl/curl/issues/11859
373
- // We disable http2 for now.
374
- g_variant_builder_add (&builder, " {sv}" , " http2" , g_variant_new_boolean (FALSE ));
375
- options = g_variant_ref_sink (g_variant_builder_end (&builder));
376
-
377
368
g_autoptr (GError) gErr = nullptr ;
378
- if (ostree_repo_remote_change (repo,
379
- nullptr ,
380
- OSTREE_REPO_REMOTE_CHANGE_DELETE_IF_EXISTS,
381
- remoteName.toUtf8 (),
382
- nullptr ,
383
- nullptr ,
384
- nullptr ,
385
- &gErr )
386
- == FALSE ) {
387
- return LINGLONG_ERR (" ostree_repo_remote_change" , gErr );
369
+
370
+ g_auto (GStrv) remoteNames = ostree_repo_remote_list (repo, nullptr );
371
+ if (!remoteNames) {
372
+ return LINGLONG_ERR (" ostree_repo_remote_list Failed to get remote list" );
388
373
}
389
374
390
- if (ostree_repo_remote_change (repo,
391
- nullptr ,
392
- OSTREE_REPO_REMOTE_CHANGE_ADD,
393
- remoteName.toUtf8 (),
394
- (url + " /repos/" + remoteName).toUtf8 (),
395
- options,
396
- nullptr ,
397
- &gErr )
398
- == FALSE ) {
399
- return LINGLONG_ERR (" ostree_repo_remote_change" , gErr );
375
+ std::unordered_set<std::string> validRemotes;
376
+ for (const auto &repoCfg : config.repos ) {
377
+ validRemotes.insert (repoCfg.alias .value_or (repoCfg.name ));
378
+ }
379
+
380
+ for (auto remoteName = remoteNames; *remoteName != nullptr ; remoteName++) {
381
+ if (validRemotes.find (*remoteName) == validRemotes.end ()) {
382
+ if (ostree_repo_remote_change (repo,
383
+ nullptr ,
384
+ OSTREE_REPO_REMOTE_CHANGE_DELETE_IF_EXISTS,
385
+ *remoteName,
386
+ nullptr ,
387
+ nullptr ,
388
+ nullptr ,
389
+ &gErr )
390
+ == FALSE ) {
391
+ return LINGLONG_ERR (" ostree_repo_remote_change" , gErr );
392
+ }
393
+ }
394
+ }
395
+
396
+ for (const auto &repoCfg : config.repos ) {
397
+ const std::string &remoteUrl = repoCfg.url + " /repos/" + repoCfg.name ;
398
+ g_autoptr (GVariant) options = NULL ;
399
+ GVariantBuilder builder;
400
+ g_variant_builder_init (&builder, G_VARIANT_TYPE (" a{sv}" ));
401
+ g_variant_builder_add (&builder, " {sv}" , " gpg-verify" , g_variant_new_boolean (FALSE ));
402
+ // NOTE:
403
+ // libcurl 8.2.1 has a http2 bug https://github.com/curl/curl/issues/11859
404
+ // We disable http2 for now.
405
+ g_variant_builder_add (&builder, " {sv}" , " http2" , g_variant_new_boolean (FALSE ));
406
+ options = g_variant_ref_sink (g_variant_builder_end (&builder));
407
+
408
+ if (ostree_repo_remote_change (repo,
409
+ nullptr ,
410
+ OSTREE_REPO_REMOTE_CHANGE_DELETE_IF_EXISTS,
411
+ repoCfg.alias .value_or (repoCfg.name ).c_str (),
412
+ nullptr ,
413
+ nullptr ,
414
+ nullptr ,
415
+ &gErr )
416
+ == FALSE ) {
417
+ return LINGLONG_ERR (" ostree_repo_remote_change" , gErr );
418
+ }
419
+
420
+ if (ostree_repo_remote_change (repo,
421
+ nullptr ,
422
+ OSTREE_REPO_REMOTE_CHANGE_ADD,
423
+ repoCfg.alias .value_or (repoCfg.name ).c_str (),
424
+ remoteUrl.c_str (),
425
+ options,
426
+ nullptr ,
427
+ &gErr )
428
+ == FALSE ) {
429
+ return LINGLONG_ERR (" ostree_repo_remote_change" , gErr );
430
+ }
400
431
}
401
432
402
433
GKeyFile *configKeyFile = ostree_repo_get_config (repo);
@@ -417,10 +448,10 @@ utils::error::Result<void> updateOstreeRepoConfig(OstreeRepo *repo,
417
448
return LINGLONG_OK;
418
449
}
419
450
420
- utils::error::Result<OstreeRepo *> createOstreeRepo ( const QDir &location,
421
- const QString &remoteName ,
422
- const QString &url ,
423
- const QString &parent = " " ) noexcept
451
+ utils::error::Result<OstreeRepo *>
452
+ createOstreeRepo ( const QDir &location ,
453
+ const linglong::api::types::v1::RepoConfigV2 &config ,
454
+ const QString &parent = " " ) noexcept
424
455
{
425
456
LINGLONG_TRACE (" create linglong repository at " + location.absolutePath ());
426
457
@@ -439,7 +470,7 @@ utils::error::Result<OstreeRepo *> createOstreeRepo(const QDir &location,
439
470
return LINGLONG_ERR (" ostree_repo_create" , gErr );
440
471
}
441
472
442
- auto result = updateOstreeRepoConfig (ostreeRepo, remoteName, url , parent);
473
+ auto result = updateOstreeRepoConfig (ostreeRepo, config , parent);
443
474
444
475
if (!result) {
445
476
return LINGLONG_ERR (result);
@@ -719,10 +750,7 @@ OSTreeRepo::OSTreeRepo(const QDir &path,
719
750
720
751
LINGLONG_TRACE (" init ostree-based linglong repository" );
721
752
722
- const auto defaultRepo = getDefaultRepo (this ->cfg );
723
- auto result = createOstreeRepo (this ->ostreeRepoDir ().absolutePath (),
724
- QString::fromStdString (defaultRepo.name ),
725
- QString::fromStdString (defaultRepo.url ));
753
+ auto result = createOstreeRepo (this ->ostreeRepoDir ().absolutePath (), this ->cfg );
726
754
if (!result) {
727
755
qCritical () << LINGLONG_ERRV (result);
728
756
qFatal (" abort" );
@@ -758,15 +786,9 @@ OSTreeRepo::updateConfig(const api::types::v1::RepoConfigV2 &newCfg) noexcept
758
786
}
759
787
760
788
utils::Transaction transaction;
761
- const auto newRepo = getDefaultRepo (newCfg);
762
- result = updateOstreeRepoConfig (this ->ostreeRepo .get (),
763
- QString::fromStdString (newRepo.name ),
764
- QString::fromStdString (newRepo.url ));
789
+ result = updateOstreeRepoConfig (this ->ostreeRepo .get (), newCfg);
765
790
transaction.addRollBack ([this ]() noexcept {
766
- const auto defaultRepo = getDefaultRepo (this ->cfg );
767
- auto result = updateOstreeRepoConfig (this ->ostreeRepo .get (),
768
- QString::fromStdString (defaultRepo.name ),
769
- QString::fromStdString (defaultRepo.url ));
791
+ auto result = updateOstreeRepoConfig (this ->ostreeRepo .get (), this ->cfg );
770
792
if (!result) {
771
793
qCritical () << result.error ();
772
794
Q_ASSERT (false );
@@ -778,6 +800,7 @@ OSTreeRepo::updateConfig(const api::types::v1::RepoConfigV2 &newCfg) noexcept
778
800
779
801
transaction.commit ();
780
802
803
+ const auto newRepo = getDefaultRepo (newCfg);
781
804
this ->m_clientFactory .setServer (QString::fromStdString (newRepo.url ));
782
805
this ->cfg = newCfg;
783
806
@@ -801,18 +824,12 @@ utils::error::Result<void> OSTreeRepo::setConfig(const api::types::v1::RepoConfi
801
824
Q_ASSERT (false );
802
825
}
803
826
});
804
- const auto newRepo = getDefaultRepo (cfg);
805
- result = updateOstreeRepoConfig (this ->ostreeRepo .get (),
806
- QString::fromStdString (newRepo.name ),
807
- QString::fromStdString (newRepo.url ));
827
+ result = updateOstreeRepoConfig (this ->ostreeRepo .get (), cfg);
808
828
if (!result) {
809
829
return LINGLONG_ERR (result);
810
830
}
811
831
transaction.addRollBack ([this ]() noexcept {
812
- const auto defaultRepo = getDefaultRepo (this ->cfg );
813
- auto result = updateOstreeRepoConfig (this ->ostreeRepo .get (),
814
- QString::fromStdString (defaultRepo.name ),
815
- QString::fromStdString (defaultRepo.url ));
832
+ auto result = updateOstreeRepoConfig (this ->ostreeRepo .get (), this ->cfg );
816
833
if (!result) {
817
834
qCritical () << result.error ();
818
835
Q_ASSERT (false );
@@ -823,6 +840,7 @@ utils::error::Result<void> OSTreeRepo::setConfig(const api::types::v1::RepoConfi
823
840
return LINGLONG_ERR (ret);
824
841
}
825
842
843
+ const auto newRepo = getDefaultRepo (cfg);
826
844
this ->m_clientFactory .setServer (newRepo.url );
827
845
this ->cfg = cfg;
828
846
@@ -1147,12 +1165,13 @@ void OSTreeRepo::pull(service::PackageTask &taskContext,
1147
1165
g_autoptr (GVariant) pull_options = g_variant_ref_sink (g_variant_builder_end (&builder));
1148
1166
// 这里不能使用g_main_context_push_thread_default,因为会阻塞Qt的事件循环
1149
1167
const auto defaultRepo = getDefaultRepo (this ->cfg );
1150
- auto status = ostree_repo_pull_with_options (this ->ostreeRepo .get (),
1151
- defaultRepo.name .c_str (),
1152
- pull_options,
1153
- progress,
1154
- cancellable,
1155
- &gErr );
1168
+ auto status =
1169
+ ostree_repo_pull_with_options (this ->ostreeRepo .get (),
1170
+ defaultRepo.alias .value_or (defaultRepo.name ).c_str (),
1171
+ pull_options,
1172
+ progress,
1173
+ cancellable,
1174
+ &gErr );
1156
1175
ostree_async_progress_finish (progress);
1157
1176
auto shouldFallback = false ;
1158
1177
if (status == FALSE ) {
@@ -1190,7 +1209,7 @@ void OSTreeRepo::pull(service::PackageTask &taskContext,
1190
1209
g_autoptr (GVariant) pull_options = g_variant_ref_sink (g_variant_builder_end (&builder));
1191
1210
1192
1211
status = ostree_repo_pull_with_options (this ->ostreeRepo .get (),
1193
- defaultRepo.name .c_str (),
1212
+ defaultRepo.alias . value_or (defaultRepo. name ) .c_str (),
1194
1213
pull_options,
1195
1214
progress,
1196
1215
cancellable,
@@ -1227,7 +1246,7 @@ void OSTreeRepo::pull(service::PackageTask &taskContext,
1227
1246
1228
1247
item.commit = commit;
1229
1248
item.info = *info;
1230
- item.repo = defaultRepo.name ;
1249
+ item.repo = defaultRepo.alias . value_or (defaultRepo. name ) ;
1231
1250
1232
1251
auto layerDir = this ->ensureEmptyLayerDir (item.commit );
1233
1252
if (!layerDir) {
0 commit comments