Blame src/os.c

Packit 87b942
/*
Packit 87b942
** 2005 November 29
Packit 87b942
**
Packit 87b942
** The author disclaims copyright to this source code.  In place of
Packit 87b942
** a legal notice, here is a blessing:
Packit 87b942
**
Packit 87b942
**    May you do good and not evil.
Packit 87b942
**    May you find forgiveness for yourself and forgive others.
Packit 87b942
**    May you share freely, never taking more than you give.
Packit 87b942
**
Packit 87b942
******************************************************************************
Packit 87b942
**
Packit 87b942
** This file contains OS interface code that is common to all
Packit 87b942
** architectures.
Packit 87b942
*/
Packit 87b942
#include "sqliteInt.h"
Packit 87b942
Packit 87b942
/*
Packit 87b942
** If we compile with the SQLITE_TEST macro set, then the following block
Packit 87b942
** of code will give us the ability to simulate a disk I/O error.  This
Packit 87b942
** is used for testing the I/O recovery logic.
Packit 87b942
*/
Packit 87b942
#if defined(SQLITE_TEST)
Packit 87b942
int sqlite3_io_error_hit = 0;            /* Total number of I/O Errors */
Packit 87b942
int sqlite3_io_error_hardhit = 0;        /* Number of non-benign errors */
Packit 87b942
int sqlite3_io_error_pending = 0;        /* Count down to first I/O error */
Packit 87b942
int sqlite3_io_error_persist = 0;        /* True if I/O errors persist */
Packit 87b942
int sqlite3_io_error_benign = 0;         /* True if errors are benign */
Packit 87b942
int sqlite3_diskfull_pending = 0;
Packit 87b942
int sqlite3_diskfull = 0;
Packit 87b942
#endif /* defined(SQLITE_TEST) */
Packit 87b942
Packit 87b942
/*
Packit 87b942
** When testing, also keep a count of the number of open files.
Packit 87b942
*/
Packit 87b942
#if defined(SQLITE_TEST)
Packit 87b942
int sqlite3_open_file_count = 0;
Packit 87b942
#endif /* defined(SQLITE_TEST) */
Packit 87b942
Packit 87b942
/*
Packit 87b942
** The default SQLite sqlite3_vfs implementations do not allocate
Packit 87b942
** memory (actually, os_unix.c allocates a small amount of memory
Packit 87b942
** from within OsOpen()), but some third-party implementations may.
Packit 87b942
** So we test the effects of a malloc() failing and the sqlite3OsXXX()
Packit 87b942
** function returning SQLITE_IOERR_NOMEM using the DO_OS_MALLOC_TEST macro.
Packit 87b942
**
Packit 87b942
** The following functions are instrumented for malloc() failure
Packit 87b942
** testing:
Packit 87b942
**
Packit 87b942
**     sqlite3OsRead()
Packit 87b942
**     sqlite3OsWrite()
Packit 87b942
**     sqlite3OsSync()
Packit 87b942
**     sqlite3OsFileSize()
Packit 87b942
**     sqlite3OsLock()
Packit 87b942
**     sqlite3OsCheckReservedLock()
Packit 87b942
**     sqlite3OsFileControl()
Packit 87b942
**     sqlite3OsShmMap()
Packit 87b942
**     sqlite3OsOpen()
Packit 87b942
**     sqlite3OsDelete()
Packit 87b942
**     sqlite3OsAccess()
Packit 87b942
**     sqlite3OsFullPathname()
Packit 87b942
**
Packit 87b942
*/
Packit 87b942
#if defined(SQLITE_TEST)
Packit 87b942
int sqlite3_memdebug_vfs_oom_test = 1;
Packit 87b942
  #define DO_OS_MALLOC_TEST(x)                                       \
Packit 87b942
  if (sqlite3_memdebug_vfs_oom_test && (!x || !sqlite3JournalIsInMemory(x))) { \
Packit 87b942
    void *pTstAlloc = sqlite3Malloc(10);                             \
Packit 87b942
    if (!pTstAlloc) return SQLITE_IOERR_NOMEM_BKPT;                  \
Packit 87b942
    sqlite3_free(pTstAlloc);                                         \
Packit 87b942
  }
Packit 87b942
#else
Packit 87b942
  #define DO_OS_MALLOC_TEST(x)
Packit 87b942
#endif
Packit 87b942
Packit 87b942
/*
Packit 87b942
** The following routines are convenience wrappers around methods
Packit 87b942
** of the sqlite3_file object.  This is mostly just syntactic sugar. All
Packit 87b942
** of this would be completely automatic if SQLite were coded using
Packit 87b942
** C++ instead of plain old C.
Packit 87b942
*/
Packit 87b942
void sqlite3OsClose(sqlite3_file *pId){
Packit 87b942
  if( pId->pMethods ){
Packit 87b942
    pId->pMethods->xClose(pId);
Packit 87b942
    pId->pMethods = 0;
Packit 87b942
  }
Packit 87b942
}
Packit 87b942
int sqlite3OsRead(sqlite3_file *id, void *pBuf, int amt, i64 offset){
Packit 87b942
  DO_OS_MALLOC_TEST(id);
Packit 87b942
  return id->pMethods->xRead(id, pBuf, amt, offset);
Packit 87b942
}
Packit 87b942
int sqlite3OsWrite(sqlite3_file *id, const void *pBuf, int amt, i64 offset){
Packit 87b942
  DO_OS_MALLOC_TEST(id);
Packit 87b942
  return id->pMethods->xWrite(id, pBuf, amt, offset);
Packit 87b942
}
Packit 87b942
int sqlite3OsTruncate(sqlite3_file *id, i64 size){
Packit 87b942
  return id->pMethods->xTruncate(id, size);
Packit 87b942
}
Packit 87b942
int sqlite3OsSync(sqlite3_file *id, int flags){
Packit 87b942
  DO_OS_MALLOC_TEST(id);
Packit 87b942
  return flags ? id->pMethods->xSync(id, flags) : SQLITE_OK;
Packit 87b942
}
Packit 87b942
int sqlite3OsFileSize(sqlite3_file *id, i64 *pSize){
Packit 87b942
  DO_OS_MALLOC_TEST(id);
Packit 87b942
  return id->pMethods->xFileSize(id, pSize);
Packit 87b942
}
Packit 87b942
int sqlite3OsLock(sqlite3_file *id, int lockType){
Packit 87b942
  DO_OS_MALLOC_TEST(id);
Packit 87b942
  return id->pMethods->xLock(id, lockType);
Packit 87b942
}
Packit 87b942
int sqlite3OsUnlock(sqlite3_file *id, int lockType){
Packit 87b942
  return id->pMethods->xUnlock(id, lockType);
Packit 87b942
}
Packit 87b942
int sqlite3OsCheckReservedLock(sqlite3_file *id, int *pResOut){
Packit 87b942
  DO_OS_MALLOC_TEST(id);
Packit 87b942
  return id->pMethods->xCheckReservedLock(id, pResOut);
Packit 87b942
}
Packit 87b942
Packit 87b942
/*
Packit 87b942
** Use sqlite3OsFileControl() when we are doing something that might fail
Packit 87b942
** and we need to know about the failures.  Use sqlite3OsFileControlHint()
Packit 87b942
** when simply tossing information over the wall to the VFS and we do not
Packit 87b942
** really care if the VFS receives and understands the information since it
Packit 87b942
** is only a hint and can be safely ignored.  The sqlite3OsFileControlHint()
Packit 87b942
** routine has no return value since the return value would be meaningless.
Packit 87b942
*/
Packit 87b942
int sqlite3OsFileControl(sqlite3_file *id, int op, void *pArg){
Packit 87b942
#ifdef SQLITE_TEST
Packit 87b942
  if( op!=SQLITE_FCNTL_COMMIT_PHASETWO ){
Packit 87b942
    /* Faults are not injected into COMMIT_PHASETWO because, assuming SQLite
Packit 87b942
    ** is using a regular VFS, it is called after the corresponding
Packit 87b942
    ** transaction has been committed. Injecting a fault at this point
Packit 87b942
    ** confuses the test scripts - the COMMIT comand returns SQLITE_NOMEM
Packit 87b942
    ** but the transaction is committed anyway.
Packit 87b942
    **
Packit 87b942
    ** The core must call OsFileControl() though, not OsFileControlHint(),
Packit 87b942
    ** as if a custom VFS (e.g. zipvfs) returns an error here, it probably
Packit 87b942
    ** means the commit really has failed and an error should be returned
Packit 87b942
    ** to the user.  */
Packit 87b942
    DO_OS_MALLOC_TEST(id);
Packit 87b942
  }
Packit 87b942
#endif
Packit 87b942
  return id->pMethods->xFileControl(id, op, pArg);
Packit 87b942
}
Packit 87b942
void sqlite3OsFileControlHint(sqlite3_file *id, int op, void *pArg){
Packit 87b942
  (void)id->pMethods->xFileControl(id, op, pArg);
Packit 87b942
}
Packit 87b942
Packit 87b942
int sqlite3OsSectorSize(sqlite3_file *id){
Packit 87b942
  int (*xSectorSize)(sqlite3_file*) = id->pMethods->xSectorSize;
Packit 87b942
  return (xSectorSize ? xSectorSize(id) : SQLITE_DEFAULT_SECTOR_SIZE);
Packit 87b942
}
Packit 87b942
int sqlite3OsDeviceCharacteristics(sqlite3_file *id){
Packit 87b942
  return id->pMethods->xDeviceCharacteristics(id);
Packit 87b942
}
Packit 87b942
#ifndef SQLITE_OMIT_WAL
Packit 87b942
int sqlite3OsShmLock(sqlite3_file *id, int offset, int n, int flags){
Packit 87b942
  return id->pMethods->xShmLock(id, offset, n, flags);
Packit 87b942
}
Packit 87b942
void sqlite3OsShmBarrier(sqlite3_file *id){
Packit 87b942
  id->pMethods->xShmBarrier(id);
Packit 87b942
}
Packit 87b942
int sqlite3OsShmUnmap(sqlite3_file *id, int deleteFlag){
Packit 87b942
  return id->pMethods->xShmUnmap(id, deleteFlag);
Packit 87b942
}
Packit 87b942
int sqlite3OsShmMap(
Packit 87b942
  sqlite3_file *id,               /* Database file handle */
Packit 87b942
  int iPage,
Packit 87b942
  int pgsz,
Packit 87b942
  int bExtend,                    /* True to extend file if necessary */
Packit 87b942
  void volatile **pp              /* OUT: Pointer to mapping */
Packit 87b942
){
Packit 87b942
  DO_OS_MALLOC_TEST(id);
Packit 87b942
  return id->pMethods->xShmMap(id, iPage, pgsz, bExtend, pp);
Packit 87b942
}
Packit 87b942
#endif /* SQLITE_OMIT_WAL */
Packit 87b942
Packit 87b942
#if SQLITE_MAX_MMAP_SIZE>0
Packit 87b942
/* The real implementation of xFetch and xUnfetch */
Packit 87b942
int sqlite3OsFetch(sqlite3_file *id, i64 iOff, int iAmt, void **pp){
Packit 87b942
  DO_OS_MALLOC_TEST(id);
Packit 87b942
  return id->pMethods->xFetch(id, iOff, iAmt, pp);
Packit 87b942
}
Packit 87b942
int sqlite3OsUnfetch(sqlite3_file *id, i64 iOff, void *p){
Packit 87b942
  return id->pMethods->xUnfetch(id, iOff, p);
Packit 87b942
}
Packit 87b942
#else
Packit 87b942
/* No-op stubs to use when memory-mapped I/O is disabled */
Packit 87b942
int sqlite3OsFetch(sqlite3_file *id, i64 iOff, int iAmt, void **pp){
Packit 87b942
  *pp = 0;
Packit 87b942
  return SQLITE_OK;
Packit 87b942
}
Packit 87b942
int sqlite3OsUnfetch(sqlite3_file *id, i64 iOff, void *p){
Packit 87b942
  return SQLITE_OK;
Packit 87b942
}
Packit 87b942
#endif
Packit 87b942
Packit 87b942
/*
Packit 87b942
** The next group of routines are convenience wrappers around the
Packit 87b942
** VFS methods.
Packit 87b942
*/
Packit 87b942
int sqlite3OsOpen(
Packit 87b942
  sqlite3_vfs *pVfs,
Packit 87b942
  const char *zPath,
Packit 87b942
  sqlite3_file *pFile,
Packit 87b942
  int flags,
Packit 87b942
  int *pFlagsOut
Packit 87b942
){
Packit 87b942
  int rc;
Packit 87b942
  DO_OS_MALLOC_TEST(0);
Packit 87b942
  /* 0x87f7f is a mask of SQLITE_OPEN_ flags that are valid to be passed
Packit 87b942
  ** down into the VFS layer.  Some SQLITE_OPEN_ flags (for example,
Packit 87b942
  ** SQLITE_OPEN_FULLMUTEX or SQLITE_OPEN_SHAREDCACHE) are blocked before
Packit 87b942
  ** reaching the VFS. */
Packit 87b942
  rc = pVfs->xOpen(pVfs, zPath, pFile, flags & 0x87f7f, pFlagsOut);
Packit 87b942
  assert( rc==SQLITE_OK || pFile->pMethods==0 );
Packit 87b942
  return rc;
Packit 87b942
}
Packit 87b942
int sqlite3OsDelete(sqlite3_vfs *pVfs, const char *zPath, int dirSync){
Packit 87b942
  DO_OS_MALLOC_TEST(0);
Packit 87b942
  assert( dirSync==0 || dirSync==1 );
Packit 87b942
  return pVfs->xDelete(pVfs, zPath, dirSync);
Packit 87b942
}
Packit 87b942
int sqlite3OsAccess(
Packit 87b942
  sqlite3_vfs *pVfs,
Packit 87b942
  const char *zPath,
Packit 87b942
  int flags,
Packit 87b942
  int *pResOut
Packit 87b942
){
Packit 87b942
  DO_OS_MALLOC_TEST(0);
Packit 87b942
  return pVfs->xAccess(pVfs, zPath, flags, pResOut);
Packit 87b942
}
Packit 87b942
int sqlite3OsFullPathname(
Packit 87b942
  sqlite3_vfs *pVfs,
Packit 87b942
  const char *zPath,
Packit 87b942
  int nPathOut,
Packit 87b942
  char *zPathOut
Packit 87b942
){
Packit 87b942
  DO_OS_MALLOC_TEST(0);
Packit 87b942
  zPathOut[0] = 0;
Packit 87b942
  return pVfs->xFullPathname(pVfs, zPath, nPathOut, zPathOut);
Packit 87b942
}
Packit 87b942
#ifndef SQLITE_OMIT_LOAD_EXTENSION
Packit 87b942
void *sqlite3OsDlOpen(sqlite3_vfs *pVfs, const char *zPath){
Packit 87b942
  return pVfs->xDlOpen(pVfs, zPath);
Packit 87b942
}
Packit 87b942
void sqlite3OsDlError(sqlite3_vfs *pVfs, int nByte, char *zBufOut){
Packit 87b942
  pVfs->xDlError(pVfs, nByte, zBufOut);
Packit 87b942
}
Packit 87b942
void (*sqlite3OsDlSym(sqlite3_vfs *pVfs, void *pHdle, const char *zSym))(void){
Packit 87b942
  return pVfs->xDlSym(pVfs, pHdle, zSym);
Packit 87b942
}
Packit 87b942
void sqlite3OsDlClose(sqlite3_vfs *pVfs, void *pHandle){
Packit 87b942
  pVfs->xDlClose(pVfs, pHandle);
Packit 87b942
}
Packit 87b942
#endif /* SQLITE_OMIT_LOAD_EXTENSION */
Packit 87b942
int sqlite3OsRandomness(sqlite3_vfs *pVfs, int nByte, char *zBufOut){
Packit 87b942
  return pVfs->xRandomness(pVfs, nByte, zBufOut);
Packit 87b942
}
Packit 87b942
int sqlite3OsSleep(sqlite3_vfs *pVfs, int nMicro){
Packit 87b942
  return pVfs->xSleep(pVfs, nMicro);
Packit 87b942
}
Packit 87b942
int sqlite3OsGetLastError(sqlite3_vfs *pVfs){
Packit 87b942
  return pVfs->xGetLastError ? pVfs->xGetLastError(pVfs, 0, 0) : 0;
Packit 87b942
}
Packit 87b942
int sqlite3OsCurrentTimeInt64(sqlite3_vfs *pVfs, sqlite3_int64 *pTimeOut){
Packit 87b942
  int rc;
Packit 87b942
  /* IMPLEMENTATION-OF: R-49045-42493 SQLite will use the xCurrentTimeInt64()
Packit 87b942
  ** method to get the current date and time if that method is available
Packit 87b942
  ** (if iVersion is 2 or greater and the function pointer is not NULL) and
Packit 87b942
  ** will fall back to xCurrentTime() if xCurrentTimeInt64() is
Packit 87b942
  ** unavailable.
Packit 87b942
  */
Packit 87b942
  if( pVfs->iVersion>=2 && pVfs->xCurrentTimeInt64 ){
Packit 87b942
    rc = pVfs->xCurrentTimeInt64(pVfs, pTimeOut);
Packit 87b942
  }else{
Packit 87b942
    double r;
Packit 87b942
    rc = pVfs->xCurrentTime(pVfs, &r);
Packit 87b942
    *pTimeOut = (sqlite3_int64)(r*86400000.0);
Packit 87b942
  }
Packit 87b942
  return rc;
Packit 87b942
}
Packit 87b942
Packit 87b942
int sqlite3OsOpenMalloc(
Packit 87b942
  sqlite3_vfs *pVfs,
Packit 87b942
  const char *zFile,
Packit 87b942
  sqlite3_file **ppFile,
Packit 87b942
  int flags,
Packit 87b942
  int *pOutFlags
Packit 87b942
){
Packit 87b942
  int rc;
Packit 87b942
  sqlite3_file *pFile;
Packit 87b942
  pFile = (sqlite3_file *)sqlite3MallocZero(pVfs->szOsFile);
Packit 87b942
  if( pFile ){
Packit 87b942
    rc = sqlite3OsOpen(pVfs, zFile, pFile, flags, pOutFlags);
Packit 87b942
    if( rc!=SQLITE_OK ){
Packit 87b942
      sqlite3_free(pFile);
Packit 87b942
    }else{
Packit 87b942
      *ppFile = pFile;
Packit 87b942
    }
Packit 87b942
  }else{
Packit 87b942
    rc = SQLITE_NOMEM_BKPT;
Packit 87b942
  }
Packit 87b942
  return rc;
Packit 87b942
}
Packit 87b942
void sqlite3OsCloseFree(sqlite3_file *pFile){
Packit 87b942
  assert( pFile );
Packit 87b942
  sqlite3OsClose(pFile);
Packit 87b942
  sqlite3_free(pFile);
Packit 87b942
}
Packit 87b942
Packit 87b942
/*
Packit 87b942
** This function is a wrapper around the OS specific implementation of
Packit 87b942
** sqlite3_os_init(). The purpose of the wrapper is to provide the
Packit 87b942
** ability to simulate a malloc failure, so that the handling of an
Packit 87b942
** error in sqlite3_os_init() by the upper layers can be tested.
Packit 87b942
*/
Packit 87b942
int sqlite3OsInit(void){
Packit 87b942
  void *p = sqlite3_malloc(10);
Packit 87b942
  if( p==0 ) return SQLITE_NOMEM_BKPT;
Packit 87b942
  sqlite3_free(p);
Packit 87b942
  return sqlite3_os_init();
Packit 87b942
}
Packit 87b942
Packit 87b942
/*
Packit 87b942
** The list of all registered VFS implementations.
Packit 87b942
*/
Packit 87b942
static sqlite3_vfs * SQLITE_WSD vfsList = 0;
Packit 87b942
#define vfsList GLOBAL(sqlite3_vfs *, vfsList)
Packit 87b942
Packit 87b942
/*
Packit 87b942
** Locate a VFS by name.  If no name is given, simply return the
Packit 87b942
** first VFS on the list.
Packit 87b942
*/
Packit 87b942
sqlite3_vfs *sqlite3_vfs_find(const char *zVfs){
Packit 87b942
  sqlite3_vfs *pVfs = 0;
Packit 87b942
#if SQLITE_THREADSAFE
Packit 87b942
  sqlite3_mutex *mutex;
Packit 87b942
#endif
Packit 87b942
#ifndef SQLITE_OMIT_AUTOINIT
Packit 87b942
  int rc = sqlite3_initialize();
Packit 87b942
  if( rc ) return 0;
Packit 87b942
#endif
Packit 87b942
#if SQLITE_THREADSAFE
Packit 87b942
  mutex = sqlite3MutexAlloc(SQLITE_MUTEX_STATIC_MASTER);
Packit 87b942
#endif
Packit 87b942
  sqlite3_mutex_enter(mutex);
Packit 87b942
  for(pVfs = vfsList; pVfs; pVfs=pVfs->pNext){
Packit 87b942
    if( zVfs==0 ) break;
Packit 87b942
    if( strcmp(zVfs, pVfs->zName)==0 ) break;
Packit 87b942
  }
Packit 87b942
  sqlite3_mutex_leave(mutex);
Packit 87b942
  return pVfs;
Packit 87b942
}
Packit 87b942
Packit 87b942
/*
Packit 87b942
** Unlink a VFS from the linked list
Packit 87b942
*/
Packit 87b942
static void vfsUnlink(sqlite3_vfs *pVfs){
Packit 87b942
  assert( sqlite3_mutex_held(sqlite3MutexAlloc(SQLITE_MUTEX_STATIC_MASTER)) );
Packit 87b942
  if( pVfs==0 ){
Packit 87b942
    /* No-op */
Packit 87b942
  }else if( vfsList==pVfs ){
Packit 87b942
    vfsList = pVfs->pNext;
Packit 87b942
  }else if( vfsList ){
Packit 87b942
    sqlite3_vfs *p = vfsList;
Packit 87b942
    while( p->pNext && p->pNext!=pVfs ){
Packit 87b942
      p = p->pNext;
Packit 87b942
    }
Packit 87b942
    if( p->pNext==pVfs ){
Packit 87b942
      p->pNext = pVfs->pNext;
Packit 87b942
    }
Packit 87b942
  }
Packit 87b942
}
Packit 87b942
Packit 87b942
/*
Packit 87b942
** Register a VFS with the system.  It is harmless to register the same
Packit 87b942
** VFS multiple times.  The new VFS becomes the default if makeDflt is
Packit 87b942
** true.
Packit 87b942
*/
Packit 87b942
int sqlite3_vfs_register(sqlite3_vfs *pVfs, int makeDflt){
Packit 87b942
  MUTEX_LOGIC(sqlite3_mutex *mutex;)
Packit 87b942
#ifndef SQLITE_OMIT_AUTOINIT
Packit 87b942
  int rc = sqlite3_initialize();
Packit 87b942
  if( rc ) return rc;
Packit 87b942
#endif
Packit 87b942
#ifdef SQLITE_ENABLE_API_ARMOR
Packit 87b942
  if( pVfs==0 ) return SQLITE_MISUSE_BKPT;
Packit 87b942
#endif
Packit 87b942
Packit 87b942
  MUTEX_LOGIC( mutex = sqlite3MutexAlloc(SQLITE_MUTEX_STATIC_MASTER); )
Packit 87b942
  sqlite3_mutex_enter(mutex);
Packit 87b942
  vfsUnlink(pVfs);
Packit 87b942
  if( makeDflt || vfsList==0 ){
Packit 87b942
    pVfs->pNext = vfsList;
Packit 87b942
    vfsList = pVfs;
Packit 87b942
  }else{
Packit 87b942
    pVfs->pNext = vfsList->pNext;
Packit 87b942
    vfsList->pNext = pVfs;
Packit 87b942
  }
Packit 87b942
  assert(vfsList);
Packit 87b942
  sqlite3_mutex_leave(mutex);
Packit 87b942
  return SQLITE_OK;
Packit 87b942
}
Packit 87b942
Packit 87b942
/*
Packit 87b942
** Unregister a VFS so that it is no longer accessible.
Packit 87b942
*/
Packit 87b942
int sqlite3_vfs_unregister(sqlite3_vfs *pVfs){
Packit 87b942
#if SQLITE_THREADSAFE
Packit 87b942
  sqlite3_mutex *mutex = sqlite3MutexAlloc(SQLITE_MUTEX_STATIC_MASTER);
Packit 87b942
#endif
Packit 87b942
  sqlite3_mutex_enter(mutex);
Packit 87b942
  vfsUnlink(pVfs);
Packit 87b942
  sqlite3_mutex_leave(mutex);
Packit 87b942
  return SQLITE_OK;
Packit 87b942
}