installationservices/swtransactionservices/source/server/journal.cpp
branchRCL_3
changeset 26 8b7f4e561641
parent 25 7333d7932ef7
child 27 e8965914fac7
equal deleted inserted replaced
25:7333d7932ef7 26:8b7f4e561641
     1 /*
       
     2 * Copyright (c) 2004-2009 Nokia Corporation and/or its subsidiary(-ies).
       
     3 * All rights reserved.
       
     4 * This component and the accompanying materials are made available
       
     5 * under the terms of the License "Eclipse Public License v1.0"
       
     6 * which accompanies this distribution, and is available
       
     7 * at the URL "http://www.eclipse.org/legal/epl-v10.html".
       
     8 *
       
     9 * Initial Contributors:
       
    10 * Nokia Corporation - initial contribution.
       
    11 *
       
    12 * Contributors:
       
    13 *
       
    14 * Description: 
       
    15 * CJournal implementation
       
    16 *
       
    17 */
       
    18 
       
    19 
       
    20 /**
       
    21  @file 
       
    22  @released
       
    23  @internalTechnology
       
    24 */
       
    25 
       
    26 #include "journal.h"
       
    27 #include "journalfile.h"
       
    28 #include "operationfunctions.h"
       
    29 #include "usiflog.h"
       
    30 #include <f32file.h>
       
    31 
       
    32 using namespace Usif;
       
    33 
       
    34 CJournal* CJournal::NewL(RFs& aFs, RLoader& aLoader, TStsTransactionId aTransactionID, const TDesC& aPath)
       
    35 	{
       
    36 	CJournal* self = CJournal::NewLC(aFs, aLoader, aTransactionID, aPath);
       
    37 	CleanupStack::Pop(self);
       
    38 	return self;
       
    39 	}
       
    40 
       
    41 CJournal* CJournal::NewLC(RFs& aFs, RLoader& aLoader, TStsTransactionId aTransactionID, const TDesC& aPath)
       
    42 	{
       
    43 	CJournal* self = new(ELeave) CJournal(aFs, aLoader);
       
    44 	CleanupStack::PushL(self);
       
    45 	self->ConstructL(aTransactionID, aPath);
       
    46 	return self;
       
    47 	}
       
    48 
       
    49 CJournal::~CJournal()
       
    50 	{
       
    51 	iCompletedDrives.Reset();
       
    52 	iAllDrives.Reset();
       
    53 	iJournalFiles.ResetAndDestroy();
       
    54 	}
       
    55 
       
    56 CJournal::CJournal(RFs& aFs, RLoader& aLoader) : iFs(aFs), iLoader(aLoader)
       
    57 	{
       
    58 	}
       
    59 
       
    60 void CJournal::ConstructL(TStsTransactionId aTransactionID, const TDesC& aPath)
       
    61 	{
       
    62 	// construct the generic journal filename (does not include a drive)
       
    63 	CJournal::CreateJournalFileNameL(aTransactionID, aPath, iJournalFileName);
       
    64 	// construct the filename for the drives journal (located on the system drive)
       
    65 	CJournal::CreateDrvFileNameL(aTransactionID, aPath, iDriveArrayFileName);	
       
    66 	InitJournalsL();
       
    67 	}
       
    68 
       
    69 void CJournal::InitJournalsL()
       
    70 	{	
       
    71 	RefreshDrivesArrayL();
       
    72 	
       
    73 	// attempt to read the journals from all drives
       
    74 	
       
    75 	TInt drivesCount(iAllDrives.Count());
       
    76 	RBuf journal;
       
    77 	journal.CreateL(KMaxFileName);
       
    78 	CleanupClosePushL(journal);
       
    79 	for (TInt i = 0; i < drivesCount; ++i)
       
    80 		{
       
    81 		TDriveUnit drive(iAllDrives[i]);
       
    82 		journal = drive.Name();
       
    83 		journal.Append(iJournalFileName);
       
    84 
       
    85 		// do not load completed drives
       
    86 		if (iCompletedDrives.Find(drive) != KErrNotFound)
       
    87 			{
       
    88 			continue;
       
    89 			}
       
    90 		
       
    91 		CJournalFile* journalFile = NULL;
       
    92 		TRAPD(err, journalFile = CJournalFile::NewL(iFs, iLoader, journal, iAllDrives[i]));
       
    93 		
       
    94 		// ignore error'd drives. These will either not be rolled back,
       
    95 		// or the user will find out later we can't write or commit to them.
       
    96 		
       
    97 		if (err == KErrNone)
       
    98 			{
       
    99 			CleanupStack::PushL(journalFile);
       
   100 			iJournalFiles.AppendL(journalFile);
       
   101 			CleanupStack::Pop(journalFile);
       
   102 			}
       
   103 		}
       
   104 	CleanupStack::PopAndDestroy(&journal);
       
   105 	}
       
   106 
       
   107 TInt CJournal::FindJournalFileIndexL(TInt aDrive) const
       
   108 	{
       
   109 	for(TInt index = 0; index < iJournalFiles.Count(); ++index)
       
   110 		if (iJournalFiles[index]->Drive() == aDrive)
       
   111 			return index;
       
   112 		
       
   113 	return KErrNotFound;
       
   114 	}
       
   115 
       
   116 void CJournal::DeleteJournalFilesL()
       
   117 	{
       
   118 	// delete all journal files
       
   119 	for(TInt drive = 0; drive < iJournalFiles.Count(); drive++)
       
   120 		{		
       
   121 		DeleteJournalFileL(iJournalFiles[drive]->Drive());		
       
   122 		}	
       
   123 	iJournalFiles.ResetAndDestroy();
       
   124 	
       
   125 	// delete the drive array only after all journals have been completed
       
   126 	// (committed or rolled back)
       
   127 	if (iAllDrives.Count() == iCompletedDrives.Count())
       
   128 		{
       
   129 		DeleteDrivesFileL();
       
   130 		}
       
   131 	}
       
   132 
       
   133 void VerifyDirectoryDeletionErrorL(TInt err)
       
   134 	{
       
   135 	if(err != KErrNone && err != KErrNotFound && err != KErrPathNotFound && err != KErrInUse && err != KErrAccessDenied)
       
   136 		{
       
   137 		User::Leave(err);
       
   138 		}	
       
   139 	}
       
   140 
       
   141 
       
   142 void CJournal::DeleteJournalFileL(TInt aDrive, TBool aRecordAllRollbackEvents /* = ETrue */)
       
   143 	{	
       
   144 	TInt index = FindJournalFileIndexL(aDrive);
       
   145 	iJournalFiles[index]->Close();
       
   146 	TDriveUnit journalDrive(aDrive);
       
   147 	RBuf journal;
       
   148 	journal.CreateL(journalDrive.Name(), KMaxFileName);
       
   149 	CleanupClosePushL(journal);
       
   150 	journal.Append(iJournalFileName);
       
   151 
       
   152 	User::LeaveIfError(iFs.Delete(journal));
       
   153 
       
   154 	// record that we have completed this drive
       
   155 	if (aRecordAllRollbackEvents)
       
   156 		UpdateDrivesFileL(aDrive);
       
   157 		
       
   158 	iCompletedDrives.InsertInOrder(aDrive);
       
   159 	
       
   160 	VerifyDirectoryDeletionErrorL(iFs.RmDir(journal));
       
   161 	
       
   162 	CleanupStack::PopAndDestroy(&journal);
       
   163 	}
       
   164 	
       
   165 void CJournal::DeleteDrivesFileL()
       
   166 	{
       
   167 	iCompletedDrives.Reset();
       
   168 	TInt err = KErrNone;
       
   169 	
       
   170 	err = iFs.Delete(iDriveArrayFileName);
       
   171 			
       
   172 	if(err != KErrNone && err != KErrPathNotFound && err != KErrNotFound)
       
   173 		{
       
   174 		User::Leave(err);
       
   175 		}
       
   176     
       
   177     // try removing the journal path
       
   178 	TParse directory;
       
   179 	User::LeaveIfError(directory.SetNoWild(iDriveArrayFileName, NULL, NULL));
       
   180 	while(!directory.IsRoot())
       
   181 		{
       
   182 		// try to remove this directory
       
   183 		TInt err = iFs.RmDir(directory.DriveAndPath());
       
   184 		if(err != KErrNone)
       
   185 			{
       
   186 			VerifyDirectoryDeletionErrorL(err);
       
   187 			break;
       
   188 			}
       
   189 		User::LeaveIfError(directory.PopDir());
       
   190 		}
       
   191 	}
       
   192 
       
   193 // This function parses all the drives registered in the main drive file for this transaction.
       
   194 // This function also checks which drives have been completed.
       
   195 // As a result, two main drive sets: iCompletedDrives and iAllDrives are populated.
       
   196 void CJournal::RefreshDrivesArrayL()
       
   197 	{
       
   198 	// clear existing journal drive arrays prior to reloading them from file
       
   199 	iCompletedDrives.Reset();
       
   200 	iAllDrives.Reset();
       
   201 	
       
   202 	RFileReadStream journalStream;
       
   203 	TInt err = journalStream.Open(iFs, iDriveArrayFileName, EFileStream);
       
   204 	if (err == KErrNotFound || err == KErrPathNotFound)
       
   205 		return;
       
   206 	User::LeaveIfError(err);
       
   207 
       
   208 	CleanupClosePushL(journalStream);
       
   209 	while(ETrue)
       
   210 		{
       
   211 		// read the next entry in the drives' file
       
   212 		TInt drive = 0;
       
   213 		TRAP(err, drive = journalStream.ReadInt32L());
       
   214 		if (err == KErrEof)
       
   215 			break;
       
   216 		
       
   217 		User::LeaveIfError(err);
       
   218 
       
   219 		if(iAllDrives.Find(drive) == KErrNotFound)
       
   220 			{
       
   221 			// first instance indicates this drive was part of this
       
   222 			// transaction
       
   223 			iAllDrives.InsertInOrder(drive);
       
   224 			}
       
   225 		else
       
   226 			{
       
   227 			// second instance indicates this drive has been committed
       
   228 			// or rolled back
       
   229 			iCompletedDrives.InsertInOrder(drive);
       
   230 			}
       
   231 		}
       
   232 	CleanupStack::PopAndDestroy(&journalStream);
       
   233 	}
       
   234 
       
   235 void CJournal::UpdateDrivesFileL(TInt aDrive)
       
   236 	{
       
   237 	RFile file;
       
   238 	CleanupClosePushL(file);
       
   239 	// try opening the file if it already exists
       
   240 	TInt err = file.Open(iFs, iDriveArrayFileName, EFileWrite);
       
   241 	if (err != KErrNone)
       
   242 		{
       
   243 		if (err == KErrNotFound || err == KErrPathNotFound)
       
   244 			{
       
   245 			err = iFs.MkDirAll(iDriveArrayFileName);
       
   246 			if(err != KErrNone && err != KErrAlreadyExists)
       
   247 				{
       
   248 				User::Leave(err);
       
   249 				}
       
   250 			// journal does not exist, try creating one
       
   251 			User::LeaveIfError(file.Create(iFs, iDriveArrayFileName, EFileWrite));
       
   252 			}
       
   253 		else
       
   254 			{
       
   255 			User::Leave(err);
       
   256 			}
       
   257 		}
       
   258 	
       
   259 	TInt fileSize;
       
   260 	User::LeaveIfError(file.Size(fileSize));
       
   261 	
       
   262 	// attach to end of file for writing
       
   263 	RFileWriteStream stream;
       
   264 	stream.Attach(file, fileSize);	
       
   265 	CleanupStack::Pop(&file); // file ownership
       
   266 	CleanupClosePushL(stream);// transfered to stream
       
   267 	stream.WriteInt32L(aDrive);
       
   268 	CleanupStack::PopAndDestroy(&stream);
       
   269 	}
       
   270 
       
   271 void CJournal::StartCommitL()
       
   272 	{
       
   273 	DEBUG_PRINTF3(_L("CJournal::StartCommitL() - iJournalFileName %S   iDriveArrayFileName %S"), &iJournalFileName, &iDriveArrayFileName);							
       
   274 	// To commit, all drives must exist. No exceptions.
       
   275 	if (iJournalFiles.Count() != iAllDrives.Count())
       
   276 		{
       
   277 		User::Leave(KErrNotReady);
       
   278 		}
       
   279 		
       
   280 	// none of the drives must yet be completed...
       
   281 	if (iCompletedDrives.Count() != 0)
       
   282 		{
       
   283 		User::Leave(KErrNotSupported);
       
   284 		}
       
   285 		
       
   286 	// synch up all the drives
       
   287 	TInt drivesCount(iJournalFiles.Count());
       
   288 	for (TInt i = 0; i < drivesCount; ++i)
       
   289 		{
       
   290  		//Check if Journal drives are present.
       
   291  		TDriveInfo info;
       
   292  		if (iFs.Drive(info, iJournalFiles[i]->Drive())!=KErrNone || info.iType==EMediaNotPresent)
       
   293  			{
       
   294  			User::Leave(KErrNotReady);	
       
   295  			}
       
   296 		}		
       
   297 	}
       
   298 
       
   299 void CJournal::FinishCommitL()
       
   300 	{
       
   301 	DEBUG_PRINTF3(_L("CJournal::FinishCommitL() - iJournalFileName %S   iDriveArrayFileName %S"), &iJournalFileName, &iDriveArrayFileName);							
       
   302 	DeleteJournalFilesL();
       
   303 	
       
   304 	// return the journal to a state where it can be used again,
       
   305 	// in the insane event that someone wants to...
       
   306 	// (I'm looking at you sisregistry)
       
   307 	iCompletedDrives.Reset();
       
   308 	iAllDrives.Reset();
       
   309 	}
       
   310 
       
   311 void CJournal::FinishRollbackL(TInt aDrive, TBool aRecordAllRollbackEvents /* = ETrue */)
       
   312 	{
       
   313 	DEBUG_PRINTF4(_L("CJournal::FinishRollbackL() - iJournalFileName %S   iDriveArrayFileName %S   aDrive %d"), &iJournalFileName, &iDriveArrayFileName, aDrive);
       
   314 	DeleteJournalFileL(aDrive, aRecordAllRollbackEvents);
       
   315 	
       
   316 	if (iCompletedDrives.Count() == iAllDrives.Count())
       
   317 		{
       
   318 		DeleteDrivesFileL();
       
   319 		}
       
   320 	}
       
   321 
       
   322 // This function verifies whether a journal exists for this drive in this transaction.
       
   323 // If it doesn't, then the journal file is created and added to iJournalFiles
       
   324 TInt CJournal::PrepareToWriteL(TInt aDrive)
       
   325 	{	
       
   326 	TInt index = FindJournalFileIndexL(aDrive);
       
   327 	if (index >= 0) // The journal already exists
       
   328 		{
       
   329 		return index;
       
   330 		}
       
   331 	
       
   332 	__ASSERT_ALWAYS(index == KErrNotFound, User::Invariant());
       
   333 	// The journal does not exist - we need to create one
       
   334 	
       
   335 	TDriveUnit drive(aDrive);
       
   336 	RBuf journalPath;
       
   337 	journalPath.CreateL(drive.Name(), KMaxFileName);
       
   338 	CleanupClosePushL(journalPath);
       
   339 	journalPath.Append(iJournalFileName);
       
   340 
       
   341 	CJournalFile* journalFile = CJournalFile::NewLC(iFs, iLoader, journalPath, aDrive);
       
   342 	iJournalFiles.AppendL(journalFile);
       
   343 	CleanupStack::Pop(journalFile);
       
   344 	CleanupStack::PopAndDestroy(&journalPath);
       
   345 	
       
   346 	UpdateDrivesFileL(aDrive);
       
   347 	
       
   348 	User::LeaveIfError(iAllDrives.InsertInOrder(aDrive));
       
   349 
       
   350 	return iJournalFiles.Count() - 1; // Since we appended the entry, we return the last index
       
   351 	}
       
   352 	
       
   353 void CJournal::DeleteFilesL(TIntegrityServicesEvent aTypeFilter)
       
   354 	{
       
   355 	TInt journalsCount(iJournalFiles.Count());
       
   356 	for (TInt i = 0; i < journalsCount; ++i)
       
   357 		{
       
   358 		iJournalFiles[i]->JournalOperationL(IntegrityDeleteFileL, aTypeFilter, CIntegrityServices::EFailDeletingFile);
       
   359 		}
       
   360 	}
       
   361 	
       
   362 void CJournal::RestoreFilesL(TInt aDrive)
       
   363 	{
       
   364 	TInt index = FindJournalFileIndexL(aDrive);
       
   365 	User::LeaveIfError(index);
       
   366 	iJournalFiles[index]->JournalOperationL(IntegrityRestoreFileL, ERemovedFile, CIntegrityServices::EFailRestoringFile);
       
   367 	}
       
   368 		
       
   369 void CJournal::DeleteFilesL(TIntegrityServicesEvent aTypeFilter, TInt aDrive)
       
   370 	{
       
   371 	TInt index = FindJournalFileIndexL(aDrive);
       
   372 	User::LeaveIfError(index);
       
   373 	iJournalFiles[index]->JournalOperationL(IntegrityDeleteFileL, aTypeFilter, CIntegrityServices::EFailDeletingFile);
       
   374 	}
       
   375 
       
   376 void CJournal::WriteJournalEventL(TIntegrityServicesEvent aEvent)
       
   377 	{
       
   378 	// write the event to each journal file
       
   379 	for(TInt index = 0; index < iJournalFiles.Count(); index++)
       
   380 		{
       
   381 		iJournalFiles[index]->EventL(aEvent);
       
   382 		}
       
   383 	}
       
   384 	
       
   385 void CJournal::WriteJournalEventL(TIntegrityServicesEvent aEvent, TInt aDrive, TBool aSerializeEventToJournal)
       
   386 	{
       
   387 	TInt index = FindJournalFileIndexL(aDrive);
       
   388 	User::LeaveIfError(index);
       
   389 	iJournalFiles[index]->EventL(aEvent, aSerializeEventToJournal);
       
   390 	}
       
   391 
       
   392 void CJournal::AddL(const TDesC& aFileName)
       
   393 	{
       
   394 	DEBUG_PRINTF2(_L("CJournal::AddL() - aFileName %S"), &aFileName);							
       
   395 	// write the filename to the journal on the same drive
       
   396 	TInt drive = CJournalFile::CheckFileNameL(iFs, aFileName);
       
   397 	TInt index = PrepareToWriteL(drive);
       
   398 	iJournalFiles[index]->AddL(aFileName);
       
   399 	}
       
   400 
       
   401 void CJournal::RemoveL(const TDesC& aFileName, TDes& backupFileName)
       
   402 	{
       
   403 	DEBUG_PRINTF2(_L("CJournal::RemoveL() - aFileName %S"), &aFileName);							
       
   404 	// write the filename to the journal on the same drive
       
   405 	TInt drive = CJournalFile::CheckFileNameL(iFs, aFileName);
       
   406 	TInt index = PrepareToWriteL(drive);
       
   407 	iJournalFiles[index]->RemoveL(aFileName, backupFileName);
       
   408 	}
       
   409 
       
   410 void CJournal::TemporaryL(const TDesC& aFileName)
       
   411 	{
       
   412 	DEBUG_PRINTF2(_L("CJournal::TemporaryL() - aFileName %S"), &aFileName);							
       
   413 	// write the filename to the journal on the same drive
       
   414 	TInt drive = CJournalFile::CheckFileNameL(iFs, aFileName);
       
   415 	TInt index = PrepareToWriteL(drive);
       
   416 	iJournalFiles[index]->TemporaryL(aFileName);
       
   417 	}
       
   418 
       
   419 TIntegrityServicesEvent CJournal::LastEventL() const
       
   420 	{
       
   421 	TIntegrityServicesEvent lastEvent = ENone;
       
   422 	// work out the "real" last event
       
   423 	// journals states may be at different since they cannot all be written
       
   424 	// to simultaneously
       
   425 	for(TInt index = 0; index < iJournalFiles.Count(); index++)
       
   426 		{
       
   427 		TInt position = iCompletedDrives.Find(iJournalFiles[index]->Drive());
       
   428 		
       
   429 		if(position!=KErrNotFound) 
       
   430 			continue; //don't check completed drives
       
   431 		
       
   432 		TIntegrityServicesEvent currentJournalLastEvent = iJournalFiles[index]->LastEvent();
       
   433 		if (currentJournalLastEvent >= lastEvent)
       
   434 			lastEvent = currentJournalLastEvent;
       
   435 		}//for
       
   436 	return lastEvent;
       
   437 	}
       
   438 	
       
   439 TIntegrityServicesEvent CJournal::LastEventL(TInt aDrive) const
       
   440 	{
       
   441 	DEBUG_PRINTF2(_L("CJournal::LastEventL() - aDrive %d"), aDrive);							
       
   442 	TInt index = FindJournalFileIndexL(aDrive);
       
   443 	User::LeaveIfError(index);
       
   444 
       
   445 	return iJournalFiles[index]->LastEvent();
       
   446 	}
       
   447 
       
   448 /*static*/ void CJournal::CreateDrvFileNameL(TStsTransactionId aTransactionID, const TDesC& aPath, TDes& aDrvFileName)
       
   449 	{
       
   450 	DEBUG_PRINTF3(_L("CJournal::CreateDrvFileNameL() - aTransactionID %X aPath %S"), aTransactionID, &aPath);							
       
   451 	aDrvFileName = TDriveUnit(::RFs::GetSystemDrive()).Name();
       
   452 	aDrvFileName.Append(aPath);
       
   453 	aDrvFileName.AppendNumUC(static_cast<TUint32>(aTransactionID), EHex);
       
   454 	aDrvFileName.Append(KExtDelimiter);
       
   455 	aDrvFileName.Append(KDriveExt);
       
   456 	}
       
   457 
       
   458 /*static*/ void CJournal::CreateJournalFileNameL(TStsTransactionId aTransactionID, const TDesC& aPath, TDes& aJournalFileName)
       
   459 	{
       
   460 	aJournalFileName.Append(aPath);
       
   461 	aJournalFileName.AppendNumUC(static_cast<TUint32>(aTransactionID), EHex);
       
   462 	aJournalFileName.Append(KExtDelimiter);
       
   463 	aJournalFileName.Append(KJournalExt);
       
   464 	}
       
   465 
       
   466 /*static*/ TInt CJournal::RecoverTransactionIdFromDrvFileName(const TDesC& aDrvFileName, TStsTransactionId& aTransactionID)
       
   467 	{
       
   468 	TLex lex(aDrvFileName);
       
   469 	TUint32 tmp;
       
   470 	TInt result = lex.Val(tmp, EHex);
       
   471 	aTransactionID = static_cast<TStsTransactionId>(tmp);
       
   472 	return result;
       
   473 	}
       
   474 
       
   475 void CJournal::RollBackDriveL(TInt aDrive, TBool aRecordAllRollbackEvents)
       
   476 	{
       
   477 	switch (LastEventL())
       
   478 		{
       
   479 		// Transaction did not complete, rollback required
       
   480 		case ERemovedFile:
       
   481 		case EBackupFile:
       
   482 		case ETempFile:
       
   483 		case EAddedFile:
       
   484 		case EAddedFilesRemoved:
       
   485 		case ERemovedFilesRestored:
       
   486 			// rollback this individual journal from where it last got to.
       
   487 			switch (LastEventL(aDrive))
       
   488 				{
       
   489 			case ERemovedFile:
       
   490 			case EBackupFile:
       
   491 			case ETempFile:
       
   492 			case EAddedFile:
       
   493 				DeleteFilesL(EAddedFile, aDrive);
       
   494 				
       
   495 				CIntegrityServices::SimulatePowerFailureL(CIntegrityServices::EFailNewFilesRemoved, CIntegrityServices::EBeforeJournal, KNullDesC);				
       
   496 				WriteJournalEventL(EAddedFilesRemoved, aDrive, aRecordAllRollbackEvents);
       
   497 				CIntegrityServices::SimulatePowerFailureL(CIntegrityServices::EFailNewFilesRemoved, CIntegrityServices::EAfterJournal, KNullDesC);
       
   498 				
       
   499 			// fall-through - automatically proceed to the next state, we'll start from EAddedFilesRemoved in case the RestoreFileL has failed
       
   500 			case EAddedFilesRemoved:
       
   501 				RestoreFilesL(aDrive);				
       
   502 				CIntegrityServices::SimulatePowerFailureL(CIntegrityServices::EFailOldFilesRestored, CIntegrityServices::EBeforeJournal, KNullDesC);			
       
   503 				WriteJournalEventL(ERemovedFilesRestored, aDrive, aRecordAllRollbackEvents);
       
   504 				CIntegrityServices::SimulatePowerFailureL(CIntegrityServices::EFailOldFilesRestored, CIntegrityServices::EAfterJournal, KNullDesC);
       
   505 
       
   506 			// fall-through - automatically proceed to the next state
       
   507 			case ERemovedFilesRestored:				
       
   508 				DeleteFilesL(ETempFile, aDrive);
       
   509 				
       
   510 				CIntegrityServices::SimulatePowerFailureL(CIntegrityServices::EFailTempFilesRemoved, CIntegrityServices::EBeforeJournal, KNullDesC);				
       
   511 				WriteJournalEventL(ETempFilesRemoved, aDrive, aRecordAllRollbackEvents);
       
   512 				CIntegrityServices::SimulatePowerFailureL(CIntegrityServices::EFailTempFilesRemoved, CIntegrityServices::EAfterJournal, KNullDesC);
       
   513 				break;
       
   514 				
       
   515 			case ETempFilesRemoved:
       
   516 				break;
       
   517 		
       
   518 			// nothing was done, just delete the journal file
       
   519 			case ENone:
       
   520 				break;
       
   521 		
       
   522 			// Erk! Bad state, bad state!
       
   523 			default:
       
   524 				User::Leave(KErrCorrupt);
       
   525 				break;
       
   526 				}
       
   527 			break;
       
   528 			
       
   529 		// Transaction complete, just need to remove the backup
       
   530 		case ECommitted:
       
   531 		case EBackupFilesRemoved:
       
   532 			switch (LastEventL(aDrive))
       
   533 				{
       
   534 			// At least one journal had a complete transaction...
       
   535 			// roll forwards all journal files.
       
   536 			case ERemovedFile:
       
   537 			case EBackupFile:
       
   538 			case ETempFile:
       
   539 			case EAddedFile:
       
   540 			case ECommitted:
       
   541 				DeleteFilesL(EBackupFile, aDrive);
       
   542 			
       
   543 				CIntegrityServices::SimulatePowerFailureL(CIntegrityServices::EFailBackupFilesRemoved, CIntegrityServices::EBeforeJournal, KNullDesC);			
       
   544 				WriteJournalEventL(EBackupFilesRemoved, aDrive, aRecordAllRollbackEvents);
       
   545 				CIntegrityServices::SimulatePowerFailureL(CIntegrityServices::EFailBackupFilesRemoved, CIntegrityServices::EAfterJournal, KNullDesC);
       
   546 
       
   547 				//fall-through - automatically proceed to the next state
       
   548 			case EBackupFilesRemoved:				
       
   549 				DeleteFilesL(ETempFile, aDrive);
       
   550 			
       
   551 				CIntegrityServices::SimulatePowerFailureL(CIntegrityServices::EFailTempFilesRemoved, CIntegrityServices::EBeforeJournal, KNullDesC);
       
   552 				WriteJournalEventL(ETempFilesRemoved, aDrive, aRecordAllRollbackEvents);
       
   553 				CIntegrityServices::SimulatePowerFailureL(CIntegrityServices::EFailTempFilesRemoved, CIntegrityServices::EAfterJournal, KNullDesC);
       
   554 				break;
       
   555 							
       
   556 			case ETempFilesRemoved:
       
   557 				break;
       
   558 		
       
   559 			// nothing was done, just delete the journal file
       
   560 			case ENone:
       
   561 				break;
       
   562 		
       
   563 			// unknown state	
       
   564 			default:
       
   565 				User::Leave(KErrCorrupt);
       
   566 				break;
       
   567 				}
       
   568 			break;
       
   569 			
       
   570 		case ETempFilesRemoved:
       
   571 			break;
       
   572 		
       
   573 		// nothing was done, just delete the journal file
       
   574 		case ENone:
       
   575 			break;
       
   576 		
       
   577 		// unknown state	
       
   578 		default:
       
   579 			User::Leave(KErrCorrupt);
       
   580 			break;
       
   581 		}
       
   582 		
       
   583 	FinishRollbackL(aDrive, aRecordAllRollbackEvents);
       
   584 	}
       
   585 
       
   586 void CJournal::CommitL()
       
   587 	{
       
   588 	StartCommitL();
       
   589 	
       
   590 	CIntegrityServices::SimulatePowerFailureL(CIntegrityServices::EFailInstallComplete, CIntegrityServices::EBeforeJournal, KNullDesC);
       
   591 	WriteJournalEventL(ECommitted);
       
   592 	CIntegrityServices::SimulatePowerFailureL(CIntegrityServices::EFailInstallComplete, CIntegrityServices::EAfterJournal, KNullDesC);
       
   593 	
       
   594 	DeleteFilesL(EBackupFile);
       
   595 	
       
   596 	CIntegrityServices::SimulatePowerFailureL(CIntegrityServices::EFailBackupFilesRemoved, CIntegrityServices::EBeforeJournal, KNullDesC);
       
   597 	WriteJournalEventL(EBackupFilesRemoved);
       
   598 	CIntegrityServices::SimulatePowerFailureL(CIntegrityServices::EFailBackupFilesRemoved, CIntegrityServices::EAfterJournal, KNullDesC);
       
   599 	
       
   600 	DeleteFilesL(ETempFile);
       
   601 	
       
   602 	CIntegrityServices::SimulatePowerFailureL(CIntegrityServices::EFailTempFilesRemoved, CIntegrityServices::EBeforeJournal, KNullDesC);
       
   603 	WriteJournalEventL(ETempFilesRemoved);
       
   604 	CIntegrityServices::SimulatePowerFailureL(CIntegrityServices::EFailTempFilesRemoved, CIntegrityServices::EAfterJournal, KNullDesC);
       
   605 	
       
   606 	FinishCommitL();
       
   607 	}
       
   608 
       
   609 void CJournal::RollBackL(TBool aRecordAllRollbackEvents /*= ETrue*/)
       
   610 	{
       
   611 #ifdef __WINSCW__
       
   612 	// For 2 minutes after initial boot, DLLs are not unloaded. If we are doing a
       
   613 	// rollback, we need to make sure any pending unloadeds are actioned, otherwise a
       
   614 	// previously loaded DLL could cause the rollback to fail on windows (on arm it is legal to
       
   615 	// delete a loaded DLL/EXE, whilst on windows it is not).
       
   616 	RLoader loader;
       
   617 	TInt r = loader.Connect();
       
   618 	if(r == KErrNone)
       
   619 		{
       
   620 		(void)loader.CancelLazyDllUnload();
       
   621 		loader.Close();
       
   622 		}
       
   623 #endif
       
   624 	
       
   625 	for(TInt index = 0; index < iJournalFiles.Count(); index++)
       
   626 		{
       
   627 		TDriveUnit drive = iJournalFiles[index]->Drive();
       
   628 		// check to see if this drive has already been completed
       
   629 		if(iCompletedDrives.Find(drive) != KErrNotFound)
       
   630 			continue;
       
   631 			
       
   632 		// only attempt to recover writeable drives that are present
       
   633 		TDriveInfo info;
       
   634 		User::LeaveIfError(iFs.Drive(info, drive));
       
   635 		if (info.iMediaAtt & KMediaAttWriteProtected || info.iType==EMediaNotPresent)
       
   636 			continue;
       
   637 
       
   638 		TRAPD(err, RollBackDriveL(drive, aRecordAllRollbackEvents));
       
   639 		if(err != KErrNone && err != KErrPathNotFound && err != KErrNotFound
       
   640 			&& err != KErrNotReady)
       
   641 			{
       
   642 			// unexpected error
       
   643 			User::Leave(err);
       
   644 			}
       
   645 		}
       
   646 	}