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