persistentstorage/sql/OsLayer/os_symbian.cpp
changeset 41 3256212fc81f
parent 35 0d6db0a14001
child 46 826b40dcb0a8
equal deleted inserted replaced
35:0d6db0a14001 41:3256212fc81f
    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 
    57 
    53 /**
    58 //The SQLite temp files willl be created in this subdir
    54 Panic category - used by asserts in this file (the OS porting layer).
    59 _LIT(KTempFileDir, "temp");
    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 
    60 
    87 //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
    88 //opened or created is a journal file.
    62 //opened or created is a journal file.
    89 const TUint KJournalFileTypeBitMask = SQLITE_OPEN_MAIN_JOURNAL | SQLITE_OPEN_TEMP_JOURNAL | SQLITE_OPEN_SUBJOURNAL | SQLITE_OPEN_MASTER_JOURNAL; 
    63 const TUint KJournalFileTypeBitMask = SQLITE_OPEN_MAIN_JOURNAL | SQLITE_OPEN_TEMP_JOURNAL | SQLITE_OPEN_SUBJOURNAL | SQLITE_OPEN_MASTER_JOURNAL; 
    90 
    64 
   483 
   457 
   484 TInt FastCounterFrequency()
   458 TInt FastCounterFrequency()
   485 	{
   459 	{
   486 	TInt ticksPerSec = 0;
   460 	TInt ticksPerSec = 0;
   487 	TInt err = HAL::Get(HAL::EFastCounterFrequency, ticksPerSec);
   461 	TInt err = HAL::Get(HAL::EFastCounterFrequency, ticksPerSec);
   488 	__ASSERT_ALWAYS(err == KErrNone, User::Panic(KPanicCategory, EPanicFastCounterFreq));
   462 	__ASSERT_ALWAYS(err == KErrNone, __SQLITEPANIC2(ESqliteOsPanicFastCounterFreq));
   489 	return ticksPerSec;
   463 	return ticksPerSec;
   490 	}
   464 	}
   491 
   465 
   492 TInt sqlite3SymbianProfilerStart(TInt aCounterType)
   466 TInt sqlite3SymbianProfilerStart(TInt aCounterType)
   493 	{
   467 	{
   677 	}
   651 	}
   678 
   652 
   679 #endif//_SQLPROFILER
   653 #endif//_SQLPROFILER
   680 
   654 
   681 ///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
   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 ///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
   682 /////////////////////       COsLayerData class declaration   //////////////////////////////////////////////////////////////////
   697 /////////////////////       COsLayerData class declaration   //////////////////////////////////////////////////////////////////
   683 ///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
   698 ///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
   684 
   699 
   685 /**
   700 /**
   686 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: 
   718 	TInt DoCreate();
   733 	TInt DoCreate();
   719 	
   734 	
   720 public:
   735 public:
   721 	RFs			iFs;		//File session instance.
   736 	RFs			iFs;		//File session instance.
   722 	TFileName	iSysPrivDir;//"<system drive>:\" + process's private data path. Initialized in sqlite3SymbianFsOpen().
   737 	TFileName	iSysPrivDir;//"<system drive>:\" + process's private data path. Initialized in sqlite3SymbianFsOpen().
   723 							//Used for storing sqlite temporary files.
       
   724 	TInt64		iSeed;
   738 	TInt64		iSeed;
   725 
   739 
   726 private:	
   740 private:	
   727 	static COsLayerData* 	iOsLayerData;
   741 	static COsLayerData* 	iOsLayerData;
   728 	TInt					iStoredOsErrorCode;	//Contains the last OS error code.
   742 	TInt					iStoredOsErrorCode;	//Contains the last OS error code.
   765 */
   779 */
   766 NONSHARABLE_STRUCT(TDbFile) : public sqlite3_file 
   780 NONSHARABLE_STRUCT(TDbFile) : public sqlite3_file 
   767 	{
   781 	{
   768 	inline TDbFile();
   782 	inline TDbFile();
   769 	RFileBuf64	iFileBuf;
   783 	RFileBuf64	iFileBuf;
   770 	HBufC*		iFullName;				//Used for the "delete file" operation (RFile64::FullName() makes an IPC call!)
       
   771 	TInt		iLockType;				//File lock type
   784 	TInt		iLockType;				//File lock type
   772 	TBool		iReadOnly;				//True if the file is read-only
   785 	TBool		iReadOnly;				//True if the file is read-only
   773 	TInt		iSectorSize;			//Media sector-size
   786 	TInt		iSectorSize;			//Media sector-size
   774 	TInt		iDeviceCharacteristics;
   787 	TInt		iDeviceCharacteristics;
   775 	TSqlFreePageCallback iFreePageCallback;
   788 	TSqlFreePageCallback iFreePageCallback;
       
   789 	TBool       iIsFileCreated;          //If the file to be created is a temp file, 
       
   790                                        //it will not be created until the data is to be written to.
   776 #ifdef _SQLPROFILER
   791 #ifdef _SQLPROFILER
   777 	TBool		iIsJournal;
   792 	TBool		iIsJournal;
   778 #endif	
   793 #endif	
   779 	};
   794 	};
   780 
   795 
   812 	static int Unlock(sqlite3_file* aDbFile, int aLockType);
   827 	static int Unlock(sqlite3_file* aDbFile, int aLockType);
   813 	static int CheckReservedLock(sqlite3_file* aDbFile, int *aResOut);
   828 	static int CheckReservedLock(sqlite3_file* aDbFile, int *aResOut);
   814 	static int FileControl(sqlite3_file* aDbFile, int aOp, void* aArg);
   829 	static int FileControl(sqlite3_file* aDbFile, int aOp, void* aArg);
   815 	static int SectorSize(sqlite3_file* aDbFile);
   830 	static int SectorSize(sqlite3_file* aDbFile);
   816 	static int DeviceCharacteristics(sqlite3_file* aDbFile);
   831 	static int DeviceCharacteristics(sqlite3_file* aDbFile);
       
   832 private:
       
   833 	static TInt DoCreateTempFile(TDbFile& aDbFile);
   817 	};
   834 	};
   818 
   835 
   819 ///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
   836 ///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
   820 ///////////////////////////////////       TVfs class declaration      /////////////////////////////////////////////////////////
   837 ///////////////////////////////////       TVfs class declaration      /////////////////////////////////////////////////////////
   821 ///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
   838 ///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
   840 	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);
   841 	static int Randomness(sqlite3_vfs* aVfs, int aBufLen, char* aBuf);
   858 	static int Randomness(sqlite3_vfs* aVfs, int aBufLen, char* aBuf);
   842 	static int Sleep(sqlite3_vfs* aVfs, int aMicrosec);
   859 	static int Sleep(sqlite3_vfs* aVfs, int aMicrosec);
   843 	static int CurrentTime(sqlite3_vfs* aVfs, double* aNow);
   860 	static int CurrentTime(sqlite3_vfs* aVfs, double* aNow);
   844 	static int GetLastError(sqlite3_vfs *sVfs, int aBufLen, char* aBuf);
   861 	static int GetLastError(sqlite3_vfs *sVfs, int aBufLen, char* aBuf);
       
   862 	static TInt DoGetDeviceCharacteristicsAndSectorSize(TDbFile& aDbFile, TInt& aRecReadBufSize);
   845 private:
   863 private:
   846 	static TInt DoOpenFromHandle(TDbFile& aDbFile, const RMessage2& aMsg, TBool aReadOnly);
   864 	static TInt DoOpenFromHandle(TDbFile& aDbFile, const RMessage2& aMsg, TBool aReadOnly);
   847 	static inline TInt DoGetVolumeIoParamInfo(RFs& aFs, TInt aDriveNo, TVolumeIOParamInfo& aVolumeInfo);
   865 	static inline TInt DoGetVolumeIoParamInfo(RFs& aFs, TInt aDriveNo, TVolumeIOParamInfo& aVolumeInfo);
   848 	static TInt DoGetDeviceCharacteristics(const TDriveInfo& aDriveInfo, const TVolumeIOParamInfo& aVolumeInfo);
   866 	static TInt DoGetDeviceCharacteristics(const TDriveInfo& aDriveInfo, const TVolumeIOParamInfo& aVolumeInfo);
   849 	static TInt DoGetSectorSize(const TDriveInfo& aDriveInfo, const TVolumeIOParamInfo& aVolumeInfo);
   867 	static TInt DoGetSectorSize(const TDriveInfo& aDriveInfo, const TVolumeIOParamInfo& aVolumeInfo);
   850 	static TInt DoGetDeviceCharacteristicsAndSectorSize(TDbFile& aDbFile, TInt& aRecReadBufSize);
       
   851 	static TInt DoFileSizeCorruptionCheck(TDbFile& aDbFile, const TDesC& aFname, TInt aFmode);
   868 	static TInt DoFileSizeCorruptionCheck(TDbFile& aDbFile, const TDesC& aFname, TInt aFmode);
   852 	};
   869 	};
   853 
   870 
   854 ///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
   871 ///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
   855 /////////////////////       Global variables, constants    ////////////////////////////////////////////////////////////////////
   872 /////////////////////       Global variables, constants    ////////////////////////////////////////////////////////////////////
   950                            
   967                            
   951 @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.
   952 */
   969 */
   953 /* static */ TInt COsLayerData::Create()
   970 /* static */ TInt COsLayerData::Create()
   954 	{
   971 	{
   955 	__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));
   956 	if(!COsLayerData::iOsLayerData)
   974 	if(!COsLayerData::iOsLayerData)
   957 		{
   975 		{
   958 		COsLayerData::iOsLayerData = new COsLayerData;
   976 		COsLayerData::iOsLayerData = new COsLayerData;
   959 		if(!COsLayerData::iOsLayerData)
   977 		if(!COsLayerData::iOsLayerData)
   960 			{
   978 			{
       
   979 			SQLITE_TRACE_OS(OstTrace0(TRACE_INTERNALS, COSLAYERDATA_CREATE_EXIT1, "OS-Exit;0;COsLayerData::Create;err=KErrNoMemory"));
   961 			return KErrNoMemory;	
   980 			return KErrNoMemory;	
   962 			}
   981 			}
   963 		TInt err = COsLayerData::iOsLayerData->DoCreate();
   982 		TInt err = COsLayerData::iOsLayerData->DoCreate();
   964 		if(err != KErrNone)
   983 		if(err != KErrNone)
   965 			{
   984 			{
   966 			delete COsLayerData::iOsLayerData;
   985 			delete COsLayerData::iOsLayerData;
   967 			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));
   968 			return err;
   988 			return err;
   969 			}
   989 			}
   970 		}
   990 		}
       
   991 	SQLITE_TRACE_OS(OstTrace0(TRACE_INTERNALS, COSLAYERDATA_CREATE_EXIT3, "OS-Exit;0;COsLayerData::Create;err=KErrNone"));
   971 	return KErrNone;
   992 	return KErrNone;
   972 	}
   993 	}
   973 
   994 
   974 /**
   995 /**
   975 Destroys the COsLayerData instance.
   996 Destroys the COsLayerData instance.
   976 */
   997 */
   977 /* static */ inline void COsLayerData::Destroy()
   998 /* static */ inline void COsLayerData::Destroy()
   978 	{
   999 	{
   979 	delete COsLayerData::iOsLayerData;
  1000 	delete COsLayerData::iOsLayerData;
   980 	COsLayerData::iOsLayerData = NULL;
  1001 	COsLayerData::iOsLayerData = NULL;
       
  1002 	SQLITE_TRACE_OS(OstTrace0(TRACE_INTERNALS, COSLAYERDATA_DESTROY, "OS;0;COsLayerData::Destroy"));
   981 	}
  1003 	}
   982 
  1004 
   983 /**
  1005 /**
   984 Returns a reference to the single COsLayerData instance.
  1006 Returns a reference to the single COsLayerData instance.
   985 
  1007 
   986 @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.
   987 */
  1009 */
   988 /* static */ inline COsLayerData& COsLayerData::Instance()
  1010 /* static */ inline COsLayerData& COsLayerData::Instance()
   989 	{
  1011 	{
   990 	__ASSERT_DEBUG(COsLayerData::iOsLayerData != NULL, User::Panic(KPanicCategory, EPanicNullOsLayerDataPtr));
  1012 	__ASSERT_DEBUG(COsLayerData::iOsLayerData != NULL, __SQLITEPANIC2(ESqliteOsPanicNullOsLayerDataPtr));
   991 	return *COsLayerData::iOsLayerData;
  1013 	return *COsLayerData::iOsLayerData;
   992 	}
  1014 	}
   993 
  1015 
   994 /**
  1016 /**
   995 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
  1059 
  1081 
  1060 @panic Sqlite 13 In _DEBUG mode - aMsg is NULL.
  1082 @panic Sqlite 13 In _DEBUG mode - aMsg is NULL.
  1061 */
  1083 */
  1062 inline void COsLayerData::RetrieveAndResetFhData(const RMessage2*& aMsg, TBool& aReadOnly)
  1084 inline void COsLayerData::RetrieveAndResetFhData(const RMessage2*& aMsg, TBool& aReadOnly)
  1063 	{
  1085 	{
  1064 	__ASSERT_DEBUG(iMessage != NULL, User::Panic(KPanicCategory, EPanicInvalidFhData));
  1086 	__ASSERT_DEBUG(iMessage != NULL, __SQLITEPANIC(ESqliteOsPanicInvalidFhData));
  1065 	aMsg = iMessage; 
  1087 	aMsg = iMessage; 
  1066 	aReadOnly = iReadOnly;
  1088 	aReadOnly = iReadOnly;
  1067 	iMessage = NULL;
  1089 	iMessage = NULL;
  1068 	}
  1090 	}
  1069 
  1091 
  1085 
  1107 
  1086 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!
  1087 */
  1109 */
  1088 inline COsLayerData::~COsLayerData()
  1110 inline COsLayerData::~COsLayerData()
  1089 	{
  1111 	{
       
  1112 	SQLITE_TRACE_OS(OstTrace1(TRACE_INTERNALS, COSLAYERDATA_COSLAYERDATA2, "OS;0;COsLayerData::~COsLayerData;iFs.Handle()=0x%X", (TUint)iFs.Handle()));
  1090 	__FS_CALL(EFsOpFsClose, 0);
  1113 	__FS_CALL(EFsOpFsClose, 0);
  1091 	iFs.Close();	
  1114 	iFs.Close();	
  1092 	}
  1115 	}
  1093 
  1116 
  1094 /**
  1117 /**
  1134 	TDriveUnit drive(sysDrive);
  1157 	TDriveUnit drive(sysDrive);
  1135 	TDriveName driveName = drive.Name();
  1158 	TDriveName driveName = drive.Name();
  1136 	TParse parse;
  1159 	TParse parse;
  1137 	(void)parse.Set(driveName, &privateDir, 0);//this call can't fail
  1160 	(void)parse.Set(driveName, &privateDir, 0);//this call can't fail
  1138 	iSysPrivDir.Copy(parse.DriveAndPath());
  1161 	iSysPrivDir.Copy(parse.DriveAndPath());
       
  1162 	//Create the temp files directory
       
  1163 	(void)parse.AddDir(KTempFileDir);//this call can't fail
       
  1164 	err = iFs.MkDir(parse.DriveAndPath());
       
  1165     if(err != KErrNone && err != KErrAlreadyExists)
       
  1166     	{
       
  1167 		return err;
       
  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)));
  1139 	return KErrNone;
  1170 	return KErrNone;
  1140 	}
  1171 	}
  1141 
  1172 
  1142 ///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
  1173 ///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
  1143 ///////////////////////     Symbian OS specific functions (called by the SQL server)        ///////////////////////////////////
  1174 ///////////////////////     Symbian OS specific functions (called by the SQL server)        ///////////////////////////////////
  1183 	if(sqliteErr != SQLITE_OK)
  1214 	if(sqliteErr != SQLITE_OK)
  1184 		{
  1215 		{
  1185 		osErr = sqliteErr == SQLITE_NOMEM ? KErrNoMemory : KErrGeneral;
  1216 		osErr = sqliteErr == SQLITE_NOMEM ? KErrNoMemory : KErrGeneral;
  1186 		COsLayerData::Destroy();
  1217 		COsLayerData::Destroy();
  1187 		}
  1218 		}
       
  1219 	SQLITE_TRACE_OS(OstTrace1(TRACE_INTERNALS, SQLITE3SYMBIANLIBINIT, "OS;0;sqlite3SymbianLibInit;osErr=%d", osErr));
  1188 	return osErr;
  1220 	return osErr;
  1189 	}
  1221 	}
  1190 
  1222 
  1191 /**
  1223 /**
  1192 This function must be called once after finishing working with sqlite.
  1224 This function must be called once after finishing working with sqlite.
  1202 */
  1234 */
  1203 void sqlite3SymbianLibFinalize(void)
  1235 void sqlite3SymbianLibFinalize(void)
  1204 	{
  1236 	{
  1205 	(void)sqlite3_shutdown();
  1237 	(void)sqlite3_shutdown();
  1206 	COsLayerData::Destroy();
  1238 	COsLayerData::Destroy();
       
  1239 	SQLITE_TRACE_OS(OstTrace0(TRACE_INTERNALS, SQLITE3SYMBIANLIBFINALIZE, "OS;0;sqlite3SymbianLibFinalize"));
  1207 	}
  1240 	}
  1208 
  1241 
  1209 /**
  1242 /**
  1210 This function is part of Symbian OS specific SQLITE API.
  1243 This function is part of Symbian OS specific SQLITE API.
  1211 
  1244 
  1238 	{
  1271 	{
  1239 	if(aFileName)
  1272 	if(aFileName)
  1240 		{
  1273 		{
  1241 		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()));
  1242 		TInt len = mbstowcs(dest, aFileName, aFileNameDestBuf.MaxLength());
  1275 		TInt len = mbstowcs(dest, aFileName, aFileNameDestBuf.MaxLength());
  1243 		__ASSERT_DEBUG(len >= 0, User::Panic(KPanicCategory, EPanicInternalError));
  1276 		__ASSERT_DEBUG(len >= 0, __SQLITEPANIC2(ESqliteOsPanicInternalError));
  1244 		//If len == aFileNameDestBuf.MaxLength(), then the output buffer is too small.
  1277 		//If len == aFileNameDestBuf.MaxLength(), then the output buffer is too small.
  1245 		if(len < aFileNameDestBuf.MaxLength())
  1278 		if(len < aFileNameDestBuf.MaxLength())
  1246 			{
  1279 			{
  1247 			aFileNameDestBuf.SetLength(len);
  1280 			aFileNameDestBuf.SetLength(len);
  1248 			return ETrue;
  1281 			return ETrue;
  1267 static TBool ConvertFromUnicode(const TDesC& aFileName, TDes8& aFileNameDestBuf)
  1300 static TBool ConvertFromUnicode(const TDesC& aFileName, TDes8& aFileNameDestBuf)
  1268 	{
  1301 	{
  1269 	char* dest = reinterpret_cast <char*> (const_cast <TUint8*> (aFileNameDestBuf.Ptr()));
  1302 	char* dest = reinterpret_cast <char*> (const_cast <TUint8*> (aFileNameDestBuf.Ptr()));
  1270 	const wchar_t* src = reinterpret_cast <const wchar_t*> (aFileName.Ptr());
  1303 	const wchar_t* src = reinterpret_cast <const wchar_t*> (aFileName.Ptr());
  1271 	TInt len = wcstombs(dest, src, aFileNameDestBuf.MaxLength());
  1304 	TInt len = wcstombs(dest, src, aFileNameDestBuf.MaxLength());
  1272 	__ASSERT_DEBUG(len >= 0, User::Panic(KPanicCategory, EPanicInternalError));
  1305 	__ASSERT_DEBUG(len >= 0, __SQLITEPANIC2(ESqliteOsPanicInternalError));
  1273 	//If len == aFileNameDestBuf.MaxLength(), then the output buffer is too small.
  1306 	//If len == aFileNameDestBuf.MaxLength(), then the output buffer is too small.
  1274 	if(len < aFileNameDestBuf.MaxLength())
  1307 	if(len < aFileNameDestBuf.MaxLength())
  1275 		{
  1308 		{
  1276 		aFileNameDestBuf.SetLength(len);
  1309 		aFileNameDestBuf.SetLength(len);
  1277 		return ETrue;
  1310 		return ETrue;
  1409 @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.
  1410 */
  1443 */
  1411 static void FhExtractAndStore(TDes& aDbFileName)
  1444 static void FhExtractAndStore(TDes& aDbFileName)
  1412 	{
  1445 	{
  1413 	TInt fhStartPos = aDbFileName.Locate(TChar(KFhSeparator));
  1446 	TInt fhStartPos = aDbFileName.Locate(TChar(KFhSeparator));
  1414 	__ASSERT_DEBUG(fhStartPos == KFhMarkPos, User::Panic(KPanicCategory, EPanicInvalidFhStr));
  1447 	__ASSERT_DEBUG(fhStartPos == KFhMarkPos, __SQLITEPANIC2(ESqliteOsPanicInvalidFhStr));
  1415 	//If this file name string contains file handles
  1448 	//If this file name string contains file handles
  1416 	if(fhStartPos == KFhMarkPos)
  1449 	if(fhStartPos == KFhMarkPos)
  1417 		{
  1450 		{
  1418 		//Extract from aDbFileName string RMessage2 object's address
  1451 		//Extract from aDbFileName string RMessage2 object's address
  1419 		TLex lex(aDbFileName.Mid(fhStartPos + KFhMsgAddrPos, KFhMsgAddrLen));
  1452 		TLex lex(aDbFileName.Mid(fhStartPos + KFhMsgAddrPos, KFhMsgAddrLen));
  1420 		TUint32 addr;
  1453 		TUint32 addr;
  1421 		TInt err = lex.Val(addr, EHex);
  1454 		TInt err = lex.Val(addr, EHex);
  1422 		__ASSERT_DEBUG(err == KErrNone, User::Panic(KPanicCategory, EPanicInvalidFhStr));
  1455 		__ASSERT_DEBUG(err == KErrNone, __SQLITEPANIC2(ESqliteOsPanicInvalidFhStr));
  1423 		if(err == KErrNone)
  1456 		if(err == KErrNone)
  1424 			{
  1457 			{
  1425 			//Cast the address to RMessage2 pointer.
  1458 			//Cast the address to RMessage2 pointer.
  1426 			const RMessage2* msg = reinterpret_cast <const RMessage2*> (addr);
  1459 			const RMessage2* msg = reinterpret_cast <const RMessage2*> (addr);
  1427 			__ASSERT_DEBUG(msg != NULL, User::Panic(KPanicCategory, EPanicInvalidFhStr));
  1460 			__ASSERT_DEBUG(msg != NULL, __SQLITEPANIC2(ESqliteOsPanicInvalidFhStr));
  1428 			if(msg)
  1461 			if(msg)
  1429 				{
  1462 				{
  1430 				//Store the data from aDbFileName in the single COsLayerData instance.
  1463 				//Store the data from aDbFileName in the single COsLayerData instance.
  1431 				TBool readOnly = aDbFileName[fhStartPos + KFhRoPos] > '0';
  1464 				TBool readOnly = aDbFileName[fhStartPos + KFhRoPos] > '0';
  1432 				COsLayerData::Instance().StoreFhData(msg, readOnly);
  1465 				COsLayerData::Instance().StoreFhData(msg, readOnly);
  1444 /**
  1477 /**
  1445 Initializes TDbFile data members with their default values.
  1478 Initializes TDbFile data members with their default values.
  1446 */
  1479 */
  1447 inline TDbFile::TDbFile() :
  1480 inline TDbFile::TDbFile() :
  1448 	iFileBuf(KFileBufSize),
  1481 	iFileBuf(KFileBufSize),
  1449 	iFullName(0),
       
  1450 	iLockType(SQLITE_LOCK_NONE),
  1482 	iLockType(SQLITE_LOCK_NONE),
  1451 	iReadOnly(EFalse),
  1483 	iReadOnly(EFalse),
  1452 	iSectorSize(0),
  1484 	iSectorSize(0),
  1453 	iDeviceCharacteristics(-1)
  1485 	iDeviceCharacteristics(-1),
       
  1486 	iIsFileCreated(ETrue)
  1454 	{
  1487 	{
  1455 #ifdef _SQLPROFILER
  1488 #ifdef _SQLPROFILER
  1456 	iIsJournal = EFalse;
  1489 	iIsJournal = EFalse;
  1457 #endif
  1490 #endif
  1458 	pMethods = 0;
  1491 	pMethods = 0;
       
  1492 	SQLITE_TRACE_OS(OstTrace1(TRACE_INTERNALS, TDBFILE_TDBFILE, "OS;0x%X;TDbFile::TDbFile", (TUint)this));
  1459 	}
  1493 	}
  1460 
  1494 
  1461 /**
  1495 /**
  1462 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&.
  1463 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. 
  1474 
  1508 
  1475 @internalComponent
  1509 @internalComponent
  1476 */
  1510 */
  1477 static inline TDbFile& DbFile(sqlite3_file* aDbFile)
  1511 static inline TDbFile& DbFile(sqlite3_file* aDbFile)
  1478 	{
  1512 	{
  1479 	__ASSERT_DEBUG(aDbFile != 0, User::Panic(KPanicCategory, EPanicNullDbFilePtr));
  1513 	__ASSERT_DEBUG(aDbFile != 0, __SQLITEPANIC2(ESqliteOsPanicNullDbFilePtr));
  1480 	return *(static_cast <TDbFile*> (aDbFile));
  1514 	return *(static_cast <TDbFile*> (aDbFile));
  1481 	}
  1515 	}
  1482 
  1516 
  1483 ///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
  1517 ///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
  1484 /////////////////////       TFileIo class definition    ///////////////////////////////////////////////////////////////////////
  1518 /////////////////////       TFileIo class definition    ///////////////////////////////////////////////////////////////////////
  1486 
  1520 
  1487 /**
  1521 /**
  1488 SQLite OS porting layer API.
  1522 SQLite OS porting layer API.
  1489 
  1523 
  1490 Closes the file referred by aDbFile parameter.
  1524 Closes the file referred by aDbFile parameter.
  1491 If aDbFile.iFullName data member is not NULL, then the file will be deleted.
       
  1492 
  1525 
  1493 @param aDbFile A pointer to a TDbFile instance, than contains the file handle to be closed.
  1526 @param aDbFile A pointer to a TDbFile instance, than contains the file handle to be closed.
  1494 
  1527 
  1495 @return SQLITE_OK
  1528 @return SQLITE_OK
  1496 
  1529 
  1497 @see TDbFile
  1530 @see TDbFile
  1498 */
  1531 */
  1499 /* static */ int TFileIo::Close(sqlite3_file* aDbFile)
  1532 /* static */ int TFileIo::Close(sqlite3_file* aDbFile)
  1500 	{
  1533 	{
  1501 	SQLUTRACE_PROFILER(aDbFile);
       
  1502 	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));
  1503 	__OS_CALL(EOsFileClose, 0, 0);
  1536 	__OS_CALL(EOsFileClose, 0, 0);
  1504 	__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);
  1505 	__FS_CALL(EFsOpFileClose, 0);
  1538 	__FS_CALL(EFsOpFileClose, 0);
  1506 	dbFile.iFileBuf.Close();
  1539 	dbFile.iFileBuf.Close();
  1507 	if(dbFile.iFullName)
       
  1508 		{//"iFullName" will not be NULL only when TVfs::Open() is called with SQLITE_OPEN_DELETEONCLOSE flag.
       
  1509 		 //That means - SQlite expects the file to be deleted after the file close operation. 
       
  1510 		__FS_CALL(EFsOpFileDelete, 0);
       
  1511 		(void)COsLayerData::Instance().iFs.Delete(*dbFile.iFullName);
       
  1512 		delete dbFile.iFullName;
       
  1513 		}
       
  1514 	return SQLITE_OK;
  1540 	return SQLITE_OK;
  1515 	}
  1541 	}
  1516 
  1542 
  1517 /**
  1543 /**
  1518 SQLite OS porting layer API.
  1544 SQLite OS porting layer API.
  1536 @see COsLayerData::SetOsErrorCode()
  1562 @see COsLayerData::SetOsErrorCode()
  1537 @see TDbFile
  1563 @see TDbFile
  1538 */
  1564 */
  1539 /* 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)
  1540 	{
  1566 	{
  1541 	SQLUTRACE_PROFILER(aDbFile);
       
  1542 	SYMBIAN_TRACE_SQL_EVENTS_ONLY(UTF::Printf(UTF::TTraceContext(UTF::EInternals), KFileRead, aAmt, aOffset));
       
  1543 	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));
  1544 	__OS_CALL(EOsFileRead, 0, 0);
  1569 	__OS_CALL(EOsFileRead, 0, 0);
  1545 	__COUNTER_INCR(TheSqlSrvProfilerFileRead);
  1570 	__COUNTER_INCR(TheSqlSrvProfilerFileRead);
  1546 	__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);
  1547 	TPtr8 ptr((TUint8*)aBuf, 0, aAmt);
  1572 	TPtr8 ptr((TUint8*)aBuf, 0, aAmt);
  1548 	TInt err = dbFile.iFileBuf.Read(aOffset, ptr);
  1573 	TInt err = dbFile.iFileBuf.Read(aOffset, ptr);
  1549 	TInt cnt = ptr.Length();
  1574 	TInt cnt = ptr.Length();
  1550 	TInt sqliteErr = SQLITE_IOERR_READ;
  1575 	TInt sqliteErr = ::Os2SqliteErr(err, SQLITE_IOERR_READ);
  1551 	switch(err)
  1576 	if(cnt != aAmt && (sqliteErr == SQLITE_OK || sqliteErr == SQLITE_IOERR_SHORT_READ))
  1552 		{
  1577 		{
  1553 		case KErrNone:
  1578 		Mem::FillZ(static_cast <TUint8*> (aBuf) + cnt, aAmt - cnt);
  1554 			sqliteErr = SQLITE_OK;	
  1579 		err = KErrEof;
  1555 			if(cnt != aAmt)
  1580 		sqliteErr = SQLITE_IOERR_SHORT_READ;
  1556 				{
       
  1557 				Mem::FillZ(static_cast <TUint8*> (aBuf) + cnt, aAmt - cnt);
       
  1558 				sqliteErr = SQLITE_IOERR_SHORT_READ;
       
  1559 				err = KErrEof;
       
  1560 				}
       
  1561 			break;
       
  1562 		case KErrEof:
       
  1563 			Mem::FillZ(static_cast <TUint8*> (aBuf) + cnt, aAmt - cnt);
       
  1564 			sqliteErr = SQLITE_IOERR_SHORT_READ;
       
  1565 			break;
       
  1566 		case KErrNoMemory:
       
  1567 			sqliteErr = SQLITE_IOERR_NOMEM;
       
  1568 			break;
       
  1569 		default:
       
  1570 			break;
       
  1571 		}
  1581 		}
  1572 	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));
  1573 	return sqliteErr;
  1584 	return sqliteErr;
  1574 	}
  1585 	}
       
  1586 
       
  1587 //Creates a temporary file in "\temp" subdirectory of osLayerData.iSysPrivDir directory.
       
  1588 //If the function fails, the temp file will be closed and deleted, 
       
  1589 //the related Symbian OS error will be returned to the caller.
       
  1590 /* static */TInt TFileIo::DoCreateTempFile(TDbFile& aDbFile)
       
  1591     {
       
  1592     COsLayerData& osLayerData = COsLayerData::Instance();
       
  1593     //TParse2 is used in order to avoid the need of another TFileName stack based variable
       
  1594 	class TParse2 : public TParse
       
  1595 		{
       
  1596 	public:
       
  1597 		inline TFileName& FileName()
       
  1598 			{
       
  1599 			return static_cast <TFileName&> (NameBuf());
       
  1600 			}
       
  1601 		};
       
  1602 	TParse2 parse;
       
  1603 	(void)parse.Set(osLayerData.iSysPrivDir, 0, 0);//this call can't fail
       
  1604 	(void)parse.AddDir(KTempFileDir);//this call can't fail
       
  1605     __FS_CALL(EFsOpFileCreateTemp, 0);
       
  1606     TInt err = aDbFile.iFileBuf.Temp(osLayerData.iFs, parse.FullName(), parse.FileName(), EFileRead|EFileWrite|EDeleteOnClose);        
       
  1607     if(err == KErrNone)
       
  1608         {
       
  1609         TInt recReadBufSize = -1;
       
  1610         err = TVfs::DoGetDeviceCharacteristicsAndSectorSize(aDbFile, recReadBufSize);
       
  1611         if(err != KErrNone)
       
  1612             {
       
  1613             aDbFile.iFileBuf.Close();//With EDeleteOnClose flag set, the file will be deleted
       
  1614             }
       
  1615         else
       
  1616             {
       
  1617             (void)aDbFile.iFileBuf.SetReadAheadSize(aDbFile.iSectorSize, recReadBufSize);
       
  1618             aDbFile.iIsFileCreated = ETrue;
       
  1619             }
       
  1620         }
       
  1621     return err;
       
  1622     }
  1575 
  1623 
  1576 /**
  1624 /**
  1577 SQLite OS porting layer API.
  1625 SQLite OS porting layer API.
  1578 
  1626 
  1579 Writes to the file referred by the aDbFile parameter.
  1627 Writes to the file referred by the aDbFile parameter.
  1580 "Write beyond the end of the file" operations are allowed.
  1628 "Write beyond the end of the file" operations are allowed.
  1581 
  1629 
  1582 If the write operation is in the 1st db file page and there is a registered "free pages" callback 
  1630 If the write operation is in the 1st db file page and there is a registered "free pages" callback 
  1583 (TDbFile::iFreePageCallback) and the free pages count is above the defined value,
  1631 (TDbFile::iFreePageCallback) and the free pages count is above the defined value,
  1584 then the callback will be called.
  1632 then the callback will be called.
       
  1633 
       
  1634 If the file to be written to is a temp file, which is not created yet, then the file will be created.
  1585 
  1635 
  1586 @param aDbFile A pointer to a TDbFile instance, that contains the file handle to be written to.
  1636 @param aDbFile A pointer to a TDbFile instance, that contains the file handle to be written to.
  1587 @param aData The data to be written to the file. The buffer size must be at least aAmt bytes.
  1637 @param aData The data to be written to the file. The buffer size must be at least aAmt bytes.
  1588 @param aAmt The amount of data to be written to the file.
  1638 @param aAmt The amount of data to be written to the file.
  1589 @param aOffset The offset in the file where the write operation should start.
  1639 @param aOffset The offset in the file where the write operation should start.
  1599 @see COsLayerData::SetOsErrorCode()
  1649 @see COsLayerData::SetOsErrorCode()
  1600 @see TDbFile
  1650 @see TDbFile
  1601 */
  1651 */
  1602 /* static */ int TFileIo::Write(sqlite3_file* aDbFile, const void* aData, int aAmt, sqlite3_int64 aOffset)
  1652 /* static */ int TFileIo::Write(sqlite3_file* aDbFile, const void* aData, int aAmt, sqlite3_int64 aOffset)
  1603 	{
  1653 	{
  1604 	SQLUTRACE_PROFILER(aDbFile);
       
  1605 	SYMBIAN_TRACE_SQL_EVENTS_ONLY(UTF::Printf(UTF::TTraceContext(UTF::EInternals), KFileWrite, aAmt, aOffset));
       
  1606 	TDbFile& dbFile = ::DbFile(aDbFile);
  1654 	TDbFile& dbFile = ::DbFile(aDbFile);
       
  1655 	SQLITE_TRACE_OS(OstTraceExt3(TRACE_INTERNALS, TFILEIO_WRITE_ENTRY, "OS-Entry;0x%X;TFileIo::Write;aAmt=%d;aOffset=%lld", (TUint)&dbFile, aAmt, aOffset));
  1607 	__OS_CALL(EOsFileWrite, 0, 0);
  1656 	__OS_CALL(EOsFileWrite, 0, 0);
  1608     __COUNTER_INCR(TheSqlSrvProfilerFileWrite);
  1657     __COUNTER_INCR(TheSqlSrvProfilerFileWrite);
  1609 	__OSTIME_COUNTER(TheOsCallTicks[EOsFileWrite], ::OsCallProfile(dbFile.iIsJournal, EOsFileWrite), aOffset, aAmt, aDbFile, 0);
  1658 	__OSTIME_COUNTER(TheOsCallTicks[EOsFileWrite], ::OsCallProfile(dbFile.iIsJournal, EOsFileWrite), aOffset, aAmt, aDbFile, 0);
  1610 	TInt err = KErrAccessDenied;
  1659 	TInt err = KErrNone;
       
  1660 	if(!dbFile.iIsFileCreated)
       
  1661 	    {//Create a temp file if it has not been created. 
       
  1662 	    err = TFileIo::DoCreateTempFile(dbFile);
       
  1663 	    }
       
  1664 	if(err != KErrNone)
       
  1665 	    {
       
  1666         COsLayerData::Instance().SetOsErrorCode(err);
       
  1667 		return ::Os2SqliteErr(err, SQLITE_FULL);
       
  1668 	    }
       
  1669 	err = KErrAccessDenied;
  1611 	if(!dbFile.iReadOnly)
  1670 	if(!dbFile.iReadOnly)
  1612 		{
  1671 		{
  1613 		TPtrC8 ptr((const TUint8*)aData, aAmt);
  1672 		TPtrC8 ptr((const TUint8*)aData, aAmt);
  1614 		err = dbFile.iFileBuf.Write(aOffset, ptr);
  1673 		err = dbFile.iFileBuf.Write(aOffset, ptr);
  1615 		}
  1674 		}
  1628 		const TUint8* ptr = static_cast <const TUint8*> (aData) + KFreePageCountOffset;
  1687 		const TUint8* ptr = static_cast <const TUint8*> (aData) + KFreePageCountOffset;
  1629 		TInt freePageCount = (ptr[0] << 24) | (ptr[1] << 16) | (ptr[2] << 8) | ptr[3];
  1688 		TInt freePageCount = (ptr[0] << 24) | (ptr[1] << 16) | (ptr[2] << 8) | ptr[3];
  1630 		dbFile.iFreePageCallback.CheckAndCallback(freePageCount);
  1689 		dbFile.iFreePageCallback.CheckAndCallback(freePageCount);
  1631 		}
  1690 		}
  1632 		
  1691 		
  1633 	return err == KErrNone ? SQLITE_OK : (err == KErrNoMemory ? SQLITE_IOERR_NOMEM : SQLITE_FULL);
  1692 	TInt sqliteErr = ::Os2SqliteErr(err, SQLITE_FULL);
       
  1693 	SQLITE_TRACE_OS(OstTraceExt3(TRACE_INTERNALS, TFILEIO_WRITE_EXIT, "OS-Exit;0x%X;TFileIo::Write;err=%d;sqliteErr=%d", (TUint)&dbFile, err, sqliteErr));
       
  1694 	return sqliteErr;
  1634 	}
  1695 	}
  1635 
  1696 
  1636 /**
  1697 /**
  1637 SQLite OS porting layer API.
  1698 SQLite OS porting layer API.
  1638 
  1699 
  1653 @see COsLayerData::SetOsErrorCode()
  1714 @see COsLayerData::SetOsErrorCode()
  1654 @see TDbFile
  1715 @see TDbFile
  1655 */
  1716 */
  1656 /* static */ int TFileIo::Truncate(sqlite3_file* aDbFile, sqlite3_int64 aLength)
  1717 /* static */ int TFileIo::Truncate(sqlite3_file* aDbFile, sqlite3_int64 aLength)
  1657 	{
  1718 	{
  1658 	SQLUTRACE_PROFILER(aDbFile);
       
  1659 	SYMBIAN_TRACE_SQL_EVENTS_ONLY(UTF::Printf(UTF::TTraceContext(UTF::EInternals), KFileTruncate, aLength));
       
  1660 	TDbFile& dbFile = ::DbFile(aDbFile);
  1719 	TDbFile& dbFile = ::DbFile(aDbFile);
       
  1720 	SQLITE_TRACE_OS(OstTraceExt2(TRACE_INTERNALS, TFILEIO_TRUNCATE_ENTRY, "OS-Entry;0x%X;TFileIo::Truncate;aLength=%lld", (TUint)&dbFile, aLength));
  1661 	__OS_CALL(EOsFileTruncate, 0, 0);
  1721 	__OS_CALL(EOsFileTruncate, 0, 0);
  1662     __COUNTER_INCR(TheSqlSrvProfilerFileSetSize);
  1722     __COUNTER_INCR(TheSqlSrvProfilerFileSetSize);
  1663 	__OSTIME_COUNTER(TheOsCallTicks[EOsFileTruncate], ::OsCallProfile(dbFile.iIsJournal, EOsFileTruncate), aLength, 0, aDbFile, 0);
  1723 	__OSTIME_COUNTER(TheOsCallTicks[EOsFileTruncate], ::OsCallProfile(dbFile.iIsJournal, EOsFileTruncate), aLength, 0, aDbFile, 0);
  1664 	if(dbFile.iReadOnly)
  1724 	if(dbFile.iReadOnly)
  1665 		{
  1725 		{
  1667   		return SQLITE_IOERR;	
  1727   		return SQLITE_IOERR;	
  1668 		}
  1728 		}
  1669 	__FS_CALL(EFsOpFileSetSize, 0);
  1729 	__FS_CALL(EFsOpFileSetSize, 0);
  1670 	TInt err = dbFile.iFileBuf.SetSize(aLength);
  1730 	TInt err = dbFile.iFileBuf.SetSize(aLength);
  1671 	COsLayerData::Instance().SetOsErrorCode(err);
  1731 	COsLayerData::Instance().SetOsErrorCode(err);
  1672 	return err == KErrNone ? SQLITE_OK : (err == KErrNoMemory ? SQLITE_IOERR_NOMEM : SQLITE_IOERR);
  1732 	TInt sqliteErr = ::Os2SqliteErr(err, SQLITE_IOERR);
       
  1733 	SQLITE_TRACE_OS(OstTraceExt3(TRACE_INTERNALS, TFILEIO_TRUNCATE_EXIT, "OS-Exit;0x%X;TFileIo::Truncate;err=%d;sqliteErr=%d", (TUint)&dbFile, err, sqliteErr));
       
  1734 	return sqliteErr;
  1673 	}
  1735 	}
  1674 
  1736 
  1675 /**
  1737 /**
  1676 SQLite OS porting layer API.
  1738 SQLite OS porting layer API.
  1677 
  1739 
  1690 @see COsLayerData::SetOsErrorCode()
  1752 @see COsLayerData::SetOsErrorCode()
  1691 @see TDbFile
  1753 @see TDbFile
  1692 */
  1754 */
  1693 /* static */int TFileIo::Sync(sqlite3_file* aDbFile, int /* aFlags */)
  1755 /* static */int TFileIo::Sync(sqlite3_file* aDbFile, int /* aFlags */)
  1694 	{
  1756 	{
  1695 	SQLUTRACE_PROFILER(aDbFile);
       
  1696 	TDbFile& dbFile = ::DbFile(aDbFile);
  1757 	TDbFile& dbFile = ::DbFile(aDbFile);
       
  1758 	SQLITE_TRACE_OS(OstTrace1(TRACE_INTERNALS, TFILEIO_SYNC_ENTRY, "OS-Entry;0x%X;TFileIo::Sync", (TUint)&dbFile));
  1697 	__OS_CALL(EOsFileSync, 0, 0);
  1759 	__OS_CALL(EOsFileSync, 0, 0);
  1698     __COUNTER_INCR(TheSqlSrvProfilerFileSync);
  1760     __COUNTER_INCR(TheSqlSrvProfilerFileSync);
  1699 	__OSTIME_COUNTER(TheOsCallTicks[EOsFileSync], ::OsCallProfile(dbFile.iIsJournal, EOsFileSync), 0, 0, aDbFile, 0);
  1761 	__OSTIME_COUNTER(TheOsCallTicks[EOsFileSync], ::OsCallProfile(dbFile.iIsJournal, EOsFileSync), 0, 0, aDbFile, 0);
  1700 	if(dbFile.iReadOnly)
  1762 	if(dbFile.iReadOnly)
  1701 		{
  1763 		{
  1703 		return SQLITE_IOERR;
  1765 		return SQLITE_IOERR;
  1704 		}
  1766 		}
  1705 	__FS_CALL(EFsOpFileSync, 0);
  1767 	__FS_CALL(EFsOpFileSync, 0);
  1706 	TInt err = dbFile.iFileBuf.Flush();
  1768 	TInt err = dbFile.iFileBuf.Flush();
  1707 	COsLayerData::Instance().SetOsErrorCode(err);
  1769 	COsLayerData::Instance().SetOsErrorCode(err);
  1708 	return err == KErrNone ? SQLITE_OK : (err == KErrNoMemory ? SQLITE_IOERR_NOMEM : SQLITE_IOERR);
  1770 	TInt sqliteErr = ::Os2SqliteErr(err, SQLITE_IOERR);
       
  1771 	SQLITE_TRACE_OS(OstTraceExt3(TRACE_INTERNALS, TFILEIO_SYNC_EXIT, "OS-Exit;0x%X;TFileIo::Sync;err=%d;sqliteErr=%d", (TUint)&dbFile, err, sqliteErr));
       
  1772 	return sqliteErr;
  1709 	}
  1773 	}
  1710 
  1774 
  1711 /**
  1775 /**
  1712 SQLite OS porting layer API.
  1776 SQLite OS porting layer API.
  1713 
  1777 
  1726 @see COsLayerData::SetOsErrorCode()
  1790 @see COsLayerData::SetOsErrorCode()
  1727 @see TDbFile
  1791 @see TDbFile
  1728 */
  1792 */
  1729 /* static */ int TFileIo::FileSize(sqlite3_file* aDbFile, sqlite3_int64* aSize)
  1793 /* static */ int TFileIo::FileSize(sqlite3_file* aDbFile, sqlite3_int64* aSize)
  1730 	{
  1794 	{
  1731 	SQLUTRACE_PROFILER(aDbFile);
       
  1732 	TDbFile& dbFile = ::DbFile(aDbFile);
  1795 	TDbFile& dbFile = ::DbFile(aDbFile);
       
  1796 	SQLITE_TRACE_OS(OstTrace1(TRACE_INTERNALS, TFILEIO_FILESIZE_ENTRY, "OS-Entry;0x%X;TFileIo::FileSize", (TUint)&dbFile));
  1733 	__OS_CALL(EOsFileFileSize, 0, 0);
  1797 	__OS_CALL(EOsFileFileSize, 0, 0);
  1734 	__OSTIME_COUNTER(TheOsCallTicks[EOsFileFileSize], ::OsCallProfile(dbFile.iIsJournal, EOsFileFileSize), 0, 0, aDbFile, 0);
  1798 	__OSTIME_COUNTER(TheOsCallTicks[EOsFileFileSize], ::OsCallProfile(dbFile.iIsJournal, EOsFileFileSize), 0, 0, aDbFile, 0);
  1735 	__FS_CALL(EFsOpFileSize, 0);
  1799 	__FS_CALL(EFsOpFileSize, 0);
  1736 	TInt err =  dbFile.iFileBuf.Size(*aSize);
  1800 	TInt err =  dbFile.iFileBuf.Size(*aSize);
  1737 	COsLayerData::Instance().SetOsErrorCode(err);
  1801 	COsLayerData::Instance().SetOsErrorCode(err);
  1738 	if(err == KErrNone)
  1802 	TInt sqliteErr = ::Os2SqliteErr(err, SQLITE_IOERR);
  1739 		{
  1803 	SQLITE_TRACE_OS(OstTraceExt3(TRACE_INTERNALS, TFILEIO_FILESIZE_EXIT, "OS-Exit;0x%X;TFileIo::FileSize;err=%d;sqliteErr=%d", (TUint)&dbFile, err, sqliteErr));
  1740 		return SQLITE_OK;
  1804 	return sqliteErr;
  1741 		}
       
  1742 	return err == KErrNoMemory ? SQLITE_IOERR_NOMEM : SQLITE_IOERR;
       
  1743 	}
  1805 	}
  1744 
  1806 
  1745 /**
  1807 /**
  1746 SQLite OS porting layer API.
  1808 SQLite OS porting layer API.
  1747 
  1809 
  1760 	    
  1822 	    
  1761 @see TDbFile
  1823 @see TDbFile
  1762 */
  1824 */
  1763 /* static */ int TFileIo::Lock(sqlite3_file* aDbFile, int aLockType)
  1825 /* static */ int TFileIo::Lock(sqlite3_file* aDbFile, int aLockType)
  1764 	{
  1826 	{
  1765 	SQLUTRACE_PROFILER(aDbFile);
       
  1766 	TDbFile& dbFile = ::DbFile(aDbFile);
  1827 	TDbFile& dbFile = ::DbFile(aDbFile);
  1767 	__OS_CALL(EOsFileLock, 0, 0);
  1828 	__OS_CALL(EOsFileLock, 0, 0);
  1768 	__OSTIME_COUNTER(TheOsCallTicks[EOsFileLock], ::OsCallProfile(dbFile.iIsJournal, EOsFileLock), aLockType, 0, aDbFile, 0);
  1829 	__OSTIME_COUNTER(TheOsCallTicks[EOsFileLock], ::OsCallProfile(dbFile.iIsJournal, EOsFileLock), aLockType, 0, aDbFile, 0);
  1769 	//If there is already a lock of this type or more restrictive on the database file, do nothing.
  1830 	//If there is already a lock of this type or more restrictive on the database file, do nothing.
  1770 	if(dbFile.iLockType >= aLockType)
  1831 	if(dbFile.iLockType >= aLockType)
  1793 	    
  1854 	    
  1794 @see TDbFile
  1855 @see TDbFile
  1795 */
  1856 */
  1796 /* static */ int TFileIo::Unlock(sqlite3_file* aDbFile, int aLockType)
  1857 /* static */ int TFileIo::Unlock(sqlite3_file* aDbFile, int aLockType)
  1797 	{
  1858 	{
  1798 	SQLUTRACE_PROFILER(aDbFile);
       
  1799 	TDbFile& dbFile = ::DbFile(aDbFile);
  1859 	TDbFile& dbFile = ::DbFile(aDbFile);
  1800 	__OS_CALL(EOsFileUnlock, 0, 0);
  1860 	__OS_CALL(EOsFileUnlock, 0, 0);
  1801 	__OSTIME_COUNTER(TheOsCallTicks[EOsFileUnlock], ::OsCallProfile(dbFile.iIsJournal, EOsFileUnlock), aLockType, 0, aDbFile, 0);
  1861 	__OSTIME_COUNTER(TheOsCallTicks[EOsFileUnlock], ::OsCallProfile(dbFile.iIsJournal, EOsFileUnlock), aLockType, 0, aDbFile, 0);
  1802 	dbFile.iLockType = aLockType;
  1862 	dbFile.iLockType = aLockType;
  1803 	return SQLITE_OK;
  1863 	return SQLITE_OK;
  1822 	    
  1882 	    
  1823 @see TDbFile
  1883 @see TDbFile
  1824 */
  1884 */
  1825 /* static */ int TFileIo::CheckReservedLock(sqlite3_file* aDbFile, int *aResOut)
  1885 /* static */ int TFileIo::CheckReservedLock(sqlite3_file* aDbFile, int *aResOut)
  1826 	{
  1886 	{
  1827 	SQLUTRACE_PROFILER(aDbFile);
       
  1828 	TDbFile& dbFile = ::DbFile(aDbFile);
  1887 	TDbFile& dbFile = ::DbFile(aDbFile);
  1829 	__OS_CALL(EOsFileCheckReservedLock, 0, 0);
  1888 	__OS_CALL(EOsFileCheckReservedLock, 0, 0);
  1830 	__OSTIME_COUNTER(TheOsCallTicks[EOsFileCheckReservedLock], ::OsCallProfile(dbFile.iIsJournal, EOsFileCheckReservedLock), 0, 0, aDbFile, 0);
  1889 	__OSTIME_COUNTER(TheOsCallTicks[EOsFileCheckReservedLock], ::OsCallProfile(dbFile.iIsJournal, EOsFileCheckReservedLock), 0, 0, aDbFile, 0);
  1831 	*aResOut = dbFile.iLockType >= SQLITE_LOCK_RESERVED ? 1 : 0;
  1890 	*aResOut = dbFile.iLockType >= SQLITE_LOCK_RESERVED ? 1 : 0;
  1832   	return SQLITE_OK;
  1891   	return SQLITE_OK;
  1857 	    
  1916 	    
  1858 @see TDbFile
  1917 @see TDbFile
  1859 */
  1918 */
  1860 /* static */ int TFileIo::FileControl(sqlite3_file* aDbFile, int aOp, void* aArg)
  1919 /* static */ int TFileIo::FileControl(sqlite3_file* aDbFile, int aOp, void* aArg)
  1861 	{
  1920 	{
  1862 	SQLUTRACE_PROFILER(aDbFile);
       
  1863 	SYMBIAN_TRACE_SQL_EVENTS_ONLY(UTF::Printf(UTF::TTraceContext(UTF::EInternals), KFileFileCtr, aOp));
       
  1864 	TDbFile& dbFile = ::DbFile(aDbFile);
  1921 	TDbFile& dbFile = ::DbFile(aDbFile);
       
  1922 	SQLITE_TRACE_OS(OstTraceExt2(TRACE_INTERNALS, TFILEIO_FILECONTROL_ENTRY, "OS-Entry;0x%X;TFileIo::FileControl;aOp=%d", (TUint)&dbFile, aOp));
  1865 	__OS_CALL(EOsFileFileControl, 0, 0);
  1923 	__OS_CALL(EOsFileFileControl, 0, 0);
  1866 	__OSTIME_COUNTER(TheOsCallTicks[EOsFileFileControl], ::OsCallProfile(dbFile.iIsJournal, EOsFileFileControl), aOp, 0, aDbFile, 0);
  1924 	__OSTIME_COUNTER(TheOsCallTicks[EOsFileFileControl], ::OsCallProfile(dbFile.iIsJournal, EOsFileFileControl), aOp, 0, aDbFile, 0);
  1867 	TInt err = KErrNone;
  1925 	TInt err = KErrNone;
  1868 	switch(aOp)
  1926 	switch(aOp)
  1869 		{
  1927 		{
  1887 		default:
  1945 		default:
  1888 			err = KErrArgument;
  1946 			err = KErrArgument;
  1889 			break;
  1947 			break;
  1890 		}
  1948 		}
  1891 	COsLayerData::Instance().SetOsErrorCode(err);
  1949 	COsLayerData::Instance().SetOsErrorCode(err);
  1892 	return err == KErrNone ? SQLITE_OK : SQLITE_ERROR;
  1950 	TInt sqliteErr = ::Os2SqliteErr(err, SQLITE_ERROR);
       
  1951 	SQLITE_TRACE_OS(OstTraceExt3(TRACE_INTERNALS, TFILEIO_FILECONTROL_EXIT, "OS-Exit;0x%X;TFileIo::FileControl;err=%d;sqliteErr=%d", (TUint)&dbFile, err, sqliteErr));
       
  1952 	return sqliteErr;
  1893 	}
  1953 	}
  1894 
  1954 
  1895 /**
  1955 /**
  1896 SQLite OS porting layer API.
  1956 SQLite OS porting layer API.
  1897 
  1957 
  1909 @see TDbFile
  1969 @see TDbFile
  1910 @see TVfs::Open()
  1970 @see TVfs::Open()
  1911 */
  1971 */
  1912 /* static */ int TFileIo::SectorSize(sqlite3_file* aDbFile)
  1972 /* static */ int TFileIo::SectorSize(sqlite3_file* aDbFile)
  1913 	{
  1973 	{
  1914 	SQLUTRACE_PROFILER(aDbFile);
       
  1915 	TDbFile& dbFile = ::DbFile(aDbFile);
  1974 	TDbFile& dbFile = ::DbFile(aDbFile);
  1916 	__OS_CALL(EOsFileSectorSize, 0, 0);
  1975 	__OS_CALL(EOsFileSectorSize, 0, 0);
  1917 	__OSTIME_COUNTER(TheOsCallTicks[EOsFileSectorSize], ::OsCallProfile(dbFile.iIsJournal, EOsFileSectorSize), 0, 0, aDbFile, 0);
  1976 	__OSTIME_COUNTER(TheOsCallTicks[EOsFileSectorSize], ::OsCallProfile(dbFile.iIsJournal, EOsFileSectorSize), 0, 0, aDbFile, 0);
  1918 	__ASSERT_DEBUG(dbFile.iSectorSize > 0, User::Panic(KPanicCategory, EPanicInternalError));
  1977 	__ASSERT_DEBUG(dbFile.iSectorSize > 0, __SQLITEPANIC2(ESqliteOsPanicInternalError));
  1919 	if(dbFile.iSectorSize > 0)
  1978 	if(dbFile.iSectorSize > 0)
  1920 		{
  1979 		{
  1921 		return dbFile.iSectorSize;	
  1980 		return dbFile.iSectorSize;	
  1922 		}
  1981 		}
  1923 	return SQLITE_DEFAULT_SECTOR_SIZE;
  1982 	return SQLITE_DEFAULT_SECTOR_SIZE;
  1940 @see TDbFile
  1999 @see TDbFile
  1941 @see TVfs::Open()
  2000 @see TVfs::Open()
  1942 */
  2001 */
  1943 /* static */ int TFileIo::DeviceCharacteristics(sqlite3_file* aDbFile)
  2002 /* static */ int TFileIo::DeviceCharacteristics(sqlite3_file* aDbFile)
  1944 	{
  2003 	{
  1945 	SQLUTRACE_PROFILER(aDbFile);
       
  1946 	TDbFile& dbFile = ::DbFile(aDbFile);
  2004 	TDbFile& dbFile = ::DbFile(aDbFile);
  1947 	__OS_CALL(EOsFileDeviceCharacteristics, 0, 0);
  2005 	__OS_CALL(EOsFileDeviceCharacteristics, 0, 0);
  1948 	__OSTIME_COUNTER(TheOsCallTicks[EOsFileDeviceCharacteristics], ::OsCallProfile(dbFile.iIsJournal, EOsFileDeviceCharacteristics), 0, 0, aDbFile, 0);
  2006 	__OSTIME_COUNTER(TheOsCallTicks[EOsFileDeviceCharacteristics], ::OsCallProfile(dbFile.iIsJournal, EOsFileDeviceCharacteristics), 0, 0, aDbFile, 0);
  1949 	__ASSERT_DEBUG(dbFile.iDeviceCharacteristics >= 0, User::Panic(KPanicCategory, EPanicInternalError));
  2007 	__ASSERT_DEBUG(dbFile.iDeviceCharacteristics >= 0, __SQLITEPANIC2(ESqliteOsPanicInternalError));
  1950 	if(dbFile.iDeviceCharacteristics >= 0)
  2008 	if(dbFile.iDeviceCharacteristics >= 0)
  1951 		{
  2009 		{
  1952 		return dbFile.iDeviceCharacteristics;	
  2010 		return dbFile.iDeviceCharacteristics;	
  1953 		}
  2011 		}
  1954 	return 0;
  2012 	return 0;
  2001 @see TVfs::Open()
  2059 @see TVfs::Open()
  2002 */
  2060 */
  2003 /* static */ inline TInt TVfs::DoGetVolumeIoParamInfo(RFs& aFs, TInt aDriveNo, TVolumeIOParamInfo& aVolumeInfo)
  2061 /* static */ inline TInt TVfs::DoGetVolumeIoParamInfo(RFs& aFs, TInt aDriveNo, TVolumeIOParamInfo& aVolumeInfo)
  2004 	{
  2062 	{
  2005 	__FS_CALL(EFsOpFsVolumeIoParam, 0);
  2063 	__FS_CALL(EFsOpFsVolumeIoParam, 0);
  2006 	return aFs.VolumeIOParam(aDriveNo, aVolumeInfo);
  2064 	TInt err = aFs.VolumeIOParam(aDriveNo, aVolumeInfo); 
       
  2065 	SQLITE_TRACE_OS(OstTraceExt2(TRACE_INTERNALS, TVFS_DOGETVOLUMEIOPARAMINFO, "OS;0;TVfs::DoGetVolumeIoParamInfo;aDriveNo=%d;err=%d", aDriveNo, err));
       
  2066 	return err;		
  2007 	}
  2067 	}
  2008 
  2068 
  2009 /**
  2069 /**
  2010 Retrieves and returns in a bit set the device characteristics.
  2070 Retrieves and returns in a bit set the device characteristics.
  2011 
  2071 
  2089 		if(aVolumeInfo.iBlockSize > SQLITE_DEFAULT_SECTOR_SIZE && (aVolumeInfo.iBlockSize & (aVolumeInfo.iBlockSize - 1)) == 0)
  2149 		if(aVolumeInfo.iBlockSize > SQLITE_DEFAULT_SECTOR_SIZE && (aVolumeInfo.iBlockSize & (aVolumeInfo.iBlockSize - 1)) == 0)
  2090 			{
  2150 			{
  2091 			sectorSize = aVolumeInfo.iBlockSize;
  2151 			sectorSize = aVolumeInfo.iBlockSize;
  2092 			}
  2152 			}
  2093 		}
  2153 		}
  2094 	__ASSERT_DEBUG(sectorSize > 0 && (sectorSize & (sectorSize - 1)) == 0, User::Panic(KPanicCategory, EPanicInternalError));
  2154 	__ASSERT_DEBUG(sectorSize > 0 && (sectorSize & (sectorSize - 1)) == 0, __SQLITEPANIC2(ESqliteOsPanicInternalError));
  2095 	return sectorSize;
  2155 	return sectorSize;
  2096 	}
  2156 	}
  2097 
  2157 
  2098 /**
  2158 /**
  2099 Retrieves in a bit set the device characteristics of the device of the file referred by the aDbFile parameter.
  2159 Retrieves in a bit set the device characteristics of the device of the file referred by the aDbFile parameter.
  2118 @see TFileIo::DeviceCharacteristics()
  2178 @see TFileIo::DeviceCharacteristics()
  2119 @see TFileIo::SectorSize()
  2179 @see TFileIo::SectorSize()
  2120 */
  2180 */
  2121 /* static */ TInt TVfs::DoGetDeviceCharacteristicsAndSectorSize(TDbFile& aDbFile, TInt& aRecReadBufSize)
  2181 /* static */ TInt TVfs::DoGetDeviceCharacteristicsAndSectorSize(TDbFile& aDbFile, TInt& aRecReadBufSize)
  2122 	{
  2182 	{
  2123 	__ASSERT_DEBUG(aDbFile.iDeviceCharacteristics < 0, User::Panic(KPanicCategory, EPanicInternalError));
  2183 	__ASSERT_DEBUG(aDbFile.iDeviceCharacteristics < 0, __SQLITEPANIC2(ESqliteOsPanicInternalError));
  2124 	__ASSERT_DEBUG(aDbFile.iSectorSize <= 0, User::Panic(KPanicCategory, EPanicInternalError));
  2184 	__ASSERT_DEBUG(aDbFile.iSectorSize <= 0, __SQLITEPANIC2(ESqliteOsPanicInternalError));
  2125 	TInt driveNo;
  2185 	TInt driveNo;
  2126 	TDriveInfo driveInfo;
  2186 	TDriveInfo driveInfo;
  2127 	__FS_CALL(EFsOpFileDrive, 0);
  2187 	__FS_CALL(EFsOpFileDrive, 0);
  2128 	TInt err = aDbFile.iFileBuf.Drive(driveNo, driveInfo);
  2188 	TInt err = aDbFile.iFileBuf.Drive(driveNo, driveInfo);
  2129 	if(err != KErrNone)
  2189 	if(err != KErrNone)
  2137 		return err;	
  2197 		return err;	
  2138 		}
  2198 		}
  2139 	aDbFile.iDeviceCharacteristics = TVfs::DoGetDeviceCharacteristics(driveInfo, volumeInfo);
  2199 	aDbFile.iDeviceCharacteristics = TVfs::DoGetDeviceCharacteristics(driveInfo, volumeInfo);
  2140 	aDbFile.iSectorSize = TVfs::DoGetSectorSize(driveInfo, volumeInfo);
  2200 	aDbFile.iSectorSize = TVfs::DoGetSectorSize(driveInfo, volumeInfo);
  2141 	aRecReadBufSize = volumeInfo.iRecReadBufSize;
  2201 	aRecReadBufSize = volumeInfo.iRecReadBufSize;
       
  2202 	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));
  2142 	return KErrNone;
  2203 	return KErrNone;
  2143 	}
  2204 	}
  2144 
  2205 
  2145 /**
  2206 /**
  2146 SQLite OS porting layer API.
  2207 SQLite OS porting layer API.
  2192     if (err == KErrCorrupt || err == KErrEof || !IsMinFileSize)
  2253     if (err == KErrCorrupt || err == KErrEof || !IsMinFileSize)
  2193         {
  2254         {
  2194         COsLayerData& osLayerData = COsLayerData::Instance();
  2255         COsLayerData& osLayerData = COsLayerData::Instance();
  2195     
  2256     
  2196         aDbFile.iFileBuf.Close();
  2257         aDbFile.iFileBuf.Close();
  2197         (void) osLayerData.iFs.Delete(aFname);
  2258         __SQLITETRACE_OSEXPR(TInt err2 =) osLayerData.iFs.Delete(aFname);
       
  2259 		SQLITE_TRACE_OS(OstTraceExt4(TRACE_INTERNALS, TVFS_DOFILESIZECORRUPTIONCHECK1, "OS;0x%X;TVfs::DoFileSizeCorruptionCheck;size=%lld;err=%d;deleteErr=%d", (TUint)&aDbFile, size, err, err2));
  2198         err = aDbFile.iFileBuf.Create(osLayerData.iFs, aFname, aFmode);
  2260         err = aDbFile.iFileBuf.Create(osLayerData.iFs, aFname, aFmode);
       
  2261 		SQLITE_TRACE_OS(OstTraceExt2(TRACE_INTERNALS, TVFS_DOFILESIZECORRUPTIONCHECK2, "OS;0x%X;TVfs::DoFileSizeCorruptionCheck;createErr=%d", (TUint)&aDbFile, err));
  2199         }
  2262         }
  2200     return err;
  2263     return err;
  2201     }
  2264     }
  2202 
  2265 
  2203 /**
  2266 /**
  2232 @see COsLayerData::SetOsErrorCode()
  2295 @see COsLayerData::SetOsErrorCode()
  2233 @see TDbFile
  2296 @see TDbFile
  2234 */
  2297 */
  2235 /* static */ int TVfs::Open(sqlite3_vfs* aVfs, const char* aFileName, sqlite3_file* aDbFile, int aFlags, int* aOutFlags)
  2298 /* static */ int TVfs::Open(sqlite3_vfs* aVfs, const char* aFileName, sqlite3_file* aDbFile, int aFlags, int* aOutFlags)
  2236 	{
  2299 	{
  2237 	SQLUTRACE_PROFILER(aVfs);
       
  2238 	__OS_CALL(EOsVfsOpen, 0, 0);
  2300 	__OS_CALL(EOsVfsOpen, 0, 0);
  2239 	__OSTIME_COUNTER(TheOsCallTicks[EOsVfsOpen], ::OsCallProfile(EFalse, EOsVfsOpen), 0, 0, aDbFile, aFileName);
  2301 	__OSTIME_COUNTER(TheOsCallTicks[EOsVfsOpen], ::OsCallProfile(EFalse, EOsVfsOpen), 0, 0, aDbFile, aFileName);
  2240 	COsLayerData& osLayerData = COsLayerData::Instance();
  2302 	COsLayerData& osLayerData = COsLayerData::Instance();
  2241 	TFileName fname;
       
  2242 	if(aFileName && !::ConvertToUnicode(aFileName, fname))
       
  2243 		{
       
  2244 		osLayerData.SetOsErrorCode(KErrBadName);
       
  2245 		return SQLITE_CANTOPEN;	
       
  2246 		}
       
  2247 	SYMBIAN_TRACE_SQL_EVENTS_ONLY(UTF::Printf(UTF::TTraceContext(UTF::EInternals), KFileOpen, aDbFile, &fname));
       
  2248 	new (aDbFile) TDbFile;
  2303 	new (aDbFile) TDbFile;
  2249 	TDbFile& dbFile = ::DbFile(aDbFile);
  2304 	TDbFile& dbFile = ::DbFile(aDbFile);
       
  2305     if(!aFileName)
       
  2306         {  
       
  2307         //It is to create and open a temp file if aFileName is NULL. In this case,
       
  2308         //we will defer the file creation util it is needed.  
       
  2309         dbFile.pMethods = &TheFileIoApi;
       
  2310         dbFile.iIsFileCreated = EFalse;
       
  2311         if(aOutFlags)
       
  2312             {
       
  2313             *aOutFlags = SQLITE_OPEN_READWRITE;
       
  2314             }
       
  2315 		osLayerData.SetOsErrorCode(KErrNone);
       
  2316         return SQLITE_OK;
       
  2317         }  
       
  2318 	TFileName fname;
       
  2319     if(!::ConvertToUnicode(aFileName, fname))
       
  2320 		{
       
  2321         osLayerData.SetOsErrorCode(KErrBadName);
       
  2322         return SQLITE_CANTOPEN;	
       
  2323     	}
       
  2324 	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));
  2250 	TFhStrType fhStrType = aFileName ? ::FhStringProps(aFileName) : ENotFhStr;
  2325 	TFhStrType fhStrType = aFileName ? ::FhStringProps(aFileName) : ENotFhStr;
  2251 	if(aFileName && (aFlags & SQLITE_OPEN_DELETEONCLOSE))
       
  2252 		{
       
  2253 		dbFile.iFullName = fname.Alloc();
       
  2254 		if(!dbFile.iFullName)
       
  2255 			{
       
  2256 			osLayerData.SetOsErrorCode(KErrNoMemory);
       
  2257 			return SQLITE_IOERR_NOMEM;
       
  2258 			}
       
  2259 		}
       
  2260 	TInt recReadBufSize = -1;
       
  2261 	TInt err = KErrNone;
  2326 	TInt err = KErrNone;
  2262 	if(fhStrType == EFhMainDbStr)
  2327 	if(fhStrType == EFhMainDbStr)
  2263 		{//Main db file, open from handle
  2328 		{//Main db file, open from handle
  2264 		const RMessage2* msg;
  2329 		const RMessage2* msg;
  2265 		TBool readOnly;
  2330 		TBool readOnly;
  2279 			}
  2344 			}
  2280 		if(aFlags & SQLITE_OPEN_EXCLUSIVE)
  2345 		if(aFlags & SQLITE_OPEN_EXCLUSIVE)
  2281 			{
  2346 			{
  2282 			fmode |= EFileShareExclusive;
  2347 			fmode |= EFileShareExclusive;
  2283 			}
  2348 			}
  2284 		if(!aFileName)	
  2349 		if(aFlags & SQLITE_OPEN_DELETEONCLOSE)
  2285 			{
  2350 			{
  2286 			__FS_CALL(EFsOpFileCreateTemp, 0);
  2351 			fmode |= EDeleteOnClose;
  2287 			err = dbFile.iFileBuf.Temp(osLayerData.iFs, osLayerData.iSysPrivDir, fname, fmode);
  2352 			}
  2288 			if(err == KErrNone)
  2353 		err = KErrAccessDenied;
       
  2354 		TInt prevErr = KErrNone;
       
  2355 		if(aFlags & SQLITE_OPEN_CREATE)
       
  2356 			{
       
  2357 			__FS_CALL(EFsOpFileCreate, 0);
       
  2358 			prevErr = err = dbFile.iFileBuf.Create(osLayerData.iFs, fname, fmode);
       
  2359 			}
       
  2360 		if(err != KErrNone && err != KErrNoMemory && err != KErrDiskFull)
       
  2361 			{
       
  2362 			__FS_CALL(EFsOpFileOpen, 0);
       
  2363 			err = dbFile.iFileBuf.Open(osLayerData.iFs, fname, fmode);
       
  2364 
       
  2365 			if(err == KErrNone && (aFlags & KJournalFileTypeBitMask))
  2289 				{
  2366 				{
  2290 				dbFile.iFullName = fname.Alloc();
  2367 				err = TVfs::DoFileSizeCorruptionCheck(dbFile, fname, fmode);
  2291 				if(!dbFile.iFullName)
       
  2292 					{
       
  2293 					err = KErrNoMemory;	
       
  2294 					}
       
  2295 				}
  2368 				}
  2296 			}
  2369 			}
  2297 		else
  2370 		if((err != KErrNone && err != KErrNoMemory && err != KErrDiskFull) && (aFlags & SQLITE_OPEN_READWRITE))
       
  2371 			{
       
  2372 			aFlags &= ~SQLITE_OPEN_READWRITE;
       
  2373 			aFlags |= SQLITE_OPEN_READONLY;
       
  2374 			fmode &= ~EFileWrite;
       
  2375 			__FS_CALL(EFsOpFileOpen, 0);
       
  2376 			err = dbFile.iFileBuf.Open(osLayerData.iFs, fname, fmode);
       
  2377 			}
       
  2378 		if(err != KErrNone && prevErr == KErrAccessDenied)
  2298 			{
  2379 			{
  2299 			err = KErrAccessDenied;
  2380 			err = KErrAccessDenied;
  2300 			TInt prevErr = KErrNone;
  2381 			}
  2301 			if(aFlags & SQLITE_OPEN_CREATE)
  2382 		}
  2302 				{
  2383 	TInt recReadBufSize = -1;
  2303 				__FS_CALL(EFsOpFileCreate, 0);
       
  2304 				prevErr = err = dbFile.iFileBuf.Create(osLayerData.iFs, fname, fmode);
       
  2305 				}
       
  2306 			if(err != KErrNone && err != KErrNoMemory && err != KErrDiskFull)
       
  2307 				{
       
  2308 				__FS_CALL(EFsOpFileOpen, 0);
       
  2309 				err = dbFile.iFileBuf.Open(osLayerData.iFs, fname, fmode);
       
  2310 				
       
  2311 				if(err == KErrNone && (aFlags & KJournalFileTypeBitMask))
       
  2312 				    {
       
  2313                     err = TVfs::DoFileSizeCorruptionCheck(dbFile, fname, fmode);
       
  2314 				    }
       
  2315 				}
       
  2316 			if((err != KErrNone && err != KErrNoMemory && err != KErrDiskFull) && (aFlags & SQLITE_OPEN_READWRITE))
       
  2317 				{
       
  2318 				aFlags &= ~SQLITE_OPEN_READWRITE;
       
  2319 				aFlags |= SQLITE_OPEN_READONLY;
       
  2320 				fmode &= ~EFileWrite;
       
  2321 				__FS_CALL(EFsOpFileOpen, 0);
       
  2322    				err = dbFile.iFileBuf.Open(osLayerData.iFs, fname, fmode);
       
  2323 				}
       
  2324 			if(err != KErrNone && prevErr == KErrAccessDenied)
       
  2325 				{
       
  2326 				err = KErrAccessDenied;
       
  2327 				}
       
  2328 			}
       
  2329 		}
       
  2330 	if(err == KErrNone)
  2384 	if(err == KErrNone)
  2331 		{
  2385 		{
  2332 		err = TVfs::DoGetDeviceCharacteristicsAndSectorSize(dbFile, recReadBufSize);
  2386 		err = TVfs::DoGetDeviceCharacteristicsAndSectorSize(dbFile, recReadBufSize);
  2333 		}
  2387 		}
  2334 	osLayerData.SetOsErrorCode(err);
  2388 	osLayerData.SetOsErrorCode(err);
  2335 	if(err != KErrNone)
  2389 	if(err != KErrNone)
  2336 		{
  2390 		{
  2337 		__FS_CALL(EFsOpFileClose, 0);
  2391 		__FS_CALL(EFsOpFileClose, 0);
  2338 		dbFile.iFileBuf.Close();	
  2392 		dbFile.iFileBuf.Close();	
  2339 		delete dbFile.iFullName;
       
  2340 		dbFile.iFullName = NULL;
       
  2341         if(!aFileName && fname.Length() > 0)
       
  2342             {//temporary file, the error is not KErrNone. Then delete the file (after a successfull 
       
  2343              //temporary file creation there could be a failed memory allocation)
       
  2344             (void)osLayerData.iFs.Delete(fname);
       
  2345             }
       
  2346 		}
  2393 		}
  2347 	else
  2394 	else
  2348 		{
  2395 		{
  2349 		dbFile.pMethods = &TheFileIoApi;
  2396 		dbFile.pMethods = &TheFileIoApi;
  2350 		if(fhStrType != EFhMainDbStr)
  2397 		if(fhStrType != EFhMainDbStr)
  2358 		(void)dbFile.iFileBuf.SetReadAheadSize(dbFile.iSectorSize, recReadBufSize);
  2405 		(void)dbFile.iFileBuf.SetReadAheadSize(dbFile.iSectorSize, recReadBufSize);
  2359 		}
  2406 		}
  2360 #ifdef _SQLPROFILER
  2407 #ifdef _SQLPROFILER
  2361 	dbFile.iIsJournal = aFlags & KJournalFileTypeBitMask; 
  2408 	dbFile.iIsJournal = aFlags & KJournalFileTypeBitMask; 
  2362 #endif
  2409 #endif
  2363 	return err == KErrNone ? SQLITE_OK : (err == KErrNoMemory ? SQLITE_IOERR_NOMEM : SQLITE_CANTOPEN);
  2410 	TInt sqliteErr = ::Os2SqliteErr(err, SQLITE_CANTOPEN);
       
  2411 	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));
       
  2412 	return sqliteErr;
  2364 	}
  2413 	}
  2365 
  2414 
  2366 /**
  2415 /**
  2367 SQLite OS porting layer API.
  2416 SQLite OS porting layer API.
  2368 
  2417 
  2381 
  2430 
  2382 @see COsLayerData::SetOsErrorCode()
  2431 @see COsLayerData::SetOsErrorCode()
  2383 */
  2432 */
  2384 /* static */ int TVfs::Delete(sqlite3_vfs* aVfs, const char* aFileName, int /*aSyncDir*/)
  2433 /* static */ int TVfs::Delete(sqlite3_vfs* aVfs, const char* aFileName, int /*aSyncDir*/)
  2385 	{
  2434 	{
  2386 	SQLUTRACE_PROFILER(aVfs);
       
  2387 	__OS_CALL(EOsVfsDelete, 0, 0);
  2435 	__OS_CALL(EOsVfsDelete, 0, 0);
  2388 	__OSTIME_COUNTER(TheOsCallTicks[EOsVfsDelete], ::OsCallProfile(EFalse, EOsVfsDelete), 0, 0, 0, aFileName);
  2436 	__OSTIME_COUNTER(TheOsCallTicks[EOsVfsDelete], ::OsCallProfile(EFalse, EOsVfsDelete), 0, 0, 0, aFileName);
  2389 	COsLayerData& osLayerData = COsLayerData::Instance();
  2437 	COsLayerData& osLayerData = COsLayerData::Instance();
  2390 	TBuf<KMaxFileName + 1> fname;
  2438 	TBuf<KMaxFileName + 1> fname;
  2391 	if(!::ConvertToUnicode(aFileName, fname))
  2439 	if(!::ConvertToUnicode(aFileName, fname))
  2392 		{
  2440 		{
  2393 		osLayerData.SetOsErrorCode(KErrBadName);
  2441 		osLayerData.SetOsErrorCode(KErrBadName);
       
  2442 		SQLITE_TRACE_OS(OstTrace0(TRACE_INTERNALS, TVFS_DELETE1, "OS;0;TVfs::Delete;err=KErrBadName"));
  2394 		return SQLITE_ERROR;	
  2443 		return SQLITE_ERROR;	
  2395 		}
  2444 		}
  2396 	SYMBIAN_TRACE_SQL_EVENTS_ONLY(UTF::Printf(UTF::TTraceContext(UTF::EInternals), KFileName, &fname));
  2445 	SQLITE_TRACE_OS(OstTraceExt1(TRACE_INTERNALS, TVFS_DELETE_ENTRY, "OS-Entry;0;TVfs::Delete;fname=%S", __SQLITEPRNSTR(fname)));
  2397 	TFhStrType fhStrType = FhStringProps(aFileName);
  2446 	TFhStrType fhStrType = FhStringProps(aFileName);
  2398 	if(fhStrType == EFhMainDbStr)
  2447 	if(fhStrType == EFhMainDbStr)
  2399 		{//Deleting files not in your own private data cage - not allowed!
  2448 		{//Deleting files not in your own private data cage - not allowed!
  2400 		osLayerData.SetOsErrorCode(KErrPermissionDenied);
  2449 		osLayerData.SetOsErrorCode(KErrPermissionDenied);
       
  2450 		SQLITE_TRACE_OS(OstTraceExt1(TRACE_INTERNALS, TVFS_DELETE2, "OS;0;TVfs::Delete;err=KErrPermissionDenied;fname=%S", __SQLITEPRNSTR(fname)));
  2401 		return SQLITE_ERROR;	
  2451 		return SQLITE_ERROR;	
  2402 		}
  2452 		}
  2403 	if(fhStrType == EFhStr)
  2453 	if(fhStrType == EFhStr)
  2404 		{
  2454 		{
  2405 		::FhConvertToFileName(fname, osLayerData.iSysPrivDir);//If fname does not have a path, iSysPrivDir will be used
  2455 		::FhConvertToFileName(fname, osLayerData.iSysPrivDir);//If fname does not have a path, iSysPrivDir will be used
  2406 		}
  2456 		}
  2407 	__FS_CALL(EFsOpFileDelete, 0);
  2457 	__FS_CALL(EFsOpFileDelete, 0);
  2408 	TInt err = osLayerData.iFs.Delete(fname);
  2458 	TInt err = osLayerData.iFs.Delete(fname);
  2409 	osLayerData.SetOsErrorCode(err);
  2459 	osLayerData.SetOsErrorCode(err);
  2410 	return err == KErrNone ? SQLITE_OK : (err == KErrNoMemory ? SQLITE_IOERR_NOMEM : SQLITE_IOERR_DELETE);
  2460 	TInt sqliteErr = ::Os2SqliteErr(err, SQLITE_IOERR_DELETE);	
       
  2461 	SQLITE_TRACE_OS(OstTraceExt2(TRACE_INTERNALS, TVFS_DELETE_EXIT, "OS-Exit;0;TVfs::Delete;err=%d;sqliteErr=%d", err, sqliteErr));
       
  2462 	return sqliteErr;
  2411 	}
  2463 	}
  2412 
  2464 
  2413 /**
  2465 /**
  2414 SQLite OS porting layer API.
  2466 SQLite OS porting layer API.
  2415 
  2467 
  2429 
  2481 
  2430 @see COsLayerData::SetOsErrorCode()
  2482 @see COsLayerData::SetOsErrorCode()
  2431 */
  2483 */
  2432 /* static */ int TVfs::Access(sqlite3_vfs* aVfs, const char* aFileName, int aFlags, int* aResOut)
  2484 /* static */ int TVfs::Access(sqlite3_vfs* aVfs, const char* aFileName, int aFlags, int* aResOut)
  2433 	{
  2485 	{
  2434 	SQLUTRACE_PROFILER(aVfs);
       
  2435 	__OS_CALL(EOsVfsAccess, 0, 0);
  2486 	__OS_CALL(EOsVfsAccess, 0, 0);
  2436 	__OSTIME_COUNTER(TheOsCallTicks[EOsVfsAccess], ::OsCallProfile(EFalse, EOsVfsAccess), aFlags, 0, 0, aFileName);
  2487 	__OSTIME_COUNTER(TheOsCallTicks[EOsVfsAccess], ::OsCallProfile(EFalse, EOsVfsAccess), aFlags, 0, 0, aFileName);
  2437 	COsLayerData& osLayerData = COsLayerData::Instance();
  2488 	COsLayerData& osLayerData = COsLayerData::Instance();
  2438 	TBuf<KMaxFileName + 1> fname;
  2489 	TBuf<KMaxFileName + 1> fname;
  2439 	if(!::ConvertToUnicode(aFileName, fname))
  2490 	if(!::ConvertToUnicode(aFileName, fname))
  2440 		{
  2491 		{
  2441 		osLayerData.SetOsErrorCode(KErrGeneral);
  2492 		osLayerData.SetOsErrorCode(KErrGeneral);
       
  2493 		SQLITE_TRACE_OS(OstTrace0(TRACE_INTERNALS, TVFS_ACCESS1, "OS;0;TVfs::Access;err=KErrGeneral"));
  2442 		return SQLITE_IOERR_ACCESS;
  2494 		return SQLITE_IOERR_ACCESS;
  2443 		}
  2495 		}
  2444 	SYMBIAN_TRACE_SQL_EVENTS_ONLY(UTF::Printf(UTF::TTraceContext(UTF::EInternals), KFileName, &fname));
  2496 	SQLITE_TRACE_OS(OstTraceExt2(TRACE_INTERNALS, TVFS_ACCESS_ENTRY, "OS-Entry;0;TVfs::Access;fname=%S;aFlags=0x%X", __SQLITEPRNSTR(fname), (TUint)aFlags));
  2445 	TFhStrType fhStrType = ::FhStringProps(aFileName);
  2497 	TFhStrType fhStrType = ::FhStringProps(aFileName);
  2446 	if(fhStrType == EFhStr)
  2498 	if(fhStrType == EFhStr)
  2447 		{
  2499 		{
  2448 		::FhConvertToFileName(fname, osLayerData.iSysPrivDir);//If fname does not have a path, iSysPrivDir will be used
  2500 		::FhConvertToFileName(fname, osLayerData.iSysPrivDir);//If fname does not have a path, iSysPrivDir will be used
  2449 		}
  2501 		}
  2452 	TInt err = osLayerData.iFs.Entry(fname, entry);
  2504 	TInt err = osLayerData.iFs.Entry(fname, entry);
  2453 	if(aFlags == SQLITE_ACCESS_EXISTS && err == KErrNotFound)
  2505 	if(aFlags == SQLITE_ACCESS_EXISTS && err == KErrNotFound)
  2454 		{
  2506 		{
  2455 		osLayerData.SetOsErrorCode(KErrNone);
  2507 		osLayerData.SetOsErrorCode(KErrNone);
  2456 		*aResOut = 0;
  2508 		*aResOut = 0;
       
  2509 		SQLITE_TRACE_OS(OstTrace0(TRACE_INTERNALS, TVFS_ACCESS_EXIT1, "OS-Exit;0;TVfs::Access;Exists-NoFound"));
  2457 		return SQLITE_OK;
  2510 		return SQLITE_OK;
  2458 		}
  2511 		}
  2459 	if(err != KErrNone)
  2512 	if(err != KErrNone)
  2460 		{
  2513 		{
  2461 		osLayerData.SetOsErrorCode(err);
  2514 		osLayerData.SetOsErrorCode(err);
       
  2515 		SQLITE_TRACE_OS(OstTrace1(TRACE_INTERNALS, TVFS_ACCESS_EXIT2, "OS-Exit;0;TVfs::Access;err=%d", err));
  2462 		return err == KErrNoMemory ? SQLITE_IOERR_NOMEM : SQLITE_IOERR_ACCESS;
  2516 		return err == KErrNoMemory ? SQLITE_IOERR_NOMEM : SQLITE_IOERR_ACCESS;
  2463 		}
  2517 		}
  2464 	*aResOut = 0;
  2518 	*aResOut = 0;
  2465 	switch(aFlags)
  2519 	switch(aFlags)
  2466 		{
  2520 		{
  2475 			break;
  2529 			break;
  2476 		default:
  2530 		default:
  2477 			break;			
  2531 			break;			
  2478 		}
  2532 		}
  2479 	osLayerData.SetOsErrorCode(KErrNone);
  2533 	osLayerData.SetOsErrorCode(KErrNone);
       
  2534 	SQLITE_TRACE_OS(OstTrace1(TRACE_INTERNALS, TVFS_ACCESS_EXIT3, "OS-Exit;0;TVfs::Access;aResOut=%d", *aResOut));
  2480 	return SQLITE_OK;
  2535 	return SQLITE_OK;
  2481 	}
  2536 	}
  2482 
  2537 
  2483 /**
  2538 /**
  2484 SQLite OS porting layer API.
  2539 SQLite OS porting layer API.
  2506 @return SQLITE_ERROR, The aRelative parameter is NULL or cannot be converted to UTF16;
  2561 @return SQLITE_ERROR, The aRelative parameter is NULL or cannot be converted to UTF16;
  2507 		SQLITE_OK The operation has completed successfully.
  2562 		SQLITE_OK The operation has completed successfully.
  2508 */
  2563 */
  2509 /* static */ int TVfs::FullPathName(sqlite3_vfs* aVfs, const char* aRelative, int aBufLen, char* aBuf)
  2564 /* static */ int TVfs::FullPathName(sqlite3_vfs* aVfs, const char* aRelative, int aBufLen, char* aBuf)
  2510 	{
  2565 	{
  2511 	SQLUTRACE_PROFILER(aVfs);
       
  2512 	__OS_CALL(EOsVfsFullPathName, 0, 0);
  2566 	__OS_CALL(EOsVfsFullPathName, 0, 0);
  2513 	__OSTIME_COUNTER(TheOsCallTicks[EOsVfsFullPathName], ::OsCallProfile(EFalse, EOsVfsFullPathName), aBufLen, 0, 0, aRelative);
  2567 	__OSTIME_COUNTER(TheOsCallTicks[EOsVfsFullPathName], ::OsCallProfile(EFalse, EOsVfsFullPathName), aBufLen, 0, 0, aRelative);
  2514 	COsLayerData& osLayerData = COsLayerData::Instance();
  2568 	COsLayerData& osLayerData = COsLayerData::Instance();
  2515 	osLayerData.StoreFhData(NULL, EFalse);
  2569 	osLayerData.StoreFhData(NULL, EFalse);
  2516 	//Convert the received file name to UTF16
  2570 	//Convert the received file name to UTF16
  2517 	TBuf<KMaxFileName + 1> fname;
  2571 	TBuf<KMaxFileName + 1> fname;
  2518 	if(!::ConvertToUnicode(aRelative, fname))
  2572 	if(!::ConvertToUnicode(aRelative, fname))
  2519 		{
  2573 		{
       
  2574 		SQLITE_TRACE_OS(OstTrace0(TRACE_INTERNALS, TVFS_FULLPATHNAME1, "OS;0;TVfs::FullPathName;err=SQLITE_ERROR"));
  2520 		return SQLITE_ERROR;
  2575 		return SQLITE_ERROR;
  2521 		}
  2576 		}
  2522 	SYMBIAN_TRACE_SQL_EVENTS_ONLY(UTF::Printf(UTF::TTraceContext(UTF::EInternals), KFileName, &fname));
  2577 	SQLITE_TRACE_OS(OstTraceExt2(TRACE_INTERNALS, TVFS_FULLPATHNAME_ENTRY, "OS-Entry;0;TVfs::FullPathName;fname=%S;aBufLen=%d", __SQLITEPRNSTR(fname), aBufLen));
  2523 	//Zero-terminate the converted file name
  2578 	//Zero-terminate the converted file name
  2524 	fname.Append(TChar(0));
  2579 	fname.Append(TChar(0));
  2525 	TParse parse;
  2580 	TParse parse;
  2526 	TFhStrType strType = ::FhStringProps(aRelative);//Detect string type - it may not be a real file name
  2581 	TFhStrType strType = ::FhStringProps(aRelative);//Detect string type - it may not be a real file name
  2527 	if(strType == EFhMainDbStr)
  2582 	if(strType == EFhMainDbStr)
  2536 		}
  2591 		}
  2537 	TPtr8 dest8(reinterpret_cast <TUint8*> (aBuf), aBufLen);	
  2592 	TPtr8 dest8(reinterpret_cast <TUint8*> (aBuf), aBufLen);	
  2538 	if(!::ConvertFromUnicode(parse.FullName(), dest8))
  2593 	if(!::ConvertFromUnicode(parse.FullName(), dest8))
  2539 		{//Zero the stored fh data, because it has been initialized by the FhExtractAndStore(fname) call (couple of lines above)
  2594 		{//Zero the stored fh data, because it has been initialized by the FhExtractAndStore(fname) call (couple of lines above)
  2540 		osLayerData.StoreFhData(NULL, EFalse);
  2595 		osLayerData.StoreFhData(NULL, EFalse);
       
  2596 		SQLITE_TRACE_OS(OstTrace0(TRACE_INTERNALS, TVFS_FULLPATHNAME_EXIT1, "OS-Exit;0;TVfs::FullPathName;err=SQLITE_ERROR"));
  2541 		return SQLITE_ERROR;	
  2597 		return SQLITE_ERROR;	
  2542 		}
  2598 		}
       
  2599 	SQLITE_TRACE_OS(OstTrace0(TRACE_INTERNALS, TVFS_FULLPATHNAME_EXIT2, "OS-Exit;0;TVfs::FullPathName;err=SQLITE_OK"));
  2543 	return SQLITE_OK;
  2600 	return SQLITE_OK;
  2544 	}
  2601 	}
  2545 
  2602 
  2546 /**
  2603 /**
  2547 SQLite OS porting layer API.
  2604 SQLite OS porting layer API.
  2553 
  2610 
  2554 @return The length of the used part of the output buffer.
  2611 @return The length of the used part of the output buffer.
  2555 */
  2612 */
  2556 /* static */ int TVfs::Randomness(sqlite3_vfs* aVfs, int aBufLen, char* aBuf)
  2613 /* static */ int TVfs::Randomness(sqlite3_vfs* aVfs, int aBufLen, char* aBuf)
  2557 	{
  2614 	{
  2558 	SQLUTRACE_PROFILER(aVfs);
       
  2559 	__OS_CALL(EOsVfsRandomness, 0, 0);
  2615 	__OS_CALL(EOsVfsRandomness, 0, 0);
  2560 	__OSTIME_COUNTER(TheOsCallTicks[EOsVfsRandomness], ::OsCallProfile(EFalse, EOsVfsRandomness), aBufLen, 0, 0, 0);
  2616 	__OSTIME_COUNTER(TheOsCallTicks[EOsVfsRandomness], ::OsCallProfile(EFalse, EOsVfsRandomness), aBufLen, 0, 0, 0);
  2561 	COsLayerData& osLayerData = COsLayerData::Instance();
  2617 	COsLayerData& osLayerData = COsLayerData::Instance();
  2562 	const TInt KRandIterations = aBufLen / sizeof(int);
  2618 	const TInt KRandIterations = aBufLen / sizeof(int);
  2563 	for(TInt i=0;i<KRandIterations;++i)
  2619 	for(TInt i=0;i<KRandIterations;++i)
  2577 
  2633 
  2578 @return The aMicrosec value.
  2634 @return The aMicrosec value.
  2579 */
  2635 */
  2580 /* static */ int TVfs::Sleep(sqlite3_vfs* aVfs, int aMicrosec)
  2636 /* static */ int TVfs::Sleep(sqlite3_vfs* aVfs, int aMicrosec)
  2581 	{
  2637 	{
  2582 	SQLUTRACE_PROFILER(aVfs);
       
  2583 	__OS_CALL(EOsVfsSleep, 0, 0);
  2638 	__OS_CALL(EOsVfsSleep, 0, 0);
  2584 	__OSTIME_COUNTER(TheOsCallTicks[EOsVfsSleep], ::OsCallProfile(EFalse, EOsVfsSleep), aMicrosec, 0, 0, 0);
  2639 	__OSTIME_COUNTER(TheOsCallTicks[EOsVfsSleep], ::OsCallProfile(EFalse, EOsVfsSleep), aMicrosec, 0, 0, 0);
  2585 	User::AfterHighRes(TTimeIntervalMicroSeconds32(aMicrosec));
  2640 	User::AfterHighRes(TTimeIntervalMicroSeconds32(aMicrosec));
  2586 	return aMicrosec;
  2641 	return aMicrosec;
  2587 	}
  2642 	}
  2597 
  2652 
  2598 @return 0.
  2653 @return 0.
  2599 */
  2654 */
  2600 /* static */ int TVfs::CurrentTime(sqlite3_vfs* aVfs, double* aNow)
  2655 /* static */ int TVfs::CurrentTime(sqlite3_vfs* aVfs, double* aNow)
  2601 	{
  2656 	{
  2602 	SQLUTRACE_PROFILER(aVfs);
       
  2603 	__OS_CALL(EOsVfsCurrentTime, 0, 0);
  2657 	__OS_CALL(EOsVfsCurrentTime, 0, 0);
  2604 	__OSTIME_COUNTER(TheOsCallTicks[EOsVfsCurrentTime], ::OsCallProfile(EFalse, EOsVfsCurrentTime), 0, 0, 0, 0);
  2658 	__OSTIME_COUNTER(TheOsCallTicks[EOsVfsCurrentTime], ::OsCallProfile(EFalse, EOsVfsCurrentTime), 0, 0, 0, 0);
  2605 	TTime now;
  2659 	TTime now;
  2606 	now.UniversalTime();
  2660 	now.UniversalTime();
  2607 	TDateTime date = now.DateTime();
  2661 	TDateTime date = now.DateTime();
  2633 
  2687 
  2634 @return 0.
  2688 @return 0.
  2635 */
  2689 */
  2636 /* static */int TVfs::GetLastError(sqlite3_vfs* aVfs, int /*aBufLen*/, char* /*aBuf*/)
  2690 /* static */int TVfs::GetLastError(sqlite3_vfs* aVfs, int /*aBufLen*/, char* /*aBuf*/)
  2637 	{
  2691 	{
  2638 	SQLUTRACE_PROFILER(aVfs);
       
  2639 	__OS_CALL(EOsVfsGetLastError, 0, 0);
  2692 	__OS_CALL(EOsVfsGetLastError, 0, 0);
  2640 	__OSTIME_COUNTER(TheOsCallTicks[EOsVfsGetLastError], ::OsCallProfile(EFalse, EOsVfsGetLastError), 0, 0, 0, 0);
  2693 	__OSTIME_COUNTER(TheOsCallTicks[EOsVfsGetLastError], ::OsCallProfile(EFalse, EOsVfsGetLastError), 0, 0, 0, 0);
  2641 	return 0;
  2694 	return 0;
  2642 	}
  2695 	}
  2643 
  2696