fix(update): replace the running binary atomically and relaunch it
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:
1 parent
209e2706ee
commit
4ef37dcda6
6 files changed
+248
-38
No files matched your search
@@ -1,9 +1,17 @@
|
||||
// Unit tests for the pure parts of UpdateManager: version comparison and the
|
||||
// update-strategy choice (no network, toolchain or filesystem needed).
|
||||
// Unit tests for UpdateManager: version comparison, the update-strategy choice
|
||||
// (pure logic), and the staged install script (run for real against a running
|
||||
// binary, no network or toolchain needed).
|
||||
#include <cassert>
|
||||
#include <iostream>
|
||||
|
||||
#include <QDir>
|
||||
#include <QFile>
|
||||
#include <QFileInfo>
|
||||
#include <QProcess>
|
||||
#include <QProcessEnvironment>
|
||||
#include <QString>
|
||||
#include <QTemporaryDir>
|
||||
#include <QtGlobal>
|
||||
|
||||
#include "update-manager.h"
|
||||
|
||||
@@ -18,6 +26,20 @@ static void check(bool cond, const char* name) {
|
||||
}
|
||||
}
|
||||
|
||||
static bool writeFile(const QString& path, const QByteArray& content) {
|
||||
QFile f(path);
|
||||
if (!f.open(QIODevice::WriteOnly)) return false;
|
||||
return f.write(content) == content.size();
|
||||
}
|
||||
|
||||
// Byte-for-byte comparison: enough to tell a replaced file from the original.
|
||||
static bool sameFileContent(const QString& a, const QString& b) {
|
||||
QFile fa(a);
|
||||
QFile fb(b);
|
||||
if (!fa.open(QIODevice::ReadOnly) || !fb.open(QIODevice::ReadOnly)) return false;
|
||||
return fa.readAll() == fb.readAll();
|
||||
}
|
||||
|
||||
using Caps = UpdateManager::UpdateCapabilities;
|
||||
using Method = UpdateManager::UpdateMethod;
|
||||
|
||||
@@ -91,6 +113,113 @@ static void testChooseMethod_noneWhenNothingAvailable() {
|
||||
"method_none_without_anything");
|
||||
}
|
||||
|
||||
// The staged install must replace an executable that is currently running,
|
||||
// which is exactly the situation of the app updating itself: opening the busy
|
||||
// file for writing fails with ETXTBSY ("Text file busy"), so the script copies
|
||||
// to a sibling temp file and renames it over the target instead.
|
||||
// Unix-only: it needs /bin/sh and exec semantics, and /bin/sleep as a known
|
||||
// long-lived program.
|
||||
#if !defined(Q_OS_WIN)
|
||||
static void testStagedInstall_replacesRunningExecutable() {
|
||||
QTemporaryDir tmp;
|
||||
check(tmp.isValid(), "staged_install_tmpdir");
|
||||
if (!tmp.isValid()) return;
|
||||
|
||||
const QString binDir = tmp.path() + "/bin";
|
||||
const QString stage = tmp.path() + "/stage";
|
||||
QDir().mkpath(binDir + "/plugins");
|
||||
QDir().mkpath(stage + "/bin/plugins");
|
||||
|
||||
// Seed the target with a long-lived executable and start it, so the
|
||||
// destination really is "text busy" for the whole duration of the test.
|
||||
const QString target = binDir + "/PacManager";
|
||||
check(QFile::copy("/bin/sleep", target), "staged_install_seed_binary");
|
||||
|
||||
QProcess running;
|
||||
running.start(target, {"30"});
|
||||
check(running.waitForStarted(5000), "staged_install_binary_running");
|
||||
|
||||
// Staged tree: a different binary, a plugin and the version file.
|
||||
check(QFile::copy("/bin/echo", stage + "/bin/PacManager"),
|
||||
"staged_install_stage_binary");
|
||||
check(writeFile(stage + "/bin/version", "9.9.9"),
|
||||
"staged_install_stage_version");
|
||||
check(writeFile(stage + "/bin/plugins/fake-provider.so", "plugin-bytes"),
|
||||
"staged_install_stage_plugin");
|
||||
|
||||
QProcess sh;
|
||||
QProcessEnvironment env = QProcessEnvironment::systemEnvironment();
|
||||
env.insert("BIN_DIR", binDir);
|
||||
env.insert("STAGE", stage);
|
||||
sh.setProcessEnvironment(env);
|
||||
sh.start("/bin/sh", {"-c", UpdateManager::stagedInstallScript()});
|
||||
const bool finished = sh.waitForFinished(15000);
|
||||
const int exitCode = sh.exitCode();
|
||||
if (!finished || exitCode != 0) {
|
||||
std::cout << " sh output: " << sh.readAllStandardOutput().constData()
|
||||
<< sh.readAllStandardError().constData() << std::endl;
|
||||
}
|
||||
check(finished && sh.exitStatus() == QProcess::NormalExit && exitCode == 0,
|
||||
"staged_install_exit_zero");
|
||||
|
||||
check(sameFileContent(target, stage + "/bin/PacManager"),
|
||||
"staged_install_binary_replaced");
|
||||
check(sameFileContent(binDir + "/version", stage + "/bin/version"),
|
||||
"staged_install_version_replaced");
|
||||
check(sameFileContent(binDir + "/plugins/fake-provider.so",
|
||||
stage + "/bin/plugins/fake-provider.so"),
|
||||
"staged_install_plugin_replaced");
|
||||
|
||||
// The temporary copy must not survive in the target directory.
|
||||
check(QDir(binDir).entryList(QStringList() << "*.new", QDir::Files).isEmpty(),
|
||||
"staged_install_no_leftover_temp");
|
||||
|
||||
// The process started before the install still runs on its old inode, which
|
||||
// is what makes the replacement safe for the running app.
|
||||
check(running.state() == QProcess::Running, "staged_install_old_process_alive");
|
||||
|
||||
running.kill();
|
||||
running.waitForFinished(2000);
|
||||
}
|
||||
#endif // !Q_OS_WIN
|
||||
|
||||
// The restart after a successful update must point at the binary the update
|
||||
// just installed, and must refuse to launch anything that is missing or not
|
||||
// runnable (the caller then asks for a manual restart instead).
|
||||
static void testRestartProgram_findsInstalledBinary() {
|
||||
QTemporaryDir tmp;
|
||||
check(tmp.isValid(), "restart_program_tmpdir");
|
||||
if (!tmp.isValid()) return;
|
||||
|
||||
// Nothing installed yet, or no directory at all: nowhere to hand over to.
|
||||
check(UpdateManager::restartProgram(tmp.path()).isEmpty(),
|
||||
"restart_program_absent");
|
||||
check(UpdateManager::restartProgram(QString()).isEmpty(),
|
||||
"restart_program_empty_bindir");
|
||||
|
||||
#ifdef Q_OS_WIN
|
||||
const QString name = QStringLiteral("PacManager.exe");
|
||||
#else
|
||||
const QString name = QStringLiteral("PacManager");
|
||||
#endif
|
||||
const QString target = tmp.path() + "/" + name;
|
||||
check(writeFile(target, "#!/bin/sh\nexit 0\n"), "restart_program_write");
|
||||
|
||||
// Present but not runnable: still not something to launch. Windows derives
|
||||
// executability from the .exe suffix, hence the guard.
|
||||
#if !defined(Q_OS_WIN)
|
||||
QFile::setPermissions(target, QFileDevice::ReadOwner | QFileDevice::WriteOwner);
|
||||
check(UpdateManager::restartProgram(tmp.path()).isEmpty(),
|
||||
"restart_program_not_executable");
|
||||
#endif
|
||||
|
||||
QFile::setPermissions(target, QFileDevice::ReadOwner | QFileDevice::WriteOwner
|
||||
| QFileDevice::ExeOwner);
|
||||
check(UpdateManager::restartProgram(tmp.path())
|
||||
== QFileInfo(target).absoluteFilePath(),
|
||||
"restart_program_found");
|
||||
}
|
||||
|
||||
int main() {
|
||||
// Basic ordering
|
||||
check(UpdateManager::isNewerVersion("1.3.2", "1.3.1") == true, "newer_patch");
|
||||
@@ -122,6 +251,11 @@ int main() {
|
||||
testChooseMethod_tarballRequiresCompiler();
|
||||
testChooseMethod_noneWhenNothingAvailable();
|
||||
|
||||
#if !defined(Q_OS_WIN)
|
||||
testStagedInstall_replacesRunningExecutable();
|
||||
#endif
|
||||
testRestartProgram_findsInstalledBinary();
|
||||
|
||||
if (failures == 0) std::cout << "All update-manager tests passed" << std::endl;
|
||||
return failures == 0 ? 0 : 1;
|
||||
}
|
||||
Reference in new issue
Block a user