[win-linux] updmanager: add service update

This commit is contained in:
SimplestStudio
2024-02-27 07:58:39 +02:00
parent 4ab10bb50c
commit 9000de3f8a
5 changed files with 189 additions and 18 deletions

View File

@ -70,7 +70,8 @@ enum MsgCommands {
MSG_OtherError,
MSG_RequestContentLenght,
MSG_UnzipProgress,
MSG_SetLanguage
MSG_SetLanguage,
MSG_StartReplacingService
};
class CSocket

View File

@ -68,6 +68,7 @@
# define APP_LAUNCH_NAME "/DesktopEditors"
# define APP_HELPER "/editors_helper"
# define DAEMON_NAME "/updatesvc"
# define DAEMON_NAME_OLD "/~updatesvc"
# define SUBFOLDER "/desktopeditors"
# define ARCHIVE_EXT _T(".tar.xz")
# define ARCHIVE_PATTERN _T("*.tar.xz")
@ -274,6 +275,12 @@ void CSvcManager::init()
__UNLOCK
break;
case MSG_StartReplacingService:
__GLOBAL_LOCK
startReplacingService(params[2] == _T("true"));
__UNLOCK
break;
case MSG_ClearTempFiles:
clearTempFiles(params[1], params[2]);
break;
@ -629,3 +636,73 @@ void CSvcManager::startReplacingFiles(const tstring &packageType, const bool res
restartService();
#endif
}
void CSvcManager::startReplacingService(const bool restartAfterUpdate)
{
tstring appPath = NS_File::appPath();
tstring updPath = NS_File::parentPath(appPath) + UPDATE_PATH;
tstring updSubPath = NS_File::fileExists(updPath + SUBFOLDER + APP_LAUNCH_NAME) ? updPath + SUBFOLDER : updPath;
if (!NS_File::dirExists(updPath)) {
NS_Logger::WriteLog(_TR("Update cancelled. Can't find folder:") + _T(" ") + updPath, true);
return;
}
#ifdef _WIN32
# ifndef DONT_VERIFY_SIGNATURE
// Verify the signature of executable files
if (!NS_File::verifyEmbeddedSignature(updSubPath + DAEMON_NAME)) {
NS_Logger::WriteLog(_TR("Update cancelled. The file signature is missing:") + _T(" ") + updSubPath + DAEMON_NAME, true);
return;
}
# endif
#endif
// Wait until the main app closes
{
#ifdef _WIN32
tstring apps[] = {APP_LAUNCH_NAME2, APP_HELPER};
#else
tstring apps[] = {APP_LAUNCH_NAME, APP_HELPER};
#endif
for (int i = 0; i < sizeof(apps) / sizeof(apps[0]); i++) {
int retries = 10;
tstring app(apps[i]);
app = app.substr(1);
while (NS_File::isProcessRunning(app) && retries-- > 0)
sleep(500);
if (NS_File::isProcessRunning(app)) {
NS_Logger::WriteLog(_TR("Update cancelled. The program is not closed:") + _T(" ") + app, true);
return;
}
}
}
// Rename updatesvc.exe to ~updatesvc.exe
if (NS_File::fileExists(appPath + DAEMON_NAME) && !NS_File::replaceFile(appPath + DAEMON_NAME, appPath + DAEMON_NAME_OLD)) {
NS_Logger::WriteLog(_TR("Update cancelled. Can't rename updatesvc.exe to ~updatesvc.exe:") + _T(" ") + NS_Utils::GetLastErrorAsString(), true);
return;
}
// Move updatesvc.exe to app path
if (!NS_File::replaceFile(updSubPath + DAEMON_NAME, appPath + DAEMON_NAME)) {
NS_Logger::WriteLog(_TR("Update cancelled. Can't replace file updatesvc.exe to app path:") + _T(" ") + NS_Utils::GetLastErrorAsString(), true);
if (NS_File::fileExists(appPath + DAEMON_NAME_OLD) && !NS_File::replaceFile(appPath + DAEMON_NAME_OLD, appPath + DAEMON_NAME))
NS_Logger::WriteLog(_TR("Can't restore file updatesvc.exe!"), true);
return;
}
// Restart program
if (restartAfterUpdate) {
if (!NS_File::runProcess(appPath + APP_LAUNCH_NAME, _T("")))
NS_Logger::WriteLog(_TR("An error occurred while restarting the program!"), true);
}
// Remove Update dir
NS_File::removeDirRecursively(updPath);
// Restart service
#ifdef _WIN32
restartService();
#endif
}

View File

@ -65,6 +65,7 @@ private:
void unzipIfNeeded(const tstring &filePath, const tstring &newVersion);
void clearTempFiles(const tstring &prefix, const tstring &except = tstring());
void startReplacingFiles(const tstring &packageType, const bool restartAfterUpdate);
void startReplacingService(const bool restartAfterUpdate);
FnVoidVoid m_quit_callback = nullptr;
tstring m_newVersion;

View File

@ -35,12 +35,16 @@
#include "classes/platform_linux/ctimer.h"
#include "classes/csvcmanager.h"
#include "classes/translator.h"
#include "version.h"
#include "../../src/defines.h"
#include "../../src/prop/defines_p.h"
#include <csignal>
#include <cstring>
#include <locale>
#define DECL_VERSION __attribute__((section(".version_info"), unused))
volatile static const char DECL_VERSION version[] = VER_STRING;
void strToNum(const char *str, int &num)
{