2
0
mirror of https://github.com/kotatogram/kotatogram-desktop synced 2025-08-30 22:25:12 +00:00

Move relaunch / update logic to Core::Launcher.

Also pass "-workdir" argument through relaunch / update.

Fixes #4149.
This commit is contained in:
John Preston
2017-12-12 11:52:53 +04:00
parent 97c15865a5
commit 251176df47
21 changed files with 505 additions and 415 deletions

View File

@@ -20,9 +20,98 @@ Copyright (c) 2014-2017 John Preston, https://desktop.telegram.org
*/
#include "platform/linux/launcher_linux.h"
namespace Platform {
#include "core/crash_reports.h"
void Launcher::initHook() {
#include <sys/stat.h>
#include <sys/types.h>
#include <cstdlib>
#include <unistd.h>
#include <dirent.h>
#include <pwd.h>
namespace Platform {
namespace {
class Arguments {
public:
void push(QByteArray argument) {
argument.append(char(0));
_argumentValues.push_back(argument);
_arguments.push_back(_argumentValues.back().data());
}
char **result() {
_arguments.push_back(nullptr);
return _arguments.data();
}
private:
std::vector<QByteArray> _argumentValues;
std::vector<char*> _arguments;
};
} // namespace
bool Launcher::launchUpdater(UpdaterLaunch action) {
if (cExeName().isEmpty()) {
return false;
}
const auto binaryName = (action == UpdaterLaunch::JustRelaunch)
? cExeName()
: QStringLiteral("Updater");
auto argumentsList = Arguments();
argumentsList.push(QFile::encodeName(cExeDir() + binaryName));
if (cLaunchMode() == LaunchModeAutoStart) {
argumentsList.push("-autostart");
}
if (cDebug()) {
argumentsList.push("-debug");
}
if (cStartInTray()) {
argumentsList.push("-startintray");
}
if (cTestMode()) {
argumentsList.push("-testmode");
}
if (cDataFile() != qsl("data")) {
argumentsList.push("-key");
argumentsList.push(QFile::encodeName(cDataFile()));
}
if (action == UpdaterLaunch::JustRelaunch) {
argumentsList.push("-noupdate");
argumentsList.push("-tosettings");
if (customWorkingDir()) {
argumentsList.push("-workdir");
argumentsList.push(QFile::encodeName(cWorkingDir()));
}
} else {
argumentsList.push("-workpath");
argumentsList.push(QFile::encodeName(cWorkingDir()));
argumentsList.push("-exename");
argumentsList.push(QFile::encodeName(cExeName()));
argumentsList.push("-exepath");
argumentsList.push(QFile::encodeName(cExeDir()));
if (customWorkingDir()) {
argumentsList.push("-workdir_custom");
}
}
Logs::closeMain();
CrashReports::Finish();
const auto args = argumentsList.result();
pid_t pid = fork();
switch (pid) {
case -1: return false;
case 0: execv(args[0], args); return false;
}
return true;
}
} // namespace

View File

@@ -29,7 +29,7 @@ public:
using Core::Launcher::Launcher;
private:
void initHook() override;
bool launchUpdater(UpdaterLaunch action) override;
};

View File

@@ -526,98 +526,6 @@ void psNewVersion() {
psRegisterCustomScheme();
}
bool _execUpdater(bool update = true, const QString &crashreport = QString()) {
if (cExeName().isEmpty()) {
return false;
}
static const int MaxLen = 65536, MaxArgsCount = 128;
char path[MaxLen] = {0};
QByteArray data(QFile::encodeName(cExeDir() + (update ? "Updater" : cExeName())));
memcpy(path, data.constData(), data.size());
char *args[MaxArgsCount] = { 0 };
char p_noupdate[] = "-noupdate";
char p_autostart[] = "-autostart";
char p_debug[] = "-debug";
char p_tosettings[] = "-tosettings";
char p_key[] = "-key";
char p_datafile[MaxLen] = { 0 };
char p_path[] = "-workpath";
char p_pathbuf[MaxLen] = { 0 };
char p_startintray[] = "-startintray";
char p_testmode[] = "-testmode";
char p_crashreport[] = "-crashreport";
char p_crashreportbuf[MaxLen] = { 0 };
char p_exe[] = "-exename";
char p_exebuf[MaxLen] = { 0 };
char p_exepath[] = "-exepath";
char p_exepathbuf[MaxLen] = { 0 };
int argIndex = 0;
args[argIndex++] = path;
if (!update) {
args[argIndex++] = p_noupdate;
args[argIndex++] = p_tosettings;
}
if (cLaunchMode() == LaunchModeAutoStart) args[argIndex++] = p_autostart;
if (cDebug()) args[argIndex++] = p_debug;
if (cStartInTray()) args[argIndex++] = p_startintray;
if (cTestMode()) args[argIndex++] = p_testmode;
if (cDataFile() != qsl("data")) {
QByteArray dataf = QFile::encodeName(cDataFile());
if (dataf.size() < MaxLen) {
memcpy(p_datafile, dataf.constData(), dataf.size());
args[argIndex++] = p_key;
args[argIndex++] = p_datafile;
}
}
QByteArray pathf = QFile::encodeName(cWorkingDir());
if (pathf.size() < MaxLen) {
memcpy(p_pathbuf, pathf.constData(), pathf.size());
args[argIndex++] = p_path;
args[argIndex++] = p_pathbuf;
}
if (!crashreport.isEmpty()) {
QByteArray crashreportf = QFile::encodeName(crashreport);
if (crashreportf.size() < MaxLen) {
memcpy(p_crashreportbuf, crashreportf.constData(), crashreportf.size());
args[argIndex++] = p_crashreport;
args[argIndex++] = p_crashreportbuf;
}
}
QByteArray exef = QFile::encodeName(cExeName());
if (exef.size() > 0 && exef.size() < MaxLen) {
memcpy(p_exebuf, exef.constData(), exef.size());
args[argIndex++] = p_exe;
args[argIndex++] = p_exebuf;
}
QByteArray exepathf = QFile::encodeName(cExeDir());
if (exepathf.size() > 0 && exepathf.size() < MaxLen) {
memcpy(p_exepathbuf, exepathf.constData(), exepathf.size());
args[argIndex++] = p_exepath;
args[argIndex++] = p_exepathbuf;
}
Logs::closeMain();
CrashReports::Finish();
pid_t pid = fork();
switch (pid) {
case -1: return false;
case 0: execv(path, args); return false;
}
return true;
}
void psExecUpdater() {
if (!_execUpdater()) {
psDeleteDir(cWorkingDir() + qsl("tupdates/temp"));
}
}
void psExecTelegram(const QString &crashreport) {
_execUpdater(false, crashreport);
}
bool psShowOpenWithMenu(int x, int y, const QString &file) {
return false;
}

View File

@@ -77,9 +77,6 @@ void psBringToBack(QWidget *w);
int psCleanup();
int psFixPrevious();
void psExecUpdater();
void psExecTelegram(const QString &arg = QString());
QAbstractNativeEventFilter *psNativeEventFilter();
void psNewVersion();