summaryrefslogtreecommitdiff
path: root/plugins/Dbx_mdbx/src/dbcheck.cpp
blob: 527e0dd9bbf260216e0d307d6e7618b9f2736497 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
/*

Miranda NG: the free IM client for Microsoft* Windows*

Copyright (C) 2012-20 Miranda NG team (https://miranda-ng.org)
all portions of this codebase are copyrighted to the people
listed in contributors.txt.

This program is free software; you can redistribute it and/or
modify it under the terms of the GNU General Public License
as published by the Free Software Foundation; either version 2
of the License, or (at your option) any later version.

This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
GNU General Public License for more details.

You should have received a copy of the GNU General Public License
along with this program; if not, write to the Free Software
Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.
*/

#include "stdafx.h"

/////////////////////////////////////////////////////////////////////////////////////////
// we are tracing EventsSort table to verify that they have correct event ids

int CDbxMDBX::CheckEvents1(void)
{
	txn_ptr trnlck(StartTran());
	cursor_ptr cursor(trnlck, m_dbEventsSort);

	MDBX_val key, data;
	for (int ret = mdbx_cursor_get(cursor, &key, &data, MDBX_FIRST); ret == MDBX_SUCCESS; ret = mdbx_cursor_get(cursor, &key, &data, MDBX_NEXT)) {
		auto *pData = (DBEventSortingKey *)key.iov_base;

		// if that's not a member of system event, check contact's existence first
		if (pData->hContact != 0) {
			auto *cc = m_cache->GetCachedContact(pData->hContact);
			if (cc == nullptr) {
				mdbx_cursor_del(cursor, 0);
				cb->pfnAddLogMessage(STATUS_ERROR, CMStringW(FORMAT, TranslateT("Orphaned sorting event with wrong contact ID %d, deleting"), pData->hContact));
				continue;
			}
		}

		if (GetBlobSize(pData->hEvent) == -1) {
			mdbx_cursor_del(cursor, 0);
			cb->pfnAddLogMessage(STATUS_ERROR, CMStringW(FORMAT, TranslateT("Orphaned sorting event with wrong event ID %d:%08X, deleting"), pData->hContact, pData->hEvent));
			continue;
		}
	}

	trnlck.commit();
	return 0;
}

/////////////////////////////////////////////////////////////////////////////////////////
// we are tracing EventId table to verify that they have correct event ids

int CDbxMDBX::CheckEvents2(void)
{
	txn_ptr trnlck(StartTran());
	cursor_ptr cursor(trnlck, m_dbEventIds);

	MDBX_val key, data;
	for (int ret = mdbx_cursor_get(cursor, &key, &data, MDBX_FIRST); ret == MDBX_SUCCESS; ret = mdbx_cursor_get(cursor, &key, &data, MDBX_NEXT)) {
		MEVENT hDbEvent = *(MEVENT *)data.iov_base;
		if (GetBlobSize(hDbEvent) == -1) {
			mdbx_cursor_del(cursor, 0);
			cb->pfnAddLogMessage(STATUS_ERROR, CMStringW(FORMAT, TranslateT("Orphaned event id with wrong event ID %08X, deleting"), hDbEvent));
			continue;
		}
	}

	trnlck.commit();
	return 0;
}

/////////////////////////////////////////////////////////////////////////////////////////
// we are tracing Settings table to verify that they have correct contact ids

int CDbxMDBX::CheckEvents3(void)
{
	txn_ptr trnlck(StartTran());
	cursor_ptr cursor(trnlck, m_dbSettings);

	MDBX_val key, data;
	for (int ret = mdbx_cursor_get(cursor, &key, &data, MDBX_FIRST); ret == MDBX_SUCCESS; ret = mdbx_cursor_get(cursor, &key, &data, MDBX_NEXT)) {
		auto *pKey = (DBSettingKey *)key.iov_base;

		if (pKey->hContact) {
			auto *cc = m_cache->GetCachedContact(pKey->hContact);
			if (cc == nullptr) {
				mdbx_cursor_del(cursor, 0);
				cb->pfnAddLogMessage(STATUS_ERROR, CMStringW(FORMAT, TranslateT("Orphaned setting with wrong contact ID %08X, deleting"), pKey->hContact));
				continue;
			}
		}
	}

	trnlck.commit();
	return 0;
}