persistentstorage/sqlite3api/OsLayer/os_symbian_mt.cpp
branchRCL_3
changeset 24 cc28652e0254
parent 23 26645d81f48d
equal deleted inserted replaced
23:26645d81f48d 24:cc28652e0254
    34 	#include "os.h"
    34 	#include "os.h"
    35 	#include "os_common.h"
    35 	#include "os_common.h"
    36 	}
    36 	}
    37 #include <e32math.h>
    37 #include <e32math.h>
    38 #include "os_symbian.h"
    38 #include "os_symbian.h"
    39 #include "SqliteUtil.h"
    39 #include "UTraceSqlite.h"
    40 #include "OstTraceDefinitions.h"
       
    41 #ifdef OST_TRACE_COMPILER_IN_USE
       
    42 #include "os_symbian_mtTraces.h"
       
    43 #endif
       
    44 #include "SqliteTraceDef.h"
       
    45 
       
    46 //Bit-mask constant. If xOpen()'s "aFlag" parameter contains one of these bits set, then the the file top be
       
    47 //opened or created is a journal file.
       
    48 const TUint KJournalFileTypeBitMask = SQLITE_OPEN_MAIN_JOURNAL | SQLITE_OPEN_TEMP_JOURNAL | SQLITE_OPEN_SUBJOURNAL | SQLITE_OPEN_MASTER_JOURNAL; 
       
    49 
    40 
    50 #ifdef SQLITE_TEST
    41 #ifdef SQLITE_TEST
    51 
    42 
    52 //Count the number of fullsyncs and normal syncs.  This is used to test
    43 //Count the number of fullsyncs and normal syncs.  This is used to test
    53 //that syncs and fullsyncs are occuring at the right times.
    44 //that syncs and fullsyncs are occuring at the right times.
   103 			return SQLITE_IOERR_NOMEM;
    94 			return SQLITE_IOERR_NOMEM;
   104 		case KErrDiskFull:
    95 		case KErrDiskFull:
   105 			return SQLITE_FULL;
    96 			return SQLITE_FULL;
   106 		default:
    97 		default:
   107 #ifdef _DEBUG		
    98 #ifdef _DEBUG		
   108 			SQLITE_TRACE_OS(OstTrace1(TRACE_INTERNALS, OS2SQLITEERR, "OS;0;Os2SqliteErr;aOsErr=%d", aOsErr));
    99 			RDebug::Print(_L("SQLite3 C API, Os2SqliteErr(), err=%d\n"), aOsErr);
   109 #endif			
   100 #endif			
   110 			break;
   101 			break;
   111 		}
   102 		}
   112 	return aDefaultErr;
   103 	return aDefaultErr;
   113 	}
   104 	}
   132 		}
   123 		}
   133 	if(err != KErrNone)
   124 	if(err != KErrNone)
   134 		{
   125 		{
   135 		iFs.Close();	
   126 		iFs.Close();	
   136 		}
   127 		}
   137 	SQLITE_TRACE_OS(OstTraceExt2(TRACE_INTERNALS, TSTATICFS_CONNECT, "OS;0;TStaticFs::Connect;iFs.Handle()=0x%X;err=%d", iFs.Handle(), err));
       
   138 	return err;
   128 	return err;
   139 	}
   129 	}
   140 
   130 
   141 ////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
   131 ////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
   142 //////////////////////////  sqlite3_mutex  /////////////////////////////////////////////////////////////////////////////////////            
   132 //////////////////////////  sqlite3_mutex  /////////////////////////////////////////////////////////////////////////////////////            
   147 */
   137 */
   148 sqlite3_mutex::sqlite3_mutex() :
   138 sqlite3_mutex::sqlite3_mutex() :
   149 	iRefCount(0),
   139 	iRefCount(0),
   150 	iOwnerThreadId(KMaxTUint64)
   140 	iOwnerThreadId(KMaxTUint64)
   151 	{
   141 	{
   152 	SQLITE_TRACE_OS(OstTrace1(TRACE_INTERNALS, SQLITE3_MUTEX_SQLITE3_MUTEX, "OS;0x%X;sqlite3_mutex::sqlite3_mutex", (TUint)this));
       
   153 	}
   142 	}
   154 
   143 
   155 /**
   144 /**
   156 Closes the mutex handle.
   145 Closes the mutex handle.
   157 */
   146 */
   158 sqlite3_mutex::~sqlite3_mutex()
   147 sqlite3_mutex::~sqlite3_mutex()
   159 	{
   148 	{
   160 	SQLITE_TRACE_OS(OstTrace1(TRACE_INTERNALS, SQLITE3_MUTEX_SQLITE3_MUTEX2, "OS;0x%X;sqlite3_mutex::~sqlite3_mutex", (TUint)this));
       
   161 	iMutex.Close();
   149 	iMutex.Close();
   162 	}
   150 	}
   163 
   151 
   164 /**
   152 /**
   165 Gives the calling thread an exclusive access to the SQLite resources (global variables, file handles, buffers, cache, etc.).
   153 Gives the calling thread an exclusive access to the SQLite resources (global variables, file handles, buffers, cache, etc.).
   169 
   157 
   170 @panic SqliteMt 23 Negative mutex lock counter (in debug builds only)
   158 @panic SqliteMt 23 Negative mutex lock counter (in debug builds only)
   171 */
   159 */
   172 void sqlite3_mutex::Enter()
   160 void sqlite3_mutex::Enter()
   173 	{
   161 	{
   174     __ASSERT_DEBUG(iRefCount >= 0, __SQLITEPANIC(ESqliteOsPanicMutexLockCounter));
   162     __ASSERT_DEBUG(iRefCount >= 0, User::Panic(KPanicCategory, EPanicMutexLockCounter));
   175 	iMutex.Wait();
   163 	iMutex.Wait();
   176 	RThread currThread;
   164 	RThread currThread;
   177 	iOwnerThreadId = currThread.Id();
   165 	iOwnerThreadId = currThread.Id();
   178 	++iRefCount;
   166 	++iRefCount;
   179 	}
   167 	}
   187 @panic SqliteMt 23 Negative mutex lock counter (in debug builds only)
   175 @panic SqliteMt 23 Negative mutex lock counter (in debug builds only)
   188 @panic SqliteMt 24 The mutex has been entered (locked) by a different thread than the current one (in debug builds only)
   176 @panic SqliteMt 24 The mutex has been entered (locked) by a different thread than the current one (in debug builds only)
   189 */
   177 */
   190 void sqlite3_mutex::Leave()
   178 void sqlite3_mutex::Leave()
   191 	{
   179 	{
   192 	__ASSERT_DEBUG(iRefCount > 0, __SQLITEPANIC(ESqliteOsPanicMutexLockCounter));
   180 	__ASSERT_DEBUG(iRefCount > 0, User::Panic(KPanicCategory, EPanicMutexLockCounter));
   193 #ifdef _DEBUG
   181 #ifdef _DEBUG
   194 	RThread currThread;	
   182 	RThread currThread;	
   195 	__ASSERT_DEBUG(iOwnerThreadId == currThread.Id(), __SQLITEPANIC(ESqliteOsPanicMutexOwner));
   183 	__ASSERT_DEBUG(iOwnerThreadId == currThread.Id(), User::Panic(KPanicCategory, EPanicMutexOwner));
   196 #endif
   184 #endif
   197 	--iRefCount;
   185 	--iRefCount;
   198 	iMutex.Signal();
   186 	iMutex.Signal();
   199 	}
   187 	}
   200 
   188 
   215 @return KErrNone The operation was completed successfully,
   203 @return KErrNone The operation was completed successfully,
   216                  System-wide error code if the operation has failed.
   204                  System-wide error code if the operation has failed.
   217 */
   205 */
   218 TInt sqlite3_mutex::Create()
   206 TInt sqlite3_mutex::Create()
   219 	{
   207 	{
   220 	TInt err = iMutex.CreateLocal();
   208 	return iMutex.CreateLocal();
   221 	SQLITE_TRACE_OS(OstTraceExt2(TRACE_INTERNALS, SQLITE3_MUTEX_CREATE, "OS;0x%X;sqlite3_mutex::Create;err=%d", (TUint)this, err));
       
   222 	return err;
       
   223 	}
   209 	}
   224 
   210 
   225 /**
   211 /**
   226 Creates new CRecursiveMutex object.
   212 Creates new CRecursiveMutex object.
   227 
   213 
   236 			{
   222 			{
   237 			delete self;	
   223 			delete self;	
   238 			self = NULL;
   224 			self = NULL;
   239 			}
   225 			}
   240 		}
   226 		}
   241 	SQLITE_TRACE_OS(OstTrace1(TRACE_INTERNALS, CRECURSIVEMUTEX_NEWL, "OS;0x%X;CRecursiveMutex::New", (TUint)self));
       
   242 	return self;
   227 	return self;
   243 	}
   228 	}
   244 
   229 
   245 CRecursiveMutex::~CRecursiveMutex()
   230 CRecursiveMutex::~CRecursiveMutex()
   246 	{
   231 	{
   256 
   241 
   257 @return SQLITE_OK
   242 @return SQLITE_OK
   258 */
   243 */
   259 int TMutexApi::Init()
   244 int TMutexApi::Init()
   260 	{
   245 	{
       
   246 	SQLUTRACE_PROFILER(0);
   261 	return SQLITE_OK;
   247 	return SQLITE_OK;
   262 	}
   248 	}
   263 	
   249 	
   264 /**
   250 /**
   265 Finalizes the mutex system.
   251 Finalizes the mutex system.
   267 
   253 
   268 @return SQLITE_OK
   254 @return SQLITE_OK
   269 */
   255 */
   270 int TMutexApi::End()
   256 int TMutexApi::End()
   271 	{
   257 	{
       
   258 	SQLUTRACE_PROFILER(0);
   272 	return SQLITE_OK;
   259 	return SQLITE_OK;
   273 	}
   260 	}
   274 	
   261 	
   275 /**
   262 /**
   276 Creates a new mutex.
   263 Creates a new mutex.
   279 @param aType  The mutex type: static, fast, recursive
   266 @param aType  The mutex type: static, fast, recursive
   280 @return A pointer to the created mutex or NULL if the operation has failed
   267 @return A pointer to the created mutex or NULL if the operation has failed
   281 */
   268 */
   282 sqlite3_mutex* TMutexApi::Alloc(int aType)
   269 sqlite3_mutex* TMutexApi::Alloc(int aType)
   283 	{
   270 	{
       
   271 	SQLUTRACE_PROFILER(0);
   284 	sqlite3_mutex* mutex = NULL;
   272 	sqlite3_mutex* mutex = NULL;
   285 	switch(aType)
   273 	switch(aType)
   286 		{
   274 		{
   287 		case SQLITE_MUTEX_FAST:
   275 		case SQLITE_MUTEX_FAST:
   288 		case SQLITE_MUTEX_RECURSIVE:
   276 		case SQLITE_MUTEX_RECURSIVE:
   291 		default:
   279 		default:
   292 			mutex = ::StaticMutex(aType - 2);//"aType - 2" because the first SQLITE_MUTEX_STATIC_<type> mutex definition 
   280 			mutex = ::StaticMutex(aType - 2);//"aType - 2" because the first SQLITE_MUTEX_STATIC_<type> mutex definition 
   293 			//value is 2 (SQLITE_MUTEX_FAST is 0, SQLITE_MUTEX_RECURSIVE is 1). 
   281 			//value is 2 (SQLITE_MUTEX_FAST is 0, SQLITE_MUTEX_RECURSIVE is 1). 
   294 			break;	
   282 			break;	
   295 		}
   283 		}
   296 	SQLITE_TRACE_OS(OstTraceExt2(TRACE_INTERNALS, TMUTEXAPI_ALLOC, "OS;0;TMutexApi::Alloc;aType=%d;mutex=0x%X", aType, (TUint)mutex));
       
   297 	return mutex;
   284 	return mutex;
   298 	}
   285 	}
   299 	
   286 	
   300 /**
   287 /**
   301 Destroys a mutex, created previously by a call to TMutexApi::Alloc().
   288 Destroys a mutex, created previously by a call to TMutexApi::Alloc().
   302 @param aMutex Pointer to the mutex object that has to be destroyed
   289 @param aMutex Pointer to the mutex object that has to be destroyed
   303 */
   290 */
   304 void TMutexApi::Free(sqlite3_mutex* aMutex)
   291 void TMutexApi::Free(sqlite3_mutex* aMutex)
   305 	{
   292 	{
   306 	SQLITE_TRACE_OS(OstTrace1(TRACE_INTERNALS, TMUTEXAPI_FREE, "OS;0;TMutexApi::Free;mutex=0x%X", (TUint)aMutex));
   293 	SQLUTRACE_PROFILER(0);
   307 	delete aMutex;
   294 	delete aMutex;
   308 	}
   295 	}
   309 	
   296 	
   310 /**
   297 /**
   311 Locks the mutex.
   298 Locks the mutex.
   315 
   302 
   316 @see sqlite3_mutex::Enter()
   303 @see sqlite3_mutex::Enter()
   317 */
   304 */
   318 void TMutexApi::Enter(sqlite3_mutex* aMutex)
   305 void TMutexApi::Enter(sqlite3_mutex* aMutex)
   319 	{
   306 	{
       
   307 	SQLUTRACE_PROFILER(0);
   320 	aMutex->Enter();
   308 	aMutex->Enter();
   321 	}
   309 	}
   322 	
   310 	
   323 /**
   311 /**
   324 No-op. Always returns SQLITE_BUSY.
   312 No-op. Always returns SQLITE_BUSY.
   325 
   313 
   326 @return SQLITE_BUSY
   314 @return SQLITE_BUSY
   327 */
   315 */
   328 int TMutexApi::Try(sqlite3_mutex*)
   316 int TMutexApi::Try(sqlite3_mutex*)
   329 	{
   317 	{
       
   318 	SQLUTRACE_PROFILER(0);
   330 	return SQLITE_BUSY;
   319 	return SQLITE_BUSY;
   331 	}
   320 	}
   332 	
   321 	
   333 /**
   322 /**
   334 Unlocks the mutex.
   323 Unlocks the mutex.
   338 
   327 
   339 @see sqlite3_mutex::Leave()
   328 @see sqlite3_mutex::Leave()
   340 */
   329 */
   341 void TMutexApi::Leave(sqlite3_mutex* aMutex)
   330 void TMutexApi::Leave(sqlite3_mutex* aMutex)
   342 	{
   331 	{
       
   332 	SQLUTRACE_PROFILER(0);
   343 	aMutex->Leave();
   333 	aMutex->Leave();
   344 	}
   334 	}
   345 	
   335 	
   346 /**
   336 /**
   347 Checks whether the mutex is locked or not.
   337 Checks whether the mutex is locked or not.
   353 
   343 
   354 @see sqlite3_mutex::IsHeld()
   344 @see sqlite3_mutex::IsHeld()
   355 */
   345 */
   356 int TMutexApi::Held(sqlite3_mutex* aMutex)
   346 int TMutexApi::Held(sqlite3_mutex* aMutex)
   357 	{
   347 	{
       
   348 	SQLUTRACE_PROFILER(0);
   358 	return aMutex->IsHeld();
   349 	return aMutex->IsHeld();
   359 	}
   350 	}
   360 	
   351 	
   361 /**
   352 /**
   362 Checks whether the mutex is locked or not.
   353 Checks whether the mutex is locked or not.
   368 
   359 
   369 @see sqlite3_mutex::IsHeld()
   360 @see sqlite3_mutex::IsHeld()
   370 */
   361 */
   371 int TMutexApi::Notheld(sqlite3_mutex* aMutex)
   362 int TMutexApi::Notheld(sqlite3_mutex* aMutex)
   372 	{
   363 	{
       
   364 	SQLUTRACE_PROFILER(0);
   373 	return !aMutex->IsHeld();
   365 	return !aMutex->IsHeld();
   374 	}
   366 	}
   375 
   367 
   376 ///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
   368 ///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
   377 ///////////////////////////////////       SQLite init/release functions     ///////////////////////////////////////////////////
   369 ///////////////////////////////////       SQLite init/release functions     ///////////////////////////////////////////////////
   380 /**
   372 /**
   381 Initializes the OS porting layer global data.
   373 Initializes the OS porting layer global data.
   382 */
   374 */
   383 extern "C" SQLITE_EXPORT int sqlite3_os_init(void)
   375 extern "C" SQLITE_EXPORT int sqlite3_os_init(void)
   384 	{
   376 	{
   385 	TInt err = sqlite3_vfs_register(VfsApi(), 1);
   377 	return sqlite3_vfs_register(VfsApi(), 1);
   386 	SQLITE_TRACE_OS(OstTrace1(TRACE_INTERNALS, SQLITE3_OS_INIT, "OS;0;sqlite3_os_init;err=%d", err));
       
   387 	return err;
       
   388 	}
   378 	}
   389 
   379 
   390 /**
   380 /**
   391 Destroys the OS porting layer global data.
   381 Destroys the OS porting layer global data.
   392 */
   382 */
   393 extern "C" SQLITE_EXPORT int sqlite3_os_end(void)
   383 extern "C" SQLITE_EXPORT int sqlite3_os_end(void)
   394 	{
   384 	{
   395 	TInt err = sqlite3_vfs_unregister(VfsApi());
   385 	return sqlite3_vfs_unregister(VfsApi());
   396 	SQLITE_TRACE_OS(OstTrace1(TRACE_INTERNALS, SQLITE3_OS_END, "OS;0;sqlite3_os_end;err=%d", err));
       
   397 	return err;
       
   398 	}
   386 	}
   399 
   387 
   400 ///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
   388 ///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
   401 //////////////////////////  TheFileIoApi  /////////////////////////////////////////////////////////////////////////////////////
   389 //////////////////////////  TheFileIoApi  /////////////////////////////////////////////////////////////////////////////////////
   402 ///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
   390 ///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
   531 	iReadOnly(EFalse),
   519 	iReadOnly(EFalse),
   532 	iSectorSize(0),
   520 	iSectorSize(0),
   533 	iDeviceCharacteristics(-1)
   521 	iDeviceCharacteristics(-1)
   534 	{
   522 	{
   535 	pMethods = 0;
   523 	pMethods = 0;
   536 	SQLITE_TRACE_OS(OstTrace1(TRACE_INTERNALS, TDBFILE_TDBFILE, "OS;0x%X;TDbFile::TDbFile", (TUint)this));
       
   537 	}
   524 	}
   538 
   525 
   539 /**
   526 /**
   540 Casts the passed sqlite3_file pointer to a reference to the derived class - TDbFile&.
   527 Casts the passed sqlite3_file pointer to a reference to the derived class - TDbFile&.
   541 All sqlite3_file pointers passed to TFileIo methods are actually pointers to TDbFile instances. 
   528 All sqlite3_file pointers passed to TFileIo methods are actually pointers to TDbFile instances. 
   552 
   539 
   553 @internalComponent
   540 @internalComponent
   554 */
   541 */
   555 static inline TDbFile& DbFile(sqlite3_file* aDbFile)
   542 static inline TDbFile& DbFile(sqlite3_file* aDbFile)
   556 	{
   543 	{
   557 	__ASSERT_DEBUG(aDbFile != 0, __SQLITEPANIC2(ESqliteOsPanicNullDbFilePtr));
   544 	__ASSERT_DEBUG(aDbFile != 0, User::Panic(KPanicCategory, EPanicNullDbFilePtr));
   558 	return *(static_cast <TDbFile*> (aDbFile));
   545 	return *(static_cast <TDbFile*> (aDbFile));
   559 	}
   546 	}
   560 
   547 
   561 ///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
   548 ///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
   562 /////////////////////       TFileIo class definition    ///////////////////////////////////////////////////////////////////////
   549 /////////////////////       TFileIo class definition    ///////////////////////////////////////////////////////////////////////
   574 
   561 
   575 @see TDbFile
   562 @see TDbFile
   576 */
   563 */
   577 /* static */ int TFileIo::Close(sqlite3_file* aDbFile)
   564 /* static */ int TFileIo::Close(sqlite3_file* aDbFile)
   578 	{
   565 	{
       
   566 	SQLUTRACE_PROFILER(aDbFile);
   579 	TDbFile& dbFile = ::DbFile(aDbFile);
   567 	TDbFile& dbFile = ::DbFile(aDbFile);
   580 	SQLITE_TRACE_OS(OstTrace1(TRACE_INTERNALS, TFILEIO_CLOSE1, "OS;0x%X;TFileIo::Close", (TUint)&dbFile));
       
   581 	dbFile.iFileBuf.Close();	
   568 	dbFile.iFileBuf.Close();	
   582 	if(dbFile.iFullName)
   569 	if(dbFile.iFullName)
   583         {//"iFullName" will not be NULL only when TVfs::Open() is called with SQLITE_OPEN_DELETEONCLOSE flag.
   570         {//"iFullName" will not be NULL only when TVfs::Open() is called with SQLITE_OPEN_DELETEONCLOSE flag.
   584          //That means - SQlite expects the file to be deleted after the file close operation. 
   571          //That means - SQlite expects the file to be deleted after the file close operation. 
   585 		__SQLITETRACE_OSEXPR(TInt err = ) TStaticFs::Fs().Delete(*dbFile.iFullName);
   572 		(void)TStaticFs::Fs().Delete(*dbFile.iFullName);
   586 		SQLITE_TRACE_OS(OstTraceExt3(TRACE_INTERNALS, TFILEIO_CLOSE2, "OS;0x%X;TFileIo::Close;delete fileName=%S;err=%d", (TUint)&dbFile, __SQLITEPRNSTR(*dbFile.iFullName), err));
       
   587 		delete dbFile.iFullName;
   573 		delete dbFile.iFullName;
   588 		dbFile.iFullName = NULL;
   574 		dbFile.iFullName = NULL;
   589 		}
   575 		}
   590     OpenCounter(-1);
   576     OpenCounter(-1);
   591 	return SQLITE_OK;
   577 	return SQLITE_OK;
   610 	    
   596 	    
   611 @see TDbFile
   597 @see TDbFile
   612 */
   598 */
   613 /* static */ int TFileIo::Read(sqlite3_file* aDbFile, void* aBuf, int aAmt, sqlite3_int64 aOffset)
   599 /* static */ int TFileIo::Read(sqlite3_file* aDbFile, void* aBuf, int aAmt, sqlite3_int64 aOffset)
   614 	{
   600 	{
       
   601 	SQLUTRACE_PROFILER(aDbFile);
       
   602 	SYMBIAN_TRACE_SQLITE_EVENTS_ONLY(UTF::Printf(UTF::TTraceContext(UTF::EInternals), KFileRead, aAmt, aOffset));
   615 	SimulateIOError(return SQLITE_IOERR_READ);
   603 	SimulateIOError(return SQLITE_IOERR_READ);
   616 	TDbFile& dbFile = ::DbFile(aDbFile);
   604 	TDbFile& dbFile = ::DbFile(aDbFile);
   617 	SQLITE_TRACE_OS(OstTraceExt3(TRACE_INTERNALS, TFILEIO_READ_ENTRY, "OS-Entry;0x%X;TFileIo::Read;aAmt=%d;aOffset=%lld", (TUint)&dbFile, aAmt, aOffset));
       
   618 	TPtr8 ptr((TUint8*)aBuf, 0, aAmt);
   605 	TPtr8 ptr((TUint8*)aBuf, 0, aAmt);
   619 	TInt err = dbFile.iFileBuf.Read(aOffset, ptr);
   606 	TInt err = dbFile.iFileBuf.Read(aOffset, ptr);
   620 	TInt cnt = ptr.Length();
   607 	TInt cnt = ptr.Length();
   621 	TInt sqliteErr = ::Os2SqliteErr(err, SQLITE_IOERR_READ);
   608 	TInt sqliteErr = ::Os2SqliteErr(err, SQLITE_IOERR_READ);
   622 	if(cnt != aAmt && (sqliteErr == SQLITE_OK || sqliteErr == SQLITE_IOERR_SHORT_READ))
   609 	if(cnt != aAmt && (sqliteErr == SQLITE_OK || sqliteErr == SQLITE_IOERR_SHORT_READ))
   623 		{
   610 		{
   624 		Mem::FillZ(static_cast <TUint8*> (aBuf) + cnt, aAmt - cnt);
   611 		Mem::FillZ(static_cast <TUint8*> (aBuf) + cnt, aAmt - cnt);
   625 		sqliteErr = SQLITE_IOERR_SHORT_READ;
   612 		sqliteErr = SQLITE_IOERR_SHORT_READ;
   626 		}
   613 		}
   627 	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));
       
   628 	return sqliteErr;
   614 	return sqliteErr;
   629 	}
   615 	}
   630 
   616 
   631 /**
   617 /**
   632 SQLite OS porting layer API.
   618 SQLite OS porting layer API.
   646 	    
   632 	    
   647 @see TDbFile
   633 @see TDbFile
   648 */
   634 */
   649 /* static */ int TFileIo::Write(sqlite3_file* aDbFile, const void* aData, int aAmt, sqlite3_int64 aOffset)
   635 /* static */ int TFileIo::Write(sqlite3_file* aDbFile, const void* aData, int aAmt, sqlite3_int64 aOffset)
   650 	{
   636 	{
       
   637 	SQLUTRACE_PROFILER(aDbFile);
       
   638 	SYMBIAN_TRACE_SQLITE_EVENTS_ONLY(UTF::Printf(UTF::TTraceContext(UTF::EInternals), KFileWrite, aAmt, aOffset));
   651 	SimulateIOError(return SQLITE_IOERR_WRITE);
   639 	SimulateIOError(return SQLITE_IOERR_WRITE);
   652 	SimulateDiskfullError(return SQLITE_FULL);
   640 	SimulateDiskfullError(return SQLITE_FULL);
   653 	TDbFile& dbFile = ::DbFile(aDbFile);
   641 	TDbFile& dbFile = ::DbFile(aDbFile);
   654 	SQLITE_TRACE_OS(OstTraceExt3(TRACE_INTERNALS, TFILEIO_WRITE_ENTRY, "OS-Entry;0x%X;TFileIo::Write;aAmt=%d;aOffset=%lld", (TUint)&dbFile, aAmt, aOffset));
       
   655 	TInt err = KErrAccessDenied;
   642 	TInt err = KErrAccessDenied;
   656 	if(!dbFile.iReadOnly)
   643 	if(!dbFile.iReadOnly)
   657 		{
   644 		{
   658 		TPtrC8 ptr((const TUint8*)aData, aAmt);
   645 		TPtrC8 ptr((const TUint8*)aData, aAmt);
   659 		err = dbFile.iFileBuf.Write(aOffset, ptr);
   646 		err = dbFile.iFileBuf.Write(aOffset, ptr);
   660 		}
   647 		}
   661 	TInt sqliteErr = ::Os2SqliteErr(err, SQLITE_IOERR_WRITE);
   648 	return ::Os2SqliteErr(err, SQLITE_IOERR_WRITE);
   662 	SQLITE_TRACE_OS(OstTraceExt3(TRACE_INTERNALS, TFILEIO_WRITE_EXIT, "OS-Exit;0x%X;TFileIo::Write;err=%d;sqliteErr=%d", (TUint)&dbFile, err, sqliteErr));
       
   663 	return sqliteErr;
       
   664 	}
   649 	}
   665 
   650 
   666 /**
   651 /**
   667 SQLite OS porting layer API.
   652 SQLite OS porting layer API.
   668 
   653 
   679 	    
   664 	    
   680 @see TDbFile
   665 @see TDbFile
   681 */
   666 */
   682 /* static */ int TFileIo::Truncate(sqlite3_file* aDbFile, sqlite3_int64 aLength)
   667 /* static */ int TFileIo::Truncate(sqlite3_file* aDbFile, sqlite3_int64 aLength)
   683 	{
   668 	{
       
   669 	SQLUTRACE_PROFILER(aDbFile);
       
   670 	SYMBIAN_TRACE_SQLITE_EVENTS_ONLY(UTF::Printf(UTF::TTraceContext(UTF::EInternals), KFileTruncate, aLength));
   684 	SimulateIOError(return SQLITE_IOERR_TRUNCATE);
   671 	SimulateIOError(return SQLITE_IOERR_TRUNCATE);
   685 	TDbFile& dbFile = ::DbFile(aDbFile);
   672 	TDbFile& dbFile = ::DbFile(aDbFile);
   686 	SQLITE_TRACE_OS(OstTraceExt2(TRACE_INTERNALS, TFILEIO_TRUNCATE_ENTRY, "OS-Entry;0x%X;TFileIo::Truncate;aLength=%lld", (TUint)&dbFile, aLength));
       
   687 	TInt err = KErrAccessDenied;
   673 	TInt err = KErrAccessDenied;
   688 	if(!dbFile.iReadOnly)
   674 	if(!dbFile.iReadOnly)
   689 		{
   675 		{
   690 		err = dbFile.iFileBuf.SetSize(aLength);
   676 		err = dbFile.iFileBuf.SetSize(aLength);
   691 		}
   677 		}
   692 	TInt sqliteErr = ::Os2SqliteErr(err, SQLITE_IOERR_TRUNCATE);
   678 	return ::Os2SqliteErr(err, SQLITE_IOERR_TRUNCATE);
   693 	SQLITE_TRACE_OS(OstTraceExt3(TRACE_INTERNALS, TFILEIO_TRUNCATE_EXIT, "OS-Exit;0x%X;TFileIo::Truncate;err=%d;sqliteErr=%d", (TUint)&dbFile, err, sqliteErr));
       
   694 	return sqliteErr;
       
   695 	}
   679 	}
   696 
   680 
   697 /**
   681 /**
   698 SQLite OS porting layer API.
   682 SQLite OS porting layer API.
   699 
   683 
   710 
   694 
   711 @see TDbFile
   695 @see TDbFile
   712 */
   696 */
   713 /* static */int TFileIo::Sync(sqlite3_file* aDbFile, int aFlags)
   697 /* static */int TFileIo::Sync(sqlite3_file* aDbFile, int aFlags)
   714 	{
   698 	{
       
   699 	SQLUTRACE_PROFILER(aDbFile);
   715 	SimulateIOError(return SQLITE_IOERR_FSYNC);
   700 	SimulateIOError(return SQLITE_IOERR_FSYNC);
   716 	TDbFile& dbFile = ::DbFile(aDbFile);
   701 	TDbFile& dbFile = ::DbFile(aDbFile);
   717 	SQLITE_TRACE_OS(OstTrace1(TRACE_INTERNALS, TFILEIO_SYNC_ENTRY, "OS-Entry;0x%X;TFileIo::Sync", (TUint)&dbFile));
       
   718 #ifdef SQLITE_TEST
   702 #ifdef SQLITE_TEST
   719 	if(aFlags & SQLITE_SYNC_FULL)
   703 	if(aFlags & SQLITE_SYNC_FULL)
   720 		{
   704 		{
   721 		sqlite3_fullsync_count++;
   705 		sqlite3_fullsync_count++;
   722 		}
   706 		}
   727 	TInt err = KErrAccessDenied;
   711 	TInt err = KErrAccessDenied;
   728 	if(!dbFile.iReadOnly)
   712 	if(!dbFile.iReadOnly)
   729 		{
   713 		{
   730 		err = dbFile.iFileBuf.Flush();
   714 		err = dbFile.iFileBuf.Flush();
   731 		}
   715 		}
   732 	TInt sqliteErr = ::Os2SqliteErr(err, SQLITE_IOERR_FSYNC);
   716 	return ::Os2SqliteErr(err, SQLITE_IOERR_FSYNC);
   733 	SQLITE_TRACE_OS(OstTraceExt3(TRACE_INTERNALS, TFILEIO_SYNC_EXIT, "OS-Exit;0x%X;TFileIo::Sync;err=%d;sqliteErr=%d", (TUint)&dbFile, err, sqliteErr));
       
   734 	return sqliteErr;
       
   735 	}
   717 	}
   736 
   718 
   737 /**
   719 /**
   738 SQLite OS porting layer API.
   720 SQLite OS porting layer API.
   739 
   721 
   748 	    
   730 	    
   749 @see TDbFile
   731 @see TDbFile
   750 */
   732 */
   751 /* static */ int TFileIo::FileSize(sqlite3_file* aDbFile, sqlite3_int64* aSize)
   733 /* static */ int TFileIo::FileSize(sqlite3_file* aDbFile, sqlite3_int64* aSize)
   752 	{
   734 	{
       
   735 	SQLUTRACE_PROFILER(aDbFile);
   753 	SimulateIOError(return SQLITE_IOERR_FSTAT);
   736 	SimulateIOError(return SQLITE_IOERR_FSTAT);
   754 	TDbFile& dbFile = ::DbFile(aDbFile);
   737 	TDbFile& dbFile = ::DbFile(aDbFile);
   755 	SQLITE_TRACE_OS(OstTrace1(TRACE_INTERNALS, TFILEIO_FILESIZE_ENTRY, "OS-Entry;0x%X;TFileIo::FileSize", (TUint)&dbFile));
       
   756 	TInt err =  dbFile.iFileBuf.Size(*aSize);
   738 	TInt err =  dbFile.iFileBuf.Size(*aSize);
   757 	TInt sqliteErr = ::Os2SqliteErr(err, SQLITE_IOERR_FSTAT);
   739 	return ::Os2SqliteErr(err, SQLITE_IOERR_FSTAT);
   758 	SQLITE_TRACE_OS(OstTraceExt4(TRACE_INTERNALS, TFILEIO_FILESIZE_EXIT, "OS-Exit;0x%X;TFileIo::FileSize;aSize=%lld;err=%d;sqliteErr=%d", (TUint)&dbFile, *aSize, err, sqliteErr));
       
   759 	return sqliteErr;
       
   760 	}
   740 	}
   761 
   741 
   762 /**
   742 /**
   763 This function is called when SQLite needs to obtain a read lock. This is done by generating a
   743 This function is called when SQLite needs to obtain a read lock. This is done by generating a
   764 random file position within the first page beyond the first Gb of the file and locking a single byte there.
   744 random file position within the first page beyond the first Gb of the file and locking a single byte there.
   797 	    	{
   777 	    	{
   798 	    	aDbFile.iSharedLockByte = sharedLockByte;
   778 	    	aDbFile.iSharedLockByte = sharedLockByte;
   799 	    	break;
   779 	    	break;
   800 	    	}
   780 	    	}
   801 		}
   781 		}
   802 	SQLITE_TRACE_OS(OstTraceExt2(TRACE_INTERNALS, TFILEIO_GETREADLOCK, "OS;0x%X;TFileIo::GetReadLock;rc=%d", (TUint)&aDbFile, rc));
       
   803 	return rc;
   782 	return rc;
   804 	}
   783 	}
   805 
   784 
   806 /**
   785 /**
   807 Unlocks the file area previously locked by the GetReadLock() call.
   786 Unlocks the file area previously locked by the GetReadLock() call.
   814 
   793 
   815 @see TFileIo::GetReadLock()
   794 @see TFileIo::GetReadLock()
   816 */
   795 */
   817 /* static */TInt TFileIo::UnlockReadLock(TDbFile& aDbFile)
   796 /* static */TInt TFileIo::UnlockReadLock(TDbFile& aDbFile)
   818 	{
   797 	{
   819 	TInt err = aDbFile.iFileBuf.UnLock(SHARED_FIRST + aDbFile.iSharedLockByte, 1);
   798 	return aDbFile.iFileBuf.UnLock(SHARED_FIRST + aDbFile.iSharedLockByte, 1);
   820 	SQLITE_TRACE_OS(OstTraceExt2(TRACE_INTERNALS, TFILEIO_UNLOCKREADLOCK, "OS;0x%X;TFileIo::UnlockReadLock;err=%d", (TUint)&aDbFile, err));
       
   821 	return err;
       
   822 	}
   799 	}
   823 
   800 
   824 /**
   801 /**
   825 SQLite OS porting layer API.
   802 SQLite OS porting layer API.
   826 
   803 
   853 	    
   830 	    
   854 @see TDbFile
   831 @see TDbFile
   855 */
   832 */
   856 /* static */ int TFileIo::Lock(sqlite3_file* aDbFile, int aLockType)
   833 /* static */ int TFileIo::Lock(sqlite3_file* aDbFile, int aLockType)
   857 	{
   834 	{
       
   835 	SQLUTRACE_PROFILER(aDbFile);
   858 	TDbFile& dbFile = ::DbFile(aDbFile);
   836 	TDbFile& dbFile = ::DbFile(aDbFile);
   859 	//If there is already a lock of this type or more restrictive on the aDbFile, then - do nothing.
   837 	//If there is already a lock of this type or more restrictive on the aDbFile, then - do nothing.
   860 	if(dbFile.iLockType >= aLockType)
   838 	if(dbFile.iLockType >= aLockType)
   861 		{
   839 		{
   862 		SQLITE_TRACE_OS(OstTraceExt3(TRACE_INTERNALS, TFILEIO_LOCK1, "OS;0x%X;TFileIo::Lock;dbFile.iLockType=%d;aLockType=%d", (TUint)&dbFile, dbFile.iLockType, aLockType));
       
   863 		return SQLITE_OK;
   840 		return SQLITE_OK;
   864 		}
   841 		}
   865 
   842 
   866 	//The file flushing here must be done in order to get the file buffer object content (iFileBuf data member))
   843 	//The file flushing here must be done in order to get the file buffer object content (iFileBuf data member))
   867 	//synchronised with the database file content (the database file content may get modified by a different connection
   844 	//synchronised with the database file content (the database file content may get modified by a different connection
   869 	if(aLockType == SQLITE_LOCK_SHARED && !dbFile.iReadOnly)
   846 	if(aLockType == SQLITE_LOCK_SHARED && !dbFile.iReadOnly)
   870 		{
   847 		{
   871 		TInt err = dbFile.iFileBuf.Flush(ETrue);
   848 		TInt err = dbFile.iFileBuf.Flush(ETrue);
   872 		if(err != KErrNone)
   849 		if(err != KErrNone)
   873 			{
   850 			{
   874 			SQLITE_TRACE_OS(OstTraceExt2(TRACE_INTERNALS, TFILEIO_LOCK2, "OS;0x%X;TFileIo::Lock;iFileBuf.Flush() failed, err=%d", (TUint)&dbFile, err));
       
   875 			return ::Os2SqliteErr(err, SQLITE_IOERR_LOCK);
   851 			return ::Os2SqliteErr(err, SQLITE_IOERR_LOCK);
   876 			}
   852 			}
   877 		}
   853 		}
   878 
   854 
   879 	//Make sure the locking sequence is correct
   855 	//Make sure the locking sequence is correct
   880 	__ASSERT_DEBUG(dbFile.iLockType != SQLITE_LOCK_NONE || aLockType == SQLITE_LOCK_SHARED, __SQLITEPANIC2(ESqliteOsPanicInvalidLock));
   856 	__ASSERT_DEBUG(dbFile.iLockType != SQLITE_LOCK_NONE || aLockType == SQLITE_LOCK_SHARED, User::Panic(KPanicCategory, EPanicInvalidLock));
   881 	__ASSERT_DEBUG(aLockType != SQLITE_LOCK_PENDING, __SQLITEPANIC2(ESqliteOsPanicInvalidLock));
   857 	__ASSERT_DEBUG(aLockType != SQLITE_LOCK_PENDING, User::Panic(KPanicCategory, EPanicInvalidLock));
   882 	__ASSERT_DEBUG(aLockType != SQLITE_LOCK_RESERVED || dbFile.iLockType == SQLITE_LOCK_SHARED, __SQLITEPANIC2(ESqliteOsPanicInvalidLock));
   858 	__ASSERT_DEBUG(aLockType != SQLITE_LOCK_RESERVED || dbFile.iLockType == SQLITE_LOCK_SHARED, User::Panic(KPanicCategory, EPanicInvalidLock));
   883 		
   859 		
   884 	TInt rc = SQLITE_OK;    //Return code from subroutines
   860 	TInt rc = SQLITE_OK;    //Return code from subroutines
   885 	TBool locked = ETrue;   //Result of a file lock call (the default value means: "lock accuired")
   861 	TBool locked = ETrue;   //Result of a file lock call (the default value means: "lock accuired")
   886   	TInt newLockType = -1;	//Set dbFile.iLockType to this value before exiting
   862   	TInt newLockType = -1;	//Set dbFile.iLockType to this value before exiting
   887 	TBool gotPendingLock = EFalse;//True if we acquired a SQLITE_LOCK_PENDING lock this time
   863 	TBool gotPendingLock = EFalse;//True if we acquired a SQLITE_LOCK_PENDING lock this time
   914 		}
   890 		}
   915 
   891 
   916 	//Acquire a shared lock
   892 	//Acquire a shared lock
   917 	if(aLockType == SQLITE_LOCK_SHARED && locked)
   893 	if(aLockType == SQLITE_LOCK_SHARED && locked)
   918 		{
   894 		{
   919 		__ASSERT_DEBUG(dbFile.iLockType == SQLITE_LOCK_NONE, __SQLITEPANIC2(ESqliteOsPanicInvalidLock));
   895 		__ASSERT_DEBUG(dbFile.iLockType == SQLITE_LOCK_NONE, User::Panic(KPanicCategory, EPanicInvalidLock));
   920 		TInt err = TFileIo::GetReadLock(dbFile);
   896 		TInt err = TFileIo::GetReadLock(dbFile);
   921 		if(err != KErrNone && err != KErrLocked) 
   897 		if(err != KErrNone && err != KErrLocked) 
   922 			{
   898 			{
   923 			SQLITE_TRACE_OS(OstTraceExt2(TRACE_INTERNALS, TFILEIO_LOCK3, "OS;0x%X;TFileIo::Lock;TFileIo::GetReadLock() failed, err=%d", (TUint)&dbFile, err));
       
   924 			return ::Os2SqliteErr(err, SQLITE_IOERR_LOCK);
   899 			return ::Os2SqliteErr(err, SQLITE_IOERR_LOCK);
   925 			}
   900 			}
   926 		locked = (err == KErrNone);
   901 		locked = (err == KErrNone);
   927 		if(locked)
   902 		if(locked)
   928 			{
   903 			{
   931   		}
   906   		}
   932 
   907 
   933 	//Acquire a RESERVED lock
   908 	//Acquire a RESERVED lock
   934 	if(aLockType == SQLITE_LOCK_RESERVED && locked)
   909 	if(aLockType == SQLITE_LOCK_RESERVED && locked)
   935 		{
   910 		{
   936 		__ASSERT_DEBUG(dbFile.iLockType == SQLITE_LOCK_SHARED, __SQLITEPANIC2(ESqliteOsPanicInvalidLock));
   911 		__ASSERT_DEBUG(dbFile.iLockType == SQLITE_LOCK_SHARED, User::Panic(KPanicCategory, EPanicInvalidLock));
   937 		TInt err = dbFile.iFileBuf.Lock(RESERVED_BYTE, 1); 
   912 		TInt err = dbFile.iFileBuf.Lock(RESERVED_BYTE, 1); 
   938 		if(err != KErrNone && err != KErrLocked) 
   913 		if(err != KErrNone && err != KErrLocked) 
   939 			{
   914 			{
   940 			SQLITE_TRACE_OS(OstTraceExt2(TRACE_INTERNALS, TFILEIO_LOCK4, "OS;0x%X;TFileIo::Lock;iFileBuf.Lock() failed, err=%d", (TUint)&dbFile, err));
       
   941 			return ::Os2SqliteErr(err, SQLITE_IOERR_LOCK);
   915 			return ::Os2SqliteErr(err, SQLITE_IOERR_LOCK);
   942 			}
   916 			}
   943 		locked = (err == KErrNone);
   917 		locked = (err == KErrNone);
   944 		if(locked)
   918 		if(locked)
   945 			{
   919 			{
   955 		}
   929 		}
   956 
   930 
   957 	//Acquire an EXCLUSIVE lock
   931 	//Acquire an EXCLUSIVE lock
   958 	if(aLockType == SQLITE_LOCK_EXCLUSIVE && locked)
   932 	if(aLockType == SQLITE_LOCK_EXCLUSIVE && locked)
   959 		{
   933 		{
   960 		__ASSERT_DEBUG(dbFile.iLockType >= SQLITE_LOCK_SHARED, __SQLITEPANIC2(ESqliteOsPanicInvalidLock));
   934 		__ASSERT_DEBUG(dbFile.iLockType >= SQLITE_LOCK_SHARED, User::Panic(KPanicCategory, EPanicInvalidLock));
   961 		(void)TFileIo::UnlockReadLock(dbFile);
   935 		(void)TFileIo::UnlockReadLock(dbFile);
   962 		TInt err = dbFile.iFileBuf.Lock(SHARED_FIRST, SHARED_SIZE);
   936 		TInt err = dbFile.iFileBuf.Lock(SHARED_FIRST, SHARED_SIZE);
   963 		if(err != KErrNone && err != KErrLocked)
   937 		if(err != KErrNone && err != KErrLocked)
   964 			{
   938 			{
   965 			SQLITE_TRACE_OS(OstTraceExt2(TRACE_INTERNALS, TFILEIO_LOCK5, "OS;0x%X;TFileIo::Lock;iFileBuf.Lock()-2 failed, err=%d", (TUint)&dbFile, err));
       
   966 			return ::Os2SqliteErr(err, SQLITE_IOERR_LOCK);
   939 			return ::Os2SqliteErr(err, SQLITE_IOERR_LOCK);
   967 			}
   940 			}
   968 		locked = (err == KErrNone);
   941 		locked = (err == KErrNone);
   969 		if(locked)
   942 		if(locked)
   970 			{
   943 			{
   974 
   947 
   975 	// If we are holding a PENDING lock that ought to be released, then
   948 	// If we are holding a PENDING lock that ought to be released, then
   976 	// release it now.
   949 	// release it now.
   977 	if(gotPendingLock && aLockType == SQLITE_LOCK_SHARED)
   950 	if(gotPendingLock && aLockType == SQLITE_LOCK_SHARED)
   978 		{
   951 		{
   979 		__SQLITETRACE_OSEXPR(TInt err =) dbFile.iFileBuf.UnLock(PENDING_BYTE, 1);
   952 		(void)dbFile.iFileBuf.UnLock(PENDING_BYTE, 1);
   980 		SQLITE_TRACE_OS(OstTraceExt2(TRACE_INTERNALS, TFILEIO_LOCK6, "OS;0x%X;TFileIo::Lock;iFileBuf.UnLock()=%d", (TUint)&dbFile, err));
       
   981   		}
   953   		}
   982 
   954 
   983 	// Update the state of the lock has held in the file descriptor then
   955 	// Update the state of the lock has held in the file descriptor then
   984 	// return the appropriate result code.
   956 	// return the appropriate result code.
   985 	rc = locked ? SQLITE_OK : SQLITE_BUSY;
   957 	rc = locked ? SQLITE_OK : SQLITE_BUSY;
   986 	dbFile.iLockType = newLockType;
   958 	dbFile.iLockType = newLockType;
   987 	SQLITE_TRACE_OS(OstTraceExt3(TRACE_INTERNALS, TFILEIO_LOCK7, "OS;0x%X;TFileIo::Lock;rc=%d;newLockType=%d", (TUint)&dbFile, rc, newLockType));
       
   988 	return rc;
   959 	return rc;
   989 	}
   960 	}
   990 
   961 
   991 /**
   962 /**
   992 SQLite OS porting layer API.
   963 SQLite OS porting layer API.
  1013 	    
   984 	    
  1014 @see TDbFile
   985 @see TDbFile
  1015 */
   986 */
  1016 /* static */ int TFileIo::Unlock(sqlite3_file* aDbFile, int aLockType)
   987 /* static */ int TFileIo::Unlock(sqlite3_file* aDbFile, int aLockType)
  1017 	{
   988 	{
  1018 	__ASSERT_DEBUG(aLockType <= SQLITE_LOCK_SHARED, __SQLITEPANIC2(ESqliteOsPanicInvalidLock));
   989 	__ASSERT_DEBUG(aLockType <= SQLITE_LOCK_SHARED, User::Panic(KPanicCategory, EPanicInvalidLock));
  1019 	
   990 	
       
   991 	SQLUTRACE_PROFILER(aDbFile);
  1020 	TDbFile& dbFile = ::DbFile(aDbFile);
   992 	TDbFile& dbFile = ::DbFile(aDbFile);
  1021 	TInt rc = SQLITE_OK;
   993 	TInt rc = SQLITE_OK;
  1022 	TInt currLockType = dbFile.iLockType;
   994 	TInt currLockType = dbFile.iLockType;
  1023 	
   995 	
  1024 	if(currLockType >= SQLITE_LOCK_EXCLUSIVE)
   996 	if(currLockType >= SQLITE_LOCK_EXCLUSIVE)
  1025 		{
   997 		{
  1026 		__SQLITETRACE_OSEXPR(TInt err2 =) dbFile.iFileBuf.UnLock(SHARED_FIRST, SHARED_SIZE);
   998 		(void)dbFile.iFileBuf.UnLock(SHARED_FIRST, SHARED_SIZE);
  1027 		SQLITE_TRACE_OS(OstTraceExt2(TRACE_INTERNALS, TFILEIO_UNLOCK1, "OS;0x%X;TFileIo::Unlock;iFileBuf.UnLock()=%d", (TUint)&dbFile, err2));
       
  1028 		if(aLockType == SQLITE_LOCK_SHARED)
   999 		if(aLockType == SQLITE_LOCK_SHARED)
  1029     		{
  1000     		{
  1030 			TInt err = TFileIo::GetReadLock(dbFile); 
  1001 			TInt err = TFileIo::GetReadLock(dbFile); 
  1031 			if(err != KErrNone && err != KErrLocked)
  1002 			if(err != KErrNone && err != KErrLocked)
  1032 				{
  1003 				{
  1033 				SQLITE_TRACE_OS(OstTraceExt2(TRACE_INTERNALS, TFILEIO_UNLOCK2, "OS;0x%X;TFileIo::Unlock;TFileIo::GetReadLock() failed, err=%d", (TUint)&dbFile, err));
       
  1034 				return ::Os2SqliteErr(err, SQLITE_IOERR_UNLOCK);
  1004 				return ::Os2SqliteErr(err, SQLITE_IOERR_UNLOCK);
  1035 				}
  1005 				}
  1036 			if(err == KErrLocked)
  1006 			if(err == KErrLocked)
  1037 				{
  1007 				{
  1038 				//This should never happen. We should always be able to reacquire the read lock
  1008 				//This should never happen. We should always be able to reacquire the read lock
  1040 				}
  1010 				}
  1041 			}
  1011 			}
  1042 		}
  1012 		}
  1043 	if(currLockType >= SQLITE_LOCK_RESERVED)
  1013 	if(currLockType >= SQLITE_LOCK_RESERVED)
  1044 		{
  1014 		{
  1045 		__SQLITETRACE_OSEXPR(TInt err2 =) dbFile.iFileBuf.UnLock(RESERVED_BYTE, 1);
  1015     	(void)dbFile.iFileBuf.UnLock(RESERVED_BYTE, 1);
  1046 		SQLITE_TRACE_OS(OstTraceExt2(TRACE_INTERNALS, TFILEIO_UNLOCK3, "OS;0x%X;TFileIo::Unlock;iFileBuf.UnLock()-2=%d", (TUint)&dbFile, err2));
       
  1047 		}
  1016 		}
  1048 	if(aLockType == SQLITE_LOCK_NONE && currLockType >= SQLITE_LOCK_SHARED)
  1017 	if(aLockType == SQLITE_LOCK_NONE && currLockType >= SQLITE_LOCK_SHARED)
  1049 		{
  1018 		{
  1050 		__SQLITETRACE_OSEXPR(TInt err2 =) TFileIo::UnlockReadLock(dbFile);
  1019 		(void)TFileIo::UnlockReadLock(dbFile);
  1051 		SQLITE_TRACE_OS(OstTraceExt2(TRACE_INTERNALS, TFILEIO_UNLOCK4, "OS;0x%X;TFileIo::Unlock;TFileIo::UnlockReadLock()=%d", (TUint)&dbFile, err2));
       
  1052 		}
  1020 		}
  1053 	if(currLockType>= SQLITE_LOCK_PENDING)
  1021 	if(currLockType>= SQLITE_LOCK_PENDING)
  1054 		{
  1022 		{
  1055 		__SQLITETRACE_OSEXPR(TInt err2 =) dbFile.iFileBuf.UnLock(PENDING_BYTE, 1);
  1023 		(void)dbFile.iFileBuf.UnLock(PENDING_BYTE, 1);
  1056 		SQLITE_TRACE_OS(OstTraceExt2(TRACE_INTERNALS, TFILEIO_UNLOCK5, "OS;0x%X;TFileIo::Unlock;iFileBuf.UnLock()-3=%d", (TUint)&dbFile, err2));
       
  1057 		}
  1024 		}
  1058 		
  1025 		
  1059 	dbFile.iLockType = aLockType;
  1026 	dbFile.iLockType = aLockType;
  1060 	SQLITE_TRACE_OS(OstTraceExt3(TRACE_INTERNALS, TFILEIO_UNLOCK6, "OS;0x%X;TFileIo::Unlock;rc=%d;newLockType=%d", (TUint)&dbFile, rc, aLockType));
       
  1061 	return rc;
  1027 	return rc;
  1062 	}
  1028 	}
  1063 
  1029 
  1064 /**
  1030 /**
  1065 SQLite OS porting layer API.
  1031 SQLite OS porting layer API.
  1078 	    
  1044 	    
  1079 @see TDbFile
  1045 @see TDbFile
  1080 */
  1046 */
  1081 /* static */ int TFileIo::CheckReservedLock(sqlite3_file* aDbFile, int *aResOut)
  1047 /* static */ int TFileIo::CheckReservedLock(sqlite3_file* aDbFile, int *aResOut)
  1082 	{
  1048 	{
       
  1049 	SQLUTRACE_PROFILER(aDbFile);
  1083 	TDbFile& dbFile = ::DbFile(aDbFile);
  1050 	TDbFile& dbFile = ::DbFile(aDbFile);
  1084 	TInt rc;
  1051 	TInt rc;
  1085 	if(dbFile.iLockType >= SQLITE_LOCK_RESERVED)
  1052 	if(dbFile.iLockType >= SQLITE_LOCK_RESERVED)
  1086 		{
  1053 		{
  1087 		rc = 1;
  1054 		rc = 1;
  1089 	else
  1056 	else
  1090 		{
  1057 		{
  1091 		TInt err = dbFile.iFileBuf.Lock(RESERVED_BYTE, 1);
  1058 		TInt err = dbFile.iFileBuf.Lock(RESERVED_BYTE, 1);
  1092 		if(err != KErrNone && err != KErrLocked)
  1059 		if(err != KErrNone && err != KErrLocked)
  1093 			{
  1060 			{
  1094 			SQLITE_TRACE_OS(OstTraceExt2(TRACE_INTERNALS, TFILEIO_CHECKRESERVEDLOCK1, "OS;0x%X;TFileIo::CheckReservedLock;iFileBuf.Lock(), err=%d", (TUint)&dbFile, err));
       
  1095 			return ::Os2SqliteErr(err, SQLITE_IOERR_CHECKRESERVEDLOCK);
  1061 			return ::Os2SqliteErr(err, SQLITE_IOERR_CHECKRESERVEDLOCK);
  1096 			}
  1062 			}
  1097 		rc = (err == KErrNone);
  1063 		rc = (err == KErrNone);
  1098 		if(rc) //non-zero rc means: the lock has been successful (there wasn't a reserved lock on this file)
  1064 		if(rc) //non-zero rc means: the lock has been successful (there wasn't a reserved lock on this file)
  1099 			{
  1065 			{
  1100 			__SQLITETRACE_OSEXPR(TInt err2 =) dbFile.iFileBuf.UnLock(RESERVED_BYTE, 1);
  1066 			(void)dbFile.iFileBuf.UnLock(RESERVED_BYTE, 1);
  1101 			SQLITE_TRACE_OS(OstTraceExt2(TRACE_INTERNALS, TFILEIO_CHECKRESERVEDLOCK2, "OS;0x%X;TFileIo::CheckReservedLock;iFileBuf.UnLock()=%d", (TUint)&dbFile, err2));
       
  1102 			}
  1067 			}
  1103     	rc = !rc;
  1068     	rc = !rc;
  1104 		}
  1069 		}
  1105 	*aResOut = rc;
  1070 	*aResOut = rc;
  1106 	SQLITE_TRACE_OS(OstTraceExt2(TRACE_INTERNALS, TFILEIO_CHECKRESERVEDLOCK3, "OS;0x%X;TFileIo::CheckReservedLock;rc=%d", (TUint)&dbFile, rc));
       
  1107 	return SQLITE_OK;
  1071 	return SQLITE_OK;
  1108 	}
  1072 	}
  1109 
  1073 
  1110 /**
  1074 /**
  1111 SQLite OS porting layer API.
  1075 SQLite OS porting layer API.
  1126 	    
  1090 	    
  1127 @see TDbFile
  1091 @see TDbFile
  1128 */
  1092 */
  1129 /* static */ int TFileIo::FileControl(sqlite3_file* aDbFile, int aOp, void* aArg)
  1093 /* static */ int TFileIo::FileControl(sqlite3_file* aDbFile, int aOp, void* aArg)
  1130 	{
  1094 	{
       
  1095 	SQLUTRACE_PROFILER(aDbFile);
  1131 	TDbFile& dbFile = ::DbFile(aDbFile);
  1096 	TDbFile& dbFile = ::DbFile(aDbFile);
       
  1097 	SYMBIAN_TRACE_SQLITE_EVENTS_ONLY(UTF::Printf(UTF::TTraceContext(UTF::EInternals), KFileFileCtr, aOp, dbFile.iFullName));
  1132 	TInt err = KErrNone;
  1098 	TInt err = KErrNone;
  1133 	switch(aOp)
  1099 	switch(aOp)
  1134 		{
  1100 		{
  1135 		case SQLITE_FCNTL_LOCKSTATE:
  1101 		case SQLITE_FCNTL_LOCKSTATE:
  1136 			*(int*)aArg = dbFile.iLockType;
  1102 			*(int*)aArg = dbFile.iLockType;
  1137 			break;
  1103 			break;
  1138 		default:
  1104 		default:
  1139 			err = KErrArgument;
  1105 			err = KErrArgument;
  1140 			break;
  1106 			break;
  1141 		}
  1107 		}
  1142 	TInt sqliteErr = err == KErrNone ? SQLITE_OK : SQLITE_ERROR;
  1108 	return err == KErrNone ? SQLITE_OK : SQLITE_ERROR;
  1143 	SQLITE_TRACE_OS(OstTraceExt3(TRACE_INTERNALS, TFILEIO_FILECONTROL, "OS;0x%X;TFileIo::FileControl;err=%d;sqliteErr=%d", (TUint)&dbFile, err, sqliteErr));
       
  1144 	return sqliteErr;
       
  1145 	}
  1109 	}
  1146 
  1110 
  1147 /**
  1111 /**
  1148 SQLite OS porting layer API.
  1112 SQLite OS porting layer API.
  1149 
  1113 
  1161 @see TDbFile
  1125 @see TDbFile
  1162 @see TVfs::Open()
  1126 @see TVfs::Open()
  1163 */
  1127 */
  1164 /* static */ int TFileIo::SectorSize(sqlite3_file* aDbFile)
  1128 /* static */ int TFileIo::SectorSize(sqlite3_file* aDbFile)
  1165 	{
  1129 	{
       
  1130 	SQLUTRACE_PROFILER(aDbFile);
  1166 	TDbFile& dbFile = ::DbFile(aDbFile);
  1131 	TDbFile& dbFile = ::DbFile(aDbFile);
  1167 	__ASSERT_DEBUG(dbFile.iSectorSize > 0, __SQLITEPANIC2(ESqliteOsPanicInternalError));
  1132 	__ASSERT_DEBUG(dbFile.iSectorSize > 0, User::Panic(KPanicCategory, EPanicInternalError));
  1168 	if(dbFile.iSectorSize > 0)
  1133 	if(dbFile.iSectorSize > 0)
  1169 		{
  1134 		{
  1170 		return dbFile.iSectorSize;	
  1135 		return dbFile.iSectorSize;	
  1171 		}
  1136 		}
  1172 	return SQLITE_DEFAULT_SECTOR_SIZE;
  1137 	return SQLITE_DEFAULT_SECTOR_SIZE;
  1189 @see TDbFile
  1154 @see TDbFile
  1190 @see TVfs::Open()
  1155 @see TVfs::Open()
  1191 */
  1156 */
  1192 /* static */ int TFileIo::DeviceCharacteristics(sqlite3_file* aDbFile)
  1157 /* static */ int TFileIo::DeviceCharacteristics(sqlite3_file* aDbFile)
  1193 	{
  1158 	{
       
  1159 	SQLUTRACE_PROFILER(aDbFile);
  1194 	TDbFile& dbFile = ::DbFile(aDbFile);
  1160 	TDbFile& dbFile = ::DbFile(aDbFile);
  1195 	__ASSERT_DEBUG(dbFile.iDeviceCharacteristics >= 0, __SQLITEPANIC2(ESqliteOsPanicInternalError));
  1161 	__ASSERT_DEBUG(dbFile.iDeviceCharacteristics >= 0, User::Panic(KPanicCategory, EPanicInternalError));
  1196 	if(dbFile.iDeviceCharacteristics >= 0)
  1162 	if(dbFile.iDeviceCharacteristics >= 0)
  1197 		{
  1163 		{
  1198 		return dbFile.iDeviceCharacteristics;	
  1164 		return dbFile.iDeviceCharacteristics;	
  1199 		}
  1165 		}
  1200 	return 0;
  1166 	return 0;
  1217 	    
  1183 	    
  1218 @see TVfs::Open()
  1184 @see TVfs::Open()
  1219 */
  1185 */
  1220 /* static */ inline TInt TVfs::DoGetVolumeIoParamInfo(RFs& aFs, TInt aDriveNo, TVolumeIOParamInfo& aVolumeInfo)
  1186 /* static */ inline TInt TVfs::DoGetVolumeIoParamInfo(RFs& aFs, TInt aDriveNo, TVolumeIOParamInfo& aVolumeInfo)
  1221 	{
  1187 	{
  1222 	TInt err = aFs.VolumeIOParam(aDriveNo, aVolumeInfo);
  1188 	return aFs.VolumeIOParam(aDriveNo, aVolumeInfo);
  1223 	SQLITE_TRACE_OS(OstTraceExt2(TRACE_INTERNALS, TVFS_DOGETVOLUMEIOPARAMINFO, "OS;0;TVfs::DoGetVolumeIoParamInfo;aDriveNo=%d;err=%d", aDriveNo, err));
       
  1224 	return err;
       
  1225 	}
  1189 	}
  1226 
  1190 
  1227 /**
  1191 /**
  1228 Retrieves and returns in a bit set the device characteristics.
  1192 Retrieves and returns in a bit set the device characteristics.
  1229 
  1193 
  1311 		if(aVolumeInfo.iBlockSize > SQLITE_DEFAULT_SECTOR_SIZE && (aVolumeInfo.iBlockSize & (aVolumeInfo.iBlockSize - 1)) == 0)
  1275 		if(aVolumeInfo.iBlockSize > SQLITE_DEFAULT_SECTOR_SIZE && (aVolumeInfo.iBlockSize & (aVolumeInfo.iBlockSize - 1)) == 0)
  1312 			{
  1276 			{
  1313 			sectorSize = aVolumeInfo.iBlockSize;
  1277 			sectorSize = aVolumeInfo.iBlockSize;
  1314 			}
  1278 			}
  1315 		}
  1279 		}
  1316 	__ASSERT_DEBUG(sectorSize > 0 && (sectorSize & (sectorSize - 1)) == 0, __SQLITEPANIC2(ESqliteOsPanicInternalError));
  1280 	__ASSERT_DEBUG(sectorSize > 0 && (sectorSize & (sectorSize - 1)) == 0, User::Panic(KPanicCategory, EPanicInternalError));
  1317 	return sectorSize;
  1281 	return sectorSize;
  1318 	}
  1282 	}
  1319 
  1283 
  1320 /**
  1284 /**
  1321 Retrieves in a bit set the device characteristics of the device of the file referred by the aDbFile parameter.
  1285 Retrieves in a bit set the device characteristics of the device of the file referred by the aDbFile parameter.
  1340 @see TFileIo::DeviceCharacteristics()
  1304 @see TFileIo::DeviceCharacteristics()
  1341 @see TFileIo::SectorSize()
  1305 @see TFileIo::SectorSize()
  1342 */
  1306 */
  1343 /* static */ TInt TVfs::DoGetDeviceCharacteristicsAndSectorSize(TDbFile& aDbFile, TInt& aRecReadBufSize)
  1307 /* static */ TInt TVfs::DoGetDeviceCharacteristicsAndSectorSize(TDbFile& aDbFile, TInt& aRecReadBufSize)
  1344 	{
  1308 	{
  1345 	__ASSERT_DEBUG(aDbFile.iDeviceCharacteristics < 0, __SQLITEPANIC2(ESqliteOsPanicInternalError));
  1309 	__ASSERT_DEBUG(aDbFile.iDeviceCharacteristics < 0, User::Panic(KPanicCategory, EPanicInternalError));
  1346 	__ASSERT_DEBUG(aDbFile.iSectorSize <= 0, __SQLITEPANIC2(ESqliteOsPanicInternalError));
  1310 	__ASSERT_DEBUG(aDbFile.iSectorSize <= 0, User::Panic(KPanicCategory, EPanicInternalError));
  1347 	TInt driveNo;
  1311 	TInt driveNo;
  1348 	TDriveInfo driveInfo;
  1312 	TDriveInfo driveInfo;
  1349 	TInt err = aDbFile.iFileBuf.Drive(driveNo, driveInfo);
  1313 	TInt err = aDbFile.iFileBuf.Drive(driveNo, driveInfo);
  1350 	if(err != KErrNone)
  1314 	if(err != KErrNone)
  1351 		{
  1315 		{
  1358 		return err;	
  1322 		return err;	
  1359 		}
  1323 		}
  1360 	aDbFile.iDeviceCharacteristics = TVfs::DoGetDeviceCharacteristics(driveInfo, volumeInfo);
  1324 	aDbFile.iDeviceCharacteristics = TVfs::DoGetDeviceCharacteristics(driveInfo, volumeInfo);
  1361 	aDbFile.iSectorSize = TVfs::DoGetSectorSize(driveInfo, volumeInfo);
  1325 	aDbFile.iSectorSize = TVfs::DoGetSectorSize(driveInfo, volumeInfo);
  1362 	aRecReadBufSize = volumeInfo.iRecReadBufSize;
  1326 	aRecReadBufSize = volumeInfo.iRecReadBufSize;
  1363 	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));
       
  1364 	return KErrNone;
  1327 	return KErrNone;
  1365 	}
  1328 	}
  1366 
  1329 
  1367 //Creates a temporary file. The file location will be the application's session path. 
  1330 //Creates a temporary file. The file location will be the application's session path. 
  1368 //If the session path does not exist, then the function will create the session path.
  1331 //If the session path does not exist, then the function will create the session path.
  1392 				}
  1355 				}
  1393 			}
  1356 			}
  1394 		}
  1357 		}
  1395 	return err;
  1358 	return err;
  1396 	}
  1359 	}
  1397 
       
  1398 /**
       
  1399 SQLite OS porting layer API.
       
  1400 
       
  1401 The behaviour of the RFile/RFile64::SetSize operation is not atomic for non-rugged drives. 
       
  1402 When RFile/RFile64::SetSize() is called 2 operations occurs:-
       
  1403 
       
  1404 1)The cluster chain of the file is updated.
       
  1405 2)The new file size is added to the file cache.
       
  1406 
       
  1407 If a power loss occurs after a SetSize there is a chance that the cluster chain was updated 
       
  1408 but the new file size is not yet flushed to the file. This puts the file into an inconsistent state.
       
  1409 This is most likely to occur in the journal file where the time between a SetSize and Flush can 
       
  1410 be long. 
       
  1411 
       
  1412 For this reason this check is added when the file is opened to see if the end of the file can 
       
  1413 be read straight away, if an error is returned then it is assumed that the SetSize has not be 
       
  1414 completed previously. In this case the file is deleted and re-created.
       
  1415  
       
  1416 @param aDbFile A pointer to a TDbFile instance, that contains the file handle.
       
  1417 @param aFname A string of 16-bit wide characters containing name of the file to be checked.
       
  1418 @param aFmode The mode in which the file is opened. These mode are documented in TFileMode.
       
  1419 
       
  1420 @return KErrNone,          The operation has completed succesfully;
       
  1421                            Note that other system-wide error codes may also be returned.
       
  1422 @see TFileMode
       
  1423 @see TVfs::Open()
       
  1424 @see TDbFile
       
  1425 */
       
  1426 /* static */ TInt TVfs::DoFileSizeCorruptionCheck(TDbFile& aDbFile, const TDesC& aFname, TInt aFmode)
       
  1427     {
       
  1428     const TInt KMinSize = 16;
       
  1429     TInt64 size;
       
  1430     TInt err = KErrNone ;
       
  1431     TBuf8<KMinSize> buf;
       
  1432 
       
  1433     err = aDbFile.iFileBuf.Size(size);
       
  1434     if (err != KErrNone)
       
  1435         {
       
  1436         return err;
       
  1437         }
       
  1438     TBool IsMinFileSize = (size >= KMinSize);
       
  1439     
       
  1440     if (IsMinFileSize)
       
  1441         {
       
  1442         err = aDbFile.iFileBuf.Read(size - KMinSize, buf);
       
  1443         }
       
  1444     
       
  1445     if (err == KErrCorrupt || err == KErrEof || !IsMinFileSize)
       
  1446         {
       
  1447         aDbFile.iFileBuf.Close();
       
  1448         __SQLITETRACE_OSEXPR(TInt err2 =) TStaticFs::Fs().Delete(aFname);
       
  1449 		SQLITE_TRACE_OS(OstTraceExt4(TRACE_INTERNALS, TVFS_DOFILESIZECORRUPTIONCHECK1, "OS;0x%X;TVfs::DoFileSizeCorruptionCheck;size=%lld;err=%d;deleteErr=%d", (TUint)&aDbFile, size, err, err2));
       
  1450         err = aDbFile.iFileBuf.Create(TStaticFs::Fs(), aFname, aFmode);
       
  1451 		SQLITE_TRACE_OS(OstTraceExt2(TRACE_INTERNALS, TVFS_DOFILESIZECORRUPTIONCHECK2, "OS;0x%X;TVfs::DoFileSizeCorruptionCheck;createErr=%d", (TUint)&aDbFile, err));
       
  1452         }
       
  1453     return err;
       
  1454     }
       
  1455 
  1360 
  1456 /**
  1361 /**
  1457 SQLite OS porting layer API.
  1362 SQLite OS porting layer API.
  1458 
  1363 
  1459 Opens or creates a file which name is in the aFileName parameter.
  1364 Opens or creates a file which name is in the aFileName parameter.
  1481 	    
  1386 	    
  1482 @see TDbFile
  1387 @see TDbFile
  1483 */
  1388 */
  1484 /* static */ int TVfs::Open(sqlite3_vfs* aVfs, const char* aFileName, sqlite3_file* aDbFile, int aFlags, int* aOutFlags)
  1389 /* static */ int TVfs::Open(sqlite3_vfs* aVfs, const char* aFileName, sqlite3_file* aDbFile, int aFlags, int* aOutFlags)
  1485 	{
  1390 	{
       
  1391 	SQLUTRACE_PROFILER(aVfs);
  1486 	TFileName fname;
  1392 	TFileName fname;
  1487 	if(aFileName && !::ConvertToUnicode(aFileName, fname))
  1393 	if(aFileName && !::ConvertToUnicode(aFileName, fname))
  1488 		{
  1394 		{
  1489 		SQLITE_TRACE_OS(OstTrace0(TRACE_INTERNALS, TVFS_OPEN1, "OS;0;TVfs::Open;ConvertToUnicode() failed"));
       
  1490 		return SQLITE_CANTOPEN;	
  1395 		return SQLITE_CANTOPEN;	
  1491 		}
  1396 		}
       
  1397 	SYMBIAN_TRACE_SQLITE_EVENTS_ONLY(UTF::Printf(UTF::TTraceContext(UTF::EInternals), KFileOpen, aDbFile, &fname));
  1492 	new (aDbFile) TDbFile;
  1398 	new (aDbFile) TDbFile;
  1493 	TDbFile& dbFile = ::DbFile(aDbFile);
  1399 	TDbFile& dbFile = ::DbFile(aDbFile);
  1494 	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));
       
  1495 	if(aFileName && (aFlags & SQLITE_OPEN_DELETEONCLOSE))
  1400 	if(aFileName && (aFlags & SQLITE_OPEN_DELETEONCLOSE))
  1496 		{
  1401 		{
  1497 		dbFile.iFullName = fname.Alloc();
  1402 		dbFile.iFullName = fname.Alloc();
  1498 		if(!dbFile.iFullName)
  1403 		if(!dbFile.iFullName)
  1499 			{
  1404 			{
  1500 			SQLITE_TRACE_OS(OstTrace0(TRACE_INTERNALS, TVFS_OPEN2, "OS;0;TVfs::Open;fname.Alloc() failed"));
       
  1501 			return SQLITE_IOERR_NOMEM;
  1405 			return SQLITE_IOERR_NOMEM;
  1502 			}
  1406 			}
  1503 		}
  1407 		}
  1504 	TInt recReadBufSize = -1;
  1408 	TInt recReadBufSize = -1;
  1505 	TInt err = KErrNone;
  1409 	TInt err = KErrNone;
  1521 		{//Create temporary file
  1425 		{//Create temporary file
  1522 		err = ::CreateTempFile(dbFile, fname, fmode);
  1426 		err = ::CreateTempFile(dbFile, fname, fmode);
  1523 		}
  1427 		}
  1524 	else
  1428 	else
  1525 		{
  1429 		{
  1526 		err = KErrAccessDenied;//The error has to be set here, because, there is a case where none of the file create/open operations will be executed
  1430 		err = KErrGeneral;//The error has to be set here, because, there is case where none of the file create/open operations will be executed
  1527 		TInt prevErr = KErrNone;
       
  1528 		if(aFlags & SQLITE_OPEN_CREATE)
  1431 		if(aFlags & SQLITE_OPEN_CREATE)
  1529 			{
  1432 			{
  1530 			prevErr = err = dbFile.iFileBuf.Create(TStaticFs::Fs(), fname, fmode);
  1433 			err = dbFile.iFileBuf.Create(TStaticFs::Fs(), fname, fmode);
  1531 			}
  1434 			}
  1532 		if(err != KErrNone && err != KErrNoMemory && err != KErrDiskFull)
  1435 		if(err != KErrNone && err != KErrNoMemory)
  1533 			{
  1436 			{
  1534 			err = dbFile.iFileBuf.Open(TStaticFs::Fs(), fname, fmode);
  1437 			err = dbFile.iFileBuf.Open(TStaticFs::Fs(), fname, fmode);
  1535 			if(err == KErrNone && (aFlags & KJournalFileTypeBitMask))
  1438 			}
  1536 			    {
  1439 		if((err != KErrNone && err != KErrNoMemory) && (aFlags & SQLITE_OPEN_READWRITE))
  1537                 err = TVfs::DoFileSizeCorruptionCheck(dbFile, fname, fmode);
       
  1538 			    }
       
  1539 			}
       
  1540 		if((err != KErrNone && err != KErrNoMemory && err != KErrDiskFull) && (aFlags & SQLITE_OPEN_READWRITE))
       
  1541 			{
  1440 			{
  1542 			aFlags &= ~SQLITE_OPEN_READWRITE;
  1441 			aFlags &= ~SQLITE_OPEN_READWRITE;
  1543 			aFlags |= SQLITE_OPEN_READONLY;
  1442 			aFlags |= SQLITE_OPEN_READONLY;
  1544 			fmode &= ~EFileWrite;
  1443 			fmode &= ~EFileWrite;
  1545 			err = dbFile.iFileBuf.Open(TStaticFs::Fs(), fname, fmode);
  1444 			err = dbFile.iFileBuf.Open(TStaticFs::Fs(), fname, fmode);
  1546 			}
  1445 			}
  1547 		if(err != KErrNone && prevErr == KErrAccessDenied)
       
  1548 			{
       
  1549 			err = KErrAccessDenied;
       
  1550 			}
       
  1551 		}
  1446 		}
  1552 	if(err == KErrNone)
  1447 	if(err == KErrNone)
  1553 		{
  1448 		{
  1554 		err = TVfs::DoGetDeviceCharacteristicsAndSectorSize(dbFile, recReadBufSize);
  1449 		err = TVfs::DoGetDeviceCharacteristicsAndSectorSize(dbFile, recReadBufSize);
  1555 		}
  1450 		}
  1556 	if(err != KErrNone)
  1451 	if(err != KErrNone)
  1557 		{
  1452 		{
  1558 		dbFile.iFileBuf.Close();	
  1453 		dbFile.iFileBuf.Close();	
  1559 		delete dbFile.iFullName;
  1454 		delete dbFile.iFullName;
  1560 		dbFile.iFullName = NULL;
  1455 		dbFile.iFullName = NULL;
  1561         if(!aFileName && fname.Length() > 0)
       
  1562             {//temporary file, the error is not KErrNone. Then delete the file (after a successfull 
       
  1563              //temporary file creation there could be a failed memory allocation)
       
  1564             (void)TStaticFs::Fs().Delete(fname);
       
  1565             }
       
  1566 		}
  1456 		}
  1567 	else
  1457 	else
  1568 		{
  1458 		{
  1569 		dbFile.pMethods = &TheFileIoApi;
  1459 		dbFile.pMethods = &TheFileIoApi;
  1570 		dbFile.iReadOnly = !(aFlags & SQLITE_OPEN_READWRITE);
  1460 		dbFile.iReadOnly = !(aFlags & SQLITE_OPEN_READWRITE);
  1573 			*aOutFlags = dbFile.iReadOnly ? SQLITE_OPEN_READONLY : SQLITE_OPEN_READWRITE;
  1463 			*aOutFlags = dbFile.iReadOnly ? SQLITE_OPEN_READONLY : SQLITE_OPEN_READWRITE;
  1574 			}
  1464 			}
  1575 		(void)dbFile.iFileBuf.SetReadAheadSize(dbFile.iSectorSize, recReadBufSize);
  1465 		(void)dbFile.iFileBuf.SetReadAheadSize(dbFile.iSectorSize, recReadBufSize);
  1576 		OpenCounter(+1);
  1466 		OpenCounter(+1);
  1577 		}
  1467 		}
  1578 	TInt sqliteErr = ::Os2SqliteErr(err, SQLITE_CANTOPEN);
  1468 	return ::Os2SqliteErr(err, SQLITE_CANTOPEN);
  1579 	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));
       
  1580 	return sqliteErr;
       
  1581 	}
  1469 	}
  1582 
  1470 
  1583 /**
  1471 /**
  1584 SQLite OS porting layer API.
  1472 SQLite OS porting layer API.
  1585 
  1473 
  1593 	    SQLITE_IOERR_DELETE,The delete file operation has failed;
  1481 	    SQLITE_IOERR_DELETE,The delete file operation has failed;
  1594 	    SQLITE_OK,			The operation has completed successfully.
  1482 	    SQLITE_OK,			The operation has completed successfully.
  1595 */
  1483 */
  1596 /* static */ int TVfs::Delete(sqlite3_vfs* aVfs, const char* aFileName, int /*aSyncDir*/)
  1484 /* static */ int TVfs::Delete(sqlite3_vfs* aVfs, const char* aFileName, int /*aSyncDir*/)
  1597 	{
  1485 	{
       
  1486 	SQLUTRACE_PROFILER(aVfs);
  1598 	SimulateIOError(return SQLITE_IOERR_DELETE);
  1487 	SimulateIOError(return SQLITE_IOERR_DELETE);
  1599 	TFileName fname;
  1488 	TFileName fname;
  1600 	if(!::ConvertToUnicode(aFileName, fname))
  1489 	if(!::ConvertToUnicode(aFileName, fname))
  1601 		{
  1490 		{
  1602 		SQLITE_TRACE_OS(OstTrace0(TRACE_INTERNALS, TVFS_DELETE1, "OS;0;TVfs::Delete;ConvertToUnicode() failed"));
       
  1603 		return SQLITE_ERROR;	
  1491 		return SQLITE_ERROR;	
  1604 		}
  1492 		}
       
  1493 	SYMBIAN_TRACE_SQLITE_EVENTS_ONLY(UTF::Printf(UTF::TTraceContext(UTF::EInternals), KFileName, &fname));
  1605 	TInt err = TStaticFs::Fs().Delete(fname);
  1494 	TInt err = TStaticFs::Fs().Delete(fname);
  1606 	TInt sqliteErr = ::Os2SqliteErr(err, SQLITE_IOERR_DELETE);
  1495 	return ::Os2SqliteErr(err, SQLITE_IOERR_DELETE);
  1607 	SQLITE_TRACE_OS(OstTraceExt2(TRACE_INTERNALS, TVFS_DELETE2, "OS;0;TVfs::Delete;err=%d;sqliteErr=%d", err, sqliteErr));
       
  1608 	return sqliteErr;
       
  1609 	}
  1496 	}
  1610 
  1497 
  1611 /**
  1498 /**
  1612 SQLite OS porting layer API.
  1499 SQLite OS porting layer API.
  1613 
  1500 
  1622 		SQLITE_IOERR_NOMEM, An out of memory conditon has occured,
  1509 		SQLITE_IOERR_NOMEM, An out of memory conditon has occured,
  1623 		SQLITE_IOERR_ACCESS,File I/O error;  
  1510 		SQLITE_IOERR_ACCESS,File I/O error;  
  1624 */
  1511 */
  1625 /* static */ int TVfs::Access(sqlite3_vfs* aVfs, const char* aFileName, int aFlags, int* aResOut)
  1512 /* static */ int TVfs::Access(sqlite3_vfs* aVfs, const char* aFileName, int aFlags, int* aResOut)
  1626 	{
  1513 	{
       
  1514 	SQLUTRACE_PROFILER(aVfs);
  1627 	TFileName fname;
  1515 	TFileName fname;
  1628 	if(!::ConvertToUnicode(aFileName, fname))
  1516 	if(!::ConvertToUnicode(aFileName, fname))
  1629 		{
  1517 		{
  1630 		SQLITE_TRACE_OS(OstTrace0(TRACE_INTERNALS, TVFS_ACCESS1, "OS;0;TVfs::Access;ConvertToUnicode() failed"));
       
  1631 		return SQLITE_IOERR_ACCESS;
  1518 		return SQLITE_IOERR_ACCESS;
  1632 		}
  1519 		}
  1633 	SQLITE_TRACE_OS(OstTraceExt2(TRACE_INTERNALS, TVFS_ACCESS_ENTRY, "OS-Entry;0;TVfs::Access;fname=%S;aFlags=0x%X", __SQLITEPRNSTR(fname), (TUint)aFlags));
  1520 	SYMBIAN_TRACE_SQLITE_EVENTS_ONLY(UTF::Printf(UTF::TTraceContext(UTF::EInternals), KFileName, &fname));
  1634 	TEntry entry;
  1521 	TEntry entry;
  1635 	TInt err = TStaticFs::Fs().Entry(fname, entry);
  1522 	TInt err = TStaticFs::Fs().Entry(fname, entry);
  1636 	if(aFlags == SQLITE_ACCESS_EXISTS && err == KErrNotFound)
  1523 	if(aFlags == SQLITE_ACCESS_EXISTS && err == KErrNotFound)
  1637 		{
  1524 		{
  1638 		*aResOut = 0;
  1525 		*aResOut = 0;
  1639 		SQLITE_TRACE_OS(OstTrace0(TRACE_INTERNALS, TVFS_ACCESS_EXIT1, "OS-Exit;0;TVfs::Access;Exists-NoFound"));
       
  1640 		return SQLITE_OK;
  1526 		return SQLITE_OK;
  1641 		}
  1527 		}
  1642 	if(err != KErrNone)
  1528 	if(err != KErrNone)
  1643 		{
  1529 		{
  1644 		SQLITE_TRACE_OS(OstTrace1(TRACE_INTERNALS, TVFS_ACCESS_EXIT2, "OS-Exit;0;TVfs::Access;err=%d", err));
       
  1645 		return err == KErrNoMemory ? SQLITE_IOERR_NOMEM : SQLITE_IOERR_ACCESS;
  1530 		return err == KErrNoMemory ? SQLITE_IOERR_NOMEM : SQLITE_IOERR_ACCESS;
  1646 		}
  1531 		}
  1647 	*aResOut = 0;
  1532 	*aResOut = 0;
  1648 	switch(aFlags)
  1533 	switch(aFlags)
  1649 		{
  1534 		{
  1657 			*aResOut = !entry.IsReadOnly();
  1542 			*aResOut = !entry.IsReadOnly();
  1658 			break;
  1543 			break;
  1659 		default:
  1544 		default:
  1660 			break;			
  1545 			break;			
  1661 		}
  1546 		}
  1662 	SQLITE_TRACE_OS(OstTrace1(TRACE_INTERNALS, TVFS_ACCESS_EXIT3, "OS-Exit;0;TVfs::Access;aResOut=%d", *aResOut));
       
  1663 	return SQLITE_OK;
  1547 	return SQLITE_OK;
  1664 	}
  1548 	}
  1665 
  1549 
  1666 /**
  1550 /**
  1667 SQLite OS porting layer API.
  1551 SQLite OS porting layer API.
  1689 @return SQLITE_ERROR, The aRelative parameter is NULL or cannot be converted to UTF16;
  1573 @return SQLITE_ERROR, The aRelative parameter is NULL or cannot be converted to UTF16;
  1690 		SQLITE_OK The operation has completed successfully.
  1574 		SQLITE_OK The operation has completed successfully.
  1691 */
  1575 */
  1692 /* static */ int TVfs::FullPathName(sqlite3_vfs* aVfs, const char* aRelative, int aBufLen, char* aBuf)
  1576 /* static */ int TVfs::FullPathName(sqlite3_vfs* aVfs, const char* aRelative, int aBufLen, char* aBuf)
  1693 	{
  1577 	{
       
  1578 	SQLUTRACE_PROFILER(aVfs);
  1694 	if(!aRelative)	//NULL argument
  1579 	if(!aRelative)	//NULL argument
  1695 		{
  1580 		{
  1696 		SQLITE_TRACE_OS(OstTrace0(TRACE_INTERNALS, TVFS_FULLPATHNAME1, "OS;0;TVfs::FullPathName;err=SQLITE_ERROR"));
       
  1697 		return SQLITE_ERROR;
  1581 		return SQLITE_ERROR;
  1698 		}
  1582 		}
  1699 	//Convert the received file name to UTF16
  1583 	//Convert the received file name to UTF16
  1700 	TBuf<KMaxFileName + 1> fname;
  1584 	TBuf<KMaxFileName + 1> fname;
  1701 	if(!::ConvertToUnicode(aRelative, fname))
  1585 	if(!::ConvertToUnicode(aRelative, fname))
  1702 		{
  1586 		{
  1703 		SQLITE_TRACE_OS(OstTrace0(TRACE_INTERNALS, TVFS_FULLPATHNAME_EXIT1, "OS-Exit;0;TVfs::FullPathName;ConvertToUnicode() failed"));
       
  1704 		return SQLITE_ERROR;
  1587 		return SQLITE_ERROR;
  1705 		}	
  1588 		}	
  1706 	SQLITE_TRACE_OS(OstTraceExt2(TRACE_INTERNALS, TVFS_FULLPATHNAME_ENTRY, "OS-Entry;0;TVfs::FullPathName;fname=%S;aBufLen=%d", __SQLITEPRNSTR(fname), aBufLen));
  1589 	SYMBIAN_TRACE_SQLITE_EVENTS_ONLY(UTF::Printf(UTF::TTraceContext(UTF::EInternals), KFileName, &fname));
  1707 	//Search if the file name begins with ".\" - current directory
  1590 	//Search if the file name begins with ".\" - current directory
  1708 	if(fname.Find(KCwd) == 0)
  1591 	if(fname.Find(KCwd) == 0)
  1709 		{
  1592 		{
  1710 		fname.Delete(0, KCwd().Length());
  1593 		fname.Delete(0, KCwd().Length());
  1711 		}
  1594 		}
  1712 	fname.Append(TChar(0));//Zero-terminate the converted file name
  1595 	fname.Append(TChar(0));//Zero-terminate the converted file name
  1713 	TFileName defaultPath;
  1596 	TFileName defaultPath;
  1714 	TInt err = TStaticFs::Fs().SessionPath(defaultPath);
  1597 	TInt err = TStaticFs::Fs().SessionPath(defaultPath);
  1715 	if(err != KErrNone)
  1598 	if(err != KErrNone)
  1716 		{
  1599 		{
  1717 		SQLITE_TRACE_OS(OstTrace1(TRACE_INTERNALS, TVFS_FULLPATHNAME_EXIT4, "OS-Exit;0;TVfs::FullPathName;SessionPath() failed, err=%d", err));
       
  1718 		return SQLITE_ERROR;
  1600 		return SQLITE_ERROR;
  1719 		}
  1601 		}
  1720 	TParse parse;
  1602 	TParse parse;
  1721 	(void)parse.Set(fname, &defaultPath, 0);//If fname does not have a path, defaultPath will be used
  1603 	(void)parse.Set(fname, &defaultPath, 0);//If fname does not have a path, defaultPath will be used
  1722 	TPtr8 dest8(reinterpret_cast <TUint8*> (aBuf), aBufLen);	
  1604 	TPtr8 dest8(reinterpret_cast <TUint8*> (aBuf), aBufLen);	
  1723 	if(!::ConvertFromUnicode(parse.FullName(), dest8))
  1605 	if(!::ConvertFromUnicode(parse.FullName(), dest8))
  1724 		{
  1606 		{
  1725 		SQLITE_TRACE_OS(OstTrace0(TRACE_INTERNALS, TVFS_FULLPATHNAME_EXIT2, "OS-Exit;0;TVfs::FullPathName;ConvertFromUnicode() failed"));
       
  1726 		return SQLITE_ERROR;	
  1607 		return SQLITE_ERROR;	
  1727 		}
  1608 		}
  1728 	SQLITE_TRACE_OS(OstTrace0(TRACE_INTERNALS, TVFS_FULLPATHNAME_EXIT3, "OS-Exit;0;TVfs::FullPathName;err=SQLITE_OK"));
       
  1729 	return SQLITE_OK;
  1609 	return SQLITE_OK;
  1730 	}
  1610 	}
  1731 
  1611 
  1732 /**
  1612 /**
  1733 SQLite OS porting layer API.
  1613 SQLite OS porting layer API.
  1739 
  1619 
  1740 @return The length of the used part of the output buffer.
  1620 @return The length of the used part of the output buffer.
  1741 */
  1621 */
  1742 /* static */ int TVfs::Randomness(sqlite3_vfs* aVfs, int aBufLen, char* aBuf)
  1622 /* static */ int TVfs::Randomness(sqlite3_vfs* aVfs, int aBufLen, char* aBuf)
  1743 	{
  1623 	{
       
  1624 	SQLUTRACE_PROFILER(aVfs);
  1744 	const TInt KRandIterations = aBufLen / sizeof(int);
  1625 	const TInt KRandIterations = aBufLen / sizeof(int);
  1745 	for(TInt i=0;i<KRandIterations;++i)
  1626 	for(TInt i=0;i<KRandIterations;++i)
  1746 		{
  1627 		{
  1747 		TInt val = Math::Rand(Seed());
  1628 		TInt val = Math::Rand(Seed());
  1748 		Mem::Copy(&aBuf[i * sizeof(int)], &val, sizeof(val));
  1629 		Mem::Copy(&aBuf[i * sizeof(int)], &val, sizeof(val));
  1759 
  1640 
  1760 @return The aMicrosec value.
  1641 @return The aMicrosec value.
  1761 */
  1642 */
  1762 /* static */ int TVfs::Sleep(sqlite3_vfs* aVfs, int aMicrosec)
  1643 /* static */ int TVfs::Sleep(sqlite3_vfs* aVfs, int aMicrosec)
  1763 	{
  1644 	{
       
  1645 	SQLUTRACE_PROFILER(aVfs);
  1764 	User::AfterHighRes(TTimeIntervalMicroSeconds32(aMicrosec));
  1646 	User::AfterHighRes(TTimeIntervalMicroSeconds32(aMicrosec));
  1765 	return aMicrosec;
  1647 	return aMicrosec;
  1766 	}
  1648 	}
  1767 
  1649 
  1768 /**
  1650 /**
  1776 
  1658 
  1777 @return 0.
  1659 @return 0.
  1778 */
  1660 */
  1779 /* static */ int TVfs::CurrentTime(sqlite3_vfs* aVfs, double* aNow)
  1661 /* static */ int TVfs::CurrentTime(sqlite3_vfs* aVfs, double* aNow)
  1780 	{
  1662 	{
       
  1663 	SQLUTRACE_PROFILER(aVfs);
  1781 	TTime now;
  1664 	TTime now;
  1782 	now.UniversalTime();
  1665 	now.UniversalTime();
  1783 	TDateTime date = now.DateTime();
  1666 	TDateTime date = now.DateTime();
  1784 	TInt year = date.Year();
  1667 	TInt year = date.Year();
  1785 	TInt month = date.Month() + 1;
  1668 	TInt month = date.Month() + 1;
  1815 
  1698 
  1816 @return 0.
  1699 @return 0.
  1817 */
  1700 */
  1818 /* static */int TVfs::GetLastError(sqlite3_vfs* aVfs, int /*aBufLen*/, char* /*aBuf*/)
  1701 /* static */int TVfs::GetLastError(sqlite3_vfs* aVfs, int /*aBufLen*/, char* /*aBuf*/)
  1819 	{
  1702 	{
       
  1703 	SQLUTRACE_PROFILER(aVfs);
  1820 	return 0;
  1704 	return 0;
  1821 	}
  1705 	}
  1822 
  1706 
  1823 ///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
  1707 ///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
  1824 
  1708