blob: 2efffff9b6fa031010ce22d220a7d9cbaa11bc46 [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.
drh29278e32007-08-21 10:44:15 +000016**
17** This header file is #include-ed by sqliteInt.h and thus ends up
18** being included by every source file.
drhe3c41372001-09-17 20:25:58 +000019*/
20#ifndef _SQLITE_OS_H_
21#define _SQLITE_OS_H_
22
drh829e8022002-11-06 14:08:11 +000023/*
drh66560ad2006-01-06 14:32:19 +000024** Figure out if we are dealing with Unix, Windows, or some other
drh29278e32007-08-21 10:44:15 +000025** operating system. After the following block of preprocess macros,
danielk197729bafea2008-06-26 10:41:19 +000026** all of SQLITE_OS_UNIX, SQLITE_OS_WIN, SQLITE_OS_OS2, and SQLITE_OS_OTHER
27** will defined to either 1 or 0. One of the four will be 1. The other
28** three will be 0.
drh829e8022002-11-06 14:08:11 +000029*/
danielk197729bafea2008-06-26 10:41:19 +000030#if defined(SQLITE_OS_OTHER)
31# if SQLITE_OS_OTHER==1
32# undef SQLITE_OS_UNIX
33# define SQLITE_OS_UNIX 0
34# undef SQLITE_OS_WIN
35# define SQLITE_OS_WIN 0
36# undef SQLITE_OS_OS2
37# define SQLITE_OS_OS2 0
drhee2ce182007-04-02 16:45:12 +000038# else
danielk197729bafea2008-06-26 10:41:19 +000039# undef SQLITE_OS_OTHER
drhee2ce182007-04-02 16:45:12 +000040# endif
41#endif
danielk197729bafea2008-06-26 10:41:19 +000042#if !defined(SQLITE_OS_UNIX) && !defined(SQLITE_OS_OTHER)
43# define SQLITE_OS_OTHER 0
44# ifndef SQLITE_OS_WIN
drh0d477432005-01-16 20:47:40 +000045# if defined(_WIN32) || defined(WIN32) || defined(__CYGWIN__) || defined(__MINGW32__) || defined(__BORLANDC__)
danielk197729bafea2008-06-26 10:41:19 +000046# define SQLITE_OS_WIN 1
47# define SQLITE_OS_UNIX 0
48# define SQLITE_OS_OS2 0
pweilbacher3f61bc72007-06-30 15:24:37 +000049# elif defined(__EMX__) || defined(_OS2) || defined(OS2) || defined(_OS2_) || defined(__OS2__)
danielk197729bafea2008-06-26 10:41:19 +000050# define SQLITE_OS_WIN 0
51# define SQLITE_OS_UNIX 0
52# define SQLITE_OS_OS2 1
drh0d477432005-01-16 20:47:40 +000053# else
danielk197729bafea2008-06-26 10:41:19 +000054# define SQLITE_OS_WIN 0
55# define SQLITE_OS_UNIX 1
56# define SQLITE_OS_OS2 0
drh27a32202002-03-20 00:00:29 +000057# endif
58# else
danielk197729bafea2008-06-26 10:41:19 +000059# define SQLITE_OS_UNIX 0
60# define SQLITE_OS_OS2 0
drh27a32202002-03-20 00:00:29 +000061# endif
drh820f3812003-01-08 13:02:52 +000062#else
danielk197729bafea2008-06-26 10:41:19 +000063# ifndef SQLITE_OS_WIN
64# define SQLITE_OS_WIN 0
drhe5e37602003-08-16 13:10:51 +000065# endif
drh1ab43002002-01-14 09:28:19 +000066#endif
67
drh11de9332012-03-01 19:14:13 +000068#if SQLITE_OS_WIN
69# include <windows.h>
70#endif
71
72#if SQLITE_OS_OS2
73# if (__GNUC__ > 3 || __GNUC__ == 3 && __GNUC_MINOR__ >= 3) && defined(OS2_HIGH_MEMORY)
74# include <os2safe.h> /* has to be included before os2.h for linking to work */
75# endif
76# define INCL_DOSDATETIME
77# define INCL_DOSFILEMGR
78# define INCL_DOSERRORS
79# define INCL_DOSMISC
80# define INCL_DOSPROCESS
81# define INCL_DOSMODULEMGR
82# define INCL_DOSSEMAPHORES
83# include <os2.h>
84# include <uconv.h>
85#endif
86
shane712d6f92008-05-29 03:54:26 +000087/*
mistachkin254582f2011-12-09 05:52:16 +000088** Determine if we are dealing with Windows NT.
drhd794e8b2012-01-30 16:02:43 +000089**
90** We ought to be able to determine if we are compiling for win98 or winNT
91** using the _WIN32_WINNT macro as follows:
92**
93** #if defined(_WIN32_WINNT)
94** # define SQLITE_OS_WINNT 1
95** #else
96** # define SQLITE_OS_WINNT 0
97** #endif
98**
99** However, vs2005 does not set _WIN32_WINNT by default, as it ought to,
100** so the above test does not work. We'll just assume that everything is
101** winNT unless the programmer explicitly says otherwise by setting
102** SQLITE_OS_WINNT to 0.
mistachkin254582f2011-12-09 05:52:16 +0000103*/
drhd794e8b2012-01-30 16:02:43 +0000104#if SQLITE_OS_WIN && !defined(SQLITE_OS_WINNT)
mistachkin254582f2011-12-09 05:52:16 +0000105# define SQLITE_OS_WINNT 1
mistachkin254582f2011-12-09 05:52:16 +0000106#endif
107
108/*
109** Determine if we are dealing with WindowsCE - which has a much
110** reduced API.
111*/
112#if defined(_WIN32_WCE)
113# define SQLITE_OS_WINCE 1
114#else
115# define SQLITE_OS_WINCE 0
116#endif
117
drhcf3d7a42012-03-01 20:05:41 +0000118/*
119** Determine if we are dealing with WindowsRT (Metro) as this has a different and
120** incompatible API from win32.
121*/
122#if !defined(SQLITE_OS_WINRT)
123# define SQLITE_OS_WINRT 0
124#endif
125
mistachkinc5484652012-03-05 22:52:33 +0000126/*
127** When compiled for WinCE or WinRT, there is no concept of the current
128** directory.
129 */
130#if !SQLITE_OS_WINCE && !SQLITE_OS_WINRT
131# define SQLITE_CURDIR 1
132#endif
133
drhb851b2c2005-03-10 14:11:12 +0000134/* If the SET_FULLSYNC macro is not defined above, then make it
135** a no-op
136*/
137#ifndef SET_FULLSYNC
138# define SET_FULLSYNC(x,y)
139#endif
140
danielk19776622cce2004-05-20 11:00:52 +0000141/*
drh3ceeb752007-03-29 18:19:52 +0000142** The default size of a disk sector
143*/
144#ifndef SQLITE_DEFAULT_SECTOR_SIZE
drh8942d412012-01-02 18:20:14 +0000145# define SQLITE_DEFAULT_SECTOR_SIZE 4096
drh3ceeb752007-03-29 18:19:52 +0000146#endif
147
148/*
drhbbd42a62004-05-22 17:41:58 +0000149** Temporary files are named starting with this prefix followed by 16 random
150** alphanumeric characters, and no file extension. They are stored in the
151** OS's standard temporary file directory, and are deleted prior to exit.
152** If sqlite is being embedded in another program, you may wish to change the
153** prefix to reflect your program's name, so that if your program exits
154** prematurely, old temporary files can be easily identified. This can be done
drh153c62c2007-08-24 03:51:33 +0000155** using -DSQLITE_TEMP_FILE_PREFIX=myprefix_ on the compiler command line.
drhfd288f32006-10-31 21:27:33 +0000156**
157** 2006-10-31: The default prefix used to be "sqlite_". But then
158** Mcafee started using SQLite in their anti-virus product and it
159** started putting files with the "sqlite" name in the c:/temp folder.
160** This annoyed many windows users. Those users would then do a
161** Google search for "sqlite", find the telephone numbers of the
162** developers and call to wake them up at night and complain.
163** For this reason, the default name prefix is changed to be "sqlite"
164** spelled backwards. So the temp files are still identified, but
165** anybody smart enough to figure out the code is also likely smart
166** enough to know that calling the developer will not help get rid
167** of the file.
danielk19776622cce2004-05-20 11:00:52 +0000168*/
drh153c62c2007-08-24 03:51:33 +0000169#ifndef SQLITE_TEMP_FILE_PREFIX
170# define SQLITE_TEMP_FILE_PREFIX "etilqs_"
drhbbd42a62004-05-22 17:41:58 +0000171#endif
172
drh66560ad2006-01-06 14:32:19 +0000173/*
drh824d7c12006-01-06 12:03:19 +0000174** The following values may be passed as the second argument to
175** sqlite3OsLock(). The various locks exhibit the following semantics:
176**
177** SHARED: Any number of processes may hold a SHARED lock simultaneously.
178** RESERVED: A single process may hold a RESERVED lock on a file at
179** any time. Other processes may hold and obtain new SHARED locks.
180** PENDING: A single process may hold a PENDING lock on a file at
181** any one time. Existing SHARED locks may persist, but no new
182** SHARED locks may be obtained by other processes.
183** EXCLUSIVE: An EXCLUSIVE lock precludes all other locks.
184**
185** PENDING_LOCK may not be passed directly to sqlite3OsLock(). Instead, a
186** process that requests an EXCLUSIVE lock may actually obtain a PENDING
187** lock. This can be upgraded to an EXCLUSIVE lock by a subsequent call to
188** sqlite3OsLock().
189*/
190#define NO_LOCK 0
191#define SHARED_LOCK 1
192#define RESERVED_LOCK 2
193#define PENDING_LOCK 3
194#define EXCLUSIVE_LOCK 4
195
196/*
197** File Locking Notes: (Mostly about windows but also some info for Unix)
198**
199** We cannot use LockFileEx() or UnlockFileEx() on Win95/98/ME because
200** those functions are not available. So we use only LockFile() and
201** UnlockFile().
202**
203** LockFile() prevents not just writing but also reading by other processes.
204** A SHARED_LOCK is obtained by locking a single randomly-chosen
205** byte out of a specific range of bytes. The lock byte is obtained at
206** random so two separate readers can probably access the file at the
207** same time, unless they are unlucky and choose the same lock byte.
208** An EXCLUSIVE_LOCK is obtained by locking all bytes in the range.
209** There can only be one writer. A RESERVED_LOCK is obtained by locking
210** a single byte of the file that is designated as the reserved lock byte.
211** A PENDING_LOCK is obtained by locking a designated byte different from
212** the RESERVED_LOCK byte.
213**
214** On WinNT/2K/XP systems, LockFileEx() and UnlockFileEx() are available,
215** which means we can use reader/writer locks. When reader/writer locks
216** are used, the lock is placed on the same range of bytes that is used
217** for probabilistic locking in Win95/98/ME. Hence, the locking scheme
218** will support two or more Win95 readers or two or more WinNT readers.
219** But a single Win95 reader will lock out all WinNT readers and a single
220** WinNT reader will lock out all other Win95 readers.
221**
222** The following #defines specify the range of bytes used for locking.
223** SHARED_SIZE is the number of bytes available in the pool from which
224** a random byte is selected for a shared lock. The pool of bytes for
225** shared locks begins at SHARED_FIRST.
226**
drhc7a3bb92009-02-05 16:31:45 +0000227** The same locking strategy and
drh824d7c12006-01-06 12:03:19 +0000228** byte ranges are used for Unix. This leaves open the possiblity of having
229** clients on win95, winNT, and unix all talking to the same shared file
230** and all locking correctly. To do so would require that samba (or whatever
231** tool is being used for file sharing) implements locks correctly between
232** windows and unix. I'm guessing that isn't likely to happen, but by
233** using the same locking range we are at least open to the possibility.
234**
235** Locking in windows is manditory. For this reason, we cannot store
236** actual data in the bytes used for locking. The pager never allocates
237** the pages involved in locking therefore. SHARED_SIZE is selected so
238** that all locks will fit on a single page even at the minimum page size.
239** PENDING_BYTE defines the beginning of the locks. By default PENDING_BYTE
240** is set high so that we don't have to allocate an unused page except
241** for very large databases. But one should test the page skipping logic
242** by setting PENDING_BYTE low and running the entire regression suite.
243**
244** Changing the value of PENDING_BYTE results in a subtly incompatible
245** file format. Depending on how it is changed, you might not notice
246** the incompatibility right away, even running a full regression test.
247** The default location of PENDING_BYTE is the first byte past the
248** 1GB boundary.
249**
250*/
drhf83dc1e2010-06-03 12:09:52 +0000251#ifdef SQLITE_OMIT_WSD
252# define PENDING_BYTE (0x40000000)
253#else
254# define PENDING_BYTE sqlite3PendingByte
255#endif
drh824d7c12006-01-06 12:03:19 +0000256#define RESERVED_BYTE (PENDING_BYTE+1)
257#define SHARED_FIRST (PENDING_BYTE+2)
258#define SHARED_SIZE 510
259
dan3d6e0602009-08-17 15:52:25 +0000260/*
261** Wrapper around OS specific sqlite3_os_init() function.
262*/
263int sqlite3OsInit(void);
264
danielk1977b4b47412007-08-17 15:53:36 +0000265/*
266** Functions for accessing sqlite3_file methods
drh824d7c12006-01-06 12:03:19 +0000267*/
danielk1977b4b47412007-08-17 15:53:36 +0000268int sqlite3OsClose(sqlite3_file*);
danielk197762079062007-08-15 17:08:46 +0000269int sqlite3OsRead(sqlite3_file*, void*, int amt, i64 offset);
270int sqlite3OsWrite(sqlite3_file*, const void*, int amt, i64 offset);
271int sqlite3OsTruncate(sqlite3_file*, i64 size);
272int sqlite3OsSync(sqlite3_file*, int);
273int sqlite3OsFileSize(sqlite3_file*, i64 *pSize);
274int sqlite3OsLock(sqlite3_file*, int);
275int sqlite3OsUnlock(sqlite3_file*, int);
danielk1977861f7452008-06-05 11:39:11 +0000276int sqlite3OsCheckReservedLock(sqlite3_file *id, int *pResOut);
drhcc6bb3e2007-08-31 16:11:35 +0000277int sqlite3OsFileControl(sqlite3_file*,int,void*);
drhc02372c2012-01-10 17:59:59 +0000278void sqlite3OsFileControlHint(sqlite3_file*,int,void*);
drh8f941bc2009-01-14 23:03:40 +0000279#define SQLITE_FCNTL_DB_UNCHANGED 0xca093fa0
danielk197762079062007-08-15 17:08:46 +0000280int sqlite3OsSectorSize(sqlite3_file *id);
281int sqlite3OsDeviceCharacteristics(sqlite3_file *id);
danda9fe0c2010-07-13 18:44:03 +0000282int sqlite3OsShmMap(sqlite3_file *,int,int,int,void volatile **);
drh73b64e42010-05-30 19:55:15 +0000283int sqlite3OsShmLock(sqlite3_file *id, int, int, int);
drh286a2882010-05-20 23:51:06 +0000284void sqlite3OsShmBarrier(sqlite3_file *id);
drhe11fedc2010-07-14 00:14:30 +0000285int sqlite3OsShmUnmap(sqlite3_file *id, int);
danielk197762079062007-08-15 17:08:46 +0000286
dan6f2f19a2012-01-10 16:56:39 +0000287
danielk1977b4b47412007-08-17 15:53:36 +0000288/*
289** Functions for accessing sqlite3_vfs methods
290*/
291int sqlite3OsOpen(sqlite3_vfs *, const char *, sqlite3_file*, int, int *);
danielk1977fee2d252007-08-18 10:59:19 +0000292int sqlite3OsDelete(sqlite3_vfs *, const char *, int);
danielk1977861f7452008-06-05 11:39:11 +0000293int sqlite3OsAccess(sqlite3_vfs *, const char *, int, int *pResOut);
danielk1977adfb9b02007-09-17 07:02:56 +0000294int sqlite3OsFullPathname(sqlite3_vfs *, const char *, int, char *);
shane75998ab2008-05-29 02:52:59 +0000295#ifndef SQLITE_OMIT_LOAD_EXTENSION
danielk1977b4b47412007-08-17 15:53:36 +0000296void *sqlite3OsDlOpen(sqlite3_vfs *, const char *);
297void sqlite3OsDlError(sqlite3_vfs *, int, char *);
drh1875f7a2008-12-08 18:19:17 +0000298void (*sqlite3OsDlSym(sqlite3_vfs *, void *, const char *))(void);
danielk1977b4b47412007-08-17 15:53:36 +0000299void sqlite3OsDlClose(sqlite3_vfs *, void *);
shane75998ab2008-05-29 02:52:59 +0000300#endif /* SQLITE_OMIT_LOAD_EXTENSION */
danielk1977b4b47412007-08-17 15:53:36 +0000301int sqlite3OsRandomness(sqlite3_vfs *, int, char *);
302int sqlite3OsSleep(sqlite3_vfs *, int);
drhb7e8ea22010-05-03 14:32:30 +0000303int sqlite3OsCurrentTimeInt64(sqlite3_vfs *, sqlite3_int64*);
danielk1977b4b47412007-08-17 15:53:36 +0000304
305/*
306** Convenience functions for opening and closing files using
307** sqlite3_malloc() to obtain space for the file-handle structure.
308*/
danielk1977967a4a12007-08-20 14:23:44 +0000309int sqlite3OsOpenMalloc(sqlite3_vfs *, const char *, sqlite3_file **, int,int*);
danielk1977b4b47412007-08-17 15:53:36 +0000310int sqlite3OsCloseFree(sqlite3_file *);
311
drhe3c41372001-09-17 20:25:58 +0000312#endif /* _SQLITE_OS_H_ */