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
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
|
// --------------------------------------------------------------------------
//
// File
// Name: BackupStoreDirectory.h
// Purpose: Representation of a backup directory
// Created: 2003/08/26
//
// --------------------------------------------------------------------------
#include "Box.h"
#include <sys/types.h>
#include "BackupStoreDirectory.h"
#include "IOStream.h"
#include "BackupStoreException.h"
#include "BackupStoreObjectMagic.h"
#include "MemLeakFindOn.h"
// set packing to one byte
#ifdef STRUCTURE_PACKING_FOR_WIRE_USE_HEADERS
#include "BeginStructPackForWire.h"
#else
BEGIN_STRUCTURE_PACKING_FOR_WIRE
#endif
typedef struct
{
int32_t mMagicValue; // also the version number
int32_t mNumEntries;
int64_t mObjectID; // this object ID
int64_t mContainerID; // ID of container
uint64_t mAttributesModTime;
int32_t mOptionsPresent; // bit mask of optional sections / features present
// Then a StreamableMemBlock for attributes
} dir_StreamFormat;
typedef enum
{
Option_DependencyInfoPresent = 1
} dir_StreamFormatOptions;
typedef struct
{
uint64_t mModificationTime;
int64_t mObjectID;
int64_t mSizeInBlocks;
uint64_t mAttributesHash;
int16_t mFlags; // order smaller items after bigger ones (for alignment)
// Then a BackupStoreFilename
// Then a StreamableMemBlock for attributes
} en_StreamFormat;
typedef struct
{
int64_t mDependsNewer;
int64_t mDependsOlder;
} en_StreamFormatDepends;
// Use default packing
#ifdef STRUCTURE_PACKING_FOR_WIRE_USE_HEADERS
#include "EndStructPackForWire.h"
#else
END_STRUCTURE_PACKING_FOR_WIRE
#endif
// --------------------------------------------------------------------------
//
// Function
// Name: BackupStoreDirectory::BackupStoreDirectory()
// Purpose: Constructor
// Created: 2003/08/26
//
// --------------------------------------------------------------------------
BackupStoreDirectory::BackupStoreDirectory()
: mRevisionID(0), mObjectID(0), mContainerID(0), mAttributesModTime(0), mUserInfo1(0)
{
ASSERT(sizeof(u_int64_t) == sizeof(box_time_t));
}
// --------------------------------------------------------------------------
//
// File
// Name: BackupStoreDirectory::BackupStoreDirectory(int64_t, int64_t)
// Purpose: Constructor giving object and container IDs
// Created: 2003/08/28
//
// --------------------------------------------------------------------------
BackupStoreDirectory::BackupStoreDirectory(int64_t ObjectID, int64_t ContainerID)
: mRevisionID(0), mObjectID(ObjectID), mContainerID(ContainerID), mAttributesModTime(0), mUserInfo1(0)
{
}
// --------------------------------------------------------------------------
//
// Function
// Name: BackupStoreDirectory::~BackupStoreDirectory()
// Purpose: Destructor
// Created: 2003/08/26
//
// --------------------------------------------------------------------------
BackupStoreDirectory::~BackupStoreDirectory()
{
for(std::vector<Entry*>::iterator i(mEntries.begin()); i != mEntries.end(); ++i)
{
delete (*i);
}
}
// --------------------------------------------------------------------------
//
// Function
// Name: BackupStoreDirectory::ReadFromStream(IOStream &, int)
// Purpose: Reads the directory contents from a stream. Exceptions will yeild incomplete reads.
// Created: 2003/08/26
//
// --------------------------------------------------------------------------
void BackupStoreDirectory::ReadFromStream(IOStream &rStream, int Timeout)
{
// Get the header
dir_StreamFormat hdr;
if(!rStream.ReadFullBuffer(&hdr, sizeof(hdr), 0 /* not interested in bytes read if this fails */, Timeout))
{
THROW_EXCEPTION(BackupStoreException, CouldntReadEntireStructureFromStream)
}
// Check magic value...
if(OBJECTMAGIC_DIR_MAGIC_VALUE != ntohl(hdr.mMagicValue))
{
THROW_EXCEPTION(BackupStoreException, BadDirectoryFormat)
}
// Get data
mObjectID = box_ntoh64(hdr.mObjectID);
mContainerID = box_ntoh64(hdr.mContainerID);
mAttributesModTime = box_ntoh64(hdr.mAttributesModTime);
// Options
int32_t options = ntohl(hdr.mOptionsPresent);
// Get attributes
mAttributes.ReadFromStream(rStream, Timeout);
// Decode count
int count = ntohl(hdr.mNumEntries);
// Clear existing list
for(std::vector<Entry*>::iterator i = mEntries.begin();
i != mEntries.end(); i++)
{
delete (*i);
}
mEntries.clear();
// Read them in!
for(int c = 0; c < count; ++c)
{
Entry *pen = new Entry;
try
{
// Read from stream
pen->ReadFromStream(rStream, Timeout);
// Add to list
mEntries.push_back(pen);
}
catch(...)
{
delete pen;
throw;
}
}
// Read in dependency info?
if(options & Option_DependencyInfoPresent)
{
// Read in extra dependency data
for(int c = 0; c < count; ++c)
{
mEntries[c]->ReadFromStreamDependencyInfo(rStream, Timeout);
}
}
}
// --------------------------------------------------------------------------
//
// Function
// Name: BackupStoreDirectory::WriteToStream(IOStream &, int16_t, int16_t, bool, bool)
// Purpose: Writes a selection of entries to a stream
// Created: 2003/08/26
//
// --------------------------------------------------------------------------
void BackupStoreDirectory::WriteToStream(IOStream &rStream, int16_t FlagsMustBeSet, int16_t FlagsNotToBeSet, bool StreamAttributes, bool StreamDependencyInfo) const
{
// Get count of entries
int32_t count = mEntries.size();
if(FlagsMustBeSet != Entry::Flags_INCLUDE_EVERYTHING || FlagsNotToBeSet != Entry::Flags_EXCLUDE_NOTHING)
{
// Need to count the entries
count = 0;
Iterator i(*this);
while(i.Next(FlagsMustBeSet, FlagsNotToBeSet) != 0)
{
count++;
}
}
// Check that sensible IDs have been set
ASSERT(mObjectID != 0);
ASSERT(mContainerID != 0);
// Need dependency info?
bool dependencyInfoRequired = false;
if(StreamDependencyInfo)
{
Iterator i(*this);
Entry *pen = 0;
while((pen = i.Next(FlagsMustBeSet, FlagsNotToBeSet)) != 0)
{
if(pen->HasDependencies())
{
dependencyInfoRequired = true;
}
}
}
// Options
int32_t options = 0;
if(dependencyInfoRequired) options |= Option_DependencyInfoPresent;
// Build header
dir_StreamFormat hdr;
hdr.mMagicValue = htonl(OBJECTMAGIC_DIR_MAGIC_VALUE);
hdr.mNumEntries = htonl(count);
hdr.mObjectID = box_hton64(mObjectID);
hdr.mContainerID = box_hton64(mContainerID);
hdr.mAttributesModTime = box_hton64(mAttributesModTime);
hdr.mOptionsPresent = htonl(options);
// Write header
rStream.Write(&hdr, sizeof(hdr));
// Write the attributes?
if(StreamAttributes)
{
mAttributes.WriteToStream(rStream);
}
else
{
// Write a blank header instead
StreamableMemBlock::WriteEmptyBlockToStream(rStream);
}
// Then write all the entries
Iterator i(*this);
Entry *pen = 0;
while((pen = i.Next(FlagsMustBeSet, FlagsNotToBeSet)) != 0)
{
pen->WriteToStream(rStream);
}
// Write dependency info?
if(dependencyInfoRequired)
{
Iterator i(*this);
Entry *pen = 0;
while((pen = i.Next(FlagsMustBeSet, FlagsNotToBeSet)) != 0)
{
pen->WriteToStreamDependencyInfo(rStream);
}
}
}
// --------------------------------------------------------------------------
//
// Function
// Name: BackupStoreDirectory::AddEntry(const Entry &)
// Purpose: Adds entry to directory (no checking)
// Created: 2003/08/27
//
// --------------------------------------------------------------------------
BackupStoreDirectory::Entry *BackupStoreDirectory::AddEntry(const Entry &rEntryToCopy)
{
Entry *pnew = new Entry(rEntryToCopy);
try
{
mEntries.push_back(pnew);
}
catch(...)
{
delete pnew;
throw;
}
return pnew;
}
// --------------------------------------------------------------------------
//
// Function
// Name: BackupStoreDirectory::AddEntry(const BackupStoreFilename &, int64_t, int64_t, int16_t)
// Purpose: Adds entry to directory (no checking)
// Created: 2003/08/27
//
// --------------------------------------------------------------------------
BackupStoreDirectory::Entry *BackupStoreDirectory::AddEntry(const BackupStoreFilename &rName, box_time_t ModificationTime, int64_t ObjectID, int64_t SizeInBlocks, int16_t Flags, box_time_t AttributesModTime)
{
Entry *pnew = new Entry(rName, ModificationTime, ObjectID, SizeInBlocks, Flags, AttributesModTime);
try
{
mEntries.push_back(pnew);
}
catch(...)
{
delete pnew;
throw;
}
return pnew;
}
// --------------------------------------------------------------------------
//
// Function
// Name: BackupStoreDirectory::DeleteEntry(int64_t)
// Purpose: Deletes entry with given object ID (uses linear search, maybe a little inefficient)
// Created: 2003/08/27
//
// --------------------------------------------------------------------------
void BackupStoreDirectory::DeleteEntry(int64_t ObjectID)
{
for(std::vector<Entry*>::iterator i(mEntries.begin());
i != mEntries.end(); ++i)
{
if((*i)->mObjectID == ObjectID)
{
// Delete
delete (*i);
// Remove from list
mEntries.erase(i);
// Done
return;
}
}
// Not found
THROW_EXCEPTION(BackupStoreException, CouldNotFindEntryInDirectory)
}
// --------------------------------------------------------------------------
//
// Function
// Name: BackupStoreDirectory::FindEntryByID(int64_t)
// Purpose: Finds a specific entry. Returns 0 if the entry doesn't exist.
// Created: 12/11/03
//
// --------------------------------------------------------------------------
BackupStoreDirectory::Entry *BackupStoreDirectory::FindEntryByID(int64_t ObjectID) const
{
for(std::vector<Entry*>::const_iterator i(mEntries.begin());
i != mEntries.end(); ++i)
{
if((*i)->mObjectID == ObjectID)
{
// Found
return (*i);
}
}
// Not found
return 0;
}
// --------------------------------------------------------------------------
//
// Function
// Name: BackupStoreDirectory::Entry::Entry()
// Purpose: Constructor
// Created: 2003/08/26
//
// --------------------------------------------------------------------------
BackupStoreDirectory::Entry::Entry()
: mModificationTime(0),
mObjectID(0),
mSizeInBlocks(0),
mFlags(0),
mAttributesHash(0),
mMinMarkNumber(0),
mMarkNumber(0),
mDependsNewer(0),
mDependsOlder(0)
{
}
// --------------------------------------------------------------------------
//
// Function
// Name: BackupStoreDirectory::Entry::~Entry()
// Purpose: Destructor
// Created: 2003/08/26
//
// --------------------------------------------------------------------------
BackupStoreDirectory::Entry::~Entry()
{
}
// --------------------------------------------------------------------------
//
// Function
// Name: BackupStoreDirectory::Entry::Entry(const Entry &)
// Purpose: Copy constructor
// Created: 2003/08/26
//
// --------------------------------------------------------------------------
BackupStoreDirectory::Entry::Entry(const Entry &rToCopy)
: mName(rToCopy.mName),
mModificationTime(rToCopy.mModificationTime),
mObjectID(rToCopy.mObjectID),
mSizeInBlocks(rToCopy.mSizeInBlocks),
mFlags(rToCopy.mFlags),
mAttributesHash(rToCopy.mAttributesHash),
mAttributes(rToCopy.mAttributes),
mMinMarkNumber(rToCopy.mMinMarkNumber),
mMarkNumber(rToCopy.mMarkNumber),
mDependsNewer(rToCopy.mDependsNewer),
mDependsOlder(rToCopy.mDependsOlder)
{
}
// --------------------------------------------------------------------------
//
// Function
// Name: BackupStoreDirectory::Entry::Entry(const BackupStoreFilename &, int64_t, int64_t, int16_t)
// Purpose: Constructor from values
// Created: 2003/08/27
//
// --------------------------------------------------------------------------
BackupStoreDirectory::Entry::Entry(const BackupStoreFilename &rName, box_time_t ModificationTime, int64_t ObjectID, int64_t SizeInBlocks, int16_t Flags, uint64_t AttributesHash)
: mName(rName),
mModificationTime(ModificationTime),
mObjectID(ObjectID),
mSizeInBlocks(SizeInBlocks),
mFlags(Flags),
mAttributesHash(AttributesHash),
mMinMarkNumber(0),
mMarkNumber(0),
mDependsNewer(0),
mDependsOlder(0)
{
}
// --------------------------------------------------------------------------
//
// Function
// Name: BackupStoreDirectory::Entry::TryReading(IOStream &, int)
// Purpose: Read an entry from a stream
// Created: 2003/08/26
//
// --------------------------------------------------------------------------
void BackupStoreDirectory::Entry::ReadFromStream(IOStream &rStream, int Timeout)
{
// Grab the raw bytes from the stream which compose the header
en_StreamFormat entry;
if(!rStream.ReadFullBuffer(&entry, sizeof(entry), 0 /* not interested in bytes read if this fails */, Timeout))
{
THROW_EXCEPTION(BackupStoreException, CouldntReadEntireStructureFromStream)
}
// Do reading first before modifying the variables, to be more exception safe
// Get the filename
BackupStoreFilename name;
name.ReadFromStream(rStream, Timeout);
// Get the attributes
mAttributes.ReadFromStream(rStream, Timeout);
// Store the rest of the bits
mModificationTime = box_ntoh64(entry.mModificationTime);
mObjectID = box_ntoh64(entry.mObjectID);
mSizeInBlocks = box_ntoh64(entry.mSizeInBlocks);
mAttributesHash = box_ntoh64(entry.mAttributesHash);
mFlags = ntohs(entry.mFlags);
mName = name;
}
// --------------------------------------------------------------------------
//
// Function
// Name: BackupStoreDirectory::Entry::WriteToStream(IOStream &)
// Purpose: Writes the entry to a stream
// Created: 2003/08/26
//
// --------------------------------------------------------------------------
void BackupStoreDirectory::Entry::WriteToStream(IOStream &rStream) const
{
// Build a structure
en_StreamFormat entry;
entry.mModificationTime = box_hton64(mModificationTime);
entry.mObjectID = box_hton64(mObjectID);
entry.mSizeInBlocks = box_hton64(mSizeInBlocks);
entry.mAttributesHash = box_hton64(mAttributesHash);
entry.mFlags = htons(mFlags);
// Write it
rStream.Write(&entry, sizeof(entry));
// Write the filename
mName.WriteToStream(rStream);
// Write any attributes
mAttributes.WriteToStream(rStream);
}
// --------------------------------------------------------------------------
//
// Function
// Name: BackupStoreDirectory::Entry::ReadFromStreamDependencyInfo(IOStream &, int)
// Purpose: Read the optional dependency info from a stream
// Created: 13/7/04
//
// --------------------------------------------------------------------------
void BackupStoreDirectory::Entry::ReadFromStreamDependencyInfo(IOStream &rStream, int Timeout)
{
// Grab the raw bytes from the stream which compose the header
en_StreamFormatDepends depends;
if(!rStream.ReadFullBuffer(&depends, sizeof(depends), 0 /* not interested in bytes read if this fails */, Timeout))
{
THROW_EXCEPTION(BackupStoreException, CouldntReadEntireStructureFromStream)
}
// Store the data
mDependsNewer = box_ntoh64(depends.mDependsNewer);
mDependsOlder = box_ntoh64(depends.mDependsOlder);
}
// --------------------------------------------------------------------------
//
// Function
// Name: BackupStoreDirectory::Entry::WriteToStreamDependencyInfo(IOStream &)
// Purpose: Write the optional dependency info to a stream
// Created: 13/7/04
//
// --------------------------------------------------------------------------
void BackupStoreDirectory::Entry::WriteToStreamDependencyInfo(IOStream &rStream) const
{
// Build structure
en_StreamFormatDepends depends;
depends.mDependsNewer = box_hton64(mDependsNewer);
depends.mDependsOlder = box_hton64(mDependsOlder);
// Write
rStream.Write(&depends, sizeof(depends));
}
|