os.c 8.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284
  1. /*
  2. ** 2005 November 29
  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 file contains OS interface code that is common to all
  14. ** architectures.
  15. */
  16. #define _SQLITE_OS_C_ 1
  17. #include "sqliteInt.h"
  18. #undef _SQLITE_OS_C_
  19. /*
  20. ** The default SQLite sqlite3_vfs implementations do not allocate
  21. ** memory (actually, os_unix.c allocates a small amount of memory
  22. ** from within OsOpen()), but some third-party implementations may.
  23. ** So we test the effects of a malloc() failing and the sqlite3OsXXX()
  24. ** function returning SQLITE_IOERR_NOMEM using the DO_OS_MALLOC_TEST macro.
  25. **
  26. ** The following functions are instrumented for malloc() failure
  27. ** testing:
  28. **
  29. ** sqlite3OsOpen()
  30. ** sqlite3OsRead()
  31. ** sqlite3OsWrite()
  32. ** sqlite3OsSync()
  33. ** sqlite3OsLock()
  34. **
  35. */
  36. #ifdef SQLITE_TEST
  37. #define DO_OS_MALLOC_TEST if (1) { \
  38. void *pTstAlloc = sqlite3_malloc(10); \
  39. if (!pTstAlloc) return SQLITE_IOERR_NOMEM; \
  40. sqlite3_free(pTstAlloc); \
  41. }
  42. #else
  43. #define DO_OS_MALLOC_TEST
  44. #endif
  45. /*
  46. ** The following routines are convenience wrappers around methods
  47. ** of the sqlite3_file object. This is mostly just syntactic sugar. All
  48. ** of this would be completely automatic if SQLite were coded using
  49. ** C++ instead of plain old C.
  50. */
  51. int sqlite3OsClose(sqlite3_file *pId){
  52. int rc = SQLITE_OK;
  53. if( pId->pMethods ){
  54. rc = pId->pMethods->xClose(pId);
  55. pId->pMethods = 0;
  56. }
  57. return rc;
  58. }
  59. int sqlite3OsRead(sqlite3_file *id, void *pBuf, int amt, i64 offset){
  60. DO_OS_MALLOC_TEST;
  61. return id->pMethods->xRead(id, pBuf, amt, offset);
  62. }
  63. int sqlite3OsWrite(sqlite3_file *id, const void *pBuf, int amt, i64 offset){
  64. DO_OS_MALLOC_TEST;
  65. return id->pMethods->xWrite(id, pBuf, amt, offset);
  66. }
  67. int sqlite3OsTruncate(sqlite3_file *id, i64 size){
  68. return id->pMethods->xTruncate(id, size);
  69. }
  70. int sqlite3OsSync(sqlite3_file *id, int flags){
  71. DO_OS_MALLOC_TEST;
  72. return id->pMethods->xSync(id, flags);
  73. }
  74. int sqlite3OsFileSize(sqlite3_file *id, i64 *pSize){
  75. return id->pMethods->xFileSize(id, pSize);
  76. }
  77. int sqlite3OsLock(sqlite3_file *id, int lockType){
  78. DO_OS_MALLOC_TEST;
  79. return id->pMethods->xLock(id, lockType);
  80. }
  81. int sqlite3OsUnlock(sqlite3_file *id, int lockType){
  82. return id->pMethods->xUnlock(id, lockType);
  83. }
  84. int sqlite3OsCheckReservedLock(sqlite3_file *id){
  85. return id->pMethods->xCheckReservedLock(id);
  86. }
  87. int sqlite3OsFileControl(sqlite3_file *id, int op, void *pArg){
  88. return id->pMethods->xFileControl(id,op,pArg);
  89. }
  90. #ifdef SQLITE_TEST
  91. /* The following two variables are used to override the values returned
  92. ** by the xSectorSize() and xDeviceCharacteristics() vfs methods for
  93. ** testing purposes. They are usually set by a test command implemented
  94. ** in test6.c.
  95. */
  96. int sqlite3_test_sector_size = 0;
  97. int sqlite3_test_device_characteristics = 0;
  98. int sqlite3OsDeviceCharacteristics(sqlite3_file *id){
  99. int dc = id->pMethods->xDeviceCharacteristics(id);
  100. return dc | sqlite3_test_device_characteristics;
  101. }
  102. int sqlite3OsSectorSize(sqlite3_file *id){
  103. if( sqlite3_test_sector_size==0 ){
  104. int (*xSectorSize)(sqlite3_file*) = id->pMethods->xSectorSize;
  105. return (xSectorSize ? xSectorSize(id) : SQLITE_DEFAULT_SECTOR_SIZE);
  106. }
  107. return sqlite3_test_sector_size;
  108. }
  109. #else
  110. int sqlite3OsSectorSize(sqlite3_file *id){
  111. int (*xSectorSize)(sqlite3_file*) = id->pMethods->xSectorSize;
  112. return (xSectorSize ? xSectorSize(id) : SQLITE_DEFAULT_SECTOR_SIZE);
  113. }
  114. int sqlite3OsDeviceCharacteristics(sqlite3_file *id){
  115. return id->pMethods->xDeviceCharacteristics(id);
  116. }
  117. #endif
  118. /*
  119. ** The next group of routines are convenience wrappers around the
  120. ** VFS methods.
  121. */
  122. int sqlite3OsOpen(
  123. sqlite3_vfs *pVfs,
  124. const char *zPath,
  125. sqlite3_file *pFile,
  126. int flags,
  127. int *pFlagsOut
  128. ){
  129. DO_OS_MALLOC_TEST;
  130. return pVfs->xOpen(pVfs, zPath, pFile, flags, pFlagsOut);
  131. }
  132. int sqlite3OsDelete(sqlite3_vfs *pVfs, const char *zPath, int dirSync){
  133. return pVfs->xDelete(pVfs, zPath, dirSync);
  134. }
  135. int sqlite3OsAccess(sqlite3_vfs *pVfs, const char *zPath, int flags){
  136. return pVfs->xAccess(pVfs, zPath, flags);
  137. }
  138. int sqlite3OsGetTempname(sqlite3_vfs *pVfs, int nBufOut, char *zBufOut){
  139. return pVfs->xGetTempname(pVfs, nBufOut, zBufOut);
  140. }
  141. int sqlite3OsFullPathname(
  142. sqlite3_vfs *pVfs,
  143. const char *zPath,
  144. int nPathOut,
  145. char *zPathOut
  146. ){
  147. return pVfs->xFullPathname(pVfs, zPath, nPathOut, zPathOut);
  148. }
  149. void *sqlite3OsDlOpen(sqlite3_vfs *pVfs, const char *zPath){
  150. return pVfs->xDlOpen(pVfs, zPath);
  151. }
  152. void sqlite3OsDlError(sqlite3_vfs *pVfs, int nByte, char *zBufOut){
  153. pVfs->xDlError(pVfs, nByte, zBufOut);
  154. }
  155. void *sqlite3OsDlSym(sqlite3_vfs *pVfs, void *pHandle, const char *zSymbol){
  156. return pVfs->xDlSym(pVfs, pHandle, zSymbol);
  157. }
  158. void sqlite3OsDlClose(sqlite3_vfs *pVfs, void *pHandle){
  159. pVfs->xDlClose(pVfs, pHandle);
  160. }
  161. int sqlite3OsRandomness(sqlite3_vfs *pVfs, int nByte, char *zBufOut){
  162. return pVfs->xRandomness(pVfs, nByte, zBufOut);
  163. }
  164. int sqlite3OsSleep(sqlite3_vfs *pVfs, int nMicro){
  165. return pVfs->xSleep(pVfs, nMicro);
  166. }
  167. int sqlite3OsCurrentTime(sqlite3_vfs *pVfs, double *pTimeOut){
  168. return pVfs->xCurrentTime(pVfs, pTimeOut);
  169. }
  170. int sqlite3OsOpenMalloc(
  171. sqlite3_vfs *pVfs,
  172. const char *zFile,
  173. sqlite3_file **ppFile,
  174. int flags,
  175. int *pOutFlags
  176. ){
  177. int rc = SQLITE_NOMEM;
  178. sqlite3_file *pFile;
  179. pFile = (sqlite3_file *)sqlite3_malloc(pVfs->szOsFile);
  180. if( pFile ){
  181. rc = sqlite3OsOpen(pVfs, zFile, pFile, flags, pOutFlags);
  182. if( rc!=SQLITE_OK ){
  183. sqlite3_free(pFile);
  184. }else{
  185. *ppFile = pFile;
  186. }
  187. }
  188. return rc;
  189. }
  190. int sqlite3OsCloseFree(sqlite3_file *pFile){
  191. int rc = SQLITE_OK;
  192. if( pFile ){
  193. rc = sqlite3OsClose(pFile);
  194. sqlite3_free(pFile);
  195. }
  196. return rc;
  197. }
  198. /*
  199. ** The list of all registered VFS implementations. This list is
  200. ** initialized to the single VFS returned by sqlite3OsDefaultVfs()
  201. ** upon the first call to sqlite3_vfs_find().
  202. */
  203. static sqlite3_vfs *vfsList = 0;
  204. /*
  205. ** Locate a VFS by name. If no name is given, simply return the
  206. ** first VFS on the list.
  207. */
  208. sqlite3_vfs *sqlite3_vfs_find(const char *zVfs){
  209. sqlite3_mutex *mutex = sqlite3_mutex_alloc(SQLITE_MUTEX_STATIC_MASTER);
  210. sqlite3_vfs *pVfs = 0;
  211. static int isInit = 0;
  212. sqlite3_mutex_enter(mutex);
  213. if( !isInit ){
  214. vfsList = sqlite3OsDefaultVfs();
  215. isInit = 1;
  216. }
  217. for(pVfs = vfsList; pVfs; pVfs=pVfs->pNext){
  218. if( zVfs==0 ) break;
  219. if( strcmp(zVfs, pVfs->zName)==0 ) break;
  220. }
  221. sqlite3_mutex_leave(mutex);
  222. return pVfs;
  223. }
  224. /*
  225. ** Unlink a VFS from the linked list
  226. */
  227. static void vfsUnlink(sqlite3_vfs *pVfs){
  228. assert( sqlite3_mutex_held(sqlite3_mutex_alloc(SQLITE_MUTEX_STATIC_MASTER)) );
  229. if( pVfs==0 ){
  230. /* No-op */
  231. }else if( vfsList==pVfs ){
  232. vfsList = pVfs->pNext;
  233. }else if( vfsList ){
  234. sqlite3_vfs *p = vfsList;
  235. while( p->pNext && p->pNext!=pVfs ){
  236. p = p->pNext;
  237. }
  238. if( p->pNext==pVfs ){
  239. p->pNext = pVfs->pNext;
  240. }
  241. }
  242. }
  243. /*
  244. ** Register a VFS with the system. It is harmless to register the same
  245. ** VFS multiple times. The new VFS becomes the default if makeDflt is
  246. ** true.
  247. */
  248. int sqlite3_vfs_register(sqlite3_vfs *pVfs, int makeDflt){
  249. sqlite3_mutex *mutex = sqlite3_mutex_alloc(SQLITE_MUTEX_STATIC_MASTER);
  250. sqlite3_vfs_find(0); /* Make sure we are initialized */
  251. sqlite3_mutex_enter(mutex);
  252. vfsUnlink(pVfs);
  253. if( makeDflt || vfsList==0 ){
  254. pVfs->pNext = vfsList;
  255. vfsList = pVfs;
  256. }else{
  257. pVfs->pNext = vfsList->pNext;
  258. vfsList->pNext = pVfs;
  259. }
  260. assert(vfsList);
  261. sqlite3_mutex_leave(mutex);
  262. return SQLITE_OK;
  263. }
  264. /*
  265. ** Unregister a VFS so that it is no longer accessible.
  266. */
  267. int sqlite3_vfs_unregister(sqlite3_vfs *pVfs){
  268. sqlite3_mutex *mutex = sqlite3_mutex_alloc(SQLITE_MUTEX_STATIC_MASTER);
  269. sqlite3_mutex_enter(mutex);
  270. vfsUnlink(pVfs);
  271. sqlite3_mutex_leave(mutex);
  272. return SQLITE_OK;
  273. }