blob: fadf1c821abc36816a6f8bf82c0001717f293fee [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/*
drh66560ad2006-01-06 14:32:19 +000021** Figure out if we are dealing with Unix, Windows, or some other
22** operating system.
drh829e8022002-11-06 14:08:11 +000023*/
drhee2ce182007-04-02 16:45:12 +000024#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
drh66560ad2006-01-06 14:32:19 +000036#if !defined(OS_UNIX) && !defined(OS_OTHER)
drh0ccebe72005-06-07 22:22:50 +000037# define OS_OTHER 0
drh27a32202002-03-20 00:00:29 +000038# ifndef OS_WIN
drh0d477432005-01-16 20:47:40 +000039# if defined(_WIN32) || defined(WIN32) || defined(__CYGWIN__) || defined(__MINGW32__) || defined(__BORLANDC__)
40# define OS_WIN 1
41# define OS_UNIX 0
drh60a1e4b2006-06-03 18:02:15 +000042# define OS_OS2 0
pweilbacher3f61bc72007-06-30 15:24:37 +000043# elif defined(__EMX__) || defined(_OS2) || defined(OS2) || defined(_OS2_) || defined(__OS2__)
drh60a1e4b2006-06-03 18:02:15 +000044# define OS_WIN 0
45# define OS_UNIX 0
46# define OS_OS2 1
drh0d477432005-01-16 20:47:40 +000047# else
48# define OS_WIN 0
49# define OS_UNIX 1
drh60a1e4b2006-06-03 18:02:15 +000050# define OS_OS2 0
drh27a32202002-03-20 00:00:29 +000051# endif
52# else
53# define OS_UNIX 0
drh60a1e4b2006-06-03 18:02:15 +000054# define OS_OS2 0
drh27a32202002-03-20 00:00:29 +000055# endif
drh820f3812003-01-08 13:02:52 +000056#else
drhe5e37602003-08-16 13:10:51 +000057# ifndef OS_WIN
58# define OS_WIN 0
59# endif
drh1ab43002002-01-14 09:28:19 +000060#endif
61
drh054889e2005-11-30 03:20:31 +000062
drhe3c41372001-09-17 20:25:58 +000063/*
drh9cbe6352005-11-29 03:13:21 +000064** Define the maximum size of a temporary filename
drh0ccebe72005-06-07 22:22:50 +000065*/
drh9cbe6352005-11-29 03:13:21 +000066#if OS_WIN
drha2eebaa2005-11-29 19:50:24 +000067# include <windows.h>
drh9cbe6352005-11-29 03:13:21 +000068# define SQLITE_TEMPNAME_SIZE (MAX_PATH+50)
drh60a1e4b2006-06-03 18:02:15 +000069#elif OS_OS2
pweilbacher53b4bd32007-06-22 20:17:37 +000070# 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
drh60a1e4b2006-06-03 18:02:15 +000073# define INCL_DOSDATETIME
74# define INCL_DOSFILEMGR
75# define INCL_DOSERRORS
76# define INCL_DOSMISC
77# define INCL_DOSPROCESS
pweilbacher691902e2007-06-22 20:04:36 +000078# define INCL_DOSMODULEMGR
drh60a1e4b2006-06-03 18:02:15 +000079# include <os2.h>
80# define SQLITE_TEMPNAME_SIZE (CCHMAXPATHCOMP)
drh9cbe6352005-11-29 03:13:21 +000081#else
82# define SQLITE_TEMPNAME_SIZE 200
drh2e66f0b2005-04-28 17:18:48 +000083#endif
drh820f3812003-01-08 13:02:52 +000084
drhb851b2c2005-03-10 14:11:12 +000085/* 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
danielk19776622cce2004-05-20 11:00:52 +000092/*
drh3ceeb752007-03-29 18:19:52 +000093** 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/*
drhbbd42a62004-05-22 17:41:58 +0000100** 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.
drhfd288f32006-10-31 21:27:33 +0000107**
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.
danielk19776622cce2004-05-20 11:00:52 +0000119*/
drhbbd42a62004-05-22 17:41:58 +0000120#ifndef TEMP_FILE_PREFIX
drhfd288f32006-10-31 21:27:33 +0000121# define TEMP_FILE_PREFIX "etilqs_"
drhbbd42a62004-05-22 17:41:58 +0000122#endif
123
drh66560ad2006-01-06 14:32:19 +0000124/*
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
drh824d7c12006-01-06 12:03:19 +0000134/*
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
217extern 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
danielk1977b4b47412007-08-17 15:53:36 +0000225/*
226** Functions for accessing sqlite3_file methods
drh824d7c12006-01-06 12:03:19 +0000227*/
danielk1977b4b47412007-08-17 15:53:36 +0000228int sqlite3OsClose(sqlite3_file*);
danielk197762079062007-08-15 17:08:46 +0000229int sqlite3OsRead(sqlite3_file*, void*, int amt, i64 offset);
230int sqlite3OsWrite(sqlite3_file*, const void*, int amt, i64 offset);
231int sqlite3OsTruncate(sqlite3_file*, i64 size);
232int sqlite3OsSync(sqlite3_file*, int);
233int sqlite3OsFileSize(sqlite3_file*, i64 *pSize);
234int sqlite3OsLock(sqlite3_file*, int);
235int sqlite3OsUnlock(sqlite3_file*, int);
236int sqlite3OsBreakLock(sqlite3_file*);
237int sqlite3OsCheckReservedLock(sqlite3_file *id);
danielk197790949c22007-08-17 16:50:38 +0000238int sqlite3OsLockState(sqlite3_file *id);
danielk197762079062007-08-15 17:08:46 +0000239int sqlite3OsSectorSize(sqlite3_file *id);
240int sqlite3OsDeviceCharacteristics(sqlite3_file *id);
241
danielk1977b4b47412007-08-17 15:53:36 +0000242/*
243** Functions for accessing sqlite3_vfs methods
244*/
245int sqlite3OsOpen(sqlite3_vfs *, const char *, sqlite3_file*, int, int *);
danielk1977fee2d252007-08-18 10:59:19 +0000246int sqlite3OsDelete(sqlite3_vfs *, const char *, int);
danielk1977b4b47412007-08-17 15:53:36 +0000247int sqlite3OsAccess(sqlite3_vfs *, const char *, int);
248int sqlite3OsGetTempName(sqlite3_vfs *, char *);
249int sqlite3OsFullPathname(sqlite3_vfs *, const char *, char *);
250void *sqlite3OsDlOpen(sqlite3_vfs *, const char *);
251void sqlite3OsDlError(sqlite3_vfs *, int, char *);
252void *sqlite3OsDlSym(sqlite3_vfs *, void *, const char *);
253void sqlite3OsDlClose(sqlite3_vfs *, void *);
254int sqlite3OsRandomness(sqlite3_vfs *, int, char *);
255int sqlite3OsSleep(sqlite3_vfs *, int);
256int 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*/
262int sqlite3OsOpenMalloc(sqlite3_vfs *, const char *, sqlite3_file **, int);
263int sqlite3OsCloseFree(sqlite3_file *);
264
drh87cc3b32007-05-08 21:45:27 +0000265#if defined(SQLITE_TEST) || defined(SQLITE_DEBUG)
danielk197762079062007-08-15 17:08:46 +0000266 int sqlite3OsFileHandle(sqlite3_file *id);
267 int sqlite3OsLockState(sqlite3_file *id);
drh87cc3b32007-05-08 21:45:27 +0000268#endif
269
drhe3c41372001-09-17 20:25:58 +0000270#endif /* _SQLITE_OS_H_ */