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 | 66560ad | 2006-01-06 14:32:19 +0000 | [diff] [blame] | 124 | /* |
| 125 | ** If using an alternative OS interface, then we must have an "os_other.h" |
| 126 | ** header file available for that interface. Presumably the "os_other.h" |
| 127 | ** header file contains #defines similar to those above. |
| 128 | */ |
| 129 | #if OS_OTHER |
| 130 | # include "os_other.h" |
| 131 | #endif |
| 132 | |
| 133 | |
drh | 824d7c1 | 2006-01-06 12:03:19 +0000 | [diff] [blame] | 134 | /* |
| 135 | ** The following values may be passed as the second argument to |
| 136 | ** sqlite3OsLock(). The various locks exhibit the following semantics: |
| 137 | ** |
| 138 | ** SHARED: Any number of processes may hold a SHARED lock simultaneously. |
| 139 | ** RESERVED: A single process may hold a RESERVED lock on a file at |
| 140 | ** any time. Other processes may hold and obtain new SHARED locks. |
| 141 | ** PENDING: A single process may hold a PENDING lock on a file at |
| 142 | ** any one time. Existing SHARED locks may persist, but no new |
| 143 | ** SHARED locks may be obtained by other processes. |
| 144 | ** EXCLUSIVE: An EXCLUSIVE lock precludes all other locks. |
| 145 | ** |
| 146 | ** PENDING_LOCK may not be passed directly to sqlite3OsLock(). Instead, a |
| 147 | ** process that requests an EXCLUSIVE lock may actually obtain a PENDING |
| 148 | ** lock. This can be upgraded to an EXCLUSIVE lock by a subsequent call to |
| 149 | ** sqlite3OsLock(). |
| 150 | */ |
| 151 | #define NO_LOCK 0 |
| 152 | #define SHARED_LOCK 1 |
| 153 | #define RESERVED_LOCK 2 |
| 154 | #define PENDING_LOCK 3 |
| 155 | #define EXCLUSIVE_LOCK 4 |
| 156 | |
| 157 | /* |
| 158 | ** File Locking Notes: (Mostly about windows but also some info for Unix) |
| 159 | ** |
| 160 | ** We cannot use LockFileEx() or UnlockFileEx() on Win95/98/ME because |
| 161 | ** those functions are not available. So we use only LockFile() and |
| 162 | ** UnlockFile(). |
| 163 | ** |
| 164 | ** LockFile() prevents not just writing but also reading by other processes. |
| 165 | ** A SHARED_LOCK is obtained by locking a single randomly-chosen |
| 166 | ** byte out of a specific range of bytes. The lock byte is obtained at |
| 167 | ** random so two separate readers can probably access the file at the |
| 168 | ** same time, unless they are unlucky and choose the same lock byte. |
| 169 | ** An EXCLUSIVE_LOCK is obtained by locking all bytes in the range. |
| 170 | ** There can only be one writer. A RESERVED_LOCK is obtained by locking |
| 171 | ** a single byte of the file that is designated as the reserved lock byte. |
| 172 | ** A PENDING_LOCK is obtained by locking a designated byte different from |
| 173 | ** the RESERVED_LOCK byte. |
| 174 | ** |
| 175 | ** On WinNT/2K/XP systems, LockFileEx() and UnlockFileEx() are available, |
| 176 | ** which means we can use reader/writer locks. When reader/writer locks |
| 177 | ** are used, the lock is placed on the same range of bytes that is used |
| 178 | ** for probabilistic locking in Win95/98/ME. Hence, the locking scheme |
| 179 | ** will support two or more Win95 readers or two or more WinNT readers. |
| 180 | ** But a single Win95 reader will lock out all WinNT readers and a single |
| 181 | ** WinNT reader will lock out all other Win95 readers. |
| 182 | ** |
| 183 | ** The following #defines specify the range of bytes used for locking. |
| 184 | ** SHARED_SIZE is the number of bytes available in the pool from which |
| 185 | ** a random byte is selected for a shared lock. The pool of bytes for |
| 186 | ** shared locks begins at SHARED_FIRST. |
| 187 | ** |
| 188 | ** These #defines are available in sqlite_aux.h so that adaptors for |
| 189 | ** connecting SQLite to other operating systems can use the same byte |
| 190 | ** ranges for locking. In particular, the same locking strategy and |
| 191 | ** byte ranges are used for Unix. This leaves open the possiblity of having |
| 192 | ** clients on win95, winNT, and unix all talking to the same shared file |
| 193 | ** and all locking correctly. To do so would require that samba (or whatever |
| 194 | ** tool is being used for file sharing) implements locks correctly between |
| 195 | ** windows and unix. I'm guessing that isn't likely to happen, but by |
| 196 | ** using the same locking range we are at least open to the possibility. |
| 197 | ** |
| 198 | ** Locking in windows is manditory. For this reason, we cannot store |
| 199 | ** actual data in the bytes used for locking. The pager never allocates |
| 200 | ** the pages involved in locking therefore. SHARED_SIZE is selected so |
| 201 | ** that all locks will fit on a single page even at the minimum page size. |
| 202 | ** PENDING_BYTE defines the beginning of the locks. By default PENDING_BYTE |
| 203 | ** is set high so that we don't have to allocate an unused page except |
| 204 | ** for very large databases. But one should test the page skipping logic |
| 205 | ** by setting PENDING_BYTE low and running the entire regression suite. |
| 206 | ** |
| 207 | ** Changing the value of PENDING_BYTE results in a subtly incompatible |
| 208 | ** file format. Depending on how it is changed, you might not notice |
| 209 | ** the incompatibility right away, even running a full regression test. |
| 210 | ** The default location of PENDING_BYTE is the first byte past the |
| 211 | ** 1GB boundary. |
| 212 | ** |
| 213 | */ |
| 214 | #ifndef SQLITE_TEST |
| 215 | #define PENDING_BYTE 0x40000000 /* First byte past the 1GB boundary */ |
| 216 | #else |
| 217 | extern unsigned int sqlite3_pending_byte; |
| 218 | #define PENDING_BYTE sqlite3_pending_byte |
| 219 | #endif |
| 220 | |
| 221 | #define RESERVED_BYTE (PENDING_BYTE+1) |
| 222 | #define SHARED_FIRST (PENDING_BYTE+2) |
| 223 | #define SHARED_SIZE 510 |
| 224 | |
danielk1977 | b4b4741 | 2007-08-17 15:53:36 +0000 | [diff] [blame] | 225 | /* |
| 226 | ** Functions for accessing sqlite3_file methods |
drh | 824d7c1 | 2006-01-06 12:03:19 +0000 | [diff] [blame] | 227 | */ |
danielk1977 | b4b4741 | 2007-08-17 15:53:36 +0000 | [diff] [blame] | 228 | int sqlite3OsClose(sqlite3_file*); |
danielk1977 | 6207906 | 2007-08-15 17:08:46 +0000 | [diff] [blame] | 229 | int sqlite3OsRead(sqlite3_file*, void*, int amt, i64 offset); |
| 230 | int sqlite3OsWrite(sqlite3_file*, const void*, int amt, i64 offset); |
| 231 | int sqlite3OsTruncate(sqlite3_file*, i64 size); |
| 232 | int sqlite3OsSync(sqlite3_file*, int); |
| 233 | int sqlite3OsFileSize(sqlite3_file*, i64 *pSize); |
| 234 | int sqlite3OsLock(sqlite3_file*, int); |
| 235 | int sqlite3OsUnlock(sqlite3_file*, int); |
| 236 | int sqlite3OsBreakLock(sqlite3_file*); |
| 237 | int sqlite3OsCheckReservedLock(sqlite3_file *id); |
danielk1977 | 90949c2 | 2007-08-17 16:50:38 +0000 | [diff] [blame] | 238 | int sqlite3OsLockState(sqlite3_file *id); |
danielk1977 | 6207906 | 2007-08-15 17:08:46 +0000 | [diff] [blame] | 239 | int sqlite3OsSectorSize(sqlite3_file *id); |
| 240 | int sqlite3OsDeviceCharacteristics(sqlite3_file *id); |
| 241 | |
danielk1977 | b4b4741 | 2007-08-17 15:53:36 +0000 | [diff] [blame] | 242 | /* |
| 243 | ** Functions for accessing sqlite3_vfs methods |
| 244 | */ |
| 245 | int sqlite3OsOpen(sqlite3_vfs *, const char *, sqlite3_file*, int, int *); |
danielk1977 | fee2d25 | 2007-08-18 10:59:19 +0000 | [diff] [blame^] | 246 | int sqlite3OsDelete(sqlite3_vfs *, const char *, int); |
danielk1977 | b4b4741 | 2007-08-17 15:53:36 +0000 | [diff] [blame] | 247 | int sqlite3OsAccess(sqlite3_vfs *, const char *, int); |
| 248 | int sqlite3OsGetTempName(sqlite3_vfs *, char *); |
| 249 | int sqlite3OsFullPathname(sqlite3_vfs *, const char *, char *); |
| 250 | void *sqlite3OsDlOpen(sqlite3_vfs *, const char *); |
| 251 | void sqlite3OsDlError(sqlite3_vfs *, int, char *); |
| 252 | void *sqlite3OsDlSym(sqlite3_vfs *, void *, const char *); |
| 253 | void sqlite3OsDlClose(sqlite3_vfs *, void *); |
| 254 | int sqlite3OsRandomness(sqlite3_vfs *, int, char *); |
| 255 | int sqlite3OsSleep(sqlite3_vfs *, int); |
| 256 | int sqlite3OsCurrentTime(sqlite3_vfs *, double*); |
| 257 | |
| 258 | /* |
| 259 | ** Convenience functions for opening and closing files using |
| 260 | ** sqlite3_malloc() to obtain space for the file-handle structure. |
| 261 | */ |
| 262 | int sqlite3OsOpenMalloc(sqlite3_vfs *, const char *, sqlite3_file **, int); |
| 263 | int sqlite3OsCloseFree(sqlite3_file *); |
| 264 | |
drh | 87cc3b3 | 2007-05-08 21:45:27 +0000 | [diff] [blame] | 265 | #if defined(SQLITE_TEST) || defined(SQLITE_DEBUG) |
danielk1977 | 6207906 | 2007-08-15 17:08:46 +0000 | [diff] [blame] | 266 | int sqlite3OsFileHandle(sqlite3_file *id); |
| 267 | int sqlite3OsLockState(sqlite3_file *id); |
drh | 87cc3b3 | 2007-05-08 21:45:27 +0000 | [diff] [blame] | 268 | #endif |
| 269 | |
drh | e3c4137 | 2001-09-17 20:25:58 +0000 | [diff] [blame] | 270 | #endif /* _SQLITE_OS_H_ */ |