summaryrefslogtreecommitdiff
path: root/lib/backupstore/BackupStoreDirectory.h
blob: 788a3ad03618f608c012aa59b0e4c700d2adc6b3 (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
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
// --------------------------------------------------------------------------
//
// File
//		Name:    BackupStoreDirectory.h
//		Purpose: Representation of a backup directory
//		Created: 2003/08/26
//
// --------------------------------------------------------------------------

#ifndef BACKUPSTOREDIRECTORY__H
#define BACKUPSTOREDIRECTORY__H

#include <string>
#include <vector>

#include "BackupStoreFilenameClear.h"
#include "StreamableMemBlock.h"
#include "BoxTime.h"

class IOStream;

// --------------------------------------------------------------------------
//
// Class
//		Name:    BackupStoreDirectory
//		Purpose: In memory representation of a directory
//		Created: 2003/08/26
//
// --------------------------------------------------------------------------
class BackupStoreDirectory
{
private:
#ifndef BOX_RELEASE_BUILD
	bool mInvalidated;
#endif

public:
#ifndef BOX_RELEASE_BUILD
	void Invalidate()
	{
		mInvalidated = true;
		for (std::vector<Entry*>::iterator i = mEntries.begin();
			i != mEntries.end(); i++)
		{
			(*i)->Invalidate();
		}
	}
#endif

	typedef enum
	{
		Option_DependencyInfoPresent = 1
	} dir_StreamFormatOptions;

	BackupStoreDirectory();
	BackupStoreDirectory(int64_t ObjectID, int64_t ContainerID);
	// Convenience constructor from a stream
	BackupStoreDirectory(IOStream& rStream,
		int Timeout = IOStream::TimeOutInfinite)
#ifndef BOX_RELEASE_BUILD
	: mInvalidated(false)
#endif
	{
		ReadFromStream(rStream, Timeout);
	}
	BackupStoreDirectory(std::auto_ptr<IOStream> apStream,
		int Timeout = IOStream::TimeOutInfinite)
#ifndef BOX_RELEASE_BUILD
	: mInvalidated(false)
#endif
	{
		ReadFromStream(*apStream, Timeout);
	}
private:
	// Copying not allowed
	BackupStoreDirectory(const BackupStoreDirectory &rToCopy);
public:
	~BackupStoreDirectory();

	class Entry
	{
	private:
#ifndef BOX_RELEASE_BUILD
		bool mInvalidated;
#endif

	public:
#ifndef BOX_RELEASE_BUILD
		void Invalidate() { mInvalidated = true; }
#endif

		friend class BackupStoreDirectory;

		Entry();
		~Entry();
		Entry(const Entry &rToCopy);
		Entry(const BackupStoreFilename &rName, box_time_t ModificationTime, int64_t ObjectID, int64_t SizeInBlocks, int16_t Flags, uint64_t AttributesHash);

		void ReadFromStream(IOStream &rStream, int Timeout);
		void WriteToStream(IOStream &rStream) const;

		const BackupStoreFilename &GetName() const
		{
			ASSERT(!mInvalidated); // Compiled out of release builds
			return mName;
		}
		box_time_t GetModificationTime() const
		{
			ASSERT(!mInvalidated); // Compiled out of release builds
			return mModificationTime;
		}
		int64_t GetObjectID() const
		{
			ASSERT(!mInvalidated); // Compiled out of release builds
			return mObjectID;
		}
		// SetObjectID is dangerous! It should only be used when
		// creating a snapshot.
		void SetObjectID(int64_t NewObjectID)
		{
			ASSERT(!mInvalidated); // Compiled out of release builds
			mObjectID = NewObjectID;
		}
		int64_t GetSizeInBlocks() const
		{
			ASSERT(!mInvalidated); // Compiled out of release builds
			return mSizeInBlocks;
		}
		int16_t GetFlags() const
		{
			ASSERT(!mInvalidated); // Compiled out of release builds
			return mFlags;
		}
		void AddFlags(int16_t Flags)
		{
			ASSERT(!mInvalidated); // Compiled out of release builds
			mFlags |= Flags;
		}
		void RemoveFlags(int16_t Flags)
		{
			ASSERT(!mInvalidated); // Compiled out of release builds
			mFlags &= ~Flags;
		}

		// Some things can be changed
		void SetName(const BackupStoreFilename &rNewName)
		{
			ASSERT(!mInvalidated); // Compiled out of release builds
			mName = rNewName;
		}
		void SetSizeInBlocks(int64_t SizeInBlocks)
		{
			ASSERT(!mInvalidated); // Compiled out of release builds
			mSizeInBlocks = SizeInBlocks;
		}

		// Attributes
		bool HasAttributes() const
		{
			ASSERT(!mInvalidated); // Compiled out of release builds
			return !mAttributes.IsEmpty();
		}
		void SetAttributes(const StreamableMemBlock &rAttr, uint64_t AttributesHash)
		{
			ASSERT(!mInvalidated); // Compiled out of release builds
			mAttributes.Set(rAttr);
			mAttributesHash = AttributesHash;
		}
		const StreamableMemBlock &GetAttributes() const
		{
			ASSERT(!mInvalidated); // Compiled out of release builds
			return mAttributes;
		}
		uint64_t GetAttributesHash() const
		{
			ASSERT(!mInvalidated); // Compiled out of release builds
			return mAttributesHash;
		}

		// Marks
		// The lowest mark number a version of a file of this name has ever had
		uint32_t GetMinMarkNumber() const
		{
			ASSERT(!mInvalidated); // Compiled out of release builds
			return mMinMarkNumber;
		}
		// The mark number on this file
		uint32_t GetMarkNumber() const
		{
			ASSERT(!mInvalidated); // Compiled out of release builds
			return mMarkNumber;
		}

		// Make sure these flags are synced with those in backupprocotol.txt
		// ListDirectory command
		enum
		{
			Flags_INCLUDE_EVERYTHING 	= -1,
			Flags_EXCLUDE_NOTHING 		= 0,
			Flags_EXCLUDE_EVERYTHING	= 31,	// make sure this is kept as sum of ones below!
			Flags_File					= 1,
			Flags_Dir					= 2,
			Flags_Deleted				= 4,
			Flags_OldVersion			= 8,
			Flags_RemoveASAP			= 16	// if this flag is set, housekeeping will remove it as it is marked Deleted or OldVersion
		};
		// characters for textual listing of files -- see bbackupquery/BackupQueries
		#define BACKUPSTOREDIRECTORY_ENTRY_FLAGS_DISPLAY_NAMES "fdXoR"

		// convenience methods
		bool inline IsDir()
		{
			ASSERT(!mInvalidated); // Compiled out of release builds
			return GetFlags() & Flags_Dir;
		}
		bool inline IsFile()
		{
			ASSERT(!mInvalidated); // Compiled out of release builds
			return GetFlags() & Flags_File;
		}
		bool inline IsOld()
		{
			ASSERT(!mInvalidated); // Compiled out of release builds
			return GetFlags() & Flags_OldVersion;
		}
		bool inline IsDeleted()
		{
			ASSERT(!mInvalidated); // Compiled out of release builds
			return GetFlags() & Flags_Deleted;
		}
		bool inline MatchesFlags(int16_t FlagsMustBeSet, int16_t FlagsNotToBeSet)
		{
			ASSERT(!mInvalidated); // Compiled out of release builds
			return ((FlagsMustBeSet == Flags_INCLUDE_EVERYTHING) || ((mFlags & FlagsMustBeSet) == FlagsMustBeSet))
				&& ((mFlags & FlagsNotToBeSet) == 0);
		};

		// Get dependency info
		// new version this depends on
		int64_t GetDependsNewer() const
		{
			ASSERT(!mInvalidated); // Compiled out of release builds
			return mDependsNewer;
		}
		void SetDependsNewer(int64_t ObjectID)
		{
			ASSERT(!mInvalidated); // Compiled out of release builds
			mDependsNewer = ObjectID;
		}
		// older version which depends on this
		int64_t GetDependsOlder() const
		{
			ASSERT(!mInvalidated); // Compiled out of release builds
			return mDependsOlder;
		}
		void SetDependsOlder(int64_t ObjectID)
		{
			ASSERT(!mInvalidated); // Compiled out of release builds
			mDependsOlder = ObjectID;
		}

		// Dependency info saving
		bool HasDependencies()
		{
			ASSERT(!mInvalidated); // Compiled out of release builds
			return mDependsNewer != 0 || mDependsOlder != 0;
		}
		void ReadFromStreamDependencyInfo(IOStream &rStream, int Timeout);
		void WriteToStreamDependencyInfo(IOStream &rStream) const;

	private:
		BackupStoreFilename mName;
		box_time_t mModificationTime;
		int64_t mObjectID;
		int64_t mSizeInBlocks;
		int16_t mFlags;
		uint64_t mAttributesHash;
		StreamableMemBlock mAttributes;
		uint32_t mMinMarkNumber;
		uint32_t mMarkNumber;

		uint64_t mDependsNewer;	// new version this depends on
		uint64_t mDependsOlder;	// older version which depends on this
	};

#ifndef BOX_RELEASE_BUILD
	bool IsInvalidated()
	{
		return mInvalidated;
	}
#endif // !BOX_RELEASE_BUILD

	void ReadFromStream(IOStream &rStream, int Timeout);
	void WriteToStream(IOStream &rStream,
			int16_t FlagsMustBeSet = Entry::Flags_INCLUDE_EVERYTHING,
			int16_t FlagsNotToBeSet = Entry::Flags_EXCLUDE_NOTHING,
			bool StreamAttributes = true, bool StreamDependencyInfo = true) const;
			
	Entry *AddEntry(const Entry &rEntryToCopy);
	Entry *AddEntry(const BackupStoreFilename &rName,
		box_time_t ModificationTime, int64_t ObjectID,
		int64_t SizeInBlocks, int16_t Flags,
		uint64_t AttributesHash);
	void DeleteEntry(int64_t ObjectID);
	Entry *FindEntryByID(int64_t ObjectID) const;

	int64_t GetObjectID() const
	{
		ASSERT(!mInvalidated); // Compiled out of release builds
		return mObjectID;
	}

	int64_t GetContainerID() const
	{
		ASSERT(!mInvalidated); // Compiled out of release builds
		return mContainerID;
	}
	// Need to be able to update the container ID when moving objects
	void SetContainerID(int64_t ContainerID)
	{
		ASSERT(!mInvalidated); // Compiled out of release builds
		mContainerID = ContainerID;
	}

	// Purely for use of server -- not serialised into streams
	int64_t GetRevisionID() const
	{
		ASSERT(!mInvalidated); // Compiled out of release builds
		return mRevisionID;
	}
	void SetRevisionID(int64_t RevisionID)
	{
		ASSERT(!mInvalidated); // Compiled out of release builds
		mRevisionID = RevisionID;
	}

	unsigned int GetNumberOfEntries() const
	{
		ASSERT(!mInvalidated); // Compiled out of release builds
		return mEntries.size();
	}

	// User info -- not serialised into streams
	int64_t GetUserInfo1_SizeInBlocks() const
	{
		ASSERT(!mInvalidated); // Compiled out of release builds
		return mUserInfo1;
	}
	void SetUserInfo1_SizeInBlocks(int64_t UserInfo1)
	{
		ASSERT(!mInvalidated); // Compiled out of release builds
		mUserInfo1 = UserInfo1;
	}

	// Attributes
	bool HasAttributes() const
	{
		ASSERT(!mInvalidated); // Compiled out of release builds
		return !mAttributes.IsEmpty();
	}
	void SetAttributes(const StreamableMemBlock &rAttr,
		box_time_t AttributesModTime)
	{
		ASSERT(!mInvalidated); // Compiled out of release builds
		mAttributes.Set(rAttr);
		mAttributesModTime = AttributesModTime;
	}
	const StreamableMemBlock &GetAttributes() const
	{
		ASSERT(!mInvalidated); // Compiled out of release builds
		return mAttributes;
	}
	box_time_t GetAttributesModTime() const
	{
		ASSERT(!mInvalidated); // Compiled out of release builds
		return mAttributesModTime;
	}

	class Iterator
	{
	public:
		Iterator(const BackupStoreDirectory &rDir)
			: mrDir(rDir), i(rDir.mEntries.begin())
		{
			ASSERT(!mrDir.mInvalidated); // Compiled out of release builds
		}

		BackupStoreDirectory::Entry *Next(int16_t FlagsMustBeSet = Entry::Flags_INCLUDE_EVERYTHING, int16_t FlagsNotToBeSet = Entry::Flags_EXCLUDE_NOTHING)
		{
			ASSERT(!mrDir.mInvalidated); // Compiled out of release builds
			// Skip over things which don't match the required flags
			while(i != mrDir.mEntries.end() && !(*i)->MatchesFlags(FlagsMustBeSet, FlagsNotToBeSet))
			{
				++i;
			}
			// Not the last one?
			if(i == mrDir.mEntries.end())
			{
				return 0;
			}
			// Return entry, and increment
			return (*(i++));
		}

		// WARNING: This function is really very inefficient.
		// Only use when you want to look up ONE filename, not in a loop looking up lots.
		// In a looping situation, cache the decrypted filenames in another memory structure.
		BackupStoreDirectory::Entry *FindMatchingClearName(const BackupStoreFilenameClear &rFilename, int16_t FlagsMustBeSet = Entry::Flags_INCLUDE_EVERYTHING, int16_t FlagsNotToBeSet = Entry::Flags_EXCLUDE_NOTHING)
		{
			ASSERT(!mrDir.mInvalidated); // Compiled out of release builds
			// Skip over things which don't match the required flags or filename
			while( (i != mrDir.mEntries.end())
				&& ( (!(*i)->MatchesFlags(FlagsMustBeSet, FlagsNotToBeSet))
					|| (BackupStoreFilenameClear((*i)->GetName()).GetClearFilename() != rFilename.GetClearFilename()) ) )
			{
				++i;
			}
			// Not the last one?
			if(i == mrDir.mEntries.end())
			{
				return 0;
			}
			// Return entry, and increment
			return (*(i++));
		}

	private:
		const BackupStoreDirectory &mrDir;
		std::vector<Entry*>::const_iterator i;
	};

	friend class Iterator;

	class ReverseIterator
	{
	public:
		ReverseIterator(const BackupStoreDirectory &rDir)
			: mrDir(rDir), i(rDir.mEntries.rbegin())
		{
			ASSERT(!mrDir.mInvalidated); // Compiled out of release builds
		}

		BackupStoreDirectory::Entry *Next(int16_t FlagsMustBeSet = Entry::Flags_INCLUDE_EVERYTHING, int16_t FlagsNotToBeSet = Entry::Flags_EXCLUDE_NOTHING)
		{
			ASSERT(!mrDir.mInvalidated); // Compiled out of release builds
			// Skip over things which don't match the required flags
			while(i != mrDir.mEntries.rend() && !(*i)->MatchesFlags(FlagsMustBeSet, FlagsNotToBeSet))
			{
				++i;
			}
			// Not the last one?
			if(i == mrDir.mEntries.rend())
			{
				return 0;
			}
			// Return entry, and increment
			return (*(i++));
		}

	private:
		const BackupStoreDirectory &mrDir;
		std::vector<Entry*>::const_reverse_iterator i;
	};

	friend class ReverseIterator;

	// For recovery of the store
	// Implemented in BackupStoreCheck2.cpp
	bool CheckAndFix();
	void AddUnattachedObject(const BackupStoreFilename &rName,
		box_time_t ModificationTime, int64_t ObjectID,
		int64_t SizeInBlocks, int16_t Flags);
	bool NameInUse(const BackupStoreFilename &rName);

	// For testing
	// Don't use these functions in normal code!
	void TESTONLY_SetObjectID(int64_t ObjectID) {mObjectID = ObjectID;}
	// Debug and diagnostics
	void Dump(void *clibFileHandle, bool ToTrace); // first arg is FILE *, but avoid including stdio.h everywhere

private:
	int64_t mRevisionID;
	int64_t mObjectID;
	int64_t mContainerID;
	std::vector<Entry*> mEntries;
	box_time_t mAttributesModTime;
	StreamableMemBlock mAttributes;
	int64_t mUserInfo1;
};

#endif // BACKUPSTOREDIRECTORY__H