drh | e3c4137 | 2001-09-17 20:25:58 +0000 | [diff] [blame] | 1 | /* |
| 2 | ** 2001 September 16 |
| 3 | ** |
| 4 | ** The author disclaims copyright to this source code. In place of |
| 5 | ** a legal notice, here is a blessing: |
| 6 | ** |
| 7 | ** May you do good and not evil. |
| 8 | ** May you find forgiveness for yourself and forgive others. |
| 9 | ** May you share freely, never taking more than you give. |
| 10 | ** |
| 11 | ****************************************************************************** |
| 12 | ** |
| 13 | ** This header file (together with is companion C source-code file |
| 14 | ** "os.c") attempt to abstract the underlying operating system so that |
| 15 | ** the SQLite library will work on both POSIX and windows systems. |
drh | 29278e3 | 2007-08-21 10:44:15 +0000 | [diff] [blame] | 16 | ** |
| 17 | ** This header file is #include-ed by sqliteInt.h and thus ends up |
| 18 | ** being included by every source file. |
drh | e3c4137 | 2001-09-17 20:25:58 +0000 | [diff] [blame] | 19 | */ |
| 20 | #ifndef _SQLITE_OS_H_ |
| 21 | #define _SQLITE_OS_H_ |
| 22 | |
drh | 829e802 | 2002-11-06 14:08:11 +0000 | [diff] [blame] | 23 | /* |
drh | 66560ad | 2006-01-06 14:32:19 +0000 | [diff] [blame] | 24 | ** Figure out if we are dealing with Unix, Windows, or some other |
drh | 29278e3 | 2007-08-21 10:44:15 +0000 | [diff] [blame] | 25 | ** operating system. After the following block of preprocess macros, |
drh | 83905c9 | 2012-06-21 13:00:37 +0000 | [diff] [blame] | 26 | ** all of SQLITE_OS_UNIX, SQLITE_OS_WIN, and SQLITE_OS_OTHER |
danielk1977 | 29bafea | 2008-06-26 10:41:19 +0000 | [diff] [blame] | 27 | ** will defined to either 1 or 0. One of the four will be 1. The other |
| 28 | ** three will be 0. |
drh | 829e802 | 2002-11-06 14:08:11 +0000 | [diff] [blame] | 29 | */ |
danielk1977 | 29bafea | 2008-06-26 10:41:19 +0000 | [diff] [blame] | 30 | #if defined(SQLITE_OS_OTHER) |
| 31 | # if SQLITE_OS_OTHER==1 |
| 32 | # undef SQLITE_OS_UNIX |
| 33 | # define SQLITE_OS_UNIX 0 |
| 34 | # undef SQLITE_OS_WIN |
| 35 | # define SQLITE_OS_WIN 0 |
drh | ee2ce18 | 2007-04-02 16:45:12 +0000 | [diff] [blame] | 36 | # else |
danielk1977 | 29bafea | 2008-06-26 10:41:19 +0000 | [diff] [blame] | 37 | # undef SQLITE_OS_OTHER |
drh | ee2ce18 | 2007-04-02 16:45:12 +0000 | [diff] [blame] | 38 | # endif |
| 39 | #endif |
danielk1977 | 29bafea | 2008-06-26 10:41:19 +0000 | [diff] [blame] | 40 | #if !defined(SQLITE_OS_UNIX) && !defined(SQLITE_OS_OTHER) |
| 41 | # define SQLITE_OS_OTHER 0 |
| 42 | # ifndef SQLITE_OS_WIN |
drh | 0d47743 | 2005-01-16 20:47:40 +0000 | [diff] [blame] | 43 | # if defined(_WIN32) || defined(WIN32) || defined(__CYGWIN__) || defined(__MINGW32__) || defined(__BORLANDC__) |
danielk1977 | 29bafea | 2008-06-26 10:41:19 +0000 | [diff] [blame] | 44 | # define SQLITE_OS_WIN 1 |
| 45 | # define SQLITE_OS_UNIX 0 |
drh | 0d47743 | 2005-01-16 20:47:40 +0000 | [diff] [blame] | 46 | # else |
danielk1977 | 29bafea | 2008-06-26 10:41:19 +0000 | [diff] [blame] | 47 | # define SQLITE_OS_WIN 0 |
| 48 | # define SQLITE_OS_UNIX 1 |
drh | 27a3220 | 2002-03-20 00:00:29 +0000 | [diff] [blame] | 49 | # endif |
| 50 | # else |
danielk1977 | 29bafea | 2008-06-26 10:41:19 +0000 | [diff] [blame] | 51 | # define SQLITE_OS_UNIX 0 |
drh | 27a3220 | 2002-03-20 00:00:29 +0000 | [diff] [blame] | 52 | # endif |
drh | 820f381 | 2003-01-08 13:02:52 +0000 | [diff] [blame] | 53 | #else |
danielk1977 | 29bafea | 2008-06-26 10:41:19 +0000 | [diff] [blame] | 54 | # ifndef SQLITE_OS_WIN |
| 55 | # define SQLITE_OS_WIN 0 |
drh | e5e3760 | 2003-08-16 13:10:51 +0000 | [diff] [blame] | 56 | # endif |
drh | 1ab4300 | 2002-01-14 09:28:19 +0000 | [diff] [blame] | 57 | #endif |
| 58 | |
drh | 11de933 | 2012-03-01 19:14:13 +0000 | [diff] [blame] | 59 | #if SQLITE_OS_WIN |
| 60 | # include <windows.h> |
| 61 | #endif |
| 62 | |
shane | 712d6f9 | 2008-05-29 03:54:26 +0000 | [diff] [blame] | 63 | /* |
mistachkin | 254582f | 2011-12-09 05:52:16 +0000 | [diff] [blame] | 64 | ** Determine if we are dealing with Windows NT. |
drh | d794e8b | 2012-01-30 16:02:43 +0000 | [diff] [blame] | 65 | ** |
| 66 | ** We ought to be able to determine if we are compiling for win98 or winNT |
| 67 | ** using the _WIN32_WINNT macro as follows: |
| 68 | ** |
| 69 | ** #if defined(_WIN32_WINNT) |
| 70 | ** # define SQLITE_OS_WINNT 1 |
| 71 | ** #else |
| 72 | ** # define SQLITE_OS_WINNT 0 |
| 73 | ** #endif |
| 74 | ** |
| 75 | ** However, vs2005 does not set _WIN32_WINNT by default, as it ought to, |
| 76 | ** so the above test does not work. We'll just assume that everything is |
| 77 | ** winNT unless the programmer explicitly says otherwise by setting |
| 78 | ** SQLITE_OS_WINNT to 0. |
mistachkin | 254582f | 2011-12-09 05:52:16 +0000 | [diff] [blame] | 79 | */ |
drh | d794e8b | 2012-01-30 16:02:43 +0000 | [diff] [blame] | 80 | #if SQLITE_OS_WIN && !defined(SQLITE_OS_WINNT) |
mistachkin | 254582f | 2011-12-09 05:52:16 +0000 | [diff] [blame] | 81 | # define SQLITE_OS_WINNT 1 |
mistachkin | 254582f | 2011-12-09 05:52:16 +0000 | [diff] [blame] | 82 | #endif |
| 83 | |
| 84 | /* |
| 85 | ** Determine if we are dealing with WindowsCE - which has a much |
| 86 | ** reduced API. |
| 87 | */ |
| 88 | #if defined(_WIN32_WCE) |
| 89 | # define SQLITE_OS_WINCE 1 |
| 90 | #else |
| 91 | # define SQLITE_OS_WINCE 0 |
| 92 | #endif |
| 93 | |
drh | cf3d7a4 | 2012-03-01 20:05:41 +0000 | [diff] [blame] | 94 | /* |
mistachkin | a36421a | 2012-08-03 10:00:56 +0000 | [diff] [blame] | 95 | ** Determine if we are dealing with WinRT, which provides only a subset of |
| 96 | ** the full Win32 API. |
drh | cf3d7a4 | 2012-03-01 20:05:41 +0000 | [diff] [blame] | 97 | */ |
| 98 | #if !defined(SQLITE_OS_WINRT) |
| 99 | # define SQLITE_OS_WINRT 0 |
| 100 | #endif |
| 101 | |
mistachkin | c548465 | 2012-03-05 22:52:33 +0000 | [diff] [blame] | 102 | /* |
| 103 | ** When compiled for WinCE or WinRT, there is no concept of the current |
| 104 | ** directory. |
| 105 | */ |
| 106 | #if !SQLITE_OS_WINCE && !SQLITE_OS_WINRT |
| 107 | # define SQLITE_CURDIR 1 |
| 108 | #endif |
| 109 | |
drh | b851b2c | 2005-03-10 14:11:12 +0000 | [diff] [blame] | 110 | /* If the SET_FULLSYNC macro is not defined above, then make it |
| 111 | ** a no-op |
| 112 | */ |
| 113 | #ifndef SET_FULLSYNC |
| 114 | # define SET_FULLSYNC(x,y) |
| 115 | #endif |
| 116 | |
danielk1977 | 6622cce | 2004-05-20 11:00:52 +0000 | [diff] [blame] | 117 | /* |
drh | 3ceeb75 | 2007-03-29 18:19:52 +0000 | [diff] [blame] | 118 | ** The default size of a disk sector |
| 119 | */ |
| 120 | #ifndef SQLITE_DEFAULT_SECTOR_SIZE |
drh | 8942d41 | 2012-01-02 18:20:14 +0000 | [diff] [blame] | 121 | # define SQLITE_DEFAULT_SECTOR_SIZE 4096 |
drh | 3ceeb75 | 2007-03-29 18:19:52 +0000 | [diff] [blame] | 122 | #endif |
| 123 | |
| 124 | /* |
drh | bbd42a6 | 2004-05-22 17:41:58 +0000 | [diff] [blame] | 125 | ** Temporary files are named starting with this prefix followed by 16 random |
| 126 | ** alphanumeric characters, and no file extension. They are stored in the |
| 127 | ** OS's standard temporary file directory, and are deleted prior to exit. |
| 128 | ** If sqlite is being embedded in another program, you may wish to change the |
| 129 | ** prefix to reflect your program's name, so that if your program exits |
| 130 | ** prematurely, old temporary files can be easily identified. This can be done |
drh | 153c62c | 2007-08-24 03:51:33 +0000 | [diff] [blame] | 131 | ** using -DSQLITE_TEMP_FILE_PREFIX=myprefix_ on the compiler command line. |
drh | fd288f3 | 2006-10-31 21:27:33 +0000 | [diff] [blame] | 132 | ** |
| 133 | ** 2006-10-31: The default prefix used to be "sqlite_". But then |
| 134 | ** Mcafee started using SQLite in their anti-virus product and it |
| 135 | ** started putting files with the "sqlite" name in the c:/temp folder. |
| 136 | ** This annoyed many windows users. Those users would then do a |
| 137 | ** Google search for "sqlite", find the telephone numbers of the |
| 138 | ** developers and call to wake them up at night and complain. |
| 139 | ** For this reason, the default name prefix is changed to be "sqlite" |
| 140 | ** spelled backwards. So the temp files are still identified, but |
| 141 | ** anybody smart enough to figure out the code is also likely smart |
| 142 | ** enough to know that calling the developer will not help get rid |
| 143 | ** of the file. |
danielk1977 | 6622cce | 2004-05-20 11:00:52 +0000 | [diff] [blame] | 144 | */ |
drh | 153c62c | 2007-08-24 03:51:33 +0000 | [diff] [blame] | 145 | #ifndef SQLITE_TEMP_FILE_PREFIX |
| 146 | # define SQLITE_TEMP_FILE_PREFIX "etilqs_" |
drh | bbd42a6 | 2004-05-22 17:41:58 +0000 | [diff] [blame] | 147 | #endif |
| 148 | |
drh | 66560ad | 2006-01-06 14:32:19 +0000 | [diff] [blame] | 149 | /* |
drh | 824d7c1 | 2006-01-06 12:03:19 +0000 | [diff] [blame] | 150 | ** The following values may be passed as the second argument to |
| 151 | ** sqlite3OsLock(). The various locks exhibit the following semantics: |
| 152 | ** |
| 153 | ** SHARED: Any number of processes may hold a SHARED lock simultaneously. |
| 154 | ** RESERVED: A single process may hold a RESERVED lock on a file at |
| 155 | ** any time. Other processes may hold and obtain new SHARED locks. |
| 156 | ** PENDING: A single process may hold a PENDING lock on a file at |
| 157 | ** any one time. Existing SHARED locks may persist, but no new |
| 158 | ** SHARED locks may be obtained by other processes. |
| 159 | ** EXCLUSIVE: An EXCLUSIVE lock precludes all other locks. |
| 160 | ** |
| 161 | ** PENDING_LOCK may not be passed directly to sqlite3OsLock(). Instead, a |
| 162 | ** process that requests an EXCLUSIVE lock may actually obtain a PENDING |
| 163 | ** lock. This can be upgraded to an EXCLUSIVE lock by a subsequent call to |
| 164 | ** sqlite3OsLock(). |
| 165 | */ |
| 166 | #define NO_LOCK 0 |
| 167 | #define SHARED_LOCK 1 |
| 168 | #define RESERVED_LOCK 2 |
| 169 | #define PENDING_LOCK 3 |
| 170 | #define EXCLUSIVE_LOCK 4 |
| 171 | |
| 172 | /* |
| 173 | ** File Locking Notes: (Mostly about windows but also some info for Unix) |
| 174 | ** |
| 175 | ** We cannot use LockFileEx() or UnlockFileEx() on Win95/98/ME because |
| 176 | ** those functions are not available. So we use only LockFile() and |
| 177 | ** UnlockFile(). |
| 178 | ** |
| 179 | ** LockFile() prevents not just writing but also reading by other processes. |
| 180 | ** A SHARED_LOCK is obtained by locking a single randomly-chosen |
| 181 | ** byte out of a specific range of bytes. The lock byte is obtained at |
| 182 | ** random so two separate readers can probably access the file at the |
| 183 | ** same time, unless they are unlucky and choose the same lock byte. |
| 184 | ** An EXCLUSIVE_LOCK is obtained by locking all bytes in the range. |
| 185 | ** There can only be one writer. A RESERVED_LOCK is obtained by locking |
| 186 | ** a single byte of the file that is designated as the reserved lock byte. |
| 187 | ** A PENDING_LOCK is obtained by locking a designated byte different from |
| 188 | ** the RESERVED_LOCK byte. |
| 189 | ** |
| 190 | ** On WinNT/2K/XP systems, LockFileEx() and UnlockFileEx() are available, |
| 191 | ** which means we can use reader/writer locks. When reader/writer locks |
| 192 | ** are used, the lock is placed on the same range of bytes that is used |
| 193 | ** for probabilistic locking in Win95/98/ME. Hence, the locking scheme |
| 194 | ** will support two or more Win95 readers or two or more WinNT readers. |
| 195 | ** But a single Win95 reader will lock out all WinNT readers and a single |
| 196 | ** WinNT reader will lock out all other Win95 readers. |
| 197 | ** |
| 198 | ** The following #defines specify the range of bytes used for locking. |
| 199 | ** SHARED_SIZE is the number of bytes available in the pool from which |
| 200 | ** a random byte is selected for a shared lock. The pool of bytes for |
| 201 | ** shared locks begins at SHARED_FIRST. |
| 202 | ** |
drh | c7a3bb9 | 2009-02-05 16:31:45 +0000 | [diff] [blame] | 203 | ** The same locking strategy and |
drh | 824d7c1 | 2006-01-06 12:03:19 +0000 | [diff] [blame] | 204 | ** byte ranges are used for Unix. This leaves open the possiblity of having |
| 205 | ** clients on win95, winNT, and unix all talking to the same shared file |
| 206 | ** and all locking correctly. To do so would require that samba (or whatever |
| 207 | ** tool is being used for file sharing) implements locks correctly between |
| 208 | ** windows and unix. I'm guessing that isn't likely to happen, but by |
| 209 | ** using the same locking range we are at least open to the possibility. |
| 210 | ** |
| 211 | ** Locking in windows is manditory. For this reason, we cannot store |
| 212 | ** actual data in the bytes used for locking. The pager never allocates |
| 213 | ** the pages involved in locking therefore. SHARED_SIZE is selected so |
| 214 | ** that all locks will fit on a single page even at the minimum page size. |
| 215 | ** PENDING_BYTE defines the beginning of the locks. By default PENDING_BYTE |
| 216 | ** is set high so that we don't have to allocate an unused page except |
| 217 | ** for very large databases. But one should test the page skipping logic |
| 218 | ** by setting PENDING_BYTE low and running the entire regression suite. |
| 219 | ** |
| 220 | ** Changing the value of PENDING_BYTE results in a subtly incompatible |
| 221 | ** file format. Depending on how it is changed, you might not notice |
| 222 | ** the incompatibility right away, even running a full regression test. |
| 223 | ** The default location of PENDING_BYTE is the first byte past the |
| 224 | ** 1GB boundary. |
| 225 | ** |
| 226 | */ |
drh | f83dc1e | 2010-06-03 12:09:52 +0000 | [diff] [blame] | 227 | #ifdef SQLITE_OMIT_WSD |
| 228 | # define PENDING_BYTE (0x40000000) |
| 229 | #else |
| 230 | # define PENDING_BYTE sqlite3PendingByte |
| 231 | #endif |
drh | 824d7c1 | 2006-01-06 12:03:19 +0000 | [diff] [blame] | 232 | #define RESERVED_BYTE (PENDING_BYTE+1) |
| 233 | #define SHARED_FIRST (PENDING_BYTE+2) |
| 234 | #define SHARED_SIZE 510 |
| 235 | |
dan | 3d6e060 | 2009-08-17 15:52:25 +0000 | [diff] [blame] | 236 | /* |
| 237 | ** Wrapper around OS specific sqlite3_os_init() function. |
| 238 | */ |
| 239 | int sqlite3OsInit(void); |
| 240 | |
danielk1977 | b4b4741 | 2007-08-17 15:53:36 +0000 | [diff] [blame] | 241 | /* |
| 242 | ** Functions for accessing sqlite3_file methods |
drh | 824d7c1 | 2006-01-06 12:03:19 +0000 | [diff] [blame] | 243 | */ |
danielk1977 | b4b4741 | 2007-08-17 15:53:36 +0000 | [diff] [blame] | 244 | int sqlite3OsClose(sqlite3_file*); |
danielk1977 | 6207906 | 2007-08-15 17:08:46 +0000 | [diff] [blame] | 245 | int sqlite3OsRead(sqlite3_file*, void*, int amt, i64 offset); |
| 246 | int sqlite3OsWrite(sqlite3_file*, const void*, int amt, i64 offset); |
| 247 | int sqlite3OsTruncate(sqlite3_file*, i64 size); |
| 248 | int sqlite3OsSync(sqlite3_file*, int); |
| 249 | int sqlite3OsFileSize(sqlite3_file*, i64 *pSize); |
| 250 | int sqlite3OsLock(sqlite3_file*, int); |
| 251 | int sqlite3OsUnlock(sqlite3_file*, int); |
danielk1977 | 861f745 | 2008-06-05 11:39:11 +0000 | [diff] [blame] | 252 | int sqlite3OsCheckReservedLock(sqlite3_file *id, int *pResOut); |
drh | cc6bb3e | 2007-08-31 16:11:35 +0000 | [diff] [blame] | 253 | int sqlite3OsFileControl(sqlite3_file*,int,void*); |
drh | c02372c | 2012-01-10 17:59:59 +0000 | [diff] [blame] | 254 | void sqlite3OsFileControlHint(sqlite3_file*,int,void*); |
drh | 8f941bc | 2009-01-14 23:03:40 +0000 | [diff] [blame] | 255 | #define SQLITE_FCNTL_DB_UNCHANGED 0xca093fa0 |
danielk1977 | 6207906 | 2007-08-15 17:08:46 +0000 | [diff] [blame] | 256 | int sqlite3OsSectorSize(sqlite3_file *id); |
| 257 | int sqlite3OsDeviceCharacteristics(sqlite3_file *id); |
dan | da9fe0c | 2010-07-13 18:44:03 +0000 | [diff] [blame] | 258 | int sqlite3OsShmMap(sqlite3_file *,int,int,int,void volatile **); |
drh | 73b64e4 | 2010-05-30 19:55:15 +0000 | [diff] [blame] | 259 | int sqlite3OsShmLock(sqlite3_file *id, int, int, int); |
drh | 286a288 | 2010-05-20 23:51:06 +0000 | [diff] [blame] | 260 | void sqlite3OsShmBarrier(sqlite3_file *id); |
drh | e11fedc | 2010-07-14 00:14:30 +0000 | [diff] [blame] | 261 | int sqlite3OsShmUnmap(sqlite3_file *id, int); |
dan | f23da96 | 2013-03-23 21:00:41 +0000 | [diff] [blame^] | 262 | int sqlite3OsFetch(sqlite3_file *id, i64, int, void **); |
| 263 | int sqlite3OsUnfetch(sqlite3_file *, void *); |
danielk1977 | 6207906 | 2007-08-15 17:08:46 +0000 | [diff] [blame] | 264 | |
dan | 6f2f19a | 2012-01-10 16:56:39 +0000 | [diff] [blame] | 265 | |
danielk1977 | b4b4741 | 2007-08-17 15:53:36 +0000 | [diff] [blame] | 266 | /* |
| 267 | ** Functions for accessing sqlite3_vfs methods |
| 268 | */ |
| 269 | int sqlite3OsOpen(sqlite3_vfs *, const char *, sqlite3_file*, int, int *); |
danielk1977 | fee2d25 | 2007-08-18 10:59:19 +0000 | [diff] [blame] | 270 | int sqlite3OsDelete(sqlite3_vfs *, const char *, int); |
danielk1977 | 861f745 | 2008-06-05 11:39:11 +0000 | [diff] [blame] | 271 | int sqlite3OsAccess(sqlite3_vfs *, const char *, int, int *pResOut); |
danielk1977 | adfb9b0 | 2007-09-17 07:02:56 +0000 | [diff] [blame] | 272 | int sqlite3OsFullPathname(sqlite3_vfs *, const char *, int, char *); |
shane | 75998ab | 2008-05-29 02:52:59 +0000 | [diff] [blame] | 273 | #ifndef SQLITE_OMIT_LOAD_EXTENSION |
danielk1977 | b4b4741 | 2007-08-17 15:53:36 +0000 | [diff] [blame] | 274 | void *sqlite3OsDlOpen(sqlite3_vfs *, const char *); |
| 275 | void sqlite3OsDlError(sqlite3_vfs *, int, char *); |
drh | 1875f7a | 2008-12-08 18:19:17 +0000 | [diff] [blame] | 276 | void (*sqlite3OsDlSym(sqlite3_vfs *, void *, const char *))(void); |
danielk1977 | b4b4741 | 2007-08-17 15:53:36 +0000 | [diff] [blame] | 277 | void sqlite3OsDlClose(sqlite3_vfs *, void *); |
shane | 75998ab | 2008-05-29 02:52:59 +0000 | [diff] [blame] | 278 | #endif /* SQLITE_OMIT_LOAD_EXTENSION */ |
danielk1977 | b4b4741 | 2007-08-17 15:53:36 +0000 | [diff] [blame] | 279 | int sqlite3OsRandomness(sqlite3_vfs *, int, char *); |
| 280 | int sqlite3OsSleep(sqlite3_vfs *, int); |
drh | b7e8ea2 | 2010-05-03 14:32:30 +0000 | [diff] [blame] | 281 | int sqlite3OsCurrentTimeInt64(sqlite3_vfs *, sqlite3_int64*); |
danielk1977 | b4b4741 | 2007-08-17 15:53:36 +0000 | [diff] [blame] | 282 | |
| 283 | /* |
| 284 | ** Convenience functions for opening and closing files using |
| 285 | ** sqlite3_malloc() to obtain space for the file-handle structure. |
| 286 | */ |
danielk1977 | 967a4a1 | 2007-08-20 14:23:44 +0000 | [diff] [blame] | 287 | int sqlite3OsOpenMalloc(sqlite3_vfs *, const char *, sqlite3_file **, int,int*); |
danielk1977 | b4b4741 | 2007-08-17 15:53:36 +0000 | [diff] [blame] | 288 | int sqlite3OsCloseFree(sqlite3_file *); |
| 289 | |
drh | e3c4137 | 2001-09-17 20:25:58 +0000 | [diff] [blame] | 290 | #endif /* _SQLITE_OS_H_ */ |