src/3rdparty/sqlite/os.h

Go to the documentation of this file.
00001 /*
00002 ** 2001 September 16
00003 **
00004 ** The author disclaims copyright to this source code.  In place of
00005 ** a legal notice, here is a blessing:
00006 **
00007 **    May you do good and not evil.
00008 **    May you find forgiveness for yourself and forgive others.
00009 **    May you share freely, never taking more than you give.
00010 **
00011 ******************************************************************************
00012 **
00013 ** This header file (together with is companion C source-code file
00014 ** "os.c") attempt to abstract the underlying operating system so that
00015 ** the SQLite library will work on both POSIX and windows systems.
00016 */
00017 #ifndef _SQLITE_OS_H_
00018 #define _SQLITE_OS_H_
00019 
00020 /*
00021 ** Figure out if we are dealing with Unix, Windows, or some other
00022 ** operating system.
00023 */
00024 #if !defined(OS_UNIX) && !defined(OS_OTHER)
00025 # define OS_OTHER 0
00026 # ifndef OS_WIN
00027 #   if defined(_WIN32) || defined(WIN32) || defined(__CYGWIN__) || defined(__MINGW32__) || defined(__BORLANDC__)
00028 #     define OS_WIN 1
00029 #     define OS_UNIX 0
00030 #     define OS_OS2 0
00031 #   elif defined(_EMX_) || defined(_OS2) || defined(OS2) || defined(OS_OS2)
00032 #     define OS_WIN 0
00033 #     define OS_UNIX 0
00034 #     define OS_OS2 1
00035 #   else
00036 #     define OS_WIN 0
00037 #     define OS_UNIX 1
00038 #     define OS_OS2 0
00039 #  endif
00040 # else
00041 #  define OS_UNIX 0
00042 #  define OS_OS2 0
00043 # endif
00044 #else
00045 # ifndef OS_WIN
00046 #  define OS_WIN 0
00047 # endif
00048 #endif
00049 
00050 
00051 /*
00052 ** Define the maximum size of a temporary filename
00053 */
00054 #if OS_WIN
00055 # include <windows.h>
00056 # define SQLITE_TEMPNAME_SIZE (MAX_PATH+50)
00057 #elif OS_OS2
00058 # define INCL_DOSDATETIME
00059 # define INCL_DOSFILEMGR
00060 # define INCL_DOSERRORS
00061 # define INCL_DOSMISC
00062 # define INCL_DOSPROCESS
00063 # include <os2.h>
00064 # define SQLITE_TEMPNAME_SIZE (CCHMAXPATHCOMP)
00065 #else
00066 # define SQLITE_TEMPNAME_SIZE 200
00067 #endif
00068 
00069 /* If the SET_FULLSYNC macro is not defined above, then make it
00070 ** a no-op
00071 */
00072 #ifndef SET_FULLSYNC
00073 # define SET_FULLSYNC(x,y)
00074 #endif
00075 
00076 /*
00077 ** Temporary files are named starting with this prefix followed by 16 random
00078 ** alphanumeric characters, and no file extension. They are stored in the
00079 ** OS's standard temporary file directory, and are deleted prior to exit.
00080 ** If sqlite is being embedded in another program, you may wish to change the
00081 ** prefix to reflect your program's name, so that if your program exits
00082 ** prematurely, old temporary files can be easily identified. This can be done
00083 ** using -DTEMP_FILE_PREFIX=myprefix_ on the compiler command line.
00084 */
00085 #ifndef TEMP_FILE_PREFIX
00086 # define TEMP_FILE_PREFIX "sqlite_"
00087 #endif
00088 
00089 /*
00090 ** Define the interfaces for Unix, Windows, and OS/2.
00091 */
00092 #if OS_UNIX
00093 #define sqlite3OsOpenReadWrite      sqlite3UnixOpenReadWrite
00094 #define sqlite3OsOpenExclusive      sqlite3UnixOpenExclusive
00095 #define sqlite3OsOpenReadOnly       sqlite3UnixOpenReadOnly
00096 #define sqlite3OsDelete             sqlite3UnixDelete
00097 #define sqlite3OsFileExists         sqlite3UnixFileExists
00098 #define sqlite3OsFullPathname       sqlite3UnixFullPathname
00099 #define sqlite3OsIsDirWritable      sqlite3UnixIsDirWritable
00100 #define sqlite3OsSyncDirectory      sqlite3UnixSyncDirectory
00101 #define sqlite3OsTempFileName       sqlite3UnixTempFileName
00102 #define sqlite3OsRandomSeed         sqlite3UnixRandomSeed
00103 #define sqlite3OsSleep              sqlite3UnixSleep
00104 #define sqlite3OsCurrentTime        sqlite3UnixCurrentTime
00105 #define sqlite3OsEnterMutex         sqlite3UnixEnterMutex
00106 #define sqlite3OsLeaveMutex         sqlite3UnixLeaveMutex
00107 #define sqlite3OsInMutex            sqlite3UnixInMutex
00108 #define sqlite3OsThreadSpecificData sqlite3UnixThreadSpecificData
00109 #define sqlite3OsMalloc             sqlite3GenericMalloc
00110 #define sqlite3OsRealloc            sqlite3GenericRealloc
00111 #define sqlite3OsFree               sqlite3GenericFree
00112 #define sqlite3OsAllocationSize     sqlite3GenericAllocationSize
00113 #endif
00114 #if OS_WIN
00115 #define sqlite3OsOpenReadWrite      sqlite3WinOpenReadWrite
00116 #define sqlite3OsOpenExclusive      sqlite3WinOpenExclusive
00117 #define sqlite3OsOpenReadOnly       sqlite3WinOpenReadOnly
00118 #define sqlite3OsDelete             sqlite3WinDelete
00119 #define sqlite3OsFileExists         sqlite3WinFileExists
00120 #define sqlite3OsFullPathname       sqlite3WinFullPathname
00121 #define sqlite3OsIsDirWritable      sqlite3WinIsDirWritable
00122 #define sqlite3OsSyncDirectory      sqlite3WinSyncDirectory
00123 #define sqlite3OsTempFileName       sqlite3WinTempFileName
00124 #define sqlite3OsRandomSeed         sqlite3WinRandomSeed
00125 #define sqlite3OsSleep              sqlite3WinSleep
00126 #define sqlite3OsCurrentTime        sqlite3WinCurrentTime
00127 #define sqlite3OsEnterMutex         sqlite3WinEnterMutex
00128 #define sqlite3OsLeaveMutex         sqlite3WinLeaveMutex
00129 #define sqlite3OsInMutex            sqlite3WinInMutex
00130 #define sqlite3OsThreadSpecificData sqlite3WinThreadSpecificData
00131 #define sqlite3OsMalloc             sqlite3GenericMalloc
00132 #define sqlite3OsRealloc            sqlite3GenericRealloc
00133 #define sqlite3OsFree               sqlite3GenericFree
00134 #define sqlite3OsAllocationSize     sqlite3GenericAllocationSize
00135 #endif
00136 #if OS_OS2
00137 #define sqlite3OsOpenReadWrite      sqlite3Os2OpenReadWrite
00138 #define sqlite3OsOpenExclusive      sqlite3Os2OpenExclusive
00139 #define sqlite3OsOpenReadOnly       sqlite3Os2OpenReadOnly
00140 #define sqlite3OsDelete             sqlite3Os2Delete
00141 #define sqlite3OsFileExists         sqlite3Os2FileExists
00142 #define sqlite3OsFullPathname       sqlite3Os2FullPathname
00143 #define sqlite3OsIsDirWritable      sqlite3Os2IsDirWritable
00144 #define sqlite3OsSyncDirectory      sqlite3Os2SyncDirectory
00145 #define sqlite3OsTempFileName       sqlite3Os2TempFileName
00146 #define sqlite3OsRandomSeed         sqlite3Os2RandomSeed
00147 #define sqlite3OsSleep              sqlite3Os2Sleep
00148 #define sqlite3OsCurrentTime        sqlite3Os2CurrentTime
00149 #define sqlite3OsEnterMutex         sqlite3Os2EnterMutex
00150 #define sqlite3OsLeaveMutex         sqlite3Os2LeaveMutex
00151 #define sqlite3OsInMutex            sqlite3Os2InMutex
00152 #define sqlite3OsThreadSpecificData sqlite3Os2ThreadSpecificData
00153 #define sqlite3OsMalloc             sqlite3GenericMalloc
00154 #define sqlite3OsRealloc            sqlite3GenericRealloc
00155 #define sqlite3OsFree               sqlite3GenericFree
00156 #define sqlite3OsAllocationSize     sqlite3GenericAllocationSize
00157 #endif
00158 
00159 
00160 /*
00161 ** If using an alternative OS interface, then we must have an "os_other.h"
00162 ** header file available for that interface.  Presumably the "os_other.h"
00163 ** header file contains #defines similar to those above.
00164 */
00165 #if OS_OTHER
00166 # include "os_other.h"
00167 #endif
00168 
00169 
00170 
00171 /*
00172 ** Forward declarations
00173 */
00174 typedef struct OsFile OsFile;
00175 typedef struct IoMethod IoMethod;
00176 
00177 /*
00178 ** An instance of the following structure contains pointers to all
00179 ** methods on an OsFile object.
00180 */
00181 struct IoMethod {
00182   int (*xClose)(OsFile**);
00183   int (*xOpenDirectory)(OsFile*, const char*);
00184   int (*xRead)(OsFile*, void*, int amt);
00185   int (*xWrite)(OsFile*, const void*, int amt);
00186   int (*xSeek)(OsFile*, i64 offset);
00187   int (*xTruncate)(OsFile*, i64 size);
00188   int (*xSync)(OsFile*, int);
00189   void (*xSetFullSync)(OsFile *id, int setting);
00190   int (*xFileHandle)(OsFile *id);
00191   int (*xFileSize)(OsFile*, i64 *pSize);
00192   int (*xLock)(OsFile*, int);
00193   int (*xUnlock)(OsFile*, int);
00194   int (*xLockState)(OsFile *id);
00195   int (*xCheckReservedLock)(OsFile *id);
00196 };
00197 
00198 /*
00199 ** The OsFile object describes an open disk file in an OS-dependent way.
00200 ** The version of OsFile defined here is a generic version.  Each OS
00201 ** implementation defines its own subclass of this structure that contains
00202 ** additional information needed to handle file I/O.  But the pMethod
00203 ** entry (pointing to the virtual function table) always occurs first
00204 ** so that we can always find the appropriate methods.
00205 */
00206 struct OsFile {
00207   IoMethod const *pMethod;
00208 };
00209 
00210 /*
00211 ** The following values may be passed as the second argument to
00212 ** sqlite3OsLock(). The various locks exhibit the following semantics:
00213 **
00214 ** SHARED:    Any number of processes may hold a SHARED lock simultaneously.
00215 ** RESERVED:  A single process may hold a RESERVED lock on a file at
00216 **            any time. Other processes may hold and obtain new SHARED locks.
00217 ** PENDING:   A single process may hold a PENDING lock on a file at
00218 **            any one time. Existing SHARED locks may persist, but no new
00219 **            SHARED locks may be obtained by other processes.
00220 ** EXCLUSIVE: An EXCLUSIVE lock precludes all other locks.
00221 **
00222 ** PENDING_LOCK may not be passed directly to sqlite3OsLock(). Instead, a
00223 ** process that requests an EXCLUSIVE lock may actually obtain a PENDING
00224 ** lock. This can be upgraded to an EXCLUSIVE lock by a subsequent call to
00225 ** sqlite3OsLock().
00226 */
00227 #define NO_LOCK         0
00228 #define SHARED_LOCK     1
00229 #define RESERVED_LOCK   2
00230 #define PENDING_LOCK    3
00231 #define EXCLUSIVE_LOCK  4
00232 
00233 /*
00234 ** File Locking Notes:  (Mostly about windows but also some info for Unix)
00235 **
00236 ** We cannot use LockFileEx() or UnlockFileEx() on Win95/98/ME because
00237 ** those functions are not available.  So we use only LockFile() and
00238 ** UnlockFile().
00239 **
00240 ** LockFile() prevents not just writing but also reading by other processes.
00241 ** A SHARED_LOCK is obtained by locking a single randomly-chosen 
00242 ** byte out of a specific range of bytes. The lock byte is obtained at 
00243 ** random so two separate readers can probably access the file at the 
00244 ** same time, unless they are unlucky and choose the same lock byte.
00245 ** An EXCLUSIVE_LOCK is obtained by locking all bytes in the range.
00246 ** There can only be one writer.  A RESERVED_LOCK is obtained by locking
00247 ** a single byte of the file that is designated as the reserved lock byte.
00248 ** A PENDING_LOCK is obtained by locking a designated byte different from
00249 ** the RESERVED_LOCK byte.
00250 **
00251 ** On WinNT/2K/XP systems, LockFileEx() and UnlockFileEx() are available,
00252 ** which means we can use reader/writer locks.  When reader/writer locks
00253 ** are used, the lock is placed on the same range of bytes that is used
00254 ** for probabilistic locking in Win95/98/ME.  Hence, the locking scheme
00255 ** will support two or more Win95 readers or two or more WinNT readers.
00256 ** But a single Win95 reader will lock out all WinNT readers and a single
00257 ** WinNT reader will lock out all other Win95 readers.
00258 **
00259 ** The following #defines specify the range of bytes used for locking.
00260 ** SHARED_SIZE is the number of bytes available in the pool from which
00261 ** a random byte is selected for a shared lock.  The pool of bytes for
00262 ** shared locks begins at SHARED_FIRST. 
00263 **
00264 ** These #defines are available in sqlite_aux.h so that adaptors for
00265 ** connecting SQLite to other operating systems can use the same byte
00266 ** ranges for locking.  In particular, the same locking strategy and
00267 ** byte ranges are used for Unix.  This leaves open the possiblity of having
00268 ** clients on win95, winNT, and unix all talking to the same shared file
00269 ** and all locking correctly.  To do so would require that samba (or whatever
00270 ** tool is being used for file sharing) implements locks correctly between
00271 ** windows and unix.  I'm guessing that isn't likely to happen, but by
00272 ** using the same locking range we are at least open to the possibility.
00273 **
00274 ** Locking in windows is manditory.  For this reason, we cannot store
00275 ** actual data in the bytes used for locking.  The pager never allocates
00276 ** the pages involved in locking therefore.  SHARED_SIZE is selected so
00277 ** that all locks will fit on a single page even at the minimum page size.
00278 ** PENDING_BYTE defines the beginning of the locks.  By default PENDING_BYTE
00279 ** is set high so that we don't have to allocate an unused page except
00280 ** for very large databases.  But one should test the page skipping logic 
00281 ** by setting PENDING_BYTE low and running the entire regression suite.
00282 **
00283 ** Changing the value of PENDING_BYTE results in a subtly incompatible
00284 ** file format.  Depending on how it is changed, you might not notice
00285 ** the incompatibility right away, even running a full regression test.
00286 ** The default location of PENDING_BYTE is the first byte past the
00287 ** 1GB boundary.
00288 **
00289 */
00290 #ifndef SQLITE_TEST
00291 #define PENDING_BYTE      0x40000000  /* First byte past the 1GB boundary */
00292 #else
00293 extern unsigned int sqlite3_pending_byte;
00294 #define PENDING_BYTE sqlite3_pending_byte
00295 #endif
00296 
00297 #define RESERVED_BYTE     (PENDING_BYTE+1)
00298 #define SHARED_FIRST      (PENDING_BYTE+2)
00299 #define SHARED_SIZE       510
00300 
00301 /*
00302 ** Prototypes for operating system interface routines.
00303 */
00304 int sqlite3OsClose(OsFile**);
00305 int sqlite3OsOpenDirectory(OsFile*, const char*);
00306 int sqlite3OsRead(OsFile*, void*, int amt);
00307 int sqlite3OsWrite(OsFile*, const void*, int amt);
00308 int sqlite3OsSeek(OsFile*, i64 offset);
00309 int sqlite3OsTruncate(OsFile*, i64 size);
00310 int sqlite3OsSync(OsFile*, int);
00311 void sqlite3OsSetFullSync(OsFile *id, int setting);
00312 int sqlite3OsFileHandle(OsFile *id);
00313 int sqlite3OsFileSize(OsFile*, i64 *pSize);
00314 int sqlite3OsLock(OsFile*, int);
00315 int sqlite3OsUnlock(OsFile*, int);
00316 int sqlite3OsLockState(OsFile *id);
00317 int sqlite3OsCheckReservedLock(OsFile *id);
00318 int sqlite3OsOpenReadWrite(const char*, OsFile**, int*);
00319 int sqlite3OsOpenExclusive(const char*, OsFile**, int);
00320 int sqlite3OsOpenReadOnly(const char*, OsFile**);
00321 int sqlite3OsDelete(const char*);
00322 int sqlite3OsFileExists(const char*);
00323 char *sqlite3OsFullPathname(const char*);
00324 int sqlite3OsIsDirWritable(char*);
00325 int sqlite3OsSyncDirectory(const char*);
00326 int sqlite3OsTempFileName(char*);
00327 int sqlite3OsRandomSeed(char*);
00328 int sqlite3OsSleep(int ms);
00329 int sqlite3OsCurrentTime(double*);
00330 void sqlite3OsEnterMutex(void);
00331 void sqlite3OsLeaveMutex(void);
00332 int sqlite3OsInMutex(int);
00333 ThreadData *sqlite3OsThreadSpecificData(int);
00334 void *sqlite3OsMalloc(int);
00335 void *sqlite3OsRealloc(void *, int);
00336 void sqlite3OsFree(void *);
00337 int sqlite3OsAllocationSize(void *);
00338 
00339 /*
00340 ** If the SQLITE_ENABLE_REDEF_IO macro is defined, then the OS-layer
00341 ** interface routines are not called directly but are invoked using
00342 ** pointers to functions.  This allows the implementation of various
00343 ** OS-layer interface routines to be modified at run-time.  There are
00344 ** obscure but legitimate reasons for wanting to do this.  But for
00345 ** most users, a direct call to the underlying interface is preferable
00346 ** so the the redefinable I/O interface is turned off by default.
00347 */
00348 #ifdef SQLITE_ENABLE_REDEF_IO
00349 
00350 /*
00351 ** When redefinable I/O is enabled, a single global instance of the
00352 ** following structure holds pointers to the routines that SQLite 
00353 ** uses to talk with the underlying operating system.  Modify this
00354 ** structure (before using any SQLite API!) to accomodate perculiar
00355 ** operating system interfaces or behaviors.
00356 */
00357 struct sqlite3OsVtbl {
00358   int (*xOpenReadWrite)(const char*, OsFile**, int*);
00359   int (*xOpenExclusive)(const char*, OsFile**, int);
00360   int (*xOpenReadOnly)(const char*, OsFile**);
00361 
00362   int (*xDelete)(const char*);
00363   int (*xFileExists)(const char*);
00364   char *(*xFullPathname)(const char*);
00365   int (*xIsDirWritable)(char*);
00366   int (*xSyncDirectory)(const char*);
00367   int (*xTempFileName)(char*);
00368 
00369   int (*xRandomSeed)(char*);
00370   int (*xSleep)(int ms);
00371   int (*xCurrentTime)(double*);
00372 
00373   void (*xEnterMutex)(void);
00374   void (*xLeaveMutex)(void);
00375   int (*xInMutex)(int);
00376   ThreadData *(*xThreadSpecificData)(int);
00377 
00378   void *(*xMalloc)(int);
00379   void *(*xRealloc)(void *, int);
00380   void (*xFree)(void *);
00381   int (*xAllocationSize)(void *);
00382 };
00383 
00384 /* Macro used to comment out routines that do not exists when there is
00385 ** no disk I/O 
00386 */
00387 #ifdef SQLITE_OMIT_DISKIO
00388 # define IF_DISKIO(X)  0
00389 #else
00390 # define IF_DISKIO(X)  X
00391 #endif
00392 
00393 #ifdef _SQLITE_OS_C_
00394   /*
00395   ** The os.c file implements the global virtual function table.
00396   */
00397   struct sqlite3OsVtbl sqlite3Os = {
00398     IF_DISKIO( sqlite3OsOpenReadWrite ),
00399     IF_DISKIO( sqlite3OsOpenExclusive ),
00400     IF_DISKIO( sqlite3OsOpenReadOnly ),
00401     IF_DISKIO( sqlite3OsDelete ),
00402     IF_DISKIO( sqlite3OsFileExists ),
00403     IF_DISKIO( sqlite3OsFullPathname ),
00404     IF_DISKIO( sqlite3OsIsDirWritable ),
00405     IF_DISKIO( sqlite3OsSyncDirectory ),
00406     IF_DISKIO( sqlite3OsTempFileName ),
00407     sqlite3OsRandomSeed,
00408     sqlite3OsSleep,
00409     sqlite3OsCurrentTime,
00410     sqlite3OsEnterMutex,
00411     sqlite3OsLeaveMutex,
00412     sqlite3OsInMutex,
00413     sqlite3OsThreadSpecificData,
00414     sqlite3OsMalloc,
00415     sqlite3OsRealloc,
00416     sqlite3OsFree,
00417     sqlite3OsAllocationSize
00418   };
00419 #else
00420   /*
00421   ** Files other than os.c just reference the global virtual function table. 
00422   */
00423   extern struct sqlite3OsVtbl sqlite3Os;
00424 #endif /* _SQLITE_OS_C_ */
00425 
00426 
00427 /* This additional API routine is available with redefinable I/O */
00428 struct sqlite3OsVtbl *sqlite3_os_switch(void);
00429 
00430 
00431 /*
00432 ** Redefine the OS interface to go through the virtual function table
00433 ** rather than calling routines directly.
00434 */
00435 #undef sqlite3OsOpenReadWrite
00436 #undef sqlite3OsOpenExclusive
00437 #undef sqlite3OsOpenReadOnly
00438 #undef sqlite3OsDelete
00439 #undef sqlite3OsFileExists
00440 #undef sqlite3OsFullPathname
00441 #undef sqlite3OsIsDirWritable
00442 #undef sqlite3OsSyncDirectory
00443 #undef sqlite3OsTempFileName
00444 #undef sqlite3OsRandomSeed
00445 #undef sqlite3OsSleep
00446 #undef sqlite3OsCurrentTime
00447 #undef sqlite3OsEnterMutex
00448 #undef sqlite3OsLeaveMutex
00449 #undef sqlite3OsInMutex
00450 #undef sqlite3OsThreadSpecificData
00451 #undef sqlite3OsMalloc
00452 #undef sqlite3OsRealloc
00453 #undef sqlite3OsFree
00454 #undef sqlite3OsAllocationSize
00455 #define sqlite3OsOpenReadWrite      sqlite3Os.xOpenReadWrite
00456 #define sqlite3OsOpenExclusive      sqlite3Os.xOpenExclusive
00457 #define sqlite3OsOpenReadOnly       sqlite3Os.xOpenReadOnly
00458 #define sqlite3OsDelete             sqlite3Os.xDelete
00459 #define sqlite3OsFileExists         sqlite3Os.xFileExists
00460 #define sqlite3OsFullPathname       sqlite3Os.xFullPathname
00461 #define sqlite3OsIsDirWritable      sqlite3Os.xIsDirWritable
00462 #define sqlite3OsSyncDirectory      sqlite3Os.xSyncDirectory
00463 #define sqlite3OsTempFileName       sqlite3Os.xTempFileName
00464 #define sqlite3OsRandomSeed         sqlite3Os.xRandomSeed
00465 #define sqlite3OsSleep              sqlite3Os.xSleep
00466 #define sqlite3OsCurrentTime        sqlite3Os.xCurrentTime
00467 #define sqlite3OsEnterMutex         sqlite3Os.xEnterMutex
00468 #define sqlite3OsLeaveMutex         sqlite3Os.xLeaveMutex
00469 #define sqlite3OsInMutex            sqlite3Os.xInMutex
00470 #define sqlite3OsThreadSpecificData sqlite3Os.xThreadSpecificData
00471 #define sqlite3OsMalloc             sqlite3Os.xMalloc
00472 #define sqlite3OsRealloc            sqlite3Os.xRealloc
00473 #define sqlite3OsFree               sqlite3Os.xFree
00474 #define sqlite3OsAllocationSize     sqlite3Os.xAllocationSize
00475 
00476 #endif /* SQLITE_ENABLE_REDEF_IO */
00477 
00478 #endif /* _SQLITE_OS_H_ */

Generated on Thu Mar 15 11:53:03 2007 for Qt 4.2 User's Guide by  doxygen 1.5.1