persistentstorage/sql/SRC/Server/SqlBur.cpp
changeset 41 3256212fc81f
parent 17 55f2396f6d25
child 40 b8bdbc8f59c7
equal deleted inserted replaced
35:0d6db0a14001 41:3256212fc81f
    13 // Description:
    13 // Description:
    14 //
    14 //
    15 
    15 
    16 #include "SqlBur.h"
    16 #include "SqlBur.h"
    17 #include "SqlAssert.h"
    17 #include "SqlAssert.h"
    18 #include "SqlPanic.h"
    18 #include "OstTraceDefinitions.h"
       
    19 #ifdef OST_TRACE_COMPILER_IN_USE
       
    20 #include "SqlBurTraces.h"
       
    21 #endif
       
    22 #include "SqlTraceDef.h"
       
    23 
       
    24 #define UNUSED_ARG(arg) arg = arg
    19 
    25 
    20 //Extracts and returns 32-bit integer from aNumBuf buffer.
    26 //Extracts and returns 32-bit integer from aNumBuf buffer.
    21 static TUint32 GetNumUint32L(const TDesC& aNumBuf)
    27 static TUint32 GetNumUint32L(const TDesC& aNumBuf)
    22 	{
    28 	{
    23 	TLex lex(aNumBuf);
    29 	TLex lex(aNumBuf);
    24 	lex.SkipSpace();
    30 	lex.SkipSpace();
    25 	TUint32 num = 0xFFFFFFFF;
    31 	TUint32 num = 0xFFFFFFFF;
    26 	__SQLLEAVE_IF_ERROR(lex.Val(num, EHex));
    32 	__SQLLEAVE_IF_ERROR2(lex.Val(num, EHex));
    27 	return num;
    33 	return num;
    28 	}
    34 	}
    29 
    35 
    30 //Extracts and returns 64-bit integer from aNumBuf buffer.
    36 //Extracts and returns 64-bit integer from aNumBuf buffer.
    31 static TInt64 GetNumInt64L(const TDesC& aNumBuf)
    37 static TInt64 GetNumInt64L(const TDesC& aNumBuf)
    32 	{
    38 	{
    33 	TLex lex(aNumBuf);
    39 	TLex lex(aNumBuf);
    34 	lex.SkipSpace();
    40 	lex.SkipSpace();
    35 	TInt64 num = -1;
    41 	TInt64 num = -1;
    36 	__SQLLEAVE_IF_ERROR(lex.Val(num, EHex));
    42 	__SQLLEAVE_IF_ERROR2(lex.Val(num, EHex));
    37 	return num;
    43 	return num;
    38 	}
    44 	}
    39 
    45 
    40 ///////////////////////////////////////////////////////////////////////////////////////////////////////////////////
    46 ///////////////////////////////////////////////////////////////////////////////////////////////////////////////////
    41 
    47 
    55 CSqlBackupClient* CSqlBackupClient::NewLC(MSqlSrvBurInterface *aInterface)
    61 CSqlBackupClient* CSqlBackupClient::NewLC(MSqlSrvBurInterface *aInterface)
    56 	{
    62 	{
    57 	CSqlBackupClient *self=(CSqlBackupClient *)new(ELeave) CSqlBackupClient(aInterface);
    63 	CSqlBackupClient *self=(CSqlBackupClient *)new(ELeave) CSqlBackupClient(aInterface);
    58 	CleanupStack::PushL(self);
    64 	CleanupStack::PushL(self);
    59 	self->ConstructL();
    65 	self->ConstructL();
       
    66 	SQL_TRACE_BUR(OstTrace1(TRACE_INTERNALS, CSQLBACKUPCLIENT_NEWLC, "0x%X;CSqlBackupClient::NewLC", (TUint)self));
    60 	return self;
    67 	return self;
    61 	}
    68 	}
    62 
    69 
    63 /** Standard two phase construction
    70 /** Standard two phase construction
    64 	@return an instance of the backup client
    71 	@return an instance of the backup client
    84 
    91 
    85 /** Usual tidy up
    92 /** Usual tidy up
    86 */
    93 */
    87 CSqlBackupClient::~CSqlBackupClient()
    94 CSqlBackupClient::~CSqlBackupClient()
    88 	{
    95 	{
       
    96 	SQL_TRACE_BUR(OstTraceExt2(TRACE_INTERNALS, CSQLBACKUPCLIENT_CSQLBACKUPCLIENT2, "0x%X;CSqlBackupClient::~CSqlBackupClient;iFile.SubSessionHandle()=0x%X", (TUint)this, (TUint)iFile.SubSessionHandle()));
       
    97 	
    89 	// cancel outstanding requests
    98 	// cancel outstanding requests
    90 	Cancel();
    99 	Cancel();
    91 	
   100 	
    92 	// release the pub/sub property
   101 	// release the pub/sub property
    93 	iBurProperty.Close();
   102 	iBurProperty.Close();
   128 
   137 
   129 /** Not implemented
   138 /** Not implemented
   130 	@return a flag indicating whether we actioned the error
   139 	@return a flag indicating whether we actioned the error
   131 	@param the error unused
   140 	@param the error unused
   132 */
   141 */
   133 TInt CSqlBackupClient::RunError(TInt /* aError */)
   142 TInt CSqlBackupClient::RunError(TInt aError)
   134 	{
   143 	{
       
   144 	UNUSED_ARG(aError);
       
   145 	SQL_TRACE_BUR(OstTraceExt2(TRACE_INTERNALS, CSQLBACKUPCLIENT_RUNERROR, "0x%X;CSqlBackupClient::RunError;aError=%d", (TUint)this, aError));
   135 	// just satisfy it that we did something!
   146 	// just satisfy it that we did something!
   136 	return KErrNone;
   147 	return KErrNone;
   137 	}
   148 	}
   138 
   149 
   139 /**	Kick off the BUR client
   150 /**	Kick off the BUR client
   147 
   158 
   148 /** Resubscribe and wait for events
   159 /** Resubscribe and wait for events
   149 */	
   160 */	
   150 void CSqlBackupClient::NotifyChange()
   161 void CSqlBackupClient::NotifyChange()
   151 	{
   162 	{
       
   163 	SQL_TRACE_BUR(OstTraceExt2(TRACE_INTERNALS, CSQLBACKUPCLIENT_NOTIFYCHANGE, "0x%X;CSqlBackupClient::NotifyChange;iBurProperty.Handle()=0x%X", (TUint)this, (TUint)iBurProperty.Handle()));
   152 	iBurProperty.Subscribe(iStatus);
   164 	iBurProperty.Subscribe(iStatus);
   153 	SetActive();
   165 	SetActive();
   154 	}
   166 	}
   155 
   167 
   156 /** Something happened. Find out what.
   168 /** Something happened. Find out what.
   159 	This is for performance reasons
   171 	This is for performance reasons
   160 	@leave if ConfirmReadyForBURL leaves
   172 	@leave if ConfirmReadyForBURL leaves
   161 */
   173 */
   162 void CSqlBackupClient::TestBurStatusL()
   174 void CSqlBackupClient::TestBurStatusL()
   163 	{
   175 	{
       
   176 	SQL_TRACE_BUR(OstTrace1(TRACE_INTERNALS, CSQLBACKUPCLIENT_TESTBURSTATUSL_ENTRY, "Entry;0x%X;CSqlBackupClient::TestBurStatusL", (TUint)this));
   164 	TInt status;
   177 	TInt status;
   165 	if(iBurProperty.Get(status)!=KErrNotFound)
   178 	__SQLTRACE_BURVAR(TInt err = KErrNone);
       
   179 	if((__SQLTRACE_BUREXPR(err =) iBurProperty.Get(status)) != KErrNotFound)
   166 		{
   180 		{
   167 		status&=KBURPartTypeMask;
   181 		status&=KBURPartTypeMask;
       
   182 #ifdef _SQL_RDEBUG_PRINT
       
   183 		SQL_TRACE_BUR(OstTraceExt2(TRACE_INTERNALS, CSQLBACKUPCLIENT_TESTBURSTATUSL1, "0x%X;CSqlBackupClient::TestBurStatusL;status=%d", (TUint)this, status));
       
   184 #else
       
   185 		SQL_TRACE_BUR(OstTraceExt2(TRACE_INTERNALS, CSQLBACKUPCLIENT_TESTBURSTATUSL2, "0x%X;CSqlBackupClient::TestBurStatusL;status=%{TBURPartType}", (TUint)this, status));
       
   186 #endif	    
   168 		switch(status)
   187 		switch(status)
   169 			{
   188 			{
   170 			case EBURUnset: // same as EBURNormal
   189 			case EBURUnset: // same as EBURNormal
   171 			case EBURNormal:
   190 			case EBURNormal:
   172 				delete iActiveBackupClient;
   191 				delete iActiveBackupClient;
   182 					iActiveBackupClient=CActiveBackupClient::NewL(this);
   201 					iActiveBackupClient=CActiveBackupClient::NewL(this);
   183 					}
   202 					}
   184 				iActiveBackupClient->ConfirmReadyForBURL(KErrNone);
   203 				iActiveBackupClient->ConfirmReadyForBURL(KErrNone);
   185 				break;
   204 				break;
   186 			default:
   205 			default:
   187 				return;
   206 				break;
   188 			}
   207 			}
   189 		}
   208 		}
       
   209 	SQL_TRACE_BUR(OstTraceExt2(TRACE_INTERNALS, CSQLBACKUPCLIENT_TESTBURSTATUSL_EXIT, "Exit;0x%X;CSqlBackupClient::TestBurStatusL;iProperty.Get() err=%d", (TUint)this, err));
   190 	}
   210 	}
   191 
   211 
   192 /** Called when BUE notifies a BUR event
   212 /** Called when BUE notifies a BUR event
   193 	@leave if TestBurStatusL leaves
   213 	@leave if TestBurStatusL leaves
   194 */
   214 */
   207 	supposed to allow the BUE to optimise its behaviour by know up front
   227 	supposed to allow the BUE to optimise its behaviour by know up front
   208 	the data volume.
   228 	the data volume.
   209 	@return an arbitrary number
   229 	@return an arbitrary number
   210 	@param TDrive unused
   230 	@param TDrive unused
   211 */
   231 */
   212 TUint CSqlBackupClient::GetExpectedDataSize(TDriveNumber /* aDrive */)
   232 TUint CSqlBackupClient::GetExpectedDataSize(TDriveNumber aDrive)
   213 	{
   233 	{
       
   234 	UNUSED_ARG(aDrive);
   214 	// we have no idea at this point - we even don't know who is to be backed up yet
   235 	// we have no idea at this point - we even don't know who is to be backed up yet
   215 	const TUint KArbitraryNumber = 1024;
   236 	const TUint KArbitraryNumber = 1024;
       
   237 	SQL_TRACE_BUR(OstTraceExt3(TRACE_INTERNALS, CSQLBACKUPCLIENT_GETEXPECTEDDATASIZE, "0x%X;CSqlBackupClient::GetExpectedDataSize;aDrive=%d;rc=%u", (TUint)this, (TInt)aDrive, KArbitraryNumber));
   216 	return KArbitraryNumber;
   238 	return KArbitraryNumber;
   217 	}
   239 	}
   218 
   240 
   219 /** This is the backup state machine
   241 /** This is the backup state machine
   220 	Because the data has to be sent back in sections and the various
   242 	Because the data has to be sent back in sections and the various
   225 	@param TBool set to true when all data has been submitted for backup
   247 	@param TBool set to true when all data has been submitted for backup
   226 	@leave
   248 	@leave
   227 */
   249 */
   228 void CSqlBackupClient::GetBackupDataSectionL(TPtr8& aBuffer, TBool& aFinishedFlag)
   250 void CSqlBackupClient::GetBackupDataSectionL(TPtr8& aBuffer, TBool& aFinishedFlag)
   229 	{
   251 	{
       
   252 	SQL_TRACE_BUR(OstTraceExt3(TRACE_INTERNALS, CSQLBACKUPCLIENT_GETBACKUPDATASECTIONL0, "0x%X;CSqlBackupClient::GetBackupDataSectionL;iState=%d;iFileIndex=%d", (TUint)this, (TInt)iState, iFileIndex));
   230 	// don't assume they set it to false
   253 	// don't assume they set it to false
   231 	aFinishedFlag=EFalse;
   254 	aFinishedFlag=EFalse;
   232 	// any files to backup
   255 	// any files to backup
   233 	if(iFileList.Count()==0)
   256 	if(iFileList.Count()==0)
   234 		{
   257 		{
       
   258 		SQL_TRACE_BUR(OstTrace1(TRACE_INTERNALS, CSQLBACKUPCLIENT_GETBACKUPDATASECTIONL1, "0x%X;CSqlBackupClient::GetBackupDataSectionL;file count is 0", (TUint)this));
   235 		// nothing to backup - just return the finished flag
   259 		// nothing to backup - just return the finished flag
   236 		aFinishedFlag=ETrue;
   260 		aFinishedFlag=ETrue;
   237 		// clear down the list
   261 		// clear down the list
   238 		iFileList.Reset();
   262 		iFileList.Reset();
   239 		// iFileList closed in dtor
   263 		// iFileList closed in dtor
   240 		return;
   264 		return;
   241 		}
   265 		}
   242 	
   266 
   243 	// run the state machine
   267 	// run the state machine
   244 	for(TInt bufFreeSpace=aBuffer.MaxSize()-aBuffer.Size(); bufFreeSpace>0; bufFreeSpace=aBuffer.MaxSize()-aBuffer.Size())
   268 	for(TInt bufFreeSpace=aBuffer.MaxSize()-aBuffer.Size(); bufFreeSpace>0; bufFreeSpace=aBuffer.MaxSize()-aBuffer.Size())
   245 		{
   269 		{
   246 		switch(iState)
   270 		switch(iState)
   247 			{
   271 			{
   248 			case EBackupNoFileOpen: // open a file for processing
   272 			case EBackupNoFileOpen: // open a file for processing
   249 				{
   273 				{
   250 				if(iFileIndex>=iFileList.Count())
   274 				if(iFileIndex>=iFileList.Count())
   251 					{
   275 					{
       
   276 					SQL_TRACE_BUR(OstTrace1(TRACE_INTERNALS, CSQLBACKUPCLIENT_GETBACKUPDATASECTIONL2, "0x%X;CSqlBackupClient::GetBackupDataSectionL;all files processed", (TUint)this));
   252 					// all files have been processed - send the finished flag
   277 					// all files have been processed - send the finished flag
   253 					aFinishedFlag=ETrue;
   278 					aFinishedFlag=ETrue;
   254 					// clear down the filelist
   279 					// clear down the filelist
   255 					iFileList.Reset();
   280 					iFileList.Reset();
   256 					return;
   281 					return;
   257 					}
   282 					}
   258 				// open the database file to send
   283 				// open the database file to send
   259 				TInt rc=iFile.Open(	iInterface->Fs(), iFileList[iFileIndex].FullName(), EFileRead | EFileShareExclusive);
   284 				TInt rc=iFile.Open(	iInterface->Fs(), iFileList[iFileIndex].FullName(), EFileRead | EFileShareExclusive);
       
   285 				__SQLTRACE_BUREXPR(TPtrC fname = iFileList[iFileIndex].FullName());
       
   286 				SQL_TRACE_BUR(OstTraceExt5(TRACE_INTERNALS, CSQLBACKUPCLIENT_GETBACKUPDATASECTIONL3, "0x%X;CSqlBackupClient::GetBackupDataSectionL;BEGIN;fname=%S;iFileIndex=%d;iFile.SubSessionHandle()=0x%X;rc=%d", (TUint)this, __SQLPRNSTR(fname), iFileIndex, (TUint)iFile.SubSessionHandle(), rc));
   260 				if(KErrNone!=rc)
   287 				if(KErrNone!=rc)
   261 					{
   288 					{
   262 					// there's nothing we can do if we can't open the file so we just skip it
   289 					// there's nothing we can do if we can't open the file so we just skip it
   263 					++iFileIndex;
   290 					++iFileIndex;
   264 					break;
   291 					break;
   288 					KMagicNum,					// %8x
   315 					KMagicNum,					// %8x
   289 					KBackupHeaderVersion,		// %4x
   316 					KBackupHeaderVersion,		// %4x
   290 					fileSize,					// %16lx
   317 					fileSize,					// %16lx
   291 					fileName.Length(),			// %8x
   318 					fileName.Length(),			// %8x
   292 					&fileName);					// %S
   319 					&fileName);					// %S
       
   320 				SQL_TRACE_BUR(OstTraceExt4(TRACE_INTERNALS, CSQLBACKUPCLIENT_GETBACKUPDATASECTIONL5, "0x%X;CSqlBackupClient::GetBackupDataSectionL;fileName=%S;hdrPtr=|%S|;fileSize=%lld", (TUint)this, __SQLPRNSTR(fileName), __SQLPRNSTR(iBuffer), fileSize));
   293 				
   321 				
   294 				// we need it to look like an 8bit buffer
   322 				// we need it to look like an 8bit buffer
   295 				TPtr8 hdrPtr8((TUint8*)iBuffer.Ptr(), iBuffer.Size(), iBuffer.Size());
   323 				TPtr8 hdrPtr8((TUint8*)iBuffer.Ptr(), iBuffer.Size(), iBuffer.Size());
   296 							
   324 							
   297 				TInt len = Min(hdrPtr8.Size(), bufFreeSpace);
   325 				TInt len = Min(hdrPtr8.Size(), bufFreeSpace);
   355 					}
   383 					}
   356 				break;
   384 				break;
   357 				}
   385 				}
   358 			case EBackupEndOfFile:
   386 			case EBackupEndOfFile:
   359 				{
   387 				{
       
   388 				SQL_TRACE_BUR(OstTraceExt3(TRACE_INTERNALS, CSQLBACKUPCLIENT_GETBACKUPDATASECTIONL4, "0x%X;CSqlBackupClient::GetBackupDataSectionL;END;iFile.SubSessionHandle()=0x%X;iFileIndex=%d", (TUint)this, (TUint)iFile.SubSessionHandle(), iFileIndex));
   360 				iFile.Close();
   389 				iFile.Close();
   361 				++iFileIndex; // move on to next file
   390 				++iFileIndex; // move on to next file
   362 				iState = EBackupNoFileOpen; // go round again
   391 				iState = EBackupNoFileOpen; // go round again
   363 				break;
   392 				break;
   364 				}
   393 				}
   372 
   401 
   373 /** This is called by BUE when the restore has completed
   402 /** This is called by BUE when the restore has completed
   374 	Nothing to do here except tell the server
   403 	Nothing to do here except tell the server
   375 	@param TDrive the drive that is being restored (unused)
   404 	@param TDrive the drive that is being restored (unused)
   376 */
   405 */
   377 void CSqlBackupClient::RestoreComplete(TDriveNumber /* aDrive */)
   406 void CSqlBackupClient::RestoreComplete(TDriveNumber aDrive)
   378 	{
   407 	{
       
   408 	UNUSED_ARG(aDrive);
       
   409 	SQL_TRACE_BUR(OstTraceExt2(TRACE_INTERNALS, CSQLBACKUPCLIENT_RESTORECOMPLETE, "0x%X;CSqlBackupClient::RestoreComplete;aDrive=%d", (TUint)this, (TInt)aDrive));
   379 	}
   410 	}
   380 
   411 
   381 /** This is called to let us know that the given SID is to be backed up
   412 /** This is called to let us know that the given SID is to be backed up
   382 	We ask the SQL server for a list of databases that want to be backed
   413 	We ask the SQL server for a list of databases that want to be backed
   383 	up - this is because the backup flag is an internal metadata object
   414 	up - this is because the backup flag is an internal metadata object
   385 	this data is stored.
   416 	this data is stored.
   386 	@param TSecureSid the UID of the application to backup
   417 	@param TSecureSid the UID of the application to backup
   387 	@param TDriveNumber the drive to be backed up (unused)
   418 	@param TDriveNumber the drive to be backed up (unused)
   388 	@leave
   419 	@leave
   389 */
   420 */
   390 void CSqlBackupClient::InitialiseGetProxyBackupDataL(TSecureId aSid, TDriveNumber /*aDrive*/)
   421 void CSqlBackupClient::InitialiseGetProxyBackupDataL(TSecureId aSid, TDriveNumber aDrive)
   391 	{
   422 	{
       
   423 	UNUSED_ARG(aDrive);
       
   424 	SQL_TRACE_BUR(OstTraceExt3(TRACE_INTERNALS, CSQLBACKUPCLIENT_INITIALIZEGETPROXYBACKUPDATAL, "0x%X;CSqlBackupClient::InitialiseGetProxyBackupDataL;aSid=0x%X;aDrive=%d", (TUint)this, (TUint)aSid.iId, (TInt)aDrive));
   392 	// get the list of database files to back up - this is provided by the SQL server
   425 	// get the list of database files to back up - this is provided by the SQL server
   393 	GetBackupListL(aSid);
   426 	GetBackupListL(aSid);
   394 	// this is the index of the file being processed - point to the beginning
   427 	// this is the index of the file being processed - point to the beginning
   395 	iFileIndex=0;
   428 	iFileIndex=0;
   396 	// the first state of the backup state machine
   429 	// the first state of the backup state machine
   402 /** Called when the BUE wants to start sending data to us
   435 /** Called when the BUE wants to start sending data to us
   403 	@param TSecureId the UID of the application that is to be restored
   436 	@param TSecureId the UID of the application that is to be restored
   404 	@param TDriveNumber the drive to restore (unused)
   437 	@param TDriveNumber the drive to restore (unused)
   405 	@leave
   438 	@leave
   406 */
   439 */
   407 void CSqlBackupClient::InitialiseRestoreProxyBaseDataL(TSecureId aSid, TDriveNumber /* aDrive */)
   440 void CSqlBackupClient::InitialiseRestoreProxyBaseDataL(TSecureId aSid, TDriveNumber aDrive)
   408 	{
   441 	{
       
   442 	UNUSED_ARG(aDrive);
       
   443 	SQL_TRACE_BUR(OstTraceExt3(TRACE_INTERNALS, CSQLBACKUPCLIENT_INITIALIZERESTOREPROXYBASEDATAL, "0x%X;CSqlBackupClient::InitialiseRestoreProxyBaseDataL;aSid=0x%X;aDrive=%d", (TUint)this, (TUint)aSid.iId, (TInt)aDrive));
   409 	iBuffer.Zero();
   444 	iBuffer.Zero();
   410 	// this is the first state of the restore state machine
   445 	// this is the first state of the restore state machine
   411 	iState=ERestoreExpectChecksum;
   446 	iState=ERestoreExpectChecksum;
   412 	iAnyData=EFalse; // to keep track in the state machine whether any data was actually sent
   447 	iAnyData=EFalse; // to keep track in the state machine whether any data was actually sent
   413 	// save the sid for notifying the server when the restore is done
   448 	// save the sid for notifying the server when the restore is done
   424 Attention!!! This function won't work properly if aInBuffer parameter contains odd number of bytes!!!
   459 Attention!!! This function won't work properly if aInBuffer parameter contains odd number of bytes!!!
   425 (a legacy problem, if it is a problem at all, because the B&R engine probably sends the data in chunks with even size)
   460 (a legacy problem, if it is a problem at all, because the B&R engine probably sends the data in chunks with even size)
   426 */
   461 */
   427 void CSqlBackupClient::RestoreBaseDataSectionL(TDesC8& aInBuffer, TBool aFinishedFlag)
   462 void CSqlBackupClient::RestoreBaseDataSectionL(TDesC8& aInBuffer, TBool aFinishedFlag)
   428 	{
   463 	{
       
   464 	SQL_TRACE_BUR(OstTraceExt4(TRACE_INTERNALS, CSQLBACKUPCLIENT_RESTOREBASEDATASECTONL0, "0x%X;CSqlBackupClient::RestoreBaseDataSectionL;iState=%d;aInBuffer.Length()=%d;aFinishedFlag=%d", (TUint)this, (TInt)iState, aInBuffer.Length(), (TInt)aFinishedFlag));
   429 	// used to walk the buffer
   465 	// used to walk the buffer
   430 	// got a new buffer - because each time this method is called, we have a
   466 	// got a new buffer - because each time this method is called, we have a
   431 	// fresh chunk of data
   467 	// fresh chunk of data
   432 	TInt inBufferPos = 0;
   468 	TInt inBufferPos = 0;
   433 
   469 
   524 					{
   560 					{
   525 					iFileSize = GetNumInt64L(iBuffer);	
   561 					iFileSize = GetNumInt64L(iBuffer);	
   526 					iState = ERestoreExpectFileNameSize;
   562 					iState = ERestoreExpectFileNameSize;
   527 					iBuffer.Zero();
   563 					iBuffer.Zero();
   528 					}
   564 					}
       
   565 				SQL_TRACE_BUR(OstTraceExt2(TRACE_INTERNALS, CSQLBACKUPCLIENT_RESTOREBASEDATASECTONL1, "0x%X;CSqlBackupClient::RestoreBaseDataSectionL;iFileSize=%lld", (TUint)this, iFileSize));
   529 				break;
   566 				break;
   530 				}
   567 				}
   531 			case ERestoreExpectFileNameSize: // the size of the file name to restore
   568 			case ERestoreExpectFileNameSize: // the size of the file name to restore
   532 				{
   569 				{
   533 				const TInt KFileNameLenStrLen = 8;
   570 				const TInt KFileNameLenStrLen = 8;
   541 				break;
   578 				break;
   542 				}
   579 				}
   543 			case ERestoreExpectFileName:  // the name of the file to restore
   580 			case ERestoreExpectFileName:  // the name of the file to restore
   544 				{
   581 				{
   545 				CopyBufData(aInBuffer, inBufferPos, iBuffer, iFileNameSize);
   582 				CopyBufData(aInBuffer, inBufferPos, iBuffer, iFileNameSize);
       
   583 				SQL_TRACE_BUR(OstTraceExt4(TRACE_INTERNALS, CSQLBACKUPCLIENT_RESTOREBASEDATASECTONL2, "0x%X;CSqlBackupClient::RestoreBaseDataSectionL;BEGIN;iBuffer=%S;iBuffer.Length()=%d;iFileNameSize=%d", (TUint)this, __SQLPRNSTR(iBuffer), iBuffer.Length(), iFileNameSize));
   546 				if(iBuffer.Length() == iFileNameSize)
   584 				if(iBuffer.Length() == iFileNameSize)
   547 					{
   585 					{
   548 					iState = ERestoreExpectData;
   586 					iState = ERestoreExpectData;
   549 					iBuffer.Append(KRestoreSuffix);
   587 					iBuffer.Append(KRestoreSuffix);
   550 					// now we start writing the data to the target file
   588 					// now we start writing the data to the target file
   551 					// write to a temp - double disk space potentially
   589 					// write to a temp - double disk space potentially
   552 					// once all the temp files are created, then they are renamed to the
   590 					// once all the temp files are created, then they are renamed to the
   553 					// real file names in one fell swoop
   591 					// real file names in one fell swoop
   554 					__SQLLEAVE_IF_ERROR(iFile.Replace(iInterface->Fs(), iBuffer, EFileWrite | EFileShareExclusive));
   592 					__SQLLEAVE_IF_ERROR(iFile.Replace(iInterface->Fs(), iBuffer, EFileWrite | EFileShareExclusive));
       
   593 					SQL_TRACE_BUR(OstTraceExt2(TRACE_INTERNALS, CSQLBACKUPCLIENT_RESTOREBASEDATASECTONL3, "0x%X;CSqlBackupClient::RestoreBaseDataSectionL;iFile.SubSessionHandle()=0x%X", (TUint)this, (TUint)iFile.SubSessionHandle()));
   555 					iBuffer.Zero();
   594 					iBuffer.Zero();
   556 					}
   595 					}
   557 				break;
   596 				break;
   558 				}
   597 				}
   559 			case ERestoreExpectData: // now for the data
   598 			case ERestoreExpectData: // now for the data
   572 				{
   611 				{
   573 				// calculate the checksum
   612 				// calculate the checksum
   574 				TUint32 cksum = CheckSumL(iFile) & KMaxTUint32;
   613 				TUint32 cksum = CheckSumL(iFile) & KMaxTUint32;
   575 				
   614 				
   576 				// done with the file now - has to follow checksum cos it
   615 				// done with the file now - has to follow checksum cos it
   577 				// expects an open file
   616 				// expects ann open file
   578                 __SQLLEAVE_IF_ERROR(iFile.Flush());
   617 				SQL_TRACE_BUR(OstTraceExt2(TRACE_INTERNALS, CSQLBACKUPCLIENT_RESTOREBASEDATASECTONL4, "0x%X;CSqlBackupClient::RestoreBaseDataSectionL;END;iFile.SubSessionHandle()=0x%X", (TUint)this, (TUint)iFile.SubSessionHandle()));
       
   618 			    __SQLLEAVE_IF_ERROR(iFile.Flush());
   579 				iFile.Close();
   619 				iFile.Close();
   580 
   620 
   581                 // validate that the checksum matches
   621                 // validate that the checksum matches
   582                 if(cksum!=iChecksum)
   622                 if(cksum!=iChecksum)
   583                     {
   623                     {
   590 					// we need to rename all the
   630 					// we need to rename all the
   591 					// temp rst files to the real database names
   631 					// temp rst files to the real database names
   592 					CDir *dir=NULL;
   632 					CDir *dir=NULL;
   593 					__SQLLEAVE_IF_ERROR(iInterface->Fs().GetDir(KRestoreFilter,KEntryAttNormal,ESortNone,dir));
   633 					__SQLLEAVE_IF_ERROR(iInterface->Fs().GetDir(KRestoreFilter,KEntryAttNormal,ESortNone,dir));
   594 					CleanupStack::PushL(dir);
   634 					CleanupStack::PushL(dir);
       
   635 					TInt err2 = KErrNone;
   595 					for(TInt a=0;a<dir->Count();++a)
   636 					for(TInt a=0;a<dir->Count();++a)
   596 						{
   637 						{
   597 						TEntry entry=(*dir)[a];
   638 						TEntry entry=(*dir)[a];
   598 						TPtrC rst=entry.iName.Des();
   639 						TPtrC rst=entry.iName.Des();
   599 						TInt len=rst.Length();
   640 						TInt len=rst.Length();
   605 						// first, rename the orig .db as .bak just in case
   646 						// first, rename the orig .db as .bak just in case
   606 						// ok if not found - might have been deleted.
   647 						// ok if not found - might have been deleted.
   607 						//the ".bak" file, if exists, will be deleted first.
   648 						//the ".bak" file, if exists, will be deleted first.
   608 						(void)iInterface->Fs().Delete(bak);
   649 						(void)iInterface->Fs().Delete(bak);
   609 						TInt err=iInterface->Fs().Rename(db,bak);
   650 						TInt err=iInterface->Fs().Rename(db,bak);
       
   651 						SQL_TRACE_BUR(OstTraceExt4(TRACE_INTERNALS, CSQLBACKUPCLIENT_RESTOREBASEDATASECTONL5, "0x%X;CSqlBackupClient::RestoreBaseDataSectionL;END;bak=%S;db=%S;err=%d", (TUint)this, __SQLPRNSTR(bak), __SQLPRNSTR(db), err));
   610 						if(err!=KErrNone && err!=KErrNotFound)
   652 						if(err!=KErrNone && err!=KErrNotFound)
   611 							{
   653 							{
   612 							__SQLLEAVE(err);
   654 							__SQLLEAVE(err);
   613 							}
   655 							}
   614 						
   656 						
   615 						// now, rename the .rst as .db
   657 						// now, rename the .rst as .db
   616 						__SQLLEAVE_IF_ERROR(iInterface->Fs().Rename(rst,db));
   658 						err = iInterface->Fs().Rename(rst,db);
   617 						
   659 						if(err != KErrNone && err2 == KErrNone)
       
   660 							{
       
   661 							//The idea here is to not report the error immediatelly by calling LeaveIfError().
       
   662 							//If we leave here, the next database restore may also fail, for example, if the current database is still open by 
       
   663 							//its owner. Then "TInt err=iInterface->Fs().Rename(db,bak);" will fail again.
       
   664 							err2 = err;
       
   665 							}
   618 						// if we got here, we have a backup of the original database in .db.bak
   666 						// if we got here, we have a backup of the original database in .db.bak
   619 						// and the new database in .db
   667 						// and the new database in .db
   620 						}
   668 						}
       
   669 					__SQLLEAVE_IF_ERROR(err2);
   621 					
   670 					
   622 					// clean up dir
   671 					// clean up dir
   623 					//delete dir;
   672 					//delete dir;
   624 					CleanupStack::PopAndDestroy(dir);
   673 					CleanupStack::PopAndDestroy(dir);
   625 					dir=NULL;
   674 					dir=NULL;
   628 					// we do this here and not part of the earlier loop
   677 					// we do this here and not part of the earlier loop
   629 					// because we want to make sure that we have a coherent set of database
   678 					// because we want to make sure that we have a coherent set of database
   630 					// files that belong together and not bits of old and new
   679 					// files that belong together and not bits of old and new
   631 					__SQLLEAVE_IF_ERROR(iInterface->Fs().GetDir(KBackupFilter,KEntryAttNormal,ESortNone,dir));
   680 					__SQLLEAVE_IF_ERROR(iInterface->Fs().GetDir(KBackupFilter,KEntryAttNormal,ESortNone,dir));
   632 					CleanupStack::PushL(dir);
   681 					CleanupStack::PushL(dir);
       
   682 					SQL_TRACE_BUR(OstTraceExt2(TRACE_INTERNALS, CSQLBACKUPCLIENT_RESTOREBASEDATASECTONL6, "0x%X;CSqlBackupClient::RestoreBaseDataSectionL;bak files count=%d", (TUint)this, dir->Count()));
   633 					for(TInt a1=0;a1<dir->Count();++a1)
   683 					for(TInt a1=0;a1<dir->Count();++a1)
   634 						{
   684 						{
   635 						TEntry entry=(*dir)[a1];
   685 						TEntry entry=(*dir)[a1];
   636 						TPtrC bak=entry.iName.Des();
   686 						TPtrC bak=entry.iName.Des();
   637 						__SQLLEAVE_IF_ERROR(iInterface->Fs().Delete(bak));
   687 						__SQLLEAVE_IF_ERROR(iInterface->Fs().Delete(bak));
   666 	{
   716 	{
   667 	// backup/restore terminated, try to tidy up! Can't leave, can't Panic!!!!!
   717 	// backup/restore terminated, try to tidy up! Can't leave, can't Panic!!!!!
   668 	// rename all the .bak files to .db
   718 	// rename all the .bak files to .db
   669 	CDir *dir=NULL;
   719 	CDir *dir=NULL;
   670 	TInt rc=iInterface->Fs().GetDir(KBackupFilter,KEntryAttNormal,ESortNone,dir);
   720 	TInt rc=iInterface->Fs().GetDir(KBackupFilter,KEntryAttNormal,ESortNone,dir);
       
   721 	SQL_TRACE_BUR(OstTraceExt3(TRACE_INTERNALS, CSQLBACKUPCLIENT_TERMINATEMULTISTAGEOPERATION1, "0x%X;CSqlBackupClient::TerminateMultiStageOperation;Fs().GetDir() err=%d;file count=%d", (TUint)this, rc, rc == KErrNone ? dir->Count() : 0));
   671 	if(KErrNone!=rc)
   722 	if(KErrNone!=rc)
   672 		{
   723 		{
   673 		// can't get a file list - can't do anything
   724 		// can't get a file list - can't do anything
   674 		return;
   725 		return;
   675 		}
   726 		}
   686 			}
   737 			}
   687         //The function cannot leave or return an error. The only thing which could be done here is to print out something
   738         //The function cannot leave or return an error. The only thing which could be done here is to print out something
   688 		//and continue with the next file.
   739 		//and continue with the next file.
   689 		if(KErrNone != rc)
   740 		if(KErrNone != rc)
   690 		    {
   741 		    {
   691 		    RDebug::Print(_L(" *** CSqlBackupClient::TerminateMultiStageOperation(), file \"%S\", err=%d\r\n"), &db, rc);
   742 			SQL_TRACE_BUR(OstTraceExt4(TRACE_INTERNALS, CSQLBACKUPCLIENT_TERMINATEMULTISTAGEOPERATION2, "0x%X;CSqlBackupClient::TerminateMultiStageOperation;Fs().Rename() err=%d;bak=%S;db=%S", (TUint)this, rc, __SQLPRNSTR(bak), __SQLPRNSTR(db)));
   692 		    }
   743 		    }
   693 		// backup restored ok
   744 		// backup restored ok
   694 		}
   745 		}
   695 	// cleanup dir
   746 	// cleanup dir
   696 	delete dir;
   747 	delete dir;
   774 	@leave
   825 	@leave
   775 	@param TSecureSid the UID of the data owner
   826 	@param TSecureSid the UID of the data owner
   776 */
   827 */
   777 void CSqlBackupClient::GetBackupListL(TSecureId aSid)
   828 void CSqlBackupClient::GetBackupListL(TSecureId aSid)
   778 	{
   829 	{
       
   830 	SQL_TRACE_BUR(OstTraceExt2(TRACE_INTERNALS, CSQLBACKUPCLIENT_GETBACKUPLISTL, "0x%X;CSqlBackupClient::GetBackupListL;aSid=0x%X", (TUint)this, (TUint)aSid.iId));
   779 	// we own the array - the SQL server just populates it
   831 	// we own the array - the SQL server just populates it
   780 	iInterface->GetBackUpListL(aSid,iFileList);
   832 	iInterface->GetBackUpListL(aSid,iFileList);
   781 	}
   833 	}
   782 
   834 
   783 /** A simple checksumming algorithm to allow a degree
   835 /** A simple checksumming algorithm to allow a degree
   834 //otherwise CopyBufData() will copy as much characters as possible (in which case RestoreBaseDataSectionL() will
   886 //otherwise CopyBufData() will copy as much characters as possible (in which case RestoreBaseDataSectionL() will
   835 //stay in the same state, waiting for more data from the B&R server).
   887 //stay in the same state, waiting for more data from the B&R server).
   836 //
   888 //
   837 void CSqlBackupClient::CopyBufData(const TDesC8& aInBuf, TInt& aInBufReadPos, TDes& aOutBuf, TInt aDataLen)
   889 void CSqlBackupClient::CopyBufData(const TDesC8& aInBuf, TInt& aInBufReadPos, TDes& aOutBuf, TInt aDataLen)
   838 	{
   890 	{
   839 	__SQLASSERT(aInBufReadPos >= 0, ESqlPanicBadArgument);
   891     __ASSERT_DEBUG(aInBufReadPos >= 0, __SQLPANIC(ESqlPanicBadArgument));
   840     __SQLASSERT(aDataLen > 0, ESqlPanicBadArgument);
   892     __ASSERT_DEBUG(aDataLen > 0, __SQLPANIC(ESqlPanicBadArgument));
   841     __SQLASSERT(!(aInBuf.Length() & 0x01), ESqlPanicInternalError);
   893     __ASSERT_DEBUG(!(aInBuf.Length() & 0x01), __SQLPANIC(ESqlPanicInternalError));
   842 	
   894 	
   843 	TInt needed = (aDataLen - aOutBuf.Length()) << K8to16bitShift;
   895 	TInt needed = (aDataLen - aOutBuf.Length()) << K8to16bitShift;
   844 	TInt available = aInBuf.Size() - aInBufReadPos;
   896 	TInt available = aInBuf.Size() - aInBufReadPos;
   845 	TInt len = Min(needed, available);
   897 	TInt len = Min(needed, available);
   846 	TPtrC8 ptr8 = aInBuf.Mid(aInBufReadPos, len);
   898 	TPtrC8 ptr8 = aInBuf.Mid(aInBufReadPos, len);