os.h 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284
  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. ** This header file is #include-ed by sqliteInt.h and thus ends up
  18. ** being included by every source file.
  19. */
  20. #ifndef _SQLITE_OS_H_
  21. #define _SQLITE_OS_H_
  22. /*
  23. ** Figure out if we are dealing with Unix, Windows, or some other
  24. ** operating system. After the following block of preprocess macros,
  25. ** all of SQLITE_OS_UNIX, SQLITE_OS_WIN, and SQLITE_OS_OTHER
  26. ** will defined to either 1 or 0. One of the four will be 1. The other
  27. ** three will be 0.
  28. */
  29. #if defined(SQLITE_OS_OTHER)
  30. # if SQLITE_OS_OTHER==1
  31. # undef SQLITE_OS_UNIX
  32. # define SQLITE_OS_UNIX 0
  33. # undef SQLITE_OS_WIN
  34. # define SQLITE_OS_WIN 0
  35. # undef SQLITE_OS_RTTHREAD
  36. # define SQLITE_OS_RTTHREAD 1
  37. # else
  38. # undef SQLITE_OS_OTHER
  39. # endif
  40. #endif
  41. #if !defined(SQLITE_OS_UNIX) && !defined(SQLITE_OS_OTHER)
  42. # define SQLITE_OS_OTHER 0
  43. # ifndef SQLITE_OS_WIN
  44. # if defined(_WIN32) || defined(WIN32) || defined(__CYGWIN__) || defined(__MINGW32__) || defined(__BORLANDC__)
  45. # define SQLITE_OS_WIN 1
  46. # define SQLITE_OS_UNIX 0
  47. # else
  48. # define SQLITE_OS_WIN 0
  49. # define SQLITE_OS_UNIX 1
  50. # endif
  51. # else
  52. # define SQLITE_OS_UNIX 0
  53. # endif
  54. #else
  55. # ifndef SQLITE_OS_WIN
  56. # define SQLITE_OS_WIN 0
  57. # endif
  58. #endif
  59. #if SQLITE_OS_WIN
  60. # include <windows.h>
  61. #endif
  62. /*
  63. ** Determine if we are dealing with Windows NT.
  64. **
  65. ** We ought to be able to determine if we are compiling for win98 or winNT
  66. ** using the _WIN32_WINNT macro as follows:
  67. **
  68. ** #if defined(_WIN32_WINNT)
  69. ** # define SQLITE_OS_WINNT 1
  70. ** #else
  71. ** # define SQLITE_OS_WINNT 0
  72. ** #endif
  73. **
  74. ** However, vs2005 does not set _WIN32_WINNT by default, as it ought to,
  75. ** so the above test does not work. We'll just assume that everything is
  76. ** winNT unless the programmer explicitly says otherwise by setting
  77. ** SQLITE_OS_WINNT to 0.
  78. */
  79. #if SQLITE_OS_WIN && !defined(SQLITE_OS_WINNT)
  80. # define SQLITE_OS_WINNT 1
  81. #endif
  82. /*
  83. ** Determine if we are dealing with WindowsCE - which has a much
  84. ** reduced API.
  85. */
  86. #if defined(_WIN32_WCE)
  87. # define SQLITE_OS_WINCE 1
  88. #else
  89. # define SQLITE_OS_WINCE 0
  90. #endif
  91. /*
  92. ** Determine if we are dealing with WinRT, which provides only a subset of
  93. ** the full Win32 API.
  94. */
  95. #if !defined(SQLITE_OS_WINRT)
  96. # define SQLITE_OS_WINRT 0
  97. #endif
  98. /* If the SET_FULLSYNC macro is not defined above, then make it
  99. ** a no-op
  100. */
  101. #ifndef SET_FULLSYNC
  102. # define SET_FULLSYNC(x,y)
  103. #endif
  104. /*
  105. ** The default size of a disk sector
  106. */
  107. #ifndef SQLITE_DEFAULT_SECTOR_SIZE
  108. # define SQLITE_DEFAULT_SECTOR_SIZE 4096
  109. #endif
  110. /*
  111. ** Temporary files are named starting with this prefix followed by 16 random
  112. ** alphanumeric characters, and no file extension. They are stored in the
  113. ** OS's standard temporary file directory, and are deleted prior to exit.
  114. ** If sqlite is being embedded in another program, you may wish to change the
  115. ** prefix to reflect your program's name, so that if your program exits
  116. ** prematurely, old temporary files can be easily identified. This can be done
  117. ** using -DSQLITE_TEMP_FILE_PREFIX=myprefix_ on the compiler command line.
  118. **
  119. ** 2006-10-31: The default prefix used to be "sqlite_". But then
  120. ** Mcafee started using SQLite in their anti-virus product and it
  121. ** started putting files with the "sqlite" name in the c:/temp folder.
  122. ** This annoyed many windows users. Those users would then do a
  123. ** Google search for "sqlite", find the telephone numbers of the
  124. ** developers and call to wake them up at night and complain.
  125. ** For this reason, the default name prefix is changed to be "sqlite"
  126. ** spelled backwards. So the temp files are still identified, but
  127. ** anybody smart enough to figure out the code is also likely smart
  128. ** enough to know that calling the developer will not help get rid
  129. ** of the file.
  130. */
  131. #ifndef SQLITE_TEMP_FILE_PREFIX
  132. # define SQLITE_TEMP_FILE_PREFIX "etilqs_"
  133. #endif
  134. /*
  135. ** The following values may be passed as the second argument to
  136. ** sqlite3OsLock(). The various locks exhibit the following semantics:
  137. **
  138. ** SHARED: Any number of processes may hold a SHARED lock simultaneously.
  139. ** RESERVED: A single process may hold a RESERVED lock on a file at
  140. ** any time. Other processes may hold and obtain new SHARED locks.
  141. ** PENDING: A single process may hold a PENDING lock on a file at
  142. ** any one time. Existing SHARED locks may persist, but no new
  143. ** SHARED locks may be obtained by other processes.
  144. ** EXCLUSIVE: An EXCLUSIVE lock precludes all other locks.
  145. **
  146. ** PENDING_LOCK may not be passed directly to sqlite3OsLock(). Instead, a
  147. ** process that requests an EXCLUSIVE lock may actually obtain a PENDING
  148. ** lock. This can be upgraded to an EXCLUSIVE lock by a subsequent call to
  149. ** sqlite3OsLock().
  150. */
  151. #define NO_LOCK 0
  152. #define SHARED_LOCK 1
  153. #define RESERVED_LOCK 2
  154. #define PENDING_LOCK 3
  155. #define EXCLUSIVE_LOCK 4
  156. /*
  157. ** File Locking Notes: (Mostly about windows but also some info for Unix)
  158. **
  159. ** We cannot use LockFileEx() or UnlockFileEx() on Win95/98/ME because
  160. ** those functions are not available. So we use only LockFile() and
  161. ** UnlockFile().
  162. **
  163. ** LockFile() prevents not just writing but also reading by other processes.
  164. ** A SHARED_LOCK is obtained by locking a single randomly-chosen
  165. ** byte out of a specific range of bytes. The lock byte is obtained at
  166. ** random so two separate readers can probably access the file at the
  167. ** same time, unless they are unlucky and choose the same lock byte.
  168. ** An EXCLUSIVE_LOCK is obtained by locking all bytes in the range.
  169. ** There can only be one writer. A RESERVED_LOCK is obtained by locking
  170. ** a single byte of the file that is designated as the reserved lock byte.
  171. ** A PENDING_LOCK is obtained by locking a designated byte different from
  172. ** the RESERVED_LOCK byte.
  173. **
  174. ** On WinNT/2K/XP systems, LockFileEx() and UnlockFileEx() are available,
  175. ** which means we can use reader/writer locks. When reader/writer locks
  176. ** are used, the lock is placed on the same range of bytes that is used
  177. ** for probabilistic locking in Win95/98/ME. Hence, the locking scheme
  178. ** will support two or more Win95 readers or two or more WinNT readers.
  179. ** But a single Win95 reader will lock out all WinNT readers and a single
  180. ** WinNT reader will lock out all other Win95 readers.
  181. **
  182. ** The following #defines specify the range of bytes used for locking.
  183. ** SHARED_SIZE is the number of bytes available in the pool from which
  184. ** a random byte is selected for a shared lock. The pool of bytes for
  185. ** shared locks begins at SHARED_FIRST.
  186. **
  187. ** The same locking strategy and
  188. ** byte ranges are used for Unix. This leaves open the possiblity of having
  189. ** clients on win95, winNT, and unix all talking to the same shared file
  190. ** and all locking correctly. To do so would require that samba (or whatever
  191. ** tool is being used for file sharing) implements locks correctly between
  192. ** windows and unix. I'm guessing that isn't likely to happen, but by
  193. ** using the same locking range we are at least open to the possibility.
  194. **
  195. ** Locking in windows is manditory. For this reason, we cannot store
  196. ** actual data in the bytes used for locking. The pager never allocates
  197. ** the pages involved in locking therefore. SHARED_SIZE is selected so
  198. ** that all locks will fit on a single page even at the minimum page size.
  199. ** PENDING_BYTE defines the beginning of the locks. By default PENDING_BYTE
  200. ** is set high so that we don't have to allocate an unused page except
  201. ** for very large databases. But one should test the page skipping logic
  202. ** by setting PENDING_BYTE low and running the entire regression suite.
  203. **
  204. ** Changing the value of PENDING_BYTE results in a subtly incompatible
  205. ** file format. Depending on how it is changed, you might not notice
  206. ** the incompatibility right away, even running a full regression test.
  207. ** The default location of PENDING_BYTE is the first byte past the
  208. ** 1GB boundary.
  209. **
  210. */
  211. #ifdef SQLITE_OMIT_WSD
  212. # define PENDING_BYTE (0x40000000)
  213. #else
  214. # define PENDING_BYTE sqlite3PendingByte
  215. #endif
  216. #define RESERVED_BYTE (PENDING_BYTE+1)
  217. #define SHARED_FIRST (PENDING_BYTE+2)
  218. #define SHARED_SIZE 510
  219. /*
  220. ** Wrapper around OS specific sqlite3_os_init() function.
  221. */
  222. int sqlite3OsInit(void);
  223. /*
  224. ** Functions for accessing sqlite3_file methods
  225. */
  226. int sqlite3OsClose(sqlite3_file*);
  227. int sqlite3OsRead(sqlite3_file*, void*, int amt, i64 offset);
  228. int sqlite3OsWrite(sqlite3_file*, const void*, int amt, i64 offset);
  229. int sqlite3OsTruncate(sqlite3_file*, i64 size);
  230. int sqlite3OsSync(sqlite3_file*, int);
  231. int sqlite3OsFileSize(sqlite3_file*, i64 *pSize);
  232. int sqlite3OsLock(sqlite3_file*, int);
  233. int sqlite3OsUnlock(sqlite3_file*, int);
  234. int sqlite3OsCheckReservedLock(sqlite3_file *id, int *pResOut);
  235. int sqlite3OsFileControl(sqlite3_file*,int,void*);
  236. void sqlite3OsFileControlHint(sqlite3_file*,int,void*);
  237. #define SQLITE_FCNTL_DB_UNCHANGED 0xca093fa0
  238. int sqlite3OsSectorSize(sqlite3_file *id);
  239. int sqlite3OsDeviceCharacteristics(sqlite3_file *id);
  240. int sqlite3OsShmMap(sqlite3_file *,int,int,int,void volatile **);
  241. int sqlite3OsShmLock(sqlite3_file *id, int, int, int);
  242. void sqlite3OsShmBarrier(sqlite3_file *id);
  243. int sqlite3OsShmUnmap(sqlite3_file *id, int);
  244. int sqlite3OsFetch(sqlite3_file *id, i64, int, void **);
  245. int sqlite3OsUnfetch(sqlite3_file *, i64, void *);
  246. /*
  247. ** Functions for accessing sqlite3_vfs methods
  248. */
  249. int sqlite3OsOpen(sqlite3_vfs *, const char *, sqlite3_file*, int, int *);
  250. int sqlite3OsDelete(sqlite3_vfs *, const char *, int);
  251. int sqlite3OsAccess(sqlite3_vfs *, const char *, int, int *pResOut);
  252. int sqlite3OsFullPathname(sqlite3_vfs *, const char *, int, char *);
  253. #ifndef SQLITE_OMIT_LOAD_EXTENSION
  254. void *sqlite3OsDlOpen(sqlite3_vfs *, const char *);
  255. void sqlite3OsDlError(sqlite3_vfs *, int, char *);
  256. void (*sqlite3OsDlSym(sqlite3_vfs *, void *, const char *))(void);
  257. void sqlite3OsDlClose(sqlite3_vfs *, void *);
  258. #endif /* SQLITE_OMIT_LOAD_EXTENSION */
  259. int sqlite3OsRandomness(sqlite3_vfs *, int, char *);
  260. int sqlite3OsSleep(sqlite3_vfs *, int);
  261. int sqlite3OsCurrentTimeInt64(sqlite3_vfs *, sqlite3_int64*);
  262. /*
  263. ** Convenience functions for opening and closing files using
  264. ** sqlite3_malloc() to obtain space for the file-handle structure.
  265. */
  266. int sqlite3OsOpenMalloc(sqlite3_vfs *, const char *, sqlite3_file **, int,int*);
  267. int sqlite3OsCloseFree(sqlite3_file *);
  268. #endif /* _SQLITE_OS_H_ */