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