diff --git a/src/main/services/updater.ts b/src/main/services/updater.ts index 011d080..16f061a 100644 --- a/src/main/services/updater.ts +++ b/src/main/services/updater.ts @@ -1,7 +1,8 @@ import { app, BrowserWindow } from 'electron' -import { createWriteStream, chmodSync, copyFileSync, mkdirSync } from 'fs' +import { createWriteStream, chmodSync, mkdirSync, writeFileSync, realpathSync } from 'fs' import { join } from 'path' -import { homedir } from 'os' +import { homedir, tmpdir } from 'os' +import { spawn } from 'child_process' import https from 'https' import http from 'http' import { URL } from 'url' @@ -88,6 +89,26 @@ function downloadFile( }) } +// Запускает детачнутый sh-скрипт, который ждёт смерти текущего процесса, +// затем заменяет AppImage по оригинальному пути и запускает его. +function spawnUpdateHelper(newPath: string, oldPath: string): void { + const scriptPath = join(tmpdir(), 'aero-mail-update.sh') + const script = [ + '#!/bin/sh', + 'PARENT="$1" NEW="$2" OLD="$3"', + 'while kill -0 "$PARENT" 2>/dev/null; do sleep 0.1; done', + 'cp "$NEW" "$OLD" && chmod +x "$OLD"', + 'exec "$OLD"', + ].join('\n') + + writeFileSync(scriptPath, script, { mode: 0o755 }) + + spawn('/bin/sh', [scriptPath, String(process.pid), newPath, oldPath], { + detached: true, + stdio: 'ignore', + }).unref() +} + export class UpdaterService { private downloadedPath: string | null = null @@ -147,16 +168,17 @@ export class UpdaterService { installUpdate(): void { if (!this.downloadedPath) return - const appImagePath = process.env['APPIMAGE'] + // Разрешаем симлинки — APPIMAGE может указывать на symlink + let appImagePath = process.env['APPIMAGE'] ?? '' if (appImagePath) { - try { - copyFileSync(this.downloadedPath, appImagePath) - chmodSync(appImagePath, 0o755) - app.relaunch() - } catch { - app.relaunch({ execPath: this.downloadedPath }) - } + try { appImagePath = realpathSync(appImagePath) } catch (_) {} + } + + if (appImagePath) { + // Запускаем детачнутый хелпер: он заменит AppImage после нашей смерти + spawnUpdateHelper(this.downloadedPath, appImagePath) } else { + // APPIMAGE не установлен — просто перезапустить из нового файла app.relaunch({ execPath: this.downloadedPath }) }