blob: 8f7c650efc30c1157c07fa08576680d662de335f [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*/
drh66560ad2006-01-06 14:32:19 +000024#if !defined(OS_UNIX) && !defined(OS_OTHER)
drh0ccebe72005-06-07 22:22:50 +000025# define OS_OTHER 0
drh27a32202002-03-20 00:00:29 +000026# ifndef OS_WIN
drh0d477432005-01-16 20:47:40 +000027# if defined(_WIN32) || defined(WIN32) || defined(__CYGWIN__) || defined(__MINGW32__) || defined(__BORLANDC__)
28# define OS_WIN 1
29# define OS_UNIX 0
drh60a1e4b2006-06-03 18:02:15 +000030# define OS_OS2 0
31# elif defined(_EMX_) || defined(_OS2) || defined(OS2) || defined(OS_OS2)
32# define OS_WIN 0
33# define OS_UNIX 0
34# define OS_OS2 1
drh0d477432005-01-16 20:47:40 +000035# else
36# define OS_WIN 0
37# define OS_UNIX 1
drh60a1e4b2006-06-03 18:02:15 +000038# define OS_OS2 0
drh27a32202002-03-20 00:00:29 +000039# endif
40# else
41# define OS_UNIX 0
drh60a1e4b2006-06-03 18:02:15 +000042# define OS_OS2 0
drh27a32202002-03-20 00:00:29 +000043# endif
drh820f3812003-01-08 13:02:52 +000044#else
drhe5e37602003-08-16 13:10:51 +000045# ifndef OS_WIN
46# define OS_WIN 0
47# endif
drh1ab43002002-01-14 09:28:19 +000048#endif
49
drh054889e2005-11-30 03:20:31 +000050
drhe3c41372001-09-17 20:25:58 +000051/*
drh9cbe6352005-11-29 03:13:21 +000052** Define the maximum size of a temporary filename
drh0ccebe72005-06-07 22:22:50 +000053*/
drh9cbe6352005-11-29 03:13:21 +000054#if OS_WIN
drha2eebaa2005-11-29 19:50:24 +000055# include <windows.h>
drh9cbe6352005-11-29 03:13:21 +000056# define SQLITE_TEMPNAME_SIZE (MAX_PATH+50)
drh60a1e4b2006-06-03 18:02:15 +000057#elif OS_OS2
58# define INCL_DOSDATETIME
59# define INCL_DOSFILEMGR
60# define INCL_DOSERRORS
61# define INCL_DOSMISC
62# define INCL_DOSPROCESS
63# include <os2.h>
64# define SQLITE_TEMPNAME_SIZE (CCHMAXPATHCOMP)
drh9cbe6352005-11-29 03:13:21 +000065#else
66# define SQLITE_TEMPNAME_SIZE 200
drh2e66f0b2005-04-28 17:18:48 +000067#endif
drh820f3812003-01-08 13:02:52 +000068
drhb851b2c2005-03-10 14:11:12 +000069/* If the SET_FULLSYNC macro is not defined above, then make it
70** a no-op
71*/
72#ifndef SET_FULLSYNC
73# define SET_FULLSYNC(x,y)
74#endif
75
danielk19776622cce2004-05-20 11:00:52 +000076/*
drhbbd42a62004-05-22 17:41:58 +000077** Temporary files are named starting with this prefix followed by 16 random
78** alphanumeric characters, and no file extension. They are stored in the
79** OS's standard temporary file directory, and are deleted prior to exit.
80** If sqlite is being embedded in another program, you may wish to change the
81** prefix to reflect your program's name, so that if your program exits
82** prematurely, old temporary files can be easily identified. This can be done
83** using -DTEMP_FILE_PREFIX=myprefix_ on the compiler command line.
danielk19776622cce2004-05-20 11:00:52 +000084*/
drhbbd42a62004-05-22 17:41:58 +000085#ifndef TEMP_FILE_PREFIX
86# define TEMP_FILE_PREFIX "sqlite_"
87#endif
88
drh824d7c12006-01-06 12:03:19 +000089/*
drh60a1e4b2006-06-03 18:02:15 +000090** Define the interfaces for Unix, Windows, and OS/2.
drh66560ad2006-01-06 14:32:19 +000091*/
92#if OS_UNIX
93#define sqlite3OsOpenReadWrite sqlite3UnixOpenReadWrite
94#define sqlite3OsOpenExclusive sqlite3UnixOpenExclusive
95#define sqlite3OsOpenReadOnly sqlite3UnixOpenReadOnly
96#define sqlite3OsDelete sqlite3UnixDelete
97#define sqlite3OsFileExists sqlite3UnixFileExists
98#define sqlite3OsFullPathname sqlite3UnixFullPathname
99#define sqlite3OsIsDirWritable sqlite3UnixIsDirWritable
100#define sqlite3OsSyncDirectory sqlite3UnixSyncDirectory
101#define sqlite3OsTempFileName sqlite3UnixTempFileName
102#define sqlite3OsRandomSeed sqlite3UnixRandomSeed
103#define sqlite3OsSleep sqlite3UnixSleep
104#define sqlite3OsCurrentTime sqlite3UnixCurrentTime
105#define sqlite3OsEnterMutex sqlite3UnixEnterMutex
106#define sqlite3OsLeaveMutex sqlite3UnixLeaveMutex
107#define sqlite3OsInMutex sqlite3UnixInMutex
108#define sqlite3OsThreadSpecificData sqlite3UnixThreadSpecificData
109#define sqlite3OsMalloc sqlite3GenericMalloc
110#define sqlite3OsRealloc sqlite3GenericRealloc
111#define sqlite3OsFree sqlite3GenericFree
112#define sqlite3OsAllocationSize sqlite3GenericAllocationSize
113#endif
114#if OS_WIN
115#define sqlite3OsOpenReadWrite sqlite3WinOpenReadWrite
116#define sqlite3OsOpenExclusive sqlite3WinOpenExclusive
117#define sqlite3OsOpenReadOnly sqlite3WinOpenReadOnly
118#define sqlite3OsDelete sqlite3WinDelete
119#define sqlite3OsFileExists sqlite3WinFileExists
120#define sqlite3OsFullPathname sqlite3WinFullPathname
121#define sqlite3OsIsDirWritable sqlite3WinIsDirWritable
122#define sqlite3OsSyncDirectory sqlite3WinSyncDirectory
123#define sqlite3OsTempFileName sqlite3WinTempFileName
124#define sqlite3OsRandomSeed sqlite3WinRandomSeed
125#define sqlite3OsSleep sqlite3WinSleep
126#define sqlite3OsCurrentTime sqlite3WinCurrentTime
127#define sqlite3OsEnterMutex sqlite3WinEnterMutex
128#define sqlite3OsLeaveMutex sqlite3WinLeaveMutex
129#define sqlite3OsInMutex sqlite3WinInMutex
130#define sqlite3OsThreadSpecificData sqlite3WinThreadSpecificData
131#define sqlite3OsMalloc sqlite3GenericMalloc
132#define sqlite3OsRealloc sqlite3GenericRealloc
133#define sqlite3OsFree sqlite3GenericFree
134#define sqlite3OsAllocationSize sqlite3GenericAllocationSize
135#endif
drh60a1e4b2006-06-03 18:02:15 +0000136#if OS_OS2
137#define sqlite3OsOpenReadWrite sqlite3Os2OpenReadWrite
138#define sqlite3OsOpenExclusive sqlite3Os2OpenExclusive
139#define sqlite3OsOpenReadOnly sqlite3Os2OpenReadOnly
140#define sqlite3OsDelete sqlite3Os2Delete
141#define sqlite3OsFileExists sqlite3Os2FileExists
142#define sqlite3OsFullPathname sqlite3Os2FullPathname
143#define sqlite3OsIsDirWritable sqlite3Os2IsDirWritable
144#define sqlite3OsSyncDirectory sqlite3Os2SyncDirectory
145#define sqlite3OsTempFileName sqlite3Os2TempFileName
146#define sqlite3OsRandomSeed sqlite3Os2RandomSeed
147#define sqlite3OsSleep sqlite3Os2Sleep
148#define sqlite3OsCurrentTime sqlite3Os2CurrentTime
149#define sqlite3OsEnterMutex sqlite3Os2EnterMutex
150#define sqlite3OsLeaveMutex sqlite3Os2LeaveMutex
151#define sqlite3OsInMutex sqlite3Os2InMutex
152#define sqlite3OsThreadSpecificData sqlite3Os2ThreadSpecificData
153#define sqlite3OsMalloc sqlite3GenericMalloc
154#define sqlite3OsRealloc sqlite3GenericRealloc
155#define sqlite3OsFree sqlite3GenericFree
156#define sqlite3OsAllocationSize sqlite3GenericAllocationSize
157#endif
158
drh66560ad2006-01-06 14:32:19 +0000159
160/*
161** If using an alternative OS interface, then we must have an "os_other.h"
162** header file available for that interface. Presumably the "os_other.h"
163** header file contains #defines similar to those above.
164*/
165#if OS_OTHER
166# include "os_other.h"
167#endif
168
169
170
171/*
drh824d7c12006-01-06 12:03:19 +0000172** Forward declarations
173*/
174typedef struct OsFile OsFile;
175typedef struct IoMethod IoMethod;
drh0ccebe72005-06-07 22:22:50 +0000176
drh824d7c12006-01-06 12:03:19 +0000177/*
178** An instance of the following structure contains pointers to all
179** methods on an OsFile object.
180*/
181struct IoMethod {
182 int (*xClose)(OsFile**);
183 int (*xOpenDirectory)(OsFile*, const char*);
184 int (*xRead)(OsFile*, void*, int amt);
185 int (*xWrite)(OsFile*, const void*, int amt);
186 int (*xSeek)(OsFile*, i64 offset);
187 int (*xTruncate)(OsFile*, i64 size);
188 int (*xSync)(OsFile*, int);
189 void (*xSetFullSync)(OsFile *id, int setting);
190 int (*xFileHandle)(OsFile *id);
191 int (*xFileSize)(OsFile*, i64 *pSize);
192 int (*xLock)(OsFile*, int);
193 int (*xUnlock)(OsFile*, int);
194 int (*xLockState)(OsFile *id);
195 int (*xCheckReservedLock)(OsFile *id);
196};
197
198/*
199** The OsFile object describes an open disk file in an OS-dependent way.
200** The version of OsFile defined here is a generic version. Each OS
201** implementation defines its own subclass of this structure that contains
202** additional information needed to handle file I/O. But the pMethod
203** entry (pointing to the virtual function table) always occurs first
204** so that we can always find the appropriate methods.
205*/
206struct OsFile {
207 IoMethod const *pMethod;
208};
209
210/*
211** The following values may be passed as the second argument to
212** sqlite3OsLock(). The various locks exhibit the following semantics:
213**
214** SHARED: Any number of processes may hold a SHARED lock simultaneously.
215** RESERVED: A single process may hold a RESERVED lock on a file at
216** any time. Other processes may hold and obtain new SHARED locks.
217** PENDING: A single process may hold a PENDING lock on a file at
218** any one time. Existing SHARED locks may persist, but no new
219** SHARED locks may be obtained by other processes.
220** EXCLUSIVE: An EXCLUSIVE lock precludes all other locks.
221**
222** PENDING_LOCK may not be passed directly to sqlite3OsLock(). Instead, a
223** process that requests an EXCLUSIVE lock may actually obtain a PENDING
224** lock. This can be upgraded to an EXCLUSIVE lock by a subsequent call to
225** sqlite3OsLock().
226*/
227#define NO_LOCK 0
228#define SHARED_LOCK 1
229#define RESERVED_LOCK 2
230#define PENDING_LOCK 3
231#define EXCLUSIVE_LOCK 4
232
233/*
234** File Locking Notes: (Mostly about windows but also some info for Unix)
235**
236** We cannot use LockFileEx() or UnlockFileEx() on Win95/98/ME because
237** those functions are not available. So we use only LockFile() and
238** UnlockFile().
239**
240** LockFile() prevents not just writing but also reading by other processes.
241** A SHARED_LOCK is obtained by locking a single randomly-chosen
242** byte out of a specific range of bytes. The lock byte is obtained at
243** random so two separate readers can probably access the file at the
244** same time, unless they are unlucky and choose the same lock byte.
245** An EXCLUSIVE_LOCK is obtained by locking all bytes in the range.
246** There can only be one writer. A RESERVED_LOCK is obtained by locking
247** a single byte of the file that is designated as the reserved lock byte.
248** A PENDING_LOCK is obtained by locking a designated byte different from
249** the RESERVED_LOCK byte.
250**
251** On WinNT/2K/XP systems, LockFileEx() and UnlockFileEx() are available,
252** which means we can use reader/writer locks. When reader/writer locks
253** are used, the lock is placed on the same range of bytes that is used
254** for probabilistic locking in Win95/98/ME. Hence, the locking scheme
255** will support two or more Win95 readers or two or more WinNT readers.
256** But a single Win95 reader will lock out all WinNT readers and a single
257** WinNT reader will lock out all other Win95 readers.
258**
259** The following #defines specify the range of bytes used for locking.
260** SHARED_SIZE is the number of bytes available in the pool from which
261** a random byte is selected for a shared lock. The pool of bytes for
262** shared locks begins at SHARED_FIRST.
263**
264** These #defines are available in sqlite_aux.h so that adaptors for
265** connecting SQLite to other operating systems can use the same byte
266** ranges for locking. In particular, the same locking strategy and
267** byte ranges are used for Unix. This leaves open the possiblity of having
268** clients on win95, winNT, and unix all talking to the same shared file
269** and all locking correctly. To do so would require that samba (or whatever
270** tool is being used for file sharing) implements locks correctly between
271** windows and unix. I'm guessing that isn't likely to happen, but by
272** using the same locking range we are at least open to the possibility.
273**
274** Locking in windows is manditory. For this reason, we cannot store
275** actual data in the bytes used for locking. The pager never allocates
276** the pages involved in locking therefore. SHARED_SIZE is selected so
277** that all locks will fit on a single page even at the minimum page size.
278** PENDING_BYTE defines the beginning of the locks. By default PENDING_BYTE
279** is set high so that we don't have to allocate an unused page except
280** for very large databases. But one should test the page skipping logic
281** by setting PENDING_BYTE low and running the entire regression suite.
282**
283** Changing the value of PENDING_BYTE results in a subtly incompatible
284** file format. Depending on how it is changed, you might not notice
285** the incompatibility right away, even running a full regression test.
286** The default location of PENDING_BYTE is the first byte past the
287** 1GB boundary.
288**
289*/
290#ifndef SQLITE_TEST
291#define PENDING_BYTE 0x40000000 /* First byte past the 1GB boundary */
292#else
293extern unsigned int sqlite3_pending_byte;
294#define PENDING_BYTE sqlite3_pending_byte
295#endif
296
297#define RESERVED_BYTE (PENDING_BYTE+1)
298#define SHARED_FIRST (PENDING_BYTE+2)
299#define SHARED_SIZE 510
300
301/*
drh66560ad2006-01-06 14:32:19 +0000302** Prototypes for operating system interface routines.
drh824d7c12006-01-06 12:03:19 +0000303*/
304int sqlite3OsClose(OsFile**);
305int sqlite3OsOpenDirectory(OsFile*, const char*);
306int sqlite3OsRead(OsFile*, void*, int amt);
307int sqlite3OsWrite(OsFile*, const void*, int amt);
308int sqlite3OsSeek(OsFile*, i64 offset);
309int sqlite3OsTruncate(OsFile*, i64 size);
310int sqlite3OsSync(OsFile*, int);
311void sqlite3OsSetFullSync(OsFile *id, int setting);
312int sqlite3OsFileHandle(OsFile *id);
313int sqlite3OsFileSize(OsFile*, i64 *pSize);
314int sqlite3OsLock(OsFile*, int);
315int sqlite3OsUnlock(OsFile*, int);
316int sqlite3OsLockState(OsFile *id);
317int sqlite3OsCheckReservedLock(OsFile *id);
drh66560ad2006-01-06 14:32:19 +0000318int sqlite3OsOpenReadWrite(const char*, OsFile**, int*);
319int sqlite3OsOpenExclusive(const char*, OsFile**, int);
320int sqlite3OsOpenReadOnly(const char*, OsFile**);
321int sqlite3OsDelete(const char*);
322int sqlite3OsFileExists(const char*);
323char *sqlite3OsFullPathname(const char*);
324int sqlite3OsIsDirWritable(char*);
325int sqlite3OsSyncDirectory(const char*);
326int sqlite3OsTempFileName(char*);
327int sqlite3OsRandomSeed(char*);
328int sqlite3OsSleep(int ms);
329int sqlite3OsCurrentTime(double*);
330void sqlite3OsEnterMutex(void);
331void sqlite3OsLeaveMutex(void);
drh757b04e2006-01-18 17:25:45 +0000332int sqlite3OsInMutex(int);
drh6f7adc82006-01-11 21:41:20 +0000333ThreadData *sqlite3OsThreadSpecificData(int);
drh66560ad2006-01-06 14:32:19 +0000334void *sqlite3OsMalloc(int);
335void *sqlite3OsRealloc(void *, int);
336void sqlite3OsFree(void *);
337int sqlite3OsAllocationSize(void *);
drh9c06c952005-11-26 00:25:00 +0000338
drh3f459022006-01-07 16:06:07 +0000339/*
340** If the SQLITE_ENABLE_REDEF_IO macro is defined, then the OS-layer
341** interface routines are not called directly but are invoked using
342** pointers to functions. This allows the implementation of various
343** OS-layer interface routines to be modified at run-time. There are
344** obscure but legitimate reasons for wanting to do this. But for
345** most users, a direct call to the underlying interface is preferable
346** so the the redefinable I/O interface is turned off by default.
347*/
348#ifdef SQLITE_ENABLE_REDEF_IO
349
350/*
351** When redefinable I/O is enabled, a single global instance of the
352** following structure holds pointers to the routines that SQLite
353** uses to talk with the underlying operating system. Modify this
354** structure (before using any SQLite API!) to accomodate perculiar
355** operating system interfaces or behaviors.
356*/
357struct sqlite3OsVtbl {
358 int (*xOpenReadWrite)(const char*, OsFile**, int*);
359 int (*xOpenExclusive)(const char*, OsFile**, int);
360 int (*xOpenReadOnly)(const char*, OsFile**);
361
362 int (*xDelete)(const char*);
363 int (*xFileExists)(const char*);
364 char *(*xFullPathname)(const char*);
365 int (*xIsDirWritable)(char*);
366 int (*xSyncDirectory)(const char*);
367 int (*xTempFileName)(char*);
368
369 int (*xRandomSeed)(char*);
370 int (*xSleep)(int ms);
371 int (*xCurrentTime)(double*);
372
373 void (*xEnterMutex)(void);
374 void (*xLeaveMutex)(void);
drhbcdb6612006-01-21 19:57:50 +0000375 int (*xInMutex)(int);
drh6f7adc82006-01-11 21:41:20 +0000376 ThreadData *(*xThreadSpecificData)(int);
drh3f459022006-01-07 16:06:07 +0000377
378 void *(*xMalloc)(int);
379 void *(*xRealloc)(void *, int);
380 void (*xFree)(void *);
381 int (*xAllocationSize)(void *);
382};
383
384/* Macro used to comment out routines that do not exists when there is
385** no disk I/O
386*/
387#ifdef SQLITE_OMIT_DISKIO
388# define IF_DISKIO(X) 0
389#else
390# define IF_DISKIO(X) X
391#endif
392
393#ifdef _SQLITE_OS_C_
394 /*
395 ** The os.c file implements the global virtual function table.
396 */
397 struct sqlite3OsVtbl sqlite3Os = {
398 IF_DISKIO( sqlite3OsOpenReadWrite ),
399 IF_DISKIO( sqlite3OsOpenExclusive ),
400 IF_DISKIO( sqlite3OsOpenReadOnly ),
401 IF_DISKIO( sqlite3OsDelete ),
402 IF_DISKIO( sqlite3OsFileExists ),
403 IF_DISKIO( sqlite3OsFullPathname ),
404 IF_DISKIO( sqlite3OsIsDirWritable ),
405 IF_DISKIO( sqlite3OsSyncDirectory ),
406 IF_DISKIO( sqlite3OsTempFileName ),
407 sqlite3OsRandomSeed,
408 sqlite3OsSleep,
409 sqlite3OsCurrentTime,
410 sqlite3OsEnterMutex,
411 sqlite3OsLeaveMutex,
412 sqlite3OsInMutex,
413 sqlite3OsThreadSpecificData,
414 sqlite3OsMalloc,
415 sqlite3OsRealloc,
416 sqlite3OsFree,
417 sqlite3OsAllocationSize
418 };
419#else
420 /*
421 ** Files other than os.c just reference the global virtual function table.
422 */
423 extern struct sqlite3OsVtbl sqlite3Os;
424#endif /* _SQLITE_OS_C_ */
425
426
427/* This additional API routine is available with redefinable I/O */
428struct sqlite3OsVtbl *sqlite3_os_switch(void);
429
430
431/*
432** Redefine the OS interface to go through the virtual function table
433** rather than calling routines directly.
434*/
435#undef sqlite3OsOpenReadWrite
436#undef sqlite3OsOpenExclusive
437#undef sqlite3OsOpenReadOnly
438#undef sqlite3OsDelete
439#undef sqlite3OsFileExists
440#undef sqlite3OsFullPathname
441#undef sqlite3OsIsDirWritable
442#undef sqlite3OsSyncDirectory
443#undef sqlite3OsTempFileName
444#undef sqlite3OsRandomSeed
445#undef sqlite3OsSleep
446#undef sqlite3OsCurrentTime
447#undef sqlite3OsEnterMutex
448#undef sqlite3OsLeaveMutex
449#undef sqlite3OsInMutex
450#undef sqlite3OsThreadSpecificData
451#undef sqlite3OsMalloc
452#undef sqlite3OsRealloc
453#undef sqlite3OsFree
454#undef sqlite3OsAllocationSize
455#define sqlite3OsOpenReadWrite sqlite3Os.xOpenReadWrite
456#define sqlite3OsOpenExclusive sqlite3Os.xOpenExclusive
457#define sqlite3OsOpenReadOnly sqlite3Os.xOpenReadOnly
458#define sqlite3OsDelete sqlite3Os.xDelete
459#define sqlite3OsFileExists sqlite3Os.xFileExists
460#define sqlite3OsFullPathname sqlite3Os.xFullPathname
461#define sqlite3OsIsDirWritable sqlite3Os.xIsDirWritable
462#define sqlite3OsSyncDirectory sqlite3Os.xSyncDirectory
463#define sqlite3OsTempFileName sqlite3Os.xTempFileName
464#define sqlite3OsRandomSeed sqlite3Os.xRandomSeed
465#define sqlite3OsSleep sqlite3Os.xSleep
466#define sqlite3OsCurrentTime sqlite3Os.xCurrentTime
467#define sqlite3OsEnterMutex sqlite3Os.xEnterMutex
468#define sqlite3OsLeaveMutex sqlite3Os.xLeaveMutex
469#define sqlite3OsInMutex sqlite3Os.xInMutex
470#define sqlite3OsThreadSpecificData sqlite3Os.xThreadSpecificData
471#define sqlite3OsMalloc sqlite3Os.xMalloc
472#define sqlite3OsRealloc sqlite3Os.xRealloc
473#define sqlite3OsFree sqlite3Os.xFree
474#define sqlite3OsAllocationSize sqlite3Os.xAllocationSize
475
476#endif /* SQLITE_ENABLE_REDEF_IO */
477
drhe3c41372001-09-17 20:25:58 +0000478#endif /* _SQLITE_OS_H_ */