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 ** This header file is #include-ed by sqliteInt.h and thus ends up 18 ** being included by every source file. 19 */ 20 #ifndef _SQLITE_OS_H_ 21 #define _SQLITE_OS_H_ 22 23 /* 24 ** Figure out if we are dealing with Unix, Windows, or some other 25 ** operating system. After the following block of preprocess macros, 26 ** all of OS_UNIX, OS_WIN, OS_OS2, and OS_OTHER will defined to either 27 ** 1 or 0. One of the four will be 1. The other three will be 0. 28 */ 29 #if defined(OS_OTHER) 30 # if OS_OTHER==1 31 # undef OS_UNIX 32 # define OS_UNIX 0 33 # undef OS_WIN 34 # define OS_WIN 0 35 # undef OS_OS2 36 # define OS_OS2 0 37 # else 38 # undef OS_OTHER 39 # endif 40 #endif 41 #if !defined(OS_UNIX) && !defined(OS_OTHER) 42 # define OS_OTHER 0 43 # ifndef OS_WIN 44 # if defined(_WIN32) || defined(WIN32) || defined(__CYGWIN__) || defined(__MINGW32__) || defined(__BORLANDC__) 45 # define OS_WIN 1 46 # define OS_UNIX 0 47 # define OS_OS2 0 48 # elif defined(__EMX__) || defined(_OS2) || defined(OS2) || defined(_OS2_) || defined(__OS2__) 49 # define OS_WIN 0 50 # define OS_UNIX 0 51 # define OS_OS2 1 52 # else 53 # define OS_WIN 0 54 # define OS_UNIX 1 55 # define OS_OS2 0 56 # endif 57 # else 58 # define OS_UNIX 0 59 # define OS_OS2 0 60 # endif 61 #else 62 # ifndef OS_WIN 63 # define OS_WIN 0 64 # endif 65 #endif 66 67 68 69 /* 70 ** Define the maximum size of a temporary filename 71 */ 72 #if OS_WIN 73 # include <windows.h> 74 # define SQLITE_TEMPNAME_SIZE (MAX_PATH+50) 75 #elif OS_OS2 76 # if (__GNUC__ > 3 || __GNUC__ == 3 && __GNUC_MINOR__ >= 3) && defined(OS2_HIGH_MEMORY) 77 # include <os2safe.h> /* has to be included before os2.h for linking to work */ 78 # endif 79 # define INCL_DOSDATETIME 80 # define INCL_DOSFILEMGR 81 # define INCL_DOSERRORS 82 # define INCL_DOSMISC 83 # define INCL_DOSPROCESS 84 # define INCL_DOSMODULEMGR 85 # define INCL_DOSSEMAPHORES 86 # include <os2.h> 87 # define SQLITE_TEMPNAME_SIZE (CCHMAXPATHCOMP) 88 #else 89 # define SQLITE_TEMPNAME_SIZE 200 90 #endif 91 92 /* If the SET_FULLSYNC macro is not defined above, then make it 93 ** a no-op 94 */ 95 #ifndef SET_FULLSYNC 96 # define SET_FULLSYNC(x,y) 97 #endif 98 99 /* 100 ** The default size of a disk sector 101 */ 102 #ifndef SQLITE_DEFAULT_SECTOR_SIZE 103 # define SQLITE_DEFAULT_SECTOR_SIZE 512 104 #endif 105 106 /* 107 ** Temporary files are named starting with this prefix followed by 16 random 108 ** alphanumeric characters, and no file extension. They are stored in the 109 ** OS's standard temporary file directory, and are deleted prior to exit. 110 ** If sqlite is being embedded in another program, you may wish to change the 111 ** prefix to reflect your program's name, so that if your program exits 112 ** prematurely, old temporary files can be easily identified. This can be done 113 ** using -DSQLITE_TEMP_FILE_PREFIX=myprefix_ on the compiler command line. 114 ** 115 ** 2006-10-31: The default prefix used to be "sqlite_". But then 116 ** Mcafee started using SQLite in their anti-virus product and it 117 ** started putting files with the "sqlite" name in the c:/temp folder. 118 ** This annoyed many windows users. Those users would then do a 119 ** Google search for "sqlite", find the telephone numbers of the 120 ** developers and call to wake them up at night and complain. 121 ** For this reason, the default name prefix is changed to be "sqlite" 122 ** spelled backwards. So the temp files are still identified, but 123 ** anybody smart enough to figure out the code is also likely smart 124 ** enough to know that calling the developer will not help get rid 125 ** of the file. 126 */ 127 #ifndef SQLITE_TEMP_FILE_PREFIX 128 # define SQLITE_TEMP_FILE_PREFIX "etilqs_" 129 #endif 130 131 /* 132 ** If using an alternative OS interface, then we must have an "os_other.h" 133 ** header file available for that interface. Presumably the "os_other.h" 134 ** header file contains #defines similar to those above. 135 */ 136 #if OS_OTHER 137 # include "os_other.h" 138 #endif 139 140 141 /* 142 ** The following values may be passed as the second argument to 143 ** sqlite3OsLock(). The various locks exhibit the following semantics: 144 ** 145 ** SHARED: Any number of processes may hold a SHARED lock simultaneously. 146 ** RESERVED: A single process may hold a RESERVED lock on a file at 147 ** any time. Other processes may hold and obtain new SHARED locks. 148 ** PENDING: A single process may hold a PENDING lock on a file at 149 ** any one time. Existing SHARED locks may persist, but no new 150 ** SHARED locks may be obtained by other processes. 151 ** EXCLUSIVE: An EXCLUSIVE lock precludes all other locks. 152 ** 153 ** PENDING_LOCK may not be passed directly to sqlite3OsLock(). Instead, a 154 ** process that requests an EXCLUSIVE lock may actually obtain a PENDING 155 ** lock. This can be upgraded to an EXCLUSIVE lock by a subsequent call to 156 ** sqlite3OsLock(). 157 */ 158 #define NO_LOCK 0 159 #define SHARED_LOCK 1 160 #define RESERVED_LOCK 2 161 #define PENDING_LOCK 3 162 #define EXCLUSIVE_LOCK 4 163 164 /* 165 ** File Locking Notes: (Mostly about windows but also some info for Unix) 166 ** 167 ** We cannot use LockFileEx() or UnlockFileEx() on Win95/98/ME because 168 ** those functions are not available. So we use only LockFile() and 169 ** UnlockFile(). 170 ** 171 ** LockFile() prevents not just writing but also reading by other processes. 172 ** A SHARED_LOCK is obtained by locking a single randomly-chosen 173 ** byte out of a specific range of bytes. The lock byte is obtained at 174 ** random so two separate readers can probably access the file at the 175 ** same time, unless they are unlucky and choose the same lock byte. 176 ** An EXCLUSIVE_LOCK is obtained by locking all bytes in the range. 177 ** There can only be one writer. A RESERVED_LOCK is obtained by locking 178 ** a single byte of the file that is designated as the reserved lock byte. 179 ** A PENDING_LOCK is obtained by locking a designated byte different from 180 ** the RESERVED_LOCK byte. 181 ** 182 ** On WinNT/2K/XP systems, LockFileEx() and UnlockFileEx() are available, 183 ** which means we can use reader/writer locks. When reader/writer locks 184 ** are used, the lock is placed on the same range of bytes that is used 185 ** for probabilistic locking in Win95/98/ME. Hence, the locking scheme 186 ** will support two or more Win95 readers or two or more WinNT readers. 187 ** But a single Win95 reader will lock out all WinNT readers and a single 188 ** WinNT reader will lock out all other Win95 readers. 189 ** 190 ** The following #defines specify the range of bytes used for locking. 191 ** SHARED_SIZE is the number of bytes available in the pool from which 192 ** a random byte is selected for a shared lock. The pool of bytes for 193 ** shared locks begins at SHARED_FIRST. 194 ** 195 ** These #defines are available in sqlite_aux.h so that adaptors for 196 ** connecting SQLite to other operating systems can use the same byte 197 ** ranges for locking. In particular, the same locking strategy and 198 ** byte ranges are used for Unix. This leaves open the possiblity of having 199 ** clients on win95, winNT, and unix all talking to the same shared file 200 ** and all locking correctly. To do so would require that samba (or whatever 201 ** tool is being used for file sharing) implements locks correctly between 202 ** windows and unix. I'm guessing that isn't likely to happen, but by 203 ** using the same locking range we are at least open to the possibility. 204 ** 205 ** Locking in windows is manditory. For this reason, we cannot store 206 ** actual data in the bytes used for locking. The pager never allocates 207 ** the pages involved in locking therefore. SHARED_SIZE is selected so 208 ** that all locks will fit on a single page even at the minimum page size. 209 ** PENDING_BYTE defines the beginning of the locks. By default PENDING_BYTE 210 ** is set high so that we don't have to allocate an unused page except 211 ** for very large databases. But one should test the page skipping logic 212 ** by setting PENDING_BYTE low and running the entire regression suite. 213 ** 214 ** Changing the value of PENDING_BYTE results in a subtly incompatible 215 ** file format. Depending on how it is changed, you might not notice 216 ** the incompatibility right away, even running a full regression test. 217 ** The default location of PENDING_BYTE is the first byte past the 218 ** 1GB boundary. 219 ** 220 */ 221 #ifndef SQLITE_TEST 222 #define PENDING_BYTE 0x40000000 /* First byte past the 1GB boundary */ 223 #else 224 extern unsigned int sqlite3_pending_byte; 225 #define PENDING_BYTE sqlite3_pending_byte 226 #endif 227 228 #define RESERVED_BYTE (PENDING_BYTE+1) 229 #define SHARED_FIRST (PENDING_BYTE+2) 230 #define SHARED_SIZE 510 231 232 /* 233 ** Functions for accessing sqlite3_file methods 234 */ 235 int sqlite3OsClose(sqlite3_file*); 236 int sqlite3OsRead(sqlite3_file*, void*, int amt, i64 offset); 237 int sqlite3OsWrite(sqlite3_file*, const void*, int amt, i64 offset); 238 int sqlite3OsTruncate(sqlite3_file*, i64 size); 239 int sqlite3OsSync(sqlite3_file*, int); 240 int sqlite3OsFileSize(sqlite3_file*, i64 *pSize); 241 int sqlite3OsLock(sqlite3_file*, int); 242 int sqlite3OsUnlock(sqlite3_file*, int); 243 int sqlite3OsCheckReservedLock(sqlite3_file *id); 244 int sqlite3OsFileControl(sqlite3_file*,int,void*); 245 int sqlite3OsSectorSize(sqlite3_file *id); 246 int sqlite3OsDeviceCharacteristics(sqlite3_file *id); 247 248 /* 249 ** Functions for accessing sqlite3_vfs methods 250 */ 251 int sqlite3OsOpen(sqlite3_vfs *, const char *, sqlite3_file*, int, int *); 252 int sqlite3OsDelete(sqlite3_vfs *, const char *, int); 253 int sqlite3OsAccess(sqlite3_vfs *, const char *, int); 254 int sqlite3OsGetTempname(sqlite3_vfs *, int, char *); 255 int sqlite3OsFullPathname(sqlite3_vfs *, const char *, int, char *); 256 void *sqlite3OsDlOpen(sqlite3_vfs *, const char *); 257 void sqlite3OsDlError(sqlite3_vfs *, int, char *); 258 void *sqlite3OsDlSym(sqlite3_vfs *, void *, const char *); 259 void sqlite3OsDlClose(sqlite3_vfs *, void *); 260 int sqlite3OsRandomness(sqlite3_vfs *, int, char *); 261 int sqlite3OsSleep(sqlite3_vfs *, int); 262 int sqlite3OsCurrentTime(sqlite3_vfs *, double*); 263 264 /* 265 ** Convenience functions for opening and closing files using 266 ** sqlite3_malloc() to obtain space for the file-handle structure. 267 */ 268 int sqlite3OsOpenMalloc(sqlite3_vfs *, const char *, sqlite3_file **, int,int*); 269 int sqlite3OsCloseFree(sqlite3_file *); 270 271 /* 272 ** Each OS-specific backend defines an instance of the following 273 ** structure for returning a pointer to its sqlite3_vfs. If OS_OTHER 274 ** is defined (meaning that the application-defined OS interface layer 275 ** is used) then there is no default VFS. The application must 276 ** register one or more VFS structures using sqlite3_vfs_register() 277 ** before attempting to use SQLite. 278 */ 279 #if OS_UNIX || OS_WIN || OS_OS2 280 sqlite3_vfs *sqlite3OsDefaultVfs(void); 281 #else 282 # define sqlite3OsDefaultVfs(X) 0 283 #endif 284 285 #endif /* _SQLITE_OS_H_ */ 286