summaryrefslogtreecommitdiff
path: root/lib/backupclient/BackupStoreFileCmbIdx.cpp
blob: c8bcc3b963518401ad8fc7162b618e792409a35f (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
321
322
323
324
// --------------------------------------------------------------------------
//
// File
//		Name:    BackupStoreFileCmbIdx.cpp
//		Purpose: Combine indicies of a delta file and the file it's a diff from.
//		Created: 8/7/04
//
// --------------------------------------------------------------------------

#include "Box.h"

#include <new>
#include <string.h>

#include "BackupStoreFile.h"
#include "BackupStoreFileWire.h"
#include "BackupStoreObjectMagic.h"
#include "BackupStoreException.h"
#include "BackupStoreConstants.h"
#include "BackupStoreFilename.h"

#include "MemLeakFindOn.h"

// Hide from outside world
namespace
{

class BSFCombinedIndexStream : public IOStream
{
public:
	BSFCombinedIndexStream(IOStream *pDiff);
	~BSFCombinedIndexStream();
	
	virtual int Read(void *pBuffer, int NBytes, int Timeout = IOStream::TimeOutInfinite);
	virtual void Write(const void *pBuffer, int NBytes);
	virtual bool StreamDataLeft();
	virtual bool StreamClosed();
	virtual void Initialise(IOStream &rFrom);
	
private:
	IOStream *mpDiff;
	bool mIsInitialised;
	bool mHeaderWritten;
	file_BlockIndexHeader mHeader;
	int64_t mNumEntriesToGo;
	int64_t mNumEntriesInFromFile;
	int64_t *mFromBlockSizes;		// NOTE: Entries in network byte order
};

};

// --------------------------------------------------------------------------
//
// Function
//		Name:    BackupStoreFile::CombineFileIndices(IOStream &, IOStream &, bool)
//		Purpose: Given a diff file and the file it's a diff from, return a stream from which
//				 can be read the index of the combined file, without actually combining them.
//				 The stream of the diff must have a lifetime greater than or equal to the
//				 lifetime of the returned stream object. The full "from" file stream
//				 only needs to exist during the actual function call.
//				 If you pass in dodgy files which aren't related, then you will either
//				 get an error or bad results. So don't do that.
//				 If DiffIsIndexOnly is true, then rDiff is assumed to be a stream positioned
//				 at the beginning of the block index. Similarly for FromIsIndexOnly.
//				 WARNING: Reads of the returned streams with buffer sizes less than 64 bytes
//				 will not return any data.
//		Created: 8/7/04
//
// --------------------------------------------------------------------------
std::auto_ptr<IOStream> BackupStoreFile::CombineFileIndices(IOStream &rDiff, IOStream &rFrom, bool DiffIsIndexOnly, bool FromIsIndexOnly)
{
	// Reposition file pointers?
	if(!DiffIsIndexOnly)
	{
		MoveStreamPositionToBlockIndex(rDiff);
	}
	if(!FromIsIndexOnly)
	{
		MoveStreamPositionToBlockIndex(rFrom);
	}

	// Create object
	std::auto_ptr<IOStream> stream(new BSFCombinedIndexStream(&rDiff));

	// Initialise it
	((BSFCombinedIndexStream *)stream.get())->Initialise(rFrom);

	// And return the stream
	return stream;
}


// --------------------------------------------------------------------------
//
// Function
//		Name:    BSFCombinedIndexStream::BSFCombinedIndexStream()
//		Purpose: Private class. Constructor.
//		Created: 8/7/04
//
// --------------------------------------------------------------------------
BSFCombinedIndexStream::BSFCombinedIndexStream(IOStream *pDiff)
	: mpDiff(pDiff),
	  mIsInitialised(false),
	  mHeaderWritten(false),
	  mNumEntriesToGo(0),
	  mNumEntriesInFromFile(0),
	  mFromBlockSizes(0)
{
	ASSERT(mpDiff != 0);
}


// --------------------------------------------------------------------------
//
// Function
//		Name:    BSFCombinedIndexStream::~BSFCombinedIndexStream()
//		Purpose: Private class. Destructor.
//		Created: 8/7/04
//
// --------------------------------------------------------------------------
BSFCombinedIndexStream::~BSFCombinedIndexStream()
{
	if(mFromBlockSizes != 0)
	{
		::free(mFromBlockSizes);
		mFromBlockSizes = 0;
	}
}


// --------------------------------------------------------------------------
//
// Function
//		Name:    BSFCombinedIndexStream::Initialise(IOStream &)
//		Purpose: Private class. Initalise from the streams (diff passed in constructor).
//				 Both streams must have file pointer positioned at the block index.
//		Created: 8/7/04
//
// --------------------------------------------------------------------------
void BSFCombinedIndexStream::Initialise(IOStream &rFrom)
{
	// Paranoia is good.
	if(mIsInitialised)
	{
		THROW_EXCEPTION(BackupStoreException, Internal)
	}
	
	// Look at the diff file: Read in the header
	if(!mpDiff->ReadFullBuffer(&mHeader, sizeof(mHeader), 0))
	{
		THROW_EXCEPTION(BackupStoreException, CouldntReadEntireStructureFromStream)
	}
	if(ntohl(mHeader.mMagicValue) != OBJECTMAGIC_FILE_BLOCKS_MAGIC_VALUE_V1)
	{
		THROW_EXCEPTION(BackupStoreException, BadBackupStoreFile)
	}
	
	// Read relevant data.
	mNumEntriesToGo = box_ntoh64(mHeader.mNumBlocks);
	
	// Adjust a bit to reflect the fact it's no longer a diff
	mHeader.mOtherFileID = box_hton64(0);
	
	// Now look at the from file: Read header
	file_BlockIndexHeader fromHdr;
	if(!rFrom.ReadFullBuffer(&fromHdr, sizeof(fromHdr), 0))
	{
		THROW_EXCEPTION(BackupStoreException, CouldntReadEntireStructureFromStream)
	}
	if(ntohl(fromHdr.mMagicValue) != OBJECTMAGIC_FILE_BLOCKS_MAGIC_VALUE_V1)
	{
		THROW_EXCEPTION(BackupStoreException, BadBackupStoreFile)
	}
	
	// Then... allocate memory for the list of sizes
	mNumEntriesInFromFile = box_ntoh64(fromHdr.mNumBlocks);
	mFromBlockSizes = (int64_t*)::malloc(mNumEntriesInFromFile * sizeof(int64_t));
	if(mFromBlockSizes == 0)
	{
		throw std::bad_alloc();
	}
	
	// And read them all in!
	for(int64_t b = 0; b < mNumEntriesInFromFile; ++b)
	{
		file_BlockIndexEntry e;
		if(!rFrom.ReadFullBuffer(&e, sizeof(e), 0))
		{
			THROW_EXCEPTION(BackupStoreException, CouldntReadEntireStructureFromStream)
		}
		
		// Check that the from file isn't a delta in itself
		if(box_ntoh64(e.mEncodedSize) <= 0)
		{
			THROW_EXCEPTION(BackupStoreException, OnCombineFromFileIsIncomplete)
		}

		// Store size (in network byte order)
		mFromBlockSizes[b] = e.mEncodedSize;
	}
	
	// Flag as initialised
	mIsInitialised = true;
}


// --------------------------------------------------------------------------
//
// Function
//		Name:    BSFCombinedIndexStream::Read(void *, int, int)
//		Purpose: Private class. As interface.
//		Created: 8/7/04
//
// --------------------------------------------------------------------------
int BSFCombinedIndexStream::Read(void *pBuffer, int NBytes, int Timeout)
{
	// Paranoia is good.
	if(!mIsInitialised || mFromBlockSizes == 0 || mpDiff == 0)
	{
		THROW_EXCEPTION(BackupStoreException, Internal)
	}
	
	int written = 0;
	
	// Header output yet?
	if(!mHeaderWritten)
	{
		// Enough space?
		if(NBytes < (int)sizeof(mHeader)) return 0;
		
		// Copy in
		::memcpy(pBuffer, &mHeader, sizeof(mHeader));
		NBytes -= sizeof(mHeader);
		written += sizeof(mHeader);
	
		// Flag it's done
		mHeaderWritten = true;
	}

	// How many entries can be written?
	int entriesToWrite = NBytes / sizeof(file_BlockIndexEntry);
	if(entriesToWrite > mNumEntriesToGo)
	{
		entriesToWrite = mNumEntriesToGo;
	}
	
	// Setup ready to go
	file_BlockIndexEntry *poutput = (file_BlockIndexEntry*)(((uint8_t*)pBuffer) + written);

	// Write entries
	for(int b = 0; b < entriesToWrite; ++b)
	{
		if(!mpDiff->ReadFullBuffer(&(poutput[b]), sizeof(file_BlockIndexEntry), 0))
		{
			THROW_EXCEPTION(BackupStoreException, CouldntReadEntireStructureFromStream)
		}
		
		// Does this need adjusting?
		int s = box_ntoh64(poutput[b].mEncodedSize);
		if(s <= 0)
		{
			// A reference to a block in the from file
			int block = 0 - s;
			ASSERT(block >= 0);
			if(block >= mNumEntriesInFromFile)
			{
				// That's not good, the block doesn't exist
				THROW_EXCEPTION(BackupStoreException, OnCombineFromFileIsIncomplete)
			}
			
			// Adjust the entry in the buffer
			poutput[b].mEncodedSize = mFromBlockSizes[block];	// stored in network byte order, no translation necessary
		}
	}
	
	// Update written count
	written += entriesToWrite * sizeof(file_BlockIndexEntry);
	mNumEntriesToGo -= entriesToWrite;
	
	return written;
}


// --------------------------------------------------------------------------
//
// Function
//		Name:    BSFCombinedIndexStream::Write(const void *, int)
//		Purpose: Private class. As interface.
//		Created: 8/7/04
//
// --------------------------------------------------------------------------
void BSFCombinedIndexStream::Write(const void *pBuffer, int NBytes)
{
	THROW_EXCEPTION(BackupStoreException, StreamDoesntHaveRequiredFeatures)
}


// --------------------------------------------------------------------------
//
// Function
//		Name:    BSFCombinedIndexStream::StreamDataLeft()
//		Purpose: Private class. As interface
//		Created: 8/7/04
//
// --------------------------------------------------------------------------
bool BSFCombinedIndexStream::StreamDataLeft()
{
	return (!mHeaderWritten) || (mNumEntriesToGo > 0);
}


// --------------------------------------------------------------------------
//
// Function
//		Name:    BSFCombinedIndexStream::StreamClosed()
//		Purpose: Private class. As interface.
//		Created: 8/7/04
//
// --------------------------------------------------------------------------
bool BSFCombinedIndexStream::StreamClosed()
{
	return true;	// doesn't do writing
}