From 88fc41f67e3114e06deba123ce972052f17e0f3e Mon Sep 17 00:00:00 2001 From: George Hazan Date: Tue, 8 Sep 2020 19:43:23 +0300 Subject: Plugin Updater to store backups in the separate folders --- plugins/PluginUpdater/src/DlgUpdate.cpp | 4 ---- plugins/PluginUpdater/src/Events.cpp | 12 +++++------ plugins/PluginUpdater/src/Utils.cpp | 38 ++++++++++++++++++++++++++++++++- plugins/PluginUpdater/src/checksum.cpp | 2 +- plugins/PluginUpdater/src/stdafx.h | 2 +- 5 files changed, 44 insertions(+), 14 deletions(-) (limited to 'plugins/PluginUpdater/src') diff --git a/plugins/PluginUpdater/src/DlgUpdate.cpp b/plugins/PluginUpdater/src/DlgUpdate.cpp index 3d0b272284..f5da0bbfe2 100644 --- a/plugins/PluginUpdater/src/DlgUpdate.cpp +++ b/plugins/PluginUpdater/src/DlgUpdate.cpp @@ -119,8 +119,6 @@ LBL_Error: } Skin_PlaySound("updatecompleted"); - g_plugin.setByte(DB_SETTING_RESTART_COUNT, 5); - if (g_plugin.bBackup) CallService(MS_AB_BACKUP, 0, 0); @@ -474,8 +472,6 @@ LBL_Error: g_plugin.bForceRedownload = false; g_plugin.bChangePlatform = false; g_plugin.delSetting(DB_SETTING_CHANGEPLATFORM); - - g_plugin.setByte(DB_SETTING_RESTART_COUNT, 5); g_plugin.setByte(DB_SETTING_NEED_RESTART, 1); if (g_plugin.bBackup) diff --git a/plugins/PluginUpdater/src/Events.cpp b/plugins/PluginUpdater/src/Events.cpp index 55a28341bd..d87904ddb4 100644 --- a/plugins/PluginUpdater/src/Events.cpp +++ b/plugins/PluginUpdater/src/Events.cpp @@ -41,16 +41,14 @@ int ModulesLoaded(WPARAM, LPARAM) if (ServiceExists(MS_ASSOCMGR_ADDNEWURLTYPE)) AssocMgr_AddNewUrlTypeW("mirpu:", TranslateT("Plugin updater URI scheme"), g_plugin.getInst(), IDI_PLGLIST, MODULENAME "/ParseUri", 0); - int iRestartCount = g_plugin.getByte(DB_SETTING_RESTART_COUNT, 2); - if (iRestartCount > 0) - g_plugin.setByte(DB_SETTING_RESTART_COUNT, iRestartCount - 1); - else - DeleteDirectoryTreeW(g_wszRoot); + int iCompatLevel = db_get_b(0, "Compatibility", MODULENAME); + if (iCompatLevel == 0) { + db_set_b(0, "Compatibility", MODULENAME, 1); + DeleteDirectoryTreeW(CMStringW(g_wszRoot) + L"\\Backups"); + } CheckUpdateOnStartup(); - CreateTimer(); - return 0; } diff --git a/plugins/PluginUpdater/src/Utils.cpp b/plugins/PluginUpdater/src/Utils.cpp index 5108c2fbd4..8634b156e1 100644 --- a/plugins/PluginUpdater/src/Utils.cpp +++ b/plugins/PluginUpdater/src/Utils.cpp @@ -437,10 +437,38 @@ bool PrepareEscalation() ///////////////////////////////////////////////////////////////////////////////////////// // Folder creation +static int __cdecl CompareDirs(const CMStringW *s1, const CMStringW *s2) +{ + return mir_wstrcmp(s1->c_str(), s2->c_str()); +} + void CreateWorkFolders(TFileName &wszTempFolder, TFileName &wszBackupFolder) { - mir_snwprintf(wszBackupFolder, L"%s\\Backups", g_wszRoot); + TFileName wszMask; + mir_snwprintf(wszMask, L"%s\\Backups\\BKP*", g_wszRoot); + + WIN32_FIND_DATAW fdata; + HANDLE hFind = FindFirstFileW(wszMask, &fdata); + if (hFind) { + // sort folder names alphabetically + OBJLIST arNames(1, CompareDirs); + do { + if (fdata.dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY) + arNames.insert(new CMStringW(fdata.cFileName)); + } while (FindNextFileW(hFind, &fdata)); + + // remove all folders with lesser dates if there're more than 10 folders + while (arNames.getCount() > 9) { + SafeDeleteDirectory(arNames[0]); + arNames.remove(00); + } + } + + SYSTEMTIME st; + GetLocalTime(&st); + mir_snwprintf(wszBackupFolder, L"%s\\Backups\\BKP%04d-%02d-%02d %02d-%02d-%02d-%03d", g_wszRoot, st.wYear, st.wMonth, st.wDay, st.wHour, st.wMinute, st.wSecond, st.wMilliseconds); SafeCreateDirectory(wszBackupFolder); + mir_snwprintf(wszTempFolder, L"%s\\Temp", g_wszRoot); SafeCreateDirectory(wszTempFolder); } @@ -539,6 +567,14 @@ int SafeCreateDirectory(const wchar_t *pwszFolder) return TransactPipe(4, pwszFolder, nullptr); } +int SafeDeleteDirectory(const wchar_t *pwszDirName) +{ + if (hPipe == nullptr) + return DeleteDirectoryTreeW(pwszDirName); + + return TransactPipe(6, pwszDirName, nullptr); +} + int SafeCreateFilePath(const wchar_t *pwszFolder) { if (hPipe == nullptr) { diff --git a/plugins/PluginUpdater/src/checksum.cpp b/plugins/PluginUpdater/src/checksum.cpp index 77b138fde3..67b4d8e633 100644 --- a/plugins/PluginUpdater/src/checksum.cpp +++ b/plugins/PluginUpdater/src/checksum.cpp @@ -25,7 +25,7 @@ struct MFileMapping PBYTE ptr; HANDLE hMap, hFile; - MFileMapping(const wchar_t* pwszFileName) + MFileMapping(const wchar_t *pwszFileName) { ptr = nullptr; hMap = nullptr; diff --git a/plugins/PluginUpdater/src/stdafx.h b/plugins/PluginUpdater/src/stdafx.h index a9628fb7ca..ed46c7c286 100644 --- a/plugins/PluginUpdater/src/stdafx.h +++ b/plugins/PluginUpdater/src/stdafx.h @@ -136,7 +136,6 @@ enum #define DB_SETTING_UPDATE_MODE "UpdateMode" #define DB_SETTING_UPDATE_URL "UpdateURL" #define DB_SETTING_NEED_RESTART "NeedRestart" -#define DB_SETTING_RESTART_COUNT "RestartCount" #define DB_SETTING_LAST_UPDATE "LastUpdate" #define DB_SETTING_DONT_SWITCH_TO_STABLE "DontSwitchToStable" #define DB_SETTING_CHANGEPLATFORM "ChangePlatform" @@ -276,6 +275,7 @@ bool PrepareEscalation(); void CreateWorkFolders(TFileName &wszTempFolder, TFileName &wszBackupFolder); int SafeCreateDirectory(const wchar_t *pwszDirName); +int SafeDeleteDirectory(const wchar_t *pwszDirName); int SafeCopyFile(const wchar_t *pwszSrc, const wchar_t *pwszDst); int SafeMoveFile(const wchar_t *pwszSrc, const wchar_t *pwszDst); int SafeDeleteFile(const wchar_t *pwszSrc); -- cgit v1.2.3