summaryrefslogtreecommitdiff
path: root/plugins/Dbx_mdbx/src/dbcheck.cpp
blob: 48449d360b76caa251c4dd4effdf1fc164a0b5f9 (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
106
107
108
109
110
111
112
113
114
115
116
117
/*

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

Copyright (C) 2012-23 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(this);
	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, MDBX_UPSERT);
				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, MDBX_UPSERT);
			cb->pfnAddLogMessage(STATUS_ERROR, CMStringW(FORMAT, TranslateT("Orphaned sorting event with wrong event ID %d:%08X, deleting"), pData->hContact, pData->hEvent));
			continue;
		}
	}

	return 0;
}

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

int CDbxMDBX::CheckEvents2(void)
{
	txn_ptr trnlck(this);
	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, MDBX_UPSERT);
			cb->pfnAddLogMessage(STATUS_ERROR, CMStringW(FORMAT, TranslateT("Orphaned event id with wrong event ID %08X, deleting"), hDbEvent));
			continue;
		}
	}

	return 0;
}

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

int CDbxMDBX::CheckEvents3(void)
{
	txn_ptr trnlck(this);
	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, MDBX_UPSERT);
				cb->pfnAddLogMessage(STATUS_ERROR, CMStringW(FORMAT, TranslateT("Orphaned setting with wrong contact ID %08X, deleting"), pKey->hContact));
				continue;
			}
		}
	}

	return 0;
}


///////////////////////////////////////////////////////////////////////////////
// MIDatabaseChecker

int CDbxMDBX::CheckDb(int phase)
{
	switch (phase) {
	case 0: return CheckEvents1();
	case 1: return CheckEvents2();
	case 2: return CheckEvents3();
	}

	return ERROR_OUT_OF_PAPER;
}