From ad0d3972a695e9eddfb45be0072eb94961382e4b Mon Sep 17 00:00:00 2001 From: Vicki Pfau Date: Sat, 28 Dec 2024 22:45:39 -0800 Subject: [PATCH] Updater: Fix rewriting folders and files on Windows (fixes #3384) --- CHANGES | 1 + src/tools/updater-main.c | 20 ++++++++++++-------- 2 files changed, 13 insertions(+), 8 deletions(-) diff --git a/CHANGES b/CHANGES index 62f51060c..b5b2f4fe1 100644 --- a/CHANGES +++ b/CHANGES @@ -48,6 +48,7 @@ Misc: - Qt: Show a dummy shader settings tab if shaders aren't supported - Res: Port NSO-gba-colors shader (closes mgba.io/i/2834) - Scripting: Add `callbacks:oneshot` for single-call callbacks + - Updater: Fix rewriting folders and files on Windows (fixes mgba.io/i/3384) 0.10.4: (2024-12-07) Emulation fixes: diff --git a/src/tools/updater-main.c b/src/tools/updater-main.c index a27a07a08..b536e3208 100644 --- a/src/tools/updater-main.c +++ b/src/tools/updater-main.c @@ -133,14 +133,11 @@ bool extractArchive(struct VDir* archive, const char* root, bool prefix) { errno = 0; vfOut = VFileOpen(path, O_WRONLY | O_CREAT | O_TRUNC); if (!vfOut) { - if (errno == EACCES) { -#ifdef _WIN32 - Sleep(1000); -#else - sleep(1); -#endif - vfOut = VFileOpen(path, O_WRONLY | O_CREAT | O_TRUNC); - } else if (errno == EISDIR) { + int error = errno; + struct stat st; + if (error == EISDIR || (stat(path, &st) >= 0 && S_ISDIR(st.st_mode))) { + // Windows maps STATUS_FILE_IS_A_DIRECTORY to ERROR_ACCESS_DENIED, + // which then gets mapped to EACCESS, because everything is awful fprintf(logfile, "rm -r %s\n", path); if (!rmdirRecursive(VDirOpen(path))) { return false; @@ -151,6 +148,13 @@ bool extractArchive(struct VDir* archive, const char* root, bool prefix) { RemoveDirectoryW(wpath); #else rmdir(path); +#endif + vfOut = VFileOpen(path, O_WRONLY | O_CREAT | O_TRUNC); + } else if (error == EACCES || error == ETXTBSY) { +#ifdef _WIN32 + Sleep(1000); +#else + sleep(1); #endif vfOut = VFileOpen(path, O_WRONLY | O_CREAT | O_TRUNC); }