blob: e4010072dbe33c669711514845803abc2abfc31f [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*/
drh2e66f0b2005-04-28 17:18:48 +000026#if !defined(OS_UNIX) && !defined(OS_TEST) && !defined(OS_MEM)
drh27a32202002-03-20 00:00:29 +000027# ifndef OS_WIN
drh0d477432005-01-16 20:47:40 +000028# if defined(_WIN32) || defined(WIN32) || defined(__CYGWIN__) || defined(__MINGW32__) || defined(__BORLANDC__)
29# define OS_WIN 1
30# define OS_UNIX 0
31# else
32# define OS_WIN 0
33# define OS_UNIX 1
drh27a32202002-03-20 00:00:29 +000034# endif
35# else
36# define OS_UNIX 0
37# endif
drh820f3812003-01-08 13:02:52 +000038#else
drhe5e37602003-08-16 13:10:51 +000039# ifndef OS_WIN
40# define OS_WIN 0
41# endif
drh1ab43002002-01-14 09:28:19 +000042#endif
drh2e66f0b2005-04-28 17:18:48 +000043#ifndef OS_MEM
44# define OS_MEM 0
45#endif
drh1ab43002002-01-14 09:28:19 +000046
drhe3c41372001-09-17 20:25:58 +000047/*
drhbbd42a62004-05-22 17:41:58 +000048** Invoke the appropriate operating-system specific header file.
drhe3c41372001-09-17 20:25:58 +000049*/
danielk1977e3026632004-06-22 11:29:02 +000050#if OS_TEST
51# include "os_test.h"
52#endif
drhe3c41372001-09-17 20:25:58 +000053#if OS_UNIX
drhbbd42a62004-05-22 17:41:58 +000054# include "os_unix.h"
drhe3c41372001-09-17 20:25:58 +000055#endif
drhe3c41372001-09-17 20:25:58 +000056#if OS_WIN
drhbbd42a62004-05-22 17:41:58 +000057# include "os_win.h"
drhe3c41372001-09-17 20:25:58 +000058#endif
drh2e66f0b2005-04-28 17:18:48 +000059#if OS_MEM
60# include "os_mem.h"
61#endif
drh820f3812003-01-08 13:02:52 +000062
drhb851b2c2005-03-10 14:11:12 +000063/* If the SET_FULLSYNC macro is not defined above, then make it
64** a no-op
65*/
66#ifndef SET_FULLSYNC
67# define SET_FULLSYNC(x,y)
68#endif
69
danielk19776622cce2004-05-20 11:00:52 +000070/*
drhbbd42a62004-05-22 17:41:58 +000071** Temporary files are named starting with this prefix followed by 16 random
72** alphanumeric characters, and no file extension. They are stored in the
73** OS's standard temporary file directory, and are deleted prior to exit.
74** If sqlite is being embedded in another program, you may wish to change the
75** prefix to reflect your program's name, so that if your program exits
76** prematurely, old temporary files can be easily identified. This can be done
77** using -DTEMP_FILE_PREFIX=myprefix_ on the compiler command line.
danielk19776622cce2004-05-20 11:00:52 +000078*/
drhbbd42a62004-05-22 17:41:58 +000079#ifndef TEMP_FILE_PREFIX
80# define TEMP_FILE_PREFIX "sqlite_"
81#endif
82
danielk19779a1d0ab2004-06-01 14:09:28 +000083/*
84** The following values may be passed as the second argument to
danielk197790ba3bd2004-06-25 08:32:25 +000085** sqlite3OsLock(). The various locks exhibit the following semantics:
86**
87** SHARED: Any number of processes may hold a SHARED lock simultaneously.
88** RESERVED: A single process may hold a RESERVED lock on a file at
89** any time. Other processes may hold and obtain new SHARED locks.
90** PENDING: A single process may hold a PENDING lock on a file at
91** any one time. Existing SHARED locks may persist, but no new
92** SHARED locks may be obtained by other processes.
93** EXCLUSIVE: An EXCLUSIVE lock precludes all other locks.
94**
95** PENDING_LOCK may not be passed directly to sqlite3OsLock(). Instead, a
96** process that requests an EXCLUSIVE lock may actually obtain a PENDING
97** lock. This can be upgraded to an EXCLUSIVE lock by a subsequent call to
danielk19779a1d0ab2004-06-01 14:09:28 +000098** sqlite3OsLock().
99*/
danielk19779eed5052004-06-04 10:38:30 +0000100#define NO_LOCK 0
danielk19779a1d0ab2004-06-01 14:09:28 +0000101#define SHARED_LOCK 1
102#define RESERVED_LOCK 2
103#define PENDING_LOCK 3
104#define EXCLUSIVE_LOCK 4
danielk19776622cce2004-05-20 11:00:52 +0000105
drh2ac3ee92004-06-07 16:27:46 +0000106/*
drh1f595712004-06-15 01:40:29 +0000107** File Locking Notes: (Mostly about windows but also some info for Unix)
drh2ac3ee92004-06-07 16:27:46 +0000108**
109** We cannot use LockFileEx() or UnlockFileEx() on Win95/98/ME because
110** those functions are not available. So we use only LockFile() and
111** UnlockFile().
112**
113** LockFile() prevents not just writing but also reading by other processes.
drh2ac3ee92004-06-07 16:27:46 +0000114** A SHARED_LOCK is obtained by locking a single randomly-chosen
115** byte out of a specific range of bytes. The lock byte is obtained at
116** random so two separate readers can probably access the file at the
117** same time, unless they are unlucky and choose the same lock byte.
118** An EXCLUSIVE_LOCK is obtained by locking all bytes in the range.
119** There can only be one writer. A RESERVED_LOCK is obtained by locking
120** a single byte of the file that is designated as the reserved lock byte.
121** A PENDING_LOCK is obtained by locking a designated byte different from
122** the RESERVED_LOCK byte.
123**
124** On WinNT/2K/XP systems, LockFileEx() and UnlockFileEx() are available,
125** which means we can use reader/writer locks. When reader/writer locks
126** are used, the lock is placed on the same range of bytes that is used
127** for probabilistic locking in Win95/98/ME. Hence, the locking scheme
128** will support two or more Win95 readers or two or more WinNT readers.
129** But a single Win95 reader will lock out all WinNT readers and a single
130** WinNT reader will lock out all other Win95 readers.
131**
132** The following #defines specify the range of bytes used for locking.
133** SHARED_SIZE is the number of bytes available in the pool from which
134** a random byte is selected for a shared lock. The pool of bytes for
135** shared locks begins at SHARED_FIRST.
136**
137** These #defines are available in os.h so that Unix can use the same
138** byte ranges for locking. This leaves open the possiblity of having
139** clients on win95, winNT, and unix all talking to the same shared file
drh1f595712004-06-15 01:40:29 +0000140** and all locking correctly. To do so would require that samba (or whatever
141** tool is being used for file sharing) implements locks correctly between
142** windows and unix. I'm guessing that isn't likely to happen, but by
143** using the same locking range we are at least open to the possibility.
drh2ac3ee92004-06-07 16:27:46 +0000144**
145** Locking in windows is manditory. For this reason, we cannot store
146** actual data in the bytes used for locking. The pager never allocates
drh1f595712004-06-15 01:40:29 +0000147** the pages involved in locking therefore. SHARED_SIZE is selected so
148** that all locks will fit on a single page even at the minimum page size.
149** PENDING_BYTE defines the beginning of the locks. By default PENDING_BYTE
150** is set high so that we don't have to allocate an unused page except
151** for very large databases. But one should test the page skipping logic
152** by setting PENDING_BYTE low and running the entire regression suite.
153**
154** Changing the value of PENDING_BYTE results in a subtly incompatible
155** file format. Depending on how it is changed, you might not notice
156** the incompatibility right away, even running a full regression test.
157** The default location of PENDING_BYTE is the first byte past the
158** 1GB boundary.
159**
drh2ac3ee92004-06-07 16:27:46 +0000160*/
drh1f595712004-06-15 01:40:29 +0000161#define PENDING_BYTE 0x40000000 /* First byte past the 1GB boundary */
danielk1977599fcba2004-11-08 07:13:13 +0000162/* #define PENDING_BYTE 0x5400 // Page 22 - for testing */
drh1f595712004-06-15 01:40:29 +0000163#define RESERVED_BYTE (PENDING_BYTE+1)
164#define SHARED_FIRST (PENDING_BYTE+2)
165#define SHARED_SIZE 510
drh2ac3ee92004-06-07 16:27:46 +0000166
167
danielk19774adee202004-05-08 08:23:19 +0000168int sqlite3OsDelete(const char*);
169int sqlite3OsFileExists(const char*);
danielk19774adee202004-05-08 08:23:19 +0000170int sqlite3OsOpenReadWrite(const char*, OsFile*, int*);
171int sqlite3OsOpenExclusive(const char*, OsFile*, int);
172int sqlite3OsOpenReadOnly(const char*, OsFile*);
173int sqlite3OsOpenDirectory(const char*, OsFile*);
danielk1977962398d2004-06-14 09:35:16 +0000174int sqlite3OsSyncDirectory(const char*);
danielk19774adee202004-05-08 08:23:19 +0000175int sqlite3OsTempFileName(char*);
tpoindex9a09a3c2004-12-20 19:01:32 +0000176int sqlite3OsIsDirWritable(char*);
danielk19774adee202004-05-08 08:23:19 +0000177int sqlite3OsClose(OsFile*);
178int sqlite3OsRead(OsFile*, void*, int amt);
179int sqlite3OsWrite(OsFile*, const void*, int amt);
drheb206252004-10-01 02:00:31 +0000180int sqlite3OsSeek(OsFile*, i64 offset);
danielk19774adee202004-05-08 08:23:19 +0000181int sqlite3OsSync(OsFile*);
drheb206252004-10-01 02:00:31 +0000182int sqlite3OsTruncate(OsFile*, i64 size);
183int sqlite3OsFileSize(OsFile*, i64 *pSize);
danielk19774adee202004-05-08 08:23:19 +0000184int sqlite3OsRandomSeed(char*);
185int sqlite3OsSleep(int ms);
186int sqlite3OsCurrentTime(double*);
drhbf9a7e42004-06-15 00:29:03 +0000187int sqlite3OsFileModTime(OsFile*, double*);
danielk19774adee202004-05-08 08:23:19 +0000188void sqlite3OsEnterMutex(void);
189void sqlite3OsLeaveMutex(void);
190char *sqlite3OsFullPathname(const char*);
danielk19779a1d0ab2004-06-01 14:09:28 +0000191int sqlite3OsLock(OsFile*, int);
drha6abd042004-06-09 17:37:22 +0000192int sqlite3OsUnlock(OsFile*, int);
193int sqlite3OsCheckReservedLock(OsFile *id);
drhe3c41372001-09-17 20:25:58 +0000194
drhe3c41372001-09-17 20:25:58 +0000195#endif /* _SQLITE_OS_H_ */