Skip to content

Commit 36af1df

Browse files
authored
Merge branch 'OpenAtom-Linyaps:master' into master
2 parents 949bae6 + 6eb10ec commit 36af1df

File tree

2 files changed

+88
-69
lines changed

2 files changed

+88
-69
lines changed

libs/linglong/src/linglong/cli/cli.cpp

+2-2
Original file line numberDiff line numberDiff line change
@@ -1841,8 +1841,8 @@ int Cli::repo(CLI::App *app)
18411841
}
18421842

18431843
// remove last slash
1844-
if (options.repoOptions.repoUrl.back() == '/') {
1845-
options.repoOptions.repoUrl.pop_back();
1844+
if (url.back() == '/') {
1845+
url.pop_back();
18461846
}
18471847
}
18481848

libs/linglong/src/linglong/repo/ostree_repo.cpp

+86-67
Original file line numberDiff line numberDiff line change
@@ -53,6 +53,7 @@
5353
#include <system_error>
5454
#include <thread>
5555
#include <unordered_map>
56+
#include <unordered_set>
5657
#include <utility>
5758
#include <vector>
5859

@@ -357,46 +358,76 @@ utils::error::Result<QString> commitDirToRepo(std::vector<GFile *> dirs,
357358
return commit;
358359
}
359360

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
364365
{
365366
LINGLONG_TRACE("update configuration");
366367

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-
377368
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");
388373
}
389374

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+
}
400431
}
401432

402433
GKeyFile *configKeyFile = ostree_repo_get_config(repo);
@@ -417,10 +448,10 @@ utils::error::Result<void> updateOstreeRepoConfig(OstreeRepo *repo,
417448
return LINGLONG_OK;
418449
}
419450

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
424455
{
425456
LINGLONG_TRACE("create linglong repository at " + location.absolutePath());
426457

@@ -439,7 +470,7 @@ utils::error::Result<OstreeRepo *> createOstreeRepo(const QDir &location,
439470
return LINGLONG_ERR("ostree_repo_create", gErr);
440471
}
441472

442-
auto result = updateOstreeRepoConfig(ostreeRepo, remoteName, url, parent);
473+
auto result = updateOstreeRepoConfig(ostreeRepo, config, parent);
443474

444475
if (!result) {
445476
return LINGLONG_ERR(result);
@@ -719,10 +750,7 @@ OSTreeRepo::OSTreeRepo(const QDir &path,
719750

720751
LINGLONG_TRACE("init ostree-based linglong repository");
721752

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);
726754
if (!result) {
727755
qCritical() << LINGLONG_ERRV(result);
728756
qFatal("abort");
@@ -758,15 +786,9 @@ OSTreeRepo::updateConfig(const api::types::v1::RepoConfigV2 &newCfg) noexcept
758786
}
759787

760788
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);
765790
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);
770792
if (!result) {
771793
qCritical() << result.error();
772794
Q_ASSERT(false);
@@ -778,6 +800,7 @@ OSTreeRepo::updateConfig(const api::types::v1::RepoConfigV2 &newCfg) noexcept
778800

779801
transaction.commit();
780802

803+
const auto newRepo = getDefaultRepo(newCfg);
781804
this->m_clientFactory.setServer(QString::fromStdString(newRepo.url));
782805
this->cfg = newCfg;
783806

@@ -801,18 +824,12 @@ utils::error::Result<void> OSTreeRepo::setConfig(const api::types::v1::RepoConfi
801824
Q_ASSERT(false);
802825
}
803826
});
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);
808828
if (!result) {
809829
return LINGLONG_ERR(result);
810830
}
811831
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);
816833
if (!result) {
817834
qCritical() << result.error();
818835
Q_ASSERT(false);
@@ -823,6 +840,7 @@ utils::error::Result<void> OSTreeRepo::setConfig(const api::types::v1::RepoConfi
823840
return LINGLONG_ERR(ret);
824841
}
825842

843+
const auto newRepo = getDefaultRepo(cfg);
826844
this->m_clientFactory.setServer(newRepo.url);
827845
this->cfg = cfg;
828846

@@ -1147,12 +1165,13 @@ void OSTreeRepo::pull(service::PackageTask &taskContext,
11471165
g_autoptr(GVariant) pull_options = g_variant_ref_sink(g_variant_builder_end(&builder));
11481166
// 这里不能使用g_main_context_push_thread_default,因为会阻塞Qt的事件循环
11491167
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);
11561175
ostree_async_progress_finish(progress);
11571176
auto shouldFallback = false;
11581177
if (status == FALSE) {
@@ -1190,7 +1209,7 @@ void OSTreeRepo::pull(service::PackageTask &taskContext,
11901209
g_autoptr(GVariant) pull_options = g_variant_ref_sink(g_variant_builder_end(&builder));
11911210

11921211
status = ostree_repo_pull_with_options(this->ostreeRepo.get(),
1193-
defaultRepo.name.c_str(),
1212+
defaultRepo.alias.value_or(defaultRepo.name).c_str(),
11941213
pull_options,
11951214
progress,
11961215
cancellable,
@@ -1227,7 +1246,7 @@ void OSTreeRepo::pull(service::PackageTask &taskContext,
12271246

12281247
item.commit = commit;
12291248
item.info = *info;
1230-
item.repo = defaultRepo.name;
1249+
item.repo = defaultRepo.alias.value_or(defaultRepo.name);
12311250

12321251
auto layerDir = this->ensureEmptyLayerDir(item.commit);
12331252
if (!layerDir) {

0 commit comments

Comments
 (0)