ds@1952: /* ds@1952: ** 2001 September 16 ds@1952: ** ds@1952: ** The author disclaims copyright to this source code. In place of ds@1952: ** a legal notice, here is a blessing: ds@1952: ** ds@1952: ** May you do good and not evil. ds@1952: ** May you find forgiveness for yourself and forgive others. ds@1952: ** May you share freely, never taking more than you give. ds@1952: ** ds@1952: ****************************************************************************** ds@1952: ** ds@1952: ** This header file (together with is companion C source-code file ds@1952: ** "os.c") attempt to abstract the underlying operating system so that ds@1952: ** the SQLite library will work on both POSIX and windows systems. ds@1952: ** ds@1952: ** This header file is #include-ed by sqliteInt.h and thus ends up ds@1952: ** being included by every source file. ds@1952: */ ds@1952: #ifndef _SQLITE_OS_H_ ds@1952: #define _SQLITE_OS_H_ ds@1952: ds@1952: /* ds@1952: ** Figure out if we are dealing with Unix, Windows, or some other ds@1952: ** operating system. After the following block of preprocess macros, ds@1952: ** all of OS_UNIX, OS_WIN, OS_OS2, and OS_OTHER will defined to either ds@1952: ** 1 or 0. One of the four will be 1. The other three will be 0. ds@1952: */ ds@1952: #if defined(OS_OTHER) ds@1952: # if OS_OTHER==1 ds@1952: # undef OS_UNIX ds@1952: # define OS_UNIX 0 ds@1952: # undef OS_WIN ds@1952: # define OS_WIN 0 ds@1952: # undef OS_OS2 ds@1952: # define OS_OS2 0 ds@1952: # else ds@1952: # undef OS_OTHER ds@1952: # endif ds@1952: #endif ds@1952: #if !defined(OS_UNIX) && !defined(OS_OTHER) ds@1952: # define OS_OTHER 0 ds@1952: # ifndef OS_WIN ds@1952: # if defined(_WIN32) || defined(WIN32) || defined(__CYGWIN__) || defined(__MINGW32__) || defined(__BORLANDC__) ds@1952: # define OS_WIN 1 ds@1952: # define OS_UNIX 0 ds@1952: # define OS_OS2 0 ds@1952: # elif defined(__EMX__) || defined(_OS2) || defined(OS2) || defined(_OS2_) || defined(__OS2__) ds@1952: # define OS_WIN 0 ds@1952: # define OS_UNIX 0 ds@1952: # define OS_OS2 1 ds@1952: # else ds@1952: # define OS_WIN 0 ds@1952: # define OS_UNIX 1 ds@1952: # define OS_OS2 0 ds@1952: # endif ds@1952: # else ds@1952: # define OS_UNIX 0 ds@1952: # define OS_OS2 0 ds@1952: # endif ds@1952: #else ds@1952: # ifndef OS_WIN ds@1952: # define OS_WIN 0 ds@1952: # endif ds@1952: #endif ds@1952: ds@1952: ds@1952: ds@1952: /* ds@1952: ** Define the maximum size of a temporary filename ds@1952: */ ds@1952: #if OS_WIN ds@1952: # include ds@1952: # define SQLITE_TEMPNAME_SIZE (MAX_PATH+50) ds@1952: #elif OS_OS2 ds@1952: # if (__GNUC__ > 3 || __GNUC__ == 3 && __GNUC_MINOR__ >= 3) && defined(OS2_HIGH_MEMORY) ds@1952: # include /* has to be included before os2.h for linking to work */ ds@1952: # endif ds@1952: # define INCL_DOSDATETIME ds@1952: # define INCL_DOSFILEMGR ds@1952: # define INCL_DOSERRORS ds@1952: # define INCL_DOSMISC ds@1952: # define INCL_DOSPROCESS ds@1952: # define INCL_DOSMODULEMGR ds@1952: # include ds@1952: # define SQLITE_TEMPNAME_SIZE (CCHMAXPATHCOMP) ds@1952: #else ds@1952: # define SQLITE_TEMPNAME_SIZE 200 ds@1952: #endif ds@1952: ds@1952: /* If the SET_FULLSYNC macro is not defined above, then make it ds@1952: ** a no-op ds@1952: */ ds@1952: #ifndef SET_FULLSYNC ds@1952: # define SET_FULLSYNC(x,y) ds@1952: #endif ds@1952: ds@1952: /* ds@1952: ** The default size of a disk sector ds@1952: */ ds@1952: #ifndef SQLITE_DEFAULT_SECTOR_SIZE ds@1952: # define SQLITE_DEFAULT_SECTOR_SIZE 512 ds@1952: #endif ds@1952: ds@1952: /* ds@1952: ** Temporary files are named starting with this prefix followed by 16 random ds@1952: ** alphanumeric characters, and no file extension. They are stored in the ds@1952: ** OS's standard temporary file directory, and are deleted prior to exit. ds@1952: ** If sqlite is being embedded in another program, you may wish to change the ds@1952: ** prefix to reflect your program's name, so that if your program exits ds@1952: ** prematurely, old temporary files can be easily identified. This can be done ds@1952: ** using -DSQLITE_TEMP_FILE_PREFIX=myprefix_ on the compiler command line. ds@1952: ** ds@1952: ** 2006-10-31: The default prefix used to be "sqlite_". But then ds@1952: ** Mcafee started using SQLite in their anti-virus product and it ds@1952: ** started putting files with the "sqlite" name in the c:/temp folder. ds@1952: ** This annoyed many windows users. Those users would then do a ds@1952: ** Google search for "sqlite", find the telephone numbers of the ds@1952: ** developers and call to wake them up at night and complain. ds@1952: ** For this reason, the default name prefix is changed to be "sqlite" ds@1952: ** spelled backwards. So the temp files are still identified, but ds@1952: ** anybody smart enough to figure out the code is also likely smart ds@1952: ** enough to know that calling the developer will not help get rid ds@1952: ** of the file. ds@1952: */ ds@1952: #ifndef SQLITE_TEMP_FILE_PREFIX ds@1952: # define SQLITE_TEMP_FILE_PREFIX "etilqs_" ds@1952: #endif ds@1952: ds@1952: /* ds@1952: ** If using an alternative OS interface, then we must have an "os_other.h" ds@1952: ** header file available for that interface. Presumably the "os_other.h" ds@1952: ** header file contains #defines similar to those above. ds@1952: */ ds@1952: #if OS_OTHER ds@1952: # include "os_other.h" ds@1952: #endif ds@1952: ds@1952: ds@1952: /* ds@1952: ** The following values may be passed as the second argument to ds@1952: ** sqlite3OsLock(). The various locks exhibit the following semantics: ds@1952: ** ds@1952: ** SHARED: Any number of processes may hold a SHARED lock simultaneously. ds@1952: ** RESERVED: A single process may hold a RESERVED lock on a file at ds@1952: ** any time. Other processes may hold and obtain new SHARED locks. ds@1952: ** PENDING: A single process may hold a PENDING lock on a file at ds@1952: ** any one time. Existing SHARED locks may persist, but no new ds@1952: ** SHARED locks may be obtained by other processes. ds@1952: ** EXCLUSIVE: An EXCLUSIVE lock precludes all other locks. ds@1952: ** ds@1952: ** PENDING_LOCK may not be passed directly to sqlite3OsLock(). Instead, a ds@1952: ** process that requests an EXCLUSIVE lock may actually obtain a PENDING ds@1952: ** lock. This can be upgraded to an EXCLUSIVE lock by a subsequent call to ds@1952: ** sqlite3OsLock(). ds@1952: */ ds@1952: #define NO_LOCK 0 ds@1952: #define SHARED_LOCK 1 ds@1952: #define RESERVED_LOCK 2 ds@1952: #define PENDING_LOCK 3 ds@1952: #define EXCLUSIVE_LOCK 4 ds@1952: ds@1952: /* ds@1952: ** File Locking Notes: (Mostly about windows but also some info for Unix) ds@1952: ** ds@1952: ** We cannot use LockFileEx() or UnlockFileEx() on Win95/98/ME because ds@1952: ** those functions are not available. So we use only LockFile() and ds@1952: ** UnlockFile(). ds@1952: ** ds@1952: ** LockFile() prevents not just writing but also reading by other processes. ds@1952: ** A SHARED_LOCK is obtained by locking a single randomly-chosen ds@1952: ** byte out of a specific range of bytes. The lock byte is obtained at ds@1952: ** random so two separate readers can probably access the file at the ds@1952: ** same time, unless they are unlucky and choose the same lock byte. ds@1952: ** An EXCLUSIVE_LOCK is obtained by locking all bytes in the range. ds@1952: ** There can only be one writer. A RESERVED_LOCK is obtained by locking ds@1952: ** a single byte of the file that is designated as the reserved lock byte. ds@1952: ** A PENDING_LOCK is obtained by locking a designated byte different from ds@1952: ** the RESERVED_LOCK byte. ds@1952: ** ds@1952: ** On WinNT/2K/XP systems, LockFileEx() and UnlockFileEx() are available, ds@1952: ** which means we can use reader/writer locks. When reader/writer locks ds@1952: ** are used, the lock is placed on the same range of bytes that is used ds@1952: ** for probabilistic locking in Win95/98/ME. Hence, the locking scheme ds@1952: ** will support two or more Win95 readers or two or more WinNT readers. ds@1952: ** But a single Win95 reader will lock out all WinNT readers and a single ds@1952: ** WinNT reader will lock out all other Win95 readers. ds@1952: ** ds@1952: ** The following #defines specify the range of bytes used for locking. ds@1952: ** SHARED_SIZE is the number of bytes available in the pool from which ds@1952: ** a random byte is selected for a shared lock. The pool of bytes for ds@1952: ** shared locks begins at SHARED_FIRST. ds@1952: ** ds@1952: ** These #defines are available in sqlite_aux.h so that adaptors for ds@1952: ** connecting SQLite to other operating systems can use the same byte ds@1952: ** ranges for locking. In particular, the same locking strategy and ds@1952: ** byte ranges are used for Unix. This leaves open the possiblity of having ds@1952: ** clients on win95, winNT, and unix all talking to the same shared file ds@1952: ** and all locking correctly. To do so would require that samba (or whatever ds@1952: ** tool is being used for file sharing) implements locks correctly between ds@1952: ** windows and unix. I'm guessing that isn't likely to happen, but by ds@1952: ** using the same locking range we are at least open to the possibility. ds@1952: ** ds@1952: ** Locking in windows is manditory. For this reason, we cannot store ds@1952: ** actual data in the bytes used for locking. The pager never allocates ds@1952: ** the pages involved in locking therefore. SHARED_SIZE is selected so ds@1952: ** that all locks will fit on a single page even at the minimum page size. ds@1952: ** PENDING_BYTE defines the beginning of the locks. By default PENDING_BYTE ds@1952: ** is set high so that we don't have to allocate an unused page except ds@1952: ** for very large databases. But one should test the page skipping logic ds@1952: ** by setting PENDING_BYTE low and running the entire regression suite. ds@1952: ** ds@1952: ** Changing the value of PENDING_BYTE results in a subtly incompatible ds@1952: ** file format. Depending on how it is changed, you might not notice ds@1952: ** the incompatibility right away, even running a full regression test. ds@1952: ** The default location of PENDING_BYTE is the first byte past the ds@1952: ** 1GB boundary. ds@1952: ** ds@1952: */ ds@1952: #ifndef SQLITE_TEST ds@1952: #define PENDING_BYTE 0x40000000 /* First byte past the 1GB boundary */ ds@1952: #else ds@1952: extern unsigned int sqlite3_pending_byte; ds@1952: #define PENDING_BYTE sqlite3_pending_byte ds@1952: #endif ds@1952: ds@1952: #define RESERVED_BYTE (PENDING_BYTE+1) ds@1952: #define SHARED_FIRST (PENDING_BYTE+2) ds@1952: #define SHARED_SIZE 510 ds@1952: ds@1952: /* ds@1952: ** Functions for accessing sqlite3_file methods ds@1952: */ ds@1952: int sqlite3OsClose(sqlite3_file*); ds@1952: int sqlite3OsRead(sqlite3_file*, void*, int amt, i64 offset); ds@1952: int sqlite3OsWrite(sqlite3_file*, const void*, int amt, i64 offset); ds@1952: int sqlite3OsTruncate(sqlite3_file*, i64 size); ds@1952: int sqlite3OsSync(sqlite3_file*, int); ds@1952: int sqlite3OsFileSize(sqlite3_file*, i64 *pSize); ds@1952: int sqlite3OsLock(sqlite3_file*, int); ds@1952: int sqlite3OsUnlock(sqlite3_file*, int); ds@1952: int sqlite3OsCheckReservedLock(sqlite3_file *id); ds@1952: int sqlite3OsFileControl(sqlite3_file*,int,void*); ds@1952: int sqlite3OsSectorSize(sqlite3_file *id); ds@1952: int sqlite3OsDeviceCharacteristics(sqlite3_file *id); ds@1952: ds@1952: /* ds@1952: ** Functions for accessing sqlite3_vfs methods ds@1952: */ ds@1952: int sqlite3OsOpen(sqlite3_vfs *, const char *, sqlite3_file*, int, int *); ds@1952: int sqlite3OsDelete(sqlite3_vfs *, const char *, int); ds@1952: int sqlite3OsAccess(sqlite3_vfs *, const char *, int); ds@1952: int sqlite3OsGetTempname(sqlite3_vfs *, int, char *); ds@1952: int sqlite3OsFullPathname(sqlite3_vfs *, const char *, int, char *); ds@1952: void *sqlite3OsDlOpen(sqlite3_vfs *, const char *); ds@1952: void sqlite3OsDlError(sqlite3_vfs *, int, char *); ds@1952: void *sqlite3OsDlSym(sqlite3_vfs *, void *, const char *); ds@1952: void sqlite3OsDlClose(sqlite3_vfs *, void *); ds@1952: int sqlite3OsRandomness(sqlite3_vfs *, int, char *); ds@1952: int sqlite3OsSleep(sqlite3_vfs *, int); ds@1952: int sqlite3OsCurrentTime(sqlite3_vfs *, double*); ds@1952: ds@1952: /* ds@1952: ** Convenience functions for opening and closing files using ds@1952: ** sqlite3_malloc() to obtain space for the file-handle structure. ds@1952: */ ds@1952: int sqlite3OsOpenMalloc(sqlite3_vfs *, const char *, sqlite3_file **, int,int*); ds@1952: int sqlite3OsCloseFree(sqlite3_file *); ds@1952: ds@1952: /* ds@1952: ** Each OS-specific backend defines an instance of the following ds@1952: ** structure for returning a pointer to its sqlite3_vfs. If OS_OTHER ds@1952: ** is defined (meaning that the application-defined OS interface layer ds@1952: ** is used) then there is no default VFS. The application must ds@1952: ** register one or more VFS structures using sqlite3_vfs_register() ds@1952: ** before attempting to use SQLite. ds@1952: */ ds@1952: #if OS_UNIX || OS_WIN || OS_OS2 ds@1952: sqlite3_vfs *sqlite3OsDefaultVfs(void); ds@1952: #else ds@1952: # define sqlite3OsDefaultVfs(X) 0 ds@1952: #endif ds@1952: ds@1952: #endif /* _SQLITE_OS_H_ */