Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix some errors #890

Merged
merged 3 commits into from
Nov 27, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
49 changes: 30 additions & 19 deletions libs/linglong/src/linglong/package_manager/package_manager.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -602,9 +602,11 @@ QVariantMap PackageManager::installFromLayer(const QDBusUnixFileDescriptor &fd,
}

auto refSpec =
QString{ "%1/%2/%3" }.arg("local",
packageRef.toString(),
QString::fromStdString(packageInfo.packageInfoV2Module));
QString{ "%1:%2/%3/%4/%5" }.arg("local",
packageRef.channel,
packageRef.id,
packageRef.arch.toString(),
QString::fromStdString(packageInfo.packageInfoV2Module));
auto task = std::find_if(this->taskList.cbegin(),
this->taskList.cend(),
[&refSpec](const PackageTask *task) {
Expand Down Expand Up @@ -843,9 +845,11 @@ QVariantMap PackageManager::installFromUAB(const QDBusUnixFileDescriptor &fd,
}

auto refSpec =
QString{ "%1/%2/%3" }.arg("local",
appRef.toString(),
QString::fromStdString(appLayer.info.packageInfoV2Module));
QString{ "%1:%2/%3/%4/%5" }.arg("local",
appRef.channel,
appRef.id,
appRef.arch.toString(),
QString::fromStdString(appLayer.info.packageInfoV2Module));
auto task = std::find_if(this->taskList.cbegin(),
this->taskList.cend(),
[&refSpec](const PackageTask *task) {
Expand Down Expand Up @@ -1151,9 +1155,11 @@ auto PackageManager::Install(const QVariantMap &parameters) noexcept -> QVariant
}

auto refSpec =
QString{ "%1:%2/%3" }.arg(QString::fromStdString(this->repo.getConfig().defaultRepo),
remoteRef.toString(),
QString::fromStdString(curModule));
QString{ "%1:%2/%3/%4/%5" }.arg(QString::fromStdString(this->repo.getConfig().defaultRepo),
remoteRef.channel,
remoteRef.id,
remoteRef.arch.toString(),
QString::fromStdString(curModule));
auto task = std::find_if(this->taskList.cbegin(),
this->taskList.cend(),
[&refSpec](const PackageTask *task) {
Expand Down Expand Up @@ -1433,9 +1439,12 @@ auto PackageManager::Uninstall(const QVariantMap &parameters) noexcept -> QVaria

auto curModule = paras->package.packageManager1PackageModule.value_or("binary");
auto refSpec =
QString{ "%1/%2/%3" }.arg(QString::fromStdString(this->repo.getConfig().defaultRepo),
reference.toString(),
QString::fromStdString(curModule));
QString{ "%1:%2/%3/%4/%5" }.arg(QString::fromStdString(this->repo.getConfig().defaultRepo),
reference.channel,
reference.id,
reference.arch.toString(),
QString::fromStdString(curModule));

auto task = std::find_if(this->taskList.cbegin(),
this->taskList.cend(),
[&refSpec](const PackageTask *task) {
Expand Down Expand Up @@ -1618,10 +1627,12 @@ auto PackageManager::Update(const QVariantMap &parameters) noexcept -> QVariantM

// FIXME: use sha256 instead of refSpec
auto curModule = package.packageManager1PackageModule.value_or("binary");
auto refSpec =
QString{ "%1/%2/%3" }.arg(QString::fromStdString(this->repo.getConfig().defaultRepo),
newReference.toString(),
QString::fromStdString(curModule));
auto refSpec = QString{ "%1:%2/%3/%4/%5" }.arg(
QString::fromStdString(this->repo.getConfig().defaultRepo),
reference.channel,
reference.id,
reference.arch.toString(),
QString::fromStdString(curModule));
auto task = std::find_if(this->taskList.cbegin(),
this->taskList.cend(),
[&refSpec](const PackageTask *task) {
Expand All @@ -1631,7 +1642,7 @@ auto PackageManager::Update(const QVariantMap &parameters) noexcept -> QVariantM
return toDBusReply(-1, "the target " % refSpec % " is being operated");
}
refSpecList.append(refSpec);
upgradeList.push_back(std::make_pair(reference, newReference));
upgradeList.emplace_back(reference, newReference);
}

auto *taskPtr = new PackageTask{ connection(), refSpecList };
Expand All @@ -1642,8 +1653,8 @@ auto PackageManager::Update(const QVariantMap &parameters) noexcept -> QVariantM
return;
}

for (auto refs : upgradeList) {
this->Update(taskRef, refs.first, refs.second);
for (const auto &[oldRef, newRef] : upgradeList) {
this->Update(taskRef, oldRef, newRef);
}
});
Q_EMIT TaskListChanged(taskRef.taskObjectPath());
Expand Down
85 changes: 58 additions & 27 deletions libs/linglong/src/linglong/repo/ostree_repo.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -612,11 +612,47 @@ utils::error::Result<void> OSTreeRepo::handleRepositoryUpdate(
return LINGLONG_OK;
}

QDir OSTreeRepo::createLayerQDir(const std::string &commit) const noexcept
utils::error::Result<QDir> OSTreeRepo::ensureEmptyLayerDir(const std::string &commit) const noexcept
{
QDir dir = this->repoDir.absoluteFilePath(QString::fromStdString("layers/" + commit));
dir.mkpath(".");
return dir;
LINGLONG_TRACE(
QString{ "ensure empty layer dir exist %1" }.arg(QString::fromStdString(commit)));

std::filesystem::path dir =
this->repoDir.absoluteFilePath(QString::fromStdString("layers/" + commit)).toStdString();

std::error_code ec;
std::optional<std::filesystem::path> target;
if (std::filesystem::is_symlink(dir, ec)) {
target = std::filesystem::read_symlink(dir, ec);
if (ec) {
return LINGLONG_ERR(
QString{ "failed to resolve symlink %1: %2" }.arg(dir.c_str(), ec.message().c_str()));
}

if (!std::filesystem::remove(dir, ec)) {
return LINGLONG_ERR(
QString{ "failed to remove symlink %1: %2" }.arg(dir.c_str(), ec.message().c_str()));
}
}
if (ec && ec != std::errc::no_such_file_or_directory) {
return LINGLONG_ERR(
QString{ "check %1 is symlink failed: %2" }.arg(dir.c_str(), ec.message().c_str()));
}

if (target && std::filesystem::exists(*target, ec)) {
if (std::filesystem::remove_all(*target, ec) == static_cast<std::uintmax_t>(-1)) {
return LINGLONG_ERR(
QString{ "failed to remove layer dir %1: %2" }.arg(target->c_str(),
ec.message().c_str()));
}
}

if (!std::filesystem::create_directories(dir, ec)) {
return LINGLONG_ERR(
QString{ "failed to create layer dir %1: %2" }.arg(dir.c_str(), ec.message().c_str()));
}

return QDir{ dir.c_str() };
}

QDir OSTreeRepo::ostreeRepoDir() const noexcept
Expand Down Expand Up @@ -859,15 +895,19 @@ OSTreeRepo::importLayerDir(const package::LayerDir &dir,
item.info = *info;
item.repo = "local";

auto layerDir = this->createLayerQDir((*commitID).toStdString());
auto result = this->handleRepositoryUpdate(layerDir, item);
auto layerDir = this->ensureEmptyLayerDir((*commitID).toStdString());
if (!layerDir) {
return LINGLONG_ERR(layerDir);
}

auto result = this->handleRepositoryUpdate(*layerDir, item);
if (!result) {
return LINGLONG_ERR(result);
}

transaction.addRollBack([this, &layerDir, &item]() noexcept {
if (!layerDir.removeRecursively()) {
qCritical() << "remove layer dir failed: " << layerDir.absolutePath();
if (!layerDir->removeRecursively()) {
qCritical() << "remove layer dir failed: " << layerDir->absolutePath();
Q_ASSERT(false);
}
auto result = this->removeOstreeRef(item);
Expand All @@ -878,7 +918,7 @@ OSTreeRepo::importLayerDir(const package::LayerDir &dir,
});

transaction.commit();
return package::LayerDir{ layerDir.absolutePath() };
return package::LayerDir{ layerDir->absolutePath() };
}

[[nodiscard]] utils::error::Result<void> OSTreeRepo::push(const package::Reference &reference,
Expand Down Expand Up @@ -1155,27 +1195,18 @@ void OSTreeRepo::pull(service::PackageTask &taskContext,
item.info = *info;
item.repo = this->cfg.defaultRepo;

auto layerDir = this->createLayerQDir(item.commit);
auto result = this->handleRepositoryUpdate(layerDir, item);
auto layerDir = this->ensureEmptyLayerDir(item.commit);
if (!layerDir) {
taskContext.reportError(LINGLONG_ERRV(layerDir));
return;
}

auto result = this->handleRepositoryUpdate(*layerDir, item);
if (!result) {
taskContext.reportError(LINGLONG_ERRV(result));
return;
}

transaction.addRollBack([this, &item, &layerDir]() noexcept {
if (!layerDir.removeRecursively()) {
qCritical() << "remove layer dir failed: " << layerDir.absolutePath();
Q_ASSERT(false);
}

auto result = this->removeOstreeRef(item);
if (!result) {
qCritical() << result.error();
Q_ASSERT(false);
}
});

transaction.commit();
}

Expand Down Expand Up @@ -1678,7 +1709,7 @@ void OSTreeRepo::updateSharedInfo() noexcept
utils::command::Exec("update-desktop-database", { applicationDir.absolutePath() });
if (!ret) {
qWarning() << "warning: failed to update desktop database in "
+ applicationDir.absolutePath() + ": " + *ret;
+ applicationDir.absolutePath() + ": " + ret.error().message();
}
}

Expand All @@ -1687,7 +1718,7 @@ void OSTreeRepo::updateSharedInfo() noexcept
auto ret = utils::command::Exec("update-mime-database", { mimeDataDir.absolutePath() });
if (!ret) {
qWarning() << "warning: failed to update mime type database in "
+ mimeDataDir.absolutePath() + ": " + *ret;
+ mimeDataDir.absolutePath() + ": " + ret.error().message();
}
}

Expand All @@ -1696,7 +1727,7 @@ void OSTreeRepo::updateSharedInfo() noexcept
auto ret = utils::command::Exec("glib-compile-schemas", { glibSchemasDir.absolutePath() });
if (!ret) {
qWarning() << "warning: failed to update schemas in " + glibSchemasDir.absolutePath()
+ ": " + *ret;
+ ": " + ret.error().message();
}
}
}
Expand Down
3 changes: 2 additions & 1 deletion libs/linglong/src/linglong/repo/ostree_repo.h
Original file line number Diff line number Diff line change
Expand Up @@ -137,7 +137,8 @@ class OSTreeRepo : public QObject

utils::error::Result<void> updateConfig(const api::types::v1::RepoConfig &newCfg) noexcept;
QDir ostreeRepoDir() const noexcept;
QDir createLayerQDir(const std::string &commit) const noexcept;
[[nodiscard]] utils::error::Result<QDir>
ensureEmptyLayerDir(const std::string &commit) const noexcept;
utils::error::Result<void> handleRepositoryUpdate(
QDir layerDir, const api::types::v1::RepositoryCacheLayersItem &layer) noexcept;
utils::error::Result<void>
Expand Down