persistentstorage/store/UFILE/UF_PERM.CPP
changeset 0 08ec8eefde2f
equal deleted inserted replaced
-1:000000000000 0:08ec8eefde2f
       
     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 "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 //
       
    15 
       
    16 #include "UF_STD.H"
       
    17 
       
    18 const TInt KBaseOffset=KFileStoreStartOffset-KPermanentStoreHeaderOffset;
       
    19 #if defined(__NO_CLASS_CONSTS__)
       
    20 #define KBase TStreamPos(KBaseOffset)
       
    21 #else
       
    22 const TStreamPos KBase=TStreamPos(KBaseOffset);
       
    23 #endif
       
    24 
       
    25 EXPORT_C TUid CPermanentFileStore::Layout() const
       
    26 /** Gets the UID that uniquely identifies this file store as a permanent file store.
       
    27 
       
    28 @return KPermanentFileStoreLayoutUid. */
       
    29 	{
       
    30 	return KPermanentFileStoreLayoutUid;
       
    31 	}
       
    32 
       
    33 EXPORT_C CPermanentFileStore::CPermanentFileStore(RFile& aFile)
       
    34 //
       
    35 // Constructor creating a new file store.
       
    36 //
       
    37 	: CFileStore(aFile)/*,iCoord(NULL)*/
       
    38 	{}
       
    39 
       
    40 EXPORT_C CPermanentFileStore::CPermanentFileStore(RFileBuf& aBuf,const TUidType& aType)
       
    41 //
       
    42 // Constructor opening an existing file store.
       
    43 //
       
    44 	: CFileStore(aBuf,aType)/*,iCoord(NULL)*/
       
    45 	{}
       
    46 
       
    47 EXPORT_C void CPermanentFileStore::MarshalL()
       
    48 	{
       
    49 	__ASSERT_DEBUG(iCoord==NULL,User::Invariant());
       
    50 	iCoord=new(ELeave) CPermanentStoreCoord(KBase,Host());
       
    51 	CFileStore::MarshalL();
       
    52 	iRoot=Coord().PrimaryL();
       
    53 	}
       
    54 
       
    55 EXPORT_C CPermanentFileStore::~CPermanentFileStore()
       
    56 /** Frees resources owned by the object, prior to its destruction. */
       
    57 	{
       
    58 	Destruct();
       
    59 	delete iCoord;
       
    60 	}
       
    61 
       
    62 EXPORT_C MStreamBuf* CPermanentFileStore::DoReadL(TStreamId anId) const
       
    63 	{
       
    64 	if (iCoord==NULL)
       
    65 		__LEAVE(KErrNotFound);
       
    66 //
       
    67 	return HPermanentStoreBuf::OpenL(CoordL(),anId,HPermanentStoreBuf::ERead);
       
    68 	}
       
    69 
       
    70 EXPORT_C MStreamBuf* CPermanentFileStore::DoCreateL(TStreamId& anId)
       
    71 	{
       
    72 	return HPermanentStoreBuf::CreateL(TrimL(),anId);
       
    73 	}
       
    74 
       
    75 EXPORT_C MStreamBuf* CPermanentFileStore::DoWriteL(TStreamId anId)
       
    76 	{
       
    77 	if (iCoord==NULL)
       
    78 		__LEAVE(KErrNotFound);
       
    79 //
       
    80 	return HPermanentStoreBuf::OpenL(CoordL(),anId);
       
    81 	}
       
    82 
       
    83 EXPORT_C MStreamBuf* CPermanentFileStore::DoReplaceL(TStreamId anId)
       
    84 	{
       
    85 	if (iCoord==NULL)
       
    86 		__LEAVE(KErrNotFound);
       
    87 //
       
    88 	return HPermanentStoreBuf::ReplaceL(TrimL(),anId);
       
    89 	}
       
    90 
       
    91 EXPORT_C void CPermanentFileStore::ExternalizeL(RWriteStream& aStream) const
       
    92 //
       
    93 // Externalize this store's structure, during construction only.
       
    94 //
       
    95 	{
       
    96 	__ASSERT_DEBUG(!Host().IsActive()&&iCoord==NULL,User::Invariant());
       
    97 	TUint8 buf[-KPermanentStoreHeaderOffset];
       
    98 	Mem::FillZ(buf,sizeof(buf));
       
    99 	aStream.WriteL(buf,sizeof(buf));
       
   100 	}
       
   101 
       
   102 EXPORT_C void CPermanentFileStore::InternalizeL(RReadStream& aStream)
       
   103 //
       
   104 // Internalize this store's structure, during marshalling or refreshment.
       
   105 //
       
   106 	{
       
   107 	__ASSERT_DEBUG(!Host().IsActive(),User::Invariant());
       
   108 	Coord().InternalizeL(aStream);
       
   109 	}
       
   110 
       
   111 EXPORT_C void CPermanentFileStore::DoSetRootL(TStreamId anId)
       
   112 //
       
   113 // Set the root stream id.
       
   114 //
       
   115 	{
       
   116 	CoordL().ChangedL();
       
   117 	iRoot=anId;
       
   118 	}
       
   119 
       
   120 EXPORT_C TStreamId CPermanentFileStore::DoExtendL()
       
   121 	{
       
   122 	return CoordL().ExtendL();
       
   123 	}
       
   124 
       
   125 EXPORT_C void CPermanentFileStore::DoDeleteL(TStreamId anId)
       
   126 	{
       
   127 	if (iCoord==NULL)
       
   128 		__LEAVE(KErrNotFound);
       
   129 //
       
   130 	CoordL().DeleteL(anId);
       
   131 	}
       
   132 
       
   133 EXPORT_C void CPermanentFileStore::DoCommitL()
       
   134 //
       
   135 // Establish a new commit point, committing changes made since the last one.
       
   136 //
       
   137 	{
       
   138 	if (iCoord!=NULL&&TrimL().CommitL(iRoot))
       
   139 		{
       
   140 		return;
       
   141 		}
       
   142 //
       
   143 	SynchL();
       
   144 	}
       
   145 
       
   146 EXPORT_C void CPermanentFileStore::DoRevertL()
       
   147 //
       
   148 // Roll back to the state at the last commit point.
       
   149 //
       
   150 	{
       
   151 	CPermanentStoreCoord* coord=iCoord;
       
   152 	if (coord!=NULL&&coord->RevertL(iRoot))
       
   153 		return;
       
   154 //
       
   155 	SynchL();
       
   156 	}
       
   157 
       
   158 EXPORT_C MIncrementalCollector* CPermanentFileStore::DoReclaimL()
       
   159 	{
       
   160 	return CPermanentStoreCollector::ReclaimerL(CoordL());
       
   161 	}
       
   162 
       
   163 EXPORT_C MIncrementalCollector* CPermanentFileStore::DoCompactL()
       
   164 	{
       
   165 	return CPermanentStoreCollector::CompactorL(TrimL());
       
   166 	}
       
   167 
       
   168 CPermanentStoreCoord& CPermanentFileStore::CoordL() const
       
   169 	{
       
   170 	CPermanentStoreCoord* coord=iCoord;
       
   171 	if (coord==NULL)
       
   172 		{
       
   173 		coord=new(ELeave) CPermanentStoreCoord(KBase,Host());
       
   174 		MUTABLE_CAST(CPermanentStoreCoord*&,iCoord)=coord;
       
   175 		}
       
   176 	else if (!Host().IsActive())
       
   177 		CONST_CAST(CPermanentFileStore*,this)->RefreshL();
       
   178 	return *coord;
       
   179 	}
       
   180 
       
   181 CPermanentStoreCoord& CPermanentFileStore::TrimL()
       
   182 	{
       
   183 	CPermanentStoreCoord& coord=CoordL();
       
   184 	if (!coord.IsTrim())
       
   185 		{
       
   186 		TInt lim=coord.LimitL().Offset(); // it can be slightly smaller than this
       
   187 		TInt size=Host().HostL()->SizeL();
       
   188 		if (size>lim)
       
   189 			SetSizeL(lim);
       
   190 		coord.Clipped();
       
   191 		}
       
   192 	return coord;
       
   193 	}
       
   194 
       
   195 EXPORT_C CFileStore* CPermanentFileStore::DoNewL(RFile& aFile)
       
   196 //
       
   197 // Factory function creating a new file.
       
   198 //
       
   199 	{
       
   200 	return new(ELeave) CPermanentFileStore(aFile);
       
   201 	}
       
   202