policymanagement/policyengine/centreptoolserver/src/RepositorySession.cpp
changeset 0 b497e44ab2fc
child 21 504e41245867
equal deleted inserted replaced
-1:000000000000 0:b497e44ab2fc
       
     1 /*
       
     2 * Copyright (c) 2002-2004 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 "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: Implementation of policymanagement components
       
    15 *
       
    16 */
       
    17 
       
    18 
       
    19 // INCLUDE FILES
       
    20 
       
    21 #include "RepositorySession.h"
       
    22 #include "IniFileHelper.h"
       
    23 #include "constants.h"
       
    24 #include "debug.h"
       
    25 #include "PolicyEnginePrivateCRKeys.h"
       
    26 
       
    27 #include <centralrepository.h>
       
    28 #include <s32file.h>
       
    29 
       
    30 const TUid KCentralRepositoryUID     = { 0x10202BE9 };
       
    31 
       
    32 
       
    33 // -----------------------------------------------------------------------------
       
    34 // CRepositoryContent::CRepositoryContent()
       
    35 // -----------------------------------------------------------------------------
       
    36 //
       
    37 
       
    38 CRepositoryContent::CRepositoryContent( const TUid& aUid, HBufC* aContent)
       
    39 	: iContentPtr( *aContent), iContent( aContent), iUid( aUid)
       
    40 {
       
    41 }
       
    42 
       
    43 // -----------------------------------------------------------------------------
       
    44 // CRepositoryContent::CRepositoryContent()
       
    45 // -----------------------------------------------------------------------------
       
    46 //
       
    47 CRepositoryContent::CRepositoryContent( const TUid& aUid)
       
    48 	: iUid( aUid)
       
    49 {
       
    50 }
       
    51 
       
    52 
       
    53 // -----------------------------------------------------------------------------
       
    54 // CRepositoryContent::~CRepositoryContent()
       
    55 // -----------------------------------------------------------------------------
       
    56 //
       
    57 
       
    58 CRepositoryContent::~CRepositoryContent()
       
    59 {
       
    60 	ReleaseResources();	
       
    61 	
       
    62 	iRangeSettings.Close();
       
    63 	iIndividualSettings.Close();	
       
    64 	iRangeMetas.Close();
       
    65 }
       
    66 
       
    67 // -----------------------------------------------------------------------------
       
    68 // CRepositoryContent::NewL()
       
    69 // -----------------------------------------------------------------------------
       
    70 //
       
    71 CRepositoryContent * CRepositoryContent::NewL( const TUid& aUid)
       
    72 {
       
    73 	CRepositoryContent *self = new (ELeave) CRepositoryContent( aUid);
       
    74 	CleanupStack::PushL( self);	
       
    75 	
       
    76 	self->ConstructL();
       
    77 	
       
    78 	CleanupStack::Pop( self);
       
    79 	return self;
       
    80 }
       
    81 
       
    82 // -----------------------------------------------------------------------------
       
    83 // CRepositoryContent::NewL()
       
    84 // -----------------------------------------------------------------------------
       
    85 //
       
    86 CRepositoryContent * CRepositoryContent::NewL( const TUid& aUid, HBufC* aContent)
       
    87 {
       
    88 	CRepositoryContent *self = new (ELeave) CRepositoryContent( aUid, aContent);
       
    89 	CleanupStack::PushL( self);	
       
    90 	
       
    91 	self->ConstructL();
       
    92 	
       
    93 	CleanupStack::Pop( self);
       
    94 	return self;
       
    95 }
       
    96 
       
    97 // -----------------------------------------------------------------------------
       
    98 // CRepositoryContent::ConstructL()
       
    99 // -----------------------------------------------------------------------------
       
   100 //
       
   101 void CRepositoryContent::ConstructL()
       
   102 {
       
   103 	RDEBUG("CentRepTool: New CRepostioryContent");
       
   104 	iDefaultSetting = new (ELeave) CDefaultSetting();
       
   105 }
       
   106 
       
   107 
       
   108 // -----------------------------------------------------------------------------
       
   109 // CRepositoryContent::ReleaseResources()
       
   110 // -----------------------------------------------------------------------------
       
   111 //
       
   112 
       
   113 void CRepositoryContent::ReleaseResources()
       
   114 {
       
   115 	RDEBUG("CentRepTool: Release CRepository content resources");
       
   116 
       
   117 	delete iContent;
       
   118 	iContent = 0;
       
   119 	
       
   120 	delete iDefaultSetting;
       
   121 	iDefaultSetting = 0;
       
   122 
       
   123 	iRangeSettings.ResetAndDestroy();
       
   124 	iIndividualSettings.ResetAndDestroy();
       
   125 	iRangeMetas.ResetAndDestroy();
       
   126 	
       
   127 
       
   128 }
       
   129 
       
   130 // -----------------------------------------------------------------------------
       
   131 // CRepositoryContent::ReadHeaderL()
       
   132 // -----------------------------------------------------------------------------
       
   133 //
       
   134 
       
   135 void CRepositoryContent::ReadHeaderL()
       
   136 {
       
   137 	RDEBUG("CentRepTool: Read ini-file header(.txt format)");
       
   138 
       
   139 
       
   140 	using namespace IniConstants;
       
   141 
       
   142 	TInt index = iContentPtr.FindF( KPlatSecSection);
       
   143 	User::LeaveIfError( index);
       
   144 	
       
   145 	//separate each section to own ptr's	
       
   146 	iHeaderPtr.Set( iContentPtr.Left( index + KPlatSecStringLength));			//content before [platsec]
       
   147 	iContentPtr.Set( iContentPtr.Mid( index + KPlatSecStringLength));		//content after [platsec] (platsec and settings)
       
   148 	
       
   149 	iDefaultMetaPtr.Set( KNullDesC);
       
   150 	iOwnerPtr.Set( KNullDesC);
       
   151 	
       
   152 	//locate owner section and insulate it own ptr
       
   153 	index = iHeaderPtr.FindF( KOwnerSection);
       
   154 	TPtrC * lastPart = 0;
       
   155 	
       
   156 	if ( index >= 0)
       
   157 	{
       
   158 		iOwnerPtr.Set( iHeaderPtr.Mid( index + KOwnerStringLength));					//content after [owner]
       
   159 		//pointer to the last section before [platsec] section
       
   160 		lastPart = &iOwnerPtr;
       
   161 	}
       
   162 	else
       
   163 	{
       
   164 		//pointer to the last section before [platsec] section
       
   165 		lastPart = &iHeaderPtr;
       
   166 	}
       
   167 
       
   168 	//locate default meta section
       
   169 	index = lastPart->FindF( KDefaultMetaSection);
       
   170 	
       
   171 	
       
   172 	if ( index >= 0)
       
   173 	{
       
   174 		iDefaultMetaPtr.Set( lastPart->Mid( index + KDefaultMetaStringLength));			//content after [defaultmeta]
       
   175 		lastPart->Set( lastPart->Left( index));
       
   176 		lastPart = &iDefaultMetaPtr;
       
   177 	}
       
   178 
       
   179 	//set last section length...
       
   180 	index = lastPart->FindF( KPlatSecSection);
       
   181 	User::LeaveIfError( index);
       
   182 	
       
   183 	lastPart->Set( lastPart->Left( index));
       
   184 }
       
   185 
       
   186 
       
   187 // -----------------------------------------------------------------------------
       
   188 // CRepositoryContent::ReadDefaultMetaL()
       
   189 // -----------------------------------------------------------------------------
       
   190 //
       
   191 void CRepositoryContent::ReadOptionalDataL()
       
   192 {
       
   193 	RDEBUG("CentRepTool: Read ini-file optional data(.txt format)");
       
   194 
       
   195 	TPtrC owner = iOwnerPtr;
       
   196 		
       
   197 	//read next line from owner section
       
   198 	TPtrC line;
       
   199 	TIniFileHelper::NextLine( owner, line);		
       
   200 	
       
   201 	//read owner secure id from line
       
   202 	if ( line.Length())
       
   203 	{
       
   204 		TLex lex( line);
       
   205 		TInt32 value;
       
   206 	 	User::LeaveIfError( TIniFileHelper::ReadNumber( value, lex));
       
   207 	 	iOwner.iUid = value;
       
   208 	}
       
   209 
       
   210 	//read 
       
   211 	TPtrC defaultMeta = iDefaultMetaPtr;
       
   212 	
       
   213 	do
       
   214 	{
       
   215 		line.Set( KNullDesC);
       
   216 		TIniFileHelper::NextLine( defaultMeta, line);		
       
   217 	
       
   218 		//try to create RangeMeta data 
       
   219 		TLex lex( line);
       
   220 		CRangeMeta * rangeMeta = CRangeMeta::NewL( lex);
       
   221 		
       
   222 		//if the line content is not a range meta data check is the content is it default meta data 
       
   223 		if ( rangeMeta)
       
   224 		{
       
   225 			iRangeMetas.AppendL( rangeMeta);
       
   226 		}
       
   227 		else
       
   228 		{
       
   229 			if ( !lex.Eos())
       
   230 			{
       
   231 				User::LeaveIfError( TIniFileHelper::ReadUNumber( iDefaultMeta, lex));
       
   232 				User::LeaveIfError( !lex.Eos());			
       
   233 			}
       
   234 		}
       
   235 		
       
   236 	} while ( defaultMeta.Length());
       
   237 	
       
   238 }
       
   239 
       
   240 
       
   241 // -----------------------------------------------------------------------------
       
   242 // CRepositoryContent::ReadPlatSecL()
       
   243 // -----------------------------------------------------------------------------
       
   244 //
       
   245 
       
   246 void CRepositoryContent::ReadPlatSecL()
       
   247 {
       
   248 	RDEBUG("CentRepTool: Read ini-file platsec-section(.txt format)");
       
   249 
       
   250 	using namespace IniConstants;
       
   251 
       
   252 	TInt index = iContentPtr.FindF( KMainSection );
       
   253 	User::LeaveIfError( index);
       
   254 
       
   255 	TPtrC platSecPtr = iContentPtr.Left( index);
       
   256 
       
   257 	do
       
   258 	{
       
   259 		TPtrC sectionPtr = platSecPtr;
       
   260 		
       
   261 		TIniFileHelper::NextLine( platSecPtr, sectionPtr);
       
   262 		
       
   263 		if ( !sectionPtr.Length())
       
   264 		{
       
   265 			break;
       
   266 		}
       
   267 		
       
   268 		TLex lex( sectionPtr);
       
   269 		CRangeSetting* rangeSetting = CRangeSetting::NewL( lex);
       
   270 		
       
   271 		if ( rangeSetting )
       
   272 		{
       
   273 			iRangeSettings.AppendL( rangeSetting);
       
   274 		}
       
   275 		else 
       
   276 		{
       
   277 			CDefaultSetting* defaultSetting = CDefaultSetting::NewL( lex);
       
   278 				
       
   279 			if ( defaultSetting  )
       
   280 			{
       
   281 				if ( iDefaultSetting && iDefaultSetting->ContainsSecuritySettings())
       
   282 				{
       
   283 					//there can be only one default settings in the repository -> Corrupted
       
   284 					User::Leave( KErrCorrupt);
       
   285 				}
       
   286 				else
       
   287 				{
       
   288 					delete iDefaultSetting;
       
   289 					iDefaultSetting = defaultSetting;	
       
   290 				}
       
   291 			}
       
   292 			else
       
   293 			{
       
   294 				//line contains invalid format -> Corrupted
       
   295 				User::Leave( KErrCorrupt);
       
   296 			}
       
   297 		}
       
   298 
       
   299 		
       
   300 	} while ( platSecPtr.Length());
       
   301 	
       
   302 	iContentPtr.Set( iContentPtr.Mid( index + IniConstants::KMainSectionStringLength));
       
   303 }
       
   304 // -----------------------------------------------------------------------------
       
   305 // CRepositoryContent::FindIndividualSetting()
       
   306 // -----------------------------------------------------------------------------
       
   307 //
       
   308 CIndividualSetting* CRepositoryContent::FindIndividualSetting( const TUint32 aSettingId)
       
   309 {
       
   310 	//Find individual setting which has same id and return it...
       
   311 	for ( TInt i(0); i < iIndividualSettings.Count(); i++)
       
   312 	{
       
   313 		if ( iIndividualSettings[i]->iSettingId == aSettingId)
       
   314 		{
       
   315 			return iIndividualSettings[i];
       
   316 		}
       
   317 	}
       
   318 	
       
   319 	return 0;
       
   320 }
       
   321 
       
   322 // -----------------------------------------------------------------------------
       
   323 // CRepositoryContent::WriteFileL()
       
   324 // -----------------------------------------------------------------------------
       
   325 //
       
   326 
       
   327 void CRepositoryContent::WriteFileL( RFile& aFile)
       
   328 {
       
   329 	TBuf<600> buffer;
       
   330 
       
   331 	TIniFileHelper ini;
       
   332 
       
   333 	ini.StartWrite( aFile);
       
   334 	
       
   335 	ini.WriteToFile( iHeaderPtr);
       
   336 	ini.LineFeed();
       
   337 	
       
   338 	if ( iDefaultSetting)
       
   339 	{
       
   340 		ini.WriteToFile( iDefaultSetting->SecurityString( buffer));
       
   341 		ini.LineFeed();
       
   342 	}
       
   343 
       
   344 	for ( TInt i(0); i < iRangeSettings.Count(); i++)
       
   345 	{
       
   346 		ini.WriteToFile( iRangeSettings[i]->SettingDefinition( buffer));
       
   347 		ini.WriteToFile( iRangeSettings[i]->SecurityString( buffer));
       
   348 		ini.LineFeed();
       
   349 	}
       
   350 	
       
   351 	ini.WriteToFile( IniConstants::KMainSection);
       
   352 	ini.LineFeed();
       
   353 	
       
   354 	for ( TInt i(0); i < iIndividualSettings.Count(); i++)
       
   355 	{
       
   356 		ini.WriteToFile( iIndividualSettings[i]->SettingDefinition( buffer));
       
   357 		ini.WriteToFile( iIndividualSettings[i]->SecurityString( buffer));
       
   358 		ini.LineFeed();
       
   359 	}
       
   360 	
       
   361 	ini.FinishWrite();
       
   362 }
       
   363 
       
   364 
       
   365 // -----------------------------------------------------------------------------
       
   366 // CRepositoryContent::CheckRangeValidity()
       
   367 // -----------------------------------------------------------------------------
       
   368 //
       
   369 
       
   370 TBool CRepositoryContent::CheckRangeValidity()
       
   371 {
       
   372 	RDEBUG("CentRepTool: Check range setting validity");
       
   373 
       
   374 	//evaluate range setting validity (range setting start value must be greater than previous setting end value)
       
   375 	TLinearOrder<CRangeSetting> linearOrder( &TIniFileHelper::CompareElements);
       
   376 	iRangeSettings.Sort( linearOrder);
       
   377 	
       
   378 	TBool rangesValid = ETrue;
       
   379 	
       
   380 	TInt rangeCount = iRangeSettings.Count();
       
   381 	for ( TInt i(0); i < rangeCount; i++)
       
   382 	{
       
   383 		if ( i + 1 < rangeCount)
       
   384 		{
       
   385 			if ( iRangeSettings[i]->iEnd >= iRangeSettings[i+1]->iStart)
       
   386 			{
       
   387 				rangesValid = EFalse;
       
   388 				break;
       
   389 			}
       
   390 		}
       
   391 	}	
       
   392 	
       
   393 	return rangesValid;
       
   394 }
       
   395 
       
   396 // -----------------------------------------------------------------------------
       
   397 // CRepositoryContent::CreateMaskBackupL()
       
   398 // -----------------------------------------------------------------------------
       
   399 //
       
   400 void CRepositoryContent::CreateMaskBackupL( RPointerArray<CRangeMeta>& aSettings, TUint32 aCompareValue, TUint32 aMask)
       
   401 {
       
   402 	RDEBUG_3("CentRepTool: Turn backup flag on for mask ( %d mask:%d)", aCompareValue, aMask);
       
   403 
       
   404 	TInt rangeCount = iRangeMetas.Count();
       
   405 
       
   406 	//search existing settings
       
   407 	for ( TInt i(0); i < rangeCount; i++)
       
   408 	{
       
   409 		CRangeMeta * setting = iRangeMetas[i];
       
   410 		
       
   411 		if ( setting->Type() != EMaskSetting )
       
   412 		{
       
   413 			continue;
       
   414 		}		
       
   415 		
       
   416 		if ( setting->iStart == aCompareValue && setting->iMask == aMask)
       
   417 		{
       
   418 			aSettings.AppendL( setting);	
       
   419 		}
       
   420 	}		
       
   421 	
       
   422 	//create new one, if setting doesn't exist
       
   423 	if ( !aSettings.Count() )
       
   424 	{	
       
   425 		CRangeMeta * setting = CRangeMeta::NewL( aCompareValue, 0, aMask, KBackupBitMask);
       
   426 		aSettings.Append( setting );
       
   427 		iRangeMetas.Append( setting);
       
   428 	}
       
   429 }
       
   430 
       
   431 
       
   432 // -----------------------------------------------------------------------------
       
   433 // CRepositoryContent::CreateRangeBackupL()
       
   434 // -----------------------------------------------------------------------------
       
   435 //
       
   436 
       
   437 void CRepositoryContent::CreateRangeBackupL( RPointerArray<CRangeMeta>& aSettings, TUint32 aRangeStart, TUint32 aRangeEnd)
       
   438 {
       
   439 	RDEBUG_3("CentRepTool: Create new range setting (%d -> %d)", aRangeStart, aRangeEnd);
       
   440 
       
   441 	//evaluate range setting validity (range setting start value must be greater than previous setting end value)
       
   442 	//evaluate range setting validity (range setting start value must be greater than previous setting end value)
       
   443 	TLinearOrder<CRangeMeta> linearOrder( &TIniFileHelper::CompareElements);
       
   444 	iRangeMetas.Sort( linearOrder);
       
   445 	
       
   446 
       
   447 	TInt rangeCount = iRangeMetas.Count();
       
   448 	for ( TInt i(0); i < rangeCount; i++)
       
   449 	{
       
   450 		if ( i + 1 < rangeCount)
       
   451 		{
       
   452 			if ( iRangeMetas[i]->iEnd >= iRangeMetas[i+1]->iStart)
       
   453 			{
       
   454 				User::Leave( KErrCorrupt);
       
   455 			}
       
   456 		}
       
   457 	}	
       
   458 	
       
   459 	//search existing settings
       
   460 	for ( TInt i(0); i < rangeCount; i++)
       
   461 	{
       
   462 		CRangeMeta * setting = iRangeMetas[i];
       
   463 		
       
   464 		if ( setting->Type() != ERangeSetting )
       
   465 		{
       
   466 			continue;
       
   467 		}		
       
   468 		
       
   469 		if ( setting->iStart == aRangeStart && setting->iEnd == aRangeEnd)
       
   470 		{
       
   471 			aSettings.AppendL( setting);	
       
   472 		}
       
   473 	}
       
   474 	
       
   475 	//create new one, if setting doesn't exist
       
   476 	if ( !aSettings.Count() )
       
   477 	{	
       
   478 		CRangeMeta * setting = CRangeMeta::NewL( aRangeStart, aRangeEnd, 0, KBackupBitMask);
       
   479 		aSettings.Append( setting );
       
   480 		iRangeMetas.Append( setting);
       
   481 	}	
       
   482 	
       
   483 	iRangeMetas.Sort( linearOrder);
       
   484 
       
   485 }
       
   486 
       
   487 
       
   488 
       
   489 // -----------------------------------------------------------------------------
       
   490 // CRepositoryContent::FindRangeSetting()
       
   491 // -----------------------------------------------------------------------------
       
   492 //
       
   493 
       
   494 void CRepositoryContent::CreateRangeSettingsL( RPointerArray<CRangeSetting>& aSettings, TUint32 aRangeStart, TUint32 aRangeEnd)
       
   495 {
       
   496 	RDEBUG_3("CentRepTool: Create new range setting (%d -> %d)", aRangeStart, aRangeEnd);
       
   497 
       
   498 	//evaluate range setting validity (range setting start value must be greater than previous setting end value)
       
   499 	//evaluate range setting validity (range setting start value must be greater than previous setting end value)
       
   500 	TLinearOrder<CRangeSetting> linearOrder( &TIniFileHelper::CompareElements);
       
   501 	iRangeSettings.Sort( linearOrder);
       
   502 	
       
   503 
       
   504 	TInt rangeCount = iRangeSettings.Count();
       
   505 	for ( TInt i(0); i < rangeCount; i++)
       
   506 	{
       
   507 		if ( i + 1 < rangeCount)
       
   508 		{
       
   509 			if ( iRangeSettings[i]->iEnd >= iRangeSettings[i+1]->iStart)
       
   510 			{
       
   511 				User::Leave( KErrCorrupt);
       
   512 			}
       
   513 		}
       
   514 	}	
       
   515 
       
   516 	//search existing settings
       
   517 	for ( TInt i(0); i < rangeCount; i++)
       
   518 	{
       
   519 		CRangeSetting * setting = iRangeSettings[i];
       
   520 		
       
   521 		if ( setting->Type() != ERangeSetting )
       
   522 		{
       
   523 			continue;
       
   524 		}		
       
   525 		
       
   526 		if ( setting->iStart == aRangeStart && setting->iEnd == aRangeEnd)
       
   527 		{
       
   528 			aSettings.AppendL( setting);	
       
   529 		}
       
   530 	}
       
   531 	
       
   532 	//create new one, if setting doesn't exist
       
   533 	if ( !aSettings.Count() )
       
   534 	{	
       
   535 		CRangeSetting * setting = CRangeSetting::NewL( aRangeStart, aRangeEnd, 0);
       
   536 		aSettings.Append( setting );
       
   537 		iRangeSettings.Append( setting);
       
   538 	}	
       
   539 	
       
   540 	iRangeSettings.Sort( linearOrder);
       
   541 
       
   542 }
       
   543 
       
   544 
       
   545 // -----------------------------------------------------------------------------
       
   546 // CRepositoryContent::CreateMaskSettingsL()
       
   547 // -----------------------------------------------------------------------------
       
   548 //
       
   549 void CRepositoryContent::CreateMaskSettingsL( RPointerArray<CRangeSetting>& aSettings, TUint32 aCompareValue, TUint32 aMask)
       
   550 {
       
   551 	RDEBUG_3("CentRepTool: Create new mask setting ( %d mask:%d)", aCompareValue, aMask);
       
   552 
       
   553 	TInt rangeCount = iRangeSettings.Count();
       
   554 
       
   555 	//search existing settings
       
   556 	for ( TInt i(0); i < rangeCount; i++)
       
   557 	{
       
   558 		CRangeSetting * setting = iRangeSettings[i];
       
   559 		
       
   560 		if ( setting->Type() != EMaskSetting )
       
   561 		{
       
   562 			continue;
       
   563 		}		
       
   564 		
       
   565 		if ( setting->iStart == aCompareValue && setting->iMask == aMask)
       
   566 		{
       
   567 			aSettings.AppendL( setting);	
       
   568 		}
       
   569 	}		
       
   570 	
       
   571 	//create new one, if setting doesn't exist
       
   572 	if ( !aSettings.Count() )
       
   573 	{	
       
   574 		CRangeSetting * setting = CRangeSetting::NewL( aCompareValue, 0, aMask);
       
   575 		aSettings.Append( setting );
       
   576 		iRangeSettings.Append( setting);
       
   577 	}
       
   578 	
       
   579 	
       
   580 }
       
   581 
       
   582 
       
   583 // -----------------------------------------------------------------------------
       
   584 // CRepositoryContent::ReadMainL()
       
   585 // Read main secttion (individual settings) from CentRep ini.
       
   586 // -----------------------------------------------------------------------------
       
   587 //
       
   588 
       
   589 void CRepositoryContent::ReadMainL()
       
   590 {
       
   591 	do
       
   592 	{
       
   593 		//get next setting from inifile
       
   594 		TPtrC settingPtr = iContentPtr;
       
   595 		TIniFileHelper::NextLine( iContentPtr, settingPtr);
       
   596 		
       
   597 		//Create new CIndividual setting
       
   598 		TLex lex( settingPtr);
       
   599 		
       
   600 		if ( lex.Eos())
       
   601 		{
       
   602 			break;
       
   603 		}
       
   604 
       
   605 		CIndividualSetting * setting = CIndividualSetting::NewL( lex);		
       
   606 		
       
   607 		if ( setting )
       
   608 		{
       
   609 			//add settting to settings list
       
   610 			AddIndividualSettingL( setting);
       
   611 		}
       
   612 		else
       
   613 		{	
       
   614 			//file is corrupted if setting is not valid
       
   615 			User::Leave( KErrCorrupt);
       
   616 		}
       
   617 
       
   618 		//repeat until contetn left
       
   619 	} while ( iContentPtr.Length());
       
   620 }
       
   621 
       
   622 
       
   623 // -----------------------------------------------------------------------------
       
   624 // CRepositoryContent::FindSettingOrCreateL()
       
   625 // -----------------------------------------------------------------------------
       
   626 //
       
   627 CIndividualSetting* CRepositoryContent::FindSettingOrCreateL( const TUint32& aSettingId)
       
   628 {
       
   629 	//try to find element with same id
       
   630 	TLinearOrder<CIndividualSetting> linearOrder( &CIndividualSetting::CompareElements);
       
   631 	CIndividualSetting* referenceElement = new (ELeave) CIndividualSetting( aSettingId);
       
   632 	
       
   633 	TInt index = iIndividualSettings.FindInOrder( referenceElement, linearOrder);
       
   634 	
       
   635 	CIndividualSetting * element = NULL;
       
   636 	if ( index >= 0)
       
   637 	{
       
   638 		//if element exists return it and delete reference element...
       
   639 		element = iIndividualSettings[index];
       
   640 		delete referenceElement;
       
   641 	}
       
   642 	else
       
   643 	{	
       
   644 		//...or if not use reference elemetn and add it to setting list...
       
   645 		element = referenceElement;
       
   646 		AddIndividualSettingL( element);
       
   647 	}
       
   648 	
       
   649 	//...return element
       
   650 	return element;
       
   651 	
       
   652 }
       
   653 
       
   654 // -----------------------------------------------------------------------------
       
   655 // CRepositoryContent::AddElementL()
       
   656 // -----------------------------------------------------------------------------
       
   657 //
       
   658 void CRepositoryContent::AddIndividualSettingL( CIndividualSetting* aSetting)
       
   659 {
       
   660 	if ( aSetting->ContainsSecuritySettings())
       
   661 	{
       
   662 		iSingleSecuritySettingCount++;	
       
   663 	}
       
   664 	
       
   665 	TLinearOrder<CIndividualSetting> linearOrder( &CIndividualSetting::CompareElements);
       
   666 	iIndividualSettings.InsertInOrder( aSetting, linearOrder);
       
   667 }
       
   668 
       
   669 
       
   670 // -----------------------------------------------------------------------------
       
   671 // CRepositoryContent::CheckAccess()
       
   672 // -----------------------------------------------------------------------------
       
   673 //
       
   674 TBool CRepositoryContent::CheckAccess( const RMessage2& aMessage, TUint32 aSettingId, TAccessType aAccessType)
       
   675 {
       
   676 	TBool retVal( ETrue);
       
   677 
       
   678 	for ( TInt i(0); i < iRangeSettings.Count(); i++)
       
   679 	{
       
   680 		CRangeSetting* setting = iRangeSettings[ i];	
       
   681 		
       
   682 		if ( setting->Mask())
       
   683 		{
       
   684 			TUint32 masked = aSettingId & setting->Mask();
       
   685 			if ( masked == setting->Start())
       
   686 			{
       
   687 				retVal = setting->CheckAccess( aMessage, aAccessType);
       
   688 				if ( !retVal )
       
   689 				{
       
   690 					return EFalse;
       
   691 				}
       
   692 			}
       
   693 		}
       
   694 		else
       
   695 		{
       
   696 			if ( setting->Start() <= aSettingId && aSettingId <= setting->End())
       
   697 			{
       
   698 				retVal = setting->CheckAccess( aMessage, aAccessType);
       
   699 				if ( !retVal )
       
   700 				{
       
   701 					return EFalse;
       
   702 				}
       
   703 			}
       
   704 		}
       
   705 	}
       
   706 
       
   707 	if ( iDefaultSetting)	
       
   708 	{
       
   709 		retVal = iDefaultSetting->CheckAccess( aMessage, aAccessType);
       
   710 	}
       
   711 	
       
   712 	return retVal;
       
   713 }
       
   714 
       
   715 
       
   716 
       
   717 
       
   718 
       
   719 // -----------------------------------------------------------------------------
       
   720 // CRepositorySession::CRepositorySession()
       
   721 // -----------------------------------------------------------------------------
       
   722 //
       
   723 
       
   724 CRepositorySession::CRepositorySession( TUid aRepositoryId)
       
   725 	: iRepositoryId( aRepositoryId), iRepositoryInUse( EFalse)
       
   726 {
       
   727 }
       
   728 
       
   729 // -----------------------------------------------------------------------------
       
   730 // CRepositorySession::~CRepositorySession()
       
   731 // -----------------------------------------------------------------------------
       
   732 //
       
   733 
       
   734 CRepositorySession::~CRepositorySession()
       
   735 {
       
   736 	//if session is not committed, restore file!
       
   737 
       
   738 
       
   739 	//release resources....
       
   740 	ReleaseResources();
       
   741 
       
   742 	//close file server sessions
       
   743 	iFile.Close();
       
   744 	iFs.Close();
       
   745 	
       
   746 }
       
   747 
       
   748 
       
   749 // -----------------------------------------------------------------------------
       
   750 // CRepositorySession::ReleaseResources()
       
   751 // -----------------------------------------------------------------------------
       
   752 //
       
   753 void CRepositorySession::ReleaseResources()
       
   754 {
       
   755 	delete iRepContent;
       
   756 	iRepContent = 0;
       
   757 	
       
   758 	}
       
   759 
       
   760 
       
   761 // -----------------------------------------------------------------------------
       
   762 // CRepositorySession::NewL()
       
   763 // -----------------------------------------------------------------------------
       
   764 //
       
   765 
       
   766 CRepositorySession * CRepositorySession::NewL( TUid aRepositoryId)
       
   767 {
       
   768 	CRepositorySession * self = new(ELeave) CRepositorySession( aRepositoryId);
       
   769 	
       
   770 	CleanupStack::PushL( self);
       
   771 //	self->ConstructL();
       
   772 	CleanupStack::Pop( self);
       
   773 	
       
   774 	return self;
       
   775 }
       
   776 
       
   777 // -----------------------------------------------------------------------------
       
   778 // CRepositorySession::CheckCommitStateL()
       
   779 // -----------------------------------------------------------------------------
       
   780 //
       
   781 
       
   782 void CRepositorySession::CheckCommitStateL()
       
   783 {
       
   784 	//Get commit flag state from centrep
       
   785 	CRepository * iCentRep = CRepository::NewLC( KPolicyEngineRepositoryID );
       
   786 	
       
   787 	TInt state;
       
   788 	__ASSERT_ALWAYS( KErrNone == iCentRep->Get( KCommitFlag, state), User::Panic( IniConstants::KCentRepToolPanic, KErrCorrupt));
       
   789 	
       
   790 	if ( state == EFalse)
       
   791 	{
       
   792 		//restore backup if changes are not committed
       
   793 		RestoreBackupL();
       
   794 	}
       
   795 	
       
   796 	//remove backup
       
   797 	RemoveBackupL();	
       
   798 	
       
   799 	CleanupStack::PopAndDestroy();	//CRepository
       
   800 }
       
   801 
       
   802 // -----------------------------------------------------------------------------
       
   803 // CRepositorySession::RestoreBackupL()
       
   804 // -----------------------------------------------------------------------------
       
   805 //
       
   806 void CRepositorySession::RestoreBackupL()
       
   807 {
       
   808 
       
   809 	using namespace IniConstants;
       
   810 
       
   811 	RFs rfs;
       
   812 	User::LeaveIfError( rfs.Connect() );
       
   813 	CleanupClosePushL( rfs);
       
   814 
       
   815 	//Open directory and get file list
       
   816 	RDir dir;
       
   817 	CleanupClosePushL( dir);
       
   818 	
       
   819 	TInt err = dir.Open( rfs, KBackupPath, KEntryAttNormal);
       
   820 	
       
   821 	if ( err == KErrPathNotFound)
       
   822 	{
       
   823 		CreatePath();
       
   824 		err = dir.Open( rfs, KBackupPath, KEntryAttNormal);
       
   825 	}
       
   826 	
       
   827 	User::LeaveIfError( err);
       
   828 	
       
   829 	err = KErrNone;
       
   830 	do 
       
   831 	{
       
   832 		//read entries (files)
       
   833 		TEntryArray array;
       
   834 		err = dir.Read( array);
       
   835 		if ( KErrEof != err)
       
   836 		{
       
   837 			User::LeaveIfError( err);
       
   838 		}	
       
   839 		
       
   840 		for ( TInt i = 0; i < array.Count(); i++)
       
   841 		{
       
   842 			const TEntry& entry = array[i];
       
   843 
       
   844 			TBuf<KPathLength> backupFile;
       
   845 			backupFile.Append( KBackupPath);
       
   846 			TBuf<KPathLength> name;
       
   847 			name.Append( KDriveC);
       
   848 		
       
   849 
       
   850 			//repository backup
       
   851 			//original file name and location
       
   852 			name.Append( entry.iName);
       
   853 		
       
   854 			//backup file name and location
       
   855 			backupFile.Append( entry.iName);
       
   856 			//copy file to work directory
       
   857 			CFileMan * fileMan = CFileMan::NewL( rfs);
       
   858 			CleanupStack::PushL( fileMan);
       
   859 			
       
   860 			//Copy file to work directory	
       
   861 			User::LeaveIfError( fileMan->Copy( backupFile, name));
       
   862 	
       
   863 			CleanupStack::PopAndDestroy();  //fileMan		
       
   864 		}	
       
   865 	} while ( err == KErrNone);
       
   866 	
       
   867 	CleanupStack::PopAndDestroy(2); //RFs
       
   868 }
       
   869 
       
   870 // -----------------------------------------------------------------------------
       
   871 // CRepositorySession::RemoveBackupL()
       
   872 // -----------------------------------------------------------------------------
       
   873 //
       
   874 void CRepositorySession::RemoveBackupL()
       
   875 {
       
   876 	using namespace IniConstants;
       
   877 
       
   878 	RFs rfs;
       
   879 	User::LeaveIfError( rfs.Connect() );
       
   880 	CleanupClosePushL( rfs);
       
   881 
       
   882 	//Open directory and get file list
       
   883 	RDir dir;
       
   884 	CleanupClosePushL( dir);
       
   885 	
       
   886 	TInt err = dir.Open( rfs, KBackupPath, KEntryAttNormal);
       
   887 	
       
   888 	if ( err == KErrPathNotFound)
       
   889 	{
       
   890 		CreatePath();
       
   891 		err = dir.Open( rfs, KBackupPath, KEntryAttNormal);
       
   892 	}	
       
   893 	
       
   894 	User::LeaveIfError( err);
       
   895 	
       
   896 	err = KErrNone;
       
   897 	do 
       
   898 	{
       
   899 		//read entries (files)
       
   900 		TEntryArray array;
       
   901 		err = dir.Read( array);
       
   902 		if ( KErrEof != err)
       
   903 		{
       
   904 			User::LeaveIfError( err);
       
   905 		}	
       
   906 		
       
   907 		
       
   908 		for ( TInt i = 0; i < array.Count(); i++)
       
   909 		{
       
   910 			const TEntry& entry = array[i];
       
   911 			
       
   912 			TBuf<KPathLength> name;
       
   913 			name.Append( KBackupPath);
       
   914 			name.Append( entry.iName);	
       
   915 
       
   916 			User::LeaveIfError( rfs.Delete( name));
       
   917 		}		
       
   918 	} while ( err == KErrNone );
       
   919 	
       
   920 	CleanupStack::PopAndDestroy(2); //RFs, RDir
       
   921 }
       
   922 
       
   923 
       
   924 
       
   925 // -----------------------------------------------------------------------------
       
   926 // CRepositorySession::InitRepositorySessionL()
       
   927 // -----------------------------------------------------------------------------
       
   928 //
       
   929 
       
   930 void CRepositorySession::InitRepositorySessionL()
       
   931 {
       
   932 	using namespace IniConstants;
       
   933 
       
   934 	//Establish connection	
       
   935 	User::LeaveIfError( iFs.Connect() );
       
   936 
       
   937 	//Buffer for file name (without extension)
       
   938 	iRepName.Zero();
       
   939 	iRepName.NumFixedWidth( iRepositoryId.iUid, EHex, KUidLengthRep);
       
   940 
       
   941 	//open repository file
       
   942 	if ( KErrNone != ReadRAML( EFalse))
       
   943 	{
       
   944 		CreateAndReadROML();
       
   945 	}
       
   946 	
       
   947 
       
   948 	//buffer for path and file name
       
   949 	TBuf<KPathLength> repFileName;
       
   950 	repFileName.Append( KDriveC);						//drive and path
       
   951 	TInt pathLength = repFileName.Length();
       
   952 	repFileName.Append( iRepName);						//file name 
       
   953 	repFileName.Append( KRepositoryFileExtensionCre);	//and extension
       
   954 
       
   955 	//create timer
       
   956 	RTimer timer;
       
   957 	User::LeaveIfError( timer.CreateLocal());
       
   958 	
       
   959 	//open or create RAM file for 	
       
   960 	TInt counter = 0;
       
   961 	TInt err = KErrNone;
       
   962 
       
   963 	do
       
   964 	{
       
   965 		err = iFile.Open( iFs, repFileName, EFileShareExclusive|EFileRead|EFileWrite );	
       
   966 		CheckRepositoryState( err);
       
   967 		
       
   968  		if ( err != KErrNone)
       
   969  		{
       
   970  			iFile.Close();
       
   971  		
       
   972  			//or if file is reserved, wait a moment...
       
   973 			TRequestStatus status;
       
   974 			timer.After( status, KFileOpenPeriod);
       
   975 			User::WaitForRequest( status);
       
   976 		}
       
   977 		
       
   978 	} while ( counter++ < KFileOpenAttemps && ( err != KErrNone));
       
   979 			
       
   980 	//close timer
       
   981 	timer.Close();
       
   982 	
       
   983 	User::LeaveIfError( err);
       
   984 	
       
   985 
       
   986 	//create backup file...
       
   987 	if ( err == KErrNone)
       
   988 	{
       
   989 		MakeBackupL( iFile);
       
   990 	}		
       
   991 
       
   992 	//file is not opened
       
   993 	User::LeaveIfError( err);
       
   994 }
       
   995 
       
   996 // -----------------------------------------------------------------------------
       
   997 // CRepositorySession::ReadRAML()
       
   998 // -----------------------------------------------------------------------------
       
   999 //
       
  1000 void CRepositorySession::CheckRepositoryState( TInt& aFileOpenResult)
       
  1001 {
       
  1002 	if ( aFileOpenResult != KErrNone)
       
  1003 	{
       
  1004 		return;
       
  1005 	}
       
  1006 
       
  1007 	//check is repository in use	
       
  1008 	CRepository* rep = 0;
       
  1009 	TRAPD( err, rep = CRepository::NewL( iRepositoryId));
       
  1010 	delete rep;
       
  1011 	
       
  1012 	if ( err == KErrNone )
       
  1013 	{
       
  1014 		iRepositoryInUse = ETrue; 
       
  1015 	}
       
  1016 }
       
  1017 
       
  1018 
       
  1019 // -----------------------------------------------------------------------------
       
  1020 // CRepositorySession::ReadRAML()
       
  1021 // -----------------------------------------------------------------------------
       
  1022 //
       
  1023 TInt CRepositorySession::ReadRAML( TBool aOnlyHeader)
       
  1024 {
       
  1025 	RDEBUG("CentRepTool: Read repository from RAM-drive");
       
  1026 
       
  1027 
       
  1028 	using namespace IniConstants;
       
  1029 
       
  1030 	RFile file;
       
  1031 
       
  1032 	//buffer for path and file name
       
  1033 	TBuf<KPathLength> repFileName;
       
  1034 	repFileName.Append( KDriveC);						//drive and path
       
  1035 	TInt pathLength = repFileName.Length();
       
  1036 	repFileName.Append( iRepName);						//file name 
       
  1037 	repFileName.Append( KRepositoryFileExtensionCre);	//and extension
       
  1038 
       
  1039 	//Open file
       
  1040 	TInt err = file.Open( iFs, repFileName, EFileRead);
       
  1041 	RDEBUG_2("CentRepTool: Open repository - %S",  &repFileName);
       
  1042 	
       
  1043 	if ( err != KErrNone)
       
  1044 	{
       
  1045 		return err;
       
  1046 	}
       
  1047 
       
  1048 	//Open file stream
       
  1049 	CDirectFileStore* directFileStore = CDirectFileStore::FromLC ( file);
       
  1050 	TStreamId rootStreamId = directFileStore->Root() ;
       
  1051 	
       
  1052 	//setup read stream
       
  1053 	RStoreReadStream rootStream ;
       
  1054 	rootStream.OpenLC( *directFileStore, rootStreamId);
       
  1055 
       
  1056 	//Internalize the repository
       
  1057 	iRepContent = CRepositoryContent::NewL( iRepositoryId);
       
  1058 	iRepContent->ReadStreamL( rootStream, aOnlyHeader);
       
  1059 
       
  1060 	CleanupStack::PopAndDestroy( 2, directFileStore);
       
  1061 	
       
  1062 	return KErrNone;
       
  1063 }
       
  1064 
       
  1065 
       
  1066 // -----------------------------------------------------------------------------
       
  1067 // CRepositorySession::CreateROMFileL()
       
  1068 // -----------------------------------------------------------------------------
       
  1069 //
       
  1070 void CRepositorySession::CreateAndReadROML()
       
  1071 {
       
  1072 	using namespace IniConstants;
       
  1073 
       
  1074 	//read content from ROM
       
  1075 	iRepContent = ReadROMFileL( ETrue);
       
  1076 
       
  1077 	__UHEAP_MARK;
       
  1078 
       
  1079 	//buffer for path and file name
       
  1080 	TBuf<KPathLength> tmpFileName;
       
  1081 	tmpFileName.Append( KBackupPath);						//drive and path
       
  1082 	TInt pathLength = tmpFileName.Length();
       
  1083 	tmpFileName.Append( iRepName);						//file name 
       
  1084 	tmpFileName.Append( KRepositoryFileExtensionTmp);	//and extension
       
  1085 	
       
  1086 	RFile file;
       
  1087 	CleanupClosePushL( file);
       
  1088 
       
  1089 	//Create RAM file. 
       
  1090 	User::LeaveIfError( file.Replace( iFs, tmpFileName, EFileShareExclusive|EFileRead|EFileWrite ));
       
  1091 	
       
  1092 	//Open file stream. RFile ownership changes for CDirectFileStore
       
  1093 	CDirectFileStore * directFileStore = CDirectFileStore::NewL( file);
       
  1094 	CleanupStack::Pop( &file);
       
  1095 	CleanupStack::PushL( directFileStore);
       
  1096 
       
  1097 	//setup write stream
       
  1098 	directFileStore->SetTypeL( TUidType(KDirectFileStoreLayoutUid, KNullUid, KCentralRepositoryUID)) ; 
       
  1099 		
       
  1100 	RStoreWriteStream rootStream ;
       
  1101 	TStreamId rootStreamId = rootStream.CreateLC(*directFileStore) ;
       
  1102 
       
  1103 	//Externalize .cre file....
       
  1104 	iRepContent->WriteStreamL( rootStream);
       
  1105 	rootStream.CommitL();
       
  1106 
       
  1107 	directFileStore->SetRootL(rootStreamId);
       
  1108 	directFileStore->CommitL();
       
  1109 
       
  1110 	//destroy
       
  1111 	CleanupStack::PopAndDestroy( 2, directFileStore);
       
  1112 
       
  1113 	
       
  1114 	//copy file to work directory
       
  1115 	CFileMan * fileMan = CFileMan::NewL( iFs);
       
  1116 	CleanupStack::PushL( fileMan);
       
  1117 			
       
  1118 	//buffer for path and file name
       
  1119 	TBuf<KPathLength> repFileName;
       
  1120 	repFileName.Append( KDriveC);						//drive and path
       
  1121 	pathLength = repFileName.Length();
       
  1122 	repFileName.Append( iRepName);						//file name 
       
  1123 	repFileName.Append( KRepositoryFileExtensionCre);	//and extension
       
  1124 
       
  1125 	//Copy file to work directory	
       
  1126 	User::LeaveIfError( fileMan->Copy( tmpFileName, repFileName));
       
  1127 	User::LeaveIfError( fileMan->Delete( tmpFileName));
       
  1128 	
       
  1129 	CleanupStack::PopAndDestroy( fileMan);  //fileMan
       
  1130 
       
  1131 	__UHEAP_MARKEND;
       
  1132 }
       
  1133 
       
  1134 
       
  1135 // -----------------------------------------------------------------------------
       
  1136 // CRepositorySession::CommitRepositoryL()
       
  1137 // -----------------------------------------------------------------------------
       
  1138 //
       
  1139 void CRepositorySession::CommitRepositoryL()
       
  1140 {
       
  1141 	RDEBUG("CentRepTool: CommitRepositoryL");
       
  1142 
       
  1143 
       
  1144 	using namespace IniConstants;
       
  1145 
       
  1146 	//buffer for path and file name
       
  1147 	TBuf<KPathLength> tmpFileName;
       
  1148 	tmpFileName.Append( KBackupPath);						//drive and path
       
  1149 	tmpFileName.Append( KTempFile);						//file name 
       
  1150 
       
  1151 	//Create RAM file
       
  1152 	RFile file;
       
  1153 	TInt err = file.Create( iFs, tmpFileName, EFileShareExclusive|EFileRead|EFileWrite );
       
  1154 	
       
  1155 	if ( err == KErrAlreadyExists)
       
  1156 	{
       
  1157 		User::LeaveIfError( file.Open( iFs, tmpFileName, EFileShareExclusive|EFileRead|EFileWrite ));
       
  1158 		User::LeaveIfError( file.SetSize(0));
       
  1159 	}
       
  1160 	
       
  1161 	//Open file stream
       
  1162 	CDirectFileStore * directFileStore = CDirectFileStore::NewLC( file);
       
  1163 
       
  1164 	//setup write stream
       
  1165 	directFileStore->SetTypeL( TUidType(KDirectFileStoreLayoutUid, KNullUid, KCentralRepositoryUID)) ; 
       
  1166 		
       
  1167 	RStoreWriteStream rootStream ;
       
  1168 	TStreamId rootStreamId = rootStream.CreateLC(*directFileStore) ;
       
  1169 
       
  1170 	//Externalize .cre file....
       
  1171 	iRepContent->WriteStreamL( rootStream);
       
  1172 	rootStream.CommitL();
       
  1173 
       
  1174 	directFileStore->SetRootL(rootStreamId);
       
  1175 	directFileStore->CommitL();
       
  1176 
       
  1177 	RDEBUG("CentRepTool: Repository streamed to temp-file");
       
  1178 
       
  1179 	//destroy
       
  1180 	CleanupStack::PopAndDestroy( 2, directFileStore);
       
  1181 
       
  1182 	//copy temp file to repository file
       
  1183 	User::LeaveIfError( file.Open( iFs, tmpFileName, EFileRead));
       
  1184 	CleanupClosePushL( file);
       
  1185 	
       
  1186 	TInt size = 0;
       
  1187 	User::LeaveIfError( file.Size( size));
       
  1188 	
       
  1189 	HBufC8 * buf = HBufC8::NewLC( size);
       
  1190 	TPtr8 ptr = buf->Des();
       
  1191 	User::LeaveIfError( file.Read( ptr));
       
  1192 	
       
  1193 	//seek to start of the file
       
  1194 	TInt seekOffset(0);
       
  1195 
       
  1196 	User::LeaveIfError( iFile.Seek( ESeekStart, seekOffset));
       
  1197 	User::LeaveIfError( iFile.Write( *buf));
       
  1198 	User::LeaveIfError( iFile.SetSize( size));
       
  1199 	
       
  1200 	User::LeaveIfError( iFile.Flush());
       
  1201 
       
  1202 	RDEBUG_2("CentRepTool: Persist repository file (.cre) committed from temp file (size: %d bytes)", size);
       
  1203 
       
  1204 
       
  1205 	//enforce central repository to re-read setting from .cre file...
       
  1206 	if ( iRepositoryInUse)
       
  1207 	{
       
  1208 		RDEBUG("CentRepTool: Repository is already used by central repository - ");
       
  1209 		
       
  1210 		//create repository session...
       
  1211 		CRepository* repository = CRepository::NewLC( iRepositoryId);
       
  1212 	
       
  1213 		//in next phase try to modifie CentRep setting. Because .cre file is locked by CRepositorySession
       
  1214 		//CentRep cannot make update and causes that repostiory goes to inconsistent state. Inconsistent
       
  1215 		//state ensures that new security settings are readed from drive before any other operation
       
  1216 		TInt errx = repository->Create( 0, 0);
       
  1217 		if ( errx == KErrAlreadyExists )
       
  1218 		{
       
  1219 			errx = repository->Set( 0, 0);
       
  1220 		}
       
  1221 	
       
  1222 		RDEBUG_2("CentRepTool: Enforce Central Repository file inconsistent state (CR state %d, must be != KErrNone)", errx);
       
  1223 		CleanupStack::PopAndDestroy( repository);
       
  1224 	}
       
  1225 	
       
  1226 	CleanupStack::PopAndDestroy( 2, &file);
       
  1227 }
       
  1228 
       
  1229 
       
  1230 
       
  1231 
       
  1232 
       
  1233 // -----------------------------------------------------------------------------
       
  1234 // CRepositorySession::ReadFileL()
       
  1235 // -----------------------------------------------------------------------------
       
  1236 //
       
  1237 CRepositoryContent* CRepositorySession::ReadROMFileL( TBool aReadSettings)
       
  1238 {
       
  1239 	using namespace IniConstants;
       
  1240 
       
  1241 	RDEBUG("CentRepTool: Read repository from ROM-drive");
       
  1242 
       
  1243 	CRepositoryContent* retVal = 0;
       
  1244 	
       
  1245 	//buffer for path and file name
       
  1246 	TBuf<KPathLength> repFileName;
       
  1247 	repFileName.Append( KDriveZ);						//drive and path
       
  1248 	TInt pathLength = repFileName.Length();
       
  1249 	repFileName.Append( iRepName);				//file name 
       
  1250 	repFileName.Append( KRepositoryFileExtensionTxt);	//and extension
       
  1251 
       
  1252 	//ROM file should always be ready to open
       
  1253 	RFile file;
       
  1254 	TInt err = file.Open( iFs, repFileName, EFileRead);	
       
  1255 
       
  1256 	if ( err == KErrNone )
       
  1257 	{
       
  1258 		RDEBUG("CentRepTool: ROM repository found (.txt)");
       
  1259 		RDEBUG_2("CentRepTool: Open repository - %S",  &repFileName);
       
  1260 
       
  1261 		CleanupClosePushL( file);
       
  1262 
       
  1263 		//read file content and set iContentPtr
       
  1264 		TIniFileHelper ini;
       
  1265 		HBufC16 * content = ini.ReadFileL( file);
       
  1266 	
       
  1267 		retVal = CRepositoryContent::NewL( iRepositoryId, content);
       
  1268 		CleanupStack::PushL( retVal);
       
  1269 	
       
  1270 		//read sections from ini file
       
  1271 		retVal->ReadHeaderL();
       
  1272 		retVal->ReadOptionalDataL();
       
  1273 		retVal->ReadPlatSecL();
       
  1274 		
       
  1275 		if (aReadSettings)
       
  1276 		{
       
  1277 			retVal->ReadMainL();
       
  1278 		}
       
  1279 	
       
  1280 		CleanupStack::Pop( retVal);
       
  1281 		CleanupStack::PopAndDestroy( &file);
       
  1282 		
       
  1283 	}
       
  1284 	else
       
  1285 	{
       
  1286 		RDEBUG("CentRepTool: ROM repository not found (.txt), try .cre-repository");
       
  1287 
       
  1288 		repFileName.Replace( repFileName.Length() - 4, 4, KRepositoryFileExtensionCre);
       
  1289 		User::LeaveIfError( file.Open( iFs, repFileName, EFileRead));
       
  1290 		
       
  1291 		RDEBUG_2("CentRepTool: Open repository - %S",  &repFileName);
       
  1292 
       
  1293 		retVal = CRepositoryContent::NewL( iRepositoryId);
       
  1294 		CleanupStack::PushL( retVal);
       
  1295 
       
  1296 		//Open file stream
       
  1297 		CDirectFileStore* directFileStore = CDirectFileStore::FromLC ( file);
       
  1298 		TStreamId rootStreamId = directFileStore->Root() ;
       
  1299 	
       
  1300 		//setup read stream
       
  1301 		RStoreReadStream rootStream ;
       
  1302 		rootStream.OpenLC( *directFileStore, rootStreamId);
       
  1303 
       
  1304 		//Internalize the repository
       
  1305 		retVal->ReadStreamL( rootStream, !aReadSettings);
       
  1306 
       
  1307 		CleanupStack::PopAndDestroy( 2, directFileStore);
       
  1308 		CleanupStack::Pop( retVal);
       
  1309 	
       
  1310 	}
       
  1311 	
       
  1312 	RDEBUG("CentRepTool: Repository read successfully");
       
  1313 	
       
  1314 	return retVal;
       
  1315 }
       
  1316 
       
  1317 
       
  1318 
       
  1319 
       
  1320 
       
  1321 // -----------------------------------------------------------------------------
       
  1322 // CRepositorySession::MakeBackupL()
       
  1323 // -----------------------------------------------------------------------------
       
  1324 //
       
  1325 
       
  1326 void CRepositorySession::MakeBackupL( RFile& aFile)
       
  1327 {
       
  1328 	using namespace IniConstants;
       
  1329 
       
  1330 	//Because file is already opened, CFileMan cannot be used!
       
  1331 	//seek to start of the file
       
  1332 	TInt seekOffset(0);
       
  1333 	aFile.Seek( ESeekStart, seekOffset);
       
  1334 	
       
  1335 	TInt size;
       
  1336 	User::LeaveIfError( aFile.Size(size));
       
  1337 
       
  1338 	//create buffer for content
       
  1339 	HBufC8* buf = HBufC8::NewLC(size);
       
  1340 	TPtr8 ptr = buf->Des();
       
  1341 	
       
  1342 	//read file
       
  1343 	aFile.Read( ptr);
       
  1344 
       
  1345 	
       
  1346 	//backup file name
       
  1347 	TBuf<KPathLength> backupFile;
       
  1348 	iFs.PrivatePath( backupFile);
       
  1349 	backupFile.Append( iRepName);							//file name 
       
  1350 	backupFile.Append( KRepositoryFileExtensionCre);		//and extension
       
  1351 	
       
  1352 	//create backup file
       
  1353 	RFile backup;
       
  1354 	CleanupClosePushL( backup);
       
  1355 	
       
  1356 	TInt err = backup.Replace( iFs, backupFile, EFileWrite);
       
  1357 	if ( err == KErrPathNotFound)
       
  1358 	{
       
  1359 		err = CreatePath();
       
  1360 	}
       
  1361 
       
  1362 	User::LeaveIfError( err);	
       
  1363 	
       
  1364 	//write to file
       
  1365 	User::LeaveIfError( backup.Write( ptr));
       
  1366 	
       
  1367 	//ensures that if file close fails, error is detected
       
  1368 	User::LeaveIfError( backup.Flush());
       
  1369 	
       
  1370 	CleanupStack::PopAndDestroy(2);  //backup, HBufC	
       
  1371 	
       
  1372 }
       
  1373 
       
  1374 // -----------------------------------------------------------------------------
       
  1375 // CRepositorySession::CreatePath()
       
  1376 // -----------------------------------------------------------------------------
       
  1377 //
       
  1378 TInt CRepositorySession::CreatePath()
       
  1379 {
       
  1380 	//create private path
       
  1381 	
       
  1382 	RFs rfs;
       
  1383 	TInt err = rfs.Connect();
       
  1384 	
       
  1385 	if ( err == KErrNone )
       
  1386 	{
       
  1387 		err = rfs.CreatePrivatePath( EDriveC);
       
  1388 	}
       
  1389 	
       
  1390 	rfs.Close();
       
  1391 	
       
  1392 	return err;
       
  1393 }
       
  1394 
       
  1395 
       
  1396 
       
  1397 
       
  1398 
       
  1399 // -----------------------------------------------------------------------------
       
  1400 // CRepositorySession::SetSecurityIdForSetting()
       
  1401 // -----------------------------------------------------------------------------
       
  1402 //
       
  1403 
       
  1404 void CRepositorySession::SetSecurityIdForSettingL( const RMessage2& aMessage)
       
  1405 {
       
  1406 	__UHEAP_MARK;
       
  1407 
       
  1408 	TIniFileHelper ini;
       
  1409 
       
  1410 	//read setting id from RMessage
       
  1411 	TUint32 settingUid;
       
  1412 	TPckg<TUint32> settingUidPack( settingUid);
       
  1413 	aMessage.ReadL(0, settingUidPack);
       
  1414 	
       
  1415 	//read SID from RMessage
       
  1416 	TUid applicationUid;
       
  1417 	TPckg<TUid> applicationUidPack( applicationUid);
       
  1418 	aMessage.ReadL(1, applicationUidPack);
       
  1419 
       
  1420 	CIndividualSetting * setting = iRepContent->FindIndividualSetting( settingUid);
       
  1421 	
       
  1422 	if ( !setting )
       
  1423 	{
       
  1424 		User::Leave( KErrNotFound);
       
  1425 	}
       
  1426 
       
  1427 	User::LeaveIfError( setting->AddSid( applicationUid));
       
  1428 
       
  1429 	
       
  1430 	__UHEAP_MARKEND;
       
  1431 }
       
  1432 
       
  1433 
       
  1434 
       
  1435 // -----------------------------------------------------------------------------
       
  1436 // CRepositorySession::SetSecurityIdForRange()
       
  1437 // -----------------------------------------------------------------------------
       
  1438 //
       
  1439 
       
  1440 void CRepositorySession::SetSecurityIdForRangeL( const RMessage2& aMessage)
       
  1441 {
       
  1442 	//read range start from RMessage
       
  1443 	TUint32 rangeStart;
       
  1444 	TPckg<TUint32> rangeStartPack( rangeStart);
       
  1445 	aMessage.ReadL(0, rangeStartPack);
       
  1446 
       
  1447 	//read range end from RMessage
       
  1448 	TUint32 rangeEnd;
       
  1449 	TPckg<TUint32> rangeEndPack( rangeEnd);
       
  1450 	aMessage.ReadL(1, rangeEndPack);
       
  1451 	
       
  1452 	//read SID from RMessage
       
  1453 	TUid applicationUid;
       
  1454 	TPckg<TUid> applicationUidPack( applicationUid);
       
  1455 	aMessage.ReadL(2, applicationUidPack);
       
  1456 
       
  1457 
       
  1458 	//Find range setting
       
  1459 	RPointerArray<CRangeSetting> settings;
       
  1460 	CleanupClosePushL( settings);
       
  1461 	iRepContent->CreateRangeSettingsL( settings, rangeStart, rangeEnd);
       
  1462 	
       
  1463 	//Add SIDs for setting inside of the range
       
  1464 	for ( TInt i(0); i < settings.Count(); i++)
       
  1465 	{
       
  1466 		User::LeaveIfError( settings[i]->AddSid( applicationUid));
       
  1467 	}
       
  1468 	
       
  1469 	
       
  1470 	CleanupStack::PopAndDestroy( &settings);
       
  1471 }
       
  1472 
       
  1473 // -----------------------------------------------------------------------------
       
  1474 // CRepositorySession::CleanSecurityIdForSetting()
       
  1475 // -----------------------------------------------------------------------------
       
  1476 //
       
  1477 
       
  1478 void CRepositorySession::RestoreSettingL( const RMessage2& aMessage)
       
  1479 {
       
  1480 	using namespace IniConstants;
       
  1481 
       
  1482 	__UHEAP_MARK;
       
  1483 
       
  1484 	TIniFileHelper ini;
       
  1485 
       
  1486 	//read setting id from RMessage
       
  1487 	TUint32 settingUid;
       
  1488 	TPckg<TUint32> settingUidPack( settingUid);
       
  1489 	aMessage.ReadL(0, settingUidPack);
       
  1490 	
       
  1491 	CIndividualSetting * setting = iRepContent->FindIndividualSetting( settingUid);
       
  1492 	
       
  1493 	if ( !setting )
       
  1494 	{
       
  1495 		User::Leave( KErrNotFound);
       
  1496 	}
       
  1497 	
       
  1498 	
       
  1499 
       
  1500 	User::LeaveIfError( setting->RemoveSid());
       
  1501 
       
  1502 	
       
  1503 	__UHEAP_MARKEND;
       
  1504 	
       
  1505 }
       
  1506 
       
  1507 
       
  1508 
       
  1509 // -----------------------------------------------------------------------------
       
  1510 // CRepositorySession::WriteStreamL()
       
  1511 // -----------------------------------------------------------------------------
       
  1512 //
       
  1513 void CRepositoryContent::WriteStreamL( RWriteStream& aStream )
       
  1514 {
       
  1515 	RDEBUG("CentRepTool: Streaming");
       
  1516 	RDEBUG("CentRepTool: initialization data");
       
  1517 
       
  1518 	//read initialization data
       
  1519 	aStream << IniConstants::KPersistsVersion;
       
  1520 	aStream << iUid;
       
  1521 	aStream << iOwner;
       
  1522 
       
  1523 	//write single policies	
       
  1524 	TInt32 numElements = SingleSecuritySettingsCount();			//Count	
       
  1525 	RDEBUG_2("CentRepTool: Single policies (count %d)", numElements);
       
  1526 	aStream << numElements;	
       
  1527 
       
  1528 	for ( TInt i = 0; i < numElements; i++)
       
  1529 	{
       
  1530 		if ( iIndividualSettings[i]->ContainsSecuritySettings())
       
  1531 		{
       
  1532 			iIndividualSettings[i]->ExternalizePlatSecL( aStream);		
       
  1533 		}
       
  1534 	}
       
  1535 		
       
  1536 	//read range policies
       
  1537 	numElements = iRangeSettings.Count();	
       
  1538 	aStream << numElements;	
       
  1539 	RDEBUG_2("CentRepTool: Range policies (count %d)", numElements);
       
  1540 			
       
  1541 	for ( TInt i = 0; i < iRangeSettings.Count(); i++)
       
  1542 	{
       
  1543 		aStream << *(iRangeSettings[i]);
       
  1544 	}
       
  1545 
       
  1546 	RDEBUG("CentRepTool: Default policies");
       
  1547 
       
  1548 	//read default policies (read and write)...	
       
  1549 	aStream << (*iDefaultSetting);
       
  1550 
       
  1551 	RDEBUG("CentRepTool: Default meta data");
       
  1552 	//read default meta	
       
  1553 	aStream << iDefaultMeta;
       
  1554 
       
  1555 	//read range meta data
       
  1556 	numElements = iRangeMetas.Count();	
       
  1557 	RDEBUG_2("CentRepTool: Range metadata (count %d)", numElements);
       
  1558 	aStream << numElements;	
       
  1559 			
       
  1560 	for ( TInt i = 0; i < iRangeMetas.Count(); i++)
       
  1561 	{
       
  1562 		aStream << *(iRangeMetas[i]);
       
  1563 	}
       
  1564 
       
  1565 	//read time stamp
       
  1566 	aStream << iTimeStamp.Int64();
       
  1567 	RDEBUG_2("CentRepTool: Timestamp %d", iTimeStamp.Int64());
       
  1568 
       
  1569 	//read setting values
       
  1570 	numElements = iIndividualSettings.Count();	
       
  1571 	aStream << numElements;	
       
  1572 	RDEBUG_2("CentRepTool: Individual settings (count %d)", numElements);
       
  1573 
       
  1574 	for ( TInt i = 0; i < iIndividualSettings.Count(); i++)
       
  1575 	{
       
  1576 		iIndividualSettings[i]->ExternalizeDataL( aStream);
       
  1577 	}	
       
  1578 	
       
  1579 	RDEBUG("CentRepTool: Streaming finished");
       
  1580 }
       
  1581 
       
  1582 
       
  1583 // -----------------------------------------------------------------------------
       
  1584 // CRepositoryContent::SingleSecurituSettingsCount()
       
  1585 // -----------------------------------------------------------------------------
       
  1586 //
       
  1587 TInt CRepositoryContent::SingleSecuritySettingsCount()
       
  1588 {
       
  1589 	return iSingleSecuritySettingCount;
       
  1590 }
       
  1591 
       
  1592 // -----------------------------------------------------------------------------
       
  1593 // CRepositoryContent::ReadStreamL()
       
  1594 // -----------------------------------------------------------------------------
       
  1595 //
       
  1596 void CRepositoryContent::ReadStreamL( RReadStream& aStream, TBool aOnlyHeader)
       
  1597 {
       
  1598 	RDEBUG("CentRepTool: Streaming");
       
  1599 	RDEBUG("CentRepTool: initialization data");
       
  1600 
       
  1601 	//read initialization data
       
  1602 	TUint8 version;
       
  1603 	aStream >> version;
       
  1604 	aStream >> iUid;
       
  1605 	aStream >> iOwner;
       
  1606 
       
  1607 	//read single policies	
       
  1608 	TUint32 countUint32;
       
  1609 	aStream >> countUint32;	
       
  1610 
       
  1611 	RDEBUG_2("CentRepTool: Single policies (count %d)", countUint32);
       
  1612 
       
  1613 	for ( TInt i = 0; i < countUint32; i++)
       
  1614 	{
       
  1615 		CIndividualSetting* singlePolicy = new(ELeave) CIndividualSetting();
       
  1616 		singlePolicy->InternalizePlatSecL( aStream);
       
  1617 		AddIndividualSettingL( singlePolicy);
       
  1618 	}
       
  1619 		
       
  1620 	//read range policies
       
  1621 	TInt32 countInt32;
       
  1622 	aStream >> countInt32;	
       
  1623 			
       
  1624 	RDEBUG_2("CentRepTool: Range policies (count %d)", countInt32);
       
  1625 
       
  1626 	for ( TInt i = 0; i < countInt32; i++)
       
  1627 	{
       
  1628 		CRangeSetting* rangePolicy = new(ELeave) CRangeSetting();
       
  1629 		aStream >> *rangePolicy;
       
  1630 		User::LeaveIfError( iRangeSettings.Append( rangePolicy));
       
  1631 	}
       
  1632 		
       
  1633 
       
  1634 	//read default policies (read and write)...
       
  1635 	RDEBUG("CentRepTool: Default policies");
       
  1636 	aStream >> (*iDefaultSetting);
       
  1637 
       
  1638 	//read default meta	
       
  1639 	RDEBUG("CentRepTool: Default meta data");
       
  1640 	aStream >> iDefaultMeta;
       
  1641 
       
  1642 	//read range meta data
       
  1643 	aStream >> countInt32;	
       
  1644 	RDEBUG_2("CentRepTool: Range metadata (count %d)", countInt32);
       
  1645 	
       
  1646 	for ( TInt i = 0; i < countInt32; i++)
       
  1647 	{
       
  1648 		CRangeMeta* rangeMeta = new(ELeave) CRangeMeta();
       
  1649 		aStream >> (*rangeMeta);
       
  1650 		User::LeaveIfError( iRangeMetas.Append( rangeMeta));
       
  1651 	}
       
  1652 	
       
  1653 	//read time stamp
       
  1654 	TInt64 timeStampInt ;
       
  1655 	aStream >> timeStampInt ;
       
  1656 	iTimeStamp = timeStampInt ;
       
  1657 	RDEBUG_2("CentRepTool: Timestamp %d", iTimeStamp.Int64());
       
  1658 
       
  1659 	if ( !aOnlyHeader)
       
  1660 	{
       
  1661 		//read setting values
       
  1662 		aStream >> countInt32;	
       
  1663 		RDEBUG_2("CentRepTool: Individual settings (count %d)", countInt32);
       
  1664 	
       
  1665 		for ( TInt i = 0; i < countInt32; i++)
       
  1666 		{
       
  1667 			//find setting id from stream and check is there already setting for that id
       
  1668 			TUint32 settingId = 0;
       
  1669 			aStream >> settingId;
       
  1670 			CIndividualSetting* setting = FindSettingOrCreateL( settingId);
       
  1671 		
       
  1672 			//get setting data
       
  1673 			setting->InternalizeDataL( aStream);
       
  1674 		}
       
  1675 	}
       
  1676 
       
  1677 	RDEBUG("CentRepTool: Streaming finished");
       
  1678 }
       
  1679 
       
  1680 
       
  1681 
       
  1682 
       
  1683 // -----------------------------------------------------------------------------
       
  1684 // CRepositorySession::CleanSecurityIdForRange()
       
  1685 // -----------------------------------------------------------------------------
       
  1686 //
       
  1687 
       
  1688 void CRepositorySession::RestoreRangeL( const RMessage2& aMessage)
       
  1689 {
       
  1690 
       
  1691 	//read range start from RMessage
       
  1692 	TUint32 rangeStart;
       
  1693 	TPckg<TUint32> rangeStartPack( rangeStart);
       
  1694 	aMessage.ReadL(0, rangeStartPack);
       
  1695 
       
  1696 	//read range end from RMessage
       
  1697 	TUint32 rangeEnd;
       
  1698 	TPckg<TUint32> rangeEndPack( rangeEnd);
       
  1699 	aMessage.ReadL(1, rangeEndPack);
       
  1700 	
       
  1701 	CRepositoryContent* romContent = ReadROMFileL( EFalse);
       
  1702 	CleanupStack::PushL( romContent);
       
  1703 
       
  1704 	for ( TInt i(0); i < iRepContent->iRangeSettings.Count(); i++)
       
  1705 	{
       
  1706 		CRangeSetting * setting = iRepContent->iRangeSettings[i];
       
  1707 		if ( setting->Type() != ERangeSetting )
       
  1708 		{
       
  1709 			continue;
       
  1710 		}
       
  1711 		
       
  1712 		if ( (setting->iStart > rangeStart && setting->iStart < rangeEnd ) ||
       
  1713 			 (setting->iEnd > rangeStart && setting->iEnd < rangeEnd ))
       
  1714 		{
       
  1715 			delete setting;
       
  1716 			iRepContent->iRangeSettings.Remove( i--);
       
  1717 		}
       
  1718 	}
       
  1719 			 
       
  1720 	for ( TInt i(0); i < romContent->iRangeSettings.Count(); i++)
       
  1721 	{
       
  1722 		CRangeSetting * setting = romContent->iRangeSettings[i];
       
  1723 		if ( setting->Type() != ERangeSetting )
       
  1724 		{
       
  1725 			continue;
       
  1726 		}
       
  1727 		
       
  1728 		if ( (setting->iStart > rangeStart && setting->iStart < rangeEnd ) ||
       
  1729 			 (setting->iEnd > rangeStart && setting->iEnd < rangeEnd ))
       
  1730 		{
       
  1731 			iRepContent->iRangeSettings.AppendL( setting);
       
  1732 			romContent->iRangeSettings.Remove( i--);
       
  1733 		}
       
  1734 	}
       
  1735 		
       
  1736 	CleanupStack::PopAndDestroy( romContent);	
       
  1737 }
       
  1738 
       
  1739 
       
  1740 
       
  1741 // -----------------------------------------------------------------------------
       
  1742 // CRepositorySession::AddSidForDefaultsL()
       
  1743 // -----------------------------------------------------------------------------
       
  1744 //
       
  1745 
       
  1746 void CRepositorySession::AddSidForDefaultsL( const RMessage2& aMessage)
       
  1747 {
       
  1748 	using namespace IniConstants;
       
  1749 
       
  1750 	TIniFileHelper ini;
       
  1751 
       
  1752 	//read SID from RMessage
       
  1753 	TUid applicationUid;
       
  1754 	TPckg<TUid> applicationUidPack( applicationUid);
       
  1755 	aMessage.ReadL(0, applicationUidPack);
       
  1756 
       
  1757 
       
  1758 	if ( iRepContent->iDefaultSetting )
       
  1759 	{
       
  1760 		iRepContent->iDefaultSetting->AddSid( applicationUid);
       
  1761 	}
       
  1762 
       
  1763 	for ( TInt i(0); i < iRepContent->iRangeSettings.Count(); i++)
       
  1764 	{
       
  1765 		iRepContent->iRangeSettings[i]->AddSid( applicationUid);
       
  1766 	}
       
  1767 
       
  1768 /*	for ( TInt i(0); i < iIndividualSettings.Count(); i++)
       
  1769 	{
       
  1770 		iIndividualSettings[i]->AddSID( applicationUid);
       
  1771 	}
       
  1772 */		
       
  1773 }
       
  1774 
       
  1775 // -----------------------------------------------------------------------------
       
  1776 // CRepositorySession::RestoreDefaultsL()
       
  1777 // -----------------------------------------------------------------------------
       
  1778 //
       
  1779 
       
  1780 
       
  1781 void CRepositorySession::RestoreDefaultsL( const RMessage2& /*aMessage*/)
       
  1782 {
       
  1783 	using namespace IniConstants;
       
  1784 
       
  1785 	CRepositoryContent* romContent = ReadROMFileL( EFalse);
       
  1786 	CleanupStack::PushL( romContent);
       
  1787 	
       
  1788 	//delete RAM policies	
       
  1789 	delete iRepContent->iDefaultSetting;
       
  1790 	iRepContent->iRangeSettings.ResetAndDestroy();
       
  1791 	
       
  1792 	//..and append new policies from rom (default, range and mask policies)
       
  1793 	iRepContent->iDefaultSetting = romContent->iDefaultSetting;
       
  1794 	romContent->iDefaultSetting = 0;
       
  1795 
       
  1796 	for ( TInt i(0); i < romContent->iRangeSettings.Count(); i++)
       
  1797 	{
       
  1798 		CRangeSetting * setting = romContent->iRangeSettings[i];
       
  1799 		iRepContent->iRangeSettings.AppendL( setting);
       
  1800 		romContent->iRangeSettings.Remove( i--);
       
  1801 	}
       
  1802 			 
       
  1803 
       
  1804 		
       
  1805 	CleanupStack::PopAndDestroy( romContent);
       
  1806 	
       
  1807 }
       
  1808 
       
  1809 // -----------------------------------------------------------------------------
       
  1810 // CRepositorySession::SetSecurityIdForMaskL()
       
  1811 // -----------------------------------------------------------------------------
       
  1812 //
       
  1813 void CRepositorySession::SetSecurityIdForMaskL( const RMessage2& aMessage )
       
  1814 	{
       
  1815 	RDEBUG("CRepositorySession::SetSecurityIdForMaskL() ... ");
       
  1816 	
       
  1817 	//read range start from RMessage
       
  1818 	TUint32 compareValue;
       
  1819 	TPckg<TUint32> compareValuePack( compareValue );
       
  1820 	aMessage.ReadL(0, compareValuePack );
       
  1821 
       
  1822 	//read range end from RMessage
       
  1823 	TUint32 mask;
       
  1824 	TPckg<TUint32> maskPack( mask );
       
  1825 	aMessage.ReadL( 1, maskPack );
       
  1826 	
       
  1827 	//read SID from RMessage
       
  1828 	TUid applicationUid;
       
  1829 	TPckg<TUid> applicationUidPack( applicationUid );
       
  1830 	aMessage.ReadL( 2, applicationUidPack );
       
  1831 
       
  1832 	//Find range setting
       
  1833 	RPointerArray<CRangeSetting> settings;
       
  1834 	CleanupClosePushL( settings );
       
  1835 	iRepContent->CreateMaskSettingsL( settings, compareValue, mask );
       
  1836 	
       
  1837 	if( settings.Count() == 0 )
       
  1838 		{
       
  1839 		RDEBUG("	**** settings count was 0 !");
       
  1840 		}
       
  1841 	
       
  1842 	//Add SIDs for setting inside of the range
       
  1843 	for ( TInt i( 0 ); i < settings.Count(); i++ )
       
  1844 		{
       
  1845 		RDEBUG_3("	Adding sid: %d/%d", i, settings.Count() );
       
  1846 		User::LeaveIfError( settings[ i ]->AddSid( applicationUid ) );
       
  1847 		}
       
  1848 	
       
  1849 	CleanupStack::PopAndDestroy( &settings );	
       
  1850 	RDEBUG("CRepositorySession::SetSecurityIdForMaskL() ... DONE!");
       
  1851 	}
       
  1852 
       
  1853 
       
  1854 // -----------------------------------------------------------------------------
       
  1855 // CRepositorySession::RestoreMaskL()
       
  1856 // -----------------------------------------------------------------------------
       
  1857 //
       
  1858 void CRepositorySession::RestoreMaskL( const RMessage2& aMessage)
       
  1859 {
       
  1860 	__UHEAP_MARK;
       
  1861 
       
  1862 	//read range start from RMessage
       
  1863 	TUint32 compareValue;
       
  1864 	TPckg<TUint32> compareValuePack( compareValue);
       
  1865 	aMessage.ReadL(0, compareValuePack);
       
  1866 
       
  1867 	//read range end from RMessage
       
  1868 	TUint32 mask;
       
  1869 	TPckg<TUint32> maskPack( mask);
       
  1870 	aMessage.ReadL(1, maskPack);
       
  1871 	
       
  1872 	CRepositoryContent* romContent = ReadROMFileL( EFalse);
       
  1873 	CleanupStack::PushL( romContent);
       
  1874 
       
  1875 	//remove old mask settings
       
  1876 	for ( TInt i(0); i < iRepContent->iRangeSettings.Count(); i++)
       
  1877 	{
       
  1878 		CRangeSetting * setting = iRepContent->iRangeSettings[i];
       
  1879 		if ( setting->Type() != EMaskSetting )
       
  1880 		{
       
  1881 			continue;
       
  1882 		}
       
  1883 		
       
  1884 		if ( setting->iMask == mask && setting->iStart == compareValue  )
       
  1885 		{
       
  1886 			delete setting;
       
  1887 			iRepContent->iRangeSettings.Remove( i--);
       
  1888 		}
       
  1889 	}
       
  1890 			 
       
  1891 	//restore original settings from rom..
       
  1892 	for ( TInt i(0); i < romContent->iRangeSettings.Count(); i++)
       
  1893 	{
       
  1894 		CRangeSetting * setting = romContent->iRangeSettings[i];
       
  1895 		if ( setting->Type() != EMaskSetting )
       
  1896 		{
       
  1897 			continue;
       
  1898 		}
       
  1899 		
       
  1900 		if ( setting->iMask == mask && setting->iStart == compareValue  )
       
  1901 		{
       
  1902 			iRepContent->iRangeSettings.AppendL( setting);
       
  1903 			romContent->iRangeSettings.Remove( i--);
       
  1904 		}
       
  1905 	}
       
  1906 		
       
  1907 	CleanupStack::PopAndDestroy( romContent);	
       
  1908 	
       
  1909 	__UHEAP_MARKEND;
       
  1910 	
       
  1911 	
       
  1912 
       
  1913 }
       
  1914 
       
  1915 // -----------------------------------------------------------------------------
       
  1916 // CRepositorySession::RemoveBackupForMaskL()
       
  1917 // -----------------------------------------------------------------------------
       
  1918 //
       
  1919 void CRepositorySession::RemoveBackupForMaskL( const RMessage2& aMessage)
       
  1920 {
       
  1921 	//read range start from RMessage
       
  1922 	TUint32 compareValue;
       
  1923 	TPckg<TUint32> compareValuePack( compareValue);
       
  1924 	aMessage.ReadL(0, compareValuePack);
       
  1925 
       
  1926 	//read range end from RMessage
       
  1927 	TUint32 mask;
       
  1928 	TPckg<TUint32> maskPack( mask);
       
  1929 	aMessage.ReadL(1, maskPack);
       
  1930 	
       
  1931 	//Find range setting
       
  1932 	RPointerArray<CRangeMeta> settings;
       
  1933 	CleanupClosePushL( settings);
       
  1934 	iRepContent->CreateMaskBackupL( settings, compareValue, mask);
       
  1935 	
       
  1936 	//Add SIDs for setting inside of the range
       
  1937 	for ( TInt i(0); i < settings.Count(); i++)
       
  1938 	{
       
  1939 		//turn backup bit OFF.
       
  1940 		TUint32& meta( settings[i]->iMeta);	
       
  1941 		meta = meta & (~KBackupBitMask);
       
  1942 	}
       
  1943 	
       
  1944 	
       
  1945 	CleanupStack::PopAndDestroy( &settings);	
       
  1946 }
       
  1947 
       
  1948 // -----------------------------------------------------------------------------
       
  1949 // CRepositorySession::RestoreMaskBackupL()
       
  1950 // -----------------------------------------------------------------------------
       
  1951 //
       
  1952 void CRepositorySession::RestoreMaskBackupL( const RMessage2& aMessage)
       
  1953 {
       
  1954 	__UHEAP_MARK;
       
  1955 
       
  1956 	//read range start from RMessage
       
  1957 	TUint32 compareValue;
       
  1958 	TPckg<TUint32> compareValuePack( compareValue);
       
  1959 	aMessage.ReadL(0, compareValuePack);
       
  1960 
       
  1961 	//read range end from RMessage
       
  1962 	TUint32 mask;
       
  1963 	TPckg<TUint32> maskPack( mask);
       
  1964 	aMessage.ReadL(1, maskPack);
       
  1965 	
       
  1966 	CRepositoryContent* romContent = ReadROMFileL( EFalse);
       
  1967 	CleanupStack::PushL( romContent);
       
  1968 
       
  1969 	//remove old mask settings
       
  1970 	for ( TInt i(0); i < iRepContent->iRangeMetas.Count(); i++)
       
  1971 	{
       
  1972 		CRangeMeta * setting = iRepContent->iRangeMetas[i];
       
  1973 		if ( setting->Type() != EMaskSetting )
       
  1974 		{
       
  1975 			continue;
       
  1976 		}
       
  1977 		
       
  1978 		if ( setting->iMask == mask && setting->iStart == compareValue  )
       
  1979 		{
       
  1980 			delete setting;
       
  1981 			iRepContent->iRangeMetas.Remove( i--);
       
  1982 		}
       
  1983 	}
       
  1984 			 
       
  1985 	//restore original settings from rom..
       
  1986 	for ( TInt i(0); i < romContent->iRangeMetas.Count(); i++)
       
  1987 	{
       
  1988 		CRangeMeta * setting = romContent->iRangeMetas[i];
       
  1989 		if ( setting->Type() != EMaskSetting )
       
  1990 		{
       
  1991 			continue;
       
  1992 		}
       
  1993 		
       
  1994 		if ( setting->iMask == mask && setting->iStart == compareValue  )
       
  1995 		{
       
  1996 			iRepContent->iRangeMetas.AppendL( setting);
       
  1997 			romContent->iRangeMetas.Remove( i--);
       
  1998 		}
       
  1999 	}
       
  2000 		
       
  2001 	CleanupStack::PopAndDestroy( romContent);	
       
  2002 	
       
  2003 	__UHEAP_MARKEND;	
       
  2004 }
       
  2005 
       
  2006 
       
  2007 void CRepositorySession::RemoveBackupForRangeL( const RMessage2& aMessage)
       
  2008 {
       
  2009 	//read range start from RMessage
       
  2010 	TUint32 startValue;
       
  2011 	TPckg<TUint32> startPack( startValue);
       
  2012 	aMessage.ReadL(0, startPack);
       
  2013 
       
  2014 	//read range end from RMessage
       
  2015 	TUint32 endValue;
       
  2016 	TPckg<TUint32> endPack( endValue);
       
  2017 	aMessage.ReadL(1, endPack);
       
  2018 	
       
  2019 	//Find range setting
       
  2020 	RPointerArray<CRangeMeta> settings;
       
  2021 	CleanupClosePushL( settings);
       
  2022 	iRepContent->CreateRangeBackupL( settings, startValue, endValue);
       
  2023 	
       
  2024 	//Add SIDs for setting inside of the range
       
  2025 	for ( TInt i(0); i < settings.Count(); i++)
       
  2026 	{
       
  2027 		//turn backup bit OFF.
       
  2028 		TUint32& meta( settings[i]->iMeta);	
       
  2029 		meta = meta & (~KBackupBitMask);
       
  2030 	}
       
  2031 	
       
  2032 	
       
  2033 	CleanupStack::PopAndDestroy( &settings);	
       
  2034 }
       
  2035 
       
  2036 void CRepositorySession::RestoreRangeBackupL( const RMessage2& aMessage)
       
  2037 {
       
  2038 	__UHEAP_MARK;
       
  2039 
       
  2040 	//read range start from RMessage
       
  2041 	TUint32 startValue;
       
  2042 	TPckg<TUint32> startPack( startValue);
       
  2043 	aMessage.ReadL(0, startPack);
       
  2044 
       
  2045 	//read range end from RMessage
       
  2046 	TUint32 endValue;
       
  2047 	TPckg<TUint32> endPack( endValue);
       
  2048 	aMessage.ReadL(1, endPack);
       
  2049 	
       
  2050 	
       
  2051 	CRepositoryContent* romContent = ReadROMFileL( EFalse);
       
  2052 	CleanupStack::PushL( romContent);
       
  2053 
       
  2054 	//remove old mask settings
       
  2055 	for ( TInt i(0); i < iRepContent->iRangeMetas.Count(); i++)
       
  2056 	{
       
  2057 		CRangeMeta * setting = iRepContent->iRangeMetas[i];
       
  2058 		if ( setting->Type() != EMaskSetting )
       
  2059 		{
       
  2060 			continue;
       
  2061 		}
       
  2062 		
       
  2063 		if ( setting->iStart == startValue && setting->iEnd == endValue  )
       
  2064 		{
       
  2065 			delete setting;
       
  2066 			iRepContent->iRangeMetas.Remove( i--);
       
  2067 		}
       
  2068 	}
       
  2069 			 
       
  2070 	//restore original settings from rom..
       
  2071 	for ( TInt i(0); i < romContent->iRangeMetas.Count(); i++)
       
  2072 	{
       
  2073 		CRangeMeta * setting = romContent->iRangeMetas[i];
       
  2074 		if ( setting->Type() != EMaskSetting )
       
  2075 		{
       
  2076 			continue;
       
  2077 		}
       
  2078 		
       
  2079 		if ( setting->iStart == startValue && setting->iEnd == endValue  )
       
  2080 		{
       
  2081 			iRepContent->iRangeMetas.AppendL( setting);
       
  2082 			romContent->iRangeMetas.Remove( i--);
       
  2083 		}
       
  2084 	}
       
  2085 		
       
  2086 	CleanupStack::PopAndDestroy( romContent);	
       
  2087 	
       
  2088 	__UHEAP_MARKEND;	
       
  2089 }
       
  2090 
       
  2091 // -----------------------------------------------------------------------------
       
  2092 // CRepositorySession::RemoveDefaultBackup()
       
  2093 // -----------------------------------------------------------------------------
       
  2094 //
       
  2095 void CRepositorySession::RemoveDefaultBackup()
       
  2096 {
       
  2097 	using namespace IniConstants;
       
  2098 
       
  2099 	TIniFileHelper ini;
       
  2100 
       
  2101 	//turn backup bit OFF.
       
  2102 	TUint32& meta( iRepContent->iDefaultMeta);	
       
  2103 	meta = meta & (~KBackupBitMask);
       
  2104 
       
  2105 
       
  2106 	for ( TInt i(0); i < iRepContent->iRangeMetas.Count(); i++)
       
  2107 	{
       
  2108 		//turn backup bit OFF.
       
  2109 		TUint32& meta( iRepContent->iRangeMetas[i]->iMeta);	
       
  2110 		meta = meta & (~KBackupBitMask);
       
  2111 
       
  2112 	}	
       
  2113 }
       
  2114 
       
  2115 // -----------------------------------------------------------------------------
       
  2116 // CRepositorySession::RestoreDefaultBackupL()
       
  2117 // -----------------------------------------------------------------------------
       
  2118 //
       
  2119 void CRepositorySession::RestoreDefaultBackupL()
       
  2120 {
       
  2121 	using namespace IniConstants;
       
  2122 
       
  2123 	CRepositoryContent* romContent = ReadROMFileL( EFalse);
       
  2124 	CleanupStack::PushL( romContent);
       
  2125 	
       
  2126 	//delete RAM policies	
       
  2127 	iRepContent->iDefaultMeta = romContent->iDefaultMeta;
       
  2128 	
       
  2129 	for ( TInt i(0); i < romContent->iRangeMetas.Count(); i++)
       
  2130 	{
       
  2131 		CRangeMeta * setting = romContent->iRangeMetas[i];
       
  2132 		iRepContent->iRangeMetas.AppendL( setting);
       
  2133 		romContent->iRangeMetas.Remove( i--);
       
  2134 	}
       
  2135 			 
       
  2136 	CleanupStack::PopAndDestroy( romContent);
       
  2137 }
       
  2138 
       
  2139 // -----------------------------------------------------------------------------
       
  2140 // CRepositorySession::RestoreDefaultBackupL()
       
  2141 // -----------------------------------------------------------------------------
       
  2142 //
       
  2143 void CRepositorySession::CheckAccessL( const RMessage2& aMessage)
       
  2144 {
       
  2145 	using namespace IniConstants;
       
  2146 
       
  2147 	TUint32 settingid;
       
  2148 	TPckg<TUint32> idPack( settingid);
       
  2149 	aMessage.ReadL(0, idPack);
       
  2150 
       
  2151 	TAccessType type;	
       
  2152 	TPckg<TAccessType> atPack( type);
       
  2153 	aMessage.ReadL(1, atPack);
       
  2154 
       
  2155 	//Establish connection	
       
  2156 	TInt err = iFs.Connect();
       
  2157 	if( err != KErrNone )
       
  2158 		{
       
  2159 		RDEBUG_2("**** CRepositorySession::CheckAccess - failed to connect to RFs: %d", err );
       
  2160 		}
       
  2161 
       
  2162 	//delete old repositories
       
  2163 	delete iRepContent;
       
  2164 	iRepContent = 0;
       
  2165 
       
  2166 
       
  2167 	//Buffer for file name (without extension)
       
  2168 	iRepName.Zero();
       
  2169 	iRepName.NumFixedWidth( iRepositoryId.iUid, EHex, KUidLengthRep);
       
  2170 
       
  2171 	//open repository file (only headers)
       
  2172 	if ( KErrNone != ReadRAML( ETrue))
       
  2173 	{
       
  2174 		iRepContent = ReadROMFileL( EFalse);
       
  2175 	}
       
  2176 
       
  2177 	TBool retVal = iRepContent->CheckAccess( aMessage, settingid, type);		
       
  2178 	
       
  2179 	TPckg<TBool> rvPack( retVal);
       
  2180 	aMessage.WriteL(2, rvPack);
       
  2181 }