[Box Backup-commit] COMMIT r2245 - in box/trunk: bin/bbackupd lib/backupclient lib/common

boxbackup-dev at fluffy.co.uk boxbackup-dev at fluffy.co.uk
Thu Aug 21 11:41:18 BST 2008


Author: chris
Date: 2008-08-21 11:41:18 +0100 (Thu, 21 Aug 2008)
New Revision: 2245

Modified:
   box/trunk/bin/bbackupd/BackupClientContext.cpp
   box/trunk/bin/bbackupd/BackupClientContext.h
   box/trunk/bin/bbackupd/BackupClientDirectoryRecord.cpp
   box/trunk/bin/bbackupd/BackupClientDirectoryRecord.h
   box/trunk/bin/bbackupd/BackupDaemon.cpp
   box/trunk/bin/bbackupd/BackupDaemon.h
   box/trunk/lib/backupclient/BackupStoreFile.cpp
   box/trunk/lib/backupclient/BackupStoreFile.h
   box/trunk/lib/backupclient/BackupStoreFileEncodeStream.cpp
   box/trunk/lib/backupclient/BackupStoreFileEncodeStream.h
   box/trunk/lib/common/ReadLoggingStream.cpp
   box/trunk/lib/common/ReadLoggingStream.h
Log:
Pass a RunStatusProvider and a ReadLoggingStream::Logger from 
BackupDaemon through BackupClientDirectoryRecord, BackupStoreFile and 
BackupStoreFileEncodeStream to ReadLoggingStream, to allow progress 
callbacks during file upload and cancelling upload part-way.

Implement ReadLoggingStream::Logger in 
BackupClientDirectoryRecord::SyncParams, which thunks the notifications 
back to the ProgressNotifier.

Add the SysadminNotifier interface from Boxi.

Add NotifyIDMapsSetup() to ProgressNotifier.

Change BackupClientDirectoryRecord::SyncParams to store references to 
the individual callback interfaces rather than BackupDaemon.

Initialise all members in BackupDaemon.

Add ability for BackupDaemon user to override the ProgressNotifier, 
LocationResolver, SysadminNotifier and RunStatusProvider that will be 
used during the backup.

Make BackupDaemon::Location class public and provide access to the 
configured locations for Boxi (dangerous, they could be modified without 
BackupDaemon knowing it).


Modified: box/trunk/bin/bbackupd/BackupClientContext.cpp
===================================================================
--- box/trunk/bin/bbackupd/BackupClientContext.cpp	2008-08-21 10:20:13 UTC (rev 2244)
+++ box/trunk/bin/bbackupd/BackupClientContext.cpp	2008-08-21 10:41:18 UTC (rev 2245)
@@ -41,7 +41,7 @@
 // --------------------------------------------------------------------------
 BackupClientContext::BackupClientContext
 (
-	BackupDaemon &rDaemon, 
+	LocationResolver &rResolver, 
 	TLSContext &rTLSContext, 
 	const std::string &rHostname,
 	int Port,
@@ -51,7 +51,7 @@
 	std::string ExtendedLogFile,
 	ProgressNotifier& rProgressNotifier
 )
-	: mrDaemon(rDaemon),
+	: mrResolver(rResolver),
 	  mrTLSContext(rTLSContext),
 	  mHostname(rHostname),
 	  mPort(Port),
@@ -470,7 +470,7 @@
 		{
 			// Location name -- look up in daemon's records
 			std::string locPath;
-			if(!mrDaemon.FindLocationPathName(elementName.GetClearFilename(), locPath))
+			if(!mrResolver.FindLocationPathName(elementName.GetClearFilename(), locPath))
 			{
 				// Didn't find the location... so can't give the local filename
 				return false;

Modified: box/trunk/bin/bbackupd/BackupClientContext.h
===================================================================
--- box/trunk/bin/bbackupd/BackupClientContext.h	2008-08-21 10:20:13 UTC (rev 2244)
+++ box/trunk/bin/bbackupd/BackupClientContext.h	2008-08-21 10:41:18 UTC (rev 2245)
@@ -29,6 +29,24 @@
 // --------------------------------------------------------------------------
 //
 // Class
+//		Name:    LocationResolver
+//		Purpose: Interface for classes that can resolve locations to paths,
+//		         like BackupDaemon
+//		Created: 2003/10/08
+//
+// --------------------------------------------------------------------------
+class LocationResolver
+{
+public:
+	virtual ~LocationResolver() { }
+	virtual bool FindLocationPathName(const std::string &rLocationName, 
+		std::string &rPathOut) const = 0;
+};
+
+
+// --------------------------------------------------------------------------
+//
+// Class
 //		Name:    BackupClientContext
 //		Purpose: 
 //		Created: 2003/10/08
@@ -39,7 +57,7 @@
 public:
 	BackupClientContext
 	(
-		BackupDaemon &rDaemon, 
+		LocationResolver &rResolver, 
 		TLSContext &rTLSContext, 
 		const std::string &rHostname,
 		int32_t Port,
@@ -207,7 +225,7 @@
 	}
 
 private:
-	BackupDaemon &mrDaemon;
+	LocationResolver &mrResolver;
 	TLSContext &mrTLSContext;
 	std::string mHostname;
 	int mPort;

Modified: box/trunk/bin/bbackupd/BackupClientDirectoryRecord.cpp
===================================================================
--- box/trunk/bin/bbackupd/BackupClientDirectoryRecord.cpp	2008-08-21 10:20:13 UTC (rev 2244)
+++ box/trunk/bin/bbackupd/BackupClientDirectoryRecord.cpp	2008-08-21 10:41:18 UTC (rev 2245)
@@ -1482,7 +1482,9 @@
 			// Prepare to upload, getting a stream which will encode the file as we go along
 			std::auto_ptr<IOStream> upload(
 				BackupStoreFile::EncodeFile(rFilename.c_str(),
-					mObjectID, rStoreFilename));
+					mObjectID, rStoreFilename, NULL,
+					&rParams,
+					&(rParams.mrRunStatusProvider)));
 		
 			// Send to store
 			std::auto_ptr<BackupProtocolClientSuccess> stored(

Modified: box/trunk/bin/bbackupd/BackupClientDirectoryRecord.h
===================================================================
--- box/trunk/bin/bbackupd/BackupClientDirectoryRecord.h	2008-08-21 10:20:13 UTC (rev 2244)
+++ box/trunk/bin/bbackupd/BackupClientDirectoryRecord.h	2008-08-21 10:41:18 UTC (rev 2245)
@@ -13,10 +13,12 @@
 #include <string>
 #include <map>
 
-#include "BoxTime.h"
 #include "BackupClientFileAttributes.h"
 #include "BackupStoreDirectory.h"
+#include "BoxTime.h"
 #include "MD5Digest.h"
+#include "ReadLoggingStream.h"
+#include "RunStatusProvider.h"
 
 class Archive;
 class BackupClientContext;
@@ -25,6 +27,21 @@
 // --------------------------------------------------------------------------
 //
 // Class
+//		Name:    SysadminNotifier
+//		Purpose: Provides a NotifySysadmin() method to send mail to the sysadmin
+//		Created: 2005/11/15
+//
+// --------------------------------------------------------------------------
+class SysadminNotifier
+{
+	public:
+	virtual ~SysadminNotifier() { }
+	virtual void NotifySysadmin(int Event) = 0;
+};
+
+// --------------------------------------------------------------------------
+//
+// Class
 //		Name:    ProgressNotifier
 //		Purpose: Provides methods for the backup library to inform the user
 //		         interface about its progress with the backup
@@ -37,6 +54,7 @@
 {
 	public:
 	virtual ~ProgressNotifier() { }
+	virtual void NotifyIDMapsSetup(BackupClientContext& rContext) = 0;
 	virtual void NotifyScanDirectory(
 		const BackupClientDirectoryRecord* pDirRecord,
 		const std::string& rLocalPath) = 0;
@@ -102,6 +120,11 @@
 	virtual void NotifyFileDeleted(
 		int64_t ObjectID,
 		const std::string& rRemotePath) = 0;
+	virtual void NotifyReadProgress(int64_t readSize, int64_t offset,
+		int64_t length, box_time_t elapsed, box_time_t finish) = 0;
+	virtual void NotifyReadProgress(int64_t readSize, int64_t offset,
+		int64_t length) = 0;
+	virtual void NotifyReadProgress(int64_t readSize, int64_t offset) = 0;
 };
 
 // --------------------------------------------------------------------------
@@ -138,11 +161,13 @@
 	//		Created: 8/3/04
 	//
 	// --------------------------------------------------------------------------
-	class SyncParams
+	class SyncParams : public ReadLoggingStream::Logger
 	{
 	public:
 		SyncParams(
-			BackupDaemon &rDaemon,
+			RunStatusProvider &rRunStatusProvider, 
+			SysadminNotifier &rSysadminNotifier,
+			ProgressNotifier &rProgressNotifier,
 			BackupClientContext &rContext);
 		~SyncParams();
 	private:
@@ -158,13 +183,43 @@
 		box_time_t mMaxFileTimeInFuture;
 		int32_t mFileTrackingSizeThreshold;
 		int32_t mDiffingUploadSizeThreshold;
-		BackupDaemon &mrDaemon;
+		RunStatusProvider &mrRunStatusProvider;
+		SysadminNotifier &mrSysadminNotifier;
+		ProgressNotifier &mrProgressNotifier;
 		BackupClientContext &mrContext;
 		bool mReadErrorsOnFilesystemObjects;
 		
 		// Member variables modified by syncing process
 		box_time_t mUploadAfterThisTimeInTheFuture;
 		bool mHaveLoggedWarningAboutFutureFileTimes;
+	
+		bool StopRun() { return mrRunStatusProvider.StopRun(); }
+		void NotifySysadmin(int Event) 
+		{ 
+			mrSysadminNotifier.NotifySysadmin(Event); 
+		}
+		ProgressNotifier& GetProgressNotifier() const 
+		{ 
+			return mrProgressNotifier;
+		}
+		
+		/* ReadLoggingStream::Logger implementation */
+		virtual void Log(int64_t readSize, int64_t offset,
+			int64_t length, box_time_t elapsed, box_time_t finish)
+		{
+			mrProgressNotifier.NotifyReadProgress(readSize, offset,
+				length, elapsed, finish);
+		}
+		virtual void Log(int64_t readSize, int64_t offset,
+			int64_t length)
+		{
+			mrProgressNotifier.NotifyReadProgress(readSize, offset,
+				length);
+		}
+		virtual void Log(int64_t readSize, int64_t offset)
+		{
+			mrProgressNotifier.NotifyReadProgress(readSize, offset);
+		}
 	};
 
 	void SyncDirectory(SyncParams &rParams,

Modified: box/trunk/bin/bbackupd/BackupDaemon.cpp
===================================================================
--- box/trunk/bin/bbackupd/BackupDaemon.cpp	2008-08-21 10:20:13 UTC (rev 2244)
+++ box/trunk/bin/bbackupd/BackupDaemon.cpp	2008-08-21 10:41:18 UTC (rev 2245)
@@ -49,34 +49,33 @@
 
 #include "SSLLib.h"
 
+#include "autogen_BackupProtocolClient.h"
+#include "autogen_ClientException.h"
+#include "autogen_ConversionException.h"
+#include "Archive.h"
+#include "BackupClientContext.h"
+#include "BackupClientCryptoKeys.h"
+#include "BackupClientDirectoryRecord.h"
+#include "BackupClientFileAttributes.h"
+#include "BackupClientInodeToIDMap.h"
+#include "BackupClientMakeExcludeList.h"
 #include "BackupDaemon.h"
 #include "BackupDaemonConfigVerify.h"
-#include "BackupClientContext.h"
-#include "BackupClientDirectoryRecord.h"
+#include "BackupStoreConstants.h"
 #include "BackupStoreDirectory.h"
-#include "BackupClientFileAttributes.h"
+#include "BackupStoreException.h"
+#include "BackupStoreFile.h"
 #include "BackupStoreFilenameClear.h"
-#include "BackupClientInodeToIDMap.h"
-#include "autogen_BackupProtocolClient.h"
-#include "autogen_ConversionException.h"
-#include "BackupClientCryptoKeys.h"
 #include "BannerText.h"
-#include "BackupStoreFile.h"
-#include "Random.h"
+#include "Conversion.h"
 #include "ExcludeList.h"
-#include "BackupClientMakeExcludeList.h"
+#include "FileStream.h"
 #include "IOStreamGetLine.h"
-#include "Utils.h"
-#include "FileStream.h"
-#include "BackupStoreException.h"
-#include "BackupStoreConstants.h"
 #include "LocalProcessStream.h"
-#include "IOStreamGetLine.h"
-#include "Conversion.h"
-#include "Archive.h"
+#include "Logging.h"
+#include "Random.h"
 #include "Timer.h"
-#include "Logging.h"
-#include "autogen_ClientException.h"
+#include "Utils.h"
 
 #ifdef WIN32
 	#include "Win32ServiceFunctions.h"
@@ -122,13 +121,23 @@
 // --------------------------------------------------------------------------
 BackupDaemon::BackupDaemon()
 	: mState(BackupDaemon::State_Initialising),
+	  mDeleteRedundantLocationsAfter(0),
 	  mpCommandSocketInfo(0),
 	  mDeleteUnusedRootDirEntriesAfter(0),
 	  mClientStoreMarker(BackupClientContext::ClientStoreMarker_NotKnown),
 	  mStorageLimitExceeded(false),
 	  mReadErrorsOnFilesystemObjects(false),
 	  mLastSyncTime(0),
-	  mLogAllFileAccess(false)
+	  mNextSyncTime(0),
+	  mCurrentSyncStartTime(0),
+	  mUpdateStoreInterval(0),
+	  mDeleteStoreObjectInfoFile(false),
+	  mDoSyncForcedByPreviousSyncError(false),
+	  mLogAllFileAccess(false),
+	  mpProgressNotifier(this),
+	  mpLocationResolver(this),
+	  mpRunStatusProvider(this),
+	  mpSysadminNotifier(this)
 	#ifdef WIN32
 	, mInstallService(false),
 	  mRemoveService(false),
@@ -1078,14 +1087,14 @@
 	// just connect, as this may be unnecessary)
 	BackupClientContext clientContext
 	(
-		*this, 
+		*mpLocationResolver, 
 		mTlsContext, 
 		conf.GetKeyValue("StoreHostname"),
 		conf.GetKeyValueInt("StorePort"),
 		conf.GetKeyValueInt("AccountNumber"), 
 		conf.GetKeyValueBool("ExtendedLogging"),
 		conf.KeyExists("ExtendedLogFile"),
-		extendedLogFile, *this
+		extendedLogFile, *mpProgressNotifier
 	);
 		
 	// The minimum age a file needs to be before it will be
@@ -1158,8 +1167,8 @@
 	}
 
 	// Set up the sync parameters
-	BackupClientDirectoryRecord::SyncParams params(
-		*this, clientContext);
+	BackupClientDirectoryRecord::SyncParams params(*mpRunStatusProvider,
+		*mpSysadminNotifier, *mpProgressNotifier, clientContext);
 	params.mSyncPeriodStart = syncPeriodStart;
 	params.mSyncPeriodEnd = syncPeriodEndExtended;
 	// use potentially extended end time
@@ -1214,6 +1223,8 @@
 		SetupLocations(clientContext, locations);
 	}
 	
+	mpProgressNotifier->NotifyIDMapsSetup(clientContext);
+	
 	// Get some ID maps going
 	SetupIDMapsForSync();
 

Modified: box/trunk/bin/bbackupd/BackupDaemon.h
===================================================================
--- box/trunk/bin/bbackupd/BackupDaemon.h	2008-08-21 10:20:13 UTC (rev 2244)
+++ box/trunk/bin/bbackupd/BackupDaemon.h	2008-08-21 10:41:18 UTC (rev 2245)
@@ -14,6 +14,7 @@
 #include <string>
 #include <memory>
 
+#include "BackupClientContext.h"
 #include "BackupClientDirectoryRecord.h"
 #include "BoxTime.h"
 #include "Daemon.h"
@@ -45,7 +46,8 @@
 //		Created: 2003/10/08
 //
 // --------------------------------------------------------------------------
-class BackupDaemon : public Daemon, ProgressNotifier
+class BackupDaemon : public Daemon, ProgressNotifier, LocationResolver,
+RunStatusProvider, SysadminNotifier
 {
 public:
 	BackupDaemon();
@@ -153,7 +155,7 @@
 
 	int UseScriptToSeeIfSyncAllowed();
 
-private:
+public:
 	class Location
 	{
 	public:
@@ -173,7 +175,11 @@
 		ExcludeList *mpExcludeFiles;
 		ExcludeList *mpExcludeDirs;
 	};
-
+	
+	typedef const std::vector<Location *> Locations;
+	Locations GetLocations() { return mLocations; }
+	
+private:
 	int mState;		// what the daemon is currently doing
 
 	std::vector<Location *> mLocations;
@@ -229,8 +235,26 @@
 private:
 	bool mLogAllFileAccess;
 
+public:
+	ProgressNotifier*  GetProgressNotifier()  { return mpProgressNotifier; }
+	LocationResolver*  GetLocationResolver()  { return mpLocationResolver; }
+	RunStatusProvider* GetRunStatusProvider() { return mpRunStatusProvider; }
+	SysadminNotifier*  GetSysadminNotifier()  { return mpSysadminNotifier; }
+	void SetProgressNotifier (ProgressNotifier*  p) { mpProgressNotifier = p; }
+	void SetLocationResolver (LocationResolver*  p) { mpLocationResolver = p; }
+	void SetRunStatusProvider(RunStatusProvider* p) { mpRunStatusProvider = p; }
+	void SetSysadminNotifier (SysadminNotifier*  p) { mpSysadminNotifier = p; }
+		
+private:
+	ProgressNotifier* mpProgressNotifier;
+	LocationResolver* mpLocationResolver;
+	RunStatusProvider* mpRunStatusProvider;
+	SysadminNotifier* mpSysadminNotifier;
+	
  	/* ProgressNotifier implementation */
 public:
+	virtual void NotifyIDMapsSetup(BackupClientContext& rContext) { }
+
  	virtual void NotifyScanDirectory(
  		const BackupClientDirectoryRecord* pDirRecord,
  		const std::string& rLocalPath) 
@@ -467,6 +491,24 @@
 				")");
 		}
 	}
+	virtual void NotifyReadProgress(int64_t readSize, int64_t offset,
+		int64_t length, box_time_t elapsed, box_time_t finish)
+	{
+		BOX_TRACE("Read " << readSize << " bytes at " << offset << 
+			", " << (length - offset) << " remain, eta " <<
+			BoxTimeToSeconds(finish - elapsed) << "s");
+	}
+	virtual void NotifyReadProgress(int64_t readSize, int64_t offset,
+		int64_t length)
+	{
+		BOX_TRACE("Read " << readSize << " bytes at " << offset << 
+			", " << (length - offset) << " remain");
+	}
+	virtual void NotifyReadProgress(int64_t readSize, int64_t offset)
+	{
+		BOX_TRACE("Read " << readSize << " bytes at " << offset << 
+			", unknown bytes remaining");
+	}
 
 #ifdef WIN32
 	public:

Modified: box/trunk/lib/backupclient/BackupStoreFile.cpp
===================================================================
--- box/trunk/lib/backupclient/BackupStoreFile.cpp	2008-08-21 10:20:13 UTC (rev 2244)
+++ box/trunk/lib/backupclient/BackupStoreFile.cpp	2008-08-21 10:41:18 UTC (rev 2245)
@@ -65,22 +65,27 @@
 // Function
 //		Name:    BackupStoreFile::EncodeFile(IOStream &, IOStream &)
 //		Purpose: Encode a file into something for storing on file server.
-//				 Requires a real filename so full info can be stored.
+//			 Requires a real filename so full info can be stored.
 //
-//				 Returns a stream. Most of the work is done by the stream
-//				 when data is actually requested -- the file will be held
-//				 open until the stream is deleted or the file finished.
+//			 Returns a stream. Most of the work is done by the stream
+//			 when data is actually requested -- the file will be held
+//			 open until the stream is deleted or the file finished.
 //		Created: 2003/08/28
 //
 // --------------------------------------------------------------------------
-std::auto_ptr<IOStream> BackupStoreFile::EncodeFile(const char *Filename, int64_t ContainerID, const BackupStoreFilename &rStoreFilename, int64_t *pModificationTime)
+std::auto_ptr<IOStream> BackupStoreFile::EncodeFile(const char *Filename,
+	int64_t ContainerID, const BackupStoreFilename &rStoreFilename,
+	int64_t *pModificationTime, ReadLoggingStream::Logger* pLogger,
+	RunStatusProvider* pRunStatusProvider)
 {
 	// Create the stream
 	std::auto_ptr<IOStream> stream(new BackupStoreFileEncodeStream);
 
 	// Do the initial setup
-	((BackupStoreFileEncodeStream*)stream.get())->Setup(Filename, 0 /* no recipe, just encode */,
-		ContainerID, rStoreFilename, pModificationTime);
+	((BackupStoreFileEncodeStream*)stream.get())->Setup(Filename,
+		0 /* no recipe, just encode */,
+		ContainerID, rStoreFilename, pModificationTime, pLogger,
+		pRunStatusProvider);
 	
 	// Return the stream for the caller
 	return stream;

Modified: box/trunk/lib/backupclient/BackupStoreFile.h
===================================================================
--- box/trunk/lib/backupclient/BackupStoreFile.h	2008-08-21 10:20:13 UTC (rev 2244)
+++ box/trunk/lib/backupclient/BackupStoreFile.h	2008-08-21 10:41:18 UTC (rev 2245)
@@ -14,9 +14,11 @@
 
 #include <memory>
 
-#include "IOStream.h"
 #include "BackupClientFileAttributes.h"
 #include "BackupStoreFilename.h"
+#include "IOStream.h"
+#include "ReadLoggingStream.h"
+#include "RunStatusProvider.h"
 
 typedef struct 
 {
@@ -116,7 +118,11 @@
 
 
 	// Main interface
-	static std::auto_ptr<IOStream> EncodeFile(const char *Filename, int64_t ContainerID, const BackupStoreFilename &rStoreFilename, int64_t *pModificationTime = 0);
+	static std::auto_ptr<IOStream> EncodeFile(const char *Filename,
+		int64_t ContainerID, const BackupStoreFilename &rStoreFilename,
+		int64_t *pModificationTime = 0,
+		ReadLoggingStream::Logger* pLogger = NULL,
+		RunStatusProvider* pRunStatusProvider = NULL);
 	static std::auto_ptr<IOStream> EncodeFileDiff
 	(
 		const char *Filename, int64_t ContainerID,

Modified: box/trunk/lib/backupclient/BackupStoreFileEncodeStream.cpp
===================================================================
--- box/trunk/lib/backupclient/BackupStoreFileEncodeStream.cpp	2008-08-21 10:20:13 UTC (rev 2244)
+++ box/trunk/lib/backupclient/BackupStoreFileEncodeStream.cpp	2008-08-21 10:41:18 UTC (rev 2245)
@@ -11,18 +11,18 @@
 
 #include <string.h>
 
+#include "BackupClientFileAttributes.h"
+#include "BackupStoreConstants.h"
+#include "BackupStoreException.h"
+#include "BackupStoreFile.h"
+#include "BackupStoreFileCryptVar.h"
 #include "BackupStoreFileEncodeStream.h"
-#include "BackupStoreFile.h"
 #include "BackupStoreFileWire.h"
-#include "BackupStoreFileCryptVar.h"
 #include "BackupStoreObjectMagic.h"
-#include "BackupStoreException.h"
-#include "BackupStoreConstants.h"
 #include "BoxTime.h"
-#include "BackupClientFileAttributes.h"
 #include "FileStream.h"
+#include "Random.h"
 #include "RollingChecksum.h"
-#include "Random.h"
 
 #include "MemLeakFindOn.h"
 
@@ -41,6 +41,7 @@
 	: mpRecipe(0),
 	  mpFile(0),
 	  mpLogging(0),
+	  mpRunStatusProvider(NULL),
 	  mStatus(Status_Header),
 	  mSendData(true),
 	  mTotalBlocks(0),
@@ -107,8 +108,11 @@
 //		Created: 8/12/03
 //
 // --------------------------------------------------------------------------
-void BackupStoreFileEncodeStream::Setup(const char *Filename, BackupStoreFileEncodeStream::Recipe *pRecipe,
-	int64_t ContainerID, const BackupStoreFilename &rStoreFilename, int64_t *pModificationTime)
+void BackupStoreFileEncodeStream::Setup(const char *Filename,
+	BackupStoreFileEncodeStream::Recipe *pRecipe,
+	int64_t ContainerID, const BackupStoreFilename &rStoreFilename,
+	int64_t *pModificationTime, ReadLoggingStream::Logger* pLogger,
+	RunStatusProvider* pRunStatusProvider)
 {
 	// Pointer to a blank recipe which we might create
 	BackupStoreFileEncodeStream::Recipe *pblankRecipe = 0;
@@ -128,9 +132,9 @@
 			pblankRecipe = new BackupStoreFileEncodeStream::Recipe(0, 0);
 			
 			BackupStoreFileEncodeStream::RecipeInstruction instruction;
-			instruction.mSpaceBefore = fileSize; 	// whole file
-			instruction.mBlocks = 0;				// no blocks
-			instruction.mpStartBlock = 0;			// no block
+			instruction.mSpaceBefore = fileSize; // whole file
+			instruction.mBlocks = 0; // no blocks
+			instruction.mpStartBlock = 0; // no block
 			pblankRecipe->push_back(instruction);		
 
 			pRecipe = pblankRecipe;
@@ -210,8 +214,18 @@
 			// Open the file
 			mpFile = new FileStream(Filename);
 
-			// Create logging stream
-			mpLogging = new ReadLoggingStream(*mpFile);
+			if (pLogger)
+			{
+				// Create logging stream
+				mpLogging = new ReadLoggingStream(*mpFile,
+					*pLogger);
+			}
+			else
+			{
+				// re-use FileStream instead
+				mpLogging = mpFile;
+				mpFile = NULL;
+			}
 		
 			// Work out the largest possible block required for the encoded data
 			mAllocatedBufferSize = BackupStoreFile::MaxBlockSizeForChunkSize(maxBlockClearSize);
@@ -259,6 +273,8 @@
 		}
 		throw;
 	}
+	
+	mpRunStatusProvider = pRunStatusProvider;
 }
 
 
@@ -316,6 +332,11 @@
 	{
 		return 0;
 	}
+	
+	if(mpRunStatusProvider && mpRunStatusProvider->StopRun())
+	{
+		THROW_EXCEPTION(BackupStoreException, SignalReceived);
+	}
 
 	int bytesToRead = NBytes;
 	uint8_t *buffer = (uint8_t*)pBuffer;
@@ -531,22 +552,25 @@
 	ASSERT(blockRawSize < mAllocatedBufferSize);
 
 	// Check file open
-	if(mpFile == 0 || mpLogging == 0)
+	if(mpLogging == 0)
 	{
 		// File should be open, but isn't. So logical error.
 		THROW_EXCEPTION(BackupStoreException, Internal)
 	}
 	
 	// Read the data in
-	if(!mpLogging->ReadFullBuffer(mpRawBuffer, blockRawSize, 0 /* not interested in size if failure */))
+	if(!mpLogging->ReadFullBuffer(mpRawBuffer, blockRawSize,
+		0 /* not interested in size if failure */))
 	{
-		// TODO: Do something more intelligent, and abort this upload because the file
-		// has changed
-		THROW_EXCEPTION(BackupStoreException, Temp_FileEncodeStreamDidntReadBuffer)
+		// TODO: Do something more intelligent, and abort
+		// this upload because the file has changed.
+		THROW_EXCEPTION(BackupStoreException,
+			Temp_FileEncodeStreamDidntReadBuffer)
 	}
 	
 	// Encode it
-	mCurrentBlockEncodedSize = BackupStoreFile::EncodeChunk(mpRawBuffer, blockRawSize, mEncodedBuffer);
+	mCurrentBlockEncodedSize = BackupStoreFile::EncodeChunk(mpRawBuffer,
+		blockRawSize, mEncodedBuffer);
 	
 	//TRACE2("Encode: Encoded size of block %d is %d\n", (int32_t)mCurrentBlock, (int32_t)mCurrentBlockEncodedSize);
 	
@@ -557,7 +581,8 @@
 	strongChecksum.Finish();
 
 	// Add entry to the index
-	StoreBlockIndexEntry(mCurrentBlockEncodedSize, blockRawSize, weakChecksum.GetChecksum(), strongChecksum.DigestAsData());
+	StoreBlockIndexEntry(mCurrentBlockEncodedSize, blockRawSize,
+		weakChecksum.GetChecksum(), strongChecksum.DigestAsData());
 	
 	// Set vars to reading this block
 	mPositionInCurrentBlock = 0;

Modified: box/trunk/lib/backupclient/BackupStoreFileEncodeStream.h
===================================================================
--- box/trunk/lib/backupclient/BackupStoreFileEncodeStream.h	2008-08-21 10:20:13 UTC (rev 2244)
+++ box/trunk/lib/backupclient/BackupStoreFileEncodeStream.h	2008-08-21 10:41:18 UTC (rev 2245)
@@ -18,6 +18,7 @@
 #include "MD5Digest.h"
 #include "BackupStoreFile.h"
 #include "ReadLoggingStream.h"
+#include "RunStatusProvider.h"
 
 namespace BackupStoreFileCreation
 {
@@ -74,7 +75,11 @@
 		int64_t mOtherFileID;
 	};
 	
-	void Setup(const char *Filename, Recipe *pRecipe, int64_t ContainerID, const BackupStoreFilename &rStoreFilename, int64_t *pModificationTime);
+	void Setup(const char *Filename, Recipe *pRecipe, int64_t ContainerID,
+		const BackupStoreFilename &rStoreFilename,
+		int64_t *pModificationTime,
+		ReadLoggingStream::Logger* pLogger = NULL,
+		RunStatusProvider* pRunStatusProvider = NULL);
 
 	virtual int Read(void *pBuffer, int NBytes, int Timeout);
 	virtual void Write(const void *pBuffer, int NBytes);
@@ -101,7 +106,8 @@
 	Recipe *mpRecipe;
 	IOStream *mpFile;					// source file
 	CollectInBufferStream mData;		// buffer for header and index entries
-	ReadLoggingStream *mpLogging;
+	IOStream *mpLogging;
+	RunStatusProvider* mpRunStatusProvider;
 	int mStatus;
 	bool mSendData;						// true if there's file data to send (ie not a symlink)
 	int64_t mTotalBlocks;				// Total number of blocks in the file

Modified: box/trunk/lib/common/ReadLoggingStream.cpp
===================================================================
--- box/trunk/lib/common/ReadLoggingStream.cpp	2008-08-21 10:20:13 UTC (rev 2244)
+++ box/trunk/lib/common/ReadLoggingStream.cpp	2008-08-21 10:41:18 UTC (rev 2245)
@@ -25,12 +25,13 @@
 //		Created: 2007/01/16
 //
 // --------------------------------------------------------------------------
-ReadLoggingStream::ReadLoggingStream(IOStream& rSource)
+ReadLoggingStream::ReadLoggingStream(IOStream& rSource, Logger& rLogger)
 : mrSource(rSource),
   mOffset(0),
   mLength(mrSource.BytesLeftToRead()),
   mTotalRead(0),
-  mStartTime(GetCurrentBoxTime())
+  mStartTime(GetCurrentBoxTime()),
+  mrLogger(rLogger)
 { }
 
 
@@ -52,26 +53,21 @@
 		mOffset += numBytesRead;
 	}
 
-	if (mLength >= 0 && mTotalRead > 0)
+	if (mLength == 0)
 	{	
-		box_time_t timeNow = GetCurrentBoxTime();
-		box_time_t elapsed = timeNow - mStartTime;
-		box_time_t finish  = (elapsed * mLength) / mTotalRead;
-		box_time_t remain  = finish - elapsed;
-
-		BOX_TRACE("Read " << numBytesRead << " bytes at " << mOffset << 
-			", " << (mLength - mOffset) << " remain, eta " <<
-			BoxTimeToSeconds(remain) << "s");
+		mrLogger.Log(numBytesRead, mOffset);
 	}
-	else if (mLength >= 0 && mTotalRead == 0)
+	else if (mTotalRead == 0)
 	{
-		BOX_TRACE("Read " << numBytesRead << " bytes at " << mOffset << 
-			", " << (mLength - mOffset) << " remain");
+		mrLogger.Log(numBytesRead, mOffset, mLength);
 	}
 	else
 	{	
-		BOX_TRACE("Read " << numBytesRead << " bytes at " << mOffset << 
-			", unknown bytes remaining");
+		box_time_t timeNow = GetCurrentBoxTime();
+		box_time_t elapsed = timeNow - mStartTime;
+		box_time_t finish  = (elapsed * mLength) / mTotalRead;
+		// box_time_t remain  = finish - elapsed;
+		mrLogger.Log(numBytesRead, mOffset, mLength, elapsed, finish);
 	}
 	
 	return numBytesRead;

Modified: box/trunk/lib/common/ReadLoggingStream.h
===================================================================
--- box/trunk/lib/common/ReadLoggingStream.h	2008-08-21 10:20:13 UTC (rev 2244)
+++ box/trunk/lib/common/ReadLoggingStream.h	2008-08-21 10:41:18 UTC (rev 2245)
@@ -15,13 +15,27 @@
 
 class ReadLoggingStream : public IOStream
 {
+public:
+	class Logger
+	{
+	public:
+		virtual ~Logger() { }
+		virtual void Log(int64_t readSize, int64_t offset,
+			int64_t length, box_time_t elapsed,
+			box_time_t finish) = 0;
+		virtual void Log(int64_t readSize, int64_t offset,
+			int64_t length) = 0;
+		virtual void Log(int64_t readSize, int64_t offset) = 0;
+	};
+
 private:
 	IOStream& mrSource;
 	IOStream::pos_type mOffset, mLength, mTotalRead;
 	box_time_t mStartTime;
+	Logger& mrLogger;
 
 public:
-	ReadLoggingStream(IOStream& rSource);
+	ReadLoggingStream(IOStream& rSource, Logger& rLogger);
 	
 	virtual int Read(void *pBuffer, int NBytes, int Timeout = IOStream::TimeOutInfinite);
 	virtual pos_type BytesLeftToRead();
@@ -35,7 +49,8 @@
 
 private:
 	ReadLoggingStream(const ReadLoggingStream &rToCopy) 
-	: mrSource(rToCopy.mrSource) { /* do not call */ }
+	: mrSource(rToCopy.mrSource), mrLogger(rToCopy.mrLogger)
+	{ /* do not call */ }
 };
 
 #endif // READLOGGINGSTREAM__H




More information about the Boxbackup-commit mailing list