2008-02-13 18:25:27 +00:00
|
|
|
/*
|
2005-11-30 03:20:31 +00:00
|
|
|
** 2005 November 29
|
|
|
|
**
|
|
|
|
** The author disclaims copyright to this source code. In place of
|
|
|
|
** a legal notice, here is a blessing:
|
|
|
|
**
|
|
|
|
** May you do good and not evil.
|
|
|
|
** May you find forgiveness for yourself and forgive others.
|
|
|
|
** May you share freely, never taking more than you give.
|
|
|
|
**
|
|
|
|
******************************************************************************
|
|
|
|
**
|
|
|
|
** This file contains OS interface code that is common to all
|
|
|
|
** architectures.
|
2008-05-16 04:51:54 +00:00
|
|
|
**
|
2008-12-08 18:19:17 +00:00
|
|
|
** $Id: os.c,v 1.125 2008/12/08 18:19:18 drh Exp $
|
2005-11-30 03:20:31 +00:00
|
|
|
*/
|
2006-01-07 16:06:07 +00:00
|
|
|
#define _SQLITE_OS_C_ 1
|
2005-11-30 03:20:31 +00:00
|
|
|
#include "sqliteInt.h"
|
2007-04-05 21:58:33 +00:00
|
|
|
#undef _SQLITE_OS_C_
|
2005-11-30 03:20:31 +00:00
|
|
|
|
2007-10-03 08:46:44 +00:00
|
|
|
/*
|
|
|
|
** The default SQLite sqlite3_vfs implementations do not allocate
|
|
|
|
** memory (actually, os_unix.c allocates a small amount of memory
|
|
|
|
** from within OsOpen()), but some third-party implementations may.
|
|
|
|
** So we test the effects of a malloc() failing and the sqlite3OsXXX()
|
|
|
|
** function returning SQLITE_IOERR_NOMEM using the DO_OS_MALLOC_TEST macro.
|
|
|
|
**
|
|
|
|
** The following functions are instrumented for malloc() failure
|
|
|
|
** testing:
|
|
|
|
**
|
|
|
|
** sqlite3OsOpen()
|
|
|
|
** sqlite3OsRead()
|
|
|
|
** sqlite3OsWrite()
|
|
|
|
** sqlite3OsSync()
|
|
|
|
** sqlite3OsLock()
|
|
|
|
**
|
|
|
|
*/
|
2008-09-23 16:41:29 +00:00
|
|
|
#if defined(SQLITE_TEST) && (SQLITE_OS_WIN==0)
|
2007-10-03 08:46:44 +00:00
|
|
|
#define DO_OS_MALLOC_TEST if (1) { \
|
2008-06-15 02:51:47 +00:00
|
|
|
void *pTstAlloc = sqlite3Malloc(10); \
|
2007-10-03 08:46:44 +00:00
|
|
|
if (!pTstAlloc) return SQLITE_IOERR_NOMEM; \
|
|
|
|
sqlite3_free(pTstAlloc); \
|
|
|
|
}
|
|
|
|
#else
|
|
|
|
#define DO_OS_MALLOC_TEST
|
|
|
|
#endif
|
|
|
|
|
2005-11-30 03:20:31 +00:00
|
|
|
/*
|
|
|
|
** The following routines are convenience wrappers around methods
|
2007-08-15 17:08:46 +00:00
|
|
|
** of the sqlite3_file object. This is mostly just syntactic sugar. All
|
2005-11-30 03:20:31 +00:00
|
|
|
** of this would be completely automatic if SQLite were coded using
|
|
|
|
** C++ instead of plain old C.
|
|
|
|
*/
|
2007-08-17 15:53:36 +00:00
|
|
|
int sqlite3OsClose(sqlite3_file *pId){
|
2007-08-22 11:22:03 +00:00
|
|
|
int rc = SQLITE_OK;
|
|
|
|
if( pId->pMethods ){
|
|
|
|
rc = pId->pMethods->xClose(pId);
|
|
|
|
pId->pMethods = 0;
|
|
|
|
}
|
|
|
|
return rc;
|
2005-11-30 03:20:31 +00:00
|
|
|
}
|
2007-08-15 17:08:46 +00:00
|
|
|
int sqlite3OsRead(sqlite3_file *id, void *pBuf, int amt, i64 offset){
|
2007-10-03 08:46:44 +00:00
|
|
|
DO_OS_MALLOC_TEST;
|
2007-08-15 17:08:46 +00:00
|
|
|
return id->pMethods->xRead(id, pBuf, amt, offset);
|
2005-11-30 03:20:31 +00:00
|
|
|
}
|
2007-08-15 17:08:46 +00:00
|
|
|
int sqlite3OsWrite(sqlite3_file *id, const void *pBuf, int amt, i64 offset){
|
2007-10-03 08:46:44 +00:00
|
|
|
DO_OS_MALLOC_TEST;
|
2007-08-15 17:08:46 +00:00
|
|
|
return id->pMethods->xWrite(id, pBuf, amt, offset);
|
2005-11-30 03:20:31 +00:00
|
|
|
}
|
2007-08-15 17:08:46 +00:00
|
|
|
int sqlite3OsTruncate(sqlite3_file *id, i64 size){
|
|
|
|
return id->pMethods->xTruncate(id, size);
|
2005-11-30 03:20:31 +00:00
|
|
|
}
|
2007-08-17 16:50:38 +00:00
|
|
|
int sqlite3OsSync(sqlite3_file *id, int flags){
|
2007-10-03 08:46:44 +00:00
|
|
|
DO_OS_MALLOC_TEST;
|
2007-08-17 16:50:38 +00:00
|
|
|
return id->pMethods->xSync(id, flags);
|
2005-11-30 03:20:31 +00:00
|
|
|
}
|
2007-08-15 17:08:46 +00:00
|
|
|
int sqlite3OsFileSize(sqlite3_file *id, i64 *pSize){
|
2008-06-05 11:39:11 +00:00
|
|
|
DO_OS_MALLOC_TEST;
|
2007-08-15 17:08:46 +00:00
|
|
|
return id->pMethods->xFileSize(id, pSize);
|
2005-11-30 03:20:31 +00:00
|
|
|
}
|
2007-08-15 17:08:46 +00:00
|
|
|
int sqlite3OsLock(sqlite3_file *id, int lockType){
|
2007-10-03 08:46:44 +00:00
|
|
|
DO_OS_MALLOC_TEST;
|
2007-08-15 17:08:46 +00:00
|
|
|
return id->pMethods->xLock(id, lockType);
|
2005-11-30 03:20:31 +00:00
|
|
|
}
|
2007-08-15 17:08:46 +00:00
|
|
|
int sqlite3OsUnlock(sqlite3_file *id, int lockType){
|
|
|
|
return id->pMethods->xUnlock(id, lockType);
|
2005-11-30 03:20:31 +00:00
|
|
|
}
|
2008-06-05 11:39:11 +00:00
|
|
|
int sqlite3OsCheckReservedLock(sqlite3_file *id, int *pResOut){
|
|
|
|
DO_OS_MALLOC_TEST;
|
|
|
|
return id->pMethods->xCheckReservedLock(id, pResOut);
|
2005-11-30 03:20:31 +00:00
|
|
|
}
|
2007-08-31 16:11:35 +00:00
|
|
|
int sqlite3OsFileControl(sqlite3_file *id, int op, void *pArg){
|
2008-06-05 11:39:11 +00:00
|
|
|
return id->pMethods->xFileControl(id, op, pArg);
|
2007-08-31 16:11:35 +00:00
|
|
|
}
|
2008-01-22 11:50:13 +00:00
|
|
|
int sqlite3OsSectorSize(sqlite3_file *id){
|
|
|
|
int (*xSectorSize)(sqlite3_file*) = id->pMethods->xSectorSize;
|
|
|
|
return (xSectorSize ? xSectorSize(id) : SQLITE_DEFAULT_SECTOR_SIZE);
|
|
|
|
}
|
|
|
|
int sqlite3OsDeviceCharacteristics(sqlite3_file *id){
|
|
|
|
return id->pMethods->xDeviceCharacteristics(id);
|
|
|
|
}
|
2006-01-07 16:06:07 +00:00
|
|
|
|
2007-08-20 22:48:41 +00:00
|
|
|
/*
|
|
|
|
** The next group of routines are convenience wrappers around the
|
|
|
|
** VFS methods.
|
|
|
|
*/
|
2007-08-17 15:53:36 +00:00
|
|
|
int sqlite3OsOpen(
|
|
|
|
sqlite3_vfs *pVfs,
|
|
|
|
const char *zPath,
|
|
|
|
sqlite3_file *pFile,
|
|
|
|
int flags,
|
|
|
|
int *pFlagsOut
|
|
|
|
){
|
2007-10-03 08:46:44 +00:00
|
|
|
DO_OS_MALLOC_TEST;
|
2007-08-24 03:51:33 +00:00
|
|
|
return pVfs->xOpen(pVfs, zPath, pFile, flags, pFlagsOut);
|
2007-08-17 15:53:36 +00:00
|
|
|
}
|
2007-08-18 10:59:19 +00:00
|
|
|
int sqlite3OsDelete(sqlite3_vfs *pVfs, const char *zPath, int dirSync){
|
2007-08-24 03:51:33 +00:00
|
|
|
return pVfs->xDelete(pVfs, zPath, dirSync);
|
2007-08-17 15:53:36 +00:00
|
|
|
}
|
2008-06-05 11:39:11 +00:00
|
|
|
int sqlite3OsAccess(
|
|
|
|
sqlite3_vfs *pVfs,
|
|
|
|
const char *zPath,
|
|
|
|
int flags,
|
|
|
|
int *pResOut
|
|
|
|
){
|
|
|
|
DO_OS_MALLOC_TEST;
|
|
|
|
return pVfs->xAccess(pVfs, zPath, flags, pResOut);
|
2007-08-17 15:53:36 +00:00
|
|
|
}
|
2007-09-17 07:02:56 +00:00
|
|
|
int sqlite3OsFullPathname(
|
|
|
|
sqlite3_vfs *pVfs,
|
|
|
|
const char *zPath,
|
|
|
|
int nPathOut,
|
|
|
|
char *zPathOut
|
|
|
|
){
|
|
|
|
return pVfs->xFullPathname(pVfs, zPath, nPathOut, zPathOut);
|
2007-08-17 15:53:36 +00:00
|
|
|
}
|
2008-05-29 02:52:59 +00:00
|
|
|
#ifndef SQLITE_OMIT_LOAD_EXTENSION
|
2007-08-17 15:53:36 +00:00
|
|
|
void *sqlite3OsDlOpen(sqlite3_vfs *pVfs, const char *zPath){
|
2007-08-24 03:51:33 +00:00
|
|
|
return pVfs->xDlOpen(pVfs, zPath);
|
2007-08-17 15:53:36 +00:00
|
|
|
}
|
|
|
|
void sqlite3OsDlError(sqlite3_vfs *pVfs, int nByte, char *zBufOut){
|
2007-08-24 03:51:33 +00:00
|
|
|
pVfs->xDlError(pVfs, nByte, zBufOut);
|
2007-08-17 15:53:36 +00:00
|
|
|
}
|
2008-12-08 18:19:17 +00:00
|
|
|
void (*sqlite3OsDlSym(sqlite3_vfs *pVfs, void *pHdle, const char *zSym))(void){
|
|
|
|
return pVfs->xDlSym(pVfs, pHdle, zSym);
|
2007-08-17 15:53:36 +00:00
|
|
|
}
|
|
|
|
void sqlite3OsDlClose(sqlite3_vfs *pVfs, void *pHandle){
|
2007-08-24 03:51:33 +00:00
|
|
|
pVfs->xDlClose(pVfs, pHandle);
|
2007-08-17 15:53:36 +00:00
|
|
|
}
|
2008-05-29 02:52:59 +00:00
|
|
|
#endif /* SQLITE_OMIT_LOAD_EXTENSION */
|
2007-08-17 15:53:36 +00:00
|
|
|
int sqlite3OsRandomness(sqlite3_vfs *pVfs, int nByte, char *zBufOut){
|
2007-08-24 03:51:33 +00:00
|
|
|
return pVfs->xRandomness(pVfs, nByte, zBufOut);
|
2007-08-17 15:53:36 +00:00
|
|
|
}
|
|
|
|
int sqlite3OsSleep(sqlite3_vfs *pVfs, int nMicro){
|
2007-08-24 03:51:33 +00:00
|
|
|
return pVfs->xSleep(pVfs, nMicro);
|
2007-08-17 15:53:36 +00:00
|
|
|
}
|
|
|
|
int sqlite3OsCurrentTime(sqlite3_vfs *pVfs, double *pTimeOut){
|
2007-08-24 03:51:33 +00:00
|
|
|
return pVfs->xCurrentTime(pVfs, pTimeOut);
|
2007-08-17 15:53:36 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
int sqlite3OsOpenMalloc(
|
|
|
|
sqlite3_vfs *pVfs,
|
|
|
|
const char *zFile,
|
|
|
|
sqlite3_file **ppFile,
|
2007-08-20 14:23:44 +00:00
|
|
|
int flags,
|
|
|
|
int *pOutFlags
|
2007-08-17 15:53:36 +00:00
|
|
|
){
|
|
|
|
int rc = SQLITE_NOMEM;
|
|
|
|
sqlite3_file *pFile;
|
2008-06-15 02:51:47 +00:00
|
|
|
pFile = (sqlite3_file *)sqlite3Malloc(pVfs->szOsFile);
|
2007-08-17 15:53:36 +00:00
|
|
|
if( pFile ){
|
2007-08-20 14:23:44 +00:00
|
|
|
rc = sqlite3OsOpen(pVfs, zFile, pFile, flags, pOutFlags);
|
2007-08-17 15:53:36 +00:00
|
|
|
if( rc!=SQLITE_OK ){
|
|
|
|
sqlite3_free(pFile);
|
|
|
|
}else{
|
|
|
|
*ppFile = pFile;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return rc;
|
|
|
|
}
|
|
|
|
int sqlite3OsCloseFree(sqlite3_file *pFile){
|
|
|
|
int rc = SQLITE_OK;
|
2008-04-08 03:09:22 +00:00
|
|
|
assert( pFile );
|
|
|
|
rc = sqlite3OsClose(pFile);
|
|
|
|
sqlite3_free(pFile);
|
2007-08-17 15:53:36 +00:00
|
|
|
return rc;
|
|
|
|
}
|
|
|
|
|
2007-08-20 22:48:41 +00:00
|
|
|
/*
|
2008-06-25 17:19:00 +00:00
|
|
|
** The list of all registered VFS implementations.
|
2007-08-20 22:48:41 +00:00
|
|
|
*/
|
2008-09-02 17:18:51 +00:00
|
|
|
static sqlite3_vfs * SQLITE_WSD vfsList = 0;
|
2008-09-02 10:22:00 +00:00
|
|
|
#define vfsList GLOBAL(sqlite3_vfs *, vfsList)
|
2007-08-20 22:48:41 +00:00
|
|
|
|
|
|
|
/*
|
|
|
|
** Locate a VFS by name. If no name is given, simply return the
|
|
|
|
** first VFS on the list.
|
|
|
|
*/
|
|
|
|
sqlite3_vfs *sqlite3_vfs_find(const char *zVfs){
|
2008-06-19 16:07:07 +00:00
|
|
|
sqlite3_vfs *pVfs = 0;
|
2008-10-07 15:25:48 +00:00
|
|
|
#if SQLITE_THREADSAFE
|
2008-06-13 18:24:27 +00:00
|
|
|
sqlite3_mutex *mutex;
|
|
|
|
#endif
|
|
|
|
#ifndef SQLITE_OMIT_AUTOINIT
|
|
|
|
int rc = sqlite3_initialize();
|
|
|
|
if( rc ) return 0;
|
|
|
|
#endif
|
2008-10-07 15:25:48 +00:00
|
|
|
#if SQLITE_THREADSAFE
|
2008-06-18 17:09:10 +00:00
|
|
|
mutex = sqlite3MutexAlloc(SQLITE_MUTEX_STATIC_MASTER);
|
2008-01-23 03:03:05 +00:00
|
|
|
#endif
|
2007-08-20 22:48:41 +00:00
|
|
|
sqlite3_mutex_enter(mutex);
|
|
|
|
for(pVfs = vfsList; pVfs; pVfs=pVfs->pNext){
|
|
|
|
if( zVfs==0 ) break;
|
|
|
|
if( strcmp(zVfs, pVfs->zName)==0 ) break;
|
|
|
|
}
|
|
|
|
sqlite3_mutex_leave(mutex);
|
|
|
|
return pVfs;
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
** Unlink a VFS from the linked list
|
|
|
|
*/
|
|
|
|
static void vfsUnlink(sqlite3_vfs *pVfs){
|
2008-06-18 17:09:10 +00:00
|
|
|
assert( sqlite3_mutex_held(sqlite3MutexAlloc(SQLITE_MUTEX_STATIC_MASTER)) );
|
2007-10-23 14:49:59 +00:00
|
|
|
if( pVfs==0 ){
|
|
|
|
/* No-op */
|
|
|
|
}else if( vfsList==pVfs ){
|
2007-08-20 22:48:41 +00:00
|
|
|
vfsList = pVfs->pNext;
|
2007-10-23 14:49:59 +00:00
|
|
|
}else if( vfsList ){
|
2007-08-20 22:48:41 +00:00
|
|
|
sqlite3_vfs *p = vfsList;
|
|
|
|
while( p->pNext && p->pNext!=pVfs ){
|
|
|
|
p = p->pNext;
|
|
|
|
}
|
|
|
|
if( p->pNext==pVfs ){
|
|
|
|
p->pNext = pVfs->pNext;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
** Register a VFS with the system. It is harmless to register the same
|
|
|
|
** VFS multiple times. The new VFS becomes the default if makeDflt is
|
|
|
|
** true.
|
|
|
|
*/
|
|
|
|
int sqlite3_vfs_register(sqlite3_vfs *pVfs, int makeDflt){
|
2008-06-25 17:19:00 +00:00
|
|
|
sqlite3_mutex *mutex = 0;
|
2008-06-13 18:24:27 +00:00
|
|
|
#ifndef SQLITE_OMIT_AUTOINIT
|
|
|
|
int rc = sqlite3_initialize();
|
|
|
|
if( rc ) return rc;
|
|
|
|
#endif
|
2008-06-26 08:29:34 +00:00
|
|
|
mutex = sqlite3MutexAlloc(SQLITE_MUTEX_STATIC_MASTER);
|
2007-08-20 22:48:41 +00:00
|
|
|
sqlite3_mutex_enter(mutex);
|
|
|
|
vfsUnlink(pVfs);
|
|
|
|
if( makeDflt || vfsList==0 ){
|
|
|
|
pVfs->pNext = vfsList;
|
|
|
|
vfsList = pVfs;
|
|
|
|
}else{
|
|
|
|
pVfs->pNext = vfsList->pNext;
|
2007-09-01 06:51:27 +00:00
|
|
|
vfsList->pNext = pVfs;
|
2007-08-20 22:48:41 +00:00
|
|
|
}
|
2007-08-21 13:07:46 +00:00
|
|
|
assert(vfsList);
|
2007-08-20 22:48:41 +00:00
|
|
|
sqlite3_mutex_leave(mutex);
|
|
|
|
return SQLITE_OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
** Unregister a VFS so that it is no longer accessible.
|
|
|
|
*/
|
|
|
|
int sqlite3_vfs_unregister(sqlite3_vfs *pVfs){
|
2008-10-07 15:25:48 +00:00
|
|
|
#if SQLITE_THREADSAFE
|
2008-06-18 17:09:10 +00:00
|
|
|
sqlite3_mutex *mutex = sqlite3MutexAlloc(SQLITE_MUTEX_STATIC_MASTER);
|
2008-01-23 03:03:05 +00:00
|
|
|
#endif
|
2007-08-20 22:48:41 +00:00
|
|
|
sqlite3_mutex_enter(mutex);
|
|
|
|
vfsUnlink(pVfs);
|
|
|
|
sqlite3_mutex_leave(mutex);
|
|
|
|
return SQLITE_OK;
|
|
|
|
}
|