os.h 10 KB

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