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