Skip to content
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
2 changes: 1 addition & 1 deletion apps/ll-cli/src/main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -112,7 +112,7 @@ int lockCheck() noexcept
return 0;
}

qCritical() << "failed to open lock" << lock << ::strerror(errno);
qCritical() << "failed to open lock" << lock << errorString(errno);
return -1;
}

Expand Down
5 changes: 2 additions & 3 deletions apps/ll-driver-detect/src/main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -8,10 +8,10 @@
#include "driver_detection_config.h"
#include "driver_detection_manager.h"
#include "driver_detector.h"
#include "linglong/utils/command/cmd.h"
#include "linglong/utils/cmd.h"

Check warning on line 11 in apps/ll-driver-detect/src/main.cpp

View workflow job for this annotation

GitHub Actions / cppcheck

Include file: "linglong/utils/command/cmd.h" not found.
#include "linglong/utils/error/error.h"

Check warning on line 12 in apps/ll-driver-detect/src/main.cpp

View workflow job for this annotation

GitHub Actions / cppcheck

Include file: "linglong/utils/error/error.h" not found.
#include "linglong/utils/gettext.h"

Check warning on line 13 in apps/ll-driver-detect/src/main.cpp

View workflow job for this annotation

GitHub Actions / cppcheck

Include file: "linglong/utils/gettext.h" not found.
#include "linglong/utils/global/initialize.h"

Check warning on line 14 in apps/ll-driver-detect/src/main.cpp

View workflow job for this annotation

GitHub Actions / cppcheck

Include file: "linglong/utils/global/initialize.h" not found.
#include "tl/expected.hpp"

#include <CLI/CLI.hpp>
Expand Down Expand Up @@ -105,8 +105,7 @@
}

// Execute ll-cli install command to install the package
auto ret = linglong::utils::command::Cmd("ll-cli").exec(
{ "install", QString::fromStdString(driverInfo.packageName) });
auto ret = linglong::utils::Cmd("ll-cli").exec({ "install", driverInfo.packageName });
if (!ret) {
return LINGLONG_ERR("Installation command failed: " + ret.error().message());
}
Expand Down
10 changes: 4 additions & 6 deletions apps/ll-driver-detect/src/nvidia_driver_detector.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,9 @@

#include "nvidia_driver_detector.h"

#include "linglong/utils/command/cmd.h"
#include "linglong/utils/cmd.h"

Check warning on line 7 in apps/ll-driver-detect/src/nvidia_driver_detector.cpp

View workflow job for this annotation

GitHub Actions / cppcheck

Include file: "linglong/utils/command/cmd.h" not found.
#include "linglong/utils/error/error.h"

Check warning on line 8 in apps/ll-driver-detect/src/nvidia_driver_detector.cpp

View workflow job for this annotation

GitHub Actions / cppcheck

Include file: "linglong/utils/error/error.h" not found.
#include "linglong/utils/log/log.h"

Check warning on line 9 in apps/ll-driver-detect/src/nvidia_driver_detector.cpp

View workflow job for this annotation

GitHub Actions / cppcheck

Include file: "linglong/utils/log/log.h" not found.

#include <filesystem>
#include <fstream>
Expand Down Expand Up @@ -53,13 +53,12 @@
{
LINGLONG_TRACE("Check if NVIDIA driver package exists in repository");
// Execute ll-cli search command to check driver package existence
auto ret = linglong::utils::command::Cmd("ll-cli").exec(
{ "search", QString::fromStdString(packageName) });
auto ret = linglong::utils::Cmd("ll-cli").exec({ "search", packageName });
if (!ret) {
return LINGLONG_ERR("Search command failed: " + ret.error().message());
}

if (!ret->contains(QString::fromStdString(packageName))) {
if (ret->find(packageName) == std::string::npos) {
return LINGLONG_ERR("Driver package not found in linglong repo: " + packageName);
}
return LINGLONG_OK;
Expand All @@ -72,8 +71,7 @@

try {
// First execute ll-cli info to get the package info
auto listResult =
linglong::utils::command::Cmd("ll-cli").exec({ "info", packageName.c_str() });
auto listResult = linglong::utils::Cmd("ll-cli").exec({ "info", packageName });

if (!listResult) {
LogD(
Expand Down
13 changes: 5 additions & 8 deletions libs/linglong/src/linglong/builder/linglong_builder.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -17,10 +17,10 @@
#include "linglong/package/layer_packager.h"
#include "linglong/package/reference.h"
#include "linglong/package/uab_packager.h"
#include "linglong/repo/config.h"

Check warning on line 20 in libs/linglong/src/linglong/builder/linglong_builder.cpp

View workflow job for this annotation

GitHub Actions / cppcheck

Include file: "linglong/repo/config.h" not found.
#include "linglong/repo/ostree_repo.h"

Check warning on line 21 in libs/linglong/src/linglong/builder/linglong_builder.cpp

View workflow job for this annotation

GitHub Actions / cppcheck

Include file: "linglong/repo/ostree_repo.h" not found.
#include "linglong/runtime/container.h"

Check warning on line 22 in libs/linglong/src/linglong/builder/linglong_builder.cpp

View workflow job for this annotation

GitHub Actions / cppcheck

Include file: "linglong/runtime/container.h" not found.
#include "linglong/utils/command/cmd.h"
#include "linglong/utils/cmd.h"
#include "linglong/utils/error/error.h"
#include "linglong/utils/file.h"
#include "linglong/utils/global/initialize.h"
Expand Down Expand Up @@ -985,11 +985,8 @@
scriptFile = dir->filePath("app-conf-generator");
QFile::copy(":/scripts/app-conf-generator", scriptFile);
}
auto output =
utils::command::Cmd("bash").exec({ "-e",
scriptFile,
QString::fromStdString(project.package.id),
QString::fromStdString(buildOutput.string()) });
auto output = utils::Cmd("bash").exec(
{ "-e", scriptFile.toStdString(), project.package.id, buildOutput.string() });
if (!output) {
return LINGLONG_ERR(output);
}
Expand Down Expand Up @@ -1700,8 +1697,8 @@
return LINGLONG_ERR(layerDir);
}

auto output =
utils::command::Cmd("cp").exec({ "-r", layerDir->absolutePath(), destDir.absolutePath() });
auto output = utils::Cmd("cp").exec(
{ "-r", layerDir->absolutePath().toStdString(), destDir.absolutePath().toStdString() });
if (!output) {
return LINGLONG_ERR(output);
}
Expand Down
19 changes: 9 additions & 10 deletions libs/linglong/src/linglong/builder/source_fetcher.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@
#include "source_fetcher.h"

#include "configure.h"
#include "linglong/utils/command/cmd.h"
#include "linglong/utils/error/error.h"
#include "linglong/utils/global/initialize.h"
#include "linglong/utils/log/log.h"
Expand Down Expand Up @@ -55,15 +54,15 @@ auto SourceFetcher::fetch(QDir destination) noexcept -> utils::error::Result<voi
LogD("Dumping {} from qrc to {}", scriptName, scriptFile);
QFile::copy(":/scripts/" + scriptName, scriptFile);
}
auto output =
m_cmd->setEnv("GIT_SUBMODULES", source.submodules.value_or(true) ? "true" : "")
.exec({
scriptFile,
destination.absoluteFilePath(getSourceName()),
QString::fromStdString(*source.url),
QString::fromStdString(source.kind == "git" ? *source.commit : *source.digest),
this->cacheDir.absolutePath(),
});
if (source.kind == "git") {
m_cmd->setEnv("GIT_SUBMODULES", source.submodules.value_or(true) ? "true" : "");
}
auto output = m_cmd->exec(
std::vector<std::string>{ scriptFile.toStdString(),
destination.absoluteFilePath(getSourceName()).toStdString(),
*source.url,
source.kind == "git" ? *source.commit : *source.digest,
this->cacheDir.absolutePath().toStdString() });
if (!output.has_value()) {
LogE("output error: {}", output.error());
return LINGLONG_ERR("stderr:", output);
Expand Down
6 changes: 3 additions & 3 deletions libs/linglong/src/linglong/builder/source_fetcher.h
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@

#include "linglong/api/types/v1/BuilderConfig.hpp"
#include "linglong/api/types/v1/BuilderProjectSource.hpp"
#include "linglong/utils/command/cmd.h"
#include "linglong/utils/cmd.h"
#include "linglong/utils/error/error.h"

#include <QDir>
Expand All @@ -25,13 +25,13 @@ class SourceFetcher

auto fetch(QDir destination) noexcept -> utils::error::Result<void>;

void setCommand(std::shared_ptr<utils::command::Cmd> cmd) { this->m_cmd = cmd; }
void setCommand(std::shared_ptr<utils::Cmd> cmd) { this->m_cmd = cmd; }

private:
QString getSourceName();
QDir cacheDir;
api::types::v1::BuilderProjectSource source;
std::shared_ptr<utils::command::Cmd> m_cmd = std::make_shared<utils::command::Cmd>("sh");
std::shared_ptr<utils::Cmd> m_cmd = std::make_shared<utils::Cmd>("sh");
};

} // namespace linglong::builder
15 changes: 7 additions & 8 deletions libs/linglong/src/linglong/cli/cli.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -483,8 +483,7 @@ int Cli::run(const RunOptions &options)
// placeholder file
auto fd = ::open(pidFile.c_str(), O_WRONLY | O_CREAT | O_EXCL, mode);
if (fd == -1) {
qCritical()
<< QString{ "create file %1 error: %2" }.arg(pidFile.c_str()).arg(::strerror(errno));
LogE("create file {} error: {}", pidFile, errorString(errno));
QCoreApplication::exit(-1);
return -1;
}
Expand Down Expand Up @@ -1712,7 +1711,7 @@ int Cli::content(const ContentOptions &options)
if (real != nullptr) {
target = real;
} else {
qCritical() << "resolve symlink " << file.c_str() << " error: " << ::strerror(errno);
LogE("resolve symlink {} error: {}", file, errorString(errno));
}
}

Expand Down Expand Up @@ -1992,7 +1991,7 @@ Cli::RequestDirectories(const api::types::v1::PackageInfoV2 &info) noexcept

auto fd = ::shm_open(info.id.c_str(), O_RDWR | O_CREAT, 0600);
if (fd < 0) {
return LINGLONG_ERR(QString{ "shm_open error:" } + ::strerror(errno));
return LINGLONG_ERR("shm_open error:" + errorString(errno));
}
auto closeFd = utils::finally::finally([fd] {
::close(fd);
Expand All @@ -2017,7 +2016,7 @@ Cli::RequestDirectories(const api::types::v1::PackageInfoV2 &info) noexcept
continue;
}

return LINGLONG_ERR(QString{ "fcntl lock error: " } + ::strerror(errno));
return LINGLONG_ERR("fcntl lock error: " + errorString(errno));
}

if (!anotherRunning) {
Expand All @@ -2027,7 +2026,7 @@ Cli::RequestDirectories(const api::types::v1::PackageInfoV2 &info) noexcept
lock.l_type = F_UNLCK;
ret = ::fcntl(fd, F_SETLK, &lock);
if (ret == -1) {
return LINGLONG_ERR(QString{ "fcntl unlock error: " } + ::strerror(errno));
return LINGLONG_ERR("fcntl unlock error: " + errorString(errno));
}

return LINGLONG_OK;
Expand All @@ -2037,11 +2036,11 @@ Cli::RequestDirectories(const api::types::v1::PackageInfoV2 &info) noexcept
auto releaseResource = utils::finally::finally([&info, &lock, fd] {
lock.l_type = F_UNLCK;
if (::fcntl(fd, F_SETLK, &lock) == -1) {
qDebug() << "failed to unlock mem file:" << ::strerror(errno);
LogD("failed to unlock mem file: {}", errorString(errno));
}

if (::shm_unlink(info.id.c_str()) == -1) {
qDebug() << "shm_unlink error:" << ::strerror(errno);
LogD("shm_unlink error: {}", errorString(errno));
}
});

Expand Down
4 changes: 3 additions & 1 deletion libs/linglong/src/linglong/package/layer_file.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,8 @@

#include "linglong/api/types/v1/Generators.hpp"
#include "linglong/api/types/v1/LayerInfo.hpp"
#include "linglong/utils/error/error.h"
#include "linglong/utils/log/formatter.h"
#include "linglong/utils/serialize/json.h"

#include <QDataStream>
Expand All @@ -31,7 +33,7 @@ utils::error::Result<QSharedPointer<LayerFile>> LayerFile::New(const QString &pa
LINGLONG_TRACE("install layer file from path")
auto fd = ::open(path.toLocal8Bit(), O_RDONLY);
if (fd < 0) {
return LINGLONG_ERR("failed to open " + path + ":" + ::strerror(errno));
return LINGLONG_ERR(fmt::format("failed to open {}: {}", path, ::errorString(errno)));
Comment thread
reddevillg marked this conversation as resolved.
}

return New(fd);
Expand Down
56 changes: 27 additions & 29 deletions libs/linglong/src/linglong/package/layer_packager.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,8 @@

#include "linglong/api/types/v1/Generators.hpp"
#include "linglong/api/types/v1/LayerInfo.hpp"
#include "linglong/utils/command/cmd.h"
#include "linglong/utils/command/env.h"
#include "linglong/utils/cmd.h"
#include "linglong/utils/file.h"
#include "linglong/utils/log/log.h"

#include <QDataStream>
Expand All @@ -28,8 +28,7 @@ LayerPackager::LayerPackager()
// maybe refactor on later
auto ret = this->initWorkDir();
if (!ret) {
qCritical() << "init work dir failed";
Q_ASSERT(false);
LogE("init work dir failed");
}
Comment on lines 30 to 32

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

high

The removal of Q_ASSERT(false) here means that if initWorkDir() fails, the constructor will silently continue, leaving the LayerPackager object in an invalid state (e.g., workDir is not initialized). This can lead to crashes or undefined behavior later on. A constructor that can fail should either throw an exception or you should use a factory function that returns a tl::expected<LayerPackager, ...> to handle the failure explicitly. Given the codebase's use of tl::expected, a factory function would be the idiomatic choice. The same issue exists in initWorkDir where another Q_ASSERT was removed.

}

Expand All @@ -48,16 +47,15 @@ utils::error::Result<void> LayerPackager::initWorkDir()
// 优先使用环境变量LINGLONG_TMPDIR指定的目录,默认为/var/tmp,避免/tmp是tmpfs内存不足
auto uuid = QUuid::createUuid().toString(QUuid::Id128);
auto dirName = "linglong-layer-workdir-" + uuid.toStdString();
auto tmpDir = utils::command::getEnv("LINGLONG_TMPDIR");
auto dirPath = std::filesystem::path(tmpDir.value_or("/var/tmp")) / dirName;
auto *tmpDir = std::getenv("LINGLONG_TMPDIR");
auto dirPath = std::filesystem::path(tmpDir ? tmpDir : "/var/tmp") / dirName;
auto ret = this->mkdirDir(dirPath);
if (!ret.has_value()) {
// 如果/var/tmp目录无权限创建,则使用临时目录
dirPath = std::filesystem::temp_directory_path() / dirName;
ret = this->mkdirDir(dirPath);
if (!ret) {
qCritical() << "failed to set work dir" << ret.error().message();
Q_ASSERT(false);
LogE("failed to set work dir: {}", ret.error());
}
}
this->workDir = dirPath;
Expand All @@ -84,16 +82,15 @@ utils::error::Result<void> LayerPackager::mkdirDir(const std::string &path) noex
LayerPackager::~LayerPackager()
{
if (this->isMounted) {
auto ret = utils::command::Cmd("fusermount")
auto ret = utils::Cmd("fusermount")
.exec({ "-z", "-u", (this->workDir / "unpack").string().c_str() });
if (!ret) {
qWarning() << "failed to umount " << (this->workDir / "unpack").c_str()
<< ", please umount it manually";
LogW("failed to umount {}, please umount it manually",
(this->workDir / "unpack").string());
}
}
if (!std::filesystem::remove_all(this->workDir)) {
LogE("failed to remove {}", this->workDir);
Q_ASSERT(false);
}
}

Expand Down Expand Up @@ -151,23 +148,21 @@ LayerPackager::pack(const LayerDir &dir, const QString &layerFilePath) const

// compress data with erofs
const auto &compressedFilePath = this->workDir / "tmp.erofs";
const auto &ignoreRegex = QString{ "--exclude-regex=minified*" };
// 使用-b统一指定block size为4096(2^12), 避免不同系统的兼容问题
// loongarch64默认使用(16384)2^14, 在x86和arm64不受支持, 会导致无法推包
auto ret = utils::command::Cmd("mkfs.erofs")
.exec({ "-z" + compressor,
"-b4096",
compressedFilePath.string().c_str(),
ignoreRegex,
dir.absolutePath() });
auto ret = utils::Cmd("mkfs.erofs")
.exec(std::vector<std::string>{ "-z" + compressor.toStdString(),
"-b4096",
compressedFilePath.string(),
"--exclude-regex=minified*",
dir.absolutePath().toStdString() });
if (!ret) {
return LINGLONG_ERR(ret);
}

ret = utils::command::Cmd("sh").exec(
{ "-c", QString("cat %1 >> %2").arg(compressedFilePath.string().c_str(), layerFilePath) });
if (!ret) {
LINGLONG_ERR(ret);
auto res = utils::concatFile(compressedFilePath, layerFilePath.toStdString());
if (!res) {
return LINGLONG_ERR(res);
}

auto result = LayerFile::New(layerFilePath);
Expand Down Expand Up @@ -242,8 +237,10 @@ utils::error::Result<LayerDir> LayerPackager::unpack(LayerFile &file)
}
fuseOffset = "0";
}
auto ret = utils::command::Cmd("erofsfuse")
.exec({ "--offset=" + fuseOffset, fdPath, unpackDir.absolutePath() });
auto ret = utils::Cmd("erofsfuse")
.exec({ "--offset=" + fuseOffset.toStdString(),
fdPath.toStdString(),
unpackDir.absolutePath().toStdString() });
if (!ret) {
return LINGLONG_ERR(ret);
}
Expand All @@ -252,15 +249,16 @@ utils::error::Result<LayerDir> LayerPackager::unpack(LayerFile &file)
}
// 判断fsck.erofs命令是否存在,fsck.erofs是erofs-utils的命令,可用于解压erofs文件
// 在旧版本中fsck.erofs不支持offset参数,所以需要提前将erofs文件复制到临时目录
auto erofsFscExistsRet = utils::command::Cmd("fsck.erofs").exists();
auto erofsFscExistsRet = utils::Cmd("fsck.erofs").exists();
if (erofsFscExistsRet) {
fdPath = (this->workDir / "layer.erofs").string().c_str();
auto ret = this->copyFile(file, fdPath.toStdString(), *offset);
if (!ret) {
return LINGLONG_ERR(ret);
}
auto cmdRet = utils::command::Cmd("fsck.erofs")
.exec({ "--extract=" + unpackDir.absolutePath(), fdPath });
auto cmdRet =
utils::Cmd("fsck.erofs")
.exec({ "--extract=" + unpackDir.absolutePath().toStdString(), fdPath.toStdString() });
if (!cmdRet) {
return LINGLONG_ERR(cmdRet);
}
Expand All @@ -278,7 +276,7 @@ void LayerPackager::setCompressor(const QString &compressor) noexcept

utils::error::Result<bool> LayerPackager::checkErofsFuseExists() const
{
return utils::command::Cmd("erofsfuse").exists();
return utils::Cmd("erofsfuse").exists();
}

} // namespace linglong::package
1 change: 1 addition & 0 deletions libs/linglong/src/linglong/package/layer_packager.h
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
#include "linglong/package/layer_file.h"
#include "linglong/utils/error/error.h"

#include <QSharedPointer>
#include <QString>
#include <QUuid>

Expand Down
Loading