applayerpluginsandutils/bookmarksupport/test/cenrepsrv/srvres.cpp
changeset 0 b16258d2340f
equal deleted inserted replaced
-1:000000000000 0:b16258d2340f
       
     1 // Copyright (c) 2004-2009 Nokia Corporation and/or its subsidiary(-ies).
       
     2 // All rights reserved.
       
     3 // This component and the accompanying materials are made available
       
     4 // under the terms of "Eclipse Public License v1.0"
       
     5 // which accompanies this distribution, and is available
       
     6 // at the URL "http://www.eclipse.org/legal/epl-v10.html".
       
     7 //
       
     8 // Initial Contributors:
       
     9 // Nokia Corporation - initial contribution.
       
    10 //
       
    11 // Contributors:
       
    12 //
       
    13 // Description:
       
    14 //
       
    15 
       
    16 #include "srvres.h"
       
    17 #include "cachemgr.h"
       
    18 
       
    19 RFs TServerResources::iFs;
       
    20 
       
    21 HBufC* TServerResources::iRomDirectory;
       
    22 HBufC* TServerResources::iDataDirectory;
       
    23 HBufC* TServerResources::iInstallDirectory;
       
    24 HBufC* TServerResources::iBURDirectory;
       
    25 
       
    26 
       
    27 HBufC* TServerResources::iIniExt;
       
    28 HBufC* TServerResources::iCreExt;
       
    29 HBufC* TServerResources::iTrnsExt;
       
    30 
       
    31 TUint8 TServerResources::iPersistsVersion;
       
    32 
       
    33 RArray<TOwnerIdMapping> TServerResources::iOwnerIdLookUpTable;
       
    34 
       
    35 CRepositoryCacheManager* TServerResources::iCacheManager;
       
    36 TInt TServerResources::nPushed;
       
    37 #ifdef __CENTREP_SERVER_MEMTEST__
       
    38 TInt32 TServerResources::iMemTestData[KMemBufMaxEntry];
       
    39 TInt32 TServerResources::iMemTestDataCount = 0;
       
    40 #endif //__CENTREP_SERVER_MEMTEST__
       
    41 
       
    42 TTime TServerResources::CentrepFileTimeStampL(TUid aUid, TCentRepLocation aLocation)
       
    43 	{
       
    44 	TEntry entry;
       
    45 	HBufC* fileName(NULL);
       
    46 	TServerResources::CreateRepositoryFileNameLC(fileName,aUid,aLocation,ECre);
       
    47 	TInt err=TServerResources::iFs.Entry(fileName->Des(), entry);
       
    48    	CleanupStack::PopAndDestroy(fileName);
       
    49 
       
    50 	if(err==KErrNone)
       
    51 		{
       
    52 		return entry.iModified;
       
    53 		}
       
    54 		
       
    55 	TServerResources::CreateRepositoryFileNameLC(fileName,aUid,aLocation,EIni);
       
    56 	User::LeaveIfError(TServerResources::iFs.Entry(fileName->Des(), entry));
       
    57    	CleanupStack::PopAndDestroy(fileName);
       
    58    			   					  
       
    59 	return entry.iModified;
       
    60 	}
       
    61 	
       
    62 TBool TServerResources::CentrepFileExistsL(TUid aUid, TCentRepLocation aLocation, TCentRepFileType aType)
       
    63 	{
       
    64 	HBufC* fileName(NULL);
       
    65 	TServerResources::CreateRepositoryFileNameLC(fileName,aUid,aLocation,aType);
       
    66 	TEntry entry;
       
    67 	TInt err=TServerResources::iFs.Entry(fileName->Des(), entry);
       
    68    	CleanupStack::PopAndDestroy(fileName);		   					  
       
    69  	
       
    70  	TBool r=EFalse;
       
    71  	
       
    72  	if(err==KErrNone)
       
    73  		{
       
    74  		r=ETrue;
       
    75  		}
       
    76  	else if(err==KErrNotFound)
       
    77  		{
       
    78  		r=EFalse;
       
    79  		}
       
    80  	// Looking for a file on a composite file system may return KErrPathNotFound when 
       
    81  	// the ROM file doesn't exist, so check for this return code as well.
       
    82  	else if((aType==ERom) && (err== KErrPathNotFound))
       
    83   		{
       
    84  		r=EFalse;
       
    85  		}
       
    86  	else
       
    87  		{
       
    88  		User::Leave(err);
       
    89  		}
       
    90 	
       
    91   	return r;	 
       
    92 	}
       
    93 
       
    94 TBool TServerResources::CentrepFileExistsL(TUid aUid, TCentRepLocation aLocation)
       
    95 	{
       
    96 	return( CentrepFileExistsL( aUid, aLocation, EIni) || CentrepFileExistsL( aUid, aLocation, ECre));
       
    97 	}
       
    98 	
       
    99 TBool TServerResources::InstallFileExistsL(TUid aUid)
       
   100 	{
       
   101 	return CentrepFileExistsL(aUid, EInstall);
       
   102 	}
       
   103 
       
   104 TBool TServerResources::RomFileExistsL(TUid aUid)
       
   105 	{
       
   106 	return CentrepFileExistsL(aUid, ERom);
       
   107 	}
       
   108 	
       
   109 TBool TServerResources::PersistsFileExistsL(TUid aUid)
       
   110 	{
       
   111 	return CentrepFileExistsL(aUid, EPersists);
       
   112 	}
       
   113 		
       
   114 void TServerResources::DeletePersistsFileL(TUid aUid)
       
   115 	{
       
   116 	HBufC* iniFileName(NULL);
       
   117 	HBufC* creFileName(NULL);
       
   118 	TServerResources::CreateRepositoryFileNameLC(iniFileName,aUid,EPersists,EIni);
       
   119 	TServerResources::CreateRepositoryFileNameLC(creFileName,aUid,EPersists,ECre);
       
   120 	
       
   121 	if(CentrepFileExistsL( aUid, EPersists, EIni))	
       
   122 		{
       
   123 		User::LeaveIfError(TServerResources::iFs.Delete(iniFileName->Des()));
       
   124 		}
       
   125 	if(CentrepFileExistsL( aUid, EPersists, ECre))	
       
   126 		{
       
   127 		User::LeaveIfError(TServerResources::iFs.Delete(creFileName->Des()));
       
   128 		}
       
   129 
       
   130 	CleanupStack::PopAndDestroy(creFileName);	 
       
   131 	CleanupStack::PopAndDestroy(iniFileName);	 
       
   132 	}	
       
   133 		   					  
       
   134 
       
   135 /**
       
   136 Generic routine for creating a full repository file name.
       
   137 aFullFileName is created on the heap and it is caller responsibility
       
   138 to delete it. 
       
   139 */
       
   140 void TServerResources::CreateRepositoryFileNameLC(HBufC*& aFullFileName,
       
   141 												  TUid aUid,
       
   142                                                   TCentRepLocation aLocation,
       
   143                                                   TCentRepFileType aFileType)
       
   144     {
       
   145     const TInt KExtLen = 4;
       
   146     const TInt KDirLen = 40;
       
   147     const TInt KUidLen = 8;
       
   148 
       
   149     TBuf<KDirLen> directory;
       
   150     TBuf<KExtLen> ext;
       
   151 
       
   152 	//path
       
   153 	switch (aLocation)
       
   154 	    {
       
   155 	    case EPersists:
       
   156 	        {
       
   157 	        directory.Copy(iDataDirectory->Des());
       
   158 	        }
       
   159 	    	break;
       
   160 	    case EInstall:
       
   161 	        {
       
   162 	        directory.Copy(iInstallDirectory->Des());
       
   163 	        }
       
   164 	    	break;
       
   165 	    case ERom:
       
   166 	        {
       
   167 	        directory.Copy(iRomDirectory->Des());
       
   168 	        }
       
   169 	    	break;
       
   170 	    default:
       
   171 	        User::Leave(KErrNotFound); //should never get here
       
   172 	    }
       
   173 	    
       
   174     //file name	    
       
   175 	TBuf<KUidLen> name;
       
   176 	name.NumFixedWidth(aUid.iUid, EHex, KUidLen);
       
   177 
       
   178 	//extension
       
   179     switch (aFileType)
       
   180         {
       
   181         case ECre:
       
   182             {
       
   183             ext.Copy(iCreExt->Des());
       
   184             }
       
   185             break;
       
   186         case EIni:
       
   187             {
       
   188             ext.Copy(iIniExt->Des());
       
   189             }
       
   190             break;
       
   191         case ETmp:
       
   192 	    	{
       
   193 	    	ext.Copy(iTrnsExt->Des());
       
   194 	    	}
       
   195 	    	break;
       
   196         default:
       
   197 	        User::Leave(KErrNotFound); //should never get here
       
   198         }
       
   199 
       
   200     TBuf<KMaxFileName>  fullFileName;
       
   201     fullFileName.Append(directory);
       
   202     fullFileName.Append(name);
       
   203     fullFileName.Append(ext);
       
   204     //allocates memory on the heap. It is caller's resposibility to delete aFullFileName
       
   205     aFullFileName = fullFileName.AllocLC(); 
       
   206     }
       
   207 
       
   208 void TServerResources::InitialiseL()
       
   209 	{
       
   210 	iPersistsVersion = KPersistFormatVersion;							// Version 0 of persists
       
   211 	User::LeaveIfError(iFs.Connect());
       
   212 	CleanupClosePushL(iFs);
       
   213 	nPushed = 1;
       
   214 
       
   215 	_LIT(KWriteableDrive, "c:"); 	// This will probably always be c:
       
   216 	_LIT(KRomDrive, "z:");			// This may not always be z:
       
   217 	_LIT(KPersistsDir, "persists\\");
       
   218 	_LIT(KBURDir, "bur\\");
       
   219 	
       
   220 	// File extensions
       
   221 	
       
   222 	_LIT(KIniFileExtension, ".txt");
       
   223 	_LIT(KExternalizedPersistsFileExt, ".cre");
       
   224 	_LIT(KTransactFileExt, ".tmp");
       
   225 
       
   226 	const TInt KMaxExtLength=4;
       
   227 	
       
   228 	iIniExt=HBufC::NewLC(KMaxExtLength);
       
   229 	++nPushed;
       
   230 	iCreExt=HBufC::NewLC(KMaxExtLength);
       
   231 	++nPushed;
       
   232 	iTrnsExt=HBufC::NewLC(KMaxExtLength);
       
   233 	++nPushed;
       
   234 
       
   235 	iIniExt->Des().Copy(KIniFileExtension);
       
   236 	iCreExt->Des().Copy(KExternalizedPersistsFileExt);
       
   237 	iTrnsExt->Des().Copy(KTransactFileExt);
       
   238 	
       
   239 	TBuf<KMaxFileName> path;
       
   240 	User::LeaveIfError(iFs.PrivatePath(path));
       
   241 
       
   242 	const TInt pathLen = path.Length();
       
   243 	
       
   244 	//
       
   245 	// ROM-drive cenrep directory
       
   246 	//
       
   247 	
       
   248 	iRomDirectory = HBufC::NewLC(KRomDrive().Length()+pathLen);
       
   249 	++nPushed;
       
   250 	TPtr ptr(iRomDirectory->Des());
       
   251 	ptr.Append(KRomDrive);
       
   252 	ptr.Append(path);
       
   253 	// If the ROM directory does not exist (very unlikely) we set iRomDirectory to zero.
       
   254 	TEntry fsEntry;
       
   255 	if(iFs.Entry(*iRomDirectory, fsEntry)!=KErrNone || !fsEntry.IsDir())
       
   256 		{
       
   257 		CleanupStack::PopAndDestroy(iRomDirectory);
       
   258 		--nPushed;
       
   259 		iRomDirectory = 0;
       
   260 		}
       
   261 
       
   262 	//
       
   263 	// Cenrep install directory
       
   264 	//
       
   265 
       
   266 	iInstallDirectory = HBufC::NewLC(KWriteableDrive().Length()+pathLen);
       
   267 	++nPushed;
       
   268 	ptr.Set(iInstallDirectory->Des());
       
   269 	ptr.Append(KWriteableDrive);
       
   270 	ptr.Append(path);
       
   271 	TInt r = iFs.MkDirAll(*iInstallDirectory);
       
   272 	if(r!=KErrNone && r!=KErrAlreadyExists)
       
   273 		{
       
   274 		User::Leave(r);		
       
   275 		}
       
   276 	//
       
   277 	// Writeable-drive data directory
       
   278 	//
       
   279 
       
   280 	iDataDirectory = HBufC::NewLC(KWriteableDrive().Length()+pathLen+KPersistsDir().Length());
       
   281 	++nPushed;
       
   282 	
       
   283 	ptr.Set(iDataDirectory->Des());
       
   284 	ptr.Append(KWriteableDrive);
       
   285 	ptr.Append(path);
       
   286 	ptr.Append(KPersistsDir);
       
   287 	
       
   288 	r = iFs.MkDirAll(*iDataDirectory);
       
   289 	if(r!=KErrNone && r!=KErrAlreadyExists)
       
   290 		User::Leave(r);
       
   291 
       
   292 	//
       
   293 	// Writeable-drive backup/restore directory
       
   294 	//
       
   295 	iBURDirectory = HBufC::NewLC(KWriteableDrive().Length()+pathLen+KBURDir().Length());
       
   296 	++nPushed;
       
   297 	
       
   298 	ptr.Set(iBURDirectory->Des());
       
   299 	ptr.Append(KWriteableDrive);
       
   300 	ptr.Append(path);
       
   301 	ptr.Append(KBURDir);
       
   302 	
       
   303 	r = iFs.MkDirAll(*iBURDirectory);
       
   304 	if(r!=KErrNone && r!=KErrAlreadyExists)
       
   305 		User::Leave(r);
       
   306 	
       
   307 	//
       
   308 	// Cache Manager 
       
   309 	//
       
   310 	iCacheManager = CRepositoryCacheManager::NewLC();
       
   311 	++nPushed;
       
   312 
       
   313 	CleanupStack::Pop(nPushed); // i*Ext, iInstallDirectory, iDataDirectory, (iRomDirectory?), iFs, iCacheManager
       
   314 	
       
   315 	iOwnerIdLookUpTable.Reset();
       
   316 	}
       
   317 
       
   318 void TServerResources::Close()
       
   319 	{
       
   320 	delete iCacheManager;
       
   321 	delete iInstallDirectory;
       
   322 	delete iDataDirectory;
       
   323 	delete iRomDirectory;
       
   324 	delete iBURDirectory;
       
   325 	delete iIniExt;
       
   326 	delete iCreExt;
       
   327 	delete iTrnsExt;
       
   328 	iFs.Close();
       
   329 
       
   330 	iCacheManager=NULL;
       
   331 	iInstallDirectory=NULL;
       
   332 	iDataDirectory=NULL;
       
   333 	iRomDirectory=NULL;
       
   334 	iBURDirectory=NULL;	
       
   335 	iIniExt=NULL;	
       
   336 	iCreExt=NULL;	
       
   337 	iTrnsExt=NULL;	
       
   338 	
       
   339 	iOwnerIdLookUpTable.Close() ;
       
   340 	}
       
   341 	
       
   342 TBool TServerResources::PersistFileExistsL(TUid aUid)
       
   343 	{
       
   344 	CDir* persistDir;
       
   345 	TPtr dataDirectory = TServerResources::iDataDirectory->Des();
       
   346 	User::LeaveIfError(TServerResources::iFs.GetDir(dataDirectory,
       
   347 													KEntryAttNormal,
       
   348 													ESortNone,
       
   349 													persistDir));
       
   350 
       
   351 	CleanupStack::PushL(persistDir);
       
   352 
       
   353 	const TInt fileCount = persistDir->Count();
       
   354 	TBool found = EFalse;
       
   355 
       
   356 	// Open each repositories in the persist directory
       
   357 	for(TInt i = 0; i < fileCount; ++i)
       
   358 		{
       
   359 		TUid uid;
       
   360 		if (!GetUid(const_cast<TEntry&>((*persistDir)[i]), uid))
       
   361 			{
       
   362 			if (uid == aUid)
       
   363 				{
       
   364 				found = ETrue;
       
   365 				break;
       
   366 				}
       
   367 			}
       
   368 		}
       
   369 
       
   370 	CleanupStack::PopAndDestroy(persistDir);
       
   371 
       
   372 	return found;
       
   373 	}
       
   374 
       
   375 TInt TServerResources::GetUid(TEntry& aEntry, TUid& aUid)
       
   376 	{
       
   377 		const TInt KUidLen = 8;
       
   378 		TPtrC uidPtr = aEntry.iName.Des().LeftTPtr(KUidLen);
       
   379 		TLex  lex=uidPtr;
       
   380 		TUint32 uidValue;
       
   381 
       
   382 		if (lex.Val(uidValue, EHex) == KErrNone)
       
   383 			{
       
   384 			aUid = TUid::Uid(uidValue);
       
   385 			}
       
   386 		else
       
   387 			{
       
   388 			return KErrNotFound;
       
   389 			}
       
   390 			
       
   391 	return KErrNone;
       
   392 	}
       
   393 
       
   394 TInt TOwnerIdMapping::CompareUids (const TOwnerIdMapping& aOwnerIdMapping1, const TOwnerIdMapping& aOwnerIdMapping2) 
       
   395 	{
       
   396 	if (aOwnerIdMapping1.iRepUid < aOwnerIdMapping2.iRepUid)
       
   397 		return -1 ;
       
   398 	else if (aOwnerIdMapping1.iRepUid > aOwnerIdMapping2.iRepUid)
       
   399 		return 1 ;
       
   400 	else
       
   401 		return 0 ;
       
   402 	}
       
   403 		
       
   404 
       
   405 #ifdef __CENTREP_SERVER_MEMTEST__
       
   406 
       
   407 void TServerResources::StopRecordTimerResult()
       
   408 	{
       
   409 	iMemTestDataCount = KMemBufMaxEntry;
       
   410 	}
       
   411 
       
   412 //aLocation: location where the memory reading is done (a method specifier)
       
   413 //aIdentifier: identifier of the memory reading (e.g. repository id, 10th reading etc)
       
   414 void TServerResources::RecordTimerResult(TMemTestLocationIdentifier aLocation, TInt32 aIdentifier)
       
   415 	{
       
   416 	if(iMemTestDataCount+6 > KMemBufMaxEntry)
       
   417 		{
       
   418 		if(iMemTestDataCount+3 <= KMemBufMaxEntry)
       
   419 			{
       
   420 			iMemTestData[iMemTestDataCount++] = static_cast<TInt32>(KMagicMemTestOutOfBounds);
       
   421 			iMemTestData[iMemTestDataCount++] = static_cast<TInt32>(KMagicMemTestOutOfBounds);
       
   422 			iMemTestData[iMemTestDataCount++] = static_cast<TInt32>(KMagicMemTestOutOfBounds);
       
   423 			}
       
   424 		}
       
   425 	else
       
   426 		{
       
   427 		RHeap& heap = User::Heap();
       
   428 		TInt biggestBlock;
       
   429 		
       
   430 		iMemTestData[iMemTestDataCount++] = aLocation;
       
   431 		iMemTestData[iMemTestDataCount++] = aIdentifier;
       
   432 		iMemTestData[iMemTestDataCount++] = static_cast<TInt32>(heap.Size() - heap.Available(biggestBlock));
       
   433 		}
       
   434 	}
       
   435 #endif //__CENTREP_SERVER_MEMTEST__