Skip to content

Commit b6cfbb7

Browse files
reddevillgdengbo11
authored andcommitted
refactor: split repo load/create
- Make OSTreeRepo::loadFromPath read repo config only - Make OSTreeRepo::create prefer repo config and persist fallback config when absent - Add repo and repo cache tests Signed-off-by: reddevillg <reddevillg@gmail.com>
1 parent 4b888f1 commit b6cfbb7

21 files changed

Lines changed: 625 additions & 319 deletions

File tree

apps/ll-builder/src/main.cpp

Lines changed: 19 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* SPDX-FileCopyrightText: 2022 UnionTech Software Technology Co., Ltd.
2+
* SPDX-FileCopyrightText: 2022 - 2026 UnionTech Software Technology Co., Ltd.
33
*
44
* SPDX-License-Identifier: LGPL-3.0-or-later
55
*/
@@ -16,6 +16,7 @@
1616
#include "linglong/repo/config.h"
1717
#include "linglong/repo/migrate.h"
1818
#include "linglong/utils/error/error.h"
19+
#include "linglong/utils/file.h"
1920
#include "linglong/utils/gettext.h"
2021
#include "linglong/utils/log/log.h"
2122
#include "linglong/utils/namespace.h"
@@ -1020,9 +1021,8 @@ You can report bugs to the linyaps team under this project: https://github.com/O
10201021
return -1;
10211022
}
10221023

1023-
auto repoCfg =
1024-
linglong::repo::loadConfig({ QString::fromStdString(builderCfg->repo + "/config.yaml"),
1025-
LINGLONG_DATA_DIR "/config.yaml" });
1024+
auto repoCfg = linglong::repo::loadConfig(
1025+
{ builderCfg->repo + "/config.yaml", LINGLONG_DATA_DIR "/config.yaml" });
10261026
if (!repoCfg) {
10271027
LogE("{}", repoCfg.error());
10281028
return -1;
@@ -1035,16 +1035,21 @@ You can report bugs to the linyaps team under this project: https://github.com/O
10351035
}
10361036
}
10371037

1038-
auto repoRoot = QDir{ QString::fromStdString(builderCfg->repo) };
1039-
if (!repoRoot.exists() && !repoRoot.mkpath(".")) {
1040-
LogE("failed to create the repository of builder.");
1038+
std::filesystem::path repoRoot(builderCfg->repo);
1039+
auto res = linglong::utils::ensureDirectory(repoRoot);
1040+
if (!res) {
1041+
LogE("failed to create the repository of builder: {}", res.error());
10411042
return -1;
10421043
}
10431044

1044-
linglong::repo::OSTreeRepo repo(repoRoot, *repoCfg);
1045+
auto repo = linglong::repo::OSTreeRepo::create(repoRoot, *repoCfg);
1046+
if (!repo) {
1047+
LogE("failed to create ostree repo {}", repo.error());
1048+
return -1;
1049+
}
10451050

10461051
if (buildRepo->parsed()) {
1047-
return handleRepo(repo,
1052+
return handleRepo(**repo,
10481053
repoCmdOpts,
10491054
buildRepoShow,
10501055
buildRepoAdd,
@@ -1056,19 +1061,19 @@ You can report bugs to the linyaps team under this project: https://github.com/O
10561061
}
10571062

10581063
if (buildImport->parsed()) {
1059-
return handleImport(repo, importOpts);
1064+
return handleImport(**repo, importOpts);
10601065
}
10611066

10621067
if (buildImportDir->parsed()) {
1063-
return handleImportDir(repo, importDirOpts);
1068+
return handleImportDir(**repo, importDirOpts);
10641069
}
10651070

10661071
if (buildList->parsed()) {
1067-
return handleList(repo, listOpts);
1072+
return handleList(**repo, listOpts);
10681073
}
10691074

10701075
if (buildRemove->parsed()) {
1071-
return handleRemove(repo, removeOpts);
1076+
return handleRemove(**repo, removeOpts);
10721077
}
10731078

10741079
// following command need builder
@@ -1120,7 +1125,7 @@ You can report bugs to the linyaps team under this project: https://github.com/O
11201125

11211126
linglong::builder::Builder builder(std::move(project),
11221127
cwd,
1123-
repo,
1128+
**repo,
11241129
*containerBuilder,
11251130
*builderCfg);
11261131

apps/ll-cli/src/main.cpp

Lines changed: 3 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* SPDX-FileCopyrightText: 2022 UnionTech Software Technology Co., Ltd.
2+
* SPDX-FileCopyrightText: 2022 - 2026 UnionTech Software Technology Co., Ltd.
33
*
44
* SPDX-License-Identifier: LGPL-3.0-or-later
55
*/
@@ -594,29 +594,6 @@ void addInspectCommand(CLI::App &commandParser,
594594

595595
} // namespace
596596

597-
// 初始化仓库
598-
linglong::utils::error::Result<linglong::repo::OSTreeRepo *> initOSTreeRepo()
599-
{
600-
LINGLONG_TRACE("initOSTreeRepo");
601-
// load repo config
602-
auto repoConfig = linglong::repo::loadConfig(
603-
{ LINGLONG_ROOT "/config.yaml", LINGLONG_DATA_DIR "/config.yaml" });
604-
if (!repoConfig) {
605-
return LINGLONG_ERR("load repo config failed", repoConfig);
606-
}
607-
608-
// check repo root
609-
auto repoRoot = QDir(LINGLONG_ROOT);
610-
if (!repoRoot.exists()) {
611-
return LINGLONG_ERR("repo root doesn't exist" + repoRoot.absolutePath().toStdString());
612-
}
613-
614-
// create repo
615-
auto repo = new linglong::repo::OSTreeRepo(repoRoot, std::move(*repoConfig));
616-
repo->setParent(QCoreApplication::instance());
617-
return repo;
618-
}
619-
620597
int runCliApplication(int argc, char **mainArgv)
621598
{
622599
CLI::App commandParser{ _(
@@ -834,9 +811,9 @@ You can report bugs to the linyaps team under this project: https://github.com/O
834811
LogW("Using DummyNotifier, expected interactions and prompts will not be displayed.");
835812
notifier = std::make_unique<linglong::cli::DummyNotifier>();
836813
}
837-
auto repo = initOSTreeRepo();
814+
auto repo = linglong::repo::OSTreeRepo::loadFromPath(LINGLONG_ROOT);
838815
if (!repo.has_value()) {
839-
LogE("initOSTreeRepo failed: {}", repo.error());
816+
LogE("failed to load repo: {}", repo.error());
840817
return -1;
841818
}
842819
// create cli

apps/ll-package-manager/src/main.cpp

Lines changed: 29 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* SPDX-FileCopyrightText: 2022 UnionTech Software Technology Co., Ltd.
2+
* SPDX-FileCopyrightText: 2022 - 2026 UnionTech Software Technology Co., Ltd.
33
*
44
* SPDX-License-Identifier: LGPL-3.0-or-later
55
*/
@@ -12,6 +12,7 @@
1212
#include "linglong/repo/config.h"
1313
#include "linglong/repo/migrate.h"
1414
#include "linglong/repo/ostree_repo.h"
15+
#include "linglong/utils/file.h"
1516
#include "ocppi/cli/CLI.hpp"
1617
#include "ocppi/cli/crun/Crun.hpp"
1718

@@ -23,16 +24,18 @@ namespace {
2324
void withDBusDaemon(ocppi::cli::CLI &cli)
2425
{
2526
auto config = linglong::repo::loadConfig(
26-
{ LINGLONG_ROOT "/config.yaml", LINGLONG_DATA_DIR "/config.yaml" });
27+
std::vector<std::filesystem::path>{ LINGLONG_ROOT "/config.yaml",
28+
LINGLONG_DATA_DIR "/config.yaml" });
2729
if (!config.has_value()) {
2830
LogE("load config failed: {}", config.error());
2931
QCoreApplication::exit(-1);
3032
return;
3133
}
3234

33-
auto repoRoot = QDir(LINGLONG_ROOT);
34-
if (!repoRoot.exists() && !repoRoot.mkpath(".")) {
35-
LogE("failed to create repository directory {}", repoRoot.absolutePath().toStdString());
35+
std::filesystem::path repoRoot(LINGLONG_ROOT);
36+
auto res = linglong::utils::ensureDirectory(repoRoot);
37+
if (!res) {
38+
LogE("failed to create repository directory {}", res.error());
3639
std::abort();
3740
}
3841

@@ -41,7 +44,14 @@ void withDBusDaemon(ocppi::cli::CLI &cli)
4144
LogE("failed to migrate repository");
4245
QCoreApplication::exit(-1);
4346
}
44-
auto *ostreeRepo = new linglong::repo::OSTreeRepo(repoRoot, *config);
47+
48+
auto repo = linglong::repo::OSTreeRepo::create(repoRoot, *config);
49+
if (!repo) {
50+
LogE("failed to create repo: {}", repo.error());
51+
QCoreApplication::exit(-1);
52+
return;
53+
}
54+
auto *ostreeRepo = std::move(repo).value().release();
4555
ostreeRepo->setParent(QCoreApplication::instance());
4656
auto result = ostreeRepo->fixExportAllEntries();
4757
if (!result.has_value()) {
@@ -91,20 +101,28 @@ void withoutDBusDaemon(ocppi::cli::CLI &cli)
91101
LogI("Running linglong package manager without dbus daemon...");
92102

93103
auto config = linglong::repo::loadConfig(
94-
{ LINGLONG_ROOT "/config.yaml", LINGLONG_DATA_DIR "/config.yaml" });
104+
std::vector<std::filesystem::path>{ LINGLONG_ROOT "/config.yaml",
105+
LINGLONG_DATA_DIR "/config.yaml" });
95106
if (!config.has_value()) {
96107
LogE("load config failed: {}", config.error());
97108
QCoreApplication::exit(-1);
98109
return;
99110
}
100111

101-
auto repoRoot = QDir(LINGLONG_ROOT);
102-
if (!repoRoot.exists() && !repoRoot.mkpath(".")) {
103-
LogE("failed to create repository directory {}", repoRoot.absolutePath().toStdString());
112+
std::filesystem::path repoRoot(LINGLONG_ROOT);
113+
auto res = linglong::utils::ensureDirectory(repoRoot);
114+
if (!res) {
115+
LogE("failed to create repository directory {}", res.error());
104116
std::abort();
105117
}
106118

107-
auto *ostreeRepo = new linglong::repo::OSTreeRepo(repoRoot, *config);
119+
auto repo = linglong::repo::OSTreeRepo::create(repoRoot, *config);
120+
if (!repo) {
121+
LogE("failed to create repo: {}", repo.error());
122+
QCoreApplication::exit(-1);
123+
return;
124+
}
125+
auto *ostreeRepo = std::move(repo).value().release();
108126
ostreeRepo->setParent(QCoreApplication::instance());
109127
auto result = ostreeRepo->fixExportAllEntries();
110128
if (!result.has_value()) {

libs/linglong/src/linglong/package_manager/action.cpp

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,10 @@
1-
// SPDX-FileCopyrightText: 2025 UnionTech Software Technology Co., Ltd.
1+
// SPDX-FileCopyrightText: 2025 - 2026 UnionTech Software Technology Co., Ltd.
22
//
33
// SPDX-License-Identifier: LGPL-3.0-or-later
44

55
#include "action.h"
66

77
#include "linglong/repo/ostree_repo.h"
8-
#include "linglong/repo/repo_cache.h"
98

109
namespace linglong::service {
1110

libs/linglong/src/linglong/repo/config.cpp

Lines changed: 13 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* SPDX-FileCopyrightText: 2022 UnionTech Software Technology Co., Ltd.
2+
* SPDX-FileCopyrightText: 2022 - 2026 UnionTech Software Technology Co., Ltd.
33
*
44
* SPDX-License-Identifier: LGPL-3.0-or-later
55
*/
@@ -14,18 +14,17 @@
1414

1515
#include <fmt/format.h>
1616

17-
#include <QStringList>
18-
1917
#include <fstream>
2018

2119
namespace linglong::repo {
2220

23-
utils::error::Result<api::types::v1::RepoConfigV2> loadConfig(const QString &file) noexcept
21+
utils::error::Result<api::types::v1::RepoConfigV2>
22+
loadConfig(const std::filesystem::path &file) noexcept
2423
{
25-
LINGLONG_TRACE(fmt::format("load repo config from {}", file.toStdString()));
24+
LINGLONG_TRACE(fmt::format("load repo config from {}", file));
2625

2726
try {
28-
auto ifs = std::ifstream(file.toLocal8Bit());
27+
auto ifs = std::ifstream(file);
2928
if (!ifs.is_open()) {
3029
return LINGLONG_ERR("open failed");
3130
}
@@ -49,28 +48,29 @@ utils::error::Result<api::types::v1::RepoConfigV2> loadConfig(const QString &fil
4948
}
5049
}
5150

52-
utils::error::Result<api::types::v1::RepoConfigV2> loadConfig(const QStringList &files) noexcept
51+
utils::error::Result<api::types::v1::RepoConfigV2>
52+
loadConfig(const std::vector<std::filesystem::path> &files) noexcept
5353
{
54-
LINGLONG_TRACE(fmt::format("load repo config from {}", files.join(" ").toStdString()));
54+
LINGLONG_TRACE("load repo config");
5555

5656
for (const auto &file : files) {
5757
auto config = loadConfig(file);
5858
if (!config.has_value()) {
59-
LogD("Failed to load repo config from {}: {}", file.toStdString(), config.error());
59+
LogD("Failed to load repo config from {}: {}", file, config.error());
6060
continue;
6161
}
6262

63-
LogD("load repo config from {}", file.toStdString());
63+
LogD("load repo config from {}", file);
6464
return config;
6565
}
6666

6767
return LINGLONG_ERR("all failed");
6868
}
6969

7070
utils::error::Result<void> saveConfig(const api::types::v1::RepoConfigV2 &cfg,
71-
const QString &path) noexcept
71+
const std::filesystem::path &path) noexcept
7272
{
73-
LINGLONG_TRACE(fmt::format("save config to {}", path.toStdString()));
73+
LINGLONG_TRACE(fmt::format("save config to {}", path));
7474

7575
try {
7676
auto defaultRepoExists =
@@ -82,7 +82,7 @@ utils::error::Result<void> saveConfig(const api::types::v1::RepoConfigV2 &cfg,
8282
return LINGLONG_ERR("default repo not found in repos");
8383
}
8484

85-
auto ofs = std::ofstream(path.toLocal8Bit());
85+
auto ofs = std::ofstream(path);
8686
if (!ofs.is_open()) {
8787
return LINGLONG_ERR("open failed");
8888
}

libs/linglong/src/linglong/repo/config.h

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* SPDX-FileCopyrightText: 2022 UnionTech Software Technology Co., Ltd.
2+
* SPDX-FileCopyrightText: 2022 - 2026 UnionTech Software Technology Co., Ltd.
33
*
44
* SPDX-License-Identifier: LGPL-3.0-or-later
55
*/
@@ -10,14 +10,16 @@
1010
#include "linglong/api/types/v1/RepoConfig.hpp"
1111
#include "linglong/utils/error/error.h"
1212

13-
#include <QString>
13+
#include <filesystem>
1414

1515
namespace linglong::repo {
1616

17-
utils::error::Result<api::types::v1::RepoConfigV2> loadConfig(const QString &file) noexcept;
18-
utils::error::Result<api::types::v1::RepoConfigV2> loadConfig(const QStringList &files) noexcept;
17+
utils::error::Result<api::types::v1::RepoConfigV2>
18+
loadConfig(const std::filesystem::path &file) noexcept;
19+
utils::error::Result<api::types::v1::RepoConfigV2>
20+
loadConfig(const std::vector<std::filesystem::path> &files) noexcept;
1921
utils::error::Result<void> saveConfig(const api::types::v1::RepoConfigV2 &cfg,
20-
const QString &path) noexcept;
22+
const std::filesystem::path &path) noexcept;
2123
int64_t getRepoMinPriority(const api::types::v1::RepoConfigV2 &cfg) noexcept;
2224
int64_t getRepoMaxPriority(const api::types::v1::RepoConfigV2 &cfg) noexcept;
2325
const api::types::v1::Repo &getDefaultRepo(const api::types::v1::RepoConfigV2 &cfg) noexcept;

0 commit comments

Comments
 (0)