diff options
author | George Hazan <ghazan@miranda.im> | 2018-04-02 11:59:43 +0300 |
---|---|---|
committer | George Hazan <ghazan@miranda.im> | 2018-04-02 11:59:43 +0300 |
commit | 82e1444c01f0ab700c385ff0f846858c7f627c36 (patch) | |
tree | b33c5ce0918fb8b5053d0079eecdefe7c8f95b48 | |
parent | 704258cde008db0e80b9d5459833c3c2b6a3f09d (diff) |
Dbx_mdbx:
- unused class members removed;
- added function TouchFile() to change access date & time on loading & exit
-rw-r--r-- | plugins/Dbx_mdbx/src/dbcontacts.cpp | 8 | ||||
-rw-r--r-- | plugins/Dbx_mdbx/src/dbintf.cpp | 160 | ||||
-rw-r--r-- | plugins/Dbx_mdbx/src/dbintf.h | 9 | ||||
-rw-r--r-- | plugins/Dbx_mdbx/src/init.cpp | 10 | ||||
-rw-r--r-- | plugins/Dbx_mdbx/src/stdafx.h | 1 |
5 files changed, 95 insertions, 93 deletions
diff --git a/plugins/Dbx_mdbx/src/dbcontacts.cpp b/plugins/Dbx_mdbx/src/dbcontacts.cpp index aa3a27f8f2..f59e57c4b3 100644 --- a/plugins/Dbx_mdbx/src/dbcontacts.cpp +++ b/plugins/Dbx_mdbx/src/dbcontacts.cpp @@ -25,7 +25,9 @@ Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. STDMETHODIMP_(LONG) CDbxMDBX::GetContactCount(void)
{
- return m_contactCount;
+ MDBX_stat st;
+ mdbx_dbi_stat(m_txn_ro, m_dbContacts, &st, sizeof(st));
+ return st.ms_entries;
}
STDMETHODIMP_(LONG) CDbxMDBX::GetContactSize(void)
@@ -78,8 +80,6 @@ STDMETHODIMP_(LONG) CDbxMDBX::DeleteContact(MCONTACT contactID) // free cache item
m_cache->FreeCachedContact(contactID);
DBFlush();
-
- InterlockedDecrement(&m_contactCount);
return 0;
}
@@ -100,7 +100,7 @@ STDMETHODIMP_(MCONTACT) CDbxMDBX::AddContact() }
DBFlush();
- InterlockedIncrement(&m_contactCount);
+
NotifyEventHooks(hContactAddedEvent, dwContactId, 0);
return dwContactId;
}
diff --git a/plugins/Dbx_mdbx/src/dbintf.cpp b/plugins/Dbx_mdbx/src/dbintf.cpp index 00440fb425..fb3dbc1609 100644 --- a/plugins/Dbx_mdbx/src/dbintf.cpp +++ b/plugins/Dbx_mdbx/src/dbintf.cpp @@ -31,8 +31,10 @@ CDbxMDBX::CDbxMDBX(const TCHAR *tszFileName, int iMode) : {
m_tszProfileName = mir_wstrdup(tszFileName);
- m_hwndTimer = CreateWindowExW(0, L"STATIC", nullptr, 0, CW_USEDEFAULT, CW_USEDEFAULT, CW_USEDEFAULT, CW_USEDEFAULT, HWND_DESKTOP, nullptr, g_hInst, nullptr);
- ::SetWindowLongPtr(m_hwndTimer, GWLP_USERDATA, (LONG_PTR)this);
+ if (!m_bReadOnly) {
+ m_hwndTimer = CreateWindowExW(0, L"STATIC", nullptr, 0, CW_USEDEFAULT, CW_USEDEFAULT, CW_USEDEFAULT, CW_USEDEFAULT, HWND_DESKTOP, nullptr, g_hInst, nullptr);
+ ::SetWindowLongPtr(m_hwndTimer, GWLP_USERDATA, (LONG_PTR)this);
+ }
mdbx_env_create(&m_env);
mdbx_env_set_maxdbs(m_env, 10);
@@ -42,10 +44,13 @@ CDbxMDBX::CDbxMDBX(const TCHAR *tszFileName, int iMode) : CDbxMDBX::~CDbxMDBX()
{
- g_Dbs.remove(this);
mdbx_env_close(m_env);
- ::DestroyWindow(m_hwndTimer);
+ if (!m_bReadOnly)
+ TouchFile();
+
+ if (m_hwndTimer != nullptr)
+ ::DestroyWindow(m_hwndTimer);
DestroyServiceFunction(hService);
UnhookEvent(hHook);
@@ -65,16 +70,13 @@ CDbxMDBX::~CDbxMDBX() mir_free(m_tszProfileName);
}
-int CDbxMDBX::Load(bool bSkipInit)
+int CDbxMDBX::Load()
{
- if (Map() != MDBX_SUCCESS)
- return EGROKPRF_CANTREAD;
+ TouchFile();
- if (!bSkipInit) {
+ unsigned int defFlags = MDBX_CREATE;
+ {
txn_ptr trnlck(StartTran());
-
- unsigned int defFlags = MDBX_CREATE;
-
mdbx_dbi_open(trnlck, "global", defFlags | MDBX_INTEGERKEY, &m_dbGlobal);
mdbx_dbi_open(trnlck, "crypto", defFlags, &m_dbCrypto);
mdbx_dbi_open(trnlck, "contacts", defFlags | MDBX_INTEGERKEY, &m_dbContacts);
@@ -83,82 +85,67 @@ int CDbxMDBX::Load(bool bSkipInit) mdbx_dbi_open_ex(trnlck, "eventsrt", defFlags, &m_dbEventsSort, DBEventSortingKey::Compare, nullptr);
mdbx_dbi_open_ex(trnlck, "settings", defFlags, &m_dbSettings, DBSettingKey::Compare, nullptr);
- {
- uint32_t keyVal = 1;
- MDBX_val key = { &keyVal, sizeof(keyVal) }, data;
- if (mdbx_get(trnlck, m_dbGlobal, &key, &data) == MDBX_SUCCESS) {
- const DBHeader *hdr = (const DBHeader*)data.iov_base;
- if (hdr->dwSignature != DBHEADER_SIGNATURE)
- return EGROKPRF_DAMAGED;
- if (hdr->dwVersion != DBHEADER_VERSION)
- return EGROKPRF_OBSOLETE;
-
- m_header = *hdr;
- }
- else {
- m_header.dwSignature = DBHEADER_SIGNATURE;
- m_header.dwVersion = DBHEADER_VERSION;
- data.iov_base = &m_header; data.iov_len = sizeof(m_header);
- mdbx_put(trnlck, m_dbGlobal, &key, &data, 0);
- DBFlush();
- }
-
- keyVal = 2;
- if (mdbx_get(trnlck, m_dbGlobal, &key, &data) == MDBX_SUCCESS)
- m_ccDummy.dbc = *(const DBContact*)data.iov_base;
-
- trnlck.commit();
- }
- {
- MDBX_val key, val;
-
- mdbx_txn_begin(m_env, nullptr, MDBX_RDONLY, &m_txn_ro);
- mdbx_cursor_open(m_txn_ro, m_dbEvents, &m_curEvents);
- if (mdbx_cursor_get(m_curEvents, &key, &val, MDBX_LAST) == MDBX_SUCCESS)
- m_dwMaxEventId = *(MEVENT*)key.iov_base;
+ uint32_t keyVal = 1;
+ MDBX_val key = { &keyVal, sizeof(keyVal) }, data;
+ if (mdbx_get(trnlck, m_dbGlobal, &key, &data) == MDBX_SUCCESS) {
+ const DBHeader *hdr = (const DBHeader*)data.iov_base;
+ if (hdr->dwSignature != DBHEADER_SIGNATURE)
+ return EGROKPRF_DAMAGED;
+ if (hdr->dwVersion != DBHEADER_VERSION)
+ return EGROKPRF_OBSOLETE;
- mdbx_cursor_open(m_txn_ro, m_dbEventsSort, &m_curEventsSort);
- mdbx_cursor_open(m_txn_ro, m_dbSettings, &m_curSettings);
- mdbx_cursor_open(m_txn_ro, m_dbModules, &m_curModules);
-
- mdbx_cursor_open(m_txn_ro, m_dbContacts, &m_curContacts);
- if (mdbx_cursor_get(m_curContacts, &key, &val, MDBX_LAST) == MDBX_SUCCESS)
- m_maxContactId = *(MCONTACT*)key.iov_base;
-
- MDBX_stat st;
- mdbx_dbi_stat(m_txn_ro, m_dbContacts, &st, sizeof(st));
- m_contactCount = st.ms_entries;
-
- mdbx_txn_reset(m_txn_ro);
+ m_header = *hdr;
+ }
+ else {
+ m_header.dwSignature = DBHEADER_SIGNATURE;
+ m_header.dwVersion = DBHEADER_VERSION;
+ data.iov_base = &m_header; data.iov_len = sizeof(m_header);
+ mdbx_put(trnlck, m_dbGlobal, &key, &data, 0);
+ DBFlush();
}
+ keyVal = 2;
+ if (mdbx_get(trnlck, m_dbGlobal, &key, &data) == MDBX_SUCCESS)
+ m_ccDummy.dbc = *(const DBContact*)data.iov_base;
- if (InitModules()) return EGROKPRF_DAMAGED;
- if (InitCrypt()) return EGROKPRF_DAMAGED;
-
- // everything is ok, go on
- if (!m_bReadOnly) {
- // retrieve the event handles
- hContactDeletedEvent = CreateHookableEvent(ME_DB_CONTACT_DELETED);
- hContactAddedEvent = CreateHookableEvent(ME_DB_CONTACT_ADDED);
- hSettingChangeEvent = CreateHookableEvent(ME_DB_CONTACT_SETTINGCHANGED);
- hEventMarkedRead = CreateHookableEvent(ME_DB_EVENT_MARKED_READ);
-
- hEventAddedEvent = CreateHookableEvent(ME_DB_EVENT_ADDED);
- hEventDeletedEvent = CreateHookableEvent(ME_DB_EVENT_DELETED);
- hEventFilterAddedEvent = CreateHookableEvent(ME_DB_EVENT_FILTER_ADD);
- }
+ trnlck.commit();
+ }
- FillContacts();
+ mdbx_txn_begin(m_env, nullptr, MDBX_RDONLY, &m_txn_ro);
+ mdbx_cursor_open(m_txn_ro, m_dbEvents, &m_curEvents);
+ mdbx_cursor_open(m_txn_ro, m_dbEventsSort, &m_curEventsSort);
+ mdbx_cursor_open(m_txn_ro, m_dbSettings, &m_curSettings);
+ mdbx_cursor_open(m_txn_ro, m_dbModules, &m_curModules);
+ mdbx_cursor_open(m_txn_ro, m_dbContacts, &m_curContacts);
+
+ MDBX_val key, val;
+ if (mdbx_cursor_get(m_curEvents, &key, &val, MDBX_LAST) == MDBX_SUCCESS)
+ m_dwMaxEventId = *(MEVENT*)key.iov_base;
+ if (mdbx_cursor_get(m_curContacts, &key, &val, MDBX_LAST) == MDBX_SUCCESS)
+ m_maxContactId = *(MCONTACT*)key.iov_base;
+
+ mdbx_txn_reset(m_txn_ro);
+
+ if (InitModules()) return EGROKPRF_DAMAGED;
+ if (InitCrypt()) return EGROKPRF_DAMAGED;
+
+ // everything is ok, go on
+ if (!m_bReadOnly) {
+ // retrieve the event handles
+ hContactDeletedEvent = CreateHookableEvent(ME_DB_CONTACT_DELETED);
+ hContactAddedEvent = CreateHookableEvent(ME_DB_CONTACT_ADDED);
+ hSettingChangeEvent = CreateHookableEvent(ME_DB_CONTACT_SETTINGCHANGED);
+ hEventMarkedRead = CreateHookableEvent(ME_DB_EVENT_MARKED_READ);
+
+ hEventAddedEvent = CreateHookableEvent(ME_DB_EVENT_ADDED);
+ hEventDeletedEvent = CreateHookableEvent(ME_DB_EVENT_DELETED);
+ hEventFilterAddedEvent = CreateHookableEvent(ME_DB_EVENT_FILTER_ADD);
}
- return EGROKPRF_NOERROR;
-}
+ FillContacts();
-int CDbxMDBX::Create(void)
-{
- return (Map() == MDBX_SUCCESS) ? 0 : EGROKPRF_CANTREAD;
+ return EGROKPRF_NOERROR;
}
size_t iDefHeaderOffset = 0;
@@ -214,6 +201,23 @@ int CDbxMDBX::Map() /////////////////////////////////////////////////////////////////////////////////////////
+void CDbxMDBX::TouchFile()
+{
+ SYSTEMTIME st;
+ ::GetLocalTime(&st);
+
+ FILETIME ft;
+ SystemTimeToFileTime(&st, &ft);
+
+ HANDLE hFile = CreateFileW(m_tszProfileName, GENERIC_READ, FILE_SHARE_READ, nullptr, OPEN_EXISTING, FILE_ATTRIBUTE_NORMAL, nullptr);
+ if (hFile) {
+ SetFileTime(hFile, nullptr, &ft, &ft);
+ CloseHandle(hFile);
+ }
+}
+
+/////////////////////////////////////////////////////////////////////////////////////////
+
static VOID CALLBACK DoBufferFlushTimerProc(HWND hwnd, UINT, UINT_PTR idEvent, DWORD)
{
KillTimer(hwnd, idEvent);
diff --git a/plugins/Dbx_mdbx/src/dbintf.h b/plugins/Dbx_mdbx/src/dbintf.h index 9c42ea77f7..0b616916ed 100644 --- a/plugins/Dbx_mdbx/src/dbintf.h +++ b/plugins/Dbx_mdbx/src/dbintf.h @@ -144,8 +144,8 @@ class CDbxMDBX : public MDatabaseCommon, public MZeroedObject }
void FillContacts(void);
- int Map();
- int PrepareCheck();
+ int PrepareCheck(void);
+ void TouchFile(void);
void UpdateMenuItem(void);
////////////////////////////////////////////////////////////////////////////
@@ -179,7 +179,6 @@ class CDbxMDBX : public MDatabaseCommon, public MZeroedObject MDBX_dbi m_dbContacts;
MDBX_cursor *m_curContacts;
- uint32_t m_contactCount;
MCONTACT m_maxContactId;
void GatherContactHistory(MCONTACT hContact, LIST<EventItem> &items);
@@ -223,10 +222,10 @@ public: virtual ~CDbxMDBX();
int Check(void);
- int Create(void);
void DBFlush(bool bForce = false);
int EnableEncryption(bool bEnable);
- int Load(bool bSkipInit);
+ int Load();
+ int Map();
void StoreKey(void);
void SetPassword(const wchar_t *ptszPassword);
diff --git a/plugins/Dbx_mdbx/src/init.cpp b/plugins/Dbx_mdbx/src/init.cpp index 1fd64d825b..f90995b02c 100644 --- a/plugins/Dbx_mdbx/src/init.cpp +++ b/plugins/Dbx_mdbx/src/init.cpp @@ -43,15 +43,13 @@ static PLUGININFOEX pluginInfo = HINSTANCE g_hInst = nullptr;
-LIST<CDbxMDBX> g_Dbs(1, HandleKeySortT);
-
/////////////////////////////////////////////////////////////////////////////////////////
// returns 0 if the profile is created, EMKPRF*
static int makeDatabase(const TCHAR *profile)
{
std::unique_ptr<CDbxMDBX> db(new CDbxMDBX(profile, 0));
- return db->Create();
+ return (db->Map() == MDBX_SUCCESS) ? 0 : EGROKPRF_CANTREAD;
}
// returns 0 if the given profile has a valid header
@@ -68,10 +66,12 @@ static MDatabaseCommon* LoadDatabase(const TCHAR *profile, BOOL bReadOnly) mir_getLP(&pluginInfo);
std::unique_ptr<CDbxMDBX> db(new CDbxMDBX(profile, (bReadOnly) ? DBMODE_READONLY : 0));
- if (db->Load(false) != ERROR_SUCCESS)
+ if (db->Map() != ERROR_SUCCESS)
+ return nullptr;
+
+ if (db->Load() != ERROR_SUCCESS)
return nullptr;
- g_Dbs.insert(db.get());
return db.release();
}
diff --git a/plugins/Dbx_mdbx/src/stdafx.h b/plugins/Dbx_mdbx/src/stdafx.h index 4805e9833a..243756e39a 100644 --- a/plugins/Dbx_mdbx/src/stdafx.h +++ b/plugins/Dbx_mdbx/src/stdafx.h @@ -162,6 +162,5 @@ public: #include "version.h"
extern HINSTANCE g_hInst;
-extern LIST<CDbxMDBX> g_Dbs;
#include "ui.h"
|