The only downloader you need. 下载器的集大成者。
TypeScript asyncasynciobittorentbtcross-platformdownload-managerdownloaderfluent-designftphttp3magnetpyqtpyside6pythonqtquicreactsoftwarestreaming
refactor(updater): Windows 全面改用 Unicode API
XiaoYouChR9e065d911349
diff --git a/tools/updater/updater.c b/tools/updater/updater.cindex 834c832e..96f69f0c 100644--- a/tools/updater/updater.c+++ b/tools/updater/updater.c@@ -13,6 +13,10 @@#define WIN32_LEAN_AND_MEAN#include <windows.h>#include <shellapi.h>+ #include <wchar.h>+ typedef wchar_t pchar;+ #define PFMT "%ls"+ #define wsnprintf(buf, n, ...) do { _snwprintf((buf), (n), __VA_ARGS__); (buf)[(n) - 1] = L'\0'; } while (0)#else#include <errno.h>#include <ftw.h>@@ -24,6 +28,8 @@#ifdef __APPLE__#include <mach-o/dyld.h>#endif+ typedef char pchar;+ #define PFMT "%s"#endif#define PATH_BUF 4096@@ -38,35 +44,48 @@ enum {};static FILE *g_log;-static char g_logPath[PATH_BUF];--static void toDirname(char *path) {- char *sep = strrchr(path, '/');#ifdef _WIN32- char *bsep = strrchr(path, '\\');- if (bsep > sep) sep = bsep;+static wchar_t g_logPath[PATH_BUF];+#else+static char g_logPath[PATH_BUF];#endif++static void toDirname(pchar *path) {+#ifdef _WIN32+ wchar_t *sep = wcsrchr(path, L'/');+ wchar_t *bsep = wcsrchr(path, L'\\');+ if (bsep && (!sep || bsep > sep)) sep = bsep;+ if (sep) *sep = L'\0';+ else path[0] = L'\0';+#else+ char *sep = strrchr(path, '/');if (sep) *sep = '\0';else path[0] = '\0';+#endif}-static void openLog(const char *appDir) {- strncpy(g_logPath, appDir, sizeof(g_logPath) - 1);- g_logPath[sizeof(g_logPath) - 1] = '\0';+static void openLog(const pchar *appDir) {+#ifdef _WIN32+ wcsncpy(g_logPath, appDir, PATH_BUF - 1);+ g_logPath[PATH_BUF - 1] = L'\0';toDirname(g_logPath);-- size_t dirLen = strlen(g_logPath);+ size_t dirLen = wcslen(g_logPath);if (dirLen > 0)- snprintf(g_logPath + dirLen, sizeof(g_logPath) - dirLen, "%cupdater.log",-#ifdef _WIN32- '\\');+ wsnprintf(g_logPath + dirLen, PATH_BUF - dirLen, L"\\updater.log");+ else+ wsnprintf(g_logPath, PATH_BUF, L"updater.log");+ g_log = _wfopen(g_logPath, L"a");#else- '/');-#endif+ strncpy(g_logPath, appDir, PATH_BUF - 1);+ g_logPath[PATH_BUF - 1] = '\0';+ toDirname(g_logPath);+ size_t dirLen = strlen(g_logPath);+ if (dirLen > 0)+ snprintf(g_logPath + dirLen, PATH_BUF - dirLen, "/updater.log");else- snprintf(g_logPath, sizeof(g_logPath), "updater.log");-+ snprintf(g_logPath, PATH_BUF, "updater.log");g_log = fopen(g_logPath, "a");+#endif}static void logMsg(const char *fmt, ...) {@@ -74,9 +93,10 @@ static void logMsg(const char *fmt, ...) {time_t now = time(NULL);struct tm *t = localtime(&now);- fprintf(g_log, "%04d-%02d-%02d %02d:%02d:%02d ",- t->tm_year + 1900, t->tm_mon + 1, t->tm_mday,- t->tm_hour, t->tm_min, t->tm_sec);+ if (t)+ fprintf(g_log, "%04d-%02d-%02d %02d:%02d:%02d ",+ t->tm_year + 1900, t->tm_mon + 1, t->tm_mday,+ t->tm_hour, t->tm_min, t->tm_sec);va_list ap;va_start(ap, fmt);@@ -91,9 +111,13 @@ static void closeLog(void) {if (g_log) fclose(g_log);}-static void findExeDir(char *buf, size_t len) {+static void findExeDir(pchar *buf, size_t len) {#ifdef _WIN32- GetModuleFileNameA(NULL, buf, (DWORD)len);+ if (!GetModuleFileNameW(NULL, buf, (DWORD)len)) {+ buf[0] = L'\0';+ return;+ }+ buf[len - 1] = L'\0';#elif defined(__APPLE__)uint32_t size = (uint32_t)len;if (_NSGetExecutablePath(buf, &size) != 0) {@@ -122,37 +146,35 @@ static int waitForProcessExit(unsigned long pid, int timeoutMs) {}#elsestatic int waitForProcessExit(unsigned long pid, int timeoutMs) {+ if (pid == 0) return 0;for (int elapsed = 0; elapsed < timeoutMs; elapsed += 100) {- if (kill((pid_t)pid, 0) != 0) return 0;+ if (kill((pid_t)pid, 0) != 0 && errno == ESRCH) return 0;usleep(100 * 1000);}return -1;}#endif-static int runHpatchz(const char *dir, const char *oldDir,- const char *patchFile, const char *newDir) {- char hpatchz[PATH_BUF];+static int runHpatchz(const pchar *dir, const pchar *oldDir,+ const pchar *patchFile, const pchar *newDir) {#ifdef _WIN32- snprintf(hpatchz, sizeof(hpatchz), "%s\\hpatchz.exe", dir);-#else- snprintf(hpatchz, sizeof(hpatchz), "%s/hpatchz", dir);-#endif+ wchar_t hpatchz[PATH_BUF];+ wsnprintf(hpatchz, PATH_BUF, L"%ls\\hpatchz.exe", dir);- logMsg("running: %s -f \"%s\" \"%s\" \"%s\"", hpatchz, oldDir, patchFile, newDir);+ logMsg("running: " PFMT " -f \"" PFMT "\" \"" PFMT "\" \"" PFMT "\"",+ hpatchz, oldDir, patchFile, newDir);-#ifdef _WIN32- char cmd[PATH_BUF * 2];- snprintf(cmd, sizeof(cmd), "\"%s\" -f \"%s\" \"%s\" \"%s\"",- hpatchz, oldDir, patchFile, newDir);+ wchar_t cmd[PATH_BUF * 2];+ wsnprintf(cmd, PATH_BUF * 2, L"\"%ls\" -f \"%ls\" \"%ls\" \"%ls\"",+ hpatchz, oldDir, patchFile, newDir);SECURITY_ATTRIBUTES sa = { .nLength = sizeof(sa), .bInheritHandle = TRUE };HANDLE hLog = g_log- ? CreateFileA(g_logPath, FILE_APPEND_DATA, FILE_SHARE_READ | FILE_SHARE_WRITE,+ ? CreateFileW(g_logPath, FILE_APPEND_DATA, FILE_SHARE_READ | FILE_SHARE_WRITE,&sa, OPEN_ALWAYS, FILE_ATTRIBUTE_NORMAL, NULL): INVALID_HANDLE_VALUE;- STARTUPINFOA si = { .cb = sizeof(si) };+ STARTUPINFOW si = { .cb = sizeof(si) };if (hLog != INVALID_HANDLE_VALUE) {si.dwFlags = STARTF_USESTDHANDLES;si.hStdOutput = hLog;@@ -160,7 +182,7 @@ static int runHpatchz(const char *dir, const char *oldDir,}PROCESS_INFORMATION pi = {0};- if (!CreateProcessA(NULL, cmd, NULL, NULL, TRUE, 0, NULL, NULL, &si, &pi)) {+ if (!CreateProcessW(NULL, cmd, NULL, NULL, TRUE, 0, NULL, NULL, &si, &pi)) {if (hLog != INVALID_HANDLE_VALUE) CloseHandle(hLog);return -1;}@@ -174,6 +196,12 @@ static int runHpatchz(const char *dir, const char *oldDir,return code == 0 ? 0 : (int)code;#else+ char hpatchz[PATH_BUF];+ snprintf(hpatchz, sizeof(hpatchz), "%s/hpatchz", dir);++ logMsg("running: " PFMT " -f \"" PFMT "\" \"" PFMT "\" \"" PFMT "\"",+ hpatchz, oldDir, patchFile, newDir);+posix_spawn_file_actions_t actions;posix_spawn_file_actions_init(&actions);if (g_log) {@@ -189,40 +217,40 @@ static int runHpatchz(const char *dir, const char *oldDir,posix_spawn_file_actions_destroy(&actions);if (err != 0) return -1;- int status;- waitpid(child, &status, 0);+ int status = 0;+ if (waitpid(child, &status, 0) == -1) return -1;return WIFEXITED(status) ? WEXITSTATUS(status) : -1;#endif}-static int moveDir(const char *from, const char *to) {+static int moveDir(const pchar *from, const pchar *to) {#ifdef _WIN32for (int i = 0; i < 5; i++) {- if (MoveFileA(from, to)) return 0;- logMsg("rename \"%s\" -> \"%s\" failed (err=%lu), retry %d",+ if (MoveFileW(from, to)) return 0;+ logMsg("rename \"" PFMT "\" -> \"" PFMT "\" failed (err=%lu), retry %d",from, to, GetLastError(), i + 1);Sleep(100u << i);}return -1;#elseif (rename(from, to) == 0) return 0;- logMsg("rename \"%s\" -> \"%s\" failed: %s", from, to, strerror(errno));+ logMsg("rename \"" PFMT "\" -> \"" PFMT "\" failed: %s", from, to, strerror(errno));return -1;#endif}#ifdef _WIN32-static void deleteDir(const char *dir) {- char buf[MAX_PATH + 2];+static void deleteDir(const wchar_t *dir) {+ wchar_t buf[PATH_BUF + 2];memset(buf, 0, sizeof(buf));- strncpy(buf, dir, MAX_PATH);+ wcsncpy(buf, dir, PATH_BUF);- SHFILEOPSTRUCTA op = {0};+ SHFILEOPSTRUCTW op = {0};op.wFunc = FO_DELETE;op.pFrom = buf;op.fFlags = FOF_NOCONFIRMATION | FOF_NOERRORUI | FOF_SILENT;- SHFileOperationA(&op);+ SHFileOperationW(&op);}#elsestatic int deletePath(const char *path, const struct stat *sb, int t, struct FTW *f) {@@ -235,14 +263,14 @@ static void deleteDir(const char *dir) {}#endif-static int startApp(const char *exe) {+static int startApp(const pchar *exe) {#ifdef _WIN32- char cmd[PATH_BUF];- snprintf(cmd, sizeof(cmd), "\"%s\"", exe);+ wchar_t cmd[PATH_BUF];+ wsnprintf(cmd, PATH_BUF, L"\"%ls\"", exe);- STARTUPINFOA si = { .cb = sizeof(si) };+ STARTUPINFOW si = { .cb = sizeof(si) };PROCESS_INFORMATION pi = {0};- if (!CreateProcessA(NULL, cmd, NULL, NULL, FALSE, 0, NULL, NULL, &si, &pi))+ if (!CreateProcessW(NULL, cmd, NULL, NULL, FALSE, 0, NULL, NULL, &si, &pi))return -1;CloseHandle(pi.hProcess);@@ -256,48 +284,63 @@ static int startApp(const char *exe) {}#ifdef _WIN32-static int relaunchFromTemp(const char *appDir, int argc, char *argv[]) {- char exePath[PATH_BUF], longExe[PATH_BUF], longApp[PATH_BUF];- GetModuleFileNameA(NULL, exePath, sizeof(exePath));- GetLongPathNameA(exePath, longExe, sizeof(longExe));-- char appDirClean[PATH_BUF];- strncpy(appDirClean, appDir, sizeof(appDirClean) - 1);- appDirClean[sizeof(appDirClean) - 1] = '\0';- size_t len = strlen(appDirClean);- while (len > 0 && (appDirClean[len - 1] == '\\' || appDirClean[len - 1] == '/'))- appDirClean[--len] = '\0';- GetLongPathNameA(appDirClean, longApp, sizeof(longApp));- len = strlen(longApp);-- if (_strnicmp(longExe, longApp, len) != 0)+static int restartFromTemp(const wchar_t *appDir, int wargc, wchar_t **wargv) {+ wchar_t exePath[PATH_BUF], longExe[PATH_BUF], longApp[PATH_BUF];+ if (!GetModuleFileNameW(NULL, exePath, PATH_BUF))return 0;- char c = longExe[len];- if (c != '\\' && c != '/')+ exePath[PATH_BUF - 1] = L'\0';+ if (!GetLongPathNameW(exePath, longExe, PATH_BUF))return 0;- char tempDir[PATH_BUF];- GetTempPathA(sizeof(tempDir), tempDir);- strncat(tempDir, "gd_updater", sizeof(tempDir) - strlen(tempDir) - 1);- CreateDirectoryA(tempDir, NULL);+ wchar_t appDirClean[PATH_BUF];+ wcsncpy(appDirClean, appDir, PATH_BUF - 1);+ appDirClean[PATH_BUF - 1] = L'\0';+ size_t cleanLen = wcslen(appDirClean);+ while (cleanLen > 0 && (appDirClean[cleanLen - 1] == L'\\' || appDirClean[cleanLen - 1] == L'/'))+ appDirClean[--cleanLen] = L'\0';+ if (!GetLongPathNameW(appDirClean, longApp, PATH_BUF))+ return 0;+ size_t len = wcslen(longApp);- char srcHpatchz[PATH_BUF], dstUpdater[PATH_BUF], dstHpatchz[PATH_BUF];- snprintf(dstUpdater, sizeof(dstUpdater), "%s\\updater.exe", tempDir);- snprintf(srcHpatchz, sizeof(srcHpatchz), "%s\\hpatchz.exe", appDirClean);- snprintf(dstHpatchz, sizeof(dstHpatchz), "%s\\hpatchz.exe", tempDir);+ if (_wcsnicmp(longExe, longApp, len) != 0)+ return 0;+ wchar_t c = longExe[len];+ if (c != L'\\' && c != L'/')+ return 0;- if (!CopyFileA(exePath, dstUpdater, FALSE) ||- !CopyFileA(srcHpatchz, dstHpatchz, FALSE))+ wchar_t tempDir[PATH_BUF];+ if (!GetTempPathW(PATH_BUF, tempDir))+ return 0;+ size_t tempLen = wcslen(tempDir);+ if (tempLen < PATH_BUF - 11)+ wcsncat(tempDir, L"gd_updater", PATH_BUF - tempLen - 1);+ CreateDirectoryW(tempDir, NULL);++ wchar_t srcHpatchz[PATH_BUF], dstUpdater[PATH_BUF], dstHpatchz[PATH_BUF];+ wsnprintf(dstUpdater, PATH_BUF, L"%ls\\updater.exe", tempDir);+ wsnprintf(srcHpatchz, PATH_BUF, L"%ls\\hpatchz.exe", appDirClean);+ wsnprintf(dstHpatchz, PATH_BUF, L"%ls\\hpatchz.exe", tempDir);++ if (!CopyFileW(exePath, dstUpdater, FALSE) ||+ !CopyFileW(srcHpatchz, dstHpatchz, FALSE))return 0;- char cmd[PATH_BUF * 4];- int pos = snprintf(cmd, sizeof(cmd), "\"%s\"", dstUpdater);- for (int i = 1; i < argc; i++)- pos += snprintf(cmd + pos, sizeof(cmd) - pos, " \"%s\"", argv[i]);+ wchar_t cmd[PATH_BUF * 4];+ size_t cmdSize = PATH_BUF * 4;+ int pos = _snwprintf(cmd, cmdSize, L"\"%ls\"", dstUpdater);+ if (pos < 0 || (size_t)pos >= cmdSize) return 0;+ int truncated = 0;+ for (int i = 1; i < wargc && (size_t)pos < cmdSize; i++) {+ int n = _snwprintf(cmd + pos, cmdSize - pos, L" \"%ls\"", wargv[i]);+ if (n < 0) { truncated = 1; break; }+ pos += n;+ }+ cmd[cmdSize - 1] = L'\0';+ if (truncated) return 0;- STARTUPINFOA si = { .cb = sizeof(si) };+ STARTUPINFOW si = { .cb = sizeof(si) };PROCESS_INFORMATION pi = {0};- if (!CreateProcessA(NULL, cmd, NULL, NULL, FALSE,+ if (!CreateProcessW(NULL, cmd, NULL, NULL, FALSE,DETACHED_PROCESS | CREATE_NEW_PROCESS_GROUP,NULL, NULL, &si, &pi))return 0;@@ -315,19 +358,33 @@ int main(int argc, char *argv[]) {}#ifdef _WIN32- if (relaunchFromTemp(argv[2], argc, argv))+ int wargc;+ wchar_t **wargv = CommandLineToArgvW(GetCommandLineW(), &wargc);+ if (!wargv || wargc < 4) return EXIT_BAD_ARGS;++ if (restartFromTemp(wargv[2], wargc, wargv))return EXIT_OK;-#endif+ unsigned long appPid = wcstoul(wargv[1], NULL, 10);+ const wchar_t *appDir = wargv[2];+ const wchar_t *appExe = wargv[3];+ const wchar_t *patchFile = wargc >= 5 ? wargv[4] : NULL;+#elseunsigned long appPid = strtoul(argv[1], NULL, 10);const char *appDir = argv[2];const char *appExe = argv[3];const char *patchFile = argc == 5 ? argv[4] : NULL;+#endif- char newDir[PATH_BUF], backupDir[PATH_BUF], updaterDir[PATH_BUF];+ pchar newDir[PATH_BUF], backupDir[PATH_BUF], updaterDir[PATH_BUF];+#ifdef _WIN32+ wsnprintf(newDir, PATH_BUF, L"%ls_new", appDir);+ wsnprintf(backupDir, PATH_BUF, L"%ls_backup", appDir);+#elsesnprintf(newDir, sizeof(newDir), "%s_new", appDir);snprintf(backupDir, sizeof(backupDir), "%s_backup", appDir);- findExeDir(updaterDir, sizeof(updaterDir));+#endif+ findExeDir(updaterDir, PATH_BUF);openLog(appDir);logMsg(" __________ __ ______ ____ ___ ________________");@@ -335,8 +392,13 @@ int main(int argc, char *argv[]) {logMsg(" / / __/ / / / / / / / /_/ / / / / /| | / / / __/ / /_/ /");logMsg("/ /_/ / /_/ / / /_/ / ____/ /_/ / ___ |/ / / /___/ _, _/");logMsg("\\____/_____/ \\____/_/ /_____/_/ |_/_/ /_____/_/ |_|");- logMsg("pid=%lu appDir=\"%s\" exe=\"%s\" patch=\"%s\"",- appPid, appDir, appExe, patchFile ? patchFile : "(full)");+#ifdef _WIN32+ const wchar_t *patchLabel = patchFile ? patchFile : L"(full)";+#else+ const char *patchLabel = patchFile ? patchFile : "(full)";+#endif+ logMsg("pid=%lu appDir=\"" PFMT "\" exe=\"" PFMT "\" patch=\"" PFMT "\"",+ appPid, appDir, appExe, patchLabel);logMsg("waiting for app %lu ...", appPid);if (waitForProcessExit(appPid, 30000) != 0) {@@ -362,7 +424,8 @@ int main(int argc, char *argv[]) {int swapOk = 0;#ifdef __APPLE__if (renamex_np(newDir, appDir, RENAME_SWAP) == 0) {- rename(newDir, backupDir);+ if (rename(newDir, backupDir) != 0)+ logMsg("rename old to backup failed (%s), continuing", strerror(errno));swapOk = 1;} else {logMsg("renamex_np failed (%s), falling back", strerror(errno));@@ -383,10 +446,15 @@ int main(int argc, char *argv[]) {}logMsg("installed");- if (patchFile)+ if (patchFile) {+#ifdef _WIN32+ _wremove(patchFile);+#elseremove(patchFile);+#endif+ }- logMsg("starting %s", appExe);+ logMsg("starting " PFMT, appExe);if (startApp(appExe) != 0) {logMsg("start failed, rolling back");if (moveDir(appDir, newDir) == 0)@@ -398,5 +466,8 @@ int main(int argc, char *argv[]) {logMsg("ok");closeLog();+#ifdef _WIN32+ LocalFree(wargv);+#endifreturn EXIT_OK;}