os/persistentdata/persistentstorage/centralrepository/cenrepsrv/srvres.cpp
changeset 0 bde4ae8d615e
     1.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     1.2 +++ b/os/persistentdata/persistentstorage/centralrepository/cenrepsrv/srvres.cpp	Fri Jun 15 03:10:57 2012 +0200
     1.3 @@ -0,0 +1,470 @@
     1.4 +// Copyright (c) 2004-2009 Nokia Corporation and/or its subsidiary(-ies).
     1.5 +// All rights reserved.
     1.6 +// This component and the accompanying materials are made available
     1.7 +// under the terms of "Eclipse Public License v1.0"
     1.8 +// which accompanies this distribution, and is available
     1.9 +// at the URL "http://www.eclipse.org/legal/epl-v10.html".
    1.10 +//
    1.11 +// Initial Contributors:
    1.12 +// Nokia Corporation - initial contribution.
    1.13 +//
    1.14 +// Contributors:
    1.15 +//
    1.16 +// Description:
    1.17 +//
    1.18 +
    1.19 +#include "srvres.h"
    1.20 +#include "cachemgr.h"
    1.21 +#include "obsrvr_noc.h"
    1.22 +#include <bautils.h> // BaflUtils::GetSystemDrive
    1.23 +#ifdef SYMBIAN_BAFL_SYSUTIL
    1.24 +#include <bafl/sysutil.h>
    1.25 +#endif
    1.26 +RFs TServerResources::iFs;
    1.27 +
    1.28 +HBufC* TServerResources::iRomDirectory;
    1.29 +HBufC* TServerResources::iDataDirectory;
    1.30 +HBufC* TServerResources::iInstallDirectory;
    1.31 +HBufC* TServerResources::iBURDirectory;
    1.32 +
    1.33 +
    1.34 +HBufC* TServerResources::iIniExt;
    1.35 +HBufC* TServerResources::iCreExt;
    1.36 +HBufC* TServerResources::iTrnsExt;
    1.37 +
    1.38 +TUint8 TServerResources::iPersistsVersion;
    1.39 +
    1.40 +RArray<TOwnerIdMapping> TServerResources::iOwnerIdLookUpTable;
    1.41 +
    1.42 +CRepositoryCacheManager* TServerResources::iCacheManager;
    1.43 +CObservable* TServerResources::iObserver;
    1.44 +
    1.45 +
    1.46 +#ifdef __CENTREP_SERVER_PERFTEST__
    1.47 +TCentRepPerfTest TServerResources::iPerfTestMgr;
    1.48 +#endif
    1.49 +#ifdef __CENTREP_SERVER_MEMTEST__
    1.50 +TInt32 TServerResources::iMemTestData[KMemBufMaxEntry];
    1.51 +TInt32 TServerResources::iMemTestDataCount = 0;
    1.52 +#endif //__CENTREP_SERVER_MEMTEST__
    1.53 +
    1.54 +TTime TServerResources::CentrepFileTimeStampL(TUid aUid, TCentRepLocation aLocation)
    1.55 +	{
    1.56 +	TEntry entry;
    1.57 +	HBufC* fileName(NULL);
    1.58 +	TServerResources::CreateRepositoryFileNameLC(fileName,aUid,aLocation,ECre);
    1.59 +	TInt err=TServerResources::iFs.Entry(fileName->Des(), entry);
    1.60 +   	CleanupStack::PopAndDestroy(fileName);
    1.61 +
    1.62 +	if(err==KErrNone)
    1.63 +		{
    1.64 +		return entry.iModified;
    1.65 +		}
    1.66 +		
    1.67 +	TServerResources::CreateRepositoryFileNameLC(fileName,aUid,aLocation,EIni);
    1.68 +	User::LeaveIfError(TServerResources::iFs.Entry(fileName->Des(), entry));
    1.69 +   	CleanupStack::PopAndDestroy(fileName);
    1.70 +   			   					  
    1.71 +	return entry.iModified;
    1.72 +	}
    1.73 +	
    1.74 +TBool TServerResources::CentrepFileExistsL(TUid aUid, TCentRepLocation aLocation, TCentRepFileType aType)
    1.75 +	{
    1.76 +	HBufC* fileName(NULL);
    1.77 +	TServerResources::CreateRepositoryFileNameLC(fileName,aUid,aLocation,aType);
    1.78 +	TEntry entry;
    1.79 +	TInt err=TServerResources::iFs.Entry(fileName->Des(), entry);
    1.80 +   	CleanupStack::PopAndDestroy(fileName);		   					  
    1.81 + 	
    1.82 + 	TBool r=EFalse;
    1.83 + 	
    1.84 + 	if(err==KErrNone)
    1.85 + 		{
    1.86 + 		r=ETrue;
    1.87 + 		}
    1.88 + 	else if(err==KErrNotFound)
    1.89 + 		{
    1.90 + 		r=EFalse;
    1.91 + 		}
    1.92 + 	// Looking for a file on a composite file system may return KErrPathNotFound when 
    1.93 + 	// the ROM file doesn't exist, so check for this return code as well.
    1.94 + 	else if((aLocation==ERom) && (err== KErrPathNotFound))
    1.95 +  		{
    1.96 + 		r=EFalse;
    1.97 + 		}
    1.98 + 	else
    1.99 + 		{
   1.100 + 		User::Leave(err);
   1.101 + 		}
   1.102 + #ifdef SYMBIAN_CENTREP_SUPPORT_MULTIROFS	
   1.103 + 	//only for ROM we still have to consider for multiple ROFS presence	
   1.104 +	if (aLocation==ERom && r==EFalse)
   1.105 +		{
   1.106 +		//if not still have to check for multi rofs, it might be the case that first file is already mangled
   1.107 +		TMultiRofsList find(aUid);
   1.108 +		TLinearOrder<TMultiRofsList> sort_order(CObservable::CompareUid);		
   1.109 +					
   1.110 +		TInt index=iObserver->GetMultiRofsList().FindInOrder(find,sort_order);		
   1.111 +		if (index!=KErrNotFound)	
   1.112 +			r=ETrue;
   1.113 +		}
   1.114 +#endif		
   1.115 +  	return r;	 
   1.116 +	}
   1.117 +
   1.118 +TBool TServerResources::CentrepFileExistsL(TUid aUid, TCentRepLocation aLocation)
   1.119 +	{
   1.120 +	return( CentrepFileExistsL( aUid, aLocation, EIni) || CentrepFileExistsL( aUid, aLocation, ECre));
   1.121 +	}
   1.122 +	
   1.123 +TBool TServerResources::InstallFileExistsL(TUid aUid)
   1.124 +	{
   1.125 +	return CentrepFileExistsL(aUid, EInstall);
   1.126 +	}
   1.127 +
   1.128 +TBool TServerResources::RomFileExistsL(TUid aUid)
   1.129 +	{
   1.130 +	return CentrepFileExistsL(aUid, ERom);
   1.131 +	}
   1.132 +	
   1.133 +TBool TServerResources::PersistsFileExistsL(TUid aUid)
   1.134 +	{
   1.135 +	return CentrepFileExistsL(aUid, EPersists);
   1.136 +	}	
   1.137 +
   1.138 +#ifdef SYMBIAN_BAFL_SYSUTIL
   1.139 +TInt TServerResources::GetTextFromFile( const TDesC& aFilename, TDes8& aValue)
   1.140 +    {
   1.141 +
   1.142 +    TInt err = KErrNone;
   1.143 +    RFile file;
   1.144 +    err = file.Open( iFs, aFilename,
   1.145 +                     EFileRead | EFileStreamText | EFileShareReadersOnly );
   1.146 +    if (err != KErrNone)
   1.147 +        {
   1.148 +        return err;
   1.149 +        }
   1.150 +
   1.151 +    err = file.Read(aValue);
   1.152 +    
   1.153 +    file.Close();
   1.154 +
   1.155 +    return err;
   1.156 +    }
   1.157 +#endif		
   1.158 +void TServerResources::DeleteCentrepFileL(TUid aUid, TCentRepLocation aLocation, TCentRepFileType aType)
   1.159 +	{
   1.160 +	HBufC* fileName(NULL);
   1.161 +	TServerResources::CreateRepositoryFileNameLC(fileName,aUid,aLocation,aType);
   1.162 +	if(CentrepFileExistsL( aUid, aLocation, aType))	
   1.163 +		{
   1.164 +		User::LeaveIfError(TServerResources::iFs.Delete(fileName->Des()));
   1.165 +		}
   1.166 +	CleanupStack::PopAndDestroy(fileName);	 
   1.167 +	}	
   1.168 +
   1.169 +void TServerResources::CreateRepositoryFileNameLC(HBufC*& aFullFileName,
   1.170 +												  TUid aUid,
   1.171 +                                                  TCentRepLocation aLocation,
   1.172 +                                                  TCentRepFileType aFileType)
   1.173 +    {
   1.174 +	TServerResources::CreateRepositoryFileNameL(aFullFileName,aUid,aLocation,aFileType);
   1.175 +	CleanupStack::PushL(aFullFileName);
   1.176 +    }
   1.177 +/**
   1.178 +Generic routine for creating a full repository file name.
   1.179 +aFullFileName is created on the heap and it is caller responsibility
   1.180 +to delete it. 
   1.181 +*/
   1.182 +void TServerResources::CreateRepositoryFileNameL(HBufC*& aFullFileName,
   1.183 +												  TUid aUid,
   1.184 +                                                  TCentRepLocation aLocation,
   1.185 +                                                  TCentRepFileType aFileType)
   1.186 +    {
   1.187 +    const TInt KExtLen = 4;
   1.188 +    const TInt KDirLen = 40;
   1.189 +    const TInt KUidLen = 8;
   1.190 +
   1.191 +    TBuf<KDirLen> directory;
   1.192 +    TBuf<KExtLen> ext;
   1.193 +
   1.194 +	//path
   1.195 +	switch (aLocation)
   1.196 +	    {
   1.197 +	    case EPersists:
   1.198 +	        {
   1.199 +	        directory.Copy(iDataDirectory->Des());
   1.200 +	        }
   1.201 +	    	break;
   1.202 +	    case EInstall:
   1.203 +	        {
   1.204 +	        directory.Copy(iInstallDirectory->Des());
   1.205 +	        }
   1.206 +	    	break;
   1.207 +	    case ERom:
   1.208 +	        {
   1.209 +	        if (iRomDirectory)
   1.210 +	        	{
   1.211 +	        	directory.Copy(iRomDirectory->Des());	        	
   1.212 +	        	}
   1.213 +			else
   1.214 +				{
   1.215 +				User::Leave(KErrNotFound); //CentRep ROM directory is empty or doesn't exist at all
   1.216 +				}
   1.217 +	        }
   1.218 +	    	break;
   1.219 +	    default:
   1.220 +	        User::Leave(KErrNotFound); //should never get here
   1.221 +	    }
   1.222 +	    
   1.223 +    //file name	    
   1.224 +	TBuf<KUidLen> name;
   1.225 +	name.NumFixedWidth(aUid.iUid, EHex, KUidLen);
   1.226 +
   1.227 +	//extension
   1.228 +    switch (aFileType)
   1.229 +        {
   1.230 +        case ECre:
   1.231 +            {
   1.232 +            ext.Copy(iCreExt->Des());
   1.233 +            }
   1.234 +            break;
   1.235 +        case EIni:
   1.236 +            {
   1.237 +            ext.Copy(iIniExt->Des());
   1.238 +            }
   1.239 +            break;
   1.240 +        case ETmp:
   1.241 +	    	{
   1.242 +	    	ext.Copy(iTrnsExt->Des());
   1.243 +	    	}
   1.244 +	    	break;
   1.245 +        default:
   1.246 +	        User::Leave(KErrNotFound); //should never get here
   1.247 +        }
   1.248 +
   1.249 +    TBuf<KMaxFileName>  fullFileName;
   1.250 +    fullFileName.Append(directory);
   1.251 +    fullFileName.Append(name);
   1.252 +    fullFileName.Append(ext);
   1.253 +    //allocates memory on the heap. It is caller's resposibility to delete aFullFileName
   1.254 +    aFullFileName = fullFileName.AllocL(); 
   1.255 +    }
   1.256 +
   1.257 +void TServerResources::InitialiseL()
   1.258 +	{
   1.259 +#ifndef SYMBIAN_CENTREP_SUPPORT_MULTIROFS
   1.260 +	iPersistsVersion = KPersistFormatVersion;	// Version 0 of persists
   1.261 +#else
   1.262 +	iPersistsVersion = KPersistFormatSupportsIndMetaIndicator;
   1.263 +#endif	
   1.264 +	User::LeaveIfError(iFs.Connect());
   1.265 +
   1.266 +	// get system drive
   1.267 +	_LIT(KDriveMask, "_:");
   1.268 +	TDriveName systemDrive(KDriveMask);
   1.269 +	systemDrive[0] = 'A' + static_cast<TInt>(RFs::GetSystemDrive());
   1.270 +		
   1.271 +	_LIT(KRomDrive, "z:");			// This may not always be z:
   1.272 +	_LIT(KPersistsDir, "persists\\");
   1.273 +	_LIT(KBURDir, "bur\\");
   1.274 +
   1.275 +	
   1.276 +	// File extensions
   1.277 +	
   1.278 +	_LIT(KIniFileExtension, ".txt");
   1.279 +	_LIT(KExternalizedPersistsFileExt, ".cre");
   1.280 +	_LIT(KTransactFileExt, ".tmp");
   1.281 +
   1.282 +	const TInt KMaxExtLength=4;
   1.283 +	
   1.284 +	iIniExt=HBufC::NewL(KMaxExtLength);
   1.285 +	iCreExt=HBufC::NewL(KMaxExtLength);
   1.286 +	iTrnsExt=HBufC::NewL(KMaxExtLength);
   1.287 +
   1.288 +	iIniExt->Des().Copy(KIniFileExtension);
   1.289 +	iCreExt->Des().Copy(KExternalizedPersistsFileExt);
   1.290 +	iTrnsExt->Des().Copy(KTransactFileExt);
   1.291 +	
   1.292 +	TBuf<KMaxFileName> path;
   1.293 +	User::LeaveIfError(iFs.PrivatePath(path));
   1.294 +
   1.295 +	const TInt pathLen = path.Length();
   1.296 +	
   1.297 +	//
   1.298 +	// ROM-drive cenrep directory
   1.299 +	//
   1.300 +	iRomDirectory = HBufC::NewL(KRomDrive().Length()+pathLen);
   1.301 +	TPtr ptr(iRomDirectory->Des());
   1.302 +	ptr.Append(KRomDrive);
   1.303 +	ptr.Append(path);
   1.304 +	// If the ROM directory does not exist (very unlikely) we set iRomDirectory to zero.
   1.305 +	TEntry fsEntry;
   1.306 +	if(iFs.Entry(*iRomDirectory, fsEntry)!=KErrNone || !fsEntry.IsDir())
   1.307 +		{
   1.308 +		delete iRomDirectory;
   1.309 +		iRomDirectory = NULL;
   1.310 +		}
   1.311 +
   1.312 +	//
   1.313 +	// Cenrep install directory
   1.314 +	//
   1.315 +	iInstallDirectory = HBufC::NewL(systemDrive.Length()+pathLen);
   1.316 +	ptr.Set(iInstallDirectory->Des());
   1.317 +	ptr.Append(systemDrive);
   1.318 +	ptr.Append(path);
   1.319 +	TInt r = iFs.MkDirAll(*iInstallDirectory);
   1.320 +	if(r!=KErrNone && r!=KErrAlreadyExists)
   1.321 +		{
   1.322 +		User::Leave(r);		
   1.323 +		}
   1.324 +	//
   1.325 +	// Writeable-drive data directory
   1.326 +	//
   1.327 +
   1.328 +	iDataDirectory = HBufC::NewL(systemDrive.Length()+pathLen+KPersistsDir().Length());
   1.329 +	ptr.Set(iDataDirectory->Des());
   1.330 +	ptr.Append(systemDrive);
   1.331 +	ptr.Append(path);
   1.332 +	ptr.Append(KPersistsDir);
   1.333 +	
   1.334 +	r = iFs.MkDirAll(*iDataDirectory);
   1.335 +	if(r!=KErrNone && r!=KErrAlreadyExists)
   1.336 +		User::Leave(r);
   1.337 +
   1.338 +	//
   1.339 +	// Writeable-drive backup/restore directory
   1.340 +	//
   1.341 +	iBURDirectory = HBufC::NewL(systemDrive.Length()+pathLen+KBURDir().Length());
   1.342 +	ptr.Set(iBURDirectory->Des());
   1.343 +	ptr.Append(systemDrive);
   1.344 +	ptr.Append(path);
   1.345 +	ptr.Append(KBURDir);
   1.346 +	
   1.347 +	r = iFs.MkDirAll(*iBURDirectory);
   1.348 +	if(r!=KErrNone && r!=KErrAlreadyExists)
   1.349 +		User::Leave(r);
   1.350 +
   1.351 +#ifdef SYMBIAN_BAFL_SYSUTIL	
   1.352 +	//
   1.353 +	// romversion directory
   1.354 +	//
   1.355 +	_LIT(KRomCache,"romversion\\");
   1.356 +	HBufC* romVersionDir = HBufC::NewLC(systemDrive.Length()+pathLen+KRomCache().Length());
   1.357 +	
   1.358 +	ptr.Set(romVersionDir->Des());
   1.359 +	ptr.Append(systemDrive);
   1.360 +	ptr.Append(path);
   1.361 +	ptr.Append(KRomCache);
   1.362 +	
   1.363 +	r = iFs.MkDirAll(*romVersionDir);
   1.364 +	if(r!=KErrNone && r!=KErrAlreadyExists)
   1.365 +		User::Leave(r);
   1.366 +	CleanupStack::PopAndDestroy();//romVersionDir
   1.367 +#endif
   1.368 +	//
   1.369 +	// Cache Manager 
   1.370 +	//
   1.371 +	iCacheManager = CRepositoryCacheManager::NewLC(iFs);
   1.372 +	CleanupStack::Pop();
   1.373 +	
   1.374 +	// Observer
   1.375 +	iObserver = CObservable::NewLC();
   1.376 +	CleanupStack::Pop();
   1.377 +	
   1.378 +	iOwnerIdLookUpTable.Reset();
   1.379 +
   1.380 +	}
   1.381 +
   1.382 +void TServerResources::Close()
   1.383 +	{
   1.384 +	delete iObserver;
   1.385 +	delete iCacheManager;
   1.386 +	delete iInstallDirectory;
   1.387 +	delete iDataDirectory;
   1.388 +	delete iRomDirectory;
   1.389 +	delete iBURDirectory;
   1.390 +	delete iIniExt;
   1.391 +	delete iCreExt;
   1.392 +	delete iTrnsExt;
   1.393 +	iFs.Close();
   1.394 +
   1.395 +	iCacheManager=NULL;
   1.396 +	iInstallDirectory=NULL;
   1.397 +	iDataDirectory=NULL;
   1.398 +	iRomDirectory=NULL;
   1.399 +	iBURDirectory=NULL;	
   1.400 +	iIniExt=NULL;	
   1.401 +	iCreExt=NULL;	
   1.402 +	iTrnsExt=NULL;	
   1.403 +	
   1.404 +	iOwnerIdLookUpTable.Close() ;
   1.405 +	}
   1.406 +	
   1.407 +TInt TServerResources::GetUid(TEntry& aEntry, TUid& aUid)
   1.408 +	{
   1.409 +		const TInt KUidLen = 8;
   1.410 +		TPtrC uidPtr = aEntry.iName.Des().LeftTPtr(KUidLen);
   1.411 +		TLex  lex=uidPtr;
   1.412 +		TUint32 uidValue;
   1.413 +
   1.414 +		if (lex.Val(uidValue, EHex) == KErrNone)
   1.415 +			{
   1.416 +			aUid = TUid::Uid(uidValue);
   1.417 +			}
   1.418 +		else
   1.419 +			{
   1.420 +			return KErrNotFound;
   1.421 +			}
   1.422 +			
   1.423 +	return KErrNone;
   1.424 +	}
   1.425 +
   1.426 +TInt TOwnerIdMapping::CompareUids (const TOwnerIdMapping& aOwnerIdMapping1, const TOwnerIdMapping& aOwnerIdMapping2) 
   1.427 +	{
   1.428 +	if (aOwnerIdMapping1.iRepUid < aOwnerIdMapping2.iRepUid)
   1.429 +		return -1 ;
   1.430 +	else if (aOwnerIdMapping1.iRepUid > aOwnerIdMapping2.iRepUid)
   1.431 +		return 1 ;
   1.432 +	else
   1.433 +		return 0 ;
   1.434 +	}
   1.435 +		
   1.436 +
   1.437 +#ifdef __CENTREP_SERVER_MEMTEST__
   1.438 +
   1.439 +void TServerResources::StopRecordTimerResult()
   1.440 +	{
   1.441 +	iMemTestDataCount = KMemBufMaxEntry;
   1.442 +	}
   1.443 +
   1.444 +//aLocation: location where the memory reading is done (a method specifier)
   1.445 +//aIdentifier: identifier of the memory reading (e.g. repository id, 10th reading etc)
   1.446 +void TServerResources::RecordTimerResult(TMemTestLocationIdentifier aLocation, TInt32 aIdentifier)
   1.447 +	{
   1.448 +	if(iMemTestDataCount+6 > KMemBufMaxEntry)
   1.449 +		{
   1.450 +		if(iMemTestDataCount+3 <= KMemBufMaxEntry)
   1.451 +			{
   1.452 +			iMemTestData[iMemTestDataCount++] = static_cast<TInt32>(KMagicMemTestOutOfBounds);
   1.453 +			iMemTestData[iMemTestDataCount++] = static_cast<TInt32>(KMagicMemTestOutOfBounds);
   1.454 +			iMemTestData[iMemTestDataCount++] = static_cast<TInt32>(KMagicMemTestOutOfBounds);
   1.455 +			}
   1.456 +		}
   1.457 +	else
   1.458 +		{
   1.459 +		RHeap& heap = User::Heap();
   1.460 +		TInt biggestBlock;
   1.461 +		
   1.462 +		iMemTestData[iMemTestDataCount++] = aLocation;
   1.463 +		iMemTestData[iMemTestDataCount++] = aIdentifier;
   1.464 +		iMemTestData[iMemTestDataCount++] = static_cast<TInt32>(heap.Size() - heap.Available(biggestBlock));
   1.465 +		}
   1.466 +	}
   1.467 +	
   1.468 +void TServerResources::StartRecordTimerResult()
   1.469 +	{
   1.470 +	iMemTestDataCount = 0;
   1.471 +	}
   1.472 +	
   1.473 +#endif //__CENTREP_SERVER_MEMTEST__