summaryrefslogtreecommitdiff
path: root/lib/backupclient/BackupClientRestore.cpp
blob: ec4ca68108d7547a21bf8692605f4a68487b723a (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
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
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
// --------------------------------------------------------------------------
//
// File
//		Name:    BackupClientRestore.cpp
//		Purpose: 
//		Created: 23/11/03
//
// --------------------------------------------------------------------------

#include "Box.h"

#ifdef HAVE_UNISTD_H
	#include <unistd.h>
#endif

#include <sys/types.h>
#include <sys/stat.h>
#include <string>
#include <set>
#include <limits.h>
#include <stdio.h>
#include <errno.h>

#include "BackupClientRestore.h"
#include "autogen_BackupProtocolClient.h"
#include "CommonException.h"
#include "BackupClientFileAttributes.h"
#include "IOStream.h"
#include "BackupStoreDirectory.h"
#include "BackupStoreFile.h"
#include "CollectInBufferStream.h"
#include "FileStream.h"
#include "Utils.h"

#include "MemLeakFindOn.h"

#define MAX_BYTES_WRITTEN_BETWEEN_RESTORE_INFO_SAVES (128*1024)

class RestoreResumeInfo
{
public:
	// constructor
	RestoreResumeInfo()
		: mNextLevelID(0),
		  mpNextLevel(0)
	{
	}
	
	// destructor
	~RestoreResumeInfo()
	{
		delete mpNextLevel;
		mpNextLevel = 0;
	}
	
	// Get a next level object
	RestoreResumeInfo &AddLevel(int64_t ID, const std::string &rLocalName)
	{
		ASSERT(mpNextLevel == 0 && mNextLevelID == 0);
		mpNextLevel = new RestoreResumeInfo;
		mNextLevelID = ID;
		mNextLevelLocalName = rLocalName;
		return *mpNextLevel;
	}
	
	// Remove the next level info
	void RemoveLevel()
	{
		ASSERT(mpNextLevel != 0 && mNextLevelID != 0);
		delete mpNextLevel;
		mpNextLevel = 0;
		mNextLevelID = 0;
		mNextLevelLocalName.erase();
	}
	
	void Save(const std::string &rFilename) const
	{
		// TODO: use proper buffered streams when they're done
		// Build info in memory buffer
		CollectInBufferStream write;
		
		// Save this level
		SaveLevel(write);
	
		// Store in file
		write.SetForReading();
		FileStream file(rFilename.c_str(), O_WRONLY | O_CREAT);
		write.CopyStreamTo(file, IOStream::TimeOutInfinite, 8*1024 /* large buffer */);
	}

	void SaveLevel(IOStream &rWrite) const
	{
		// Write the restored objects
		int64_t numObjects = mRestoredObjects.size();
		rWrite.Write(&numObjects, sizeof(numObjects));
		for(std::set<int64_t>::const_iterator i(mRestoredObjects.begin()); i != mRestoredObjects.end(); ++i)
		{
			int64_t id = *i;
			rWrite.Write(&id, sizeof(id));
		}
		
		// Next level?
		if(mpNextLevel != 0)
		{
			// ID
			rWrite.Write(&mNextLevelID, sizeof(mNextLevelID));
			// Name string
			int32_t nsize = mNextLevelLocalName.size();
			rWrite.Write(&nsize, sizeof(nsize));
			rWrite.Write(mNextLevelLocalName.c_str(), nsize);
			// And then the level itself
			mpNextLevel->SaveLevel(rWrite);
		}
		else
		{
			// Just write a zero
			int64_t zero = 0;
			rWrite.Write(&zero, sizeof(zero));
		}
	}

	// Not written to be efficient -- shouldn't be called very often.
	bool Load(const std::string &rFilename)
	{
		// Delete and reset if necessary
		if(mpNextLevel != 0)
		{
			RemoveLevel();
		}
		
		// Open file
		FileStream file(rFilename.c_str());
		
		// Load this level
		return LoadLevel(file);
	}
	
	#define CHECKED_READ(x, s) if(!rRead.ReadFullBuffer(x, s, 0)) {return false;}
	bool LoadLevel(IOStream &rRead)
	{
		// Load the restored objects list
		mRestoredObjects.clear();
		int64_t numObjects = 0;
		CHECKED_READ(&numObjects, sizeof(numObjects));
		for(int64_t o = 0; o < numObjects; ++o)
		{
			int64_t id;
			CHECKED_READ(&id, sizeof(id));
			mRestoredObjects.insert(id);
		}

		// ID of next level?
		int64_t nextID = 0;
		CHECKED_READ(&nextID, sizeof(nextID));
		if(nextID != 0)
		{
			// Load the next level!
			std::string name;
			int32_t nsize = 0;
			CHECKED_READ(&nsize, sizeof(nsize));
			char n[PATH_MAX];
			if(nsize > PATH_MAX) return false;
			CHECKED_READ(n, nsize);
			name.assign(n, nsize);
			
			// Create a new level
			mpNextLevel = new RestoreResumeInfo;
			mNextLevelID = nextID;
			mNextLevelLocalName = name;
			
			// And ask it to read itself in
			if(!mpNextLevel->LoadLevel(rRead))
			{
				return false;
			}
		}

		return true;
	}

	// List of objects at this level which have been done already
	std::set<int64_t> mRestoredObjects;
	// Next level ID
	int64_t mNextLevelID;
	// Pointer to next level
	RestoreResumeInfo *mpNextLevel;
	// Local filename of next level
	std::string mNextLevelLocalName;
};

// parameters structure
typedef struct
{
	bool PrintDots;
	bool RestoreDeleted;
	std::string mRestoreResumeInfoFilename;
	RestoreResumeInfo mResumeInfo;
} RestoreParams;



// --------------------------------------------------------------------------
//
// Function
//		Name:    BackupClientRestoreDir(BackupProtocolClient &, int64_t, const char *, bool)
//		Purpose: Restore a directory
//		Created: 23/11/03
//
// --------------------------------------------------------------------------
static int BackupClientRestoreDir(BackupProtocolClient &rConnection, int64_t DirectoryID, std::string &rLocalDirectoryName,
	RestoreParams &Params, RestoreResumeInfo &rLevel)
{
	// If we're resuming... check that we haven't got a next level to look at
	if(rLevel.mpNextLevel != 0)
	{
		// Recurse immediately
		std::string localDirname(rLocalDirectoryName + DIRECTORY_SEPARATOR_ASCHAR + rLevel.mNextLevelLocalName);
		BackupClientRestoreDir(rConnection, rLevel.mNextLevelID, localDirname, Params, *rLevel.mpNextLevel);
		
		// Add it to the list of done itmes
		rLevel.mRestoredObjects.insert(rLevel.mNextLevelID);

		// Remove the level for the recursed directory
		rLevel.RemoveLevel();		
	}
	
	// Create the local directory, if not already done.
	// Path and owner set later, just use restrictive owner mode.

	int exists;

	try
	{
		exists = ObjectExists(rLocalDirectoryName.c_str());
	}
	catch (BoxException &e)
	{
		::syslog(LOG_ERR, "Failed to check existence for %s: %s", 
			rLocalDirectoryName.c_str(), e.what());
		return Restore_UnknownError;
	}
	catch(std::exception &e)
	{
		::syslog(LOG_ERR, "Failed to check existence for %s: %s", 
			rLocalDirectoryName.c_str(), e.what());
		return Restore_UnknownError;
	}
	catch(...)
	{
		::syslog(LOG_ERR, "Failed to check existence for %s: "
			"unknown error", rLocalDirectoryName.c_str());
		return Restore_UnknownError;
	}

	switch(exists)
	{
		case ObjectExists_Dir:
			// Do nothing
			break;
		case ObjectExists_File:
			{
				// File exists with this name, which is fun. Get rid of it.
				::printf("WARNING: File present with name '%s', removing out of the way of restored directory. Use specific restore with ID to restore this object.", rLocalDirectoryName.c_str());
				if(::unlink(rLocalDirectoryName.c_str()) != 0)
				{
					::syslog(LOG_ERR, "Failed to delete "
						"file %s: %s",
						rLocalDirectoryName.c_str(),
						strerror(errno));
					return Restore_UnknownError;
				}
				TRACE1("In restore, directory name collision with file %s", rLocalDirectoryName.c_str());
			}
			break;
		case ObjectExists_NoObject:
			// we'll create it in a second, after checking
			// whether the parent directory exists
			break;
		default:
			ASSERT(false);
			break;
	}

	std::string parentDirectoryName(rLocalDirectoryName);
	if(parentDirectoryName[parentDirectoryName.size() - 1] == 
		DIRECTORY_SEPARATOR_ASCHAR)
	{
		parentDirectoryName.resize(parentDirectoryName.size() - 1);
	}

	size_t lastSlash = parentDirectoryName.rfind(DIRECTORY_SEPARATOR_ASCHAR);

	if(lastSlash == std::string::npos)
	{
		// might be a forward slash separator, 
		// especially in the unit tests!
		lastSlash = parentDirectoryName.rfind('/');
	}

	if(lastSlash != std::string::npos)
	{
		// the target directory is a deep path, remove the last
		// directory name and check that the resulting parent
		// exists, otherwise the restore should fail.
		parentDirectoryName.resize(lastSlash);

		int parentExists;

		try
		{
			parentExists = ObjectExists(parentDirectoryName.c_str());
		}
		catch (BoxException &e)
		{
			::syslog(LOG_ERR, "Failed to check existence for %s: "
				"%s", parentDirectoryName.c_str(), e.what());
			return Restore_UnknownError;
		}
		catch(std::exception &e)
		{
			::syslog(LOG_ERR, "Failed to check existence for %s: "
				"%s", parentDirectoryName.c_str(), e.what());
			return Restore_UnknownError;
		}
		catch(...)
		{
			::syslog(LOG_ERR, "Failed to check existence for %s: "
				"unknown error", parentDirectoryName.c_str());
			return Restore_UnknownError;
		}

		switch(parentExists)
		{
			case ObjectExists_Dir:
				// this is fine, do nothing
				break;

			case ObjectExists_File:
				fprintf(stderr, "Failed to restore: '%s' "
					"is a file, but should be a "
					"directory.\n", 
					parentDirectoryName.c_str());
				return Restore_TargetPathNotFound;

			case ObjectExists_NoObject:
				fprintf(stderr, "Failed to restore: "
					"parent '%s' of target directory "
					"does not exist.\n",
					parentDirectoryName.c_str());
				return Restore_TargetPathNotFound;

			default:
				fprintf(stderr, "Failed to restore: "
					"unknown result from "
					"ObjectExists('%s').\n",
					parentDirectoryName.c_str());
				return Restore_UnknownError;
		}
	}

	if((exists == ObjectExists_NoObject ||
		exists == ObjectExists_File) && 
		::mkdir(rLocalDirectoryName.c_str(), S_IRWXU) != 0)
	{
		::syslog(LOG_ERR, "Failed to create directory %s: %s",
			rLocalDirectoryName.c_str(),
			strerror(errno));
		return Restore_UnknownError;
	}

	// Save the restore info, in case it's needed later
	try
	{
		Params.mResumeInfo.Save(Params.mRestoreResumeInfoFilename);
	}
	catch (BoxException &e)
	{
		::syslog(LOG_ERR, "Failed to save resume info file %s: %s", 
			Params.mRestoreResumeInfoFilename.c_str(), e.what());
		return Restore_UnknownError;
	}
	catch(std::exception &e)
	{
		::syslog(LOG_ERR, "Failed to save resume info file %s: %s", 
			Params.mRestoreResumeInfoFilename.c_str(), e.what());
		return Restore_UnknownError;
	}
	catch(...)
	{
		::syslog(LOG_ERR, "Failed to save resume info file %s: "
			"unknown error", 
			Params.mRestoreResumeInfoFilename.c_str());
		return Restore_UnknownError;
	}

	// Fetch the directory listing from the server -- getting a
	// list of files which is appropriate to the restore type
	rConnection.QueryListDirectory(
			DirectoryID,
			Params.RestoreDeleted?(BackupProtocolClientListDirectory::Flags_Deleted):(BackupProtocolClientListDirectory::Flags_INCLUDE_EVERYTHING),
			BackupProtocolClientListDirectory::Flags_OldVersion | (Params.RestoreDeleted?(0):(BackupProtocolClientListDirectory::Flags_Deleted)),
			true /* want attributes */);

	// Retrieve the directory from the stream following
	BackupStoreDirectory dir;
	std::auto_ptr<IOStream> dirstream(rConnection.ReceiveStream());
	dir.ReadFromStream(*dirstream, rConnection.GetTimeout());

	// Apply attributes to the directory
	const StreamableMemBlock &dirAttrBlock(dir.GetAttributes());
	BackupClientFileAttributes dirAttr(dirAttrBlock);

	try
	{
		dirAttr.WriteAttributes(rLocalDirectoryName.c_str());
	}
	catch (BoxException &e)
	{
		::syslog(LOG_ERR, "Failed to restore attributes for %s: %s", 
			rLocalDirectoryName.c_str(), e.what());
		return Restore_UnknownError;
	}
	catch(std::exception &e)
	{
		::syslog(LOG_ERR, "Failed to restore attributes for %s: %s", 
			rLocalDirectoryName.c_str(), e.what());
		return Restore_UnknownError;
	}
	catch(...)
	{
		::syslog(LOG_ERR, "Failed to restore attributes for %s: "
			"unknown error", rLocalDirectoryName.c_str());
		return Restore_UnknownError;
	}

	int64_t bytesWrittenSinceLastRestoreInfoSave = 0;
	
	// Process files
	{
		BackupStoreDirectory::Iterator i(dir);
		BackupStoreDirectory::Entry *en = 0;
		while((en = i.Next(BackupStoreDirectory::Entry::Flags_File)) != 0)
		{
			// Check ID hasn't already been done
			if(rLevel.mRestoredObjects.find(en->GetObjectID()) == rLevel.mRestoredObjects.end())
			{
				// Local name
				BackupStoreFilenameClear nm(en->GetName());
				std::string localFilename(rLocalDirectoryName + DIRECTORY_SEPARATOR_ASCHAR + nm.GetClearFilename());
				
				// Unlink anything which already exists -- for resuming restores, we can't overwrite files already there.
				if(::unlink(localFilename.c_str()) == 0)
				{
					::syslog(LOG_ERR, "Failed to delete "
						"file %s: %s",
						localFilename.c_str(),
						strerror(errno));
					return Restore_UnknownError;
				}
				
				// Request it from the store
				rConnection.QueryGetFile(DirectoryID, en->GetObjectID());
		
				// Stream containing encoded file
				std::auto_ptr<IOStream> objectStream(rConnection.ReceiveStream());
		
				// Decode the file -- need to do different things depending on whether 
				// the directory entry has additional attributes
				try
				{
					if(en->HasAttributes())
					{
						// Use these attributes
						const StreamableMemBlock &storeAttr(en->GetAttributes());
						BackupClientFileAttributes attr(storeAttr);
						BackupStoreFile::DecodeFile(*objectStream, localFilename.c_str(), rConnection.GetTimeout(), &attr);
					}
					else
					{
						// Use attributes stored in file
						BackupStoreFile::DecodeFile(*objectStream, localFilename.c_str(), rConnection.GetTimeout());
					}
				}
				catch (BoxException &e)
				{
					::syslog(LOG_ERR, "Failed to restore "
						"file %s: %s", 
						localFilename.c_str(), 
						e.what());
					return Restore_UnknownError;
				}
				catch(std::exception &e)
				{
					::syslog(LOG_ERR, "Failed to restore "
						"file %s: %s", 
						localFilename.c_str(), 
						e.what());
					return Restore_UnknownError;
				}
				catch(...)
				{
					::syslog(LOG_ERR, "Failed to restore "
						"file %s: unknown error", 
						localFilename.c_str());
					return Restore_UnknownError;
				}
				
				// Progress display?
				if(Params.PrintDots)
				{
					printf(".");
					fflush(stdout);
				}

				// Add it to the list of done itmes
				rLevel.mRestoredObjects.insert(en->GetObjectID());
				
				// Save restore info?
				int64_t fileSize;
				int exists;

				try
				{
					exists = FileExists(
						localFilename.c_str(),
						&fileSize, 
						true /* treat links as not 
							existing */);
				}
				catch (BoxException &e)
				{
					::syslog(LOG_ERR, "Failed to determine "
						"whether file exists: %s: %s", 
						localFilename.c_str(), 
						e.what());
					return Restore_UnknownError;
				}
				catch(std::exception &e)
				{
					::syslog(LOG_ERR, "Failed to determine "
						"whether file exists: %s: %s", 
						localFilename.c_str(), 
						e.what());
					return Restore_UnknownError;
				}
				catch(...)
				{
					::syslog(LOG_ERR, "Failed to determine "
						"whether file exists: %s: "
						"unknown error", 
						localFilename.c_str());
					return Restore_UnknownError;
				}

				if(exists)
				{
					// File exists...
					bytesWrittenSinceLastRestoreInfoSave += fileSize;
					
					if(bytesWrittenSinceLastRestoreInfoSave > MAX_BYTES_WRITTEN_BETWEEN_RESTORE_INFO_SAVES)
					{
						// Save the restore info, in case it's needed later
						try
						{
							Params.mResumeInfo.Save(Params.mRestoreResumeInfoFilename);
						}
						catch (BoxException &e)
						{
							::syslog(LOG_ERR, "Failed to save resume info file %s: %s", 
								Params.mRestoreResumeInfoFilename.c_str(), e.what());
							return Restore_UnknownError;
						}
						catch(std::exception &e)
						{
							::syslog(LOG_ERR, "Failed to save resume info file %s: %s", 
								Params.mRestoreResumeInfoFilename.c_str(), e.what());
							return Restore_UnknownError;
						}
						catch(...)
						{
							::syslog(LOG_ERR, "Failed to save resume info file %s: "
								"unknown error", 
								Params.mRestoreResumeInfoFilename.c_str());
							return Restore_UnknownError;
						}

						bytesWrittenSinceLastRestoreInfoSave = 0;
					}
				}
			}
		}
	}

	// Make sure the restore info has been saved	
	if(bytesWrittenSinceLastRestoreInfoSave != 0)
	{
		// Save the restore info, in case it's needed later
		try
		{
			Params.mResumeInfo.Save(
				Params.mRestoreResumeInfoFilename);
		}
		catch (BoxException &e)
		{
			::syslog(LOG_ERR, "Failed to save resume info file "
				"%s: %s", 
				Params.mRestoreResumeInfoFilename.c_str(),
				e.what());
			return Restore_UnknownError;
		}
		catch(std::exception &e)
		{
			::syslog(LOG_ERR, "Failed to save resume info file "
				"%s: %s", 
				Params.mRestoreResumeInfoFilename.c_str(),
				e.what());
			return Restore_UnknownError;
		}
		catch(...)
		{
			::syslog(LOG_ERR, "Failed to save resume info file "
				"%s: unknown error", 
				Params.mRestoreResumeInfoFilename.c_str());
			return Restore_UnknownError;
		}
		
		bytesWrittenSinceLastRestoreInfoSave = 0;
	}

	
	// Recurse to directories
	{
		BackupStoreDirectory::Iterator i(dir);
		BackupStoreDirectory::Entry *en = 0;
		while((en = i.Next(BackupStoreDirectory::Entry::Flags_Dir)) != 0)
		{
			// Check ID hasn't already been done
			if(rLevel.mRestoredObjects.find(en->GetObjectID()) == rLevel.mRestoredObjects.end())
			{
				// Local name
				BackupStoreFilenameClear nm(en->GetName());
				std::string localDirname(rLocalDirectoryName + DIRECTORY_SEPARATOR_ASCHAR + nm.GetClearFilename());
				
				// Add the level for the next entry
				RestoreResumeInfo &rnextLevel(rLevel.AddLevel(en->GetObjectID(), nm.GetClearFilename()));
				
				// Recurse
				int result = BackupClientRestoreDir(
					rConnection, en->GetObjectID(), 
					localDirname, Params, rnextLevel);

				if (result != Restore_Complete)
				{
					return result;
				}
				
				// Remove the level for the above call
				rLevel.RemoveLevel();
				
				// Add it to the list of done itmes
				rLevel.mRestoredObjects.insert(en->GetObjectID());
			}
		}
	}

	return Restore_Complete;
}


// --------------------------------------------------------------------------
//
// Function
//		Name:    BackupClientRestore(BackupProtocolClient &, int64_t, const char *, bool, bool)
//		Purpose: Restore a directory on the server to a local directory on the disc.
//
//				 The local directory must not already exist.
//
//				 If a restore is aborted for any reason, then it may be resumed if
//				 Resume == true. If Resume == false and resumption is possible, then
//				 Restore_ResumePossible is returned.
//
//				 Set RestoreDeleted to restore a deleted directory. This may not give the
//				 directory structure when it was deleted, because files may have been deleted
//				 within it before it was deleted.
//
//				 Returns Restore_TargetExists if the target directory exists, but
//				 there is no restore possible. (Won't attempt to overwrite things.)
//
//				 Returns Restore_Complete on success. (Exceptions on error.)
//		Created: 23/11/03
//
// --------------------------------------------------------------------------
int BackupClientRestore(BackupProtocolClient &rConnection, int64_t DirectoryID, const char *LocalDirectoryName,
	bool PrintDots, bool RestoreDeleted, bool UndeleteAfterRestoreDeleted, bool Resume)
{
	// Parameter block
	RestoreParams params;
	params.PrintDots = PrintDots;
	params.RestoreDeleted = RestoreDeleted;
	params.mRestoreResumeInfoFilename = LocalDirectoryName;
	params.mRestoreResumeInfoFilename += ".boxbackupresume";

	// Target exists?
	int targetExistance = ObjectExists(LocalDirectoryName);

	// Does any resumption information exist?
	bool doingResume = false;
	if(FileExists(params.mRestoreResumeInfoFilename.c_str()) && targetExistance == ObjectExists_Dir)
	{
		if(!Resume)
		{
			// Caller didn't specify that resume should be done, so refuse to do it
			// but say why.
			return Restore_ResumePossible;
		}
		
		// Attempt to load the resume info file
		if(!params.mResumeInfo.Load(params.mRestoreResumeInfoFilename))
		{
			// failed -- bad file, so things have gone a bit wrong
			return Restore_TargetExists;
		}
		
		// Flag as doing resume so next check isn't actually performed
		doingResume = true;
	}

	// Does the directory already exist?
	if(targetExistance != ObjectExists_NoObject && !doingResume)
	{
		// Don't do anything in this case!
		return Restore_TargetExists;
	}
	
	// Restore the directory
	std::string localName(LocalDirectoryName);
	int result = BackupClientRestoreDir(rConnection, DirectoryID, 
		localName, params, params.mResumeInfo);
	if (result != Restore_Complete)
	{
		return result;
	}

	// Undelete the directory on the server?
	if(RestoreDeleted && UndeleteAfterRestoreDeleted)
	{
		// Send the command
		rConnection.QueryUndeleteDirectory(DirectoryID);
	}

	// Finish progress display?
	if(PrintDots)
	{
		printf("\n");
		fflush(stdout);
	}
	
	// Delete the resume information file
	::unlink(params.mRestoreResumeInfoFilename.c_str());

	return Restore_Complete;
}