userlibandfileserver/fileserver/sfat32/inc/sl_std.h
changeset 0 a41df078684a
child 6 0173bcd7697c
equal deleted inserted replaced
-1:000000000000 0:a41df078684a
       
     1 // Copyright (c) 1998-2009 Nokia Corporation and/or its subsidiary(-ies).
       
     2 // All rights reserved.
       
     3 // This component and the accompanying materials are made available
       
     4 // under the terms of the License "Eclipse Public License v1.0"
       
     5 // which accompanies this distribution, and is available
       
     6 // at the URL "http://www.eclipse.org/legal/epl-v10.html".
       
     7 //
       
     8 // Initial Contributors:
       
     9 // Nokia Corporation - initial contribution.
       
    10 //
       
    11 // Contributors:
       
    12 //
       
    13 // Description:
       
    14 // fileserver\sfat32\inc\sl_std.h
       
    15 //
       
    16 
       
    17 /**
       
    18  @file
       
    19  @internalTechnology
       
    20 */
       
    21 
       
    22 #ifndef SL_STD_H
       
    23 #define SL_STD_H
       
    24 
       
    25 
       
    26 //
       
    27 // #define _DEBUG_RELEASE
       
    28 //
       
    29 
       
    30 #include "common.h"
       
    31 #include <f32ver.h>
       
    32 #include <e32svr.h>
       
    33 #include <kernel/localise.h>
       
    34 #include "filesystem_fat.h"
       
    35 
       
    36 #include "common_constants.h"
       
    37 #include "sl_bpb.h"
       
    38 #include "fat_config.h"
       
    39 #include "fat_dir_entry.h"
       
    40 
       
    41 using namespace FileSystem_FAT;
       
    42 
       
    43 #ifdef _DEBUG
       
    44 _LIT(KThisFsyName,"EFAT32.FSY"); ///< This FSY name
       
    45 #endif
       
    46 
       
    47 //-----------------------------------------------------------------------------
       
    48 //-- FAT32 specific declarations
       
    49 
       
    50 const TUint32 KFat32EntryMask = 0x0FFFFFFF;
       
    51 
       
    52 //-----------------------------------------------------------------------------
       
    53 
       
    54 class CFatMountCB;
       
    55 class CFatFileSystem;
       
    56 
       
    57 /**
       
    58 Represents the position of a directory entery in terms of a cluster and off set into it
       
    59 */
       
    60 class TEntryPos
       
    61 	{
       
    62 public:
       
    63 	TEntryPos() {}
       
    64 	TEntryPos(TInt aCluster,TUint aPos) : iCluster(aCluster), iPos(aPos) {}
       
    65 
       
    66     inline TUint32 Cluster() const;
       
    67     inline TUint32 Pos() const;
       
    68     inline TBool operator==(const TEntryPos& aRhs) const;
       
    69 
       
    70 public:
       
    71 	TInt iCluster;
       
    72 	TUint iPos;
       
    73 	};
       
    74 
       
    75 
       
    76 /**
       
    77     Interface class between the file system and the local drive media interface,
       
    78     handles incomplete writes to media with the ability to notify the user.
       
    79     This class can't be instantinated by user; only CFatMountCB can do this; see CFatMountCB::DriveInterface()
       
    80 
       
    81 */
       
    82 class TFatDriveInterface
       
    83     {
       
    84 public:
       
    85 	enum TAction {ERetry=1};
       
    86 
       
    87 public:
       
    88 
       
    89     //-- public interface to the local drive. Provides media driver's error handling (critical and non-critical user notifiers)
       
    90     //-- and thread-safety if required.
       
    91 	TInt ReadNonCritical(TInt64 aPos,TInt aLength,const TAny* aTrg,const RMessagePtr2 &aMessage,TInt anOffset) const;
       
    92 	TInt ReadNonCritical(TInt64 aPos,TInt aLength,TDes8& aTrg) const;
       
    93 	TInt ReadCritical(TInt64 aPos,TInt aLength,TDes8& aTrg) const;
       
    94 	
       
    95     TInt WriteCritical(TInt64 aPos,const TDesC8& aSrc);
       
    96     TInt WriteNonCritical(TInt64 aPos,TInt aLength,const TAny* aSrc,const RMessagePtr2 &aMessage,TInt anOffset);
       
    97 	
       
    98     TInt GetLastErrorInfo(TDes8& aErrorInfo) const;
       
    99 
       
   100     //-- lock the mutex guarding CProxyDrive interface in order to be sure that no other thread can access it.
       
   101     //-- The thread that calls this method may be suspended until another signals the mutex, i.e. leaves the critical section.
       
   102     inline void AcquireLock() const {iProxyDrive.EnterCriticalSection();}
       
   103     
       
   104     //-- release the mutex guarding CProxyDrive.
       
   105     inline void ReleaseLock() const {iProxyDrive.LeaveCriticalSection();} 
       
   106 
       
   107 
       
   108 protected:
       
   109     TFatDriveInterface();
       
   110     TFatDriveInterface(const TFatDriveInterface&);
       
   111     TFatDriveInterface& operator=(const TFatDriveInterface&);
       
   112 
       
   113     TBool Init(CFatMountCB* aMount);
       
   114     void Close(); 
       
   115 
       
   116 	inline TBool NotifyUser() const;
       
   117 	TInt HandleRecoverableError(TInt aRes) const;
       
   118 	TInt HandleCriticalError(TInt aRes) const;
       
   119 	TInt UnlockAndReMount() const;
       
   120 	TBool IsDriveWriteProtected() const;
       
   121 	TBool IsRecoverableRemount() const;
       
   122 
       
   123 private:
       
   124 	
       
   125 	/** 
       
   126         An internal class that represents a thread-safe wrapper around raw interface to the CProxyDrive 
       
   127         and restricts access to it.
       
   128     */
       
   129     class XProxyDriveWrapper
       
   130         {
       
   131      public:
       
   132        
       
   133         XProxyDriveWrapper();
       
   134        ~XProxyDriveWrapper();
       
   135 
       
   136         TBool Init(CProxyDrive* aProxyDrive);
       
   137             
       
   138         inline void EnterCriticalSection() const {iLock.Wait();}
       
   139         inline void LeaveCriticalSection() const {iLock.Signal();}
       
   140 
       
   141         //-- methods' wrappers that are used by TFatDriveInterface
       
   142         TInt Read(TInt64 aPos,TInt aLength,const TAny* aTrg,const RMessagePtr2 &aMessage,TInt anOffset) const;
       
   143         TInt Read(TInt64 aPos,TInt aLength,TDes8& aTrg) const;
       
   144         TInt Write(TInt64 aPos,TInt aLength,const TAny* aSrc,const RMessagePtr2 &aMessage,TInt anOffset);
       
   145         TInt Write(TInt64 aPos, const TDesC8& aSrc);
       
   146         TInt GetLastErrorInfo(TDes8& aErrorInfo) const;
       
   147         TInt Caps(TDes8& anInfo) const;
       
   148 
       
   149      private:
       
   150         CProxyDrive*    iLocalDrive; ///< raw interface to the media operations
       
   151         mutable RMutex  iLock;       ///< used for sorting out multithreaded access to the iLocalDrive
       
   152         };
       
   153     
       
   154     CFatMountCB*        iMount;      ///< Pointer to the owning file system mount
       
   155     XProxyDriveWrapper  iProxyDrive; ///< wrapper around raw interface to the media operations
       
   156     
       
   157     };
       
   158 
       
   159 
       
   160 /**
       
   161     Class providing FAT table interface and basic functionality.
       
   162 */	
       
   163 class CFatTable : public CBase
       
   164 	{
       
   165     
       
   166 public:
       
   167 	
       
   168     virtual ~CFatTable();
       
   169     
       
   170     static CFatTable* NewL(CFatMountCB& aOwner, const TLocalDriveCaps& aLocDrvCaps);
       
   171 
       
   172     /** FAT table mounting parameters */
       
   173     struct TMountParams
       
   174         {
       
   175         TMountParams() :iFreeClusters(0), iFirstFreeCluster(0), iFsInfoValid(0) {};
       
   176         
       
   177         TUint32 iFreeClusters;      ///< Free clusters count, obtained from FSInfo
       
   178         TUint32 iFirstFreeCluster;  ///< First free cluster,  obtained from FSInfo
       
   179         TUint32 iFsInfoValid : 1;   ///< ETrue if the information in iFreeClusters &  iFirstFreeCluster is valid
       
   180         };
       
   181 
       
   182     //-----------------------------------------------------------------
       
   183     //-- pure virtual interface
       
   184     virtual TUint32 ReadL(TUint32 aFatIndex) const = 0;
       
   185 	virtual void WriteL(TUint32 aFatIndex, TUint32 aValue) = 0;
       
   186 	virtual TInt64 DataPositionInBytes(TUint32 aCluster) const = 0;
       
   187     virtual void MountL(const TMountParams& aMountParam) = 0;
       
   188     //-----------------------------------------------------------------
       
   189     //-- just virtual interface
       
   190     
       
   191     virtual void Dismount(TBool /*aDiscardDirtyData*/) {}
       
   192 	virtual void FlushL() {};
       
   193     
       
   194     virtual void InvalidateCacheL() {};
       
   195     virtual void InvalidateCacheL(TInt64 /*aPos*/,TUint32 /*aLength*/) {};
       
   196 
       
   197 	virtual void FreeClusterListL(TUint32 aCluster);
       
   198 	virtual void ExtendClusterListL(TUint32 aNumber,TInt& aCluster);
       
   199 	
       
   200     virtual TUint32 AllocateSingleClusterL(TUint32 aNearestCluster);
       
   201 	virtual TUint32 AllocateClusterListL(TUint32 aNumber,TUint32 aNearestCluster);
       
   202     
       
   203     virtual void RequestRawWriteAccess(TInt64 /*aPos*/, TUint32 /*aLen*/) const {};
       
   204 
       
   205     virtual TUint32 FreeClusterHint() const; 
       
   206     virtual TUint32 NumberOfFreeClusters(TBool aSyncOperation=EFalse) const;
       
   207     virtual TBool RequestFreeClusters(TUint32 aClustersRequired) const; 
       
   208 
       
   209     virtual void InitializeL();
       
   210     virtual TBool ConsistentState() const {return ETrue;} //-- dummy
       
   211 
       
   212     //-----------------------------------------------------------------
       
   213     //-- non-virtual interface
       
   214     TBool GetNextClusterL(TInt& aCluster) const;
       
   215     void WriteFatEntryEofL(TUint32 aFatIndex);
       
   216 
       
   217     void MarkAsBadClusterL(TUint32 aCluster);
       
   218     TInt CountContiguousClustersL(TUint32 aStartCluster,TInt& anEndCluster, TUint32 aMaxCount) const;
       
   219     
       
   220     inline TUint32 MaxEntries() const;
       
   221     
       
   222     TUint32 PosInBytes(TUint32 aFatIndex) const;	    
       
   223 
       
   224 
       
   225 protected:
       
   226 	CFatTable(CFatMountCB& aOwner);
       
   227 
       
   228     //-- outlawed
       
   229     CFatTable();
       
   230     CFatTable(const CFatTable&);
       
   231     CFatTable& operator=(const CFatTable&);
       
   232 
       
   233     virtual void SetFreeClusterHint(TUint32 aCluster);
       
   234 
       
   235     virtual void DecrementFreeClusterCount(TUint32 aCount);
       
   236 	virtual void IncrementFreeClusterCount(TUint32 aCount);
       
   237 
       
   238     virtual void SetFreeClusters(TUint32 aFreeClusters); 
       
   239     virtual void CountFreeClustersL();
       
   240     virtual TUint32 FindClosestFreeClusterL(TUint32 aCluster);
       
   241     
       
   242     
       
   243     inline TInt     SectorSizeLog2() const;
       
   244     inline TUint32  FreeClusters() const;
       
   245 
       
   246 	inline TBool IsEof32Bit(TInt aCluster) const;
       
   247 	inline TBool IsEof16Bit(TInt aCluster) const;
       
   248 	inline TBool IsEof12Bit(TInt aCluster) const;
       
   249 	
       
   250 
       
   251     inline TFatType FatType() const;
       
   252     inline TBool IsFat12() const;
       
   253     inline TBool IsFat16() const;
       
   254     inline TBool IsFat32() const;
       
   255 
       
   256     inline TBool ClusterNumberValid(TUint32 aClusterNo) const;
       
   257 
       
   258     typedef RArray<TUint> RClusterArray;
       
   259     void DoFreedClustersNotify(RClusterArray &aFreedClusters);
       
   260 
       
   261 
       
   262 protected:
       
   263 	
       
   264     CFatMountCB* iOwner;            ///< Owning file system mount
       
   265     TUint        iMediaAtt;         ///< Cached copy of TLocalDriveCaps::iMediaAtt
       
   266 
       
   267 private:   
       
   268 
       
   269     TUint32  iFreeClusters;     ///< Number of free cluster in the fat table
       
   270 	TUint32  iFreeClusterHint;  ///< this is just a hint to the free cluster number, not required to contain exact information.
       
   271 	TFatType iFatType;          ///< FAT type 12/16/32, cached from the iOwner
       
   272     TUint32  iMaxEntries;       ///< maximal number of FAT entries in the table. This value is taken from the CFatMount that calculates it
       
   273 
       
   274     };
       
   275 
       
   276 
       
   277 class MWTCacheInterface;
       
   278 
       
   279 
       
   280 //---------------------------------------------------------------------------------------------------------------------------------
       
   281 
       
   282 /**
       
   283 Base class abstraction of a raw media disk
       
   284 */
       
   285 
       
   286 
       
   287 class CRawDisk : public CBase
       
   288 	{
       
   289 public:
       
   290 
       
   291     static CRawDisk* NewL(CFatMountCB& aOwner, const TLocalDriveCaps& aLocDrvCaps);
       
   292 
       
   293     virtual void InitializeL();
       
   294 	
       
   295     virtual TInt GetLastErrorInfo(TDes8& aErrorInfo) const;
       
   296 public:
       
   297 	
       
   298     /**
       
   299 	Read data from the media via simple WT data cache if it is present. Some media types, like RAM do not have caches.
       
   300     This method is mostly used to read UIDs of executable modules and store them in the cache.
       
   301 
       
   302 	@param aPos		Media position in bytes
       
   303 	@param aLength	Length in bytes of read
       
   304 	@param aDes		Data from read
       
   305 	*/
       
   306 	virtual void ReadCachedL(TInt64 aPos,TInt aLength,TDes8& aDes) const = 0;
       
   307 	
       
   308     /**
       
   309 	Write data to the media via simple WT data cache if it is present. Some media types, like RAM do not have caches.
       
   310 	@param aPos		Media position in bytes
       
   311 	@param aDes		Data to write
       
   312 	*/
       
   313 	virtual void WriteCachedL(TInt64 aPos,const TDesC8& aDes) = 0;
       
   314     
       
   315     virtual void InvalidateUidCache() {}
       
   316     virtual void InvalidateUidCachePage(TUint64 /*aPos*/) {}
       
   317     
       
   318 
       
   319 	/**
       
   320 	Disk read function
       
   321 	
       
   322 	@param aPos		Media position in bytes
       
   323 	@param aLength	Length in bytes of read
       
   324 	@param aTrg		Pointer to the data descriptor, i.e. (const TAny*)(&TDes8)
       
   325 	@param aMessage	Refrence to server message from request
       
   326 	@param anOffset	Offset into read data to write
       
   327 	*/
       
   328 	virtual void ReadL(TInt64 aPos,TInt aLength,const TAny* aTrg,const RMessagePtr2 &aMessage,TInt anOffset) const = 0;
       
   329 
       
   330 	/**
       
   331 	Disk write function
       
   332 
       
   333 	@param aPos		Media position in bytes
       
   334 	@param aLength	Length in bytes of write
       
   335 	@param aTrg		Pointer to the data descriptor, i.e. (const TAny*)(&TDes8)
       
   336 	@param aMessage	Refrence to server message from request, contains data
       
   337 	@param anOffset	Offset into write data to use in write
       
   338 	*/
       
   339 	virtual void WriteL(TInt64 aPos,TInt aLength,const TAny* aSrc,const RMessagePtr2 &aMessage,TInt anOffset) = 0;
       
   340 
       
   341     
       
   342     virtual inline MWTCacheInterface* DirCacheInterface();
       
   343 
       
   344 
       
   345 
       
   346 
       
   347 protected:
       
   348 
       
   349     CRawDisk(CFatMountCB& aOwner);
       
   350 
       
   351     //-- outlawed
       
   352     CRawDisk(); 
       
   353     CRawDisk(const CRawDisk&); 
       
   354     CRawDisk& operator=(const CRawDisk&);
       
   355 
       
   356 
       
   357 protected:
       
   358 
       
   359 	CFatMountCB* iFatMount; ///< Owning file system mount
       
   360     
       
   361     
       
   362 
       
   363 	};
       
   364 
       
   365 class CFatFileCB;
       
   366 class RBitVector;
       
   367 
       
   368 /** 
       
   369     A helper class. Holds the FAT volume parameters, which in turn are obtained from the Boot Sector
       
   370 */
       
   371 class TFatVolParam
       
   372     {
       
   373 public:   
       
   374 
       
   375     TFatVolParam();
       
   376     void Populate(const TFatBootSector& aBootSector);
       
   377     TBool operator==(const TFatVolParam& aRhs) const;
       
   378 
       
   379     //-- simple getters
       
   380     TUint32 ClusterSizeLog2() const     {return iClusterSizeLog2;    }        
       
   381     TUint32 SectorSizeLog2() const      {return iSectorSizeLog2;     }        
       
   382     TUint32 RootDirEnd() const          {return iRootDirEnd;         }        
       
   383     TUint32 SectorsPerCluster() const   {return iSectorsPerCluster;  }        
       
   384     TUint32 RootDirectorySector() const {return iRootDirectorySector;}        
       
   385     TUint32 FirstFatSector() const      {return iFirstFatSector;     }        
       
   386     TUint32 TotalSectors() const        {return iTotalSectors;       }        
       
   387     TUint32 NumberOfFats() const        {return iNumberOfFats;       }        
       
   388     TUint32 FatSizeInBytes() const      {return iFatSizeInBytes;     }        
       
   389     TUint32 RootClusterNum() const      {return iRootClusterNum;     }        
       
   390     TUint32 FSInfoSectorNum() const     {return iFSInfoSectorNum;    }        
       
   391     TUint32 BkFSInfoSectorNum() const   {return iBkFSInfoSectorNum;  }        
       
   392  
       
   393 protected:
       
   394     TUint32 iClusterSizeLog2;      ///< Log2 of fat file system cluster size
       
   395     TUint32 iSectorSizeLog2;       ///< Log2 of media sector size
       
   396     TUint32 iRootDirEnd;           ///< End position of the root directory for Fat12/16
       
   397     TUint32 iSectorsPerCluster;    ///< Sector per cluster ratio for mounted Fat file system volume 
       
   398     TUint32 iRootDirectorySector;  ///< Start sector of the root directory for Fat12/16
       
   399     TUint32 iFirstFatSector;       ///< Start sector of the first Fat table in volume
       
   400     TUint32 iTotalSectors;         ///< Total sectors on media partition
       
   401     TUint32 iNumberOfFats;         ///< Number of Fats the volume has
       
   402     TUint32 iFatSizeInBytes;       ///< Size of a single Fat table in volume
       
   403     TUint32 iRootClusterNum;       ///< Cluster number for Root directory, for Fat32
       
   404     TUint32 iFSInfoSectorNum;      ///< FSInfo Sector number. If 0, this means that corresponding value isn't set in BPB
       
   405     TUint32 iBkFSInfoSectorNum;    ///< backup FSInfo Sector number
       
   406     };
       
   407 
       
   408 
       
   409 TBool IsLegalDosName(const TDesC&  aName, TBool anAllowWildCards, TBool aUseExtendedChars, TBool aInScanDrive, TBool aAllowLowerCase, TBool aIsForFileCreation);
       
   410 TBool IsLegalDOSNameChar(TChar aCharacter, TBool aUseExtendedChars);
       
   411 
       
   412 TUint32 CalculatePageOffsetInCluster(TUint32 aPos, TUint aPageSzLog2);
       
   413 
       
   414 class CLruCache;
       
   415 class TLeafDirData;
       
   416 class CLeafDirCache;
       
   417 
       
   418 
       
   419 /**
       
   420 Fat file system mount implmentation, provides all that is required of a plug in
       
   421 file system mount as well as Fat mount specific functionality 
       
   422 */
       
   423 class CFatMountCB : public CLocDrvMountCB, 
       
   424 					public MFileSystemSubType,
       
   425 					public MFileSystemClusterSize,
       
   426 					public CMountCB::MFileAccessor,
       
   427 					public CMountCB::MFileExtendedInterface
       
   428 	{
       
   429 public:
       
   430 	static CFatMountCB* NewL();
       
   431 	~CFatMountCB();
       
   432     void ConstructL();
       
   433 
       
   434 public:
       
   435 	
       
   436 	//-- overrides from the abstract CMountCB
       
   437     void MountL(TBool aForceMount);
       
   438 	TInt ReMount();
       
   439 	void Dismounted();
       
   440 	void VolumeL(TVolumeInfo& aVolume) const;
       
   441 	void SetVolumeL(TDes& aName);
       
   442 	void MkDirL(const TDesC& aName);
       
   443 	void RmDirL(const TDesC& aName);
       
   444 	void DeleteL(const TDesC& aName);
       
   445 	void RenameL(const TDesC& anOldName,const TDesC& anNewName);
       
   446 	void ReplaceL(const TDesC& anOldName,const TDesC& anNewName);
       
   447 	void EntryL(const TDesC& aName,TEntry& anEntry) const;
       
   448 	void SetEntryL(const TDesC& aName,const TTime& aTime,TUint aMask,TUint aVal);
       
   449 	void FileOpenL(const TDesC& aName,TUint aMode,TFileOpen anOpen,CFileCB* aFile);
       
   450 	void DirOpenL(const TDesC& aName,CDirCB* aDir);
       
   451 	void RawReadL(TInt64 aPos,TInt aLength,const TAny* aTrg,TInt anOffset,const RMessagePtr2& aMessage) const;
       
   452 	void RawWriteL(TInt64 aPos,TInt aLength,const TAny* aSrc,TInt anOffset,const RMessagePtr2& aMessage);
       
   453 	void GetShortNameL(const TDesC& aLongName,TDes& aShortName);
       
   454 	void GetLongNameL(const TDesC& aShortName,TDes& aLongName);
       
   455 	void ReadSectionL(const TDesC& aName,TInt aPos,TAny* aTrg,TInt aLength,const RMessagePtr2& aMessage);
       
   456     TInt CheckDisk();
       
   457 	TInt ScanDrive();
       
   458 	TInt ControlIO(const RMessagePtr2& aMessage,TInt aCommand,TAny* aParam1,TAny* aParam2);
       
   459 	TInt Lock(TMediaPassword& aOld,TMediaPassword& aNew,TBool aStore);
       
   460 	TInt Unlock(TMediaPassword& aPassword,TBool aStore);
       
   461 	TInt ClearPassword(TMediaPassword& aPassword);
       
   462 	TInt ErasePassword();
       
   463 	TInt ForceRemountDrive(const TDesC8* aMountInfo,TInt aMountInfoMessageHandle,TUint aFlags);
       
   464     
       
   465     void FinaliseMountL();
       
   466     void FinaliseMountL(TInt aOperation, TAny* aParam1=NULL, TAny* aParam2=NULL);
       
   467     TInt MountControl(TInt aLevel, TInt aOption, TAny* aParam);
       
   468 	TTimeIntervalSeconds TimeOffset() const;
       
   469 
       
   470 protected:
       
   471 
       
   472     /** CFatMountCB states */
       
   473     enum  TFatMntState
       
   474         {
       
   475         ENotMounted = 0, ///< 0, initial state, not mounted     (mount state is inconsistent)
       
   476         EMounting,       ///< 1, Mounting started               (mount state is inconsistent)
       
   477         EInit_R,         ///< 2, Initialised and not written    (mount state is Consistent)
       
   478         EInit_W,         ///< 3, Initialised and written        (mount state is Consistent)
       
   479         EFinalised,      ///< 4, Finalised                      (mount state is Consistent)
       
   480         EDismounted,     ///< 5, Dismounted                     (mount state is inconsistent)
       
   481         EInit_Forced,    ///< 6, forcedly mounted, special case (mount state is inconsistent)
       
   482         };
       
   483 
       
   484     inline TFatMntState State() const;
       
   485     inline void SetState(TFatMntState aState); 
       
   486     TInt OpenMountForWrite();
       
   487     TInt IsFinalised(TBool& aFinalised);
       
   488 
       
   489     /** 
       
   490         A wrapper around TFatDriveInterface providing its instantination and destruction.
       
   491         You must not create objects of this class, use DriveInterface() instead.
       
   492     */
       
   493     class XDriveInterface: public TFatDriveInterface
       
   494         {
       
   495       public:
       
   496         XDriveInterface() : TFatDriveInterface() {}
       
   497         ~XDriveInterface() {Close();}
       
   498         TBool Init(CFatMountCB* aMount) {return TFatDriveInterface::Init(aMount);}
       
   499         };
       
   500 
       
   501 
       
   502 public:
       
   503     
       
   504 	enum TRenMode {EModeReplace,EModeRename};
       
   505 
       
   506     TBool ConsistentState() const; 
       
   507     void CheckWritableL() const;
       
   508     void CheckStateConsistentL() const;
       
   509 
       
   510 	inline TBool ReadOnly(void) const;
       
   511     inline void  SetReadOnly(TBool aReadOnlyMode);
       
   512 	inline TInt StartCluster(const TFatDirEntry & anEntry) const;
       
   513 	inline CRawDisk& RawDisk() const;
       
   514 	inline CFatFileSystem& FatFileSystem() const;
       
   515 	inline CFatTable& FAT() const;
       
   516 	inline TInt ClusterSizeLog2() const;
       
   517 	inline TInt SectorSizeLog2() const;
       
   518 	inline TInt TotalSectors() const;
       
   519 	inline TInt SectorsPerCluster() const;
       
   520 	inline TInt ClusterBasePosition() const;
       
   521     inline TInt RootDirectorySector() const;
       
   522     inline TUint RootDirEnd() const;
       
   523     inline TUint32 RootClusterNum() const;
       
   524 
       
   525 	inline TFatType FatType() const;
       
   526     inline TBool Is16BitFat() const;
       
   527     inline TBool Is32BitFat() const;
       
   528 
       
   529 	inline TUint32 MaxClusterNumber() const;
       
   530 	inline TInt StartOfFatInBytes() const;
       
   531     inline TUint32 FirstFatSector() const;
       
   532 
       
   533 	inline TInt NumberOfFats() const;
       
   534 	inline TInt FatSizeInBytes() const;
       
   535 	inline TInt ClusterRelativePos(TInt aPos) const;
       
   536 	inline TUint StartOfRootDirInBytes() const;
       
   537 	inline TUint32 UsableClusters() const;
       
   538 	inline TBool IsBadCluster(TInt aCluster) const;
       
   539 	inline TBool IsRuggedFSys() const;
       
   540 	inline void SetRuggedFSys(TBool aVal);
       
   541 	
       
   542 	inline TInt RootIndicator() const;
       
   543 	
       
   544     inline TBool IsRootDir(const TEntryPos &aEntry) const;
       
   545 	inline CAsyncNotifier* Notifier() const;
       
   546 	inline TFatDriveInterface& DriveInterface() const;
       
   547     
       
   548     void ReadUidL(TInt aCluster,TEntry& anEntry) const;
       
   549 	
       
   550     void ReadDirEntryL(const TEntryPos& aPos,TFatDirEntry& aDirEntry) const;
       
   551 	void WriteDirEntryL(const TEntryPos& aPos,const TFatDirEntry& aDirEntry);
       
   552 
       
   553     void DirReadL(const TEntryPos& aPos,TInt aLength,TDes8& aDes) const;
       
   554     void DirWriteL(const TEntryPos& aPos,const TDesC8& aDes);
       
   555 
       
   556 	void ReadFromClusterListL(TEntryPos& aPos,TInt aLength,const TAny* aTrg,const RMessagePtr2& aMessage,TInt anOffset) const;
       
   557 	void WriteToClusterListL(TEntryPos& aPos,TInt aLength,const TAny* aSrc,const RMessagePtr2& aMessage,TInt anOffset, TInt& aBadcluster, TInt &aGoodcluster);
       
   558 	void MoveToNextEntryL(TEntryPos& aPos) const;
       
   559 	void MoveToDosEntryL(TEntryPos& aPos,TFatDirEntry& anEntry) const;
       
   560 	void EnlargeL(TInt aSize);
       
   561 	void ReduceSizeL(TInt aPos,TInt aLength);
       
   562 	void DoDismount();
       
   563     TBool ProcessFSInfoSectors(CFatTable::TMountParams& aFatInitParams) const;
       
   564     
       
   565 
       
   566 	void DoRenameOrReplaceL(const TDesC& anOldName,const TDesC& aNewName,TRenMode aMode,TEntryPos& aNewPos);
       
   567 	void FindDosNameL(const TDesC& aName,TUint anAtt,TEntryPos& aDosEntryPos,TFatDirEntry& aDosEntry,TDes& aFileName,TInt anError) const;
       
   568 	
       
   569 	void Dismount();
       
   570 	TBool IsEndOfClusterCh(TInt aCluster) const;
       
   571 	void SetEndOfClusterCh(TInt &aCluster) const;
       
   572 	void InitializeRootEntry(TFatDirEntry & anEntry) const;
       
   573 
       
   574     TInt64 MakeLinAddrL(const TEntryPos& aPos) const;
       
   575 	
       
   576     inline const TFatConfig& FatConfig() const;
       
   577     TBool CheckVolumeTheSame();
       
   578 
       
   579     void InvalidateLeafDirCache();
       
   580     
       
   581     void BlockMapReadFromClusterListL(TEntryPos& aPos, TInt aLength, SBlockMapInfo& aInfo);
       
   582 	virtual TInt GetInterface(TInt aInterfaceId,TAny*& aInterface,TAny* aInput);
       
   583 	virtual TInt GetFileUniqueId(const TDesC& aName, TInt64& aUniqueId);
       
   584 	virtual TInt Spare3(TInt aVal, TAny* aPtr1, TAny* aPtr2);
       
   585 	virtual TInt Spare2(TInt aVal, TAny* aPtr1, TAny* aPtr2);
       
   586 	virtual TInt Spare1(TInt aVal, TAny* aPtr1, TAny* aPtr2);
       
   587 
       
   588 public:
       
   589 	
       
   590     // interface extension implementation
       
   591 	virtual TInt SubType(TDes& aName) const;
       
   592 	virtual TInt ClusterSize() const;
       
   593     virtual void ReadSection64L(const TDesC& aName, TInt64 aPos, TAny* aTrg, TInt aLength, const RMessagePtr2& aMessage);
       
   594 
       
   595 private:
       
   596     
       
   597     /** An ad hoc internal helper object for using in DoFindL() method and its derivatives */
       
   598     class TFindHelper
       
   599         {
       
   600         public:
       
   601             TFindHelper() {isInitialised = EFalse;}
       
   602             void  InitialiseL(const TDesC&  aTargetName);
       
   603             TBool MatchDosEntryName(const TUint8* apDosEntryName) const;
       
   604             TBool TrgtNameIsLegalDos() const {ASSERT (isInitialised) ;return isLegalDosName;}   
       
   605         
       
   606         private:
       
   607             TFindHelper(const TFindHelper&);
       
   608             TFindHelper& operator=(const TFindHelper&);
       
   609         public:
       
   610             TPtrC       iTargetName;        ///< pointer to the aTargetName, used in DoRummageDirCacheL() as a parameter
       
   611         private:
       
   612             TBool       isInitialised  :1;  ///< ETrue if the object is initialised. It can be done only once.
       
   613             TBool       isLegalDosName :1;  ///< ETrue if iTargetName is a legal DOS name
       
   614             TShortName  iShortName;         ///< a short DOS name in XXXXXXXXYYY format generated from aTargetName
       
   615         };
       
   616 
       
   617 
       
   618     	/** 
       
   619 	   	An ad hoc internal helper object for entry creations 
       
   620 	    */
       
   621 	    class XFileCreationHelper
       
   622 	    {
       
   623 	    public:
       
   624 	    	XFileCreationHelper();
       
   625 	    	~XFileCreationHelper();
       
   626 	    	void Close();
       
   627 	    	void InitialiseL(const TDesC&  aTargetName);
       
   628 	        TInt GetValidatedShortName(TShortName& aShortName) const;
       
   629 	    	void CheckShortNameCandidates(const TUint8* apDosEntryName);
       
   630 	
       
   631 	        // inline functions for sets and gets
       
   632 	        //  note all the get functions have been checked against initialisation status
       
   633 	    	inline TBool 	IsInitialised() const;
       
   634 	        inline TUint16	NumOfAddingEntries() const;
       
   635 	        inline TEntryPos EntryAddingPos()const;
       
   636 	        inline TBool 	IsNewEntryPosFound() const;
       
   637 	    	inline TBool 	IsTrgNameLegalDosName() const;
       
   638 	
       
   639 	    	inline void	SetEntryAddingPos(const TEntryPos& aEntryPos);
       
   640 	    	inline void	SetIsNewEntryPosFound(TBool aFound);
       
   641 	
       
   642 	    private:
       
   643 	    	XFileCreationHelper(const XFileCreationHelper&);
       
   644 	    	XFileCreationHelper& operator=(const TFindHelper&);
       
   645 	
       
   646 	    private:
       
   647 	        TPtrC       iTargetName;		///< pointer to hold the long file name of the target file
       
   648 	        TUint16		iNumOfAddingEntries;///< calculated number of entries to add
       
   649 	        TEntryPos	iEntryAddingPos;	///< contains new entry position for adding if found any
       
   650 	        TBool		isNewEntryPosFound; ///< flags whether the position for new entries is found
       
   651 	        TBool       isInitialised	:1;	///< flags whether the object is initialised
       
   652 	        TBool       isTrgNameLegalDosName	:1;	///< flags whether the target file name is a valid Dos name
       
   653 	        /**
       
   654 	        an array that holds short name candidates, prepared on initialisation.
       
   655 	        */
       
   656 	        RArray<TShortName>  iShortNameCandidates;
       
   657 	    };
       
   658 
       
   659 	
       
   660 
       
   661 	TBool DoRummageDirCacheL(TUint anAtt,TEntryPos& aStartEntryPos,TFatDirEntry& aStartEntry,TEntryPos& aDosEntryPos,TFatDirEntry& aDosEntry,TDes& aFileName, const TFindHelper& aAuxParam, XFileCreationHelper* aFileCreationHelper, const TLeafDirData& aLeafDir) const;
       
   662     TBool DoFindL(const TDesC& aName,TUint anAtt,TEntryPos& aStartEntryPos,TFatDirEntry& aStartEntry,TEntryPos& aDosEntryPos,TFatDirEntry& aDosEntry,TDes& aFileName,TInt anError, XFileCreationHelper* aFileCreationHelper, const TLeafDirData& aLeafDirData) const;
       
   663     void FindEntryStartL(const TDesC& aName,TUint anAtt,TFatDirEntry& anEntry,TEntryPos& aPos, XFileCreationHelper* aFileCreationHelper) const;
       
   664 
       
   665     void FindEntryStartL(const TDesC& aName,TUint anAtt,TFatDirEntry& anEntry,TEntryPos& aPos) const;
       
   666 
       
   667 	void CheckFatForLoopsL(const TFatDirEntry& anEntry) const;
       
   668 	void DoCheckFatForLoopsL(TInt aCluster,TInt& aPreviousCluster,TInt& aChangePreviousCluster,TInt& aCount) const;
       
   669     void InitializeL(const TLocalDriveCaps& aLocDrvCaps, TBool aIgnoreFSInfo=EFalse);
       
   670 	void DoReadFromClusterListL(TEntryPos& aPos,TInt aLength,const TAny* aTrg,const RMessagePtr2& aMessage,TInt anOffset) const;
       
   671 	void DoWriteToClusterListL(TEntryPos& aPos,TInt aLength,const TAny* aSrc,const RMessagePtr2& aMessage,TInt anOffset, TInt aLastcluster, TInt& aBadcluster, TInt& aGoodcluster);
       
   672 	TBool IsUniqueNameL(const TShortName& aName,TInt aDirCluster);
       
   673 	TBool FindShortNameL(const TShortName& aName,TEntryPos& anEntryPos);
       
   674 	void ReplaceClashingNameL(const TShortName& aNewName,const TEntryPos& anEntryPos);
       
   675     TBool GenerateShortNameL(TInt aDirCluster,const TDesC& aLongName,TShortName& aShortName, TBool aForceRandomize=EFalse);
       
   676     TInt FindLeafDirL(const TDesC& aName, TLeafDirData& aLeafDir) const;
       
   677 	
       
   678 	TInt GetDirEntry(TEntryPos& aPos,TFatDirEntry& aDosEntry,TFatDirEntry& aStartEntry,TDes& aLongFileName) const;
       
   679     TBool DoGetDirEntryL(TEntryPos& aPos,TFatDirEntry& aDosEntry,TFatDirEntry& aStartEntry,TDes& aLongFileName) const;
       
   680     
       
   681 	void WriteDirEntryL(TEntryPos& aPos,const TFatDirEntry& aFatDirEntry,const TDesC& aLongFileName);
       
   682 	void EraseDirEntryL(TEntryPos aPos,const TFatDirEntry& anEntry);
       
   683 	void EraseDirEntryL(const TEntryPos& aPos);
       
   684 	void InitializeFirstDirClusterL(TInt aCluster,TInt aParentCluster);
       
   685 	void AddDirEntryL(TEntryPos& aPos,TInt aNameLength);
       
   686 	void ZeroDirClusterL(TInt aCluster);
       
   687 	
       
   688     TInt DoWriteBootSector(TInt64 aMediaPos, const TFatBootSector& aBootSector) const;
       
   689 	TInt DoReadBootSector(TInt64 aMediaPos, TFatBootSector& aBootSector) const;
       
   690     TInt ReadBootSector(TFatBootSector& aBootSector, TBool aDoNotReadBkBootSec=EFalse);
       
   691 
       
   692     TInt WriteFSInfoSector(TInt64 aMediaPos, const TFSInfo& aFSInfo) const;
       
   693 	TInt ReadFSInfoSector(TInt64 aMediaPos, TFSInfo& aFSInfo) const;
       
   694 
       
   695     TBool IsDirectoryEmptyL(TInt aCluster);
       
   696 	void ExtendClusterListZeroedL(TInt aNumber,TInt& aCluster);
       
   697 	void WritePasswordData();
       
   698 	
       
   699     void WriteVolumeLabelL(const TDesC8& aVolumeLabel) const;
       
   700     TInt ReadVolumeLabelFile(TDes8& aLabel);
       
   701 	void WriteVolumeLabelFileL(const TDesC8& aNewName);
       
   702 	void FindVolumeLabelFileL(TDes8& aLabel, TEntryPos& aDosEntryPos, TFatDirEntry& aDosEntry);
       
   703 	void GetVolumeLabelFromDiskL(const TFatBootSector& aBootSector);
       
   704 	void TrimVolumeLabel(TDes8& aLabel) const;
       
   705 
       
   706     TInt    DoRunScanDrive();
       
   707     TBool   VolumeCleanL();
       
   708     void    SetVolumeCleanL(TBool aClean);
       
   709     TBool   VolCleanFlagSupported() const;
       
   710 
       
   711     void    DoUpdateFSInfoSectorsL(TBool aInvalidateFSInfo);
       
   712     void    UnFinaliseMountL();
       
   713     void    DoReMountL();
       
   714 
       
   715 private:
       
   716 	
       
   717     TBool ValidClusterNumber(TUint32 aCluster) const;
       
   718     void  CheckUnvisitedClustersL(const RBitVector& aFatBitVec) const;
       
   719 	TInt  WalkClusterListL(RBitVector& aFatBitVec, TInt aCluster);
       
   720 	void  ChkEntryL(RBitVector& aFatBitVec, const TFatDirEntry& anEntry);
       
   721 	void  ChkDirL(RBitVector& aFatBitVec, TInt aDirCluster);
       
   722 
       
   723 	CFatMountCB();
       
   724 
       
   725 	TInt GetDosEntryFromNameL(const TDesC& aName, TEntryPos& aDosEntryPos, TFatDirEntry& aDosEntry);
       
   726 
       
   727     TInt MntCtl_DoCheckFileSystemMountable();
       
   728 
       
   729 
       
   730 private:
       
   731 
       
   732     TBool iReadOnly         : 1;///< if true, the drive is in read-only mode 
       
   733     TBool iRamDrive         : 1;///< true if this is a RAM drive    
       
   734     TBool iMainBootSecValid : 1;///< true if the main boot sector is valid, if false, a backup boot sector may be in use. 
       
   735 
       
   736     TFatMntState iState;        ///< this mounnt internal state
       
   737     TFatType iFatType;          ///< FAT type, FAT12,16 or 32
       
   738 
       
   739     CLeafDirCache* iLeafDirCache;	///< A cache for most recently visited directories, only valid when limit is set bigger than 1
       
   740     HBufC* iLastLeafDir;        	///< The last visited directory, only valid when limit of iLeafDirCache is less than 1 
       
   741     TInt iLastLeafDirCluster;   	///< Cluster number of the last visited cluster, only valid when limit of iLeafDirCache is less than 1
       
   742 
       
   743 	TFatVolParam iVolParam;         ///< FAT volume parameters, populated form the boot sector values.
       
   744     
       
   745 	TInt iFirstFreeByte;            ///< First free byte in media (start of the data area on the volume)
       
   746 	TUint32 iUsableClusters;        ///< Number of usable cluster on the volume 
       
   747 	
       
   748     CFatTable* iFatTable;           ///< Pointer to the volume Fat 
       
   749 	CRawDisk*  iRawDisk;            ///< Pointer to the raw data interface class
       
   750 	
       
   751     CAsyncNotifier* iNotifier;  ///< Async notifier for notifying user of Fat error conditions 
       
   752 
       
   753     XDriveInterface iDriverInterface; ///< the object representing interface to the drive, provides read/write access and notifiers
       
   754     TInt            iChkDiscRecLevel; ///< Check disk recursion level counter. A temporary measure. 
       
   755 	TFatConfig      iFatConfig;       ///< FAT parametrers from estart.txt
       
   756 
       
   757 	XFileCreationHelper iFileCreationHelper;
       
   758 
       
   759 
       
   760 #ifdef  _DEBUG
       
   761     private:
       
   762     //-- debug odds and ends
       
   763     inline TBool IsWriteFail()const;
       
   764 	inline void SetWriteFail(TBool aIsWriteFail);
       
   765 	inline TInt WriteFailCount()const;
       
   766 	inline void SetWriteFailCount(TInt aFailCount);
       
   767 	inline void DecWriteFailCount();
       
   768 	inline TInt WriteFailError()const;
       
   769 	inline void SetWriteFailError(TInt aErrorValue);
       
   770 
       
   771 
       
   772 	TBool   iIsWriteFail : 1; ///< Flag to indicate if write failed used for debugging
       
   773     TBool   iCBRecFlag   : 1; ///< in debug mode used for checking unwanted recursion
       
   774 
       
   775     TInt    iWriteFailCount;  ///< Write fail count for debug
       
   776 	TInt    iWriteFailError;  ///< Write fail error to use for debug
       
   777 
       
   778 #endif
       
   779 
       
   780 friend class CFatFormatCB;
       
   781 friend class CScanDrive;
       
   782 friend class TFatDriveInterface;
       
   783 	};
       
   784 
       
   785 
       
   786 
       
   787 
       
   788 /**
       
   789 Fat file system file subsession implmentation, provides all that is required of a plug in
       
   790 file system file as well as Fat specific functionality
       
   791 */
       
   792 class CFatFileCB : public CFileCB, public CFileCB::MBlockMapInterface, public CFileCB::MExtendedFileInterface
       
   793 	{
       
   794 public:
       
   795 	CFatFileCB();
       
   796 	~CFatFileCB();
       
   797 public:
       
   798 	void RenameL(const TDesC& aNewName);
       
   799 	void ReadL(TInt aPos,TInt& aLength,const TAny* aDes,const RMessagePtr2& aMessage);
       
   800 	void WriteL(TInt aPos,TInt& aLength,const TAny* aDes,const RMessagePtr2& aMessage);
       
   801 	void SetSizeL(TInt aSize);
       
   802 	void SetEntryL(const TTime& aTime,TUint aMask,TUint aVal);
       
   803 	void FlushDataL();
       
   804 	void FlushAllL();
       
   805 public:
       
   806 	void CheckPosL(TUint aPos);
       
   807 	void SetL(const TFatDirEntry& aFatDirEntry,TShare aShare,const TEntryPos& aPos);
       
   808 	void CreateSeekIndex();
       
   809 	
       
   810     inline TBool IsSeekIndex() const;	
       
   811 	
       
   812 	// from MBlockMapInterface
       
   813 	TInt BlockMap(SBlockMapInfo& aInfo, TInt64& aStartPos, TInt64 aEndPos);
       
   814 
       
   815 	// from CFileCB
       
   816 	virtual TInt GetInterface(TInt aInterfaceId,TAny*& aInterface,TAny* aInput);
       
   817 
       
   818 	// from CFileCB::MExtendedFileInterface
       
   819 	virtual void ReadL(TInt64 aPos,TInt& aLength,TDes8* aDes,const RMessagePtr2& aMessage, TInt aOffset);
       
   820 	virtual void WriteL(TInt64 aPos,TInt& aLength,const TDesC8* aDes,const RMessagePtr2& aMessage, TInt aOffset);
       
   821 	virtual void SetSizeL(TInt64 aSize);
       
   822 
       
   823 private:
       
   824 	inline CFatMountCB& FatMount() const;
       
   825     inline CFatTable& FAT();
       
   826 	inline TInt ClusterSizeLog2();
       
   827 	inline TInt ClusterRelativePos(TInt aPos);
       
   828 
       
   829 
       
   830     void FlushStartClusterL();
       
   831 	TInt SeekToPosition(TInt aNewCluster,TInt aClusterOffset);
       
   832 	void SetSeekIndexValueL(TInt aFileCluster,TInt aStoredCluster);
       
   833 	void ResizeIndex(TInt aNewMult,TUint aNewSize);
       
   834 	TInt CalcSeekIndexSize(TUint aSize);
       
   835 	TBool IsSeekBackwards(TUint aPos);
       
   836 	void ClearIndex(TUint aNewSize);
       
   837 	void DoSetSizeL(TUint aSize,TBool aIsSizeWrite);
       
   838 	void WriteFileSizeL(TUint aSize);
       
   839 
       
   840 private:
       
   841 
       
   842 	TUint32* iSeekIndex;    ///< Seek index into file
       
   843 	TInt iSeekIndexSize;    ///< size of seek index
       
   844 	TBool iAttPending;
       
   845 	TInt iStartCluster;     ///< Start cluster number of file
       
   846 	TEntryPos iCurrentPos;  ///< Current position in file data
       
   847 	TEntryPos iFileDirPos;  ///< File directory entry position
       
   848 	TBool iFileSizeModified; 
       
   849 	};
       
   850 //
       
   851 
       
   852 /**
       
   853 Fat file system directory subsession implmentation, provides all that is required of a plug in
       
   854 file system directory as well as Fat specific functionality
       
   855 */
       
   856 class CFatDirCB : public CDirCB
       
   857 	{
       
   858 public:
       
   859 	static CFatDirCB* NewL();
       
   860 	~CFatDirCB();
       
   861 public:
       
   862 	void ReadL(TEntry& anEntry);
       
   863 	void StoreLongEntryNameL(const TDesC& aName);
       
   864 public:
       
   865 	void SetDirL(const TFatDirEntry& anEntry,const TDesC& aMatchName);
       
   866 	inline CFatMountCB& FatMount();
       
   867 private:
       
   868 	CFatDirCB();
       
   869 private:
       
   870 	TFatDirEntry iEntry;       ///< Current directory entry in this directory
       
   871 	TEntryPos    iCurrentPos;  ///< Current position in directory
       
   872 	HBufC*       iMatch;       ///< Current name being searched in directory (Dos Name)
       
   873 	HBufC*       iLongNameBuf; ///< Long name storage	
       
   874 	TBool        iMatchUid;    ///< Flag to indicate if UID matches
       
   875 	};
       
   876 
       
   877 /**
       
   878     Fat file system Format subsession implmentation, provides all that is required of a plug in
       
   879     file system format as well as Fat specific functionality
       
   880 */
       
   881 class CFatFormatCB : public CFormatCB
       
   882 	{
       
   883 public:
       
   884 	CFatFormatCB();
       
   885 	~CFatFormatCB();
       
   886 public:
       
   887 	
       
   888     //-- overrides from CFormatCB
       
   889     void DoFormatStepL();
       
   890 
       
   891 private:
       
   892     //-- overrides from CFormatCB
       
   893     TInt GetInterface(TInt aInterfaceId,TAny*& aInterface,TAny* aInput);
       
   894 
       
   895 private:	
       
   896     
       
   897     TInt DoProcessTVolFormatParam(const TVolFormatParam_FAT* apVolFormatParam);
       
   898 
       
   899     void CreateBootSectorL();
       
   900 	void CreateFSInfoSectorL();
       
   901 	void CreateReservedBootSectorL();
       
   902 	void InitializeFormatDataL();
       
   903 	void DoZeroFillMediaL(TInt64 aStartPos, TInt64 aEndPos);
       
   904 
       
   905     TInt InitFormatDataForVariableSizeDisk(TInt aDiskSizeInSectors);
       
   906 	TInt InitFormatDataForFixedSizeDiskNormal(TInt aDiskSizeInSectors, const TLocalDriveCapsV6& aCaps);
       
   907 	TInt InitFormatDataForFixedSizeDiskCustom(const TLDFormatInfo& aFormatInfo);
       
   908     TInt InitFormatDataForFixedSizeDiskUser(TInt aDiskSizeInSectors);
       
   909 	void AdjustClusterSize(TInt aRecommendedSectorsPerCluster);
       
   910 	TInt AdjustFirstDataSectorAlignment(TInt aBlockSize);
       
   911 	TInt FirstDataSector() const;
       
   912 
       
   913 	TInt HandleCorrupt(TInt aError);
       
   914 	TInt BadSectorToCluster();
       
   915     void TranslateL();
       
   916     TInt DoTranslate(TPtr8& aBuf, RArray<TInt>& aArray);
       
   917     void RecordOldInfoL();
       
   918 	TInt MaxFat12Sectors() const;
       
   919 	TInt MaxFat16Sectors() const;
       
   920 	TUint32 MaxFat32Sectors() const;
       
   921 	inline TBool Is16BitFat() const;
       
   922 	inline TBool Is32BitFat() const;
       
   923 	inline CFatMountCB& FatMount();
       
   924 	inline CProxyDrive* LocalDrive();
       
   925     TFatType SuggestFatType() const;
       
   926 
       
   927 private:
       
   928 	
       
   929     TBool   iVariableSize;      ///< Flag to indicat if we are dealing with a variable size volume
       
   930 	TInt    iBytesPerSector;    ///< Byte per sector of media
       
   931     TInt    iSectorSizeLog2;    ///< Sector size in log2
       
   932 	TInt    iNumberOfFats;      ///< Number of Fats the volume will contain
       
   933 	TInt    iReservedSectors;   ///< Number of reserved sectors in the volume
       
   934 	TInt    iRootDirEntries;    ///< Nummer of root directory entries the root dir will have, specific to Fat12/16 volumes
       
   935 	TInt    iSectorsPerCluster; ///< Sector per cluster ration the volume will be formatted with
       
   936 	TInt    iSectorsPerFat;     ///< Number of sectors the Fat uses
       
   937 	TInt    iMaxDiskSectors;    ///< number of sectors the volume has
       
   938 	TFormatInfo iFormatInfo;    ///< format information for a custom format
       
   939 	TBuf8<16>   iFileSystemName;///< Buffer to contain the volume name 
       
   940 	TInt    iHiddenSectors;     ///< Number of hidden sectors in the volume
       
   941 	TInt    iNumberOfHeads;     ///< Number of heads the media device has, not used so far as only used on solid state media.
       
   942 	TInt    iSectorsPerTrack;   ///< Number of sectors the media device has, not used so far as only used on solid state media.
       
   943 	TUint32 iRootClusterNum;    ///< cluster number used for root directory, Fat32 specific
       
   944 	TUint32 iCountOfClusters;   ///< Count of clusters on the media
       
   945     RArray<TInt> iBadClusters;  ///< Array of bad cluster numbers
       
   946     RArray<TInt> iBadSectors;   ///< Array of bad sector numbers
       
   947     TBool   iDiskCorrupt;       ///< Disk is corrupt when format or not
       
   948     TInt    iOldFirstFreeSector;
       
   949     TInt    iOldSectorsPerCluster;
       
   950 	};
       
   951 
       
   952 /**
       
   953 Required file system class used by file server to create the file system objects
       
   954 */
       
   955 class CFatFileSystem : public CFileSystem
       
   956 	{
       
   957 public:
       
   958 	static CFatFileSystem* New();
       
   959 	~CFatFileSystem();
       
   960 public:
       
   961 	TInt Install();
       
   962 	CMountCB* NewMountL() const;
       
   963 	CFileCB* NewFileL() const;
       
   964 	CDirCB* NewDirL() const;
       
   965 	CFormatCB* NewFormatL() const;
       
   966 	void DriveInfo(TDriveInfo& anInfo,TInt aDriveNumber) const;
       
   967 	TInt DefaultPath(TDes& aPath) const;
       
   968 	TBool IsExtensionSupported() const;
       
   969 	TBool GetUseLocalTime() const;
       
   970 	void SetUseLocalTime(TBool aFlag);
       
   971 	TInt GetInterface(TInt aInterfaceId, TAny*& aInterface,TAny* aInput);
       
   972 protected:
       
   973 	CFatFileSystem();
       
   974 	/**
       
   975 	If true, then local time will be used when writing timestamps to FS. When reading,
       
   976 	timestamps will be assumed local and converted back to UTC.
       
   977 	At present, this behaviour will also be conditional upon a particular drive being logically removable.
       
   978 	*/
       
   979 	TBool iUseLocalTimeIfRemovable;
       
   980 	};
       
   981 
       
   982 
       
   983 
       
   984 /**
       
   985 Locale utilities allows the file system to call into a specific locale for tasks
       
   986 such as Dos name to unicode conversions and testing the legality of characters for
       
   987 any given locale.
       
   988 */
       
   989 class LocaleUtils
       
   990 
       
   991 	{
       
   992 public:
       
   993 	static void ConvertFromUnicodeL(TDes8& aForeign, const TDesC16& aUnicode, TFatUtilityFunctions::TOverflowAction aOverflowAction=TFatUtilityFunctions::EOverflowActionTruncate);
       
   994 	static void ConvertToUnicodeL(TDes16& aUnicode, const TDesC8& aForeign, TFatUtilityFunctions::TOverflowAction aOverflowAction=TFatUtilityFunctions::EOverflowActionTruncate);
       
   995 	static TBool IsLegalShortNameCharacter(TUint aCharacter,TBool aUseExtendedChars=EFalse);
       
   996 	};
       
   997 //
       
   998 
       
   999 TPtrC RemoveTrailingDots(const TDesC& aName);
       
  1000 
       
  1001 /**
       
  1002 Indicates if a number passed in is a power of two
       
  1003 @return ETrue if aVal is a power of 2 
       
  1004 */
       
  1005 inline TBool IsPowerOf2(TUint32 aVal);
       
  1006 
       
  1007 /**
       
  1008 Calculates the log2 of a number
       
  1009 
       
  1010 @param aNum Number to calulate the log two of
       
  1011 @return The log two of the number passed in
       
  1012 */
       
  1013 TUint32 Log2(TUint32 aVal);
       
  1014 
       
  1015 /** @return 2^aVal*/
       
  1016 inline TUint32 Pow2(TUint32 aVal);
       
  1017 
       
  1018 
       
  1019 /**
       
  1020 Converts Dos time (from a directory entry) to TTime format
       
  1021 
       
  1022 @param aDosTime Dos format time
       
  1023 @param aDosDate Dos format Date
       
  1024 @return TTime format of Dos time passed in 
       
  1025 */
       
  1026 TTime DosTimeToTTime(TInt aDosTime,TInt aDosDate);
       
  1027 /**
       
  1028 Converts TTime format to Dos time format
       
  1029 
       
  1030 @param aTime TTime to convert to Dos time
       
  1031 @return Dos time format
       
  1032 */
       
  1033 TInt DosTimeFromTTime(const TTime& aTime);
       
  1034 /**
       
  1035 Converts TTime format to Dos time format
       
  1036 
       
  1037 @param aTime TTime to convert to Dos Date
       
  1038 @return Dos Date format
       
  1039 */
       
  1040 TInt DosDateFromTTime(const TTime& aTime);
       
  1041 /**
       
  1042 Converts Dos Directory entry format to 8.3 format
       
  1043 
       
  1044 @param aDosName Directory entry format with space delimeter
       
  1045 @return 8.3 Dos filename format
       
  1046 */
       
  1047 TBuf8<12> DosNameToStdFormat(const TDesC8& aDosName);
       
  1048 /**
       
  1049 Converts 8.3 format to Dos Directory entry format 
       
  1050 
       
  1051 @param aStdFormatName 8.3 Dos filename format
       
  1052 @return Directory entry format with space delimeter
       
  1053 */
       
  1054 TBuf8<12> DosNameFromStdFormat(const TDesC8& aStdFormatName);
       
  1055 /**
       
  1056 Fault function calls user panic with a fault reason
       
  1057 
       
  1058 @param Enumerated fault reason
       
  1059 */
       
  1060 void Fault(TFault aFault);
       
  1061 /**
       
  1062 calculates the number of VFat directory entries for a given file/directory name length
       
  1063 
       
  1064 @param the length in characters of the name
       
  1065 @return the number of VFat entries required
       
  1066 */
       
  1067 TInt NumberOfVFatEntries(TInt aNameLength);
       
  1068 /**
       
  1069 Calculates the check sum for a standard directory entry
       
  1070 
       
  1071 @param the Dos name for the directory entry
       
  1072 @return the checksum
       
  1073 */
       
  1074 TUint8 CalculateShortNameCheckSum(const TDesC8& aShortName);
       
  1075 
       
  1076 
       
  1077 
       
  1078 
       
  1079 //-----------------------------------------------------------------------------
       
  1080 
       
  1081 /**
       
  1082     This class represents a bit vector i.e. an array of bits. Vector size can be 1..2^32 bits.
       
  1083 */
       
  1084 class RBitVector
       
  1085     {
       
  1086  public:
       
  1087     
       
  1088     RBitVector(); //-- Creates an empty vector. see Create() methods for memory allocation
       
  1089    ~RBitVector(); 
       
  1090     
       
  1091     void Close(); 
       
  1092     
       
  1093     TInt Create(TUint32 aNumBits);
       
  1094     void CreateL(TUint32 aNumBits);
       
  1095 
       
  1096     inline TUint32 Size() const;
       
  1097 
       
  1098     //-- single bit manipulation methods
       
  1099     inline TBool operator[](TUint32 aIndex) const;
       
  1100     inline void SetBit(TUint32 aIndex);
       
  1101     inline void ResetBit(TUint32 aIndex);
       
  1102     inline void InvertBit(TUint32 aIndex);
       
  1103     inline void SetBitVal(TUint32 aIndex, TBool aVal);
       
  1104     
       
  1105     void Fill(TBool aVal);
       
  1106     void Fill(TUint32 aIndexFrom, TUint32 aIndexTo, TBool aVal);
       
  1107 
       
  1108     void Invert();
       
  1109    
       
  1110     TBool operator==(const RBitVector& aRhs) const; 
       
  1111     TBool operator!=(const RBitVector& aRhs) const;
       
  1112 
       
  1113     //-- logical operations between 2 vectors. 
       
  1114     void And(const RBitVector& aRhs);
       
  1115     void Or (const RBitVector& aRhs);
       
  1116     void Xor(const RBitVector& aRhs);
       
  1117 
       
  1118     TBool Diff(const RBitVector& aRhs, TUint32& aDiffIndex) const;
       
  1119 
       
  1120     /** Bit search specifiers */
       
  1121     enum TFindDirection
       
  1122         {
       
  1123         ELeft,      ///< Search from the given position to the left (towards lower index)
       
  1124         ERight,     ///< Search from the given position to the right (towards higher index)
       
  1125         ENearestL,  ///< Search in both directions starting from the given position; in the case of the equal distances return the position to the left
       
  1126         ENearestR   ///< Search in both directions starting from the given position; in the case of the equal distances return the position to the right
       
  1127 
       
  1128         //-- N.B the current position the search starts with isn't included to the search.
       
  1129         };
       
  1130 
       
  1131     TBool Find(TUint32& aStartPos, TBool aBitVal, TFindDirection aDir) const;
       
  1132 
       
  1133     /** panic codes */
       
  1134     enum TPanicCode
       
  1135         {
       
  1136         EIndexOutOfRange,       ///< index out of range
       
  1137         EWrondFindDirection,    ///< a value doesn't belong to TFindDirection
       
  1138         ESizeMismatch,          ///< Size mismatch for binary operators
       
  1139         ENotInitialised,        ///< No memory allocated for the array
       
  1140         ENotImplemented,        ///< functionality isn't implemented
       
  1141         };
       
  1142 
       
  1143  protected:
       
  1144     
       
  1145     //-- these are outlawed. Can't use them because memory allocator can leave and we don't have conthrol on it  in these methods. 
       
  1146     RBitVector(const RBitVector& aRhs);            
       
  1147     RBitVector& operator=(const RBitVector& aRhs); 
       
  1148   
       
  1149     void Panic(TPanicCode aPanicCode) const;
       
  1150 
       
  1151     inline TUint32 WordNum(TUint32 aBitPos)  const;
       
  1152     inline TUint32 BitInWord(TUint32 aBitPos) const;
       
  1153 
       
  1154  private:
       
  1155     TBool FindToRight(TUint32& aStartPos, TBool aBitVal) const;
       
  1156     TBool FindToLeft (TUint32& aStartPos, TBool aBitVal) const;
       
  1157     TBool FindNearest(TUint32& aStartPos, TBool aBitVal, TBool aToLeft) const;
       
  1158    
       
  1159     inline TUint32 MaskLastWord(TUint32 aVal) const; 
       
  1160     inline TBool ItrLeft(TUint32& aIdx) const;
       
  1161     inline TBool ItrRight(TUint32& aIdx) const;
       
  1162 
       
  1163 
       
  1164  protected:
       
  1165 
       
  1166     TUint32   iNumBits; ///< number of bits in the vector
       
  1167     TUint32*  ipData;   ///< pointer to the data 
       
  1168     TUint32   iNumWords;///< number of 32-bit words that store bits
       
  1169     };
       
  1170 
       
  1171 
       
  1172 //-----------------------------------------------------------------------------
       
  1173 
       
  1174 
       
  1175 #include "sl_std.inl"
       
  1176 #include "sl_bpb.inl"
       
  1177 #include "fat_dir_entry.inl"
       
  1178               
       
  1179 #endif //SL_STD_H