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