1.1 --- /dev/null Thu Jan 01 00:00:00 1970 +0000
1.2 +++ b/os/persistentdata/persistentstorage/store/UFILE/UF_DICT.CPP Fri Jun 15 03:10:57 2012 +0200
1.3 @@ -0,0 +1,261 @@
1.4 +// Copyright (c) 1998-2009 Nokia Corporation and/or its subsidiary(-ies).
1.5 +// All rights reserved.
1.6 +// This component and the accompanying materials are made available
1.7 +// under the terms of "Eclipse Public License v1.0"
1.8 +// which accompanies this distribution, and is available
1.9 +// at the URL "http://www.eclipse.org/legal/epl-v10.html".
1.10 +//
1.11 +// Initial Contributors:
1.12 +// Nokia Corporation - initial contribution.
1.13 +//
1.14 +// Contributors:
1.15 +//
1.16 +// Description:
1.17 +//
1.18 +
1.19 +#include "UF_STD.H"
1.20 +#include <e32math.h>
1.21 +
1.22 +const TUid KUidDictionaryFile={0x100000E4};
1.23 +
1.24 +_LIT(KSystemIniFileLocationSpec,"Z:\\System\\System.ini");
1.25 +
1.26 +const TUid KSystemIniFileUid = {0x1000010C};
1.27 +
1.28 +// Thread contention resolution constants
1.29 +
1.30 +const TInt KSpinCount=40;
1.31 +const TInt KLockoutLimit=0xc0000; // to keep the lock-out time down to ~1sec (this is microseconds)
1.32 +const TInt KFailTime=0x1000; // ~ time to fail to open file
1.33 +const TInt KSpinLimit=KLockoutLimit-KSpinCount*KFailTime;
1.34 +
1.35 +LOCAL_C TUint8 const WaitDistribution[8]={0,0,0,0,0x1f,0x3f,0x7f,0xff}; // wait-time distribution mask
1.36 +
1.37 +class RYieldThread : public RThread
1.38 + {
1.39 +public:
1.40 + inline void Yield() const {SetPriority(Priority());}
1.41 + };
1.42 +
1.43 +/////////////////////////////////////////////
1.44 +// CDictionaryFileStore
1.45 +/////////////////////////////////////////////
1.46 +
1.47 +LOCAL_C void EnsurePathL(RFs& aFs,const TDesC& aName)
1.48 + {
1.49 + TInt r=aFs.MkDirAll(TParsePtrC(aName).DriveAndPath());
1.50 + if (r!=KErrAlreadyExists)
1.51 + User::LeaveIfError(r);
1.52 + }
1.53 +
1.54 +EXPORT_C CDictionaryFileStore* CDictionaryFileStore::SystemL(RFs& aFs)
1.55 +/** Opens the system dictionary file store.
1.56 +
1.57 +@param aFs Handle to a file server session.
1.58 +@return A pointer to the system file based dictionary store object. */
1.59 + {
1.60 + CDictionaryFileStore* store=SystemLC(aFs);
1.61 + CleanupStack::Pop();
1.62 + return store;
1.63 + }
1.64 +
1.65 +EXPORT_C CDictionaryFileStore* CDictionaryFileStore::SystemLC(RFs& aFs)
1.66 +/** Opens the system dictionary file store and puts the pointer to the file store
1.67 +object onto the cleanup stack.
1.68 +
1.69 +@param aFs Handle to a file server session.
1.70 +@return A pointer to the system file based dictionary store object. */
1.71 + {
1.72 + TDriveUnit drive(static_cast<TUint>(RFs::GetSystemDrive()));
1.73 + TParse parse;
1.74 + User::LeaveIfError(parse.Set(drive.Name(), &KSystemIniFileLocationSpec, NULL));
1.75 +
1.76 + EnsurePathL(aFs, parse.FullName());
1.77 + return OpenLC(aFs, parse.FullName(), KSystemIniFileUid);
1.78 + }
1.79 +
1.80 +EXPORT_C CDictionaryFileStore* CDictionaryFileStore::OpenL(RFs& aFs,const TDesC& aName,TUid aFileId)
1.81 + /** Creates a file based dictionary store object.
1.82 +
1.83 + If the file with the specified full path name exists, then an attempt is made
1.84 + to open an existing file store contained within this file. Any existing file
1.85 + store must satisfy the following conditions:
1.86 +
1.87 + it must be a valid dictionary store
1.88 +
1.89 + the third UID component of the file store type must match the specified UID;
1.90 + this UID serves to differentiate between dictionary stores
1.91 +
1.92 + otherwise the function leaves with KErrCorrupt.
1.93 +
1.94 + If the file with the specified full path name does not exist, then an attempt
1.95 + is made to create a new file and to create a file based dictionary within
1.96 + it. The third UID component of the file store type is set to the specified
1.97 + UID value.
1.98 +
1.99 + Note that the file is opened in exclusive access mode.
1.100 +
1.101 + @param aFs Handle to a file server session.
1.102 + @param aName The full path name of the file.
1.103 + @param aUid3 The UID used to differentiate between dictionary stores.
1.104 + @return A pointer to the file based dictionary store object.
1.105 + @see TUid
1.106 + @see TUidType */
1.107 + {
1.108 + CDictionaryFileStore* self = CDictionaryFileStore::OpenLC(aFs,aName,aFileId);
1.109 + CleanupStack::Pop();
1.110 + return self;
1.111 + }
1.112 +
1.113 +EXPORT_C CDictionaryFileStore* CDictionaryFileStore::OpenLC(RFs& aFs,const TDesC& aName,TUid aFileId)
1.114 + /** Creates a file based dictionary store object and puts the pointer onto the
1.115 + cleanup stack.
1.116 +
1.117 + If the file with the specified full path name exists, then an attempt is made
1.118 + to open an existing file store contained within this file. Any existing file
1.119 + store must satisfy the following conditions:
1.120 +
1.121 + it must be a valid dictionary store
1.122 +
1.123 + the third UID component of the file store type must match the specified UID;
1.124 + this UID serves to differentiate between dictionary stores
1.125 +
1.126 + otherwise the function leaves with KErrCorrupt.
1.127 +
1.128 + If the file with the specified full path name does not exist, then an attempt
1.129 + is made to create a new file and to create a file based dictionary within
1.130 + it. The third UID component of the file store type is set to the specified
1.131 + UID value.
1.132 +
1.133 + Note that the file is opened in exclusive access mode.
1.134 +
1.135 + @param aFs Handle to a file server session.
1.136 + @param aName The full path name of the file.
1.137 + @param aUid3 The UID used to differentiate between dictionary stores.
1.138 + @return A pointer to the file based dictionary store object.
1.139 + @see TUid
1.140 + @see TUidType */
1.141 + {
1.142 + CDictionaryFileStore* self = new(ELeave) CDictionaryFileStore();
1.143 + CleanupStack::PushL(self);
1.144 + self->ConstructL(aFs,aName,aFileId);
1.145 + return self;
1.146 + }
1.147 +
1.148 +void CDictionaryFileStore::ConstructL(RFs& aFs,const TDesC& aName,TUid aFileId)
1.149 +//
1.150 +// try to open the file - if this fails KErrNotFound try to create it
1.151 +// if the file is in use retry after a pause
1.152 +//
1.153 + {
1.154 + RYieldThread thread;
1.155 + TInt64 seed;
1.156 + const TUidType type(KPermanentFileStoreLayoutUid,KUidDictionaryFile,aFileId);
1.157 + for (TInt wait=KLockoutLimit;;)
1.158 + {
1.159 + RFile file;
1.160 + // When the file server write caching is enabled, the order of file write operations is not guaranteed.
1.161 + // This could cause data inconsistency in some circumstances,for example, when the power is lost in the
1.162 + // middle of a database transaction.Therefore, the file write caching is disabled for this file to maintain integrity.
1.163 +
1.164 + TInt r=file.Open(aFs,aName,EFileShareExclusive|EFileWrite|EFileWriteDirectIO);
1.165 + switch (r)
1.166 + {
1.167 + case KErrNone:
1.168 + {
1.169 + TInt size;
1.170 + if (file.Size(size)==KErrNone && size!=0)
1.171 + {
1.172 + CFileStore* store=NULL;
1.173 + TRAP(r,store=CPermanentFileStore::FromL(file));
1.174 + if (r==KErrNotSupported||r==KErrEof)
1.175 + r=KErrCorrupt; // treat a bad store file as corrupt
1.176 + else if (r==KErrNone && store->Type()!=type)
1.177 + {
1.178 + // treat a wrong 3rd UID as corrupt
1.179 + delete store;
1.180 + store = NULL;
1.181 + r=KErrCorrupt;
1.182 + }
1.183 + if (r==KErrCorrupt)
1.184 + {
1.185 + // silently replace the entire file if it is corrupt
1.186 + // When the file server write caching is enabled, the order of file write operations is not guaranteed.
1.187 + // This could cause data inconsistency in some circumstances,for example, when the power is lost in the
1.188 + // middle of a database transaction.Therefore, the file write caching is disabled for this file to maintain integrity.
1.189 + //
1.190 + r=file.Replace(aFs,aName,EFileShareExclusive|EFileWrite|EFileWriteDirectIO);
1.191 + if (r==KErrInUse)
1.192 + break; // try again later...
1.193 + if (r==KErrNone)
1.194 + {
1.195 + CreateStoreL(file,type);
1.196 + return;
1.197 + }
1.198 + }
1.199 + __LEAVE_IF_ERROR(r);
1.200 + __ASSERT_DEBUG(store != NULL, User::Invariant());
1.201 + //coverity[use_after_free]
1.202 + iStore = store;
1.203 + if (store->Root()==KNullStreamId)
1.204 + CDictionaryStore::ConstructL();
1.205 + }
1.206 + else
1.207 + CreateStoreL(file,type);
1.208 + return;
1.209 + }
1.210 + case KErrNotFound:
1.211 + // When the file server write caching is enabled, the order of file write operations is not guaranteed.
1.212 + // This could cause data inconsistency in some circumstances,for example, when the power is lost in the
1.213 + // middle of a database transaction.Therefore, the file write caching is disabled for this file to maintain integrity.
1.214 + r=file.Create(aFs,aName,EFileShareExclusive|EFileWrite|EFileWriteDirectIO);
1.215 + if (r==KErrNone)
1.216 + {
1.217 + CreateStoreL(file,type);
1.218 + return;
1.219 + }
1.220 + else if (r==KErrAlreadyExists)
1.221 + ; // try and open after delay
1.222 + else
1.223 + __LEAVE(r);
1.224 + break;
1.225 + case KErrInUse:
1.226 + break;
1.227 + default:
1.228 + __LEAVE(r);
1.229 + }
1.230 + wait-=KFailTime;
1.231 + if (wait<=0)
1.232 + break; // waited too long
1.233 + if (wait>KSpinLimit)
1.234 + { // straight back to retry
1.235 + thread.Yield(); // force another reschedule
1.236 + continue;
1.237 + }
1.238 + // random wait time...
1.239 + if (wait==KSpinLimit)
1.240 + { // initialise random number generator
1.241 + TThreadId id=thread.Id();
1.242 + TUint idVal=*(const TUint*)&id;
1.243 + seed = MAKE_TINT64(idVal^TUint(this),idVal^TUint(&id));
1.244 + Math::Rand(seed);
1.245 + Math::Rand(seed);
1.246 + }
1.247 + TInt pause=Math::Rand(seed)>>11;
1.248 + pause=(pause&WaitDistribution[(pause>>8)&0x7])<<10;
1.249 + if (pause)
1.250 + {
1.251 + wait-=pause;
1.252 + User::After(pause);
1.253 + }
1.254 + }
1.255 + __LEAVE(KErrInUse);
1.256 + }
1.257 +
1.258 +void CDictionaryFileStore::CreateStoreL(RFile& aFile,const TUidType& aType)
1.259 + {
1.260 + CFileStore* store = CPermanentFileStore::NewL(aFile);
1.261 + iStore = store;
1.262 + store->SetTypeL(aType);
1.263 + CDictionaryStore::ConstructL();
1.264 + }