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