From f9e33a21e734d927f224cf93fd7be05dce3f7391 Mon Sep 17 00:00:00 2001 From: George Hazan Date: Sun, 21 Dec 2014 15:04:18 +0000 Subject: - CDb3Mmap::FillContacts(): returned support for the old contact's format; - CDb3Mmap::AdaptEvent: fast enough method to unify old & new event's formats; - CDb3Mmap::m_csDbAccess finally switched to mir_cs; - code cleaning; - pre-release version bump git-svn-id: http://svn.miranda-ng.org/main/trunk@11556 1316c22d-e87f-b044-9b9b-93d7a3e3ba9c --- plugins/Db3x_mmap/src/database.cpp | 17 +++++------- plugins/Db3x_mmap/src/dbcontacts.cpp | 10 ++++--- plugins/Db3x_mmap/src/dbevents.cpp | 42 ++++++++++++++++++++--------- plugins/Db3x_mmap/src/dbintf.cpp | 20 ++++++-------- plugins/Db3x_mmap/src/dbintf.h | 13 ++++++--- plugins/Db3x_mmap/src/dbtool/eventchain.cpp | 18 ++++++------- plugins/Db3x_mmap/src/version.h | 4 +-- 7 files changed, 72 insertions(+), 52 deletions(-) diff --git a/plugins/Db3x_mmap/src/database.cpp b/plugins/Db3x_mmap/src/database.cpp index 8d4cddfb4c..c4d9788879 100644 --- a/plugins/Db3x_mmap/src/database.cpp +++ b/plugins/Db3x_mmap/src/database.cpp @@ -78,23 +78,20 @@ DWORD CDb3Mmap::ReallocSpace(DWORD ofs, int oldSize, int newSize) static DWORD DatabaseCorrupted = 0; static TCHAR *msg = NULL; static DWORD dwErr = 0; +static TCHAR tszPanic[] = LPGENT("Miranda has detected corruption in your database. This corruption may be fixed by DbChecker plugin. Please download it from http://miranda-ng.org/p/DbChecker/. Miranda will now shut down."); void __cdecl dbpanic(void *) { - if (msg) - { - TCHAR err[256]; - + if (msg) { if (dwErr == ERROR_DISK_FULL) msg = TranslateT("Disk is full. Miranda will now shut down."); + TCHAR err[256]; mir_sntprintf(err, SIZEOF(err), msg, TranslateT("Database failure. Miranda will now shut down."), dwErr); MessageBox(0, err, TranslateT("Database Error"), MB_SETFOREGROUND | MB_TOPMOST | MB_APPLMODAL | MB_ICONWARNING | MB_OK); } - else - MessageBox(0, TranslateT("Miranda has detected corruption in your database. This corruption may be fixed by DbChecker plugin. Please download it from http://miranda-ng.org/p/DbChecker/. Miranda will now shut down."), - TranslateT("Database Panic"), MB_SETFOREGROUND | MB_TOPMOST | MB_APPLMODAL | MB_ICONWARNING | MB_OK); + else MessageBox(0, TranslateTS(tszPanic), TranslateT("Database Panic"), MB_SETFOREGROUND | MB_TOPMOST | MB_APPLMODAL | MB_ICONWARNING | MB_OK); TerminateProcess(GetCurrentProcess(), 255); } @@ -102,7 +99,7 @@ void CDb3Mmap::DatabaseCorruption(TCHAR *text) { int kill = 0; - EnterCriticalSection(&m_csDbAccess); + mir_cslockfull lck(m_csDbAccess); if (DatabaseCorrupted == 0) { DatabaseCorrupted++; kill++; @@ -112,11 +109,11 @@ void CDb3Mmap::DatabaseCorruption(TCHAR *text) else { /* db is already corrupted, someone else is dealing with it, wait here so that we don't do any more damage */ - LeaveCriticalSection(&m_csDbAccess); Sleep(INFINITE); return; } - LeaveCriticalSection(&m_csDbAccess); + lck.unlock(); + if (kill) { _beginthread(dbpanic, 0, NULL); Sleep(INFINITE); diff --git a/plugins/Db3x_mmap/src/dbcontacts.cpp b/plugins/Db3x_mmap/src/dbcontacts.cpp index f2ed1da4f1..c497b10214 100644 --- a/plugins/Db3x_mmap/src/dbcontacts.cpp +++ b/plugins/Db3x_mmap/src/dbcontacts.cpp @@ -400,9 +400,13 @@ void CDb3Mmap::FillContacts() if (p->signature != DBCONTACT_SIGNATURE) break; - DWORD dwContactID = p->dwContactID; - if (dwContactID >= m_dwMaxContactId) - m_dwMaxContactId = dwContactID + 1; + DWORD dwContactID; + if (m_dbHeader.version >= DB_095_VERSION) { + dwContactID = p->dwContactID; + if (dwContactID > m_dwMaxContactId) + m_dwMaxContactId = dwContactID + 1; + } + else dwContactID = m_dwMaxContactId++; DBCachedContact *cc = m_cache->AddContactToCache(dwContactID); cc->dwDriverData = dwOffset; diff --git a/plugins/Db3x_mmap/src/dbevents.cpp b/plugins/Db3x_mmap/src/dbevents.cpp index 0283a93c45..978e051d6f 100644 --- a/plugins/Db3x_mmap/src/dbevents.cpp +++ b/plugins/Db3x_mmap/src/dbevents.cpp @@ -78,7 +78,6 @@ STDMETHODIMP_(HANDLE) CDb3Mmap::AddEvent(MCONTACT contactID, DBEVENTINFO *dbei) } } - bool neednotify; mir_cslockfull lck(m_csDbAccess); DWORD ofsContact = GetContactOffset(contactID); @@ -134,6 +133,7 @@ STDMETHODIMP_(HANDLE) CDb3Mmap::AddEvent(MCONTACT contactID, DBEVENTINFO *dbei) } dbc.eventCount++; + bool neednotify; if (!(dbe.flags & (DBEF_READ | DBEF_SENT))) { if (dbe.timestamp < dbc.tsFirstUnread || dbc.tsFirstUnread == 0) { dbc.tsFirstUnread = dbe.timestamp; @@ -261,7 +261,7 @@ STDMETHODIMP_(BOOL) CDb3Mmap::DeleteEvent(MCONTACT contactID, HANDLE hDbEvent) STDMETHODIMP_(LONG) CDb3Mmap::GetBlobSize(HANDLE hDbEvent) { mir_cslock lck(m_csDbAccess); - DBEvent *dbe = (DBEvent*)DBRead((DWORD)hDbEvent, NULL); + DBEvent *dbe = AdaptEvent((DWORD)hDbEvent, 0); return (dbe->signature != DBEVENT_SIGNATURE) ? -1 : dbe->cbBlob; } @@ -274,7 +274,7 @@ STDMETHODIMP_(BOOL) CDb3Mmap::GetEvent(HANDLE hDbEvent, DBEVENTINFO *dbei) } mir_cslock lck(m_csDbAccess); - DBEvent *dbe = (DBEvent*)DBRead((DWORD)hDbEvent, NULL); + DBEvent *dbe = AdaptEvent((DWORD)hDbEvent, 0); if (dbe->signature != DBEVENT_SIGNATURE) return 1; @@ -285,7 +285,11 @@ STDMETHODIMP_(BOOL) CDb3Mmap::GetEvent(HANDLE hDbEvent, DBEVENTINFO *dbei) int bytesToCopy = (dbei->cbBlob < dbe->cbBlob) ? dbei->cbBlob : dbe->cbBlob; dbei->cbBlob = dbe->cbBlob; if (bytesToCopy && dbei->pBlob) { - BYTE *pSrc = DBRead(DWORD(hDbEvent) + offsetof(DBEvent, blob), NULL); + BYTE *pSrc; + if (m_dbHeader.version >= DB_095_1_VERSION) + pSrc = DBRead(DWORD(hDbEvent) + offsetof(DBEvent, blob), NULL); + else + pSrc = DBRead(DWORD(hDbEvent) + offsetof(DBEvent_094, blob), NULL); if (dbe->flags & DBEF_ENCRYPTED) { dbei->flags &= ~DBEF_ENCRYPTED; size_t len; @@ -298,7 +302,7 @@ STDMETHODIMP_(BOOL) CDb3Mmap::GetEvent(HANDLE hDbEvent, DBEVENTINFO *dbei) memset(dbei->pBlob + len, 0, bytesToCopy - len); mir_free(pBlob); } - else memmove(dbei->pBlob, pSrc, bytesToCopy); + else memcpy(dbei->pBlob, pSrc, bytesToCopy); } return 0; } @@ -356,7 +360,7 @@ STDMETHODIMP_(BOOL) CDb3Mmap::MarkEventRead(MCONTACT contactID, HANDLE hDbEvent) STDMETHODIMP_(MCONTACT) CDb3Mmap::GetEventContact(HANDLE hDbEvent) { mir_cslock lck(m_csDbAccess); - DBEvent *dbe = (DBEvent*)DBRead((DWORD)hDbEvent, NULL); + DBEvent *dbe = AdaptEvent((DWORD)hDbEvent, INVALID_CONTACT_ID); return (dbe->signature != DBEVENT_SIGNATURE) ? INVALID_CONTACT_ID : dbe->contactID; } @@ -379,7 +383,7 @@ STDMETHODIMP_(HANDLE) CDb3Mmap::FindFirstEvent(MCONTACT contactID) return NULL; for (DWORD dwOffset = dbc->ofsFirstEvent; dwOffset != 0;) { - DBEvent *dbe = (DBEvent*)DBRead(dwOffset, NULL); + DBEvent *dbe = AdaptEvent(dwOffset, contactID); if (dbe->signature != DBEVENT_SIGNATURE) return NULL; if (dbe->contactID == contactID) @@ -408,7 +412,7 @@ STDMETHODIMP_(HANDLE) CDb3Mmap::FindFirstUnreadEvent(MCONTACT contactID) return NULL; for (DWORD dwOffset = dbc->ofsFirstUnread; dwOffset != 0;) { - DBEvent *dbe = (DBEvent*)DBRead(dwOffset, NULL); + DBEvent *dbe = AdaptEvent(dwOffset, contactID); if (dbe->signature != DBEVENT_SIGNATURE) return NULL; if (dbe->contactID == contactID && !dbe->markedRead()) @@ -437,7 +441,7 @@ STDMETHODIMP_(HANDLE) CDb3Mmap::FindLastEvent(MCONTACT contactID) return NULL; for (DWORD dwOffset = dbc->ofsLastEvent; dwOffset != 0;) { - DBEvent *dbe = (DBEvent*)DBRead(dwOffset, NULL); + DBEvent *dbe = AdaptEvent(dwOffset, contactID); if (dbe->signature != DBEVENT_SIGNATURE) return NULL; if (dbe->contactID == contactID) @@ -452,14 +456,14 @@ STDMETHODIMP_(HANDLE) CDb3Mmap::FindNextEvent(MCONTACT contactID, HANDLE hDbEven DBCachedContact *cc = (contactID) ? m_cache->GetCachedContact(contactID) : NULL; mir_cslock lck(m_csDbAccess); - DBEvent *dbe = (DBEvent*)DBRead((DWORD)hDbEvent, NULL); + DBEvent *dbe = AdaptEvent((DWORD)hDbEvent, contactID); if (dbe->signature != DBEVENT_SIGNATURE) return NULL; if (!cc || !cc->IsSub()) return HANDLE(dbe->ofsNext); for (DWORD dwOffset = dbe->ofsNext; dwOffset != 0;) { - dbe = (DBEvent*)DBRead(dwOffset, NULL); + dbe = AdaptEvent(dwOffset, contactID); if (dbe->signature != DBEVENT_SIGNATURE) return NULL; if (dbe->contactID == contactID) @@ -474,14 +478,14 @@ STDMETHODIMP_(HANDLE) CDb3Mmap::FindPrevEvent(MCONTACT contactID, HANDLE hDbEven DBCachedContact *cc = (contactID) ? m_cache->GetCachedContact(contactID) : NULL; mir_cslock lck(m_csDbAccess); - DBEvent *dbe = (DBEvent*)DBRead((DWORD)hDbEvent, NULL); + DBEvent *dbe = AdaptEvent((DWORD)hDbEvent, contactID); if (dbe->signature != DBEVENT_SIGNATURE) return NULL; if (!cc || !cc->IsSub()) return HANDLE(dbe->ofsPrev); for (DWORD dwOffset = dbe->ofsPrev; dwOffset != 0;) { - dbe = (DBEvent*)DBRead(dwOffset, NULL); + dbe = AdaptEvent(dwOffset, contactID); if (dbe->signature != DBEVENT_SIGNATURE) return NULL; if (dbe->contactID == contactID) @@ -491,6 +495,18 @@ STDMETHODIMP_(HANDLE) CDb3Mmap::FindPrevEvent(MCONTACT contactID, HANDLE hDbEven return NULL; } +DBEvent* CDb3Mmap::AdaptEvent(DWORD ofs, DWORD dwContactID) +{ + if (m_dbHeader.version >= DB_095_1_VERSION) + return (DBEvent*)DBRead(ofs, NULL); + + DBEvent_094 *pOldEvent = (DBEvent_094*)DBRead(ofs, NULL); + m_tmpEvent.signature = pOldEvent->signature; + m_tmpEvent.contactID = dwContactID; + memcpy(&m_tmpEvent.ofsPrev, &pOldEvent->ofsPrev, sizeof(DBEvent_094) - sizeof(DWORD)); + return &m_tmpEvent; +} + ///////////////////////////////////////////////////////////////////////////////////////// // low-level history cleaner diff --git a/plugins/Db3x_mmap/src/dbintf.cpp b/plugins/Db3x_mmap/src/dbintf.cpp index ef2106f5cb..8f44ca98b2 100644 --- a/plugins/Db3x_mmap/src/dbintf.cpp +++ b/plugins/Db3x_mmap/src/dbintf.cpp @@ -45,20 +45,18 @@ static int stringCompare2(const char *p1, const char *p2) } CDb3Mmap::CDb3Mmap(const TCHAR *tszFileName, int iMode) : -m_hDbFile(INVALID_HANDLE_VALUE), -m_safetyMode(true), -m_bReadOnly((iMode & DBMODE_READONLY) != 0), -m_bShared((iMode & DBMODE_SHARED) != 0), -m_dwMaxContactId(1), -m_lMods(50, ModCompare), -m_lOfs(50, OfsCompare), -m_lResidentSettings(50, stringCompare2) + m_hDbFile(INVALID_HANDLE_VALUE), + m_safetyMode(true), + m_bReadOnly((iMode & DBMODE_READONLY) != 0), + m_bShared((iMode & DBMODE_SHARED) != 0), + m_dwMaxContactId(1), + m_lMods(50, ModCompare), + m_lOfs(50, OfsCompare), + m_lResidentSettings(50, stringCompare2) { m_tszProfileName = mir_tstrdup(tszFileName); InitDbInstance(this); - InitializeCriticalSection(&m_csDbAccess); - SYSTEM_INFO sinf; GetSystemInfo(&sinf); m_ChunkSize = sinf.dwAllocationGranularity; @@ -107,8 +105,6 @@ CDb3Mmap::~CDb3Mmap() DestroyHookableEvent(hEventDeletedEvent); DestroyHookableEvent(hEventFilterAddedEvent); - DeleteCriticalSection(&m_csDbAccess); - DestroyDbInstance(this); mir_free(m_tszProfileName); diff --git a/plugins/Db3x_mmap/src/dbintf.h b/plugins/Db3x_mmap/src/dbintf.h index fc75cc2c31..9dcde4bca1 100644 --- a/plugins/Db3x_mmap/src/dbintf.h +++ b/plugins/Db3x_mmap/src/dbintf.h @@ -293,13 +293,12 @@ protected: DWORD m_dwFileSize, m_dwMaxContactId; HANDLE hSettingChangeEvent, hContactDeletedEvent, hContactAddedEvent, hEventMarkedRead; - CRITICAL_SECTION m_csDbAccess; + mir_cs m_csDbAccess; int CheckProto(DBCachedContact *cc, const char *proto); DWORD CreateNewSpace(int bytes); void DeleteSpace(DWORD ofs, int bytes); DWORD ReallocSpace(DWORD ofs, int oldSize, int newSize); - DWORD GetContactOffset(MCONTACT contactID, DBCachedContact **cc = NULL); //////////////////////////////////////////////////////////////////////////// // settings @@ -311,6 +310,14 @@ protected: // contacts int WipeContactHistory(DBContact *dbc); + DWORD GetContactOffset(MCONTACT contactID, DBCachedContact **cc = NULL); + + //////////////////////////////////////////////////////////////////////////// + // events + + DBEvent m_tmpEvent; + + DBEvent* AdaptEvent(DWORD offset, DWORD hContact); //////////////////////////////////////////////////////////////////////////// // modules @@ -346,7 +353,7 @@ protected: DWORD WriteSegment(DWORD ofs, PVOID buf, int cbBytes); DWORD WriteEvent(DBEvent *dbe); - DWORD PeekEvent(DWORD ofs, DWORD dwContactID, DBEvent *dbe); + DWORD PeekEvent(DWORD ofs, DWORD dwContactID, DBEvent &dbe); void WriteOfsNextToPrevious(DWORD ofsPrev, DBContact *dbc, DWORD ofsNext); void FinishUp(DWORD ofsLast, DBContact *dbc); diff --git a/plugins/Db3x_mmap/src/dbtool/eventchain.cpp b/plugins/Db3x_mmap/src/dbtool/eventchain.cpp index 1b0ea041be..67a9a613ac 100644 --- a/plugins/Db3x_mmap/src/dbtool/eventchain.cpp +++ b/plugins/Db3x_mmap/src/dbtool/eventchain.cpp @@ -102,19 +102,19 @@ void CDb3Mmap::FinishUp(DWORD ofsLast, DBContact *dbc) } } -DWORD CDb3Mmap::PeekEvent(DWORD ofs, DWORD dwContactID, DBEvent *dbe) +DWORD CDb3Mmap::PeekEvent(DWORD ofs, DWORD dwContactID, DBEvent &dbe) { - if (m_dbHeader.version == DB_095_1_VERSION) - return PeekSegment(ofs, dbe, sizeof(DBEvent)); + if (m_dbHeader.version >= DB_095_1_VERSION) + return PeekSegment(ofs, &dbe, sizeof(DBEvent)); DBEvent_094 oldEvent; DWORD ret = PeekSegment(ofs, &oldEvent, sizeof(oldEvent)); if (ret != ERROR_SUCCESS) return ret; - dbe->signature = oldEvent.signature; - dbe->contactID = dwContactID; - memcpy(&dbe->ofsPrev, &oldEvent.ofsPrev, sizeof(oldEvent) - sizeof(DWORD)); + dbe.signature = oldEvent.signature; + dbe.contactID = dwContactID; + memcpy(&dbe.ofsPrev, &oldEvent.ofsPrev, sizeof(oldEvent) - sizeof(DWORD)); return ERROR_SUCCESS; } @@ -166,7 +166,7 @@ int CDb3Mmap::WorkEventChain(DWORD ofsContact, DBContact *dbc, int firstTime) if (!backLookup && ofsTmp) { backLookup = 1; while (SignatureValid(ofsTmp, DBEVENT_SIGNATURE)) { - if (PeekEvent(ofsTmp, dbc->dwContactID, &dbeOld) != ERROR_SUCCESS) + if (PeekEvent(ofsTmp, dbc->dwContactID, dbeOld) != ERROR_SUCCESS) break; ofsNew = ofsTmp; ofsTmp = dbeOld.ofsPrev; @@ -183,7 +183,7 @@ int CDb3Mmap::WorkEventChain(DWORD ofsContact, DBContact *dbc, int firstTime) } } - if (PeekEvent(ofsThisEvent, dbc->dwContactID, &dbeOld) != ERROR_SUCCESS) { + if (PeekEvent(ofsThisEvent, dbc->dwContactID, dbeOld) != ERROR_SUCCESS) { FinishUp(ofsDestPrevEvent, dbc); return ERROR_NO_MORE_ITEMS; } @@ -292,7 +292,7 @@ int CDb3Mmap::WorkEventChain(DWORD ofsContact, DBContact *dbc, int firstTime) if (cb->bCheckOnly) { if (!cb->bAggressive) { ofsTmp = dbeOld.ofsPrev; - while (PeekEvent(ofsTmp, dbc->dwContactID, &dbeTmp) == ERROR_SUCCESS) { + while (PeekEvent(ofsTmp, dbc->dwContactID, dbeTmp) == ERROR_SUCCESS) { if (dbeTmp.ofsPrev == ofsContact) { found = 1; break; diff --git a/plugins/Db3x_mmap/src/version.h b/plugins/Db3x_mmap/src/version.h index 0e20cc7daf..d88e5aee74 100644 --- a/plugins/Db3x_mmap/src/version.h +++ b/plugins/Db3x_mmap/src/version.h @@ -1,7 +1,7 @@ #define __MAJOR_VERSION 0 #define __MINOR_VERSION 95 -#define __RELEASE_NUM 1 -#define __BUILD_NUM 3 +#define __RELEASE_NUM 3 +#define __BUILD_NUM 1 #include -- cgit v1.2.3