From 7ed4eaac11ed4e9d4be6b7d61bc3d467aedb776c Mon Sep 17 00:00:00 2001 From: Caelan Sayler Date: Mon, 26 Feb 2024 21:27:51 +0000 Subject: [PATCH] Pass wait pid instead of wait parent to update.exe --- for-cpp/Velopack.cpp | 28 +++++++++++++++++++++++++--- for-cpp/Velopack.hpp | 13 +------------ for-cs/Velopack.cs | 18 +++++++++++++----- for-js/Velopack.js | 23 ++++++++++------------- for-js/Velopack.ts | 25 ++++++++++++------------- src/Platform.fu | 11 +++++++---- src/UpdateManager.fu | 3 ++- src/include/ts_begin.ts | 4 ++++ src/include/velopack.cpp | 18 ++++++++++++++++-- src/include/velopack.cs | 5 +++++ 10 files changed, 95 insertions(+), 53 deletions(-) diff --git a/for-cpp/Velopack.cpp b/for-cpp/Velopack.cpp index 98ce036..bfff845 100644 --- a/for-cpp/Velopack.cpp +++ b/for-cpp/Velopack.cpp @@ -1247,7 +1247,9 @@ int subprocess_alive(struct subprocess_s *const process) { #define WIN32_LEAN_AND_MEAN #define PATH_MAX MAX_PATH #include -#endif // VELO_MSVC +#elif defined(__unix__) || defined(__APPLE__) && defined(__MACH__) +#include // For getpid on UNIX-like systems +#endif static std::string nativeCurrentOsName() { @@ -1356,7 +1358,7 @@ static void nativeStartProcessFireAndForget(const std::vector *comm // } // accumulatedData.erase(0, pos + 1); // } -// } +// } // }); // return outputThread; @@ -1382,6 +1384,18 @@ static std::string nativeStartProcessBlocking(const std::vector *co return buffer.str(); } +static int32_t nativeCurrentProcessId() +{ +#if defined(_WIN32) + return GetCurrentProcessId(); +#elif defined(__unix__) || defined(__APPLE__) && defined(__MACH__) + return getpid(); +#else +#error "Unsupported platform" + return -1; // Indicate error or unsupported platform +#endif +} + namespace Velopack { #if UNICODE @@ -1435,6 +1449,7 @@ namespace Velopack #include #include +#include #include #include #include "Velopack.hpp" @@ -1918,6 +1933,12 @@ void Platform::startProcessFireAndForget(const std::vector * comman } nativeStartProcessFireAndForget(command_line); } +int Platform::getCurrentProcessId() +{ + int ret = 0; + ret = nativeCurrentProcessId(); return ret; +} + std::string Platform::getCurrentProcessPath() { std::string ret{""}; @@ -2243,7 +2264,8 @@ void UpdateManagerSync::waitExitThenApplyUpdates(std::string assetPath, bool sil command.push_back("--silent"); } command.push_back("apply"); - command.push_back("--wait"); + command.push_back("--waitPid"); + command.push_back(std::format("{}", Platform::getCurrentProcessId())); if (!assetPath.empty()) { command.push_back("--package"); command.push_back(assetPath); diff --git a/for-cpp/Velopack.hpp b/for-cpp/Velopack.hpp index 651036e..6e753e7 100644 --- a/for-cpp/Velopack.hpp +++ b/for-cpp/Velopack.hpp @@ -201,17 +201,9 @@ class JsonParser class Platform { public: - /** - * Starts a new process and sychronously reads/returns its output. - */ static std::string startProcessBlocking(const std::vector * command_line); - /** - * Starts a new process and returns immediately. - */ static void startProcessFireAndForget(const std::vector * command_line); - /** - * Returns the path of the current process. - */ + static int getCurrentProcessId(); static std::string getCurrentProcessPath(); static bool fileExists(std::string path); static bool isInstalled(); @@ -224,9 +216,6 @@ class Platform static bool isWindows(); static bool isLinux(); static bool isOsx(); - /** - * Returns the name of the operating system. - */ static std::string getOsName(); static void exit(int code); private: diff --git a/for-cs/Velopack.cs b/for-cs/Velopack.cs index d182932..bb21110 100644 --- a/for-cs/Velopack.cs +++ b/for-cs/Velopack.cs @@ -570,7 +570,6 @@ public JsonNode ParseValue() static class Platform { - /// Starts a new process and sychronously reads/returns its output. public static string StartProcessBlocking(List command_line) { if (command_line.Count == 0) @@ -581,7 +580,6 @@ public static string StartProcessBlocking(List command_line) ret = NativeMethods.NativeStartProcessBlocking(command_line); return StrTrim(ret); } - /// Starts a new process and returns immediately. public static void StartProcessFireAndForget(List command_line) { if (command_line.Count == 0) @@ -591,7 +589,12 @@ public static void StartProcessFireAndForget(List command_line) NativeMethods.NativeStartProcessFireAndForget(command_line); } - /// Returns the path of the current process. + public static int GetCurrentProcessId() + { + int ret = 0; + ret = NativeMethods.NativeCurrentProcessId(); return ret; + } + public static string GetCurrentProcessPath() { string ret = ""; @@ -731,7 +734,6 @@ public static bool IsOsx() return GetOsName() == "darwin"; } - /// Returns the name of the operating system. public static string GetOsName() { string ret = ""; @@ -1067,7 +1069,8 @@ public void WaitExitThenApplyUpdates(string assetPath, bool silent, bool restart command.Add("--silent"); } command.Add("apply"); - command.Add("--wait"); + command.Add("--waitPid"); + command.Add($"{Platform.GetCurrentProcessId()}"); if (assetPath.Length > 0) { command.Add("--package"); @@ -1186,6 +1189,11 @@ public async Task DownloadUpdatesAsync(UpdateInfo updateInfo, Action progre static class NativeMethods { + public static int NativeCurrentProcessId() + { + return Process.GetCurrentProcess().Id; + } + public static void NativeExitProcess(int code) { Environment.Exit(code); diff --git a/for-js/Velopack.js b/for-js/Velopack.js index 4b2dddc..01031bc 100644 --- a/for-js/Velopack.js +++ b/for-js/Velopack.js @@ -65,6 +65,9 @@ function emitLines(stream) { function nativeDoesFileExist(path) { return fs.existsSync(path); } +function nativeCurrentProcessId() { + return process.pid; +} function nativeGetCurrentProcessPath() { return process.execPath; } @@ -578,9 +581,6 @@ _JsonParser_text = new WeakMap(), _JsonParser_position = new WeakMap(), _JsonPar }; class Platform { constructor() { } - /** - * Starts a new process and sychronously reads/returns its output. - */ static startProcessBlocking(command_line) { if (command_line.length == 0) { throw new Error("Command line is empty"); @@ -589,18 +589,17 @@ class Platform { ret = nativeStartProcessBlocking(command_line); return _a.strTrim(ret); } - /** - * Starts a new process and returns immediately. - */ static startProcessFireAndForget(command_line) { if (command_line.length == 0) { throw new Error("Command line is empty"); } nativeStartProcessFireAndForget(command_line); } - /** - * Returns the path of the current process. - */ + static getCurrentProcessId() { + let ret = 0; + ret = nativeCurrentProcessId(); + return ret; + } static getCurrentProcessPath() { let ret = ""; ret = nativeGetCurrentProcessPath(); @@ -668,9 +667,6 @@ class Platform { static isOsx() { return _a.getOsName() == "darwin"; } - /** - * Returns the name of the operating system. - */ static getOsName() { let ret = ""; ret = nativeCurrentOsName(); @@ -1013,7 +1009,8 @@ export class UpdateManagerSync { command.push("--silent"); } command.push("apply"); - command.push("--wait"); + command.push("--waitPid"); + command.push(`${Platform.getCurrentProcessId()}`); if (assetPath.length > 0) { command.push("--package"); command.push(assetPath); diff --git a/for-js/Velopack.ts b/for-js/Velopack.ts index 929ba6b..1101c4b 100644 --- a/for-js/Velopack.ts +++ b/for-js/Velopack.ts @@ -57,6 +57,10 @@ function nativeDoesFileExist(path: string): boolean { return fs.existsSync(path); } +function nativeCurrentProcessId(): number { + return process.pid; +} + function nativeGetCurrentProcessPath(): string { return process.execPath; } @@ -633,9 +637,6 @@ class JsonParser { class Platform { private constructor() {} - /** - * Starts a new process and sychronously reads/returns its output. - */ public static startProcessBlocking(command_line: readonly string[]): string { if (command_line.length == 0) { throw new Error("Command line is empty"); @@ -645,9 +646,6 @@ class Platform { return Platform.strTrim(ret); } - /** - * Starts a new process and returns immediately. - */ public static startProcessFireAndForget( command_line: readonly string[], ): void { @@ -657,9 +655,12 @@ class Platform { nativeStartProcessFireAndForget(command_line); } - /** - * Returns the path of the current process. - */ + public static getCurrentProcessId(): number { + let ret: number = 0; + ret = nativeCurrentProcessId(); + return ret; + } + public static getCurrentProcessPath(): string { let ret: string = ""; ret = nativeGetCurrentProcessPath(); @@ -771,9 +772,6 @@ class Platform { return Platform.getOsName() == "darwin"; } - /** - * Returns the name of the operating system. - */ public static getOsName(): string { let ret: string = ""; ret = nativeCurrentOsName(); @@ -1119,7 +1117,8 @@ export class UpdateManagerSync { command.push("--silent"); } command.push("apply"); - command.push("--wait"); + command.push("--waitPid"); + command.push(`${Platform.getCurrentProcessId()}`); if (assetPath.length > 0) { command.push("--package"); command.push(assetPath); diff --git a/src/Platform.fu b/src/Platform.fu index 36af4b3..2008449 100644 --- a/src/Platform.fu +++ b/src/Platform.fu @@ -1,7 +1,6 @@ static class Platform { - /// Starts a new process and sychronously reads/returns its output. public static string() StartProcessBlocking(List command_line) throws Exception { if (command_line.Count == 0) { @@ -13,7 +12,6 @@ static class Platform return StrTrim(ret); } - /// Starts a new process and returns immediately. public static void StartProcessFireAndForget(List command_line) throws Exception { if (command_line.Count == 0) { @@ -22,7 +20,13 @@ static class Platform native { VMACRO_NativeStartProcessFireAndForget(command_line); } } - /// Returns the path of the current process. + public static int GetCurrentProcessId() + { + int ret = 0; + native { ret = VMACRO_NativeCurrentProcessId(); } + return ret; + } + public static string() GetCurrentProcessPath() { string() ret = ""; @@ -131,7 +135,6 @@ static class Platform public static bool IsLinux() { return GetOsName() == "linux"; } public static bool IsOsx() { return GetOsName() == "darwin"; } - /// Returns the name of the operating system. public static string() GetOsName() { string() ret = ""; diff --git a/src/UpdateManager.fu b/src/UpdateManager.fu index 9aebd81..00b9f6f 100644 --- a/src/UpdateManager.fu +++ b/src/UpdateManager.fu @@ -153,7 +153,8 @@ public class UpdateManagerSync command.Add("--silent"); } command.Add("apply"); - command.Add("--wait"); + command.Add("--waitPid"); + command.Add($"{Platform.GetCurrentProcessId()}"); if (assetPath.Length > 0) { command.Add("--package"); diff --git a/src/include/ts_begin.ts b/src/include/ts_begin.ts index c4a58b6..4025059 100644 --- a/src/include/ts_begin.ts +++ b/src/include/ts_begin.ts @@ -24,6 +24,10 @@ function nativeDoesFileExist(path: string): boolean { return fs.existsSync(path); } +function nativeCurrentProcessId(): number { + return process.pid; +} + function nativeGetCurrentProcessPath(): string { return process.execPath; } diff --git a/src/include/velopack.cpp b/src/include/velopack.cpp index f01fb63..6ae9fbd 100644 --- a/src/include/velopack.cpp +++ b/src/include/velopack.cpp @@ -14,7 +14,9 @@ #define WIN32_LEAN_AND_MEAN #define PATH_MAX MAX_PATH #include -#endif // VELO_MSVC +#elif defined(__unix__) || defined(__APPLE__) && defined(__MACH__) +#include // For getpid on UNIX-like systems +#endif static std::string nativeCurrentOsName() { @@ -123,7 +125,7 @@ static void nativeStartProcessFireAndForget(const std::vector *comm // } // accumulatedData.erase(0, pos + 1); // } -// } +// } // }); // return outputThread; @@ -149,6 +151,18 @@ static std::string nativeStartProcessBlocking(const std::vector *co return buffer.str(); } +static int32_t nativeCurrentProcessId() +{ +#if defined(_WIN32) + return GetCurrentProcessId(); +#elif defined(__unix__) || defined(__APPLE__) && defined(__MACH__) + return getpid(); +#else +#error "Unsupported platform" + return -1; // Indicate error or unsupported platform +#endif +} + namespace Velopack { #if UNICODE diff --git a/src/include/velopack.cs b/src/include/velopack.cs index 16b8adf..7c0a4f5 100644 --- a/src/include/velopack.cs +++ b/src/include/velopack.cs @@ -57,6 +57,11 @@ public async Task DownloadUpdatesAsync(UpdateInfo updateInfo, Action progre static class NativeMethods { + public static int NativeCurrentProcessId() + { + return Process.GetCurrentProcess().Id; + } + public static void NativeExitProcess(int code) { Environment.Exit(code);