blob: c406b1409991538ed0908333232c8cf94f9cc814 [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.
danielk1977822a5162008-05-16 04:51:54 +000019**
shane712d6f92008-05-29 03:54:26 +000020** $Id: os.h,v 1.101 2008/05/29 03:54:27 shane Exp $
drhe3c41372001-09-17 20:25:58 +000021*/
22#ifndef _SQLITE_OS_H_
23#define _SQLITE_OS_H_
24
drh829e8022002-11-06 14:08:11 +000025/*
drh66560ad2006-01-06 14:32:19 +000026** Figure out if we are dealing with Unix, Windows, or some other
drh29278e32007-08-21 10:44:15 +000027** operating system. After the following block of preprocess macros,
28** all of OS_UNIX, OS_WIN, OS_OS2, and OS_OTHER will defined to either
29** 1 or 0. One of the four will be 1. The other three will be 0.
drh829e8022002-11-06 14:08:11 +000030*/
drhee2ce182007-04-02 16:45:12 +000031#if defined(OS_OTHER)
32# if OS_OTHER==1
33# undef OS_UNIX
34# define OS_UNIX 0
35# undef OS_WIN
36# define OS_WIN 0
37# undef OS_OS2
38# define OS_OS2 0
39# else
40# undef OS_OTHER
41# endif
42#endif
drh66560ad2006-01-06 14:32:19 +000043#if !defined(OS_UNIX) && !defined(OS_OTHER)
drh0ccebe72005-06-07 22:22:50 +000044# define OS_OTHER 0
drh27a32202002-03-20 00:00:29 +000045# ifndef OS_WIN
drh0d477432005-01-16 20:47:40 +000046# if defined(_WIN32) || defined(WIN32) || defined(__CYGWIN__) || defined(__MINGW32__) || defined(__BORLANDC__)
47# define OS_WIN 1
48# define OS_UNIX 0
drh60a1e4b2006-06-03 18:02:15 +000049# define OS_OS2 0
pweilbacher3f61bc72007-06-30 15:24:37 +000050# elif defined(__EMX__) || defined(_OS2) || defined(OS2) || defined(_OS2_) || defined(__OS2__)
drh60a1e4b2006-06-03 18:02:15 +000051# define OS_WIN 0
52# define OS_UNIX 0
53# define OS_OS2 1
drh0d477432005-01-16 20:47:40 +000054# else
55# define OS_WIN 0
56# define OS_UNIX 1
drh60a1e4b2006-06-03 18:02:15 +000057# define OS_OS2 0
drh27a32202002-03-20 00:00:29 +000058# endif
59# else
60# define OS_UNIX 0
drh60a1e4b2006-06-03 18:02:15 +000061# define OS_OS2 0
drh27a32202002-03-20 00:00:29 +000062# endif
drh820f3812003-01-08 13:02:52 +000063#else
drhe5e37602003-08-16 13:10:51 +000064# ifndef OS_WIN
65# define OS_WIN 0
66# endif
drh1ab43002002-01-14 09:28:19 +000067#endif
68
shane712d6f92008-05-29 03:54:26 +000069/*
70** Determine if we are dealing with WindowsCE - which has a much
71** reduced API.
72*/
73#if defined(_WIN32_WCE)
74# define OS_WINCE 1
75#else
76# define OS_WINCE 0
77#endif
drh054889e2005-11-30 03:20:31 +000078
drh29278e32007-08-21 10:44:15 +000079
drhe3c41372001-09-17 20:25:58 +000080/*
drh9cbe6352005-11-29 03:13:21 +000081** Define the maximum size of a temporary filename
drh0ccebe72005-06-07 22:22:50 +000082*/
drh9cbe6352005-11-29 03:13:21 +000083#if OS_WIN
drha2eebaa2005-11-29 19:50:24 +000084# include <windows.h>
drh9cbe6352005-11-29 03:13:21 +000085# define SQLITE_TEMPNAME_SIZE (MAX_PATH+50)
drh60a1e4b2006-06-03 18:02:15 +000086#elif OS_OS2
pweilbacher53b4bd32007-06-22 20:17:37 +000087# if (__GNUC__ > 3 || __GNUC__ == 3 && __GNUC_MINOR__ >= 3) && defined(OS2_HIGH_MEMORY)
88# include <os2safe.h> /* has to be included before os2.h for linking to work */
89# endif
drh60a1e4b2006-06-03 18:02:15 +000090# define INCL_DOSDATETIME
91# define INCL_DOSFILEMGR
92# define INCL_DOSERRORS
93# define INCL_DOSMISC
94# define INCL_DOSPROCESS
pweilbacher691902e2007-06-22 20:04:36 +000095# define INCL_DOSMODULEMGR
pweilbacher0663dd22007-10-21 22:59:12 +000096# define INCL_DOSSEMAPHORES
drh60a1e4b2006-06-03 18:02:15 +000097# include <os2.h>
pweilbacherd190be82008-04-15 18:50:02 +000098# include <uconv.h>
drh60a1e4b2006-06-03 18:02:15 +000099# define SQLITE_TEMPNAME_SIZE (CCHMAXPATHCOMP)
drh9cbe6352005-11-29 03:13:21 +0000100#else
101# define SQLITE_TEMPNAME_SIZE 200
drh2e66f0b2005-04-28 17:18:48 +0000102#endif
drh820f3812003-01-08 13:02:52 +0000103
drhb851b2c2005-03-10 14:11:12 +0000104/* If the SET_FULLSYNC macro is not defined above, then make it
105** a no-op
106*/
107#ifndef SET_FULLSYNC
108# define SET_FULLSYNC(x,y)
109#endif
110
danielk19776622cce2004-05-20 11:00:52 +0000111/*
drh3ceeb752007-03-29 18:19:52 +0000112** The default size of a disk sector
113*/
114#ifndef SQLITE_DEFAULT_SECTOR_SIZE
115# define SQLITE_DEFAULT_SECTOR_SIZE 512
116#endif
117
118/*
drhbbd42a62004-05-22 17:41:58 +0000119** Temporary files are named starting with this prefix followed by 16 random
120** alphanumeric characters, and no file extension. They are stored in the
121** OS's standard temporary file directory, and are deleted prior to exit.
122** If sqlite is being embedded in another program, you may wish to change the
123** prefix to reflect your program's name, so that if your program exits
124** prematurely, old temporary files can be easily identified. This can be done
drh153c62c2007-08-24 03:51:33 +0000125** using -DSQLITE_TEMP_FILE_PREFIX=myprefix_ on the compiler command line.
drhfd288f32006-10-31 21:27:33 +0000126**
127** 2006-10-31: The default prefix used to be "sqlite_". But then
128** Mcafee started using SQLite in their anti-virus product and it
129** started putting files with the "sqlite" name in the c:/temp folder.
130** This annoyed many windows users. Those users would then do a
131** Google search for "sqlite", find the telephone numbers of the
132** developers and call to wake them up at night and complain.
133** For this reason, the default name prefix is changed to be "sqlite"
134** spelled backwards. So the temp files are still identified, but
135** anybody smart enough to figure out the code is also likely smart
136** enough to know that calling the developer will not help get rid
137** of the file.
danielk19776622cce2004-05-20 11:00:52 +0000138*/
drh153c62c2007-08-24 03:51:33 +0000139#ifndef SQLITE_TEMP_FILE_PREFIX
140# define SQLITE_TEMP_FILE_PREFIX "etilqs_"
drhbbd42a62004-05-22 17:41:58 +0000141#endif
142
drh66560ad2006-01-06 14:32:19 +0000143/*
drh824d7c12006-01-06 12:03:19 +0000144** The following values may be passed as the second argument to
145** sqlite3OsLock(). The various locks exhibit the following semantics:
146**
147** SHARED: Any number of processes may hold a SHARED lock simultaneously.
148** RESERVED: A single process may hold a RESERVED lock on a file at
149** any time. Other processes may hold and obtain new SHARED locks.
150** PENDING: A single process may hold a PENDING lock on a file at
151** any one time. Existing SHARED locks may persist, but no new
152** SHARED locks may be obtained by other processes.
153** EXCLUSIVE: An EXCLUSIVE lock precludes all other locks.
154**
155** PENDING_LOCK may not be passed directly to sqlite3OsLock(). Instead, a
156** process that requests an EXCLUSIVE lock may actually obtain a PENDING
157** lock. This can be upgraded to an EXCLUSIVE lock by a subsequent call to
158** sqlite3OsLock().
159*/
160#define NO_LOCK 0
161#define SHARED_LOCK 1
162#define RESERVED_LOCK 2
163#define PENDING_LOCK 3
164#define EXCLUSIVE_LOCK 4
165
166/*
167** File Locking Notes: (Mostly about windows but also some info for Unix)
168**
169** We cannot use LockFileEx() or UnlockFileEx() on Win95/98/ME because
170** those functions are not available. So we use only LockFile() and
171** UnlockFile().
172**
173** LockFile() prevents not just writing but also reading by other processes.
174** A SHARED_LOCK is obtained by locking a single randomly-chosen
175** byte out of a specific range of bytes. The lock byte is obtained at
176** random so two separate readers can probably access the file at the
177** same time, unless they are unlucky and choose the same lock byte.
178** An EXCLUSIVE_LOCK is obtained by locking all bytes in the range.
179** There can only be one writer. A RESERVED_LOCK is obtained by locking
180** a single byte of the file that is designated as the reserved lock byte.
181** A PENDING_LOCK is obtained by locking a designated byte different from
182** the RESERVED_LOCK byte.
183**
184** On WinNT/2K/XP systems, LockFileEx() and UnlockFileEx() are available,
185** which means we can use reader/writer locks. When reader/writer locks
186** are used, the lock is placed on the same range of bytes that is used
187** for probabilistic locking in Win95/98/ME. Hence, the locking scheme
188** will support two or more Win95 readers or two or more WinNT readers.
189** But a single Win95 reader will lock out all WinNT readers and a single
190** WinNT reader will lock out all other Win95 readers.
191**
192** The following #defines specify the range of bytes used for locking.
193** SHARED_SIZE is the number of bytes available in the pool from which
194** a random byte is selected for a shared lock. The pool of bytes for
195** shared locks begins at SHARED_FIRST.
196**
197** These #defines are available in sqlite_aux.h so that adaptors for
198** connecting SQLite to other operating systems can use the same byte
199** ranges for locking. In particular, the same locking strategy and
200** byte ranges are used for Unix. This leaves open the possiblity of having
201** clients on win95, winNT, and unix all talking to the same shared file
202** and all locking correctly. To do so would require that samba (or whatever
203** tool is being used for file sharing) implements locks correctly between
204** windows and unix. I'm guessing that isn't likely to happen, but by
205** using the same locking range we are at least open to the possibility.
206**
207** Locking in windows is manditory. For this reason, we cannot store
208** actual data in the bytes used for locking. The pager never allocates
209** the pages involved in locking therefore. SHARED_SIZE is selected so
210** that all locks will fit on a single page even at the minimum page size.
211** PENDING_BYTE defines the beginning of the locks. By default PENDING_BYTE
212** is set high so that we don't have to allocate an unused page except
213** for very large databases. But one should test the page skipping logic
214** by setting PENDING_BYTE low and running the entire regression suite.
215**
216** Changing the value of PENDING_BYTE results in a subtly incompatible
217** file format. Depending on how it is changed, you might not notice
218** the incompatibility right away, even running a full regression test.
219** The default location of PENDING_BYTE is the first byte past the
220** 1GB boundary.
221**
222*/
223#ifndef SQLITE_TEST
224#define PENDING_BYTE 0x40000000 /* First byte past the 1GB boundary */
225#else
226extern unsigned int sqlite3_pending_byte;
227#define PENDING_BYTE sqlite3_pending_byte
228#endif
229
230#define RESERVED_BYTE (PENDING_BYTE+1)
231#define SHARED_FIRST (PENDING_BYTE+2)
232#define SHARED_SIZE 510
233
danielk1977b4b47412007-08-17 15:53:36 +0000234/*
235** Functions for accessing sqlite3_file methods
drh824d7c12006-01-06 12:03:19 +0000236*/
danielk1977b4b47412007-08-17 15:53:36 +0000237int sqlite3OsClose(sqlite3_file*);
danielk197762079062007-08-15 17:08:46 +0000238int sqlite3OsRead(sqlite3_file*, void*, int amt, i64 offset);
239int sqlite3OsWrite(sqlite3_file*, const void*, int amt, i64 offset);
240int sqlite3OsTruncate(sqlite3_file*, i64 size);
241int sqlite3OsSync(sqlite3_file*, int);
242int sqlite3OsFileSize(sqlite3_file*, i64 *pSize);
243int sqlite3OsLock(sqlite3_file*, int);
244int sqlite3OsUnlock(sqlite3_file*, int);
danielk197762079062007-08-15 17:08:46 +0000245int sqlite3OsCheckReservedLock(sqlite3_file *id);
drhcc6bb3e2007-08-31 16:11:35 +0000246int sqlite3OsFileControl(sqlite3_file*,int,void*);
danielk197762079062007-08-15 17:08:46 +0000247int sqlite3OsSectorSize(sqlite3_file *id);
248int sqlite3OsDeviceCharacteristics(sqlite3_file *id);
249
danielk1977b4b47412007-08-17 15:53:36 +0000250/*
251** Functions for accessing sqlite3_vfs methods
252*/
253int sqlite3OsOpen(sqlite3_vfs *, const char *, sqlite3_file*, int, int *);
danielk1977fee2d252007-08-18 10:59:19 +0000254int sqlite3OsDelete(sqlite3_vfs *, const char *, int);
danielk1977b4b47412007-08-17 15:53:36 +0000255int sqlite3OsAccess(sqlite3_vfs *, const char *, int);
danielk1977adfb9b02007-09-17 07:02:56 +0000256int sqlite3OsGetTempname(sqlite3_vfs *, int, char *);
257int sqlite3OsFullPathname(sqlite3_vfs *, const char *, int, char *);
shane75998ab2008-05-29 02:52:59 +0000258#ifndef SQLITE_OMIT_LOAD_EXTENSION
danielk1977b4b47412007-08-17 15:53:36 +0000259void *sqlite3OsDlOpen(sqlite3_vfs *, const char *);
260void sqlite3OsDlError(sqlite3_vfs *, int, char *);
261void *sqlite3OsDlSym(sqlite3_vfs *, void *, const char *);
262void sqlite3OsDlClose(sqlite3_vfs *, void *);
shane75998ab2008-05-29 02:52:59 +0000263#endif /* SQLITE_OMIT_LOAD_EXTENSION */
danielk1977b4b47412007-08-17 15:53:36 +0000264int sqlite3OsRandomness(sqlite3_vfs *, int, char *);
265int sqlite3OsSleep(sqlite3_vfs *, int);
266int sqlite3OsCurrentTime(sqlite3_vfs *, double*);
267
268/*
269** Convenience functions for opening and closing files using
270** sqlite3_malloc() to obtain space for the file-handle structure.
271*/
danielk1977967a4a12007-08-20 14:23:44 +0000272int sqlite3OsOpenMalloc(sqlite3_vfs *, const char *, sqlite3_file **, int,int*);
danielk1977b4b47412007-08-17 15:53:36 +0000273int sqlite3OsCloseFree(sqlite3_file *);
274
drh153c62c2007-08-24 03:51:33 +0000275/*
276** Each OS-specific backend defines an instance of the following
277** structure for returning a pointer to its sqlite3_vfs. If OS_OTHER
278** is defined (meaning that the application-defined OS interface layer
279** is used) then there is no default VFS. The application must
280** register one or more VFS structures using sqlite3_vfs_register()
281** before attempting to use SQLite.
282*/
drh153c62c2007-08-24 03:51:33 +0000283sqlite3_vfs *sqlite3OsDefaultVfs(void);
drh153c62c2007-08-24 03:51:33 +0000284
drhe3c41372001-09-17 20:25:58 +0000285#endif /* _SQLITE_OS_H_ */