summaryrefslogtreecommitdiff
path: root/lib/bbackupd/BackupClientInodeToIDMap.cpp
blob: 6eaf739477ffd1710096596c0a97fc1495587cec (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
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
// --------------------------------------------------------------------------
//
// File
//		Name:    BackupClientInodeToIDMap.cpp
//		Purpose: Map of inode numbers to file IDs on the store
//		Created: 11/11/03
//
// --------------------------------------------------------------------------

#include "Box.h"

#include <stdlib.h>
#include <depot.h>

#define BACKIPCLIENTINODETOIDMAP_IMPLEMENTATION
#include "BackupClientInodeToIDMap.h"
#undef BACKIPCLIENTINODETOIDMAP_IMPLEMENTATION

#include "Archive.h"
#include "BackupStoreException.h"
#include "CollectInBufferStream.h"
#include "MemBlockStream.h"
#include "autogen_CommonException.h"

#include "MemLeakFindOn.h"

#define BOX_DBM_INODE_DB_VERSION_KEY "BackupClientInodeToIDMap.Version"
#define BOX_DBM_INODE_DB_VERSION_CURRENT 2

#define BOX_DBM_MESSAGE(stuff) stuff << " (qdbm): " << dperrmsg(dpecode)

#define BOX_LOG_DBM_ERROR(stuff) \
	BOX_ERROR(BOX_DBM_MESSAGE(stuff))

#define THROW_DBM_ERROR(message, filename, exception, subtype) \
	BOX_LOG_DBM_ERROR(message << ": " << filename); \
	THROW_EXCEPTION_MESSAGE(exception, subtype, \
		BOX_DBM_MESSAGE(message << ": " << filename));

#define ASSERT_DBM_OK(operation, message, filename, exception, subtype) \
	if(!(operation)) \
	{ \
		THROW_DBM_ERROR(message, filename, exception, subtype); \
	}

#define ASSERT_DBM_OPEN() \
	if(mpDepot == 0) \
	{ \
		THROW_EXCEPTION_MESSAGE(BackupStoreException, InodeMapNotOpen, \
			"Inode database not open"); \
	}

#define ASSERT_DBM_CLOSED() \
	if(mpDepot != 0) \
	{ \
		THROW_EXCEPTION_MESSAGE(CommonException, Internal, \
			"Inode database already open: " << mFilename); \
	} 

// --------------------------------------------------------------------------
//
// Function
//		Name:    BackupClientInodeToIDMap::BackupClientInodeToIDMap()
//		Purpose: Constructor
//		Created: 11/11/03
//
// --------------------------------------------------------------------------
BackupClientInodeToIDMap::BackupClientInodeToIDMap()
	: mReadOnly(true),
	  mEmpty(false),
	  mpDepot(0)
{
}

// --------------------------------------------------------------------------
//
// Function
//		Name:    BackupClientInodeToIDMap::~BackupClientInodeToIDMap()
//		Purpose: Destructor
//		Created: 11/11/03
//
// --------------------------------------------------------------------------
BackupClientInodeToIDMap::~BackupClientInodeToIDMap()
{
	if(mpDepot != 0)
	{
		Close();
	}
}

// --------------------------------------------------------------------------
//
// Function
//		Name:    BackupClientInodeToIDMap::Open(const char *, bool, bool)
//		Purpose: Open the database map, creating a file on disc to store everything
//		Created: 20/11/03
//
// --------------------------------------------------------------------------
void BackupClientInodeToIDMap::Open(const char *Filename, bool ReadOnly,
	bool CreateNew)
{
	mFilename = Filename;

	// Correct arguments?
	ASSERT(!(CreateNew && ReadOnly));
	
	// Correct usage?
	ASSERT_DBM_CLOSED();
	ASSERT(!mEmpty);
	
	// Open the database file
	int mode = ReadOnly ? DP_OREADER : DP_OWRITER;
	if(CreateNew)
	{
		mode |= DP_OCREAT;
	}
	
	mpDepot = dpopen(Filename, mode, 0);
	
	if(!mpDepot)
	{
		THROW_EXCEPTION_MESSAGE(BackupStoreException, BerkelyDBFailure,
			BOX_DBM_MESSAGE("Failed to open inode database: " <<
				mFilename));
	}

	const char* version_key = BOX_DBM_INODE_DB_VERSION_KEY;
	int32_t version = 0;

	if(CreateNew)
	{
		version = BOX_DBM_INODE_DB_VERSION_CURRENT;

		int ret = dpput(mpDepot, version_key, strlen(version_key),
			(char *)(&version), sizeof(version), DP_DKEEP);

		if(!ret)
		{
			THROW_EXCEPTION_MESSAGE(BackupStoreException, BerkelyDBFailure,
				BOX_DBM_MESSAGE("Failed to write version number to inode "
					"database: " << mFilename));
		}
	}
	else
	{
		int ret = dpgetwb(mpDepot, version_key, strlen(version_key), 0,
			sizeof(version), (char *)(&version));

		if(ret == -1)
		{
			THROW_EXCEPTION_MESSAGE(BackupStoreException, BerkelyDBFailure,
				"Missing version number in inode database. Perhaps it "
				"needs to be recreated: " << mFilename);
		}

		if(ret != sizeof(version))
		{
			THROW_EXCEPTION_MESSAGE(BackupStoreException, BerkelyDBFailure,
				"Wrong size version number in inode database: expected "
				<< sizeof(version) << " bytes but found " << ret);
		}

		if(version != BOX_DBM_INODE_DB_VERSION_CURRENT)
		{
			THROW_EXCEPTION_MESSAGE(BackupStoreException, BerkelyDBFailure,
				"Wrong version number in inode database: expected " <<
				BOX_DBM_INODE_DB_VERSION_CURRENT << " but found " <<
				version << ". Perhaps it needs to be recreated: " <<
				mFilename);
		}

		// By this point the version number has been checked and is OK.
	}
	
	// Read only flag
	mReadOnly = ReadOnly;
}

// --------------------------------------------------------------------------
//
// Function
//		Name:    BackupClientInodeToIDMap::OpenEmpty()
//		Purpose: 'Open' this map. Not associated with a disc file.
//			 Useful for when a map is required, but is against
//			 an empty file on disc which shouldn't be created.
//			 Implies read only.
//		Created: 20/11/03
//
// --------------------------------------------------------------------------
void BackupClientInodeToIDMap::OpenEmpty()
{
	ASSERT_DBM_CLOSED();
	ASSERT(mpDepot == 0);
	mEmpty = true;
	mReadOnly = true;
}

// --------------------------------------------------------------------------
//
// Function
//		Name:    BackupClientInodeToIDMap::Close()
//		Purpose: Close the database file
//		Created: 20/11/03
//
// --------------------------------------------------------------------------
void BackupClientInodeToIDMap::Close()
{
	ASSERT_DBM_OPEN();
	ASSERT_DBM_OK(dpclose(mpDepot), "Failed to close inode database",
		mFilename, BackupStoreException, BerkelyDBFailure);
	mpDepot = 0;
}

// --------------------------------------------------------------------------
//
// Function
//		Name:    BackupClientInodeToIDMap::AddToMap(InodeRefType,
//			 int64_t, int64_t)
//		Purpose: Adds an entry to the map. Overwrites any existing
//			 entry.
//		Created: 11/11/03
//
// --------------------------------------------------------------------------
void BackupClientInodeToIDMap::AddToMap(InodeRefType InodeRef, int64_t ObjectID,
	int64_t InDirectory, const std::string& LocalPath)
{
	if(mReadOnly)
	{
		THROW_EXCEPTION(BackupStoreException, InodeMapIsReadOnly);
	}

	if(mpDepot == 0)
	{
		THROW_EXCEPTION(BackupStoreException, InodeMapNotOpen);
	}

	ASSERT_DBM_OPEN();

	// Setup structures
	CollectInBufferStream buf;
	Archive arc(buf, IOStream::TimeOutInfinite);
	arc.WriteExact((uint64_t)ObjectID);
	arc.WriteExact((uint64_t)InDirectory);
	arc.Write(LocalPath);
	buf.SetForReading();

	ASSERT_DBM_OK(dpput(mpDepot, (const char *)&InodeRef, sizeof(InodeRef),
		(const char *)buf.GetBuffer(), buf.GetSize(), DP_DOVER),
		"Failed to add record to inode database", mFilename,
		BackupStoreException, BerkelyDBFailure);
}

// --------------------------------------------------------------------------
//
// Function
//		Name:    BackupClientInodeToIDMap::Lookup(InodeRefType,
//			 int64_t &, int64_t &) const
//		Purpose: Looks up an inode in the map, returning true if it
//			 exists, and the object ids of it and the directory
//			 it's in the reference arguments.
//		Created: 11/11/03
//
// --------------------------------------------------------------------------
bool BackupClientInodeToIDMap::Lookup(InodeRefType InodeRef, int64_t &rObjectIDOut,
	int64_t &rInDirectoryOut, std::string* pLocalPathOut) const
{
	if(mEmpty)
	{
		// Map is empty
		return false;
	}

	if(mpDepot == 0)
	{
		THROW_EXCEPTION(BackupStoreException, InodeMapNotOpen);
	}
	
	ASSERT_DBM_OPEN();
	int size;
	char* data = dpget(mpDepot, (const char *)&InodeRef, sizeof(InodeRef),
		0, -1, &size);
	if(data == NULL)
	{
		// key not in file
		return false;
	}

	// Free data automatically when the guard goes out of scope.
	MemoryBlockGuard<char *> guard(data);
	MemBlockStream stream(data, size);
	Archive arc(stream, IOStream::TimeOutInfinite);

	// Return data
	try
	{
		arc.Read(rObjectIDOut);
		arc.Read(rInDirectoryOut);
		if(pLocalPathOut)
		{
			arc.Read(*pLocalPathOut);
		}
	}
	catch(CommonException &e)
	{
		if(e.GetSubType() == CommonException::ArchiveBlockIncompleteRead)
		{
			THROW_FILE_ERROR("Failed to lookup record in inode database: "
				<< InodeRef << ": not enough data in record", mFilename,
				BackupStoreException, BerkelyDBFailure);
			// Need to throw precisely that exception to ensure that the
			// invalid database is deleted, so that we don't hit the same
			// error next time.
		}

		throw;
	}

	// Found
	return true;
}