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. |
| 16 | */ |
| 17 | #ifndef _SQLITE_OS_H_ |
| 18 | #define _SQLITE_OS_H_ |
| 19 | |
drh | 829e802 | 2002-11-06 14:08:11 +0000 | [diff] [blame] | 20 | /* |
drh | 66560ad | 2006-01-06 14:32:19 +0000 | [diff] [blame] | 21 | ** Figure out if we are dealing with Unix, Windows, or some other |
| 22 | ** operating system. |
drh | 829e802 | 2002-11-06 14:08:11 +0000 | [diff] [blame] | 23 | */ |
drh | ee2ce18 | 2007-04-02 16:45:12 +0000 | [diff] [blame] | 24 | #if defined(OS_OTHER) |
| 25 | # if OS_OTHER==1 |
| 26 | # undef OS_UNIX |
| 27 | # define OS_UNIX 0 |
| 28 | # undef OS_WIN |
| 29 | # define OS_WIN 0 |
| 30 | # undef OS_OS2 |
| 31 | # define OS_OS2 0 |
| 32 | # else |
| 33 | # undef OS_OTHER |
| 34 | # endif |
| 35 | #endif |
drh | 66560ad | 2006-01-06 14:32:19 +0000 | [diff] [blame] | 36 | #if !defined(OS_UNIX) && !defined(OS_OTHER) |
drh | 0ccebe7 | 2005-06-07 22:22:50 +0000 | [diff] [blame] | 37 | # define OS_OTHER 0 |
drh | 27a3220 | 2002-03-20 00:00:29 +0000 | [diff] [blame] | 38 | # ifndef OS_WIN |
drh | 0d47743 | 2005-01-16 20:47:40 +0000 | [diff] [blame] | 39 | # if defined(_WIN32) || defined(WIN32) || defined(__CYGWIN__) || defined(__MINGW32__) || defined(__BORLANDC__) |
| 40 | # define OS_WIN 1 |
| 41 | # define OS_UNIX 0 |
drh | 60a1e4b | 2006-06-03 18:02:15 +0000 | [diff] [blame] | 42 | # define OS_OS2 0 |
pweilbacher | 3f61bc7 | 2007-06-30 15:24:37 +0000 | [diff] [blame^] | 43 | # elif defined(__EMX__) || defined(_OS2) || defined(OS2) || defined(_OS2_) || defined(__OS2__) |
drh | 60a1e4b | 2006-06-03 18:02:15 +0000 | [diff] [blame] | 44 | # define OS_WIN 0 |
| 45 | # define OS_UNIX 0 |
| 46 | # define OS_OS2 1 |
drh | 0d47743 | 2005-01-16 20:47:40 +0000 | [diff] [blame] | 47 | # else |
| 48 | # define OS_WIN 0 |
| 49 | # define OS_UNIX 1 |
drh | 60a1e4b | 2006-06-03 18:02:15 +0000 | [diff] [blame] | 50 | # define OS_OS2 0 |
drh | 27a3220 | 2002-03-20 00:00:29 +0000 | [diff] [blame] | 51 | # endif |
| 52 | # else |
| 53 | # define OS_UNIX 0 |
drh | 60a1e4b | 2006-06-03 18:02:15 +0000 | [diff] [blame] | 54 | # define OS_OS2 0 |
drh | 27a3220 | 2002-03-20 00:00:29 +0000 | [diff] [blame] | 55 | # endif |
drh | 820f381 | 2003-01-08 13:02:52 +0000 | [diff] [blame] | 56 | #else |
drh | e5e3760 | 2003-08-16 13:10:51 +0000 | [diff] [blame] | 57 | # ifndef OS_WIN |
| 58 | # define OS_WIN 0 |
| 59 | # endif |
drh | 1ab4300 | 2002-01-14 09:28:19 +0000 | [diff] [blame] | 60 | #endif |
| 61 | |
drh | 054889e | 2005-11-30 03:20:31 +0000 | [diff] [blame] | 62 | |
drh | e3c4137 | 2001-09-17 20:25:58 +0000 | [diff] [blame] | 63 | /* |
drh | 9cbe635 | 2005-11-29 03:13:21 +0000 | [diff] [blame] | 64 | ** Define the maximum size of a temporary filename |
drh | 0ccebe7 | 2005-06-07 22:22:50 +0000 | [diff] [blame] | 65 | */ |
drh | 9cbe635 | 2005-11-29 03:13:21 +0000 | [diff] [blame] | 66 | #if OS_WIN |
drh | a2eebaa | 2005-11-29 19:50:24 +0000 | [diff] [blame] | 67 | # include <windows.h> |
drh | 9cbe635 | 2005-11-29 03:13:21 +0000 | [diff] [blame] | 68 | # define SQLITE_TEMPNAME_SIZE (MAX_PATH+50) |
drh | 60a1e4b | 2006-06-03 18:02:15 +0000 | [diff] [blame] | 69 | #elif OS_OS2 |
pweilbacher | 53b4bd3 | 2007-06-22 20:17:37 +0000 | [diff] [blame] | 70 | # if (__GNUC__ > 3 || __GNUC__ == 3 && __GNUC_MINOR__ >= 3) && defined(OS2_HIGH_MEMORY) |
| 71 | # include <os2safe.h> /* has to be included before os2.h for linking to work */ |
| 72 | # endif |
drh | 60a1e4b | 2006-06-03 18:02:15 +0000 | [diff] [blame] | 73 | # define INCL_DOSDATETIME |
| 74 | # define INCL_DOSFILEMGR |
| 75 | # define INCL_DOSERRORS |
| 76 | # define INCL_DOSMISC |
| 77 | # define INCL_DOSPROCESS |
pweilbacher | 691902e | 2007-06-22 20:04:36 +0000 | [diff] [blame] | 78 | # define INCL_DOSMODULEMGR |
drh | 60a1e4b | 2006-06-03 18:02:15 +0000 | [diff] [blame] | 79 | # include <os2.h> |
| 80 | # define SQLITE_TEMPNAME_SIZE (CCHMAXPATHCOMP) |
drh | 9cbe635 | 2005-11-29 03:13:21 +0000 | [diff] [blame] | 81 | #else |
| 82 | # define SQLITE_TEMPNAME_SIZE 200 |
drh | 2e66f0b | 2005-04-28 17:18:48 +0000 | [diff] [blame] | 83 | #endif |
drh | 820f381 | 2003-01-08 13:02:52 +0000 | [diff] [blame] | 84 | |
drh | b851b2c | 2005-03-10 14:11:12 +0000 | [diff] [blame] | 85 | /* If the SET_FULLSYNC macro is not defined above, then make it |
| 86 | ** a no-op |
| 87 | */ |
| 88 | #ifndef SET_FULLSYNC |
| 89 | # define SET_FULLSYNC(x,y) |
| 90 | #endif |
| 91 | |
danielk1977 | 6622cce | 2004-05-20 11:00:52 +0000 | [diff] [blame] | 92 | /* |
drh | 3ceeb75 | 2007-03-29 18:19:52 +0000 | [diff] [blame] | 93 | ** The default size of a disk sector |
| 94 | */ |
| 95 | #ifndef SQLITE_DEFAULT_SECTOR_SIZE |
| 96 | # define SQLITE_DEFAULT_SECTOR_SIZE 512 |
| 97 | #endif |
| 98 | |
| 99 | /* |
drh | bbd42a6 | 2004-05-22 17:41:58 +0000 | [diff] [blame] | 100 | ** Temporary files are named starting with this prefix followed by 16 random |
| 101 | ** alphanumeric characters, and no file extension. They are stored in the |
| 102 | ** OS's standard temporary file directory, and are deleted prior to exit. |
| 103 | ** If sqlite is being embedded in another program, you may wish to change the |
| 104 | ** prefix to reflect your program's name, so that if your program exits |
| 105 | ** prematurely, old temporary files can be easily identified. This can be done |
| 106 | ** using -DTEMP_FILE_PREFIX=myprefix_ on the compiler command line. |
drh | fd288f3 | 2006-10-31 21:27:33 +0000 | [diff] [blame] | 107 | ** |
| 108 | ** 2006-10-31: The default prefix used to be "sqlite_". But then |
| 109 | ** Mcafee started using SQLite in their anti-virus product and it |
| 110 | ** started putting files with the "sqlite" name in the c:/temp folder. |
| 111 | ** This annoyed many windows users. Those users would then do a |
| 112 | ** Google search for "sqlite", find the telephone numbers of the |
| 113 | ** developers and call to wake them up at night and complain. |
| 114 | ** For this reason, the default name prefix is changed to be "sqlite" |
| 115 | ** spelled backwards. So the temp files are still identified, but |
| 116 | ** anybody smart enough to figure out the code is also likely smart |
| 117 | ** enough to know that calling the developer will not help get rid |
| 118 | ** of the file. |
danielk1977 | 6622cce | 2004-05-20 11:00:52 +0000 | [diff] [blame] | 119 | */ |
drh | bbd42a6 | 2004-05-22 17:41:58 +0000 | [diff] [blame] | 120 | #ifndef TEMP_FILE_PREFIX |
drh | fd288f3 | 2006-10-31 21:27:33 +0000 | [diff] [blame] | 121 | # define TEMP_FILE_PREFIX "etilqs_" |
drh | bbd42a6 | 2004-05-22 17:41:58 +0000 | [diff] [blame] | 122 | #endif |
| 123 | |
drh | 824d7c1 | 2006-01-06 12:03:19 +0000 | [diff] [blame] | 124 | /* |
drh | 60a1e4b | 2006-06-03 18:02:15 +0000 | [diff] [blame] | 125 | ** Define the interfaces for Unix, Windows, and OS/2. |
drh | 66560ad | 2006-01-06 14:32:19 +0000 | [diff] [blame] | 126 | */ |
| 127 | #if OS_UNIX |
| 128 | #define sqlite3OsOpenReadWrite sqlite3UnixOpenReadWrite |
| 129 | #define sqlite3OsOpenExclusive sqlite3UnixOpenExclusive |
| 130 | #define sqlite3OsOpenReadOnly sqlite3UnixOpenReadOnly |
| 131 | #define sqlite3OsDelete sqlite3UnixDelete |
| 132 | #define sqlite3OsFileExists sqlite3UnixFileExists |
| 133 | #define sqlite3OsFullPathname sqlite3UnixFullPathname |
| 134 | #define sqlite3OsIsDirWritable sqlite3UnixIsDirWritable |
| 135 | #define sqlite3OsSyncDirectory sqlite3UnixSyncDirectory |
| 136 | #define sqlite3OsTempFileName sqlite3UnixTempFileName |
| 137 | #define sqlite3OsRandomSeed sqlite3UnixRandomSeed |
| 138 | #define sqlite3OsSleep sqlite3UnixSleep |
| 139 | #define sqlite3OsCurrentTime sqlite3UnixCurrentTime |
| 140 | #define sqlite3OsEnterMutex sqlite3UnixEnterMutex |
| 141 | #define sqlite3OsLeaveMutex sqlite3UnixLeaveMutex |
| 142 | #define sqlite3OsInMutex sqlite3UnixInMutex |
| 143 | #define sqlite3OsThreadSpecificData sqlite3UnixThreadSpecificData |
| 144 | #define sqlite3OsMalloc sqlite3GenericMalloc |
| 145 | #define sqlite3OsRealloc sqlite3GenericRealloc |
| 146 | #define sqlite3OsFree sqlite3GenericFree |
| 147 | #define sqlite3OsAllocationSize sqlite3GenericAllocationSize |
drh | 761df87 | 2006-12-21 01:29:22 +0000 | [diff] [blame] | 148 | #define sqlite3OsDlopen sqlite3UnixDlopen |
| 149 | #define sqlite3OsDlsym sqlite3UnixDlsym |
| 150 | #define sqlite3OsDlclose sqlite3UnixDlclose |
drh | 66560ad | 2006-01-06 14:32:19 +0000 | [diff] [blame] | 151 | #endif |
| 152 | #if OS_WIN |
| 153 | #define sqlite3OsOpenReadWrite sqlite3WinOpenReadWrite |
| 154 | #define sqlite3OsOpenExclusive sqlite3WinOpenExclusive |
| 155 | #define sqlite3OsOpenReadOnly sqlite3WinOpenReadOnly |
| 156 | #define sqlite3OsDelete sqlite3WinDelete |
| 157 | #define sqlite3OsFileExists sqlite3WinFileExists |
| 158 | #define sqlite3OsFullPathname sqlite3WinFullPathname |
| 159 | #define sqlite3OsIsDirWritable sqlite3WinIsDirWritable |
| 160 | #define sqlite3OsSyncDirectory sqlite3WinSyncDirectory |
| 161 | #define sqlite3OsTempFileName sqlite3WinTempFileName |
| 162 | #define sqlite3OsRandomSeed sqlite3WinRandomSeed |
| 163 | #define sqlite3OsSleep sqlite3WinSleep |
| 164 | #define sqlite3OsCurrentTime sqlite3WinCurrentTime |
| 165 | #define sqlite3OsEnterMutex sqlite3WinEnterMutex |
| 166 | #define sqlite3OsLeaveMutex sqlite3WinLeaveMutex |
| 167 | #define sqlite3OsInMutex sqlite3WinInMutex |
| 168 | #define sqlite3OsThreadSpecificData sqlite3WinThreadSpecificData |
| 169 | #define sqlite3OsMalloc sqlite3GenericMalloc |
| 170 | #define sqlite3OsRealloc sqlite3GenericRealloc |
| 171 | #define sqlite3OsFree sqlite3GenericFree |
| 172 | #define sqlite3OsAllocationSize sqlite3GenericAllocationSize |
drh | 761df87 | 2006-12-21 01:29:22 +0000 | [diff] [blame] | 173 | #define sqlite3OsDlopen sqlite3WinDlopen |
| 174 | #define sqlite3OsDlsym sqlite3WinDlsym |
| 175 | #define sqlite3OsDlclose sqlite3WinDlclose |
drh | 66560ad | 2006-01-06 14:32:19 +0000 | [diff] [blame] | 176 | #endif |
drh | 60a1e4b | 2006-06-03 18:02:15 +0000 | [diff] [blame] | 177 | #if OS_OS2 |
| 178 | #define sqlite3OsOpenReadWrite sqlite3Os2OpenReadWrite |
| 179 | #define sqlite3OsOpenExclusive sqlite3Os2OpenExclusive |
| 180 | #define sqlite3OsOpenReadOnly sqlite3Os2OpenReadOnly |
| 181 | #define sqlite3OsDelete sqlite3Os2Delete |
| 182 | #define sqlite3OsFileExists sqlite3Os2FileExists |
| 183 | #define sqlite3OsFullPathname sqlite3Os2FullPathname |
| 184 | #define sqlite3OsIsDirWritable sqlite3Os2IsDirWritable |
| 185 | #define sqlite3OsSyncDirectory sqlite3Os2SyncDirectory |
| 186 | #define sqlite3OsTempFileName sqlite3Os2TempFileName |
| 187 | #define sqlite3OsRandomSeed sqlite3Os2RandomSeed |
| 188 | #define sqlite3OsSleep sqlite3Os2Sleep |
| 189 | #define sqlite3OsCurrentTime sqlite3Os2CurrentTime |
| 190 | #define sqlite3OsEnterMutex sqlite3Os2EnterMutex |
| 191 | #define sqlite3OsLeaveMutex sqlite3Os2LeaveMutex |
| 192 | #define sqlite3OsInMutex sqlite3Os2InMutex |
| 193 | #define sqlite3OsThreadSpecificData sqlite3Os2ThreadSpecificData |
| 194 | #define sqlite3OsMalloc sqlite3GenericMalloc |
| 195 | #define sqlite3OsRealloc sqlite3GenericRealloc |
| 196 | #define sqlite3OsFree sqlite3GenericFree |
| 197 | #define sqlite3OsAllocationSize sqlite3GenericAllocationSize |
drh | 761df87 | 2006-12-21 01:29:22 +0000 | [diff] [blame] | 198 | #define sqlite3OsDlopen sqlite3Os2Dlopen |
| 199 | #define sqlite3OsDlsym sqlite3Os2Dlsym |
| 200 | #define sqlite3OsDlclose sqlite3Os2Dlclose |
drh | 60a1e4b | 2006-06-03 18:02:15 +0000 | [diff] [blame] | 201 | #endif |
| 202 | |
drh | 66560ad | 2006-01-06 14:32:19 +0000 | [diff] [blame] | 203 | |
pweilbacher | 04bd0c1 | 2006-06-24 12:38:59 +0000 | [diff] [blame] | 204 | |
| 205 | |
drh | 66560ad | 2006-01-06 14:32:19 +0000 | [diff] [blame] | 206 | /* |
| 207 | ** If using an alternative OS interface, then we must have an "os_other.h" |
| 208 | ** header file available for that interface. Presumably the "os_other.h" |
| 209 | ** header file contains #defines similar to those above. |
| 210 | */ |
| 211 | #if OS_OTHER |
| 212 | # include "os_other.h" |
| 213 | #endif |
| 214 | |
| 215 | |
| 216 | |
| 217 | /* |
drh | 824d7c1 | 2006-01-06 12:03:19 +0000 | [diff] [blame] | 218 | ** Forward declarations |
| 219 | */ |
| 220 | typedef struct OsFile OsFile; |
| 221 | typedef struct IoMethod IoMethod; |
drh | 0ccebe7 | 2005-06-07 22:22:50 +0000 | [diff] [blame] | 222 | |
drh | 824d7c1 | 2006-01-06 12:03:19 +0000 | [diff] [blame] | 223 | /* |
| 224 | ** An instance of the following structure contains pointers to all |
| 225 | ** methods on an OsFile object. |
| 226 | */ |
| 227 | struct IoMethod { |
| 228 | int (*xClose)(OsFile**); |
| 229 | int (*xOpenDirectory)(OsFile*, const char*); |
| 230 | int (*xRead)(OsFile*, void*, int amt); |
| 231 | int (*xWrite)(OsFile*, const void*, int amt); |
| 232 | int (*xSeek)(OsFile*, i64 offset); |
| 233 | int (*xTruncate)(OsFile*, i64 size); |
| 234 | int (*xSync)(OsFile*, int); |
| 235 | void (*xSetFullSync)(OsFile *id, int setting); |
| 236 | int (*xFileHandle)(OsFile *id); |
| 237 | int (*xFileSize)(OsFile*, i64 *pSize); |
| 238 | int (*xLock)(OsFile*, int); |
| 239 | int (*xUnlock)(OsFile*, int); |
| 240 | int (*xLockState)(OsFile *id); |
| 241 | int (*xCheckReservedLock)(OsFile *id); |
danielk1977 | b472117 | 2007-03-19 05:54:48 +0000 | [diff] [blame] | 242 | int (*xSectorSize)(OsFile *id); |
drh | 824d7c1 | 2006-01-06 12:03:19 +0000 | [diff] [blame] | 243 | }; |
| 244 | |
| 245 | /* |
| 246 | ** The OsFile object describes an open disk file in an OS-dependent way. |
| 247 | ** The version of OsFile defined here is a generic version. Each OS |
| 248 | ** implementation defines its own subclass of this structure that contains |
| 249 | ** additional information needed to handle file I/O. But the pMethod |
| 250 | ** entry (pointing to the virtual function table) always occurs first |
| 251 | ** so that we can always find the appropriate methods. |
| 252 | */ |
| 253 | struct OsFile { |
| 254 | IoMethod const *pMethod; |
| 255 | }; |
| 256 | |
| 257 | /* |
| 258 | ** The following values may be passed as the second argument to |
| 259 | ** sqlite3OsLock(). The various locks exhibit the following semantics: |
| 260 | ** |
| 261 | ** SHARED: Any number of processes may hold a SHARED lock simultaneously. |
| 262 | ** RESERVED: A single process may hold a RESERVED lock on a file at |
| 263 | ** any time. Other processes may hold and obtain new SHARED locks. |
| 264 | ** PENDING: A single process may hold a PENDING lock on a file at |
| 265 | ** any one time. Existing SHARED locks may persist, but no new |
| 266 | ** SHARED locks may be obtained by other processes. |
| 267 | ** EXCLUSIVE: An EXCLUSIVE lock precludes all other locks. |
| 268 | ** |
| 269 | ** PENDING_LOCK may not be passed directly to sqlite3OsLock(). Instead, a |
| 270 | ** process that requests an EXCLUSIVE lock may actually obtain a PENDING |
| 271 | ** lock. This can be upgraded to an EXCLUSIVE lock by a subsequent call to |
| 272 | ** sqlite3OsLock(). |
| 273 | */ |
| 274 | #define NO_LOCK 0 |
| 275 | #define SHARED_LOCK 1 |
| 276 | #define RESERVED_LOCK 2 |
| 277 | #define PENDING_LOCK 3 |
| 278 | #define EXCLUSIVE_LOCK 4 |
| 279 | |
| 280 | /* |
| 281 | ** File Locking Notes: (Mostly about windows but also some info for Unix) |
| 282 | ** |
| 283 | ** We cannot use LockFileEx() or UnlockFileEx() on Win95/98/ME because |
| 284 | ** those functions are not available. So we use only LockFile() and |
| 285 | ** UnlockFile(). |
| 286 | ** |
| 287 | ** LockFile() prevents not just writing but also reading by other processes. |
| 288 | ** A SHARED_LOCK is obtained by locking a single randomly-chosen |
| 289 | ** byte out of a specific range of bytes. The lock byte is obtained at |
| 290 | ** random so two separate readers can probably access the file at the |
| 291 | ** same time, unless they are unlucky and choose the same lock byte. |
| 292 | ** An EXCLUSIVE_LOCK is obtained by locking all bytes in the range. |
| 293 | ** There can only be one writer. A RESERVED_LOCK is obtained by locking |
| 294 | ** a single byte of the file that is designated as the reserved lock byte. |
| 295 | ** A PENDING_LOCK is obtained by locking a designated byte different from |
| 296 | ** the RESERVED_LOCK byte. |
| 297 | ** |
| 298 | ** On WinNT/2K/XP systems, LockFileEx() and UnlockFileEx() are available, |
| 299 | ** which means we can use reader/writer locks. When reader/writer locks |
| 300 | ** are used, the lock is placed on the same range of bytes that is used |
| 301 | ** for probabilistic locking in Win95/98/ME. Hence, the locking scheme |
| 302 | ** will support two or more Win95 readers or two or more WinNT readers. |
| 303 | ** But a single Win95 reader will lock out all WinNT readers and a single |
| 304 | ** WinNT reader will lock out all other Win95 readers. |
| 305 | ** |
| 306 | ** The following #defines specify the range of bytes used for locking. |
| 307 | ** SHARED_SIZE is the number of bytes available in the pool from which |
| 308 | ** a random byte is selected for a shared lock. The pool of bytes for |
| 309 | ** shared locks begins at SHARED_FIRST. |
| 310 | ** |
| 311 | ** These #defines are available in sqlite_aux.h so that adaptors for |
| 312 | ** connecting SQLite to other operating systems can use the same byte |
| 313 | ** ranges for locking. In particular, the same locking strategy and |
| 314 | ** byte ranges are used for Unix. This leaves open the possiblity of having |
| 315 | ** clients on win95, winNT, and unix all talking to the same shared file |
| 316 | ** and all locking correctly. To do so would require that samba (or whatever |
| 317 | ** tool is being used for file sharing) implements locks correctly between |
| 318 | ** windows and unix. I'm guessing that isn't likely to happen, but by |
| 319 | ** using the same locking range we are at least open to the possibility. |
| 320 | ** |
| 321 | ** Locking in windows is manditory. For this reason, we cannot store |
| 322 | ** actual data in the bytes used for locking. The pager never allocates |
| 323 | ** the pages involved in locking therefore. SHARED_SIZE is selected so |
| 324 | ** that all locks will fit on a single page even at the minimum page size. |
| 325 | ** PENDING_BYTE defines the beginning of the locks. By default PENDING_BYTE |
| 326 | ** is set high so that we don't have to allocate an unused page except |
| 327 | ** for very large databases. But one should test the page skipping logic |
| 328 | ** by setting PENDING_BYTE low and running the entire regression suite. |
| 329 | ** |
| 330 | ** Changing the value of PENDING_BYTE results in a subtly incompatible |
| 331 | ** file format. Depending on how it is changed, you might not notice |
| 332 | ** the incompatibility right away, even running a full regression test. |
| 333 | ** The default location of PENDING_BYTE is the first byte past the |
| 334 | ** 1GB boundary. |
| 335 | ** |
| 336 | */ |
| 337 | #ifndef SQLITE_TEST |
| 338 | #define PENDING_BYTE 0x40000000 /* First byte past the 1GB boundary */ |
| 339 | #else |
| 340 | extern unsigned int sqlite3_pending_byte; |
| 341 | #define PENDING_BYTE sqlite3_pending_byte |
| 342 | #endif |
| 343 | |
| 344 | #define RESERVED_BYTE (PENDING_BYTE+1) |
| 345 | #define SHARED_FIRST (PENDING_BYTE+2) |
| 346 | #define SHARED_SIZE 510 |
| 347 | |
| 348 | /* |
drh | 66560ad | 2006-01-06 14:32:19 +0000 | [diff] [blame] | 349 | ** Prototypes for operating system interface routines. |
drh | 824d7c1 | 2006-01-06 12:03:19 +0000 | [diff] [blame] | 350 | */ |
| 351 | int sqlite3OsClose(OsFile**); |
| 352 | int sqlite3OsOpenDirectory(OsFile*, const char*); |
| 353 | int sqlite3OsRead(OsFile*, void*, int amt); |
| 354 | int sqlite3OsWrite(OsFile*, const void*, int amt); |
| 355 | int sqlite3OsSeek(OsFile*, i64 offset); |
| 356 | int sqlite3OsTruncate(OsFile*, i64 size); |
| 357 | int sqlite3OsSync(OsFile*, int); |
| 358 | void sqlite3OsSetFullSync(OsFile *id, int setting); |
drh | 824d7c1 | 2006-01-06 12:03:19 +0000 | [diff] [blame] | 359 | int sqlite3OsFileSize(OsFile*, i64 *pSize); |
| 360 | int sqlite3OsLock(OsFile*, int); |
| 361 | int sqlite3OsUnlock(OsFile*, int); |
drh | 824d7c1 | 2006-01-06 12:03:19 +0000 | [diff] [blame] | 362 | int sqlite3OsCheckReservedLock(OsFile *id); |
drh | 66560ad | 2006-01-06 14:32:19 +0000 | [diff] [blame] | 363 | int sqlite3OsOpenReadWrite(const char*, OsFile**, int*); |
| 364 | int sqlite3OsOpenExclusive(const char*, OsFile**, int); |
| 365 | int sqlite3OsOpenReadOnly(const char*, OsFile**); |
| 366 | int sqlite3OsDelete(const char*); |
| 367 | int sqlite3OsFileExists(const char*); |
| 368 | char *sqlite3OsFullPathname(const char*); |
| 369 | int sqlite3OsIsDirWritable(char*); |
| 370 | int sqlite3OsSyncDirectory(const char*); |
danielk1977 | b472117 | 2007-03-19 05:54:48 +0000 | [diff] [blame] | 371 | int sqlite3OsSectorSize(OsFile *id); |
drh | 66560ad | 2006-01-06 14:32:19 +0000 | [diff] [blame] | 372 | int sqlite3OsTempFileName(char*); |
| 373 | int sqlite3OsRandomSeed(char*); |
| 374 | int sqlite3OsSleep(int ms); |
| 375 | int sqlite3OsCurrentTime(double*); |
| 376 | void sqlite3OsEnterMutex(void); |
| 377 | void sqlite3OsLeaveMutex(void); |
drh | 757b04e | 2006-01-18 17:25:45 +0000 | [diff] [blame] | 378 | int sqlite3OsInMutex(int); |
drh | 6f7adc8 | 2006-01-11 21:41:20 +0000 | [diff] [blame] | 379 | ThreadData *sqlite3OsThreadSpecificData(int); |
drh | 66560ad | 2006-01-06 14:32:19 +0000 | [diff] [blame] | 380 | void *sqlite3OsMalloc(int); |
| 381 | void *sqlite3OsRealloc(void *, int); |
| 382 | void sqlite3OsFree(void *); |
| 383 | int sqlite3OsAllocationSize(void *); |
drh | 761df87 | 2006-12-21 01:29:22 +0000 | [diff] [blame] | 384 | void *sqlite3OsDlopen(const char*); |
| 385 | void *sqlite3OsDlsym(void*, const char*); |
| 386 | int sqlite3OsDlclose(void*); |
drh | 9c06c95 | 2005-11-26 00:25:00 +0000 | [diff] [blame] | 387 | |
drh | 87cc3b3 | 2007-05-08 21:45:27 +0000 | [diff] [blame] | 388 | #if defined(SQLITE_TEST) || defined(SQLITE_DEBUG) |
| 389 | int sqlite3OsFileHandle(OsFile *id); |
| 390 | int sqlite3OsLockState(OsFile *id); |
| 391 | #endif |
| 392 | |
drh | 3f45902 | 2006-01-07 16:06:07 +0000 | [diff] [blame] | 393 | /* |
| 394 | ** If the SQLITE_ENABLE_REDEF_IO macro is defined, then the OS-layer |
| 395 | ** interface routines are not called directly but are invoked using |
| 396 | ** pointers to functions. This allows the implementation of various |
| 397 | ** OS-layer interface routines to be modified at run-time. There are |
| 398 | ** obscure but legitimate reasons for wanting to do this. But for |
| 399 | ** most users, a direct call to the underlying interface is preferable |
| 400 | ** so the the redefinable I/O interface is turned off by default. |
| 401 | */ |
| 402 | #ifdef SQLITE_ENABLE_REDEF_IO |
| 403 | |
| 404 | /* |
| 405 | ** When redefinable I/O is enabled, a single global instance of the |
| 406 | ** following structure holds pointers to the routines that SQLite |
| 407 | ** uses to talk with the underlying operating system. Modify this |
| 408 | ** structure (before using any SQLite API!) to accomodate perculiar |
| 409 | ** operating system interfaces or behaviors. |
| 410 | */ |
| 411 | struct sqlite3OsVtbl { |
| 412 | int (*xOpenReadWrite)(const char*, OsFile**, int*); |
| 413 | int (*xOpenExclusive)(const char*, OsFile**, int); |
| 414 | int (*xOpenReadOnly)(const char*, OsFile**); |
| 415 | |
| 416 | int (*xDelete)(const char*); |
| 417 | int (*xFileExists)(const char*); |
| 418 | char *(*xFullPathname)(const char*); |
| 419 | int (*xIsDirWritable)(char*); |
| 420 | int (*xSyncDirectory)(const char*); |
| 421 | int (*xTempFileName)(char*); |
| 422 | |
| 423 | int (*xRandomSeed)(char*); |
| 424 | int (*xSleep)(int ms); |
| 425 | int (*xCurrentTime)(double*); |
| 426 | |
| 427 | void (*xEnterMutex)(void); |
| 428 | void (*xLeaveMutex)(void); |
drh | bcdb661 | 2006-01-21 19:57:50 +0000 | [diff] [blame] | 429 | int (*xInMutex)(int); |
drh | 6f7adc8 | 2006-01-11 21:41:20 +0000 | [diff] [blame] | 430 | ThreadData *(*xThreadSpecificData)(int); |
drh | 3f45902 | 2006-01-07 16:06:07 +0000 | [diff] [blame] | 431 | |
| 432 | void *(*xMalloc)(int); |
| 433 | void *(*xRealloc)(void *, int); |
| 434 | void (*xFree)(void *); |
| 435 | int (*xAllocationSize)(void *); |
drh | 761df87 | 2006-12-21 01:29:22 +0000 | [diff] [blame] | 436 | |
| 437 | void *(*xDlopen)(const char*); |
| 438 | void *(*xDlsym)(void*, const char*); |
| 439 | int (*xDlclose)(void*); |
drh | 3f45902 | 2006-01-07 16:06:07 +0000 | [diff] [blame] | 440 | }; |
| 441 | |
| 442 | /* Macro used to comment out routines that do not exists when there is |
drh | 761df87 | 2006-12-21 01:29:22 +0000 | [diff] [blame] | 443 | ** no disk I/O or extension loading |
drh | 3f45902 | 2006-01-07 16:06:07 +0000 | [diff] [blame] | 444 | */ |
| 445 | #ifdef SQLITE_OMIT_DISKIO |
| 446 | # define IF_DISKIO(X) 0 |
| 447 | #else |
| 448 | # define IF_DISKIO(X) X |
| 449 | #endif |
drh | 761df87 | 2006-12-21 01:29:22 +0000 | [diff] [blame] | 450 | #ifdef SQLITE_OMIT_LOAD_EXTENSION |
| 451 | # define IF_DLOPEN(X) 0 |
| 452 | #else |
| 453 | # define IF_DLOPEN(X) X |
| 454 | #endif |
| 455 | |
drh | 3f45902 | 2006-01-07 16:06:07 +0000 | [diff] [blame] | 456 | |
drh | bd08af4 | 2007-04-05 21:58:33 +0000 | [diff] [blame] | 457 | #if defined(_SQLITE_OS_C_) || defined(SQLITE_AMALGAMATION) |
drh | 3f45902 | 2006-01-07 16:06:07 +0000 | [diff] [blame] | 458 | /* |
| 459 | ** The os.c file implements the global virtual function table. |
drh | bd08af4 | 2007-04-05 21:58:33 +0000 | [diff] [blame] | 460 | ** We have to put this file here because the initializers |
| 461 | ** (ex: sqlite3OsRandomSeed) are macros that are about to be |
| 462 | ** redefined. |
drh | 3f45902 | 2006-01-07 16:06:07 +0000 | [diff] [blame] | 463 | */ |
| 464 | struct sqlite3OsVtbl sqlite3Os = { |
| 465 | IF_DISKIO( sqlite3OsOpenReadWrite ), |
| 466 | IF_DISKIO( sqlite3OsOpenExclusive ), |
| 467 | IF_DISKIO( sqlite3OsOpenReadOnly ), |
| 468 | IF_DISKIO( sqlite3OsDelete ), |
| 469 | IF_DISKIO( sqlite3OsFileExists ), |
| 470 | IF_DISKIO( sqlite3OsFullPathname ), |
| 471 | IF_DISKIO( sqlite3OsIsDirWritable ), |
| 472 | IF_DISKIO( sqlite3OsSyncDirectory ), |
| 473 | IF_DISKIO( sqlite3OsTempFileName ), |
| 474 | sqlite3OsRandomSeed, |
| 475 | sqlite3OsSleep, |
| 476 | sqlite3OsCurrentTime, |
| 477 | sqlite3OsEnterMutex, |
| 478 | sqlite3OsLeaveMutex, |
| 479 | sqlite3OsInMutex, |
| 480 | sqlite3OsThreadSpecificData, |
| 481 | sqlite3OsMalloc, |
| 482 | sqlite3OsRealloc, |
| 483 | sqlite3OsFree, |
drh | 761df87 | 2006-12-21 01:29:22 +0000 | [diff] [blame] | 484 | sqlite3OsAllocationSize, |
| 485 | IF_DLOPEN( sqlite3OsDlopen ), |
| 486 | IF_DLOPEN( sqlite3OsDlsym ), |
| 487 | IF_DLOPEN( sqlite3OsDlclose ), |
drh | 3f45902 | 2006-01-07 16:06:07 +0000 | [diff] [blame] | 488 | }; |
| 489 | #else |
| 490 | /* |
| 491 | ** Files other than os.c just reference the global virtual function table. |
| 492 | */ |
| 493 | extern struct sqlite3OsVtbl sqlite3Os; |
| 494 | #endif /* _SQLITE_OS_C_ */ |
| 495 | |
| 496 | |
| 497 | /* This additional API routine is available with redefinable I/O */ |
| 498 | struct sqlite3OsVtbl *sqlite3_os_switch(void); |
| 499 | |
| 500 | |
| 501 | /* |
| 502 | ** Redefine the OS interface to go through the virtual function table |
| 503 | ** rather than calling routines directly. |
| 504 | */ |
| 505 | #undef sqlite3OsOpenReadWrite |
| 506 | #undef sqlite3OsOpenExclusive |
| 507 | #undef sqlite3OsOpenReadOnly |
| 508 | #undef sqlite3OsDelete |
| 509 | #undef sqlite3OsFileExists |
| 510 | #undef sqlite3OsFullPathname |
| 511 | #undef sqlite3OsIsDirWritable |
| 512 | #undef sqlite3OsSyncDirectory |
| 513 | #undef sqlite3OsTempFileName |
| 514 | #undef sqlite3OsRandomSeed |
| 515 | #undef sqlite3OsSleep |
| 516 | #undef sqlite3OsCurrentTime |
| 517 | #undef sqlite3OsEnterMutex |
| 518 | #undef sqlite3OsLeaveMutex |
| 519 | #undef sqlite3OsInMutex |
| 520 | #undef sqlite3OsThreadSpecificData |
| 521 | #undef sqlite3OsMalloc |
| 522 | #undef sqlite3OsRealloc |
| 523 | #undef sqlite3OsFree |
| 524 | #undef sqlite3OsAllocationSize |
| 525 | #define sqlite3OsOpenReadWrite sqlite3Os.xOpenReadWrite |
| 526 | #define sqlite3OsOpenExclusive sqlite3Os.xOpenExclusive |
| 527 | #define sqlite3OsOpenReadOnly sqlite3Os.xOpenReadOnly |
| 528 | #define sqlite3OsDelete sqlite3Os.xDelete |
| 529 | #define sqlite3OsFileExists sqlite3Os.xFileExists |
| 530 | #define sqlite3OsFullPathname sqlite3Os.xFullPathname |
| 531 | #define sqlite3OsIsDirWritable sqlite3Os.xIsDirWritable |
| 532 | #define sqlite3OsSyncDirectory sqlite3Os.xSyncDirectory |
| 533 | #define sqlite3OsTempFileName sqlite3Os.xTempFileName |
| 534 | #define sqlite3OsRandomSeed sqlite3Os.xRandomSeed |
| 535 | #define sqlite3OsSleep sqlite3Os.xSleep |
| 536 | #define sqlite3OsCurrentTime sqlite3Os.xCurrentTime |
| 537 | #define sqlite3OsEnterMutex sqlite3Os.xEnterMutex |
| 538 | #define sqlite3OsLeaveMutex sqlite3Os.xLeaveMutex |
| 539 | #define sqlite3OsInMutex sqlite3Os.xInMutex |
| 540 | #define sqlite3OsThreadSpecificData sqlite3Os.xThreadSpecificData |
| 541 | #define sqlite3OsMalloc sqlite3Os.xMalloc |
| 542 | #define sqlite3OsRealloc sqlite3Os.xRealloc |
| 543 | #define sqlite3OsFree sqlite3Os.xFree |
| 544 | #define sqlite3OsAllocationSize sqlite3Os.xAllocationSize |
| 545 | |
| 546 | #endif /* SQLITE_ENABLE_REDEF_IO */ |
| 547 | |
drh | e3c4137 | 2001-09-17 20:25:58 +0000 | [diff] [blame] | 548 | #endif /* _SQLITE_OS_H_ */ |