From b204d6814a105d39910c9325e14e4cd68d0a9e56 Mon Sep 17 00:00:00 2001 From: BlackAngelTV Date: Wed, 23 Sep 2026 21:58:21 +0200 Subject: [PATCH] feat(update): install missing build dependencies before rebuilding MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit A minimal Arch system has no cmake, no compiler and no qt6-base, so the self-update used to fail or fall back to the releases page. The update now asks pacman what is missing, installs it through sudo with the password typed in the update dialog, then resumes the build. 🤖 Generated with Codebuff Co-Authored-By: Codebuff --- include/update-manager.h | 26 ++++++++++ src/main-window.cpp | 31 +++++++++--- src/update-manager.cpp | 90 ++++++++++++++++++++++++++++++++--- tests/update-manager.test.cpp | 78 ++++++++++++++++++++++++++++++ 4 files changed, 212 insertions(+), 13 deletions(-) diff --git a/include/update-manager.h b/include/update-manager.h index c8146e9..362d230 100644 --- a/include/update-manager.h +++ b/include/update-manager.h @@ -5,6 +5,7 @@ #include #include #include +#include #include // Checks the Gitea repository for new releases and tracks the last check @@ -19,6 +20,10 @@ // into ~/.local/share/PacManager/src, built, and installed in place; // - Prebuilt: the release's prebuilt archive is unpacked (no compiler); // - Tarball: the release sources are built and installed in place. +// The three rebuilding methods (Checkout, Clone, Tarball) first make sure the +// build dependencies (base-devel, cmake, git, qt6-base) are installed: the +// update script asks pacman what is missing and installs it through sudo, so +// a minimal Arch system only needs its sudo password to self-update. // The running binary stays untouched (it keeps the inode that was replaced on // disk); the UI hands over to the freshly installed build by relaunching it. class UpdateManager : public QObject { @@ -95,6 +100,16 @@ public: enum class UpdateMethod { None, Checkout, Clone, Prebuilt, Tarball }; UpdateMethod updateMethod() const; + // Packages needed to rebuild the app. base-devel pulls gcc, make and the + // rest of the toolchain, git feeds the Clone method, and qt6-base provides + // the Widgets + Network modules the build links against. + static QStringList buildDependencies(); + + // Subset of buildDependencies() that pacman reports as missing (`pacman -T`, + // exit code 127), cached for the process lifetime. Empty when everything is + // installed, when pacman is absent (non-Arch system) or on Windows. + static QStringList missingBuildDependencies(); + // Inputs of the strategy decision, split out so the choice is a pure // function that can be unit-tested without a network, a filesystem or an // installed toolchain. @@ -105,6 +120,9 @@ public: bool hasCompiler = false; bool hasPrebuilt = false; // the release ships a prebuilt binary archive bool hasTarball = false; // the release ships a source tarball + // pacman is available, so whatever build tool is missing can be + // installed (with the user's sudo password) before rebuilding. + bool canInstallPackages = false; }; static UpdateMethod chooseUpdateMethod(const UpdateCapabilities& capabilities); @@ -122,6 +140,14 @@ public: // binary. static QString stagedInstallScript(); + // Shell snippet run at the start of every rebuilding method: installs the + // build dependencies pacman reports as missing (`pacman -T`) through sudo, + // piping the given (already shell-quoted) password to `sudo -S`. Without a + // password, `sudo -n` keeps a passwordless-sudo setup working while a + // password-requiring one fails fast instead of hanging the background run. + // Exposed so the unit test can assert what the update would install. + static QString dependencyInstallScript(const QString& quotedPassword); + // Whether a self-update is possible with the current state. bool canSelfUpdate() const; diff --git a/src/main-window.cpp b/src/main-window.cpp index 861f970..15e4e70 100644 --- a/src/main-window.cpp +++ b/src/main-window.cpp @@ -1149,21 +1149,40 @@ void MainWindow::onCoreUpdateCheckFinished(bool hasUpdate, const QString& latest return; } - // Prompt for the sudo password only when the update must write into a - // root-owned directory (e.g. /usr/bin). Every method installs into the - // running binary's directory, and only that final copy is privileged: - // everything else builds in a temp directory owned by the user. + // Prompt for the sudo password when the update needs privileges: either the + // final install writes into a root-owned directory (e.g. /usr/bin), or the + // build dependencies (qt6-base, cmake, gcc/base-devel, git) are missing and + // the update script must install them first — on a minimal Arch system both + // are why the update cannot run unprivileged. The password is consumed by + // `sudo -S` inside the update script itself (install of the dependencies, + // then the final copy). QString sudoPassword; - if (!m_core->binDirWritable()) { + const bool needsDeps = !UpdateManager::missingBuildDependencies().isEmpty(); + if (!m_core->binDirWritable() || needsDeps) { + QString reason; + if (needsDeps) { + reason = QStringLiteral( + "Des dépendances de compilation manquent (%1).\n") + .arg(UpdateManager::missingBuildDependencies().join(", ")); + } else { + reason = QStringLiteral("L'installation doit écrire dans %1.\n").arg(targetDir); + } bool ok = false; sudoPassword = QInputDialog::getText( this, "Mise à jour", - QString("Mot de passe sudo pour installer dans %1 :").arg(targetDir), + QString("%1Mot de passe sudo pour mettre à jour PacManager :\n") + .arg(reason), QLineEdit::Password, QString(), &ok); if (!ok) { statusBar()->showMessage("Mise à jour annulée."); return; } + if (!sudoPassword.isEmpty()) { + // The update pipeline also serves the other privileged flows, and + // it drops the password after each use: priming it here avoids a + // second prompt when the update itself was started from one. + m_core->setSudoPassword(sudoPassword); + } } qCInfo(pacmgrUpdate) << "applying update, method" << static_cast(method) diff --git a/src/update-manager.cpp b/src/update-manager.cpp index 9353bbc..0310653 100644 --- a/src/update-manager.cpp +++ b/src/update-manager.cpp @@ -119,6 +119,50 @@ QString UpdateManager::releasePageUrl() const { return m_releasePageUrl; } +// Packages required to rebuild the app. base-devel is the Arch metapackage +// pulling the whole toolchain (gcc, make, sudo…), git feeds the Clone method +// and qt6-base provides the Widgets + Network modules the build links against. +// cmake ships in base-devel on current Arch but is listed explicitly so a +// stripped-down system still gets it. +QStringList UpdateManager::buildDependencies() { + return {QStringLiteral("base-devel"), QStringLiteral("cmake"), + QStringLiteral("gcc"), QStringLiteral("git"), + QStringLiteral("qt6-base")}; +} + +// `pacman -T` answers "which of these packages is NOT installed" (exit 127 with +// the missing names on stdout), the exact question the update asks. The result +// is cached for the process lifetime: a package does not vanish between the +// dialog and the update, and the check spawns one pacman per call. +QStringList UpdateManager::missingBuildDependencies() { + static QStringList cached; + static bool fetched = false; + if (fetched) return cached; + fetched = true; + +#ifdef Q_OS_WIN + return cached; +#else + if (QProcess::execute("/bin/sh", {"-c", "command -v pacman >/dev/null 2>&1"}) != 0) { + return cached; // not an Arch system: nothing to install, and the + // rebuild methods will fail on their own if attempted + } + + QProcess pacman; + QStringList args{QStringLiteral("-T")}; + args += buildDependencies(); + pacman.start("pacman", args); + if (!pacman.waitForFinished(15000)) return cached; + // simplified() collapses newlines and runs of spaces into single spaces: + // pacman -T prints the missing targets whitespace-separated, and splitting + // the raw output would keep line breaks inside the tokens. + cached = QString::fromUtf8(pacman.readAllStandardOutput()) + .simplified() + .split(QLatin1Char(' '), Qt::SkipEmptyParts); + return cached; +#endif +} + // Cached `command -v` lookup. The update dialog queries several tools on // every check; caching keeps that from spawning a shell per query. bool UpdateManager::toolAvailable(const char* tool) { @@ -186,25 +230,33 @@ UpdateManager::UpdateMethod UpdateManager::updateMethod() const { capabilities.hasCompiler = compilerAvailable(); capabilities.hasPrebuilt = !m_prebuiltUrl.isEmpty(); capabilities.hasTarball = !m_tarballUrl.isEmpty(); + // pacman present = every missing build tool can be installed first, which + // makes the Clone/Tarball methods reachable on a minimal Arch system. + capabilities.canInstallPackages = toolAvailable("pacman"); return chooseUpdateMethod(capabilities); } // Strategy, in order of preference: // 1. Checkout: a source checkout is the developer's tree, pull + rebuild it; -// 2. Clone: rebuild from the real sources (git + cmake + compiler present), -// so an installed app follows the repo whatever its location; -// 3. Prebuilt: no toolchain, unpack the release's binary archive; -// 4. Tarball: no git but a compiler, build the release sources. +// 2. Clone: rebuild from the real sources (git + cmake + compiler present, or +// installable through pacman first), so an installed app follows the repo +// whatever its location; +// 3. Prebuilt: no toolchain and pacman cannot fill the gap, unpack the +// release's binary archive; +// 4. Tarball: no git but a buildable toolchain, build the release sources. // Anything else cannot update locally and falls back to the releases page. UpdateManager::UpdateMethod UpdateManager::chooseUpdateMethod( const UpdateCapabilities& capabilities) { if (capabilities.checkoutUsable) return UpdateMethod::Checkout; - if (capabilities.hasGit && capabilities.hasCmake && capabilities.hasCompiler) { + const bool buildable = capabilities.hasCmake && capabilities.hasCompiler; + // pacman present = every missing build tool can be installed first, which + // makes the rebuilding methods reachable on a minimal Arch system. + const bool buildableWithPacman = capabilities.canInstallPackages; + if (capabilities.hasGit && (buildable || buildableWithPacman)) { return UpdateMethod::Clone; } if (capabilities.hasPrebuilt) return UpdateMethod::Prebuilt; - if (capabilities.hasTarball && capabilities.hasCmake - && capabilities.hasCompiler) { + if (capabilities.hasTarball && (buildable || buildableWithPacman)) { return UpdateMethod::Tarball; } return UpdateMethod::None; @@ -392,6 +444,27 @@ bool UpdateManager::isNewerVersion(const QString& remote, const QString& local) return false; } +// Shell snippet shared by every rebuilding method: asks pacman which of the +// build dependencies are missing (`pacman -T`, exit 127) and installs them +// through `pacman -S --needed --noconfirm`. --needed skips what is already +// installed, --noconfirm keeps the unattended update from blocking on a +// pacman prompt. With a password it is piped to `sudo -S`; without one, `-n` +// keeps a passwordless-sudo setup working while a password-requiring sudo +// fails fast instead of hanging the background run on an unanswerable prompt. +// On a fully equipped machine the whole block is skipped. +QString UpdateManager::dependencyInstallScript(const QString& quotedPassword) { + const QString sudoPrefix = quotedPassword.isEmpty() + ? QStringLiteral("sudo -n") + : QStringLiteral("printf '%s\\n' %1 | sudo -S").arg(quotedPassword); + return QStringLiteral( + "MISSING=$(pacman -T %1 2>/dev/null || true)\n" + "if [ -n \"$MISSING\" ]; then\n" + " echo \"Installation des dependances de compilation : $MISSING\"\n" + " %2 pacman -S --needed --noconfirm $MISSING 2>&1\n" + "fi\n") + .arg(buildDependencies().join(QLatin1Char(' ')), sudoPrefix); +} + // Rebuilds + reinstalls the app in the background, using the strategy picked // by updateMethod(). The running process is untouched (it keeps the replaced // inode); the caller relaunches into the new build. Exits non-zero on failure @@ -427,6 +500,7 @@ void UpdateManager::performSelfUpdate(const QString& sudoPassword) { // leave the copy in use (and its version marker) untouched. void UpdateManager::performCheckoutUpdate(const QString& sudoPassword) { QString script = updateScriptPrologue(); + script += dependencyInstallScript(quotedPassword(sudoPassword)); script += QStringLiteral( "git pull --ff-only origin main 2>&1 || git pull --ff-only 2>&1\n" "if [ -d build ]; then\n" @@ -542,6 +616,7 @@ QString UpdateManager::quotedPassword(const QString& sudoPassword) { // running binary lives. void UpdateManager::performCloneUpdate(const QString& sudoPassword) { QString script = updateScriptPrologue(); + script += dependencyInstallScript(quotedPassword(sudoPassword)); script += QStringLiteral( "SRC=%1\n" "export SRC\n" @@ -593,6 +668,7 @@ void UpdateManager::performPrebuiltUpdate(const QString& sudoPassword) { // result where the running binary lives. Used only when git is unavailable. void UpdateManager::performTarballUpdate(const QString& sudoPassword) { QString script = updateScriptPrologue(); + script += dependencyInstallScript(quotedPassword(sudoPassword)); script += QStringLiteral( "command -v cmake >/dev/null 2>&1 || { echo 'cmake est requis pour la mise a jour'; exit 1; }\n" "URL=%1\n" diff --git a/tests/update-manager.test.cpp b/tests/update-manager.test.cpp index ccc468d..2342cbd 100644 --- a/tests/update-manager.test.cpp +++ b/tests/update-manager.test.cpp @@ -119,6 +119,79 @@ static void testChooseMethod_noneWhenNothingAvailable() { "method_none_without_anything"); } +// pacman can install whatever build tool is missing, so a minimal Arch system +// (no cmake, no compiler, not even git) still reaches the rebuilding methods: +// the update script installs the dependencies first, with the sudo password the +// user typed in the update dialog. +static void testChooseMethod_pacmanUnlocksClone() { + Caps caps; + caps.canInstallPackages = true; // nothing else installed + caps.hasGit = true; // git is the only tool the clone path + // strictly needs up front (deps install + // brings cmake + the compiler) + check(UpdateManager::chooseUpdateMethod(caps) == Method::Clone, + "method_clone_with_pacman_only"); +} + +static void testChooseMethod_pacmanWithoutGit_stillTarball() { + Caps caps; + caps.canInstallPackages = true; + caps.hasTarball = true; // no git: the tarball path also needs no tool now + check(UpdateManager::chooseUpdateMethod(caps) == Method::Tarball, + "method_tarball_with_pacman_only"); +} + +// Without pacman there is no way to fill a missing toolchain: the prebuilt +// archive (when the release ships one) stays the only option. +static void testChooseMethod_noPacman_prebuiltStillFallback() { + Caps caps; + caps.hasGit = true; // git alone cannot build, and pacman cannot fix it + caps.hasPrebuilt = true; + check(UpdateManager::chooseUpdateMethod(caps) == Method::Prebuilt, + "method_prebuilt_without_pacman"); +} + +// The dependency list and its `pacman -T` probe are pure data/IO: the list is +// asserted literally so a rename cannot silently break the install script, and +// the missing-dependency lookup must agree with pacman itself (empty on this +// machine means "nothing to install", not "unknown"). +static void testBuildDependencies() { + const QStringList deps = UpdateManager::buildDependencies(); + check(deps.contains("base-devel"), "deps_base_devel"); + check(deps.contains("cmake"), "deps_cmake"); + check(deps.contains("gcc"), "deps_gcc"); + check(deps.contains("qt6-base"), "deps_qt6_base"); + check(deps.contains("git"), "deps_git"); + + // On a machine without pacman this is empty and the update simply keeps + // its current behaviour; with pacman it must never report an installed + // package as missing. + const QStringList missing = UpdateManager::missingBuildDependencies(); + for (const QString& pkg : missing) { + check(deps.contains(pkg), "missing_dependency_is_from_the_list"); + } +} + +// The generated snippet must install exactly what pacman -T reports, through +// sudo (with the password piped to -S), and skip everything on a complete +// system. Quoted here is the password safety: the quoting is the same as the +// one used for the install step. +static void testDependencyInstallScript() { + const QString script = UpdateManager::dependencyInstallScript("'secret'"); + check(script.contains("pacman -T"), "script_asks_pacman_what_is_missing"); + check(script.contains("base-devel"), "script_installs_base_devel"); + check(script.contains("qt6-base"), "script_installs_qt6_base"); + check(script.contains("--needed --noconfirm"), "script_pacman_flags"); + check(script.contains("| sudo -S"), "script_pipes_password_to_sudo"); + check(script.contains("'secret'"), "script_uses_the_given_password"); + + // No password: sudo -n fails fast on a password-requiring setup instead of + // hanging the background update on an unanswerable prompt. + const QString passwordless = UpdateManager::dependencyInstallScript(QString()); + check(passwordless.contains("sudo -n"), "script_passwordless_uses_sudo_n"); + check(!passwordless.contains("sudo -S"), "script_passwordless_has_no_pipe"); +} + // Reconciliation of the two sources of truth: whichever is ahead wins, an empty // side never does. This is what stops a release from being offered again when the // binary and the installed marker disagree. @@ -327,6 +400,11 @@ int main() { testChooseMethod_tarballWithoutGit(); testChooseMethod_tarballRequiresCompiler(); testChooseMethod_noneWhenNothingAvailable(); + testChooseMethod_pacmanUnlocksClone(); + testChooseMethod_pacmanWithoutGit_stillTarball(); + testChooseMethod_noPacman_prebuiltStillFallback(); + testBuildDependencies(); + testDependencyInstallScript(); #if !defined(Q_OS_WIN) testStagedInstall_replacesRunningExecutable();