remotestoragefw/remotefileengine/inc/rsfwpermanentstore.h
changeset 13 6b4fc789785b
parent 2 c32dc0be5eb4
equal deleted inserted replaced
2:c32dc0be5eb4 13:6b4fc789785b
     1 /*
       
     2 * Copyright (c) 2004-2006 Nokia Corporation and/or its subsidiary(-ies).
       
     3 * All rights reserved.
       
     4 * This component and the accompanying materials are made available
       
     5 * under the terms of "Eclipse Public License v1.0"
       
     6 * which accompanies this distribution, and is available
       
     7 * at the URL "http://www.eclipse.org/legal/epl-v10.html".
       
     8 *
       
     9 * Initial Contributors:
       
    10 * Nokia Corporation - initial contribution.
       
    11 *
       
    12 * Contributors:
       
    13 *
       
    14 * Description:  Provides persistent storage for memory blocks
       
    15 *
       
    16 */
       
    17 
       
    18 
       
    19 #ifndef C_RSFWPERMANENTSTORE_H
       
    20 #define C_RSFWPERMANENTSTORE_H
       
    21 
       
    22 // INCLUDES
       
    23 #include <e32std.h>
       
    24 #include <f32file.h>
       
    25 #include <s32file.h>
       
    26 
       
    27 // CLASS DECLARATION
       
    28 class TFileHeader
       
    29     {
       
    30 public:
       
    31     TUint   iHeaderStart;           // magic number
       
    32     TInt    iHeaderSize;            // (max) header size
       
    33     TInt    iBlockSize;             // block size
       
    34     HBufC8* iHeader;                // header 
       
    35 
       
    36 public:
       
    37     void ExternalizeL(RWriteStream& aStream) const;
       
    38     void InternalizeL(RReadStream& aStream);
       
    39     };
       
    40 
       
    41 class TSlot
       
    42     {
       
    43  public:
       
    44     TInt iIndex;        // data index (used as key for RArray)
       
    45     TInt iBlockNumber;  // starting block number
       
    46     TInt iBlockCount;   // number of blocks
       
    47     };
       
    48 
       
    49 class CRsfwPermanentStore: public CBase
       
    50     {
       
    51 
       
    52 class TFreeBlockList
       
    53     {
       
    54  public:
       
    55     RArray<TInt> iFreeBlockList; // list of continuous free block sequences
       
    56     };
       
    57 
       
    58     enum TFileStateInPermanentStore
       
    59         {
       
    60         EFileStateClosed = 0,
       
    61         EFileStateReading,
       
    62         EFileStateWriting
       
    63         };
       
    64 public:
       
    65     static CRsfwPermanentStore* NewL(const TDesC& aPath,
       
    66                                  TInt aHeaderSize = 0,
       
    67                                  TInt aBlockSize = 0);
       
    68     ~CRsfwPermanentStore();
       
    69     void ResetL(TBool aWriting);
       
    70     TInt Commit();
       
    71     TInt Purge();
       
    72     void CompactL();
       
    73     void SetHeaderL(TDesC8& aHeader);
       
    74     const HBufC8* Header();
       
    75     void GetNextDataL(TUint8* aData, TInt& aDataLength, TInt& aIndex);
       
    76     void PutDataL(const TUint8* aData, TInt aDataLength, TInt& aIndex);
       
    77 
       
    78 protected:
       
    79     void ConstructL(const TDesC& aPath,
       
    80                     TInt aHeaderSize,
       
    81                     TInt aBlockSize);
       
    82 
       
    83 private:
       
    84     TInt BlockCount(TInt aDataLength);
       
    85     TInt StreamPosition(TInt aBlockNumber);
       
    86     TSlot* Slot(TInt aIndex);
       
    87     void FixSlot(TInt aOldBlockNumber, TInt aNewBlockNumber);
       
    88     void SetFileStateL(TInt aFileState);
       
    89     void LoadHeaderL();
       
    90     void SaveHeaderL();
       
    91     void ClearFreeBlockLists();
       
    92     void ClearSlotL(TInt aIndex);
       
    93     void WriteBlocksL(const TUint8* aData,
       
    94                       TInt aDataLength,
       
    95                       TInt aBlockNumber);
       
    96     void ReserveSlot(TInt aIndex, TInt aBlockNumber, TInt aBlockCount);
       
    97     void PutToFreeBlockList(TInt aBlockPos, TInt aBlockCount);
       
    98     TInt GetFromFreeBlockList(TInt aBlockCount);
       
    99 
       
   100 private:
       
   101     RFs                    iFs;               // file server session
       
   102     RFile                  iFile;             // the file
       
   103     RFileReadStream        iFileReadStream;   // iFile mapped to read stream
       
   104     RFileWriteStream       iFileWriteStream;  // iFile mapped to write stream
       
   105     TBuf<KMaxPath>         iPath;             // path name of the store file
       
   106     TInt                   iHeaderSize;       // size of header payload
       
   107     TInt                   iFileHeaderSize;   // total size of header
       
   108     TInt                   iBlockSize;        // size of elementary block
       
   109     TInt                   iFileState;        // file opening state
       
   110     RArray<TFreeBlockList> iFreeBlockLists;   // lists of free blocks
       
   111     TFileHeader            iFileHeader;       // file information in the header
       
   112     RArray<TSlot>          iSlots;            // maps index/block number
       
   113     TInt                   iIndex;            // next slot position
       
   114     TInt                   iReadBlockNumber;  // next block to read
       
   115     TInt                   iWriteBlockNumber; // next block to write
       
   116     HBufC8*                iZeroBlock;        // a filler block
       
   117     };
       
   118 
       
   119 #endif // PERMANENTSTORE_H
       
   120 
       
   121 // End of File