1e3c41372Sdrh /* 2e3c41372Sdrh ** 2001 September 16 3e3c41372Sdrh ** 4e3c41372Sdrh ** The author disclaims copyright to this source code. In place of 5e3c41372Sdrh ** a legal notice, here is a blessing: 6e3c41372Sdrh ** 7e3c41372Sdrh ** May you do good and not evil. 8e3c41372Sdrh ** May you find forgiveness for yourself and forgive others. 9e3c41372Sdrh ** May you share freely, never taking more than you give. 10e3c41372Sdrh ** 11e3c41372Sdrh ****************************************************************************** 12e3c41372Sdrh ** 13e3c41372Sdrh ** This header file (together with is companion C source-code file 14e3c41372Sdrh ** "os.c") attempt to abstract the underlying operating system so that 15e3c41372Sdrh ** the SQLite library will work on both POSIX and windows systems. 1629278e3dSdrh ** 1729278e3dSdrh ** This header file is #include-ed by sqliteInt.h and thus ends up 1829278e3dSdrh ** being included by every source file. 19e3c41372Sdrh */ 20e3c41372Sdrh #ifndef _SQLITE_OS_H_ 21e3c41372Sdrh #define _SQLITE_OS_H_ 22e3c41372Sdrh 23829e8029Sdrh /* 24f74b9e09Smistachkin ** Attempt to automatically detect the operating system and setup the 25f74b9e09Smistachkin ** necessary pre-processor macros for it. 26829e8029Sdrh */ 27f74b9e09Smistachkin #include "os_setup.h" 281ab4300eSdrh 29b851b2c9Sdrh /* If the SET_FULLSYNC macro is not defined above, then make it 30b851b2c9Sdrh ** a no-op 31b851b2c9Sdrh */ 32b851b2c9Sdrh #ifndef SET_FULLSYNC 33b851b2c9Sdrh # define SET_FULLSYNC(x,y) 34b851b2c9Sdrh #endif 35b851b2c9Sdrh 36cf145047Sdrh /* Maximum pathname length. Note: FILENAME_MAX defined by stdio.h 37cf145047Sdrh */ 38cf145047Sdrh #ifndef SQLITE_MAX_PATHLEN 39cf145047Sdrh # define SQLITE_MAX_PATHLEN FILENAME_MAX 40cf145047Sdrh #endif 41cf145047Sdrh 42*e8346d0aSdrh /* Maximum number of symlinks that will be resolved while trying to 43*e8346d0aSdrh ** expand a filename in xFullPathname() in the VFS. 44*e8346d0aSdrh */ 45*e8346d0aSdrh #ifndef SQLITE_MAX_SYMLINK 46*e8346d0aSdrh # define SQLITE_MAX_SYMLINK 200 47*e8346d0aSdrh #endif 48*e8346d0aSdrh 496622cce3Sdanielk1977 /* 503ceeb756Sdrh ** The default size of a disk sector 513ceeb756Sdrh */ 523ceeb756Sdrh #ifndef SQLITE_DEFAULT_SECTOR_SIZE 538942d412Sdrh # define SQLITE_DEFAULT_SECTOR_SIZE 4096 543ceeb756Sdrh #endif 553ceeb756Sdrh 563ceeb756Sdrh /* 57bbd42a6dSdrh ** Temporary files are named starting with this prefix followed by 16 random 58bbd42a6dSdrh ** alphanumeric characters, and no file extension. They are stored in the 59bbd42a6dSdrh ** OS's standard temporary file directory, and are deleted prior to exit. 60bbd42a6dSdrh ** If sqlite is being embedded in another program, you may wish to change the 61bbd42a6dSdrh ** prefix to reflect your program's name, so that if your program exits 62bbd42a6dSdrh ** prematurely, old temporary files can be easily identified. This can be done 63153c62c4Sdrh ** using -DSQLITE_TEMP_FILE_PREFIX=myprefix_ on the compiler command line. 64fd288f35Sdrh ** 65fd288f35Sdrh ** 2006-10-31: The default prefix used to be "sqlite_". But then 66fd288f35Sdrh ** Mcafee started using SQLite in their anti-virus product and it 67fd288f35Sdrh ** started putting files with the "sqlite" name in the c:/temp folder. 68fd288f35Sdrh ** This annoyed many windows users. Those users would then do a 69fd288f35Sdrh ** Google search for "sqlite", find the telephone numbers of the 70fd288f35Sdrh ** developers and call to wake them up at night and complain. 71fd288f35Sdrh ** For this reason, the default name prefix is changed to be "sqlite" 72fd288f35Sdrh ** spelled backwards. So the temp files are still identified, but 73fd288f35Sdrh ** anybody smart enough to figure out the code is also likely smart 74fd288f35Sdrh ** enough to know that calling the developer will not help get rid 75fd288f35Sdrh ** of the file. 766622cce3Sdanielk1977 */ 77153c62c4Sdrh #ifndef SQLITE_TEMP_FILE_PREFIX 78153c62c4Sdrh # define SQLITE_TEMP_FILE_PREFIX "etilqs_" 79bbd42a6dSdrh #endif 80bbd42a6dSdrh 8166560adaSdrh /* 82824d7c18Sdrh ** The following values may be passed as the second argument to 83824d7c18Sdrh ** sqlite3OsLock(). The various locks exhibit the following semantics: 84824d7c18Sdrh ** 85824d7c18Sdrh ** SHARED: Any number of processes may hold a SHARED lock simultaneously. 86824d7c18Sdrh ** RESERVED: A single process may hold a RESERVED lock on a file at 87824d7c18Sdrh ** any time. Other processes may hold and obtain new SHARED locks. 88824d7c18Sdrh ** PENDING: A single process may hold a PENDING lock on a file at 89824d7c18Sdrh ** any one time. Existing SHARED locks may persist, but no new 90824d7c18Sdrh ** SHARED locks may be obtained by other processes. 91824d7c18Sdrh ** EXCLUSIVE: An EXCLUSIVE lock precludes all other locks. 92824d7c18Sdrh ** 93824d7c18Sdrh ** PENDING_LOCK may not be passed directly to sqlite3OsLock(). Instead, a 94824d7c18Sdrh ** process that requests an EXCLUSIVE lock may actually obtain a PENDING 95824d7c18Sdrh ** lock. This can be upgraded to an EXCLUSIVE lock by a subsequent call to 96824d7c18Sdrh ** sqlite3OsLock(). 97824d7c18Sdrh */ 98824d7c18Sdrh #define NO_LOCK 0 99824d7c18Sdrh #define SHARED_LOCK 1 100824d7c18Sdrh #define RESERVED_LOCK 2 101824d7c18Sdrh #define PENDING_LOCK 3 102824d7c18Sdrh #define EXCLUSIVE_LOCK 4 103824d7c18Sdrh 104824d7c18Sdrh /* 105824d7c18Sdrh ** File Locking Notes: (Mostly about windows but also some info for Unix) 106824d7c18Sdrh ** 107824d7c18Sdrh ** We cannot use LockFileEx() or UnlockFileEx() on Win95/98/ME because 108824d7c18Sdrh ** those functions are not available. So we use only LockFile() and 109824d7c18Sdrh ** UnlockFile(). 110824d7c18Sdrh ** 111824d7c18Sdrh ** LockFile() prevents not just writing but also reading by other processes. 112824d7c18Sdrh ** A SHARED_LOCK is obtained by locking a single randomly-chosen 113824d7c18Sdrh ** byte out of a specific range of bytes. The lock byte is obtained at 114824d7c18Sdrh ** random so two separate readers can probably access the file at the 115824d7c18Sdrh ** same time, unless they are unlucky and choose the same lock byte. 116824d7c18Sdrh ** An EXCLUSIVE_LOCK is obtained by locking all bytes in the range. 117824d7c18Sdrh ** There can only be one writer. A RESERVED_LOCK is obtained by locking 118824d7c18Sdrh ** a single byte of the file that is designated as the reserved lock byte. 119824d7c18Sdrh ** A PENDING_LOCK is obtained by locking a designated byte different from 120824d7c18Sdrh ** the RESERVED_LOCK byte. 121824d7c18Sdrh ** 122824d7c18Sdrh ** On WinNT/2K/XP systems, LockFileEx() and UnlockFileEx() are available, 123824d7c18Sdrh ** which means we can use reader/writer locks. When reader/writer locks 124824d7c18Sdrh ** are used, the lock is placed on the same range of bytes that is used 125824d7c18Sdrh ** for probabilistic locking in Win95/98/ME. Hence, the locking scheme 126824d7c18Sdrh ** will support two or more Win95 readers or two or more WinNT readers. 127824d7c18Sdrh ** But a single Win95 reader will lock out all WinNT readers and a single 128824d7c18Sdrh ** WinNT reader will lock out all other Win95 readers. 129824d7c18Sdrh ** 130824d7c18Sdrh ** The following #defines specify the range of bytes used for locking. 131824d7c18Sdrh ** SHARED_SIZE is the number of bytes available in the pool from which 132824d7c18Sdrh ** a random byte is selected for a shared lock. The pool of bytes for 133824d7c18Sdrh ** shared locks begins at SHARED_FIRST. 134824d7c18Sdrh ** 135c7a3bb94Sdrh ** The same locking strategy and 13660ec914cSpeter.d.reid ** byte ranges are used for Unix. This leaves open the possibility of having 137824d7c18Sdrh ** clients on win95, winNT, and unix all talking to the same shared file 138824d7c18Sdrh ** and all locking correctly. To do so would require that samba (or whatever 139824d7c18Sdrh ** tool is being used for file sharing) implements locks correctly between 140824d7c18Sdrh ** windows and unix. I'm guessing that isn't likely to happen, but by 141824d7c18Sdrh ** using the same locking range we are at least open to the possibility. 142824d7c18Sdrh ** 143824d7c18Sdrh ** Locking in windows is manditory. For this reason, we cannot store 144824d7c18Sdrh ** actual data in the bytes used for locking. The pager never allocates 145824d7c18Sdrh ** the pages involved in locking therefore. SHARED_SIZE is selected so 146824d7c18Sdrh ** that all locks will fit on a single page even at the minimum page size. 147824d7c18Sdrh ** PENDING_BYTE defines the beginning of the locks. By default PENDING_BYTE 148824d7c18Sdrh ** is set high so that we don't have to allocate an unused page except 149824d7c18Sdrh ** for very large databases. But one should test the page skipping logic 150824d7c18Sdrh ** by setting PENDING_BYTE low and running the entire regression suite. 151824d7c18Sdrh ** 152824d7c18Sdrh ** Changing the value of PENDING_BYTE results in a subtly incompatible 153824d7c18Sdrh ** file format. Depending on how it is changed, you might not notice 154824d7c18Sdrh ** the incompatibility right away, even running a full regression test. 155824d7c18Sdrh ** The default location of PENDING_BYTE is the first byte past the 156824d7c18Sdrh ** 1GB boundary. 157824d7c18Sdrh ** 158824d7c18Sdrh */ 159f83dc1efSdrh #ifdef SQLITE_OMIT_WSD 160f83dc1efSdrh # define PENDING_BYTE (0x40000000) 161f83dc1efSdrh #else 162c7a3bb94Sdrh # define PENDING_BYTE sqlite3PendingByte 163f83dc1efSdrh #endif 164824d7c18Sdrh #define RESERVED_BYTE (PENDING_BYTE+1) 165824d7c18Sdrh #define SHARED_FIRST (PENDING_BYTE+2) 166824d7c18Sdrh #define SHARED_SIZE 510 167824d7c18Sdrh 168824d7c18Sdrh /* 1693d6e060bSdan ** Wrapper around OS specific sqlite3_os_init() function. 1703d6e060bSdan */ 1713d6e060bSdan int sqlite3OsInit(void); 1723d6e060bSdan 1733d6e060bSdan /* 174b4b47411Sdanielk1977 ** Functions for accessing sqlite3_file methods 175824d7c18Sdrh */ 1768f2ce914Sdrh void sqlite3OsClose(sqlite3_file*); 17762079060Sdanielk1977 int sqlite3OsRead(sqlite3_file*, void*, int amt, i64 offset); 17862079060Sdanielk1977 int sqlite3OsWrite(sqlite3_file*, const void*, int amt, i64 offset); 17962079060Sdanielk1977 int sqlite3OsTruncate(sqlite3_file*, i64 size); 18062079060Sdanielk1977 int sqlite3OsSync(sqlite3_file*, int); 18162079060Sdanielk1977 int sqlite3OsFileSize(sqlite3_file*, i64 *pSize); 18262079060Sdanielk1977 int sqlite3OsLock(sqlite3_file*, int); 18362079060Sdanielk1977 int sqlite3OsUnlock(sqlite3_file*, int); 184861f7456Sdanielk1977 int sqlite3OsCheckReservedLock(sqlite3_file *id, int *pResOut); 185cc6bb3eaSdrh int sqlite3OsFileControl(sqlite3_file*,int,void*); 186c02372ceSdrh void sqlite3OsFileControlHint(sqlite3_file*,int,void*); 1878f941bc7Sdrh #define SQLITE_FCNTL_DB_UNCHANGED 0xca093fa0 18862079060Sdanielk1977 int sqlite3OsSectorSize(sqlite3_file *id); 18962079060Sdanielk1977 int sqlite3OsDeviceCharacteristics(sqlite3_file *id); 1902ed5737aSdrh #ifndef SQLITE_OMIT_WAL 191da9fe0c3Sdan int sqlite3OsShmMap(sqlite3_file *,int,int,int,void volatile **); 19273b64e4dSdrh int sqlite3OsShmLock(sqlite3_file *id, int, int, int); 193286a2884Sdrh void sqlite3OsShmBarrier(sqlite3_file *id); 194e11fedc5Sdrh int sqlite3OsShmUnmap(sqlite3_file *id, int); 1952ed5737aSdrh #endif /* SQLITE_OMIT_WAL */ 196f23da966Sdan int sqlite3OsFetch(sqlite3_file *id, i64, int, void **); 197df737fe6Sdan int sqlite3OsUnfetch(sqlite3_file *, i64, void *); 19862079060Sdanielk1977 1996f2f19a1Sdan 200b4b47411Sdanielk1977 /* 201b4b47411Sdanielk1977 ** Functions for accessing sqlite3_vfs methods 202b4b47411Sdanielk1977 */ 203b4b47411Sdanielk1977 int sqlite3OsOpen(sqlite3_vfs *, const char *, sqlite3_file*, int, int *); 204fee2d25aSdanielk1977 int sqlite3OsDelete(sqlite3_vfs *, const char *, int); 205861f7456Sdanielk1977 int sqlite3OsAccess(sqlite3_vfs *, const char *, int, int *pResOut); 206adfb9b05Sdanielk1977 int sqlite3OsFullPathname(sqlite3_vfs *, const char *, int, char *); 20775998ab3Sshane #ifndef SQLITE_OMIT_LOAD_EXTENSION 208b4b47411Sdanielk1977 void *sqlite3OsDlOpen(sqlite3_vfs *, const char *); 209b4b47411Sdanielk1977 void sqlite3OsDlError(sqlite3_vfs *, int, char *); 2101875f7a3Sdrh void (*sqlite3OsDlSym(sqlite3_vfs *, void *, const char *))(void); 211b4b47411Sdanielk1977 void sqlite3OsDlClose(sqlite3_vfs *, void *); 21275998ab3Sshane #endif /* SQLITE_OMIT_LOAD_EXTENSION */ 213b4b47411Sdanielk1977 int sqlite3OsRandomness(sqlite3_vfs *, int, char *); 214b4b47411Sdanielk1977 int sqlite3OsSleep(sqlite3_vfs *, int); 2151b9f2141Sdrh int sqlite3OsGetLastError(sqlite3_vfs*); 216b7e8ea20Sdrh int sqlite3OsCurrentTimeInt64(sqlite3_vfs *, sqlite3_int64*); 217b4b47411Sdanielk1977 218b4b47411Sdanielk1977 /* 219b4b47411Sdanielk1977 ** Convenience functions for opening and closing files using 220b4b47411Sdanielk1977 ** sqlite3_malloc() to obtain space for the file-handle structure. 221b4b47411Sdanielk1977 */ 222967a4a1cSdanielk1977 int sqlite3OsOpenMalloc(sqlite3_vfs *, const char *, sqlite3_file **, int,int*); 2238f2ce914Sdrh void sqlite3OsCloseFree(sqlite3_file *); 224b4b47411Sdanielk1977 225e3c41372Sdrh #endif /* _SQLITE_OS_H_ */ 226