From 8f648b6808819c3b37ce1f1994c82cf783f6eb50 Mon Sep 17 00:00:00 2001 From: Vicki Pfau Date: Tue, 18 Jul 2023 01:42:30 -0700 Subject: [PATCH] Updater: Fix overwriting files with directories --- src/feature/updater-main.c | 24 +++++++++++++++++++++--- 1 file changed, 21 insertions(+), 3 deletions(-) diff --git a/src/feature/updater-main.c b/src/feature/updater-main.c index d495dfb98..f9e09269a 100644 --- a/src/feature/updater-main.c +++ b/src/feature/updater-main.c @@ -92,9 +92,27 @@ bool extractArchive(struct VDir* archive, const char* root, bool prefix) { switch (vde->type(vde)) { case VFS_DIRECTORY: fprintf(logfile, "mkdir %s\n", fname); - if (mkdir(path, 0755) < 0 && errno != EEXIST) { - fprintf(logfile, "error %i\n", errno); - return false; + if (mkdir(path, 0755) < 0) { + bool redo = false; + if (errno == EEXIST) { + struct stat st; + if (stat(path, &st) >= 0 && !S_ISDIR(st.st_mode)) { +#ifdef _WIN32 + wchar_t wpath[MAX_PATH + 1]; + MultiByteToWideChar(CP_UTF8, 0, path, -1, wpath, MAX_PATH); + DeleteFileW(wpath); +#else + unlink(path); +#endif + if (mkdir(path, 0755) >= 0) { + redo = true; + } + } + } + if (!redo) { + fprintf(logfile, "error %i\n", errno); + return false; + } } if (!prefix) { struct VDir* subdir = archive->openDir(archive, fname);