os/persistentdata/persistentstorage/dbms/security/SC_DomainLoader.cpp
author sl
Tue, 10 Jun 2014 14:32:02 +0200 (2014-06-10)
changeset 1 260cb5ec6c19
permissions -rw-r--r--
Update contrib.
     1 // Copyright (c) 2004-2009 Nokia Corporation and/or its subsidiary(-ies).
     2 // All rights reserved.
     3 // This component and the accompanying materials are made available
     4 // under the terms of "Eclipse Public License v1.0"
     5 // which accompanies this distribution, and is available
     6 // at the URL "http://www.eclipse.org/legal/epl-v10.html".
     7 //
     8 // Initial Contributors:
     9 // Nokia Corporation - initial contribution.
    10 //
    11 // Contributors:
    12 //
    13 // Description:
    14 // CPolicyDomainLoader class
    15 // 
    16 //
    17 
    18 #include <s32file.h>
    19 #include "D32Strings.h"
    20 #include "SC_StrmIn.h"
    21 #include "SC_DomainLoader.h"
    22 
    23 //This function returns ETrue if the drives in aPath1 and aPath2 are equal.
    24 TBool CheckDrivesL(const TDesC& aPath1, const TDesC& aPath2)
    25 	{
    26 	__ASSERT(aPath1.Length() > 0);
    27 	__ASSERT(aPath2.Length() > 0);
    28 	
    29 	TChar drvc1 = aPath1[0];
    30 	TInt drv1;
    31 	__LEAVE_IF_ERROR(RFs::CharToDrive(drvc1, drv1));
    32 	
    33 	TChar drvc2 = aPath2[0];
    34 	TInt drv2;
    35 	__LEAVE_IF_ERROR(RFs::CharToDrive(drvc2, drv2));
    36 	
    37 	return drv1 == drv2;
    38 	}
    39 
    40 namespace DBSC
    41 {
    42 
    43 /**
    44 @param aFs A reference to a file server session object
    45 @param aPolicyDir A reference to policy file sdirectory
    46 @param aPDCollection The created policy domain objects will be stored in this output parameter.
    47 */
    48 inline CPolicyDomainLoader::CPolicyDomainLoader(RFs& aFs, const TDesC& aPolicyDir, 
    49 												RMap<TInt, CPolicyDomain*>& aPDCollection) :
    50 	iFs(aFs),
    51 	iPolicyDir(aPolicyDir),
    52 	iPDCollection(aPDCollection)
    53 	{
    54 	__ASSERT(iPolicyDir.Length() > 0);
    55 	__ASSERT(iPDCollection.Count() == 0);
    56 	}
    57 
    58 /**
    59 Standard phase-one CPolicyDomainLoader factory method
    60 @param aFs A reference to a file server session object
    61 @param aPolicyDir A reference to policy file sdirectory
    62 @param aPDCollection The created policy domain objects will be stored in this output parameter.
    63 @return A pointer to just created CPolicyDomainLoader instance.
    64 @leave System-wide error codes, including KErrNoMemory
    65 */
    66 CPolicyDomainLoader* CPolicyDomainLoader::NewLC(RFs& aFs, const TDesC& aPolicyDir, 
    67 												RMap<TInt,CPolicyDomain*>& aPDCollection)
    68 	{
    69 	CPolicyDomainLoader* self = new (ELeave) CPolicyDomainLoader(aFs, aPolicyDir, aPDCollection);
    70 	CleanupStack::PushL(self);
    71 	self->ConstructL();
    72 	return self;
    73 	}
    74 
    75 /**
    76 */
    77 CPolicyDomainLoader::~CPolicyDomainLoader()
    78 	{
    79 	delete iFoundPolicyFiles;
    80 	}
    81 
    82 /**
    83 Standard phase-two CPolicyDomainLoader construction method.
    84 It does a search for binary policy files in the system and creates and stores
    85 a list of found ones in iFoundPolicyFiles data member.
    86 @leave System-wide error codes, including KErrNoMemory
    87 */
    88 void CPolicyDomainLoader::ConstructL()
    89 	{
    90 	TFindFile findFile(iFs);
    91 	TInt err = findFile.FindWildByDir(KPolicyFileExtMask, iPolicyDir, iFoundPolicyFiles);
    92 	if(err == KErrNone)
    93 		{//Check if the found drive is the same as the drive in iPolicyDir.
    94 		if(!::CheckDrivesL(findFile.File(), iPolicyDir))
    95 			{
    96 			err = KErrNotFound;
    97 			}
    98 		}
    99 	__LEAVE_IF_ERROR(err);
   100 	}
   101 
   102 /**
   103 The method creates the policy file path related to aPolicyFileIndex entry in
   104 iFoundPolicyFiles list of binary policy file names. The path will be stored
   105 in iPolicyFilePath data member.
   106 @param aPolicyFileIndex Index of binary policy file name in iFoundPolicyFiles list.
   107 @return The UID, extracted from binary policy file name.
   108 @leave System-wide error codes, including KErrNoMemory
   109 */
   110 TUid CPolicyDomainLoader::CreatePolicyFilePathL(TInt aPolicyFileIndex)
   111 	{
   112 	__ASSERT(aPolicyFileIndex < iFoundPolicyFiles->Count());
   113 	const ::TEntry& entry = (*iFoundPolicyFiles)[aPolicyFileIndex];
   114 	__LEAVE_IF_ERROR(iParser.Set(entry.iName, NULL, NULL));
   115 	TPtrC fileName = iParser.Name();
   116 	TUid domainUid = KNullUid;
   117 	TLex lex(fileName);
   118 	__LEAVE_IF_ERROR(lex.Val(*(TUint32*)&domainUid, EHex));
   119 	if(domainUid == KNullUid)
   120 		{
   121 		__LEAVE(KErrCorrupt);
   122 		}
   123 	iPolicyFilePath.Copy(iPolicyDir);
   124 	iPolicyFilePath.Append(entry.iName);
   125 	return domainUid;
   126 	}
   127 
   128 /**
   129 The method creates the policy domain object related to aPolicyFileIndex entry in
   130 iFoundPolicyFiles list of binary policy file names. The created policy domain object
   131 will be added to iPDCollection collection of policy domain objects.
   132 @param aPolicyFileIndex Index of binary policy file name in iFoundPolicyFiles list.
   133 @leave System-wide error codes, including KErrNoMemory
   134 */
   135 void CPolicyDomainLoader::DoCreatePolicyDomainL(TInt aPolicyFileIndex)
   136 	{
   137 	TUid domainUid = CreatePolicyFilePathL(aPolicyFileIndex);
   138 	RFileReadStream strm;
   139 	CleanupClosePushL(strm);
   140 	__LEAVE_IF_ERROR(strm.Open(iFs, iPolicyFilePath, EFileRead));
   141 	TPDStreamLoader in(strm);
   142 	CPolicyDomain* policyDomain = CPolicyDomain::NewLC(domainUid, in);
   143 	__ASSERT(policyDomain);
   144 	__LEAVE_IF_ERROR(iPDCollection.Insert(domainUid.iUid, policyDomain));
   145 	CleanupStack::Pop(policyDomain);
   146 	CleanupStack::PopAndDestroy(&strm);
   147 	}
   148 
   149 /**
   150 The method creates the policy domain object related to aPolicyFileIndex entry in
   151 iFoundPolicyFiles list of binary policy file names. The created policy domain object
   152 will be added to iPDCollection collection of policy domain objects.
   153 @param aPolicyFileIndex Index of binary policy file name in iFoundPolicyFiles list.
   154 @leave KErrNoMemory is the only possible leaving error code. The  rest of errors
   155                     will be suppressed.
   156 */
   157 void CPolicyDomainLoader::CreatePolicyDomainL(TInt aPolicyFileIndex)
   158 	{
   159 	TRAPD(err, DoCreatePolicyDomainL(aPolicyFileIndex));
   160 #ifdef _DEBUG
   161 	if(err != KErrNone)
   162 		{
   163 		_LIT(KText, "Failed to load policy file \"%S\"!\n");
   164 		TBuf<KMaxPath> buf;
   165 		buf.Format(KText, &iPolicyFilePath);
   166 		RDebug::Print(buf);
   167 		}
   168 #endif//_DEBUG
   169 	if(err == KErrNoMemory)
   170 		{
   171 		__LEAVE(err);
   172 		}
   173 	}
   174 
   175 /**
   176 The method creates a policy domain object for each binary policy file found in the system.
   177 The created objects will be added to iPDCollection collection of policy domain objects.
   178 @leave KErrNoMemory is the only possible leaving error code. The  rest of errors
   179                     will be suppressed.
   180 */
   181 void CPolicyDomainLoader::RunL()
   182 	{
   183 	__ASSERT(iFoundPolicyFiles);
   184 	for(TInt i=(iFoundPolicyFiles->Count()-1);i>-1;--i)
   185 		{
   186 		CreatePolicyDomainL(i);
   187 		}
   188 	}
   189 
   190 } //end of - namespace DBSC