Gpt4all Custom Updater

This PR establishes the initial infrastructure required to migrate gpt4all away from reliance on the IFW framwork by implementing a custom updater.
This custom updater (currently headless only) can perform the same operations as the existing updater with the option for more functionality to be added and most importantly, is installer agnostic, meaning gpt4all can start to leverage platform specific installers.

Implements both offline and online installation and update mechanisms.

Initial implementation of: https://github.com/nomic-ai/gpt4all/issues/2878

Signed-off-by: John Parent <john.parent@kitware.com>
This commit is contained in:
John Parent 2024-10-19 01:06:36 -04:00
parent 9cafd38dcf
commit 0765f917a9
34 changed files with 1252 additions and 0 deletions

View File

@ -0,0 +1,73 @@
cmake_minimum_required(VERSION 3.16)
set(CMAKE_CXX_STANDARD 20)
set(CMAKE_CXX_STANDARD_REQUIRED ON)
set(APP_VERSION_MAJOR 0)
set(APP_VERSION_MINOR 0)
set(APP_VERSION_PATCH 0)
set(APP_VERSION ${APP_VERSION_MAJOR}.${APP_VERSION_MINOR}.${APP_VERSION_PATCH})
project(Gpt4AllAutoUpdater VERSION ${APP_VERSION} LANGUAGES CXX)
option(BUILD_OFFLINE_UPDATER "Build an offline updater" OFF)
if(BUILD_OFFLINE_UPDATER AND NOT GPT4ALL_INSTALLER_PATH)
message(FATAL_ERROR "The path to GPT4ALL's installer is required to construct this updater.
Please provide it on the command line using the argument -DGPT4ALL_INSTALLER_PATH=<pth>")
endif()
if(NOT BUILD_OFFLINE_UPDATER AND NOT GPT4ALL_MANIFEST_ENDPOINT)
message(FATAL_ERROR "The manifest endpoint was not provided, the online installer will be unable to detect updates")
endif()
if(APPLE)
option(BUILD_UNIVERSAL "Build universal binary on MacOS" OFF)
if(BUILD_UNIVERSAL)
set(CMAKE_OSX_ARCHITECTURES "arm64;x86_64" CACHE STRING "" FORCE)
else()
set(CMAKE_OSX_ARCHITECTURES "${CMAKE_HOST_SYSTEM_PROCESSOR}" CACHE STRING "" FORCE)
endif()
endif()
if(APPLE AND BUILD_OFFLINE_UPDATER)
enable_language(ASM)
configure_file(src/asm/bake_installer.S.in ${CMAKE_BINARY_DIR}/bake_installer.S @ONLY)
set(ASSEMBLER_SOURCES ${CMAKE_BINARY_DIR}/bake_installer.S src/Embedded.cxx)
elseif(WIN32 AND BUILD_OFFLINE_UPDATER)
configure_file(src/resources/installer.rc.in ${CMAKE_BINARY_DIR}/resource.rc @ONLY)
set(RC_FILES ${CMAKE_BINARY_DIR}/resource.rc src/Resource.cxx)
endif()
if(NOT BUILD_OFFLINE_UPDATER)
configure_file(src/Download.cxx.in ${CMAKE_BINARY_DIR}/Download.cxx @ONLY)
endif()
find_package(Qt6 REQUIRED COMPONENTS Core Network)
set(auto_updater_sources
src/Command.cxx
src/CommandFactory.cxx
src/CommandLine.cxx
src/Downgrade.cxx
${CMAKE_BINARY_DIR}/Download.cxx
src/Manifest.cxx
src/Modify.cxx
src/Package.cxx
src/State.cxx
src/Uninstall.cxx
src/Update.cxx
src/utils.cxx
src/main.cxx
${ASSEMBLER_SOURCES}
${RC_FILES}
)
add_executable(autoupdater ${auto_updater_sources})
target_link_libraries(autoupdater PRIVATE Qt6::Core Qt6::Network)
target_include_directories(autoupdater PRIVATE include)
if(BUILD_OFFLINE_UPDATER)
target_compile_definitions(autoupdater PRIVATE OFFLINE)
endif()

30
gpt4all-updater/README Normal file
View File

@ -0,0 +1,30 @@
# Gpt4All Updater
## Testing
Testing this updater requires a bit of manual setup and walking through the
integration with gpt4all's installer, as this process has yet to be fully automated.
There are two modes, offline and online, each is tested differently:
### Online
The online updater workflow takes a url endpoint (for early testing, this will be a file url) that points to a manifest file, a sample of which has been provided under the `tmp` directory relative to this README. The manifest file includes, amount other things specified in the updater RFC, another file URL to a gpt4all installer.
The first thing testing this will require is a manual update of that file url in the manifest xml file to point to the DMG on MacOS or exe on Windows, of an existing online Gpt4All installer, as well as a corresponding sha256 sum of the given installer. The manifest is filled with other stub info for testing, you're welcome to leave it as is or fill it out correctly for each testing iteration.
That is all that is required for configuration. Now simply build this project via CMake, using standard CMake build practices. CMake will build the online installer by default, so no extra arguments are required.
One argument is required for the online installer, the url where the updater should expect the manifest file to be. This can be any url accessible to your system, but for simplicity and testing reasons, its usually best to use a file url.
This is provided via the cmake command line argument `-DGPT4ALL_MANIFEST_ENDPOINT=<url>`.
Now configure and build the updater with CMake.
To test the installer, query the command line interface using the `--help` argument to determine which actions are available. Then select a given action, and provide the required arguments (there shouldn't be any at the moment), and let the updater drive. The updater will determine the operation requested, fetch the appropriate installer, and drive said installer with the appropriate arguments to effect the requested operation. If the operation is a modification or uninstall, the updater will not fetch a new installer, and instead will execute the older installer, as a new installer is not required.
### Offline
The offline updater is somewhat simpler. To instruct CMake to build the offline updater, specify the `-DBUILD_OFFLINE_UPDATER=ON` argument to CMake on the command line. One additional argument is required to properly configure CMake for the offline updater project, `-DGPT4ALL_INSTALLER_PATH` which should be set to the path to the DMG file containing an offline version of the GPT4All installer.
Now, after building, simply run the updater. It should remove your current installation of Gpt4All (but not the config or models), and then run the offline installer in install mode. Once that process is complete, you should have an upgraded Gpt4All available on your system.

View File

@ -0,0 +1,18 @@
#pragma once
#include <QCoreApplication>
#include <QProcess>
#include <QStringList>
#include <QFile>
namespace gpt4all {
namespace command{
class Command
{
public:
virtual bool execute();
QStringList arguments;
QFile* installer;
};
}
}

View File

@ -0,0 +1,12 @@
#pragma once
#include "Command.h"
#include "CommandLine.h"
class CommandFactory : public QObject
{
public:
std::shared_ptr<gpt4all::command::Command> GetCommand(gpt4all::command::CommandType type);
};

View File

@ -0,0 +1,30 @@
#pragma once
#include <QCommandLineParser>
namespace gpt4all {
namespace command {
enum CommandType {
UPDATE,
MODIFY,
DOWNGRADE,
UNINSTALL
};
class CommandLine : public QObject
{
public:
CommandLine();
~CommandLine();
void parse(QCoreApplication &app);
CommandType command();
private:
CommandType type;
QCommandLineParser * parser;
};
}
}

View File

@ -0,0 +1,17 @@
#pragma once
#include "Command.h"
namespace gpt4all {
namespace downgrade {
class Downgrade : public gpt4all::command::Command
{
public:
Downgrade(QFile &installer);
};
}
}

View File

@ -0,0 +1,65 @@
#pragma once
#include "Manifest.h"
#include "State.h"
#include <QCryptographicHash>
#include <QDateTime>
#include <QFile>
#include <QHash>
#include <QList>
#include <QMap>
#include <QObject>
#include <QtGlobal>
#include <QVersionNumber>
#include <QSslError>
#include <QThread>
#include <QString>
#include <QNetworkAccessManager>
#include <QNetworkReply>
#include <QCoreApplication>
namespace gpt4all {
namespace download {
class HashFile : public QObject
{
public:
HashFile() : QObject(nullptr) {}
void hashAndInstall(const QString &expected, QCryptographicHash::Algorithm algo, QFile *temp, const QString &file, QNetworkReply *reply);
};
class Download : public QObject
{
public:
static Download *instance();
Q_INVOKABLE void driveFetchAndInstall();
Q_INVOKABLE void downloadManifest();
Q_INVOKABLE void downloadInstaller();
Q_INVOKABLE void cancelDownload();
Q_INVOKABLE void installInstaller(QString &expected, QFile *temp, QNetworkReply *installerResponse);
private Q_SLOTS:
void handleSslErrors(QNetworkReply *reply, const QList<QSslError> &errors);
void handleErrorOccurred(QNetworkReply::NetworkError code);
void handleInstallerDownloadFinished();
void handleReadyRead();
private:
explicit Download();
~Download();
QNetworkReply * downloadInMemory(QUrl &url);
QNetworkReply * downloadLargeFile(QUrl &url);
QIODevice * handleManifestRequestResponse(QNetworkReply * reply);
gpt4all::manifest::ManifestFile *manifest;
QNetworkAccessManager m_networkManager;
QMap<QNetworkReply*, QFile*> download_tracking;
HashFile *saver;
QDateTime m_startTime;
QString platform_ext;
QFile *downloadPath;
friend class Downloader;
};
}
}

View File

@ -0,0 +1,37 @@
#pragma once
#include <iostream>
#include <fstream>
#include <sstream>
#include <QCoreApplication>
#include <QFile>
#include <QByteArray>
// Symbols indicating beginning and end of embedded installer
extern "C" {
extern char data_start_gpt4all_installer, data_stop_gpt4all_installer;
extern int gpt4all_installer_size;
}
namespace gpt4all {
namespace embedded {
class EmbeddedInstaller : public QObject
{
public:
EmbeddedInstaller();
void extractAndDecode();
void installInstaller();
private:
char * start;
char * end;
int size;
QByteArray data;
};
}
}

View File

@ -0,0 +1,46 @@
#pragma once
#include "Package.h"
#include <QVersionNumber>
#include <QDate>
#include <QFile>
#include <QXmlStreamReader>
#include <QString>
#include <QStringList>
namespace gpt4all{
namespace manifest {
enum releaseType{
RELEASE,
DEBUG
};
class ManifestFile {
public:
static ManifestFile * parseManifest(QIODevice *manifestInput);
QUrl & getInstallerEndpoint();
QString & getExpectedHash();
private:
ManifestFile() {}
QString name;
QVersionNumber release_ver;
QString notes;
QStringList authors;
QDate release_date;
releaseType config;
QVersionNumber last_supported_version;
QString entity;
QStringList component_list;
Package pkg;
void parsePkgDescription();
void parsePkgManifest();
QXmlStreamReader xml;
};
}
}

View File

@ -0,0 +1,15 @@
#pragma once
#include "Command.h"
namespace gpt4all {
namespace modify {
class Modify : public gpt4all::command::Command
{
public:
Modify(QFile &installer);
};
}
}

View File

@ -0,0 +1,29 @@
#pragma once
#include <string>
#include <vector>
#include <QCryptographicHash>
#include <QXmlStreamReader>
#include <QString>
#include <QStringList>
#include <QUrl>
namespace gpt4all {
namespace manifest {
class Package {
public:
Package() {}
static Package parsePackage(QXmlStreamReader &xml);
QString checksum_sha256;
bool is_signed;
QUrl installer_endpoint;
QUrl sbom_manifest;
QStringList installer_args;
};
}
}

View File

@ -0,0 +1,18 @@
#pragma once
#include <stdio.h>
#include <windows.h>
#include <QCoreApplication>
namespace gpt4all {
namespace resource {
class WinInstallerResources : public QObject
{
public:
static int extractAndInstall();
}
}
}

View File

@ -0,0 +1,38 @@
#pragma once
#include <QDir>
#include <QVersionNumber>
#include <QString>
#include <QStringList>
#include <QFile>
#include <QCoreApplication>
namespace gpt4all {
namespace state {
class Gpt4AllState : public QObject {
public:
virtual ~Gpt4AllState() = default;
static Gpt4AllState & getInstance();
QVersionNumber &getCurrentGpt4AllVersion();
QFile &getCurrentGpt4AllConfig();
QDir &getCurrentGpt4AllInstallRoot();
bool checkForExistingInstall();
QFile &getInstaller();
void setInstaller(QFile *installer);
void removeCurrentInstallation();
bool getExistingInstaller();
#ifdef OFFLINE
void driveOffline();
#endif
private:
explicit Gpt4AllState(QObject *parent = nullptr) {}
QFile *installer;
QFile currentGpt4AllConfig;
QDir currentGpt4AllInstallPath;
QVersionNumber currentGpt4AllVersion;
};
}
}

View File

@ -0,0 +1,16 @@
#pragma once
#include "Command.h"
namespace gpt4all {
namespace uninstall {
class Uninstall : public gpt4all::command::Command
{
public:
Uninstall(QFile &uninstaller);
};
}
}

View File

@ -0,0 +1,15 @@
#pragma once
#include "Command.h"
namespace gpt4all {
namespace updater {
class Update : public gpt4all::command::Command
{
public:
Update(QFile &installer);
};
}
}

View File

@ -0,0 +1,7 @@
#pragma once
#include <stdlib.h>
#include <QString>
constexpr uint32_t hash(const char* data) noexcept;
void mountAndExtract(const QString &mountPoint, const QString &saveFilePath, const QString &appBundlePath);

View File

@ -0,0 +1,11 @@
#include "Command.h"
using namespace gpt4all::command;
bool Command::execute() {
if (!this->installer->exists()) {
qDebug() << "Couldn't find the installer, there may have been an issue extracting or downloading the installer";
return false;
}
return QProcess::startDetached(this->installer->fileName(), this->arguments);
}

View File

@ -0,0 +1,49 @@
#include "CommandFactory.h"
#include "Downgrade.h"
#include "Modify.h"
#include "Uninstall.h"
#include "Update.h"
#include "State.h"
#include "Download.h"
using namespace gpt4all::command;
std::shared_ptr<Command> CommandFactory::GetCommand(CommandType type)
{
std::shared_ptr<Command> active_command;
#ifdef OFFLINE
// If we're offline, we're only installing or removing
gpt4all::state::Gpt4AllState::getInstance().driveOffline();
// This command always removes the current install
// only return a command if updating
if (type == CommandType::UPDATE)
{
active_command = std::make_shared<gpt4all::updater::Update>(gpt4all::state::Gpt4AllState::getInstance().getInstaller());
}
else {
active_command = std::shared_ptr<gpt4all::command::Command>(nullptr);
}
#else
if (type == CommandType::UPDATE || type == CommandType::DOWNGRADE) {
gpt4all::download::Download::instance()->driveFetchAndInstall();
}
else {
if(!gpt4all::state::Gpt4AllState::getInstance().getExistingInstaller()){
qWarning() << "Unable to execute requested command, requires existing installation of gpt4all";
QCoreApplication.exit(1);
}
}
QFile &installer = gpt4all::state::Gpt4AllState::getInstance().getInstaller();
switch(type){
case CommandType::UPDATE:
active_command = std::make_shared<gpt4all::updater::Update>(installer);
case CommandType::DOWNGRADE:
active_command = std::make_shared<gpt4all::downgrade::Downgrade>(installer);
case CommandType::MODIFY:
active_command = std::make_shared<gpt4all::modify::Modify>(installer);
case CommandType::UNINSTALL:
active_command = std::make_shared<gpt4all::uninstall::Uninstall>(installer);
}
#endif
return active_command;
}

View File

@ -0,0 +1,61 @@
#include "CommandLine.h"
#include "utils.h"
gpt4all::command::CommandLine::CommandLine()
{
this->parser = new QCommandLineParser();
this->parser->addHelpOption();
this->parser->addVersionOption();
this->parser->addPositionalArgument("command", QCoreApplication::translate("main", "Gpt4All installation modification command, must be one of: 'update', 'downgrade', 'uninstall', 'modify' (note: only upgrade and uninstall are available offline)"));
}
gpt4all::command::CommandLine::~CommandLine()
{
free(this->parser);
}
void gpt4all::command::CommandLine::parse(QCoreApplication &app)
{
this->parser->process(app);
}
constexpr uint32_t hash(const char* data) noexcept{
uint32_t hash = 5381;
for(const char *c = data; *c; ++c)
hash = ((hash << 5) + hash) + (unsigned char) *c;
return hash;
}
gpt4all::command::CommandType gpt4all::command::CommandLine::command()
{
const QStringList args(this->parser->positionalArguments());
if (args.empty()){
this->parser->showHelp(1);
}
gpt4all::command::CommandType ret;
QString command_arg = args.first();
switch(hash(command_arg.toStdString().c_str())) {
case hash("update"):
ret = gpt4all::command::CommandType::UPDATE;
break;
#ifndef OFFLINE
case hash("modify"):
ret = gpt4all::command::CommandType::MODIFY;
break;
case hash("downgrade"):
ret = gpt4all::command::CommandType::DOWNGRADE;
break;
#endif
case hash("uninstall"):
ret = gpt4all::command::CommandType::UNINSTALL;
break;
}
if(!ret)
qWarning() << "Invalid command option";
QCoreApplication::exit(1);
return ret;
}

View File

@ -0,0 +1,9 @@
#include "Downgrade.h"
using namespace gpt4all::downgrade;
Downgrade::Downgrade(QFile &installer)
{
this->installer = &installer;
this->arguments = {"-c", "rm"};
}

View File

@ -0,0 +1,241 @@
#include "Download.h"
#include "utils.h"
#include <QNetworkRequest>
#include <QProcess>
#include <QUrl>
#include <QSslConfiguration>
#include <QSslSocket>
#include <QGlobalStatic>
using namespace gpt4all::download;
using namespace Qt::Literals::StringLiterals;
Download * Download::instance()
{
return new Download();
}
Download::Download()
: QObject(nullptr),
saver(new HashFile)
{
#if defined(Q_OS_WINDOWS)
platform_ext = ".exe";
#else
platform_ext = ".dmg";
#endif
connect(&m_networkManager, &QNetworkAccessManager::sslErrors, this, &Download::handleSslErrors);
}
Download::~Download(){
free(this->saver);
free(this->manifest);
free(this->downloadPath);
}
void Download::driveFetchAndInstall()
{
this->downloadManifest();
this->downloadInstaller();
}
void Download::downloadInstaller()
{
qWarning() << "Downloading installer from " << this->manifest->getInstallerEndpoint();
this->downloadLargeFile(this->manifest->getInstallerEndpoint());
}
void Download::downloadManifest()
{
QUrl manifestEndpoint("@GPT4ALL_MANIFEST_ENDPOINT@");
QNetworkReply *manifestResponse = this->downloadInMemory(manifestEndpoint);
this->manifest = gpt4all::manifest::ManifestFile::parseManifest(manifestResponse);
}
void Download::installInstaller(QString &expected, QFile *temp, QNetworkReply *installerResponse)
{
const QString saveFilePath = QDir::tempPath() + "gpt4all-installer" + platform_ext;
this->saver->hashAndInstall(expected,
QCryptographicHash::Sha256,
temp, saveFilePath, installerResponse);
#if defined(Q_OS_DARWIN)
QString mountPoint = "/Volumes/gpt4all-installer-darwin";
QString appBundlePath = QDir::tempPath() + "gpt4all-installer-darwin.app";
mountAndExtract(mountPoint, saveFilePath, appBundlePath);
this->downloadPath = new QFile(appBundlePath + "/Contents/MacOS/gpt4all-installer-darwin");
#elif defined(Q_OS_WINDOWS)
this->downloadPath = new QFile(saveFilePath);
#endif
temp->deleteLater();
// Extraction is not required on Windows because we download an exe directly
gpt4all::state::Gpt4AllState::getInstance().setInstaller(this->downloadPath);
}
QNetworkReply * Download::downloadInMemory(QUrl &url)
{
QNetworkRequest request(url);
QSslConfiguration conf = request.sslConfiguration();
conf.setPeerVerifyMode(QSslSocket::VerifyNone);
request.setSslConfiguration(conf);
QNetworkReply *reply = m_networkManager.get(request);
if (!reply) {
return nullptr;
}
if (reply->error() != QNetworkReply::NoError) {
qWarning() << "Error: network error occurred while downloading the release manifest:" << reply->errorString();
reply->deleteLater();
}
return reply;
}
QNetworkReply * Download::downloadLargeFile(QUrl &url)
{
QString tempFilePath = QDir::tempPath() + "gpt4all-installer" + "-partial" + platform_ext;
QFile *tempFile = new QFile(tempFilePath);
bool success = tempFile->open(QIODevice::WriteOnly | QIODevice::Append);
qWarning() << "Opening temp file for writing:" << tempFile->fileName();
if (!success) {
const QString error = u"ERROR: Could not open temp file: %1 %2"_s.arg(tempFile->fileName());
qWarning() << error;
return nullptr;
}
tempFile->flush();
size_t incomplete_size = tempFile->size();
if (incomplete_size > 0) {
bool success = tempFile->seek(incomplete_size);
if (!success) {
incomplete_size = 0;
success = tempFile->seek(incomplete_size);
Q_ASSERT(success);
}
}
QNetworkRequest request(url);
request.setAttribute(QNetworkRequest::User, tempFilePath);
request.setRawHeader("range", u"bytes=%1-"_s.arg(tempFile->pos()).toUtf8());
QSslConfiguration conf = request.sslConfiguration();
conf.setPeerVerifyMode(QSslSocket::VerifyNone);
request.setSslConfiguration(conf);
QEventLoop loop;
QNetworkReply *installerDownResponse = m_networkManager.get(request);
connect(installerDownResponse, &QNetworkReply::errorOccurred, this, &Download::handleErrorOccurred);
connect(installerDownResponse, &QNetworkReply::finished, this, &Download::handleInstallerDownloadFinished);
connect(installerDownResponse, &QNetworkReply::readyRead, this, &Download::handleReadyRead);
connect(installerDownResponse, &QNetworkReply::finished, &loop, &QEventLoop::quit);
download_tracking.insert(installerDownResponse, tempFile);
loop.exec();
return installerDownResponse;
}
void Download::handleSslErrors(QNetworkReply *reply, const QList<QSslError> &errors)
{
QUrl url = reply->request().url();
for (const auto &e : errors)
qWarning() << "ERROR: Received ssl error:" << e.errorString() << "for" << url;
}
void Download::handleErrorOccurred(QNetworkReply::NetworkError code)
{
QNetworkReply *installerResponse = qobject_cast<QNetworkReply *>(sender());
if (!installerResponse)
return;
if (code == QNetworkReply::OperationCanceledError)
return;
QString installerfile = installerResponse->request().attribute(QNetworkRequest::User).toString();
const QString error = u"ERROR: Network error occurred attempting to download %1 code: %2 errorString %3"_s.arg(installerfile).arg(code).arg(installerResponse->errorString());
qWarning() << error;
disconnect(installerResponse, &QNetworkReply::finished, this, &Download::handleInstallerDownloadFinished);
installerResponse->abort();
installerResponse->deleteLater();
QFile * tempfile = download_tracking.value(installerResponse);
tempfile->deleteLater();
download_tracking.remove(installerResponse);
}
void Download::handleReadyRead()
{
QNetworkReply *installerResponse = qobject_cast<QNetworkReply *>(sender());
if (!installerResponse)
return;
QFile * tempfile = download_tracking.value(installerResponse);
while(!installerResponse->atEnd()) {
tempfile->write(installerResponse->read(8192));
}
tempfile->flush();
}
void Download::handleInstallerDownloadFinished()
{
QNetworkReply *installerResponse = qobject_cast<QNetworkReply *>(sender());
if (!installerResponse)
return;
QString installerName = installerResponse->request().attribute(QNetworkRequest::User).toString();
QFile *tempfile = download_tracking.value(installerResponse);
download_tracking.remove(installerResponse);
if (installerResponse->error()) {
const QString errorString
= u"ERROR: Downloading failed with code %1 \"%2\""_s.arg(installerResponse->error()).arg(installerResponse->errorString());
qWarning() << errorString;
installerResponse->deleteLater();
tempfile->deleteLater();
return;
}
tempfile->close();
QString expected = this->manifest->getExpectedHash();
this->installInstaller(expected, tempfile, installerResponse);
}
void HashFile::hashAndInstall(const QString &expected, QCryptographicHash::Algorithm algo, QFile * temp, const QString &file, QNetworkReply *response)
{
Q_ASSERT(!temp->isOpen());
QString installerFile = response->request().attribute(QNetworkRequest::User).toString();
if (!temp->open(QIODevice::ReadOnly)) {
const QString error = u"ERROR: Could not open temp file for hashing: %1 %2"_s.arg(temp->fileName(), installerFile);
qWarning() << error;
QCoreApplication::exit(1);
}
QCryptographicHash hash(algo);
while(!temp->atEnd())
hash.addData(temp->read(8192));
if (hash.result().toHex() != expected.toLatin1()) {
temp->close();
const QString error = u"ERROR: Download error hash did not match: %1 != %2 for %3"_s.arg(hash.result().toHex(), expected.toLatin1(), installerFile);
qWarning() << error;
temp->remove();
QCoreApplication::exit(1);
}
temp->close();
if (temp->rename(file)) {
return;
}
if (!temp->open(QIODevice::ReadOnly)) {
const QString error = u"ERROR: Could not open temp file at finish: %1 %2"_s.arg(temp->fileName(), file);
qWarning() << error;
QCoreApplication::exit(1);
}
QFile savefile(file);
if (savefile.open(QIODevice::WriteOnly)) {
QByteArray buffer;
while (!temp->atEnd()) {
buffer = temp->read(8192);
savefile.write(buffer);
}
savefile.close();
temp->close();
} else {
QFile::FileError error = savefile.error();
const QString errorString = u"ERROR: Could not save installer to: %1 failed with code %1"_s.arg(file).arg(error);
qWarning() << errorString;
temp->close();
QCoreApplication::exit(1);
}
}

View File

@ -0,0 +1,38 @@
#include "Embedded.h"
#include "utils.h"
#include <QByteArray>
#include <QDir>
using namespace gpt4all::embedded;
using namespace Qt::Literals::StringLiterals;
EmbeddedInstaller::EmbeddedInstaller()
: start(&data_start_gpt4all_installer),
end(&data_stop_gpt4all_installer),
size(gpt4all_installer_size) {}
void EmbeddedInstaller::extractAndDecode()
{
QByteArray installerDat64(this->start, this->size);
QByteArray installerDat = QByteArray::fromBase64(installerDat64);
this->data = installerDat;
}
void EmbeddedInstaller::installInstaller()
{
QString mountPoint = "/Volumes/gpt4all-installer-darwin";
QString appBundlePath = QDir::tempPath() + "gpt4all-installer-darwin.app";
QString installerStr = QDir::tempPath() + "gpt4all-installer.dmg";
QFile installerPath(installerStr);
bool success = installerPath.open(QIODevice::WriteOnly | QIODevice::Append);
qWarning() << "Opening installer file for writing:" << installerPath.fileName();
if (!success) {
const QString error
= u"ERROR: Could not open temp file: %1 %2"_s.arg(installerPath.fileName());
qWarning() << error;
}
installerPath.write(this->data, this->size);
installerPath.close();
mountAndExtract(mountPoint, installerStr, appBundlePath);
}

View File

@ -0,0 +1,83 @@
#include "Manifest.h"
using namespace gpt4all::manifest;
using namespace Qt::Literals::StringLiterals;
ManifestFile *ManifestFile::parseManifest(QIODevice *manifestInput)
{
ManifestFile * manifest = new ManifestFile();
manifest->xml.setDevice(manifestInput);
if(manifest->xml.readNextStartElement()) {
if (manifest->xml.name() == "gpt4all-updater"_L1){
if(manifest->xml.readNextStartElement()) {
manifest->parsePkgDescription();
}
}
}
return manifest;
}
void ManifestFile::parsePkgDescription()
{
Q_ASSERT(xml.isStartElement() && xml.name() == "pkg-description"_L1);
while(xml.readNextStartElement()){
if(xml.name() == "name"_L1){
this->name = xml.readElementText();
}
else if(xml.name() == "version"_L1){
qsizetype suffix;
this->release_ver = QVersionNumber::fromString(xml.readElementText(), &suffix);
}
else if(xml.name() == "notes"_L1) {
this->notes = xml.readElementText();
}
else if(xml.name() == "authors"_L1) {
this->authors = xml.readElementText().split(",");
}
else if(xml.name() == "date"_L1) {
this->release_date = QDate::fromString(xml.readElementText(), Qt::ISODate);
}
else if(xml.name() == "release-type"_L1) {
if ( xml.readElementText() == "release"_L1 ) {
this->config = releaseType::RELEASE;
}
else {
this->config = releaseType::DEBUG;
}
}
else if(xml.name() == "last-compatible-version"_L1) {
qsizetype suffix;
this->last_supported_version = QVersionNumber::fromString(xml.readElementText(), &suffix);
}
else if(xml.name() == "entity"_L1) {
this->entity = xml.readElementText();
}
else if(xml.name() == "component-list"_L1) {
this->component_list = xml.readElementText().split(",");
}
else if(xml.name() == "pkg-manifest"_L1) {
this->parsePkgManifest();
}
else {
// Should probably throw here, but I'd rather implement schema validation
xml.skipCurrentElement();
}
}
}
void ManifestFile::parsePkgManifest()
{
this->pkg = Package::parsePackage(xml);
}
QUrl & ManifestFile::getInstallerEndpoint()
{
return this->pkg.installer_endpoint;
}
QString & ManifestFile::getExpectedHash()
{
return this->pkg.checksum_sha256;
}

View File

@ -0,0 +1,9 @@
#include "Modify.h"
using namespace gpt4all::modify;
Modify::Modify(QFile &installer)
{
this->installer = &installer;
this->arguments = {"-c", "ch"};
}

View File

@ -0,0 +1,30 @@
#include "Package.h"
using namespace Qt::Literals::StringLiterals;
using namespace gpt4all::manifest;
Package Package::parsePackage(QXmlStreamReader &xml)
{
Q_ASSERT(xml.isStartElement() && xml.name() == "pkg-manifest"_L1);
Package p;
while(xml.readNextStartElement()) {
if(xml.name() == "installer-uri"_L1) {
p.installer_endpoint = QUrl(xml.readElementText());
}
else if(xml.name() == "sha256"_L1) {
p.checksum_sha256 = xml.readElementText();
}
else if(xml.name() == "args"_L1) {
p.installer_args = xml.readElementText().split(" ");
}
else if(xml.name() == "signed"_L1) {
p.is_signed = xml.readElementText() == "true";
}
else {
xml.skipCurrentElement();
}
}
return p;
}

View File

@ -0,0 +1,35 @@
#include "Resource.h"
using namespace gpt4all::resource
int WinInstallerResources::extractAndInstall(QFile *installerPath)
{
HMODULE hModule = NULL;
HRSRC resourceInfo = FindResourceA(hModule, "gpt4allInstaller", "CUSTOMDATA");
if (!resourceInfo)
{
fprintf(stderr, "Could not find resource\n");
return -1;
}
HGLOBAL resource = LoadResource(hModule, resourceInfo);
if (!resource)
{
qWarning() << "Could not load resource";
return -1;
}
DWORD resourceSize = SizeofResource(hModule, resourceInfo);
bool success = installerPath->open(QIODevice::WriteOnly | QIODevice::Append);
qWarning() << "Opening installer file for writing:" << installerPath->fileName();
if (!success) {
const QString error
= u"ERROR: Could not open temp file: %1 %2"_s.arg(installerPath->fileName());
qWarning() << error;
return nullptr;
}
const char* installerdat = (const char*)resource;
installerPath.write(installerdat);
installerPath.close();
}

View File

@ -0,0 +1,105 @@
#include "State.h"
#if defined(Q_OS_WINDOWS)
#include "Resource.h"
#elif defined(Q_OS_DARWIN)
#include "Embedded.h"
#endif
using namespace gpt4all::state;
Gpt4AllState & Gpt4AllState::getInstance()
{
static Gpt4AllState instance;
return instance;
}
#ifdef OFFLINE
void Gpt4AllState::driveOffline()
{
// if(this->checkForExistingInstall())
// this->removeCurrentInstallation();
#if defined(Q_OS_WINDOWS)
this->installer = new QString(QDir::tempPath() + "gpt4all-installer.exe");
gpt4all::resource::WinInstallerResources::extractAndInstall(installer);
#elif defined(Q_OS_DARWIN)
QString installer(QDir::tempPath() + "gpt4all-installer.dmg");
QString appBundlePath = QDir::tempPath() + "gpt4all-installer-darwin.app";
this->installer = new QFile(appBundlePath + "/Contents/MacOS/gpt4all-installer-darwin");
gpt4all::embedded::EmbeddedInstaller embed;
embed.extractAndDecode();
embed.installInstaller();
#endif
}
#endif
QVersionNumber &Gpt4AllState::getCurrentGpt4AllVersion()
{
return this->currentGpt4AllVersion;
}
QFile &Gpt4AllState::getCurrentGpt4AllConfig()
{
return this->currentGpt4AllConfig;
}
QDir &Gpt4AllState::getCurrentGpt4AllInstallRoot()
{
return this->currentGpt4AllInstallPath;
}
QFile & Gpt4AllState::getInstaller()
{
return *this->installer;
}
void Gpt4AllState::setInstaller(QFile *installer)
{
this->installer = installer;
}
void Gpt4AllState::removeCurrentInstallation() {
if (this->currentGpt4AllInstallPath.exists())
this->currentGpt4AllInstallPath.removeRecursively();
}
bool Gpt4AllState::checkForExistingInstall()
{
#if defined(Q_OS_DARWIN)
QDir potentialInstallDir = QDir(QCoreApplication::applicationDirPath())
.filePath("maintenancetool.app/Contents/MacOS/maintenancetool");
QStringList potentialInstallPaths = {"/Applications/gpt4all"};
#elif defined(Q_OS_WINDOWS)
QDir potentialInstallDir = QDir(QCoreApplication::applicationDirPath()).filePath("maintenancetool.exe");
QStringList potentialInstallPaths = {QStandardPaths::writableLocation(QStandardPaths::ApplicationsLocation)
+ "\\gpt4all", QStandardPaths::writableLocation(QStandardPaths::AppDataLocation)
+ "\\gpt4all"};
#endif
if(potentialInstallDir.exists()){
this->currentGpt4AllInstallPath = QDir(QCoreApplication::applicationDirPath());
return true;
}
foreach( const QString &str, potentialInstallPaths) {
QDir potentialPath(str);
if(potentialPath.exists())
this->currentGpt4AllInstallPath = potentialPath;
return true;
}
return false;
}
bool Gpt4AllState::getExistingInstaller()
{
if(this->currentGpt4AllInstallPath.exists()){
#if defined(Q_OS_DARWIN)
this->installer = new QFile(this->currentGpt4AllInstallPath.filePath("maintenancetool.app/Contents/MacOS/maintenancetool"));
#elif defined(Q_OS_WINDOWS)
this->installer = new QFile(this->currentGpt4AllInstallPath.filePath("maintenancetool.exe"));
#endif
return true;
}
return false;
}

View File

@ -0,0 +1,11 @@
#include "Uninstall.h"
using namespace gpt4all::uninstall;
Uninstall::Uninstall(QFile &uninstaller)
{
this->installer = &uninstaller;
this->arguments = {"-c", "pr"};
}

View File

@ -0,0 +1,11 @@
#include "Update.h"
using namespace gpt4all::updater;
Update::Update(QFile &installer)
{
this->installer = &installer;
this->arguments = {"-c", "install"};
}

View File

@ -0,0 +1,8 @@
.global _data_start_gpt4all_installer
.global _data_stop_gpt4all_installer
_data_start_gpt4all_installer:
.incbin "@GPT4ALL_INSTALLER_PATH@"
_data_stop_gpt4all_installer:
.global _gpt4all_installer_size
_gpt4all_installer_size:
.int _data_stop_gpt4all_installer - _data_start_gpt4all_installer

View File

@ -0,0 +1,20 @@
#include "CommandLine.h"
#include "CommandFactory.h"
int main(int argc, char ** argv)
{
QCoreApplication app(argc, argv);
QCoreApplication::setOrganizationName("nomic.ai");
QCoreApplication::setOrganizationDomain("gpt4all.io");
QCoreApplication::setApplicationName("gpt4all-auto-updater");
QCoreApplication::setApplicationVersion("0.0.1");
gpt4all::command::CommandLine cli;
cli.parse(app);
CommandFactory cmd;
gpt4all::command::CommandType command_type(cli.command());
std::shared_ptr<gpt4all::command::Command> installer_command = cmd.GetCommand(command_type);
if(installer_command)
installer_command->execute();
return 0;
}

View File

@ -0,0 +1 @@
gpt4allInstaller CUSTOMDATA @GPT4ALL_INSTALLER_PATH@

View File

@ -0,0 +1,46 @@
#include "utils.h"
#include <QProcess>
#include <QDebug>
constexpr uint32_t hash(const char* data) noexcept
{
uint32_t hash = 5381;
for(const char *c = data; *c; ++c)
hash = ((hash << 5) + hash) + (unsigned char) *c;
return hash;
}
void mountAndExtract(const QString &mountPoint, const QString &saveFilePath, const QString &appBundlePath)
{
// Mount the DMG
QProcess mountProcess;
mountProcess.start("hdiutil", QStringList() << "attach" << "-mountpoint" << mountPoint << saveFilePath);
mountProcess.waitForFinished();
if (mountProcess.exitCode() != 0) {
qDebug() << "Error mounting DMG:" << mountProcess.readAllStandardError();
}
// Extract files
QProcess extractProcess;
extractProcess.start("cp", QStringList() << "-R" << mountPoint + "/gpt4all-installer-darwin.app" << appBundlePath);
extractProcess.waitForFinished();
if (extractProcess.exitCode() != 0) {
qDebug() << "Error extracting files:" << extractProcess.readAllStandardError();
}
// Unmount the DMG
QProcess unmountProcess;
unmountProcess.start("hdiutil", QStringList() << "detach" << mountPoint);
unmountProcess.waitForFinished();
if (unmountProcess.exitCode() != 0) {
qDebug() << "Error unmounting DMG:" << unmountProcess.readAllStandardError();
}
qDebug() << "DMG mounted, extracted, and unmounted successfully.";
}

View File

@ -0,0 +1,18 @@
<?xml version="1.0" encoding="UTF-8"?>
<gpt4all-updater>
<pkg-description>
<name>gpt4all</name>
<version>2.0.0</version>
<notes>TESTS!</notes>
<authors>whoami</authors>
<date>10-18-2024</date>
<release-type>release</release-type>
<last-compatible-version>1.0.0</last-compatible-version>
<entity>Nomic</entity>
<pkg-manifest>
<installer-uri>REPLACE-ME</installer-uri>
<sha256>REPLACE-ME</sha256>
<signed>False</signed>
</pkg-manifest>
</pkg-description>
</gpt4all-updater>