blob: 4854964fbef9819ff26689762eabb35276f4ae1c [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*/
drh0ccebe72005-06-07 22:22:50 +000026#if !defined(OS_UNIX) && !defined(OS_TEST) && !defined(OS_OTHER)
27# 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
drhe3c41372001-09-17 20:25:58 +000045/*
drhbbd42a62004-05-22 17:41:58 +000046** Invoke the appropriate operating-system specific header file.
drhe3c41372001-09-17 20:25:58 +000047*/
danielk1977e3026632004-06-22 11:29:02 +000048#if OS_TEST
49# include "os_test.h"
50#endif
drhe3c41372001-09-17 20:25:58 +000051#if OS_UNIX
drhbbd42a62004-05-22 17:41:58 +000052# include "os_unix.h"
drhe3c41372001-09-17 20:25:58 +000053#endif
drhe3c41372001-09-17 20:25:58 +000054#if OS_WIN
drhbbd42a62004-05-22 17:41:58 +000055# include "os_win.h"
drhe3c41372001-09-17 20:25:58 +000056#endif
drh0ccebe72005-06-07 22:22:50 +000057
58/* os_other.c and os_other.h are not delivered with SQLite. These files
59** are place-holders that can be filled in by third-party developers to
60** implement backends to their on proprietary operating systems.
61*/
62#if OS_OTHER
63# include "os_other.h"
drh2e66f0b2005-04-28 17:18:48 +000064#endif
drh820f3812003-01-08 13:02:52 +000065
drhb851b2c2005-03-10 14:11:12 +000066/* If the SET_FULLSYNC macro is not defined above, then make it
67** a no-op
68*/
69#ifndef SET_FULLSYNC
70# define SET_FULLSYNC(x,y)
71#endif
72
danielk19776622cce2004-05-20 11:00:52 +000073/*
drhbbd42a62004-05-22 17:41:58 +000074** Temporary files are named starting with this prefix followed by 16 random
75** alphanumeric characters, and no file extension. They are stored in the
76** OS's standard temporary file directory, and are deleted prior to exit.
77** If sqlite is being embedded in another program, you may wish to change the
78** prefix to reflect your program's name, so that if your program exits
79** prematurely, old temporary files can be easily identified. This can be done
80** using -DTEMP_FILE_PREFIX=myprefix_ on the compiler command line.
danielk19776622cce2004-05-20 11:00:52 +000081*/
drhbbd42a62004-05-22 17:41:58 +000082#ifndef TEMP_FILE_PREFIX
83# define TEMP_FILE_PREFIX "sqlite_"
84#endif
85
danielk19779a1d0ab2004-06-01 14:09:28 +000086/*
87** The following values may be passed as the second argument to
danielk197790ba3bd2004-06-25 08:32:25 +000088** sqlite3OsLock(). The various locks exhibit the following semantics:
89**
90** SHARED: Any number of processes may hold a SHARED lock simultaneously.
91** RESERVED: A single process may hold a RESERVED lock on a file at
92** any time. Other processes may hold and obtain new SHARED locks.
93** PENDING: A single process may hold a PENDING lock on a file at
94** any one time. Existing SHARED locks may persist, but no new
95** SHARED locks may be obtained by other processes.
96** EXCLUSIVE: An EXCLUSIVE lock precludes all other locks.
97**
98** PENDING_LOCK may not be passed directly to sqlite3OsLock(). Instead, a
99** process that requests an EXCLUSIVE lock may actually obtain a PENDING
100** lock. This can be upgraded to an EXCLUSIVE lock by a subsequent call to
danielk19779a1d0ab2004-06-01 14:09:28 +0000101** sqlite3OsLock().
102*/
danielk19779eed5052004-06-04 10:38:30 +0000103#define NO_LOCK 0
danielk19779a1d0ab2004-06-01 14:09:28 +0000104#define SHARED_LOCK 1
105#define RESERVED_LOCK 2
106#define PENDING_LOCK 3
107#define EXCLUSIVE_LOCK 4
danielk19776622cce2004-05-20 11:00:52 +0000108
drh2ac3ee92004-06-07 16:27:46 +0000109/*
drh1f595712004-06-15 01:40:29 +0000110** File Locking Notes: (Mostly about windows but also some info for Unix)
drh2ac3ee92004-06-07 16:27:46 +0000111**
112** We cannot use LockFileEx() or UnlockFileEx() on Win95/98/ME because
113** those functions are not available. So we use only LockFile() and
114** UnlockFile().
115**
116** LockFile() prevents not just writing but also reading by other processes.
drh2ac3ee92004-06-07 16:27:46 +0000117** A SHARED_LOCK is obtained by locking a single randomly-chosen
118** byte out of a specific range of bytes. The lock byte is obtained at
119** random so two separate readers can probably access the file at the
120** same time, unless they are unlucky and choose the same lock byte.
121** An EXCLUSIVE_LOCK is obtained by locking all bytes in the range.
122** There can only be one writer. A RESERVED_LOCK is obtained by locking
123** a single byte of the file that is designated as the reserved lock byte.
124** A PENDING_LOCK is obtained by locking a designated byte different from
125** the RESERVED_LOCK byte.
126**
127** On WinNT/2K/XP systems, LockFileEx() and UnlockFileEx() are available,
128** which means we can use reader/writer locks. When reader/writer locks
129** are used, the lock is placed on the same range of bytes that is used
130** for probabilistic locking in Win95/98/ME. Hence, the locking scheme
131** will support two or more Win95 readers or two or more WinNT readers.
132** But a single Win95 reader will lock out all WinNT readers and a single
133** WinNT reader will lock out all other Win95 readers.
134**
135** The following #defines specify the range of bytes used for locking.
136** SHARED_SIZE is the number of bytes available in the pool from which
137** a random byte is selected for a shared lock. The pool of bytes for
138** shared locks begins at SHARED_FIRST.
139**
140** These #defines are available in os.h so that Unix can use the same
141** byte ranges for locking. This leaves open the possiblity of having
142** clients on win95, winNT, and unix all talking to the same shared file
drh1f595712004-06-15 01:40:29 +0000143** and all locking correctly. To do so would require that samba (or whatever
144** tool is being used for file sharing) implements locks correctly between
145** windows and unix. I'm guessing that isn't likely to happen, but by
146** using the same locking range we are at least open to the possibility.
drh2ac3ee92004-06-07 16:27:46 +0000147**
148** Locking in windows is manditory. For this reason, we cannot store
149** actual data in the bytes used for locking. The pager never allocates
drh1f595712004-06-15 01:40:29 +0000150** the pages involved in locking therefore. SHARED_SIZE is selected so
151** that all locks will fit on a single page even at the minimum page size.
152** PENDING_BYTE defines the beginning of the locks. By default PENDING_BYTE
153** is set high so that we don't have to allocate an unused page except
154** for very large databases. But one should test the page skipping logic
155** by setting PENDING_BYTE low and running the entire regression suite.
156**
157** Changing the value of PENDING_BYTE results in a subtly incompatible
158** file format. Depending on how it is changed, you might not notice
159** the incompatibility right away, even running a full regression test.
160** The default location of PENDING_BYTE is the first byte past the
161** 1GB boundary.
162**
drh2ac3ee92004-06-07 16:27:46 +0000163*/
drh1f595712004-06-15 01:40:29 +0000164#define PENDING_BYTE 0x40000000 /* First byte past the 1GB boundary */
danielk1977599fcba2004-11-08 07:13:13 +0000165/* #define PENDING_BYTE 0x5400 // Page 22 - for testing */
drh1f595712004-06-15 01:40:29 +0000166#define RESERVED_BYTE (PENDING_BYTE+1)
167#define SHARED_FIRST (PENDING_BYTE+2)
168#define SHARED_SIZE 510
drh2ac3ee92004-06-07 16:27:46 +0000169
170
danielk19774adee202004-05-08 08:23:19 +0000171int sqlite3OsDelete(const char*);
172int sqlite3OsFileExists(const char*);
danielk19774adee202004-05-08 08:23:19 +0000173int sqlite3OsOpenReadWrite(const char*, OsFile*, int*);
174int sqlite3OsOpenExclusive(const char*, OsFile*, int);
175int sqlite3OsOpenReadOnly(const char*, OsFile*);
176int sqlite3OsOpenDirectory(const char*, OsFile*);
danielk1977962398d2004-06-14 09:35:16 +0000177int sqlite3OsSyncDirectory(const char*);
danielk19774adee202004-05-08 08:23:19 +0000178int sqlite3OsTempFileName(char*);
tpoindex9a09a3c2004-12-20 19:01:32 +0000179int sqlite3OsIsDirWritable(char*);
danielk19774adee202004-05-08 08:23:19 +0000180int sqlite3OsClose(OsFile*);
181int sqlite3OsRead(OsFile*, void*, int amt);
182int sqlite3OsWrite(OsFile*, const void*, int amt);
drheb206252004-10-01 02:00:31 +0000183int sqlite3OsSeek(OsFile*, i64 offset);
danielk19774adee202004-05-08 08:23:19 +0000184int sqlite3OsSync(OsFile*);
drheb206252004-10-01 02:00:31 +0000185int sqlite3OsTruncate(OsFile*, i64 size);
186int sqlite3OsFileSize(OsFile*, i64 *pSize);
danielk19774adee202004-05-08 08:23:19 +0000187char *sqlite3OsFullPathname(const char*);
danielk19779a1d0ab2004-06-01 14:09:28 +0000188int sqlite3OsLock(OsFile*, int);
drha6abd042004-06-09 17:37:22 +0000189int sqlite3OsUnlock(OsFile*, int);
190int sqlite3OsCheckReservedLock(OsFile *id);
drhe3c41372001-09-17 20:25:58 +0000191
drh0ccebe72005-06-07 22:22:50 +0000192
193/* The interface for file I/O is above. Other miscellaneous functions
194** are below */
195
196int sqlite3OsRandomSeed(char*);
197int sqlite3OsSleep(int ms);
198int sqlite3OsCurrentTime(double*);
199void sqlite3OsEnterMutex(void);
200void sqlite3OsLeaveMutex(void);
201
drhe3c41372001-09-17 20:25:58 +0000202#endif /* _SQLITE_OS_H_ */