From 85341d5502ce3db9f60c0f9231e7f66a57f46546 Mon Sep 17 00:00:00 2001 From: George Hazan Date: Sat, 3 Aug 2019 19:38:42 +0300 Subject: Db_autobackups: ME_SYSTEM_OKTOEXIT used instead of ME_SYSTEM_PRESHUTDOWN to launch backups on exit, thus allowing them to finish correctly --- plugins/Db_autobackups/src/backup.cpp | 20 +++++++++----------- plugins/Db_autobackups/src/main.cpp | 18 ++++++++++++------ plugins/Db_autobackups/src/stdafx.h | 4 ++-- plugins/Db_autobackups/src/version.h | 2 +- 4 files changed, 24 insertions(+), 20 deletions(-) diff --git a/plugins/Db_autobackups/src/backup.cpp b/plugins/Db_autobackups/src/backup.cpp index 34b13ca302..dbbb416260 100644 --- a/plugins/Db_autobackups/src/backup.cpp +++ b/plugins/Db_autobackups/src/backup.cpp @@ -28,7 +28,7 @@ static LRESULT CALLBACK DlgProcPopup(HWND hWnd, UINT msg, WPARAM wParam, LPARAM static void ShowPopup(const wchar_t *ptszText, wchar_t *ptszHeader, wchar_t *ptszPath) { - if (g_plugin.bTerminated) + if (Miranda_IsTerminated()) return; POPUPDATAW ppd; @@ -304,22 +304,20 @@ static void BackupThread(void *backup_filename) mir_free(backup_filename); } -void BackupStart(wchar_t *backup_filename, bool bInThread) +int BackupStatus() { - LONG cur_state = InterlockedCompareExchange(&g_iState, 1, 0); - if (cur_state != 0) { // Backup allready in process. + return InterlockedCompareExchange(&g_iState, 1, 0); +} + +void BackupStart(wchar_t *backup_filename) +{ + if (BackupStatus() != 0) { // Backup allready in process. ShowPopup(TranslateT("Database back up in process..."), TranslateT("Error"), nullptr); return; } - if (bInThread) { - if (mir_forkthread(BackupThread, mir_wstrdup(backup_filename)) == INVALID_HANDLE_VALUE) - InterlockedExchange(&g_iState, 0); // Backup done. - } - else { - Backup(backup_filename); + if (mir_forkthread(BackupThread, mir_wstrdup(backup_filename)) == INVALID_HANDLE_VALUE) InterlockedExchange(&g_iState, 0); // Backup done. - } } VOID CALLBACK TimerProc(HWND, UINT, UINT_PTR, DWORD) diff --git a/plugins/Db_autobackups/src/main.cpp b/plugins/Db_autobackups/src/main.cpp index b315b4a9a4..3695a19350 100644 --- a/plugins/Db_autobackups/src/main.cpp +++ b/plugins/Db_autobackups/src/main.cpp @@ -140,12 +140,18 @@ static int ModulesLoad(WPARAM, LPARAM) // can't do this on unload, since other plugins will be have already been unloaded, but their hooks // for setting changed event not cleared. the backup on exit function will write to the db, calling those hooks. -static int PreShutdown(WPARAM, LPARAM) -{ - g_plugin.bTerminated = true; +static bool bStarted = false; - if (g_plugin.backup_types & BT_EXIT) - BackupStart(nullptr, false); +static int OkToExit(WPARAM, LPARAM) +{ + if (g_plugin.backup_types & BT_EXIT) { + if (bStarted) + return BackupStatus() ? 1 : 0; + + bStarted = true; + BackupStart(nullptr); + return 1; + } return 0; } @@ -153,7 +159,7 @@ int CMPlugin::Load() { Miranda_GetVersionText(g_szMirVer, sizeof(g_szMirVer)); - HookEvent(ME_SYSTEM_PRESHUTDOWN, PreShutdown); + HookEvent(ME_SYSTEM_OKTOEXIT, OkToExit); HookEvent(ME_SYSTEM_MODULESLOADED, ModulesLoad); g_plugin.registerIcon(LPGEN("Database") "/" LPGEN("Database backups"), iconList); diff --git a/plugins/Db_autobackups/src/stdafx.h b/plugins/Db_autobackups/src/stdafx.h index f4dd0be3bc..ad48212350 100644 --- a/plugins/Db_autobackups/src/stdafx.h +++ b/plugins/Db_autobackups/src/stdafx.h @@ -34,7 +34,6 @@ struct CMPlugin : public PLUGIN { CMPlugin(); - bool bTerminated; CMOption backup_types; CMOption period; CMOption period_type; @@ -60,7 +59,8 @@ struct CMPlugin : public PLUGIN int SetBackupTimer(void); int OptionsInit(WPARAM wParam, LPARAM lParam); -void BackupStart(wchar_t *backup_filename, bool bInThread = true); +void BackupStart(wchar_t *backup_filename); +int BackupStatus(void); struct ZipFile { diff --git a/plugins/Db_autobackups/src/version.h b/plugins/Db_autobackups/src/version.h index 4f3f7f9111..0b7695160f 100644 --- a/plugins/Db_autobackups/src/version.h +++ b/plugins/Db_autobackups/src/version.h @@ -1,7 +1,7 @@ #define __MAJOR_VERSION 0 #define __MINOR_VERSION 1 #define __RELEASE_NUM 0 -#define __BUILD_NUM 3 +#define __BUILD_NUM 4 #include -- cgit v1.2.3