Merge branch 'master' into prerelease
[sqlcipher.git] / src / os.h
blob1a8eff32981a82252781c170a48838b9b1df2ea1
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.
11 ******************************************************************************
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.
17 ** This header file is #include-ed by sqliteInt.h and thus ends up
18 ** being included by every source file.
20 #ifndef _SQLITE_OS_H_
21 #define _SQLITE_OS_H_
24 ** Attempt to automatically detect the operating system and setup the
25 ** necessary pre-processor macros for it.
27 #include "os_setup.h"
29 /* If the SET_FULLSYNC macro is not defined above, then make it
30 ** a no-op
32 #ifndef SET_FULLSYNC
33 # define SET_FULLSYNC(x,y)
34 #endif
36 /* Maximum pathname length. Note: FILENAME_MAX defined by stdio.h
38 #ifndef SQLITE_MAX_PATHLEN
39 # define SQLITE_MAX_PATHLEN FILENAME_MAX
40 #endif
43 ** The default size of a disk sector
45 #ifndef SQLITE_DEFAULT_SECTOR_SIZE
46 # define SQLITE_DEFAULT_SECTOR_SIZE 4096
47 #endif
50 ** Temporary files are named starting with this prefix followed by 16 random
51 ** alphanumeric characters, and no file extension. They are stored in the
52 ** OS's standard temporary file directory, and are deleted prior to exit.
53 ** If sqlite is being embedded in another program, you may wish to change the
54 ** prefix to reflect your program's name, so that if your program exits
55 ** prematurely, old temporary files can be easily identified. This can be done
56 ** using -DSQLITE_TEMP_FILE_PREFIX=myprefix_ on the compiler command line.
58 ** 2006-10-31: The default prefix used to be "sqlite_". But then
59 ** Mcafee started using SQLite in their anti-virus product and it
60 ** started putting files with the "sqlite" name in the c:/temp folder.
61 ** This annoyed many windows users. Those users would then do a
62 ** Google search for "sqlite", find the telephone numbers of the
63 ** developers and call to wake them up at night and complain.
64 ** For this reason, the default name prefix is changed to be "sqlite"
65 ** spelled backwards. So the temp files are still identified, but
66 ** anybody smart enough to figure out the code is also likely smart
67 ** enough to know that calling the developer will not help get rid
68 ** of the file.
70 #ifndef SQLITE_TEMP_FILE_PREFIX
71 # define SQLITE_TEMP_FILE_PREFIX "etilqs_"
72 #endif
75 ** The following values may be passed as the second argument to
76 ** sqlite3OsLock(). The various locks exhibit the following semantics:
78 ** SHARED: Any number of processes may hold a SHARED lock simultaneously.
79 ** RESERVED: A single process may hold a RESERVED lock on a file at
80 ** any time. Other processes may hold and obtain new SHARED locks.
81 ** PENDING: A single process may hold a PENDING lock on a file at
82 ** any one time. Existing SHARED locks may persist, but no new
83 ** SHARED locks may be obtained by other processes.
84 ** EXCLUSIVE: An EXCLUSIVE lock precludes all other locks.
86 ** PENDING_LOCK may not be passed directly to sqlite3OsLock(). Instead, a
87 ** process that requests an EXCLUSIVE lock may actually obtain a PENDING
88 ** lock. This can be upgraded to an EXCLUSIVE lock by a subsequent call to
89 ** sqlite3OsLock().
91 #define NO_LOCK 0
92 #define SHARED_LOCK 1
93 #define RESERVED_LOCK 2
94 #define PENDING_LOCK 3
95 #define EXCLUSIVE_LOCK 4
98 ** File Locking Notes: (Mostly about windows but also some info for Unix)
100 ** We cannot use LockFileEx() or UnlockFileEx() on Win95/98/ME because
101 ** those functions are not available. So we use only LockFile() and
102 ** UnlockFile().
104 ** LockFile() prevents not just writing but also reading by other processes.
105 ** A SHARED_LOCK is obtained by locking a single randomly-chosen
106 ** byte out of a specific range of bytes. The lock byte is obtained at
107 ** random so two separate readers can probably access the file at the
108 ** same time, unless they are unlucky and choose the same lock byte.
109 ** An EXCLUSIVE_LOCK is obtained by locking all bytes in the range.
110 ** There can only be one writer. A RESERVED_LOCK is obtained by locking
111 ** a single byte of the file that is designated as the reserved lock byte.
112 ** A PENDING_LOCK is obtained by locking a designated byte different from
113 ** the RESERVED_LOCK byte.
115 ** On WinNT/2K/XP systems, LockFileEx() and UnlockFileEx() are available,
116 ** which means we can use reader/writer locks. When reader/writer locks
117 ** are used, the lock is placed on the same range of bytes that is used
118 ** for probabilistic locking in Win95/98/ME. Hence, the locking scheme
119 ** will support two or more Win95 readers or two or more WinNT readers.
120 ** But a single Win95 reader will lock out all WinNT readers and a single
121 ** WinNT reader will lock out all other Win95 readers.
123 ** The following #defines specify the range of bytes used for locking.
124 ** SHARED_SIZE is the number of bytes available in the pool from which
125 ** a random byte is selected for a shared lock. The pool of bytes for
126 ** shared locks begins at SHARED_FIRST.
128 ** The same locking strategy and
129 ** byte ranges are used for Unix. This leaves open the possibility of having
130 ** clients on win95, winNT, and unix all talking to the same shared file
131 ** and all locking correctly. To do so would require that samba (or whatever
132 ** tool is being used for file sharing) implements locks correctly between
133 ** windows and unix. I'm guessing that isn't likely to happen, but by
134 ** using the same locking range we are at least open to the possibility.
136 ** Locking in windows is manditory. For this reason, we cannot store
137 ** actual data in the bytes used for locking. The pager never allocates
138 ** the pages involved in locking therefore. SHARED_SIZE is selected so
139 ** that all locks will fit on a single page even at the minimum page size.
140 ** PENDING_BYTE defines the beginning of the locks. By default PENDING_BYTE
141 ** is set high so that we don't have to allocate an unused page except
142 ** for very large databases. But one should test the page skipping logic
143 ** by setting PENDING_BYTE low and running the entire regression suite.
145 ** Changing the value of PENDING_BYTE results in a subtly incompatible
146 ** file format. Depending on how it is changed, you might not notice
147 ** the incompatibility right away, even running a full regression test.
148 ** The default location of PENDING_BYTE is the first byte past the
149 ** 1GB boundary.
152 #ifdef SQLITE_OMIT_WSD
153 # define PENDING_BYTE (0x40000000)
154 #else
155 # define PENDING_BYTE sqlite3PendingByte
156 #endif
157 #define RESERVED_BYTE (PENDING_BYTE+1)
158 #define SHARED_FIRST (PENDING_BYTE+2)
159 #define SHARED_SIZE 510
162 ** Wrapper around OS specific sqlite3_os_init() function.
164 int sqlite3OsInit(void);
167 ** Functions for accessing sqlite3_file methods
169 void sqlite3OsClose(sqlite3_file*);
170 int sqlite3OsRead(sqlite3_file*, void*, int amt, i64 offset);
171 int sqlite3OsWrite(sqlite3_file*, const void*, int amt, i64 offset);
172 int sqlite3OsTruncate(sqlite3_file*, i64 size);
173 int sqlite3OsSync(sqlite3_file*, int);
174 int sqlite3OsFileSize(sqlite3_file*, i64 *pSize);
175 int sqlite3OsLock(sqlite3_file*, int);
176 int sqlite3OsUnlock(sqlite3_file*, int);
177 int sqlite3OsCheckReservedLock(sqlite3_file *id, int *pResOut);
178 int sqlite3OsFileControl(sqlite3_file*,int,void*);
179 void sqlite3OsFileControlHint(sqlite3_file*,int,void*);
180 #define SQLITE_FCNTL_DB_UNCHANGED 0xca093fa0
181 int sqlite3OsSectorSize(sqlite3_file *id);
182 int sqlite3OsDeviceCharacteristics(sqlite3_file *id);
183 #ifndef SQLITE_OMIT_WAL
184 int sqlite3OsShmMap(sqlite3_file *,int,int,int,void volatile **);
185 int sqlite3OsShmLock(sqlite3_file *id, int, int, int);
186 void sqlite3OsShmBarrier(sqlite3_file *id);
187 int sqlite3OsShmUnmap(sqlite3_file *id, int);
188 #endif /* SQLITE_OMIT_WAL */
189 int sqlite3OsFetch(sqlite3_file *id, i64, int, void **);
190 int sqlite3OsUnfetch(sqlite3_file *, i64, void *);
194 ** Functions for accessing sqlite3_vfs methods
196 int sqlite3OsOpen(sqlite3_vfs *, const char *, sqlite3_file*, int, int *);
197 int sqlite3OsDelete(sqlite3_vfs *, const char *, int);
198 int sqlite3OsAccess(sqlite3_vfs *, const char *, int, int *pResOut);
199 int sqlite3OsFullPathname(sqlite3_vfs *, const char *, int, char *);
200 #ifndef SQLITE_OMIT_LOAD_EXTENSION
201 void *sqlite3OsDlOpen(sqlite3_vfs *, const char *);
202 void sqlite3OsDlError(sqlite3_vfs *, int, char *);
203 void (*sqlite3OsDlSym(sqlite3_vfs *, void *, const char *))(void);
204 void sqlite3OsDlClose(sqlite3_vfs *, void *);
205 #endif /* SQLITE_OMIT_LOAD_EXTENSION */
206 int sqlite3OsRandomness(sqlite3_vfs *, int, char *);
207 int sqlite3OsSleep(sqlite3_vfs *, int);
208 int sqlite3OsGetLastError(sqlite3_vfs*);
209 int sqlite3OsCurrentTimeInt64(sqlite3_vfs *, sqlite3_int64*);
212 ** Convenience functions for opening and closing files using
213 ** sqlite3_malloc() to obtain space for the file-handle structure.
215 int sqlite3OsOpenMalloc(sqlite3_vfs *, const char *, sqlite3_file **, int,int*);
216 void sqlite3OsCloseFree(sqlite3_file *);
218 #endif /* _SQLITE_OS_H_ */