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 * CJournalFile implementation |
|
16 * |
|
17 */ |
|
18 |
|
19 |
|
20 /** |
|
21 @file |
|
22 @released |
|
23 @internalTechnology |
|
24 */ |
|
25 |
|
26 #include "journalfile.h" |
|
27 #include "usiflog.h" |
|
28 |
|
29 _LIT(KBackupExt, "bak"); // extension for removed files in backup location |
|
30 |
|
31 namespace Usif |
|
32 { |
|
33 CJournalFile* CJournalFile::NewL(RFs& aFs, RLoader& aLoader, const TDesC& aFileName, TInt aDrive) |
|
34 { |
|
35 CJournalFile* self = CJournalFile::NewLC(aFs, aLoader, aFileName, aDrive); |
|
36 CleanupStack::Pop(self); |
|
37 return self; |
|
38 } |
|
39 |
|
40 CJournalFile* CJournalFile::NewLC(RFs& aFs, RLoader& aLoader, const TDesC& aFileName, TInt aDrive) |
|
41 { |
|
42 CJournalFile* self = new(ELeave) CJournalFile(aFs, aLoader, aDrive); |
|
43 CleanupStack::PushL(self); |
|
44 self->ConstructL(aFileName); |
|
45 return self; |
|
46 } |
|
47 |
|
48 CJournalFile::CJournalFile(RFs& aFs, RLoader& aLoader, TInt aDrive) |
|
49 : iFs(aFs), iLoader(aLoader), iLastEvent(ENone), iDrive(aDrive) |
|
50 { |
|
51 } |
|
52 |
|
53 CJournalFile::~CJournalFile() |
|
54 { |
|
55 iWriteStream.Close(); |
|
56 delete iTreeRoot; |
|
57 } |
|
58 |
|
59 void CJournalFile::ConstructL(const TDesC& aFileName) |
|
60 { |
|
61 DEBUG_PRINTF2(_L("CJournalFile::ConstructL() - aFileName %S"), &aFileName); |
|
62 iJournalFileName = aFileName; |
|
63 |
|
64 TChar drive; |
|
65 User::LeaveIfError(iFs.DriveToChar(iDrive, drive)); |
|
66 |
|
67 TBuf<2> driveSpec; |
|
68 driveSpec.Append(drive); |
|
69 driveSpec.Append(KDriveDelimiter); |
|
70 iTreeRoot = CIntegrityTreeNode::NewL(driveSpec); |
|
71 |
|
72 // read any existing entries from this journal |
|
73 TRAPD(err, ReadL()); |
|
74 if(err != KErrNone) |
|
75 { |
|
76 DEBUG_PRINTF2(_L("CJournalFile::ConstructL() - CJournalFile::ReadL() has left with error %d"), err); |
|
77 // if necessary create the path |
|
78 if(err == KErrPathNotFound) |
|
79 { |
|
80 User::LeaveIfError(iFs.MkDirAll(iJournalFileName)); |
|
81 } |
|
82 else if (err != KErrNotFound) |
|
83 { |
|
84 DEBUG_PRINTF2(_L("CJournalFile::ConstructL() - Leaving with error %d"), err); |
|
85 User::Leave(err); |
|
86 } |
|
87 } |
|
88 // now prepare for writing |
|
89 PrepareToWriteL(); |
|
90 } |
|
91 |
|
92 void CJournalFile::ReadL() |
|
93 { |
|
94 RFileReadStream journalStream; |
|
95 User::LeaveIfError(journalStream.Open(iFs, iJournalFileName, EFileRead | EFileShareAny)); |
|
96 CleanupClosePushL(journalStream); |
|
97 |
|
98 // we need to check that any filenames read from the journal are on the |
|
99 // same drive as the journal itself |
|
100 TDriveUnit journalDrive(iJournalFileName); |
|
101 |
|
102 TInt err = KErrNone; |
|
103 while(err == KErrNone) |
|
104 { |
|
105 // read the next journal entry |
|
106 TRAP(err, ReadEntryL(journalStream, journalDrive)); |
|
107 if(err != KErrNone && err != KErrEof) |
|
108 { |
|
109 DEBUG_PRINTF2(_L("CJournalFile::ReadL() - CJournalFile::ReadEntryL() has left with error %d Leaving with this error!"), err); |
|
110 User::Leave(err); |
|
111 } |
|
112 } |
|
113 CleanupStack::PopAndDestroy(&journalStream); |
|
114 } |
|
115 |
|
116 void VerifyIsFileEventL(TIntegrityServicesEvent aEvent) |
|
117 { |
|
118 if (aEvent != ERemovedFile && |
|
119 aEvent != ETempFile && aEvent != EAddedFile) |
|
120 { |
|
121 User::Leave(KErrCorrupt); |
|
122 } |
|
123 } |
|
124 |
|
125 void VerifyIsEmptyOrFileEventL(TIntegrityServicesEvent aEvent) |
|
126 { |
|
127 if (aEvent == ENone) |
|
128 return; |
|
129 |
|
130 VerifyIsFileEventL(aEvent); |
|
131 } |
|
132 |
|
133 void CJournalFile::ReadEntryL(RFileReadStream& aJournalStream, TInt aDrive) |
|
134 { |
|
135 TIntegrityServicesEvent event = static_cast<TIntegrityServicesEvent> (aJournalStream.ReadInt32L()); |
|
136 |
|
137 HBufC* removedFile = NULL; |
|
138 HBufC* backupFile = NULL; |
|
139 HBufC* tempFile = NULL; |
|
140 HBufC* addedFile = NULL; |
|
141 |
|
142 switch(event) |
|
143 { |
|
144 case ERemovedFile: |
|
145 { |
|
146 VerifyIsEmptyOrFileEventL(iLastEvent); |
|
147 removedFile = HBufC::NewLC(aJournalStream, KMaxFileName); |
|
148 // should be followed by the backup event |
|
149 if(aJournalStream.ReadInt32L() != EBackupFile) |
|
150 { |
|
151 User::Leave(KErrCorrupt); |
|
152 } |
|
153 backupFile = HBufC::NewLC(aJournalStream, KMaxFileName); |
|
154 // make sure files are located on the same drive as the journal |
|
155 if(CJournalFile::CheckFileNameL(iFs, *removedFile) != aDrive |
|
156 || CJournalFile::CheckFileNameL(iFs, *backupFile) != aDrive) |
|
157 { |
|
158 User::Leave(KErrCorrupt); |
|
159 } |
|
160 |
|
161 CIntegrityTreeLeaf* removeLeaf = iTreeRoot->AddNodeL(*removedFile, ERemovedFile, iJournalFileName); |
|
162 CIntegrityTreeLeaf* backupLeaf = iTreeRoot->AddNodeL(*backupFile, EBackupFile, iJournalFileName); |
|
163 |
|
164 removeLeaf->SetPeer(backupLeaf); |
|
165 backupLeaf->SetPeer(removeLeaf); |
|
166 |
|
167 CleanupStack::PopAndDestroy(backupFile); |
|
168 CleanupStack::PopAndDestroy(removedFile); |
|
169 iBackupFilesCount++; |
|
170 iLastEvent = ERemovedFile; |
|
171 } |
|
172 break; |
|
173 |
|
174 case EBackupFile: |
|
175 // cannot have a backup file on its own |
|
176 User::Leave(KErrCorrupt); |
|
177 break; |
|
178 |
|
179 case ETempFile: |
|
180 VerifyIsEmptyOrFileEventL(iLastEvent); |
|
181 |
|
182 tempFile = HBufC::NewLC(aJournalStream, KMaxFileName); |
|
183 // make the file is located on the same drive as the journal |
|
184 if(CJournalFile::CheckFileNameL(iFs, *tempFile) != aDrive) |
|
185 { |
|
186 User::Leave(KErrCorrupt); |
|
187 } |
|
188 iTreeRoot->AddNodeL(*tempFile, ETempFile, iJournalFileName); |
|
189 CleanupStack::PopAndDestroy(tempFile); |
|
190 iLastEvent = ETempFile; |
|
191 break; |
|
192 |
|
193 case EAddedFile: |
|
194 VerifyIsEmptyOrFileEventL(iLastEvent); |
|
195 |
|
196 addedFile = HBufC::NewLC(aJournalStream, KMaxFileName); |
|
197 // make the file is located on the same drive as the journal |
|
198 if(CJournalFile::CheckFileNameL(iFs, *addedFile) != aDrive) |
|
199 { |
|
200 User::Leave(KErrCorrupt); |
|
201 } |
|
202 iTreeRoot->AddNodeL(*addedFile, EAddedFile, iJournalFileName); |
|
203 CleanupStack::PopAndDestroy(addedFile); |
|
204 iLastEvent = EAddedFile; |
|
205 break; |
|
206 |
|
207 case ECommitted: |
|
208 VerifyIsFileEventL(iLastEvent); |
|
209 |
|
210 iLastEvent = ECommitted; |
|
211 break; |
|
212 |
|
213 case EBackupFilesRemoved: |
|
214 if(iLastEvent != ECommitted) |
|
215 { |
|
216 User::Leave(KErrCorrupt); |
|
217 } |
|
218 iLastEvent = EBackupFilesRemoved; |
|
219 break; |
|
220 |
|
221 case EAddedFilesRemoved: |
|
222 if(iLastEvent != ECommitted) |
|
223 VerifyIsFileEventL(iLastEvent); // The last event can be only a commit, or one of the file events |
|
224 |
|
225 iLastEvent = EAddedFilesRemoved; |
|
226 break; |
|
227 |
|
228 case ETempFilesRemoved: |
|
229 if(iLastEvent != EBackupFilesRemoved && iLastEvent != ERemovedFilesRestored) |
|
230 { |
|
231 User::Leave(KErrCorrupt); |
|
232 } |
|
233 iLastEvent = ETempFilesRemoved; |
|
234 break; |
|
235 |
|
236 case ERemovedFilesRestored: |
|
237 if(iLastEvent != EAddedFilesRemoved) |
|
238 { |
|
239 User::Leave(KErrCorrupt); |
|
240 } |
|
241 iLastEvent = ERemovedFilesRestored; |
|
242 break; |
|
243 |
|
244 default: |
|
245 User::Leave(KErrCorrupt); |
|
246 break; |
|
247 } |
|
248 } |
|
249 |
|
250 void CJournalFile::PrepareToWriteL() |
|
251 { |
|
252 DEBUG_PRINTF2(_L("CJournalFile::PrepareToWriteL() iJournalFileName %S"), &iJournalFileName); |
|
253 RFile file; |
|
254 CleanupClosePushL(file); |
|
255 // try opening the journal if it already exists |
|
256 TEntry entry; |
|
257 TBool journalFileExists = (iFs.Entry(iJournalFileName, entry) == KErrNone); |
|
258 if (journalFileExists) |
|
259 { |
|
260 User::LeaveIfError(file.Open(iFs, iJournalFileName, EFileWrite | EFileShareAny)); |
|
261 } |
|
262 else |
|
263 { |
|
264 // journal does not exist, try creating one |
|
265 User::LeaveIfError(file.Create(iFs, iJournalFileName, EFileWrite | EFileShareAny)); |
|
266 } |
|
267 |
|
268 TInt fileSize; |
|
269 User::LeaveIfError(file.Size(fileSize)); |
|
270 |
|
271 // attach to end of file for writing |
|
272 iWriteStream.Attach(file, fileSize); |
|
273 CleanupStack::Pop(&file); // file ownership transfered to stream |
|
274 } |
|
275 |
|
276 void CJournalFile::EventL(TIntegrityServicesEvent aEvent, TBool aSerializeEventToJournal /* = ETrue*/ ) |
|
277 { |
|
278 DEBUG_PRINTF2(_L("CJournalFile::EventL() - aEvent %d"), aEvent); |
|
279 if (aSerializeEventToJournal) |
|
280 { |
|
281 iWriteStream.WriteInt32L(static_cast<TInt> (aEvent)); |
|
282 iWriteStream.CommitL(); |
|
283 } |
|
284 |
|
285 iLastEvent = aEvent; |
|
286 } |
|
287 |
|
288 void CJournalFile::AddL(const TDesC& aFileName) |
|
289 { |
|
290 DEBUG_PRINTF2(_L("CJournalFile::AddL() - aFileName %S"), &aFileName); |
|
291 iTreeRoot->AddNodeL(aFileName, EAddedFile, iJournalFileName); |
|
292 |
|
293 // write filename to journal |
|
294 iWriteStream.WriteInt32L(static_cast<TInt> (EAddedFile)); |
|
295 iWriteStream << aFileName; |
|
296 iWriteStream.CommitL(); |
|
297 |
|
298 iLastEvent = EAddedFile; |
|
299 } |
|
300 |
|
301 void CJournalFile::RemoveL(const TDesC& aFileName, TDes& aBackupFileName) |
|
302 { |
|
303 DEBUG_PRINTF2(_L("CJournalFile::RemoveL() - aFileName %S"), &aFileName); |
|
304 // Only journal removals if the file hasn't been added in the same |
|
305 // journalfile. |
|
306 TInt err = iTreeRoot->FindNode(aFileName, EAddedFile); |
|
307 if (err == KErrNotFound) |
|
308 { |
|
309 CIntegrityTreeLeaf* removalNode = iTreeRoot->AddNodeL(aFileName, ERemovedFile, iJournalFileName); |
|
310 |
|
311 // generate the next backup filename for this journal |
|
312 NextBackupFileNameL(aBackupFileName); |
|
313 |
|
314 CIntegrityTreeLeaf* backupNode = iTreeRoot->AddNodeL(aBackupFileName, EBackupFile, iJournalFileName); |
|
315 |
|
316 // set peers on both nodes |
|
317 removalNode->SetPeer(backupNode); |
|
318 backupNode->SetPeer(removalNode); |
|
319 |
|
320 // write filenames to journal |
|
321 iWriteStream.WriteInt32L(static_cast<TInt> (ERemovedFile)); |
|
322 iWriteStream << aFileName; |
|
323 iWriteStream.WriteInt32L(static_cast<TInt> (EBackupFile)); |
|
324 iWriteStream << aBackupFileName; |
|
325 iWriteStream.CommitL(); |
|
326 |
|
327 iLastEvent = ERemovedFile; |
|
328 } |
|
329 else |
|
330 { |
|
331 User::LeaveIfError(err); |
|
332 } |
|
333 } |
|
334 |
|
335 void CJournalFile::TemporaryL(const TDesC& aFileName) |
|
336 { |
|
337 DEBUG_PRINTF2(_L("CJournalFile::TemporaryL() - aFileName %S"), &aFileName); |
|
338 iTreeRoot->AddNodeL(aFileName, ETempFile, iJournalFileName); |
|
339 |
|
340 // write filename to journal |
|
341 iWriteStream.WriteInt32L(static_cast<TInt> (ETempFile)); |
|
342 iWriteStream << aFileName; |
|
343 iWriteStream.CommitL(); |
|
344 |
|
345 iLastEvent = ETempFile; |
|
346 } |
|
347 |
|
348 void CJournalFile::JournalOperationL(TTreeWalkFunctionL aFunc, TIntegrityServicesEvent aTypeFilter, |
|
349 CIntegrityServices::TFailType aFailType) |
|
350 { |
|
351 CFileMan* fileman = CFileMan::NewL(iFs); |
|
352 CleanupStack::PushL(fileman); |
|
353 iTreeRoot->WalkTreeL(aFunc, aTypeFilter, iFs, iLoader, *fileman, aFailType); |
|
354 CleanupStack::PopAndDestroy(fileman); |
|
355 } |
|
356 |
|
357 void CJournalFile::NextBackupFileNameL(TDes& aBackupFileName) |
|
358 { |
|
359 TParsePtrC fileNamePtr(iJournalFileName); |
|
360 aBackupFileName = fileNamePtr.DriveAndPath(); |
|
361 aBackupFileName.Append(fileNamePtr.Name()); |
|
362 aBackupFileName.Append(KPathDelimiter); |
|
363 aBackupFileName.AppendNumUC(iBackupFilesCount, EHex); |
|
364 aBackupFileName.Append(KExtDelimiter); |
|
365 aBackupFileName.Append(KBackupExt); |
|
366 |
|
367 iBackupFilesCount++; |
|
368 } |
|
369 |
|
370 |
|
371 TIntegrityServicesEvent CJournalFile::LastEvent() const |
|
372 { |
|
373 return iLastEvent; |
|
374 } |
|
375 |
|
376 TInt CJournalFile::Drive() |
|
377 { |
|
378 return iDrive; |
|
379 } |
|
380 |
|
381 void CJournalFile::Close() |
|
382 { |
|
383 iWriteStream.Close(); |
|
384 } |
|
385 |
|
386 /*static*/ TInt CJournalFile::CheckFileNameL(RFs&, const TDesC& aFileName) |
|
387 { |
|
388 TParse parse; |
|
389 |
|
390 User::LeaveIfError(parse.Set(aFileName, NULL, NULL)); |
|
391 |
|
392 if (!parse.DrivePresent()) |
|
393 User::Leave(KErrArgument); |
|
394 |
|
395 if (!parse.PathPresent()) |
|
396 User::Leave(KErrArgument); |
|
397 |
|
398 TDriveUnit drive(parse.Drive()); |
|
399 return drive; |
|
400 } |
|
401 }//End of namespace Usif |
|
402 |
|