| OLD | NEW |
| 1 /* | 1 /* |
| 2 ** 2005 November 29 | 2 ** 2005 November 29 |
| 3 ** | 3 ** |
| 4 ** The author disclaims copyright to this source code. In place of | 4 ** The author disclaims copyright to this source code. In place of |
| 5 ** a legal notice, here is a blessing: | 5 ** a legal notice, here is a blessing: |
| 6 ** | 6 ** |
| 7 ** May you do good and not evil. | 7 ** May you do good and not evil. |
| 8 ** May you find forgiveness for yourself and forgive others. | 8 ** May you find forgiveness for yourself and forgive others. |
| 9 ** May you share freely, never taking more than you give. | 9 ** May you share freely, never taking more than you give. |
| 10 ** | 10 ** |
| 11 ****************************************************************************** | 11 ****************************************************************************** |
| 12 ** | 12 ** |
| 13 ** This file contains OS interface code that is common to all | 13 ** This file contains OS interface code that is common to all |
| 14 ** architectures. | 14 ** architectures. |
| 15 */ | 15 */ |
| 16 #define _SQLITE_OS_C_ 1 | 16 #define _SQLITE_OS_C_ 1 |
| 17 #include "sqliteInt.h" | 17 #include "sqliteInt.h" |
| 18 #undef _SQLITE_OS_C_ | 18 #undef _SQLITE_OS_C_ |
| 19 | 19 |
| 20 /* | 20 /* |
| 21 ** The default SQLite sqlite3_vfs implementations do not allocate | 21 ** The default SQLite sqlite3_vfs implementations do not allocate |
| 22 ** memory (actually, os_unix.c allocates a small amount of memory | 22 ** memory (actually, os_unix.c allocates a small amount of memory |
| 23 ** from within OsOpen()), but some third-party implementations may. | 23 ** from within OsOpen()), but some third-party implementations may. |
| 24 ** So we test the effects of a malloc() failing and the sqlite3OsXXX() | 24 ** So we test the effects of a malloc() failing and the sqlite3OsXXX() |
| 25 ** function returning SQLITE_IOERR_NOMEM using the DO_OS_MALLOC_TEST macro. | 25 ** function returning SQLITE_IOERR_NOMEM using the DO_OS_MALLOC_TEST macro. |
| 26 ** | 26 ** |
| 27 ** The following functions are instrumented for malloc() failure | 27 ** The following functions are instrumented for malloc() failure |
| 28 ** testing: | 28 ** testing: |
| 29 ** | 29 ** |
| 30 ** sqlite3OsOpen() | |
| 31 ** sqlite3OsRead() | 30 ** sqlite3OsRead() |
| 32 ** sqlite3OsWrite() | 31 ** sqlite3OsWrite() |
| 33 ** sqlite3OsSync() | 32 ** sqlite3OsSync() |
| 33 ** sqlite3OsFileSize() |
| 34 ** sqlite3OsLock() | 34 ** sqlite3OsLock() |
| 35 ** sqlite3OsCheckReservedLock() |
| 36 ** sqlite3OsFileControl() |
| 37 ** sqlite3OsShmMap() |
| 38 ** sqlite3OsOpen() |
| 39 ** sqlite3OsDelete() |
| 40 ** sqlite3OsAccess() |
| 41 ** sqlite3OsFullPathname() |
| 35 ** | 42 ** |
| 36 */ | 43 */ |
| 37 #if defined(SQLITE_TEST) | 44 #if defined(SQLITE_TEST) |
| 38 int sqlite3_memdebug_vfs_oom_test = 1; | 45 int sqlite3_memdebug_vfs_oom_test = 1; |
| 39 #define DO_OS_MALLOC_TEST(x) \ | 46 #define DO_OS_MALLOC_TEST(x) \ |
| 40 if (sqlite3_memdebug_vfs_oom_test && (!x || !sqlite3IsMemJournal(x))) { \ | 47 if (sqlite3_memdebug_vfs_oom_test && (!x || !sqlite3IsMemJournal(x))) { \ |
| 41 void *pTstAlloc = sqlite3Malloc(10); \ | 48 void *pTstAlloc = sqlite3Malloc(10); \ |
| 42 if (!pTstAlloc) return SQLITE_IOERR_NOMEM; \ | 49 if (!pTstAlloc) return SQLITE_IOERR_NOMEM; \ |
| 43 sqlite3_free(pTstAlloc); \ | 50 sqlite3_free(pTstAlloc); \ |
| 44 } | 51 } |
| (...skipping 38 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 83 DO_OS_MALLOC_TEST(id); | 90 DO_OS_MALLOC_TEST(id); |
| 84 return id->pMethods->xLock(id, lockType); | 91 return id->pMethods->xLock(id, lockType); |
| 85 } | 92 } |
| 86 int sqlite3OsUnlock(sqlite3_file *id, int lockType){ | 93 int sqlite3OsUnlock(sqlite3_file *id, int lockType){ |
| 87 return id->pMethods->xUnlock(id, lockType); | 94 return id->pMethods->xUnlock(id, lockType); |
| 88 } | 95 } |
| 89 int sqlite3OsCheckReservedLock(sqlite3_file *id, int *pResOut){ | 96 int sqlite3OsCheckReservedLock(sqlite3_file *id, int *pResOut){ |
| 90 DO_OS_MALLOC_TEST(id); | 97 DO_OS_MALLOC_TEST(id); |
| 91 return id->pMethods->xCheckReservedLock(id, pResOut); | 98 return id->pMethods->xCheckReservedLock(id, pResOut); |
| 92 } | 99 } |
| 100 |
| 101 /* |
| 102 ** Use sqlite3OsFileControl() when we are doing something that might fail |
| 103 ** and we need to know about the failures. Use sqlite3OsFileControlHint() |
| 104 ** when simply tossing information over the wall to the VFS and we do not |
| 105 ** really care if the VFS receives and understands the information since it |
| 106 ** is only a hint and can be safely ignored. The sqlite3OsFileControlHint() |
| 107 ** routine has no return value since the return value would be meaningless. |
| 108 */ |
| 93 int sqlite3OsFileControl(sqlite3_file *id, int op, void *pArg){ | 109 int sqlite3OsFileControl(sqlite3_file *id, int op, void *pArg){ |
| 110 #ifdef SQLITE_TEST |
| 111 if( op!=SQLITE_FCNTL_COMMIT_PHASETWO ){ |
| 112 /* Faults are not injected into COMMIT_PHASETWO because, assuming SQLite |
| 113 ** is using a regular VFS, it is called after the corresponding |
| 114 ** transaction has been committed. Injecting a fault at this point |
| 115 ** confuses the test scripts - the COMMIT comand returns SQLITE_NOMEM |
| 116 ** but the transaction is committed anyway. |
| 117 ** |
| 118 ** The core must call OsFileControl() though, not OsFileControlHint(), |
| 119 ** as if a custom VFS (e.g. zipvfs) returns an error here, it probably |
| 120 ** means the commit really has failed and an error should be returned |
| 121 ** to the user. */ |
| 122 DO_OS_MALLOC_TEST(id); |
| 123 } |
| 124 #endif |
| 94 return id->pMethods->xFileControl(id, op, pArg); | 125 return id->pMethods->xFileControl(id, op, pArg); |
| 95 } | 126 } |
| 127 void sqlite3OsFileControlHint(sqlite3_file *id, int op, void *pArg){ |
| 128 (void)id->pMethods->xFileControl(id, op, pArg); |
| 129 } |
| 130 |
| 96 int sqlite3OsSectorSize(sqlite3_file *id){ | 131 int sqlite3OsSectorSize(sqlite3_file *id){ |
| 97 int (*xSectorSize)(sqlite3_file*) = id->pMethods->xSectorSize; | 132 int (*xSectorSize)(sqlite3_file*) = id->pMethods->xSectorSize; |
| 98 return (xSectorSize ? xSectorSize(id) : SQLITE_DEFAULT_SECTOR_SIZE); | 133 return (xSectorSize ? xSectorSize(id) : SQLITE_DEFAULT_SECTOR_SIZE); |
| 99 } | 134 } |
| 100 int sqlite3OsDeviceCharacteristics(sqlite3_file *id){ | 135 int sqlite3OsDeviceCharacteristics(sqlite3_file *id){ |
| 101 return id->pMethods->xDeviceCharacteristics(id); | 136 return id->pMethods->xDeviceCharacteristics(id); |
| 102 } | 137 } |
| 103 int sqlite3OsShmLock(sqlite3_file *id, int offset, int n, int flags){ | 138 int sqlite3OsShmLock(sqlite3_file *id, int offset, int n, int flags){ |
| 104 return id->pMethods->xShmLock(id, offset, n, flags); | 139 return id->pMethods->xShmLock(id, offset, n, flags); |
| 105 } | 140 } |
| 106 void sqlite3OsShmBarrier(sqlite3_file *id){ | 141 void sqlite3OsShmBarrier(sqlite3_file *id){ |
| 107 id->pMethods->xShmBarrier(id); | 142 id->pMethods->xShmBarrier(id); |
| 108 } | 143 } |
| 109 int sqlite3OsShmUnmap(sqlite3_file *id, int deleteFlag){ | 144 int sqlite3OsShmUnmap(sqlite3_file *id, int deleteFlag){ |
| 110 return id->pMethods->xShmUnmap(id, deleteFlag); | 145 return id->pMethods->xShmUnmap(id, deleteFlag); |
| 111 } | 146 } |
| 112 int sqlite3OsShmMap( | 147 int sqlite3OsShmMap( |
| 113 sqlite3_file *id, /* Database file handle */ | 148 sqlite3_file *id, /* Database file handle */ |
| 114 int iPage, | 149 int iPage, |
| 115 int pgsz, | 150 int pgsz, |
| 116 int bExtend, /* True to extend file if necessary */ | 151 int bExtend, /* True to extend file if necessary */ |
| 117 void volatile **pp /* OUT: Pointer to mapping */ | 152 void volatile **pp /* OUT: Pointer to mapping */ |
| 118 ){ | 153 ){ |
| 154 DO_OS_MALLOC_TEST(id); |
| 119 return id->pMethods->xShmMap(id, iPage, pgsz, bExtend, pp); | 155 return id->pMethods->xShmMap(id, iPage, pgsz, bExtend, pp); |
| 120 } | 156 } |
| 121 | 157 |
| 158 #if SQLITE_MAX_MMAP_SIZE>0 |
| 159 /* The real implementation of xFetch and xUnfetch */ |
| 160 int sqlite3OsFetch(sqlite3_file *id, i64 iOff, int iAmt, void **pp){ |
| 161 DO_OS_MALLOC_TEST(id); |
| 162 return id->pMethods->xFetch(id, iOff, iAmt, pp); |
| 163 } |
| 164 int sqlite3OsUnfetch(sqlite3_file *id, i64 iOff, void *p){ |
| 165 return id->pMethods->xUnfetch(id, iOff, p); |
| 166 } |
| 167 #else |
| 168 /* No-op stubs to use when memory-mapped I/O is disabled */ |
| 169 int sqlite3OsFetch(sqlite3_file *id, i64 iOff, int iAmt, void **pp){ |
| 170 *pp = 0; |
| 171 return SQLITE_OK; |
| 172 } |
| 173 int sqlite3OsUnfetch(sqlite3_file *id, i64 iOff, void *p){ |
| 174 return SQLITE_OK; |
| 175 } |
| 176 #endif |
| 177 |
| 122 /* | 178 /* |
| 123 ** The next group of routines are convenience wrappers around the | 179 ** The next group of routines are convenience wrappers around the |
| 124 ** VFS methods. | 180 ** VFS methods. |
| 125 */ | 181 */ |
| 126 int sqlite3OsOpen( | 182 int sqlite3OsOpen( |
| 127 sqlite3_vfs *pVfs, | 183 sqlite3_vfs *pVfs, |
| 128 const char *zPath, | 184 const char *zPath, |
| 129 sqlite3_file *pFile, | 185 sqlite3_file *pFile, |
| 130 int flags, | 186 int flags, |
| 131 int *pFlagsOut | 187 int *pFlagsOut |
| 132 ){ | 188 ){ |
| 133 int rc; | 189 int rc; |
| 134 DO_OS_MALLOC_TEST(0); | 190 DO_OS_MALLOC_TEST(0); |
| 135 /* 0x87f3f is a mask of SQLITE_OPEN_ flags that are valid to be passed | 191 /* 0x87f7f is a mask of SQLITE_OPEN_ flags that are valid to be passed |
| 136 ** down into the VFS layer. Some SQLITE_OPEN_ flags (for example, | 192 ** down into the VFS layer. Some SQLITE_OPEN_ flags (for example, |
| 137 ** SQLITE_OPEN_FULLMUTEX or SQLITE_OPEN_SHAREDCACHE) are blocked before | 193 ** SQLITE_OPEN_FULLMUTEX or SQLITE_OPEN_SHAREDCACHE) are blocked before |
| 138 ** reaching the VFS. */ | 194 ** reaching the VFS. */ |
| 139 rc = pVfs->xOpen(pVfs, zPath, pFile, flags & 0x87f3f, pFlagsOut); | 195 rc = pVfs->xOpen(pVfs, zPath, pFile, flags & 0x87f7f, pFlagsOut); |
| 140 assert( rc==SQLITE_OK || pFile->pMethods==0 ); | 196 assert( rc==SQLITE_OK || pFile->pMethods==0 ); |
| 141 return rc; | 197 return rc; |
| 142 } | 198 } |
| 143 int sqlite3OsDelete(sqlite3_vfs *pVfs, const char *zPath, int dirSync){ | 199 int sqlite3OsDelete(sqlite3_vfs *pVfs, const char *zPath, int dirSync){ |
| 200 DO_OS_MALLOC_TEST(0); |
| 201 assert( dirSync==0 || dirSync==1 ); |
| 144 return pVfs->xDelete(pVfs, zPath, dirSync); | 202 return pVfs->xDelete(pVfs, zPath, dirSync); |
| 145 } | 203 } |
| 146 int sqlite3OsAccess( | 204 int sqlite3OsAccess( |
| 147 sqlite3_vfs *pVfs, | 205 sqlite3_vfs *pVfs, |
| 148 const char *zPath, | 206 const char *zPath, |
| 149 int flags, | 207 int flags, |
| 150 int *pResOut | 208 int *pResOut |
| 151 ){ | 209 ){ |
| 152 DO_OS_MALLOC_TEST(0); | 210 DO_OS_MALLOC_TEST(0); |
| 153 return pVfs->xAccess(pVfs, zPath, flags, pResOut); | 211 return pVfs->xAccess(pVfs, zPath, flags, pResOut); |
| 154 } | 212 } |
| 155 int sqlite3OsFullPathname( | 213 int sqlite3OsFullPathname( |
| 156 sqlite3_vfs *pVfs, | 214 sqlite3_vfs *pVfs, |
| 157 const char *zPath, | 215 const char *zPath, |
| 158 int nPathOut, | 216 int nPathOut, |
| 159 char *zPathOut | 217 char *zPathOut |
| 160 ){ | 218 ){ |
| 219 DO_OS_MALLOC_TEST(0); |
| 161 zPathOut[0] = 0; | 220 zPathOut[0] = 0; |
| 162 return pVfs->xFullPathname(pVfs, zPath, nPathOut, zPathOut); | 221 return pVfs->xFullPathname(pVfs, zPath, nPathOut, zPathOut); |
| 163 } | 222 } |
| 164 #ifndef SQLITE_OMIT_LOAD_EXTENSION | 223 #ifndef SQLITE_OMIT_LOAD_EXTENSION |
| 165 void *sqlite3OsDlOpen(sqlite3_vfs *pVfs, const char *zPath){ | 224 void *sqlite3OsDlOpen(sqlite3_vfs *pVfs, const char *zPath){ |
| 166 return pVfs->xDlOpen(pVfs, zPath); | 225 return pVfs->xDlOpen(pVfs, zPath); |
| 167 } | 226 } |
| 168 void sqlite3OsDlError(sqlite3_vfs *pVfs, int nByte, char *zBufOut){ | 227 void sqlite3OsDlError(sqlite3_vfs *pVfs, int nByte, char *zBufOut){ |
| 169 pVfs->xDlError(pVfs, nByte, zBufOut); | 228 pVfs->xDlError(pVfs, nByte, zBufOut); |
| 170 } | 229 } |
| (...skipping 30 matching lines...) Expand all Loading... |
| 201 | 260 |
| 202 int sqlite3OsOpenMalloc( | 261 int sqlite3OsOpenMalloc( |
| 203 sqlite3_vfs *pVfs, | 262 sqlite3_vfs *pVfs, |
| 204 const char *zFile, | 263 const char *zFile, |
| 205 sqlite3_file **ppFile, | 264 sqlite3_file **ppFile, |
| 206 int flags, | 265 int flags, |
| 207 int *pOutFlags | 266 int *pOutFlags |
| 208 ){ | 267 ){ |
| 209 int rc = SQLITE_NOMEM; | 268 int rc = SQLITE_NOMEM; |
| 210 sqlite3_file *pFile; | 269 sqlite3_file *pFile; |
| 211 pFile = (sqlite3_file *)sqlite3Malloc(pVfs->szOsFile); | 270 pFile = (sqlite3_file *)sqlite3MallocZero(pVfs->szOsFile); |
| 212 if( pFile ){ | 271 if( pFile ){ |
| 213 rc = sqlite3OsOpen(pVfs, zFile, pFile, flags, pOutFlags); | 272 rc = sqlite3OsOpen(pVfs, zFile, pFile, flags, pOutFlags); |
| 214 if( rc!=SQLITE_OK ){ | 273 if( rc!=SQLITE_OK ){ |
| 215 sqlite3_free(pFile); | 274 sqlite3_free(pFile); |
| 216 }else{ | 275 }else{ |
| 217 *ppFile = pFile; | 276 *ppFile = pFile; |
| 218 } | 277 } |
| 219 } | 278 } |
| 220 return rc; | 279 return rc; |
| 221 } | 280 } |
| (...skipping 68 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 290 } | 349 } |
| 291 } | 350 } |
| 292 } | 351 } |
| 293 | 352 |
| 294 /* | 353 /* |
| 295 ** Register a VFS with the system. It is harmless to register the same | 354 ** Register a VFS with the system. It is harmless to register the same |
| 296 ** VFS multiple times. The new VFS becomes the default if makeDflt is | 355 ** VFS multiple times. The new VFS becomes the default if makeDflt is |
| 297 ** true. | 356 ** true. |
| 298 */ | 357 */ |
| 299 int sqlite3_vfs_register(sqlite3_vfs *pVfs, int makeDflt){ | 358 int sqlite3_vfs_register(sqlite3_vfs *pVfs, int makeDflt){ |
| 300 sqlite3_mutex *mutex = 0; | 359 MUTEX_LOGIC(sqlite3_mutex *mutex;) |
| 301 #ifndef SQLITE_OMIT_AUTOINIT | 360 #ifndef SQLITE_OMIT_AUTOINIT |
| 302 int rc = sqlite3_initialize(); | 361 int rc = sqlite3_initialize(); |
| 303 if( rc ) return rc; | 362 if( rc ) return rc; |
| 304 #endif | 363 #endif |
| 305 mutex = sqlite3MutexAlloc(SQLITE_MUTEX_STATIC_MASTER); | 364 MUTEX_LOGIC( mutex = sqlite3MutexAlloc(SQLITE_MUTEX_STATIC_MASTER); ) |
| 306 sqlite3_mutex_enter(mutex); | 365 sqlite3_mutex_enter(mutex); |
| 307 vfsUnlink(pVfs); | 366 vfsUnlink(pVfs); |
| 308 if( makeDflt || vfsList==0 ){ | 367 if( makeDflt || vfsList==0 ){ |
| 309 pVfs->pNext = vfsList; | 368 pVfs->pNext = vfsList; |
| 310 vfsList = pVfs; | 369 vfsList = pVfs; |
| 311 }else{ | 370 }else{ |
| 312 pVfs->pNext = vfsList->pNext; | 371 pVfs->pNext = vfsList->pNext; |
| 313 vfsList->pNext = pVfs; | 372 vfsList->pNext = pVfs; |
| 314 } | 373 } |
| 315 assert(vfsList); | 374 assert(vfsList); |
| 316 sqlite3_mutex_leave(mutex); | 375 sqlite3_mutex_leave(mutex); |
| 317 return SQLITE_OK; | 376 return SQLITE_OK; |
| 318 } | 377 } |
| 319 | 378 |
| 320 /* | 379 /* |
| 321 ** Unregister a VFS so that it is no longer accessible. | 380 ** Unregister a VFS so that it is no longer accessible. |
| 322 */ | 381 */ |
| 323 int sqlite3_vfs_unregister(sqlite3_vfs *pVfs){ | 382 int sqlite3_vfs_unregister(sqlite3_vfs *pVfs){ |
| 324 #if SQLITE_THREADSAFE | 383 #if SQLITE_THREADSAFE |
| 325 sqlite3_mutex *mutex = sqlite3MutexAlloc(SQLITE_MUTEX_STATIC_MASTER); | 384 sqlite3_mutex *mutex = sqlite3MutexAlloc(SQLITE_MUTEX_STATIC_MASTER); |
| 326 #endif | 385 #endif |
| 327 sqlite3_mutex_enter(mutex); | 386 sqlite3_mutex_enter(mutex); |
| 328 vfsUnlink(pVfs); | 387 vfsUnlink(pVfs); |
| 329 sqlite3_mutex_leave(mutex); | 388 sqlite3_mutex_leave(mutex); |
| 330 return SQLITE_OK; | 389 return SQLITE_OK; |
| 331 } | 390 } |
| OLD | NEW |