persistentstorage/sql/OsLayer/os_symbian.cpp
branchRCL_3
changeset 23 26645d81f48d
parent 21 28839de615b4
child 24 cc28652e0254
equal deleted inserted replaced
21:28839de615b4 23:26645d81f48d
    38 }
    38 }
    39 #include "os_common.h"
    39 #include "os_common.h"
    40 #include "SqliteSymbian.h"
    40 #include "SqliteSymbian.h"
    41 #include "FileBuf64.h"
    41 #include "FileBuf64.h"
    42 #include <e32math.h>
    42 #include <e32math.h>
    43 #include "UTraceSql.h"
       
    44 #ifdef _SQLPROFILER
    43 #ifdef _SQLPROFILER
    45 #include <hal.h>
    44 #include <hal.h>
    46 #include "../INC/SqlResourceProfiler.h"
    45 #include "../INC/SqlResourceProfiler.h"
    47 #endif
    46 #endif
       
    47 #include "SqliteUtil.h"
       
    48 #include "OstTraceDefinitions.h"
       
    49 #ifdef OST_TRACE_COMPILER_IN_USE
       
    50 #include "os_symbianTraces.h"
       
    51 #endif
       
    52 #include "SqliteTraceDef.h"
    48 
    53 
    49 /////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
    54 /////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
    50 /////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
    55 /////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
    51 /////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
    56 /////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
    52 
       
    53 /**
       
    54 Panic category - used by asserts in this file (the OS porting layer).
       
    55 
       
    56 @see TPanicCodes
       
    57 
       
    58 @internalComponent
       
    59 */
       
    60 _LIT(KPanicCategory, "Sqlite");
       
    61 
       
    62 /**
       
    63 Panic codes - used by asserts in this file (the OS porting layer).
       
    64 
       
    65 @see KPanicCategory
       
    66 
       
    67 @internalComponent
       
    68 */
       
    69 enum TPanicCodes
       
    70 	{
       
    71 	EPanicNullOsLayerDataPtr	= 1,
       
    72 	EPanicInvalidWAmount 		= 2,
       
    73 	EPanicOffset64bit 			= 3,
       
    74 	EPanicInvalidOpType			=11,
       
    75 	EPanicInvalidFhStr			=12,
       
    76 	EPanicInvalidFhData			=13,
       
    77 	EPanicInvalidArg			=14,
       
    78 	EPanicInvalidRAmount 		=15,
       
    79 	EPanicOsLayerDataExists		=16,
       
    80 	EPanicInvalidDrive			=17,
       
    81 	EPanicInvalidSectorSize		=18,
       
    82 	EPanicInternalError			=19,
       
    83 	EPanicNullDbFilePtr			=20,
       
    84 	EPanicFastCounterFreq		=21
       
    85 	};
       
    86 
    57 
    87 //The SQLite temp files willl be created in this subdir
    58 //The SQLite temp files willl be created in this subdir
    88 _LIT(KTempFileDir, "temp");
    59 _LIT(KTempFileDir, "temp");
    89 
    60 
    90 //Bit-mask constant. If xOpen()'s "aFlag" parameter contains one of these bits set, then the the file top be
    61 //Bit-mask constant. If xOpen()'s "aFlag" parameter contains one of these bits set, then the the file top be
   422 						{
   393 						{
   423 						TPtrC8 fn8(iOptional);
   394 						TPtrC8 fn8(iOptional);
   424 						fname.Copy(fn8);
   395 						fname.Copy(fn8);
   425 						}
   396 						}
   426 					//                                           0    1  2  3  4  5   6  7   8   9   10
   397 					//                                           0    1  2  3  4  5   6  7   8   9   10
   427 					RDebug::Print(_L("[SQL-OS]¬¬¬¬¬¬¬¬¬¬¬¬¬¬¬¬\"%X\"¬%c¬%S¬%d¬%d¬%ld¬%d¬%ld¬%ld¬%ld¬%S\r\n"),
   398 					RDebug::Print(_L("[SQL-OS]¬¬¬¬¬¬¬¬¬¬¬¬¬¬¬¬\"%X\"¬%c¬%S¬%d¬%d¬%ld¬%d¬%ld¬%ld¬%ld¬%S\n"),
   428 															//[SQL-OS]
   399 															//[SQL-OS]
   429 															//Handle
   400 															//Handle
   430 															//Time from start, microseconds
   401 															//Time from start, microseconds
   431 															//Subtype
   402 															//Subtype
   432 															//IPC sequence counter
   403 															//IPC sequence counter
   486 
   457 
   487 TInt FastCounterFrequency()
   458 TInt FastCounterFrequency()
   488 	{
   459 	{
   489 	TInt ticksPerSec = 0;
   460 	TInt ticksPerSec = 0;
   490 	TInt err = HAL::Get(HAL::EFastCounterFrequency, ticksPerSec);
   461 	TInt err = HAL::Get(HAL::EFastCounterFrequency, ticksPerSec);
   491 	__ASSERT_ALWAYS(err == KErrNone, User::Panic(KPanicCategory, EPanicFastCounterFreq));
   462 	__ASSERT_ALWAYS(err == KErrNone, __SQLITEPANIC2(ESqliteOsPanicFastCounterFreq));
   492 	return ticksPerSec;
   463 	return ticksPerSec;
   493 	}
   464 	}
   494 
   465 
   495 TInt sqlite3SymbianProfilerStart(TInt aCounterType)
   466 TInt sqlite3SymbianProfilerStart(TInt aCounterType)
   496 	{
   467 	{
   680 	}
   651 	}
   681 
   652 
   682 #endif//_SQLPROFILER
   653 #endif//_SQLPROFILER
   683 
   654 
   684 ///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
   655 ///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
       
   656 
       
   657 /**
       
   658 Os2SqliteErr() is called at the end of many of the interface functions of the OS porting layer (wherever it is appropriate - 
       
   659 TFileIo and TVfs interfaces). The purpose of this function is to identify the "out of memory" and "disk is full" errors
       
   660 reported by the used Symbian OS APIs (aOsErr parameter) and report them to SQLite as SQLITE_FULL and SQLITE_NOMEM errors.
       
   661 The KErrEof error (TFileIo::Read() can return KErrEof) is reported to SQLite as SQLITE_IOERR_SHORT_READ. The rest of failures
       
   662 are reported as the error specified in aDefaultErr parameter.
       
   663  
       
   664 @param aOsErr      Symbian OS error
       
   665 @param aDefaultErr The default SQLite error that should be used if the aOsErr parameter is not one of:
       
   666                      KErrNone, KErrEof, KErrNoMemory, KErrDiskFull
       
   667 @return SQLITE_OK,               The OS porting layer function call has completed successfully, 
       
   668           SQLITE_IOERR_SHORT_READ, The amount of the data read is less than the requested amount,
       
   669           SQLITE_IOERR_NOMEM,      Out of memory,
       
   670           SQLITE_FULL,             The disk is full,
       
   671           aDefaultErr,             The rest of failures will be reported as aDefaultErr.
       
   672 */
       
   673 static TInt Os2SqliteErr(TInt aOsErr, TInt aDefaultErr)
       
   674 	{
       
   675 	switch(aOsErr)
       
   676 		{
       
   677 		case KErrNone:
       
   678 			return SQLITE_OK;	
       
   679 		case KErrEof:
       
   680 			return SQLITE_IOERR_SHORT_READ;
       
   681 		case KErrNoMemory:
       
   682 			return SQLITE_IOERR_NOMEM;
       
   683 		case KErrDiskFull:
       
   684 			return SQLITE_FULL;
       
   685 		default:
       
   686 #ifdef _DEBUG		
       
   687 			RDebug::Print(_L("SQLite OS porting layer, Os2SqliteErr(), err=%d\n"), aOsErr);
       
   688 			SQLITE_TRACE_OS(OstTrace1(TRACE_INTERNALS, OS2SQLITEERR, "OS;0;Os2SqliteErr;aOsErr=%d", aOsErr));
       
   689 #endif			
       
   690 			break;
       
   691 		}
       
   692 	return aDefaultErr;
       
   693 	}
       
   694 	
       
   695 
       
   696 ///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
   685 /////////////////////       COsLayerData class declaration   //////////////////////////////////////////////////////////////////
   697 /////////////////////       COsLayerData class declaration   //////////////////////////////////////////////////////////////////
   686 ///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
   698 ///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
   687 
   699 
   688 /**
   700 /**
   689 COsLayerData singleton class manages a single SQLite OS layer copy of the following data: 
   701 COsLayerData singleton class manages a single SQLite OS layer copy of the following data: 
   845 	static int FullPathName(sqlite3_vfs* aVfs, const char* aRelative, int aBufLen, char* aBuf);
   857 	static int FullPathName(sqlite3_vfs* aVfs, const char* aRelative, int aBufLen, char* aBuf);
   846 	static int Randomness(sqlite3_vfs* aVfs, int aBufLen, char* aBuf);
   858 	static int Randomness(sqlite3_vfs* aVfs, int aBufLen, char* aBuf);
   847 	static int Sleep(sqlite3_vfs* aVfs, int aMicrosec);
   859 	static int Sleep(sqlite3_vfs* aVfs, int aMicrosec);
   848 	static int CurrentTime(sqlite3_vfs* aVfs, double* aNow);
   860 	static int CurrentTime(sqlite3_vfs* aVfs, double* aNow);
   849 	static int GetLastError(sqlite3_vfs *sVfs, int aBufLen, char* aBuf);
   861 	static int GetLastError(sqlite3_vfs *sVfs, int aBufLen, char* aBuf);
   850     static TInt DoGetDeviceCharacteristicsAndSectorSize(TDbFile& aDbFile, TInt& aRecReadBufSize);
   862 	static TInt DoGetDeviceCharacteristicsAndSectorSize(TDbFile& aDbFile, TInt& aRecReadBufSize);
   851 
       
   852 private:
   863 private:
   853 	static TInt DoOpenFromHandle(TDbFile& aDbFile, const RMessage2& aMsg, TBool aReadOnly);
   864 	static TInt DoOpenFromHandle(TDbFile& aDbFile, const RMessage2& aMsg, TBool aReadOnly);
   854 	static inline TInt DoGetVolumeIoParamInfo(RFs& aFs, TInt aDriveNo, TVolumeIOParamInfo& aVolumeInfo);
   865 	static inline TInt DoGetVolumeIoParamInfo(RFs& aFs, TInt aDriveNo, TVolumeIOParamInfo& aVolumeInfo);
   855 	static TInt DoGetDeviceCharacteristics(const TDriveInfo& aDriveInfo, const TVolumeIOParamInfo& aVolumeInfo);
   866 	static TInt DoGetDeviceCharacteristics(const TDriveInfo& aDriveInfo, const TVolumeIOParamInfo& aVolumeInfo);
   856 	static TInt DoGetSectorSize(const TDriveInfo& aDriveInfo, const TVolumeIOParamInfo& aVolumeInfo);
   867 	static TInt DoGetSectorSize(const TDriveInfo& aDriveInfo, const TVolumeIOParamInfo& aVolumeInfo);
   956                            
   967                            
   957 @panic Sqlite 16 In _DEBUG mode - the COsLayerData instance has been created already.
   968 @panic Sqlite 16 In _DEBUG mode - the COsLayerData instance has been created already.
   958 */
   969 */
   959 /* static */ TInt COsLayerData::Create()
   970 /* static */ TInt COsLayerData::Create()
   960 	{
   971 	{
   961 	__ASSERT_DEBUG(!COsLayerData::iOsLayerData, User::Panic(KPanicCategory, EPanicOsLayerDataExists));
   972 	SQLITE_TRACE_OS(OstTrace0(TRACE_INTERNALS, COSLAYERDATA_CREATE_ENTRY, "OS-Entry;0;COsLayerData::Create"));
       
   973 	__ASSERT_DEBUG(!COsLayerData::iOsLayerData, __SQLITEPANIC2(ESqliteOsPanicOsLayerDataExists));
   962 	if(!COsLayerData::iOsLayerData)
   974 	if(!COsLayerData::iOsLayerData)
   963 		{
   975 		{
   964 		COsLayerData::iOsLayerData = new COsLayerData;
   976 		COsLayerData::iOsLayerData = new COsLayerData;
   965 		if(!COsLayerData::iOsLayerData)
   977 		if(!COsLayerData::iOsLayerData)
   966 			{
   978 			{
       
   979 			SQLITE_TRACE_OS(OstTrace0(TRACE_INTERNALS, COSLAYERDATA_CREATE_EXIT1, "OS-Exit;0;COsLayerData::Create;err=KErrNoMemory"));
   967 			return KErrNoMemory;	
   980 			return KErrNoMemory;	
   968 			}
   981 			}
   969 		TInt err = COsLayerData::iOsLayerData->DoCreate();
   982 		TInt err = COsLayerData::iOsLayerData->DoCreate();
   970 		if(err != KErrNone)
   983 		if(err != KErrNone)
   971 			{
   984 			{
   972 			delete COsLayerData::iOsLayerData;
   985 			delete COsLayerData::iOsLayerData;
   973 			COsLayerData::iOsLayerData = NULL;
   986 			COsLayerData::iOsLayerData = NULL;
       
   987 			SQLITE_TRACE_OS(OstTrace1(TRACE_INTERNALS, COSLAYERDATA_CREATE_EXIT2, "OS-Exit;0;COsLayerData::Create;err=%d", err));
   974 			return err;
   988 			return err;
   975 			}
   989 			}
   976 		}
   990 		}
       
   991 	SQLITE_TRACE_OS(OstTrace0(TRACE_INTERNALS, COSLAYERDATA_CREATE_EXIT3, "OS-Exit;0;COsLayerData::Create;err=KErrNone"));
   977 	return KErrNone;
   992 	return KErrNone;
   978 	}
   993 	}
   979 
   994 
   980 /**
   995 /**
   981 Destroys the COsLayerData instance.
   996 Destroys the COsLayerData instance.
   982 */
   997 */
   983 /* static */ inline void COsLayerData::Destroy()
   998 /* static */ inline void COsLayerData::Destroy()
   984 	{
   999 	{
   985 	delete COsLayerData::iOsLayerData;
  1000 	delete COsLayerData::iOsLayerData;
   986 	COsLayerData::iOsLayerData = NULL;
  1001 	COsLayerData::iOsLayerData = NULL;
       
  1002 	SQLITE_TRACE_OS(OstTrace0(TRACE_INTERNALS, COSLAYERDATA_DESTROY, "OS;0;COsLayerData::Destroy"));
   987 	}
  1003 	}
   988 
  1004 
   989 /**
  1005 /**
   990 Returns a reference to the single COsLayerData instance.
  1006 Returns a reference to the single COsLayerData instance.
   991 
  1007 
   992 @panic Sqlite 1 In _DEBUG mode if the COsLayerData instance is NULL.
  1008 @panic Sqlite 1 In _DEBUG mode if the COsLayerData instance is NULL.
   993 */
  1009 */
   994 /* static */ inline COsLayerData& COsLayerData::Instance()
  1010 /* static */ inline COsLayerData& COsLayerData::Instance()
   995 	{
  1011 	{
   996 	__ASSERT_DEBUG(COsLayerData::iOsLayerData != NULL, User::Panic(KPanicCategory, EPanicNullOsLayerDataPtr));
  1012 	__ASSERT_DEBUG(COsLayerData::iOsLayerData != NULL, __SQLITEPANIC2(ESqliteOsPanicNullOsLayerDataPtr));
   997 	return *COsLayerData::iOsLayerData;
  1013 	return *COsLayerData::iOsLayerData;
   998 	}
  1014 	}
   999 
  1015 
  1000 /**
  1016 /**
  1001 Sets the last OS error code data member. The stored OS error code data member will be set only if it is
  1017 Sets the last OS error code data member. The stored OS error code data member will be set only if it is
  1065 
  1081 
  1066 @panic Sqlite 13 In _DEBUG mode - aMsg is NULL.
  1082 @panic Sqlite 13 In _DEBUG mode - aMsg is NULL.
  1067 */
  1083 */
  1068 inline void COsLayerData::RetrieveAndResetFhData(const RMessage2*& aMsg, TBool& aReadOnly)
  1084 inline void COsLayerData::RetrieveAndResetFhData(const RMessage2*& aMsg, TBool& aReadOnly)
  1069 	{
  1085 	{
  1070 	__ASSERT_DEBUG(iMessage != NULL, User::Panic(KPanicCategory, EPanicInvalidFhData));
  1086 	__ASSERT_DEBUG(iMessage != NULL, __SQLITEPANIC(ESqliteOsPanicInvalidFhData));
  1071 	aMsg = iMessage; 
  1087 	aMsg = iMessage; 
  1072 	aReadOnly = iReadOnly;
  1088 	aReadOnly = iReadOnly;
  1073 	iMessage = NULL;
  1089 	iMessage = NULL;
  1074 	}
  1090 	}
  1075 
  1091 
  1091 
  1107 
  1092 Note: No SQLite functions should be called inside the destructor, because SQLite is already shutdown-ed!
  1108 Note: No SQLite functions should be called inside the destructor, because SQLite is already shutdown-ed!
  1093 */
  1109 */
  1094 inline COsLayerData::~COsLayerData()
  1110 inline COsLayerData::~COsLayerData()
  1095 	{
  1111 	{
       
  1112 	SQLITE_TRACE_OS(OstTrace1(TRACE_INTERNALS, COSLAYERDATA_COSLAYERDATA2, "OS;0;COsLayerData::~COsLayerData;iFs.Handle()=0x%X", (TUint)iFs.Handle()));
  1096 	__FS_CALL(EFsOpFsClose, 0);
  1113 	__FS_CALL(EFsOpFsClose, 0);
  1097 	iFs.Close();	
  1114 	iFs.Close();	
  1098 	}
  1115 	}
  1099 
  1116 
  1100 /**
  1117 /**
  1147 	err = iFs.MkDir(parse.DriveAndPath());
  1164 	err = iFs.MkDir(parse.DriveAndPath());
  1148     if(err != KErrNone && err != KErrAlreadyExists)
  1165     if(err != KErrNone && err != KErrAlreadyExists)
  1149     	{
  1166     	{
  1150 		return err;
  1167 		return err;
  1151     	}
  1168     	}
       
  1169 	SQLITE_TRACE_OS(OstTraceExt3(TRACE_INTERNALS, COSLAYERDATA_DOCREATE, "OS;0;COsLayerData::DoCreate;iFs.Handle()=0x%X;sysDrive=%d;iSysPrivDir=%S", (TUint)iFs.Handle(), sysDrive, __SQLITEPRNSTR(iSysPrivDir)));
  1152 	return KErrNone;
  1170 	return KErrNone;
  1153 	}
  1171 	}
  1154 
  1172 
  1155 ///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
  1173 ///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
  1156 ///////////////////////     Symbian OS specific functions (called by the SQL server)        ///////////////////////////////////
  1174 ///////////////////////     Symbian OS specific functions (called by the SQL server)        ///////////////////////////////////
  1196 	if(sqliteErr != SQLITE_OK)
  1214 	if(sqliteErr != SQLITE_OK)
  1197 		{
  1215 		{
  1198 		osErr = sqliteErr == SQLITE_NOMEM ? KErrNoMemory : KErrGeneral;
  1216 		osErr = sqliteErr == SQLITE_NOMEM ? KErrNoMemory : KErrGeneral;
  1199 		COsLayerData::Destroy();
  1217 		COsLayerData::Destroy();
  1200 		}
  1218 		}
       
  1219 	SQLITE_TRACE_OS(OstTrace1(TRACE_INTERNALS, SQLITE3SYMBIANLIBINIT, "OS;0;sqlite3SymbianLibInit;osErr=%d", osErr));
  1201 	return osErr;
  1220 	return osErr;
  1202 	}
  1221 	}
  1203 
  1222 
  1204 /**
  1223 /**
  1205 This function must be called once after finishing working with sqlite.
  1224 This function must be called once after finishing working with sqlite.
  1215 */
  1234 */
  1216 void sqlite3SymbianLibFinalize(void)
  1235 void sqlite3SymbianLibFinalize(void)
  1217 	{
  1236 	{
  1218 	(void)sqlite3_shutdown();
  1237 	(void)sqlite3_shutdown();
  1219 	COsLayerData::Destroy();
  1238 	COsLayerData::Destroy();
       
  1239 	SQLITE_TRACE_OS(OstTrace0(TRACE_INTERNALS, SQLITE3SYMBIANLIBFINALIZE, "OS;0;sqlite3SymbianLibFinalize"));
  1220 	}
  1240 	}
  1221 
  1241 
  1222 /**
  1242 /**
  1223 This function is part of Symbian OS specific SQLITE API.
  1243 This function is part of Symbian OS specific SQLITE API.
  1224 
  1244 
  1251 	{
  1271 	{
  1252 	if(aFileName)
  1272 	if(aFileName)
  1253 		{
  1273 		{
  1254 		wchar_t* dest = reinterpret_cast <wchar_t*> (const_cast <TUint16*> (aFileNameDestBuf.Ptr()));
  1274 		wchar_t* dest = reinterpret_cast <wchar_t*> (const_cast <TUint16*> (aFileNameDestBuf.Ptr()));
  1255 		TInt len = mbstowcs(dest, aFileName, aFileNameDestBuf.MaxLength());
  1275 		TInt len = mbstowcs(dest, aFileName, aFileNameDestBuf.MaxLength());
  1256 		__ASSERT_DEBUG(len >= 0, User::Panic(KPanicCategory, EPanicInternalError));
  1276 		__ASSERT_DEBUG(len >= 0, __SQLITEPANIC2(ESqliteOsPanicInternalError));
  1257 		//If len == aFileNameDestBuf.MaxLength(), then the output buffer is too small.
  1277 		//If len == aFileNameDestBuf.MaxLength(), then the output buffer is too small.
  1258 		if(len < aFileNameDestBuf.MaxLength())
  1278 		if(len < aFileNameDestBuf.MaxLength())
  1259 			{
  1279 			{
  1260 			aFileNameDestBuf.SetLength(len);
  1280 			aFileNameDestBuf.SetLength(len);
  1261 			return ETrue;
  1281 			return ETrue;
  1280 static TBool ConvertFromUnicode(const TDesC& aFileName, TDes8& aFileNameDestBuf)
  1300 static TBool ConvertFromUnicode(const TDesC& aFileName, TDes8& aFileNameDestBuf)
  1281 	{
  1301 	{
  1282 	char* dest = reinterpret_cast <char*> (const_cast <TUint8*> (aFileNameDestBuf.Ptr()));
  1302 	char* dest = reinterpret_cast <char*> (const_cast <TUint8*> (aFileNameDestBuf.Ptr()));
  1283 	const wchar_t* src = reinterpret_cast <const wchar_t*> (aFileName.Ptr());
  1303 	const wchar_t* src = reinterpret_cast <const wchar_t*> (aFileName.Ptr());
  1284 	TInt len = wcstombs(dest, src, aFileNameDestBuf.MaxLength());
  1304 	TInt len = wcstombs(dest, src, aFileNameDestBuf.MaxLength());
  1285 	__ASSERT_DEBUG(len >= 0, User::Panic(KPanicCategory, EPanicInternalError));
  1305 	__ASSERT_DEBUG(len >= 0, __SQLITEPANIC2(ESqliteOsPanicInternalError));
  1286 	//If len == aFileNameDestBuf.MaxLength(), then the output buffer is too small.
  1306 	//If len == aFileNameDestBuf.MaxLength(), then the output buffer is too small.
  1287 	if(len < aFileNameDestBuf.MaxLength())
  1307 	if(len < aFileNameDestBuf.MaxLength())
  1288 		{
  1308 		{
  1289 		aFileNameDestBuf.SetLength(len);
  1309 		aFileNameDestBuf.SetLength(len);
  1290 		return ETrue;
  1310 		return ETrue;
  1422 @panic Sqlite 12 In _DEBUG mode - the extracted RMessage2 pointer is NULL.
  1442 @panic Sqlite 12 In _DEBUG mode - the extracted RMessage2 pointer is NULL.
  1423 */
  1443 */
  1424 static void FhExtractAndStore(TDes& aDbFileName)
  1444 static void FhExtractAndStore(TDes& aDbFileName)
  1425 	{
  1445 	{
  1426 	TInt fhStartPos = aDbFileName.Locate(TChar(KFhSeparator));
  1446 	TInt fhStartPos = aDbFileName.Locate(TChar(KFhSeparator));
  1427 	__ASSERT_DEBUG(fhStartPos == KFhMarkPos, User::Panic(KPanicCategory, EPanicInvalidFhStr));
  1447 	__ASSERT_DEBUG(fhStartPos == KFhMarkPos, __SQLITEPANIC2(ESqliteOsPanicInvalidFhStr));
  1428 	//If this file name string contains file handles
  1448 	//If this file name string contains file handles
  1429 	if(fhStartPos == KFhMarkPos)
  1449 	if(fhStartPos == KFhMarkPos)
  1430 		{
  1450 		{
  1431 		//Extract from aDbFileName string RMessage2 object's address
  1451 		//Extract from aDbFileName string RMessage2 object's address
  1432 		TLex lex(aDbFileName.Mid(fhStartPos + KFhMsgAddrPos, KFhMsgAddrLen));
  1452 		TLex lex(aDbFileName.Mid(fhStartPos + KFhMsgAddrPos, KFhMsgAddrLen));
  1433 		TUint32 addr;
  1453 		TUint32 addr;
  1434 		TInt err = lex.Val(addr, EHex);
  1454 		TInt err = lex.Val(addr, EHex);
  1435 		__ASSERT_DEBUG(err == KErrNone, User::Panic(KPanicCategory, EPanicInvalidFhStr));
  1455 		__ASSERT_DEBUG(err == KErrNone, __SQLITEPANIC2(ESqliteOsPanicInvalidFhStr));
  1436 		if(err == KErrNone)
  1456 		if(err == KErrNone)
  1437 			{
  1457 			{
  1438 			//Cast the address to RMessage2 pointer.
  1458 			//Cast the address to RMessage2 pointer.
  1439 			const RMessage2* msg = reinterpret_cast <const RMessage2*> (addr);
  1459 			const RMessage2* msg = reinterpret_cast <const RMessage2*> (addr);
  1440 			__ASSERT_DEBUG(msg != NULL, User::Panic(KPanicCategory, EPanicInvalidFhStr));
  1460 			__ASSERT_DEBUG(msg != NULL, __SQLITEPANIC2(ESqliteOsPanicInvalidFhStr));
  1441 			if(msg)
  1461 			if(msg)
  1442 				{
  1462 				{
  1443 				//Store the data from aDbFileName in the single COsLayerData instance.
  1463 				//Store the data from aDbFileName in the single COsLayerData instance.
  1444 				TBool readOnly = aDbFileName[fhStartPos + KFhRoPos] > '0';
  1464 				TBool readOnly = aDbFileName[fhStartPos + KFhRoPos] > '0';
  1445 				COsLayerData::Instance().StoreFhData(msg, readOnly);
  1465 				COsLayerData::Instance().StoreFhData(msg, readOnly);
  1467 	{
  1487 	{
  1468 #ifdef _SQLPROFILER
  1488 #ifdef _SQLPROFILER
  1469 	iIsJournal = EFalse;
  1489 	iIsJournal = EFalse;
  1470 #endif
  1490 #endif
  1471 	pMethods = 0;
  1491 	pMethods = 0;
       
  1492 	SQLITE_TRACE_OS(OstTrace1(TRACE_INTERNALS, TDBFILE_TDBFILE, "OS;0x%X;TDbFile::TDbFile", (TUint)this));
  1472 	}
  1493 	}
  1473 
  1494 
  1474 /**
  1495 /**
  1475 Casts the passed sqlite3_file pointer to a reference to the derived class - TDbFile&.
  1496 Casts the passed sqlite3_file pointer to a reference to the derived class - TDbFile&.
  1476 All sqlite3_file pointers passed to TFileIo methods are actually pointers to TDbFile instances. 
  1497 All sqlite3_file pointers passed to TFileIo methods are actually pointers to TDbFile instances. 
  1487 
  1508 
  1488 @internalComponent
  1509 @internalComponent
  1489 */
  1510 */
  1490 static inline TDbFile& DbFile(sqlite3_file* aDbFile)
  1511 static inline TDbFile& DbFile(sqlite3_file* aDbFile)
  1491 	{
  1512 	{
  1492 	__ASSERT_DEBUG(aDbFile != 0, User::Panic(KPanicCategory, EPanicNullDbFilePtr));
  1513 	__ASSERT_DEBUG(aDbFile != 0, __SQLITEPANIC2(ESqliteOsPanicNullDbFilePtr));
  1493 	return *(static_cast <TDbFile*> (aDbFile));
  1514 	return *(static_cast <TDbFile*> (aDbFile));
  1494 	}
  1515 	}
  1495 
  1516 
  1496 ///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
  1517 ///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
  1497 /////////////////////       TFileIo class definition    ///////////////////////////////////////////////////////////////////////
  1518 /////////////////////       TFileIo class definition    ///////////////////////////////////////////////////////////////////////
  1508 
  1529 
  1509 @see TDbFile
  1530 @see TDbFile
  1510 */
  1531 */
  1511 /* static */ int TFileIo::Close(sqlite3_file* aDbFile)
  1532 /* static */ int TFileIo::Close(sqlite3_file* aDbFile)
  1512 	{
  1533 	{
  1513 	SQLUTRACE_PROFILER(aDbFile);
       
  1514 	TDbFile& dbFile = ::DbFile(aDbFile);
  1534 	TDbFile& dbFile = ::DbFile(aDbFile);
       
  1535 	SQLITE_TRACE_OS(OstTrace1(TRACE_INTERNALS, TFILEIO_CLOSE1, "OS;0x%X;TFileIo::Close", (TUint)&dbFile));
  1515 	__OS_CALL(EOsFileClose, 0, 0);
  1536 	__OS_CALL(EOsFileClose, 0, 0);
  1516 	__OSTIME_COUNTER(TheOsCallTicks[EOsFileClose], ::OsCallProfile(dbFile.iIsJournal, EOsFileClose), 0, 0, aDbFile, 0);
  1537 	__OSTIME_COUNTER(TheOsCallTicks[EOsFileClose], ::OsCallProfile(dbFile.iIsJournal, EOsFileClose), 0, 0, aDbFile, 0);
  1517 	__FS_CALL(EFsOpFileClose, 0);
  1538 	__FS_CALL(EFsOpFileClose, 0);
  1518   dbFile.iFileBuf.Close();
  1539 	dbFile.iFileBuf.Close();
  1519 	return SQLITE_OK;
  1540 	return SQLITE_OK;
  1520 	}
  1541 	}
  1521 
  1542 
  1522 /**
  1543 /**
  1523 SQLite OS porting layer API.
  1544 SQLite OS porting layer API.
  1541 @see COsLayerData::SetOsErrorCode()
  1562 @see COsLayerData::SetOsErrorCode()
  1542 @see TDbFile
  1563 @see TDbFile
  1543 */
  1564 */
  1544 /* static */ int TFileIo::Read(sqlite3_file* aDbFile, void* aBuf, int aAmt, sqlite3_int64 aOffset)
  1565 /* static */ int TFileIo::Read(sqlite3_file* aDbFile, void* aBuf, int aAmt, sqlite3_int64 aOffset)
  1545 	{
  1566 	{
  1546 	SQLUTRACE_PROFILER(aDbFile);
       
  1547 	SYMBIAN_TRACE_SQL_EVENTS_ONLY(UTF::Printf(UTF::TTraceContext(UTF::EInternals), KFileRead, aAmt, aOffset));
       
  1548 	TDbFile& dbFile = ::DbFile(aDbFile);
  1567 	TDbFile& dbFile = ::DbFile(aDbFile);
       
  1568 	SQLITE_TRACE_OS(OstTraceExt3(TRACE_INTERNALS, TFILEIO_READ_ENTRY, "OS-Entry;0x%X;TFileIo::Read;aAmt=%d;aOffset=%lld", (TUint)&dbFile, aAmt, aOffset));
  1549 	__OS_CALL(EOsFileRead, 0, 0);
  1569 	__OS_CALL(EOsFileRead, 0, 0);
  1550 	__COUNTER_INCR(TheSqlSrvProfilerFileRead);
  1570 	__COUNTER_INCR(TheSqlSrvProfilerFileRead);
  1551 	__OSTIME_COUNTER(TheOsCallTicks[EOsFileRead], ::OsCallProfile(dbFile.iIsJournal, EOsFileRead), aOffset, aAmt, aDbFile, 0);
  1571 	__OSTIME_COUNTER(TheOsCallTicks[EOsFileRead], ::OsCallProfile(dbFile.iIsJournal, EOsFileRead), aOffset, aAmt, aDbFile, 0);
  1552 	TPtr8 ptr((TUint8*)aBuf, 0, aAmt);
  1572 	TPtr8 ptr((TUint8*)aBuf, 0, aAmt);
  1553 	TInt err = dbFile.iFileBuf.Read(aOffset, ptr);
  1573 	TInt err = dbFile.iFileBuf.Read(aOffset, ptr);
  1554 	TInt cnt = ptr.Length();
  1574 	TInt cnt = ptr.Length();
  1555 	TInt sqliteErr = SQLITE_IOERR_READ;
  1575 	TInt sqliteErr = ::Os2SqliteErr(err, SQLITE_IOERR_READ);
  1556 	switch(err)
  1576 	if(cnt != aAmt && (sqliteErr == SQLITE_OK || sqliteErr == SQLITE_IOERR_SHORT_READ))
  1557 		{
  1577 		{
  1558 		case KErrNone:
  1578 		Mem::FillZ(static_cast <TUint8*> (aBuf) + cnt, aAmt - cnt);
  1559 			sqliteErr = SQLITE_OK;	
  1579 		err = KErrEof;
  1560 			if(cnt != aAmt)
  1580 		sqliteErr = SQLITE_IOERR_SHORT_READ;
  1561 				{
       
  1562 				Mem::FillZ(static_cast <TUint8*> (aBuf) + cnt, aAmt - cnt);
       
  1563 				sqliteErr = SQLITE_IOERR_SHORT_READ;
       
  1564 				err = KErrEof;
       
  1565 				}
       
  1566 			break;
       
  1567 		case KErrEof:
       
  1568 			Mem::FillZ(static_cast <TUint8*> (aBuf) + cnt, aAmt - cnt);
       
  1569 			sqliteErr = SQLITE_IOERR_SHORT_READ;
       
  1570 			break;
       
  1571 		case KErrNoMemory:
       
  1572 			sqliteErr = SQLITE_IOERR_NOMEM;
       
  1573 			break;
       
  1574 		default:
       
  1575 			break;
       
  1576 		}
  1581 		}
  1577 	COsLayerData::Instance().SetOsErrorCode(err);
  1582 	COsLayerData::Instance().SetOsErrorCode(err);
       
  1583 	SQLITE_TRACE_OS(OstTraceExt4(TRACE_INTERNALS, TFILEIO_READ_EXIT, "OS-Exit;0x%X;TFileIo::Read;cnt=%d;err=%d;sqliteErr=%d", (TUint)&dbFile, cnt, err, sqliteErr));
  1578 	return sqliteErr;
  1584 	return sqliteErr;
  1579 	}
  1585 	}
  1580 
  1586 
  1581 //Creates a temporary file in "\temp" subdirectory of osLayerData.iSysPrivDir directory.
  1587 //Creates a temporary file in "\temp" subdirectory of osLayerData.iSysPrivDir directory.
  1582 //If the function fails, the temp file will be closed and deleted, 
  1588 //If the function fails, the temp file will be closed and deleted, 
  1651 @see COsLayerData::SetOsErrorCode()
  1657 @see COsLayerData::SetOsErrorCode()
  1652 @see TDbFile
  1658 @see TDbFile
  1653 */
  1659 */
  1654 /* static */ int TFileIo::Write(sqlite3_file* aDbFile, const void* aData, int aAmt, sqlite3_int64 aOffset)
  1660 /* static */ int TFileIo::Write(sqlite3_file* aDbFile, const void* aData, int aAmt, sqlite3_int64 aOffset)
  1655 	{
  1661 	{
  1656 	SQLUTRACE_PROFILER(aDbFile);
       
  1657 	SYMBIAN_TRACE_SQL_EVENTS_ONLY(UTF::Printf(UTF::TTraceContext(UTF::EInternals), KFileWrite, aAmt, aOffset));
       
  1658 	TDbFile& dbFile = ::DbFile(aDbFile);
  1662 	TDbFile& dbFile = ::DbFile(aDbFile);
       
  1663 	SQLITE_TRACE_OS(OstTraceExt3(TRACE_INTERNALS, TFILEIO_WRITE_ENTRY, "OS-Entry;0x%X;TFileIo::Write;aAmt=%d;aOffset=%lld", (TUint)&dbFile, aAmt, aOffset));
       
  1664 	__OS_CALL(EOsFileWrite, 0, 0);
       
  1665     __COUNTER_INCR(TheSqlSrvProfilerFileWrite);
       
  1666 	__OSTIME_COUNTER(TheOsCallTicks[EOsFileWrite], ::OsCallProfile(dbFile.iIsJournal, EOsFileWrite), aOffset, aAmt, aDbFile, 0);
  1659 	TInt err = KErrNone;
  1667 	TInt err = KErrNone;
  1660 	if(!dbFile.iIsFileCreated)
  1668 	if(!dbFile.iIsFileCreated)
  1661 	    {//Create a temp file if it has not been created. 
  1669 	    {//Create a temp file if it has not been created. 
  1662 	    err = TFileIo::DoCreateTempFile(dbFile);
  1670 	    err = TFileIo::DoCreateTempFile(dbFile);
  1663 	    }
  1671 	    }
  1664 	if(err != KErrNone)
  1672 	if(err != KErrNone)
  1665 	    {
  1673 	    {
  1666         COsLayerData::Instance().SetOsErrorCode(err);
  1674         COsLayerData::Instance().SetOsErrorCode(err);
  1667         return err == KErrNoMemory ? SQLITE_IOERR_NOMEM : SQLITE_FULL;
  1675 		return ::Os2SqliteErr(err, SQLITE_FULL);
  1668 	    }
  1676 	    }
       
  1677 	err = KErrAccessDenied;
       
  1678 	if(!dbFile.iReadOnly)
       
  1679 		{
       
  1680 		TPtrC8 ptr((const TUint8*)aData, aAmt);
       
  1681 		err = dbFile.iFileBuf.Write(aOffset, ptr);
       
  1682 		}
       
  1683 	COsLayerData::Instance().SetOsErrorCode(err);
  1669 	
  1684 	
  1670     __OS_CALL(EOsFileWrite, 0, 0);
  1685 	const TInt KFreePageCountOffset = 36;//hard-coded constant. SQLite does not offer anything - a constant or #define.
  1671     __COUNTER_INCR(TheSqlSrvProfilerFileWrite);
  1686 	//The checks in the "if" bellow do:
  1672     __OSTIME_COUNTER(TheOsCallTicks[EOsFileWrite], ::OsCallProfile(dbFile.iIsJournal, EOsFileWrite), aOffset, aAmt, aDbFile, 0);
  1687 	// - "err == KErrNone" - check the free page count only after a successful "write";
  1673     err = KErrAccessDenied;
  1688 	// - "aOffset == 0"    - check the free page count only if the write operation affects the system page (at aOffset = 0);
  1674     if(!dbFile.iReadOnly)
  1689 	// - "aAmt >= (KFreePageCountOffset + sizeof(int))" - check the free page count only if the amount of bytes to be written
  1675         {
  1690 	//						 is more than the offset of the free page counter (othewrise the free page counter is not affected
  1676         TPtrC8 ptr((const TUint8*)aData, aAmt);
  1691 	//						 by this write operation);
  1677         err = dbFile.iFileBuf.Write(aOffset, ptr);
  1692 	// - "dbFile.iFreePageCallback.IsValid()" - check the free page count only if there is a valid callback;
  1678         }
  1693 	if(err == KErrNone  && aOffset == 0 && aAmt >= (KFreePageCountOffset + sizeof(int)) && dbFile.iFreePageCallback.IsValid())
  1679     COsLayerData::Instance().SetOsErrorCode(err);
  1694 		{
  1680     
  1695 		const TUint8* ptr = static_cast <const TUint8*> (aData) + KFreePageCountOffset;
  1681     const TInt KFreePageCountOffset = 36;//hard-coded constant. SQLite does not offer anything - a constant or #define.
  1696 		TInt freePageCount = (ptr[0] << 24) | (ptr[1] << 16) | (ptr[2] << 8) | ptr[3];
  1682     //The checks in the "if" bellow do:
  1697 		dbFile.iFreePageCallback.CheckAndCallback(freePageCount);
  1683     // - "err == KErrNone" - check the free page count only after a successful "write";
  1698 		}
  1684     // - "aOffset == 0"    - check the free page count only if the write operation affects the system page (at aOffset = 0);
  1699 		
  1685     // - "aAmt >= (KFreePageCountOffset + sizeof(int))" - check the free page count only if the amount of bytes to be written
  1700 	TInt sqliteErr = ::Os2SqliteErr(err, SQLITE_FULL);
  1686     //						 is more than the offset of the free page counter (othewrise the free page counter is not affected
  1701 	SQLITE_TRACE_OS(OstTraceExt3(TRACE_INTERNALS, TFILEIO_WRITE_EXIT, "OS-Exit;0x%X;TFileIo::Write;err=%d;sqliteErr=%d", (TUint)&dbFile, err, sqliteErr));
  1687     //						 by this write operation);
  1702 	return sqliteErr;
  1688     // - "dbFile.iFreePageCallback.IsValid()" - check the free page count only if there is a valid callback;
       
  1689     if(err == KErrNone  && aOffset == 0 && aAmt >= (KFreePageCountOffset + sizeof(int)) && dbFile.iFreePageCallback.IsValid())
       
  1690         {
       
  1691         const TUint8* ptr = static_cast <const TUint8*> (aData) + KFreePageCountOffset;
       
  1692         TInt freePageCount = (ptr[0] << 24) | (ptr[1] << 16) | (ptr[2] << 8) | ptr[3];
       
  1693         dbFile.iFreePageCallback.CheckAndCallback(freePageCount);
       
  1694         }
       
  1695 	return err == KErrNone ? SQLITE_OK : (err == KErrNoMemory ? SQLITE_IOERR_NOMEM : SQLITE_FULL);
       
  1696 	}
  1703 	}
  1697 
  1704 
  1698 /**
  1705 /**
  1699 SQLite OS porting layer API.
  1706 SQLite OS porting layer API.
  1700 
  1707 
  1715 @see COsLayerData::SetOsErrorCode()
  1722 @see COsLayerData::SetOsErrorCode()
  1716 @see TDbFile
  1723 @see TDbFile
  1717 */
  1724 */
  1718 /* static */ int TFileIo::Truncate(sqlite3_file* aDbFile, sqlite3_int64 aLength)
  1725 /* static */ int TFileIo::Truncate(sqlite3_file* aDbFile, sqlite3_int64 aLength)
  1719 	{
  1726 	{
  1720 	SQLUTRACE_PROFILER(aDbFile);
       
  1721 	SYMBIAN_TRACE_SQL_EVENTS_ONLY(UTF::Printf(UTF::TTraceContext(UTF::EInternals), KFileTruncate, aLength));
       
  1722 	TDbFile& dbFile = ::DbFile(aDbFile);
  1727 	TDbFile& dbFile = ::DbFile(aDbFile);
       
  1728 	SQLITE_TRACE_OS(OstTraceExt2(TRACE_INTERNALS, TFILEIO_TRUNCATE_ENTRY, "OS-Entry;0x%X;TFileIo::Truncate;aLength=%lld", (TUint)&dbFile, aLength));
  1723 	__OS_CALL(EOsFileTruncate, 0, 0);
  1729 	__OS_CALL(EOsFileTruncate, 0, 0);
  1724     __COUNTER_INCR(TheSqlSrvProfilerFileSetSize);
  1730     __COUNTER_INCR(TheSqlSrvProfilerFileSetSize);
  1725 	__OSTIME_COUNTER(TheOsCallTicks[EOsFileTruncate], ::OsCallProfile(dbFile.iIsJournal, EOsFileTruncate), aLength, 0, aDbFile, 0);
  1731 	__OSTIME_COUNTER(TheOsCallTicks[EOsFileTruncate], ::OsCallProfile(dbFile.iIsJournal, EOsFileTruncate), aLength, 0, aDbFile, 0);
  1726 	if(dbFile.iReadOnly)
  1732 	if(dbFile.iReadOnly)
  1727 		{
  1733 		{
  1729   		return SQLITE_IOERR;	
  1735   		return SQLITE_IOERR;	
  1730 		}
  1736 		}
  1731 	__FS_CALL(EFsOpFileSetSize, 0);
  1737 	__FS_CALL(EFsOpFileSetSize, 0);
  1732 	TInt err = dbFile.iFileBuf.SetSize(aLength);
  1738 	TInt err = dbFile.iFileBuf.SetSize(aLength);
  1733 	COsLayerData::Instance().SetOsErrorCode(err);
  1739 	COsLayerData::Instance().SetOsErrorCode(err);
  1734 	return err == KErrNone ? SQLITE_OK : (err == KErrNoMemory ? SQLITE_IOERR_NOMEM : SQLITE_IOERR);
  1740 	TInt sqliteErr = ::Os2SqliteErr(err, SQLITE_IOERR);
       
  1741 	SQLITE_TRACE_OS(OstTraceExt3(TRACE_INTERNALS, TFILEIO_TRUNCATE_EXIT, "OS-Exit;0x%X;TFileIo::Truncate;err=%d;sqliteErr=%d", (TUint)&dbFile, err, sqliteErr));
       
  1742 	return sqliteErr;
  1735 	}
  1743 	}
  1736 
  1744 
  1737 /**
  1745 /**
  1738 SQLite OS porting layer API.
  1746 SQLite OS porting layer API.
  1739 
  1747 
  1752 @see COsLayerData::SetOsErrorCode()
  1760 @see COsLayerData::SetOsErrorCode()
  1753 @see TDbFile
  1761 @see TDbFile
  1754 */
  1762 */
  1755 /* static */int TFileIo::Sync(sqlite3_file* aDbFile, int /* aFlags */)
  1763 /* static */int TFileIo::Sync(sqlite3_file* aDbFile, int /* aFlags */)
  1756 	{
  1764 	{
  1757 	SQLUTRACE_PROFILER(aDbFile);
       
  1758 	TDbFile& dbFile = ::DbFile(aDbFile);
  1765 	TDbFile& dbFile = ::DbFile(aDbFile);
       
  1766 	SQLITE_TRACE_OS(OstTrace1(TRACE_INTERNALS, TFILEIO_SYNC_ENTRY, "OS-Entry;0x%X;TFileIo::Sync", (TUint)&dbFile));
  1759 	__OS_CALL(EOsFileSync, 0, 0);
  1767 	__OS_CALL(EOsFileSync, 0, 0);
  1760     __COUNTER_INCR(TheSqlSrvProfilerFileSync);
  1768     __COUNTER_INCR(TheSqlSrvProfilerFileSync);
  1761 	__OSTIME_COUNTER(TheOsCallTicks[EOsFileSync], ::OsCallProfile(dbFile.iIsJournal, EOsFileSync), 0, 0, aDbFile, 0);
  1769 	__OSTIME_COUNTER(TheOsCallTicks[EOsFileSync], ::OsCallProfile(dbFile.iIsJournal, EOsFileSync), 0, 0, aDbFile, 0);
  1762 	if(dbFile.iReadOnly)
  1770 	if(dbFile.iReadOnly)
  1763 		{
  1771 		{
  1765 		return SQLITE_IOERR;
  1773 		return SQLITE_IOERR;
  1766 		}
  1774 		}
  1767 	__FS_CALL(EFsOpFileSync, 0);
  1775 	__FS_CALL(EFsOpFileSync, 0);
  1768 	TInt err = dbFile.iFileBuf.Flush();
  1776 	TInt err = dbFile.iFileBuf.Flush();
  1769 	COsLayerData::Instance().SetOsErrorCode(err);
  1777 	COsLayerData::Instance().SetOsErrorCode(err);
  1770 	return err == KErrNone ? SQLITE_OK : (err == KErrNoMemory ? SQLITE_IOERR_NOMEM : SQLITE_IOERR);
  1778 	TInt sqliteErr = ::Os2SqliteErr(err, SQLITE_IOERR);
       
  1779 	SQLITE_TRACE_OS(OstTraceExt3(TRACE_INTERNALS, TFILEIO_SYNC_EXIT, "OS-Exit;0x%X;TFileIo::Sync;err=%d;sqliteErr=%d", (TUint)&dbFile, err, sqliteErr));
       
  1780 	return sqliteErr;
  1771 	}
  1781 	}
  1772 
  1782 
  1773 /**
  1783 /**
  1774 SQLite OS porting layer API.
  1784 SQLite OS porting layer API.
  1775 
  1785 
  1788 @see COsLayerData::SetOsErrorCode()
  1798 @see COsLayerData::SetOsErrorCode()
  1789 @see TDbFile
  1799 @see TDbFile
  1790 */
  1800 */
  1791 /* static */ int TFileIo::FileSize(sqlite3_file* aDbFile, sqlite3_int64* aSize)
  1801 /* static */ int TFileIo::FileSize(sqlite3_file* aDbFile, sqlite3_int64* aSize)
  1792 	{
  1802 	{
  1793 	SQLUTRACE_PROFILER(aDbFile);
       
  1794 	TDbFile& dbFile = ::DbFile(aDbFile);
  1803 	TDbFile& dbFile = ::DbFile(aDbFile);
       
  1804 	SQLITE_TRACE_OS(OstTrace1(TRACE_INTERNALS, TFILEIO_FILESIZE_ENTRY, "OS-Entry;0x%X;TFileIo::FileSize", (TUint)&dbFile));
  1795 	__OS_CALL(EOsFileFileSize, 0, 0);
  1805 	__OS_CALL(EOsFileFileSize, 0, 0);
  1796 	__OSTIME_COUNTER(TheOsCallTicks[EOsFileFileSize], ::OsCallProfile(dbFile.iIsJournal, EOsFileFileSize), 0, 0, aDbFile, 0);
  1806 	__OSTIME_COUNTER(TheOsCallTicks[EOsFileFileSize], ::OsCallProfile(dbFile.iIsJournal, EOsFileFileSize), 0, 0, aDbFile, 0);
  1797 	__FS_CALL(EFsOpFileSize, 0);
  1807 	__FS_CALL(EFsOpFileSize, 0);
  1798 	TInt err =  dbFile.iFileBuf.Size(*aSize);
  1808 	TInt err =  dbFile.iFileBuf.Size(*aSize);
  1799 	COsLayerData::Instance().SetOsErrorCode(err);
  1809 	COsLayerData::Instance().SetOsErrorCode(err);
  1800 	if(err == KErrNone)
  1810 	TInt sqliteErr = ::Os2SqliteErr(err, SQLITE_IOERR);
  1801 		{
  1811 	SQLITE_TRACE_OS(OstTraceExt3(TRACE_INTERNALS, TFILEIO_FILESIZE_EXIT, "OS-Exit;0x%X;TFileIo::FileSize;err=%d;sqliteErr=%d", (TUint)&dbFile, err, sqliteErr));
  1802 		return SQLITE_OK;
  1812 	return sqliteErr;
  1803 		}
       
  1804 	return err == KErrNoMemory ? SQLITE_IOERR_NOMEM : SQLITE_IOERR;
       
  1805 	}
  1813 	}
  1806 
  1814 
  1807 /**
  1815 /**
  1808 SQLite OS porting layer API.
  1816 SQLite OS porting layer API.
  1809 
  1817 
  1822 	    
  1830 	    
  1823 @see TDbFile
  1831 @see TDbFile
  1824 */
  1832 */
  1825 /* static */ int TFileIo::Lock(sqlite3_file* aDbFile, int aLockType)
  1833 /* static */ int TFileIo::Lock(sqlite3_file* aDbFile, int aLockType)
  1826 	{
  1834 	{
  1827 	SQLUTRACE_PROFILER(aDbFile);
       
  1828 	TDbFile& dbFile = ::DbFile(aDbFile);
  1835 	TDbFile& dbFile = ::DbFile(aDbFile);
  1829 	__OS_CALL(EOsFileLock, 0, 0);
  1836 	__OS_CALL(EOsFileLock, 0, 0);
  1830 	__OSTIME_COUNTER(TheOsCallTicks[EOsFileLock], ::OsCallProfile(dbFile.iIsJournal, EOsFileLock), aLockType, 0, aDbFile, 0);
  1837 	__OSTIME_COUNTER(TheOsCallTicks[EOsFileLock], ::OsCallProfile(dbFile.iIsJournal, EOsFileLock), aLockType, 0, aDbFile, 0);
  1831 	//If there is already a lock of this type or more restrictive on the database file, do nothing.
  1838 	//If there is already a lock of this type or more restrictive on the database file, do nothing.
  1832 	if(dbFile.iLockType >= aLockType)
  1839 	if(dbFile.iLockType >= aLockType)
  1855 	    
  1862 	    
  1856 @see TDbFile
  1863 @see TDbFile
  1857 */
  1864 */
  1858 /* static */ int TFileIo::Unlock(sqlite3_file* aDbFile, int aLockType)
  1865 /* static */ int TFileIo::Unlock(sqlite3_file* aDbFile, int aLockType)
  1859 	{
  1866 	{
  1860 	SQLUTRACE_PROFILER(aDbFile);
       
  1861 	TDbFile& dbFile = ::DbFile(aDbFile);
  1867 	TDbFile& dbFile = ::DbFile(aDbFile);
  1862 	__OS_CALL(EOsFileUnlock, 0, 0);
  1868 	__OS_CALL(EOsFileUnlock, 0, 0);
  1863 	__OSTIME_COUNTER(TheOsCallTicks[EOsFileUnlock], ::OsCallProfile(dbFile.iIsJournal, EOsFileUnlock), aLockType, 0, aDbFile, 0);
  1869 	__OSTIME_COUNTER(TheOsCallTicks[EOsFileUnlock], ::OsCallProfile(dbFile.iIsJournal, EOsFileUnlock), aLockType, 0, aDbFile, 0);
  1864 	dbFile.iLockType = aLockType;
  1870 	dbFile.iLockType = aLockType;
  1865 	return SQLITE_OK;
  1871 	return SQLITE_OK;
  1884 	    
  1890 	    
  1885 @see TDbFile
  1891 @see TDbFile
  1886 */
  1892 */
  1887 /* static */ int TFileIo::CheckReservedLock(sqlite3_file* aDbFile, int *aResOut)
  1893 /* static */ int TFileIo::CheckReservedLock(sqlite3_file* aDbFile, int *aResOut)
  1888 	{
  1894 	{
  1889 	SQLUTRACE_PROFILER(aDbFile);
       
  1890 	TDbFile& dbFile = ::DbFile(aDbFile);
  1895 	TDbFile& dbFile = ::DbFile(aDbFile);
  1891 	__OS_CALL(EOsFileCheckReservedLock, 0, 0);
  1896 	__OS_CALL(EOsFileCheckReservedLock, 0, 0);
  1892 	__OSTIME_COUNTER(TheOsCallTicks[EOsFileCheckReservedLock], ::OsCallProfile(dbFile.iIsJournal, EOsFileCheckReservedLock), 0, 0, aDbFile, 0);
  1897 	__OSTIME_COUNTER(TheOsCallTicks[EOsFileCheckReservedLock], ::OsCallProfile(dbFile.iIsJournal, EOsFileCheckReservedLock), 0, 0, aDbFile, 0);
  1893 	*aResOut = dbFile.iLockType >= SQLITE_LOCK_RESERVED ? 1 : 0;
  1898 	*aResOut = dbFile.iLockType >= SQLITE_LOCK_RESERVED ? 1 : 0;
  1894   	return SQLITE_OK;
  1899   	return SQLITE_OK;
  1919 	    
  1924 	    
  1920 @see TDbFile
  1925 @see TDbFile
  1921 */
  1926 */
  1922 /* static */ int TFileIo::FileControl(sqlite3_file* aDbFile, int aOp, void* aArg)
  1927 /* static */ int TFileIo::FileControl(sqlite3_file* aDbFile, int aOp, void* aArg)
  1923 	{
  1928 	{
  1924 	SQLUTRACE_PROFILER(aDbFile);
       
  1925 	SYMBIAN_TRACE_SQL_EVENTS_ONLY(UTF::Printf(UTF::TTraceContext(UTF::EInternals), KFileFileCtr, aOp));
       
  1926 	TDbFile& dbFile = ::DbFile(aDbFile);
  1929 	TDbFile& dbFile = ::DbFile(aDbFile);
       
  1930 	SQLITE_TRACE_OS(OstTraceExt2(TRACE_INTERNALS, TFILEIO_FILECONTROL_ENTRY, "OS-Entry;0x%X;TFileIo::FileControl;aOp=%d", (TUint)&dbFile, aOp));
  1927 	__OS_CALL(EOsFileFileControl, 0, 0);
  1931 	__OS_CALL(EOsFileFileControl, 0, 0);
  1928 	__OSTIME_COUNTER(TheOsCallTicks[EOsFileFileControl], ::OsCallProfile(dbFile.iIsJournal, EOsFileFileControl), aOp, 0, aDbFile, 0);
  1932 	__OSTIME_COUNTER(TheOsCallTicks[EOsFileFileControl], ::OsCallProfile(dbFile.iIsJournal, EOsFileFileControl), aOp, 0, aDbFile, 0);
  1929 	TInt err = KErrNone;
  1933 	TInt err = KErrNone;
  1930 	switch(aOp)
  1934 	switch(aOp)
  1931 		{
  1935 		{
  1949 		default:
  1953 		default:
  1950 			err = KErrArgument;
  1954 			err = KErrArgument;
  1951 			break;
  1955 			break;
  1952 		}
  1956 		}
  1953 	COsLayerData::Instance().SetOsErrorCode(err);
  1957 	COsLayerData::Instance().SetOsErrorCode(err);
  1954 	return err == KErrNone ? SQLITE_OK : SQLITE_ERROR;
  1958 	TInt sqliteErr = ::Os2SqliteErr(err, SQLITE_ERROR);
       
  1959 	SQLITE_TRACE_OS(OstTraceExt3(TRACE_INTERNALS, TFILEIO_FILECONTROL_EXIT, "OS-Exit;0x%X;TFileIo::FileControl;err=%d;sqliteErr=%d", (TUint)&dbFile, err, sqliteErr));
       
  1960 	return sqliteErr;
  1955 	}
  1961 	}
  1956 
  1962 
  1957 /**
  1963 /**
  1958 SQLite OS porting layer API.
  1964 SQLite OS porting layer API.
  1959 
  1965 
  1971 @see TDbFile
  1977 @see TDbFile
  1972 @see TVfs::Open()
  1978 @see TVfs::Open()
  1973 */
  1979 */
  1974 /* static */ int TFileIo::SectorSize(sqlite3_file* aDbFile)
  1980 /* static */ int TFileIo::SectorSize(sqlite3_file* aDbFile)
  1975 	{
  1981 	{
  1976 	SQLUTRACE_PROFILER(aDbFile);
       
  1977 	TDbFile& dbFile = ::DbFile(aDbFile);
  1982 	TDbFile& dbFile = ::DbFile(aDbFile);
  1978 	__OS_CALL(EOsFileSectorSize, 0, 0);
  1983 	__OS_CALL(EOsFileSectorSize, 0, 0);
  1979 	__OSTIME_COUNTER(TheOsCallTicks[EOsFileSectorSize], ::OsCallProfile(dbFile.iIsJournal, EOsFileSectorSize), 0, 0, aDbFile, 0);
  1984 	__OSTIME_COUNTER(TheOsCallTicks[EOsFileSectorSize], ::OsCallProfile(dbFile.iIsJournal, EOsFileSectorSize), 0, 0, aDbFile, 0);
  1980 	__ASSERT_DEBUG(dbFile.iSectorSize > 0, User::Panic(KPanicCategory, EPanicInternalError));
  1985 	__ASSERT_DEBUG(dbFile.iSectorSize > 0, __SQLITEPANIC2(ESqliteOsPanicInternalError));
  1981 	if(dbFile.iSectorSize > 0)
  1986 	if(dbFile.iSectorSize > 0)
  1982 		{
  1987 		{
  1983 		return dbFile.iSectorSize;	
  1988 		return dbFile.iSectorSize;	
  1984 		}
  1989 		}
  1985 	return SQLITE_DEFAULT_SECTOR_SIZE;
  1990 	return SQLITE_DEFAULT_SECTOR_SIZE;
  2002 @see TDbFile
  2007 @see TDbFile
  2003 @see TVfs::Open()
  2008 @see TVfs::Open()
  2004 */
  2009 */
  2005 /* static */ int TFileIo::DeviceCharacteristics(sqlite3_file* aDbFile)
  2010 /* static */ int TFileIo::DeviceCharacteristics(sqlite3_file* aDbFile)
  2006 	{
  2011 	{
  2007 	SQLUTRACE_PROFILER(aDbFile);
       
  2008 	TDbFile& dbFile = ::DbFile(aDbFile);
  2012 	TDbFile& dbFile = ::DbFile(aDbFile);
  2009 	__OS_CALL(EOsFileDeviceCharacteristics, 0, 0);
  2013 	__OS_CALL(EOsFileDeviceCharacteristics, 0, 0);
  2010 	__OSTIME_COUNTER(TheOsCallTicks[EOsFileDeviceCharacteristics], ::OsCallProfile(dbFile.iIsJournal, EOsFileDeviceCharacteristics), 0, 0, aDbFile, 0);
  2014 	__OSTIME_COUNTER(TheOsCallTicks[EOsFileDeviceCharacteristics], ::OsCallProfile(dbFile.iIsJournal, EOsFileDeviceCharacteristics), 0, 0, aDbFile, 0);
  2011 	__ASSERT_DEBUG(dbFile.iDeviceCharacteristics >= 0, User::Panic(KPanicCategory, EPanicInternalError));
  2015 	__ASSERT_DEBUG(dbFile.iDeviceCharacteristics >= 0, __SQLITEPANIC2(ESqliteOsPanicInternalError));
  2012 	if(dbFile.iDeviceCharacteristics >= 0)
  2016 	if(dbFile.iDeviceCharacteristics >= 0)
  2013 		{
  2017 		{
  2014 		return dbFile.iDeviceCharacteristics;	
  2018 		return dbFile.iDeviceCharacteristics;	
  2015 		}
  2019 		}
  2016 	return 0;
  2020 	return 0;
  2063 @see TVfs::Open()
  2067 @see TVfs::Open()
  2064 */
  2068 */
  2065 /* static */ inline TInt TVfs::DoGetVolumeIoParamInfo(RFs& aFs, TInt aDriveNo, TVolumeIOParamInfo& aVolumeInfo)
  2069 /* static */ inline TInt TVfs::DoGetVolumeIoParamInfo(RFs& aFs, TInt aDriveNo, TVolumeIOParamInfo& aVolumeInfo)
  2066 	{
  2070 	{
  2067 	__FS_CALL(EFsOpFsVolumeIoParam, 0);
  2071 	__FS_CALL(EFsOpFsVolumeIoParam, 0);
  2068 	return aFs.VolumeIOParam(aDriveNo, aVolumeInfo);
  2072 	TInt err = aFs.VolumeIOParam(aDriveNo, aVolumeInfo); 
       
  2073 	SQLITE_TRACE_OS(OstTraceExt2(TRACE_INTERNALS, TVFS_DOGETVOLUMEIOPARAMINFO, "OS;0;TVfs::DoGetVolumeIoParamInfo;aDriveNo=%d;err=%d", aDriveNo, err));
       
  2074 	return err;		
  2069 	}
  2075 	}
  2070 
  2076 
  2071 /**
  2077 /**
  2072 Retrieves and returns in a bit set the device characteristics.
  2078 Retrieves and returns in a bit set the device characteristics.
  2073 
  2079 
  2151 		if(aVolumeInfo.iBlockSize > SQLITE_DEFAULT_SECTOR_SIZE && (aVolumeInfo.iBlockSize & (aVolumeInfo.iBlockSize - 1)) == 0)
  2157 		if(aVolumeInfo.iBlockSize > SQLITE_DEFAULT_SECTOR_SIZE && (aVolumeInfo.iBlockSize & (aVolumeInfo.iBlockSize - 1)) == 0)
  2152 			{
  2158 			{
  2153 			sectorSize = aVolumeInfo.iBlockSize;
  2159 			sectorSize = aVolumeInfo.iBlockSize;
  2154 			}
  2160 			}
  2155 		}
  2161 		}
  2156 	__ASSERT_DEBUG(sectorSize > 0 && (sectorSize & (sectorSize - 1)) == 0, User::Panic(KPanicCategory, EPanicInternalError));
  2162 	__ASSERT_DEBUG(sectorSize > 0 && (sectorSize & (sectorSize - 1)) == 0, __SQLITEPANIC2(ESqliteOsPanicInternalError));
  2157 	return sectorSize;
  2163 	return sectorSize;
  2158 	}
  2164 	}
  2159 
  2165 
  2160 /**
  2166 /**
  2161 Retrieves in a bit set the device characteristics of the device of the file referred by the aDbFile parameter.
  2167 Retrieves in a bit set the device characteristics of the device of the file referred by the aDbFile parameter.
  2180 @see TFileIo::DeviceCharacteristics()
  2186 @see TFileIo::DeviceCharacteristics()
  2181 @see TFileIo::SectorSize()
  2187 @see TFileIo::SectorSize()
  2182 */
  2188 */
  2183 /* static */ TInt TVfs::DoGetDeviceCharacteristicsAndSectorSize(TDbFile& aDbFile, TInt& aRecReadBufSize)
  2189 /* static */ TInt TVfs::DoGetDeviceCharacteristicsAndSectorSize(TDbFile& aDbFile, TInt& aRecReadBufSize)
  2184 	{
  2190 	{
  2185 	__ASSERT_DEBUG(aDbFile.iDeviceCharacteristics < 0, User::Panic(KPanicCategory, EPanicInternalError));
  2191 	__ASSERT_DEBUG(aDbFile.iDeviceCharacteristics < 0, __SQLITEPANIC2(ESqliteOsPanicInternalError));
  2186 	__ASSERT_DEBUG(aDbFile.iSectorSize <= 0, User::Panic(KPanicCategory, EPanicInternalError));
  2192 	__ASSERT_DEBUG(aDbFile.iSectorSize <= 0, __SQLITEPANIC2(ESqliteOsPanicInternalError));
  2187 	TInt driveNo;
  2193 	TInt driveNo;
  2188 	TDriveInfo driveInfo;
  2194 	TDriveInfo driveInfo;
  2189 	__FS_CALL(EFsOpFileDrive, 0);
  2195 	__FS_CALL(EFsOpFileDrive, 0);
  2190 	TInt err = aDbFile.iFileBuf.Drive(driveNo, driveInfo);
  2196 	TInt err = aDbFile.iFileBuf.Drive(driveNo, driveInfo);
  2191 	if(err != KErrNone)
  2197 	if(err != KErrNone)
  2199 		return err;	
  2205 		return err;	
  2200 		}
  2206 		}
  2201 	aDbFile.iDeviceCharacteristics = TVfs::DoGetDeviceCharacteristics(driveInfo, volumeInfo);
  2207 	aDbFile.iDeviceCharacteristics = TVfs::DoGetDeviceCharacteristics(driveInfo, volumeInfo);
  2202 	aDbFile.iSectorSize = TVfs::DoGetSectorSize(driveInfo, volumeInfo);
  2208 	aDbFile.iSectorSize = TVfs::DoGetSectorSize(driveInfo, volumeInfo);
  2203 	aRecReadBufSize = volumeInfo.iRecReadBufSize;
  2209 	aRecReadBufSize = volumeInfo.iRecReadBufSize;
       
  2210 	SQLITE_TRACE_OS(OstTraceExt5(TRACE_INTERNALS, TVFS_DOGETGETDEVICECHARACTERISTICSANDSECTORSIZE, "OS;0x%X;TVfs::DoGetDeviceCharacteristicsAndSectorSize;driveNo=%d;sectorSize=%d;devCharact=0x%X;readBufSize=%d", (TUint)&aDbFile, driveNo, aDbFile.iSectorSize, (TUint)aDbFile.iDeviceCharacteristics, volumeInfo.iRecReadBufSize));
  2204 	return KErrNone;
  2211 	return KErrNone;
  2205 	}
  2212 	}
  2206 
  2213 
  2207 /**
  2214 /**
  2208 SQLite OS porting layer API.
  2215 SQLite OS porting layer API.
  2254     if (err == KErrCorrupt || err == KErrEof || !IsMinFileSize)
  2261     if (err == KErrCorrupt || err == KErrEof || !IsMinFileSize)
  2255         {
  2262         {
  2256         COsLayerData& osLayerData = COsLayerData::Instance();
  2263         COsLayerData& osLayerData = COsLayerData::Instance();
  2257     
  2264     
  2258         aDbFile.iFileBuf.Close();
  2265         aDbFile.iFileBuf.Close();
  2259         (void) osLayerData.iFs.Delete(aFname);
  2266         __SQLITETRACE_OSEXPR(TInt err2 =) osLayerData.iFs.Delete(aFname);
       
  2267 		SQLITE_TRACE_OS(OstTraceExt4(TRACE_INTERNALS, TVFS_DOFILESIZECORRUPTIONCHECK1, "OS;0x%X;TVfs::DoFileSizeCorruptionCheck;size=%lld;err=%d;deleteErr=%d", (TUint)&aDbFile, size, err, err2));
  2260         err = aDbFile.iFileBuf.Create(osLayerData.iFs, aFname, aFmode);
  2268         err = aDbFile.iFileBuf.Create(osLayerData.iFs, aFname, aFmode);
       
  2269 		SQLITE_TRACE_OS(OstTraceExt2(TRACE_INTERNALS, TVFS_DOFILESIZECORRUPTIONCHECK2, "OS;0x%X;TVfs::DoFileSizeCorruptionCheck;createErr=%d", (TUint)&aDbFile, err));
  2261         }
  2270         }
  2262     return err;
  2271     return err;
  2263     }
  2272     }
  2264 
  2273 
  2265 /**
  2274 /**
  2293 
  2302 
  2294 @see COsLayerData::SetOsErrorCode()
  2303 @see COsLayerData::SetOsErrorCode()
  2295 @see TDbFile
  2304 @see TDbFile
  2296 */
  2305 */
  2297 /* static */ int TVfs::Open(sqlite3_vfs* aVfs, const char* aFileName, sqlite3_file* aDbFile, int aFlags, int* aOutFlags)
  2306 /* static */ int TVfs::Open(sqlite3_vfs* aVfs, const char* aFileName, sqlite3_file* aDbFile, int aFlags, int* aOutFlags)
  2298 	{   
  2307 	{
  2299     SQLUTRACE_PROFILER(aVfs);
  2308 	__OS_CALL(EOsVfsOpen, 0, 0);
  2300     __OS_CALL(EOsVfsOpen, 0, 0);
  2309 	__OSTIME_COUNTER(TheOsCallTicks[EOsVfsOpen], ::OsCallProfile(EFalse, EOsVfsOpen), 0, 0, aDbFile, aFileName);
  2301     __OSTIME_COUNTER(TheOsCallTicks[EOsVfsOpen], ::OsCallProfile(EFalse, EOsVfsOpen), 0, 0, aDbFile, aFileName);
  2310 	COsLayerData& osLayerData = COsLayerData::Instance();
  2302  
  2311 	new (aDbFile) TDbFile;
  2303     COsLayerData& osLayerData = COsLayerData::Instance();
  2312 	TDbFile& dbFile = ::DbFile(aDbFile);
  2304     TFhStrType fhStrType = ENotFhStr;
       
  2305     new (aDbFile) TDbFile;
       
  2306     TDbFile& dbFile = ::DbFile(aDbFile);
       
  2307 
       
  2308     if(!aFileName)
  2313     if(!aFileName)
  2309         {  
  2314         {  
  2310         //It is to create and open a temp file if aFileName is NULL. In this case,
  2315         //It is to create and open a temp file if aFileName is NULL. In this case,
  2311         //we will defer the file creation util it is needed.  
  2316         //we will defer the file creation util it is needed.  
  2312     
       
  2313         dbFile.pMethods = &TheFileIoApi;
  2317         dbFile.pMethods = &TheFileIoApi;
  2314         dbFile.iIsFileCreated = EFalse;
  2318         dbFile.iIsFileCreated = EFalse;
  2315         if(aOutFlags)
  2319         if(aOutFlags)
  2316             {
  2320             {
  2317             *aOutFlags = SQLITE_OPEN_READWRITE;
  2321             *aOutFlags = SQLITE_OPEN_READWRITE;
  2318             }
  2322             }
       
  2323 		osLayerData.SetOsErrorCode(KErrNone);
  2319         return SQLITE_OK;
  2324         return SQLITE_OK;
  2320         }  
  2325         }  
  2321     
  2326 	TFileName fname;
  2322     TFileName fname;
       
  2323     if(!::ConvertToUnicode(aFileName, fname))
  2327     if(!::ConvertToUnicode(aFileName, fname))
  2324         {
  2328 		{
  2325         osLayerData.SetOsErrorCode(KErrBadName);
  2329         osLayerData.SetOsErrorCode(KErrBadName);
  2326         return SQLITE_CANTOPEN;	
  2330         return SQLITE_CANTOPEN;	
  2327         }
  2331     	}
  2328     SYMBIAN_TRACE_SQL_EVENTS_ONLY(UTF::Printf(UTF::TTraceContext(UTF::EInternals), KFileOpen, aDbFile, &fname));
  2332 	SQLITE_TRACE_OS(OstTraceExt3(TRACE_INTERNALS, TVFS_OPEN_ENTRY, "OS-Entry;0x%X;TVfs::Open;fname=%S;aFlags=0x%X", (TUint)&aDbFile, __SQLITEPRNSTR(fname), (TUint)aFlags));
  2329     fhStrType = ::FhStringProps(aFileName);
  2333 	TFhStrType fhStrType = aFileName ? ::FhStringProps(aFileName) : ENotFhStr;
  2330     TInt err = KErrNone;
  2334 	TInt err = KErrNone;
  2331     TInt recReadBufSize = -1;
  2335 	if(fhStrType == EFhMainDbStr)
  2332     if(fhStrType == EFhMainDbStr)
  2336 		{//Main db file, open from handle
  2333         {//Main db file, open from handle
  2337 		const RMessage2* msg;
  2334         const RMessage2* msg;
  2338 		TBool readOnly;
  2335         TBool readOnly;
  2339 		osLayerData.RetrieveAndResetFhData(msg, readOnly);
  2336         osLayerData.RetrieveAndResetFhData(msg, readOnly);
  2340 		err = msg != NULL ? TVfs::DoOpenFromHandle(dbFile, *msg, readOnly) : KErrGeneral;
  2337         err = msg != NULL ? TVfs::DoOpenFromHandle(dbFile, *msg, readOnly) : KErrGeneral;
  2341 		}
  2338         }
  2342 	else
  2339     else
  2343 		{
  2340         {
  2344 		if(fhStrType == EFhStr)
  2341         if(fhStrType == EFhStr)
  2345 			{//Not the main db file. Remove invalid characters in the file name
  2342             {//Not the main db file. Remove invalid characters in the file name
  2346 			::FhConvertToFileName(fname, osLayerData.iSysPrivDir);//If fname does not have a path, iSysPrivDir will be used
  2343             ::FhConvertToFileName(fname, osLayerData.iSysPrivDir);//If fname does not have a path, iSysPrivDir will be used
  2347 			}
  2344             }
  2348 		TInt fmode = EFileRead;
  2345         TInt fmode = EFileRead;
  2349 		if(aFlags & SQLITE_OPEN_READWRITE)
  2346         if(aFlags & SQLITE_OPEN_READWRITE)
  2350 			{
  2347             {
  2351 			fmode |= EFileWrite;
  2348             fmode |= EFileWrite;
  2352 			}
  2349             }
  2353 		if(aFlags & SQLITE_OPEN_EXCLUSIVE)
  2350         if(aFlags & SQLITE_OPEN_EXCLUSIVE)
  2354 			{
  2351             {
  2355 			fmode |= EFileShareExclusive;
  2352             fmode |= EFileShareExclusive;
  2356 			}
  2353             }
  2357 		if(aFlags & SQLITE_OPEN_DELETEONCLOSE)
  2354             err = KErrAccessDenied;
  2358 			{
  2355             TInt prevErr = KErrNone;
  2359 			fmode |= EDeleteOnClose;
  2356             if(aFlags & SQLITE_OPEN_DELETEONCLOSE)
  2360 			}
  2357                 {
  2361 		err = KErrAccessDenied;
  2358                 fmode |= EDeleteOnClose;
  2362 		TInt prevErr = KErrNone;
  2359                 }
  2363 		if(aFlags & SQLITE_OPEN_CREATE)
  2360             if(aFlags & SQLITE_OPEN_CREATE)
  2364 			{
  2361                 {
  2365 			__FS_CALL(EFsOpFileCreate, 0);
  2362                 __FS_CALL(EFsOpFileCreate, 0);
  2366 			prevErr = err = dbFile.iFileBuf.Create(osLayerData.iFs, fname, fmode);
  2363                 prevErr = err = dbFile.iFileBuf.Create(osLayerData.iFs, fname, fmode);
  2367 			}
  2364                 }
  2368 		if(err != KErrNone && err != KErrNoMemory && err != KErrDiskFull)
  2365             if(err != KErrNone && err != KErrNoMemory && err != KErrDiskFull)
  2369 			{
  2366                 {
  2370 			__FS_CALL(EFsOpFileOpen, 0);
  2367                 __FS_CALL(EFsOpFileOpen, 0);
  2371 			err = dbFile.iFileBuf.Open(osLayerData.iFs, fname, fmode);
  2368                 err = dbFile.iFileBuf.Open(osLayerData.iFs, fname, fmode);
  2372 
  2369                 
  2373 			if(err == KErrNone && (aFlags & KJournalFileTypeBitMask))
  2370                 if(err == KErrNone && (aFlags & KJournalFileTypeBitMask))
  2374 				{
  2371                     {
  2375 				err = TVfs::DoFileSizeCorruptionCheck(dbFile, fname, fmode);
  2372                     err = TVfs::DoFileSizeCorruptionCheck(dbFile, fname, fmode);
  2376 				}
  2373                     }
  2377 			}
  2374                 }
  2378 		if((err != KErrNone && err != KErrNoMemory && err != KErrDiskFull) && (aFlags & SQLITE_OPEN_READWRITE))
  2375             if((err != KErrNone && err != KErrNoMemory && err != KErrDiskFull) && (aFlags & SQLITE_OPEN_READWRITE))
  2379 			{
  2376                 {
  2380 			aFlags &= ~SQLITE_OPEN_READWRITE;
  2377                 aFlags &= ~SQLITE_OPEN_READWRITE;
  2381 			aFlags |= SQLITE_OPEN_READONLY;
  2378                 aFlags |= SQLITE_OPEN_READONLY;
  2382 			fmode &= ~EFileWrite;
  2379                 fmode &= ~EFileWrite;
  2383 			__FS_CALL(EFsOpFileOpen, 0);
  2380                 __FS_CALL(EFsOpFileOpen, 0);
  2384 			err = dbFile.iFileBuf.Open(osLayerData.iFs, fname, fmode);
  2381                 err = dbFile.iFileBuf.Open(osLayerData.iFs, fname, fmode);
  2385 			}
  2382                 }
  2386 		if(err != KErrNone && prevErr == KErrAccessDenied)
  2383             if(err != KErrNone && prevErr == KErrAccessDenied)
  2387 			{
  2384                 {
  2388 			err = KErrAccessDenied;
  2385                 err = KErrAccessDenied;
  2389 			}
  2386                 }
  2390 		}
  2387         }
  2391 	TInt recReadBufSize = -1;
  2388     if(err == KErrNone)
  2392 	if(err == KErrNone)
  2389         {
  2393 		{
  2390         err = TVfs::DoGetDeviceCharacteristicsAndSectorSize(dbFile, recReadBufSize);
  2394 		err = TVfs::DoGetDeviceCharacteristicsAndSectorSize(dbFile, recReadBufSize);
  2391         }	
  2395 		}
  2392  	
       
  2393 	osLayerData.SetOsErrorCode(err);
  2396 	osLayerData.SetOsErrorCode(err);
  2394 	if(err != KErrNone)
  2397 	if(err != KErrNone)
  2395 		{
  2398 		{
  2396 		__FS_CALL(EFsOpFileClose, 0);
  2399 		__FS_CALL(EFsOpFileClose, 0);
  2397 		dbFile.iFileBuf.Close();	
  2400 		dbFile.iFileBuf.Close();	
  2410 		(void)dbFile.iFileBuf.SetReadAheadSize(dbFile.iSectorSize, recReadBufSize);
  2413 		(void)dbFile.iFileBuf.SetReadAheadSize(dbFile.iSectorSize, recReadBufSize);
  2411 		}
  2414 		}
  2412 #ifdef _SQLPROFILER
  2415 #ifdef _SQLPROFILER
  2413 	dbFile.iIsJournal = aFlags & KJournalFileTypeBitMask; 
  2416 	dbFile.iIsJournal = aFlags & KJournalFileTypeBitMask; 
  2414 #endif
  2417 #endif
  2415 	return err == KErrNone ? SQLITE_OK : (err == KErrNoMemory ? SQLITE_IOERR_NOMEM : SQLITE_CANTOPEN);
  2418 	TInt sqliteErr = ::Os2SqliteErr(err, SQLITE_CANTOPEN);
       
  2419 	SQLITE_TRACE_OS(OstTraceExt4(TRACE_INTERNALS, TVFS_OPEN_EXIT, "OS-Exit;0x%X;TVfs::Open;outFlags=0x%X;err=%d;sqliteErr=%d", (TUint)&aDbFile, aOutFlags ? (TUint)*aOutFlags : 0, err, sqliteErr));
       
  2420 	return sqliteErr;
  2416 	}
  2421 	}
  2417 
  2422 
  2418 /**
  2423 /**
  2419 SQLite OS porting layer API.
  2424 SQLite OS porting layer API.
  2420 
  2425 
  2433 
  2438 
  2434 @see COsLayerData::SetOsErrorCode()
  2439 @see COsLayerData::SetOsErrorCode()
  2435 */
  2440 */
  2436 /* static */ int TVfs::Delete(sqlite3_vfs* aVfs, const char* aFileName, int /*aSyncDir*/)
  2441 /* static */ int TVfs::Delete(sqlite3_vfs* aVfs, const char* aFileName, int /*aSyncDir*/)
  2437 	{
  2442 	{
  2438 	SQLUTRACE_PROFILER(aVfs);
       
  2439 	__OS_CALL(EOsVfsDelete, 0, 0);
  2443 	__OS_CALL(EOsVfsDelete, 0, 0);
  2440 	__OSTIME_COUNTER(TheOsCallTicks[EOsVfsDelete], ::OsCallProfile(EFalse, EOsVfsDelete), 0, 0, 0, aFileName);
  2444 	__OSTIME_COUNTER(TheOsCallTicks[EOsVfsDelete], ::OsCallProfile(EFalse, EOsVfsDelete), 0, 0, 0, aFileName);
  2441 	COsLayerData& osLayerData = COsLayerData::Instance();
  2445 	COsLayerData& osLayerData = COsLayerData::Instance();
  2442 	TBuf<KMaxFileName + 1> fname;
  2446 	TBuf<KMaxFileName + 1> fname;
  2443 	if(!::ConvertToUnicode(aFileName, fname))
  2447 	if(!::ConvertToUnicode(aFileName, fname))
  2444 		{
  2448 		{
  2445 		osLayerData.SetOsErrorCode(KErrBadName);
  2449 		osLayerData.SetOsErrorCode(KErrBadName);
       
  2450 		SQLITE_TRACE_OS(OstTrace0(TRACE_INTERNALS, TVFS_DELETE1, "OS;0;TVfs::Delete;err=KErrBadName"));
  2446 		return SQLITE_ERROR;	
  2451 		return SQLITE_ERROR;	
  2447 		}
  2452 		}
  2448 	SYMBIAN_TRACE_SQL_EVENTS_ONLY(UTF::Printf(UTF::TTraceContext(UTF::EInternals), KFileName, &fname));
  2453 	SQLITE_TRACE_OS(OstTraceExt1(TRACE_INTERNALS, TVFS_DELETE_ENTRY, "OS-Entry;0;TVfs::Delete;fname=%S", __SQLITEPRNSTR(fname)));
  2449 	TFhStrType fhStrType = FhStringProps(aFileName);
  2454 	TFhStrType fhStrType = FhStringProps(aFileName);
  2450 	if(fhStrType == EFhMainDbStr)
  2455 	if(fhStrType == EFhMainDbStr)
  2451 		{//Deleting files not in your own private data cage - not allowed!
  2456 		{//Deleting files not in your own private data cage - not allowed!
  2452 		osLayerData.SetOsErrorCode(KErrPermissionDenied);
  2457 		osLayerData.SetOsErrorCode(KErrPermissionDenied);
       
  2458 		SQLITE_TRACE_OS(OstTraceExt1(TRACE_INTERNALS, TVFS_DELETE2, "OS;0;TVfs::Delete;err=KErrPermissionDenied;fname=%S", __SQLITEPRNSTR(fname)));
  2453 		return SQLITE_ERROR;	
  2459 		return SQLITE_ERROR;	
  2454 		}
  2460 		}
  2455 	if(fhStrType == EFhStr)
  2461 	if(fhStrType == EFhStr)
  2456 		{
  2462 		{
  2457 		::FhConvertToFileName(fname, osLayerData.iSysPrivDir);//If fname does not have a path, iSysPrivDir will be used
  2463 		::FhConvertToFileName(fname, osLayerData.iSysPrivDir);//If fname does not have a path, iSysPrivDir will be used
  2458 		}
  2464 		}
  2459 	__FS_CALL(EFsOpFileDelete, 0);
  2465 	__FS_CALL(EFsOpFileDelete, 0);
  2460 	TInt err = osLayerData.iFs.Delete(fname);
  2466 	TInt err = osLayerData.iFs.Delete(fname);
  2461 	osLayerData.SetOsErrorCode(err);
  2467 	osLayerData.SetOsErrorCode(err);
  2462 	return err == KErrNone ? SQLITE_OK : (err == KErrNoMemory ? SQLITE_IOERR_NOMEM : SQLITE_IOERR_DELETE);
  2468 	TInt sqliteErr = ::Os2SqliteErr(err, SQLITE_IOERR_DELETE);	
       
  2469 	SQLITE_TRACE_OS(OstTraceExt2(TRACE_INTERNALS, TVFS_DELETE_EXIT, "OS-Exit;0;TVfs::Delete;err=%d;sqliteErr=%d", err, sqliteErr));
       
  2470 	return sqliteErr;
  2463 	}
  2471 	}
  2464 
  2472 
  2465 /**
  2473 /**
  2466 SQLite OS porting layer API.
  2474 SQLite OS porting layer API.
  2467 
  2475 
  2481 
  2489 
  2482 @see COsLayerData::SetOsErrorCode()
  2490 @see COsLayerData::SetOsErrorCode()
  2483 */
  2491 */
  2484 /* static */ int TVfs::Access(sqlite3_vfs* aVfs, const char* aFileName, int aFlags, int* aResOut)
  2492 /* static */ int TVfs::Access(sqlite3_vfs* aVfs, const char* aFileName, int aFlags, int* aResOut)
  2485 	{
  2493 	{
  2486 	SQLUTRACE_PROFILER(aVfs);
       
  2487 	__OS_CALL(EOsVfsAccess, 0, 0);
  2494 	__OS_CALL(EOsVfsAccess, 0, 0);
  2488 	__OSTIME_COUNTER(TheOsCallTicks[EOsVfsAccess], ::OsCallProfile(EFalse, EOsVfsAccess), aFlags, 0, 0, aFileName);
  2495 	__OSTIME_COUNTER(TheOsCallTicks[EOsVfsAccess], ::OsCallProfile(EFalse, EOsVfsAccess), aFlags, 0, 0, aFileName);
  2489 	COsLayerData& osLayerData = COsLayerData::Instance();
  2496 	COsLayerData& osLayerData = COsLayerData::Instance();
  2490 	TBuf<KMaxFileName + 1> fname;
  2497 	TBuf<KMaxFileName + 1> fname;
  2491 	if(!::ConvertToUnicode(aFileName, fname))
  2498 	if(!::ConvertToUnicode(aFileName, fname))
  2492 		{
  2499 		{
  2493 		osLayerData.SetOsErrorCode(KErrGeneral);
  2500 		osLayerData.SetOsErrorCode(KErrGeneral);
       
  2501 		SQLITE_TRACE_OS(OstTrace0(TRACE_INTERNALS, TVFS_ACCESS1, "OS;0;TVfs::Access;err=KErrGeneral"));
  2494 		return SQLITE_IOERR_ACCESS;
  2502 		return SQLITE_IOERR_ACCESS;
  2495 		}
  2503 		}
  2496 	SYMBIAN_TRACE_SQL_EVENTS_ONLY(UTF::Printf(UTF::TTraceContext(UTF::EInternals), KFileName, &fname));
  2504 	SQLITE_TRACE_OS(OstTraceExt2(TRACE_INTERNALS, TVFS_ACCESS_ENTRY, "OS-Entry;0;TVfs::Access;fname=%S;aFlags=0x%X", __SQLITEPRNSTR(fname), (TUint)aFlags));
  2497 	TFhStrType fhStrType = ::FhStringProps(aFileName);
  2505 	TFhStrType fhStrType = ::FhStringProps(aFileName);
  2498 	if(fhStrType == EFhStr)
  2506 	if(fhStrType == EFhStr)
  2499 		{
  2507 		{
  2500 		::FhConvertToFileName(fname, osLayerData.iSysPrivDir);//If fname does not have a path, iSysPrivDir will be used
  2508 		::FhConvertToFileName(fname, osLayerData.iSysPrivDir);//If fname does not have a path, iSysPrivDir will be used
  2501 		}
  2509 		}
  2504 	TInt err = osLayerData.iFs.Entry(fname, entry);
  2512 	TInt err = osLayerData.iFs.Entry(fname, entry);
  2505 	if(aFlags == SQLITE_ACCESS_EXISTS && err == KErrNotFound)
  2513 	if(aFlags == SQLITE_ACCESS_EXISTS && err == KErrNotFound)
  2506 		{
  2514 		{
  2507 		osLayerData.SetOsErrorCode(KErrNone);
  2515 		osLayerData.SetOsErrorCode(KErrNone);
  2508 		*aResOut = 0;
  2516 		*aResOut = 0;
       
  2517 		SQLITE_TRACE_OS(OstTrace0(TRACE_INTERNALS, TVFS_ACCESS_EXIT1, "OS-Exit;0;TVfs::Access;Exists-NoFound"));
  2509 		return SQLITE_OK;
  2518 		return SQLITE_OK;
  2510 		}
  2519 		}
  2511 	if(err != KErrNone)
  2520 	if(err != KErrNone)
  2512 		{
  2521 		{
  2513 		osLayerData.SetOsErrorCode(err);
  2522 		osLayerData.SetOsErrorCode(err);
       
  2523 		SQLITE_TRACE_OS(OstTrace1(TRACE_INTERNALS, TVFS_ACCESS_EXIT2, "OS-Exit;0;TVfs::Access;err=%d", err));
  2514 		return err == KErrNoMemory ? SQLITE_IOERR_NOMEM : SQLITE_IOERR_ACCESS;
  2524 		return err == KErrNoMemory ? SQLITE_IOERR_NOMEM : SQLITE_IOERR_ACCESS;
  2515 		}
  2525 		}
  2516 	*aResOut = 0;
  2526 	*aResOut = 0;
  2517 	switch(aFlags)
  2527 	switch(aFlags)
  2518 		{
  2528 		{
  2527 			break;
  2537 			break;
  2528 		default:
  2538 		default:
  2529 			break;			
  2539 			break;			
  2530 		}
  2540 		}
  2531 	osLayerData.SetOsErrorCode(KErrNone);
  2541 	osLayerData.SetOsErrorCode(KErrNone);
       
  2542 	SQLITE_TRACE_OS(OstTrace1(TRACE_INTERNALS, TVFS_ACCESS_EXIT3, "OS-Exit;0;TVfs::Access;aResOut=%d", *aResOut));
  2532 	return SQLITE_OK;
  2543 	return SQLITE_OK;
  2533 	}
  2544 	}
  2534 
  2545 
  2535 /**
  2546 /**
  2536 SQLite OS porting layer API.
  2547 SQLite OS porting layer API.
  2558 @return SQLITE_ERROR, The aRelative parameter is NULL or cannot be converted to UTF16;
  2569 @return SQLITE_ERROR, The aRelative parameter is NULL or cannot be converted to UTF16;
  2559 		SQLITE_OK The operation has completed successfully.
  2570 		SQLITE_OK The operation has completed successfully.
  2560 */
  2571 */
  2561 /* static */ int TVfs::FullPathName(sqlite3_vfs* aVfs, const char* aRelative, int aBufLen, char* aBuf)
  2572 /* static */ int TVfs::FullPathName(sqlite3_vfs* aVfs, const char* aRelative, int aBufLen, char* aBuf)
  2562 	{
  2573 	{
  2563 	SQLUTRACE_PROFILER(aVfs);
       
  2564 	__OS_CALL(EOsVfsFullPathName, 0, 0);
  2574 	__OS_CALL(EOsVfsFullPathName, 0, 0);
  2565 	__OSTIME_COUNTER(TheOsCallTicks[EOsVfsFullPathName], ::OsCallProfile(EFalse, EOsVfsFullPathName), aBufLen, 0, 0, aRelative);
  2575 	__OSTIME_COUNTER(TheOsCallTicks[EOsVfsFullPathName], ::OsCallProfile(EFalse, EOsVfsFullPathName), aBufLen, 0, 0, aRelative);
  2566 	COsLayerData& osLayerData = COsLayerData::Instance();
  2576 	COsLayerData& osLayerData = COsLayerData::Instance();
  2567 	osLayerData.StoreFhData(NULL, EFalse);
  2577 	osLayerData.StoreFhData(NULL, EFalse);
  2568 	//Convert the received file name to UTF16
  2578 	//Convert the received file name to UTF16
  2569 	TBuf<KMaxFileName + 1> fname;
  2579 	TBuf<KMaxFileName + 1> fname;
  2570 	if(!::ConvertToUnicode(aRelative, fname))
  2580 	if(!::ConvertToUnicode(aRelative, fname))
  2571 		{
  2581 		{
       
  2582 		SQLITE_TRACE_OS(OstTrace0(TRACE_INTERNALS, TVFS_FULLPATHNAME1, "OS;0;TVfs::FullPathName;err=SQLITE_ERROR"));
  2572 		return SQLITE_ERROR;
  2583 		return SQLITE_ERROR;
  2573 		}
  2584 		}
  2574 	SYMBIAN_TRACE_SQL_EVENTS_ONLY(UTF::Printf(UTF::TTraceContext(UTF::EInternals), KFileName, &fname));
  2585 	SQLITE_TRACE_OS(OstTraceExt2(TRACE_INTERNALS, TVFS_FULLPATHNAME_ENTRY, "OS-Entry;0;TVfs::FullPathName;fname=%S;aBufLen=%d", __SQLITEPRNSTR(fname), aBufLen));
  2575 	//Zero-terminate the converted file name
  2586 	//Zero-terminate the converted file name
  2576 	fname.Append(TChar(0));
  2587 	fname.Append(TChar(0));
  2577 	TParse parse;
  2588 	TParse parse;
  2578 	TFhStrType strType = ::FhStringProps(aRelative);//Detect string type - it may not be a real file name
  2589 	TFhStrType strType = ::FhStringProps(aRelative);//Detect string type - it may not be a real file name
  2579 	if(strType == EFhMainDbStr)
  2590 	if(strType == EFhMainDbStr)
  2588 		}
  2599 		}
  2589 	TPtr8 dest8(reinterpret_cast <TUint8*> (aBuf), aBufLen);	
  2600 	TPtr8 dest8(reinterpret_cast <TUint8*> (aBuf), aBufLen);	
  2590 	if(!::ConvertFromUnicode(parse.FullName(), dest8))
  2601 	if(!::ConvertFromUnicode(parse.FullName(), dest8))
  2591 		{//Zero the stored fh data, because it has been initialized by the FhExtractAndStore(fname) call (couple of lines above)
  2602 		{//Zero the stored fh data, because it has been initialized by the FhExtractAndStore(fname) call (couple of lines above)
  2592 		osLayerData.StoreFhData(NULL, EFalse);
  2603 		osLayerData.StoreFhData(NULL, EFalse);
       
  2604 		SQLITE_TRACE_OS(OstTrace0(TRACE_INTERNALS, TVFS_FULLPATHNAME_EXIT1, "OS-Exit;0;TVfs::FullPathName;err=SQLITE_ERROR"));
  2593 		return SQLITE_ERROR;	
  2605 		return SQLITE_ERROR;	
  2594 		}
  2606 		}
       
  2607 	SQLITE_TRACE_OS(OstTrace0(TRACE_INTERNALS, TVFS_FULLPATHNAME_EXIT2, "OS-Exit;0;TVfs::FullPathName;err=SQLITE_OK"));
  2595 	return SQLITE_OK;
  2608 	return SQLITE_OK;
  2596 	}
  2609 	}
  2597 
  2610 
  2598 /**
  2611 /**
  2599 SQLite OS porting layer API.
  2612 SQLite OS porting layer API.
  2605 
  2618 
  2606 @return The length of the used part of the output buffer.
  2619 @return The length of the used part of the output buffer.
  2607 */
  2620 */
  2608 /* static */ int TVfs::Randomness(sqlite3_vfs* aVfs, int aBufLen, char* aBuf)
  2621 /* static */ int TVfs::Randomness(sqlite3_vfs* aVfs, int aBufLen, char* aBuf)
  2609 	{
  2622 	{
  2610 	SQLUTRACE_PROFILER(aVfs);
       
  2611 	__OS_CALL(EOsVfsRandomness, 0, 0);
  2623 	__OS_CALL(EOsVfsRandomness, 0, 0);
  2612 	__OSTIME_COUNTER(TheOsCallTicks[EOsVfsRandomness], ::OsCallProfile(EFalse, EOsVfsRandomness), aBufLen, 0, 0, 0);
  2624 	__OSTIME_COUNTER(TheOsCallTicks[EOsVfsRandomness], ::OsCallProfile(EFalse, EOsVfsRandomness), aBufLen, 0, 0, 0);
  2613 	COsLayerData& osLayerData = COsLayerData::Instance();
  2625 	COsLayerData& osLayerData = COsLayerData::Instance();
  2614 	const TInt KRandIterations = aBufLen / sizeof(int);
  2626 	const TInt KRandIterations = aBufLen / sizeof(int);
  2615 	for(TInt i=0;i<KRandIterations;++i)
  2627 	for(TInt i=0;i<KRandIterations;++i)
  2629 
  2641 
  2630 @return The aMicrosec value.
  2642 @return The aMicrosec value.
  2631 */
  2643 */
  2632 /* static */ int TVfs::Sleep(sqlite3_vfs* aVfs, int aMicrosec)
  2644 /* static */ int TVfs::Sleep(sqlite3_vfs* aVfs, int aMicrosec)
  2633 	{
  2645 	{
  2634 	SQLUTRACE_PROFILER(aVfs);
       
  2635 	__OS_CALL(EOsVfsSleep, 0, 0);
  2646 	__OS_CALL(EOsVfsSleep, 0, 0);
  2636 	__OSTIME_COUNTER(TheOsCallTicks[EOsVfsSleep], ::OsCallProfile(EFalse, EOsVfsSleep), aMicrosec, 0, 0, 0);
  2647 	__OSTIME_COUNTER(TheOsCallTicks[EOsVfsSleep], ::OsCallProfile(EFalse, EOsVfsSleep), aMicrosec, 0, 0, 0);
  2637 	User::AfterHighRes(TTimeIntervalMicroSeconds32(aMicrosec));
  2648 	User::AfterHighRes(TTimeIntervalMicroSeconds32(aMicrosec));
  2638 	return aMicrosec;
  2649 	return aMicrosec;
  2639 	}
  2650 	}
  2649 
  2660 
  2650 @return 0.
  2661 @return 0.
  2651 */
  2662 */
  2652 /* static */ int TVfs::CurrentTime(sqlite3_vfs* aVfs, double* aNow)
  2663 /* static */ int TVfs::CurrentTime(sqlite3_vfs* aVfs, double* aNow)
  2653 	{
  2664 	{
  2654 	SQLUTRACE_PROFILER(aVfs);
       
  2655 	__OS_CALL(EOsVfsCurrentTime, 0, 0);
  2665 	__OS_CALL(EOsVfsCurrentTime, 0, 0);
  2656 	__OSTIME_COUNTER(TheOsCallTicks[EOsVfsCurrentTime], ::OsCallProfile(EFalse, EOsVfsCurrentTime), 0, 0, 0, 0);
  2666 	__OSTIME_COUNTER(TheOsCallTicks[EOsVfsCurrentTime], ::OsCallProfile(EFalse, EOsVfsCurrentTime), 0, 0, 0, 0);
  2657 	TTime now;
  2667 	TTime now;
  2658 	now.UniversalTime();
  2668 	now.UniversalTime();
  2659 	TDateTime date = now.DateTime();
  2669 	TDateTime date = now.DateTime();
  2685 
  2695 
  2686 @return 0.
  2696 @return 0.
  2687 */
  2697 */
  2688 /* static */int TVfs::GetLastError(sqlite3_vfs* aVfs, int /*aBufLen*/, char* /*aBuf*/)
  2698 /* static */int TVfs::GetLastError(sqlite3_vfs* aVfs, int /*aBufLen*/, char* /*aBuf*/)
  2689 	{
  2699 	{
  2690 	SQLUTRACE_PROFILER(aVfs);
       
  2691 	__OS_CALL(EOsVfsGetLastError, 0, 0);
  2700 	__OS_CALL(EOsVfsGetLastError, 0, 0);
  2692 	__OSTIME_COUNTER(TheOsCallTicks[EOsVfsGetLastError], ::OsCallProfile(EFalse, EOsVfsGetLastError), 0, 0, 0, 0);
  2701 	__OSTIME_COUNTER(TheOsCallTicks[EOsVfsGetLastError], ::OsCallProfile(EFalse, EOsVfsGetLastError), 0, 0, 0, 0);
  2693 	return 0;
  2702 	return 0;
  2694 	}
  2703 	}
  2695 
  2704 
  2700 /**
  2709 /**
  2701 SQLite OS porting layer API.
  2710 SQLite OS porting layer API.
  2702 
  2711 
  2703 Memory allocation routine.
  2712 Memory allocation routine.
  2704 
  2713 
       
  2714 EXPORT_C required to match IMPORT_C in stdlib.h and avoid compiler warning
       
  2715 
  2705 @internalComponent
  2716 @internalComponent
  2706 */
  2717 */
  2707 extern "C" void* sqlite3SymbianMalloc(size_t aSize)
  2718 extern "C" EXPORT_C void* sqlite3SymbianMalloc(size_t aSize)
  2708 	{
  2719 	{
  2709 	__MEM_CALL(EMemOpAlloc, aSize, 0);
  2720 	__MEM_CALL(EMemOpAlloc, aSize, 0);
  2710 	return Allocator().Alloc(aSize);
  2721 	return Allocator().Alloc(aSize);
  2711 	}
  2722 	}
  2712 
  2723 
  2713 /**
  2724 /**
  2714 SQLite OS porting layer API.
  2725 SQLite OS porting layer API.
  2715 
  2726 
  2716 Memory reallocation routine.
  2727 Memory reallocation routine.
  2717 
  2728 
       
  2729 EXPORT_C required to match IMPORT_C in stdlib.h and avoid compiler warning
       
  2730 
  2718 @internalComponent
  2731 @internalComponent
  2719 */
  2732 */
  2720 extern "C" void* sqlite3SymbianRealloc(void* aPtr, size_t aSize)
  2733 extern "C" EXPORT_C void* sqlite3SymbianRealloc(void* aPtr, size_t aSize)
  2721 	{
  2734 	{
  2722 #ifdef _SQLPROFILER
  2735 #ifdef _SQLPROFILER
  2723 	TInt size = Allocator().AllocLen(aPtr);
  2736 	TInt size = Allocator().AllocLen(aPtr);
  2724 	__MEM_CALL(EMemOpRealloc, aSize, size);
  2737 	__MEM_CALL(EMemOpRealloc, aSize, size);
  2725 #endif
  2738 #endif
  2729 /**
  2742 /**
  2730 SQLite OS porting layer API.
  2743 SQLite OS porting layer API.
  2731 
  2744 
  2732 Memory free routine.
  2745 Memory free routine.
  2733 
  2746 
       
  2747 EXPORT_C required to match IMPORT_C in stdlib.h and avoid compiler warning
       
  2748 
  2734 @internalComponent
  2749 @internalComponent
  2735 */
  2750 */
  2736 extern "C" void sqlite3SymbianFree(void* aPtr)
  2751 extern "C" EXPORT_C void sqlite3SymbianFree(void* aPtr)
  2737 	{
  2752 	{
  2738 #ifdef _SQLPROFILER
  2753 #ifdef _SQLPROFILER
  2739 	TInt size = Allocator().AllocLen(aPtr);
  2754 	TInt size = Allocator().AllocLen(aPtr);
  2740 	__MEM_CALL(EMemOpFree, size, 0);
  2755 	__MEM_CALL(EMemOpFree, size, 0);
  2741 #endif
  2756 #endif