installationservices/swtransactionservices/source/server/integritytree.cpp
branchRCL_3
changeset 26 8b7f4e561641
parent 25 7333d7932ef7
child 27 e8965914fac7
equal deleted inserted replaced
25:7333d7932ef7 26:8b7f4e561641
     1 /*
       
     2 * Copyright (c) 2008-2009 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 the License "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: 
       
    15 *
       
    16 */
       
    17 
       
    18 
       
    19 /**
       
    20  @file 
       
    21  @released
       
    22  @internalTechnology
       
    23 */
       
    24 
       
    25 #include "integritytree.h"
       
    26 
       
    27 #include "integrityservices.h"
       
    28 #include <f32file.h>
       
    29 
       
    30 namespace Usif
       
    31 {
       
    32 CIntegrityTreeLeaf* CIntegrityTreeLeaf::NewLC(const TDesC& aFileName, TIntegrityServicesEvent aType,
       
    33 	const TDesC& aOwningJournal)
       
    34 	{
       
    35 	CIntegrityTreeLeaf* self = new (ELeave) CIntegrityTreeLeaf(aType, aOwningJournal);
       
    36 	CleanupStack::PushL(self);
       
    37 	self->ConstructL(aFileName);
       
    38 	return self;
       
    39 	}
       
    40 	
       
    41 CIntegrityTreeLeaf::~CIntegrityTreeLeaf()
       
    42 	{
       
    43 	delete iFileName;
       
    44 	}
       
    45 
       
    46 CIntegrityTreeLeaf::CIntegrityTreeLeaf(TIntegrityServicesEvent aType, const TDesC& aOwningJournal)
       
    47 	: iType(aType), iOwningJournal(aOwningJournal)
       
    48 	{
       
    49 	}
       
    50 	
       
    51 void CIntegrityTreeLeaf::ConstructL(const TDesC& aFileName)	
       
    52 	{
       
    53 	iFileName = aFileName.AllocL();
       
    54 	}
       
    55 
       
    56 CIntegrityTreeLeaf* CIntegrityTreeLeaf::Peer()
       
    57 	{
       
    58 	return iPeer;
       
    59 	}
       
    60 	
       
    61 TIntegrityServicesEvent CIntegrityTreeLeaf::Type()
       
    62 	{
       
    63 	return iType;
       
    64 	}
       
    65 	
       
    66 const TDesC& CIntegrityTreeLeaf::Name()
       
    67 	{
       
    68 	return *iFileName;
       
    69 	}
       
    70 	
       
    71 const TDesC& CIntegrityTreeLeaf::Journal()
       
    72 	{
       
    73 	return iOwningJournal;
       
    74 	}
       
    75 	
       
    76 void CIntegrityTreeLeaf::SetPeer(CIntegrityTreeLeaf* aPeer)
       
    77 	{
       
    78 	iPeer = aPeer;
       
    79 	} 
       
    80 	
       
    81 CIntegrityTreeNode* CIntegrityTreeNode::NewL(const TDesC& aDirectoryName)
       
    82 	{
       
    83 	CIntegrityTreeNode* self = CIntegrityTreeNode::NewLC(aDirectoryName);
       
    84 	CleanupStack::Pop(self);
       
    85 	return self;
       
    86 	}
       
    87 
       
    88 CIntegrityTreeNode* CIntegrityTreeNode::NewLC(const TDesC& aDirectoryName)
       
    89 	{
       
    90 	CIntegrityTreeNode* self = new (ELeave) CIntegrityTreeNode;
       
    91 	CleanupStack::PushL(self);
       
    92 	self->ConstructL(aDirectoryName);
       
    93 	return self;
       
    94 	}
       
    95 	
       
    96 	
       
    97 const TDesC& CIntegrityTreeNode::Name()
       
    98 	{
       
    99 	return *iDirName;
       
   100 	}
       
   101 	
       
   102 CIntegrityTreeLeaf* CIntegrityTreeNode::AddNodeL(const TDesC& aFileName, TIntegrityServicesEvent aType, const TDesC& aOwningJournal)
       
   103 	{
       
   104 	RBuf filename;
       
   105 	filename.CreateL(aFileName, aFileName.Length());
       
   106 	CleanupClosePushL(filename);
       
   107 	CIntegrityTreeLeaf* leaf = DoAddNodeL(filename, aType, aOwningJournal);
       
   108 	CleanupStack::PopAndDestroy(&filename);
       
   109 	return leaf;
       
   110 	}
       
   111 
       
   112 TInt CIntegrityTreeNode::FindNode(const TDesC& aFileName, TIntegrityServicesEvent aType)
       
   113 	{
       
   114 	RBuf filename;
       
   115 	TInt found = 0;
       
   116 	TRAPD(err, 
       
   117 			filename.CreateL(aFileName, aFileName.Length());
       
   118 			CleanupClosePushL(filename);
       
   119 			found = DoFindNode(filename, aType);
       
   120 			CleanupStack::PopAndDestroy(&filename);
       
   121 		);
       
   122 	if (err != KErrNone)
       
   123 		{
       
   124 		return err;
       
   125 		}
       
   126 	return found;
       
   127 	}
       
   128 
       
   129 void CIntegrityTreeNode::WalkTreeL(TTreeWalkFunctionL aFunc, TIntegrityServicesEvent aTypeFilter,
       
   130 	RFs& aFs, RLoader& aLoader, CFileMan& aFileMan, CIntegrityServices::TFailType aFailType)
       
   131 	{
       
   132 	RBuf filename;
       
   133 	filename.CreateL(*iDirName, KMaxFileName);
       
   134 	CleanupClosePushL(filename);
       
   135 	DoWalkTreeL(filename, aFunc, aTypeFilter, aFs, aLoader, aFileMan, aFailType);
       
   136 	CleanupStack::PopAndDestroy(&filename);
       
   137 	}
       
   138 	
       
   139 CIntegrityTreeLeaf* CIntegrityTreeNode::DoAddNodeL(TDes& aFileName, TIntegrityServicesEvent aType, const TDesC& aOwningJournal)
       
   140 	{
       
   141 	TInt pathDividerPos = aFileName.Locate(KPathDelimiter);
       
   142 	User::LeaveIfError(pathDividerPos); // The must be at least /this/ directory name
       
   143 	
       
   144 	TPtrC dirPtr(aFileName.Left(pathDividerPos));
       
   145 	if (dirPtr.CompareF(*iDirName) != 0)
       
   146 		{
       
   147 		User::Leave(KErrBadName); // this doesn't belong to us.
       
   148 		}
       
   149 	
       
   150 	// remove the directory section of the path.
       
   151 	aFileName.Delete(0, pathDividerPos+1);
       
   152 	
       
   153 	// Now, see if we need to pass it on up the chain.
       
   154 	pathDividerPos = aFileName.Locate(KPathDelimiter);
       
   155 	if (pathDividerPos == KErrNotFound)
       
   156 		{
       
   157 		// This is now just a file name, add a new file name leaf here.
       
   158 		CIntegrityTreeLeaf* leaf = CIntegrityTreeLeaf::NewLC(aFileName, aType, aOwningJournal);
       
   159 		iFiles.AppendL(leaf);
       
   160 		CleanupStack::Pop(leaf);
       
   161 		return leaf;
       
   162 		}
       
   163 	else
       
   164 		{
       
   165 		// There are more directories in the chain... See if we have an entry...
       
   166 		dirPtr.Set(aFileName.Left(pathDividerPos));
       
   167 	 
       
   168 		TInt dirCount(iDirectories.Count());
       
   169 		for (TInt i = 0; i < dirCount; ++i)
       
   170 			{
       
   171 			if (dirPtr.CompareF(iDirectories[i]->Name()) == 0)
       
   172 				{
       
   173 				return iDirectories[i]->DoAddNodeL(aFileName, aType, aOwningJournal);
       
   174 				}
       
   175 			}
       
   176 		// doesn't yet exist. Add it to the tree.
       
   177 		CIntegrityTreeNode* node = CIntegrityTreeNode::NewLC(dirPtr);
       
   178 		iDirectories.AppendL(node);
       
   179 		CleanupStack::Pop(node);
       
   180 		
       
   181 		// pass the remaining path down the tree
       
   182 		return node->DoAddNodeL(aFileName, aType, aOwningJournal);
       
   183 		}
       
   184 	}
       
   185 	
       
   186 TInt CIntegrityTreeNode::DoFindNode(TDes& aFileName, TIntegrityServicesEvent aType)
       
   187 	{
       
   188 	TInt pathDividerPos = aFileName.Locate(KPathDelimiter);
       
   189 	if (pathDividerPos < 0)
       
   190 		return pathDividerPos; // The must be at least one path delimiter in the file name	
       
   191 	
       
   192 	TPtrC dirPtr(aFileName.Left(pathDividerPos));
       
   193 	if (dirPtr.CompareF(*iDirName) != 0)
       
   194 		{
       
   195 		return KErrBadName; // this doesn't belong to us.
       
   196 		}
       
   197 	
       
   198 	// remove the directory section of the path.
       
   199 	aFileName.Delete(0, pathDividerPos+1);
       
   200 	
       
   201 	// Now, see if we need to pass it on up the chain.
       
   202 	pathDividerPos = aFileName.Locate(KPathDelimiter);
       
   203 	if (pathDividerPos == KErrNotFound)
       
   204 		{
       
   205 		TInt nameCount(iFiles.Count());
       
   206 		for (TInt i = 0; i < nameCount; ++i)
       
   207 			{
       
   208 			if (iFiles[i]->Type() == aType &&
       
   209 				iFiles[i]->Name().CompareF(aFileName) == 0)
       
   210 				{
       
   211 				return KErrNone;
       
   212 				}
       
   213 			}
       
   214 		return KErrNotFound;
       
   215 		}
       
   216 	else
       
   217 		{
       
   218 		// There are more directories in the chain... See if we have an entry...
       
   219 		dirPtr.Set(aFileName.Left(pathDividerPos));
       
   220 	 
       
   221 		TInt dirCount(iDirectories.Count());
       
   222 		for (TInt i = 0; i < dirCount; ++i)
       
   223 			{
       
   224 			if (dirPtr.CompareF(iDirectories[i]->Name()) == 0)
       
   225 				{
       
   226 				return iDirectories[i]->DoFindNode(aFileName, aType);
       
   227 				}
       
   228 			}
       
   229 		return KErrNotFound;
       
   230 		}
       
   231 	}
       
   232 	
       
   233 void CIntegrityTreeNode::DoWalkTreeL(TDes& aPath, TTreeWalkFunctionL aFunc, 
       
   234 	TIntegrityServicesEvent aTypeFilter, RFs& aFs, RLoader& aLoader, CFileMan& aFileMan,
       
   235 	CIntegrityServices::TFailType aFailType)
       
   236 	{
       
   237 	aPath.Append(KPathDelimiter);
       
   238 	
       
   239 	// Feed all the files for this node to the walk function
       
   240 	
       
   241 	TInt fileCount(iFiles.Count());
       
   242 	TInt i;
       
   243 	for (i = 0; i < fileCount; ++i)
       
   244 		{
       
   245 		CIntegrityTreeLeaf* leaf = iFiles[i];
       
   246 		if (leaf->Type() == aTypeFilter)
       
   247 			{
       
   248 			CIntegrityServices::SimulatePowerFailureL(aFailType, CIntegrityServices::EBeforeAction, leaf->Name());
       
   249 			aFunc(aPath, leaf, aFs, aLoader, aFileMan);
       
   250 			CIntegrityServices::SimulatePowerFailureL(aFailType, CIntegrityServices::EAfterAction, leaf->Name());
       
   251 			}
       
   252 		}
       
   253 	
       
   254 	// Walk the child nodes for this tree	
       
   255 	TInt len = aPath.Length();
       
   256 	TInt dirCount(iDirectories.Count());
       
   257 	for (i = 0; i < dirCount; ++i)
       
   258 		{
       
   259 		CIntegrityTreeNode* node = iDirectories[i];
       
   260 		aPath.SetLength(len);
       
   261 		aPath.Append(node->Name());
       
   262 		node->DoWalkTreeL(aPath, aFunc, aTypeFilter, aFs, aLoader, aFileMan, aFailType);
       
   263 		}
       
   264 	}
       
   265 	
       
   266 CIntegrityTreeNode::CIntegrityTreeNode()
       
   267 	{
       
   268 	}
       
   269 	
       
   270 void CIntegrityTreeNode::ConstructL(const TDesC& aDirectoryName)
       
   271 	{
       
   272 	iDirName = aDirectoryName.AllocL();
       
   273 	}
       
   274 	
       
   275 CIntegrityTreeNode::~CIntegrityTreeNode()
       
   276 	{
       
   277 	delete iDirName;
       
   278 	iDirectories.ResetAndDestroy();
       
   279 	iFiles.ResetAndDestroy();
       
   280 	}
       
   281 }//End of namespace Usif