diff options
author | George Hazan <ghazan@miranda.im> | 2018-06-29 14:31:56 +0300 |
---|---|---|
committer | George Hazan <ghazan@miranda.im> | 2018-06-29 14:31:56 +0300 |
commit | 0d6ff4d0ed05a2a35cd7ade12e2c924704211bc8 (patch) | |
tree | 72cc7e24988aa9f35691b1d0bdccbc172208db70 | |
parent | 6233a4777edce1b30af96035e31ae3df074b15cb (diff) |
safer method of backing up databases instead of trying to read them manually
-rw-r--r-- | include/m_db_int.h | 2 | ||||
-rw-r--r-- | libs/win32/mir_app.lib | bin | 172462 -> 172762 bytes | |||
-rw-r--r-- | libs/win64/mir_app.lib | bin | 167540 -> 167850 bytes | |||
-rw-r--r-- | plugins/Db_autobackups/src/backup.cpp | 53 | ||||
-rw-r--r-- | plugins/Db_autobackups/src/stdafx.h | 1 | ||||
-rw-r--r-- | plugins/Dbx_mdbx/src/dbintf.cpp | 17 | ||||
-rw-r--r-- | plugins/Dbx_mdbx/src/dbintf.h | 1 | ||||
-rw-r--r-- | src/mir_app/src/MDatabaseCommon.cpp | 5 | ||||
-rw-r--r-- | src/mir_app/src/mir_app.def | 1 | ||||
-rw-r--r-- | src/mir_app/src/mir_app64.def | 1 |
10 files changed, 67 insertions, 14 deletions
diff --git a/include/m_db_int.h b/include/m_db_int.h index d0b66e8f25..591985573a 100644 --- a/include/m_db_int.h +++ b/include/m_db_int.h @@ -127,6 +127,7 @@ interface MIR_APP_EXPORT MIDatabase STDMETHOD_(BOOL, MetaSplitHistory)(DBCachedContact *ccMeta, DBCachedContact *ccSub) PURE;
STDMETHOD_(BOOL, Compact)(void) PURE;
+ STDMETHOD_(BOOL, Backup)(LPCWSTR) PURE;
};
/////////////////////////////////////////////////////////////////////////////////////////
@@ -173,6 +174,7 @@ public: STDMETHODIMP_(BOOL) SetSettingResident(BOOL bIsResident, const char *pszSettingName);
STDMETHODIMP_(BOOL) Compact(void);
+ STDMETHODIMP_(BOOL) Backup(LPCWSTR);
};
///////////////////////////////////////////////////////////////////////////////
diff --git a/libs/win32/mir_app.lib b/libs/win32/mir_app.lib Binary files differindex 3fced1bb1f..9d22c43486 100644 --- a/libs/win32/mir_app.lib +++ b/libs/win32/mir_app.lib diff --git a/libs/win64/mir_app.lib b/libs/win64/mir_app.lib Binary files differindex fdca68a76e..67c18be9df 100644 --- a/libs/win64/mir_app.lib +++ b/libs/win64/mir_app.lib diff --git a/plugins/Db_autobackups/src/backup.cpp b/plugins/Db_autobackups/src/backup.cpp index 0d9764b75e..cd87c676a1 100644 --- a/plugins/Db_autobackups/src/backup.cpp +++ b/plugins/Db_autobackups/src/backup.cpp @@ -3,7 +3,6 @@ static UINT_PTR timer_id = 0;
volatile long m_state = 0;
-
LRESULT CALLBACK DlgProcPopup(HWND hWnd, UINT msg, WPARAM wParam, LPARAM lParam)
{
switch (msg) {
@@ -78,18 +77,36 @@ wchar_t* DoubleSlash(wchar_t *sorce) return ret;
}
-bool MakeZip_Dir(LPCWSTR szDir, LPCWSTR szDest, LPCWSTR pwszBackupFolder, HWND progress_dialog)
+bool MakeZip_Dir(LPCWSTR szDir, LPCWSTR pwszProfile, LPCWSTR szDest, LPCWSTR pwszBackupFolder, HWND progress_dialog)
{
HWND hProgBar = GetDlgItem(progress_dialog, IDC_PROGRESS);
size_t count = 0, folderNameLen = mir_wstrlen(pwszBackupFolder);
OBJLIST<ZipFile> lstFiles(15);
+ wchar_t wszTempName[MAX_PATH];
+ if (!GetTempPathW(_countof(wszTempName), wszTempName))
+ return false;
+
+ if (!GetTempFileNameW(wszTempName, L"mir_backup_", 0, wszTempName))
+ return false;
+
+ if (db_get_current()->Backup(wszTempName))
+ return false;
+
+ lstFiles.insert(new ZipFile(wszTempName, pwszProfile));
+
+ CMStringW wszProfile;
+ wszProfile.Format(L"%s\\%s", szDir, pwszProfile);
+
for (auto it = fs::recursive_directory_iterator(fs::path(szDir)); it != fs::recursive_directory_iterator(); ++it) {
const auto& file = it->path();
if (fs::is_directory(file))
continue;
const std::wstring &filepath = file.wstring();
+ if (wszProfile == filepath.c_str())
+ continue;
+
if (filepath.find(szDest) != std::wstring::npos || !mir_wstrncmpi(filepath.c_str(), pwszBackupFolder, folderNameLen))
continue;
@@ -106,19 +123,29 @@ bool MakeZip_Dir(LPCWSTR szDir, LPCWSTR szDest, LPCWSTR pwszBackupFolder, HWND p return GetWindowLongPtr(progress_dialog, GWLP_USERDATA) != 1;
});
+ DeleteFileW(wszTempName);
return 1;
}
-bool MakeZip(wchar_t *tszSource, wchar_t *tszDest, wchar_t *dbname, HWND progress_dialog)
+bool MakeZip(wchar_t *tszDest, wchar_t *dbname, HWND progress_dialog)
{
HWND hProgBar = GetDlgItem(progress_dialog, IDC_PROGRESS);
- ptrW tszDestPath(DoubleSlash(tszDest));
- OBJLIST<ZipFile> lstFiles(15);
- lstFiles.insert(new ZipFile(tszSource, dbname));
+ wchar_t wszTempName[MAX_PATH];
+ if (!GetTempPathW(_countof(wszTempName), wszTempName))
+ return false;
- CreateZipFile(tszDest, lstFiles, [&](size_t)->bool { SendMessage(hProgBar, PBM_SETPOS, (WPARAM)(100), 0); return true; });
+ if (!GetTempFileNameW(wszTempName, L"mir_backup_", 0, wszTempName))
+ return false;
+ if (db_get_current()->Backup(wszTempName))
+ return false;
+
+ OBJLIST<ZipFile> lstFiles(1);
+ lstFiles.insert(new ZipFile(wszTempName, dbname));
+
+ CreateZipFile(tszDest, lstFiles, [&](size_t)->bool { SendMessage(hProgBar, PBM_SETPOS, (WPARAM)(100), 0); return true; });
+ DeleteFileW(wszTempName);
return true;
}
@@ -183,7 +210,7 @@ err_out: int Backup(wchar_t *backup_filename)
{
bool bZip = false;
- wchar_t dbname[MAX_PATH], source_file[MAX_PATH] = { 0 }, dest_file[MAX_PATH];
+ wchar_t dbname[MAX_PATH], dest_file[MAX_PATH];
HWND progress_dialog = nullptr;
Profile_GetNameW(_countof(dbname), dbname);
@@ -223,15 +250,13 @@ int Backup(wchar_t *backup_filename) SetDlgItemText(progress_dialog, IDC_PROGRESSMESSAGE, TranslateT("Copying database file..."));
- VARSW profile_path(L"%miranda_userdata%");
- mir_snwprintf(source_file, L"%s\\%s", profile_path, dbname);
- BOOL res = 0;
+ BOOL res;
if (bZip) {
res = options.backup_profile
- ? MakeZip_Dir(profile_path, dest_file, backupfolder, progress_dialog)
- : MakeZip(source_file, dest_file, dbname, progress_dialog);
+ ? MakeZip_Dir(VARSW(L"%miranda_userdata%"), dbname, dest_file, backupfolder, progress_dialog)
+ : MakeZip(dest_file, dbname, progress_dialog);
}
- else res = CopyFile(source_file, dest_file, 0);
+ else res = db_get_current()->Backup(dest_file) == ERROR_SUCCESS;
if (res) {
if (!bZip) { // Set the backup file to the current time for rotator's correct work
diff --git a/plugins/Db_autobackups/src/stdafx.h b/plugins/Db_autobackups/src/stdafx.h index e6d992c8ca..1c2036e736 100644 --- a/plugins/Db_autobackups/src/stdafx.h +++ b/plugins/Db_autobackups/src/stdafx.h @@ -15,6 +15,7 @@ namespace fs = std::experimental::filesystem; #include <newpluginapi.h>
#include <m_clist.h>
#include <m_database.h>
+#include <m_db_int.h>
#include <m_langpack.h>
#include <m_options.h>
#include <m_popup.h>
diff --git a/plugins/Dbx_mdbx/src/dbintf.cpp b/plugins/Dbx_mdbx/src/dbintf.cpp index 9fffe8f732..cb663276c6 100644 --- a/plugins/Dbx_mdbx/src/dbintf.cpp +++ b/plugins/Dbx_mdbx/src/dbintf.cpp @@ -200,6 +200,23 @@ BOOL CDbxMDBX::Compact() return 0;
}
+BOOL CDbxMDBX::Backup(const wchar_t *pwszPath)
+{
+ HANDLE pFile = ::CreateFile(pwszPath, GENERIC_WRITE, 0, nullptr, CREATE_ALWAYS, FILE_ATTRIBUTE_NORMAL, nullptr);
+ if (pFile == nullptr) {
+ Netlib_Logf(0, "Backup file <%S> cannot be created", pwszPath);
+ return 1;
+ }
+
+ int res = mdbx_env_copy2fd(m_env, pFile, 0);
+ CloseHandle(pFile);
+ if (res == MDBX_SUCCESS)
+ return 0;
+
+ DeleteFileW(pwszPath);
+ return res;
+}
+
/////////////////////////////////////////////////////////////////////////////////////////
int CDbxMDBX::PrepareCheck()
diff --git a/plugins/Dbx_mdbx/src/dbintf.h b/plugins/Dbx_mdbx/src/dbintf.h index 1eebe92b2c..73fb14e36b 100644 --- a/plugins/Dbx_mdbx/src/dbintf.h +++ b/plugins/Dbx_mdbx/src/dbintf.h @@ -268,6 +268,7 @@ public: STDMETHODIMP_(BOOL) MetaSplitHistory(DBCachedContact *ccMeta, DBCachedContact *ccSub);
STDMETHODIMP_(BOOL) Compact();
+ STDMETHODIMP_(BOOL) Backup(LPCWSTR);
public:
MICryptoEngine *m_crypto;
diff --git a/src/mir_app/src/MDatabaseCommon.cpp b/src/mir_app/src/MDatabaseCommon.cpp index db053e03fd..6eb0e0582a 100644 --- a/src/mir_app/src/MDatabaseCommon.cpp +++ b/src/mir_app/src/MDatabaseCommon.cpp @@ -121,6 +121,11 @@ BOOL MDatabaseCommon::Compact(void) return ERROR_NOT_SUPPORTED; } +BOOL MDatabaseCommon::Backup(LPCWSTR) +{ + return ERROR_NOT_SUPPORTED; +} + ///////////////////////////////////////////////////////////////////////////////////////// // Contacts diff --git a/src/mir_app/src/mir_app.def b/src/mir_app/src/mir_app.def index 7b06a851ec..950ef2f568 100644 --- a/src/mir_app/src/mir_app.def +++ b/src/mir_app/src/mir_app.def @@ -628,3 +628,4 @@ Profile_GetSetting @656 Profile_GetSettingInt @657
Netlib_GetUserSettingsByName @658
Netlib_SetUserSettingsByName @659
+?Backup@MDatabaseCommon@@UAGHPB_W@Z @660 NONAME
diff --git a/src/mir_app/src/mir_app64.def b/src/mir_app/src/mir_app64.def index a58eaaa30d..f0153a3013 100644 --- a/src/mir_app/src/mir_app64.def +++ b/src/mir_app/src/mir_app64.def @@ -628,3 +628,4 @@ Profile_GetSetting @656 Profile_GetSettingInt @657
Netlib_GetUserSettingsByName @658
Netlib_SetUserSettingsByName @659
+?Backup@MDatabaseCommon@@UEAAHPEB_W@Z @660 NONAME
|