Skip to content

Commit

Permalink
fix: error to export applications entries directory
Browse files Browse the repository at this point in the history
1. we must fix export entries when in no dbus mode;
2. we must copy application entries directorys to the root entries
   directorys before rewrite file;
  • Loading branch information
dengbo11 committed Jan 10, 2025
1 parent 033dfde commit 5db9d54
Show file tree
Hide file tree
Showing 3 changed files with 67 additions and 23 deletions.
27 changes: 8 additions & 19 deletions apps/ll-package-manager/src/main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,6 @@
#include "linglong/repo/ostree_repo.h"
#include "linglong/utils/configure.h"
#include "linglong/utils/dbus/register.h"
#include "linglong/utils/file.h"
#include "linglong/utils/global/initialize.h"
#include "ocppi/cli/CLI.hpp"
#include "ocppi/cli/crun/Crun.hpp"
Expand Down Expand Up @@ -51,23 +50,9 @@ void withDBusDaemon(ocppi::cli::CLI &cli)
}
auto *ostreeRepo = new linglong::repo::OSTreeRepo(repoRoot, *config, *clientFactory);
ostreeRepo->setParent(QCoreApplication::instance());
{
auto exportVersion = repoRoot.absoluteFilePath("entries/.version").toStdString();
auto data = linglong::utils::readFile(exportVersion);
if (data && data == LINGLONG_EXPORT_VERSION) {
qDebug() << exportVersion.c_str() << data->c_str();
qDebug() << "skip export entry, already exported";
} else {
auto ret = ostreeRepo->exportAllEntries();
if (!ret.has_value()) {
qCritical() << "failed to export entries:" << ret.error();
} else {
ret = linglong::utils::writeFile(exportVersion, LINGLONG_EXPORT_VERSION);
if (!ret.has_value()) {
qCritical() << "failed to write export version:" << ret.error();
}
}
}
auto result = ostreeRepo->fixExportAllEntries();
if (!result.has_value()) {
qCritical() << result.error().message();
}

auto *containerBuilder = new linglong::runtime::ContainerBuilder(cli);
Expand All @@ -78,7 +63,7 @@ void withDBusDaemon(ocppi::cli::CLI &cli)
*containerBuilder,
QCoreApplication::instance());
new linglong::adaptors::package_manger::PackageManager1(packageManager);
auto result = registerDBusObject(conn, "/org/deepin/linglong/PackageManager1", packageManager);
result = registerDBusObject(conn, "/org/deepin/linglong/PackageManager1", packageManager);
if (!result.has_value()) {
qCritical().noquote() << "Launching failed:" << Qt::endl << result.error().message();
QCoreApplication::exit(-1);
Expand Down Expand Up @@ -126,6 +111,10 @@ void withoutDBusDaemon(ocppi::cli::CLI &cli)

auto *ostreeRepo = new linglong::repo::OSTreeRepo(repoRoot, *config, *clientFactory);
ostreeRepo->setParent(QCoreApplication::instance());
auto result = ostreeRepo->fixExportAllEntries();
if (!result.has_value()) {
qCritical() << result.error().message();
}

auto *containerBuilder = new linglong::runtime::ContainerBuilder(cli);
containerBuilder->setParent(QCoreApplication::instance());
Expand Down
58 changes: 56 additions & 2 deletions libs/linglong/src/linglong/repo/ostree_repo.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@
#include "linglong/repo/config.h"
#include "linglong/utils/command/env.h"
#include "linglong/utils/error/error.h"
#include "linglong/utils/file.h"
#include "linglong/utils/finally/finally.h"
#include "linglong/utils/gkeyfile_wrapper.h"
#include "linglong/utils/packageinfo_handler.h"
Expand Down Expand Up @@ -1640,8 +1641,37 @@ utils::error::Result<void> OSTreeRepo::exportDir(const std::string &appID,
return LINGLONG_ERR("remove file failed", ec);
}
}
auto ret = IniLikeFileRewrite(QFileInfo(source_path.c_str()), appID.c_str());
if (!ret) { }

{
auto info = QFileInfo(target_path.c_str());
if ((info.path().contains("share/applications") && info.suffix() == "desktop")
|| (info.path().contains("share/dbus-1") && info.suffix() == "service")
|| (info.path().contains("share/systemd/user") && info.suffix() == "service")
|| (info.path().contains("share/applications/context-menus"))) {
// We should not modify the files of the checked application directly, but
// should copy them to root entries directory and then modify.
std::filesystem::copy(source_path, target_path, ec);
if (ec) {
return LINGLONG_ERR("copy file failed: " + target_path.string(), ec);
}

exists = std::filesystem::exists(target_path, ec);
if (ec) {
return LINGLONG_ERR("check file exists", ec);
}

if (!exists) {
qWarning() << "failed to copy file: " << source_path.c_str();
continue;
}

auto ret = IniLikeFileRewrite(QFileInfo(target_path.c_str()), appID.c_str());
if (ret) {
continue;
}
}
}

std::filesystem::create_symlink(source_path, target_path, ec);
if (ec) {
return LINGLONG_ERR("create symlink failed: " + target_path.string(), ec);
Expand Down Expand Up @@ -1735,6 +1765,30 @@ OSTreeRepo::exportEntries(const std::filesystem::path &rootEntriesDir,
return LINGLONG_OK;
}

utils::error::Result<void> OSTreeRepo::fixExportAllEntries() noexcept
{
auto exportVersion = repoDir.absoluteFilePath("entries/.version").toStdString();
auto data = linglong::utils::readFile(exportVersion);
if (data && data == LINGLONG_EXPORT_VERSION) {
qDebug() << exportVersion.c_str() << data->c_str();
qDebug() << "skip export entry, already exported";
} else {
auto ret = exportAllEntries();
if (!ret.has_value()) {
qCritical() << "failed to export entries:" << ret.error();
return ret;
} else {
ret = linglong::utils::writeFile(exportVersion, LINGLONG_EXPORT_VERSION);
if (!ret.has_value()) {
qCritical() << "failed to write export version:" << ret.error();
return ret;
}
}
}

return LINGLONG_OK;
}

utils::error::Result<void> OSTreeRepo::exportAllEntries() noexcept
{
LINGLONG_TRACE("export all entries");
Expand Down
5 changes: 3 additions & 2 deletions libs/linglong/src/linglong/repo/ostree_repo.h
Original file line number Diff line number Diff line change
Expand Up @@ -90,8 +90,6 @@ class OSTreeRepo : public QObject
utils::error::Result<void> prune();

void removeDanglingXDGIntergation() noexcept;
// exportEntries will clear the entries/share and export all applications to the entries/share
utils::error::Result<void> exportAllEntries() noexcept;
// exportReference should be called when LayerDir of ref is existed in local repo
void exportReference(const package::Reference &ref) noexcept;
// unexportReference should be called when LayerDir of ref is existed in local repo
Expand Down Expand Up @@ -121,6 +119,7 @@ class OSTreeRepo : public QObject
getLayerItem(const package::Reference &ref,
std::string module = "binary",
const std::optional<std::string> &subRef = std::nullopt) const noexcept;
utils::error::Result<void> fixExportAllEntries() noexcept;

private:
api::types::v1::RepoConfig cfg;
Expand Down Expand Up @@ -164,6 +163,8 @@ class OSTreeRepo : public QObject
const std::filesystem::path &source,
const std::filesystem::path &destination,
const int &max_depth);
// exportEntries will clear the entries/share and export all applications to the entries/share
utils::error::Result<void> exportAllEntries() noexcept;
};

} // namespace linglong::repo

0 comments on commit 5db9d54

Please sign in to comment.