summaryrefslogtreecommitdiff
path: root/plugins/Dbx_tree/FileAccess.h
blob: f49643e2a3f3ba1888d8fb7e87cedd496b76e5cf (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
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
/*

dbx_tree: tree database driver for Miranda IM

Copyright 2007-2010 Michael "Protogenes" Kunz,

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.

*/

#pragma once

#include <windows.h>
#include <time.h>
#include "stdint.h"
#include "sigslot.h"

class CFileAccess
{
public:
	static const uint8_t cJournalSignature[20];

	CFileAccess(const TCHAR* FileName);
	virtual ~CFileAccess();


	uint32_t Read(void* Buf, uint32_t Source, uint32_t Size)
		{
			return _Read(Buf, Source, Size);
		};

	bool Write(void* Buf, uint32_t Dest, uint32_t Size)
		{
			if (m_Journal.Use)
			{
				DWORD written;

				TJournalEntry * data = reinterpret_cast<TJournalEntry*>(m_Journal.Buffer + m_Journal.BufUse);
				data->Signature = 'writ';
				data->Address = Dest;
				data->Size = Size;
				m_Journal.BufUse += 12;
				if (Size + m_Journal.BufUse < sizeof(m_Journal.Buffer) - 12) // one journal header has always to fit in
				{
					memcpy(m_Journal.Buffer + m_Journal.BufUse, Buf, Size);
					m_Journal.BufUse += Size;
				} else {
					WriteFile(m_Journal.hFile, m_Journal.Buffer, m_Journal.BufUse, &written, NULL);
					WriteFile(m_Journal.hFile, Buf, Size, &written, NULL);

					m_Journal.BufUse = 0;
				}
			} else {
				_Write(Buf, Dest, Size);
			}

			return true;
		};

	void Invalidate(uint32_t Dest, uint32_t Size)
		{
			if (m_Journal.Use)
			{
				DWORD written;

				TJournalEntry * data = reinterpret_cast<TJournalEntry*>(m_Journal.Buffer + m_Journal.BufUse);
				data->Signature = 'inva';
				data->Address = Dest;
				data->Size = Size;
				m_Journal.BufUse += 12;
				if (m_Journal.BufUse > sizeof(m_Journal.Buffer) - 12)
				{
					WriteFile(m_Journal.hFile, m_Journal.Buffer, m_Journal.BufUse, &written, NULL);
					m_Journal.BufUse = 0;
				}
			} else {
				_Invalidate(Dest, Size);
			}
		};

	void Flush()
		{
			if (m_Journal.Use)
			{
				if (m_Journal.BufUse)
				{
					DWORD written;
					WriteFile(m_Journal.hFile, m_Journal.Buffer, m_Journal.BufUse, &written, NULL);
					m_Journal.BufUse = 0;
				}
				FlushFileBuffers(m_Journal.hFile);
			} else {
				_Flush();
			}
		};

	void UseJournal(bool UseIt)
		{
			m_Journal.Use = UseIt;
		};

	void CompleteTransaction()
		{
			if (m_Size != m_LastSize)
			{
				m_sigFileSizeChanged.emit(this, m_Size);
				m_LastSize = m_Size;
			}
		};
	void CloseTransaction()
		{
			if (m_Journal.Use)
			{
				DWORD written;

				TJournalEntry * data = reinterpret_cast<TJournalEntry*>(m_Journal.Buffer + m_Journal.BufUse);
				data->Signature = 'fini';
				data->Address = 0;
				data->Size = m_Size;
				
				WriteFile(m_Journal.hFile, m_Journal.Buffer, m_Journal.BufUse + 12, &written, NULL);
				m_Journal.BufUse = 0;
			}
		};

	void CleanJournal();

	uint32_t Size(uint32_t NewSize);
	uint32_t Size()
		{	return m_Size; };
	void ReadOnly(bool ReadOnly)
		{	m_ReadOnly = ReadOnly; };
	bool ReadOnly()
		{	return m_ReadOnly; };

	typedef sigslot::signal2<CFileAccess *, uint32_t> TOnFileSizeChanged;

	TOnFileSizeChanged & sigFileSizeChanged()
	{	return m_sigFileSizeChanged; };

protected:
	TCHAR* m_FileName;
	struct {
		bool Use;
		TCHAR* FileName;
		HANDLE hFile;

		uint8_t Buffer[4096];
		uint32_t BufUse;
	} m_Journal;

	uint32_t m_Size;
	uint32_t m_AllocSize;
	uint32_t m_AllocGranularity;
	uint32_t m_MinAllocGranularity;
	uint32_t m_MaxAllocGranularity;
	uint32_t m_LastAllocTime;
	bool     m_ReadOnly;
	uint32_t m_LastSize;

	TOnFileSizeChanged m_sigFileSizeChanged;
	virtual uint32_t _Read(void* Buf, uint32_t Source, uint32_t Size) = 0;
  virtual uint32_t _Write(void* Buf, uint32_t Dest, uint32_t Size) = 0;
	virtual void     _Invalidate(uint32_t Dest, uint32_t Size) = 0;
	virtual uint32_t _SetSize(uint32_t Size) = 0;
	virtual void     _Flush() = 0;

#pragma pack (push, 1)
	typedef struct TJournalEntry
	{
		uint32_t Signature;
		uint32_t Address;
		uint32_t Size;
	} TJournalEntry;
#pragma pack (pop)

	void InitJournal();
	void ProcessJournal();
};