installationservices/swi/source/integrityservices/journalfile.cpp
changeset 0 ba25891c3a9e
equal deleted inserted replaced
-1:000000000000 0:ba25891c3a9e
       
     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 * CJournalFile implementation
       
    16 *
       
    17 */
       
    18 
       
    19 
       
    20 /**
       
    21  @file 
       
    22  @released
       
    23  @internalTechnology
       
    24 */
       
    25 
       
    26 #include "journalfile.h"
       
    27 
       
    28 _LIT(KBackupExt, "bak"); // extension for removed files in backup location
       
    29 
       
    30 using namespace Swi;
       
    31 
       
    32 CJournalFile* CJournalFile::NewL(RFs& aFs, RLoader& aLoader, const TDesC& aFileName, TInt aDrive)
       
    33 	{
       
    34 	CJournalFile* self = CJournalFile::NewLC(aFs, aLoader, aFileName, aDrive);
       
    35 	CleanupStack::Pop(self);
       
    36 	return self;
       
    37 	}
       
    38 
       
    39 CJournalFile* CJournalFile::NewLC(RFs& aFs, RLoader& aLoader, const TDesC& aFileName, TInt aDrive)
       
    40 	{
       
    41 	CJournalFile* self = new(ELeave) CJournalFile(aFs, aLoader, aDrive);
       
    42 	CleanupStack::PushL(self);
       
    43 	self->ConstructL(aFileName);
       
    44 	return self;
       
    45 	}
       
    46 
       
    47 CJournalFile::CJournalFile(RFs& aFs, RLoader& aLoader, TInt aDrive) 
       
    48 	: iFs(aFs), iLoader(aLoader), iLastEvent(ENone), iDrive(aDrive)
       
    49 	{
       
    50 	}
       
    51 
       
    52 CJournalFile::~CJournalFile()
       
    53 	{
       
    54 	iWriteStream.Close();
       
    55 	delete iTreeRoot;
       
    56 	}
       
    57 
       
    58 void CJournalFile::ConstructL(const TDesC& aFileName)
       
    59 	{
       
    60 	iJournalFileName = aFileName;
       
    61 	
       
    62 	TChar drive;
       
    63 	User::LeaveIfError(iFs.DriveToChar(iDrive, drive));
       
    64 	
       
    65 	TBuf<2> driveSpec;
       
    66 	driveSpec.Append(drive);
       
    67 	driveSpec.Append(KDriveDelimiter);
       
    68 	iTreeRoot = CIntegrityTreeNode::NewL(driveSpec);
       
    69 	
       
    70 	// read any existing entries from this journal
       
    71 	TRAPD(err, ReadL());
       
    72 	if(err != KErrNone)
       
    73 		{
       
    74 		// if necessary create the path
       
    75 		if(err == KErrPathNotFound)
       
    76 			{
       
    77 			err = iFs.MkDirAll(iJournalFileName);
       
    78 			if(err != KErrNone && err != KErrAlreadyExists)
       
    79 				{
       
    80 				User::Leave(err);
       
    81 				}
       
    82 			}
       
    83 		else if (err != KErrNotFound)
       
    84 			{
       
    85 			User::Leave(err);
       
    86 			}
       
    87 		}
       
    88 	// now prepare for writing
       
    89 	PrepareToWriteL();
       
    90 	}
       
    91 
       
    92 void CJournalFile::ReadL()
       
    93 	{
       
    94 	RFileReadStream journalStream;
       
    95 	User::LeaveIfError(journalStream.Open(iFs, iJournalFileName, EFileWrite | EFileShareAny));
       
    96 	CleanupClosePushL(journalStream);
       
    97 	
       
    98 	// we need to check that any filenames read from the journal are on the
       
    99 	// same drive as the journal itself
       
   100 	TDriveUnit journalDrive(iJournalFileName);
       
   101 	
       
   102 	TInt err = KErrNone;
       
   103 	while(err == KErrNone)
       
   104 		{
       
   105 		// read the next journal entry
       
   106 		TRAP(err, ReadEntryL(journalStream, journalDrive));
       
   107 		if(err != KErrNone && err != KErrEof)
       
   108 			{
       
   109 			User::Leave(err);
       
   110 			}
       
   111 		}
       
   112 	CleanupStack::PopAndDestroy(&journalStream);
       
   113 	}
       
   114 
       
   115 void CJournalFile::ReadEntryL(RFileReadStream& aJournalStream, TInt aDrive)
       
   116 	{
       
   117 	TIntegrityServicesEvent event = static_cast<TIntegrityServicesEvent> (aJournalStream.ReadInt32L());
       
   118 	
       
   119 	HBufC* removedFile = NULL;
       
   120 	HBufC* backupFile = NULL;
       
   121 	HBufC* tempFile = NULL;
       
   122 	HBufC* addedFile = NULL;
       
   123 	
       
   124 	switch(event)
       
   125 		{
       
   126 		case ERemovedFile:
       
   127 			{
       
   128 			if(iLastEvent != ENone && iLastEvent != ERemovedFile &&
       
   129 				iLastEvent != ETempFile && iLastEvent != EAddedFile)
       
   130 				{
       
   131 				User::Leave(KErrCorrupt);
       
   132 				}
       
   133 
       
   134 			removedFile = HBufC::NewLC(aJournalStream, KMaxFileName);
       
   135 			// should be followed by the backup event
       
   136 			if(aJournalStream.ReadInt32L() != EBackupFile)
       
   137 				{
       
   138 				User::Leave(KErrCorrupt);
       
   139 				}
       
   140 			backupFile = HBufC::NewLC(aJournalStream, KMaxFileName);
       
   141 			// make sure files are located on the same drive as the journal
       
   142 			if(CJournalFile::CheckFileNameL(iFs, *removedFile) != aDrive 
       
   143 				|| CJournalFile::CheckFileNameL(iFs, *backupFile) != aDrive)
       
   144 				{
       
   145 				User::Leave(KErrCorrupt);
       
   146 				}
       
   147 	
       
   148 			CIntegrityTreeLeaf* removeLeaf = iTreeRoot->AddNodeL(*removedFile, ERemovedFile, iJournalFileName);
       
   149 			CIntegrityTreeLeaf* backupLeaf = iTreeRoot->AddNodeL(*backupFile, EBackupFile, iJournalFileName);
       
   150 
       
   151 			removeLeaf->SetPeer(backupLeaf);
       
   152 			backupLeaf->SetPeer(removeLeaf);
       
   153 
       
   154 			CleanupStack::PopAndDestroy(backupFile);
       
   155 			CleanupStack::PopAndDestroy(removedFile);
       
   156 			iBackupFilesCount++;
       
   157 			iLastEvent = ERemovedFile;
       
   158 			}
       
   159 			break;
       
   160 			
       
   161 		case EBackupFile:
       
   162 			// cannot have a backup file on its own
       
   163 			User::Leave(KErrCorrupt);
       
   164 			break;
       
   165 			
       
   166 		case ETempFile:
       
   167 			if(iLastEvent != ENone && iLastEvent != ERemovedFile &&
       
   168 				iLastEvent != ETempFile && iLastEvent != EAddedFile)
       
   169 				{
       
   170 				User::Leave(KErrCorrupt);
       
   171 				}
       
   172 			
       
   173 			tempFile = HBufC::NewLC(aJournalStream, KMaxFileName);
       
   174 			// make the file is located on the same drive as the journal
       
   175 			if(CJournalFile::CheckFileNameL(iFs, *tempFile) != aDrive)
       
   176 				{
       
   177 				User::Leave(KErrCorrupt);
       
   178 				}
       
   179 			iTreeRoot->AddNodeL(*tempFile, ETempFile, iJournalFileName);
       
   180 			CleanupStack::PopAndDestroy(tempFile);
       
   181 			iLastEvent = ETempFile;
       
   182 			break;
       
   183 			
       
   184 		case EAddedFile:
       
   185 			if(iLastEvent != ENone && iLastEvent != ERemovedFile &&
       
   186 				iLastEvent != ETempFile && iLastEvent != EAddedFile)
       
   187 				{
       
   188 				User::Leave(KErrCorrupt);
       
   189 				}
       
   190 			
       
   191 			addedFile = HBufC::NewLC(aJournalStream, KMaxFileName);
       
   192 			// make the file is located on the same drive as the journal
       
   193 			if(CJournalFile::CheckFileNameL(iFs, *addedFile) != aDrive)
       
   194 				{
       
   195 				User::Leave(KErrCorrupt);
       
   196 				}
       
   197 			iTreeRoot->AddNodeL(*addedFile, EAddedFile, iJournalFileName);
       
   198 			CleanupStack::PopAndDestroy(addedFile);
       
   199 			iLastEvent = EAddedFile;
       
   200 			break;
       
   201 			
       
   202 		case EInstallComplete:
       
   203 			if(iLastEvent != ERemovedFile && iLastEvent != ETempFile &&
       
   204 				iLastEvent != EAddedFile)
       
   205 				{
       
   206 				User::Leave(KErrCorrupt);
       
   207 				}
       
   208 				
       
   209 			iLastEvent = EInstallComplete;
       
   210 			break;
       
   211 			
       
   212 		case EBackupFilesRemoved:
       
   213 			if(iLastEvent != EInstallComplete)
       
   214 				{
       
   215 				User::Leave(KErrCorrupt);
       
   216 				}
       
   217 			iLastEvent = EBackupFilesRemoved;
       
   218 			break;
       
   219 			
       
   220 		case EAddedFilesRemoved:
       
   221 			if(iLastEvent != EInstallComplete && iLastEvent != ERemovedFile &&
       
   222 				iLastEvent != ETempFile && iLastEvent != EAddedFile)
       
   223 				{
       
   224 				User::Leave(KErrCorrupt);
       
   225 				}
       
   226 			iLastEvent = EAddedFilesRemoved;
       
   227 			break;
       
   228 			
       
   229 		case ETempFilesRemoved:
       
   230 			if(iLastEvent != EBackupFilesRemoved || iLastEvent != ERemovedFilesRestored)
       
   231 				{
       
   232 				User::Leave(KErrCorrupt);
       
   233 				}
       
   234 			iLastEvent = ETempFilesRemoved;
       
   235 			break;
       
   236 			
       
   237 		case ERemovedFilesRestored:
       
   238 			if(iLastEvent != EAddedFilesRemoved)
       
   239 				{
       
   240 				User::Leave(KErrCorrupt);
       
   241 				}
       
   242 			iLastEvent = ERemovedFilesRestored;
       
   243 			break;
       
   244 			
       
   245 		default:
       
   246 			User::Leave(KErrCorrupt);
       
   247 			break;
       
   248    		}
       
   249 	}
       
   250 
       
   251 void CJournalFile::PrepareToWriteL()
       
   252 	{
       
   253 	RFile file;
       
   254 	CleanupClosePushL(file);
       
   255 	// try opening the journal if it already exists
       
   256 	TInt err = file.Open(iFs, iJournalFileName, EFileWrite | EFileShareAny);
       
   257 	if (err != KErrNone)
       
   258 		{
       
   259 		if (err == KErrNotFound)
       
   260 			{
       
   261 			// journal does not exist, try creating one
       
   262 			User::LeaveIfError(file.Create(iFs, iJournalFileName, EFileWrite | EFileShareAny));
       
   263 			}
       
   264 		else
       
   265 			{
       
   266 			User::Leave(err);
       
   267 			}
       
   268 		}
       
   269 	
       
   270 	TInt fileSize;
       
   271 	User::LeaveIfError(file.Size(fileSize));
       
   272 	
       
   273 	// attach to end of file for writing
       
   274 	iWriteStream.Attach(file, fileSize);
       
   275 	CleanupStack::Pop(&file); // file ownership transfered to stream
       
   276 	
       
   277 	// record the last known size.
       
   278 	iLastSize = fileSize;
       
   279 	}
       
   280 
       
   281 void CJournalFile::EventL(TIntegrityServicesEvent aEvent)
       
   282 	{
       
   283 	iWriteStream.WriteInt32L(static_cast<TInt> (aEvent));
       
   284 	iWriteStream.CommitL();
       
   285 	
       
   286 	iLastEvent = aEvent;
       
   287 	iLastSize = iWriteStream.Sink()->SizeL();
       
   288 	}
       
   289 
       
   290 void CJournalFile::AddL(const TDesC& aFileName)
       
   291 	{
       
   292 	iTreeRoot->AddNodeL(aFileName, EAddedFile, iJournalFileName);
       
   293 
       
   294 	// write filename to journal
       
   295 	iWriteStream.WriteInt32L(static_cast<TInt> (EAddedFile));
       
   296 	iWriteStream << aFileName;
       
   297 	iWriteStream.CommitL();
       
   298 	
       
   299 	iLastEvent = EAddedFile;
       
   300 	iLastSize = iWriteStream.Sink()->SizeL();
       
   301 	}
       
   302 
       
   303 void CJournalFile::RemoveL(const TDesC& aFileName, TDes& aBackupFileName)
       
   304 	{
       
   305 	// Only journal removals if the file hasn't been added in the same
       
   306 	// journalfile.
       
   307 	TInt err = iTreeRoot->FindNode(aFileName, EAddedFile);
       
   308 	if (err == KErrNotFound)
       
   309 		{
       
   310 		CIntegrityTreeLeaf* removalNode = iTreeRoot->AddNodeL(aFileName, ERemovedFile, iJournalFileName);
       
   311 
       
   312 		// generate the next backup filename for this journal
       
   313 		NextBackupFileNameL(aBackupFileName);
       
   314 	
       
   315 		CIntegrityTreeLeaf* backupNode = iTreeRoot->AddNodeL(aBackupFileName, EBackupFile, iJournalFileName);
       
   316 	
       
   317 		// set peers on both nodes
       
   318 		removalNode->SetPeer(backupNode);
       
   319 		backupNode->SetPeer(removalNode);
       
   320 		
       
   321 		// write filenames to journal
       
   322 		iWriteStream.WriteInt32L(static_cast<TInt> (ERemovedFile));
       
   323 		iWriteStream << aFileName;
       
   324 		iWriteStream.WriteInt32L(static_cast<TInt> (EBackupFile));
       
   325 		iWriteStream << aBackupFileName;
       
   326 		iWriteStream.CommitL();
       
   327 	
       
   328 		iLastEvent = ERemovedFile;
       
   329 		iLastSize = iWriteStream.Sink()->SizeL();
       
   330 		}
       
   331 	else
       
   332 		{
       
   333 		User::LeaveIfError(err);
       
   334 		}
       
   335 	}
       
   336 	
       
   337 void CJournalFile::TemporaryL(const TDesC& aFileName)
       
   338 	{
       
   339 	iTreeRoot->AddNodeL(aFileName, ETempFile, iJournalFileName);
       
   340 	
       
   341 	// write filename to journal
       
   342 	iWriteStream.WriteInt32L(static_cast<TInt> (ETempFile));
       
   343 	iWriteStream << aFileName;
       
   344 	iWriteStream.CommitL();
       
   345 	
       
   346 	iLastEvent = ETempFile;
       
   347 	iLastSize = iWriteStream.Sink()->SizeL();
       
   348 	}
       
   349 	
       
   350 void CJournalFile::SynchL()
       
   351 	{
       
   352 	// check if the journal is in synch with the underlying media
       
   353 	iWriteStream.Sink()->SynchL();
       
   354 	TInt size = iWriteStream.Sink()->SizeL();
       
   355 	if (size != iLastSize)
       
   356 		{
       
   357 		// it isn't in synch! read from the underlying media.
       
   358 		delete iTreeRoot;
       
   359 		iTreeRoot = NULL;
       
   360 		iWriteStream.Close();
       
   361 		iBackupFilesCount = 0;
       
   362 		
       
   363 		TChar drive;
       
   364 		User::LeaveIfError(iFs.DriveToChar(iDrive, drive));
       
   365 		
       
   366 		TBuf<2> driveSpec;
       
   367 		driveSpec.Append(drive);
       
   368 		driveSpec.Append(KDriveDelimiter);
       
   369 		iTreeRoot = CIntegrityTreeNode::NewL(driveSpec);
       
   370 		
       
   371 		ReadL();
       
   372 		PrepareToWriteL();
       
   373 		}
       
   374 	}	
       
   375 	
       
   376 void CJournalFile::JournalOperationL(TTreeWalkFunctionL aFunc, TIntegrityServicesEvent aTypeFilter,
       
   377 	CIntegrityServices& aIntegrityServices, CIntegrityServices::TFailType aFailType)
       
   378 	{
       
   379 	CFileMan* fileman = CFileMan::NewL(iFs);
       
   380 	CleanupStack::PushL(fileman);
       
   381 	iTreeRoot->WalkTreeL(aFunc, aTypeFilter, iFs, iLoader, *fileman, aIntegrityServices, aFailType);
       
   382 	CleanupStack::PopAndDestroy(fileman);
       
   383 	}
       
   384 
       
   385 void CJournalFile::NextBackupFileNameL(TDes& aBackupFileName)
       
   386 	{
       
   387 	TParsePtrC fileNamePtr(iJournalFileName);
       
   388 	aBackupFileName = fileNamePtr.DriveAndPath();
       
   389 	aBackupFileName.Append(fileNamePtr.Name());
       
   390 	aBackupFileName.Append(KPathDelimiter);
       
   391 	aBackupFileName.AppendNumUC(iBackupFilesCount, EHex);
       
   392 	aBackupFileName.Append(KExtDelimiter);
       
   393 	aBackupFileName.Append(KBackupExt);
       
   394 	
       
   395 	iBackupFilesCount++;
       
   396 	}
       
   397 
       
   398 
       
   399 TIntegrityServicesEvent CJournalFile::LastEvent() const
       
   400 	{
       
   401 	return iLastEvent;
       
   402 	}
       
   403 
       
   404 TInt CJournalFile::Drive()
       
   405 	{
       
   406 	return iDrive;
       
   407 	}
       
   408 	
       
   409 void CJournalFile::Close()
       
   410 	{
       
   411 	iWriteStream.Close();
       
   412 	}
       
   413 
       
   414 /*static*/ TInt CJournalFile::CheckFileNameL(RFs&, const TDesC& aFileName)
       
   415 	{
       
   416 	TParse parse;
       
   417 	// We allow wildcards because this is also used to generate the
       
   418 	// uninstall log which may contain FN entries using wildcards.
       
   419 	User::LeaveIfError(parse.Set(aFileName, NULL, NULL));
       
   420 	User::LeaveIfError(parse.DrivePresent());
       
   421 	User::LeaveIfError(parse.PathPresent());
       
   422 	
       
   423 	TDriveUnit drive(parse.Drive());
       
   424 	return drive;
       
   425 	}