summaryrefslogtreecommitdiff
path: root/plugins/Db3x_mmap/dbintf.cpp
blob: 831b147ea5f685ba8d6fd8510c12116d3c4841d3 (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
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
/*

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

Copyright 2012 Miranda NG project,
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 "commonheaders.h"

static int stringCompare(const char* p1, const char* p2)
{
	return strcmp(p1+1, p2+1);
}

static int stringCompare2(const char* p1, const char* p2)
{
	return strcmp(p1, p2);
}

static int compareGlobals(const DBCachedGlobalValue* p1, const DBCachedGlobalValue* p2)
{
	return strcmp(p1->name, p2->name);
}

static int ModCompare(const ModuleName *mn1, const ModuleName *mn2 )
{
	return strcmp( mn1->name, mn2->name );
}

static int OfsCompare(const ModuleName *mn1, const ModuleName *mn2 )
{
	return ( mn1->ofs - mn2->ofs );
}

CDdxMmap::CDdxMmap(const TCHAR* tszFileName) :
	m_hDbFile(INVALID_HANDLE_VALUE),
	m_safetyMode(TRUE),
	m_lSettings(100, stringCompare),
	m_lContacts(50, LIST<DBCachedContactValueList>::FTSortFunc(HandleKeySort)),
	m_lGlobalSettings(50, compareGlobals),
	m_lResidentSettings(50, stringCompare2),
	m_lMods(50, ModCompare),
	m_lOfs(50, OfsCompare)
{
	m_tszProfileName = mir_tstrdup(tszFileName);

	InitializeCriticalSection(&m_csDbAccess);

	SYSTEM_INFO sinf;
	GetSystemInfo(&sinf);
	m_ChunkSize = sinf.dwAllocationGranularity;

	m_codePage = CallService(MS_LANGPACK_GETCODEPAGE, 0, 0);
	m_hCacheHeap = HeapCreate(0, 0, 0);
	m_hModHeap = HeapCreate(0,0,0);

	hSettingChangeEvent = CreateHookableEvent(ME_DB_CONTACT_SETTINGCHANGED);
	hEventAddedEvent = CreateHookableEvent(ME_DB_EVENT_ADDED);
	hEventDeletedEvent = CreateHookableEvent(ME_DB_EVENT_DELETED);
	hEventFilterAddedEvent = CreateHookableEvent(ME_DB_EVENT_FILTER_ADD);
}

CDdxMmap::~CDdxMmap()
{
	// destroy settings
	HeapDestroy(m_hCacheHeap);
	m_lContacts.destroy();
	m_lSettings.destroy();
	m_lGlobalSettings.destroy();
	m_lResidentSettings.destroy();

	// destroy modules
	HeapDestroy(m_hModHeap);
	m_lMods.destroy();
	m_lOfs.destroy();

	// destroy map
	KillTimer(NULL,m_flushBuffersTimerId);
	if (m_pDbCache) {
		FlushViewOfFile(m_pDbCache, 0);
		UnmapViewOfFile(m_pDbCache);
	}
	if (m_hMap)
		CloseHandle(m_hMap);
	if (m_pNull)
		free(m_pNull);

	// update profile last modified time
	DWORD bytesWritten;
	SetFilePointer(m_hDbFile,0,NULL,FILE_BEGIN);
	WriteFile(m_hDbFile,&dbSignature,1,&bytesWritten,NULL);
	CloseHandle(m_hDbFile);

	DeleteCriticalSection(&m_csDbAccess);

	mir_free(m_tszProfileName);
}

int CDdxMmap::Load(bool bSkipInit)
{
	log0("DB logging running");
	
	DWORD dummy = 0;
	m_hDbFile = CreateFile(m_tszProfileName, GENERIC_READ | GENERIC_WRITE, FILE_SHARE_READ, NULL, OPEN_ALWAYS, 0, NULL);
	if ( m_hDbFile == INVALID_HANDLE_VALUE )
		return 1;

	if ( !ReadFile(m_hDbFile,&m_dbHeader,sizeof(m_dbHeader),&dummy,NULL)) {
		CloseHandle(m_hDbFile);
		return 1;
	}

	if ( !bSkipInit) {
		if (InitCache()) return 1;
		if (InitModuleNames()) return 1;
	}
	return 0;
}

int CDdxMmap::Create()
{
	m_hDbFile = CreateFile(m_tszProfileName, GENERIC_READ | GENERIC_WRITE, FILE_SHARE_READ, NULL, CREATE_ALWAYS, 0, NULL);
	return ( m_hDbFile == INVALID_HANDLE_VALUE );
}

STDMETHODIMP_(void) CDdxMmap::SetCacheSafetyMode(BOOL bIsSet)
{
	{	mir_cslock lck(m_csDbAccess);
		m_safetyMode = bIsSet;
	}
	DBFlush(1);
}