sl@0
|
1 |
// Copyright (c) 1996-2009 Nokia Corporation and/or its subsidiary(-ies).
|
sl@0
|
2 |
// All rights reserved.
|
sl@0
|
3 |
// This component and the accompanying materials are made available
|
sl@0
|
4 |
// under the terms of the License "Eclipse Public License v1.0"
|
sl@0
|
5 |
// which accompanies this distribution, and is available
|
sl@0
|
6 |
// at the URL "http://www.eclipse.org/legal/epl-v10.html".
|
sl@0
|
7 |
//
|
sl@0
|
8 |
// Initial Contributors:
|
sl@0
|
9 |
// Nokia Corporation - initial contribution.
|
sl@0
|
10 |
//
|
sl@0
|
11 |
// Contributors:
|
sl@0
|
12 |
//
|
sl@0
|
13 |
// Description:
|
sl@0
|
14 |
// f32\sfat\ram_fat_table.cpp
|
sl@0
|
15 |
// FAT16 File Allocation Table classes implementation for the RAM media
|
sl@0
|
16 |
//
|
sl@0
|
17 |
//
|
sl@0
|
18 |
|
sl@0
|
19 |
/**
|
sl@0
|
20 |
@file
|
sl@0
|
21 |
@internalTechnology
|
sl@0
|
22 |
*/
|
sl@0
|
23 |
|
sl@0
|
24 |
//!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
|
sl@0
|
25 |
//!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
|
sl@0
|
26 |
//!!
|
sl@0
|
27 |
//!! WARNING!! DO NOT edit this file !! '\sfat' component is obsolete and is not being used. '\sfat32'replaces it
|
sl@0
|
28 |
//!!
|
sl@0
|
29 |
//!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
|
sl@0
|
30 |
//!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
|
sl@0
|
31 |
|
sl@0
|
32 |
|
sl@0
|
33 |
#include "sl_std.h"
|
sl@0
|
34 |
#include "sl_fatcache.h"
|
sl@0
|
35 |
#include "fat_table.h"
|
sl@0
|
36 |
|
sl@0
|
37 |
|
sl@0
|
38 |
//#######################################################################################################################################
|
sl@0
|
39 |
//# CRamFatTable class implementation
|
sl@0
|
40 |
//#######################################################################################################################################
|
sl@0
|
41 |
|
sl@0
|
42 |
/**
|
sl@0
|
43 |
Constructor, the RamFatTable allows disk compression by redirecting the FAT
|
sl@0
|
44 |
|
sl@0
|
45 |
@param aOwner Owning mount.
|
sl@0
|
46 |
*/
|
sl@0
|
47 |
CRamFatTable::CRamFatTable(CFatMountCB& aOwner)
|
sl@0
|
48 |
:CFatTable(aOwner)
|
sl@0
|
49 |
{
|
sl@0
|
50 |
iFatTablePos=aOwner.FirstFatSector()<<aOwner.SectorSizeLog2();
|
sl@0
|
51 |
iIndirectionTablePos=iFatTablePos+aOwner.FatSizeInBytes();
|
sl@0
|
52 |
}
|
sl@0
|
53 |
|
sl@0
|
54 |
/** factory method */
|
sl@0
|
55 |
CRamFatTable* CRamFatTable::NewL(CFatMountCB& aOwner)
|
sl@0
|
56 |
{
|
sl@0
|
57 |
__PRINT1(_L("CRamFatTable::NewL() drv:%d"),aOwner.DriveNumber());
|
sl@0
|
58 |
|
sl@0
|
59 |
CRamFatTable* pSelf = new (ELeave) CRamFatTable(aOwner);
|
sl@0
|
60 |
|
sl@0
|
61 |
CleanupStack::PushL(pSelf);
|
sl@0
|
62 |
pSelf->InitializeL();
|
sl@0
|
63 |
CleanupStack::Pop();
|
sl@0
|
64 |
|
sl@0
|
65 |
return pSelf;
|
sl@0
|
66 |
}
|
sl@0
|
67 |
|
sl@0
|
68 |
void CRamFatTable::InitializeL()
|
sl@0
|
69 |
{
|
sl@0
|
70 |
CFatTable::InitializeL();
|
sl@0
|
71 |
|
sl@0
|
72 |
ASSERT(iMediaAtt & KMediaAttVariableSize);
|
sl@0
|
73 |
|
sl@0
|
74 |
iFatTablePos=iOwner->FirstFatSector()<<iOwner->SectorSizeLog2();
|
sl@0
|
75 |
iIndirectionTablePos=iFatTablePos+iOwner->FatSizeInBytes();
|
sl@0
|
76 |
|
sl@0
|
77 |
//-- set RAM disk base
|
sl@0
|
78 |
TLocalDriveCapsV2 caps;
|
sl@0
|
79 |
TPckg<TLocalDriveCapsV2> capsPckg(caps);
|
sl@0
|
80 |
User::LeaveIfError(iOwner->LocalDrive()->Caps(capsPckg));
|
sl@0
|
81 |
|
sl@0
|
82 |
iRamDiskBase = caps.iBaseAddress;
|
sl@0
|
83 |
}
|
sl@0
|
84 |
|
sl@0
|
85 |
/**
|
sl@0
|
86 |
Remount the FAT table. This method call means that the media parameters wasn't changed,
|
sl@0
|
87 |
otherwise CFatMountCB::DoReMountL() would reject it.
|
sl@0
|
88 |
Just do some re-initialisation work.
|
sl@0
|
89 |
*/
|
sl@0
|
90 |
void CRamFatTable::ReMountL()
|
sl@0
|
91 |
{
|
sl@0
|
92 |
//-- re-initialise, actually
|
sl@0
|
93 |
ASSERT(iMediaAtt & KMediaAttVariableSize);
|
sl@0
|
94 |
ASSERT(FatType() == EFat16);
|
sl@0
|
95 |
|
sl@0
|
96 |
iFatTablePos=iOwner->FirstFatSector()<<iOwner->SectorSizeLog2();
|
sl@0
|
97 |
iIndirectionTablePos=iFatTablePos+iOwner->FatSizeInBytes();
|
sl@0
|
98 |
|
sl@0
|
99 |
//-- set RAM disk base
|
sl@0
|
100 |
TLocalDriveCapsV2 caps;
|
sl@0
|
101 |
TPckg<TLocalDriveCapsV2> capsPckg(caps);
|
sl@0
|
102 |
User::LeaveIfError(iOwner->LocalDrive()->Caps(capsPckg));
|
sl@0
|
103 |
|
sl@0
|
104 |
iRamDiskBase = caps.iBaseAddress;
|
sl@0
|
105 |
}
|
sl@0
|
106 |
|
sl@0
|
107 |
|
sl@0
|
108 |
/**
|
sl@0
|
109 |
Return the start address of the Ram Drive
|
sl@0
|
110 |
|
sl@0
|
111 |
@return start address of the Ram Drive
|
sl@0
|
112 |
*/
|
sl@0
|
113 |
TUint8 *CRamFatTable::RamDiskBase() const
|
sl@0
|
114 |
{
|
sl@0
|
115 |
return(iRamDiskBase);
|
sl@0
|
116 |
}
|
sl@0
|
117 |
|
sl@0
|
118 |
|
sl@0
|
119 |
/**
|
sl@0
|
120 |
Allocate a new cluster number
|
sl@0
|
121 |
|
sl@0
|
122 |
@return New cluster number
|
sl@0
|
123 |
*/
|
sl@0
|
124 |
TInt CRamFatTable::AllocateClusterNumber()
|
sl@0
|
125 |
{
|
sl@0
|
126 |
return(iOwner->MaxClusterNumber()-NumberOfFreeClusters());
|
sl@0
|
127 |
}
|
sl@0
|
128 |
|
sl@0
|
129 |
/**
|
sl@0
|
130 |
Write a value to the FAT (indirection table)
|
sl@0
|
131 |
|
sl@0
|
132 |
@param aFatIndex Cluster to write to
|
sl@0
|
133 |
@param aValue value to write to Fat
|
sl@0
|
134 |
@leave
|
sl@0
|
135 |
*/
|
sl@0
|
136 |
void CRamFatTable::WriteL(TUint32 aFatIndex, TUint32 aValue)
|
sl@0
|
137 |
{
|
sl@0
|
138 |
//__PRINT(_L("CRamFatTable::WriteL"));
|
sl@0
|
139 |
|
sl@0
|
140 |
__ASSERT_ALWAYS(aFatIndex>=2 && (aValue>=2 || aValue==0) && aValue<=0xFFFF,User::Leave(KErrCorrupt));
|
sl@0
|
141 |
TUint32 indirectCluster=aFatIndex;
|
sl@0
|
142 |
TUint32 indirectClusterNewVal=0;
|
sl@0
|
143 |
ReadIndirectionTable(indirectCluster);
|
sl@0
|
144 |
// If value in indirection table!=0 we assume we have already written to the indirection table
|
sl@0
|
145 |
// So just update the FAT table
|
sl@0
|
146 |
if (indirectCluster!=0 && aValue!=0)
|
sl@0
|
147 |
{
|
sl@0
|
148 |
WriteFatTable(aFatIndex,aValue);
|
sl@0
|
149 |
return;
|
sl@0
|
150 |
}
|
sl@0
|
151 |
// If value in indirection table is 0, we haven't written to it yet, though the memory has
|
sl@0
|
152 |
// already been allocated by the EnlargeL() function
|
sl@0
|
153 |
if (indirectCluster==0 && aValue!=0) // Assumes memory has already been allocated
|
sl@0
|
154 |
indirectClusterNewVal=AllocateClusterNumber();
|
sl@0
|
155 |
// Write aValue into aFaxIndex and indirectClusterNewVal into the corresponding position
|
sl@0
|
156 |
// in the indirection table
|
sl@0
|
157 |
WriteFatTable(aFatIndex,aValue,indirectClusterNewVal);
|
sl@0
|
158 |
}
|
sl@0
|
159 |
|
sl@0
|
160 |
/**
|
sl@0
|
161 |
Read the value of a cluster in the Fat
|
sl@0
|
162 |
|
sl@0
|
163 |
@param aFatIndex A cluster to read
|
sl@0
|
164 |
@leave
|
sl@0
|
165 |
@return The cluster value read
|
sl@0
|
166 |
*/
|
sl@0
|
167 |
|
sl@0
|
168 |
TUint32 CRamFatTable::ReadL(TUint32 aFatIndex) const
|
sl@0
|
169 |
{
|
sl@0
|
170 |
__ASSERT_ALWAYS(aFatIndex>=KFatFirstSearchCluster,User::Leave(KErrCorrupt));
|
sl@0
|
171 |
TUint clusterVal=*(TUint16*)(RamDiskBase()+PosInBytes(aFatIndex)+iFatTablePos);
|
sl@0
|
172 |
return(clusterVal);
|
sl@0
|
173 |
}
|
sl@0
|
174 |
|
sl@0
|
175 |
/**
|
sl@0
|
176 |
Write a value to the FAT and indirection table
|
sl@0
|
177 |
|
sl@0
|
178 |
@param aFatIndex Cluster number to write to
|
sl@0
|
179 |
@param aFatValue Cluster value for Fat
|
sl@0
|
180 |
@param anIndirectionValue Value for indirection table
|
sl@0
|
181 |
*/
|
sl@0
|
182 |
void CRamFatTable::WriteFatTable(TInt aFatIndex,TInt aFatValue,TInt anIndirectionValue)
|
sl@0
|
183 |
{
|
sl@0
|
184 |
TUint8* pos=RamDiskBase()+PosInBytes(aFatIndex);
|
sl@0
|
185 |
*(TUint16*)(pos+iFatTablePos)=(TUint16)aFatValue;
|
sl@0
|
186 |
*(TUint16*)(pos+iIndirectionTablePos)=(TUint16)anIndirectionValue;
|
sl@0
|
187 |
}
|
sl@0
|
188 |
|
sl@0
|
189 |
/**
|
sl@0
|
190 |
Write to just the fat table
|
sl@0
|
191 |
|
sl@0
|
192 |
@param aFatIndex Cluster number to write to
|
sl@0
|
193 |
@param aFatValue Cluster value for Fat
|
sl@0
|
194 |
*/
|
sl@0
|
195 |
void CRamFatTable::WriteFatTable(TInt aFatIndex,TInt aFatValue)
|
sl@0
|
196 |
{
|
sl@0
|
197 |
*(TUint16*)(RamDiskBase()+PosInBytes(aFatIndex)+iFatTablePos)=(TUint16)aFatValue;
|
sl@0
|
198 |
}
|
sl@0
|
199 |
|
sl@0
|
200 |
/**
|
sl@0
|
201 |
Write to just the fat table
|
sl@0
|
202 |
|
sl@0
|
203 |
@param aFatIndex Cluster number to write to
|
sl@0
|
204 |
@param aFatValue Value for indirection table
|
sl@0
|
205 |
*/
|
sl@0
|
206 |
void CRamFatTable::WriteIndirectionTable(TInt aFatIndex,TInt aFatValue)
|
sl@0
|
207 |
{
|
sl@0
|
208 |
*(TUint16*)(RamDiskBase()+PosInBytes(aFatIndex)+iIndirectionTablePos)=(TUint16)aFatValue;
|
sl@0
|
209 |
}
|
sl@0
|
210 |
|
sl@0
|
211 |
/**
|
sl@0
|
212 |
Find the real location of aCluster
|
sl@0
|
213 |
|
sl@0
|
214 |
@param aCluster Cluster to read, contians cluster value upon return
|
sl@0
|
215 |
*/
|
sl@0
|
216 |
void CRamFatTable::ReadIndirectionTable(TUint32& aCluster) const
|
sl@0
|
217 |
{
|
sl@0
|
218 |
aCluster=*(TUint16*)(RamDiskBase()+PosInBytes(aCluster)+iIndirectionTablePos);
|
sl@0
|
219 |
}
|
sl@0
|
220 |
|
sl@0
|
221 |
/**
|
sl@0
|
222 |
Copy memory in RAM drive area, unlocking required
|
sl@0
|
223 |
|
sl@0
|
224 |
@param aTrg Pointer to destination location
|
sl@0
|
225 |
@param aSrc Pointer to source location
|
sl@0
|
226 |
@param aLength Length of data to copy
|
sl@0
|
227 |
@return Pointer to end of data copied
|
sl@0
|
228 |
*/
|
sl@0
|
229 |
TUint8* CRamFatTable::MemCopy(TAny* aTrg,const TAny* aSrc,TInt aLength)
|
sl@0
|
230 |
{
|
sl@0
|
231 |
TUint8* p=Mem::Copy(aTrg,aSrc,aLength);
|
sl@0
|
232 |
return(p);
|
sl@0
|
233 |
}
|
sl@0
|
234 |
|
sl@0
|
235 |
/**
|
sl@0
|
236 |
Copy memory with filling the source buffer with zeroes. Target and source buffers can overlap.
|
sl@0
|
237 |
Used on RAMDrive srinking in order to wipe data from the file that is being deleted.
|
sl@0
|
238 |
|
sl@0
|
239 |
@param aTrg pointer to the target address
|
sl@0
|
240 |
@param aSrc pointer to the destination address
|
sl@0
|
241 |
@param aLength how many bytes to copy
|
sl@0
|
242 |
@return A pointer to a location aLength bytes beyond aTrg (i.e. the location aTrg+aLength).
|
sl@0
|
243 |
*/
|
sl@0
|
244 |
TUint8* CRamFatTable::MemCopyFillZ(TAny* aTrg, TAny* aSrc,TInt aLength)
|
sl@0
|
245 |
{
|
sl@0
|
246 |
//-- just copy src to the trg, the memory areas can overlap.
|
sl@0
|
247 |
TUint8* p=Mem::Copy(aTrg, aSrc, aLength);
|
sl@0
|
248 |
|
sl@0
|
249 |
//-- now zero-fill the source memory area taking into account possible overlap.
|
sl@0
|
250 |
TUint8* pSrc = static_cast<TUint8*>(aSrc);
|
sl@0
|
251 |
TUint8* pTrg = static_cast<TUint8*>(aTrg);
|
sl@0
|
252 |
|
sl@0
|
253 |
TUint8* pZFill = NULL; //-- pointer to the beginning of zerofilled area
|
sl@0
|
254 |
TInt zFillLen = 0; //-- a number of bytes to zero-fill
|
sl@0
|
255 |
|
sl@0
|
256 |
if(aTrg < aSrc)
|
sl@0
|
257 |
{
|
sl@0
|
258 |
if(pTrg+aLength < pSrc)
|
sl@0
|
259 |
{//-- target and source areas do not overlap
|
sl@0
|
260 |
pZFill = pSrc;
|
sl@0
|
261 |
zFillLen = aLength;
|
sl@0
|
262 |
}
|
sl@0
|
263 |
else
|
sl@0
|
264 |
{//-- target and source areas overlap, try not to corrupt the target area
|
sl@0
|
265 |
zFillLen = pSrc-pTrg;
|
sl@0
|
266 |
pZFill = pTrg+aLength;
|
sl@0
|
267 |
}
|
sl@0
|
268 |
}
|
sl@0
|
269 |
else
|
sl@0
|
270 |
{
|
sl@0
|
271 |
if(pSrc+aLength < pTrg)
|
sl@0
|
272 |
{//-- target and source areas do not overlap
|
sl@0
|
273 |
pZFill = pSrc;
|
sl@0
|
274 |
zFillLen = aLength;
|
sl@0
|
275 |
}
|
sl@0
|
276 |
else
|
sl@0
|
277 |
{//-- target and source areas overlap, try not to corrupt the target area
|
sl@0
|
278 |
zFillLen = pSrc+aLength-pTrg;
|
sl@0
|
279 |
pZFill = pSrc;
|
sl@0
|
280 |
}
|
sl@0
|
281 |
}
|
sl@0
|
282 |
|
sl@0
|
283 |
Mem::FillZ(pZFill, zFillLen);
|
sl@0
|
284 |
|
sl@0
|
285 |
return(p);
|
sl@0
|
286 |
}
|
sl@0
|
287 |
|
sl@0
|
288 |
/**
|
sl@0
|
289 |
Zero fill RAM area corresponding to the cluster number aCluster
|
sl@0
|
290 |
@param aCluster a cluster number to be zero-filled
|
sl@0
|
291 |
*/
|
sl@0
|
292 |
void CRamFatTable::ZeroFillCluster(TInt aCluster)
|
sl@0
|
293 |
{
|
sl@0
|
294 |
TLinAddr clusterPos= I64LOW(DataPositionInBytes(aCluster));
|
sl@0
|
295 |
Mem::FillZ(iRamDiskBase+clusterPos, 1<< iOwner->ClusterSizeLog2());
|
sl@0
|
296 |
}
|
sl@0
|
297 |
|
sl@0
|
298 |
|
sl@0
|
299 |
/**
|
sl@0
|
300 |
Return the location of a Cluster in the data section of the media
|
sl@0
|
301 |
|
sl@0
|
302 |
@param aCluster to find location of
|
sl@0
|
303 |
@return Byte offset of the cluster data
|
sl@0
|
304 |
*/
|
sl@0
|
305 |
TInt64 CRamFatTable::DataPositionInBytes(TUint32 aCluster) const
|
sl@0
|
306 |
{
|
sl@0
|
307 |
//__PRINT(_L("CRamFatTable::DataPositionInBytes"));
|
sl@0
|
308 |
ReadIndirectionTable(aCluster);
|
sl@0
|
309 |
return(aCluster<<iOwner->ClusterSizeLog2());
|
sl@0
|
310 |
}
|
sl@0
|
311 |
|
sl@0
|
312 |
/**
|
sl@0
|
313 |
Allocate and mark as EOF a single cluster as close as possible to aNearestCluster,
|
sl@0
|
314 |
calls base class implementation but must Enlarge the RAM drive first. Allocated cluster RAM area will be zero-filled.
|
sl@0
|
315 |
|
sl@0
|
316 |
@param aNearestCluster Cluster the new cluster should be nearest to
|
sl@0
|
317 |
@leave System wide error codes
|
sl@0
|
318 |
@return The cluster number allocated
|
sl@0
|
319 |
*/
|
sl@0
|
320 |
TUint32 CRamFatTable::AllocateSingleClusterL(TUint32 aNearestCluster)
|
sl@0
|
321 |
{
|
sl@0
|
322 |
__PRINT(_L("CRamFatTable::AllocateSingleClusterL"));
|
sl@0
|
323 |
iOwner->EnlargeL(1<<iOwner->ClusterSizeLog2()); // First enlarge the RAM drive
|
sl@0
|
324 |
TInt fileAllocated=CFatTable::AllocateSingleClusterL(aNearestCluster); // Now update the free cluster and fat/fit
|
sl@0
|
325 |
ZeroFillCluster(fileAllocated); //-- zero-fill allocated cluster
|
sl@0
|
326 |
return(fileAllocated);
|
sl@0
|
327 |
}
|
sl@0
|
328 |
|
sl@0
|
329 |
|
sl@0
|
330 |
/**
|
sl@0
|
331 |
Extend a file or directory cluster chain, enlarging RAM drive first. Allocated clusters are zero-filled.
|
sl@0
|
332 |
Leaves if there are no free clusters (the disk is full).
|
sl@0
|
333 |
Note that method now doesn't call CFatTable::ExtendClusterListL() from its base class, be careful making changes there.
|
sl@0
|
334 |
|
sl@0
|
335 |
@param aNumber number of clusters to allocate
|
sl@0
|
336 |
@param aCluster starting cluster number / ending cluster number after
|
sl@0
|
337 |
@leave KErrDiskFull + system wide error codes
|
sl@0
|
338 |
*/
|
sl@0
|
339 |
void CRamFatTable::ExtendClusterListL(TUint32 aNumber,TInt& aCluster)
|
sl@0
|
340 |
{
|
sl@0
|
341 |
__PRINT(_L("CRamFatTable::ExtendClusterListL"));
|
sl@0
|
342 |
__ASSERT_DEBUG(aNumber>0,Fault(EFatBadParameter));
|
sl@0
|
343 |
|
sl@0
|
344 |
iOwner->EnlargeL(aNumber<<iOwner->ClusterSizeLog2());
|
sl@0
|
345 |
|
sl@0
|
346 |
while(aNumber && GetNextClusterL(aCluster))
|
sl@0
|
347 |
aNumber--;
|
sl@0
|
348 |
|
sl@0
|
349 |
if(!aNumber)
|
sl@0
|
350 |
return;
|
sl@0
|
351 |
|
sl@0
|
352 |
if (NumberOfFreeClusters() < aNumber)
|
sl@0
|
353 |
{
|
sl@0
|
354 |
__PRINT(_L("CRamFatTable::ExtendClusterListL - leaving KErrDirFull"));
|
sl@0
|
355 |
User::Leave(KErrDiskFull);
|
sl@0
|
356 |
}
|
sl@0
|
357 |
|
sl@0
|
358 |
while (aNumber--)
|
sl@0
|
359 |
{
|
sl@0
|
360 |
const TInt freeCluster=FindClosestFreeClusterL(aCluster);
|
sl@0
|
361 |
|
sl@0
|
362 |
WriteFatEntryEofL(freeCluster); // Must write EOF for FindClosestFreeCluster to work again
|
sl@0
|
363 |
DecrementFreeClusterCount(1);
|
sl@0
|
364 |
WriteL(aCluster,freeCluster);
|
sl@0
|
365 |
aCluster=freeCluster;
|
sl@0
|
366 |
ZeroFillCluster(freeCluster); //-- zero fill just allocated cluster (RAM area)
|
sl@0
|
367 |
}
|
sl@0
|
368 |
|
sl@0
|
369 |
SetFreeClusterHint(aCluster);
|
sl@0
|
370 |
|
sl@0
|
371 |
}
|
sl@0
|
372 |
|
sl@0
|
373 |
/**
|
sl@0
|
374 |
Mark a chain of clusters as free in the FAT. Shrinks the RAM drive once the
|
sl@0
|
375 |
clusters are free
|
sl@0
|
376 |
|
sl@0
|
377 |
@param aCluster Start cluster of cluster chain to free
|
sl@0
|
378 |
@leave System wide error codes
|
sl@0
|
379 |
*/
|
sl@0
|
380 |
void CRamFatTable::FreeClusterListL(TUint32 aCluster)
|
sl@0
|
381 |
{
|
sl@0
|
382 |
__PRINT1(_L("CRamFatTable::FreeClusterListL aCluster=%d"),aCluster);
|
sl@0
|
383 |
if (aCluster==0)
|
sl@0
|
384 |
return; // File has no cluster allocated
|
sl@0
|
385 |
|
sl@0
|
386 |
const TInt clusterShift=iOwner->ClusterSizeLog2();
|
sl@0
|
387 |
TInt startCluster=aCluster;
|
sl@0
|
388 |
TInt endCluster=0;
|
sl@0
|
389 |
TInt totalFreed=0;
|
sl@0
|
390 |
TLinAddr srcEnd=0;
|
sl@0
|
391 |
|
sl@0
|
392 |
while(endCluster!=EOF_16Bit)
|
sl@0
|
393 |
{
|
sl@0
|
394 |
TInt num=CountContiguousClustersL(startCluster,endCluster,KMaxTInt);
|
sl@0
|
395 |
if (GetNextClusterL(endCluster)==EFalse || endCluster==0)
|
sl@0
|
396 |
endCluster=EOF_16Bit; // endCluster==0 -> file contained FAT loop
|
sl@0
|
397 |
|
sl@0
|
398 |
// Real position in bytes of the start cluster in the data area
|
sl@0
|
399 |
TLinAddr startClusterPos= I64LOW(DataPositionInBytes(startCluster));
|
sl@0
|
400 |
// Sliding value when more than one block is freed
|
sl@0
|
401 |
TLinAddr trg=startClusterPos-(totalFreed<<clusterShift);
|
sl@0
|
402 |
__PRINT1(_L("trg=0x%x"),trg);
|
sl@0
|
403 |
|
sl@0
|
404 |
// Beginning of data area to move
|
sl@0
|
405 |
TLinAddr srcStart=startClusterPos+(num<<clusterShift);
|
sl@0
|
406 |
__PRINT1(_L("srcStart=0x%x"),srcStart);
|
sl@0
|
407 |
// Position of next part of cluster chain or position of end of ram drive
|
sl@0
|
408 |
if (endCluster==EOF_16Bit) // Last cluster is the end of the chain
|
sl@0
|
409 |
{
|
sl@0
|
410 |
|
sl@0
|
411 |
|
sl@0
|
412 |
// Fixed to use the genuine RAM drive size rather than the number
|
sl@0
|
413 |
// of free clusters - though they *should* be the same
|
sl@0
|
414 |
// It avoids the problem of iFreeClusters getting out of sync with
|
sl@0
|
415 |
// the RAM drive size but doesn't solve the issue of why it can happen...
|
sl@0
|
416 |
|
sl@0
|
417 |
srcEnd=I64LOW(iOwner->Size());
|
sl@0
|
418 |
__PRINT1(_L("srcEnd=0x%x"),srcEnd);
|
sl@0
|
419 |
}
|
sl@0
|
420 |
else // Just move up to the next part of the chain
|
sl@0
|
421 |
srcEnd=I64LOW(DataPositionInBytes(endCluster));
|
sl@0
|
422 |
|
sl@0
|
423 |
//-- Copy (srcEnd-srcStart) bytes from iRamDiskBase+srcStart onto iRamDiskBase+trg
|
sl@0
|
424 |
//-- zero-filling free space to avoid leaving something important there
|
sl@0
|
425 |
ASSERT(srcEnd >= srcStart);
|
sl@0
|
426 |
if(srcEnd-srcStart > 0)
|
sl@0
|
427 |
{
|
sl@0
|
428 |
MemCopyFillZ(iRamDiskBase+trg,iRamDiskBase+srcStart,srcEnd-srcStart);
|
sl@0
|
429 |
}
|
sl@0
|
430 |
else
|
sl@0
|
431 |
{//-- we are freeing the cluster chain at the end of the RAMdrive; Nothing to copy to the drive space that has become free,
|
sl@0
|
432 |
//-- but nevertheless zero fill this space.
|
sl@0
|
433 |
Mem::FillZ(iRamDiskBase+trg, num<<clusterShift);
|
sl@0
|
434 |
}
|
sl@0
|
435 |
|
sl@0
|
436 |
totalFreed+=num;
|
sl@0
|
437 |
startCluster=endCluster;
|
sl@0
|
438 |
UpdateIndirectionTable(srcStart>>clusterShift,srcEnd>>clusterShift,totalFreed);
|
sl@0
|
439 |
}
|
sl@0
|
440 |
TInt bytesFreed=totalFreed<<clusterShift;
|
sl@0
|
441 |
|
sl@0
|
442 |
// First free the cluster list
|
sl@0
|
443 |
CFatTable::FreeClusterListL(aCluster);
|
sl@0
|
444 |
// Now reduce the size of the RAM drive
|
sl@0
|
445 |
iOwner->ReduceSizeL(srcEnd-bytesFreed,bytesFreed);
|
sl@0
|
446 |
}
|
sl@0
|
447 |
|
sl@0
|
448 |
/**
|
sl@0
|
449 |
Shift any clusters between aStart and anEnd backwards by aClusterShift
|
sl@0
|
450 |
|
sl@0
|
451 |
@param aStart Start of shift region
|
sl@0
|
452 |
@param anEnd End of shift region
|
sl@0
|
453 |
@param aClusterShift amount to shift cluster by
|
sl@0
|
454 |
*/
|
sl@0
|
455 |
void CRamFatTable::UpdateIndirectionTable(TUint32 aStart,TUint32 anEnd,TInt aClusterShift)
|
sl@0
|
456 |
{
|
sl@0
|
457 |
__PRINT(_L("CRamFatTable::UpdateIndirectionTable"));
|
sl@0
|
458 |
#if defined(__WINS__)
|
sl@0
|
459 |
TUint32 count=iOwner->MaxClusterNumber();
|
sl@0
|
460 |
while (count--)
|
sl@0
|
461 |
{
|
sl@0
|
462 |
TUint32 cluster=count;
|
sl@0
|
463 |
ReadIndirectionTable(cluster);
|
sl@0
|
464 |
if (cluster>=aStart && cluster<anEnd)
|
sl@0
|
465 |
WriteIndirectionTable(count,cluster-aClusterShift);
|
sl@0
|
466 |
}
|
sl@0
|
467 |
#else
|
sl@0
|
468 |
TUint16* table=(TUint16*)(RamDiskBase()+iIndirectionTablePos);
|
sl@0
|
469 |
TUint16* entry=table+iOwner->MaxClusterNumber();
|
sl@0
|
470 |
while (entry>table)
|
sl@0
|
471 |
{
|
sl@0
|
472 |
TUint32 cluster=*--entry;
|
sl@0
|
473 |
if (cluster<aStart)
|
sl@0
|
474 |
continue;
|
sl@0
|
475 |
if (cluster<anEnd)
|
sl@0
|
476 |
*entry=TUint16(cluster-aClusterShift);
|
sl@0
|
477 |
}
|
sl@0
|
478 |
#endif
|
sl@0
|
479 |
}
|
sl@0
|
480 |
|
sl@0
|
481 |
|
sl@0
|
482 |
|
sl@0
|
483 |
|
sl@0
|
484 |
|
sl@0
|
485 |
|