os/kernelhwsrv/userlibandfileserver/fileserver/sfat32/ram_fat_table32.cpp
author sl@SLION-WIN7.fritz.box
Fri, 15 Jun 2012 03:10:57 +0200
changeset 0 bde4ae8d615e
permissions -rw-r--r--
First public contribution.
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\sfat32\ram_fat_table32.cpp
sl@0
    15
// FAT16/32 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
#include "sl_std.h"
sl@0
    27
#include "sl_fatcache32.h"
sl@0
    28
#include "fat_table32.h"
sl@0
    29
sl@0
    30
sl@0
    31
//---------------------------------------------------------------------------------------------------------------------------------------
sl@0
    32
sl@0
    33
//#######################################################################################################################################
sl@0
    34
//#     CRamFatTable class implementation 
sl@0
    35
//#######################################################################################################################################
sl@0
    36
sl@0
    37
/**
sl@0
    38
    Constructor, the RamFatTable allows disk compression by redirecting the FAT
sl@0
    39
sl@0
    40
    @param aOwner Owning mount.
sl@0
    41
*/
sl@0
    42
CRamFatTable::CRamFatTable(CFatMountCB& aOwner)
sl@0
    43
             :CFatTable(aOwner)
sl@0
    44
{
sl@0
    45
    iFatTablePos=aOwner.FirstFatSector()<<aOwner.SectorSizeLog2();
sl@0
    46
    iIndirectionTablePos=iFatTablePos+aOwner.FatSizeInBytes();
sl@0
    47
}
sl@0
    48
sl@0
    49
/** factory method */
sl@0
    50
CRamFatTable* CRamFatTable::NewL(CFatMountCB& aOwner)
sl@0
    51
{
sl@0
    52
    __PRINT1(_L("CRamFatTable::NewL() drv:%d"),aOwner.DriveNumber());
sl@0
    53
sl@0
    54
    CRamFatTable* pSelf = new (ELeave) CRamFatTable(aOwner);
sl@0
    55
sl@0
    56
    CleanupStack::PushL(pSelf);
sl@0
    57
    pSelf->InitializeL();
sl@0
    58
    CleanupStack::Pop();
sl@0
    59
sl@0
    60
    return pSelf;
sl@0
    61
}
sl@0
    62
sl@0
    63
sl@0
    64
void CRamFatTable::InitializeL()
sl@0
    65
{
sl@0
    66
    CFatTable::InitializeL();
sl@0
    67
sl@0
    68
    ASSERT(iMediaAtt & KMediaAttVariableSize);
sl@0
    69
    ASSERT(FatType() == EFat16 || FatType()== EFat32);
sl@0
    70
sl@0
    71
    iFatTablePos=iOwner->FirstFatSector()<<iOwner->SectorSizeLog2();
sl@0
    72
    iIndirectionTablePos=iFatTablePos+iOwner->FatSizeInBytes();
sl@0
    73
sl@0
    74
    //-- set RAM disk base
sl@0
    75
    TLocalDriveCapsV2 caps;
sl@0
    76
    TPckg<TLocalDriveCapsV2> capsPckg(caps);
sl@0
    77
    User::LeaveIfError(iOwner->LocalDrive()->Caps(capsPckg));
sl@0
    78
  
sl@0
    79
    iRamDiskBase = caps.iBaseAddress; 
sl@0
    80
}
sl@0
    81
sl@0
    82
/**
sl@0
    83
    Just Count free clusters in the FAT
sl@0
    84
*/
sl@0
    85
void CRamFatTable::MountL(const TMountParams& /*aMountParam*/)
sl@0
    86
{
sl@0
    87
    CountFreeClustersL();
sl@0
    88
}
sl@0
    89
sl@0
    90
sl@0
    91
/**
sl@0
    92
    Return the start address of the Ram Drive
sl@0
    93
    @return start address of the Ram Drive 
sl@0
    94
*/
sl@0
    95
TUint8 *CRamFatTable::RamDiskBase() const
sl@0
    96
    {
sl@0
    97
    return(iRamDiskBase);
sl@0
    98
    }
sl@0
    99
sl@0
   100
sl@0
   101
/**
sl@0
   102
    Allocate a new cluster number
sl@0
   103
sl@0
   104
    @return New cluster number
sl@0
   105
*/
sl@0
   106
TInt CRamFatTable::AllocateClusterNumber()
sl@0
   107
    {
sl@0
   108
    return(iOwner->MaxClusterNumber()-NumberOfFreeClusters());
sl@0
   109
    }
sl@0
   110
sl@0
   111
/**
sl@0
   112
    Write a value to the FAT (indirection table) 
sl@0
   113
sl@0
   114
    @param aFatIndex Cluster to write to
sl@0
   115
    @param aValue value to write to Fat
sl@0
   116
*/
sl@0
   117
void CRamFatTable::WriteL(TUint32 aFatIndex, TUint32 aValue)
sl@0
   118
    {
sl@0
   119
    //__PRINT(_L("CRamFatTable::WriteL"));
sl@0
   120
sl@0
   121
//  __ASSERT_ALWAYS(aFatIndex>=2 && (aValue>=2 || aValue==0) && aValue<=0xFFFF,User::Leave(KErrCorrupt));
sl@0
   122
    TUint32 indirectCluster=aFatIndex;
sl@0
   123
    TUint32 indirectClusterNewVal=0;
sl@0
   124
    ReadIndirectionTable(indirectCluster);
sl@0
   125
//  If value in indirection table!=0 we assume we have already written to the indirection table
sl@0
   126
//  So just update the FAT table
sl@0
   127
    if (indirectCluster!=0 && aValue!=0)
sl@0
   128
        {
sl@0
   129
        WriteFatTable(aFatIndex,aValue);
sl@0
   130
        return;
sl@0
   131
        }
sl@0
   132
//  If value in indirection table is 0, we haven't written to it yet, though the memory has
sl@0
   133
//  already been allocated by the EnlargeL() function
sl@0
   134
    if (indirectCluster==0 && aValue!=0) // Assumes memory has already been allocated
sl@0
   135
        indirectClusterNewVal=AllocateClusterNumber();
sl@0
   136
//  Write aValue into aFaxIndex and indirectClusterNewVal into the corresponding position
sl@0
   137
//  in the indirection table    
sl@0
   138
    WriteFatTable(aFatIndex,aValue,indirectClusterNewVal);
sl@0
   139
    }   
sl@0
   140
sl@0
   141
/**
sl@0
   142
    Read the value of a cluster in the Fat
sl@0
   143
sl@0
   144
    @param aFatIndex A cluster to read
sl@0
   145
    @return The cluster value read
sl@0
   146
*/
sl@0
   147
TUint32 CRamFatTable::ReadL(TUint32 aFatIndex) const
sl@0
   148
    {
sl@0
   149
    __ASSERT_ALWAYS(aFatIndex>=KFatFirstSearchCluster,User::Leave(KErrCorrupt));
sl@0
   150
sl@0
   151
    TUint32 clusterVal;
sl@0
   152
sl@0
   153
    switch(FatType())
sl@0
   154
        {
sl@0
   155
        case EFat16:
sl@0
   156
            clusterVal=*(TUint16*)(RamDiskBase()+PosInBytes(aFatIndex)+iFatTablePos);
sl@0
   157
        break;
sl@0
   158
sl@0
   159
        case EFat32:
sl@0
   160
            clusterVal=*(TUint32*)(RamDiskBase()+PosInBytes(aFatIndex)+iFatTablePos);
sl@0
   161
        break;
sl@0
   162
    
sl@0
   163
        default:
sl@0
   164
            ASSERT(0);
sl@0
   165
        return 0;
sl@0
   166
        }
sl@0
   167
    
sl@0
   168
    return clusterVal;
sl@0
   169
    }
sl@0
   170
sl@0
   171
/**
sl@0
   172
    Write a value to the FAT and indirection table
sl@0
   173
sl@0
   174
    @param aFatIndex Cluster number to write to
sl@0
   175
    @param aFatValue Cluster value for Fat
sl@0
   176
    @param anIndirectionValue Value for indirection table
sl@0
   177
*/
sl@0
   178
void CRamFatTable::WriteFatTable(TInt aFatIndex,TInt aFatValue,TInt anIndirectionValue)
sl@0
   179
    {
sl@0
   180
    const TUint8* pos=RamDiskBase()+PosInBytes(aFatIndex);
sl@0
   181
sl@0
   182
    switch(FatType())
sl@0
   183
        {
sl@0
   184
        case EFat16:
sl@0
   185
            *(TUint16*)(pos+iFatTablePos)=(TUint16)aFatValue;
sl@0
   186
            *(TUint16*)(pos+iIndirectionTablePos)=(TUint16)anIndirectionValue;
sl@0
   187
        break;
sl@0
   188
sl@0
   189
        case EFat32:
sl@0
   190
            *(TUint32*)(pos+iFatTablePos)=(TUint32)aFatValue;
sl@0
   191
            *(TUint32*)(pos+iIndirectionTablePos)=(TUint32)anIndirectionValue;
sl@0
   192
        break;
sl@0
   193
    
sl@0
   194
        default:
sl@0
   195
            ASSERT(0);
sl@0
   196
        return;
sl@0
   197
        }
sl@0
   198
    
sl@0
   199
    }
sl@0
   200
sl@0
   201
/**
sl@0
   202
    Write to just the fat table
sl@0
   203
sl@0
   204
    @param aFatIndex Cluster number to write to
sl@0
   205
    @param aFatValue Cluster value for Fat
sl@0
   206
*/
sl@0
   207
void CRamFatTable::WriteFatTable(TInt aFatIndex,TInt aFatValue)
sl@0
   208
    {
sl@0
   209
sl@0
   210
    switch(FatType())
sl@0
   211
        {
sl@0
   212
        case EFat16:
sl@0
   213
            *(TUint16*)(RamDiskBase()+PosInBytes(aFatIndex)+iFatTablePos)=(TUint16)aFatValue;
sl@0
   214
        break;
sl@0
   215
sl@0
   216
        case EFat32:
sl@0
   217
            *(TUint32*)(RamDiskBase()+PosInBytes(aFatIndex)+iFatTablePos)=(TUint32)aFatValue;
sl@0
   218
        break;
sl@0
   219
    
sl@0
   220
        default:
sl@0
   221
            ASSERT(0);
sl@0
   222
        return;
sl@0
   223
        }
sl@0
   224
sl@0
   225
    }
sl@0
   226
sl@0
   227
/**
sl@0
   228
    Write to just the fat table
sl@0
   229
sl@0
   230
    @param aFatIndex Cluster number to write to
sl@0
   231
    @param aFatValue Value for indirection table
sl@0
   232
*/
sl@0
   233
void CRamFatTable::WriteIndirectionTable(TInt aFatIndex,TInt aFatValue)
sl@0
   234
    {
sl@0
   235
    switch(FatType())
sl@0
   236
        {
sl@0
   237
        case EFat16:
sl@0
   238
            *(TUint16*)(RamDiskBase()+PosInBytes(aFatIndex)+iIndirectionTablePos)=(TUint16)aFatValue;
sl@0
   239
        break;
sl@0
   240
sl@0
   241
        case EFat32:
sl@0
   242
            *(TUint32*)(RamDiskBase()+PosInBytes(aFatIndex)+iIndirectionTablePos)=(TUint32)aFatValue;
sl@0
   243
        break;
sl@0
   244
    
sl@0
   245
        default:
sl@0
   246
            ASSERT(0);
sl@0
   247
        return;
sl@0
   248
        }
sl@0
   249
    }
sl@0
   250
sl@0
   251
/**
sl@0
   252
    Find the real location of aCluster
sl@0
   253
    @param aCluster Cluster to read, contians cluster value upon return
sl@0
   254
*/
sl@0
   255
void CRamFatTable::ReadIndirectionTable(TUint32& aCluster) const
sl@0
   256
    {
sl@0
   257
    switch(FatType())
sl@0
   258
        {
sl@0
   259
        case EFat16:
sl@0
   260
            aCluster=*(TUint16*)(RamDiskBase()+PosInBytes(aCluster)+iIndirectionTablePos);    
sl@0
   261
        break;
sl@0
   262
sl@0
   263
        case EFat32:
sl@0
   264
            aCluster=*(TUint32*)(RamDiskBase()+PosInBytes(aCluster)+iIndirectionTablePos);
sl@0
   265
        break;
sl@0
   266
    
sl@0
   267
        default:
sl@0
   268
            ASSERT(0);
sl@0
   269
        return;
sl@0
   270
        }
sl@0
   271
    
sl@0
   272
    }
sl@0
   273
sl@0
   274
/**
sl@0
   275
    Copy memory in RAM drive area, unlocking required
sl@0
   276
sl@0
   277
    @param aTrg Pointer to destination location
sl@0
   278
    @param aSrc Pointer to source location
sl@0
   279
    @param aLength Length of data to copy
sl@0
   280
    @return Pointer to end of data copied
sl@0
   281
*/
sl@0
   282
TUint8* CRamFatTable::MemCopy(TAny* aTrg,const TAny* aSrc,TInt aLength)
sl@0
   283
    {
sl@0
   284
    TUint8* p=Mem::Copy(aTrg,aSrc,aLength);
sl@0
   285
    return(p);
sl@0
   286
    }
sl@0
   287
sl@0
   288
/**
sl@0
   289
    Copy memory with filling the source buffer with zeroes. Target and source buffers can overlap.
sl@0
   290
    Used on RAMDrive srinking in order to wipe data from the file that is being deleted.
sl@0
   291
    
sl@0
   292
    @param   aTrg       pointer to the target address
sl@0
   293
    @param   aSrc       pointer to the destination address
sl@0
   294
    @param   aLength    how many bytes to copy
sl@0
   295
    @return  A pointer to a location aLength bytes beyond aTrg (i.e. the location aTrg+aLength).
sl@0
   296
*/
sl@0
   297
TUint8* CRamFatTable::MemCopyFillZ(TAny* aTrg, TAny* aSrc,TInt aLength)
sl@0
   298
{
sl@0
   299
    //-- just copy src to the trg, the memory areas can overlap.
sl@0
   300
    TUint8* p=Mem::Copy(aTrg, aSrc, aLength);
sl@0
   301
    
sl@0
   302
    //-- now zero-fill the source memory area taking into account possible overlap.
sl@0
   303
    TUint8* pSrc = static_cast<TUint8*>(aSrc);
sl@0
   304
    TUint8* pTrg = static_cast<TUint8*>(aTrg);
sl@0
   305
    
sl@0
   306
    TUint8* pZFill = NULL; //-- pointer to the beginning of zerofilled area
sl@0
   307
    TInt    zFillLen = 0;  //-- a number of bytes to zero-fill
sl@0
   308
    
sl@0
   309
    if(aTrg < aSrc)
sl@0
   310
    {
sl@0
   311
        if(pTrg+aLength < pSrc)
sl@0
   312
        {//-- target and source areas do not overlap
sl@0
   313
         pZFill = pSrc;
sl@0
   314
         zFillLen = aLength;
sl@0
   315
        }
sl@0
   316
        else
sl@0
   317
        {//-- target and source areas overlap, try not to corrupt the target area
sl@0
   318
         zFillLen = pSrc-pTrg;
sl@0
   319
         pZFill = pTrg+aLength;
sl@0
   320
        }
sl@0
   321
    }
sl@0
   322
    else
sl@0
   323
    {
sl@0
   324
        if(pSrc+aLength < pTrg)
sl@0
   325
        {//-- target and source areas do not overlap
sl@0
   326
         pZFill = pSrc;
sl@0
   327
         zFillLen = aLength;
sl@0
   328
        }
sl@0
   329
        else
sl@0
   330
        {//-- target and source areas overlap, try not to corrupt the target area
sl@0
   331
         zFillLen = pSrc+aLength-pTrg;
sl@0
   332
         pZFill = pSrc;
sl@0
   333
        }
sl@0
   334
    }
sl@0
   335
sl@0
   336
    Mem::FillZ(pZFill, zFillLen);
sl@0
   337
sl@0
   338
    return(p);
sl@0
   339
}
sl@0
   340
sl@0
   341
sl@0
   342
/**
sl@0
   343
    Zero fill RAM area corresponding to the cluster number aCluster
sl@0
   344
    @param  aCluster a cluster number to be zero-filled
sl@0
   345
*/
sl@0
   346
void CRamFatTable::ZeroFillCluster(TInt aCluster)
sl@0
   347
    {
sl@0
   348
    TLinAddr clusterPos= I64LOW(DataPositionInBytes(aCluster));
sl@0
   349
    Mem::FillZ(iRamDiskBase+clusterPos, 1<< iOwner->ClusterSizeLog2());     
sl@0
   350
    }
sl@0
   351
sl@0
   352
sl@0
   353
/**
sl@0
   354
Return the location of a Cluster in the data section of the media
sl@0
   355
sl@0
   356
@param aCluster to find location of
sl@0
   357
@return Byte offset of the cluster data 
sl@0
   358
*/
sl@0
   359
TInt64 CRamFatTable::DataPositionInBytes(TUint32 aCluster) const
sl@0
   360
    {
sl@0
   361
    //__PRINT(_L("CRamFatTable::DataPositionInBytes"));
sl@0
   362
    ReadIndirectionTable(aCluster);
sl@0
   363
    return(aCluster<<iOwner->ClusterSizeLog2());
sl@0
   364
    }
sl@0
   365
sl@0
   366
//-----------------------------------------------------------------------------
sl@0
   367
sl@0
   368
/**
sl@0
   369
    Allocate and link a cluster chain, leaves if there are not enough free clusters.
sl@0
   370
    Chain starts as close as possible to aNearestCluster, last cluster will be marked as EOF.
sl@0
   371
sl@0
   372
    @param aNumber Number of clusters to allocate
sl@0
   373
    @param aNearestCluster Cluster the new chain should be nearest to
sl@0
   374
    @leave System wide error codes
sl@0
   375
    @return The first cluster number allocated
sl@0
   376
*/
sl@0
   377
TUint32 CRamFatTable::AllocateClusterListL(TUint32 aNumber, TUint32 aNearestCluster)
sl@0
   378
	{
sl@0
   379
    __PRINT2(_L("CRamFatTable::AllocateClusterList() N:%d,NearestCL:%d"),aNumber,aNearestCluster);
sl@0
   380
	__ASSERT_DEBUG(aNumber>0, Fault(EFatBadParameter));
sl@0
   381
sl@0
   382
	if(!RequestFreeClusters(aNumber))
sl@0
   383
    	{
sl@0
   384
		__PRINT(_L("CRamFatTable::AllocateClusterListL - leaving KErrDirFull"));
sl@0
   385
		User::Leave(KErrDiskFull);
sl@0
   386
		}
sl@0
   387
sl@0
   388
	//-- if this leaves for some reason, there will be no lost clusters
sl@0
   389
    TInt firstCluster = aNearestCluster = AllocateSingleClusterL(aNearestCluster);
sl@0
   390
	
sl@0
   391
    
sl@0
   392
    if (aNumber>1)
sl@0
   393
	    {//-- if this part leaves (e.g. fail to expand the RAM drive), we will need to handle the first allocated EOC
sl@0
   394
    	TRAPD(nRes, ExtendClusterListL(aNumber-1, (TInt&)aNearestCluster));
sl@0
   395
        if(nRes != KErrNone)
sl@0
   396
            {
sl@0
   397
            __PRINT1(_L("CRamFatTable::AllocateClusterListL:ExtendClusterListL() failed with %d") ,nRes);
sl@0
   398
            FreeClusterListL(firstCluster); //-- clean up EOC in firstCluster
sl@0
   399
            User::Leave(nRes);
sl@0
   400
            }
sl@0
   401
        }
sl@0
   402
sl@0
   403
sl@0
   404
    return firstCluster;
sl@0
   405
	}	
sl@0
   406
sl@0
   407
/**
sl@0
   408
Allocate and mark as EOF a single cluster as close as possible to aNearestCluster,
sl@0
   409
calls base class implementation but must Enlarge the RAM drive first. Allocated cluster RAM area will be zero-filled.
sl@0
   410
sl@0
   411
@param aNearestCluster Cluster the new cluster should be nearest to
sl@0
   412
@leave System wide error codes
sl@0
   413
@return The cluster number allocated
sl@0
   414
*/
sl@0
   415
TUint32 CRamFatTable::AllocateSingleClusterL(TUint32 aNearestCluster)
sl@0
   416
    {
sl@0
   417
    __PRINT(_L("CRamFatTable::AllocateSingleClusterL"));
sl@0
   418
    iOwner->EnlargeL(1<<iOwner->ClusterSizeLog2()); //  First enlarge the RAM drive
sl@0
   419
    TInt fileAllocated=CFatTable::AllocateSingleClusterL(aNearestCluster); //   Now update the free cluster and fat/fit
sl@0
   420
    ZeroFillCluster(fileAllocated);  //-- zero-fill allocated cluster 
sl@0
   421
    return(fileAllocated);
sl@0
   422
    }   
sl@0
   423
sl@0
   424
sl@0
   425
/**
sl@0
   426
    Extend a file or directory cluster chain, enlarging RAM drive first. Allocated clusters are zero-filled.
sl@0
   427
    Leaves if there are no free clusters (the disk is full).
sl@0
   428
    Note that method now doesn't call CFatTable::ExtendClusterListL() from its base class, be careful making changes there.
sl@0
   429
sl@0
   430
    @param aNumber      number of clusters to allocate
sl@0
   431
    @param aCluster     starting cluster number / ending cluster number after
sl@0
   432
    @leave KErrDiskFull + system wide error codes
sl@0
   433
*/
sl@0
   434
void CRamFatTable::ExtendClusterListL(TUint32 aNumber, TInt& aCluster)
sl@0
   435
    {
sl@0
   436
    __PRINT2(_L("CRamFatTable::ExtendClusterListL(%d, %d)"), aNumber, aCluster);
sl@0
   437
    __ASSERT_DEBUG(aNumber>0,Fault(EFatBadParameter));
sl@0
   438
sl@0
   439
    iOwner->EnlargeL(aNumber<<iOwner->ClusterSizeLog2());
sl@0
   440
sl@0
   441
    while(aNumber && GetNextClusterL(aCluster))
sl@0
   442
        aNumber--;
sl@0
   443
sl@0
   444
    if(!aNumber)
sl@0
   445
        return;
sl@0
   446
sl@0
   447
    if (NumberOfFreeClusters() < aNumber)
sl@0
   448
        {
sl@0
   449
        __PRINT(_L("CRamFatTable::ExtendClusterListL - leaving KErrDirFull"));
sl@0
   450
        User::Leave(KErrDiskFull);
sl@0
   451
        }
sl@0
   452
sl@0
   453
    while(aNumber--)
sl@0
   454
        {
sl@0
   455
        const TInt freeCluster=FindClosestFreeClusterL(aCluster);
sl@0
   456
sl@0
   457
        WriteFatEntryEofL(freeCluster); //  Must write EOF for FindClosestFreeCluster to work again
sl@0
   458
        DecrementFreeClusterCount(1);
sl@0
   459
        WriteL(aCluster,freeCluster);
sl@0
   460
        aCluster=freeCluster;
sl@0
   461
        ZeroFillCluster(freeCluster); //-- zero fill just allocated cluster (RAM area)
sl@0
   462
        }
sl@0
   463
sl@0
   464
    SetFreeClusterHint(aCluster); 
sl@0
   465
  
sl@0
   466
    }
sl@0
   467
sl@0
   468
/**
sl@0
   469
Mark a chain of clusters as free in the FAT. Shrinks the RAM drive once the
sl@0
   470
clusters are free 
sl@0
   471
sl@0
   472
@param aCluster Start cluster of cluster chain to free
sl@0
   473
@leave System wide error codes
sl@0
   474
*/
sl@0
   475
void CRamFatTable::FreeClusterListL(TUint32 aCluster)
sl@0
   476
    {
sl@0
   477
    __PRINT1(_L("CRamFatTable::FreeClusterListL aCluster=%d"),aCluster);
sl@0
   478
    if (aCluster==0)
sl@0
   479
        return; // File has no cluster allocated
sl@0
   480
sl@0
   481
    const TInt clusterShift=iOwner->ClusterSizeLog2();
sl@0
   482
    TInt startCluster=aCluster;
sl@0
   483
    TInt endCluster=0;
sl@0
   484
    TInt totalFreed=0;
sl@0
   485
    TLinAddr srcEnd=0;
sl@0
   486
sl@0
   487
    if(IsFat32())
sl@0
   488
        {
sl@0
   489
        while(endCluster!=EOF_32Bit)
sl@0
   490
            {
sl@0
   491
            TInt num=CountContiguousClustersL(startCluster,endCluster,KMaxTInt);
sl@0
   492
            if (GetNextClusterL(endCluster)==EFalse || endCluster==0)
sl@0
   493
                endCluster=EOF_32Bit;   // endCluster==0 -> file contained FAT loop
sl@0
   494
sl@0
   495
        //  Real position in bytes of the start cluster in the data area
sl@0
   496
            TLinAddr startClusterPos=I64LOW(DataPositionInBytes(startCluster));
sl@0
   497
        //  Sliding value when more than one block is freed
sl@0
   498
            TLinAddr trg=startClusterPos-(totalFreed<<clusterShift);
sl@0
   499
            __PRINT1(_L("trg=0x%x"),trg);
sl@0
   500
sl@0
   501
        //  Beginning of data area to move
sl@0
   502
            TLinAddr srcStart=startClusterPos+(num<<clusterShift);
sl@0
   503
            __PRINT1(_L("srcStart=0x%x"),srcStart);
sl@0
   504
        //  Position of next part of cluster chain or position of end of ram drive
sl@0
   505
            if (endCluster==EOF_32Bit)  //  Last cluster is the end of the chain
sl@0
   506
                {
sl@0
   507
            
sl@0
   508
        
sl@0
   509
            //  Fixed to use the genuine RAM drive size rather than the number
sl@0
   510
            //  of free clusters - though they *should* be the same
sl@0
   511
            //  It avoids the problem of iFreeClusters getting out of sync with 
sl@0
   512
            //  the RAM drive size but doesn't solve the issue of why it can happen...
sl@0
   513
                
sl@0
   514
                srcEnd=I64LOW(iOwner->Size());
sl@0
   515
                __PRINT1(_L("srcEnd=0x%x"),srcEnd);
sl@0
   516
                }
sl@0
   517
            else                        //  Just move up to the next part of the chain
sl@0
   518
                srcEnd=I64LOW(DataPositionInBytes(endCluster));
sl@0
   519
sl@0
   520
        //-- Copy (srcEnd-srcStart) bytes from iRamDiskBase+srcStart onto iRamDiskBase+trg
sl@0
   521
        //-- zero-filling free space to avoid leaving something important there
sl@0
   522
        ASSERT(srcEnd >= srcStart);
sl@0
   523
        if(srcEnd-srcStart > 0)
sl@0
   524
            { 
sl@0
   525
            MemCopyFillZ(iRamDiskBase+trg,iRamDiskBase+srcStart,srcEnd-srcStart);
sl@0
   526
            }
sl@0
   527
        else
sl@0
   528
            {//-- we are freeing the cluster chain at the end of the RAM drive; Nothing to copy to the drive space that has become free,
sl@0
   529
             //-- but nevertheless zero fill this space.
sl@0
   530
            Mem::FillZ(iRamDiskBase+trg, num<<clusterShift);
sl@0
   531
            }
sl@0
   532
sl@0
   533
sl@0
   534
            totalFreed+=num;
sl@0
   535
            startCluster=endCluster;
sl@0
   536
            UpdateIndirectionTable(srcStart>>clusterShift,srcEnd>>clusterShift,totalFreed);
sl@0
   537
            }
sl@0
   538
        }
sl@0
   539
    else
sl@0
   540
        {
sl@0
   541
        while(endCluster!=EOF_16Bit)
sl@0
   542
            {
sl@0
   543
            TInt num=CountContiguousClustersL(startCluster,endCluster,KMaxTInt);
sl@0
   544
            if (GetNextClusterL(endCluster)==EFalse || endCluster==0)
sl@0
   545
                endCluster=EOF_16Bit;   // endCluster==0 -> file contained FAT loop
sl@0
   546
sl@0
   547
        //  Real position in bytes of the start cluster in the data area
sl@0
   548
            TLinAddr startClusterPos=I64LOW(DataPositionInBytes(startCluster));
sl@0
   549
        //  Sliding value when more than one block is freed
sl@0
   550
            TLinAddr trg=startClusterPos-(totalFreed<<clusterShift);
sl@0
   551
            __PRINT1(_L("trg=0x%x"),trg);
sl@0
   552
sl@0
   553
        //  Beginning of data area to move
sl@0
   554
            TLinAddr srcStart=startClusterPos+(num<<clusterShift);
sl@0
   555
            __PRINT1(_L("srcStart=0x%x"),srcStart);
sl@0
   556
        //  Position of next part of cluster chain or position of end of ram drive
sl@0
   557
            if (endCluster==EOF_16Bit)  //  Last cluster is the end of the chain
sl@0
   558
                {
sl@0
   559
            
sl@0
   560
        
sl@0
   561
            //  Fixed to use the genuine RAM drive size rather than the number
sl@0
   562
            //  of free clusters - though they *should* be the same
sl@0
   563
            //  It avoids the problem of iFreeClusters getting out of sync with 
sl@0
   564
            //  the RAM drive size but doesn't solve the issue of why it can happen...
sl@0
   565
                
sl@0
   566
                srcEnd=I64LOW(iOwner->Size());
sl@0
   567
                __PRINT1(_L("srcEnd=0x%x"),srcEnd);
sl@0
   568
                }
sl@0
   569
            else                        //  Just move up to the next part of the chain
sl@0
   570
                srcEnd=I64LOW(DataPositionInBytes(endCluster));
sl@0
   571
sl@0
   572
        //-- Copy (srcEnd-srcStart) bytes from iRamDiskBase+srcStart onto iRamDiskBase+trg
sl@0
   573
        //-- zero-filling free space to avoid leaving something important there
sl@0
   574
        ASSERT(srcEnd >= srcStart);
sl@0
   575
        if(srcEnd-srcStart > 0)
sl@0
   576
            { 
sl@0
   577
            MemCopyFillZ(iRamDiskBase+trg,iRamDiskBase+srcStart,srcEnd-srcStart);
sl@0
   578
            }    
sl@0
   579
        else
sl@0
   580
            {//-- 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
   581
             //-- but nevertheless zero fill this space.
sl@0
   582
            Mem::FillZ(iRamDiskBase+trg, num<<clusterShift);
sl@0
   583
            }    
sl@0
   584
        
sl@0
   585
            totalFreed+=num;
sl@0
   586
            startCluster=endCluster;
sl@0
   587
            UpdateIndirectionTable(srcStart>>clusterShift,srcEnd>>clusterShift,totalFreed);
sl@0
   588
            }
sl@0
   589
        }
sl@0
   590
    TInt bytesFreed=totalFreed<<clusterShift;
sl@0
   591
    
sl@0
   592
//  First free the cluster list
sl@0
   593
    CFatTable::FreeClusterListL(aCluster);
sl@0
   594
//  Now reduce the size of the RAM drive
sl@0
   595
    iOwner->ReduceSizeL(srcEnd-bytesFreed,bytesFreed);
sl@0
   596
    }
sl@0
   597
sl@0
   598
/**
sl@0
   599
Shift any clusters between aStart and anEnd backwards by aClusterShift
sl@0
   600
sl@0
   601
@param aStart Start of shift region
sl@0
   602
@param anEnd End of shift region
sl@0
   603
@param aClusterShift amount to shift cluster by
sl@0
   604
*/
sl@0
   605
void CRamFatTable::UpdateIndirectionTable(TUint32 aStart,TUint32 anEnd,TInt aClusterShift)
sl@0
   606
    {
sl@0
   607
    __PRINT(_L("CRamFatTable::UpdateIndirectionTable"));
sl@0
   608
#if defined(__WINS__)
sl@0
   609
    TUint32 count=iOwner->MaxClusterNumber();
sl@0
   610
    while (count--)
sl@0
   611
        {
sl@0
   612
        TUint32 cluster=count;
sl@0
   613
        ReadIndirectionTable(cluster);
sl@0
   614
        if (cluster>=aStart && cluster<anEnd)
sl@0
   615
            WriteIndirectionTable(count,cluster-aClusterShift);
sl@0
   616
        }
sl@0
   617
#else
sl@0
   618
    TUint16* table=(TUint16*)(RamDiskBase()+iIndirectionTablePos);
sl@0
   619
    TUint16* entry=table+iOwner->MaxClusterNumber();
sl@0
   620
    while (entry>table)
sl@0
   621
        {
sl@0
   622
        TUint32 cluster=*--entry;
sl@0
   623
        if (cluster<aStart)
sl@0
   624
            continue;
sl@0
   625
        if (cluster<anEnd)
sl@0
   626
            *entry=TUint16(cluster-aClusterShift);
sl@0
   627
        }
sl@0
   628
#endif
sl@0
   629
    }
sl@0
   630
sl@0
   631
sl@0
   632