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 <noreply@codebuff.com>
202 lines
10 KiB
C++
202 lines
10 KiB
C++
#pragma once
|
|
|
|
#include <QObject>
|
|
#include <QNetworkAccessManager>
|
|
#include <QNetworkReply>
|
|
#include <QDateTime>
|
|
#include <QString>
|
|
#include <QStringList>
|
|
#include <QProcess>
|
|
|
|
// Checks the Gitea repository for new releases and tracks the last check
|
|
// state in a JSON file so the app only queries the API once per day.
|
|
// Self-update is location-independent: the fresh binary is always written to
|
|
// the directory that holds the running executable, wherever that is
|
|
// (~/.local/bin, /usr/bin, an arbitrary extracted folder, a build tree...).
|
|
// The strategy is picked by updateMethod():
|
|
// - Checkout: a source checkout around the binary is pulled + rebuilt, then
|
|
// installed into the running binary's directory like the other methods;
|
|
// - Clone: installed without a checkout, the repo is cloned (then pulled)
|
|
// 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 {
|
|
Q_OBJECT
|
|
public:
|
|
explicit UpdateManager(QObject* parent = nullptr);
|
|
|
|
// Current app version: the newest of the version compiled in from the
|
|
// repo-root `version` file (see CMakeLists.txt) and the one recorded in the
|
|
// `version` file installed next to the executable.
|
|
static QString currentVersion();
|
|
|
|
// Highest of two dotted versions. Used to reconcile the two places that know
|
|
// the version: either can lag behind (a build made without configuring
|
|
// again, or a marker left by an older install), and reporting the lower one
|
|
// would offer an already installed release for ever.
|
|
static QString newestVersion(const QString& a, const QString& b);
|
|
|
|
// Path of the state file storing the last check date and the latest
|
|
// known remote version (e.g. ~/.local/share/PacManager/update-state.json).
|
|
static QString stateFilePath();
|
|
|
|
// True when the last check happened less than 24h ago, so we can skip
|
|
// the network call at launch.
|
|
bool shouldCheck() const;
|
|
|
|
// Latest remote version recorded in the state file (empty if never checked).
|
|
QString lastKnownRemoteVersion() const;
|
|
|
|
// Fetches the latest release from the Gitea API. Emits checkFinished.
|
|
void checkForUpdates();
|
|
|
|
// True when the remote version is newer than the local one.
|
|
static bool isNewerVersion(const QString& remote, const QString& local);
|
|
|
|
// Path of the source repository, or empty when the app is installed
|
|
// without the repo (e.g. ~/.local/bin).
|
|
QString repoPath() const;
|
|
|
|
// Download URL of the source tarball for the pending update (empty until
|
|
// a check completes), plus the human-readable release page.
|
|
QString tarballUrl() const;
|
|
QString releasePageUrl() const;
|
|
|
|
// Prebuilt binary archive attached to the release
|
|
// (PacManager-<version>-x86_64.tar.gz), when the release workflow
|
|
// uploaded one. Used as a fallback when no build toolchain is installed.
|
|
QString prebuiltUrl() const;
|
|
|
|
// True when the pending update can be applied by unpacking a prebuilt
|
|
// archive instead of recompiling from source.
|
|
bool hasPrebuiltUpdate() const;
|
|
|
|
// Directory holding the running executable. The update always installs the
|
|
// new binary here (in place), so it works whatever the layout on disk.
|
|
QString binDir() const;
|
|
|
|
// Path of the freshly installed binary to hand over to after a successful
|
|
// update, or empty when `binDir` holds no runnable binary (the caller then
|
|
// falls back to asking for a manual restart). The running process still
|
|
// maps the replaced inode, so starting this file is the only way to pick up
|
|
// the new build without a manual restart. Static and parameterised so it
|
|
// can be unit-tested without performing an update.
|
|
static QString restartProgram(const QString& binDir);
|
|
|
|
// Persistent source clone used to update an installed app that has no
|
|
// local checkout (~/.local/share/PacManager/src). The first update clones,
|
|
// later ones fast-forward to the latest main.
|
|
QString sourceClonePath() const;
|
|
|
|
// How the pending update will be applied. Exposed so the UI can describe
|
|
// the operation (and offer the releases page when there is no way to
|
|
// update locally).
|
|
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.
|
|
struct UpdateCapabilities {
|
|
bool checkoutUsable = false; // a FWS source checkout surrounds the binary
|
|
bool hasGit = false;
|
|
bool hasCmake = false;
|
|
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);
|
|
|
|
// Human-readable description of the given method and install target, for
|
|
// the "update available" dialog.
|
|
static QString methodDescription(UpdateMethod method, const QString& targetDir);
|
|
|
|
// Shell script that installs a staged tree (`$STAGE/bin`, plugins under
|
|
// `$STAGE/bin/plugins` or `$STAGE/plugins`) into the running binary's
|
|
// directory (`$BIN_DIR`), both taken from the environment. Each file is
|
|
// copied to a sibling temp file and renamed into place, so the install
|
|
// also works when the target is the executable (or plugin) currently in
|
|
// use, where a plain `cp` fails with ETXTBSY ("Text file busy").
|
|
// Exposed so the unit test can drive it against a genuinely running
|
|
// 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;
|
|
|
|
// Rebuilds + reinstalls the app in the background. Pass the sudo password
|
|
// when the target directory is not user-writable (empty = no sudo pipe,
|
|
// `sudo` will prompt on its own if needed). Emits selfUpdateFinished.
|
|
void performSelfUpdate(const QString& sudoPassword = QString());
|
|
|
|
signals:
|
|
// Emitted after a check completes. hasUpdate is true when a newer
|
|
// release exists; errorMessage is non-empty when the request failed.
|
|
void checkFinished(bool hasUpdate, const QString& latestVersion,
|
|
const QString& errorMessage);
|
|
|
|
// Emitted when the background self-update completes. success is true
|
|
// if the pull + build produced a fresh executable.
|
|
void selfUpdateFinished(bool success, const QString& message);
|
|
|
|
private:
|
|
void saveState(const QString& remoteVersion);
|
|
void loadState();
|
|
void runShell(const QString& script, const QString& workingDir);
|
|
void performCheckoutUpdate(const QString& sudoPassword);
|
|
void performCloneUpdate(const QString& sudoPassword);
|
|
void performPrebuiltUpdate(const QString& sudoPassword);
|
|
void performTarballUpdate(const QString& sudoPassword);
|
|
// Shared script header for every update path: temp work dir, cleanup trap,
|
|
// the release being installed, and the export of the paths the install step
|
|
// needs.
|
|
QString updateScriptPrologue() const;
|
|
// Copy command that installs a staged tree ($STAGE/bin) into the running
|
|
// binary's directory, wrapped in sudo when the target is not writable.
|
|
QString installCommand(const QString& quotedPassword,
|
|
const QString& action) const;
|
|
// Directory holding the installed binary, quoted for the shell.
|
|
QString quotedBinDir() const;
|
|
static QString quoteShell(const QString& s);
|
|
static QString quotedPassword(const QString& sudoPassword);
|
|
// Tool lookup cached per process, so the update dialog does not spawn a
|
|
// fresh `command -v` for every check.
|
|
static bool toolAvailable(const char* tool);
|
|
static bool compilerAvailable();
|
|
|
|
QNetworkAccessManager m_network;
|
|
QProcess m_process; // runs the git pull + build script
|
|
QString m_lastCheckDate; // ISO date of the last successful check
|
|
QString m_lastKnownRemoteVersion; // latest version seen on the server
|
|
QString m_tarballUrl; // source tarball for the pending update
|
|
QString m_prebuiltUrl; // prebuilt binary archive, when attached
|
|
QString m_releasePageUrl; // human-readable release page
|
|
QString m_repoPath; // source repo dir when found
|
|
}; |