os/kernelhwsrv/userlibandfileserver/fileserver/sfat/sl_file.cpp
changeset 0 bde4ae8d615e
     1.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     1.2 +++ b/os/kernelhwsrv/userlibandfileserver/fileserver/sfat/sl_file.cpp	Fri Jun 15 03:10:57 2012 +0200
     1.3 @@ -0,0 +1,782 @@
     1.4 +// Copyright (c) 1996-2009 Nokia Corporation and/or its subsidiary(-ies).
     1.5 +// All rights reserved.
     1.6 +// This component and the accompanying materials are made available
     1.7 +// under the terms of the License "Eclipse Public License v1.0"
     1.8 +// which accompanies this distribution, and is available
     1.9 +// at the URL "http://www.eclipse.org/legal/epl-v10.html".
    1.10 +//
    1.11 +// Initial Contributors:
    1.12 +// Nokia Corporation - initial contribution.
    1.13 +//
    1.14 +// Contributors:
    1.15 +//
    1.16 +// Description:
    1.17 +// f32\sfat\sl_file.cpp
    1.18 +// 
    1.19 +//
    1.20 +
    1.21 +
    1.22 +//!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
    1.23 +//!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
    1.24 +//!!
    1.25 +//!! WARNING!! DO NOT edit this file !! '\sfat' component is obsolete and is not being used. '\sfat32'replaces it
    1.26 +//!!
    1.27 +//!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
    1.28 +//!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
    1.29 +
    1.30 +
    1.31 +#include "sl_std.h"
    1.32 +#include "sl_cache.h"
    1.33 +#include <e32math.h>
    1.34 +
    1.35 +const TInt KSeekIndexSize=128; // Cache 128 clusters
    1.36 +const TInt KSeekIndexSizeLog2=7;
    1.37 +const TInt KFirstClusterNum=2;
    1.38 +
    1.39 +CFatFileCB::CFatFileCB()
    1.40 +    {
    1.41 +
    1.42 +    __PRINT1(_L("CFatFileCB created 0x%x"),this);
    1.43 +    }
    1.44 +
    1.45 +CFatFileCB::~CFatFileCB()
    1.46 +    {
    1.47 +    __PRINT1(_L("CFatFileCB deleted 0x%x"),this);
    1.48 +
    1.49 +    //-- a nasty trick to find out if the CFatFileCB is in consistent state on the moment of destruction.
    1.50 +    //-- Because of OOM conditions CFatFileCB might not be fully constructed and to be deleted, while FlushAll()
    1.51 +    //-- implies valid iMount.
    1.52 +    const CMountCB* pMount  = &Mount();
    1.53 +    if(pMount)
    1.54 +        {//-- do some finalisation work if CMountCB is valid
    1.55 +        if (iAtt&KEntryAttModified)
    1.56 +            TRAP_IGNORE(FlushAllL());
    1.57 +        }
    1.58 +
    1.59 +    delete[] iSeekIndex;
    1.60 +    }
    1.61 +
    1.62 +
    1.63 +void CFatFileCB::CreateSeekIndex()
    1.64 +//
    1.65 +// Create a seek index
    1.66 +//
    1.67 +    {
    1.68 +
    1.69 +    iSeekIndex = new TUint32[KSeekIndexSize];
    1.70 +    if (iSeekIndex == NULL)
    1.71 +        return;
    1.72 +
    1.73 +    Mem::FillZ(iSeekIndex, sizeof(TUint32) * KSeekIndexSize);
    1.74 +
    1.75 +    iSeekIndexSize=CalcSeekIndexSize(Size());
    1.76 +    }
    1.77 +
    1.78 +TInt CFatFileCB::SeekToPosition(TInt aNewRelCluster,TInt aClusterOffset)
    1.79 +//
    1.80 +// Use the seek index to set iCurrentPos.iCluster as close as possible to aNewRelCluster
    1.81 +// Return aNewRelCluster-aCurrentPos.iCluster
    1.82 +//
    1.83 +    {
    1.84 +    TInt clusterOffset=aClusterOffset;
    1.85 +    TInt seekPos=(aNewRelCluster>>iSeekIndexSize)-1;
    1.86 +    __ASSERT_DEBUG(seekPos<KSeekIndexSize,Fault(EFatFileSeekIndexTooSmall));
    1.87 +
    1.88 +    while(seekPos>=0 && iSeekIndex[seekPos]==0 && clusterOffset!=0)
    1.89 +        {
    1.90 +        seekPos--;
    1.91 +        clusterOffset--;
    1.92 +        }
    1.93 +    if (clusterOffset==0) // Counted back to the current cluster
    1.94 +        return(aClusterOffset);
    1.95 +    if (seekPos<0)
    1.96 +        {
    1.97 +        iCurrentPos.iCluster=iStartCluster;
    1.98 +        return(aNewRelCluster);
    1.99 +        }
   1.100 +
   1.101 +    iCurrentPos.iCluster=iSeekIndex[seekPos];
   1.102 +    return(aNewRelCluster-((seekPos+1)<<iSeekIndexSize));
   1.103 +    }
   1.104 +
   1.105 +void CFatFileCB::SetSeekIndexValueL(TInt aRelCluster,TInt aStoredCluster)
   1.106 +//
   1.107 +// Sets a value in the seekindex
   1.108 +//
   1.109 +    {
   1.110 +
   1.111 +    TInt seekPos=(aRelCluster>>iSeekIndexSize)-1;
   1.112 +    __ASSERT_DEBUG(seekPos<KSeekIndexSize,Fault(EFatFileSeekIndexTooSmall));
   1.113 +    __ASSERT_DEBUG(seekPos>=0,Fault(EFatFileSeekIndexTooSmall2));
   1.114 +    iSeekIndex[seekPos] = aStoredCluster;
   1.115 +    }
   1.116 +
   1.117 +TBool CFatFileCB::IsSeekBackwards(TUint aPos)
   1.118 +//
   1.119 +// Return true if aPos<currentPos
   1.120 +//
   1.121 +    {
   1.122 +    
   1.123 +    TUint cluster=iCurrentPos.iCluster<<ClusterSizeLog2();
   1.124 +    TInt offset=ClusterRelativePos(iCurrentPos.iPos);
   1.125 +    TUint currentPos=cluster+offset;
   1.126 +    return(aPos<currentPos);
   1.127 +    }
   1.128 +
   1.129 +void CFatFileCB::CheckPosL(TUint aPos)
   1.130 +//
   1.131 +// Check that the file is positioned correctly.
   1.132 +// If aPos<currentPos attempt to guess the new position.
   1.133 +//
   1.134 +    {
   1.135 +    __PRINT1(_L("CFatFileCB::CheckPosL(%d)"), aPos);
   1.136 +    if (aPos==iCurrentPos.iPos)
   1.137 +        return;
   1.138 +    __ASSERT_DEBUG(aPos <= (TUint)Size(), Fault(EFatFilePosBeyondEnd));
   1.139 +
   1.140 +    if (iFileSizeModified && IsSeekBackwards(aPos))
   1.141 +        FlushDataL(); 
   1.142 +    
   1.143 +    TUint newRelCluster=aPos>>ClusterSizeLog2();
   1.144 +    if ( aPos && (aPos==(newRelCluster<<ClusterSizeLog2())) )
   1.145 +        newRelCluster--;
   1.146 +    TUint oldRelCluster=iCurrentPos.iPos>>ClusterSizeLog2();
   1.147 +    if ( iCurrentPos.iPos && (iCurrentPos.iPos==(oldRelCluster<<ClusterSizeLog2())) )
   1.148 +        oldRelCluster--;    
   1.149 +    TInt clusterOffset=newRelCluster-oldRelCluster;
   1.150 +    TInt oldCluster=iCurrentPos.iCluster;
   1.151 +    iCurrentPos.iPos=aPos;
   1.152 +    if (clusterOffset==0)
   1.153 +        return;
   1.154 +    TInt seekOffset=clusterOffset;
   1.155 +    if (iSeekIndex!=NULL)
   1.156 +        { // Can alter iCurrentPos.iCluster
   1.157 +        seekOffset=SeekToPosition(newRelCluster,seekOffset);
   1.158 +        if (seekOffset==0)
   1.159 +            return;
   1.160 +        }
   1.161 +    if (clusterOffset==-1 && seekOffset!=1)
   1.162 +        { // Check previous cluster
   1.163 +        TInt cluster=oldCluster-1;
   1.164 +        if (FAT().GetNextClusterL(cluster) && cluster==oldCluster)
   1.165 +            {
   1.166 +            iCurrentPos.iCluster=oldCluster-1;
   1.167 +            return;
   1.168 +            }
   1.169 +        }
   1.170 +    if (seekOffset<0)
   1.171 +        {
   1.172 +        seekOffset=newRelCluster;
   1.173 +        iCurrentPos.iCluster=iStartCluster;
   1.174 +        }
   1.175 +    while (seekOffset--)
   1.176 +        {
   1.177 +        if (!FAT().GetNextClusterL(iCurrentPos.iCluster))
   1.178 +            {
   1.179 +            __PRINT(_L("CFatFileCB::CheckPosL() corrupt#1"));
   1.180 +            User::Leave(KErrCorrupt);
   1.181 +            }
   1.182 +        TInt cluster=newRelCluster-seekOffset;
   1.183 +        if (iSeekIndex!=NULL && cluster && (cluster>>iSeekIndexSize)<<iSeekIndexSize==cluster)
   1.184 +            SetSeekIndexValueL(cluster,iCurrentPos.iCluster);
   1.185 +        }
   1.186 +    }
   1.187 +
   1.188 +void CFatFileCB::SetL(const TFatDirEntry& aFatDirEntry,TShare aShare,const TEntryPos& aPos)
   1.189 +//
   1.190 +// Initialize FileCB from entry data
   1.191 +//
   1.192 +    {
   1.193 +
   1.194 +    __PRINT(_L("CFatFileCB::SetL"));
   1.195 +    SetSize(aFatDirEntry.Size()); 
   1.196 +    iCurrentPos.iCluster= FatMount().StartCluster(aFatDirEntry);
   1.197 +    iStartCluster=iCurrentPos.iCluster;
   1.198 +    iCurrentPos.iPos=0;
   1.199 +    iAtt=aFatDirEntry.Attributes();
   1.200 +    iModified= aFatDirEntry.Time(FatMount().TimeOffset());
   1.201 +    iShare=aShare;
   1.202 +    iFileDirPos=aPos;
   1.203 +
   1.204 +    SetMaxSupportedSize(KMaxSupportedFatFileSize);
   1.205 +    }
   1.206 +
   1.207 +//-----------------------------------------------------------------------------
   1.208 +// from CFileCB::MExtendedFileInterface
   1.209 +void CFatFileCB::ReadL(TInt64 aPos,TInt& aLength, TDes8* aDes, const RMessagePtr2& aMessage, TInt aOffset)
   1.210 +    {
   1.211 +    __PRINT2(_L("CFatFileCB::ReadL aFilePos=%LU aLength=%d"),aPos,aLength);
   1.212 +    
   1.213 +    if((TUint64)aPos > KMaxSupportedFatFileSize-1)
   1.214 +        User::Leave(KErrNotSupported);  //-- max. position in the file is 0xFFFFFFFE
   1.215 +
   1.216 +    FatMount().CheckStateConsistentL();
   1.217 +    
   1.218 +    CheckPosL(I64LOW(aPos));
   1.219 +    
   1.220 +    const TUint startPos = iCurrentPos.iPos;
   1.221 +    const TUint curSize  = (TUint)Size();
   1.222 +    const TUint length   = (TUint)aLength;
   1.223 +    
   1.224 +    if((startPos + length > curSize) || (startPos > startPos + length) )
   1.225 +        aLength=curSize-startPos;
   1.226 +    
   1.227 +    FatMount().ReadFromClusterListL(iCurrentPos,aLength,aDes,aMessage,aOffset);
   1.228 +    aLength=iCurrentPos.iPos-startPos;
   1.229 +    }
   1.230 +
   1.231 +
   1.232 +void CFatFileCB::ReadL(TInt aFilePos,TInt& aLength,const TAny* aTrg,const RMessagePtr2& aMessage)
   1.233 +    {
   1.234 +    ReadL(TInt64(aFilePos),aLength,(TDes8*) aTrg,aMessage, 0);
   1.235 +    }
   1.236 +
   1.237 +//-----------------------------------------------------------------------------
   1.238 +// from CFileCB::MExtendedFileInterface
   1.239 +void CFatFileCB::WriteL(TInt64 aPos,TInt& aLength,const TDesC8* aSrc,const RMessagePtr2& aMessage, TInt aOffset)
   1.240 +    {
   1.241 +    __PRINT2(_L("CFatFileCB::WriteL aFilePos=%LU aLength=%d"),aPos,aLength);
   1.242 +    // FAT supports 32 bits only for file size
   1.243 +    TUint64 endPos = aPos + aLength;
   1.244 +    if(endPos > KMaxSupportedFatFileSize)
   1.245 +        User::Leave(KErrNotSupported);
   1.246 +    
   1.247 +    FatMount().CheckStateConsistentL();
   1.248 +    FatMount().CheckWritableL();
   1.249 +    const TUint pos = I64LOW(aPos);
   1.250 +    CheckPosL(pos);
   1.251 +    
   1.252 +    const TUint startCluster = (TUint)iStartCluster;
   1.253 +    const TUint length       = (TUint)aLength;
   1.254 +    
   1.255 +    endPos = iCurrentPos.iPos + length; 
   1.256 +    if ((endPos           > (TUint)Size()) ||
   1.257 +        (iCurrentPos.iPos > endPos)         ) // Overflow condition 
   1.258 +        DoSetSizeL(iCurrentPos.iPos+length,EFalse);
   1.259 +    
   1.260 +    TUint startPos=iCurrentPos.iPos;
   1.261 +    TInt badcluster=0;
   1.262 +    TInt goodcluster=0;
   1.263 +    
   1.264 +    TRAPD(ret, FatMount().WriteToClusterListL(iCurrentPos,aLength,aSrc,aMessage,aOffset,badcluster, goodcluster));
   1.265 +    
   1.266 +    if (ret == KErrCorrupt || ret == KErrDied)
   1.267 +        {
   1.268 +        if(startCluster == 0)
   1.269 +            { //Empty File, revert all the clusters allocated.
   1.270 +            TInt cluster = iStartCluster;
   1.271 +            iStartCluster = 0;
   1.272 +            SetSize(0);
   1.273 +            FlushAllL();
   1.274 +
   1.275 +            iCurrentPos.iCluster = 0;
   1.276 +            iCurrentPos.iPos = 0;
   1.277 +
   1.278 +            FAT().FreeClusterListL(cluster);
   1.279 +            FAT().FlushL();
   1.280 +            }
   1.281 +        else
   1.282 +            { //Calculate the clusters required based on file size, revert extra clusters if allocated.
   1.283 +            const TUint curSize = (TUint)Size();
   1.284 +            TUint ClustersNeeded = curSize >> ClusterSizeLog2();
   1.285 +            if(curSize > (ClustersNeeded << ClusterSizeLog2()))
   1.286 +                {
   1.287 +                ClustersNeeded++;
   1.288 +                }
   1.289 +
   1.290 +            TInt cluster = iStartCluster;
   1.291 +            while(--ClustersNeeded)
   1.292 +                {
   1.293 +                FAT().GetNextClusterL(cluster);
   1.294 +                }
   1.295 +                
   1.296 +            iCurrentPos.iCluster = cluster;
   1.297 +
   1.298 +            if (FAT().GetNextClusterL(cluster))
   1.299 +                {
   1.300 +                FAT().FreeClusterListL(cluster);
   1.301 +                }
   1.302 +
   1.303 +            FAT().WriteFatEntryEofL(iCurrentPos.iCluster);
   1.304 +            FAT().FlushL();
   1.305 +            }
   1.306 +        }
   1.307 +
   1.308 +    User::LeaveIfError(ret);
   1.309 +
   1.310 +    if(badcluster != 0)
   1.311 +        {
   1.312 +        if(iStartCluster == badcluster)
   1.313 +            {
   1.314 +            iStartCluster = goodcluster;
   1.315 +            FlushStartClusterL();
   1.316 +            }
   1.317 +        else
   1.318 +            {
   1.319 +            TInt aCluster = iStartCluster;
   1.320 +            do
   1.321 +                {
   1.322 +                if((TUint)badcluster == FAT().ReadL(aCluster))
   1.323 +                    {
   1.324 +                    FAT().WriteL(aCluster, goodcluster);
   1.325 +                    FAT().FlushL();
   1.326 +                    break;
   1.327 +                    }
   1.328 +                }
   1.329 +            while(FAT().GetNextClusterL(aCluster));
   1.330 +            }
   1.331 +        }
   1.332 +    aLength=iCurrentPos.iPos-startPos;
   1.333 +
   1.334 +    if(FatMount().IsRuggedFSys() && pos+(TUint)aLength>(TUint)Size())
   1.335 +        {
   1.336 +        WriteFileSizeL(pos+aLength);
   1.337 +        }
   1.338 +
   1.339 +    }
   1.340 +
   1.341 +
   1.342 +void CFatFileCB::WriteL(TInt aFilePos,TInt& aLength,const TAny* aSrc,const RMessagePtr2& aMessage)
   1.343 +    {
   1.344 +    WriteL(TInt64(aFilePos),aLength,(TDesC8*) aSrc,aMessage, 0);
   1.345 +    }
   1.346 +
   1.347 +
   1.348 +
   1.349 +//-----------------------------------------------------------------------------
   1.350 +
   1.351 +void CFatFileCB::ResizeIndex(TInt aNewMult,TUint aNewSize)
   1.352 +//
   1.353 +// Resize the seek index to accomodate a larger or smaller filesize
   1.354 +// Assumes KSeekIndexSize is a power of 2.
   1.355 +//
   1.356 +    {
   1.357 +
   1.358 +    TInt maxNewIndex=aNewSize>>(ClusterSizeLog2()+aNewMult);
   1.359 +
   1.360 +
   1.361 +    TInt    index=0;
   1.362 +    TInt    indexEnd=KSeekIndexSize;
   1.363 +    TInt    newValEnd=maxNewIndex;
   1.364 +
   1.365 +    if (iSeekIndexSize<aNewMult)
   1.366 +        {
   1.367 +        TInt newVal=index;
   1.368 +        TInt step=1<<(aNewMult-iSeekIndexSize);
   1.369 +        index+=step-1;
   1.370 +        while(index<indexEnd && newVal<newValEnd)
   1.371 +            {
   1.372 +            iSeekIndex[newVal] =  iSeekIndex[index];
   1.373 +            newVal++;
   1.374 +            index+=step;
   1.375 +            }
   1.376 +        while(newVal<indexEnd)
   1.377 +            iSeekIndex[newVal++] =  0;
   1.378 +        }
   1.379 +    else
   1.380 +        {
   1.381 +        TInt diffSize = iSeekIndexSize-aNewMult;
   1.382 +        TInt oldVal=(KSeekIndexSize>>diffSize) - 1;
   1.383 +        TInt newVal=indexEnd-1;
   1.384 +        TInt skip=(1<<diffSize)-1;
   1.385 +
   1.386 +        if ((iSeekIndexSize - aNewMult) > KSeekIndexSizeLog2)
   1.387 +            {
   1.388 +            ClearIndex(0); //-- Invalidate every entry.
   1.389 +            }
   1.390 +        else
   1.391 +            {
   1.392 +            while(newVal>=index)
   1.393 +                {
   1.394 +
   1.395 +                iSeekIndex[newVal--] =  iSeekIndex[oldVal--];
   1.396 +
   1.397 +
   1.398 +                for(TInt i=skip;i>0;i--)
   1.399 +                    {   
   1.400 +                    iSeekIndex[newVal--] = 0;
   1.401 +
   1.402 +                    }
   1.403 +                }
   1.404 +            }
   1.405 +        }
   1.406 +    iSeekIndexSize=aNewMult;
   1.407 +    }
   1.408 +
   1.409 +
   1.410 +/**
   1.411 +    Zero freed clusters in the index
   1.412 +
   1.413 +    @param  aNewSize new size of the file that the index corresponds to.
   1.414 +            if = 0  all existing index will be zero filled
   1.415 +*/ 
   1.416 +void CFatFileCB::ClearIndex(TUint aNewSize)
   1.417 +    {
   1.418 +
   1.419 +    if (!iSeekIndex)
   1.420 +        return;
   1.421 +
   1.422 +    if(aNewSize==0)
   1.423 +        {
   1.424 +        //-- zero fill all the array
   1.425 +        Mem::FillZ(iSeekIndex, KSeekIndexSize*sizeof(TUint32));
   1.426 +        return;
   1.427 +        }
   1.428 +
   1.429 +    // Files that fill up a cluster exactly do not have a trailing empty
   1.430 +    // cluster. So the entry for that position must also be invalidated
   1.431 +    aNewSize--;
   1.432 +    TInt firstInvalidIndex=aNewSize>>(iSeekIndexSize+ClusterSizeLog2());
   1.433 +        
   1.434 +    TInt indexLen=KSeekIndexSize-firstInvalidIndex;
   1.435 +
   1.436 +    Mem::FillZ(iSeekIndex+firstInvalidIndex, indexLen * sizeof(TUint32));
   1.437 +    }
   1.438 +
   1.439 +TInt CFatFileCB::CalcSeekIndexSize(TUint aSize)
   1.440 +//
   1.441 +// Find the nearest power of 2 > aSize
   1.442 +//
   1.443 +    {
   1.444 +    TInt count = 0;
   1.445 +    const TUint indexSize=KSeekIndexSize<<ClusterSizeLog2();//KSeekIndexSize=128
   1.446 +    if (aSize<=indexSize)
   1.447 +      return(count);
   1.448 +    
   1.449 +    while((aSize>>=1)>0)
   1.450 +        {
   1.451 +        count++;
   1.452 +        }
   1.453 +    return (count - (KSeekIndexSizeLog2 + ClusterSizeLog2()) + 1);
   1.454 +    }
   1.455 +
   1.456 +//-----------------------------------------------------------------------------
   1.457 +
   1.458 +void CFatFileCB::SetSizeL(TInt64 aSize)
   1.459 +    {
   1.460 +    __PRINT(_L("CFatFileCB::SetSizeL"));
   1.461 +    
   1.462 +    // FAT supports 32 bits only for file size
   1.463 +    if (I64HIGH(aSize))
   1.464 +        User::Leave(KErrNotSupported);
   1.465 +
   1.466 +    if(FatMount().IsRuggedFSys())
   1.467 +        DoSetSizeL(I64LOW(aSize),ETrue);
   1.468 +    else
   1.469 +        DoSetSizeL(I64LOW(aSize),EFalse);
   1.470 +    }
   1.471 +
   1.472 +
   1.473 +void CFatFileCB::SetSizeL(TInt aSize)
   1.474 +//
   1.475 +// Envelope function around DoSetSizeL to enable aSize to
   1.476 +// be written to disk for rugged fat file system
   1.477 +//
   1.478 +    {
   1.479 +    SetSizeL(TInt64(aSize));
   1.480 +    }
   1.481 +
   1.482 +void CFatFileCB::DoSetSizeL(TUint aSize,TBool aIsSizeWrite)
   1.483 +//
   1.484 +// Extend or truncate the file.
   1.485 +// Expects the modified attribute and iSize are set afterwards.
   1.486 +// Does not alter iCurrentPos, the current file position.
   1.487 +// Writes size of file to disk if aIsSizeWrite set
   1.488 +//
   1.489 +    {
   1.490 +    __PRINT2(_L("CFatFileCB::DoSetSizeL sz:%d, fileWrite=%d"),aSize ,aIsSizeWrite);
   1.491 +
   1.492 +    FatMount().CheckStateConsistentL();
   1.493 +    FatMount().CheckWritableL();
   1.494 +
   1.495 +    
   1.496 +    // Can not change the file size if it is clamped
   1.497 +    if(Mount().IsFileClamped(MAKE_TINT64(0,iStartCluster)) > 0)
   1.498 +        User::Leave(KErrInUse);
   1.499 +    
   1.500 +    iFileSizeModified=ETrue;
   1.501 +
   1.502 +    TInt newIndexMult=CalcSeekIndexSize(aSize);
   1.503 +    if (iSeekIndex!=NULL && newIndexMult!=iSeekIndexSize)
   1.504 +        ResizeIndex(newIndexMult,aSize);
   1.505 +    if (aSize == 0)
   1.506 +        {
   1.507 +        if (Size() != 0)
   1.508 +            {
   1.509 +            ClearIndex(0); //-- clear seek index array
   1.510 +            TInt cluster=iStartCluster;
   1.511 +            iStartCluster = 0;
   1.512 +            SetSize(0);
   1.513 +            FlushAllL();
   1.514 +            CheckPosL(0);
   1.515 +            FAT().FreeClusterListL(cluster);
   1.516 +            FAT().FlushL();
   1.517 +            }
   1.518 +        return;
   1.519 +        }
   1.520 +    if (aSize<(TUint)Size())
   1.521 +        {
   1.522 +        if(aIsSizeWrite)        // write file size if decreasing
   1.523 +                WriteFileSizeL(aSize);
   1.524 +        CheckPosL(aSize);
   1.525 +        TInt cluster=iCurrentPos.iCluster;
   1.526 +        if (FAT().GetNextClusterL(cluster))
   1.527 +            {
   1.528 +            FAT().WriteFatEntryEofL(iCurrentPos.iCluster);
   1.529 +            FAT().FreeClusterListL(cluster);
   1.530 +            }
   1.531 +        ClearIndex(aSize);
   1.532 +        FAT().FlushL();
   1.533 +        return;
   1.534 +        }
   1.535 +    
   1.536 +    TUint newSize=aSize>>ClusterSizeLog2(); //  Number of clusters we now need
   1.537 +    if (aSize > (newSize<<ClusterSizeLog2()))
   1.538 +        newSize++;  //  File size is not an exact multiple of cluster size
   1.539 +                    //  Increment the number of clusters required to accomodate tail
   1.540 +    
   1.541 +    if (iStartCluster==0)
   1.542 +        {
   1.543 +        //-- FAT().FreeClusterHint() will give us a hint of the last free cluster
   1.544 +        ClearIndex(0); //-- clear seek index array
   1.545 +        TInt tempStartCluster=FAT().AllocateClusterListL(newSize, FAT().FreeClusterHint());
   1.546 +        FAT().FlushL();
   1.547 +        iCurrentPos.iCluster=tempStartCluster;
   1.548 +        iStartCluster=tempStartCluster;
   1.549 +        SetSize(aSize);
   1.550 +        FlushAllL();
   1.551 +        }
   1.552 +    else
   1.553 +        {
   1.554 +        const TUint curSize = (TUint)Size(); 
   1.555 +        TUint oldSize=curSize>>ClusterSizeLog2();   //  Number of clusters we had previously
   1.556 +        if (curSize>(oldSize<<ClusterSizeLog2()))
   1.557 +            oldSize++;
   1.558 +    
   1.559 +        TInt newClusters=newSize-oldSize;   //  Number of clusters we need to prepare
   1.560 +        if (newClusters)
   1.561 +            {
   1.562 +            TEntryPos currentPos=iCurrentPos;
   1.563 +            CheckPosL(Size());
   1.564 +            FAT().ExtendClusterListL(newClusters,iCurrentPos.iCluster);
   1.565 +            iCurrentPos=currentPos;
   1.566 +            }
   1.567 +        FAT().FlushL();
   1.568 +        if(aIsSizeWrite)            // write file size if increasing
   1.569 +            WriteFileSizeL(aSize);
   1.570 +        }
   1.571 +    }
   1.572 +
   1.573 +//-----------------------------------------------------------------------------
   1.574 +/**
   1.575 +    Set the entry's attributes and modified time.
   1.576 +*/
   1.577 +void CFatFileCB::SetEntryL(const TTime& aTime,TUint aSetAttMask,TUint aClearAttMask)
   1.578 +    {
   1.579 +    __PRINT(_L("CFatFileCB::SetEntryL"));
   1.580 +    
   1.581 +    FatMount().CheckStateConsistentL();
   1.582 +    FatMount().CheckWritableL();
   1.583 +
   1.584 +    TUint setAttMask=aSetAttMask&KEntryAttMaskSupported;
   1.585 +    if (setAttMask|aClearAttMask)
   1.586 +        {
   1.587 +        iAtt|=setAttMask;
   1.588 +        iAtt&=(~aClearAttMask);
   1.589 +        }
   1.590 +    if (aSetAttMask&KEntryAttModified)
   1.591 +        iModified=aTime;
   1.592 +    iAtt|=KEntryAttModified;
   1.593 +    }
   1.594 +
   1.595 +/**
   1.596 +    This is a RuggedFAT - specific method. Writes file size to the corresponding field of this
   1.597 +    file direcrory entry.
   1.598 +*/
   1.599 +void CFatFileCB::WriteFileSizeL(TUint aSize)
   1.600 +    {
   1.601 +    __PRINT(_L("CFatFileCB::WriteFileSizeL"));
   1.602 +    TEntryPos entryPos=iFileDirPos;
   1.603 +    entryPos.iPos+=_FOFF(SFatDirEntry,iSize);
   1.604 +    TPtrC8 size((TUint8*)&aSize,sizeof(TUint));
   1.605 +    
   1.606 +    //-- use directory cache when dealing with directories
   1.607 +    FatMount().DirWriteL(entryPos,size);
   1.608 +    iFileSizeModified=EFalse;
   1.609 +    }
   1.610 +
   1.611 +//-----------------------------------------------------------------------------
   1.612 +/** 
   1.613 +    Flush file size, attributes, time etc. to the media.
   1.614 +    It doesn't matter if whole directory entry is being written of only part of it. Anyway, a single DOS
   1.615 +    dir. entry always fits into 1 sector.
   1.616 +*/
   1.617 +void CFatFileCB::FlushDataL()
   1.618 +    {
   1.619 +    __PRINT(_L("CFatFileCB::FlushDataL"));
   1.620 +    FlushAllL();
   1.621 +    }
   1.622 +
   1.623 +//-----------------------------------------------------------------------------
   1.624 +/** 
   1.625 +    Flush the fide directory entry data: files size, attributes, time etc. 
   1.626 +*/
   1.627 +void CFatFileCB::FlushAllL()
   1.628 +    {
   1.629 +    __PRINT(_L("CFatFileCB::FlushAllL()"));
   1.630 +
   1.631 +    if (Mount().IsCurrentMount()==EFalse)
   1.632 +        User::Leave(KErrDisMounted);
   1.633 +
   1.634 +    FatMount().CheckStateConsistentL();
   1.635 +    FatMount().CheckWritableL();
   1.636 +
   1.637 +    TFatDirEntry entry;
   1.638 +    FatMount().ReadDirEntryL(iFileDirPos,entry);
   1.639 +    __ASSERT_ALWAYS(entry.IsEndOfDirectory()==EFalse,User::Leave(KErrCorrupt));
   1.640 +    entry.SetAttributes(iAtt&KEntryAttMaskSupported);
   1.641 +    entry.SetSize(Size());
   1.642 +    entry.SetTime(iModified, FatMount().TimeOffset());
   1.643 +    entry.SetStartCluster(iStartCluster);
   1.644 +
   1.645 +    TBool setNotify = FatMount().GetNotifyUser();
   1.646 +    if(setNotify)
   1.647 +        {
   1.648 +        FatMount().SetNotifyOff();  // do not launch a notifier
   1.649 +        }
   1.650 +
   1.651 +    TRAPD(ret, FatMount().WriteDirEntryL(iFileDirPos,entry));
   1.652 +    
   1.653 +    if(setNotify)
   1.654 +        {
   1.655 +        FatMount().SetNotifyOn();
   1.656 +        }
   1.657 +
   1.658 +    User::LeaveIfError(ret);
   1.659 +    iAtt&=(~KEntryAttModified);
   1.660 +    iFileSizeModified=EFalse;
   1.661 +    }
   1.662 +
   1.663 +//-----------------------------------------------------------------------------
   1.664 +
   1.665 +/**
   1.666 +    Rename already opened file.
   1.667 +    @param  aNewName new file name; all trailing dots from the name will be removed
   1.668 +*/
   1.669 +void CFatFileCB::RenameL(const TDesC& aNewName)
   1.670 +    {
   1.671 +    __PRINT2(_L("CFatFileCB::RenameL[0x%x], name:%S"),this, &aNewName);
   1.672 +
   1.673 +    FatMount().CheckStateConsistentL();
   1.674 +    FatMount().CheckWritableL();
   1.675 +
   1.676 +    const TPtrC fileName = RemoveTrailingDots(aNewName); //-- remove trailing dots from the name
   1.677 +
   1.678 +
   1.679 +    FatMount().DoRenameOrReplaceL(*iFileName, fileName, CFatMountCB::EModeRename,iFileDirPos);
   1.680 +    
   1.681 +    AllocBufferL(iFileName, fileName);
   1.682 +    
   1.683 +    if(!FatMount().IsRuggedFSys())
   1.684 +        FAT().FlushL();
   1.685 +    }
   1.686 +
   1.687 +
   1.688 +//***********************************************************
   1.689 +//* BlockMap interface
   1.690 +//***********************************************************
   1.691 +    
   1.692 +TInt CFatFileCB::BlockMap(SBlockMapInfo& aInfo, TInt64& aStartPos, TInt64 aEndPos)
   1.693 +//
   1.694 +// Retrieves the block map of a given section of the file, in the FAT file system.
   1.695 +//  
   1.696 +    {
   1.697 +    __PRINT2(_L("CFatFileCB::BlockMap aStartPos=%ld aEndPos=%ld"), aStartPos, aEndPos);
   1.698 +    
   1.699 +    if ( I64HIGH(aStartPos) || I64HIGH(aEndPos) )
   1.700 +        return KErrNotSupported;
   1.701 +
   1.702 +    TUint startPos = I64LOW(aStartPos);
   1.703 +    TUint endPos = I64LOW(aEndPos);
   1.704 +
   1.705 +    // aEndPos will always be >=0 at this point
   1.706 +    const TUint length = endPos - startPos;
   1.707 +    
   1.708 +    // Store the position of cluster zero in aInfo
   1.709 +    CFatMountCB& fatMount = FatMount();
   1.710 +
   1.711 +    TInt drvNo=-1;
   1.712 +    TBusLocalDrive* locDrv;
   1.713 +    if((fatMount.LocalDrive()->GetLocalDrive(locDrv)==KErrNone) && ((drvNo=GetLocalDriveNumber(locDrv))>=0) && (drvNo<KMaxLocalDrives))
   1.714 +        aInfo.iLocalDriveNumber=drvNo;
   1.715 +    else
   1.716 +        return KErrNotSupported;
   1.717 +
   1.718 +    // Fetch the address of cluster 0
   1.719 +    aInfo.iStartBlockAddress = fatMount.FAT().DataPositionInBytes(KFirstClusterNum);
   1.720 +
   1.721 +    TRAPD(r, CheckPosL(startPos));
   1.722 +    if (r != KErrNone)
   1.723 +        return r;
   1.724 +
   1.725 +    aInfo.iBlockStartOffset = fatMount.ClusterRelativePos(iCurrentPos.iPos);
   1.726 +    aInfo.iBlockGranularity = 1 << FatMount().ClusterSizeLog2();
   1.727 +    const TUint myStartPos = iCurrentPos.iPos;
   1.728 +    if ( myStartPos + length > (TUint)Size())
   1.729 +        return KErrArgument;
   1.730 +
   1.731 +    TRAP(r, FatMount().BlockMapReadFromClusterListL(iCurrentPos, length, aInfo));
   1.732 +    if (r != KErrNone)
   1.733 +        return r;
   1.734 +
   1.735 +    aStartPos = iCurrentPos.iPos;
   1.736 +    if ((I64LOW(aStartPos) == (TUint)Size()) || ( I64LOW(aStartPos) == (myStartPos + length)))
   1.737 +        return KErrCompletion;
   1.738 +    else
   1.739 +        return KErrNone;
   1.740 +    }
   1.741 +
   1.742 +
   1.743 +
   1.744 +TInt CFatFileCB::GetInterface(TInt aInterfaceId,TAny*& aInterface,TAny* aInput)
   1.745 +    {
   1.746 +    switch(aInterfaceId)
   1.747 +        {
   1.748 +        case EExtendedFileInterface:
   1.749 +            ((CFileCB::MExtendedFileInterface*&) aInterface) = this;
   1.750 +            return KErrNone;
   1.751 +
   1.752 +        case EBlockMapInterface:
   1.753 +            aInterface = (CFileCB::MBlockMapInterface*) this;
   1.754 +            return KErrNone;
   1.755 +
   1.756 +        case EGetLocalDrive:
   1.757 +            return FatMount().LocalDrive()->GetLocalDrive((TBusLocalDrive*&) aInterface);
   1.758 +
   1.759 +        default:
   1.760 +            return CFileCB::GetInterface(aInterfaceId,aInterface,aInput);
   1.761 +        }
   1.762 +    }
   1.763 +
   1.764 +
   1.765 +
   1.766 +
   1.767 +/**
   1.768 +    Overwrites file's start cluster (iStartCluster) in its directory entry.
   1.769 +*/
   1.770 +void CFatFileCB::FlushStartClusterL()
   1.771 +    {
   1.772 +    __PRINT(_L("CFatFileCB::FlushStartClusterL"));
   1.773 +
   1.774 +    CFatMountCB& mount = FatMount();
   1.775 +    TFatDirEntry dirEntry;
   1.776 +    
   1.777 +    mount.ReadDirEntryL(iFileDirPos, dirEntry);      //-- read this file's dir. entry
   1.778 +    dirEntry.SetStartCluster(iStartCluster);         //-- set new start cluster
   1.779 +    mount.WriteDirEntryL(iFileDirPos, dirEntry);//-- write the entry back
   1.780 +    }
   1.781 +
   1.782 +
   1.783 +
   1.784 +
   1.785 +