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