#pragma once #include #include #include #include #include #include // 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; // - 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 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, read from the version file at build time. static QString currentVersion(); // 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--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; // 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 }; 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(); // 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(); void performCloneUpdate(const QString& sudoPassword); void performPrebuiltUpdate(const QString& sudoPassword); void performTarballUpdate(const QString& sudoPassword); // Shared script header for the installed (no checkout) paths: temp work // dir, cleanup trap, and the export of the paths the install step needs. QString installedScriptPrologue() 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 };