os/persistentdata/persistentstorage/centralrepository/common/src/cregen.cpp
changeset 0 bde4ae8d615e
     1.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     1.2 +++ b/os/persistentdata/persistentstorage/centralrepository/common/src/cregen.cpp	Fri Jun 15 03:10:57 2012 +0200
     1.3 @@ -0,0 +1,219 @@
     1.4 +// Copyright (c) 2008-2009 Nokia Corporation and/or its subsidiary(-ies).
     1.5 +// All rights reserved.
     1.6 +// This component and the accompanying materials are made available
     1.7 +// under the terms of "Eclipse Public License v1.0"
     1.8 +// which accompanies this distribution, and is available
     1.9 +// at the URL "http://www.eclipse.org/legal/epl-v10.html".
    1.10 +//
    1.11 +// Initial Contributors:
    1.12 +// Nokia Corporation - initial contribution.
    1.13 +//
    1.14 +// Contributors:
    1.15 +//
    1.16 +// Description:
    1.17 +//
    1.18 +
    1.19 +#include <s32file.h>
    1.20 +#include "cregen.h"
    1.21 +#include "heaprepos.h"
    1.22 +#include "srvparams.h"
    1.23 +
    1.24 +_LIT(KTmpExtension,"tmp");
    1.25 +
    1.26 +void CCreGenerator::CommitChangesToCreL(RFs& aFs,TUint8 aPersistVersion,CHeapRepository& aRep,const TDesC& aTargetFilePath)
    1.27 +	{
    1.28 +	HBufC* tmpFilePath=aTargetFilePath.AllocLC();
    1.29 +	TPtr tmpFilePathPtr(tmpFilePath->Des());
    1.30 +	tmpFilePathPtr.Replace(tmpFilePath->Length()-3,3,KTmpExtension());
    1.31 +
    1.32 +	CDirectFileStore* store = CDirectFileStore::ReplaceLC(aFs, *tmpFilePath,(EFileWrite | EFileShareExclusive));
    1.33 +	
    1.34 +	const TUid uid2	 = KNullUid ;														 
    1.35 +	store->SetTypeL(TUidType(KDirectFileStoreLayoutUid, uid2, KServerUid3)) ; 
    1.36 +
    1.37 +	// Write the stream index/dictionary as root stream within the store
    1.38 +	// so we can access it when we do a restore later on
    1.39 +	RStoreWriteStream rootStream ;
    1.40 +	TStreamId rootStreamId = rootStream.CreateLC(*store) ;
    1.41 +	ExternalizeCre(aPersistVersion,aRep, rootStream) ;
    1.42 +	rootStream.CommitL() ;
    1.43 +		
    1.44 +	CleanupStack::PopAndDestroy(&rootStream) ;
    1.45 +	store->SetRootL(rootStreamId);
    1.46 +	store->CommitL();
    1.47 +	CleanupStack::PopAndDestroy(store) ;	
    1.48 +	User::LeaveIfError(aFs.Replace(*tmpFilePath,aTargetFilePath));
    1.49 +	CleanupStack::PopAndDestroy();
    1.50 +	}
    1.51 +
    1.52 +void CCreGenerator::CreateReposFromCreL(RFs& aFs,CHeapRepository& aRep, const TDesC& aFilePath
    1.53 +#ifdef SYMBIAN_CENTREP_SUPPORT_MULTIROFS
    1.54 +	,TUint8& aCreVersion
    1.55 +#endif	
    1.56 +	)
    1.57 +	{
    1.58 +	RFile file;
    1.59 +	TInt err = file.Open(aFs,aFilePath,EFileRead|EFileShareReadersOnly);	
    1.60 +	
    1.61 +	if (err != KErrNone)
    1.62 +		{
    1.63 +		if(err ==KErrNotFound || err==KErrPathNotFound)
    1.64 +			User::Leave(KErrNotFound);
    1.65 +		else
    1.66 +			User::Leave(err);
    1.67 +		}
    1.68 +	
    1.69 +	CleanupClosePushL(file);
    1.70 +	
    1.71 +	CDirectFileStore* store = CDirectFileStore::FromLC (file);
    1.72 +	if(store->Type()[0] != KDirectFileStoreLayoutUid)
    1.73 +		{
    1.74 +		User::Leave(KErrCorrupt);
    1.75 +		}
    1.76 +
    1.77 +	// Get the root stream and attempt to read the index from it
    1.78 +	TStreamId rootStreamId = store->Root() ;
    1.79 +	RStoreReadStream rootStream ;
    1.80 +	rootStream.OpenLC(*store, rootStreamId);
    1.81 +	// Internalize the repository
    1.82 +	InternalizeCreL(aRep, rootStream
    1.83 +#ifdef SYMBIAN_CENTREP_SUPPORT_MULTIROFS
    1.84 +	,aCreVersion
    1.85 +#endif	
    1.86 +	);
    1.87 +	CleanupStack::PopAndDestroy(3, &file);
    1.88 +	}
    1.89 +
    1.90 +void CCreGenerator::ExternalizeCre(TUint8 aPersistVersion,const CHeapRepository& aRep, RWriteStream& aStream)
    1.91 +	{
    1.92 +	aStream << aPersistVersion;
    1.93 +	aStream << aRep.iUid ;
    1.94 +	aStream << aRep.iOwner ;
    1.95 +	
    1.96 +	TUint32 count=aRep.iSinglePolicies.Count();
    1.97 +	aStream << count;
    1.98 +	for(TUint32 i=0; i<count;i++)
    1.99 +		{
   1.100 +		aStream << *(aRep.iSinglePolicies[i]);
   1.101 +		}
   1.102 +	
   1.103 +	aStream << aRep.iRangePolicies ;
   1.104 +	aStream << aRep.iDefaultPolicy.GetReadAccessPolicy()->Package() ;
   1.105 +	aStream << aRep.iDefaultPolicy.GetWriteAccessPolicy()->Package() ;
   1.106 +#ifdef SYMBIAN_CENTREP_SUPPORT_MULTIROFS	
   1.107 +	if (aPersistVersion>=KPersistFormatSupportsIndMetaIndicator)
   1.108 +		{
   1.109 +		aStream << aRep.iDefaultPolicy.HighKey();
   1.110 +		aStream << aRep.iDefaultPolicy.KeyMask();		
   1.111 +		}
   1.112 +#endif		
   1.113 +		
   1.114 +	aStream << aRep.iDefaultMeta ;
   1.115 +	aStream << aRep.iRangeMeta ;
   1.116 +	aStream << aRep.iTimeStamp.Int64() ;
   1.117 +
   1.118 +	aStream << aRep.iSettings ;
   1.119 +	
   1.120 +	// Deleted settings
   1.121 +	count = aRep.iDeletedSettings.Count() ;
   1.122 +	aStream << count ;
   1.123 +	for (TUint32 i=0; i<count; i++)
   1.124 +		{
   1.125 +		aStream << aRep.iDeletedSettings[i];
   1.126 +		}
   1.127 +	}
   1.128 +
   1.129 +void CCreGenerator::InternalizeCreL(CHeapRepository& aRep, RReadStream& aStream
   1.130 +#ifdef SYMBIAN_CENTREP_SUPPORT_MULTIROFS
   1.131 +	,TUint8& aCreVersion
   1.132 +#endif	
   1.133 +	)
   1.134 +	{
   1.135 +	TUint8 version;
   1.136 +	aStream >> version;
   1.137 +	 
   1.138 + 	// Check the UID is the same as that expected.
   1.139 + 	// UID should always be correctly initialised by the time we reach
   1.140 + 	// this point.
   1.141 + 	TUid tempUid;
   1.142 + 	aStream >> tempUid;
   1.143 + 
   1.144 + 	if (tempUid != aRep.iUid)
   1.145 + 		{
   1.146 + 		#ifdef _DEBUG
   1.147 + 		RDebug::Print(_L("CSharedRepository::InternalizeCreL - expected UID (from filename) - %08X,  UID extracted from binary file - %08X "), aRep.iUid.iUid, tempUid.iUid);
   1.148 + 		#endif
   1.149 + 		User::Leave(KErrCorrupt);
   1.150 + 		}
   1.151 +  
   1.152 +	aStream >> aRep.iOwner ;
   1.153 + 
   1.154 +	TUint32 count;
   1.155 +	aStream >> count;
   1.156 +	for(TUint32 i=0; i<count;i++)
   1.157 +		{
   1.158 +		TSettingsAccessPolicy* singlePolicy = new(ELeave) TSettingsAccessPolicy;
   1.159 +		CleanupStack::PushL(singlePolicy);
   1.160 +		aStream >> *singlePolicy;
   1.161 +		aRep.iSinglePolicies.AppendL(singlePolicy);
   1.162 +		CleanupStack::Pop(singlePolicy);
   1.163 +		}
   1.164 + 	
   1.165 +	aRep.iRangePolicies.Reset();		
   1.166 +	aStream >> aRep.iRangePolicies ;
   1.167 +	
   1.168 +	HBufC8* securityPolicyPackage ;
   1.169 +	securityPolicyPackage = HBufC8::NewLC(aStream, 10000) ;
   1.170 +	TSecurityPolicy defaultReadPolicy;
   1.171 +	defaultReadPolicy.Set(securityPolicyPackage->Des()) ;
   1.172 +	CleanupStack::PopAndDestroy(securityPolicyPackage) ;
   1.173 +	securityPolicyPackage = HBufC8::NewLC(aStream, 10000) ;
   1.174 +	TSecurityPolicy defaultWritePolicy;
   1.175 +	defaultWritePolicy.Set(securityPolicyPackage->Des()) ;
   1.176 +	CleanupStack::PopAndDestroy(securityPolicyPackage) ;
   1.177 +#ifdef SYMBIAN_CENTREP_SUPPORT_MULTIROFS	
   1.178 +	aCreVersion=version;
   1.179 +	TUint32 highKey=0;
   1.180 +	TUint32 keyMask=0;
   1.181 +	if (aCreVersion>=KPersistFormatSupportsIndMetaIndicator)
   1.182 +		{
   1.183 +		aStream >> highKey;
   1.184 +		aStream >> keyMask;		
   1.185 +		}
   1.186 +	aRep.iDefaultPolicy=TSettingsAccessPolicy(defaultReadPolicy,defaultWritePolicy, KUnspecifiedKey,highKey,keyMask);		
   1.187 +#else				
   1.188 +	aRep.iDefaultPolicy=TSettingsAccessPolicy(defaultReadPolicy,defaultWritePolicy, KUnspecifiedKey);
   1.189 +#endif	
   1.190 +	aStream >> aRep.iDefaultMeta ;
   1.191 +	
   1.192 +	aRep.iRangeMeta.Reset();
   1.193 +	aStream >> aRep.iRangeMeta ;
   1.194 +	
   1.195 +	TInt64 timeStampInt ;
   1.196 +	aStream >> timeStampInt ;
   1.197 +	aRep.iTimeStamp = timeStampInt ;
   1.198 +
   1.199 +	aRep.iSettings.Reset() ;
   1.200 +	aStream >> aRep.iSettings ;
   1.201 + 	
   1.202 +	if (version >= KPersistFormatSupportsDeletedSettings)
   1.203 +		{
   1.204 +		// Deleted Settings 
   1.205 +		aStream >> count  ;
   1.206 +		for (TUint32 i=0; i<count; i++)
   1.207 +			{
   1.208 +			TUint32 keyValue ;
   1.209 +			aStream >> keyValue ;
   1.210 +			aRep.iDeletedSettings.InsertInUnsignedKeyOrderL(keyValue);
   1.211 +			}
   1.212 +		}
   1.213 + 
   1.214 +	// Set up access policies
   1.215 +	TInt numElements = aRep.iSettings.Count();
   1.216 +	for (TInt count = 0; count < numElements; count++)
   1.217 +		{
   1.218 +		TServerSetting* setting= &(aRep.iSettings[count]);
   1.219 +		TUint32 key=setting->Key();
   1.220 +		setting->SetAccessPolicy(aRep.GetFallbackAccessPolicy(key));
   1.221 +		}
   1.222 +	}