From 070b16e6115c804f833e3769eadfc726fe40ac0f Mon Sep 17 00:00:00 2001 From: Connor McLaughlin Date: Thu, 6 Aug 2020 22:02:30 +1000 Subject: [PATCH] Updater/Win32: Use shell for deleting directories --- src/updater/updater.cpp | 27 ++++++++++++++++++++++++--- src/updater/updater.h | 2 ++ 2 files changed, 26 insertions(+), 3 deletions(-) diff --git a/src/updater/updater.cpp b/src/updater/updater.cpp index cd804739e..993bd3b0a 100644 --- a/src/updater/updater.cpp +++ b/src/updater/updater.cpp @@ -12,6 +12,10 @@ #include #include +#ifdef WIN32 +#include +#endif + Updater::Updater(ProgressCallback* progress) : m_progress(progress) { progress->SetTitle("DuckStation Update Installer"); @@ -49,6 +53,24 @@ bool Updater::OpenUpdateZip(const char* path) return ParseZip(); } +bool Updater::RecursiveDeleteDirectory(const char* path) +{ +#ifdef WIN32 + // making this safer on Win32... + std::wstring wpath(StringUtil::UTF8StringToWideString(path)); + wpath += L'\0'; + + SHFILEOPSTRUCTW op = {}; + op.wFunc = FO_DELETE; + op.pFrom = wpath.c_str(); + op.fFlags = FOF_NOCONFIRMATION; + + return (SHFileOperationW(&op) == 0 && !op.fAnyOperationsAborted); +#else + return FileSystem::DeleteDirectory(path, true); +#endif +} + bool Updater::ParseZip() { if (unzGoToFirstFile(m_zf) != UNZ_OK) @@ -140,8 +162,7 @@ bool Updater::PrepareStagingDirectory() if (FileSystem::DirectoryExists(m_staging_directory.c_str())) { m_progress->DisplayFormattedWarning("Update staging directory already exists, removing"); - FileSystem::DeleteDirectory(m_staging_directory.c_str(), true); - if (FileSystem::DirectoryExists(m_staging_directory.c_str())) + if (!RecursiveDeleteDirectory(m_staging_directory.c_str()) || FileSystem::DirectoryExists(m_staging_directory.c_str())) { m_progress->ModalError("Failed to remove old staging directory"); return false; @@ -285,6 +306,6 @@ bool Updater::CommitUpdate() void Updater::CleanupStagingDirectory() { // remove staging directory itself - if (!FileSystem::DeleteDirectory(m_staging_directory.c_str(), true)) + if (!RecursiveDeleteDirectory(m_staging_directory.c_str())) m_progress->DisplayFormattedError("Failed to remove staging directory '%s'", m_staging_directory.c_str()); } diff --git a/src/updater/updater.h b/src/updater/updater.h index d0dd69493..0c636a668 100644 --- a/src/updater/updater.h +++ b/src/updater/updater.h @@ -19,6 +19,8 @@ public: void CleanupStagingDirectory(); private: + static bool RecursiveDeleteDirectory(const char* path); + struct FileToUpdate { std::string original_zip_filename;