fix(update): replace the running binary atomically and relaunch it
tests / version-guard (push) Has been cancelled
tests / build-test (push) Has been cancelled

Copying over the executable the update is running from fails with ETXTBSY, so
the self-update aborted after a successful build and left the user to restart
by hand. Installing through a rename, then handing over to the new build, lets
the update complete on its own.

🤖 Generated with Codebuff
Co-Authored-By: Codebuff <noreply@codebuff.com>
This commit is contained in:
BlackAngelTV committed 2026-09-11 21:45:25 +02:00
1 parent 209e2706ee
commit 4ef37dcda6
6 files changed
+248 -38

No files matched your search

+33 -11
View File
@@ -32,6 +32,8 @@
#include <QFileDialog>
#include <QTextStream>
#include <QDir>
#include <QCoreApplication>
#include <QProcess>
#include "app-logging.h"
@@ -2555,8 +2557,8 @@ void MainWindow::onUpdateCheckFinished(bool hasUpdate, const QString& latestVers
// Ask the user whether to update now or skip. The update runs in the
// background (git pull from a checkout, clone + rebuild, prebuilt unpack,
// or tarball rebuild); the running binary stays untouched until the next
// launch.
// or tarball rebuild); the running binary stays untouched and the app
// hands over to the new build once the update succeeds.
const UpdateManager::UpdateMethod method = m_updateManager->updateMethod();
const QString targetDir = m_updateManager->binDir();
@@ -2613,21 +2615,41 @@ void MainWindow::onUpdateCheckFinished(bool hasUpdate, const QString& latestVers
m_updateManager->performSelfUpdate(sudoPassword);
}
// Called when the background git pull + build finishes. Reports the result in
// a dialog so the user knows whether to restart the app.
// Called when the background git pull + build finishes. A success hands over to
// the freshly installed binary right away; a failure is reported in a dialog.
void MainWindow::onSelfUpdateFinished(bool success, const QString& message) {
disconnect(m_updateManager, &UpdateManager::selfUpdateFinished,
this, &MainWindow::onSelfUpdateFinished);
if (success) {
QMessageBox::information(this, "Mise à jour terminée",
QString("<h3>PacManager a été mis à jour</h3>"
"<p>La nouvelle version sera active au prochain démarrage.</p>"
"<pre>%1</pre>")
.arg(message.toHtmlEscaped()));
} else {
if (!success) {
QMessageBox::warning(this, "Échec de la mise à jour",
QString("<p>La mise à jour a échoué :</p><pre>%1</pre>")
.arg(message.toHtmlEscaped()));
return;
}
// The build log is no longer shown to the user, so keep it in the app log
// for troubleshooting.
qCInfo(pacmgrUpdate) << "self-update output:" << message;
// Restarting is the only way to run the new build: this process still maps
// the inode that was replaced on disk. Ask for a manual restart only when
// no runnable binary is there to hand over to.
const QString program = UpdateManager::restartProgram(m_updateManager->binDir());
if (!program.isEmpty()
&& QProcess::startDetached(program, QCoreApplication::arguments().mid(1))) {
qCInfo(pacmgrUpdate) << "update installed, relaunching" << program;
statusBar()->showMessage("Mise à jour terminée, redémarrage de PacManager...");
// Quit on the next event-loop pass so the message paints first. The new
// process is detached and outlives this one.
QTimer::singleShot(0, qApp, &QCoreApplication::quit);
return;
}
qCWarning(pacmgrUpdate) << "could not relaunch" << program;
QMessageBox::information(this, "Mise à jour terminée",
QString("<h3>PacManager a été mis à jour</h3>"
"<p>La nouvelle version sera active au prochain démarrage.</p>"
"<pre>%1</pre>")
.arg(message.toHtmlEscaped()));
}
+56 -22
View File
@@ -131,6 +131,20 @@ QString UpdateManager::binDir() const {
return QCoreApplication::applicationDirPath();
}
// The install step always writes <binDir>/PacManager (CMake appends .exe on
// Windows), whatever name the running copy was launched under.
QString UpdateManager::restartProgram(const QString& binDir) {
if (binDir.isEmpty()) return {};
#ifdef Q_OS_WIN
const QString name = QStringLiteral("PacManager.exe");
#else
const QString name = QStringLiteral("PacManager");
#endif
const QFileInfo info(QDir(binDir).filePath(name));
if (!info.exists() || !info.isExecutable()) return {};
return info.absoluteFilePath();
}
// ~/.local/share/PacManager/src (honouring XDG_DATA_HOME): the same user data
// directory as the drop-in plugins, so it is writable and independent of where
// the binary itself lives.
@@ -189,23 +203,23 @@ QString UpdateManager::methodDescription(UpdateMethod method, const QString& tar
case UpdateMethod::Checkout:
return QStringLiteral(
"La mise à jour va récupérer le code (git pull), recompiler et "
"réinstaller.\nRedémarrez PacManager ensuite pour utiliser la "
"nouvelle version.");
"réinstaller.\nPacManager redémarrera automatiquement pour "
"utiliser la nouvelle version.");
case UpdateMethod::Clone:
return QStringLiteral(
"La mise à jour va cloner (puis mettre à jour) le dépôt source "
"et recompiler l'application dans %1.\nRedémarrez PacManager "
"ensuite pour utiliser la nouvelle version.").arg(targetDir);
"et recompiler l'application dans %1.\nPacManager redémarrera "
"automatiquement pour utiliser la nouvelle version.").arg(targetDir);
case UpdateMethod::Prebuilt:
return QStringLiteral(
"La mise à jour va télécharger la version précompilée et "
"l'installer dans %1 (sans compilation).\nRedémarrez PacManager "
"ensuite pour utiliser la nouvelle version.").arg(targetDir);
"l'installer dans %1 (sans compilation).\nPacManager redémarrera "
"automatiquement pour utiliser la nouvelle version.").arg(targetDir);
case UpdateMethod::Tarball:
return QStringLiteral(
"La mise à jour va télécharger les sources, les compiler et "
"installer l'application dans %1.\nRedémarrez PacManager "
"ensuite pour utiliser la nouvelle version.").arg(targetDir);
"installer l'application dans %1.\nPacManager redémarrera "
"automatiquement pour utiliser la nouvelle version.").arg(targetDir);
case UpdateMethod::None:
return QStringLiteral(
"Le dépôt source n'est pas disponible sur ce système. "
@@ -367,9 +381,9 @@ bool UpdateManager::isNewerVersion(const QString& remote, const QString& local)
}
// Rebuilds + reinstalls the app in the background, using the strategy picked
// by updateMethod(). The new binary takes effect at the next launch; the
// running process is untouched. Exits non-zero on failure so the caller can
// report it.
// by updateMethod(). The running process is untouched (it keeps the replaced
// inode); the caller relaunches into the new build. Exits non-zero on failure
// so the caller can report it.
void UpdateManager::performSelfUpdate(const QString& sudoPassword) {
const UpdateMethod method = updateMethod();
qCInfo(pacmgrUpdate) << "starting self-update, method"
@@ -409,7 +423,7 @@ void UpdateManager::performCheckoutUpdate() {
" cmake --build build -j$(nproc) 2>&1\n"
"fi\n"
"cmake --install build 2>&1\n"
"echo 'Mise a jour terminee. Redemarrez PacManager pour utiliser la nouvelle version.'");
"echo 'Mise a jour terminee.'");
runShell(script, m_repoPath);
}
@@ -431,18 +445,38 @@ QString UpdateManager::installedScriptPrologue() const {
// Copies a staged install tree ($STAGE/bin, plugins under bin/plugins or
// plugins/ depending on the source) into the running binary's directory.
static QString stagedCopyAction() {
//
// Every file goes through a sibling temp file + rename instead of a direct
// `cp`: the update replaces the very binary (and the plugins) the running
// process has open, and Linux refuses to open a file being executed for
// writing (ETXTBSY, "Text file busy"). rename(2) only swaps the directory
// entry, so the running process keeps its old inode and the next launch picks
// up the new build. The temp name is fixed, so a failed copy is overwritten by
// the next attempt instead of piling up in the target directory.
QString UpdateManager::stagedInstallScript() {
return QStringLiteral(
"set -e\n"
"umask 022\n" // installed files keep their source mode (binary stays executable)
"install_file() {\n"
" TMP=\"$2.new\"\n"
" rm -f \"$TMP\"\n"
" cp \"$1\" \"$TMP\"\n"
" mv -f \"$TMP\" \"$2\"\n"
"}\n"
"mkdir -p \"$BIN_DIR\"\n"
"cp \"$STAGE/bin/PacManager\" \"$BIN_DIR/PacManager\"\n"
"if [ -f \"$STAGE/bin/version\" ]; then cp \"$STAGE/bin/version\" \"$BIN_DIR/version\"; fi\n"
"install_file \"$STAGE/bin/PacManager\" \"$BIN_DIR/PacManager\"\n"
"if [ -f \"$STAGE/bin/version\" ]; then\n"
" install_file \"$STAGE/bin/version\" \"$BIN_DIR/version\"\n"
"fi\n"
"if [ -d \"$STAGE/bin/plugins\" ]; then SRC_PLUGINS=\"$STAGE/bin/plugins\";\n"
"elif [ -d \"$STAGE/plugins\" ]; then SRC_PLUGINS=\"$STAGE/plugins\";\n"
"else SRC_PLUGINS=\"\"; fi\n"
"if [ -n \"$SRC_PLUGINS\" ]; then\n"
" mkdir -p \"$BIN_DIR/plugins\"\n"
" cp \"$SRC_PLUGINS/\"*.so \"$BIN_DIR/plugins/\" 2>/dev/null || true\n"
" for SO in \"$SRC_PLUGINS\"/*.so; do\n"
" [ -e \"$SO\" ] || continue\n"
" install_file \"$SO\" \"$BIN_DIR/plugins/$(basename \"$SO\")\"\n"
" done\n"
"fi\n");
}
@@ -505,9 +539,9 @@ void UpdateManager::performCloneUpdate(const QString& sudoPassword) {
"cmake --build \"$SRC/build\" -j$(nproc) 2>&1\n"
"cmake --install \"$SRC/build\" --prefix \"$STAGE\" 2>&1\n")
.arg(quoteShell(sourceClonePath()), quoteShell(QString::fromLatin1(kRepoUrl)));
script += installCommand(quotedPassword(sudoPassword), stagedCopyAction());
script += installCommand(quotedPassword(sudoPassword), stagedInstallScript());
script += QStringLiteral(
"echo 'Mise a jour terminee. Redemarrez PacManager pour utiliser la nouvelle version.'");
"echo 'Mise a jour terminee.'");
runShell(script, QDir::homePath());
}
@@ -527,9 +561,9 @@ void UpdateManager::performPrebuiltUpdate(const QString& sudoPassword) {
"export STAGE\n"
"[ -x \"$STAGE/bin/PacManager\" ] || { echo 'Archive invalide'; exit 1; }\n")
.arg(quoteShell(m_prebuiltUrl));
script += installCommand(quotedPassword(sudoPassword), stagedCopyAction());
script += installCommand(quotedPassword(sudoPassword), stagedInstallScript());
script += QStringLiteral(
"echo 'Mise a jour terminee. Redemarrez PacManager pour utiliser la nouvelle version.'");
"echo 'Mise a jour terminee.'");
runShell(script, QDir::homePath());
}
@@ -558,8 +592,8 @@ void UpdateManager::performTarballUpdate(const QString& sudoPassword) {
"cmake --build \"$WORK/build\" -j$(nproc) 2>&1\n"
"cmake --install \"$WORK/build\" --prefix \"$STAGE\" 2>&1\n")
.arg(quoteShell(m_tarballUrl));
script += installCommand(quotedPassword(sudoPassword), stagedCopyAction());
script += installCommand(quotedPassword(sudoPassword), stagedInstallScript());
script += QStringLiteral(
"echo 'Mise a jour terminee. Redemarrez PacManager pour utiliser la nouvelle version.'");
"echo 'Mise a jour terminee.'");
runShell(script, QDir::homePath());
}