|
1 // Copyright (c) 1996-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 the License "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 // f32\sfile\sf_cache.cpp |
|
15 // |
|
16 // |
|
17 |
|
18 #include <e32std.h> |
|
19 #include <e32std_private.h> |
|
20 #include "sf_std.h" |
|
21 #include <e32uid.h> |
|
22 #include <e32wins.h> |
|
23 #include <f32file.h> |
|
24 #include "sf_cache.h" |
|
25 |
|
26 const TInt KMaxCachedDirectories=6; |
|
27 |
|
28 TInt RefreshDriveInfo(); |
|
29 void DestroyCachedDirectories(TPathListRecord* aPathRec); |
|
30 void DestroyCachedDirectory(TDriveNumber aDrive, TDirectoryCacheHeader* aDirCache); |
|
31 void DestroyCachedDirectory(TDriveNumber aDrive, TPathListRecord* aPathRec); |
|
32 |
|
33 const TInt KCacheHeapGranularity=0x0800; // Allocate heap in 2K chunks |
|
34 |
|
35 |
|
36 TBool gInitCacheCheckDrivesAndAddNotifications = EFalse; |
|
37 TBool gCacheCheckDrives = ETrue; |
|
38 |
|
39 |
|
40 // Cache per drive |
|
41 TDriveCacheHeader* gDriveFileNamesCache[KMaxDrives]; |
|
42 |
|
43 TPathListRecord* TPathListRecord::First; |
|
44 TPathListRecord* TPathListRecord::LastStatic; |
|
45 |
|
46 |
|
47 #if defined(_DEBUG) || defined(_DEBUG_RELEASE) |
|
48 void dumpCache() |
|
49 { |
|
50 TDriveNumber drive; |
|
51 for (drive=EDriveA; drive<=EDriveZ; ((TInt&)drive)++) |
|
52 { |
|
53 TDriveCacheHeader* pDH = gDriveFileNamesCache[drive]; |
|
54 RDebug::Printf("Dumping Drive %d", drive); |
|
55 if (!pDH) |
|
56 { |
|
57 RDebug::Printf("Drive %d not cached",drive); |
|
58 continue; |
|
59 } |
|
60 TDirectoryCacheHeader* p = pDH->iDirectoryList; |
|
61 for (; p; p=p->iNext) |
|
62 { |
|
63 RDebug::Printf(" Dumping directory %S", p->iPath->PathName()); |
|
64 TFileCacheRecord** pIndexes = p->iCache; |
|
65 TInt j; |
|
66 for(j=0; j<p->iRecordCount; j++) |
|
67 { |
|
68 TFileCacheRecord& f = *pIndexes[j]; |
|
69 TBuf8<20> en = _S8("Entry "); |
|
70 en.AppendNum(j); |
|
71 f.Dump((const char*)en.Ptr()); |
|
72 } |
|
73 } |
|
74 } |
|
75 } |
|
76 #endif |
|
77 |
|
78 CCacheNotifyDirChange::CCacheNotifyDirChange(TDriveNumber aDrive, TDirectoryCacheHeader& aDirHead) |
|
79 : CActive(EPriorityHigh), iDrive(aDrive), iDirHead(&aDirHead) |
|
80 {} |
|
81 |
|
82 CCacheNotifyDirChange::~CCacheNotifyDirChange() |
|
83 { |
|
84 __IF_DEBUG(Printf("~CCacheNotifyDirChange")); |
|
85 Cancel(); |
|
86 } |
|
87 |
|
88 void CCacheNotifyDirChange::DoCancel() |
|
89 { |
|
90 __IF_DEBUG(Printf("DoCancel")); |
|
91 gTheLoaderFs.NotifyChangeCancel(iStatus); |
|
92 } |
|
93 |
|
94 void CCacheNotifyDirChange::RunL() |
|
95 { |
|
96 __IF_DEBUG(Printf("Pop!! for drive %d path %S (%d)", iDrive, iDirHead->iPath->PathName(), iStatus.Int())); |
|
97 |
|
98 // unlink directory & delete it |
|
99 DestroyCachedDirectory(iDrive, iDirHead); |
|
100 gCacheCheckDrives = ETrue; |
|
101 } |
|
102 |
|
103 TInt CCacheNotifyDirChange::RegisterNotification(TPathListRecord* aPathRec, TNotifyType aType) |
|
104 // |
|
105 // Notification that a card has been mounted/removed |
|
106 // |
|
107 { |
|
108 TDriveUnit drive(iDrive); |
|
109 TFileName pathName(drive.Name()); |
|
110 pathName.Append('\\'); |
|
111 TInt dl = pathName.Length(); |
|
112 const TText* ppp = pathName.Ptr() + dl; |
|
113 const TDesC8& p8 = *aPathRec->PathName(); |
|
114 TInt p8l = p8.Length(); |
|
115 pathName.SetLength(dl + p8l); |
|
116 TPtr pp((TText*)ppp, 0, KMaxFileName - dl); |
|
117 pp.Copy(p8); |
|
118 if (pathName[dl + p8l -1] != '\\') |
|
119 pathName.Append('\\'); |
|
120 |
|
121 __IF_DEBUG(Printf("RegisterNotification for drive %d path %S", iDrive, &p8)); |
|
122 __IF_DEBUG(Print(_L("register notification for %S"), &pathName)); |
|
123 gTheLoaderFs.NotifyChange(aType, iStatus, pathName); |
|
124 SetActive(); |
|
125 __LDRTRACE({ \ |
|
126 if (iStatus != KRequestPending) \ |
|
127 RDebug::Printf("Notifier Immediate Complete %d", iStatus.Int()); \ |
|
128 }); |
|
129 return KErrNone; |
|
130 } |
|
131 |
|
132 TInt SetupNotify(TDriveNumber aDrive, TDirectoryCacheHeader& aDirHead) |
|
133 { |
|
134 if (aDirHead.iNotify != NULL) |
|
135 { |
|
136 __IF_DEBUG(Printf("SetupNotify!! notification already registered on drive %d path %S", aDrive, aDirHead.iPath->PathName())); |
|
137 return KErrNone; |
|
138 } |
|
139 |
|
140 __IF_DEBUG(Printf("SetupNotify!! on drive %d path %S", aDrive, aDirHead.iPath->PathName())); |
|
141 CCacheNotifyDirChange* pNotifier = new CCacheNotifyDirChange(aDrive, aDirHead); |
|
142 if (!pNotifier) |
|
143 return KErrNoMemory; |
|
144 aDirHead.iNotify = pNotifier; |
|
145 CActiveSchedulerLoader::Add(pNotifier); |
|
146 return pNotifier->RegisterNotification(aDirHead.iPath, ENotifyFile); |
|
147 } |
|
148 |
|
149 TInt AddNotifications() |
|
150 { |
|
151 TDriveNumber drive; |
|
152 for (drive=EDriveA; drive<EDriveZ; ((TInt&)drive)++) // Z always read-only so no notifiers required |
|
153 { |
|
154 TDriveCacheHeader* pDH = gDriveFileNamesCache[drive]; |
|
155 if (!pDH) |
|
156 continue; |
|
157 |
|
158 __IF_DEBUG(Printf("AddNotifications on drive %d att=0x%08x", drive, pDH->iDriveAtt)); |
|
159 |
|
160 TDirectoryCacheHeader* p = pDH->iDirectoryList; |
|
161 for (; p; p=p->iNext) |
|
162 { |
|
163 TInt r = SetupNotify(drive, *p); |
|
164 if (r != KErrNone) |
|
165 { |
|
166 DestroyCachedDirectory(drive, p); |
|
167 return r; |
|
168 } |
|
169 } |
|
170 } |
|
171 gCacheCheckDrives = EFalse; |
|
172 return KErrNone; |
|
173 } |
|
174 |
|
175 //=============================== TFileCacheRecord ================================== |
|
176 // |
|
177 TInt TFileCacheRecord::Order(const TFileCacheRecord& aL, const TFileCacheRecord& aR) |
|
178 { |
|
179 return aL.Name().CompareF(aR.Name()); |
|
180 } |
|
181 |
|
182 //=============================== TPathListRecord ================================== |
|
183 // |
|
184 #if defined(_DEBUG) || defined(_DEBUG_RELEASE) |
|
185 void dumpPathList() |
|
186 { |
|
187 RDebug::Printf("Dumping Pathlist"); |
|
188 TPathListRecord* p = TPathListRecord::First; |
|
189 TInt count=0; |
|
190 for (; p; p=p->iNext, ++count) |
|
191 RDebug::Printf("Pathlist pos %d %S",count,p->PathName()); |
|
192 } |
|
193 #endif |
|
194 |
|
195 _LIT8(KDirSystemPrograms, "System\\Programs"); |
|
196 _LIT8(KDirSystemLibs, "System\\Libs"); |
|
197 _LIT8(KDirSystemBin, "System\\Bin"); |
|
198 _LIT8(KDirSysBin, "Sys\\Bin"); |
|
199 |
|
200 TInt TPathListRecord::Init() |
|
201 { |
|
202 if (!AddToPathList(KDirSysBin, ETrue)) |
|
203 return KErrNoMemory; |
|
204 if(!PlatSec::ConfigSetting(PlatSec::EPlatSecEnforceSysBin)) |
|
205 { |
|
206 if (!AddToPathList(KDirSystemPrograms, ETrue)) |
|
207 return KErrNoMemory; |
|
208 if (!AddToPathList(KDirSystemLibs, ETrue)) |
|
209 return KErrNoMemory; |
|
210 if (!AddToPathList(KDirSystemBin, ETrue)) |
|
211 return KErrNoMemory; |
|
212 } |
|
213 return KErrNone; |
|
214 } |
|
215 |
|
216 void TPathListRecord::MoveToHead() |
|
217 { |
|
218 if (iKeep || this == LastStatic->iNext) |
|
219 return; |
|
220 TPathListRecord* p = First; |
|
221 // the record is always in the list |
|
222 for (; p && p->iNext!=this; p=p->iNext) {} |
|
223 __ASSERT_DEBUG(p, User::Invariant()); |
|
224 p->iNext = iNext; |
|
225 iNext = LastStatic->iNext; |
|
226 LastStatic->iNext = this; |
|
227 } |
|
228 |
|
229 TPathListRecord* TPathListRecord::FindPathNameInList(const TDesC8& aPath) |
|
230 { |
|
231 __IF_DEBUG(Printf("TPathListRecord::FindPathNameInList %S",&aPath)); |
|
232 TPathListRecord* p = DoFindPathNameInList(aPath); |
|
233 if (!p) |
|
234 p = AddToPathList(aPath, EFalse); |
|
235 return p; |
|
236 } |
|
237 |
|
238 TPathListRecord* TPathListRecord::DoFindPathNameInList(const TDesC8& aPath) |
|
239 { |
|
240 // Accesses pathname list to retrieve pathname record |
|
241 TPathListRecord* p = First; |
|
242 for (; p; p=p->iNext) |
|
243 { |
|
244 if (p->PathName()->CompareF(aPath) == 0) |
|
245 { |
|
246 p->MoveToHead(); |
|
247 return p; |
|
248 } |
|
249 } |
|
250 return NULL; |
|
251 } |
|
252 |
|
253 TPathListRecord* TPathListRecord::AddToPathList(const TDesC8& aPath, TBool aKeep) |
|
254 { |
|
255 __LDRTRACE(dumpPathList()); |
|
256 __IF_DEBUG(Printf("Pathlist adding path %S keep %d",&aPath,aKeep)); |
|
257 |
|
258 TPathListRecord* n = TPathListRecord::New(aPath, aKeep); |
|
259 if (!n) |
|
260 return NULL; |
|
261 TPathListRecord* p = First; |
|
262 TPathListRecord* q = NULL; |
|
263 TInt count = 0; |
|
264 for (; p && ++count<KMaxCachedDirectories; q=p, p=p->iNext) {} |
|
265 if (p) |
|
266 { |
|
267 // need to kill off entry pointed to by p |
|
268 __IF_DEBUG(Printf("In AddToPathList killing %S", p->PathName())); |
|
269 __ASSERT_ALWAYS(!aKeep, User::Invariant()); |
|
270 q->iNext = NULL; |
|
271 DestroyCachedDirectories(p); |
|
272 delete p; |
|
273 } |
|
274 |
|
275 if (aKeep) |
|
276 { |
|
277 // add to front of list |
|
278 n->iNext = First; |
|
279 First = n; |
|
280 if (!LastStatic) |
|
281 LastStatic = n; |
|
282 } |
|
283 else |
|
284 { |
|
285 // add new entry to front of dynamic list |
|
286 n->iNext = LastStatic->iNext; |
|
287 LastStatic->iNext = n; |
|
288 } |
|
289 |
|
290 // Refresh cache now we've added a new path |
|
291 gCacheCheckDrives = ETrue; |
|
292 |
|
293 __LDRTRACE(dumpPathList()); |
|
294 return n; |
|
295 } |
|
296 |
|
297 TPathListRecord* TPathListRecord::New(const TDesC8& aPath, TBool aKeep) |
|
298 { |
|
299 TInt l = aPath.Length(); |
|
300 TInt size = sizeof(TPathListRecord) + Align4(l) + sizeof(TDesC8); |
|
301 TPathListRecord* p = (TPathListRecord*)User::Alloc(size); |
|
302 if (p) |
|
303 { |
|
304 p->iNext = NULL; |
|
305 p->iKeep = aKeep; |
|
306 TInt* pb = (TInt*)(p+1); |
|
307 *pb = l; |
|
308 memcpy(pb+1, aPath.Ptr(), l); |
|
309 } |
|
310 return p; |
|
311 } |
|
312 |
|
313 void DestroyCachedDirectory(TDriveNumber aDrive, TPathListRecord* aPathRec) |
|
314 { |
|
315 __IF_DEBUG(Printf("DestroyCachedDirectory drive=%d path=%S",aDrive,aPathRec->PathName())); |
|
316 TDriveCacheHeader* pDH = gDriveFileNamesCache[aDrive]; |
|
317 TDirectoryCacheHeader* p = pDH->iDirectoryList; |
|
318 TDirectoryCacheHeader* q = 0; |
|
319 for (; p && p->iPath!=aPathRec; q=p, p=p->iNext) {} |
|
320 if (p) |
|
321 { |
|
322 __IF_DEBUG(Printf(" unlinking directory %S",p->iPath->PathName())); |
|
323 if (q) |
|
324 q->iNext = p->iNext; |
|
325 else |
|
326 pDH->iDirectoryList = p->iNext; |
|
327 __IF_DEBUG(Printf(" deleting directory %S",p->iPath->PathName())); |
|
328 delete p; |
|
329 } |
|
330 } |
|
331 |
|
332 void DestroyCachedDirectory(TDriveNumber aDrive, TDirectoryCacheHeader* aDirCache) |
|
333 { |
|
334 // First see if it contained in our list |
|
335 __IF_DEBUG(Printf("DestroyCachedDirectory drive=%d path=%S", aDrive, aDirCache->iPath->PathName())); |
|
336 |
|
337 TDriveCacheHeader* pDH = gDriveFileNamesCache[aDrive]; |
|
338 TDirectoryCacheHeader* p = pDH->iDirectoryList; |
|
339 TDirectoryCacheHeader* q = 0; |
|
340 for (; p && p!=aDirCache; q=p, p=p->iNext) {} |
|
341 if (p) |
|
342 { |
|
343 __IF_DEBUG(Printf(" unlinking directory %S",p->iPath->PathName())); |
|
344 if (q) |
|
345 q->iNext = p->iNext; |
|
346 else |
|
347 pDH->iDirectoryList = p->iNext; |
|
348 } |
|
349 __IF_DEBUG(Printf(" deleting directory %S", aDirCache->iPath->PathName())); |
|
350 delete aDirCache; |
|
351 } |
|
352 |
|
353 void DestroyCachedDirectories(TPathListRecord* aPathRec) |
|
354 { |
|
355 __IF_DEBUG(Printf("DestroyCachedDirectories %S",aPathRec->PathName())); |
|
356 TDriveNumber drive; |
|
357 for (drive=EDriveA; drive<=EDriveZ; ((TInt&)drive)++) |
|
358 { |
|
359 TDriveCacheHeader* pDH = gDriveFileNamesCache[drive]; |
|
360 if (pDH) |
|
361 { |
|
362 __IF_DEBUG(Printf("DestroyCachedDirectories drive=%d driveatt=0x%08x",drive,pDH->iDriveAtt)); |
|
363 DestroyCachedDirectory(drive, aPathRec); |
|
364 } |
|
365 } |
|
366 } |
|
367 |
|
368 //=============================== TCacheHeapList ================================== |
|
369 // |
|
370 TCacheHeapList::TCacheHeapList(TInt aSize) |
|
371 : iAllocated(Align4(sizeof(TCacheHeapList))), |
|
372 iSize(aSize) |
|
373 {} |
|
374 |
|
375 TAny* TCacheHeapList::Allocate(TInt aBytes) |
|
376 { |
|
377 __IF_DEBUG(Printf("TCacheHeapList::Allocate used=%d request=%d",iAllocated,aBytes)); |
|
378 TInt req = Align4(aBytes); |
|
379 if (iAllocated+req > iSize) |
|
380 return NULL; |
|
381 TAny* p = PtrAdd(this, iAllocated); |
|
382 iAllocated+=req; |
|
383 return p; |
|
384 } |
|
385 |
|
386 |
|
387 //=============================== TDriveCacheHeader================================== |
|
388 // |
|
389 TDriveCacheHeader::TDriveCacheHeader() |
|
390 : iDirectoryList(NULL) |
|
391 {} |
|
392 |
|
393 TDriveCacheHeader::~TDriveCacheHeader() |
|
394 { |
|
395 __IF_DEBUG(Printf("~TDriveCacheHeader")); |
|
396 while (iDirectoryList) |
|
397 { |
|
398 TDirectoryCacheHeader* p = iDirectoryList; |
|
399 iDirectoryList = p->iNext; |
|
400 __IF_DEBUG(Printf(" deleting directory %S",p->iPath->PathName())); |
|
401 delete p; |
|
402 } |
|
403 } |
|
404 |
|
405 TDirectoryCacheHeader* TDriveCacheHeader::FindDirCache(TPathListRecord* aPath) |
|
406 { |
|
407 TDirectoryCacheHeader* p = iDirectoryList; |
|
408 for (; p && p->iPath!=aPath; p=p->iNext) {} |
|
409 return p; |
|
410 } |
|
411 |
|
412 TInt TDriveCacheHeader::GetDirCache(TDirectoryCacheHeader*& aCache, TPathListRecord* aPath, const TDesC8& aDriveAndPath) |
|
413 { |
|
414 __IF_DEBUG(Printf(">GetDirCache %S", &aDriveAndPath)); |
|
415 aCache = FindDirCache(aPath); |
|
416 if (aCache) |
|
417 { |
|
418 __IF_DEBUG(Printf("<GetDirCache already exists %08x", aCache)); |
|
419 return KErrNone; |
|
420 } |
|
421 TDirectoryCacheHeader* p = new TDirectoryCacheHeader(aPath); |
|
422 if (!p) |
|
423 return KErrNoMemory; |
|
424 TInt r = p->PopulateFromDrive(aDriveAndPath); |
|
425 __IF_DEBUG(Printf("PopulateFromDrive ret %d", r)); |
|
426 if (r != KErrNoMemory && r != KErrLocked && iDriveNumber != EDriveZ) |
|
427 r = SetupNotify((TDriveNumber)iDriveNumber, *p); |
|
428 if (r == KErrNoMemory || r == KErrLocked) |
|
429 { |
|
430 delete p; |
|
431 return r; |
|
432 } |
|
433 // Ignore other errors and keep created entry anyway, so for things like 'path not found' |
|
434 // we have an empty directory cache which will get updated (via notification) if it get created. |
|
435 // This empty entry also allows for a quicker check the second time around. |
|
436 |
|
437 p->iNext = iDirectoryList; |
|
438 iDirectoryList = p; |
|
439 __IF_DEBUG(Printf("<GetDirCache new %08x", p)); |
|
440 aCache = p; |
|
441 return KErrNone; |
|
442 } |
|
443 |
|
444 //============================= TDirectoryCacheHeader================================ |
|
445 // |
|
446 TDirectoryCacheHeader::TDirectoryCacheHeader(TPathListRecord* aPath) |
|
447 : iFirstHeapBlock(NULL), iPath(aPath), iRecordCount(0), iCache(NULL), |
|
448 iNotPresent(ETrue), iNotify(NULL) |
|
449 {} |
|
450 |
|
451 TDirectoryCacheHeader::~TDirectoryCacheHeader() |
|
452 { |
|
453 __IF_DEBUG(Printf("~TDirectoryCacheHeader %S",iPath->PathName())); |
|
454 User::Free(iCache); |
|
455 delete iNotify; |
|
456 |
|
457 while (iFirstHeapBlock) |
|
458 { |
|
459 TCacheHeapList* p = iFirstHeapBlock; |
|
460 iFirstHeapBlock = p->iNext; |
|
461 User::Free(p); |
|
462 } |
|
463 } |
|
464 |
|
465 TInt TDirectoryCacheHeader::GetMoreHeap() |
|
466 { |
|
467 TAny* mem = User::Alloc(KCacheHeapGranularity); |
|
468 if (!mem) |
|
469 return KErrNoMemory; |
|
470 TCacheHeapList* n = new (mem) TCacheHeapList(KCacheHeapGranularity); |
|
471 n->iNext = iFirstHeapBlock; |
|
472 iFirstHeapBlock = n; |
|
473 return KErrNone; |
|
474 } |
|
475 |
|
476 TAny* TDirectoryCacheHeader::Allocate(const TInt aBytes) |
|
477 { |
|
478 TAny* p = iFirstHeapBlock ? iFirstHeapBlock->Allocate(aBytes) : NULL; |
|
479 if (!p) |
|
480 { |
|
481 if (GetMoreHeap()!=KErrNone) |
|
482 return NULL; |
|
483 p = iFirstHeapBlock->Allocate(aBytes); |
|
484 } |
|
485 return p; |
|
486 } |
|
487 |
|
488 TFileCacheRecord* TDirectoryCacheHeader::NewRecord(const TDesC8& aName, TUint32 aAttr, TUint32 aVer, const TEntry& aEntry) |
|
489 { |
|
490 TInt l = aName.Length(); |
|
491 TInt minsize = l + sizeof(TFileCacheRecord); |
|
492 TInt extra = aEntry.iSize >> 12; // allow for 8 exports per 4K of file |
|
493 if(extra>128) |
|
494 extra = 128; |
|
495 TInt size = (minsize + extra + 15) &~ 15; |
|
496 |
|
497 TFileCacheRecord* p = (TFileCacheRecord*)Allocate(size); |
|
498 if (p) |
|
499 { |
|
500 memclr(p, sizeof(TFileCacheRecord)); |
|
501 p->iAttr = aAttr; |
|
502 p->iModuleVersion = aVer; |
|
503 p->iExportDirCount = size - minsize; |
|
504 p->iNameLength = l; |
|
505 p->iExportDescType = (aEntry.iAtt & KEntryAttXIP) ? KImageHdr_ExpD_Xip : KImageHdr_ExpD_NoHoles; // for now |
|
506 p->iCacheStatus = 0; |
|
507 memcpy(p+1, aName.Ptr(), l); |
|
508 } |
|
509 return p; |
|
510 } |
|
511 |
|
512 TFileCacheRecord* TDirectoryCacheHeader::NewRecord(const TFileCacheRecord& aRecord, TInt aEDS) |
|
513 { |
|
514 TInt l = aRecord.Name().Length(); |
|
515 TInt minsize = l + sizeof(TFileCacheRecord); |
|
516 TInt extra = aEDS + 2; |
|
517 TInt size = (minsize + extra + 15) &~ 15; |
|
518 |
|
519 TFileCacheRecord* p = (TFileCacheRecord*)Allocate(size); |
|
520 if (p) |
|
521 { |
|
522 memcpy(p, &aRecord, minsize); |
|
523 memclr((TUint8*)p + minsize, size - minsize); |
|
524 } |
|
525 return p; |
|
526 } |
|
527 |
|
528 TInt TDirectoryCacheHeader::PopulateFromDrive(const TDesC8& aPathName) |
|
529 { |
|
530 // Wildcard searches through a named directory on a drive. |
|
531 // Creates and fills records to newly populate the drive cache.. |
|
532 |
|
533 // only want gen-u-ine files |
|
534 RDir d; |
|
535 TFileName dp; |
|
536 dp.Copy(aPathName); |
|
537 __IF_DEBUG(Print(_L("Opening Directory %S"), &dp)); |
|
538 |
|
539 iNotPresent=ETrue; |
|
540 TInt r=d.Open(gTheLoaderFs, dp, KEntryAttMatchExclude|KEntryAttDir|KEntryAttVolume); |
|
541 __IF_DEBUG(Printf("Returns %d", r)); |
|
542 if (r != KErrNone) |
|
543 { |
|
544 return r; |
|
545 } |
|
546 |
|
547 TEntryArray array; |
|
548 TInt sizeofIndexArray=0; |
|
549 TFileCacheRecord** pIndexes=NULL; |
|
550 TInt currentIndex=0; |
|
551 do { |
|
552 r=d.Read(array); |
|
553 if (r==KErrNone || r==KErrEof) |
|
554 { |
|
555 TInt count=array.Count(); |
|
556 if (count==0) |
|
557 break; |
|
558 TInt newSize=currentIndex+count; |
|
559 // Round alloc granularity up to the size of the cache cells, to avoid heap fragmentation when |
|
560 // indexing large dirs (z:\sys\bin) - interference effect is minimised by allocating in larger blocks and by |
|
561 // ensuring the freed memory can be reused for cache cells. See INC065949 for original defect. |
|
562 const TInt arrayGranularity = KCacheHeapGranularity + RHeap::EAllocCellSize; |
|
563 sizeofIndexArray = (sizeof(TFileCacheRecord*)*newSize + arrayGranularity - 1) / arrayGranularity * arrayGranularity; |
|
564 TFileCacheRecord** p=(TFileCacheRecord**)User::ReAlloc(pIndexes,sizeofIndexArray); |
|
565 if (!p) |
|
566 { |
|
567 r=KErrNoMemory; |
|
568 break; |
|
569 } |
|
570 pIndexes=p; |
|
571 TInt i=0; |
|
572 while (i<count) |
|
573 { |
|
574 const TEntry& e = array[i++]; |
|
575 TInt nl = e.iName.Length(); |
|
576 if (nl > KMaxKernelName) |
|
577 { |
|
578 __IF_DEBUG(Printf("Name length %d - too long", nl)); |
|
579 continue; |
|
580 } |
|
581 TBuf8<KMaxKernelName> n8; |
|
582 r = CheckedCollapse(n8, e.iName); |
|
583 if (r != KErrNone) |
|
584 { |
|
585 __IF_DEBUG(Printf("Non-ASCII name")); |
|
586 continue; |
|
587 } |
|
588 TFileNameInfo fni; |
|
589 r = fni.Set(n8, 0); |
|
590 if (r != KErrNone) |
|
591 { |
|
592 __IF_DEBUG(Printf("Bad name")); |
|
593 continue; |
|
594 } |
|
595 TBuf8<KMaxKernelName> rootname; |
|
596 fni.GetName(rootname, TFileNameInfo::EIncludeBaseExt); |
|
597 TUint32 attr = fni.VerLen() ? ECodeSegAttExpVer : 0; |
|
598 TFileCacheRecord* pR = NewRecord(rootname, attr, fni.Version(), e); |
|
599 if (!pR) |
|
600 { |
|
601 r=KErrNoMemory; |
|
602 break; |
|
603 } |
|
604 pIndexes[currentIndex] = pR; |
|
605 currentIndex++; |
|
606 } |
|
607 } |
|
608 } while (r==KErrNone); |
|
609 d.Close(); |
|
610 if(r==KErrNoMemory) |
|
611 return r; |
|
612 |
|
613 iNotPresent = EFalse; |
|
614 iCache = (TFileCacheRecord**)User::ReAlloc(pIndexes,sizeof(TFileCacheRecord*)*currentIndex); |
|
615 iRecordCount = currentIndex; |
|
616 if (currentIndex>1) |
|
617 { |
|
618 // don't sort an empty list, or a list with only 1 element |
|
619 RPointerArray<TFileCacheRecord> rarray(iCache, iRecordCount); |
|
620 rarray.Sort(&TFileCacheRecord::Order); |
|
621 } |
|
622 |
|
623 __LDRTRACE( { \ |
|
624 RDebug::Printf("RArray sorted"); \ |
|
625 TInt i; \ |
|
626 for (i=0; i<iRecordCount; i++) \ |
|
627 { \ |
|
628 TFileCacheRecord* f = iCache[i]; \ |
|
629 const TDesC8& name = f->Name(); \ |
|
630 RDebug::Printf("%d: Entry=%S att %08x ver %08x", i, &name, f->iAttr, f->iModuleVersion); \ |
|
631 } \ |
|
632 }); |
|
633 return KErrNone; |
|
634 } |
|
635 |
|
636 TInt RefreshDriveInfo() |
|
637 { |
|
638 // Find out what drives are present |
|
639 __IF_DEBUG(Printf(">RefreshDriveInfo")); |
|
640 TDriveList list; |
|
641 TInt r = gTheLoaderFs.DriveList(list); |
|
642 if (r!=KErrNone) |
|
643 { |
|
644 return r; |
|
645 } |
|
646 TDriveInfo d; |
|
647 TDriveNumber drive; |
|
648 for (drive=EDriveA; drive<=EDriveZ; ((TInt&)drive)++) |
|
649 { |
|
650 TInt att = list[drive]; |
|
651 if (att) |
|
652 { |
|
653 r = gTheLoaderFs.Drive(d,drive); |
|
654 if (r != KErrNone) |
|
655 continue; |
|
656 if ((d.iDriveAtt & KDriveAttRemote) || (d.iDriveAtt & KDriveAttSubsted)) |
|
657 continue; //Don't cache remote or substituted drives |
|
658 if (gDriveFileNamesCache[drive] == NULL) |
|
659 { |
|
660 __IF_DEBUG(Printf("In RefreshDriveInfo adding drive %d, drive= 0x%08x media=0x%08x", drive, d.iDriveAtt, d.iMediaAtt)); |
|
661 TDriveCacheHeader* pDH = new TDriveCacheHeader; |
|
662 if (!pDH) |
|
663 return KErrNoMemory; |
|
664 gDriveFileNamesCache[drive] = pDH; |
|
665 pDH->iDriveAtt = d.iDriveAtt; |
|
666 pDH->iDriveNumber = drive; |
|
667 } |
|
668 continue; |
|
669 } |
|
670 TDriveCacheHeader* pDH = gDriveFileNamesCache[drive]; |
|
671 delete pDH; |
|
672 gDriveFileNamesCache[drive] = NULL; |
|
673 } |
|
674 __IF_DEBUG(Printf("<RefreshDriveInfo")); |
|
675 return KErrNone; |
|
676 } |
|
677 |
|
678 // |
|
679 void InitializeFileNameCache() |
|
680 { |
|
681 __IF_DEBUG(Printf("InitializeFileNameCache")); |
|
682 gInitCacheCheckDrivesAndAddNotifications = EFalse; |
|
683 gCacheCheckDrives = ETrue; |
|
684 __ASSERT_ALWAYS(TPathListRecord::Init()==KErrNone, User::Invariant()); |
|
685 } |
|
686 |
|
687 TInt CheckLoaderCacheInit() |
|
688 { |
|
689 TInt r=KErrNone; |
|
690 if(RefreshZDriveCache) |
|
691 { |
|
692 // force z: drive cache to be refreshed |
|
693 __IF_DEBUG(Print(_L("Deleting z: drive cache\r\n"))); |
|
694 TDriveCacheHeader* pDH=gDriveFileNamesCache[EDriveZ]; |
|
695 delete pDH; |
|
696 gDriveFileNamesCache[EDriveZ]=NULL; |
|
697 gCacheCheckDrives=ETrue; |
|
698 RefreshZDriveCache=EFalse; |
|
699 } |
|
700 if (gCacheCheckDrives) |
|
701 { |
|
702 __IF_DEBUG(Printf("Refreshing cache")); |
|
703 r = RefreshDriveInfo(); // refreshing is a 'once-only' operation after setting |
|
704 gCacheCheckDrives = EFalse; // gCacheCheckDrives so as to prevent excessive refreshing |
|
705 } |
|
706 if (!gInitCacheCheckDrivesAndAddNotifications && StartupInitCompleted) |
|
707 { |
|
708 __IF_DEBUG(Printf("Refreshing cache and adding notifications after FS initialisation")); |
|
709 r = RefreshDriveInfo(); // this is to provide an extra refresh to explicitly find |
|
710 r = AddNotifications(); // all drives set up during FS initialisation |
|
711 gInitCacheCheckDrivesAndAddNotifications = ETrue; |
|
712 } |
|
713 return r; |
|
714 } |
|
715 |
|
716 TFileCacheRecordSearch::TFileCacheRecordSearch(const TDesC8& aSearchName) |
|
717 { |
|
718 iNameLength = aSearchName.Length(); |
|
719 if(iNameLength>sizeof(iSearchName)) |
|
720 User::Invariant(); |
|
721 memcpy(iSearchName, aSearchName.Ptr(), iNameLength); |
|
722 } |
|
723 |
|
724 TInt RImageFinder::SearchSingleDir() |
|
725 { |
|
726 __IF_DEBUG(Printf("SearchSingleDir %S drive %d", &iCurrentPath, iCurrentDrive)); |
|
727 |
|
728 TDriveCacheHeader* pDH = gDriveFileNamesCache[iCurrentDrive]; |
|
729 if (!pDH) |
|
730 { |
|
731 __IF_DEBUG(Printf("No such drive")); |
|
732 return KErrNone; |
|
733 } |
|
734 |
|
735 TInt pl = iCurrentPath.Length(); |
|
736 TInt start = 0; |
|
737 TInt len = pl; |
|
738 if (pl) |
|
739 { |
|
740 if (iCurrentPath[0] == '\\') |
|
741 start = 1, --len; |
|
742 if (len>0 && iCurrentPath[start + len - 1] == '\\') |
|
743 --len; |
|
744 } |
|
745 TPtrC8 path(iCurrentPath.Mid(start, len)); |
|
746 __IF_DEBUG(Printf("Normalised path %S", &path)); |
|
747 TChar c; |
|
748 RFs::DriveToChar(iCurrentDrive, c); |
|
749 TBuf8<KMaxFileName> drive_and_path = _S8("?:\\"); |
|
750 drive_and_path[0] = (TText8)c; |
|
751 drive_and_path.Append(path); |
|
752 if (drive_and_path[drive_and_path.Length()-1] != '\\') |
|
753 drive_and_path.Append('\\'); |
|
754 |
|
755 TPathListRecord* prec = TPathListRecord::FindPathNameInList(path); |
|
756 if (!prec) |
|
757 return KErrNoMemory; |
|
758 TDirectoryCacheHeader* dch = NULL; |
|
759 TInt r = pDH->GetDirCache(dch, prec, drive_and_path); |
|
760 if (r != KErrNone || dch->iNotPresent || dch->iRecordCount==0) |
|
761 { |
|
762 return r; |
|
763 } |
|
764 |
|
765 // set up to search for root name |
|
766 __IF_DEBUG(Printf("Search directory for %S", &iRootName)); |
|
767 TFileCacheRecordSearch search(iRootName); |
|
768 __LDRTRACE({const TDesC8& sr = search.Name(); RDebug::Printf("Search record %S", &sr);}); |
|
769 RPointerArray<TFileCacheRecord> rarray(dch->iCache, dch->iRecordCount); |
|
770 TInt first = rarray.SpecificFindInOrder(&search, &TFileCacheRecord::Order, EArrayFindMode_First); |
|
771 TInt last = rarray.SpecificFindInOrder(&search, &TFileCacheRecord::Order, EArrayFindMode_Last); |
|
772 __IF_DEBUG(Printf("First %d Last %d", first, last)); |
|
773 TInt ix; |
|
774 for (ix = first; ix < last; ++ix) |
|
775 { |
|
776 TFileCacheRecord* f = dch->iCache[ix]; |
|
777 RImageInfo img_info; |
|
778 r = KErrNone; |
|
779 if (!f->ExtrasValid()) |
|
780 { |
|
781 r = f->GetImageInfo(img_info, drive_and_path, dch, ix); |
|
782 if (r == KErrNoMemory) |
|
783 return r; |
|
784 f = dch->iCache[ix]; // may have been moved |
|
785 } |
|
786 if (r==KErrNone) |
|
787 { |
|
788 img_info = *f; |
|
789 r = Try(img_info, f->Name(), drive_and_path); |
|
790 if (r == KErrNoMemory) |
|
791 { |
|
792 img_info.Close(); |
|
793 return r; |
|
794 } |
|
795 f->iCacheStatus = img_info.iCacheStatus; |
|
796 } |
|
797 else |
|
798 RecordCorruptFile(); |
|
799 img_info.Close(); |
|
800 if (r==KErrCompletion) |
|
801 break; |
|
802 } |
|
803 return KErrNone; |
|
804 } |
|
805 |
|
806 // Populate the 'extras' in the cache record by reading the file header |
|
807 // aPathName must be of the form ?:\dir\...\dir\ so that a fully qualified file name is obtained by |
|
808 // appending the file name. |
|
809 TInt TFileCacheRecord::GetImageInfo(RImageInfo& aInfo, const TDesC8& aPathName, TDirectoryCacheHeader* aDirHead, TInt aIndex) |
|
810 { |
|
811 const TDesC8& rootname = Name(); |
|
812 TBuf8<KMaxFileName> fn = aPathName; |
|
813 TFileNameInfo fni; |
|
814 fni.Set(rootname, 0); |
|
815 fni.iVersion = iModuleVersion; |
|
816 TUint flags = (iAttr & ECodeSegAttExpVer) ? TFileNameInfo::EForceVer : 0; |
|
817 fni.GetName(fn, TFileNameInfo::EIncludeBaseExt | flags); |
|
818 __IF_DEBUG(Printf("Opening file %S", &fn)); |
|
819 TInt r = OpenFile8(aInfo.iFile, fn); |
|
820 __IF_DEBUG(Printf("Open file returns %d", r)); |
|
821 if (r != KErrNone) |
|
822 return r; |
|
823 TInt address = 0; |
|
824 r = aInfo.iFile.Seek(ESeekAddress, address); |
|
825 if (r!=KErrNotSupported) |
|
826 { |
|
827 __IF_DEBUG(Printf("ROM file at %08x", address)); |
|
828 TUint att; |
|
829 r = aInfo.iFile.Att(att); |
|
830 if (r!=KErrNone) |
|
831 { |
|
832 aInfo.Close(); |
|
833 return r; |
|
834 } |
|
835 if (att & KEntryAttXIP) |
|
836 { |
|
837 const TRomImageHeader& rih = *(const TRomImageHeader*)address; |
|
838 __IF_DEBUG(Printf("XIP file")); |
|
839 SetXIP(&rih); |
|
840 if ((iAttr & ECodeSegAttExpVer) && iModuleVersion != rih.iModuleVersion) |
|
841 { |
|
842 // version in file name does not match version in header |
|
843 aInfo.Close(); |
|
844 return KErrBadName; |
|
845 } |
|
846 iModuleVersion = rih.iModuleVersion; |
|
847 iS = rih.iS; |
|
848 iExportDirCount = (TUint16)rih.iExportDirCount; |
|
849 iExportDescType = (TUint8)KImageHdr_ExpD_Xip; |
|
850 iAttr |= rih.iFlags & (KRomImageFlagFixedAddressExe|KRomImageABIMask); |
|
851 __LDRTRACE(Dump("Cached Info XIP")); |
|
852 return KErrNone; |
|
853 } |
|
854 } |
|
855 TFileCacheRecord* t = this; |
|
856 r = E32ImageHeader::New(aInfo.iHeader, aInfo.iFile); |
|
857 if (r != KErrNone) |
|
858 { |
|
859 aInfo.Close(); |
|
860 __IF_DEBUG(Printf("E32ImageHeader::New returns %d", r)); |
|
861 return r; |
|
862 } |
|
863 E32ImageHeader* h = aInfo.iHeader; |
|
864 if ((iAttr & ECodeSegAttExpVer) && iModuleVersion != h->iModuleVersion) |
|
865 { |
|
866 // version in file name does not match version in header |
|
867 aInfo.Close(); |
|
868 return KErrBadName; |
|
869 } |
|
870 wordmove(iUid, &h->iUid1, sizeof(iUid)); |
|
871 iModuleVersion = h->ModuleVersion(); |
|
872 h->GetSecurityInfo(iS); |
|
873 iAttr |= (h->iFlags & ECodeSegAttFixed) | h->ABI(); |
|
874 if(h->iFlags&KImageNmdExpData) |
|
875 iAttr |= ECodeSegAttNmdExpData; |
|
876 TUint avail = iExportDirCount; |
|
877 iExportDirCount = (TUint16)h->iExportDirCount; |
|
878 iExportDescType = KImageHdr_ExpD_NoHoles; |
|
879 |
|
880 // get export description... |
|
881 E32ImageHeaderV* v = (E32ImageHeaderV*)h; |
|
882 iExportDescType = v->iExportDescType; |
|
883 TUint eds = v->iExportDescSize; |
|
884 if (eds + 2 > avail) |
|
885 { |
|
886 // must reallocate this entry |
|
887 t = aDirHead->NewRecord(*this, eds); |
|
888 if (!t) |
|
889 { |
|
890 aInfo.Close(); |
|
891 return KErrNoMemory; |
|
892 } |
|
893 aDirHead->iCache[aIndex] = t; |
|
894 } |
|
895 TUint8* xd = (TUint8*)t->ExportDescription(); |
|
896 xd[0] = (TUint8)eds; |
|
897 xd[1] = (TUint8)(eds>>8); |
|
898 memcpy(xd+2, v->iExportDesc, eds); |
|
899 |
|
900 __LDRTRACE(t->Dump("Cached Info")); |
|
901 return KErrNone; |
|
902 } |
|
903 |
|
904 RImageInfo& RImageInfo::operator=(const TFileCacheRecord& aRecord) |
|
905 { |
|
906 wordmove(this, &aRecord, sizeof(TImageInfo)); |
|
907 if (aRecord.ExtrasValid()) |
|
908 { |
|
909 if (aRecord.IsXIP()) |
|
910 { |
|
911 iRomImageHeader = aRecord.RomImageHeader(); |
|
912 wordmove(iUid, &iRomImageHeader->iUid1, sizeof(iUid)); |
|
913 } |
|
914 else if (iExportDescType != KImageHdr_ExpD_NoHoles) |
|
915 { |
|
916 const TUint8* xd = (TUint8*)aRecord.ExportDescription(); |
|
917 iExportDescSize = (TUint16)(xd[0] | (xd[1]<<8)); |
|
918 iExportDesc = xd + 2; |
|
919 } |
|
920 } |
|
921 return *this; |
|
922 } |
|
923 |
|
924 |
|
925 #if defined(_DEBUG) || defined(_DEBUG_RELEASE) |
|
926 extern void memory_dump(const TAny* a, TUint l); |
|
927 |
|
928 void TFileCacheRecord::Dump(const char* aTitle) |
|
929 { |
|
930 RDebug::Printf(aTitle); |
|
931 TUint32 uid1 = iUid[0]; |
|
932 TBool xip = (uid1 != (TUint32)KExecutableImageUidValue && uid1 != (TUint32)KDynamicLibraryUidValue); |
|
933 const TDesC8& name = Name(); |
|
934 if (xip) |
|
935 { |
|
936 const TRomImageHeader* rih = RomImageHeader(); |
|
937 RDebug::Printf("Name: %S Ver %08x Attr %08x", &name, rih->iModuleVersion, iAttr); |
|
938 RDebug::Printf("UIDS %08x %08x %08x SID %08x CAP %08x %08x", rih->iUid1, rih->iUid2, rih->iUid3, |
|
939 rih->iS.iSecureId, rih->iS.iCaps[1], rih->iS.iCaps[0]); |
|
940 RDebug::Printf("ExportDirCount %d ExportDescType %02x", rih->iExportDirCount, iExportDescType); |
|
941 } |
|
942 else |
|
943 { |
|
944 RDebug::Printf("Name: %S Ver %08x Attr %08x", &name, iModuleVersion, iAttr); |
|
945 RDebug::Printf("UIDS %08x %08x %08x SID %08x CAP %08x %08x", iUid[0], iUid[1], iUid[2], |
|
946 iS.iSecureId, iS.iCaps[1], iS.iCaps[0]); |
|
947 RDebug::Printf("ExportDirCount %d ExportDescType %02x", iExportDirCount, iExportDescType); |
|
948 if (iExportDescType != KImageHdr_ExpD_NoHoles) |
|
949 { |
|
950 const TUint8* xd = ExportDescription(); |
|
951 TUint eds = (xd[1]<<8) | xd[0]; |
|
952 RDebug::Printf("ExportDescSize %04x", eds); |
|
953 memory_dump(xd+2, eds); |
|
954 } |
|
955 } |
|
956 } |
|
957 #endif |
|
958 |
|
959 |