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 | 820f381 | 2003-01-08 13:02:52 +0000 | [diff] [blame] | 21 | ** Figure out if we are dealing with Unix, Windows or MacOS. |
| 22 | ** |
| 23 | ** N.B. MacOS means Mac Classic (or Carbon). Treat Darwin (OS X) as Unix. |
| 24 | ** The MacOS build is designed to use CodeWarrior (tested with v8) |
drh | 829e802 | 2002-11-06 14:08:11 +0000 | [diff] [blame] | 25 | */ |
drh | d86959f | 2005-11-26 03:51:18 +0000 | [diff] [blame] | 26 | #if !defined(OS_UNIX) && !defined(OS_ALT) |
drh | 0ccebe7 | 2005-06-07 22:22:50 +0000 | [diff] [blame] | 27 | # define OS_OTHER 0 |
drh | 27a3220 | 2002-03-20 00:00:29 +0000 | [diff] [blame] | 28 | # ifndef OS_WIN |
drh | 0d47743 | 2005-01-16 20:47:40 +0000 | [diff] [blame] | 29 | # if defined(_WIN32) || defined(WIN32) || defined(__CYGWIN__) || defined(__MINGW32__) || defined(__BORLANDC__) |
| 30 | # define OS_WIN 1 |
| 31 | # define OS_UNIX 0 |
| 32 | # else |
| 33 | # define OS_WIN 0 |
| 34 | # define OS_UNIX 1 |
drh | 27a3220 | 2002-03-20 00:00:29 +0000 | [diff] [blame] | 35 | # endif |
| 36 | # else |
| 37 | # define OS_UNIX 0 |
| 38 | # endif |
drh | 820f381 | 2003-01-08 13:02:52 +0000 | [diff] [blame] | 39 | #else |
drh | e5e3760 | 2003-08-16 13:10:51 +0000 | [diff] [blame] | 40 | # ifndef OS_WIN |
| 41 | # define OS_WIN 0 |
| 42 | # endif |
drh | 1ab4300 | 2002-01-14 09:28:19 +0000 | [diff] [blame] | 43 | #endif |
| 44 | |
drh | 054889e | 2005-11-30 03:20:31 +0000 | [diff] [blame] | 45 | |
drh | e3c4137 | 2001-09-17 20:25:58 +0000 | [diff] [blame] | 46 | /* |
drh | 9cbe635 | 2005-11-29 03:13:21 +0000 | [diff] [blame] | 47 | ** Define the maximum size of a temporary filename |
drh | 0ccebe7 | 2005-06-07 22:22:50 +0000 | [diff] [blame] | 48 | */ |
drh | 9cbe635 | 2005-11-29 03:13:21 +0000 | [diff] [blame] | 49 | #if OS_WIN |
drh | a2eebaa | 2005-11-29 19:50:24 +0000 | [diff] [blame] | 50 | # include <windows.h> |
drh | 9cbe635 | 2005-11-29 03:13:21 +0000 | [diff] [blame] | 51 | # define SQLITE_TEMPNAME_SIZE (MAX_PATH+50) |
| 52 | #else |
| 53 | # define SQLITE_TEMPNAME_SIZE 200 |
drh | 2e66f0b | 2005-04-28 17:18:48 +0000 | [diff] [blame] | 54 | #endif |
drh | 820f381 | 2003-01-08 13:02:52 +0000 | [diff] [blame] | 55 | |
drh | b851b2c | 2005-03-10 14:11:12 +0000 | [diff] [blame] | 56 | /* If the SET_FULLSYNC macro is not defined above, then make it |
| 57 | ** a no-op |
| 58 | */ |
| 59 | #ifndef SET_FULLSYNC |
| 60 | # define SET_FULLSYNC(x,y) |
| 61 | #endif |
| 62 | |
danielk1977 | 6622cce | 2004-05-20 11:00:52 +0000 | [diff] [blame] | 63 | /* |
drh | bbd42a6 | 2004-05-22 17:41:58 +0000 | [diff] [blame] | 64 | ** Temporary files are named starting with this prefix followed by 16 random |
| 65 | ** alphanumeric characters, and no file extension. They are stored in the |
| 66 | ** OS's standard temporary file directory, and are deleted prior to exit. |
| 67 | ** If sqlite is being embedded in another program, you may wish to change the |
| 68 | ** prefix to reflect your program's name, so that if your program exits |
| 69 | ** prematurely, old temporary files can be easily identified. This can be done |
| 70 | ** using -DTEMP_FILE_PREFIX=myprefix_ on the compiler command line. |
danielk1977 | 6622cce | 2004-05-20 11:00:52 +0000 | [diff] [blame] | 71 | */ |
drh | bbd42a6 | 2004-05-22 17:41:58 +0000 | [diff] [blame] | 72 | #ifndef TEMP_FILE_PREFIX |
| 73 | # define TEMP_FILE_PREFIX "sqlite_" |
| 74 | #endif |
| 75 | |
drh | 824d7c1 | 2006-01-06 12:03:19 +0000 | [diff] [blame^] | 76 | /* |
| 77 | ** Forward declarations |
| 78 | */ |
| 79 | typedef struct OsFile OsFile; |
| 80 | typedef struct IoMethod IoMethod; |
drh | 0ccebe7 | 2005-06-07 22:22:50 +0000 | [diff] [blame] | 81 | |
drh | 824d7c1 | 2006-01-06 12:03:19 +0000 | [diff] [blame^] | 82 | /* |
| 83 | ** An instance of the following structure contains pointers to all |
| 84 | ** methods on an OsFile object. |
| 85 | */ |
| 86 | struct IoMethod { |
| 87 | int (*xClose)(OsFile**); |
| 88 | int (*xOpenDirectory)(OsFile*, const char*); |
| 89 | int (*xRead)(OsFile*, void*, int amt); |
| 90 | int (*xWrite)(OsFile*, const void*, int amt); |
| 91 | int (*xSeek)(OsFile*, i64 offset); |
| 92 | int (*xTruncate)(OsFile*, i64 size); |
| 93 | int (*xSync)(OsFile*, int); |
| 94 | void (*xSetFullSync)(OsFile *id, int setting); |
| 95 | int (*xFileHandle)(OsFile *id); |
| 96 | int (*xFileSize)(OsFile*, i64 *pSize); |
| 97 | int (*xLock)(OsFile*, int); |
| 98 | int (*xUnlock)(OsFile*, int); |
| 99 | int (*xLockState)(OsFile *id); |
| 100 | int (*xCheckReservedLock)(OsFile *id); |
| 101 | }; |
| 102 | |
| 103 | /* |
| 104 | ** The OsFile object describes an open disk file in an OS-dependent way. |
| 105 | ** The version of OsFile defined here is a generic version. Each OS |
| 106 | ** implementation defines its own subclass of this structure that contains |
| 107 | ** additional information needed to handle file I/O. But the pMethod |
| 108 | ** entry (pointing to the virtual function table) always occurs first |
| 109 | ** so that we can always find the appropriate methods. |
| 110 | */ |
| 111 | struct OsFile { |
| 112 | IoMethod const *pMethod; |
| 113 | }; |
| 114 | |
| 115 | /* |
| 116 | ** The following values may be passed as the second argument to |
| 117 | ** sqlite3OsLock(). The various locks exhibit the following semantics: |
| 118 | ** |
| 119 | ** SHARED: Any number of processes may hold a SHARED lock simultaneously. |
| 120 | ** RESERVED: A single process may hold a RESERVED lock on a file at |
| 121 | ** any time. Other processes may hold and obtain new SHARED locks. |
| 122 | ** PENDING: A single process may hold a PENDING lock on a file at |
| 123 | ** any one time. Existing SHARED locks may persist, but no new |
| 124 | ** SHARED locks may be obtained by other processes. |
| 125 | ** EXCLUSIVE: An EXCLUSIVE lock precludes all other locks. |
| 126 | ** |
| 127 | ** PENDING_LOCK may not be passed directly to sqlite3OsLock(). Instead, a |
| 128 | ** process that requests an EXCLUSIVE lock may actually obtain a PENDING |
| 129 | ** lock. This can be upgraded to an EXCLUSIVE lock by a subsequent call to |
| 130 | ** sqlite3OsLock(). |
| 131 | */ |
| 132 | #define NO_LOCK 0 |
| 133 | #define SHARED_LOCK 1 |
| 134 | #define RESERVED_LOCK 2 |
| 135 | #define PENDING_LOCK 3 |
| 136 | #define EXCLUSIVE_LOCK 4 |
| 137 | |
| 138 | /* |
| 139 | ** File Locking Notes: (Mostly about windows but also some info for Unix) |
| 140 | ** |
| 141 | ** We cannot use LockFileEx() or UnlockFileEx() on Win95/98/ME because |
| 142 | ** those functions are not available. So we use only LockFile() and |
| 143 | ** UnlockFile(). |
| 144 | ** |
| 145 | ** LockFile() prevents not just writing but also reading by other processes. |
| 146 | ** A SHARED_LOCK is obtained by locking a single randomly-chosen |
| 147 | ** byte out of a specific range of bytes. The lock byte is obtained at |
| 148 | ** random so two separate readers can probably access the file at the |
| 149 | ** same time, unless they are unlucky and choose the same lock byte. |
| 150 | ** An EXCLUSIVE_LOCK is obtained by locking all bytes in the range. |
| 151 | ** There can only be one writer. A RESERVED_LOCK is obtained by locking |
| 152 | ** a single byte of the file that is designated as the reserved lock byte. |
| 153 | ** A PENDING_LOCK is obtained by locking a designated byte different from |
| 154 | ** the RESERVED_LOCK byte. |
| 155 | ** |
| 156 | ** On WinNT/2K/XP systems, LockFileEx() and UnlockFileEx() are available, |
| 157 | ** which means we can use reader/writer locks. When reader/writer locks |
| 158 | ** are used, the lock is placed on the same range of bytes that is used |
| 159 | ** for probabilistic locking in Win95/98/ME. Hence, the locking scheme |
| 160 | ** will support two or more Win95 readers or two or more WinNT readers. |
| 161 | ** But a single Win95 reader will lock out all WinNT readers and a single |
| 162 | ** WinNT reader will lock out all other Win95 readers. |
| 163 | ** |
| 164 | ** The following #defines specify the range of bytes used for locking. |
| 165 | ** SHARED_SIZE is the number of bytes available in the pool from which |
| 166 | ** a random byte is selected for a shared lock. The pool of bytes for |
| 167 | ** shared locks begins at SHARED_FIRST. |
| 168 | ** |
| 169 | ** These #defines are available in sqlite_aux.h so that adaptors for |
| 170 | ** connecting SQLite to other operating systems can use the same byte |
| 171 | ** ranges for locking. In particular, the same locking strategy and |
| 172 | ** byte ranges are used for Unix. This leaves open the possiblity of having |
| 173 | ** clients on win95, winNT, and unix all talking to the same shared file |
| 174 | ** and all locking correctly. To do so would require that samba (or whatever |
| 175 | ** tool is being used for file sharing) implements locks correctly between |
| 176 | ** windows and unix. I'm guessing that isn't likely to happen, but by |
| 177 | ** using the same locking range we are at least open to the possibility. |
| 178 | ** |
| 179 | ** Locking in windows is manditory. For this reason, we cannot store |
| 180 | ** actual data in the bytes used for locking. The pager never allocates |
| 181 | ** the pages involved in locking therefore. SHARED_SIZE is selected so |
| 182 | ** that all locks will fit on a single page even at the minimum page size. |
| 183 | ** PENDING_BYTE defines the beginning of the locks. By default PENDING_BYTE |
| 184 | ** is set high so that we don't have to allocate an unused page except |
| 185 | ** for very large databases. But one should test the page skipping logic |
| 186 | ** by setting PENDING_BYTE low and running the entire regression suite. |
| 187 | ** |
| 188 | ** Changing the value of PENDING_BYTE results in a subtly incompatible |
| 189 | ** file format. Depending on how it is changed, you might not notice |
| 190 | ** the incompatibility right away, even running a full regression test. |
| 191 | ** The default location of PENDING_BYTE is the first byte past the |
| 192 | ** 1GB boundary. |
| 193 | ** |
| 194 | */ |
| 195 | #ifndef SQLITE_TEST |
| 196 | #define PENDING_BYTE 0x40000000 /* First byte past the 1GB boundary */ |
| 197 | #else |
| 198 | extern unsigned int sqlite3_pending_byte; |
| 199 | #define PENDING_BYTE sqlite3_pending_byte |
| 200 | #endif |
| 201 | |
| 202 | #define RESERVED_BYTE (PENDING_BYTE+1) |
| 203 | #define SHARED_FIRST (PENDING_BYTE+2) |
| 204 | #define SHARED_SIZE 510 |
| 205 | |
| 206 | /* |
| 207 | ** A single global instance of the following structure holds pointers to |
| 208 | ** the routines that SQLite uses to talk with the underlying operating |
| 209 | ** system. Modify this structure (before using any SQLite API!) to |
| 210 | ** accomodate perculiar operating system interfaces or behaviors. |
| 211 | */ |
| 212 | extern struct sqlite3OsVtbl { |
| 213 | int (*xOpenReadWrite)(const char*, OsFile**, int*); |
| 214 | int (*xOpenExclusive)(const char*, OsFile**, int); |
| 215 | int (*xOpenReadOnly)(const char*, OsFile**); |
| 216 | |
| 217 | int (*xDelete)(const char*); |
| 218 | int (*xFileExists)(const char*); |
| 219 | char *(*xFullPathname)(const char*); |
| 220 | int (*xIsDirWritable)(char*); |
| 221 | int (*xSyncDirectory)(const char*); |
| 222 | int (*xTempFileName)(char*); |
| 223 | |
| 224 | int (*xRandomSeed)(char*); |
| 225 | int (*xSleep)(int ms); |
| 226 | int (*xCurrentTime)(double*); |
| 227 | |
| 228 | void (*xEnterMutex)(void); |
| 229 | void (*xLeaveMutex)(void); |
| 230 | int (*xInMutex)(void); |
| 231 | void *(*xThreadSpecificData)(int); |
| 232 | |
| 233 | void *(*xMalloc)(int); |
| 234 | void *(*xRealloc)(void *, int); |
| 235 | void (*xFree)(void *); |
| 236 | int (*xAllocationSize)(void *); |
| 237 | } sqlite3Os; |
| 238 | |
| 239 | /* |
| 240 | ** The following API routine returns a pointer to the sqlite3Os global |
| 241 | ** variable. It is probably easier just to reference the global variable |
| 242 | ** directly. This routine is provided for backwards compatibility with |
| 243 | ** an older interface design. |
| 244 | */ |
| 245 | struct sqlite3OsVtbl *sqlite3_os_switch(void); |
| 246 | |
| 247 | |
| 248 | /* |
| 249 | ** The following are prototypes of convenience routines that simply |
| 250 | ** call the corresponding routines in the OsFile.pMethod virtual |
| 251 | ** function table. |
| 252 | */ |
| 253 | int sqlite3OsClose(OsFile**); |
| 254 | int sqlite3OsOpenDirectory(OsFile*, const char*); |
| 255 | int sqlite3OsRead(OsFile*, void*, int amt); |
| 256 | int sqlite3OsWrite(OsFile*, const void*, int amt); |
| 257 | int sqlite3OsSeek(OsFile*, i64 offset); |
| 258 | int sqlite3OsTruncate(OsFile*, i64 size); |
| 259 | int sqlite3OsSync(OsFile*, int); |
| 260 | void sqlite3OsSetFullSync(OsFile *id, int setting); |
| 261 | int sqlite3OsFileHandle(OsFile *id); |
| 262 | int sqlite3OsFileSize(OsFile*, i64 *pSize); |
| 263 | int sqlite3OsLock(OsFile*, int); |
| 264 | int sqlite3OsUnlock(OsFile*, int); |
| 265 | int sqlite3OsLockState(OsFile *id); |
| 266 | int sqlite3OsCheckReservedLock(OsFile *id); |
drh | 9c06c95 | 2005-11-26 00:25:00 +0000 | [diff] [blame] | 267 | |
drh | e3c4137 | 2001-09-17 20:25:58 +0000 | [diff] [blame] | 268 | #endif /* _SQLITE_OS_H_ */ |