|
1 /* |
|
2 * Copyright (c) 2006-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 using namespace Swi; |
|
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, |
|
131 CIntegrityServices& aIntegrityServices, CIntegrityServices::TFailType aFailType) |
|
132 { |
|
133 RBuf filename; |
|
134 filename.CreateL(*iDirName, KMaxFileName); |
|
135 CleanupClosePushL(filename); |
|
136 DoWalkTreeL(filename, aFunc, aTypeFilter, aFs, aLoader, aFileMan, aIntegrityServices, aFailType); |
|
137 CleanupStack::PopAndDestroy(&filename); |
|
138 } |
|
139 |
|
140 CIntegrityTreeLeaf* CIntegrityTreeNode::DoAddNodeL(TDes& aFileName, TIntegrityServicesEvent aType, const TDesC& aOwningJournal) |
|
141 { |
|
142 TInt pathDividerPos = aFileName.Locate(KPathDelimiter); |
|
143 User::LeaveIfError(pathDividerPos); // The must be at least /this/ directory name |
|
144 |
|
145 TPtrC dirPtr(aFileName.Left(pathDividerPos)); |
|
146 if (dirPtr.CompareF(*iDirName) != 0) |
|
147 { |
|
148 User::Leave(KErrBadName); // this doesn't belong to us. |
|
149 } |
|
150 |
|
151 // remove the directory section of the path. |
|
152 aFileName.Delete(0, pathDividerPos+1); |
|
153 |
|
154 // Now, see if we need to pass it on up the chain. |
|
155 pathDividerPos = aFileName.Locate(KPathDelimiter); |
|
156 if (pathDividerPos == KErrNotFound) |
|
157 { |
|
158 // This is now just a file name, add a new file name leaf here. |
|
159 CIntegrityTreeLeaf* leaf = CIntegrityTreeLeaf::NewLC(aFileName, aType, aOwningJournal); |
|
160 iFiles.AppendL(leaf); |
|
161 CleanupStack::Pop(leaf); |
|
162 return leaf; |
|
163 } |
|
164 else |
|
165 { |
|
166 // There are more directories in the chain... See if we have an entry... |
|
167 dirPtr.Set(aFileName.Left(pathDividerPos)); |
|
168 |
|
169 TInt dirCount(iDirectories.Count()); |
|
170 for (TInt i = 0; i < dirCount; ++i) |
|
171 { |
|
172 if (dirPtr.CompareF(iDirectories[i]->Name()) == 0) |
|
173 { |
|
174 return iDirectories[i]->DoAddNodeL(aFileName, aType, aOwningJournal); |
|
175 } |
|
176 } |
|
177 // doesn't yet exist. Add it to the tree. |
|
178 CIntegrityTreeNode* node = CIntegrityTreeNode::NewLC(dirPtr); |
|
179 iDirectories.AppendL(node); |
|
180 CleanupStack::Pop(node); |
|
181 |
|
182 // pass the remaining path down the tree |
|
183 return node->DoAddNodeL(aFileName, aType, aOwningJournal); |
|
184 } |
|
185 } |
|
186 |
|
187 TInt CIntegrityTreeNode::DoFindNode(TDes& aFileName, TIntegrityServicesEvent aType) |
|
188 { |
|
189 TInt pathDividerPos = aFileName.Locate(KPathDelimiter); |
|
190 if (pathDividerPos < 0) |
|
191 return pathDividerPos; // The must be at least one path delimiter in the file name |
|
192 |
|
193 TPtrC dirPtr(aFileName.Left(pathDividerPos)); |
|
194 if (dirPtr.CompareF(*iDirName) != 0) |
|
195 { |
|
196 return KErrBadName; // this doesn't belong to us. |
|
197 } |
|
198 |
|
199 // remove the directory section of the path. |
|
200 aFileName.Delete(0, pathDividerPos+1); |
|
201 |
|
202 // Now, see if we need to pass it on up the chain. |
|
203 pathDividerPos = aFileName.Locate(KPathDelimiter); |
|
204 if (pathDividerPos == KErrNotFound) |
|
205 { |
|
206 TInt nameCount(iFiles.Count()); |
|
207 for (TInt i = 0; i < nameCount; ++i) |
|
208 { |
|
209 if (iFiles[i]->Type() == aType && |
|
210 iFiles[i]->Name().CompareF(aFileName) == 0) |
|
211 { |
|
212 return KErrNone; |
|
213 } |
|
214 } |
|
215 return KErrNotFound; |
|
216 } |
|
217 else |
|
218 { |
|
219 // There are more directories in the chain... See if we have an entry... |
|
220 dirPtr.Set(aFileName.Left(pathDividerPos)); |
|
221 |
|
222 TInt dirCount(iDirectories.Count()); |
|
223 for (TInt i = 0; i < dirCount; ++i) |
|
224 { |
|
225 if (dirPtr.CompareF(iDirectories[i]->Name()) == 0) |
|
226 { |
|
227 return iDirectories[i]->DoFindNode(aFileName, aType); |
|
228 } |
|
229 } |
|
230 return KErrNotFound; |
|
231 } |
|
232 } |
|
233 |
|
234 void CIntegrityTreeNode::DoWalkTreeL(TDes& aPath, TTreeWalkFunctionL aFunc, |
|
235 TIntegrityServicesEvent aTypeFilter, RFs& aFs, RLoader& aLoader, CFileMan& aFileMan, |
|
236 CIntegrityServices& aIntegrityServices, CIntegrityServices::TFailType aFailType) |
|
237 { |
|
238 aPath.Append(KPathDelimiter); |
|
239 |
|
240 // Feed all the files for this node to the walk function |
|
241 |
|
242 TInt fileCount(iFiles.Count()); |
|
243 TInt i; |
|
244 for (i = 0; i < fileCount; ++i) |
|
245 { |
|
246 CIntegrityTreeLeaf* leaf = iFiles[i]; |
|
247 if (leaf->Type() == aTypeFilter) |
|
248 { |
|
249 aIntegrityServices.SimulatePowerFailureL(aFailType, CIntegrityServices::EBeforeAction, leaf->Name()); |
|
250 aFunc(aPath, leaf, aFs, aLoader, aFileMan); |
|
251 aIntegrityServices.SimulatePowerFailureL(aFailType, CIntegrityServices::EAfterAction, leaf->Name()); |
|
252 } |
|
253 } |
|
254 |
|
255 // Walk the child nodes for this tree |
|
256 TInt len = aPath.Length(); |
|
257 TInt dirCount(iDirectories.Count()); |
|
258 for (i = 0; i < dirCount; ++i) |
|
259 { |
|
260 CIntegrityTreeNode* node = iDirectories[i]; |
|
261 aPath.SetLength(len); |
|
262 aPath.Append(node->Name()); |
|
263 node->DoWalkTreeL(aPath, aFunc, aTypeFilter, aFs, aLoader, aFileMan, aIntegrityServices, aFailType); |
|
264 } |
|
265 } |
|
266 |
|
267 CIntegrityTreeNode::CIntegrityTreeNode() |
|
268 { |
|
269 } |
|
270 |
|
271 void CIntegrityTreeNode::ConstructL(const TDesC& aDirectoryName) |
|
272 { |
|
273 iDirName = aDirectoryName.AllocL(); |
|
274 } |
|
275 |
|
276 CIntegrityTreeNode::~CIntegrityTreeNode() |
|
277 { |
|
278 delete iDirName; |
|
279 iDirectories.ResetAndDestroy(); |
|
280 iFiles.ResetAndDestroy(); |
|
281 } |