xref: /sqlite-3.40.0/src/os.h (revision ef5ecb41)
1 /*
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 
20 /*
21 ** 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)
25 */
26 #ifndef OS_UNIX
27 # ifndef OS_WIN
28 #  ifndef OS_MAC
29 #    if defined(__MACOS__)
30 #      define OS_MAC 1
31 #      define OS_WIN 0
32 #      define OS_UNIX 0
33 #    elif defined(_WIN32) || defined(WIN32) || defined(__CYGWIN__) || defined(__MINGW32__) || defined(__BORLANDC__)
34 #      define OS_MAC 0
35 #      define OS_WIN 1
36 #      define OS_UNIX 0
37 #    else
38 #      define OS_MAC 0
39 #      define OS_WIN 0
40 #      define OS_UNIX 1
41 #    endif
42 #  else
43 #    define OS_WIN 0
44 #    define OS_UNIX 0
45 #  endif
46 # else
47 #  define OS_MAC 0
48 #  define OS_UNIX 0
49 # endif
50 #else
51 # define OS_MAC 0
52 # ifndef OS_WIN
53 #  define OS_WIN 0
54 # endif
55 #endif
56 
57 /*
58 ** Invoke the appropriate operating-system specific header file.
59 */
60 #if OS_UNIX
61 # include "os_unix.h"
62 #endif
63 #if OS_WIN
64 # include "os_win.h"
65 #endif
66 #if OS_MAC
67 # include "os_mac.h"
68 #endif
69 
70 /*
71 ** 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.
78 */
79 #ifndef TEMP_FILE_PREFIX
80 # define TEMP_FILE_PREFIX "sqlite_"
81 #endif
82 
83 /*
84 ** The following values may be passed as the second argument to
85 ** sqlite3OsLock().
86 */
87 #define NO_LOCK         0
88 #define SHARED_LOCK     1
89 #define RESERVED_LOCK   2
90 #define PENDING_LOCK    3
91 #define EXCLUSIVE_LOCK  4
92 
93 /*
94 ** Windows file locking notes:
95 **
96 ** We cannot use LockFileEx() or UnlockFileEx() on Win95/98/ME because
97 ** those functions are not available.  So we use only LockFile() and
98 ** UnlockFile().
99 **
100 ** LockFile() prevents not just writing but also reading by other processes.
101 ** (This is a design error on the part of Windows, but there is nothing
102 ** we can do about that.)  So the region used for locking is at the
103 ** end of the file where it is unlikely to ever interfere with an
104 ** actual read attempt.
105 **
106 ** A SHARED_LOCK is obtained by locking a single randomly-chosen
107 ** byte out of a specific range of bytes. The lock byte is obtained at
108 ** random so two separate readers can probably access the file at the
109 ** same time, unless they are unlucky and choose the same lock byte.
110 ** An EXCLUSIVE_LOCK is obtained by locking all bytes in the range.
111 ** There can only be one writer.  A RESERVED_LOCK is obtained by locking
112 ** a single byte of the file that is designated as the reserved lock byte.
113 ** A PENDING_LOCK is obtained by locking a designated byte different from
114 ** the RESERVED_LOCK byte.
115 **
116 ** On WinNT/2K/XP systems, LockFileEx() and UnlockFileEx() are available,
117 ** which means we can use reader/writer locks.  When reader/writer locks
118 ** are used, the lock is placed on the same range of bytes that is used
119 ** for probabilistic locking in Win95/98/ME.  Hence, the locking scheme
120 ** will support two or more Win95 readers or two or more WinNT readers.
121 ** But a single Win95 reader will lock out all WinNT readers and a single
122 ** WinNT reader will lock out all other Win95 readers.
123 **
124 ** The following #defines specify the range of bytes used for locking.
125 ** SHARED_SIZE is the number of bytes available in the pool from which
126 ** a random byte is selected for a shared lock.  The pool of bytes for
127 ** shared locks begins at SHARED_FIRST.
128 **
129 ** These #defines are available in os.h so that Unix can use the same
130 ** byte ranges for locking.  This leaves open the possiblity of having
131 ** clients on win95, winNT, and unix all talking to the same shared file
132 ** and all locking correctly.
133 **
134 ** Locking in windows is manditory.  For this reason, we cannot store
135 ** actual data in the bytes used for locking.  The pager never allocates
136 ** the pages involved in locking therefore.
137 */
138 #define SHARED_SIZE       10238
139 #define SHARED_FIRST      (0x3fffffff - (SHARED_SIZE - 1))
140 #define RESERVED_BYTE     (SHARED_FIRST - 1)
141 #define PENDING_BYTE      (RESERVED_BYTE - 1)
142 
143 
144 int sqlite3OsDelete(const char*);
145 int sqlite3OsFileExists(const char*);
146 int sqliteOsFileRename(const char*, const char*);
147 int sqlite3OsOpenReadWrite(const char*, OsFile*, int*);
148 int sqlite3OsOpenExclusive(const char*, OsFile*, int);
149 int sqlite3OsOpenReadOnly(const char*, OsFile*);
150 int sqlite3OsOpenDirectory(const char*, OsFile*);
151 int sqlite3OsTempFileName(char*);
152 int sqlite3OsClose(OsFile*);
153 int sqlite3OsRead(OsFile*, void*, int amt);
154 int sqlite3OsWrite(OsFile*, const void*, int amt);
155 int sqlite3OsSeek(OsFile*, off_t offset);
156 int sqlite3OsSync(OsFile*);
157 int sqlite3OsTruncate(OsFile*, off_t size);
158 int sqlite3OsFileSize(OsFile*, off_t *pSize);
159 int sqlite3OsRandomSeed(char*);
160 int sqlite3OsSleep(int ms);
161 int sqlite3OsCurrentTime(double*);
162 void sqlite3OsEnterMutex(void);
163 void sqlite3OsLeaveMutex(void);
164 char *sqlite3OsFullPathname(const char*);
165 int sqlite3OsLock(OsFile*, int);
166 int sqlite3OsUnlock(OsFile*, int);
167 int sqlite3OsCheckReservedLock(OsFile *id);
168 
169 #endif /* _SQLITE_OS_H_ */
170