blob: 5bbc724177c9cb43a71e9d798403c7ca9ac6df54 [file] [log] [blame]
drhe3c41372001-09-17 20:25:58 +00001/*
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
drh829e8022002-11-06 14:08:11 +000020/*
drh820f3812003-01-08 13:02:52 +000021** 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)
drh829e8022002-11-06 14:08:11 +000025*/
drhd86959f2005-11-26 03:51:18 +000026#if !defined(OS_UNIX) && !defined(OS_ALT)
drh0ccebe72005-06-07 22:22:50 +000027# define OS_OTHER 0
drh27a32202002-03-20 00:00:29 +000028# ifndef OS_WIN
drh0d477432005-01-16 20:47:40 +000029# 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
drh27a32202002-03-20 00:00:29 +000035# endif
36# else
37# define OS_UNIX 0
38# endif
drh820f3812003-01-08 13:02:52 +000039#else
drhe5e37602003-08-16 13:10:51 +000040# ifndef OS_WIN
41# define OS_WIN 0
42# endif
drh1ab43002002-01-14 09:28:19 +000043#endif
44
drh054889e2005-11-30 03:20:31 +000045
drhe3c41372001-09-17 20:25:58 +000046/*
drh054889e2005-11-30 03:20:31 +000047** Forward declarations
drhe3c41372001-09-17 20:25:58 +000048*/
drh9cbe6352005-11-29 03:13:21 +000049typedef struct OsFile OsFile;
drh054889e2005-11-30 03:20:31 +000050typedef struct IoMethod IoMethod;
51
52/*
53** An instance of the following structure contains pointers to all
54** methods on an OsFile object.
55*/
56struct 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*/
79struct OsFile {
80 IoMethod const *pMethod;
81};
drh0ccebe72005-06-07 22:22:50 +000082
drh9cbe6352005-11-29 03:13:21 +000083/*
84** Define the maximum size of a temporary filename
drh0ccebe72005-06-07 22:22:50 +000085*/
drh9cbe6352005-11-29 03:13:21 +000086#if OS_WIN
drha2eebaa2005-11-29 19:50:24 +000087# include <windows.h>
drh9cbe6352005-11-29 03:13:21 +000088# define SQLITE_TEMPNAME_SIZE (MAX_PATH+50)
89#else
90# define SQLITE_TEMPNAME_SIZE 200
drh2e66f0b2005-04-28 17:18:48 +000091#endif
drh820f3812003-01-08 13:02:52 +000092
drhb851b2c2005-03-10 14:11:12 +000093/* 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
danielk19776622cce2004-05-20 11:00:52 +0000100/*
drhbbd42a62004-05-22 17:41:58 +0000101** 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.
danielk19776622cce2004-05-20 11:00:52 +0000108*/
drhbbd42a62004-05-22 17:41:58 +0000109#ifndef TEMP_FILE_PREFIX
110# define TEMP_FILE_PREFIX "sqlite_"
111#endif
112
danielk19779a1d0ab2004-06-01 14:09:28 +0000113/*
114** The following values may be passed as the second argument to
danielk197790ba3bd2004-06-25 08:32:25 +0000115** 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
danielk19779a1d0ab2004-06-01 14:09:28 +0000128** sqlite3OsLock().
129*/
danielk19779eed5052004-06-04 10:38:30 +0000130#define NO_LOCK 0
danielk19779a1d0ab2004-06-01 14:09:28 +0000131#define SHARED_LOCK 1
132#define RESERVED_LOCK 2
133#define PENDING_LOCK 3
134#define EXCLUSIVE_LOCK 4
danielk19776622cce2004-05-20 11:00:52 +0000135
drh2ac3ee92004-06-07 16:27:46 +0000136/*
drh1f595712004-06-15 01:40:29 +0000137** File Locking Notes: (Mostly about windows but also some info for Unix)
drh2ac3ee92004-06-07 16:27:46 +0000138**
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.
drh2ac3ee92004-06-07 16:27:46 +0000144** 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
drh1f595712004-06-15 01:40:29 +0000170** 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.
drh2ac3ee92004-06-07 16:27:46 +0000174**
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
drh1f595712004-06-15 01:40:29 +0000177** 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**
drh2ac3ee92004-06-07 16:27:46 +0000190*/
danielk1977fd5f5b62005-09-16 09:52:29 +0000191#ifndef SQLITE_TEST
drh1f595712004-06-15 01:40:29 +0000192#define PENDING_BYTE 0x40000000 /* First byte past the 1GB boundary */
danielk1977fd5f5b62005-09-16 09:52:29 +0000193#else
danielk1977fd5f5b62005-09-16 09:52:29 +0000194extern unsigned int sqlite3_pending_byte;
195#define PENDING_BYTE sqlite3_pending_byte
196#endif
197
drh1f595712004-06-15 01:40:29 +0000198#define RESERVED_BYTE (PENDING_BYTE+1)
199#define SHARED_FIRST (PENDING_BYTE+2)
200#define SHARED_SIZE 510
drh2ac3ee92004-06-07 16:27:46 +0000201
drh9c06c952005-11-26 00:25:00 +0000202/*
203** A single global instance of the following structure holds pointers to the
drh054889e2005-11-30 03:20:31 +0000204** various system-specific interface routines.
drh9c06c952005-11-26 00:25:00 +0000205*/
drh054889e2005-11-30 03:20:31 +0000206extern struct sqlite3OsVtbl {
drh9cbe6352005-11-29 03:13:21 +0000207 int (*xOpenReadWrite)(const char*, OsFile**, int*);
208 int (*xOpenExclusive)(const char*, OsFile**, int);
209 int (*xOpenReadOnly)(const char*, OsFile**);
drh054889e2005-11-30 03:20:31 +0000210
211 int (*xDelete)(const char*);
212 int (*xFileExists)(const char*);
213 char *(*xFullPathname)(const char*);
214 int (*xIsDirWritable)(char*);
drh9c06c952005-11-26 00:25:00 +0000215 int (*xSyncDirectory)(const char*);
216 int (*xTempFileName)(char*);
drh0ccebe72005-06-07 22:22:50 +0000217
drh054889e2005-11-30 03:20:31 +0000218 int (*xRandomSeed)(char*);
219 int (*xSleep)(int ms);
220 int (*xCurrentTime)(double*);
221 void (*xEnterMutex)(void);
222 void (*xLeaveMutex)(void);
223} sqlite3Os;
drh0ccebe72005-06-07 22:22:50 +0000224
drh054889e2005-11-30 03:20:31 +0000225/*
226** Prototypes for routines found in os.c
227*/
228int sqlite3OsClose(OsFile**);
229int sqlite3OsOpenDirectory(OsFile*, const char*);
230int sqlite3OsRead(OsFile*, void*, int amt);
231int sqlite3OsWrite(OsFile*, const void*, int amt);
232int sqlite3OsSeek(OsFile*, i64 offset);
233int sqlite3OsTruncate(OsFile*, i64 size);
234int sqlite3OsSync(OsFile*, int);
235void sqlite3OsSetFullSync(OsFile *id, int setting);
236int sqlite3OsFileHandle(OsFile *id);
237int sqlite3OsFileSize(OsFile*, i64 *pSize);
238int sqlite3OsLock(OsFile*, int);
239int sqlite3OsUnlock(OsFile*, int);
240int sqlite3OsLockState(OsFile *id);
241int sqlite3OsCheckReservedLock(OsFile *id);
drh0ccebe72005-06-07 22:22:50 +0000242
drh9c06c952005-11-26 00:25:00 +0000243
drhe3c41372001-09-17 20:25:58 +0000244#endif /* _SQLITE_OS_H_ */