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

+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());
}