Skip to content

Commit

Permalink
feat: recheck minified layer after uninstalling or installing layer
Browse files Browse the repository at this point in the history
support for looking up minified depends in `run` process

Signed-off-by: ComixHe <[email protected]>
  • Loading branch information
ComixHe authored and dengbo11 committed Jul 2, 2024
1 parent fd30959 commit 73c1bee
Show file tree
Hide file tree
Showing 6 changed files with 226 additions and 53 deletions.
12 changes: 6 additions & 6 deletions libs/linglong/src/linglong/builder/linglong_builder.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -708,19 +708,19 @@ utils::error::Result<void> Builder::build(const QStringList &args) noexcept
if (!result) {
qWarning() << "remove" << ref->toString() << result.error().message();
}
result = this->repo.importLayerDir(binaryOutputLayerDir);
if (!result) {
return LINGLONG_ERR(result);
auto localLayer = this->repo.importLayerDir(binaryOutputLayerDir);
if (!localLayer) {
return LINGLONG_ERR(localLayer);
}

package::LayerDir developOutputLayerDir = developOutput.absoluteFilePath("..");
result = this->repo.remove(*ref, true);
if (!result) {
qWarning() << "remove" << ref->toString() << result.error().message();
}
result = this->repo.importLayerDir(developOutputLayerDir);
if (!result) {
return LINGLONG_ERR(result);
localLayer = this->repo.importLayerDir(developOutputLayerDir);
if (!localLayer) {
return LINGLONG_ERR(localLayer);
}

printMessage("Successfully build " + this->project.package.id);
Expand Down
86 changes: 64 additions & 22 deletions libs/linglong/src/linglong/cli/cli.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -143,6 +143,48 @@ Cli::Cli(Printer &printer,
{
}

utils::error::Result<package::LayerDir> Cli::getDependLayerDir(
const package::Reference &appRef, const package::Reference &dependRef) const noexcept
{
LINGLONG_TRACE("get layer dir of depends")

auto appLayerDir = this->repository.getLayerDir(appRef);
if (!appLayerDir) {
return LINGLONG_ERR(appLayerDir);
}

auto dependLayerDir = this->repository.getLayerDir(dependRef);
if (!dependLayerDir) {
return LINGLONG_ERR(dependLayerDir);
}

if (!appLayerDir->exists(QString{ ".minified-%1" }.arg(dependRef.id))) {
return dependLayerDir;
}

auto minified = utils::serialize::LoadJSONFile<api::types::v1::MinifiedInfo>(
dependLayerDir->absoluteFilePath("minified.json"));
if (!minified) {
return LINGLONG_ERR(minified);
}

const auto &appRefStr = appRef.toString().toStdString();
QString subRef;
for (const auto &[appRef, uuid] : minified->infos) {
if (appRef == appRefStr) {
subRef = QString{ "minified/%1" }.arg(QString::fromStdString(uuid));
break;
}
}

if (subRef.isEmpty()) {
return LINGLONG_ERR("couldn't find the association between app and depend in minified.json "
"which under the layer directory.");
}

return this->repository.getLayerDir(dependRef, false, subRef);
}

int Cli::run(std::map<std::string, docopt::value> &args)
{
LINGLONG_TRACE("command run");
Expand All @@ -156,30 +198,29 @@ int Cli::run(std::map<std::string, docopt::value> &args)
return -1;
}

auto ref = this->repository.clearReference(*fuzzyRef,
{
.forceRemote = false,
.fallbackToRemote = false,
});
if (!ref) {
this->printer.printErr(ref.error());
auto curAppRef = this->repository.clearReference(*fuzzyRef,
{
.forceRemote = false,
.fallbackToRemote = false,
});
if (!curAppRef) {
this->printer.printErr(curAppRef.error());
return -1;
}

auto layerDir = this->repository.getLayerDir(*ref, false);
if (!layerDir) {
this->printer.printErr(layerDir.error());
auto appLayerDir = this->repository.getLayerDir(*curAppRef, false);
if (!appLayerDir) {
this->printer.printErr(appLayerDir.error());
return -1;
}

auto info = layerDir->info();
auto info = appLayerDir->info();
if (!info) {
this->printer.printErr(info.error());
return -1;
}

std::optional<package::LayerDir> runtimeLayerDir;

if (info->runtime) {
auto runtimeFuzzyRef =
package::FuzzyReference::parse(QString::fromStdString(*info->runtime));
Expand All @@ -198,13 +239,13 @@ int Cli::run(std::map<std::string, docopt::value> &args)
return -1;
}

auto layerDir = this->repository.getLayerDir(*runtimeRef);
if (!layerDir) {
this->printer.printErr(layerDir.error());
auto dependRet = getDependLayerDir(*curAppRef, *runtimeRef);
if (!dependRet) {
this->printer.printErr(dependRet.error());
return -1;
}

runtimeLayerDir = *layerDir;
runtimeLayerDir = *dependRet;
}

auto baseFuzzyRef = package::FuzzyReference::parse(QString::fromStdString(info->base));
Expand All @@ -223,7 +264,7 @@ int Cli::run(std::map<std::string, docopt::value> &args)
return -1;
}

auto baseLayerDir = this->repository.getLayerDir(*baseRef);
auto baseLayerDir = getDependLayerDir(*curAppRef, *baseRef);
if (!baseLayerDir) {
this->printer.printErr(LINGLONG_ERRV(baseLayerDir));
return -1;
Expand All @@ -242,7 +283,7 @@ int Cli::run(std::map<std::string, docopt::value> &args)
auto containers = this->ociCLI.list().value_or(std::vector<ocppi::types::ContainerListItem>{});
for (const auto &container : containers) {
const auto &decodedID = QString(QByteArray::fromBase64(container.id.c_str()));
if (!decodedID.startsWith(ref->toString())) {
if (!decodedID.startsWith(curAppRef->toString())) {
continue;
}

Expand Down Expand Up @@ -310,17 +351,18 @@ int Cli::run(std::map<std::string, docopt::value> &args)
if (perm->innerBinds) {
const auto &innerBinds = perm->innerBinds;
const auto &hostSourceDir =
std::filesystem::path{ layerDir->absolutePath().toStdString() };
std::filesystem::path{ appLayerDir->absolutePath().toStdString() };
std::for_each(innerBinds->cbegin(), innerBinds->cend(), bindInnerMount);
}
}

auto container = this->containerBuilder.create({
.appID = ref->id,
.containerID = (ref->toString() + "-" + QUuid::createUuid().toString()).toUtf8().toBase64(),
.appID = curAppRef->id,
.containerID =
(curAppRef->toString() + "-" + QUuid::createUuid().toString()).toUtf8().toBase64(),
.runtimeDir = runtimeLayerDir,
.baseDir = *baseLayerDir,
.appDir = *layerDir,
.appDir = *appLayerDir,
.patches = {},
.mounts = std::move(applicationMounts),
});
Expand Down
4 changes: 3 additions & 1 deletion libs/linglong/src/linglong/cli/cli.h
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,8 @@ class Cli : public QObject
static void filterPackageInfosFromType(std::vector<api::types::v1::PackageInfoV2> &list,
const QString &type) noexcept;
void updateAM() noexcept;
[[nodiscard]] utils::error::Result<package::LayerDir> getDependLayerDir(
const package::Reference &appRef, const package::Reference &ref) const noexcept;

public:
int run(std::map<std::string, docopt::value> &args);
Expand All @@ -74,7 +76,7 @@ class Cli : public QObject
void cancelCurrentTask();

private Q_SLOTS:
int installFromFile(const QFileInfo& fileInfo);
int installFromFile(const QFileInfo &fileInfo);
void processDownloadStatus(const QString &recTaskID,
const QString &percentage,
const QString &message,
Expand Down
50 changes: 44 additions & 6 deletions libs/linglong/src/linglong/package_manager/package_manager.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -315,7 +315,8 @@ QVariantMap PackageManager::installFromUAB(const QDBusUnixFileDescriptor &fd) no
}

layerInfos.erase(appLayerIt);
layerInfos.insert(layerInfos.begin(), std::move(appLayer));
layerInfos.insert(layerInfos.begin(),
std::move(appLayer)); // app layer should place to the first of vector
auto &taskRef = this->taskList.emplace_back(std::move(task));
connect(&taskRef, &InstallTask::TaskChanged, this, &PackageManager::TaskChanged);

Expand All @@ -335,6 +336,13 @@ QVariantMap PackageManager::installFromUAB(const QDBusUnixFileDescriptor &fd) no
}
this->taskList.erase(elem);
});

if (taskRef.currentStatus() == InstallTask::Canceled) {
qInfo() << "task" << taskRef.taskID() << "has been canceled by user, layer"
<< taskRef.layer();
return;
}

taskRef.updateStatus(InstallTask::preInstall, "prepare for installing uab");
auto verifyRet = uab->verify();
if (!verifyRet) {
Expand All @@ -347,12 +355,24 @@ QVariantMap PackageManager::installFromUAB(const QDBusUnixFileDescriptor &fd) no
return;
}

if (taskRef.currentStatus() == InstallTask::Canceled) {
qInfo() << "task" << taskRef.taskID() << "has been canceled by user, layer"
<< taskRef.layer();
return;
}

auto mountPoint = uab->mountUab();
if (!mountPoint) {
taskRef.updateStatus(InstallTask::Failed, std::move(mountPoint).error());
return;
}

if (taskRef.currentStatus() == InstallTask::Canceled) {
qInfo() << "task" << taskRef.taskID() << "has been canceled by user, layer"
<< taskRef.layer();
return;
}

const auto &uabLayersDirInfo = QFileInfo{ mountPoint->absoluteFilePath("layers") };
if (!uabLayersDirInfo.exists() || !uabLayersDirInfo.isDir()) {
taskRef.updateStatus(InstallTask::Failed,
Expand All @@ -362,7 +382,14 @@ QVariantMap PackageManager::installFromUAB(const QDBusUnixFileDescriptor &fd) no

utils::Transaction transaction;
const auto &uabLayersDir = QDir{ uabLayersDirInfo.absoluteFilePath() };
package::LayerDir appLayerDir;
for (const auto &layer : layerInfos) {
if (taskRef.currentStatus() == InstallTask::Canceled) {
qInfo() << "task" << taskRef.taskID() << "has been canceled by user, layer"
<< taskRef.layer();
return;
}

QDir layerDirPath = uabLayersDir.absoluteFilePath(
QString::fromStdString(layer.info.id) % QDir::separator()
% QString::fromStdString(layer.info.packageInfoV2Module));
Expand All @@ -380,11 +407,6 @@ QVariantMap PackageManager::installFromUAB(const QDBusUnixFileDescriptor &fd) no
subRef = "minified/" + QString::fromStdString(metaInfo.get().uuid);
}

bool isAppLayer = layer.info.kind == "app";
if (isAppLayer) { // it's meaningless for app layer that declare minified is true
subRef.clear();
}

auto infoRet = layerDir.info();
if (!infoRet) {
taskRef.updateStatus(InstallTask::Failed, std::move(infoRet).error());
Expand All @@ -399,6 +421,11 @@ QVariantMap PackageManager::installFromUAB(const QDBusUnixFileDescriptor &fd) no
}
auto &ref = *refRet;

bool isAppLayer = layer.info.kind == "app";
if (isAppLayer) { // it's meaningless for app layer that declare minified is true
subRef.clear();
}

auto ret = this->repo.importLayerDir(layerDir, subRef);
if (!ret) {
if (ret.error().code() == 0
Expand All @@ -419,7 +446,18 @@ QVariantMap PackageManager::installFromUAB(const QDBusUnixFileDescriptor &fd) no
}
});

if (isAppLayer) {
appLayerDir = *ret;
}

if (!subRef.isEmpty()) {
QFile tagFile =
appLayerDir.absoluteFilePath(QString{ ".minified-%1" }.arg(ref.id));
if (!tagFile.open(QIODevice::NewOnly | QIODevice::WriteOnly)) {
taskRef.updateStatus(InstallTask::Failed, tagFile.errorString());
return;
}

const auto &completedLayer = this->repo.getLayerDir(ref);
if (!completedLayer) {
taskRef.updateStatus(InstallTask::Failed, std::move(ret).error());
Expand Down
Loading

0 comments on commit 73c1bee

Please sign in to comment.