OLD | NEW |
(Empty) | |
| 1 /************** Begin file mutex_unix.c **************************************/ |
| 2 /* |
| 3 ** 2007 August 28 |
| 4 ** |
| 5 ** The author disclaims copyright to this source code. In place of |
| 6 ** a legal notice, here is a blessing: |
| 7 ** |
| 8 ** May you do good and not evil. |
| 9 ** May you find forgiveness for yourself and forgive others. |
| 10 ** May you share freely, never taking more than you give. |
| 11 ** |
| 12 ************************************************************************* |
| 13 ** This file contains the C functions that implement mutexes for pthreads |
| 14 */ |
| 15 /* #include "sqliteInt.h" */ |
| 16 |
| 17 /* |
| 18 ** The code in this file is only used if we are compiling threadsafe |
| 19 ** under unix with pthreads. |
| 20 ** |
| 21 ** Note that this implementation requires a version of pthreads that |
| 22 ** supports recursive mutexes. |
| 23 */ |
| 24 #ifdef SQLITE_MUTEX_PTHREADS |
| 25 |
| 26 #include <pthread.h> |
| 27 |
| 28 /* |
| 29 ** The sqlite3_mutex.id, sqlite3_mutex.nRef, and sqlite3_mutex.owner fields |
| 30 ** are necessary under two condidtions: (1) Debug builds and (2) using |
| 31 ** home-grown mutexes. Encapsulate these conditions into a single #define. |
| 32 */ |
| 33 #if defined(SQLITE_DEBUG) || defined(SQLITE_HOMEGROWN_RECURSIVE_MUTEX) |
| 34 # define SQLITE_MUTEX_NREF 1 |
| 35 #else |
| 36 # define SQLITE_MUTEX_NREF 0 |
| 37 #endif |
| 38 |
| 39 /* |
| 40 ** Each recursive mutex is an instance of the following structure. |
| 41 */ |
| 42 struct sqlite3_mutex { |
| 43 pthread_mutex_t mutex; /* Mutex controlling the lock */ |
| 44 #if SQLITE_MUTEX_NREF || defined(SQLITE_ENABLE_API_ARMOR) |
| 45 int id; /* Mutex type */ |
| 46 #endif |
| 47 #if SQLITE_MUTEX_NREF |
| 48 volatile int nRef; /* Number of entrances */ |
| 49 volatile pthread_t owner; /* Thread that is within this mutex */ |
| 50 int trace; /* True to trace changes */ |
| 51 #endif |
| 52 }; |
| 53 #if SQLITE_MUTEX_NREF |
| 54 #define SQLITE3_MUTEX_INITIALIZER {PTHREAD_MUTEX_INITIALIZER,0,0,(pthread_t)0,0} |
| 55 #elif defined(SQLITE_ENABLE_API_ARMOR) |
| 56 #define SQLITE3_MUTEX_INITIALIZER { PTHREAD_MUTEX_INITIALIZER, 0 } |
| 57 #else |
| 58 #define SQLITE3_MUTEX_INITIALIZER { PTHREAD_MUTEX_INITIALIZER } |
| 59 #endif |
| 60 |
| 61 /* |
| 62 ** The sqlite3_mutex_held() and sqlite3_mutex_notheld() routine are |
| 63 ** intended for use only inside assert() statements. On some platforms, |
| 64 ** there might be race conditions that can cause these routines to |
| 65 ** deliver incorrect results. In particular, if pthread_equal() is |
| 66 ** not an atomic operation, then these routines might delivery |
| 67 ** incorrect results. On most platforms, pthread_equal() is a |
| 68 ** comparison of two integers and is therefore atomic. But we are |
| 69 ** told that HPUX is not such a platform. If so, then these routines |
| 70 ** will not always work correctly on HPUX. |
| 71 ** |
| 72 ** On those platforms where pthread_equal() is not atomic, SQLite |
| 73 ** should be compiled without -DSQLITE_DEBUG and with -DNDEBUG to |
| 74 ** make sure no assert() statements are evaluated and hence these |
| 75 ** routines are never called. |
| 76 */ |
| 77 #if !defined(NDEBUG) || defined(SQLITE_DEBUG) |
| 78 static int pthreadMutexHeld(sqlite3_mutex *p){ |
| 79 return (p->nRef!=0 && pthread_equal(p->owner, pthread_self())); |
| 80 } |
| 81 static int pthreadMutexNotheld(sqlite3_mutex *p){ |
| 82 return p->nRef==0 || pthread_equal(p->owner, pthread_self())==0; |
| 83 } |
| 84 #endif |
| 85 |
| 86 /* |
| 87 ** Try to provide a memory barrier operation, needed for initialization |
| 88 ** and also for the implementation of xShmBarrier in the VFS in cases |
| 89 ** where SQLite is compiled without mutexes. |
| 90 */ |
| 91 SQLITE_PRIVATE void sqlite3MemoryBarrier(void){ |
| 92 #if defined(SQLITE_MEMORY_BARRIER) |
| 93 SQLITE_MEMORY_BARRIER; |
| 94 #elif defined(__GNUC__) && GCC_VERSION>=4001000 |
| 95 __sync_synchronize(); |
| 96 #endif |
| 97 } |
| 98 |
| 99 /* |
| 100 ** Initialize and deinitialize the mutex subsystem. |
| 101 */ |
| 102 static int pthreadMutexInit(void){ return SQLITE_OK; } |
| 103 static int pthreadMutexEnd(void){ return SQLITE_OK; } |
| 104 |
| 105 /* |
| 106 ** The sqlite3_mutex_alloc() routine allocates a new |
| 107 ** mutex and returns a pointer to it. If it returns NULL |
| 108 ** that means that a mutex could not be allocated. SQLite |
| 109 ** will unwind its stack and return an error. The argument |
| 110 ** to sqlite3_mutex_alloc() is one of these integer constants: |
| 111 ** |
| 112 ** <ul> |
| 113 ** <li> SQLITE_MUTEX_FAST |
| 114 ** <li> SQLITE_MUTEX_RECURSIVE |
| 115 ** <li> SQLITE_MUTEX_STATIC_MASTER |
| 116 ** <li> SQLITE_MUTEX_STATIC_MEM |
| 117 ** <li> SQLITE_MUTEX_STATIC_OPEN |
| 118 ** <li> SQLITE_MUTEX_STATIC_PRNG |
| 119 ** <li> SQLITE_MUTEX_STATIC_LRU |
| 120 ** <li> SQLITE_MUTEX_STATIC_PMEM |
| 121 ** <li> SQLITE_MUTEX_STATIC_APP1 |
| 122 ** <li> SQLITE_MUTEX_STATIC_APP2 |
| 123 ** <li> SQLITE_MUTEX_STATIC_APP3 |
| 124 ** <li> SQLITE_MUTEX_STATIC_VFS1 |
| 125 ** <li> SQLITE_MUTEX_STATIC_VFS2 |
| 126 ** <li> SQLITE_MUTEX_STATIC_VFS3 |
| 127 ** </ul> |
| 128 ** |
| 129 ** The first two constants cause sqlite3_mutex_alloc() to create |
| 130 ** a new mutex. The new mutex is recursive when SQLITE_MUTEX_RECURSIVE |
| 131 ** is used but not necessarily so when SQLITE_MUTEX_FAST is used. |
| 132 ** The mutex implementation does not need to make a distinction |
| 133 ** between SQLITE_MUTEX_RECURSIVE and SQLITE_MUTEX_FAST if it does |
| 134 ** not want to. But SQLite will only request a recursive mutex in |
| 135 ** cases where it really needs one. If a faster non-recursive mutex |
| 136 ** implementation is available on the host platform, the mutex subsystem |
| 137 ** might return such a mutex in response to SQLITE_MUTEX_FAST. |
| 138 ** |
| 139 ** The other allowed parameters to sqlite3_mutex_alloc() each return |
| 140 ** a pointer to a static preexisting mutex. Six static mutexes are |
| 141 ** used by the current version of SQLite. Future versions of SQLite |
| 142 ** may add additional static mutexes. Static mutexes are for internal |
| 143 ** use by SQLite only. Applications that use SQLite mutexes should |
| 144 ** use only the dynamic mutexes returned by SQLITE_MUTEX_FAST or |
| 145 ** SQLITE_MUTEX_RECURSIVE. |
| 146 ** |
| 147 ** Note that if one of the dynamic mutex parameters (SQLITE_MUTEX_FAST |
| 148 ** or SQLITE_MUTEX_RECURSIVE) is used then sqlite3_mutex_alloc() |
| 149 ** returns a different mutex on every call. But for the static |
| 150 ** mutex types, the same mutex is returned on every call that has |
| 151 ** the same type number. |
| 152 */ |
| 153 static sqlite3_mutex *pthreadMutexAlloc(int iType){ |
| 154 static sqlite3_mutex staticMutexes[] = { |
| 155 SQLITE3_MUTEX_INITIALIZER, |
| 156 SQLITE3_MUTEX_INITIALIZER, |
| 157 SQLITE3_MUTEX_INITIALIZER, |
| 158 SQLITE3_MUTEX_INITIALIZER, |
| 159 SQLITE3_MUTEX_INITIALIZER, |
| 160 SQLITE3_MUTEX_INITIALIZER, |
| 161 SQLITE3_MUTEX_INITIALIZER, |
| 162 SQLITE3_MUTEX_INITIALIZER, |
| 163 SQLITE3_MUTEX_INITIALIZER, |
| 164 SQLITE3_MUTEX_INITIALIZER, |
| 165 SQLITE3_MUTEX_INITIALIZER, |
| 166 SQLITE3_MUTEX_INITIALIZER |
| 167 }; |
| 168 sqlite3_mutex *p; |
| 169 switch( iType ){ |
| 170 case SQLITE_MUTEX_RECURSIVE: { |
| 171 p = sqlite3MallocZero( sizeof(*p) ); |
| 172 if( p ){ |
| 173 #ifdef SQLITE_HOMEGROWN_RECURSIVE_MUTEX |
| 174 /* If recursive mutexes are not available, we will have to |
| 175 ** build our own. See below. */ |
| 176 pthread_mutex_init(&p->mutex, 0); |
| 177 #else |
| 178 /* Use a recursive mutex if it is available */ |
| 179 pthread_mutexattr_t recursiveAttr; |
| 180 pthread_mutexattr_init(&recursiveAttr); |
| 181 pthread_mutexattr_settype(&recursiveAttr, PTHREAD_MUTEX_RECURSIVE); |
| 182 pthread_mutex_init(&p->mutex, &recursiveAttr); |
| 183 pthread_mutexattr_destroy(&recursiveAttr); |
| 184 #endif |
| 185 } |
| 186 break; |
| 187 } |
| 188 case SQLITE_MUTEX_FAST: { |
| 189 p = sqlite3MallocZero( sizeof(*p) ); |
| 190 if( p ){ |
| 191 pthread_mutex_init(&p->mutex, 0); |
| 192 } |
| 193 break; |
| 194 } |
| 195 default: { |
| 196 #ifdef SQLITE_ENABLE_API_ARMOR |
| 197 if( iType-2<0 || iType-2>=ArraySize(staticMutexes) ){ |
| 198 (void)SQLITE_MISUSE_BKPT; |
| 199 return 0; |
| 200 } |
| 201 #endif |
| 202 p = &staticMutexes[iType-2]; |
| 203 break; |
| 204 } |
| 205 } |
| 206 #if SQLITE_MUTEX_NREF || defined(SQLITE_ENABLE_API_ARMOR) |
| 207 if( p ) p->id = iType; |
| 208 #endif |
| 209 return p; |
| 210 } |
| 211 |
| 212 |
| 213 /* |
| 214 ** This routine deallocates a previously |
| 215 ** allocated mutex. SQLite is careful to deallocate every |
| 216 ** mutex that it allocates. |
| 217 */ |
| 218 static void pthreadMutexFree(sqlite3_mutex *p){ |
| 219 assert( p->nRef==0 ); |
| 220 #if SQLITE_ENABLE_API_ARMOR |
| 221 if( p->id==SQLITE_MUTEX_FAST || p->id==SQLITE_MUTEX_RECURSIVE ) |
| 222 #endif |
| 223 { |
| 224 pthread_mutex_destroy(&p->mutex); |
| 225 sqlite3_free(p); |
| 226 } |
| 227 #ifdef SQLITE_ENABLE_API_ARMOR |
| 228 else{ |
| 229 (void)SQLITE_MISUSE_BKPT; |
| 230 } |
| 231 #endif |
| 232 } |
| 233 |
| 234 /* |
| 235 ** The sqlite3_mutex_enter() and sqlite3_mutex_try() routines attempt |
| 236 ** to enter a mutex. If another thread is already within the mutex, |
| 237 ** sqlite3_mutex_enter() will block and sqlite3_mutex_try() will return |
| 238 ** SQLITE_BUSY. The sqlite3_mutex_try() interface returns SQLITE_OK |
| 239 ** upon successful entry. Mutexes created using SQLITE_MUTEX_RECURSIVE can |
| 240 ** be entered multiple times by the same thread. In such cases the, |
| 241 ** mutex must be exited an equal number of times before another thread |
| 242 ** can enter. If the same thread tries to enter any other kind of mutex |
| 243 ** more than once, the behavior is undefined. |
| 244 */ |
| 245 static void pthreadMutexEnter(sqlite3_mutex *p){ |
| 246 assert( p->id==SQLITE_MUTEX_RECURSIVE || pthreadMutexNotheld(p) ); |
| 247 |
| 248 #ifdef SQLITE_HOMEGROWN_RECURSIVE_MUTEX |
| 249 /* If recursive mutexes are not available, then we have to grow |
| 250 ** our own. This implementation assumes that pthread_equal() |
| 251 ** is atomic - that it cannot be deceived into thinking self |
| 252 ** and p->owner are equal if p->owner changes between two values |
| 253 ** that are not equal to self while the comparison is taking place. |
| 254 ** This implementation also assumes a coherent cache - that |
| 255 ** separate processes cannot read different values from the same |
| 256 ** address at the same time. If either of these two conditions |
| 257 ** are not met, then the mutexes will fail and problems will result. |
| 258 */ |
| 259 { |
| 260 pthread_t self = pthread_self(); |
| 261 if( p->nRef>0 && pthread_equal(p->owner, self) ){ |
| 262 p->nRef++; |
| 263 }else{ |
| 264 pthread_mutex_lock(&p->mutex); |
| 265 assert( p->nRef==0 ); |
| 266 p->owner = self; |
| 267 p->nRef = 1; |
| 268 } |
| 269 } |
| 270 #else |
| 271 /* Use the built-in recursive mutexes if they are available. |
| 272 */ |
| 273 pthread_mutex_lock(&p->mutex); |
| 274 #if SQLITE_MUTEX_NREF |
| 275 assert( p->nRef>0 || p->owner==0 ); |
| 276 p->owner = pthread_self(); |
| 277 p->nRef++; |
| 278 #endif |
| 279 #endif |
| 280 |
| 281 #ifdef SQLITE_DEBUG |
| 282 if( p->trace ){ |
| 283 printf("enter mutex %p (%d) with nRef=%d\n", p, p->trace, p->nRef); |
| 284 } |
| 285 #endif |
| 286 } |
| 287 static int pthreadMutexTry(sqlite3_mutex *p){ |
| 288 int rc; |
| 289 assert( p->id==SQLITE_MUTEX_RECURSIVE || pthreadMutexNotheld(p) ); |
| 290 |
| 291 #ifdef SQLITE_HOMEGROWN_RECURSIVE_MUTEX |
| 292 /* If recursive mutexes are not available, then we have to grow |
| 293 ** our own. This implementation assumes that pthread_equal() |
| 294 ** is atomic - that it cannot be deceived into thinking self |
| 295 ** and p->owner are equal if p->owner changes between two values |
| 296 ** that are not equal to self while the comparison is taking place. |
| 297 ** This implementation also assumes a coherent cache - that |
| 298 ** separate processes cannot read different values from the same |
| 299 ** address at the same time. If either of these two conditions |
| 300 ** are not met, then the mutexes will fail and problems will result. |
| 301 */ |
| 302 { |
| 303 pthread_t self = pthread_self(); |
| 304 if( p->nRef>0 && pthread_equal(p->owner, self) ){ |
| 305 p->nRef++; |
| 306 rc = SQLITE_OK; |
| 307 }else if( pthread_mutex_trylock(&p->mutex)==0 ){ |
| 308 assert( p->nRef==0 ); |
| 309 p->owner = self; |
| 310 p->nRef = 1; |
| 311 rc = SQLITE_OK; |
| 312 }else{ |
| 313 rc = SQLITE_BUSY; |
| 314 } |
| 315 } |
| 316 #else |
| 317 /* Use the built-in recursive mutexes if they are available. |
| 318 */ |
| 319 if( pthread_mutex_trylock(&p->mutex)==0 ){ |
| 320 #if SQLITE_MUTEX_NREF |
| 321 p->owner = pthread_self(); |
| 322 p->nRef++; |
| 323 #endif |
| 324 rc = SQLITE_OK; |
| 325 }else{ |
| 326 rc = SQLITE_BUSY; |
| 327 } |
| 328 #endif |
| 329 |
| 330 #ifdef SQLITE_DEBUG |
| 331 if( rc==SQLITE_OK && p->trace ){ |
| 332 printf("enter mutex %p (%d) with nRef=%d\n", p, p->trace, p->nRef); |
| 333 } |
| 334 #endif |
| 335 return rc; |
| 336 } |
| 337 |
| 338 /* |
| 339 ** The sqlite3_mutex_leave() routine exits a mutex that was |
| 340 ** previously entered by the same thread. The behavior |
| 341 ** is undefined if the mutex is not currently entered or |
| 342 ** is not currently allocated. SQLite will never do either. |
| 343 */ |
| 344 static void pthreadMutexLeave(sqlite3_mutex *p){ |
| 345 assert( pthreadMutexHeld(p) ); |
| 346 #if SQLITE_MUTEX_NREF |
| 347 p->nRef--; |
| 348 if( p->nRef==0 ) p->owner = 0; |
| 349 #endif |
| 350 assert( p->nRef==0 || p->id==SQLITE_MUTEX_RECURSIVE ); |
| 351 |
| 352 #ifdef SQLITE_HOMEGROWN_RECURSIVE_MUTEX |
| 353 if( p->nRef==0 ){ |
| 354 pthread_mutex_unlock(&p->mutex); |
| 355 } |
| 356 #else |
| 357 pthread_mutex_unlock(&p->mutex); |
| 358 #endif |
| 359 |
| 360 #ifdef SQLITE_DEBUG |
| 361 if( p->trace ){ |
| 362 printf("leave mutex %p (%d) with nRef=%d\n", p, p->trace, p->nRef); |
| 363 } |
| 364 #endif |
| 365 } |
| 366 |
| 367 SQLITE_PRIVATE sqlite3_mutex_methods const *sqlite3DefaultMutex(void){ |
| 368 static const sqlite3_mutex_methods sMutex = { |
| 369 pthreadMutexInit, |
| 370 pthreadMutexEnd, |
| 371 pthreadMutexAlloc, |
| 372 pthreadMutexFree, |
| 373 pthreadMutexEnter, |
| 374 pthreadMutexTry, |
| 375 pthreadMutexLeave, |
| 376 #ifdef SQLITE_DEBUG |
| 377 pthreadMutexHeld, |
| 378 pthreadMutexNotheld |
| 379 #else |
| 380 0, |
| 381 0 |
| 382 #endif |
| 383 }; |
| 384 |
| 385 return &sMutex; |
| 386 } |
| 387 |
| 388 #endif /* SQLITE_MUTEX_PTHREADS */ |
| 389 |
| 390 /************** End of mutex_unix.c ******************************************/ |
| 391 /************** Begin file mutex_w32.c ***************************************/ |
| 392 /* |
| 393 ** 2007 August 14 |
| 394 ** |
| 395 ** The author disclaims copyright to this source code. In place of |
| 396 ** a legal notice, here is a blessing: |
| 397 ** |
| 398 ** May you do good and not evil. |
| 399 ** May you find forgiveness for yourself and forgive others. |
| 400 ** May you share freely, never taking more than you give. |
| 401 ** |
| 402 ************************************************************************* |
| 403 ** This file contains the C functions that implement mutexes for Win32. |
| 404 */ |
| 405 /* #include "sqliteInt.h" */ |
| 406 |
| 407 #if SQLITE_OS_WIN |
| 408 /* |
| 409 ** Include code that is common to all os_*.c files |
| 410 */ |
| 411 /************** Include os_common.h in the middle of mutex_w32.c *************/ |
| 412 /************** Begin file os_common.h ***************************************/ |
| 413 /* |
| 414 ** 2004 May 22 |
| 415 ** |
| 416 ** The author disclaims copyright to this source code. In place of |
| 417 ** a legal notice, here is a blessing: |
| 418 ** |
| 419 ** May you do good and not evil. |
| 420 ** May you find forgiveness for yourself and forgive others. |
| 421 ** May you share freely, never taking more than you give. |
| 422 ** |
| 423 ****************************************************************************** |
| 424 ** |
| 425 ** This file contains macros and a little bit of code that is common to |
| 426 ** all of the platform-specific files (os_*.c) and is #included into those |
| 427 ** files. |
| 428 ** |
| 429 ** This file should be #included by the os_*.c files only. It is not a |
| 430 ** general purpose header file. |
| 431 */ |
| 432 #ifndef _OS_COMMON_H_ |
| 433 #define _OS_COMMON_H_ |
| 434 |
| 435 /* |
| 436 ** At least two bugs have slipped in because we changed the MEMORY_DEBUG |
| 437 ** macro to SQLITE_DEBUG and some older makefiles have not yet made the |
| 438 ** switch. The following code should catch this problem at compile-time. |
| 439 */ |
| 440 #ifdef MEMORY_DEBUG |
| 441 # error "The MEMORY_DEBUG macro is obsolete. Use SQLITE_DEBUG instead." |
| 442 #endif |
| 443 |
| 444 /* |
| 445 ** Macros for performance tracing. Normally turned off. Only works |
| 446 ** on i486 hardware. |
| 447 */ |
| 448 #ifdef SQLITE_PERFORMANCE_TRACE |
| 449 |
| 450 /* |
| 451 ** hwtime.h contains inline assembler code for implementing |
| 452 ** high-performance timing routines. |
| 453 */ |
| 454 /************** Include hwtime.h in the middle of os_common.h ****************/ |
| 455 /************** Begin file hwtime.h ******************************************/ |
| 456 /* |
| 457 ** 2008 May 27 |
| 458 ** |
| 459 ** The author disclaims copyright to this source code. In place of |
| 460 ** a legal notice, here is a blessing: |
| 461 ** |
| 462 ** May you do good and not evil. |
| 463 ** May you find forgiveness for yourself and forgive others. |
| 464 ** May you share freely, never taking more than you give. |
| 465 ** |
| 466 ****************************************************************************** |
| 467 ** |
| 468 ** This file contains inline asm code for retrieving "high-performance" |
| 469 ** counters for x86 class CPUs. |
| 470 */ |
| 471 #ifndef _HWTIME_H_ |
| 472 #define _HWTIME_H_ |
| 473 |
| 474 /* |
| 475 ** The following routine only works on pentium-class (or newer) processors. |
| 476 ** It uses the RDTSC opcode to read the cycle count value out of the |
| 477 ** processor and returns that value. This can be used for high-res |
| 478 ** profiling. |
| 479 */ |
| 480 #if (defined(__GNUC__) || defined(_MSC_VER)) && \ |
| 481 (defined(i386) || defined(__i386__) || defined(_M_IX86)) |
| 482 |
| 483 #if defined(__GNUC__) |
| 484 |
| 485 __inline__ sqlite_uint64 sqlite3Hwtime(void){ |
| 486 unsigned int lo, hi; |
| 487 __asm__ __volatile__ ("rdtsc" : "=a" (lo), "=d" (hi)); |
| 488 return (sqlite_uint64)hi << 32 | lo; |
| 489 } |
| 490 |
| 491 #elif defined(_MSC_VER) |
| 492 |
| 493 __declspec(naked) __inline sqlite_uint64 __cdecl sqlite3Hwtime(void){ |
| 494 __asm { |
| 495 rdtsc |
| 496 ret ; return value at EDX:EAX |
| 497 } |
| 498 } |
| 499 |
| 500 #endif |
| 501 |
| 502 #elif (defined(__GNUC__) && defined(__x86_64__)) |
| 503 |
| 504 __inline__ sqlite_uint64 sqlite3Hwtime(void){ |
| 505 unsigned long val; |
| 506 __asm__ __volatile__ ("rdtsc" : "=A" (val)); |
| 507 return val; |
| 508 } |
| 509 |
| 510 #elif (defined(__GNUC__) && defined(__ppc__)) |
| 511 |
| 512 __inline__ sqlite_uint64 sqlite3Hwtime(void){ |
| 513 unsigned long long retval; |
| 514 unsigned long junk; |
| 515 __asm__ __volatile__ ("\n\ |
| 516 1: mftbu %1\n\ |
| 517 mftb %L0\n\ |
| 518 mftbu %0\n\ |
| 519 cmpw %0,%1\n\ |
| 520 bne 1b" |
| 521 : "=r" (retval), "=r" (junk)); |
| 522 return retval; |
| 523 } |
| 524 |
| 525 #else |
| 526 |
| 527 #error Need implementation of sqlite3Hwtime() for your platform. |
| 528 |
| 529 /* |
| 530 ** To compile without implementing sqlite3Hwtime() for your platform, |
| 531 ** you can remove the above #error and use the following |
| 532 ** stub function. You will lose timing support for many |
| 533 ** of the debugging and testing utilities, but it should at |
| 534 ** least compile and run. |
| 535 */ |
| 536 SQLITE_PRIVATE sqlite_uint64 sqlite3Hwtime(void){ return ((sqlite_uint64)0); } |
| 537 |
| 538 #endif |
| 539 |
| 540 #endif /* !defined(_HWTIME_H_) */ |
| 541 |
| 542 /************** End of hwtime.h **********************************************/ |
| 543 /************** Continuing where we left off in os_common.h ******************/ |
| 544 |
| 545 static sqlite_uint64 g_start; |
| 546 static sqlite_uint64 g_elapsed; |
| 547 #define TIMER_START g_start=sqlite3Hwtime() |
| 548 #define TIMER_END g_elapsed=sqlite3Hwtime()-g_start |
| 549 #define TIMER_ELAPSED g_elapsed |
| 550 #else |
| 551 #define TIMER_START |
| 552 #define TIMER_END |
| 553 #define TIMER_ELAPSED ((sqlite_uint64)0) |
| 554 #endif |
| 555 |
| 556 /* |
| 557 ** If we compile with the SQLITE_TEST macro set, then the following block |
| 558 ** of code will give us the ability to simulate a disk I/O error. This |
| 559 ** is used for testing the I/O recovery logic. |
| 560 */ |
| 561 #ifdef SQLITE_TEST |
| 562 SQLITE_API int sqlite3_io_error_hit = 0; /* Total number of I/O Error
s */ |
| 563 SQLITE_API int sqlite3_io_error_hardhit = 0; /* Number of non-benign erro
rs */ |
| 564 SQLITE_API int sqlite3_io_error_pending = 0; /* Count down to first I/O e
rror */ |
| 565 SQLITE_API int sqlite3_io_error_persist = 0; /* True if I/O errors persis
t */ |
| 566 SQLITE_API int sqlite3_io_error_benign = 0; /* True if errors are benign
*/ |
| 567 SQLITE_API int sqlite3_diskfull_pending = 0; |
| 568 SQLITE_API int sqlite3_diskfull = 0; |
| 569 #define SimulateIOErrorBenign(X) sqlite3_io_error_benign=(X) |
| 570 #define SimulateIOError(CODE) \ |
| 571 if( (sqlite3_io_error_persist && sqlite3_io_error_hit) \ |
| 572 || sqlite3_io_error_pending-- == 1 ) \ |
| 573 { local_ioerr(); CODE; } |
| 574 static void local_ioerr(){ |
| 575 IOTRACE(("IOERR\n")); |
| 576 sqlite3_io_error_hit++; |
| 577 if( !sqlite3_io_error_benign ) sqlite3_io_error_hardhit++; |
| 578 } |
| 579 #define SimulateDiskfullError(CODE) \ |
| 580 if( sqlite3_diskfull_pending ){ \ |
| 581 if( sqlite3_diskfull_pending == 1 ){ \ |
| 582 local_ioerr(); \ |
| 583 sqlite3_diskfull = 1; \ |
| 584 sqlite3_io_error_hit = 1; \ |
| 585 CODE; \ |
| 586 }else{ \ |
| 587 sqlite3_diskfull_pending--; \ |
| 588 } \ |
| 589 } |
| 590 #else |
| 591 #define SimulateIOErrorBenign(X) |
| 592 #define SimulateIOError(A) |
| 593 #define SimulateDiskfullError(A) |
| 594 #endif |
| 595 |
| 596 /* |
| 597 ** When testing, keep a count of the number of open files. |
| 598 */ |
| 599 #ifdef SQLITE_TEST |
| 600 SQLITE_API int sqlite3_open_file_count = 0; |
| 601 #define OpenCounter(X) sqlite3_open_file_count+=(X) |
| 602 #else |
| 603 #define OpenCounter(X) |
| 604 #endif |
| 605 |
| 606 #endif /* !defined(_OS_COMMON_H_) */ |
| 607 |
| 608 /************** End of os_common.h *******************************************/ |
| 609 /************** Continuing where we left off in mutex_w32.c ******************/ |
| 610 |
| 611 /* |
| 612 ** Include the header file for the Windows VFS. |
| 613 */ |
| 614 /************** Include os_win.h in the middle of mutex_w32.c ****************/ |
| 615 /************** Begin file os_win.h ******************************************/ |
| 616 /* |
| 617 ** 2013 November 25 |
| 618 ** |
| 619 ** The author disclaims copyright to this source code. In place of |
| 620 ** a legal notice, here is a blessing: |
| 621 ** |
| 622 ** May you do good and not evil. |
| 623 ** May you find forgiveness for yourself and forgive others. |
| 624 ** May you share freely, never taking more than you give. |
| 625 ** |
| 626 ****************************************************************************** |
| 627 ** |
| 628 ** This file contains code that is specific to Windows. |
| 629 */ |
| 630 #ifndef _OS_WIN_H_ |
| 631 #define _OS_WIN_H_ |
| 632 |
| 633 /* |
| 634 ** Include the primary Windows SDK header file. |
| 635 */ |
| 636 #include "windows.h" |
| 637 |
| 638 #ifdef __CYGWIN__ |
| 639 # include <sys/cygwin.h> |
| 640 # include <errno.h> /* amalgamator: dontcache */ |
| 641 #endif |
| 642 |
| 643 /* |
| 644 ** Determine if we are dealing with Windows NT. |
| 645 ** |
| 646 ** We ought to be able to determine if we are compiling for Windows 9x or |
| 647 ** Windows NT using the _WIN32_WINNT macro as follows: |
| 648 ** |
| 649 ** #if defined(_WIN32_WINNT) |
| 650 ** # define SQLITE_OS_WINNT 1 |
| 651 ** #else |
| 652 ** # define SQLITE_OS_WINNT 0 |
| 653 ** #endif |
| 654 ** |
| 655 ** However, Visual Studio 2005 does not set _WIN32_WINNT by default, as |
| 656 ** it ought to, so the above test does not work. We'll just assume that |
| 657 ** everything is Windows NT unless the programmer explicitly says otherwise |
| 658 ** by setting SQLITE_OS_WINNT to 0. |
| 659 */ |
| 660 #if SQLITE_OS_WIN && !defined(SQLITE_OS_WINNT) |
| 661 # define SQLITE_OS_WINNT 1 |
| 662 #endif |
| 663 |
| 664 /* |
| 665 ** Determine if we are dealing with Windows CE - which has a much reduced |
| 666 ** API. |
| 667 */ |
| 668 #if defined(_WIN32_WCE) |
| 669 # define SQLITE_OS_WINCE 1 |
| 670 #else |
| 671 # define SQLITE_OS_WINCE 0 |
| 672 #endif |
| 673 |
| 674 /* |
| 675 ** Determine if we are dealing with WinRT, which provides only a subset of |
| 676 ** the full Win32 API. |
| 677 */ |
| 678 #if !defined(SQLITE_OS_WINRT) |
| 679 # define SQLITE_OS_WINRT 0 |
| 680 #endif |
| 681 |
| 682 /* |
| 683 ** For WinCE, some API function parameters do not appear to be declared as |
| 684 ** volatile. |
| 685 */ |
| 686 #if SQLITE_OS_WINCE |
| 687 # define SQLITE_WIN32_VOLATILE |
| 688 #else |
| 689 # define SQLITE_WIN32_VOLATILE volatile |
| 690 #endif |
| 691 |
| 692 /* |
| 693 ** For some Windows sub-platforms, the _beginthreadex() / _endthreadex() |
| 694 ** functions are not available (e.g. those not using MSVC, Cygwin, etc). |
| 695 */ |
| 696 #if SQLITE_OS_WIN && !SQLITE_OS_WINCE && !SQLITE_OS_WINRT && \ |
| 697 SQLITE_THREADSAFE>0 && !defined(__CYGWIN__) |
| 698 # define SQLITE_OS_WIN_THREADS 1 |
| 699 #else |
| 700 # define SQLITE_OS_WIN_THREADS 0 |
| 701 #endif |
| 702 |
| 703 #endif /* _OS_WIN_H_ */ |
| 704 |
| 705 /************** End of os_win.h **********************************************/ |
| 706 /************** Continuing where we left off in mutex_w32.c ******************/ |
| 707 #endif |
| 708 |
| 709 /* |
| 710 ** The code in this file is only used if we are compiling multithreaded |
| 711 ** on a Win32 system. |
| 712 */ |
| 713 #ifdef SQLITE_MUTEX_W32 |
| 714 |
| 715 /* |
| 716 ** Each recursive mutex is an instance of the following structure. |
| 717 */ |
| 718 struct sqlite3_mutex { |
| 719 CRITICAL_SECTION mutex; /* Mutex controlling the lock */ |
| 720 int id; /* Mutex type */ |
| 721 #ifdef SQLITE_DEBUG |
| 722 volatile int nRef; /* Number of enterances */ |
| 723 volatile DWORD owner; /* Thread holding this mutex */ |
| 724 volatile int trace; /* True to trace changes */ |
| 725 #endif |
| 726 }; |
| 727 |
| 728 /* |
| 729 ** These are the initializer values used when declaring a "static" mutex |
| 730 ** on Win32. It should be noted that all mutexes require initialization |
| 731 ** on the Win32 platform. |
| 732 */ |
| 733 #define SQLITE_W32_MUTEX_INITIALIZER { 0 } |
| 734 |
| 735 #ifdef SQLITE_DEBUG |
| 736 #define SQLITE3_MUTEX_INITIALIZER { SQLITE_W32_MUTEX_INITIALIZER, 0, \ |
| 737 0L, (DWORD)0, 0 } |
| 738 #else |
| 739 #define SQLITE3_MUTEX_INITIALIZER { SQLITE_W32_MUTEX_INITIALIZER, 0 } |
| 740 #endif |
| 741 |
| 742 #ifdef SQLITE_DEBUG |
| 743 /* |
| 744 ** The sqlite3_mutex_held() and sqlite3_mutex_notheld() routine are |
| 745 ** intended for use only inside assert() statements. |
| 746 */ |
| 747 static int winMutexHeld(sqlite3_mutex *p){ |
| 748 return p->nRef!=0 && p->owner==GetCurrentThreadId(); |
| 749 } |
| 750 |
| 751 static int winMutexNotheld2(sqlite3_mutex *p, DWORD tid){ |
| 752 return p->nRef==0 || p->owner!=tid; |
| 753 } |
| 754 |
| 755 static int winMutexNotheld(sqlite3_mutex *p){ |
| 756 DWORD tid = GetCurrentThreadId(); |
| 757 return winMutexNotheld2(p, tid); |
| 758 } |
| 759 #endif |
| 760 |
| 761 /* |
| 762 ** Try to provide a memory barrier operation, needed for initialization |
| 763 ** and also for the xShmBarrier method of the VFS in cases when SQLite is |
| 764 ** compiled without mutexes (SQLITE_THREADSAFE=0). |
| 765 */ |
| 766 SQLITE_PRIVATE void sqlite3MemoryBarrier(void){ |
| 767 #if defined(SQLITE_MEMORY_BARRIER) |
| 768 SQLITE_MEMORY_BARRIER; |
| 769 #elif defined(__GNUC__) |
| 770 __sync_synchronize(); |
| 771 #elif !defined(SQLITE_DISABLE_INTRINSIC) && \ |
| 772 defined(_MSC_VER) && _MSC_VER>=1300 |
| 773 _ReadWriteBarrier(); |
| 774 #elif defined(MemoryBarrier) |
| 775 MemoryBarrier(); |
| 776 #endif |
| 777 } |
| 778 |
| 779 /* |
| 780 ** Initialize and deinitialize the mutex subsystem. |
| 781 */ |
| 782 static sqlite3_mutex winMutex_staticMutexes[] = { |
| 783 SQLITE3_MUTEX_INITIALIZER, |
| 784 SQLITE3_MUTEX_INITIALIZER, |
| 785 SQLITE3_MUTEX_INITIALIZER, |
| 786 SQLITE3_MUTEX_INITIALIZER, |
| 787 SQLITE3_MUTEX_INITIALIZER, |
| 788 SQLITE3_MUTEX_INITIALIZER, |
| 789 SQLITE3_MUTEX_INITIALIZER, |
| 790 SQLITE3_MUTEX_INITIALIZER, |
| 791 SQLITE3_MUTEX_INITIALIZER, |
| 792 SQLITE3_MUTEX_INITIALIZER, |
| 793 SQLITE3_MUTEX_INITIALIZER, |
| 794 SQLITE3_MUTEX_INITIALIZER |
| 795 }; |
| 796 |
| 797 static int winMutex_isInit = 0; |
| 798 static int winMutex_isNt = -1; /* <0 means "need to query" */ |
| 799 |
| 800 /* As the winMutexInit() and winMutexEnd() functions are called as part |
| 801 ** of the sqlite3_initialize() and sqlite3_shutdown() processing, the |
| 802 ** "interlocked" magic used here is probably not strictly necessary. |
| 803 */ |
| 804 static LONG SQLITE_WIN32_VOLATILE winMutex_lock = 0; |
| 805 |
| 806 SQLITE_API int SQLITE_STDCALL sqlite3_win32_is_nt(void); /* os_win.c */ |
| 807 SQLITE_API void SQLITE_STDCALL sqlite3_win32_sleep(DWORD milliseconds); /* os_wi
n.c */ |
| 808 |
| 809 static int winMutexInit(void){ |
| 810 /* The first to increment to 1 does actual initialization */ |
| 811 if( InterlockedCompareExchange(&winMutex_lock, 1, 0)==0 ){ |
| 812 int i; |
| 813 for(i=0; i<ArraySize(winMutex_staticMutexes); i++){ |
| 814 #if SQLITE_OS_WINRT |
| 815 InitializeCriticalSectionEx(&winMutex_staticMutexes[i].mutex, 0, 0); |
| 816 #else |
| 817 InitializeCriticalSection(&winMutex_staticMutexes[i].mutex); |
| 818 #endif |
| 819 } |
| 820 winMutex_isInit = 1; |
| 821 }else{ |
| 822 /* Another thread is (in the process of) initializing the static |
| 823 ** mutexes */ |
| 824 while( !winMutex_isInit ){ |
| 825 sqlite3_win32_sleep(1); |
| 826 } |
| 827 } |
| 828 return SQLITE_OK; |
| 829 } |
| 830 |
| 831 static int winMutexEnd(void){ |
| 832 /* The first to decrement to 0 does actual shutdown |
| 833 ** (which should be the last to shutdown.) */ |
| 834 if( InterlockedCompareExchange(&winMutex_lock, 0, 1)==1 ){ |
| 835 if( winMutex_isInit==1 ){ |
| 836 int i; |
| 837 for(i=0; i<ArraySize(winMutex_staticMutexes); i++){ |
| 838 DeleteCriticalSection(&winMutex_staticMutexes[i].mutex); |
| 839 } |
| 840 winMutex_isInit = 0; |
| 841 } |
| 842 } |
| 843 return SQLITE_OK; |
| 844 } |
| 845 |
| 846 /* |
| 847 ** The sqlite3_mutex_alloc() routine allocates a new |
| 848 ** mutex and returns a pointer to it. If it returns NULL |
| 849 ** that means that a mutex could not be allocated. SQLite |
| 850 ** will unwind its stack and return an error. The argument |
| 851 ** to sqlite3_mutex_alloc() is one of these integer constants: |
| 852 ** |
| 853 ** <ul> |
| 854 ** <li> SQLITE_MUTEX_FAST |
| 855 ** <li> SQLITE_MUTEX_RECURSIVE |
| 856 ** <li> SQLITE_MUTEX_STATIC_MASTER |
| 857 ** <li> SQLITE_MUTEX_STATIC_MEM |
| 858 ** <li> SQLITE_MUTEX_STATIC_OPEN |
| 859 ** <li> SQLITE_MUTEX_STATIC_PRNG |
| 860 ** <li> SQLITE_MUTEX_STATIC_LRU |
| 861 ** <li> SQLITE_MUTEX_STATIC_PMEM |
| 862 ** <li> SQLITE_MUTEX_STATIC_APP1 |
| 863 ** <li> SQLITE_MUTEX_STATIC_APP2 |
| 864 ** <li> SQLITE_MUTEX_STATIC_APP3 |
| 865 ** <li> SQLITE_MUTEX_STATIC_VFS1 |
| 866 ** <li> SQLITE_MUTEX_STATIC_VFS2 |
| 867 ** <li> SQLITE_MUTEX_STATIC_VFS3 |
| 868 ** </ul> |
| 869 ** |
| 870 ** The first two constants cause sqlite3_mutex_alloc() to create |
| 871 ** a new mutex. The new mutex is recursive when SQLITE_MUTEX_RECURSIVE |
| 872 ** is used but not necessarily so when SQLITE_MUTEX_FAST is used. |
| 873 ** The mutex implementation does not need to make a distinction |
| 874 ** between SQLITE_MUTEX_RECURSIVE and SQLITE_MUTEX_FAST if it does |
| 875 ** not want to. But SQLite will only request a recursive mutex in |
| 876 ** cases where it really needs one. If a faster non-recursive mutex |
| 877 ** implementation is available on the host platform, the mutex subsystem |
| 878 ** might return such a mutex in response to SQLITE_MUTEX_FAST. |
| 879 ** |
| 880 ** The other allowed parameters to sqlite3_mutex_alloc() each return |
| 881 ** a pointer to a static preexisting mutex. Six static mutexes are |
| 882 ** used by the current version of SQLite. Future versions of SQLite |
| 883 ** may add additional static mutexes. Static mutexes are for internal |
| 884 ** use by SQLite only. Applications that use SQLite mutexes should |
| 885 ** use only the dynamic mutexes returned by SQLITE_MUTEX_FAST or |
| 886 ** SQLITE_MUTEX_RECURSIVE. |
| 887 ** |
| 888 ** Note that if one of the dynamic mutex parameters (SQLITE_MUTEX_FAST |
| 889 ** or SQLITE_MUTEX_RECURSIVE) is used then sqlite3_mutex_alloc() |
| 890 ** returns a different mutex on every call. But for the static |
| 891 ** mutex types, the same mutex is returned on every call that has |
| 892 ** the same type number. |
| 893 */ |
| 894 static sqlite3_mutex *winMutexAlloc(int iType){ |
| 895 sqlite3_mutex *p; |
| 896 |
| 897 switch( iType ){ |
| 898 case SQLITE_MUTEX_FAST: |
| 899 case SQLITE_MUTEX_RECURSIVE: { |
| 900 p = sqlite3MallocZero( sizeof(*p) ); |
| 901 if( p ){ |
| 902 p->id = iType; |
| 903 #ifdef SQLITE_DEBUG |
| 904 #ifdef SQLITE_WIN32_MUTEX_TRACE_DYNAMIC |
| 905 p->trace = 1; |
| 906 #endif |
| 907 #endif |
| 908 #if SQLITE_OS_WINRT |
| 909 InitializeCriticalSectionEx(&p->mutex, 0, 0); |
| 910 #else |
| 911 InitializeCriticalSection(&p->mutex); |
| 912 #endif |
| 913 } |
| 914 break; |
| 915 } |
| 916 default: { |
| 917 #ifdef SQLITE_ENABLE_API_ARMOR |
| 918 if( iType-2<0 || iType-2>=ArraySize(winMutex_staticMutexes) ){ |
| 919 (void)SQLITE_MISUSE_BKPT; |
| 920 return 0; |
| 921 } |
| 922 #endif |
| 923 p = &winMutex_staticMutexes[iType-2]; |
| 924 p->id = iType; |
| 925 #ifdef SQLITE_DEBUG |
| 926 #ifdef SQLITE_WIN32_MUTEX_TRACE_STATIC |
| 927 p->trace = 1; |
| 928 #endif |
| 929 #endif |
| 930 break; |
| 931 } |
| 932 } |
| 933 return p; |
| 934 } |
| 935 |
| 936 |
| 937 /* |
| 938 ** This routine deallocates a previously |
| 939 ** allocated mutex. SQLite is careful to deallocate every |
| 940 ** mutex that it allocates. |
| 941 */ |
| 942 static void winMutexFree(sqlite3_mutex *p){ |
| 943 assert( p ); |
| 944 assert( p->nRef==0 && p->owner==0 ); |
| 945 if( p->id==SQLITE_MUTEX_FAST || p->id==SQLITE_MUTEX_RECURSIVE ){ |
| 946 DeleteCriticalSection(&p->mutex); |
| 947 sqlite3_free(p); |
| 948 }else{ |
| 949 #ifdef SQLITE_ENABLE_API_ARMOR |
| 950 (void)SQLITE_MISUSE_BKPT; |
| 951 #endif |
| 952 } |
| 953 } |
| 954 |
| 955 /* |
| 956 ** The sqlite3_mutex_enter() and sqlite3_mutex_try() routines attempt |
| 957 ** to enter a mutex. If another thread is already within the mutex, |
| 958 ** sqlite3_mutex_enter() will block and sqlite3_mutex_try() will return |
| 959 ** SQLITE_BUSY. The sqlite3_mutex_try() interface returns SQLITE_OK |
| 960 ** upon successful entry. Mutexes created using SQLITE_MUTEX_RECURSIVE can |
| 961 ** be entered multiple times by the same thread. In such cases the, |
| 962 ** mutex must be exited an equal number of times before another thread |
| 963 ** can enter. If the same thread tries to enter any other kind of mutex |
| 964 ** more than once, the behavior is undefined. |
| 965 */ |
| 966 static void winMutexEnter(sqlite3_mutex *p){ |
| 967 #if defined(SQLITE_DEBUG) || defined(SQLITE_TEST) |
| 968 DWORD tid = GetCurrentThreadId(); |
| 969 #endif |
| 970 #ifdef SQLITE_DEBUG |
| 971 assert( p ); |
| 972 assert( p->id==SQLITE_MUTEX_RECURSIVE || winMutexNotheld2(p, tid) ); |
| 973 #else |
| 974 assert( p ); |
| 975 #endif |
| 976 assert( winMutex_isInit==1 ); |
| 977 EnterCriticalSection(&p->mutex); |
| 978 #ifdef SQLITE_DEBUG |
| 979 assert( p->nRef>0 || p->owner==0 ); |
| 980 p->owner = tid; |
| 981 p->nRef++; |
| 982 if( p->trace ){ |
| 983 OSTRACE(("ENTER-MUTEX tid=%lu, mutex=%p (%d), nRef=%d\n", |
| 984 tid, p, p->trace, p->nRef)); |
| 985 } |
| 986 #endif |
| 987 } |
| 988 |
| 989 static int winMutexTry(sqlite3_mutex *p){ |
| 990 #if defined(SQLITE_DEBUG) || defined(SQLITE_TEST) |
| 991 DWORD tid = GetCurrentThreadId(); |
| 992 #endif |
| 993 int rc = SQLITE_BUSY; |
| 994 assert( p ); |
| 995 assert( p->id==SQLITE_MUTEX_RECURSIVE || winMutexNotheld2(p, tid) ); |
| 996 /* |
| 997 ** The sqlite3_mutex_try() routine is very rarely used, and when it |
| 998 ** is used it is merely an optimization. So it is OK for it to always |
| 999 ** fail. |
| 1000 ** |
| 1001 ** The TryEnterCriticalSection() interface is only available on WinNT. |
| 1002 ** And some windows compilers complain if you try to use it without |
| 1003 ** first doing some #defines that prevent SQLite from building on Win98. |
| 1004 ** For that reason, we will omit this optimization for now. See |
| 1005 ** ticket #2685. |
| 1006 */ |
| 1007 #if defined(_WIN32_WINNT) && _WIN32_WINNT >= 0x0400 |
| 1008 assert( winMutex_isInit==1 ); |
| 1009 assert( winMutex_isNt>=-1 && winMutex_isNt<=1 ); |
| 1010 if( winMutex_isNt<0 ){ |
| 1011 winMutex_isNt = sqlite3_win32_is_nt(); |
| 1012 } |
| 1013 assert( winMutex_isNt==0 || winMutex_isNt==1 ); |
| 1014 if( winMutex_isNt && TryEnterCriticalSection(&p->mutex) ){ |
| 1015 #ifdef SQLITE_DEBUG |
| 1016 p->owner = tid; |
| 1017 p->nRef++; |
| 1018 #endif |
| 1019 rc = SQLITE_OK; |
| 1020 } |
| 1021 #else |
| 1022 UNUSED_PARAMETER(p); |
| 1023 #endif |
| 1024 #ifdef SQLITE_DEBUG |
| 1025 if( p->trace ){ |
| 1026 OSTRACE(("TRY-MUTEX tid=%lu, mutex=%p (%d), owner=%lu, nRef=%d, rc=%s\n", |
| 1027 tid, p, p->trace, p->owner, p->nRef, sqlite3ErrName(rc))); |
| 1028 } |
| 1029 #endif |
| 1030 return rc; |
| 1031 } |
| 1032 |
| 1033 /* |
| 1034 ** The sqlite3_mutex_leave() routine exits a mutex that was |
| 1035 ** previously entered by the same thread. The behavior |
| 1036 ** is undefined if the mutex is not currently entered or |
| 1037 ** is not currently allocated. SQLite will never do either. |
| 1038 */ |
| 1039 static void winMutexLeave(sqlite3_mutex *p){ |
| 1040 #if defined(SQLITE_DEBUG) || defined(SQLITE_TEST) |
| 1041 DWORD tid = GetCurrentThreadId(); |
| 1042 #endif |
| 1043 assert( p ); |
| 1044 #ifdef SQLITE_DEBUG |
| 1045 assert( p->nRef>0 ); |
| 1046 assert( p->owner==tid ); |
| 1047 p->nRef--; |
| 1048 if( p->nRef==0 ) p->owner = 0; |
| 1049 assert( p->nRef==0 || p->id==SQLITE_MUTEX_RECURSIVE ); |
| 1050 #endif |
| 1051 assert( winMutex_isInit==1 ); |
| 1052 LeaveCriticalSection(&p->mutex); |
| 1053 #ifdef SQLITE_DEBUG |
| 1054 if( p->trace ){ |
| 1055 OSTRACE(("LEAVE-MUTEX tid=%lu, mutex=%p (%d), nRef=%d\n", |
| 1056 tid, p, p->trace, p->nRef)); |
| 1057 } |
| 1058 #endif |
| 1059 } |
| 1060 |
| 1061 SQLITE_PRIVATE sqlite3_mutex_methods const *sqlite3DefaultMutex(void){ |
| 1062 static const sqlite3_mutex_methods sMutex = { |
| 1063 winMutexInit, |
| 1064 winMutexEnd, |
| 1065 winMutexAlloc, |
| 1066 winMutexFree, |
| 1067 winMutexEnter, |
| 1068 winMutexTry, |
| 1069 winMutexLeave, |
| 1070 #ifdef SQLITE_DEBUG |
| 1071 winMutexHeld, |
| 1072 winMutexNotheld |
| 1073 #else |
| 1074 0, |
| 1075 0 |
| 1076 #endif |
| 1077 }; |
| 1078 return &sMutex; |
| 1079 } |
| 1080 |
| 1081 #endif /* SQLITE_MUTEX_W32 */ |
| 1082 |
| 1083 /************** End of mutex_w32.c *******************************************/ |
| 1084 /************** Begin file malloc.c ******************************************/ |
| 1085 /* |
| 1086 ** 2001 September 15 |
| 1087 ** |
| 1088 ** The author disclaims copyright to this source code. In place of |
| 1089 ** a legal notice, here is a blessing: |
| 1090 ** |
| 1091 ** May you do good and not evil. |
| 1092 ** May you find forgiveness for yourself and forgive others. |
| 1093 ** May you share freely, never taking more than you give. |
| 1094 ** |
| 1095 ************************************************************************* |
| 1096 ** |
| 1097 ** Memory allocation functions used throughout sqlite. |
| 1098 */ |
| 1099 /* #include "sqliteInt.h" */ |
| 1100 /* #include <stdarg.h> */ |
| 1101 |
| 1102 /* |
| 1103 ** Attempt to release up to n bytes of non-essential memory currently |
| 1104 ** held by SQLite. An example of non-essential memory is memory used to |
| 1105 ** cache database pages that are not currently in use. |
| 1106 */ |
| 1107 SQLITE_API int SQLITE_STDCALL sqlite3_release_memory(int n){ |
| 1108 #ifdef SQLITE_ENABLE_MEMORY_MANAGEMENT |
| 1109 return sqlite3PcacheReleaseMemory(n); |
| 1110 #else |
| 1111 /* IMPLEMENTATION-OF: R-34391-24921 The sqlite3_release_memory() routine |
| 1112 ** is a no-op returning zero if SQLite is not compiled with |
| 1113 ** SQLITE_ENABLE_MEMORY_MANAGEMENT. */ |
| 1114 UNUSED_PARAMETER(n); |
| 1115 return 0; |
| 1116 #endif |
| 1117 } |
| 1118 |
| 1119 /* |
| 1120 ** An instance of the following object records the location of |
| 1121 ** each unused scratch buffer. |
| 1122 */ |
| 1123 typedef struct ScratchFreeslot { |
| 1124 struct ScratchFreeslot *pNext; /* Next unused scratch buffer */ |
| 1125 } ScratchFreeslot; |
| 1126 |
| 1127 /* |
| 1128 ** State information local to the memory allocation subsystem. |
| 1129 */ |
| 1130 static SQLITE_WSD struct Mem0Global { |
| 1131 sqlite3_mutex *mutex; /* Mutex to serialize access */ |
| 1132 sqlite3_int64 alarmThreshold; /* The soft heap limit */ |
| 1133 |
| 1134 /* |
| 1135 ** Pointers to the end of sqlite3GlobalConfig.pScratch memory |
| 1136 ** (so that a range test can be used to determine if an allocation |
| 1137 ** being freed came from pScratch) and a pointer to the list of |
| 1138 ** unused scratch allocations. |
| 1139 */ |
| 1140 void *pScratchEnd; |
| 1141 ScratchFreeslot *pScratchFree; |
| 1142 u32 nScratchFree; |
| 1143 |
| 1144 /* |
| 1145 ** True if heap is nearly "full" where "full" is defined by the |
| 1146 ** sqlite3_soft_heap_limit() setting. |
| 1147 */ |
| 1148 int nearlyFull; |
| 1149 } mem0 = { 0, 0, 0, 0, 0, 0 }; |
| 1150 |
| 1151 #define mem0 GLOBAL(struct Mem0Global, mem0) |
| 1152 |
| 1153 /* |
| 1154 ** Return the memory allocator mutex. sqlite3_status() needs it. |
| 1155 */ |
| 1156 SQLITE_PRIVATE sqlite3_mutex *sqlite3MallocMutex(void){ |
| 1157 return mem0.mutex; |
| 1158 } |
| 1159 |
| 1160 #ifndef SQLITE_OMIT_DEPRECATED |
| 1161 /* |
| 1162 ** Deprecated external interface. It used to set an alarm callback |
| 1163 ** that was invoked when memory usage grew too large. Now it is a |
| 1164 ** no-op. |
| 1165 */ |
| 1166 SQLITE_API int SQLITE_STDCALL sqlite3_memory_alarm( |
| 1167 void(*xCallback)(void *pArg, sqlite3_int64 used,int N), |
| 1168 void *pArg, |
| 1169 sqlite3_int64 iThreshold |
| 1170 ){ |
| 1171 (void)xCallback; |
| 1172 (void)pArg; |
| 1173 (void)iThreshold; |
| 1174 return SQLITE_OK; |
| 1175 } |
| 1176 #endif |
| 1177 |
| 1178 /* |
| 1179 ** Set the soft heap-size limit for the library. Passing a zero or |
| 1180 ** negative value indicates no limit. |
| 1181 */ |
| 1182 SQLITE_API sqlite3_int64 SQLITE_STDCALL sqlite3_soft_heap_limit64(sqlite3_int64
n){ |
| 1183 sqlite3_int64 priorLimit; |
| 1184 sqlite3_int64 excess; |
| 1185 sqlite3_int64 nUsed; |
| 1186 #ifndef SQLITE_OMIT_AUTOINIT |
| 1187 int rc = sqlite3_initialize(); |
| 1188 if( rc ) return -1; |
| 1189 #endif |
| 1190 sqlite3_mutex_enter(mem0.mutex); |
| 1191 priorLimit = mem0.alarmThreshold; |
| 1192 if( n<0 ){ |
| 1193 sqlite3_mutex_leave(mem0.mutex); |
| 1194 return priorLimit; |
| 1195 } |
| 1196 mem0.alarmThreshold = n; |
| 1197 nUsed = sqlite3StatusValue(SQLITE_STATUS_MEMORY_USED); |
| 1198 mem0.nearlyFull = (n>0 && n<=nUsed); |
| 1199 sqlite3_mutex_leave(mem0.mutex); |
| 1200 excess = sqlite3_memory_used() - n; |
| 1201 if( excess>0 ) sqlite3_release_memory((int)(excess & 0x7fffffff)); |
| 1202 return priorLimit; |
| 1203 } |
| 1204 SQLITE_API void SQLITE_STDCALL sqlite3_soft_heap_limit(int n){ |
| 1205 if( n<0 ) n = 0; |
| 1206 sqlite3_soft_heap_limit64(n); |
| 1207 } |
| 1208 |
| 1209 /* |
| 1210 ** Initialize the memory allocation subsystem. |
| 1211 */ |
| 1212 SQLITE_PRIVATE int sqlite3MallocInit(void){ |
| 1213 int rc; |
| 1214 if( sqlite3GlobalConfig.m.xMalloc==0 ){ |
| 1215 sqlite3MemSetDefault(); |
| 1216 } |
| 1217 memset(&mem0, 0, sizeof(mem0)); |
| 1218 mem0.mutex = sqlite3MutexAlloc(SQLITE_MUTEX_STATIC_MEM); |
| 1219 if( sqlite3GlobalConfig.pScratch && sqlite3GlobalConfig.szScratch>=100 |
| 1220 && sqlite3GlobalConfig.nScratch>0 ){ |
| 1221 int i, n, sz; |
| 1222 ScratchFreeslot *pSlot; |
| 1223 sz = ROUNDDOWN8(sqlite3GlobalConfig.szScratch); |
| 1224 sqlite3GlobalConfig.szScratch = sz; |
| 1225 pSlot = (ScratchFreeslot*)sqlite3GlobalConfig.pScratch; |
| 1226 n = sqlite3GlobalConfig.nScratch; |
| 1227 mem0.pScratchFree = pSlot; |
| 1228 mem0.nScratchFree = n; |
| 1229 for(i=0; i<n-1; i++){ |
| 1230 pSlot->pNext = (ScratchFreeslot*)(sz+(char*)pSlot); |
| 1231 pSlot = pSlot->pNext; |
| 1232 } |
| 1233 pSlot->pNext = 0; |
| 1234 mem0.pScratchEnd = (void*)&pSlot[1]; |
| 1235 }else{ |
| 1236 mem0.pScratchEnd = 0; |
| 1237 sqlite3GlobalConfig.pScratch = 0; |
| 1238 sqlite3GlobalConfig.szScratch = 0; |
| 1239 sqlite3GlobalConfig.nScratch = 0; |
| 1240 } |
| 1241 if( sqlite3GlobalConfig.pPage==0 || sqlite3GlobalConfig.szPage<512 |
| 1242 || sqlite3GlobalConfig.nPage<=0 ){ |
| 1243 sqlite3GlobalConfig.pPage = 0; |
| 1244 sqlite3GlobalConfig.szPage = 0; |
| 1245 } |
| 1246 rc = sqlite3GlobalConfig.m.xInit(sqlite3GlobalConfig.m.pAppData); |
| 1247 if( rc!=SQLITE_OK ) memset(&mem0, 0, sizeof(mem0)); |
| 1248 return rc; |
| 1249 } |
| 1250 |
| 1251 /* |
| 1252 ** Return true if the heap is currently under memory pressure - in other |
| 1253 ** words if the amount of heap used is close to the limit set by |
| 1254 ** sqlite3_soft_heap_limit(). |
| 1255 */ |
| 1256 SQLITE_PRIVATE int sqlite3HeapNearlyFull(void){ |
| 1257 return mem0.nearlyFull; |
| 1258 } |
| 1259 |
| 1260 /* |
| 1261 ** Deinitialize the memory allocation subsystem. |
| 1262 */ |
| 1263 SQLITE_PRIVATE void sqlite3MallocEnd(void){ |
| 1264 if( sqlite3GlobalConfig.m.xShutdown ){ |
| 1265 sqlite3GlobalConfig.m.xShutdown(sqlite3GlobalConfig.m.pAppData); |
| 1266 } |
| 1267 memset(&mem0, 0, sizeof(mem0)); |
| 1268 } |
| 1269 |
| 1270 /* |
| 1271 ** Return the amount of memory currently checked out. |
| 1272 */ |
| 1273 SQLITE_API sqlite3_int64 SQLITE_STDCALL sqlite3_memory_used(void){ |
| 1274 sqlite3_int64 res, mx; |
| 1275 sqlite3_status64(SQLITE_STATUS_MEMORY_USED, &res, &mx, 0); |
| 1276 return res; |
| 1277 } |
| 1278 |
| 1279 /* |
| 1280 ** Return the maximum amount of memory that has ever been |
| 1281 ** checked out since either the beginning of this process |
| 1282 ** or since the most recent reset. |
| 1283 */ |
| 1284 SQLITE_API sqlite3_int64 SQLITE_STDCALL sqlite3_memory_highwater(int resetFlag){ |
| 1285 sqlite3_int64 res, mx; |
| 1286 sqlite3_status64(SQLITE_STATUS_MEMORY_USED, &res, &mx, resetFlag); |
| 1287 return mx; |
| 1288 } |
| 1289 |
| 1290 /* |
| 1291 ** Trigger the alarm |
| 1292 */ |
| 1293 static void sqlite3MallocAlarm(int nByte){ |
| 1294 if( mem0.alarmThreshold<=0 ) return; |
| 1295 sqlite3_mutex_leave(mem0.mutex); |
| 1296 sqlite3_release_memory(nByte); |
| 1297 sqlite3_mutex_enter(mem0.mutex); |
| 1298 } |
| 1299 |
| 1300 /* |
| 1301 ** Do a memory allocation with statistics and alarms. Assume the |
| 1302 ** lock is already held. |
| 1303 */ |
| 1304 static int mallocWithAlarm(int n, void **pp){ |
| 1305 int nFull; |
| 1306 void *p; |
| 1307 assert( sqlite3_mutex_held(mem0.mutex) ); |
| 1308 nFull = sqlite3GlobalConfig.m.xRoundup(n); |
| 1309 sqlite3StatusHighwater(SQLITE_STATUS_MALLOC_SIZE, n); |
| 1310 if( mem0.alarmThreshold>0 ){ |
| 1311 sqlite3_int64 nUsed = sqlite3StatusValue(SQLITE_STATUS_MEMORY_USED); |
| 1312 if( nUsed >= mem0.alarmThreshold - nFull ){ |
| 1313 mem0.nearlyFull = 1; |
| 1314 sqlite3MallocAlarm(nFull); |
| 1315 }else{ |
| 1316 mem0.nearlyFull = 0; |
| 1317 } |
| 1318 } |
| 1319 p = sqlite3GlobalConfig.m.xMalloc(nFull); |
| 1320 #ifdef SQLITE_ENABLE_MEMORY_MANAGEMENT |
| 1321 if( p==0 && mem0.alarmThreshold>0 ){ |
| 1322 sqlite3MallocAlarm(nFull); |
| 1323 p = sqlite3GlobalConfig.m.xMalloc(nFull); |
| 1324 } |
| 1325 #endif |
| 1326 if( p ){ |
| 1327 nFull = sqlite3MallocSize(p); |
| 1328 sqlite3StatusUp(SQLITE_STATUS_MEMORY_USED, nFull); |
| 1329 sqlite3StatusUp(SQLITE_STATUS_MALLOC_COUNT, 1); |
| 1330 } |
| 1331 *pp = p; |
| 1332 return nFull; |
| 1333 } |
| 1334 |
| 1335 /* |
| 1336 ** Allocate memory. This routine is like sqlite3_malloc() except that it |
| 1337 ** assumes the memory subsystem has already been initialized. |
| 1338 */ |
| 1339 SQLITE_PRIVATE void *sqlite3Malloc(u64 n){ |
| 1340 void *p; |
| 1341 if( n==0 || n>=0x7fffff00 ){ |
| 1342 /* A memory allocation of a number of bytes which is near the maximum |
| 1343 ** signed integer value might cause an integer overflow inside of the |
| 1344 ** xMalloc(). Hence we limit the maximum size to 0x7fffff00, giving |
| 1345 ** 255 bytes of overhead. SQLite itself will never use anything near |
| 1346 ** this amount. The only way to reach the limit is with sqlite3_malloc() */ |
| 1347 p = 0; |
| 1348 }else if( sqlite3GlobalConfig.bMemstat ){ |
| 1349 sqlite3_mutex_enter(mem0.mutex); |
| 1350 mallocWithAlarm((int)n, &p); |
| 1351 sqlite3_mutex_leave(mem0.mutex); |
| 1352 }else{ |
| 1353 p = sqlite3GlobalConfig.m.xMalloc((int)n); |
| 1354 } |
| 1355 assert( EIGHT_BYTE_ALIGNMENT(p) ); /* IMP: R-11148-40995 */ |
| 1356 return p; |
| 1357 } |
| 1358 |
| 1359 /* |
| 1360 ** This version of the memory allocation is for use by the application. |
| 1361 ** First make sure the memory subsystem is initialized, then do the |
| 1362 ** allocation. |
| 1363 */ |
| 1364 SQLITE_API void *SQLITE_STDCALL sqlite3_malloc(int n){ |
| 1365 #ifndef SQLITE_OMIT_AUTOINIT |
| 1366 if( sqlite3_initialize() ) return 0; |
| 1367 #endif |
| 1368 return n<=0 ? 0 : sqlite3Malloc(n); |
| 1369 } |
| 1370 SQLITE_API void *SQLITE_STDCALL sqlite3_malloc64(sqlite3_uint64 n){ |
| 1371 #ifndef SQLITE_OMIT_AUTOINIT |
| 1372 if( sqlite3_initialize() ) return 0; |
| 1373 #endif |
| 1374 return sqlite3Malloc(n); |
| 1375 } |
| 1376 |
| 1377 /* |
| 1378 ** Each thread may only have a single outstanding allocation from |
| 1379 ** xScratchMalloc(). We verify this constraint in the single-threaded |
| 1380 ** case by setting scratchAllocOut to 1 when an allocation |
| 1381 ** is outstanding clearing it when the allocation is freed. |
| 1382 */ |
| 1383 #if SQLITE_THREADSAFE==0 && !defined(NDEBUG) |
| 1384 static int scratchAllocOut = 0; |
| 1385 #endif |
| 1386 |
| 1387 |
| 1388 /* |
| 1389 ** Allocate memory that is to be used and released right away. |
| 1390 ** This routine is similar to alloca() in that it is not intended |
| 1391 ** for situations where the memory might be held long-term. This |
| 1392 ** routine is intended to get memory to old large transient data |
| 1393 ** structures that would not normally fit on the stack of an |
| 1394 ** embedded processor. |
| 1395 */ |
| 1396 SQLITE_PRIVATE void *sqlite3ScratchMalloc(int n){ |
| 1397 void *p; |
| 1398 assert( n>0 ); |
| 1399 |
| 1400 sqlite3_mutex_enter(mem0.mutex); |
| 1401 sqlite3StatusHighwater(SQLITE_STATUS_SCRATCH_SIZE, n); |
| 1402 if( mem0.nScratchFree && sqlite3GlobalConfig.szScratch>=n ){ |
| 1403 p = mem0.pScratchFree; |
| 1404 mem0.pScratchFree = mem0.pScratchFree->pNext; |
| 1405 mem0.nScratchFree--; |
| 1406 sqlite3StatusUp(SQLITE_STATUS_SCRATCH_USED, 1); |
| 1407 sqlite3_mutex_leave(mem0.mutex); |
| 1408 }else{ |
| 1409 sqlite3_mutex_leave(mem0.mutex); |
| 1410 p = sqlite3Malloc(n); |
| 1411 if( sqlite3GlobalConfig.bMemstat && p ){ |
| 1412 sqlite3_mutex_enter(mem0.mutex); |
| 1413 sqlite3StatusUp(SQLITE_STATUS_SCRATCH_OVERFLOW, sqlite3MallocSize(p)); |
| 1414 sqlite3_mutex_leave(mem0.mutex); |
| 1415 } |
| 1416 sqlite3MemdebugSetType(p, MEMTYPE_SCRATCH); |
| 1417 } |
| 1418 assert( sqlite3_mutex_notheld(mem0.mutex) ); |
| 1419 |
| 1420 |
| 1421 #if SQLITE_THREADSAFE==0 && !defined(NDEBUG) |
| 1422 /* EVIDENCE-OF: R-12970-05880 SQLite will not use more than one scratch |
| 1423 ** buffers per thread. |
| 1424 ** |
| 1425 ** This can only be checked in single-threaded mode. |
| 1426 */ |
| 1427 assert( scratchAllocOut==0 ); |
| 1428 if( p ) scratchAllocOut++; |
| 1429 #endif |
| 1430 |
| 1431 return p; |
| 1432 } |
| 1433 SQLITE_PRIVATE void sqlite3ScratchFree(void *p){ |
| 1434 if( p ){ |
| 1435 |
| 1436 #if SQLITE_THREADSAFE==0 && !defined(NDEBUG) |
| 1437 /* Verify that no more than two scratch allocation per thread |
| 1438 ** is outstanding at one time. (This is only checked in the |
| 1439 ** single-threaded case since checking in the multi-threaded case |
| 1440 ** would be much more complicated.) */ |
| 1441 assert( scratchAllocOut>=1 && scratchAllocOut<=2 ); |
| 1442 scratchAllocOut--; |
| 1443 #endif |
| 1444 |
| 1445 if( SQLITE_WITHIN(p, sqlite3GlobalConfig.pScratch, mem0.pScratchEnd) ){ |
| 1446 /* Release memory from the SQLITE_CONFIG_SCRATCH allocation */ |
| 1447 ScratchFreeslot *pSlot; |
| 1448 pSlot = (ScratchFreeslot*)p; |
| 1449 sqlite3_mutex_enter(mem0.mutex); |
| 1450 pSlot->pNext = mem0.pScratchFree; |
| 1451 mem0.pScratchFree = pSlot; |
| 1452 mem0.nScratchFree++; |
| 1453 assert( mem0.nScratchFree <= (u32)sqlite3GlobalConfig.nScratch ); |
| 1454 sqlite3StatusDown(SQLITE_STATUS_SCRATCH_USED, 1); |
| 1455 sqlite3_mutex_leave(mem0.mutex); |
| 1456 }else{ |
| 1457 /* Release memory back to the heap */ |
| 1458 assert( sqlite3MemdebugHasType(p, MEMTYPE_SCRATCH) ); |
| 1459 assert( sqlite3MemdebugNoType(p, (u8)~MEMTYPE_SCRATCH) ); |
| 1460 sqlite3MemdebugSetType(p, MEMTYPE_HEAP); |
| 1461 if( sqlite3GlobalConfig.bMemstat ){ |
| 1462 int iSize = sqlite3MallocSize(p); |
| 1463 sqlite3_mutex_enter(mem0.mutex); |
| 1464 sqlite3StatusDown(SQLITE_STATUS_SCRATCH_OVERFLOW, iSize); |
| 1465 sqlite3StatusDown(SQLITE_STATUS_MEMORY_USED, iSize); |
| 1466 sqlite3StatusDown(SQLITE_STATUS_MALLOC_COUNT, 1); |
| 1467 sqlite3GlobalConfig.m.xFree(p); |
| 1468 sqlite3_mutex_leave(mem0.mutex); |
| 1469 }else{ |
| 1470 sqlite3GlobalConfig.m.xFree(p); |
| 1471 } |
| 1472 } |
| 1473 } |
| 1474 } |
| 1475 |
| 1476 /* |
| 1477 ** TRUE if p is a lookaside memory allocation from db |
| 1478 */ |
| 1479 #ifndef SQLITE_OMIT_LOOKASIDE |
| 1480 static int isLookaside(sqlite3 *db, void *p){ |
| 1481 return SQLITE_WITHIN(p, db->lookaside.pStart, db->lookaside.pEnd); |
| 1482 } |
| 1483 #else |
| 1484 #define isLookaside(A,B) 0 |
| 1485 #endif |
| 1486 |
| 1487 /* |
| 1488 ** Return the size of a memory allocation previously obtained from |
| 1489 ** sqlite3Malloc() or sqlite3_malloc(). |
| 1490 */ |
| 1491 SQLITE_PRIVATE int sqlite3MallocSize(void *p){ |
| 1492 assert( sqlite3MemdebugHasType(p, MEMTYPE_HEAP) ); |
| 1493 return sqlite3GlobalConfig.m.xSize(p); |
| 1494 } |
| 1495 SQLITE_PRIVATE int sqlite3DbMallocSize(sqlite3 *db, void *p){ |
| 1496 assert( p!=0 ); |
| 1497 if( db==0 || !isLookaside(db,p) ){ |
| 1498 #if SQLITE_DEBUG |
| 1499 if( db==0 ){ |
| 1500 assert( sqlite3MemdebugNoType(p, (u8)~MEMTYPE_HEAP) ); |
| 1501 assert( sqlite3MemdebugHasType(p, MEMTYPE_HEAP) ); |
| 1502 }else{ |
| 1503 assert( sqlite3MemdebugHasType(p, (MEMTYPE_LOOKASIDE|MEMTYPE_HEAP)) ); |
| 1504 assert( sqlite3MemdebugNoType(p, (u8)~(MEMTYPE_LOOKASIDE|MEMTYPE_HEAP)) ); |
| 1505 } |
| 1506 #endif |
| 1507 return sqlite3GlobalConfig.m.xSize(p); |
| 1508 }else{ |
| 1509 assert( sqlite3_mutex_held(db->mutex) ); |
| 1510 return db->lookaside.sz; |
| 1511 } |
| 1512 } |
| 1513 SQLITE_API sqlite3_uint64 SQLITE_STDCALL sqlite3_msize(void *p){ |
| 1514 assert( sqlite3MemdebugNoType(p, (u8)~MEMTYPE_HEAP) ); |
| 1515 assert( sqlite3MemdebugHasType(p, MEMTYPE_HEAP) ); |
| 1516 return p ? sqlite3GlobalConfig.m.xSize(p) : 0; |
| 1517 } |
| 1518 |
| 1519 /* |
| 1520 ** Free memory previously obtained from sqlite3Malloc(). |
| 1521 */ |
| 1522 SQLITE_API void SQLITE_STDCALL sqlite3_free(void *p){ |
| 1523 if( p==0 ) return; /* IMP: R-49053-54554 */ |
| 1524 assert( sqlite3MemdebugHasType(p, MEMTYPE_HEAP) ); |
| 1525 assert( sqlite3MemdebugNoType(p, (u8)~MEMTYPE_HEAP) ); |
| 1526 if( sqlite3GlobalConfig.bMemstat ){ |
| 1527 sqlite3_mutex_enter(mem0.mutex); |
| 1528 sqlite3StatusDown(SQLITE_STATUS_MEMORY_USED, sqlite3MallocSize(p)); |
| 1529 sqlite3StatusDown(SQLITE_STATUS_MALLOC_COUNT, 1); |
| 1530 sqlite3GlobalConfig.m.xFree(p); |
| 1531 sqlite3_mutex_leave(mem0.mutex); |
| 1532 }else{ |
| 1533 sqlite3GlobalConfig.m.xFree(p); |
| 1534 } |
| 1535 } |
| 1536 |
| 1537 /* |
| 1538 ** Add the size of memory allocation "p" to the count in |
| 1539 ** *db->pnBytesFreed. |
| 1540 */ |
| 1541 static SQLITE_NOINLINE void measureAllocationSize(sqlite3 *db, void *p){ |
| 1542 *db->pnBytesFreed += sqlite3DbMallocSize(db,p); |
| 1543 } |
| 1544 |
| 1545 /* |
| 1546 ** Free memory that might be associated with a particular database |
| 1547 ** connection. |
| 1548 */ |
| 1549 SQLITE_PRIVATE void sqlite3DbFree(sqlite3 *db, void *p){ |
| 1550 assert( db==0 || sqlite3_mutex_held(db->mutex) ); |
| 1551 if( p==0 ) return; |
| 1552 if( db ){ |
| 1553 if( db->pnBytesFreed ){ |
| 1554 measureAllocationSize(db, p); |
| 1555 return; |
| 1556 } |
| 1557 if( isLookaside(db, p) ){ |
| 1558 LookasideSlot *pBuf = (LookasideSlot*)p; |
| 1559 #if SQLITE_DEBUG |
| 1560 /* Trash all content in the buffer being freed */ |
| 1561 memset(p, 0xaa, db->lookaside.sz); |
| 1562 #endif |
| 1563 pBuf->pNext = db->lookaside.pFree; |
| 1564 db->lookaside.pFree = pBuf; |
| 1565 db->lookaside.nOut--; |
| 1566 return; |
| 1567 } |
| 1568 } |
| 1569 assert( sqlite3MemdebugHasType(p, (MEMTYPE_LOOKASIDE|MEMTYPE_HEAP)) ); |
| 1570 assert( sqlite3MemdebugNoType(p, (u8)~(MEMTYPE_LOOKASIDE|MEMTYPE_HEAP)) ); |
| 1571 assert( db!=0 || sqlite3MemdebugNoType(p, MEMTYPE_LOOKASIDE) ); |
| 1572 sqlite3MemdebugSetType(p, MEMTYPE_HEAP); |
| 1573 sqlite3_free(p); |
| 1574 } |
| 1575 |
| 1576 /* |
| 1577 ** Change the size of an existing memory allocation |
| 1578 */ |
| 1579 SQLITE_PRIVATE void *sqlite3Realloc(void *pOld, u64 nBytes){ |
| 1580 int nOld, nNew, nDiff; |
| 1581 void *pNew; |
| 1582 assert( sqlite3MemdebugHasType(pOld, MEMTYPE_HEAP) ); |
| 1583 assert( sqlite3MemdebugNoType(pOld, (u8)~MEMTYPE_HEAP) ); |
| 1584 if( pOld==0 ){ |
| 1585 return sqlite3Malloc(nBytes); /* IMP: R-04300-56712 */ |
| 1586 } |
| 1587 if( nBytes==0 ){ |
| 1588 sqlite3_free(pOld); /* IMP: R-26507-47431 */ |
| 1589 return 0; |
| 1590 } |
| 1591 if( nBytes>=0x7fffff00 ){ |
| 1592 /* The 0x7ffff00 limit term is explained in comments on sqlite3Malloc() */ |
| 1593 return 0; |
| 1594 } |
| 1595 nOld = sqlite3MallocSize(pOld); |
| 1596 /* IMPLEMENTATION-OF: R-46199-30249 SQLite guarantees that the second |
| 1597 ** argument to xRealloc is always a value returned by a prior call to |
| 1598 ** xRoundup. */ |
| 1599 nNew = sqlite3GlobalConfig.m.xRoundup((int)nBytes); |
| 1600 if( nOld==nNew ){ |
| 1601 pNew = pOld; |
| 1602 }else if( sqlite3GlobalConfig.bMemstat ){ |
| 1603 sqlite3_mutex_enter(mem0.mutex); |
| 1604 sqlite3StatusHighwater(SQLITE_STATUS_MALLOC_SIZE, (int)nBytes); |
| 1605 nDiff = nNew - nOld; |
| 1606 if( sqlite3StatusValue(SQLITE_STATUS_MEMORY_USED) >= |
| 1607 mem0.alarmThreshold-nDiff ){ |
| 1608 sqlite3MallocAlarm(nDiff); |
| 1609 } |
| 1610 pNew = sqlite3GlobalConfig.m.xRealloc(pOld, nNew); |
| 1611 if( pNew==0 && mem0.alarmThreshold>0 ){ |
| 1612 sqlite3MallocAlarm((int)nBytes); |
| 1613 pNew = sqlite3GlobalConfig.m.xRealloc(pOld, nNew); |
| 1614 } |
| 1615 if( pNew ){ |
| 1616 nNew = sqlite3MallocSize(pNew); |
| 1617 sqlite3StatusUp(SQLITE_STATUS_MEMORY_USED, nNew-nOld); |
| 1618 } |
| 1619 sqlite3_mutex_leave(mem0.mutex); |
| 1620 }else{ |
| 1621 pNew = sqlite3GlobalConfig.m.xRealloc(pOld, nNew); |
| 1622 } |
| 1623 assert( EIGHT_BYTE_ALIGNMENT(pNew) ); /* IMP: R-11148-40995 */ |
| 1624 return pNew; |
| 1625 } |
| 1626 |
| 1627 /* |
| 1628 ** The public interface to sqlite3Realloc. Make sure that the memory |
| 1629 ** subsystem is initialized prior to invoking sqliteRealloc. |
| 1630 */ |
| 1631 SQLITE_API void *SQLITE_STDCALL sqlite3_realloc(void *pOld, int n){ |
| 1632 #ifndef SQLITE_OMIT_AUTOINIT |
| 1633 if( sqlite3_initialize() ) return 0; |
| 1634 #endif |
| 1635 if( n<0 ) n = 0; /* IMP: R-26507-47431 */ |
| 1636 return sqlite3Realloc(pOld, n); |
| 1637 } |
| 1638 SQLITE_API void *SQLITE_STDCALL sqlite3_realloc64(void *pOld, sqlite3_uint64 n){ |
| 1639 #ifndef SQLITE_OMIT_AUTOINIT |
| 1640 if( sqlite3_initialize() ) return 0; |
| 1641 #endif |
| 1642 return sqlite3Realloc(pOld, n); |
| 1643 } |
| 1644 |
| 1645 |
| 1646 /* |
| 1647 ** Allocate and zero memory. |
| 1648 */ |
| 1649 SQLITE_PRIVATE void *sqlite3MallocZero(u64 n){ |
| 1650 void *p = sqlite3Malloc(n); |
| 1651 if( p ){ |
| 1652 memset(p, 0, (size_t)n); |
| 1653 } |
| 1654 return p; |
| 1655 } |
| 1656 |
| 1657 /* |
| 1658 ** Allocate and zero memory. If the allocation fails, make |
| 1659 ** the mallocFailed flag in the connection pointer. |
| 1660 */ |
| 1661 SQLITE_PRIVATE void *sqlite3DbMallocZero(sqlite3 *db, u64 n){ |
| 1662 void *p = sqlite3DbMallocRaw(db, n); |
| 1663 if( p ){ |
| 1664 memset(p, 0, (size_t)n); |
| 1665 } |
| 1666 return p; |
| 1667 } |
| 1668 |
| 1669 /* |
| 1670 ** Allocate and zero memory. If the allocation fails, make |
| 1671 ** the mallocFailed flag in the connection pointer. |
| 1672 ** |
| 1673 ** If db!=0 and db->mallocFailed is true (indicating a prior malloc |
| 1674 ** failure on the same database connection) then always return 0. |
| 1675 ** Hence for a particular database connection, once malloc starts |
| 1676 ** failing, it fails consistently until mallocFailed is reset. |
| 1677 ** This is an important assumption. There are many places in the |
| 1678 ** code that do things like this: |
| 1679 ** |
| 1680 ** int *a = (int*)sqlite3DbMallocRaw(db, 100); |
| 1681 ** int *b = (int*)sqlite3DbMallocRaw(db, 200); |
| 1682 ** if( b ) a[10] = 9; |
| 1683 ** |
| 1684 ** In other words, if a subsequent malloc (ex: "b") worked, it is assumed |
| 1685 ** that all prior mallocs (ex: "a") worked too. |
| 1686 */ |
| 1687 SQLITE_PRIVATE void *sqlite3DbMallocRaw(sqlite3 *db, u64 n){ |
| 1688 void *p; |
| 1689 assert( db==0 || sqlite3_mutex_held(db->mutex) ); |
| 1690 assert( db==0 || db->pnBytesFreed==0 ); |
| 1691 #ifndef SQLITE_OMIT_LOOKASIDE |
| 1692 if( db ){ |
| 1693 LookasideSlot *pBuf; |
| 1694 if( db->mallocFailed ){ |
| 1695 return 0; |
| 1696 } |
| 1697 if( db->lookaside.bEnabled ){ |
| 1698 if( n>db->lookaside.sz ){ |
| 1699 db->lookaside.anStat[1]++; |
| 1700 }else if( (pBuf = db->lookaside.pFree)==0 ){ |
| 1701 db->lookaside.anStat[2]++; |
| 1702 }else{ |
| 1703 db->lookaside.pFree = pBuf->pNext; |
| 1704 db->lookaside.nOut++; |
| 1705 db->lookaside.anStat[0]++; |
| 1706 if( db->lookaside.nOut>db->lookaside.mxOut ){ |
| 1707 db->lookaside.mxOut = db->lookaside.nOut; |
| 1708 } |
| 1709 return (void*)pBuf; |
| 1710 } |
| 1711 } |
| 1712 } |
| 1713 #else |
| 1714 if( db && db->mallocFailed ){ |
| 1715 return 0; |
| 1716 } |
| 1717 #endif |
| 1718 p = sqlite3Malloc(n); |
| 1719 if( !p && db ){ |
| 1720 db->mallocFailed = 1; |
| 1721 } |
| 1722 sqlite3MemdebugSetType(p, |
| 1723 (db && db->lookaside.bEnabled) ? MEMTYPE_LOOKASIDE : MEMTYPE_HEAP); |
| 1724 return p; |
| 1725 } |
| 1726 |
| 1727 /* |
| 1728 ** Resize the block of memory pointed to by p to n bytes. If the |
| 1729 ** resize fails, set the mallocFailed flag in the connection object. |
| 1730 */ |
| 1731 SQLITE_PRIVATE void *sqlite3DbRealloc(sqlite3 *db, void *p, u64 n){ |
| 1732 void *pNew = 0; |
| 1733 assert( db!=0 ); |
| 1734 assert( sqlite3_mutex_held(db->mutex) ); |
| 1735 if( db->mallocFailed==0 ){ |
| 1736 if( p==0 ){ |
| 1737 return sqlite3DbMallocRaw(db, n); |
| 1738 } |
| 1739 if( isLookaside(db, p) ){ |
| 1740 if( n<=db->lookaside.sz ){ |
| 1741 return p; |
| 1742 } |
| 1743 pNew = sqlite3DbMallocRaw(db, n); |
| 1744 if( pNew ){ |
| 1745 memcpy(pNew, p, db->lookaside.sz); |
| 1746 sqlite3DbFree(db, p); |
| 1747 } |
| 1748 }else{ |
| 1749 assert( sqlite3MemdebugHasType(p, (MEMTYPE_LOOKASIDE|MEMTYPE_HEAP)) ); |
| 1750 assert( sqlite3MemdebugNoType(p, (u8)~(MEMTYPE_LOOKASIDE|MEMTYPE_HEAP)) ); |
| 1751 sqlite3MemdebugSetType(p, MEMTYPE_HEAP); |
| 1752 pNew = sqlite3_realloc64(p, n); |
| 1753 if( !pNew ){ |
| 1754 db->mallocFailed = 1; |
| 1755 } |
| 1756 sqlite3MemdebugSetType(pNew, |
| 1757 (db->lookaside.bEnabled ? MEMTYPE_LOOKASIDE : MEMTYPE_HEAP)); |
| 1758 } |
| 1759 } |
| 1760 return pNew; |
| 1761 } |
| 1762 |
| 1763 /* |
| 1764 ** Attempt to reallocate p. If the reallocation fails, then free p |
| 1765 ** and set the mallocFailed flag in the database connection. |
| 1766 */ |
| 1767 SQLITE_PRIVATE void *sqlite3DbReallocOrFree(sqlite3 *db, void *p, u64 n){ |
| 1768 void *pNew; |
| 1769 pNew = sqlite3DbRealloc(db, p, n); |
| 1770 if( !pNew ){ |
| 1771 sqlite3DbFree(db, p); |
| 1772 } |
| 1773 return pNew; |
| 1774 } |
| 1775 |
| 1776 /* |
| 1777 ** Make a copy of a string in memory obtained from sqliteMalloc(). These |
| 1778 ** functions call sqlite3MallocRaw() directly instead of sqliteMalloc(). This |
| 1779 ** is because when memory debugging is turned on, these two functions are |
| 1780 ** called via macros that record the current file and line number in the |
| 1781 ** ThreadData structure. |
| 1782 */ |
| 1783 SQLITE_PRIVATE char *sqlite3DbStrDup(sqlite3 *db, const char *z){ |
| 1784 char *zNew; |
| 1785 size_t n; |
| 1786 if( z==0 ){ |
| 1787 return 0; |
| 1788 } |
| 1789 n = sqlite3Strlen30(z) + 1; |
| 1790 assert( (n&0x7fffffff)==n ); |
| 1791 zNew = sqlite3DbMallocRaw(db, (int)n); |
| 1792 if( zNew ){ |
| 1793 memcpy(zNew, z, n); |
| 1794 } |
| 1795 return zNew; |
| 1796 } |
| 1797 SQLITE_PRIVATE char *sqlite3DbStrNDup(sqlite3 *db, const char *z, u64 n){ |
| 1798 char *zNew; |
| 1799 if( z==0 ){ |
| 1800 return 0; |
| 1801 } |
| 1802 assert( (n&0x7fffffff)==n ); |
| 1803 zNew = sqlite3DbMallocRaw(db, n+1); |
| 1804 if( zNew ){ |
| 1805 memcpy(zNew, z, (size_t)n); |
| 1806 zNew[n] = 0; |
| 1807 } |
| 1808 return zNew; |
| 1809 } |
| 1810 |
| 1811 /* |
| 1812 ** Free any prior content in *pz and replace it with a copy of zNew. |
| 1813 */ |
| 1814 SQLITE_PRIVATE void sqlite3SetString(char **pz, sqlite3 *db, const char *zNew){ |
| 1815 sqlite3DbFree(db, *pz); |
| 1816 *pz = sqlite3DbStrDup(db, zNew); |
| 1817 } |
| 1818 |
| 1819 /* |
| 1820 ** Take actions at the end of an API call to indicate an OOM error |
| 1821 */ |
| 1822 static SQLITE_NOINLINE int apiOomError(sqlite3 *db){ |
| 1823 db->mallocFailed = 0; |
| 1824 sqlite3Error(db, SQLITE_NOMEM); |
| 1825 return SQLITE_NOMEM; |
| 1826 } |
| 1827 |
| 1828 /* |
| 1829 ** This function must be called before exiting any API function (i.e. |
| 1830 ** returning control to the user) that has called sqlite3_malloc or |
| 1831 ** sqlite3_realloc. |
| 1832 ** |
| 1833 ** The returned value is normally a copy of the second argument to this |
| 1834 ** function. However, if a malloc() failure has occurred since the previous |
| 1835 ** invocation SQLITE_NOMEM is returned instead. |
| 1836 ** |
| 1837 ** If an OOM as occurred, then the connection error-code (the value |
| 1838 ** returned by sqlite3_errcode()) is set to SQLITE_NOMEM. |
| 1839 */ |
| 1840 SQLITE_PRIVATE int sqlite3ApiExit(sqlite3* db, int rc){ |
| 1841 /* If the db handle must hold the connection handle mutex here. |
| 1842 ** Otherwise the read (and possible write) of db->mallocFailed |
| 1843 ** is unsafe, as is the call to sqlite3Error(). |
| 1844 */ |
| 1845 assert( db!=0 ); |
| 1846 assert( sqlite3_mutex_held(db->mutex) ); |
| 1847 if( db->mallocFailed || rc==SQLITE_IOERR_NOMEM ){ |
| 1848 return apiOomError(db); |
| 1849 } |
| 1850 return rc & db->errMask; |
| 1851 } |
| 1852 |
| 1853 /************** End of malloc.c **********************************************/ |
| 1854 /************** Begin file printf.c ******************************************/ |
| 1855 /* |
| 1856 ** The "printf" code that follows dates from the 1980's. It is in |
| 1857 ** the public domain. |
| 1858 ** |
| 1859 ************************************************************************** |
| 1860 ** |
| 1861 ** This file contains code for a set of "printf"-like routines. These |
| 1862 ** routines format strings much like the printf() from the standard C |
| 1863 ** library, though the implementation here has enhancements to support |
| 1864 ** SQLite. |
| 1865 */ |
| 1866 /* #include "sqliteInt.h" */ |
| 1867 |
| 1868 /* |
| 1869 ** Conversion types fall into various categories as defined by the |
| 1870 ** following enumeration. |
| 1871 */ |
| 1872 #define etRADIX 1 /* Integer types. %d, %x, %o, and so forth */ |
| 1873 #define etFLOAT 2 /* Floating point. %f */ |
| 1874 #define etEXP 3 /* Exponentional notation. %e and %E */ |
| 1875 #define etGENERIC 4 /* Floating or exponential, depending on exponent. %g */ |
| 1876 #define etSIZE 5 /* Return number of characters processed so far. %n */ |
| 1877 #define etSTRING 6 /* Strings. %s */ |
| 1878 #define etDYNSTRING 7 /* Dynamically allocated strings. %z */ |
| 1879 #define etPERCENT 8 /* Percent symbol. %% */ |
| 1880 #define etCHARX 9 /* Characters. %c */ |
| 1881 /* The rest are extensions, not normally found in printf() */ |
| 1882 #define etSQLESCAPE 10 /* Strings with '\'' doubled. %q */ |
| 1883 #define etSQLESCAPE2 11 /* Strings with '\'' doubled and enclosed in '', |
| 1884 NULL pointers replaced by SQL NULL. %Q */ |
| 1885 #define etTOKEN 12 /* a pointer to a Token structure */ |
| 1886 #define etSRCLIST 13 /* a pointer to a SrcList */ |
| 1887 #define etPOINTER 14 /* The %p conversion */ |
| 1888 #define etSQLESCAPE3 15 /* %w -> Strings with '\"' doubled */ |
| 1889 #define etORDINAL 16 /* %r -> 1st, 2nd, 3rd, 4th, etc. English only */ |
| 1890 |
| 1891 #define etINVALID 0 /* Any unrecognized conversion type */ |
| 1892 |
| 1893 |
| 1894 /* |
| 1895 ** An "etByte" is an 8-bit unsigned value. |
| 1896 */ |
| 1897 typedef unsigned char etByte; |
| 1898 |
| 1899 /* |
| 1900 ** Each builtin conversion character (ex: the 'd' in "%d") is described |
| 1901 ** by an instance of the following structure |
| 1902 */ |
| 1903 typedef struct et_info { /* Information about each format field */ |
| 1904 char fmttype; /* The format field code letter */ |
| 1905 etByte base; /* The base for radix conversion */ |
| 1906 etByte flags; /* One or more of FLAG_ constants below */ |
| 1907 etByte type; /* Conversion paradigm */ |
| 1908 etByte charset; /* Offset into aDigits[] of the digits string */ |
| 1909 etByte prefix; /* Offset into aPrefix[] of the prefix string */ |
| 1910 } et_info; |
| 1911 |
| 1912 /* |
| 1913 ** Allowed values for et_info.flags |
| 1914 */ |
| 1915 #define FLAG_SIGNED 1 /* True if the value to convert is signed */ |
| 1916 #define FLAG_INTERN 2 /* True if for internal use only */ |
| 1917 #define FLAG_STRING 4 /* Allow infinity precision */ |
| 1918 |
| 1919 |
| 1920 /* |
| 1921 ** The following table is searched linearly, so it is good to put the |
| 1922 ** most frequently used conversion types first. |
| 1923 */ |
| 1924 static const char aDigits[] = "0123456789ABCDEF0123456789abcdef"; |
| 1925 static const char aPrefix[] = "-x0\000X0"; |
| 1926 static const et_info fmtinfo[] = { |
| 1927 { 'd', 10, 1, etRADIX, 0, 0 }, |
| 1928 { 's', 0, 4, etSTRING, 0, 0 }, |
| 1929 { 'g', 0, 1, etGENERIC, 30, 0 }, |
| 1930 { 'z', 0, 4, etDYNSTRING, 0, 0 }, |
| 1931 { 'q', 0, 4, etSQLESCAPE, 0, 0 }, |
| 1932 { 'Q', 0, 4, etSQLESCAPE2, 0, 0 }, |
| 1933 { 'w', 0, 4, etSQLESCAPE3, 0, 0 }, |
| 1934 { 'c', 0, 0, etCHARX, 0, 0 }, |
| 1935 { 'o', 8, 0, etRADIX, 0, 2 }, |
| 1936 { 'u', 10, 0, etRADIX, 0, 0 }, |
| 1937 { 'x', 16, 0, etRADIX, 16, 1 }, |
| 1938 { 'X', 16, 0, etRADIX, 0, 4 }, |
| 1939 #ifndef SQLITE_OMIT_FLOATING_POINT |
| 1940 { 'f', 0, 1, etFLOAT, 0, 0 }, |
| 1941 { 'e', 0, 1, etEXP, 30, 0 }, |
| 1942 { 'E', 0, 1, etEXP, 14, 0 }, |
| 1943 { 'G', 0, 1, etGENERIC, 14, 0 }, |
| 1944 #endif |
| 1945 { 'i', 10, 1, etRADIX, 0, 0 }, |
| 1946 { 'n', 0, 0, etSIZE, 0, 0 }, |
| 1947 { '%', 0, 0, etPERCENT, 0, 0 }, |
| 1948 { 'p', 16, 0, etPOINTER, 0, 1 }, |
| 1949 |
| 1950 /* All the rest have the FLAG_INTERN bit set and are thus for internal |
| 1951 ** use only */ |
| 1952 { 'T', 0, 2, etTOKEN, 0, 0 }, |
| 1953 { 'S', 0, 2, etSRCLIST, 0, 0 }, |
| 1954 { 'r', 10, 3, etORDINAL, 0, 0 }, |
| 1955 }; |
| 1956 |
| 1957 /* |
| 1958 ** If SQLITE_OMIT_FLOATING_POINT is defined, then none of the floating point |
| 1959 ** conversions will work. |
| 1960 */ |
| 1961 #ifndef SQLITE_OMIT_FLOATING_POINT |
| 1962 /* |
| 1963 ** "*val" is a double such that 0.1 <= *val < 10.0 |
| 1964 ** Return the ascii code for the leading digit of *val, then |
| 1965 ** multiply "*val" by 10.0 to renormalize. |
| 1966 ** |
| 1967 ** Example: |
| 1968 ** input: *val = 3.14159 |
| 1969 ** output: *val = 1.4159 function return = '3' |
| 1970 ** |
| 1971 ** The counter *cnt is incremented each time. After counter exceeds |
| 1972 ** 16 (the number of significant digits in a 64-bit float) '0' is |
| 1973 ** always returned. |
| 1974 */ |
| 1975 static char et_getdigit(LONGDOUBLE_TYPE *val, int *cnt){ |
| 1976 int digit; |
| 1977 LONGDOUBLE_TYPE d; |
| 1978 if( (*cnt)<=0 ) return '0'; |
| 1979 (*cnt)--; |
| 1980 digit = (int)*val; |
| 1981 d = digit; |
| 1982 digit += '0'; |
| 1983 *val = (*val - d)*10.0; |
| 1984 return (char)digit; |
| 1985 } |
| 1986 #endif /* SQLITE_OMIT_FLOATING_POINT */ |
| 1987 |
| 1988 /* |
| 1989 ** Set the StrAccum object to an error mode. |
| 1990 */ |
| 1991 static void setStrAccumError(StrAccum *p, u8 eError){ |
| 1992 assert( eError==STRACCUM_NOMEM || eError==STRACCUM_TOOBIG ); |
| 1993 p->accError = eError; |
| 1994 p->nAlloc = 0; |
| 1995 } |
| 1996 |
| 1997 /* |
| 1998 ** Extra argument values from a PrintfArguments object |
| 1999 */ |
| 2000 static sqlite3_int64 getIntArg(PrintfArguments *p){ |
| 2001 if( p->nArg<=p->nUsed ) return 0; |
| 2002 return sqlite3_value_int64(p->apArg[p->nUsed++]); |
| 2003 } |
| 2004 static double getDoubleArg(PrintfArguments *p){ |
| 2005 if( p->nArg<=p->nUsed ) return 0.0; |
| 2006 return sqlite3_value_double(p->apArg[p->nUsed++]); |
| 2007 } |
| 2008 static char *getTextArg(PrintfArguments *p){ |
| 2009 if( p->nArg<=p->nUsed ) return 0; |
| 2010 return (char*)sqlite3_value_text(p->apArg[p->nUsed++]); |
| 2011 } |
| 2012 |
| 2013 |
| 2014 /* |
| 2015 ** On machines with a small stack size, you can redefine the |
| 2016 ** SQLITE_PRINT_BUF_SIZE to be something smaller, if desired. |
| 2017 */ |
| 2018 #ifndef SQLITE_PRINT_BUF_SIZE |
| 2019 # define SQLITE_PRINT_BUF_SIZE 70 |
| 2020 #endif |
| 2021 #define etBUFSIZE SQLITE_PRINT_BUF_SIZE /* Size of the output buffer */ |
| 2022 |
| 2023 /* |
| 2024 ** Render a string given by "fmt" into the StrAccum object. |
| 2025 */ |
| 2026 SQLITE_PRIVATE void sqlite3VXPrintf( |
| 2027 StrAccum *pAccum, /* Accumulate results here */ |
| 2028 u32 bFlags, /* SQLITE_PRINTF_* flags */ |
| 2029 const char *fmt, /* Format string */ |
| 2030 va_list ap /* arguments */ |
| 2031 ){ |
| 2032 int c; /* Next character in the format string */ |
| 2033 char *bufpt; /* Pointer to the conversion buffer */ |
| 2034 int precision; /* Precision of the current field */ |
| 2035 int length; /* Length of the field */ |
| 2036 int idx; /* A general purpose loop counter */ |
| 2037 int width; /* Width of the current field */ |
| 2038 etByte flag_leftjustify; /* True if "-" flag is present */ |
| 2039 etByte flag_plussign; /* True if "+" flag is present */ |
| 2040 etByte flag_blanksign; /* True if " " flag is present */ |
| 2041 etByte flag_alternateform; /* True if "#" flag is present */ |
| 2042 etByte flag_altform2; /* True if "!" flag is present */ |
| 2043 etByte flag_zeropad; /* True if field width constant starts with zero */ |
| 2044 etByte flag_long; /* True if "l" flag is present */ |
| 2045 etByte flag_longlong; /* True if the "ll" flag is present */ |
| 2046 etByte done; /* Loop termination flag */ |
| 2047 etByte xtype = 0; /* Conversion paradigm */ |
| 2048 u8 bArgList; /* True for SQLITE_PRINTF_SQLFUNC */ |
| 2049 u8 useIntern; /* Ok to use internal conversions (ex: %T) */ |
| 2050 char prefix; /* Prefix character. "+" or "-" or " " or '\0'. */ |
| 2051 sqlite_uint64 longvalue; /* Value for integer types */ |
| 2052 LONGDOUBLE_TYPE realvalue; /* Value for real types */ |
| 2053 const et_info *infop; /* Pointer to the appropriate info structure */ |
| 2054 char *zOut; /* Rendering buffer */ |
| 2055 int nOut; /* Size of the rendering buffer */ |
| 2056 char *zExtra = 0; /* Malloced memory used by some conversion */ |
| 2057 #ifndef SQLITE_OMIT_FLOATING_POINT |
| 2058 int exp, e2; /* exponent of real numbers */ |
| 2059 int nsd; /* Number of significant digits returned */ |
| 2060 double rounder; /* Used for rounding floating point values */ |
| 2061 etByte flag_dp; /* True if decimal point should be shown */ |
| 2062 etByte flag_rtz; /* True if trailing zeros should be removed */ |
| 2063 #endif |
| 2064 PrintfArguments *pArgList = 0; /* Arguments for SQLITE_PRINTF_SQLFUNC */ |
| 2065 char buf[etBUFSIZE]; /* Conversion buffer */ |
| 2066 |
| 2067 bufpt = 0; |
| 2068 if( bFlags ){ |
| 2069 if( (bArgList = (bFlags & SQLITE_PRINTF_SQLFUNC))!=0 ){ |
| 2070 pArgList = va_arg(ap, PrintfArguments*); |
| 2071 } |
| 2072 useIntern = bFlags & SQLITE_PRINTF_INTERNAL; |
| 2073 }else{ |
| 2074 bArgList = useIntern = 0; |
| 2075 } |
| 2076 for(; (c=(*fmt))!=0; ++fmt){ |
| 2077 if( c!='%' ){ |
| 2078 bufpt = (char *)fmt; |
| 2079 #if HAVE_STRCHRNUL |
| 2080 fmt = strchrnul(fmt, '%'); |
| 2081 #else |
| 2082 do{ fmt++; }while( *fmt && *fmt != '%' ); |
| 2083 #endif |
| 2084 sqlite3StrAccumAppend(pAccum, bufpt, (int)(fmt - bufpt)); |
| 2085 if( *fmt==0 ) break; |
| 2086 } |
| 2087 if( (c=(*++fmt))==0 ){ |
| 2088 sqlite3StrAccumAppend(pAccum, "%", 1); |
| 2089 break; |
| 2090 } |
| 2091 /* Find out what flags are present */ |
| 2092 flag_leftjustify = flag_plussign = flag_blanksign = |
| 2093 flag_alternateform = flag_altform2 = flag_zeropad = 0; |
| 2094 done = 0; |
| 2095 do{ |
| 2096 switch( c ){ |
| 2097 case '-': flag_leftjustify = 1; break; |
| 2098 case '+': flag_plussign = 1; break; |
| 2099 case ' ': flag_blanksign = 1; break; |
| 2100 case '#': flag_alternateform = 1; break; |
| 2101 case '!': flag_altform2 = 1; break; |
| 2102 case '0': flag_zeropad = 1; break; |
| 2103 default: done = 1; break; |
| 2104 } |
| 2105 }while( !done && (c=(*++fmt))!=0 ); |
| 2106 /* Get the field width */ |
| 2107 if( c=='*' ){ |
| 2108 if( bArgList ){ |
| 2109 width = (int)getIntArg(pArgList); |
| 2110 }else{ |
| 2111 width = va_arg(ap,int); |
| 2112 } |
| 2113 if( width<0 ){ |
| 2114 flag_leftjustify = 1; |
| 2115 width = width >= -2147483647 ? -width : 0; |
| 2116 } |
| 2117 c = *++fmt; |
| 2118 }else{ |
| 2119 unsigned wx = 0; |
| 2120 while( c>='0' && c<='9' ){ |
| 2121 wx = wx*10 + c - '0'; |
| 2122 c = *++fmt; |
| 2123 } |
| 2124 testcase( wx>0x7fffffff ); |
| 2125 width = wx & 0x7fffffff; |
| 2126 } |
| 2127 assert( width>=0 ); |
| 2128 #ifdef SQLITE_PRINTF_PRECISION_LIMIT |
| 2129 if( width>SQLITE_PRINTF_PRECISION_LIMIT ){ |
| 2130 width = SQLITE_PRINTF_PRECISION_LIMIT; |
| 2131 } |
| 2132 #endif |
| 2133 |
| 2134 /* Get the precision */ |
| 2135 if( c=='.' ){ |
| 2136 c = *++fmt; |
| 2137 if( c=='*' ){ |
| 2138 if( bArgList ){ |
| 2139 precision = (int)getIntArg(pArgList); |
| 2140 }else{ |
| 2141 precision = va_arg(ap,int); |
| 2142 } |
| 2143 c = *++fmt; |
| 2144 if( precision<0 ){ |
| 2145 precision = precision >= -2147483647 ? -precision : -1; |
| 2146 } |
| 2147 }else{ |
| 2148 unsigned px = 0; |
| 2149 while( c>='0' && c<='9' ){ |
| 2150 px = px*10 + c - '0'; |
| 2151 c = *++fmt; |
| 2152 } |
| 2153 testcase( px>0x7fffffff ); |
| 2154 precision = px & 0x7fffffff; |
| 2155 } |
| 2156 }else{ |
| 2157 precision = -1; |
| 2158 } |
| 2159 assert( precision>=(-1) ); |
| 2160 #ifdef SQLITE_PRINTF_PRECISION_LIMIT |
| 2161 if( precision>SQLITE_PRINTF_PRECISION_LIMIT ){ |
| 2162 precision = SQLITE_PRINTF_PRECISION_LIMIT; |
| 2163 } |
| 2164 #endif |
| 2165 |
| 2166 |
| 2167 /* Get the conversion type modifier */ |
| 2168 if( c=='l' ){ |
| 2169 flag_long = 1; |
| 2170 c = *++fmt; |
| 2171 if( c=='l' ){ |
| 2172 flag_longlong = 1; |
| 2173 c = *++fmt; |
| 2174 }else{ |
| 2175 flag_longlong = 0; |
| 2176 } |
| 2177 }else{ |
| 2178 flag_long = flag_longlong = 0; |
| 2179 } |
| 2180 /* Fetch the info entry for the field */ |
| 2181 infop = &fmtinfo[0]; |
| 2182 xtype = etINVALID; |
| 2183 for(idx=0; idx<ArraySize(fmtinfo); idx++){ |
| 2184 if( c==fmtinfo[idx].fmttype ){ |
| 2185 infop = &fmtinfo[idx]; |
| 2186 if( useIntern || (infop->flags & FLAG_INTERN)==0 ){ |
| 2187 xtype = infop->type; |
| 2188 }else{ |
| 2189 return; |
| 2190 } |
| 2191 break; |
| 2192 } |
| 2193 } |
| 2194 |
| 2195 /* |
| 2196 ** At this point, variables are initialized as follows: |
| 2197 ** |
| 2198 ** flag_alternateform TRUE if a '#' is present. |
| 2199 ** flag_altform2 TRUE if a '!' is present. |
| 2200 ** flag_plussign TRUE if a '+' is present. |
| 2201 ** flag_leftjustify TRUE if a '-' is present or if the |
| 2202 ** field width was negative. |
| 2203 ** flag_zeropad TRUE if the width began with 0. |
| 2204 ** flag_long TRUE if the letter 'l' (ell) prefixed |
| 2205 ** the conversion character. |
| 2206 ** flag_longlong TRUE if the letter 'll' (ell ell) prefixed |
| 2207 ** the conversion character. |
| 2208 ** flag_blanksign TRUE if a ' ' is present. |
| 2209 ** width The specified field width. This is |
| 2210 ** always non-negative. Zero is the default. |
| 2211 ** precision The specified precision. The default |
| 2212 ** is -1. |
| 2213 ** xtype The class of the conversion. |
| 2214 ** infop Pointer to the appropriate info struct. |
| 2215 */ |
| 2216 switch( xtype ){ |
| 2217 case etPOINTER: |
| 2218 flag_longlong = sizeof(char*)==sizeof(i64); |
| 2219 flag_long = sizeof(char*)==sizeof(long int); |
| 2220 /* Fall through into the next case */ |
| 2221 case etORDINAL: |
| 2222 case etRADIX: |
| 2223 if( infop->flags & FLAG_SIGNED ){ |
| 2224 i64 v; |
| 2225 if( bArgList ){ |
| 2226 v = getIntArg(pArgList); |
| 2227 }else if( flag_longlong ){ |
| 2228 v = va_arg(ap,i64); |
| 2229 }else if( flag_long ){ |
| 2230 v = va_arg(ap,long int); |
| 2231 }else{ |
| 2232 v = va_arg(ap,int); |
| 2233 } |
| 2234 if( v<0 ){ |
| 2235 if( v==SMALLEST_INT64 ){ |
| 2236 longvalue = ((u64)1)<<63; |
| 2237 }else{ |
| 2238 longvalue = -v; |
| 2239 } |
| 2240 prefix = '-'; |
| 2241 }else{ |
| 2242 longvalue = v; |
| 2243 if( flag_plussign ) prefix = '+'; |
| 2244 else if( flag_blanksign ) prefix = ' '; |
| 2245 else prefix = 0; |
| 2246 } |
| 2247 }else{ |
| 2248 if( bArgList ){ |
| 2249 longvalue = (u64)getIntArg(pArgList); |
| 2250 }else if( flag_longlong ){ |
| 2251 longvalue = va_arg(ap,u64); |
| 2252 }else if( flag_long ){ |
| 2253 longvalue = va_arg(ap,unsigned long int); |
| 2254 }else{ |
| 2255 longvalue = va_arg(ap,unsigned int); |
| 2256 } |
| 2257 prefix = 0; |
| 2258 } |
| 2259 if( longvalue==0 ) flag_alternateform = 0; |
| 2260 if( flag_zeropad && precision<width-(prefix!=0) ){ |
| 2261 precision = width-(prefix!=0); |
| 2262 } |
| 2263 if( precision<etBUFSIZE-10 ){ |
| 2264 nOut = etBUFSIZE; |
| 2265 zOut = buf; |
| 2266 }else{ |
| 2267 nOut = precision + 10; |
| 2268 zOut = zExtra = sqlite3Malloc( nOut ); |
| 2269 if( zOut==0 ){ |
| 2270 setStrAccumError(pAccum, STRACCUM_NOMEM); |
| 2271 return; |
| 2272 } |
| 2273 } |
| 2274 bufpt = &zOut[nOut-1]; |
| 2275 if( xtype==etORDINAL ){ |
| 2276 static const char zOrd[] = "thstndrd"; |
| 2277 int x = (int)(longvalue % 10); |
| 2278 if( x>=4 || (longvalue/10)%10==1 ){ |
| 2279 x = 0; |
| 2280 } |
| 2281 *(--bufpt) = zOrd[x*2+1]; |
| 2282 *(--bufpt) = zOrd[x*2]; |
| 2283 } |
| 2284 { |
| 2285 const char *cset = &aDigits[infop->charset]; |
| 2286 u8 base = infop->base; |
| 2287 do{ /* Convert to ascii */ |
| 2288 *(--bufpt) = cset[longvalue%base]; |
| 2289 longvalue = longvalue/base; |
| 2290 }while( longvalue>0 ); |
| 2291 } |
| 2292 length = (int)(&zOut[nOut-1]-bufpt); |
| 2293 for(idx=precision-length; idx>0; idx--){ |
| 2294 *(--bufpt) = '0'; /* Zero pad */ |
| 2295 } |
| 2296 if( prefix ) *(--bufpt) = prefix; /* Add sign */ |
| 2297 if( flag_alternateform && infop->prefix ){ /* Add "0" or "0x" */ |
| 2298 const char *pre; |
| 2299 char x; |
| 2300 pre = &aPrefix[infop->prefix]; |
| 2301 for(; (x=(*pre))!=0; pre++) *(--bufpt) = x; |
| 2302 } |
| 2303 length = (int)(&zOut[nOut-1]-bufpt); |
| 2304 break; |
| 2305 case etFLOAT: |
| 2306 case etEXP: |
| 2307 case etGENERIC: |
| 2308 if( bArgList ){ |
| 2309 realvalue = getDoubleArg(pArgList); |
| 2310 }else{ |
| 2311 realvalue = va_arg(ap,double); |
| 2312 } |
| 2313 #ifdef SQLITE_OMIT_FLOATING_POINT |
| 2314 length = 0; |
| 2315 #else |
| 2316 if( precision<0 ) precision = 6; /* Set default precision */ |
| 2317 if( realvalue<0.0 ){ |
| 2318 realvalue = -realvalue; |
| 2319 prefix = '-'; |
| 2320 }else{ |
| 2321 if( flag_plussign ) prefix = '+'; |
| 2322 else if( flag_blanksign ) prefix = ' '; |
| 2323 else prefix = 0; |
| 2324 } |
| 2325 if( xtype==etGENERIC && precision>0 ) precision--; |
| 2326 testcase( precision>0xfff ); |
| 2327 for(idx=precision&0xfff, rounder=0.5; idx>0; idx--, rounder*=0.1){} |
| 2328 if( xtype==etFLOAT ) realvalue += rounder; |
| 2329 /* Normalize realvalue to within 10.0 > realvalue >= 1.0 */ |
| 2330 exp = 0; |
| 2331 if( sqlite3IsNaN((double)realvalue) ){ |
| 2332 bufpt = "NaN"; |
| 2333 length = 3; |
| 2334 break; |
| 2335 } |
| 2336 if( realvalue>0.0 ){ |
| 2337 LONGDOUBLE_TYPE scale = 1.0; |
| 2338 while( realvalue>=1e100*scale && exp<=350 ){ scale *= 1e100;exp+=100;} |
| 2339 while( realvalue>=1e10*scale && exp<=350 ){ scale *= 1e10; exp+=10; } |
| 2340 while( realvalue>=10.0*scale && exp<=350 ){ scale *= 10.0; exp++; } |
| 2341 realvalue /= scale; |
| 2342 while( realvalue<1e-8 ){ realvalue *= 1e8; exp-=8; } |
| 2343 while( realvalue<1.0 ){ realvalue *= 10.0; exp--; } |
| 2344 if( exp>350 ){ |
| 2345 bufpt = buf; |
| 2346 buf[0] = prefix; |
| 2347 memcpy(buf+(prefix!=0),"Inf",4); |
| 2348 length = 3+(prefix!=0); |
| 2349 break; |
| 2350 } |
| 2351 } |
| 2352 bufpt = buf; |
| 2353 /* |
| 2354 ** If the field type is etGENERIC, then convert to either etEXP |
| 2355 ** or etFLOAT, as appropriate. |
| 2356 */ |
| 2357 if( xtype!=etFLOAT ){ |
| 2358 realvalue += rounder; |
| 2359 if( realvalue>=10.0 ){ realvalue *= 0.1; exp++; } |
| 2360 } |
| 2361 if( xtype==etGENERIC ){ |
| 2362 flag_rtz = !flag_alternateform; |
| 2363 if( exp<-4 || exp>precision ){ |
| 2364 xtype = etEXP; |
| 2365 }else{ |
| 2366 precision = precision - exp; |
| 2367 xtype = etFLOAT; |
| 2368 } |
| 2369 }else{ |
| 2370 flag_rtz = flag_altform2; |
| 2371 } |
| 2372 if( xtype==etEXP ){ |
| 2373 e2 = 0; |
| 2374 }else{ |
| 2375 e2 = exp; |
| 2376 } |
| 2377 if( MAX(e2,0)+(i64)precision+(i64)width > etBUFSIZE - 15 ){ |
| 2378 bufpt = zExtra |
| 2379 = sqlite3Malloc( MAX(e2,0)+(i64)precision+(i64)width+15 ); |
| 2380 if( bufpt==0 ){ |
| 2381 setStrAccumError(pAccum, STRACCUM_NOMEM); |
| 2382 return; |
| 2383 } |
| 2384 } |
| 2385 zOut = bufpt; |
| 2386 nsd = 16 + flag_altform2*10; |
| 2387 flag_dp = (precision>0 ?1:0) | flag_alternateform | flag_altform2; |
| 2388 /* The sign in front of the number */ |
| 2389 if( prefix ){ |
| 2390 *(bufpt++) = prefix; |
| 2391 } |
| 2392 /* Digits prior to the decimal point */ |
| 2393 if( e2<0 ){ |
| 2394 *(bufpt++) = '0'; |
| 2395 }else{ |
| 2396 for(; e2>=0; e2--){ |
| 2397 *(bufpt++) = et_getdigit(&realvalue,&nsd); |
| 2398 } |
| 2399 } |
| 2400 /* The decimal point */ |
| 2401 if( flag_dp ){ |
| 2402 *(bufpt++) = '.'; |
| 2403 } |
| 2404 /* "0" digits after the decimal point but before the first |
| 2405 ** significant digit of the number */ |
| 2406 for(e2++; e2<0; precision--, e2++){ |
| 2407 assert( precision>0 ); |
| 2408 *(bufpt++) = '0'; |
| 2409 } |
| 2410 /* Significant digits after the decimal point */ |
| 2411 while( (precision--)>0 ){ |
| 2412 *(bufpt++) = et_getdigit(&realvalue,&nsd); |
| 2413 } |
| 2414 /* Remove trailing zeros and the "." if no digits follow the "." */ |
| 2415 if( flag_rtz && flag_dp ){ |
| 2416 while( bufpt[-1]=='0' ) *(--bufpt) = 0; |
| 2417 assert( bufpt>zOut ); |
| 2418 if( bufpt[-1]=='.' ){ |
| 2419 if( flag_altform2 ){ |
| 2420 *(bufpt++) = '0'; |
| 2421 }else{ |
| 2422 *(--bufpt) = 0; |
| 2423 } |
| 2424 } |
| 2425 } |
| 2426 /* Add the "eNNN" suffix */ |
| 2427 if( xtype==etEXP ){ |
| 2428 *(bufpt++) = aDigits[infop->charset]; |
| 2429 if( exp<0 ){ |
| 2430 *(bufpt++) = '-'; exp = -exp; |
| 2431 }else{ |
| 2432 *(bufpt++) = '+'; |
| 2433 } |
| 2434 if( exp>=100 ){ |
| 2435 *(bufpt++) = (char)((exp/100)+'0'); /* 100's digit */ |
| 2436 exp %= 100; |
| 2437 } |
| 2438 *(bufpt++) = (char)(exp/10+'0'); /* 10's digit */ |
| 2439 *(bufpt++) = (char)(exp%10+'0'); /* 1's digit */ |
| 2440 } |
| 2441 *bufpt = 0; |
| 2442 |
| 2443 /* The converted number is in buf[] and zero terminated. Output it. |
| 2444 ** Note that the number is in the usual order, not reversed as with |
| 2445 ** integer conversions. */ |
| 2446 length = (int)(bufpt-zOut); |
| 2447 bufpt = zOut; |
| 2448 |
| 2449 /* Special case: Add leading zeros if the flag_zeropad flag is |
| 2450 ** set and we are not left justified */ |
| 2451 if( flag_zeropad && !flag_leftjustify && length < width){ |
| 2452 int i; |
| 2453 int nPad = width - length; |
| 2454 for(i=width; i>=nPad; i--){ |
| 2455 bufpt[i] = bufpt[i-nPad]; |
| 2456 } |
| 2457 i = prefix!=0; |
| 2458 while( nPad-- ) bufpt[i++] = '0'; |
| 2459 length = width; |
| 2460 } |
| 2461 #endif /* !defined(SQLITE_OMIT_FLOATING_POINT) */ |
| 2462 break; |
| 2463 case etSIZE: |
| 2464 if( !bArgList ){ |
| 2465 *(va_arg(ap,int*)) = pAccum->nChar; |
| 2466 } |
| 2467 length = width = 0; |
| 2468 break; |
| 2469 case etPERCENT: |
| 2470 buf[0] = '%'; |
| 2471 bufpt = buf; |
| 2472 length = 1; |
| 2473 break; |
| 2474 case etCHARX: |
| 2475 if( bArgList ){ |
| 2476 bufpt = getTextArg(pArgList); |
| 2477 c = bufpt ? bufpt[0] : 0; |
| 2478 }else{ |
| 2479 c = va_arg(ap,int); |
| 2480 } |
| 2481 if( precision>1 ){ |
| 2482 width -= precision-1; |
| 2483 if( width>1 && !flag_leftjustify ){ |
| 2484 sqlite3AppendChar(pAccum, width-1, ' '); |
| 2485 width = 0; |
| 2486 } |
| 2487 sqlite3AppendChar(pAccum, precision-1, c); |
| 2488 } |
| 2489 length = 1; |
| 2490 buf[0] = c; |
| 2491 bufpt = buf; |
| 2492 break; |
| 2493 case etSTRING: |
| 2494 case etDYNSTRING: |
| 2495 if( bArgList ){ |
| 2496 bufpt = getTextArg(pArgList); |
| 2497 xtype = etSTRING; |
| 2498 }else{ |
| 2499 bufpt = va_arg(ap,char*); |
| 2500 } |
| 2501 if( bufpt==0 ){ |
| 2502 bufpt = ""; |
| 2503 }else if( xtype==etDYNSTRING ){ |
| 2504 zExtra = bufpt; |
| 2505 } |
| 2506 if( precision>=0 ){ |
| 2507 for(length=0; length<precision && bufpt[length]; length++){} |
| 2508 }else{ |
| 2509 length = sqlite3Strlen30(bufpt); |
| 2510 } |
| 2511 break; |
| 2512 case etSQLESCAPE: /* Escape ' characters */ |
| 2513 case etSQLESCAPE2: /* Escape ' and enclose in '...' */ |
| 2514 case etSQLESCAPE3: { /* Escape " characters */ |
| 2515 int i, j, k, n, isnull; |
| 2516 int needQuote; |
| 2517 char ch; |
| 2518 char q = ((xtype==etSQLESCAPE3)?'"':'\''); /* Quote character */ |
| 2519 char *escarg; |
| 2520 |
| 2521 if( bArgList ){ |
| 2522 escarg = getTextArg(pArgList); |
| 2523 }else{ |
| 2524 escarg = va_arg(ap,char*); |
| 2525 } |
| 2526 isnull = escarg==0; |
| 2527 if( isnull ) escarg = (xtype==etSQLESCAPE2 ? "NULL" : "(NULL)"); |
| 2528 k = precision; |
| 2529 for(i=n=0; k!=0 && (ch=escarg[i])!=0; i++, k--){ |
| 2530 if( ch==q ) n++; |
| 2531 } |
| 2532 needQuote = !isnull && xtype==etSQLESCAPE2; |
| 2533 n += i + 3; |
| 2534 if( n>etBUFSIZE ){ |
| 2535 bufpt = zExtra = sqlite3Malloc( n ); |
| 2536 if( bufpt==0 ){ |
| 2537 setStrAccumError(pAccum, STRACCUM_NOMEM); |
| 2538 return; |
| 2539 } |
| 2540 }else{ |
| 2541 bufpt = buf; |
| 2542 } |
| 2543 j = 0; |
| 2544 if( needQuote ) bufpt[j++] = q; |
| 2545 k = i; |
| 2546 for(i=0; i<k; i++){ |
| 2547 bufpt[j++] = ch = escarg[i]; |
| 2548 if( ch==q ) bufpt[j++] = ch; |
| 2549 } |
| 2550 if( needQuote ) bufpt[j++] = q; |
| 2551 bufpt[j] = 0; |
| 2552 length = j; |
| 2553 /* The precision in %q and %Q means how many input characters to |
| 2554 ** consume, not the length of the output... |
| 2555 ** if( precision>=0 && precision<length ) length = precision; */ |
| 2556 break; |
| 2557 } |
| 2558 case etTOKEN: { |
| 2559 Token *pToken = va_arg(ap, Token*); |
| 2560 assert( bArgList==0 ); |
| 2561 if( pToken && pToken->n ){ |
| 2562 sqlite3StrAccumAppend(pAccum, (const char*)pToken->z, pToken->n); |
| 2563 } |
| 2564 length = width = 0; |
| 2565 break; |
| 2566 } |
| 2567 case etSRCLIST: { |
| 2568 SrcList *pSrc = va_arg(ap, SrcList*); |
| 2569 int k = va_arg(ap, int); |
| 2570 struct SrcList_item *pItem = &pSrc->a[k]; |
| 2571 assert( bArgList==0 ); |
| 2572 assert( k>=0 && k<pSrc->nSrc ); |
| 2573 if( pItem->zDatabase ){ |
| 2574 sqlite3StrAccumAppendAll(pAccum, pItem->zDatabase); |
| 2575 sqlite3StrAccumAppend(pAccum, ".", 1); |
| 2576 } |
| 2577 sqlite3StrAccumAppendAll(pAccum, pItem->zName); |
| 2578 length = width = 0; |
| 2579 break; |
| 2580 } |
| 2581 default: { |
| 2582 assert( xtype==etINVALID ); |
| 2583 return; |
| 2584 } |
| 2585 }/* End switch over the format type */ |
| 2586 /* |
| 2587 ** The text of the conversion is pointed to by "bufpt" and is |
| 2588 ** "length" characters long. The field width is "width". Do |
| 2589 ** the output. |
| 2590 */ |
| 2591 width -= length; |
| 2592 if( width>0 && !flag_leftjustify ) sqlite3AppendChar(pAccum, width, ' '); |
| 2593 sqlite3StrAccumAppend(pAccum, bufpt, length); |
| 2594 if( width>0 && flag_leftjustify ) sqlite3AppendChar(pAccum, width, ' '); |
| 2595 |
| 2596 if( zExtra ){ |
| 2597 sqlite3DbFree(pAccum->db, zExtra); |
| 2598 zExtra = 0; |
| 2599 } |
| 2600 }/* End for loop over the format string */ |
| 2601 } /* End of function */ |
| 2602 |
| 2603 /* |
| 2604 ** Enlarge the memory allocation on a StrAccum object so that it is |
| 2605 ** able to accept at least N more bytes of text. |
| 2606 ** |
| 2607 ** Return the number of bytes of text that StrAccum is able to accept |
| 2608 ** after the attempted enlargement. The value returned might be zero. |
| 2609 */ |
| 2610 static int sqlite3StrAccumEnlarge(StrAccum *p, int N){ |
| 2611 char *zNew; |
| 2612 assert( p->nChar+(i64)N >= p->nAlloc ); /* Only called if really needed */ |
| 2613 if( p->accError ){ |
| 2614 testcase(p->accError==STRACCUM_TOOBIG); |
| 2615 testcase(p->accError==STRACCUM_NOMEM); |
| 2616 return 0; |
| 2617 } |
| 2618 if( p->mxAlloc==0 ){ |
| 2619 N = p->nAlloc - p->nChar - 1; |
| 2620 setStrAccumError(p, STRACCUM_TOOBIG); |
| 2621 return N; |
| 2622 }else{ |
| 2623 char *zOld = p->bMalloced ? p->zText : 0; |
| 2624 i64 szNew = p->nChar; |
| 2625 assert( (p->zText==0 || p->zText==p->zBase)==(p->bMalloced==0) ); |
| 2626 szNew += N + 1; |
| 2627 if( szNew+p->nChar<=p->mxAlloc ){ |
| 2628 /* Force exponential buffer size growth as long as it does not overflow, |
| 2629 ** to avoid having to call this routine too often */ |
| 2630 szNew += p->nChar; |
| 2631 } |
| 2632 if( szNew > p->mxAlloc ){ |
| 2633 sqlite3StrAccumReset(p); |
| 2634 setStrAccumError(p, STRACCUM_TOOBIG); |
| 2635 return 0; |
| 2636 }else{ |
| 2637 p->nAlloc = (int)szNew; |
| 2638 } |
| 2639 if( p->db ){ |
| 2640 zNew = sqlite3DbRealloc(p->db, zOld, p->nAlloc); |
| 2641 }else{ |
| 2642 zNew = sqlite3_realloc64(zOld, p->nAlloc); |
| 2643 } |
| 2644 if( zNew ){ |
| 2645 assert( p->zText!=0 || p->nChar==0 ); |
| 2646 if( !p->bMalloced && p->nChar>0 ) memcpy(zNew, p->zText, p->nChar); |
| 2647 p->zText = zNew; |
| 2648 p->nAlloc = sqlite3DbMallocSize(p->db, zNew); |
| 2649 p->bMalloced = 1; |
| 2650 }else{ |
| 2651 sqlite3StrAccumReset(p); |
| 2652 setStrAccumError(p, STRACCUM_NOMEM); |
| 2653 return 0; |
| 2654 } |
| 2655 } |
| 2656 return N; |
| 2657 } |
| 2658 |
| 2659 /* |
| 2660 ** Append N copies of character c to the given string buffer. |
| 2661 */ |
| 2662 SQLITE_PRIVATE void sqlite3AppendChar(StrAccum *p, int N, char c){ |
| 2663 testcase( p->nChar + (i64)N > 0x7fffffff ); |
| 2664 if( p->nChar+(i64)N >= p->nAlloc && (N = sqlite3StrAccumEnlarge(p, N))<=0 ){ |
| 2665 return; |
| 2666 } |
| 2667 assert( (p->zText==p->zBase)==(p->bMalloced==0) ); |
| 2668 while( (N--)>0 ) p->zText[p->nChar++] = c; |
| 2669 } |
| 2670 |
| 2671 /* |
| 2672 ** The StrAccum "p" is not large enough to accept N new bytes of z[]. |
| 2673 ** So enlarge if first, then do the append. |
| 2674 ** |
| 2675 ** This is a helper routine to sqlite3StrAccumAppend() that does special-case |
| 2676 ** work (enlarging the buffer) using tail recursion, so that the |
| 2677 ** sqlite3StrAccumAppend() routine can use fast calling semantics. |
| 2678 */ |
| 2679 static void SQLITE_NOINLINE enlargeAndAppend(StrAccum *p, const char *z, int N){ |
| 2680 N = sqlite3StrAccumEnlarge(p, N); |
| 2681 if( N>0 ){ |
| 2682 memcpy(&p->zText[p->nChar], z, N); |
| 2683 p->nChar += N; |
| 2684 } |
| 2685 assert( (p->zText==0 || p->zText==p->zBase)==(p->bMalloced==0) ); |
| 2686 } |
| 2687 |
| 2688 /* |
| 2689 ** Append N bytes of text from z to the StrAccum object. Increase the |
| 2690 ** size of the memory allocation for StrAccum if necessary. |
| 2691 */ |
| 2692 SQLITE_PRIVATE void sqlite3StrAccumAppend(StrAccum *p, const char *z, int N){ |
| 2693 assert( z!=0 || N==0 ); |
| 2694 assert( p->zText!=0 || p->nChar==0 || p->accError ); |
| 2695 assert( N>=0 ); |
| 2696 assert( p->accError==0 || p->nAlloc==0 ); |
| 2697 if( p->nChar+N >= p->nAlloc ){ |
| 2698 enlargeAndAppend(p,z,N); |
| 2699 }else{ |
| 2700 assert( p->zText ); |
| 2701 p->nChar += N; |
| 2702 memcpy(&p->zText[p->nChar-N], z, N); |
| 2703 } |
| 2704 } |
| 2705 |
| 2706 /* |
| 2707 ** Append the complete text of zero-terminated string z[] to the p string. |
| 2708 */ |
| 2709 SQLITE_PRIVATE void sqlite3StrAccumAppendAll(StrAccum *p, const char *z){ |
| 2710 sqlite3StrAccumAppend(p, z, sqlite3Strlen30(z)); |
| 2711 } |
| 2712 |
| 2713 |
| 2714 /* |
| 2715 ** Finish off a string by making sure it is zero-terminated. |
| 2716 ** Return a pointer to the resulting string. Return a NULL |
| 2717 ** pointer if any kind of error was encountered. |
| 2718 */ |
| 2719 SQLITE_PRIVATE char *sqlite3StrAccumFinish(StrAccum *p){ |
| 2720 if( p->zText ){ |
| 2721 assert( (p->zText==p->zBase)==(p->bMalloced==0) ); |
| 2722 p->zText[p->nChar] = 0; |
| 2723 if( p->mxAlloc>0 && p->bMalloced==0 ){ |
| 2724 p->zText = sqlite3DbMallocRaw(p->db, p->nChar+1 ); |
| 2725 if( p->zText ){ |
| 2726 memcpy(p->zText, p->zBase, p->nChar+1); |
| 2727 p->bMalloced = 1; |
| 2728 }else{ |
| 2729 setStrAccumError(p, STRACCUM_NOMEM); |
| 2730 } |
| 2731 } |
| 2732 } |
| 2733 return p->zText; |
| 2734 } |
| 2735 |
| 2736 /* |
| 2737 ** Reset an StrAccum string. Reclaim all malloced memory. |
| 2738 */ |
| 2739 SQLITE_PRIVATE void sqlite3StrAccumReset(StrAccum *p){ |
| 2740 assert( (p->zText==0 || p->zText==p->zBase)==(p->bMalloced==0) ); |
| 2741 if( p->bMalloced ){ |
| 2742 sqlite3DbFree(p->db, p->zText); |
| 2743 p->bMalloced = 0; |
| 2744 } |
| 2745 p->zText = 0; |
| 2746 } |
| 2747 |
| 2748 /* |
| 2749 ** Initialize a string accumulator. |
| 2750 ** |
| 2751 ** p: The accumulator to be initialized. |
| 2752 ** db: Pointer to a database connection. May be NULL. Lookaside |
| 2753 ** memory is used if not NULL. db->mallocFailed is set appropriately |
| 2754 ** when not NULL. |
| 2755 ** zBase: An initial buffer. May be NULL in which case the initial buffer |
| 2756 ** is malloced. |
| 2757 ** n: Size of zBase in bytes. If total space requirements never exceed |
| 2758 ** n then no memory allocations ever occur. |
| 2759 ** mx: Maximum number of bytes to accumulate. If mx==0 then no memory |
| 2760 ** allocations will ever occur. |
| 2761 */ |
| 2762 SQLITE_PRIVATE void sqlite3StrAccumInit(StrAccum *p, sqlite3 *db, char *zBase, i
nt n, int mx){ |
| 2763 p->zText = p->zBase = zBase; |
| 2764 p->db = db; |
| 2765 p->nChar = 0; |
| 2766 p->nAlloc = n; |
| 2767 p->mxAlloc = mx; |
| 2768 p->accError = 0; |
| 2769 p->bMalloced = 0; |
| 2770 } |
| 2771 |
| 2772 /* |
| 2773 ** Print into memory obtained from sqliteMalloc(). Use the internal |
| 2774 ** %-conversion extensions. |
| 2775 */ |
| 2776 SQLITE_PRIVATE char *sqlite3VMPrintf(sqlite3 *db, const char *zFormat, va_list a
p){ |
| 2777 char *z; |
| 2778 char zBase[SQLITE_PRINT_BUF_SIZE]; |
| 2779 StrAccum acc; |
| 2780 assert( db!=0 ); |
| 2781 sqlite3StrAccumInit(&acc, db, zBase, sizeof(zBase), |
| 2782 db->aLimit[SQLITE_LIMIT_LENGTH]); |
| 2783 sqlite3VXPrintf(&acc, SQLITE_PRINTF_INTERNAL, zFormat, ap); |
| 2784 z = sqlite3StrAccumFinish(&acc); |
| 2785 if( acc.accError==STRACCUM_NOMEM ){ |
| 2786 db->mallocFailed = 1; |
| 2787 } |
| 2788 return z; |
| 2789 } |
| 2790 |
| 2791 /* |
| 2792 ** Print into memory obtained from sqliteMalloc(). Use the internal |
| 2793 ** %-conversion extensions. |
| 2794 */ |
| 2795 SQLITE_PRIVATE char *sqlite3MPrintf(sqlite3 *db, const char *zFormat, ...){ |
| 2796 va_list ap; |
| 2797 char *z; |
| 2798 va_start(ap, zFormat); |
| 2799 z = sqlite3VMPrintf(db, zFormat, ap); |
| 2800 va_end(ap); |
| 2801 return z; |
| 2802 } |
| 2803 |
| 2804 /* |
| 2805 ** Print into memory obtained from sqlite3_malloc(). Omit the internal |
| 2806 ** %-conversion extensions. |
| 2807 */ |
| 2808 SQLITE_API char *SQLITE_STDCALL sqlite3_vmprintf(const char *zFormat, va_list ap
){ |
| 2809 char *z; |
| 2810 char zBase[SQLITE_PRINT_BUF_SIZE]; |
| 2811 StrAccum acc; |
| 2812 |
| 2813 #ifdef SQLITE_ENABLE_API_ARMOR |
| 2814 if( zFormat==0 ){ |
| 2815 (void)SQLITE_MISUSE_BKPT; |
| 2816 return 0; |
| 2817 } |
| 2818 #endif |
| 2819 #ifndef SQLITE_OMIT_AUTOINIT |
| 2820 if( sqlite3_initialize() ) return 0; |
| 2821 #endif |
| 2822 sqlite3StrAccumInit(&acc, 0, zBase, sizeof(zBase), SQLITE_MAX_LENGTH); |
| 2823 sqlite3VXPrintf(&acc, 0, zFormat, ap); |
| 2824 z = sqlite3StrAccumFinish(&acc); |
| 2825 return z; |
| 2826 } |
| 2827 |
| 2828 /* |
| 2829 ** Print into memory obtained from sqlite3_malloc()(). Omit the internal |
| 2830 ** %-conversion extensions. |
| 2831 */ |
| 2832 SQLITE_API char *SQLITE_CDECL sqlite3_mprintf(const char *zFormat, ...){ |
| 2833 va_list ap; |
| 2834 char *z; |
| 2835 #ifndef SQLITE_OMIT_AUTOINIT |
| 2836 if( sqlite3_initialize() ) return 0; |
| 2837 #endif |
| 2838 va_start(ap, zFormat); |
| 2839 z = sqlite3_vmprintf(zFormat, ap); |
| 2840 va_end(ap); |
| 2841 return z; |
| 2842 } |
| 2843 |
| 2844 /* |
| 2845 ** sqlite3_snprintf() works like snprintf() except that it ignores the |
| 2846 ** current locale settings. This is important for SQLite because we |
| 2847 ** are not able to use a "," as the decimal point in place of "." as |
| 2848 ** specified by some locales. |
| 2849 ** |
| 2850 ** Oops: The first two arguments of sqlite3_snprintf() are backwards |
| 2851 ** from the snprintf() standard. Unfortunately, it is too late to change |
| 2852 ** this without breaking compatibility, so we just have to live with the |
| 2853 ** mistake. |
| 2854 ** |
| 2855 ** sqlite3_vsnprintf() is the varargs version. |
| 2856 */ |
| 2857 SQLITE_API char *SQLITE_STDCALL sqlite3_vsnprintf(int n, char *zBuf, const char
*zFormat, va_list ap){ |
| 2858 StrAccum acc; |
| 2859 if( n<=0 ) return zBuf; |
| 2860 #ifdef SQLITE_ENABLE_API_ARMOR |
| 2861 if( zBuf==0 || zFormat==0 ) { |
| 2862 (void)SQLITE_MISUSE_BKPT; |
| 2863 if( zBuf ) zBuf[0] = 0; |
| 2864 return zBuf; |
| 2865 } |
| 2866 #endif |
| 2867 sqlite3StrAccumInit(&acc, 0, zBuf, n, 0); |
| 2868 sqlite3VXPrintf(&acc, 0, zFormat, ap); |
| 2869 return sqlite3StrAccumFinish(&acc); |
| 2870 } |
| 2871 SQLITE_API char *SQLITE_CDECL sqlite3_snprintf(int n, char *zBuf, const char *zF
ormat, ...){ |
| 2872 char *z; |
| 2873 va_list ap; |
| 2874 va_start(ap,zFormat); |
| 2875 z = sqlite3_vsnprintf(n, zBuf, zFormat, ap); |
| 2876 va_end(ap); |
| 2877 return z; |
| 2878 } |
| 2879 |
| 2880 /* |
| 2881 ** This is the routine that actually formats the sqlite3_log() message. |
| 2882 ** We house it in a separate routine from sqlite3_log() to avoid using |
| 2883 ** stack space on small-stack systems when logging is disabled. |
| 2884 ** |
| 2885 ** sqlite3_log() must render into a static buffer. It cannot dynamically |
| 2886 ** allocate memory because it might be called while the memory allocator |
| 2887 ** mutex is held. |
| 2888 ** |
| 2889 ** sqlite3VXPrintf() might ask for *temporary* memory allocations for |
| 2890 ** certain format characters (%q) or for very large precisions or widths. |
| 2891 ** Care must be taken that any sqlite3_log() calls that occur while the |
| 2892 ** memory mutex is held do not use these mechanisms. |
| 2893 */ |
| 2894 static void renderLogMsg(int iErrCode, const char *zFormat, va_list ap){ |
| 2895 StrAccum acc; /* String accumulator */ |
| 2896 char zMsg[SQLITE_PRINT_BUF_SIZE*3]; /* Complete log message */ |
| 2897 |
| 2898 sqlite3StrAccumInit(&acc, 0, zMsg, sizeof(zMsg), 0); |
| 2899 sqlite3VXPrintf(&acc, 0, zFormat, ap); |
| 2900 sqlite3GlobalConfig.xLog(sqlite3GlobalConfig.pLogArg, iErrCode, |
| 2901 sqlite3StrAccumFinish(&acc)); |
| 2902 } |
| 2903 |
| 2904 /* |
| 2905 ** Format and write a message to the log if logging is enabled. |
| 2906 */ |
| 2907 SQLITE_API void SQLITE_CDECL sqlite3_log(int iErrCode, const char *zFormat, ...)
{ |
| 2908 va_list ap; /* Vararg list */ |
| 2909 if( sqlite3GlobalConfig.xLog ){ |
| 2910 va_start(ap, zFormat); |
| 2911 renderLogMsg(iErrCode, zFormat, ap); |
| 2912 va_end(ap); |
| 2913 } |
| 2914 } |
| 2915 |
| 2916 #if defined(SQLITE_DEBUG) || defined(SQLITE_HAVE_OS_TRACE) |
| 2917 /* |
| 2918 ** A version of printf() that understands %lld. Used for debugging. |
| 2919 ** The printf() built into some versions of windows does not understand %lld |
| 2920 ** and segfaults if you give it a long long int. |
| 2921 */ |
| 2922 SQLITE_PRIVATE void sqlite3DebugPrintf(const char *zFormat, ...){ |
| 2923 va_list ap; |
| 2924 StrAccum acc; |
| 2925 char zBuf[500]; |
| 2926 sqlite3StrAccumInit(&acc, 0, zBuf, sizeof(zBuf), 0); |
| 2927 va_start(ap,zFormat); |
| 2928 sqlite3VXPrintf(&acc, 0, zFormat, ap); |
| 2929 va_end(ap); |
| 2930 sqlite3StrAccumFinish(&acc); |
| 2931 fprintf(stdout,"%s", zBuf); |
| 2932 fflush(stdout); |
| 2933 } |
| 2934 #endif |
| 2935 |
| 2936 |
| 2937 /* |
| 2938 ** variable-argument wrapper around sqlite3VXPrintf(). The bFlags argument |
| 2939 ** can contain the bit SQLITE_PRINTF_INTERNAL enable internal formats. |
| 2940 */ |
| 2941 SQLITE_PRIVATE void sqlite3XPrintf(StrAccum *p, u32 bFlags, const char *zFormat,
...){ |
| 2942 va_list ap; |
| 2943 va_start(ap,zFormat); |
| 2944 sqlite3VXPrintf(p, bFlags, zFormat, ap); |
| 2945 va_end(ap); |
| 2946 } |
| 2947 |
| 2948 /************** End of printf.c **********************************************/ |
| 2949 /************** Begin file treeview.c ****************************************/ |
| 2950 /* |
| 2951 ** 2015-06-08 |
| 2952 ** |
| 2953 ** The author disclaims copyright to this source code. In place of |
| 2954 ** a legal notice, here is a blessing: |
| 2955 ** |
| 2956 ** May you do good and not evil. |
| 2957 ** May you find forgiveness for yourself and forgive others. |
| 2958 ** May you share freely, never taking more than you give. |
| 2959 ** |
| 2960 ************************************************************************* |
| 2961 ** |
| 2962 ** This file contains C code to implement the TreeView debugging routines. |
| 2963 ** These routines print a parse tree to standard output for debugging and |
| 2964 ** analysis. |
| 2965 ** |
| 2966 ** The interfaces in this file is only available when compiling |
| 2967 ** with SQLITE_DEBUG. |
| 2968 */ |
| 2969 /* #include "sqliteInt.h" */ |
| 2970 #ifdef SQLITE_DEBUG |
| 2971 |
| 2972 /* |
| 2973 ** Add a new subitem to the tree. The moreToFollow flag indicates that this |
| 2974 ** is not the last item in the tree. |
| 2975 */ |
| 2976 static TreeView *sqlite3TreeViewPush(TreeView *p, u8 moreToFollow){ |
| 2977 if( p==0 ){ |
| 2978 p = sqlite3_malloc64( sizeof(*p) ); |
| 2979 if( p==0 ) return 0; |
| 2980 memset(p, 0, sizeof(*p)); |
| 2981 }else{ |
| 2982 p->iLevel++; |
| 2983 } |
| 2984 assert( moreToFollow==0 || moreToFollow==1 ); |
| 2985 if( p->iLevel<sizeof(p->bLine) ) p->bLine[p->iLevel] = moreToFollow; |
| 2986 return p; |
| 2987 } |
| 2988 |
| 2989 /* |
| 2990 ** Finished with one layer of the tree |
| 2991 */ |
| 2992 static void sqlite3TreeViewPop(TreeView *p){ |
| 2993 if( p==0 ) return; |
| 2994 p->iLevel--; |
| 2995 if( p->iLevel<0 ) sqlite3_free(p); |
| 2996 } |
| 2997 |
| 2998 /* |
| 2999 ** Generate a single line of output for the tree, with a prefix that contains |
| 3000 ** all the appropriate tree lines |
| 3001 */ |
| 3002 static void sqlite3TreeViewLine(TreeView *p, const char *zFormat, ...){ |
| 3003 va_list ap; |
| 3004 int i; |
| 3005 StrAccum acc; |
| 3006 char zBuf[500]; |
| 3007 sqlite3StrAccumInit(&acc, 0, zBuf, sizeof(zBuf), 0); |
| 3008 if( p ){ |
| 3009 for(i=0; i<p->iLevel && i<sizeof(p->bLine)-1; i++){ |
| 3010 sqlite3StrAccumAppend(&acc, p->bLine[i] ? "| " : " ", 4); |
| 3011 } |
| 3012 sqlite3StrAccumAppend(&acc, p->bLine[i] ? "|-- " : "'-- ", 4); |
| 3013 } |
| 3014 va_start(ap, zFormat); |
| 3015 sqlite3VXPrintf(&acc, 0, zFormat, ap); |
| 3016 va_end(ap); |
| 3017 if( zBuf[acc.nChar-1]!='\n' ) sqlite3StrAccumAppend(&acc, "\n", 1); |
| 3018 sqlite3StrAccumFinish(&acc); |
| 3019 fprintf(stdout,"%s", zBuf); |
| 3020 fflush(stdout); |
| 3021 } |
| 3022 |
| 3023 /* |
| 3024 ** Shorthand for starting a new tree item that consists of a single label |
| 3025 */ |
| 3026 static void sqlite3TreeViewItem(TreeView *p, const char *zLabel,u8 moreFollows){ |
| 3027 p = sqlite3TreeViewPush(p, moreFollows); |
| 3028 sqlite3TreeViewLine(p, "%s", zLabel); |
| 3029 } |
| 3030 |
| 3031 /* |
| 3032 ** Generate a human-readable description of a WITH clause. |
| 3033 */ |
| 3034 SQLITE_PRIVATE void sqlite3TreeViewWith(TreeView *pView, const With *pWith, u8 m
oreToFollow){ |
| 3035 int i; |
| 3036 if( pWith==0 ) return; |
| 3037 if( pWith->nCte==0 ) return; |
| 3038 if( pWith->pOuter ){ |
| 3039 sqlite3TreeViewLine(pView, "WITH (0x%p, pOuter=0x%p)",pWith,pWith->pOuter); |
| 3040 }else{ |
| 3041 sqlite3TreeViewLine(pView, "WITH (0x%p)", pWith); |
| 3042 } |
| 3043 if( pWith->nCte>0 ){ |
| 3044 pView = sqlite3TreeViewPush(pView, 1); |
| 3045 for(i=0; i<pWith->nCte; i++){ |
| 3046 StrAccum x; |
| 3047 char zLine[1000]; |
| 3048 const struct Cte *pCte = &pWith->a[i]; |
| 3049 sqlite3StrAccumInit(&x, 0, zLine, sizeof(zLine), 0); |
| 3050 sqlite3XPrintf(&x, 0, "%s", pCte->zName); |
| 3051 if( pCte->pCols && pCte->pCols->nExpr>0 ){ |
| 3052 char cSep = '('; |
| 3053 int j; |
| 3054 for(j=0; j<pCte->pCols->nExpr; j++){ |
| 3055 sqlite3XPrintf(&x, 0, "%c%s", cSep, pCte->pCols->a[j].zName); |
| 3056 cSep = ','; |
| 3057 } |
| 3058 sqlite3XPrintf(&x, 0, ")"); |
| 3059 } |
| 3060 sqlite3XPrintf(&x, 0, " AS"); |
| 3061 sqlite3StrAccumFinish(&x); |
| 3062 sqlite3TreeViewItem(pView, zLine, i<pWith->nCte-1); |
| 3063 sqlite3TreeViewSelect(pView, pCte->pSelect, 0); |
| 3064 sqlite3TreeViewPop(pView); |
| 3065 } |
| 3066 sqlite3TreeViewPop(pView); |
| 3067 } |
| 3068 } |
| 3069 |
| 3070 |
| 3071 /* |
| 3072 ** Generate a human-readable description of a the Select object. |
| 3073 */ |
| 3074 SQLITE_PRIVATE void sqlite3TreeViewSelect(TreeView *pView, const Select *p, u8 m
oreToFollow){ |
| 3075 int n = 0; |
| 3076 int cnt = 0; |
| 3077 pView = sqlite3TreeViewPush(pView, moreToFollow); |
| 3078 if( p->pWith ){ |
| 3079 sqlite3TreeViewWith(pView, p->pWith, 1); |
| 3080 cnt = 1; |
| 3081 sqlite3TreeViewPush(pView, 1); |
| 3082 } |
| 3083 do{ |
| 3084 sqlite3TreeViewLine(pView, "SELECT%s%s (0x%p) selFlags=0x%x", |
| 3085 ((p->selFlags & SF_Distinct) ? " DISTINCT" : ""), |
| 3086 ((p->selFlags & SF_Aggregate) ? " agg_flag" : ""), p, p->selFlags |
| 3087 ); |
| 3088 if( cnt++ ) sqlite3TreeViewPop(pView); |
| 3089 if( p->pPrior ){ |
| 3090 n = 1000; |
| 3091 }else{ |
| 3092 n = 0; |
| 3093 if( p->pSrc && p->pSrc->nSrc ) n++; |
| 3094 if( p->pWhere ) n++; |
| 3095 if( p->pGroupBy ) n++; |
| 3096 if( p->pHaving ) n++; |
| 3097 if( p->pOrderBy ) n++; |
| 3098 if( p->pLimit ) n++; |
| 3099 if( p->pOffset ) n++; |
| 3100 } |
| 3101 sqlite3TreeViewExprList(pView, p->pEList, (n--)>0, "result-set"); |
| 3102 if( p->pSrc && p->pSrc->nSrc ){ |
| 3103 int i; |
| 3104 pView = sqlite3TreeViewPush(pView, (n--)>0); |
| 3105 sqlite3TreeViewLine(pView, "FROM"); |
| 3106 for(i=0; i<p->pSrc->nSrc; i++){ |
| 3107 struct SrcList_item *pItem = &p->pSrc->a[i]; |
| 3108 StrAccum x; |
| 3109 char zLine[100]; |
| 3110 sqlite3StrAccumInit(&x, 0, zLine, sizeof(zLine), 0); |
| 3111 sqlite3XPrintf(&x, 0, "{%d,*}", pItem->iCursor); |
| 3112 if( pItem->zDatabase ){ |
| 3113 sqlite3XPrintf(&x, 0, " %s.%s", pItem->zDatabase, pItem->zName); |
| 3114 }else if( pItem->zName ){ |
| 3115 sqlite3XPrintf(&x, 0, " %s", pItem->zName); |
| 3116 } |
| 3117 if( pItem->pTab ){ |
| 3118 sqlite3XPrintf(&x, 0, " tabname=%Q", pItem->pTab->zName); |
| 3119 } |
| 3120 if( pItem->zAlias ){ |
| 3121 sqlite3XPrintf(&x, 0, " (AS %s)", pItem->zAlias); |
| 3122 } |
| 3123 if( pItem->fg.jointype & JT_LEFT ){ |
| 3124 sqlite3XPrintf(&x, 0, " LEFT-JOIN"); |
| 3125 } |
| 3126 sqlite3StrAccumFinish(&x); |
| 3127 sqlite3TreeViewItem(pView, zLine, i<p->pSrc->nSrc-1); |
| 3128 if( pItem->pSelect ){ |
| 3129 sqlite3TreeViewSelect(pView, pItem->pSelect, 0); |
| 3130 } |
| 3131 if( pItem->fg.isTabFunc ){ |
| 3132 sqlite3TreeViewExprList(pView, pItem->u1.pFuncArg, 0, "func-args:"); |
| 3133 } |
| 3134 sqlite3TreeViewPop(pView); |
| 3135 } |
| 3136 sqlite3TreeViewPop(pView); |
| 3137 } |
| 3138 if( p->pWhere ){ |
| 3139 sqlite3TreeViewItem(pView, "WHERE", (n--)>0); |
| 3140 sqlite3TreeViewExpr(pView, p->pWhere, 0); |
| 3141 sqlite3TreeViewPop(pView); |
| 3142 } |
| 3143 if( p->pGroupBy ){ |
| 3144 sqlite3TreeViewExprList(pView, p->pGroupBy, (n--)>0, "GROUPBY"); |
| 3145 } |
| 3146 if( p->pHaving ){ |
| 3147 sqlite3TreeViewItem(pView, "HAVING", (n--)>0); |
| 3148 sqlite3TreeViewExpr(pView, p->pHaving, 0); |
| 3149 sqlite3TreeViewPop(pView); |
| 3150 } |
| 3151 if( p->pOrderBy ){ |
| 3152 sqlite3TreeViewExprList(pView, p->pOrderBy, (n--)>0, "ORDERBY"); |
| 3153 } |
| 3154 if( p->pLimit ){ |
| 3155 sqlite3TreeViewItem(pView, "LIMIT", (n--)>0); |
| 3156 sqlite3TreeViewExpr(pView, p->pLimit, 0); |
| 3157 sqlite3TreeViewPop(pView); |
| 3158 } |
| 3159 if( p->pOffset ){ |
| 3160 sqlite3TreeViewItem(pView, "OFFSET", (n--)>0); |
| 3161 sqlite3TreeViewExpr(pView, p->pOffset, 0); |
| 3162 sqlite3TreeViewPop(pView); |
| 3163 } |
| 3164 if( p->pPrior ){ |
| 3165 const char *zOp = "UNION"; |
| 3166 switch( p->op ){ |
| 3167 case TK_ALL: zOp = "UNION ALL"; break; |
| 3168 case TK_INTERSECT: zOp = "INTERSECT"; break; |
| 3169 case TK_EXCEPT: zOp = "EXCEPT"; break; |
| 3170 } |
| 3171 sqlite3TreeViewItem(pView, zOp, 1); |
| 3172 } |
| 3173 p = p->pPrior; |
| 3174 }while( p!=0 ); |
| 3175 sqlite3TreeViewPop(pView); |
| 3176 } |
| 3177 |
| 3178 /* |
| 3179 ** Generate a human-readable explanation of an expression tree. |
| 3180 */ |
| 3181 SQLITE_PRIVATE void sqlite3TreeViewExpr(TreeView *pView, const Expr *pExpr, u8 m
oreToFollow){ |
| 3182 const char *zBinOp = 0; /* Binary operator */ |
| 3183 const char *zUniOp = 0; /* Unary operator */ |
| 3184 char zFlgs[30]; |
| 3185 pView = sqlite3TreeViewPush(pView, moreToFollow); |
| 3186 if( pExpr==0 ){ |
| 3187 sqlite3TreeViewLine(pView, "nil"); |
| 3188 sqlite3TreeViewPop(pView); |
| 3189 return; |
| 3190 } |
| 3191 if( pExpr->flags ){ |
| 3192 sqlite3_snprintf(sizeof(zFlgs),zFlgs," flags=0x%x",pExpr->flags); |
| 3193 }else{ |
| 3194 zFlgs[0] = 0; |
| 3195 } |
| 3196 switch( pExpr->op ){ |
| 3197 case TK_AGG_COLUMN: { |
| 3198 sqlite3TreeViewLine(pView, "AGG{%d:%d}%s", |
| 3199 pExpr->iTable, pExpr->iColumn, zFlgs); |
| 3200 break; |
| 3201 } |
| 3202 case TK_COLUMN: { |
| 3203 if( pExpr->iTable<0 ){ |
| 3204 /* This only happens when coding check constraints */ |
| 3205 sqlite3TreeViewLine(pView, "COLUMN(%d)%s", pExpr->iColumn, zFlgs); |
| 3206 }else{ |
| 3207 sqlite3TreeViewLine(pView, "{%d:%d}%s", |
| 3208 pExpr->iTable, pExpr->iColumn, zFlgs); |
| 3209 } |
| 3210 break; |
| 3211 } |
| 3212 case TK_INTEGER: { |
| 3213 if( pExpr->flags & EP_IntValue ){ |
| 3214 sqlite3TreeViewLine(pView, "%d", pExpr->u.iValue); |
| 3215 }else{ |
| 3216 sqlite3TreeViewLine(pView, "%s", pExpr->u.zToken); |
| 3217 } |
| 3218 break; |
| 3219 } |
| 3220 #ifndef SQLITE_OMIT_FLOATING_POINT |
| 3221 case TK_FLOAT: { |
| 3222 sqlite3TreeViewLine(pView,"%s", pExpr->u.zToken); |
| 3223 break; |
| 3224 } |
| 3225 #endif |
| 3226 case TK_STRING: { |
| 3227 sqlite3TreeViewLine(pView,"%Q", pExpr->u.zToken); |
| 3228 break; |
| 3229 } |
| 3230 case TK_NULL: { |
| 3231 sqlite3TreeViewLine(pView,"NULL"); |
| 3232 break; |
| 3233 } |
| 3234 #ifndef SQLITE_OMIT_BLOB_LITERAL |
| 3235 case TK_BLOB: { |
| 3236 sqlite3TreeViewLine(pView,"%s", pExpr->u.zToken); |
| 3237 break; |
| 3238 } |
| 3239 #endif |
| 3240 case TK_VARIABLE: { |
| 3241 sqlite3TreeViewLine(pView,"VARIABLE(%s,%d)", |
| 3242 pExpr->u.zToken, pExpr->iColumn); |
| 3243 break; |
| 3244 } |
| 3245 case TK_REGISTER: { |
| 3246 sqlite3TreeViewLine(pView,"REGISTER(%d)", pExpr->iTable); |
| 3247 break; |
| 3248 } |
| 3249 case TK_ID: { |
| 3250 sqlite3TreeViewLine(pView,"ID \"%w\"", pExpr->u.zToken); |
| 3251 break; |
| 3252 } |
| 3253 #ifndef SQLITE_OMIT_CAST |
| 3254 case TK_CAST: { |
| 3255 /* Expressions of the form: CAST(pLeft AS token) */ |
| 3256 sqlite3TreeViewLine(pView,"CAST %Q", pExpr->u.zToken); |
| 3257 sqlite3TreeViewExpr(pView, pExpr->pLeft, 0); |
| 3258 break; |
| 3259 } |
| 3260 #endif /* SQLITE_OMIT_CAST */ |
| 3261 case TK_LT: zBinOp = "LT"; break; |
| 3262 case TK_LE: zBinOp = "LE"; break; |
| 3263 case TK_GT: zBinOp = "GT"; break; |
| 3264 case TK_GE: zBinOp = "GE"; break; |
| 3265 case TK_NE: zBinOp = "NE"; break; |
| 3266 case TK_EQ: zBinOp = "EQ"; break; |
| 3267 case TK_IS: zBinOp = "IS"; break; |
| 3268 case TK_ISNOT: zBinOp = "ISNOT"; break; |
| 3269 case TK_AND: zBinOp = "AND"; break; |
| 3270 case TK_OR: zBinOp = "OR"; break; |
| 3271 case TK_PLUS: zBinOp = "ADD"; break; |
| 3272 case TK_STAR: zBinOp = "MUL"; break; |
| 3273 case TK_MINUS: zBinOp = "SUB"; break; |
| 3274 case TK_REM: zBinOp = "REM"; break; |
| 3275 case TK_BITAND: zBinOp = "BITAND"; break; |
| 3276 case TK_BITOR: zBinOp = "BITOR"; break; |
| 3277 case TK_SLASH: zBinOp = "DIV"; break; |
| 3278 case TK_LSHIFT: zBinOp = "LSHIFT"; break; |
| 3279 case TK_RSHIFT: zBinOp = "RSHIFT"; break; |
| 3280 case TK_CONCAT: zBinOp = "CONCAT"; break; |
| 3281 case TK_DOT: zBinOp = "DOT"; break; |
| 3282 |
| 3283 case TK_UMINUS: zUniOp = "UMINUS"; break; |
| 3284 case TK_UPLUS: zUniOp = "UPLUS"; break; |
| 3285 case TK_BITNOT: zUniOp = "BITNOT"; break; |
| 3286 case TK_NOT: zUniOp = "NOT"; break; |
| 3287 case TK_ISNULL: zUniOp = "ISNULL"; break; |
| 3288 case TK_NOTNULL: zUniOp = "NOTNULL"; break; |
| 3289 |
| 3290 case TK_COLLATE: { |
| 3291 sqlite3TreeViewLine(pView, "COLLATE %Q", pExpr->u.zToken); |
| 3292 sqlite3TreeViewExpr(pView, pExpr->pLeft, 0); |
| 3293 break; |
| 3294 } |
| 3295 |
| 3296 case TK_AGG_FUNCTION: |
| 3297 case TK_FUNCTION: { |
| 3298 ExprList *pFarg; /* List of function arguments */ |
| 3299 if( ExprHasProperty(pExpr, EP_TokenOnly) ){ |
| 3300 pFarg = 0; |
| 3301 }else{ |
| 3302 pFarg = pExpr->x.pList; |
| 3303 } |
| 3304 if( pExpr->op==TK_AGG_FUNCTION ){ |
| 3305 sqlite3TreeViewLine(pView, "AGG_FUNCTION%d %Q", |
| 3306 pExpr->op2, pExpr->u.zToken); |
| 3307 }else{ |
| 3308 sqlite3TreeViewLine(pView, "FUNCTION %Q", pExpr->u.zToken); |
| 3309 } |
| 3310 if( pFarg ){ |
| 3311 sqlite3TreeViewExprList(pView, pFarg, 0, 0); |
| 3312 } |
| 3313 break; |
| 3314 } |
| 3315 #ifndef SQLITE_OMIT_SUBQUERY |
| 3316 case TK_EXISTS: { |
| 3317 sqlite3TreeViewLine(pView, "EXISTS-expr"); |
| 3318 sqlite3TreeViewSelect(pView, pExpr->x.pSelect, 0); |
| 3319 break; |
| 3320 } |
| 3321 case TK_SELECT: { |
| 3322 sqlite3TreeViewLine(pView, "SELECT-expr"); |
| 3323 sqlite3TreeViewSelect(pView, pExpr->x.pSelect, 0); |
| 3324 break; |
| 3325 } |
| 3326 case TK_IN: { |
| 3327 sqlite3TreeViewLine(pView, "IN"); |
| 3328 sqlite3TreeViewExpr(pView, pExpr->pLeft, 1); |
| 3329 if( ExprHasProperty(pExpr, EP_xIsSelect) ){ |
| 3330 sqlite3TreeViewSelect(pView, pExpr->x.pSelect, 0); |
| 3331 }else{ |
| 3332 sqlite3TreeViewExprList(pView, pExpr->x.pList, 0, 0); |
| 3333 } |
| 3334 break; |
| 3335 } |
| 3336 #endif /* SQLITE_OMIT_SUBQUERY */ |
| 3337 |
| 3338 /* |
| 3339 ** x BETWEEN y AND z |
| 3340 ** |
| 3341 ** This is equivalent to |
| 3342 ** |
| 3343 ** x>=y AND x<=z |
| 3344 ** |
| 3345 ** X is stored in pExpr->pLeft. |
| 3346 ** Y is stored in pExpr->pList->a[0].pExpr. |
| 3347 ** Z is stored in pExpr->pList->a[1].pExpr. |
| 3348 */ |
| 3349 case TK_BETWEEN: { |
| 3350 Expr *pX = pExpr->pLeft; |
| 3351 Expr *pY = pExpr->x.pList->a[0].pExpr; |
| 3352 Expr *pZ = pExpr->x.pList->a[1].pExpr; |
| 3353 sqlite3TreeViewLine(pView, "BETWEEN"); |
| 3354 sqlite3TreeViewExpr(pView, pX, 1); |
| 3355 sqlite3TreeViewExpr(pView, pY, 1); |
| 3356 sqlite3TreeViewExpr(pView, pZ, 0); |
| 3357 break; |
| 3358 } |
| 3359 case TK_TRIGGER: { |
| 3360 /* If the opcode is TK_TRIGGER, then the expression is a reference |
| 3361 ** to a column in the new.* or old.* pseudo-tables available to |
| 3362 ** trigger programs. In this case Expr.iTable is set to 1 for the |
| 3363 ** new.* pseudo-table, or 0 for the old.* pseudo-table. Expr.iColumn |
| 3364 ** is set to the column of the pseudo-table to read, or to -1 to |
| 3365 ** read the rowid field. |
| 3366 */ |
| 3367 sqlite3TreeViewLine(pView, "%s(%d)", |
| 3368 pExpr->iTable ? "NEW" : "OLD", pExpr->iColumn); |
| 3369 break; |
| 3370 } |
| 3371 case TK_CASE: { |
| 3372 sqlite3TreeViewLine(pView, "CASE"); |
| 3373 sqlite3TreeViewExpr(pView, pExpr->pLeft, 1); |
| 3374 sqlite3TreeViewExprList(pView, pExpr->x.pList, 0, 0); |
| 3375 break; |
| 3376 } |
| 3377 #ifndef SQLITE_OMIT_TRIGGER |
| 3378 case TK_RAISE: { |
| 3379 const char *zType = "unk"; |
| 3380 switch( pExpr->affinity ){ |
| 3381 case OE_Rollback: zType = "rollback"; break; |
| 3382 case OE_Abort: zType = "abort"; break; |
| 3383 case OE_Fail: zType = "fail"; break; |
| 3384 case OE_Ignore: zType = "ignore"; break; |
| 3385 } |
| 3386 sqlite3TreeViewLine(pView, "RAISE %s(%Q)", zType, pExpr->u.zToken); |
| 3387 break; |
| 3388 } |
| 3389 #endif |
| 3390 default: { |
| 3391 sqlite3TreeViewLine(pView, "op=%d", pExpr->op); |
| 3392 break; |
| 3393 } |
| 3394 } |
| 3395 if( zBinOp ){ |
| 3396 sqlite3TreeViewLine(pView, "%s%s", zBinOp, zFlgs); |
| 3397 sqlite3TreeViewExpr(pView, pExpr->pLeft, 1); |
| 3398 sqlite3TreeViewExpr(pView, pExpr->pRight, 0); |
| 3399 }else if( zUniOp ){ |
| 3400 sqlite3TreeViewLine(pView, "%s%s", zUniOp, zFlgs); |
| 3401 sqlite3TreeViewExpr(pView, pExpr->pLeft, 0); |
| 3402 } |
| 3403 sqlite3TreeViewPop(pView); |
| 3404 } |
| 3405 |
| 3406 /* |
| 3407 ** Generate a human-readable explanation of an expression list. |
| 3408 */ |
| 3409 SQLITE_PRIVATE void sqlite3TreeViewExprList( |
| 3410 TreeView *pView, |
| 3411 const ExprList *pList, |
| 3412 u8 moreToFollow, |
| 3413 const char *zLabel |
| 3414 ){ |
| 3415 int i; |
| 3416 pView = sqlite3TreeViewPush(pView, moreToFollow); |
| 3417 if( zLabel==0 || zLabel[0]==0 ) zLabel = "LIST"; |
| 3418 if( pList==0 ){ |
| 3419 sqlite3TreeViewLine(pView, "%s (empty)", zLabel); |
| 3420 }else{ |
| 3421 sqlite3TreeViewLine(pView, "%s", zLabel); |
| 3422 for(i=0; i<pList->nExpr; i++){ |
| 3423 int j = pList->a[i].u.x.iOrderByCol; |
| 3424 if( j ){ |
| 3425 sqlite3TreeViewPush(pView, 0); |
| 3426 sqlite3TreeViewLine(pView, "iOrderByCol=%d", j); |
| 3427 } |
| 3428 sqlite3TreeViewExpr(pView, pList->a[i].pExpr, i<pList->nExpr-1); |
| 3429 if( j ) sqlite3TreeViewPop(pView); |
| 3430 } |
| 3431 } |
| 3432 sqlite3TreeViewPop(pView); |
| 3433 } |
| 3434 |
| 3435 #endif /* SQLITE_DEBUG */ |
| 3436 |
| 3437 /************** End of treeview.c ********************************************/ |
| 3438 /************** Begin file random.c ******************************************/ |
| 3439 /* |
| 3440 ** 2001 September 15 |
| 3441 ** |
| 3442 ** The author disclaims copyright to this source code. In place of |
| 3443 ** a legal notice, here is a blessing: |
| 3444 ** |
| 3445 ** May you do good and not evil. |
| 3446 ** May you find forgiveness for yourself and forgive others. |
| 3447 ** May you share freely, never taking more than you give. |
| 3448 ** |
| 3449 ************************************************************************* |
| 3450 ** This file contains code to implement a pseudo-random number |
| 3451 ** generator (PRNG) for SQLite. |
| 3452 ** |
| 3453 ** Random numbers are used by some of the database backends in order |
| 3454 ** to generate random integer keys for tables or random filenames. |
| 3455 */ |
| 3456 /* #include "sqliteInt.h" */ |
| 3457 |
| 3458 |
| 3459 /* All threads share a single random number generator. |
| 3460 ** This structure is the current state of the generator. |
| 3461 */ |
| 3462 static SQLITE_WSD struct sqlite3PrngType { |
| 3463 unsigned char isInit; /* True if initialized */ |
| 3464 unsigned char i, j; /* State variables */ |
| 3465 unsigned char s[256]; /* State variables */ |
| 3466 } sqlite3Prng; |
| 3467 |
| 3468 /* |
| 3469 ** Return N random bytes. |
| 3470 */ |
| 3471 SQLITE_API void SQLITE_STDCALL sqlite3_randomness(int N, void *pBuf){ |
| 3472 unsigned char t; |
| 3473 unsigned char *zBuf = pBuf; |
| 3474 |
| 3475 /* The "wsdPrng" macro will resolve to the pseudo-random number generator |
| 3476 ** state vector. If writable static data is unsupported on the target, |
| 3477 ** we have to locate the state vector at run-time. In the more common |
| 3478 ** case where writable static data is supported, wsdPrng can refer directly |
| 3479 ** to the "sqlite3Prng" state vector declared above. |
| 3480 */ |
| 3481 #ifdef SQLITE_OMIT_WSD |
| 3482 struct sqlite3PrngType *p = &GLOBAL(struct sqlite3PrngType, sqlite3Prng); |
| 3483 # define wsdPrng p[0] |
| 3484 #else |
| 3485 # define wsdPrng sqlite3Prng |
| 3486 #endif |
| 3487 |
| 3488 #if SQLITE_THREADSAFE |
| 3489 sqlite3_mutex *mutex; |
| 3490 #endif |
| 3491 |
| 3492 #ifndef SQLITE_OMIT_AUTOINIT |
| 3493 if( sqlite3_initialize() ) return; |
| 3494 #endif |
| 3495 |
| 3496 #if SQLITE_THREADSAFE |
| 3497 mutex = sqlite3MutexAlloc(SQLITE_MUTEX_STATIC_PRNG); |
| 3498 #endif |
| 3499 |
| 3500 sqlite3_mutex_enter(mutex); |
| 3501 if( N<=0 || pBuf==0 ){ |
| 3502 wsdPrng.isInit = 0; |
| 3503 sqlite3_mutex_leave(mutex); |
| 3504 return; |
| 3505 } |
| 3506 |
| 3507 /* Initialize the state of the random number generator once, |
| 3508 ** the first time this routine is called. The seed value does |
| 3509 ** not need to contain a lot of randomness since we are not |
| 3510 ** trying to do secure encryption or anything like that... |
| 3511 ** |
| 3512 ** Nothing in this file or anywhere else in SQLite does any kind of |
| 3513 ** encryption. The RC4 algorithm is being used as a PRNG (pseudo-random |
| 3514 ** number generator) not as an encryption device. |
| 3515 */ |
| 3516 if( !wsdPrng.isInit ){ |
| 3517 int i; |
| 3518 char k[256]; |
| 3519 wsdPrng.j = 0; |
| 3520 wsdPrng.i = 0; |
| 3521 sqlite3OsRandomness(sqlite3_vfs_find(0), 256, k); |
| 3522 for(i=0; i<256; i++){ |
| 3523 wsdPrng.s[i] = (u8)i; |
| 3524 } |
| 3525 for(i=0; i<256; i++){ |
| 3526 wsdPrng.j += wsdPrng.s[i] + k[i]; |
| 3527 t = wsdPrng.s[wsdPrng.j]; |
| 3528 wsdPrng.s[wsdPrng.j] = wsdPrng.s[i]; |
| 3529 wsdPrng.s[i] = t; |
| 3530 } |
| 3531 wsdPrng.isInit = 1; |
| 3532 } |
| 3533 |
| 3534 assert( N>0 ); |
| 3535 do{ |
| 3536 wsdPrng.i++; |
| 3537 t = wsdPrng.s[wsdPrng.i]; |
| 3538 wsdPrng.j += t; |
| 3539 wsdPrng.s[wsdPrng.i] = wsdPrng.s[wsdPrng.j]; |
| 3540 wsdPrng.s[wsdPrng.j] = t; |
| 3541 t += wsdPrng.s[wsdPrng.i]; |
| 3542 *(zBuf++) = wsdPrng.s[t]; |
| 3543 }while( --N ); |
| 3544 sqlite3_mutex_leave(mutex); |
| 3545 } |
| 3546 |
| 3547 #ifndef SQLITE_OMIT_BUILTIN_TEST |
| 3548 /* |
| 3549 ** For testing purposes, we sometimes want to preserve the state of |
| 3550 ** PRNG and restore the PRNG to its saved state at a later time, or |
| 3551 ** to reset the PRNG to its initial state. These routines accomplish |
| 3552 ** those tasks. |
| 3553 ** |
| 3554 ** The sqlite3_test_control() interface calls these routines to |
| 3555 ** control the PRNG. |
| 3556 */ |
| 3557 static SQLITE_WSD struct sqlite3PrngType sqlite3SavedPrng; |
| 3558 SQLITE_PRIVATE void sqlite3PrngSaveState(void){ |
| 3559 memcpy( |
| 3560 &GLOBAL(struct sqlite3PrngType, sqlite3SavedPrng), |
| 3561 &GLOBAL(struct sqlite3PrngType, sqlite3Prng), |
| 3562 sizeof(sqlite3Prng) |
| 3563 ); |
| 3564 } |
| 3565 SQLITE_PRIVATE void sqlite3PrngRestoreState(void){ |
| 3566 memcpy( |
| 3567 &GLOBAL(struct sqlite3PrngType, sqlite3Prng), |
| 3568 &GLOBAL(struct sqlite3PrngType, sqlite3SavedPrng), |
| 3569 sizeof(sqlite3Prng) |
| 3570 ); |
| 3571 } |
| 3572 #endif /* SQLITE_OMIT_BUILTIN_TEST */ |
| 3573 |
| 3574 /************** End of random.c **********************************************/ |
| 3575 /************** Begin file threads.c *****************************************/ |
| 3576 /* |
| 3577 ** 2012 July 21 |
| 3578 ** |
| 3579 ** The author disclaims copyright to this source code. In place of |
| 3580 ** a legal notice, here is a blessing: |
| 3581 ** |
| 3582 ** May you do good and not evil. |
| 3583 ** May you find forgiveness for yourself and forgive others. |
| 3584 ** May you share freely, never taking more than you give. |
| 3585 ** |
| 3586 ****************************************************************************** |
| 3587 ** |
| 3588 ** This file presents a simple cross-platform threading interface for |
| 3589 ** use internally by SQLite. |
| 3590 ** |
| 3591 ** A "thread" can be created using sqlite3ThreadCreate(). This thread |
| 3592 ** runs independently of its creator until it is joined using |
| 3593 ** sqlite3ThreadJoin(), at which point it terminates. |
| 3594 ** |
| 3595 ** Threads do not have to be real. It could be that the work of the |
| 3596 ** "thread" is done by the main thread at either the sqlite3ThreadCreate() |
| 3597 ** or sqlite3ThreadJoin() call. This is, in fact, what happens in |
| 3598 ** single threaded systems. Nothing in SQLite requires multiple threads. |
| 3599 ** This interface exists so that applications that want to take advantage |
| 3600 ** of multiple cores can do so, while also allowing applications to stay |
| 3601 ** single-threaded if desired. |
| 3602 */ |
| 3603 /* #include "sqliteInt.h" */ |
| 3604 #if SQLITE_OS_WIN |
| 3605 /* # include "os_win.h" */ |
| 3606 #endif |
| 3607 |
| 3608 #if SQLITE_MAX_WORKER_THREADS>0 |
| 3609 |
| 3610 /********************************* Unix Pthreads ****************************/ |
| 3611 #if SQLITE_OS_UNIX && defined(SQLITE_MUTEX_PTHREADS) && SQLITE_THREADSAFE>0 |
| 3612 |
| 3613 #define SQLITE_THREADS_IMPLEMENTED 1 /* Prevent the single-thread code below */ |
| 3614 /* #include <pthread.h> */ |
| 3615 |
| 3616 /* A running thread */ |
| 3617 struct SQLiteThread { |
| 3618 pthread_t tid; /* Thread ID */ |
| 3619 int done; /* Set to true when thread finishes */ |
| 3620 void *pOut; /* Result returned by the thread */ |
| 3621 void *(*xTask)(void*); /* The thread routine */ |
| 3622 void *pIn; /* Argument to the thread */ |
| 3623 }; |
| 3624 |
| 3625 /* Create a new thread */ |
| 3626 SQLITE_PRIVATE int sqlite3ThreadCreate( |
| 3627 SQLiteThread **ppThread, /* OUT: Write the thread object here */ |
| 3628 void *(*xTask)(void*), /* Routine to run in a separate thread */ |
| 3629 void *pIn /* Argument passed into xTask() */ |
| 3630 ){ |
| 3631 SQLiteThread *p; |
| 3632 int rc; |
| 3633 |
| 3634 assert( ppThread!=0 ); |
| 3635 assert( xTask!=0 ); |
| 3636 /* This routine is never used in single-threaded mode */ |
| 3637 assert( sqlite3GlobalConfig.bCoreMutex!=0 ); |
| 3638 |
| 3639 *ppThread = 0; |
| 3640 p = sqlite3Malloc(sizeof(*p)); |
| 3641 if( p==0 ) return SQLITE_NOMEM; |
| 3642 memset(p, 0, sizeof(*p)); |
| 3643 p->xTask = xTask; |
| 3644 p->pIn = pIn; |
| 3645 /* If the SQLITE_TESTCTRL_FAULT_INSTALL callback is registered to a |
| 3646 ** function that returns SQLITE_ERROR when passed the argument 200, that |
| 3647 ** forces worker threads to run sequentially and deterministically |
| 3648 ** for testing purposes. */ |
| 3649 if( sqlite3FaultSim(200) ){ |
| 3650 rc = 1; |
| 3651 }else{ |
| 3652 rc = pthread_create(&p->tid, 0, xTask, pIn); |
| 3653 } |
| 3654 if( rc ){ |
| 3655 p->done = 1; |
| 3656 p->pOut = xTask(pIn); |
| 3657 } |
| 3658 *ppThread = p; |
| 3659 return SQLITE_OK; |
| 3660 } |
| 3661 |
| 3662 /* Get the results of the thread */ |
| 3663 SQLITE_PRIVATE int sqlite3ThreadJoin(SQLiteThread *p, void **ppOut){ |
| 3664 int rc; |
| 3665 |
| 3666 assert( ppOut!=0 ); |
| 3667 if( NEVER(p==0) ) return SQLITE_NOMEM; |
| 3668 if( p->done ){ |
| 3669 *ppOut = p->pOut; |
| 3670 rc = SQLITE_OK; |
| 3671 }else{ |
| 3672 rc = pthread_join(p->tid, ppOut) ? SQLITE_ERROR : SQLITE_OK; |
| 3673 } |
| 3674 sqlite3_free(p); |
| 3675 return rc; |
| 3676 } |
| 3677 |
| 3678 #endif /* SQLITE_OS_UNIX && defined(SQLITE_MUTEX_PTHREADS) */ |
| 3679 /******************************** End Unix Pthreads *************************/ |
| 3680 |
| 3681 |
| 3682 /********************************* Win32 Threads ****************************/ |
| 3683 #if SQLITE_OS_WIN_THREADS |
| 3684 |
| 3685 #define SQLITE_THREADS_IMPLEMENTED 1 /* Prevent the single-thread code below */ |
| 3686 #include <process.h> |
| 3687 |
| 3688 /* A running thread */ |
| 3689 struct SQLiteThread { |
| 3690 void *tid; /* The thread handle */ |
| 3691 unsigned id; /* The thread identifier */ |
| 3692 void *(*xTask)(void*); /* The routine to run as a thread */ |
| 3693 void *pIn; /* Argument to xTask */ |
| 3694 void *pResult; /* Result of xTask */ |
| 3695 }; |
| 3696 |
| 3697 /* Thread procedure Win32 compatibility shim */ |
| 3698 static unsigned __stdcall sqlite3ThreadProc( |
| 3699 void *pArg /* IN: Pointer to the SQLiteThread structure */ |
| 3700 ){ |
| 3701 SQLiteThread *p = (SQLiteThread *)pArg; |
| 3702 |
| 3703 assert( p!=0 ); |
| 3704 #if 0 |
| 3705 /* |
| 3706 ** This assert appears to trigger spuriously on certain |
| 3707 ** versions of Windows, possibly due to _beginthreadex() |
| 3708 ** and/or CreateThread() not fully setting their thread |
| 3709 ** ID parameter before starting the thread. |
| 3710 */ |
| 3711 assert( p->id==GetCurrentThreadId() ); |
| 3712 #endif |
| 3713 assert( p->xTask!=0 ); |
| 3714 p->pResult = p->xTask(p->pIn); |
| 3715 |
| 3716 _endthreadex(0); |
| 3717 return 0; /* NOT REACHED */ |
| 3718 } |
| 3719 |
| 3720 /* Create a new thread */ |
| 3721 SQLITE_PRIVATE int sqlite3ThreadCreate( |
| 3722 SQLiteThread **ppThread, /* OUT: Write the thread object here */ |
| 3723 void *(*xTask)(void*), /* Routine to run in a separate thread */ |
| 3724 void *pIn /* Argument passed into xTask() */ |
| 3725 ){ |
| 3726 SQLiteThread *p; |
| 3727 |
| 3728 assert( ppThread!=0 ); |
| 3729 assert( xTask!=0 ); |
| 3730 *ppThread = 0; |
| 3731 p = sqlite3Malloc(sizeof(*p)); |
| 3732 if( p==0 ) return SQLITE_NOMEM; |
| 3733 /* If the SQLITE_TESTCTRL_FAULT_INSTALL callback is registered to a |
| 3734 ** function that returns SQLITE_ERROR when passed the argument 200, that |
| 3735 ** forces worker threads to run sequentially and deterministically |
| 3736 ** (via the sqlite3FaultSim() term of the conditional) for testing |
| 3737 ** purposes. */ |
| 3738 if( sqlite3GlobalConfig.bCoreMutex==0 || sqlite3FaultSim(200) ){ |
| 3739 memset(p, 0, sizeof(*p)); |
| 3740 }else{ |
| 3741 p->xTask = xTask; |
| 3742 p->pIn = pIn; |
| 3743 p->tid = (void*)_beginthreadex(0, 0, sqlite3ThreadProc, p, 0, &p->id); |
| 3744 if( p->tid==0 ){ |
| 3745 memset(p, 0, sizeof(*p)); |
| 3746 } |
| 3747 } |
| 3748 if( p->xTask==0 ){ |
| 3749 p->id = GetCurrentThreadId(); |
| 3750 p->pResult = xTask(pIn); |
| 3751 } |
| 3752 *ppThread = p; |
| 3753 return SQLITE_OK; |
| 3754 } |
| 3755 |
| 3756 SQLITE_PRIVATE DWORD sqlite3Win32Wait(HANDLE hObject); /* os_win.c */ |
| 3757 |
| 3758 /* Get the results of the thread */ |
| 3759 SQLITE_PRIVATE int sqlite3ThreadJoin(SQLiteThread *p, void **ppOut){ |
| 3760 DWORD rc; |
| 3761 BOOL bRc; |
| 3762 |
| 3763 assert( ppOut!=0 ); |
| 3764 if( NEVER(p==0) ) return SQLITE_NOMEM; |
| 3765 if( p->xTask==0 ){ |
| 3766 /* assert( p->id==GetCurrentThreadId() ); */ |
| 3767 rc = WAIT_OBJECT_0; |
| 3768 assert( p->tid==0 ); |
| 3769 }else{ |
| 3770 assert( p->id!=0 && p->id!=GetCurrentThreadId() ); |
| 3771 rc = sqlite3Win32Wait((HANDLE)p->tid); |
| 3772 assert( rc!=WAIT_IO_COMPLETION ); |
| 3773 bRc = CloseHandle((HANDLE)p->tid); |
| 3774 assert( bRc ); |
| 3775 } |
| 3776 if( rc==WAIT_OBJECT_0 ) *ppOut = p->pResult; |
| 3777 sqlite3_free(p); |
| 3778 return (rc==WAIT_OBJECT_0) ? SQLITE_OK : SQLITE_ERROR; |
| 3779 } |
| 3780 |
| 3781 #endif /* SQLITE_OS_WIN_THREADS */ |
| 3782 /******************************** End Win32 Threads *************************/ |
| 3783 |
| 3784 |
| 3785 /********************************* Single-Threaded **************************/ |
| 3786 #ifndef SQLITE_THREADS_IMPLEMENTED |
| 3787 /* |
| 3788 ** This implementation does not actually create a new thread. It does the |
| 3789 ** work of the thread in the main thread, when either the thread is created |
| 3790 ** or when it is joined |
| 3791 */ |
| 3792 |
| 3793 /* A running thread */ |
| 3794 struct SQLiteThread { |
| 3795 void *(*xTask)(void*); /* The routine to run as a thread */ |
| 3796 void *pIn; /* Argument to xTask */ |
| 3797 void *pResult; /* Result of xTask */ |
| 3798 }; |
| 3799 |
| 3800 /* Create a new thread */ |
| 3801 SQLITE_PRIVATE int sqlite3ThreadCreate( |
| 3802 SQLiteThread **ppThread, /* OUT: Write the thread object here */ |
| 3803 void *(*xTask)(void*), /* Routine to run in a separate thread */ |
| 3804 void *pIn /* Argument passed into xTask() */ |
| 3805 ){ |
| 3806 SQLiteThread *p; |
| 3807 |
| 3808 assert( ppThread!=0 ); |
| 3809 assert( xTask!=0 ); |
| 3810 *ppThread = 0; |
| 3811 p = sqlite3Malloc(sizeof(*p)); |
| 3812 if( p==0 ) return SQLITE_NOMEM; |
| 3813 if( (SQLITE_PTR_TO_INT(p)/17)&1 ){ |
| 3814 p->xTask = xTask; |
| 3815 p->pIn = pIn; |
| 3816 }else{ |
| 3817 p->xTask = 0; |
| 3818 p->pResult = xTask(pIn); |
| 3819 } |
| 3820 *ppThread = p; |
| 3821 return SQLITE_OK; |
| 3822 } |
| 3823 |
| 3824 /* Get the results of the thread */ |
| 3825 SQLITE_PRIVATE int sqlite3ThreadJoin(SQLiteThread *p, void **ppOut){ |
| 3826 |
| 3827 assert( ppOut!=0 ); |
| 3828 if( NEVER(p==0) ) return SQLITE_NOMEM; |
| 3829 if( p->xTask ){ |
| 3830 *ppOut = p->xTask(p->pIn); |
| 3831 }else{ |
| 3832 *ppOut = p->pResult; |
| 3833 } |
| 3834 sqlite3_free(p); |
| 3835 |
| 3836 #if defined(SQLITE_TEST) |
| 3837 { |
| 3838 void *pTstAlloc = sqlite3Malloc(10); |
| 3839 if (!pTstAlloc) return SQLITE_NOMEM; |
| 3840 sqlite3_free(pTstAlloc); |
| 3841 } |
| 3842 #endif |
| 3843 |
| 3844 return SQLITE_OK; |
| 3845 } |
| 3846 |
| 3847 #endif /* !defined(SQLITE_THREADS_IMPLEMENTED) */ |
| 3848 /****************************** End Single-Threaded *************************/ |
| 3849 #endif /* SQLITE_MAX_WORKER_THREADS>0 */ |
| 3850 |
| 3851 /************** End of threads.c *********************************************/ |
| 3852 /************** Begin file utf.c *********************************************/ |
| 3853 /* |
| 3854 ** 2004 April 13 |
| 3855 ** |
| 3856 ** The author disclaims copyright to this source code. In place of |
| 3857 ** a legal notice, here is a blessing: |
| 3858 ** |
| 3859 ** May you do good and not evil. |
| 3860 ** May you find forgiveness for yourself and forgive others. |
| 3861 ** May you share freely, never taking more than you give. |
| 3862 ** |
| 3863 ************************************************************************* |
| 3864 ** This file contains routines used to translate between UTF-8, |
| 3865 ** UTF-16, UTF-16BE, and UTF-16LE. |
| 3866 ** |
| 3867 ** Notes on UTF-8: |
| 3868 ** |
| 3869 ** Byte-0 Byte-1 Byte-2 Byte-3 Value |
| 3870 ** 0xxxxxxx 00000000 00000000 0xxxxxxx |
| 3871 ** 110yyyyy 10xxxxxx 00000000 00000yyy yyxxxxxx |
| 3872 ** 1110zzzz 10yyyyyy 10xxxxxx 00000000 zzzzyyyy yyxxxxxx |
| 3873 ** 11110uuu 10uuzzzz 10yyyyyy 10xxxxxx 000uuuuu zzzzyyyy yyxxxxxx |
| 3874 ** |
| 3875 ** |
| 3876 ** Notes on UTF-16: (with wwww+1==uuuuu) |
| 3877 ** |
| 3878 ** Word-0 Word-1 Value |
| 3879 ** 110110ww wwzzzzyy 110111yy yyxxxxxx 000uuuuu zzzzyyyy yyxxxxxx |
| 3880 ** zzzzyyyy yyxxxxxx 00000000 zzzzyyyy yyxxxxxx |
| 3881 ** |
| 3882 ** |
| 3883 ** BOM or Byte Order Mark: |
| 3884 ** 0xff 0xfe little-endian utf-16 follows |
| 3885 ** 0xfe 0xff big-endian utf-16 follows |
| 3886 ** |
| 3887 */ |
| 3888 /* #include "sqliteInt.h" */ |
| 3889 /* #include <assert.h> */ |
| 3890 /* #include "vdbeInt.h" */ |
| 3891 |
| 3892 #if !defined(SQLITE_AMALGAMATION) && SQLITE_BYTEORDER==0 |
| 3893 /* |
| 3894 ** The following constant value is used by the SQLITE_BIGENDIAN and |
| 3895 ** SQLITE_LITTLEENDIAN macros. |
| 3896 */ |
| 3897 SQLITE_PRIVATE const int sqlite3one = 1; |
| 3898 #endif /* SQLITE_AMALGAMATION && SQLITE_BYTEORDER==0 */ |
| 3899 |
| 3900 /* |
| 3901 ** This lookup table is used to help decode the first byte of |
| 3902 ** a multi-byte UTF8 character. |
| 3903 */ |
| 3904 static const unsigned char sqlite3Utf8Trans1[] = { |
| 3905 0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, |
| 3906 0x08, 0x09, 0x0a, 0x0b, 0x0c, 0x0d, 0x0e, 0x0f, |
| 3907 0x10, 0x11, 0x12, 0x13, 0x14, 0x15, 0x16, 0x17, |
| 3908 0x18, 0x19, 0x1a, 0x1b, 0x1c, 0x1d, 0x1e, 0x1f, |
| 3909 0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, |
| 3910 0x08, 0x09, 0x0a, 0x0b, 0x0c, 0x0d, 0x0e, 0x0f, |
| 3911 0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, |
| 3912 0x00, 0x01, 0x02, 0x03, 0x00, 0x01, 0x00, 0x00, |
| 3913 }; |
| 3914 |
| 3915 |
| 3916 #define WRITE_UTF8(zOut, c) { \ |
| 3917 if( c<0x00080 ){ \ |
| 3918 *zOut++ = (u8)(c&0xFF); \ |
| 3919 } \ |
| 3920 else if( c<0x00800 ){ \ |
| 3921 *zOut++ = 0xC0 + (u8)((c>>6)&0x1F); \ |
| 3922 *zOut++ = 0x80 + (u8)(c & 0x3F); \ |
| 3923 } \ |
| 3924 else if( c<0x10000 ){ \ |
| 3925 *zOut++ = 0xE0 + (u8)((c>>12)&0x0F); \ |
| 3926 *zOut++ = 0x80 + (u8)((c>>6) & 0x3F); \ |
| 3927 *zOut++ = 0x80 + (u8)(c & 0x3F); \ |
| 3928 }else{ \ |
| 3929 *zOut++ = 0xF0 + (u8)((c>>18) & 0x07); \ |
| 3930 *zOut++ = 0x80 + (u8)((c>>12) & 0x3F); \ |
| 3931 *zOut++ = 0x80 + (u8)((c>>6) & 0x3F); \ |
| 3932 *zOut++ = 0x80 + (u8)(c & 0x3F); \ |
| 3933 } \ |
| 3934 } |
| 3935 |
| 3936 #define WRITE_UTF16LE(zOut, c) { \ |
| 3937 if( c<=0xFFFF ){ \ |
| 3938 *zOut++ = (u8)(c&0x00FF); \ |
| 3939 *zOut++ = (u8)((c>>8)&0x00FF); \ |
| 3940 }else{ \ |
| 3941 *zOut++ = (u8)(((c>>10)&0x003F) + (((c-0x10000)>>10)&0x00C0)); \ |
| 3942 *zOut++ = (u8)(0x00D8 + (((c-0x10000)>>18)&0x03)); \ |
| 3943 *zOut++ = (u8)(c&0x00FF); \ |
| 3944 *zOut++ = (u8)(0x00DC + ((c>>8)&0x03)); \ |
| 3945 } \ |
| 3946 } |
| 3947 |
| 3948 #define WRITE_UTF16BE(zOut, c) { \ |
| 3949 if( c<=0xFFFF ){ \ |
| 3950 *zOut++ = (u8)((c>>8)&0x00FF); \ |
| 3951 *zOut++ = (u8)(c&0x00FF); \ |
| 3952 }else{ \ |
| 3953 *zOut++ = (u8)(0x00D8 + (((c-0x10000)>>18)&0x03)); \ |
| 3954 *zOut++ = (u8)(((c>>10)&0x003F) + (((c-0x10000)>>10)&0x00C0)); \ |
| 3955 *zOut++ = (u8)(0x00DC + ((c>>8)&0x03)); \ |
| 3956 *zOut++ = (u8)(c&0x00FF); \ |
| 3957 } \ |
| 3958 } |
| 3959 |
| 3960 #define READ_UTF16LE(zIn, TERM, c){ \ |
| 3961 c = (*zIn++); \ |
| 3962 c += ((*zIn++)<<8); \ |
| 3963 if( c>=0xD800 && c<0xE000 && TERM ){ \ |
| 3964 int c2 = (*zIn++); \ |
| 3965 c2 += ((*zIn++)<<8); \ |
| 3966 c = (c2&0x03FF) + ((c&0x003F)<<10) + (((c&0x03C0)+0x0040)<<10); \ |
| 3967 } \ |
| 3968 } |
| 3969 |
| 3970 #define READ_UTF16BE(zIn, TERM, c){ \ |
| 3971 c = ((*zIn++)<<8); \ |
| 3972 c += (*zIn++); \ |
| 3973 if( c>=0xD800 && c<0xE000 && TERM ){ \ |
| 3974 int c2 = ((*zIn++)<<8); \ |
| 3975 c2 += (*zIn++); \ |
| 3976 c = (c2&0x03FF) + ((c&0x003F)<<10) + (((c&0x03C0)+0x0040)<<10); \ |
| 3977 } \ |
| 3978 } |
| 3979 |
| 3980 /* |
| 3981 ** Translate a single UTF-8 character. Return the unicode value. |
| 3982 ** |
| 3983 ** During translation, assume that the byte that zTerm points |
| 3984 ** is a 0x00. |
| 3985 ** |
| 3986 ** Write a pointer to the next unread byte back into *pzNext. |
| 3987 ** |
| 3988 ** Notes On Invalid UTF-8: |
| 3989 ** |
| 3990 ** * This routine never allows a 7-bit character (0x00 through 0x7f) to |
| 3991 ** be encoded as a multi-byte character. Any multi-byte character that |
| 3992 ** attempts to encode a value between 0x00 and 0x7f is rendered as 0xfffd. |
| 3993 ** |
| 3994 ** * This routine never allows a UTF16 surrogate value to be encoded. |
| 3995 ** If a multi-byte character attempts to encode a value between |
| 3996 ** 0xd800 and 0xe000 then it is rendered as 0xfffd. |
| 3997 ** |
| 3998 ** * Bytes in the range of 0x80 through 0xbf which occur as the first |
| 3999 ** byte of a character are interpreted as single-byte characters |
| 4000 ** and rendered as themselves even though they are technically |
| 4001 ** invalid characters. |
| 4002 ** |
| 4003 ** * This routine accepts over-length UTF8 encodings |
| 4004 ** for unicode values 0x80 and greater. It does not change over-length |
| 4005 ** encodings to 0xfffd as some systems recommend. |
| 4006 */ |
| 4007 #define READ_UTF8(zIn, zTerm, c) \ |
| 4008 c = *(zIn++); \ |
| 4009 if( c>=0xc0 ){ \ |
| 4010 c = sqlite3Utf8Trans1[c-0xc0]; \ |
| 4011 while( zIn!=zTerm && (*zIn & 0xc0)==0x80 ){ \ |
| 4012 c = (c<<6) + (0x3f & *(zIn++)); \ |
| 4013 } \ |
| 4014 if( c<0x80 \ |
| 4015 || (c&0xFFFFF800)==0xD800 \ |
| 4016 || (c&0xFFFFFFFE)==0xFFFE ){ c = 0xFFFD; } \ |
| 4017 } |
| 4018 SQLITE_PRIVATE u32 sqlite3Utf8Read( |
| 4019 const unsigned char **pz /* Pointer to string from which to read char */ |
| 4020 ){ |
| 4021 unsigned int c; |
| 4022 |
| 4023 /* Same as READ_UTF8() above but without the zTerm parameter. |
| 4024 ** For this routine, we assume the UTF8 string is always zero-terminated. |
| 4025 */ |
| 4026 c = *((*pz)++); |
| 4027 if( c>=0xc0 ){ |
| 4028 c = sqlite3Utf8Trans1[c-0xc0]; |
| 4029 while( (*(*pz) & 0xc0)==0x80 ){ |
| 4030 c = (c<<6) + (0x3f & *((*pz)++)); |
| 4031 } |
| 4032 if( c<0x80 |
| 4033 || (c&0xFFFFF800)==0xD800 |
| 4034 || (c&0xFFFFFFFE)==0xFFFE ){ c = 0xFFFD; } |
| 4035 } |
| 4036 return c; |
| 4037 } |
| 4038 |
| 4039 |
| 4040 |
| 4041 |
| 4042 /* |
| 4043 ** If the TRANSLATE_TRACE macro is defined, the value of each Mem is |
| 4044 ** printed on stderr on the way into and out of sqlite3VdbeMemTranslate(). |
| 4045 */ |
| 4046 /* #define TRANSLATE_TRACE 1 */ |
| 4047 |
| 4048 #ifndef SQLITE_OMIT_UTF16 |
| 4049 /* |
| 4050 ** This routine transforms the internal text encoding used by pMem to |
| 4051 ** desiredEnc. It is an error if the string is already of the desired |
| 4052 ** encoding, or if *pMem does not contain a string value. |
| 4053 */ |
| 4054 SQLITE_PRIVATE SQLITE_NOINLINE int sqlite3VdbeMemTranslate(Mem *pMem, u8 desired
Enc){ |
| 4055 int len; /* Maximum length of output string in bytes */ |
| 4056 unsigned char *zOut; /* Output buffer */ |
| 4057 unsigned char *zIn; /* Input iterator */ |
| 4058 unsigned char *zTerm; /* End of input */ |
| 4059 unsigned char *z; /* Output iterator */ |
| 4060 unsigned int c; |
| 4061 |
| 4062 assert( pMem->db==0 || sqlite3_mutex_held(pMem->db->mutex) ); |
| 4063 assert( pMem->flags&MEM_Str ); |
| 4064 assert( pMem->enc!=desiredEnc ); |
| 4065 assert( pMem->enc!=0 ); |
| 4066 assert( pMem->n>=0 ); |
| 4067 |
| 4068 #if defined(TRANSLATE_TRACE) && defined(SQLITE_DEBUG) |
| 4069 { |
| 4070 char zBuf[100]; |
| 4071 sqlite3VdbeMemPrettyPrint(pMem, zBuf); |
| 4072 fprintf(stderr, "INPUT: %s\n", zBuf); |
| 4073 } |
| 4074 #endif |
| 4075 |
| 4076 /* If the translation is between UTF-16 little and big endian, then |
| 4077 ** all that is required is to swap the byte order. This case is handled |
| 4078 ** differently from the others. |
| 4079 */ |
| 4080 if( pMem->enc!=SQLITE_UTF8 && desiredEnc!=SQLITE_UTF8 ){ |
| 4081 u8 temp; |
| 4082 int rc; |
| 4083 rc = sqlite3VdbeMemMakeWriteable(pMem); |
| 4084 if( rc!=SQLITE_OK ){ |
| 4085 assert( rc==SQLITE_NOMEM ); |
| 4086 return SQLITE_NOMEM; |
| 4087 } |
| 4088 zIn = (u8*)pMem->z; |
| 4089 zTerm = &zIn[pMem->n&~1]; |
| 4090 while( zIn<zTerm ){ |
| 4091 temp = *zIn; |
| 4092 *zIn = *(zIn+1); |
| 4093 zIn++; |
| 4094 *zIn++ = temp; |
| 4095 } |
| 4096 pMem->enc = desiredEnc; |
| 4097 goto translate_out; |
| 4098 } |
| 4099 |
| 4100 /* Set len to the maximum number of bytes required in the output buffer. */ |
| 4101 if( desiredEnc==SQLITE_UTF8 ){ |
| 4102 /* When converting from UTF-16, the maximum growth results from |
| 4103 ** translating a 2-byte character to a 4-byte UTF-8 character. |
| 4104 ** A single byte is required for the output string |
| 4105 ** nul-terminator. |
| 4106 */ |
| 4107 pMem->n &= ~1; |
| 4108 len = pMem->n * 2 + 1; |
| 4109 }else{ |
| 4110 /* When converting from UTF-8 to UTF-16 the maximum growth is caused |
| 4111 ** when a 1-byte UTF-8 character is translated into a 2-byte UTF-16 |
| 4112 ** character. Two bytes are required in the output buffer for the |
| 4113 ** nul-terminator. |
| 4114 */ |
| 4115 len = pMem->n * 2 + 2; |
| 4116 } |
| 4117 |
| 4118 /* Set zIn to point at the start of the input buffer and zTerm to point 1 |
| 4119 ** byte past the end. |
| 4120 ** |
| 4121 ** Variable zOut is set to point at the output buffer, space obtained |
| 4122 ** from sqlite3_malloc(). |
| 4123 */ |
| 4124 zIn = (u8*)pMem->z; |
| 4125 zTerm = &zIn[pMem->n]; |
| 4126 zOut = sqlite3DbMallocRaw(pMem->db, len); |
| 4127 if( !zOut ){ |
| 4128 return SQLITE_NOMEM; |
| 4129 } |
| 4130 z = zOut; |
| 4131 |
| 4132 if( pMem->enc==SQLITE_UTF8 ){ |
| 4133 if( desiredEnc==SQLITE_UTF16LE ){ |
| 4134 /* UTF-8 -> UTF-16 Little-endian */ |
| 4135 while( zIn<zTerm ){ |
| 4136 READ_UTF8(zIn, zTerm, c); |
| 4137 WRITE_UTF16LE(z, c); |
| 4138 } |
| 4139 }else{ |
| 4140 assert( desiredEnc==SQLITE_UTF16BE ); |
| 4141 /* UTF-8 -> UTF-16 Big-endian */ |
| 4142 while( zIn<zTerm ){ |
| 4143 READ_UTF8(zIn, zTerm, c); |
| 4144 WRITE_UTF16BE(z, c); |
| 4145 } |
| 4146 } |
| 4147 pMem->n = (int)(z - zOut); |
| 4148 *z++ = 0; |
| 4149 }else{ |
| 4150 assert( desiredEnc==SQLITE_UTF8 ); |
| 4151 if( pMem->enc==SQLITE_UTF16LE ){ |
| 4152 /* UTF-16 Little-endian -> UTF-8 */ |
| 4153 while( zIn<zTerm ){ |
| 4154 READ_UTF16LE(zIn, zIn<zTerm, c); |
| 4155 WRITE_UTF8(z, c); |
| 4156 } |
| 4157 }else{ |
| 4158 /* UTF-16 Big-endian -> UTF-8 */ |
| 4159 while( zIn<zTerm ){ |
| 4160 READ_UTF16BE(zIn, zIn<zTerm, c); |
| 4161 WRITE_UTF8(z, c); |
| 4162 } |
| 4163 } |
| 4164 pMem->n = (int)(z - zOut); |
| 4165 } |
| 4166 *z = 0; |
| 4167 assert( (pMem->n+(desiredEnc==SQLITE_UTF8?1:2))<=len ); |
| 4168 |
| 4169 c = pMem->flags; |
| 4170 sqlite3VdbeMemRelease(pMem); |
| 4171 pMem->flags = MEM_Str|MEM_Term|(c&MEM_AffMask); |
| 4172 pMem->enc = desiredEnc; |
| 4173 pMem->z = (char*)zOut; |
| 4174 pMem->zMalloc = pMem->z; |
| 4175 pMem->szMalloc = sqlite3DbMallocSize(pMem->db, pMem->z); |
| 4176 |
| 4177 translate_out: |
| 4178 #if defined(TRANSLATE_TRACE) && defined(SQLITE_DEBUG) |
| 4179 { |
| 4180 char zBuf[100]; |
| 4181 sqlite3VdbeMemPrettyPrint(pMem, zBuf); |
| 4182 fprintf(stderr, "OUTPUT: %s\n", zBuf); |
| 4183 } |
| 4184 #endif |
| 4185 return SQLITE_OK; |
| 4186 } |
| 4187 |
| 4188 /* |
| 4189 ** This routine checks for a byte-order mark at the beginning of the |
| 4190 ** UTF-16 string stored in *pMem. If one is present, it is removed and |
| 4191 ** the encoding of the Mem adjusted. This routine does not do any |
| 4192 ** byte-swapping, it just sets Mem.enc appropriately. |
| 4193 ** |
| 4194 ** The allocation (static, dynamic etc.) and encoding of the Mem may be |
| 4195 ** changed by this function. |
| 4196 */ |
| 4197 SQLITE_PRIVATE int sqlite3VdbeMemHandleBom(Mem *pMem){ |
| 4198 int rc = SQLITE_OK; |
| 4199 u8 bom = 0; |
| 4200 |
| 4201 assert( pMem->n>=0 ); |
| 4202 if( pMem->n>1 ){ |
| 4203 u8 b1 = *(u8 *)pMem->z; |
| 4204 u8 b2 = *(((u8 *)pMem->z) + 1); |
| 4205 if( b1==0xFE && b2==0xFF ){ |
| 4206 bom = SQLITE_UTF16BE; |
| 4207 } |
| 4208 if( b1==0xFF && b2==0xFE ){ |
| 4209 bom = SQLITE_UTF16LE; |
| 4210 } |
| 4211 } |
| 4212 |
| 4213 if( bom ){ |
| 4214 rc = sqlite3VdbeMemMakeWriteable(pMem); |
| 4215 if( rc==SQLITE_OK ){ |
| 4216 pMem->n -= 2; |
| 4217 memmove(pMem->z, &pMem->z[2], pMem->n); |
| 4218 pMem->z[pMem->n] = '\0'; |
| 4219 pMem->z[pMem->n+1] = '\0'; |
| 4220 pMem->flags |= MEM_Term; |
| 4221 pMem->enc = bom; |
| 4222 } |
| 4223 } |
| 4224 return rc; |
| 4225 } |
| 4226 #endif /* SQLITE_OMIT_UTF16 */ |
| 4227 |
| 4228 /* |
| 4229 ** pZ is a UTF-8 encoded unicode string. If nByte is less than zero, |
| 4230 ** return the number of unicode characters in pZ up to (but not including) |
| 4231 ** the first 0x00 byte. If nByte is not less than zero, return the |
| 4232 ** number of unicode characters in the first nByte of pZ (or up to |
| 4233 ** the first 0x00, whichever comes first). |
| 4234 */ |
| 4235 SQLITE_PRIVATE int sqlite3Utf8CharLen(const char *zIn, int nByte){ |
| 4236 int r = 0; |
| 4237 const u8 *z = (const u8*)zIn; |
| 4238 const u8 *zTerm; |
| 4239 if( nByte>=0 ){ |
| 4240 zTerm = &z[nByte]; |
| 4241 }else{ |
| 4242 zTerm = (const u8*)(-1); |
| 4243 } |
| 4244 assert( z<=zTerm ); |
| 4245 while( *z!=0 && z<zTerm ){ |
| 4246 SQLITE_SKIP_UTF8(z); |
| 4247 r++; |
| 4248 } |
| 4249 return r; |
| 4250 } |
| 4251 |
| 4252 /* This test function is not currently used by the automated test-suite. |
| 4253 ** Hence it is only available in debug builds. |
| 4254 */ |
| 4255 #if defined(SQLITE_TEST) && defined(SQLITE_DEBUG) |
| 4256 /* |
| 4257 ** Translate UTF-8 to UTF-8. |
| 4258 ** |
| 4259 ** This has the effect of making sure that the string is well-formed |
| 4260 ** UTF-8. Miscoded characters are removed. |
| 4261 ** |
| 4262 ** The translation is done in-place and aborted if the output |
| 4263 ** overruns the input. |
| 4264 */ |
| 4265 SQLITE_PRIVATE int sqlite3Utf8To8(unsigned char *zIn){ |
| 4266 unsigned char *zOut = zIn; |
| 4267 unsigned char *zStart = zIn; |
| 4268 u32 c; |
| 4269 |
| 4270 while( zIn[0] && zOut<=zIn ){ |
| 4271 c = sqlite3Utf8Read((const u8**)&zIn); |
| 4272 if( c!=0xfffd ){ |
| 4273 WRITE_UTF8(zOut, c); |
| 4274 } |
| 4275 } |
| 4276 *zOut = 0; |
| 4277 return (int)(zOut - zStart); |
| 4278 } |
| 4279 #endif |
| 4280 |
| 4281 #ifndef SQLITE_OMIT_UTF16 |
| 4282 /* |
| 4283 ** Convert a UTF-16 string in the native encoding into a UTF-8 string. |
| 4284 ** Memory to hold the UTF-8 string is obtained from sqlite3_malloc and must |
| 4285 ** be freed by the calling function. |
| 4286 ** |
| 4287 ** NULL is returned if there is an allocation error. |
| 4288 */ |
| 4289 SQLITE_PRIVATE char *sqlite3Utf16to8(sqlite3 *db, const void *z, int nByte, u8 e
nc){ |
| 4290 Mem m; |
| 4291 memset(&m, 0, sizeof(m)); |
| 4292 m.db = db; |
| 4293 sqlite3VdbeMemSetStr(&m, z, nByte, enc, SQLITE_STATIC); |
| 4294 sqlite3VdbeChangeEncoding(&m, SQLITE_UTF8); |
| 4295 if( db->mallocFailed ){ |
| 4296 sqlite3VdbeMemRelease(&m); |
| 4297 m.z = 0; |
| 4298 } |
| 4299 assert( (m.flags & MEM_Term)!=0 || db->mallocFailed ); |
| 4300 assert( (m.flags & MEM_Str)!=0 || db->mallocFailed ); |
| 4301 assert( m.z || db->mallocFailed ); |
| 4302 return m.z; |
| 4303 } |
| 4304 |
| 4305 /* |
| 4306 ** zIn is a UTF-16 encoded unicode string at least nChar characters long. |
| 4307 ** Return the number of bytes in the first nChar unicode characters |
| 4308 ** in pZ. nChar must be non-negative. |
| 4309 */ |
| 4310 SQLITE_PRIVATE int sqlite3Utf16ByteLen(const void *zIn, int nChar){ |
| 4311 int c; |
| 4312 unsigned char const *z = zIn; |
| 4313 int n = 0; |
| 4314 |
| 4315 if( SQLITE_UTF16NATIVE==SQLITE_UTF16BE ){ |
| 4316 while( n<nChar ){ |
| 4317 READ_UTF16BE(z, 1, c); |
| 4318 n++; |
| 4319 } |
| 4320 }else{ |
| 4321 while( n<nChar ){ |
| 4322 READ_UTF16LE(z, 1, c); |
| 4323 n++; |
| 4324 } |
| 4325 } |
| 4326 return (int)(z-(unsigned char const *)zIn); |
| 4327 } |
| 4328 |
| 4329 #if defined(SQLITE_TEST) |
| 4330 /* |
| 4331 ** This routine is called from the TCL test function "translate_selftest". |
| 4332 ** It checks that the primitives for serializing and deserializing |
| 4333 ** characters in each encoding are inverses of each other. |
| 4334 */ |
| 4335 SQLITE_PRIVATE void sqlite3UtfSelfTest(void){ |
| 4336 unsigned int i, t; |
| 4337 unsigned char zBuf[20]; |
| 4338 unsigned char *z; |
| 4339 int n; |
| 4340 unsigned int c; |
| 4341 |
| 4342 for(i=0; i<0x00110000; i++){ |
| 4343 z = zBuf; |
| 4344 WRITE_UTF8(z, i); |
| 4345 n = (int)(z-zBuf); |
| 4346 assert( n>0 && n<=4 ); |
| 4347 z[0] = 0; |
| 4348 z = zBuf; |
| 4349 c = sqlite3Utf8Read((const u8**)&z); |
| 4350 t = i; |
| 4351 if( i>=0xD800 && i<=0xDFFF ) t = 0xFFFD; |
| 4352 if( (i&0xFFFFFFFE)==0xFFFE ) t = 0xFFFD; |
| 4353 assert( c==t ); |
| 4354 assert( (z-zBuf)==n ); |
| 4355 } |
| 4356 for(i=0; i<0x00110000; i++){ |
| 4357 if( i>=0xD800 && i<0xE000 ) continue; |
| 4358 z = zBuf; |
| 4359 WRITE_UTF16LE(z, i); |
| 4360 n = (int)(z-zBuf); |
| 4361 assert( n>0 && n<=4 ); |
| 4362 z[0] = 0; |
| 4363 z = zBuf; |
| 4364 READ_UTF16LE(z, 1, c); |
| 4365 assert( c==i ); |
| 4366 assert( (z-zBuf)==n ); |
| 4367 } |
| 4368 for(i=0; i<0x00110000; i++){ |
| 4369 if( i>=0xD800 && i<0xE000 ) continue; |
| 4370 z = zBuf; |
| 4371 WRITE_UTF16BE(z, i); |
| 4372 n = (int)(z-zBuf); |
| 4373 assert( n>0 && n<=4 ); |
| 4374 z[0] = 0; |
| 4375 z = zBuf; |
| 4376 READ_UTF16BE(z, 1, c); |
| 4377 assert( c==i ); |
| 4378 assert( (z-zBuf)==n ); |
| 4379 } |
| 4380 } |
| 4381 #endif /* SQLITE_TEST */ |
| 4382 #endif /* SQLITE_OMIT_UTF16 */ |
| 4383 |
| 4384 /************** End of utf.c *************************************************/ |
| 4385 /************** Begin file util.c ********************************************/ |
| 4386 /* |
| 4387 ** 2001 September 15 |
| 4388 ** |
| 4389 ** The author disclaims copyright to this source code. In place of |
| 4390 ** a legal notice, here is a blessing: |
| 4391 ** |
| 4392 ** May you do good and not evil. |
| 4393 ** May you find forgiveness for yourself and forgive others. |
| 4394 ** May you share freely, never taking more than you give. |
| 4395 ** |
| 4396 ************************************************************************* |
| 4397 ** Utility functions used throughout sqlite. |
| 4398 ** |
| 4399 ** This file contains functions for allocating memory, comparing |
| 4400 ** strings, and stuff like that. |
| 4401 ** |
| 4402 */ |
| 4403 /* #include "sqliteInt.h" */ |
| 4404 /* #include <stdarg.h> */ |
| 4405 #if HAVE_ISNAN || SQLITE_HAVE_ISNAN |
| 4406 # include <math.h> |
| 4407 #endif |
| 4408 |
| 4409 /* |
| 4410 ** Routine needed to support the testcase() macro. |
| 4411 */ |
| 4412 #ifdef SQLITE_COVERAGE_TEST |
| 4413 SQLITE_PRIVATE void sqlite3Coverage(int x){ |
| 4414 static unsigned dummy = 0; |
| 4415 dummy += (unsigned)x; |
| 4416 } |
| 4417 #endif |
| 4418 |
| 4419 /* |
| 4420 ** Give a callback to the test harness that can be used to simulate faults |
| 4421 ** in places where it is difficult or expensive to do so purely by means |
| 4422 ** of inputs. |
| 4423 ** |
| 4424 ** The intent of the integer argument is to let the fault simulator know |
| 4425 ** which of multiple sqlite3FaultSim() calls has been hit. |
| 4426 ** |
| 4427 ** Return whatever integer value the test callback returns, or return |
| 4428 ** SQLITE_OK if no test callback is installed. |
| 4429 */ |
| 4430 #ifndef SQLITE_OMIT_BUILTIN_TEST |
| 4431 SQLITE_PRIVATE int sqlite3FaultSim(int iTest){ |
| 4432 int (*xCallback)(int) = sqlite3GlobalConfig.xTestCallback; |
| 4433 return xCallback ? xCallback(iTest) : SQLITE_OK; |
| 4434 } |
| 4435 #endif |
| 4436 |
| 4437 #ifndef SQLITE_OMIT_FLOATING_POINT |
| 4438 /* |
| 4439 ** Return true if the floating point value is Not a Number (NaN). |
| 4440 ** |
| 4441 ** Use the math library isnan() function if compiled with SQLITE_HAVE_ISNAN. |
| 4442 ** Otherwise, we have our own implementation that works on most systems. |
| 4443 */ |
| 4444 SQLITE_PRIVATE int sqlite3IsNaN(double x){ |
| 4445 int rc; /* The value return */ |
| 4446 #if !SQLITE_HAVE_ISNAN && !HAVE_ISNAN |
| 4447 /* |
| 4448 ** Systems that support the isnan() library function should probably |
| 4449 ** make use of it by compiling with -DSQLITE_HAVE_ISNAN. But we have |
| 4450 ** found that many systems do not have a working isnan() function so |
| 4451 ** this implementation is provided as an alternative. |
| 4452 ** |
| 4453 ** This NaN test sometimes fails if compiled on GCC with -ffast-math. |
| 4454 ** On the other hand, the use of -ffast-math comes with the following |
| 4455 ** warning: |
| 4456 ** |
| 4457 ** This option [-ffast-math] should never be turned on by any |
| 4458 ** -O option since it can result in incorrect output for programs |
| 4459 ** which depend on an exact implementation of IEEE or ISO |
| 4460 ** rules/specifications for math functions. |
| 4461 ** |
| 4462 ** Under MSVC, this NaN test may fail if compiled with a floating- |
| 4463 ** point precision mode other than /fp:precise. From the MSDN |
| 4464 ** documentation: |
| 4465 ** |
| 4466 ** The compiler [with /fp:precise] will properly handle comparisons |
| 4467 ** involving NaN. For example, x != x evaluates to true if x is NaN |
| 4468 ** ... |
| 4469 */ |
| 4470 #ifdef __FAST_MATH__ |
| 4471 # error SQLite will not work correctly with the -ffast-math option of GCC. |
| 4472 #endif |
| 4473 volatile double y = x; |
| 4474 volatile double z = y; |
| 4475 rc = (y!=z); |
| 4476 #else /* if HAVE_ISNAN */ |
| 4477 rc = isnan(x); |
| 4478 #endif /* HAVE_ISNAN */ |
| 4479 testcase( rc ); |
| 4480 return rc; |
| 4481 } |
| 4482 #endif /* SQLITE_OMIT_FLOATING_POINT */ |
| 4483 |
| 4484 /* |
| 4485 ** Compute a string length that is limited to what can be stored in |
| 4486 ** lower 30 bits of a 32-bit signed integer. |
| 4487 ** |
| 4488 ** The value returned will never be negative. Nor will it ever be greater |
| 4489 ** than the actual length of the string. For very long strings (greater |
| 4490 ** than 1GiB) the value returned might be less than the true string length. |
| 4491 */ |
| 4492 SQLITE_PRIVATE int sqlite3Strlen30(const char *z){ |
| 4493 if( z==0 ) return 0; |
| 4494 return 0x3fffffff & (int)strlen(z); |
| 4495 } |
| 4496 |
| 4497 /* |
| 4498 ** Set the current error code to err_code and clear any prior error message. |
| 4499 */ |
| 4500 SQLITE_PRIVATE void sqlite3Error(sqlite3 *db, int err_code){ |
| 4501 assert( db!=0 ); |
| 4502 db->errCode = err_code; |
| 4503 if( db->pErr ) sqlite3ValueSetNull(db->pErr); |
| 4504 } |
| 4505 |
| 4506 /* |
| 4507 ** Set the most recent error code and error string for the sqlite |
| 4508 ** handle "db". The error code is set to "err_code". |
| 4509 ** |
| 4510 ** If it is not NULL, string zFormat specifies the format of the |
| 4511 ** error string in the style of the printf functions: The following |
| 4512 ** format characters are allowed: |
| 4513 ** |
| 4514 ** %s Insert a string |
| 4515 ** %z A string that should be freed after use |
| 4516 ** %d Insert an integer |
| 4517 ** %T Insert a token |
| 4518 ** %S Insert the first element of a SrcList |
| 4519 ** |
| 4520 ** zFormat and any string tokens that follow it are assumed to be |
| 4521 ** encoded in UTF-8. |
| 4522 ** |
| 4523 ** To clear the most recent error for sqlite handle "db", sqlite3Error |
| 4524 ** should be called with err_code set to SQLITE_OK and zFormat set |
| 4525 ** to NULL. |
| 4526 */ |
| 4527 SQLITE_PRIVATE void sqlite3ErrorWithMsg(sqlite3 *db, int err_code, const char *z
Format, ...){ |
| 4528 assert( db!=0 ); |
| 4529 db->errCode = err_code; |
| 4530 if( zFormat==0 ){ |
| 4531 sqlite3Error(db, err_code); |
| 4532 }else if( db->pErr || (db->pErr = sqlite3ValueNew(db))!=0 ){ |
| 4533 char *z; |
| 4534 va_list ap; |
| 4535 va_start(ap, zFormat); |
| 4536 z = sqlite3VMPrintf(db, zFormat, ap); |
| 4537 va_end(ap); |
| 4538 sqlite3ValueSetStr(db->pErr, -1, z, SQLITE_UTF8, SQLITE_DYNAMIC); |
| 4539 } |
| 4540 } |
| 4541 |
| 4542 /* |
| 4543 ** Add an error message to pParse->zErrMsg and increment pParse->nErr. |
| 4544 ** The following formatting characters are allowed: |
| 4545 ** |
| 4546 ** %s Insert a string |
| 4547 ** %z A string that should be freed after use |
| 4548 ** %d Insert an integer |
| 4549 ** %T Insert a token |
| 4550 ** %S Insert the first element of a SrcList |
| 4551 ** |
| 4552 ** This function should be used to report any error that occurs while |
| 4553 ** compiling an SQL statement (i.e. within sqlite3_prepare()). The |
| 4554 ** last thing the sqlite3_prepare() function does is copy the error |
| 4555 ** stored by this function into the database handle using sqlite3Error(). |
| 4556 ** Functions sqlite3Error() or sqlite3ErrorWithMsg() should be used |
| 4557 ** during statement execution (sqlite3_step() etc.). |
| 4558 */ |
| 4559 SQLITE_PRIVATE void sqlite3ErrorMsg(Parse *pParse, const char *zFormat, ...){ |
| 4560 char *zMsg; |
| 4561 va_list ap; |
| 4562 sqlite3 *db = pParse->db; |
| 4563 va_start(ap, zFormat); |
| 4564 zMsg = sqlite3VMPrintf(db, zFormat, ap); |
| 4565 va_end(ap); |
| 4566 if( db->suppressErr ){ |
| 4567 sqlite3DbFree(db, zMsg); |
| 4568 }else{ |
| 4569 pParse->nErr++; |
| 4570 sqlite3DbFree(db, pParse->zErrMsg); |
| 4571 pParse->zErrMsg = zMsg; |
| 4572 pParse->rc = SQLITE_ERROR; |
| 4573 } |
| 4574 } |
| 4575 |
| 4576 /* |
| 4577 ** Convert an SQL-style quoted string into a normal string by removing |
| 4578 ** the quote characters. The conversion is done in-place. If the |
| 4579 ** input does not begin with a quote character, then this routine |
| 4580 ** is a no-op. |
| 4581 ** |
| 4582 ** The input string must be zero-terminated. A new zero-terminator |
| 4583 ** is added to the dequoted string. |
| 4584 ** |
| 4585 ** The return value is -1 if no dequoting occurs or the length of the |
| 4586 ** dequoted string, exclusive of the zero terminator, if dequoting does |
| 4587 ** occur. |
| 4588 ** |
| 4589 ** 2002-Feb-14: This routine is extended to remove MS-Access style |
| 4590 ** brackets from around identifiers. For example: "[a-b-c]" becomes |
| 4591 ** "a-b-c". |
| 4592 */ |
| 4593 SQLITE_PRIVATE int sqlite3Dequote(char *z){ |
| 4594 char quote; |
| 4595 int i, j; |
| 4596 if( z==0 ) return -1; |
| 4597 quote = z[0]; |
| 4598 switch( quote ){ |
| 4599 case '\'': break; |
| 4600 case '"': break; |
| 4601 case '`': break; /* For MySQL compatibility */ |
| 4602 case '[': quote = ']'; break; /* For MS SqlServer compatibility */ |
| 4603 default: return -1; |
| 4604 } |
| 4605 for(i=1, j=0;; i++){ |
| 4606 assert( z[i] ); |
| 4607 if( z[i]==quote ){ |
| 4608 if( z[i+1]==quote ){ |
| 4609 z[j++] = quote; |
| 4610 i++; |
| 4611 }else{ |
| 4612 break; |
| 4613 } |
| 4614 }else{ |
| 4615 z[j++] = z[i]; |
| 4616 } |
| 4617 } |
| 4618 z[j] = 0; |
| 4619 return j; |
| 4620 } |
| 4621 |
| 4622 /* Convenient short-hand */ |
| 4623 #define UpperToLower sqlite3UpperToLower |
| 4624 |
| 4625 /* |
| 4626 ** Some systems have stricmp(). Others have strcasecmp(). Because |
| 4627 ** there is no consistency, we will define our own. |
| 4628 ** |
| 4629 ** IMPLEMENTATION-OF: R-30243-02494 The sqlite3_stricmp() and |
| 4630 ** sqlite3_strnicmp() APIs allow applications and extensions to compare |
| 4631 ** the contents of two buffers containing UTF-8 strings in a |
| 4632 ** case-independent fashion, using the same definition of "case |
| 4633 ** independence" that SQLite uses internally when comparing identifiers. |
| 4634 */ |
| 4635 SQLITE_API int SQLITE_STDCALL sqlite3_stricmp(const char *zLeft, const char *zRi
ght){ |
| 4636 register unsigned char *a, *b; |
| 4637 if( zLeft==0 ){ |
| 4638 return zRight ? -1 : 0; |
| 4639 }else if( zRight==0 ){ |
| 4640 return 1; |
| 4641 } |
| 4642 a = (unsigned char *)zLeft; |
| 4643 b = (unsigned char *)zRight; |
| 4644 while( *a!=0 && UpperToLower[*a]==UpperToLower[*b]){ a++; b++; } |
| 4645 return UpperToLower[*a] - UpperToLower[*b]; |
| 4646 } |
| 4647 SQLITE_API int SQLITE_STDCALL sqlite3_strnicmp(const char *zLeft, const char *zR
ight, int N){ |
| 4648 register unsigned char *a, *b; |
| 4649 if( zLeft==0 ){ |
| 4650 return zRight ? -1 : 0; |
| 4651 }else if( zRight==0 ){ |
| 4652 return 1; |
| 4653 } |
| 4654 a = (unsigned char *)zLeft; |
| 4655 b = (unsigned char *)zRight; |
| 4656 while( N-- > 0 && *a!=0 && UpperToLower[*a]==UpperToLower[*b]){ a++; b++; } |
| 4657 return N<0 ? 0 : UpperToLower[*a] - UpperToLower[*b]; |
| 4658 } |
| 4659 |
| 4660 /* |
| 4661 ** The string z[] is an text representation of a real number. |
| 4662 ** Convert this string to a double and write it into *pResult. |
| 4663 ** |
| 4664 ** The string z[] is length bytes in length (bytes, not characters) and |
| 4665 ** uses the encoding enc. The string is not necessarily zero-terminated. |
| 4666 ** |
| 4667 ** Return TRUE if the result is a valid real number (or integer) and FALSE |
| 4668 ** if the string is empty or contains extraneous text. Valid numbers |
| 4669 ** are in one of these formats: |
| 4670 ** |
| 4671 ** [+-]digits[E[+-]digits] |
| 4672 ** [+-]digits.[digits][E[+-]digits] |
| 4673 ** [+-].digits[E[+-]digits] |
| 4674 ** |
| 4675 ** Leading and trailing whitespace is ignored for the purpose of determining |
| 4676 ** validity. |
| 4677 ** |
| 4678 ** If some prefix of the input string is a valid number, this routine |
| 4679 ** returns FALSE but it still converts the prefix and writes the result |
| 4680 ** into *pResult. |
| 4681 */ |
| 4682 SQLITE_PRIVATE int sqlite3AtoF(const char *z, double *pResult, int length, u8 en
c){ |
| 4683 #ifndef SQLITE_OMIT_FLOATING_POINT |
| 4684 int incr; |
| 4685 const char *zEnd = z + length; |
| 4686 /* sign * significand * (10 ^ (esign * exponent)) */ |
| 4687 int sign = 1; /* sign of significand */ |
| 4688 i64 s = 0; /* significand */ |
| 4689 int d = 0; /* adjust exponent for shifting decimal point */ |
| 4690 int esign = 1; /* sign of exponent */ |
| 4691 int e = 0; /* exponent */ |
| 4692 int eValid = 1; /* True exponent is either not used or is well-formed */ |
| 4693 double result; |
| 4694 int nDigits = 0; |
| 4695 int nonNum = 0; |
| 4696 |
| 4697 assert( enc==SQLITE_UTF8 || enc==SQLITE_UTF16LE || enc==SQLITE_UTF16BE ); |
| 4698 *pResult = 0.0; /* Default return value, in case of an error */ |
| 4699 |
| 4700 if( enc==SQLITE_UTF8 ){ |
| 4701 incr = 1; |
| 4702 }else{ |
| 4703 int i; |
| 4704 incr = 2; |
| 4705 assert( SQLITE_UTF16LE==2 && SQLITE_UTF16BE==3 ); |
| 4706 for(i=3-enc; i<length && z[i]==0; i+=2){} |
| 4707 nonNum = i<length; |
| 4708 zEnd = z+i+enc-3; |
| 4709 z += (enc&1); |
| 4710 } |
| 4711 |
| 4712 /* skip leading spaces */ |
| 4713 while( z<zEnd && sqlite3Isspace(*z) ) z+=incr; |
| 4714 if( z>=zEnd ) return 0; |
| 4715 |
| 4716 /* get sign of significand */ |
| 4717 if( *z=='-' ){ |
| 4718 sign = -1; |
| 4719 z+=incr; |
| 4720 }else if( *z=='+' ){ |
| 4721 z+=incr; |
| 4722 } |
| 4723 |
| 4724 /* skip leading zeroes */ |
| 4725 while( z<zEnd && z[0]=='0' ) z+=incr, nDigits++; |
| 4726 |
| 4727 /* copy max significant digits to significand */ |
| 4728 while( z<zEnd && sqlite3Isdigit(*z) && s<((LARGEST_INT64-9)/10) ){ |
| 4729 s = s*10 + (*z - '0'); |
| 4730 z+=incr, nDigits++; |
| 4731 } |
| 4732 |
| 4733 /* skip non-significant significand digits |
| 4734 ** (increase exponent by d to shift decimal left) */ |
| 4735 while( z<zEnd && sqlite3Isdigit(*z) ) z+=incr, nDigits++, d++; |
| 4736 if( z>=zEnd ) goto do_atof_calc; |
| 4737 |
| 4738 /* if decimal point is present */ |
| 4739 if( *z=='.' ){ |
| 4740 z+=incr; |
| 4741 /* copy digits from after decimal to significand |
| 4742 ** (decrease exponent by d to shift decimal right) */ |
| 4743 while( z<zEnd && sqlite3Isdigit(*z) && s<((LARGEST_INT64-9)/10) ){ |
| 4744 s = s*10 + (*z - '0'); |
| 4745 z+=incr, nDigits++, d--; |
| 4746 } |
| 4747 /* skip non-significant digits */ |
| 4748 while( z<zEnd && sqlite3Isdigit(*z) ) z+=incr, nDigits++; |
| 4749 } |
| 4750 if( z>=zEnd ) goto do_atof_calc; |
| 4751 |
| 4752 /* if exponent is present */ |
| 4753 if( *z=='e' || *z=='E' ){ |
| 4754 z+=incr; |
| 4755 eValid = 0; |
| 4756 if( z>=zEnd ) goto do_atof_calc; |
| 4757 /* get sign of exponent */ |
| 4758 if( *z=='-' ){ |
| 4759 esign = -1; |
| 4760 z+=incr; |
| 4761 }else if( *z=='+' ){ |
| 4762 z+=incr; |
| 4763 } |
| 4764 /* copy digits to exponent */ |
| 4765 while( z<zEnd && sqlite3Isdigit(*z) ){ |
| 4766 e = e<10000 ? (e*10 + (*z - '0')) : 10000; |
| 4767 z+=incr; |
| 4768 eValid = 1; |
| 4769 } |
| 4770 } |
| 4771 |
| 4772 /* skip trailing spaces */ |
| 4773 if( nDigits && eValid ){ |
| 4774 while( z<zEnd && sqlite3Isspace(*z) ) z+=incr; |
| 4775 } |
| 4776 |
| 4777 do_atof_calc: |
| 4778 /* adjust exponent by d, and update sign */ |
| 4779 e = (e*esign) + d; |
| 4780 if( e<0 ) { |
| 4781 esign = -1; |
| 4782 e *= -1; |
| 4783 } else { |
| 4784 esign = 1; |
| 4785 } |
| 4786 |
| 4787 /* if 0 significand */ |
| 4788 if( !s ) { |
| 4789 /* In the IEEE 754 standard, zero is signed. |
| 4790 ** Add the sign if we've seen at least one digit */ |
| 4791 result = (sign<0 && nDigits) ? -(double)0 : (double)0; |
| 4792 } else { |
| 4793 /* attempt to reduce exponent */ |
| 4794 if( esign>0 ){ |
| 4795 while( s<(LARGEST_INT64/10) && e>0 ) e--,s*=10; |
| 4796 }else{ |
| 4797 while( !(s%10) && e>0 ) e--,s/=10; |
| 4798 } |
| 4799 |
| 4800 /* adjust the sign of significand */ |
| 4801 s = sign<0 ? -s : s; |
| 4802 |
| 4803 /* if exponent, scale significand as appropriate |
| 4804 ** and store in result. */ |
| 4805 if( e ){ |
| 4806 LONGDOUBLE_TYPE scale = 1.0; |
| 4807 /* attempt to handle extremely small/large numbers better */ |
| 4808 if( e>307 && e<342 ){ |
| 4809 while( e%308 ) { scale *= 1.0e+1; e -= 1; } |
| 4810 if( esign<0 ){ |
| 4811 result = s / scale; |
| 4812 result /= 1.0e+308; |
| 4813 }else{ |
| 4814 result = s * scale; |
| 4815 result *= 1.0e+308; |
| 4816 } |
| 4817 }else if( e>=342 ){ |
| 4818 if( esign<0 ){ |
| 4819 result = 0.0*s; |
| 4820 }else{ |
| 4821 result = 1e308*1e308*s; /* Infinity */ |
| 4822 } |
| 4823 }else{ |
| 4824 /* 1.0e+22 is the largest power of 10 than can be |
| 4825 ** represented exactly. */ |
| 4826 while( e%22 ) { scale *= 1.0e+1; e -= 1; } |
| 4827 while( e>0 ) { scale *= 1.0e+22; e -= 22; } |
| 4828 if( esign<0 ){ |
| 4829 result = s / scale; |
| 4830 }else{ |
| 4831 result = s * scale; |
| 4832 } |
| 4833 } |
| 4834 } else { |
| 4835 result = (double)s; |
| 4836 } |
| 4837 } |
| 4838 |
| 4839 /* store the result */ |
| 4840 *pResult = result; |
| 4841 |
| 4842 /* return true if number and no extra non-whitespace chracters after */ |
| 4843 return z>=zEnd && nDigits>0 && eValid && nonNum==0; |
| 4844 #else |
| 4845 return !sqlite3Atoi64(z, pResult, length, enc); |
| 4846 #endif /* SQLITE_OMIT_FLOATING_POINT */ |
| 4847 } |
| 4848 |
| 4849 /* |
| 4850 ** Compare the 19-character string zNum against the text representation |
| 4851 ** value 2^63: 9223372036854775808. Return negative, zero, or positive |
| 4852 ** if zNum is less than, equal to, or greater than the string. |
| 4853 ** Note that zNum must contain exactly 19 characters. |
| 4854 ** |
| 4855 ** Unlike memcmp() this routine is guaranteed to return the difference |
| 4856 ** in the values of the last digit if the only difference is in the |
| 4857 ** last digit. So, for example, |
| 4858 ** |
| 4859 ** compare2pow63("9223372036854775800", 1) |
| 4860 ** |
| 4861 ** will return -8. |
| 4862 */ |
| 4863 static int compare2pow63(const char *zNum, int incr){ |
| 4864 int c = 0; |
| 4865 int i; |
| 4866 /* 012345678901234567 */ |
| 4867 const char *pow63 = "922337203685477580"; |
| 4868 for(i=0; c==0 && i<18; i++){ |
| 4869 c = (zNum[i*incr]-pow63[i])*10; |
| 4870 } |
| 4871 if( c==0 ){ |
| 4872 c = zNum[18*incr] - '8'; |
| 4873 testcase( c==(-1) ); |
| 4874 testcase( c==0 ); |
| 4875 testcase( c==(+1) ); |
| 4876 } |
| 4877 return c; |
| 4878 } |
| 4879 |
| 4880 /* |
| 4881 ** Convert zNum to a 64-bit signed integer. zNum must be decimal. This |
| 4882 ** routine does *not* accept hexadecimal notation. |
| 4883 ** |
| 4884 ** If the zNum value is representable as a 64-bit twos-complement |
| 4885 ** integer, then write that value into *pNum and return 0. |
| 4886 ** |
| 4887 ** If zNum is exactly 9223372036854775808, return 2. This special |
| 4888 ** case is broken out because while 9223372036854775808 cannot be a |
| 4889 ** signed 64-bit integer, its negative -9223372036854775808 can be. |
| 4890 ** |
| 4891 ** If zNum is too big for a 64-bit integer and is not |
| 4892 ** 9223372036854775808 or if zNum contains any non-numeric text, |
| 4893 ** then return 1. |
| 4894 ** |
| 4895 ** length is the number of bytes in the string (bytes, not characters). |
| 4896 ** The string is not necessarily zero-terminated. The encoding is |
| 4897 ** given by enc. |
| 4898 */ |
| 4899 SQLITE_PRIVATE int sqlite3Atoi64(const char *zNum, i64 *pNum, int length, u8 enc
){ |
| 4900 int incr; |
| 4901 u64 u = 0; |
| 4902 int neg = 0; /* assume positive */ |
| 4903 int i; |
| 4904 int c = 0; |
| 4905 int nonNum = 0; |
| 4906 const char *zStart; |
| 4907 const char *zEnd = zNum + length; |
| 4908 assert( enc==SQLITE_UTF8 || enc==SQLITE_UTF16LE || enc==SQLITE_UTF16BE ); |
| 4909 if( enc==SQLITE_UTF8 ){ |
| 4910 incr = 1; |
| 4911 }else{ |
| 4912 incr = 2; |
| 4913 assert( SQLITE_UTF16LE==2 && SQLITE_UTF16BE==3 ); |
| 4914 for(i=3-enc; i<length && zNum[i]==0; i+=2){} |
| 4915 nonNum = i<length; |
| 4916 zEnd = zNum+i+enc-3; |
| 4917 zNum += (enc&1); |
| 4918 } |
| 4919 while( zNum<zEnd && sqlite3Isspace(*zNum) ) zNum+=incr; |
| 4920 if( zNum<zEnd ){ |
| 4921 if( *zNum=='-' ){ |
| 4922 neg = 1; |
| 4923 zNum+=incr; |
| 4924 }else if( *zNum=='+' ){ |
| 4925 zNum+=incr; |
| 4926 } |
| 4927 } |
| 4928 zStart = zNum; |
| 4929 while( zNum<zEnd && zNum[0]=='0' ){ zNum+=incr; } /* Skip leading zeros. */ |
| 4930 for(i=0; &zNum[i]<zEnd && (c=zNum[i])>='0' && c<='9'; i+=incr){ |
| 4931 u = u*10 + c - '0'; |
| 4932 } |
| 4933 if( u>LARGEST_INT64 ){ |
| 4934 *pNum = neg ? SMALLEST_INT64 : LARGEST_INT64; |
| 4935 }else if( neg ){ |
| 4936 *pNum = -(i64)u; |
| 4937 }else{ |
| 4938 *pNum = (i64)u; |
| 4939 } |
| 4940 testcase( i==18 ); |
| 4941 testcase( i==19 ); |
| 4942 testcase( i==20 ); |
| 4943 if( (c!=0 && &zNum[i]<zEnd) || (i==0 && zStart==zNum) |
| 4944 || i>19*incr || nonNum ){ |
| 4945 /* zNum is empty or contains non-numeric text or is longer |
| 4946 ** than 19 digits (thus guaranteeing that it is too large) */ |
| 4947 return 1; |
| 4948 }else if( i<19*incr ){ |
| 4949 /* Less than 19 digits, so we know that it fits in 64 bits */ |
| 4950 assert( u<=LARGEST_INT64 ); |
| 4951 return 0; |
| 4952 }else{ |
| 4953 /* zNum is a 19-digit numbers. Compare it against 9223372036854775808. */ |
| 4954 c = compare2pow63(zNum, incr); |
| 4955 if( c<0 ){ |
| 4956 /* zNum is less than 9223372036854775808 so it fits */ |
| 4957 assert( u<=LARGEST_INT64 ); |
| 4958 return 0; |
| 4959 }else if( c>0 ){ |
| 4960 /* zNum is greater than 9223372036854775808 so it overflows */ |
| 4961 return 1; |
| 4962 }else{ |
| 4963 /* zNum is exactly 9223372036854775808. Fits if negative. The |
| 4964 ** special case 2 overflow if positive */ |
| 4965 assert( u-1==LARGEST_INT64 ); |
| 4966 return neg ? 0 : 2; |
| 4967 } |
| 4968 } |
| 4969 } |
| 4970 |
| 4971 /* |
| 4972 ** Transform a UTF-8 integer literal, in either decimal or hexadecimal, |
| 4973 ** into a 64-bit signed integer. This routine accepts hexadecimal literals, |
| 4974 ** whereas sqlite3Atoi64() does not. |
| 4975 ** |
| 4976 ** Returns: |
| 4977 ** |
| 4978 ** 0 Successful transformation. Fits in a 64-bit signed integer. |
| 4979 ** 1 Integer too large for a 64-bit signed integer or is malformed |
| 4980 ** 2 Special case of 9223372036854775808 |
| 4981 */ |
| 4982 SQLITE_PRIVATE int sqlite3DecOrHexToI64(const char *z, i64 *pOut){ |
| 4983 #ifndef SQLITE_OMIT_HEX_INTEGER |
| 4984 if( z[0]=='0' |
| 4985 && (z[1]=='x' || z[1]=='X') |
| 4986 && sqlite3Isxdigit(z[2]) |
| 4987 ){ |
| 4988 u64 u = 0; |
| 4989 int i, k; |
| 4990 for(i=2; z[i]=='0'; i++){} |
| 4991 for(k=i; sqlite3Isxdigit(z[k]); k++){ |
| 4992 u = u*16 + sqlite3HexToInt(z[k]); |
| 4993 } |
| 4994 memcpy(pOut, &u, 8); |
| 4995 return (z[k]==0 && k-i<=16) ? 0 : 1; |
| 4996 }else |
| 4997 #endif /* SQLITE_OMIT_HEX_INTEGER */ |
| 4998 { |
| 4999 return sqlite3Atoi64(z, pOut, sqlite3Strlen30(z), SQLITE_UTF8); |
| 5000 } |
| 5001 } |
| 5002 |
| 5003 /* |
| 5004 ** If zNum represents an integer that will fit in 32-bits, then set |
| 5005 ** *pValue to that integer and return true. Otherwise return false. |
| 5006 ** |
| 5007 ** This routine accepts both decimal and hexadecimal notation for integers. |
| 5008 ** |
| 5009 ** Any non-numeric characters that following zNum are ignored. |
| 5010 ** This is different from sqlite3Atoi64() which requires the |
| 5011 ** input number to be zero-terminated. |
| 5012 */ |
| 5013 SQLITE_PRIVATE int sqlite3GetInt32(const char *zNum, int *pValue){ |
| 5014 sqlite_int64 v = 0; |
| 5015 int i, c; |
| 5016 int neg = 0; |
| 5017 if( zNum[0]=='-' ){ |
| 5018 neg = 1; |
| 5019 zNum++; |
| 5020 }else if( zNum[0]=='+' ){ |
| 5021 zNum++; |
| 5022 } |
| 5023 #ifndef SQLITE_OMIT_HEX_INTEGER |
| 5024 else if( zNum[0]=='0' |
| 5025 && (zNum[1]=='x' || zNum[1]=='X') |
| 5026 && sqlite3Isxdigit(zNum[2]) |
| 5027 ){ |
| 5028 u32 u = 0; |
| 5029 zNum += 2; |
| 5030 while( zNum[0]=='0' ) zNum++; |
| 5031 for(i=0; sqlite3Isxdigit(zNum[i]) && i<8; i++){ |
| 5032 u = u*16 + sqlite3HexToInt(zNum[i]); |
| 5033 } |
| 5034 if( (u&0x80000000)==0 && sqlite3Isxdigit(zNum[i])==0 ){ |
| 5035 memcpy(pValue, &u, 4); |
| 5036 return 1; |
| 5037 }else{ |
| 5038 return 0; |
| 5039 } |
| 5040 } |
| 5041 #endif |
| 5042 while( zNum[0]=='0' ) zNum++; |
| 5043 for(i=0; i<11 && (c = zNum[i] - '0')>=0 && c<=9; i++){ |
| 5044 v = v*10 + c; |
| 5045 } |
| 5046 |
| 5047 /* The longest decimal representation of a 32 bit integer is 10 digits: |
| 5048 ** |
| 5049 ** 1234567890 |
| 5050 ** 2^31 -> 2147483648 |
| 5051 */ |
| 5052 testcase( i==10 ); |
| 5053 if( i>10 ){ |
| 5054 return 0; |
| 5055 } |
| 5056 testcase( v-neg==2147483647 ); |
| 5057 if( v-neg>2147483647 ){ |
| 5058 return 0; |
| 5059 } |
| 5060 if( neg ){ |
| 5061 v = -v; |
| 5062 } |
| 5063 *pValue = (int)v; |
| 5064 return 1; |
| 5065 } |
| 5066 |
| 5067 /* |
| 5068 ** Return a 32-bit integer value extracted from a string. If the |
| 5069 ** string is not an integer, just return 0. |
| 5070 */ |
| 5071 SQLITE_PRIVATE int sqlite3Atoi(const char *z){ |
| 5072 int x = 0; |
| 5073 if( z ) sqlite3GetInt32(z, &x); |
| 5074 return x; |
| 5075 } |
| 5076 |
| 5077 /* |
| 5078 ** The variable-length integer encoding is as follows: |
| 5079 ** |
| 5080 ** KEY: |
| 5081 ** A = 0xxxxxxx 7 bits of data and one flag bit |
| 5082 ** B = 1xxxxxxx 7 bits of data and one flag bit |
| 5083 ** C = xxxxxxxx 8 bits of data |
| 5084 ** |
| 5085 ** 7 bits - A |
| 5086 ** 14 bits - BA |
| 5087 ** 21 bits - BBA |
| 5088 ** 28 bits - BBBA |
| 5089 ** 35 bits - BBBBA |
| 5090 ** 42 bits - BBBBBA |
| 5091 ** 49 bits - BBBBBBA |
| 5092 ** 56 bits - BBBBBBBA |
| 5093 ** 64 bits - BBBBBBBBC |
| 5094 */ |
| 5095 |
| 5096 /* |
| 5097 ** Write a 64-bit variable-length integer to memory starting at p[0]. |
| 5098 ** The length of data write will be between 1 and 9 bytes. The number |
| 5099 ** of bytes written is returned. |
| 5100 ** |
| 5101 ** A variable-length integer consists of the lower 7 bits of each byte |
| 5102 ** for all bytes that have the 8th bit set and one byte with the 8th |
| 5103 ** bit clear. Except, if we get to the 9th byte, it stores the full |
| 5104 ** 8 bits and is the last byte. |
| 5105 */ |
| 5106 static int SQLITE_NOINLINE putVarint64(unsigned char *p, u64 v){ |
| 5107 int i, j, n; |
| 5108 u8 buf[10]; |
| 5109 if( v & (((u64)0xff000000)<<32) ){ |
| 5110 p[8] = (u8)v; |
| 5111 v >>= 8; |
| 5112 for(i=7; i>=0; i--){ |
| 5113 p[i] = (u8)((v & 0x7f) | 0x80); |
| 5114 v >>= 7; |
| 5115 } |
| 5116 return 9; |
| 5117 } |
| 5118 n = 0; |
| 5119 do{ |
| 5120 buf[n++] = (u8)((v & 0x7f) | 0x80); |
| 5121 v >>= 7; |
| 5122 }while( v!=0 ); |
| 5123 buf[0] &= 0x7f; |
| 5124 assert( n<=9 ); |
| 5125 for(i=0, j=n-1; j>=0; j--, i++){ |
| 5126 p[i] = buf[j]; |
| 5127 } |
| 5128 return n; |
| 5129 } |
| 5130 SQLITE_PRIVATE int sqlite3PutVarint(unsigned char *p, u64 v){ |
| 5131 if( v<=0x7f ){ |
| 5132 p[0] = v&0x7f; |
| 5133 return 1; |
| 5134 } |
| 5135 if( v<=0x3fff ){ |
| 5136 p[0] = ((v>>7)&0x7f)|0x80; |
| 5137 p[1] = v&0x7f; |
| 5138 return 2; |
| 5139 } |
| 5140 return putVarint64(p,v); |
| 5141 } |
| 5142 |
| 5143 /* |
| 5144 ** Bitmasks used by sqlite3GetVarint(). These precomputed constants |
| 5145 ** are defined here rather than simply putting the constant expressions |
| 5146 ** inline in order to work around bugs in the RVT compiler. |
| 5147 ** |
| 5148 ** SLOT_2_0 A mask for (0x7f<<14) | 0x7f |
| 5149 ** |
| 5150 ** SLOT_4_2_0 A mask for (0x7f<<28) | SLOT_2_0 |
| 5151 */ |
| 5152 #define SLOT_2_0 0x001fc07f |
| 5153 #define SLOT_4_2_0 0xf01fc07f |
| 5154 |
| 5155 |
| 5156 /* |
| 5157 ** Read a 64-bit variable-length integer from memory starting at p[0]. |
| 5158 ** Return the number of bytes read. The value is stored in *v. |
| 5159 */ |
| 5160 SQLITE_PRIVATE u8 sqlite3GetVarint(const unsigned char *p, u64 *v){ |
| 5161 u32 a,b,s; |
| 5162 |
| 5163 a = *p; |
| 5164 /* a: p0 (unmasked) */ |
| 5165 if (!(a&0x80)) |
| 5166 { |
| 5167 *v = a; |
| 5168 return 1; |
| 5169 } |
| 5170 |
| 5171 p++; |
| 5172 b = *p; |
| 5173 /* b: p1 (unmasked) */ |
| 5174 if (!(b&0x80)) |
| 5175 { |
| 5176 a &= 0x7f; |
| 5177 a = a<<7; |
| 5178 a |= b; |
| 5179 *v = a; |
| 5180 return 2; |
| 5181 } |
| 5182 |
| 5183 /* Verify that constants are precomputed correctly */ |
| 5184 assert( SLOT_2_0 == ((0x7f<<14) | (0x7f)) ); |
| 5185 assert( SLOT_4_2_0 == ((0xfU<<28) | (0x7f<<14) | (0x7f)) ); |
| 5186 |
| 5187 p++; |
| 5188 a = a<<14; |
| 5189 a |= *p; |
| 5190 /* a: p0<<14 | p2 (unmasked) */ |
| 5191 if (!(a&0x80)) |
| 5192 { |
| 5193 a &= SLOT_2_0; |
| 5194 b &= 0x7f; |
| 5195 b = b<<7; |
| 5196 a |= b; |
| 5197 *v = a; |
| 5198 return 3; |
| 5199 } |
| 5200 |
| 5201 /* CSE1 from below */ |
| 5202 a &= SLOT_2_0; |
| 5203 p++; |
| 5204 b = b<<14; |
| 5205 b |= *p; |
| 5206 /* b: p1<<14 | p3 (unmasked) */ |
| 5207 if (!(b&0x80)) |
| 5208 { |
| 5209 b &= SLOT_2_0; |
| 5210 /* moved CSE1 up */ |
| 5211 /* a &= (0x7f<<14)|(0x7f); */ |
| 5212 a = a<<7; |
| 5213 a |= b; |
| 5214 *v = a; |
| 5215 return 4; |
| 5216 } |
| 5217 |
| 5218 /* a: p0<<14 | p2 (masked) */ |
| 5219 /* b: p1<<14 | p3 (unmasked) */ |
| 5220 /* 1:save off p0<<21 | p1<<14 | p2<<7 | p3 (masked) */ |
| 5221 /* moved CSE1 up */ |
| 5222 /* a &= (0x7f<<14)|(0x7f); */ |
| 5223 b &= SLOT_2_0; |
| 5224 s = a; |
| 5225 /* s: p0<<14 | p2 (masked) */ |
| 5226 |
| 5227 p++; |
| 5228 a = a<<14; |
| 5229 a |= *p; |
| 5230 /* a: p0<<28 | p2<<14 | p4 (unmasked) */ |
| 5231 if (!(a&0x80)) |
| 5232 { |
| 5233 /* we can skip these cause they were (effectively) done above |
| 5234 ** while calculating s */ |
| 5235 /* a &= (0x7f<<28)|(0x7f<<14)|(0x7f); */ |
| 5236 /* b &= (0x7f<<14)|(0x7f); */ |
| 5237 b = b<<7; |
| 5238 a |= b; |
| 5239 s = s>>18; |
| 5240 *v = ((u64)s)<<32 | a; |
| 5241 return 5; |
| 5242 } |
| 5243 |
| 5244 /* 2:save off p0<<21 | p1<<14 | p2<<7 | p3 (masked) */ |
| 5245 s = s<<7; |
| 5246 s |= b; |
| 5247 /* s: p0<<21 | p1<<14 | p2<<7 | p3 (masked) */ |
| 5248 |
| 5249 p++; |
| 5250 b = b<<14; |
| 5251 b |= *p; |
| 5252 /* b: p1<<28 | p3<<14 | p5 (unmasked) */ |
| 5253 if (!(b&0x80)) |
| 5254 { |
| 5255 /* we can skip this cause it was (effectively) done above in calc'ing s */ |
| 5256 /* b &= (0x7f<<28)|(0x7f<<14)|(0x7f); */ |
| 5257 a &= SLOT_2_0; |
| 5258 a = a<<7; |
| 5259 a |= b; |
| 5260 s = s>>18; |
| 5261 *v = ((u64)s)<<32 | a; |
| 5262 return 6; |
| 5263 } |
| 5264 |
| 5265 p++; |
| 5266 a = a<<14; |
| 5267 a |= *p; |
| 5268 /* a: p2<<28 | p4<<14 | p6 (unmasked) */ |
| 5269 if (!(a&0x80)) |
| 5270 { |
| 5271 a &= SLOT_4_2_0; |
| 5272 b &= SLOT_2_0; |
| 5273 b = b<<7; |
| 5274 a |= b; |
| 5275 s = s>>11; |
| 5276 *v = ((u64)s)<<32 | a; |
| 5277 return 7; |
| 5278 } |
| 5279 |
| 5280 /* CSE2 from below */ |
| 5281 a &= SLOT_2_0; |
| 5282 p++; |
| 5283 b = b<<14; |
| 5284 b |= *p; |
| 5285 /* b: p3<<28 | p5<<14 | p7 (unmasked) */ |
| 5286 if (!(b&0x80)) |
| 5287 { |
| 5288 b &= SLOT_4_2_0; |
| 5289 /* moved CSE2 up */ |
| 5290 /* a &= (0x7f<<14)|(0x7f); */ |
| 5291 a = a<<7; |
| 5292 a |= b; |
| 5293 s = s>>4; |
| 5294 *v = ((u64)s)<<32 | a; |
| 5295 return 8; |
| 5296 } |
| 5297 |
| 5298 p++; |
| 5299 a = a<<15; |
| 5300 a |= *p; |
| 5301 /* a: p4<<29 | p6<<15 | p8 (unmasked) */ |
| 5302 |
| 5303 /* moved CSE2 up */ |
| 5304 /* a &= (0x7f<<29)|(0x7f<<15)|(0xff); */ |
| 5305 b &= SLOT_2_0; |
| 5306 b = b<<8; |
| 5307 a |= b; |
| 5308 |
| 5309 s = s<<4; |
| 5310 b = p[-4]; |
| 5311 b &= 0x7f; |
| 5312 b = b>>3; |
| 5313 s |= b; |
| 5314 |
| 5315 *v = ((u64)s)<<32 | a; |
| 5316 |
| 5317 return 9; |
| 5318 } |
| 5319 |
| 5320 /* |
| 5321 ** Read a 32-bit variable-length integer from memory starting at p[0]. |
| 5322 ** Return the number of bytes read. The value is stored in *v. |
| 5323 ** |
| 5324 ** If the varint stored in p[0] is larger than can fit in a 32-bit unsigned |
| 5325 ** integer, then set *v to 0xffffffff. |
| 5326 ** |
| 5327 ** A MACRO version, getVarint32, is provided which inlines the |
| 5328 ** single-byte case. All code should use the MACRO version as |
| 5329 ** this function assumes the single-byte case has already been handled. |
| 5330 */ |
| 5331 SQLITE_PRIVATE u8 sqlite3GetVarint32(const unsigned char *p, u32 *v){ |
| 5332 u32 a,b; |
| 5333 |
| 5334 /* The 1-byte case. Overwhelmingly the most common. Handled inline |
| 5335 ** by the getVarin32() macro */ |
| 5336 a = *p; |
| 5337 /* a: p0 (unmasked) */ |
| 5338 #ifndef getVarint32 |
| 5339 if (!(a&0x80)) |
| 5340 { |
| 5341 /* Values between 0 and 127 */ |
| 5342 *v = a; |
| 5343 return 1; |
| 5344 } |
| 5345 #endif |
| 5346 |
| 5347 /* The 2-byte case */ |
| 5348 p++; |
| 5349 b = *p; |
| 5350 /* b: p1 (unmasked) */ |
| 5351 if (!(b&0x80)) |
| 5352 { |
| 5353 /* Values between 128 and 16383 */ |
| 5354 a &= 0x7f; |
| 5355 a = a<<7; |
| 5356 *v = a | b; |
| 5357 return 2; |
| 5358 } |
| 5359 |
| 5360 /* The 3-byte case */ |
| 5361 p++; |
| 5362 a = a<<14; |
| 5363 a |= *p; |
| 5364 /* a: p0<<14 | p2 (unmasked) */ |
| 5365 if (!(a&0x80)) |
| 5366 { |
| 5367 /* Values between 16384 and 2097151 */ |
| 5368 a &= (0x7f<<14)|(0x7f); |
| 5369 b &= 0x7f; |
| 5370 b = b<<7; |
| 5371 *v = a | b; |
| 5372 return 3; |
| 5373 } |
| 5374 |
| 5375 /* A 32-bit varint is used to store size information in btrees. |
| 5376 ** Objects are rarely larger than 2MiB limit of a 3-byte varint. |
| 5377 ** A 3-byte varint is sufficient, for example, to record the size |
| 5378 ** of a 1048569-byte BLOB or string. |
| 5379 ** |
| 5380 ** We only unroll the first 1-, 2-, and 3- byte cases. The very |
| 5381 ** rare larger cases can be handled by the slower 64-bit varint |
| 5382 ** routine. |
| 5383 */ |
| 5384 #if 1 |
| 5385 { |
| 5386 u64 v64; |
| 5387 u8 n; |
| 5388 |
| 5389 p -= 2; |
| 5390 n = sqlite3GetVarint(p, &v64); |
| 5391 assert( n>3 && n<=9 ); |
| 5392 if( (v64 & SQLITE_MAX_U32)!=v64 ){ |
| 5393 *v = 0xffffffff; |
| 5394 }else{ |
| 5395 *v = (u32)v64; |
| 5396 } |
| 5397 return n; |
| 5398 } |
| 5399 |
| 5400 #else |
| 5401 /* For following code (kept for historical record only) shows an |
| 5402 ** unrolling for the 3- and 4-byte varint cases. This code is |
| 5403 ** slightly faster, but it is also larger and much harder to test. |
| 5404 */ |
| 5405 p++; |
| 5406 b = b<<14; |
| 5407 b |= *p; |
| 5408 /* b: p1<<14 | p3 (unmasked) */ |
| 5409 if (!(b&0x80)) |
| 5410 { |
| 5411 /* Values between 2097152 and 268435455 */ |
| 5412 b &= (0x7f<<14)|(0x7f); |
| 5413 a &= (0x7f<<14)|(0x7f); |
| 5414 a = a<<7; |
| 5415 *v = a | b; |
| 5416 return 4; |
| 5417 } |
| 5418 |
| 5419 p++; |
| 5420 a = a<<14; |
| 5421 a |= *p; |
| 5422 /* a: p0<<28 | p2<<14 | p4 (unmasked) */ |
| 5423 if (!(a&0x80)) |
| 5424 { |
| 5425 /* Values between 268435456 and 34359738367 */ |
| 5426 a &= SLOT_4_2_0; |
| 5427 b &= SLOT_4_2_0; |
| 5428 b = b<<7; |
| 5429 *v = a | b; |
| 5430 return 5; |
| 5431 } |
| 5432 |
| 5433 /* We can only reach this point when reading a corrupt database |
| 5434 ** file. In that case we are not in any hurry. Use the (relatively |
| 5435 ** slow) general-purpose sqlite3GetVarint() routine to extract the |
| 5436 ** value. */ |
| 5437 { |
| 5438 u64 v64; |
| 5439 u8 n; |
| 5440 |
| 5441 p -= 4; |
| 5442 n = sqlite3GetVarint(p, &v64); |
| 5443 assert( n>5 && n<=9 ); |
| 5444 *v = (u32)v64; |
| 5445 return n; |
| 5446 } |
| 5447 #endif |
| 5448 } |
| 5449 |
| 5450 /* |
| 5451 ** Return the number of bytes that will be needed to store the given |
| 5452 ** 64-bit integer. |
| 5453 */ |
| 5454 SQLITE_PRIVATE int sqlite3VarintLen(u64 v){ |
| 5455 int i; |
| 5456 for(i=1; (v >>= 7)!=0; i++){ assert( i<9 ); } |
| 5457 return i; |
| 5458 } |
| 5459 |
| 5460 |
| 5461 /* |
| 5462 ** Read or write a four-byte big-endian integer value. |
| 5463 */ |
| 5464 SQLITE_PRIVATE u32 sqlite3Get4byte(const u8 *p){ |
| 5465 #if SQLITE_BYTEORDER==4321 |
| 5466 u32 x; |
| 5467 memcpy(&x,p,4); |
| 5468 return x; |
| 5469 #elif SQLITE_BYTEORDER==1234 && !defined(SQLITE_DISABLE_INTRINSIC) \ |
| 5470 && defined(__GNUC__) && GCC_VERSION>=4003000 |
| 5471 u32 x; |
| 5472 memcpy(&x,p,4); |
| 5473 return __builtin_bswap32(x); |
| 5474 #elif SQLITE_BYTEORDER==1234 && !defined(SQLITE_DISABLE_INTRINSIC) \ |
| 5475 && defined(_MSC_VER) && _MSC_VER>=1300 |
| 5476 u32 x; |
| 5477 memcpy(&x,p,4); |
| 5478 return _byteswap_ulong(x); |
| 5479 #else |
| 5480 testcase( p[0]&0x80 ); |
| 5481 return ((unsigned)p[0]<<24) | (p[1]<<16) | (p[2]<<8) | p[3]; |
| 5482 #endif |
| 5483 } |
| 5484 SQLITE_PRIVATE void sqlite3Put4byte(unsigned char *p, u32 v){ |
| 5485 #if SQLITE_BYTEORDER==4321 |
| 5486 memcpy(p,&v,4); |
| 5487 #elif SQLITE_BYTEORDER==1234 && defined(__GNUC__) && GCC_VERSION>=4003000 |
| 5488 u32 x = __builtin_bswap32(v); |
| 5489 memcpy(p,&x,4); |
| 5490 #elif SQLITE_BYTEORDER==1234 && defined(_MSC_VER) && _MSC_VER>=1300 |
| 5491 u32 x = _byteswap_ulong(v); |
| 5492 memcpy(p,&x,4); |
| 5493 #else |
| 5494 p[0] = (u8)(v>>24); |
| 5495 p[1] = (u8)(v>>16); |
| 5496 p[2] = (u8)(v>>8); |
| 5497 p[3] = (u8)v; |
| 5498 #endif |
| 5499 } |
| 5500 |
| 5501 |
| 5502 |
| 5503 /* |
| 5504 ** Translate a single byte of Hex into an integer. |
| 5505 ** This routine only works if h really is a valid hexadecimal |
| 5506 ** character: 0..9a..fA..F |
| 5507 */ |
| 5508 SQLITE_PRIVATE u8 sqlite3HexToInt(int h){ |
| 5509 assert( (h>='0' && h<='9') || (h>='a' && h<='f') || (h>='A' && h<='F') ); |
| 5510 #ifdef SQLITE_ASCII |
| 5511 h += 9*(1&(h>>6)); |
| 5512 #endif |
| 5513 #ifdef SQLITE_EBCDIC |
| 5514 h += 9*(1&~(h>>4)); |
| 5515 #endif |
| 5516 return (u8)(h & 0xf); |
| 5517 } |
| 5518 |
| 5519 #if !defined(SQLITE_OMIT_BLOB_LITERAL) || defined(SQLITE_HAS_CODEC) |
| 5520 /* |
| 5521 ** Convert a BLOB literal of the form "x'hhhhhh'" into its binary |
| 5522 ** value. Return a pointer to its binary value. Space to hold the |
| 5523 ** binary value has been obtained from malloc and must be freed by |
| 5524 ** the calling routine. |
| 5525 */ |
| 5526 SQLITE_PRIVATE void *sqlite3HexToBlob(sqlite3 *db, const char *z, int n){ |
| 5527 char *zBlob; |
| 5528 int i; |
| 5529 |
| 5530 zBlob = (char *)sqlite3DbMallocRaw(db, n/2 + 1); |
| 5531 n--; |
| 5532 if( zBlob ){ |
| 5533 for(i=0; i<n; i+=2){ |
| 5534 zBlob[i/2] = (sqlite3HexToInt(z[i])<<4) | sqlite3HexToInt(z[i+1]); |
| 5535 } |
| 5536 zBlob[i/2] = 0; |
| 5537 } |
| 5538 return zBlob; |
| 5539 } |
| 5540 #endif /* !SQLITE_OMIT_BLOB_LITERAL || SQLITE_HAS_CODEC */ |
| 5541 |
| 5542 /* |
| 5543 ** Log an error that is an API call on a connection pointer that should |
| 5544 ** not have been used. The "type" of connection pointer is given as the |
| 5545 ** argument. The zType is a word like "NULL" or "closed" or "invalid". |
| 5546 */ |
| 5547 static void logBadConnection(const char *zType){ |
| 5548 sqlite3_log(SQLITE_MISUSE, |
| 5549 "API call with %s database connection pointer", |
| 5550 zType |
| 5551 ); |
| 5552 } |
| 5553 |
| 5554 /* |
| 5555 ** Check to make sure we have a valid db pointer. This test is not |
| 5556 ** foolproof but it does provide some measure of protection against |
| 5557 ** misuse of the interface such as passing in db pointers that are |
| 5558 ** NULL or which have been previously closed. If this routine returns |
| 5559 ** 1 it means that the db pointer is valid and 0 if it should not be |
| 5560 ** dereferenced for any reason. The calling function should invoke |
| 5561 ** SQLITE_MISUSE immediately. |
| 5562 ** |
| 5563 ** sqlite3SafetyCheckOk() requires that the db pointer be valid for |
| 5564 ** use. sqlite3SafetyCheckSickOrOk() allows a db pointer that failed to |
| 5565 ** open properly and is not fit for general use but which can be |
| 5566 ** used as an argument to sqlite3_errmsg() or sqlite3_close(). |
| 5567 */ |
| 5568 SQLITE_PRIVATE int sqlite3SafetyCheckOk(sqlite3 *db){ |
| 5569 u32 magic; |
| 5570 if( db==0 ){ |
| 5571 logBadConnection("NULL"); |
| 5572 return 0; |
| 5573 } |
| 5574 magic = db->magic; |
| 5575 if( magic!=SQLITE_MAGIC_OPEN ){ |
| 5576 if( sqlite3SafetyCheckSickOrOk(db) ){ |
| 5577 testcase( sqlite3GlobalConfig.xLog!=0 ); |
| 5578 logBadConnection("unopened"); |
| 5579 } |
| 5580 return 0; |
| 5581 }else{ |
| 5582 return 1; |
| 5583 } |
| 5584 } |
| 5585 SQLITE_PRIVATE int sqlite3SafetyCheckSickOrOk(sqlite3 *db){ |
| 5586 u32 magic; |
| 5587 magic = db->magic; |
| 5588 if( magic!=SQLITE_MAGIC_SICK && |
| 5589 magic!=SQLITE_MAGIC_OPEN && |
| 5590 magic!=SQLITE_MAGIC_BUSY ){ |
| 5591 testcase( sqlite3GlobalConfig.xLog!=0 ); |
| 5592 logBadConnection("invalid"); |
| 5593 return 0; |
| 5594 }else{ |
| 5595 return 1; |
| 5596 } |
| 5597 } |
| 5598 |
| 5599 /* |
| 5600 ** Attempt to add, substract, or multiply the 64-bit signed value iB against |
| 5601 ** the other 64-bit signed integer at *pA and store the result in *pA. |
| 5602 ** Return 0 on success. Or if the operation would have resulted in an |
| 5603 ** overflow, leave *pA unchanged and return 1. |
| 5604 */ |
| 5605 SQLITE_PRIVATE int sqlite3AddInt64(i64 *pA, i64 iB){ |
| 5606 i64 iA = *pA; |
| 5607 testcase( iA==0 ); testcase( iA==1 ); |
| 5608 testcase( iB==-1 ); testcase( iB==0 ); |
| 5609 if( iB>=0 ){ |
| 5610 testcase( iA>0 && LARGEST_INT64 - iA == iB ); |
| 5611 testcase( iA>0 && LARGEST_INT64 - iA == iB - 1 ); |
| 5612 if( iA>0 && LARGEST_INT64 - iA < iB ) return 1; |
| 5613 }else{ |
| 5614 testcase( iA<0 && -(iA + LARGEST_INT64) == iB + 1 ); |
| 5615 testcase( iA<0 && -(iA + LARGEST_INT64) == iB + 2 ); |
| 5616 if( iA<0 && -(iA + LARGEST_INT64) > iB + 1 ) return 1; |
| 5617 } |
| 5618 *pA += iB; |
| 5619 return 0; |
| 5620 } |
| 5621 SQLITE_PRIVATE int sqlite3SubInt64(i64 *pA, i64 iB){ |
| 5622 testcase( iB==SMALLEST_INT64+1 ); |
| 5623 if( iB==SMALLEST_INT64 ){ |
| 5624 testcase( (*pA)==(-1) ); testcase( (*pA)==0 ); |
| 5625 if( (*pA)>=0 ) return 1; |
| 5626 *pA -= iB; |
| 5627 return 0; |
| 5628 }else{ |
| 5629 return sqlite3AddInt64(pA, -iB); |
| 5630 } |
| 5631 } |
| 5632 #define TWOPOWER32 (((i64)1)<<32) |
| 5633 #define TWOPOWER31 (((i64)1)<<31) |
| 5634 SQLITE_PRIVATE int sqlite3MulInt64(i64 *pA, i64 iB){ |
| 5635 i64 iA = *pA; |
| 5636 i64 iA1, iA0, iB1, iB0, r; |
| 5637 |
| 5638 iA1 = iA/TWOPOWER32; |
| 5639 iA0 = iA % TWOPOWER32; |
| 5640 iB1 = iB/TWOPOWER32; |
| 5641 iB0 = iB % TWOPOWER32; |
| 5642 if( iA1==0 ){ |
| 5643 if( iB1==0 ){ |
| 5644 *pA *= iB; |
| 5645 return 0; |
| 5646 } |
| 5647 r = iA0*iB1; |
| 5648 }else if( iB1==0 ){ |
| 5649 r = iA1*iB0; |
| 5650 }else{ |
| 5651 /* If both iA1 and iB1 are non-zero, overflow will result */ |
| 5652 return 1; |
| 5653 } |
| 5654 testcase( r==(-TWOPOWER31)-1 ); |
| 5655 testcase( r==(-TWOPOWER31) ); |
| 5656 testcase( r==TWOPOWER31 ); |
| 5657 testcase( r==TWOPOWER31-1 ); |
| 5658 if( r<(-TWOPOWER31) || r>=TWOPOWER31 ) return 1; |
| 5659 r *= TWOPOWER32; |
| 5660 if( sqlite3AddInt64(&r, iA0*iB0) ) return 1; |
| 5661 *pA = r; |
| 5662 return 0; |
| 5663 } |
| 5664 |
| 5665 /* |
| 5666 ** Compute the absolute value of a 32-bit signed integer, of possible. Or |
| 5667 ** if the integer has a value of -2147483648, return +2147483647 |
| 5668 */ |
| 5669 SQLITE_PRIVATE int sqlite3AbsInt32(int x){ |
| 5670 if( x>=0 ) return x; |
| 5671 if( x==(int)0x80000000 ) return 0x7fffffff; |
| 5672 return -x; |
| 5673 } |
| 5674 |
| 5675 #ifdef SQLITE_ENABLE_8_3_NAMES |
| 5676 /* |
| 5677 ** If SQLITE_ENABLE_8_3_NAMES is set at compile-time and if the database |
| 5678 ** filename in zBaseFilename is a URI with the "8_3_names=1" parameter and |
| 5679 ** if filename in z[] has a suffix (a.k.a. "extension") that is longer than |
| 5680 ** three characters, then shorten the suffix on z[] to be the last three |
| 5681 ** characters of the original suffix. |
| 5682 ** |
| 5683 ** If SQLITE_ENABLE_8_3_NAMES is set to 2 at compile-time, then always |
| 5684 ** do the suffix shortening regardless of URI parameter. |
| 5685 ** |
| 5686 ** Examples: |
| 5687 ** |
| 5688 ** test.db-journal => test.nal |
| 5689 ** test.db-wal => test.wal |
| 5690 ** test.db-shm => test.shm |
| 5691 ** test.db-mj7f3319fa => test.9fa |
| 5692 */ |
| 5693 SQLITE_PRIVATE void sqlite3FileSuffix3(const char *zBaseFilename, char *z){ |
| 5694 #if SQLITE_ENABLE_8_3_NAMES<2 |
| 5695 if( sqlite3_uri_boolean(zBaseFilename, "8_3_names", 0) ) |
| 5696 #endif |
| 5697 { |
| 5698 int i, sz; |
| 5699 sz = sqlite3Strlen30(z); |
| 5700 for(i=sz-1; i>0 && z[i]!='/' && z[i]!='.'; i--){} |
| 5701 if( z[i]=='.' && ALWAYS(sz>i+4) ) memmove(&z[i+1], &z[sz-3], 4); |
| 5702 } |
| 5703 } |
| 5704 #endif |
| 5705 |
| 5706 /* |
| 5707 ** Find (an approximate) sum of two LogEst values. This computation is |
| 5708 ** not a simple "+" operator because LogEst is stored as a logarithmic |
| 5709 ** value. |
| 5710 ** |
| 5711 */ |
| 5712 SQLITE_PRIVATE LogEst sqlite3LogEstAdd(LogEst a, LogEst b){ |
| 5713 static const unsigned char x[] = { |
| 5714 10, 10, /* 0,1 */ |
| 5715 9, 9, /* 2,3 */ |
| 5716 8, 8, /* 4,5 */ |
| 5717 7, 7, 7, /* 6,7,8 */ |
| 5718 6, 6, 6, /* 9,10,11 */ |
| 5719 5, 5, 5, /* 12-14 */ |
| 5720 4, 4, 4, 4, /* 15-18 */ |
| 5721 3, 3, 3, 3, 3, 3, /* 19-24 */ |
| 5722 2, 2, 2, 2, 2, 2, 2, /* 25-31 */ |
| 5723 }; |
| 5724 if( a>=b ){ |
| 5725 if( a>b+49 ) return a; |
| 5726 if( a>b+31 ) return a+1; |
| 5727 return a+x[a-b]; |
| 5728 }else{ |
| 5729 if( b>a+49 ) return b; |
| 5730 if( b>a+31 ) return b+1; |
| 5731 return b+x[b-a]; |
| 5732 } |
| 5733 } |
| 5734 |
| 5735 /* |
| 5736 ** Convert an integer into a LogEst. In other words, compute an |
| 5737 ** approximation for 10*log2(x). |
| 5738 */ |
| 5739 SQLITE_PRIVATE LogEst sqlite3LogEst(u64 x){ |
| 5740 static LogEst a[] = { 0, 2, 3, 5, 6, 7, 8, 9 }; |
| 5741 LogEst y = 40; |
| 5742 if( x<8 ){ |
| 5743 if( x<2 ) return 0; |
| 5744 while( x<8 ){ y -= 10; x <<= 1; } |
| 5745 }else{ |
| 5746 while( x>255 ){ y += 40; x >>= 4; } |
| 5747 while( x>15 ){ y += 10; x >>= 1; } |
| 5748 } |
| 5749 return a[x&7] + y - 10; |
| 5750 } |
| 5751 |
| 5752 #ifndef SQLITE_OMIT_VIRTUALTABLE |
| 5753 /* |
| 5754 ** Convert a double into a LogEst |
| 5755 ** In other words, compute an approximation for 10*log2(x). |
| 5756 */ |
| 5757 SQLITE_PRIVATE LogEst sqlite3LogEstFromDouble(double x){ |
| 5758 u64 a; |
| 5759 LogEst e; |
| 5760 assert( sizeof(x)==8 && sizeof(a)==8 ); |
| 5761 if( x<=1 ) return 0; |
| 5762 if( x<=2000000000 ) return sqlite3LogEst((u64)x); |
| 5763 memcpy(&a, &x, 8); |
| 5764 e = (a>>52) - 1022; |
| 5765 return e*10; |
| 5766 } |
| 5767 #endif /* SQLITE_OMIT_VIRTUALTABLE */ |
| 5768 |
| 5769 /* |
| 5770 ** Convert a LogEst into an integer. |
| 5771 */ |
| 5772 SQLITE_PRIVATE u64 sqlite3LogEstToInt(LogEst x){ |
| 5773 u64 n; |
| 5774 if( x<10 ) return 1; |
| 5775 n = x%10; |
| 5776 x /= 10; |
| 5777 if( n>=5 ) n -= 2; |
| 5778 else if( n>=1 ) n -= 1; |
| 5779 if( x>=3 ){ |
| 5780 return x>60 ? (u64)LARGEST_INT64 : (n+8)<<(x-3); |
| 5781 } |
| 5782 return (n+8)>>(3-x); |
| 5783 } |
| 5784 |
| 5785 /************** End of util.c ************************************************/ |
| 5786 /************** Begin file hash.c ********************************************/ |
| 5787 /* |
| 5788 ** 2001 September 22 |
| 5789 ** |
| 5790 ** The author disclaims copyright to this source code. In place of |
| 5791 ** a legal notice, here is a blessing: |
| 5792 ** |
| 5793 ** May you do good and not evil. |
| 5794 ** May you find forgiveness for yourself and forgive others. |
| 5795 ** May you share freely, never taking more than you give. |
| 5796 ** |
| 5797 ************************************************************************* |
| 5798 ** This is the implementation of generic hash-tables |
| 5799 ** used in SQLite. |
| 5800 */ |
| 5801 /* #include "sqliteInt.h" */ |
| 5802 /* #include <assert.h> */ |
| 5803 |
| 5804 /* Turn bulk memory into a hash table object by initializing the |
| 5805 ** fields of the Hash structure. |
| 5806 ** |
| 5807 ** "pNew" is a pointer to the hash table that is to be initialized. |
| 5808 */ |
| 5809 SQLITE_PRIVATE void sqlite3HashInit(Hash *pNew){ |
| 5810 assert( pNew!=0 ); |
| 5811 pNew->first = 0; |
| 5812 pNew->count = 0; |
| 5813 pNew->htsize = 0; |
| 5814 pNew->ht = 0; |
| 5815 } |
| 5816 |
| 5817 /* Remove all entries from a hash table. Reclaim all memory. |
| 5818 ** Call this routine to delete a hash table or to reset a hash table |
| 5819 ** to the empty state. |
| 5820 */ |
| 5821 SQLITE_PRIVATE void sqlite3HashClear(Hash *pH){ |
| 5822 HashElem *elem; /* For looping over all elements of the table */ |
| 5823 |
| 5824 assert( pH!=0 ); |
| 5825 elem = pH->first; |
| 5826 pH->first = 0; |
| 5827 sqlite3_free(pH->ht); |
| 5828 pH->ht = 0; |
| 5829 pH->htsize = 0; |
| 5830 while( elem ){ |
| 5831 HashElem *next_elem = elem->next; |
| 5832 sqlite3_free(elem); |
| 5833 elem = next_elem; |
| 5834 } |
| 5835 pH->count = 0; |
| 5836 } |
| 5837 |
| 5838 /* |
| 5839 ** The hashing function. |
| 5840 */ |
| 5841 static unsigned int strHash(const char *z){ |
| 5842 unsigned int h = 0; |
| 5843 unsigned char c; |
| 5844 while( (c = (unsigned char)*z++)!=0 ){ |
| 5845 h = (h<<3) ^ h ^ sqlite3UpperToLower[c]; |
| 5846 } |
| 5847 return h; |
| 5848 } |
| 5849 |
| 5850 |
| 5851 /* Link pNew element into the hash table pH. If pEntry!=0 then also |
| 5852 ** insert pNew into the pEntry hash bucket. |
| 5853 */ |
| 5854 static void insertElement( |
| 5855 Hash *pH, /* The complete hash table */ |
| 5856 struct _ht *pEntry, /* The entry into which pNew is inserted */ |
| 5857 HashElem *pNew /* The element to be inserted */ |
| 5858 ){ |
| 5859 HashElem *pHead; /* First element already in pEntry */ |
| 5860 if( pEntry ){ |
| 5861 pHead = pEntry->count ? pEntry->chain : 0; |
| 5862 pEntry->count++; |
| 5863 pEntry->chain = pNew; |
| 5864 }else{ |
| 5865 pHead = 0; |
| 5866 } |
| 5867 if( pHead ){ |
| 5868 pNew->next = pHead; |
| 5869 pNew->prev = pHead->prev; |
| 5870 if( pHead->prev ){ pHead->prev->next = pNew; } |
| 5871 else { pH->first = pNew; } |
| 5872 pHead->prev = pNew; |
| 5873 }else{ |
| 5874 pNew->next = pH->first; |
| 5875 if( pH->first ){ pH->first->prev = pNew; } |
| 5876 pNew->prev = 0; |
| 5877 pH->first = pNew; |
| 5878 } |
| 5879 } |
| 5880 |
| 5881 |
| 5882 /* Resize the hash table so that it cantains "new_size" buckets. |
| 5883 ** |
| 5884 ** The hash table might fail to resize if sqlite3_malloc() fails or |
| 5885 ** if the new size is the same as the prior size. |
| 5886 ** Return TRUE if the resize occurs and false if not. |
| 5887 */ |
| 5888 static int rehash(Hash *pH, unsigned int new_size){ |
| 5889 struct _ht *new_ht; /* The new hash table */ |
| 5890 HashElem *elem, *next_elem; /* For looping over existing elements */ |
| 5891 |
| 5892 #if SQLITE_MALLOC_SOFT_LIMIT>0 |
| 5893 if( new_size*sizeof(struct _ht)>SQLITE_MALLOC_SOFT_LIMIT ){ |
| 5894 new_size = SQLITE_MALLOC_SOFT_LIMIT/sizeof(struct _ht); |
| 5895 } |
| 5896 if( new_size==pH->htsize ) return 0; |
| 5897 #endif |
| 5898 |
| 5899 /* The inability to allocates space for a larger hash table is |
| 5900 ** a performance hit but it is not a fatal error. So mark the |
| 5901 ** allocation as a benign. Use sqlite3Malloc()/memset(0) instead of |
| 5902 ** sqlite3MallocZero() to make the allocation, as sqlite3MallocZero() |
| 5903 ** only zeroes the requested number of bytes whereas this module will |
| 5904 ** use the actual amount of space allocated for the hash table (which |
| 5905 ** may be larger than the requested amount). |
| 5906 */ |
| 5907 sqlite3BeginBenignMalloc(); |
| 5908 new_ht = (struct _ht *)sqlite3Malloc( new_size*sizeof(struct _ht) ); |
| 5909 sqlite3EndBenignMalloc(); |
| 5910 |
| 5911 if( new_ht==0 ) return 0; |
| 5912 sqlite3_free(pH->ht); |
| 5913 pH->ht = new_ht; |
| 5914 pH->htsize = new_size = sqlite3MallocSize(new_ht)/sizeof(struct _ht); |
| 5915 memset(new_ht, 0, new_size*sizeof(struct _ht)); |
| 5916 for(elem=pH->first, pH->first=0; elem; elem = next_elem){ |
| 5917 unsigned int h = strHash(elem->pKey) % new_size; |
| 5918 next_elem = elem->next; |
| 5919 insertElement(pH, &new_ht[h], elem); |
| 5920 } |
| 5921 return 1; |
| 5922 } |
| 5923 |
| 5924 /* This function (for internal use only) locates an element in an |
| 5925 ** hash table that matches the given key. The hash for this key is |
| 5926 ** also computed and returned in the *pH parameter. |
| 5927 */ |
| 5928 static HashElem *findElementWithHash( |
| 5929 const Hash *pH, /* The pH to be searched */ |
| 5930 const char *pKey, /* The key we are searching for */ |
| 5931 unsigned int *pHash /* Write the hash value here */ |
| 5932 ){ |
| 5933 HashElem *elem; /* Used to loop thru the element list */ |
| 5934 int count; /* Number of elements left to test */ |
| 5935 unsigned int h; /* The computed hash */ |
| 5936 |
| 5937 if( pH->ht ){ |
| 5938 struct _ht *pEntry; |
| 5939 h = strHash(pKey) % pH->htsize; |
| 5940 pEntry = &pH->ht[h]; |
| 5941 elem = pEntry->chain; |
| 5942 count = pEntry->count; |
| 5943 }else{ |
| 5944 h = 0; |
| 5945 elem = pH->first; |
| 5946 count = pH->count; |
| 5947 } |
| 5948 *pHash = h; |
| 5949 while( count-- ){ |
| 5950 assert( elem!=0 ); |
| 5951 if( sqlite3StrICmp(elem->pKey,pKey)==0 ){ |
| 5952 return elem; |
| 5953 } |
| 5954 elem = elem->next; |
| 5955 } |
| 5956 return 0; |
| 5957 } |
| 5958 |
| 5959 /* Remove a single entry from the hash table given a pointer to that |
| 5960 ** element and a hash on the element's key. |
| 5961 */ |
| 5962 static void removeElementGivenHash( |
| 5963 Hash *pH, /* The pH containing "elem" */ |
| 5964 HashElem* elem, /* The element to be removed from the pH */ |
| 5965 unsigned int h /* Hash value for the element */ |
| 5966 ){ |
| 5967 struct _ht *pEntry; |
| 5968 if( elem->prev ){ |
| 5969 elem->prev->next = elem->next; |
| 5970 }else{ |
| 5971 pH->first = elem->next; |
| 5972 } |
| 5973 if( elem->next ){ |
| 5974 elem->next->prev = elem->prev; |
| 5975 } |
| 5976 if( pH->ht ){ |
| 5977 pEntry = &pH->ht[h]; |
| 5978 if( pEntry->chain==elem ){ |
| 5979 pEntry->chain = elem->next; |
| 5980 } |
| 5981 pEntry->count--; |
| 5982 assert( pEntry->count>=0 ); |
| 5983 } |
| 5984 sqlite3_free( elem ); |
| 5985 pH->count--; |
| 5986 if( pH->count==0 ){ |
| 5987 assert( pH->first==0 ); |
| 5988 assert( pH->count==0 ); |
| 5989 sqlite3HashClear(pH); |
| 5990 } |
| 5991 } |
| 5992 |
| 5993 /* Attempt to locate an element of the hash table pH with a key |
| 5994 ** that matches pKey. Return the data for this element if it is |
| 5995 ** found, or NULL if there is no match. |
| 5996 */ |
| 5997 SQLITE_PRIVATE void *sqlite3HashFind(const Hash *pH, const char *pKey){ |
| 5998 HashElem *elem; /* The element that matches key */ |
| 5999 unsigned int h; /* A hash on key */ |
| 6000 |
| 6001 assert( pH!=0 ); |
| 6002 assert( pKey!=0 ); |
| 6003 elem = findElementWithHash(pH, pKey, &h); |
| 6004 return elem ? elem->data : 0; |
| 6005 } |
| 6006 |
| 6007 /* Insert an element into the hash table pH. The key is pKey |
| 6008 ** and the data is "data". |
| 6009 ** |
| 6010 ** If no element exists with a matching key, then a new |
| 6011 ** element is created and NULL is returned. |
| 6012 ** |
| 6013 ** If another element already exists with the same key, then the |
| 6014 ** new data replaces the old data and the old data is returned. |
| 6015 ** The key is not copied in this instance. If a malloc fails, then |
| 6016 ** the new data is returned and the hash table is unchanged. |
| 6017 ** |
| 6018 ** If the "data" parameter to this function is NULL, then the |
| 6019 ** element corresponding to "key" is removed from the hash table. |
| 6020 */ |
| 6021 SQLITE_PRIVATE void *sqlite3HashInsert(Hash *pH, const char *pKey, void *data){ |
| 6022 unsigned int h; /* the hash of the key modulo hash table size */ |
| 6023 HashElem *elem; /* Used to loop thru the element list */ |
| 6024 HashElem *new_elem; /* New element added to the pH */ |
| 6025 |
| 6026 assert( pH!=0 ); |
| 6027 assert( pKey!=0 ); |
| 6028 elem = findElementWithHash(pH,pKey,&h); |
| 6029 if( elem ){ |
| 6030 void *old_data = elem->data; |
| 6031 if( data==0 ){ |
| 6032 removeElementGivenHash(pH,elem,h); |
| 6033 }else{ |
| 6034 elem->data = data; |
| 6035 elem->pKey = pKey; |
| 6036 } |
| 6037 return old_data; |
| 6038 } |
| 6039 if( data==0 ) return 0; |
| 6040 new_elem = (HashElem*)sqlite3Malloc( sizeof(HashElem) ); |
| 6041 if( new_elem==0 ) return data; |
| 6042 new_elem->pKey = pKey; |
| 6043 new_elem->data = data; |
| 6044 pH->count++; |
| 6045 if( pH->count>=10 && pH->count > 2*pH->htsize ){ |
| 6046 if( rehash(pH, pH->count*2) ){ |
| 6047 assert( pH->htsize>0 ); |
| 6048 h = strHash(pKey) % pH->htsize; |
| 6049 } |
| 6050 } |
| 6051 insertElement(pH, pH->ht ? &pH->ht[h] : 0, new_elem); |
| 6052 return 0; |
| 6053 } |
| 6054 |
| 6055 /************** End of hash.c ************************************************/ |
| 6056 /************** Begin file opcodes.c *****************************************/ |
| 6057 /* Automatically generated. Do not edit */ |
| 6058 /* See the tool/mkopcodec.tcl script for details. */ |
| 6059 #if !defined(SQLITE_OMIT_EXPLAIN) \ |
| 6060 || defined(VDBE_PROFILE) \ |
| 6061 || defined(SQLITE_DEBUG) |
| 6062 #if defined(SQLITE_ENABLE_EXPLAIN_COMMENTS) || defined(SQLITE_DEBUG) |
| 6063 # define OpHelp(X) "\0" X |
| 6064 #else |
| 6065 # define OpHelp(X) |
| 6066 #endif |
| 6067 SQLITE_PRIVATE const char *sqlite3OpcodeName(int i){ |
| 6068 static const char *const azName[] = { "?", |
| 6069 /* 1 */ "Savepoint" OpHelp(""), |
| 6070 /* 2 */ "AutoCommit" OpHelp(""), |
| 6071 /* 3 */ "Transaction" OpHelp(""), |
| 6072 /* 4 */ "SorterNext" OpHelp(""), |
| 6073 /* 5 */ "PrevIfOpen" OpHelp(""), |
| 6074 /* 6 */ "NextIfOpen" OpHelp(""), |
| 6075 /* 7 */ "Prev" OpHelp(""), |
| 6076 /* 8 */ "Next" OpHelp(""), |
| 6077 /* 9 */ "Checkpoint" OpHelp(""), |
| 6078 /* 10 */ "JournalMode" OpHelp(""), |
| 6079 /* 11 */ "Vacuum" OpHelp(""), |
| 6080 /* 12 */ "VFilter" OpHelp("iplan=r[P3] zplan='P4'"), |
| 6081 /* 13 */ "VUpdate" OpHelp("data=r[P3@P2]"), |
| 6082 /* 14 */ "Goto" OpHelp(""), |
| 6083 /* 15 */ "Gosub" OpHelp(""), |
| 6084 /* 16 */ "Return" OpHelp(""), |
| 6085 /* 17 */ "InitCoroutine" OpHelp(""), |
| 6086 /* 18 */ "EndCoroutine" OpHelp(""), |
| 6087 /* 19 */ "Not" OpHelp("r[P2]= !r[P1]"), |
| 6088 /* 20 */ "Yield" OpHelp(""), |
| 6089 /* 21 */ "HaltIfNull" OpHelp("if r[P3]=null halt"), |
| 6090 /* 22 */ "Halt" OpHelp(""), |
| 6091 /* 23 */ "Integer" OpHelp("r[P2]=P1"), |
| 6092 /* 24 */ "Int64" OpHelp("r[P2]=P4"), |
| 6093 /* 25 */ "String" OpHelp("r[P2]='P4' (len=P1)"), |
| 6094 /* 26 */ "Null" OpHelp("r[P2..P3]=NULL"), |
| 6095 /* 27 */ "SoftNull" OpHelp("r[P1]=NULL"), |
| 6096 /* 28 */ "Blob" OpHelp("r[P2]=P4 (len=P1)"), |
| 6097 /* 29 */ "Variable" OpHelp("r[P2]=parameter(P1,P4)"), |
| 6098 /* 30 */ "Move" OpHelp("r[P2@P3]=r[P1@P3]"), |
| 6099 /* 31 */ "Copy" OpHelp("r[P2@P3+1]=r[P1@P3+1]"), |
| 6100 /* 32 */ "SCopy" OpHelp("r[P2]=r[P1]"), |
| 6101 /* 33 */ "IntCopy" OpHelp("r[P2]=r[P1]"), |
| 6102 /* 34 */ "ResultRow" OpHelp("output=r[P1@P2]"), |
| 6103 /* 35 */ "CollSeq" OpHelp(""), |
| 6104 /* 36 */ "Function0" OpHelp("r[P3]=func(r[P2@P5])"), |
| 6105 /* 37 */ "Function" OpHelp("r[P3]=func(r[P2@P5])"), |
| 6106 /* 38 */ "AddImm" OpHelp("r[P1]=r[P1]+P2"), |
| 6107 /* 39 */ "MustBeInt" OpHelp(""), |
| 6108 /* 40 */ "RealAffinity" OpHelp(""), |
| 6109 /* 41 */ "Cast" OpHelp("affinity(r[P1])"), |
| 6110 /* 42 */ "Permutation" OpHelp(""), |
| 6111 /* 43 */ "Compare" OpHelp("r[P1@P3] <-> r[P2@P3]"), |
| 6112 /* 44 */ "Jump" OpHelp(""), |
| 6113 /* 45 */ "Once" OpHelp(""), |
| 6114 /* 46 */ "If" OpHelp(""), |
| 6115 /* 47 */ "IfNot" OpHelp(""), |
| 6116 /* 48 */ "Column" OpHelp("r[P3]=PX"), |
| 6117 /* 49 */ "Affinity" OpHelp("affinity(r[P1@P2])"), |
| 6118 /* 50 */ "MakeRecord" OpHelp("r[P3]=mkrec(r[P1@P2])"), |
| 6119 /* 51 */ "Count" OpHelp("r[P2]=count()"), |
| 6120 /* 52 */ "ReadCookie" OpHelp(""), |
| 6121 /* 53 */ "SetCookie" OpHelp(""), |
| 6122 /* 54 */ "ReopenIdx" OpHelp("root=P2 iDb=P3"), |
| 6123 /* 55 */ "OpenRead" OpHelp("root=P2 iDb=P3"), |
| 6124 /* 56 */ "OpenWrite" OpHelp("root=P2 iDb=P3"), |
| 6125 /* 57 */ "OpenAutoindex" OpHelp("nColumn=P2"), |
| 6126 /* 58 */ "OpenEphemeral" OpHelp("nColumn=P2"), |
| 6127 /* 59 */ "SorterOpen" OpHelp(""), |
| 6128 /* 60 */ "SequenceTest" OpHelp("if( cursor[P1].ctr++ ) pc = P2"), |
| 6129 /* 61 */ "OpenPseudo" OpHelp("P3 columns in r[P2]"), |
| 6130 /* 62 */ "Close" OpHelp(""), |
| 6131 /* 63 */ "ColumnsUsed" OpHelp(""), |
| 6132 /* 64 */ "SeekLT" OpHelp("key=r[P3@P4]"), |
| 6133 /* 65 */ "SeekLE" OpHelp("key=r[P3@P4]"), |
| 6134 /* 66 */ "SeekGE" OpHelp("key=r[P3@P4]"), |
| 6135 /* 67 */ "SeekGT" OpHelp("key=r[P3@P4]"), |
| 6136 /* 68 */ "Seek" OpHelp("intkey=r[P2]"), |
| 6137 /* 69 */ "NoConflict" OpHelp("key=r[P3@P4]"), |
| 6138 /* 70 */ "NotFound" OpHelp("key=r[P3@P4]"), |
| 6139 /* 71 */ "Or" OpHelp("r[P3]=(r[P1] || r[P2])"), |
| 6140 /* 72 */ "And" OpHelp("r[P3]=(r[P1] && r[P2])"), |
| 6141 /* 73 */ "Found" OpHelp("key=r[P3@P4]"), |
| 6142 /* 74 */ "NotExists" OpHelp("intkey=r[P3]"), |
| 6143 /* 75 */ "Sequence" OpHelp("r[P2]=cursor[P1].ctr++"), |
| 6144 /* 76 */ "IsNull" OpHelp("if r[P1]==NULL goto P2"), |
| 6145 /* 77 */ "NotNull" OpHelp("if r[P1]!=NULL goto P2"), |
| 6146 /* 78 */ "Ne" OpHelp("if r[P1]!=r[P3] goto P2"), |
| 6147 /* 79 */ "Eq" OpHelp("if r[P1]==r[P3] goto P2"), |
| 6148 /* 80 */ "Gt" OpHelp("if r[P1]>r[P3] goto P2"), |
| 6149 /* 81 */ "Le" OpHelp("if r[P1]<=r[P3] goto P2"), |
| 6150 /* 82 */ "Lt" OpHelp("if r[P1]<r[P3] goto P2"), |
| 6151 /* 83 */ "Ge" OpHelp("if r[P1]>=r[P3] goto P2"), |
| 6152 /* 84 */ "NewRowid" OpHelp("r[P2]=rowid"), |
| 6153 /* 85 */ "BitAnd" OpHelp("r[P3]=r[P1]&r[P2]"), |
| 6154 /* 86 */ "BitOr" OpHelp("r[P3]=r[P1]|r[P2]"), |
| 6155 /* 87 */ "ShiftLeft" OpHelp("r[P3]=r[P2]<<r[P1]"), |
| 6156 /* 88 */ "ShiftRight" OpHelp("r[P3]=r[P2]>>r[P1]"), |
| 6157 /* 89 */ "Add" OpHelp("r[P3]=r[P1]+r[P2]"), |
| 6158 /* 90 */ "Subtract" OpHelp("r[P3]=r[P2]-r[P1]"), |
| 6159 /* 91 */ "Multiply" OpHelp("r[P3]=r[P1]*r[P2]"), |
| 6160 /* 92 */ "Divide" OpHelp("r[P3]=r[P2]/r[P1]"), |
| 6161 /* 93 */ "Remainder" OpHelp("r[P3]=r[P2]%r[P1]"), |
| 6162 /* 94 */ "Concat" OpHelp("r[P3]=r[P2]+r[P1]"), |
| 6163 /* 95 */ "Insert" OpHelp("intkey=r[P3] data=r[P2]"), |
| 6164 /* 96 */ "BitNot" OpHelp("r[P1]= ~r[P1]"), |
| 6165 /* 97 */ "String8" OpHelp("r[P2]='P4'"), |
| 6166 /* 98 */ "InsertInt" OpHelp("intkey=P3 data=r[P2]"), |
| 6167 /* 99 */ "Delete" OpHelp(""), |
| 6168 /* 100 */ "ResetCount" OpHelp(""), |
| 6169 /* 101 */ "SorterCompare" OpHelp("if key(P1)!=trim(r[P3],P4) goto P2"), |
| 6170 /* 102 */ "SorterData" OpHelp("r[P2]=data"), |
| 6171 /* 103 */ "RowKey" OpHelp("r[P2]=key"), |
| 6172 /* 104 */ "RowData" OpHelp("r[P2]=data"), |
| 6173 /* 105 */ "Rowid" OpHelp("r[P2]=rowid"), |
| 6174 /* 106 */ "NullRow" OpHelp(""), |
| 6175 /* 107 */ "Last" OpHelp(""), |
| 6176 /* 108 */ "SorterSort" OpHelp(""), |
| 6177 /* 109 */ "Sort" OpHelp(""), |
| 6178 /* 110 */ "Rewind" OpHelp(""), |
| 6179 /* 111 */ "SorterInsert" OpHelp(""), |
| 6180 /* 112 */ "IdxInsert" OpHelp("key=r[P2]"), |
| 6181 /* 113 */ "IdxDelete" OpHelp("key=r[P2@P3]"), |
| 6182 /* 114 */ "IdxRowid" OpHelp("r[P2]=rowid"), |
| 6183 /* 115 */ "IdxLE" OpHelp("key=r[P3@P4]"), |
| 6184 /* 116 */ "IdxGT" OpHelp("key=r[P3@P4]"), |
| 6185 /* 117 */ "IdxLT" OpHelp("key=r[P3@P4]"), |
| 6186 /* 118 */ "IdxGE" OpHelp("key=r[P3@P4]"), |
| 6187 /* 119 */ "Destroy" OpHelp(""), |
| 6188 /* 120 */ "Clear" OpHelp(""), |
| 6189 /* 121 */ "ResetSorter" OpHelp(""), |
| 6190 /* 122 */ "CreateIndex" OpHelp("r[P2]=root iDb=P1"), |
| 6191 /* 123 */ "CreateTable" OpHelp("r[P2]=root iDb=P1"), |
| 6192 /* 124 */ "ParseSchema" OpHelp(""), |
| 6193 /* 125 */ "LoadAnalysis" OpHelp(""), |
| 6194 /* 126 */ "DropTable" OpHelp(""), |
| 6195 /* 127 */ "DropIndex" OpHelp(""), |
| 6196 /* 128 */ "DropTrigger" OpHelp(""), |
| 6197 /* 129 */ "IntegrityCk" OpHelp(""), |
| 6198 /* 130 */ "RowSetAdd" OpHelp("rowset(P1)=r[P2]"), |
| 6199 /* 131 */ "RowSetRead" OpHelp("r[P3]=rowset(P1)"), |
| 6200 /* 132 */ "RowSetTest" OpHelp("if r[P3] in rowset(P1) goto P2"), |
| 6201 /* 133 */ "Real" OpHelp("r[P2]=P4"), |
| 6202 /* 134 */ "Program" OpHelp(""), |
| 6203 /* 135 */ "Param" OpHelp(""), |
| 6204 /* 136 */ "FkCounter" OpHelp("fkctr[P1]+=P2"), |
| 6205 /* 137 */ "FkIfZero" OpHelp("if fkctr[P1]==0 goto P2"), |
| 6206 /* 138 */ "MemMax" OpHelp("r[P1]=max(r[P1],r[P2])"), |
| 6207 /* 139 */ "IfPos" OpHelp("if r[P1]>0 then r[P1]-=P3, goto P2"), |
| 6208 /* 140 */ "SetIfNotPos" OpHelp("if r[P1]<=0 then r[P2]=P3"), |
| 6209 /* 141 */ "IfNotZero" OpHelp("if r[P1]!=0 then r[P1]-=P3, goto P2"), |
| 6210 /* 142 */ "DecrJumpZero" OpHelp("if (--r[P1])==0 goto P2"), |
| 6211 /* 143 */ "JumpZeroIncr" OpHelp("if (r[P1]++)==0 ) goto P2"), |
| 6212 /* 144 */ "AggStep0" OpHelp("accum=r[P3] step(r[P2@P5])"), |
| 6213 /* 145 */ "AggStep" OpHelp("accum=r[P3] step(r[P2@P5])"), |
| 6214 /* 146 */ "AggFinal" OpHelp("accum=r[P1] N=P2"), |
| 6215 /* 147 */ "IncrVacuum" OpHelp(""), |
| 6216 /* 148 */ "Expire" OpHelp(""), |
| 6217 /* 149 */ "TableLock" OpHelp("iDb=P1 root=P2 write=P3"), |
| 6218 /* 150 */ "VBegin" OpHelp(""), |
| 6219 /* 151 */ "VCreate" OpHelp(""), |
| 6220 /* 152 */ "VDestroy" OpHelp(""), |
| 6221 /* 153 */ "VOpen" OpHelp(""), |
| 6222 /* 154 */ "VColumn" OpHelp("r[P3]=vcolumn(P2)"), |
| 6223 /* 155 */ "VNext" OpHelp(""), |
| 6224 /* 156 */ "VRename" OpHelp(""), |
| 6225 /* 157 */ "Pagecount" OpHelp(""), |
| 6226 /* 158 */ "MaxPgcnt" OpHelp(""), |
| 6227 /* 159 */ "Init" OpHelp("Start at P2"), |
| 6228 /* 160 */ "CursorHint" OpHelp(""), |
| 6229 /* 161 */ "Noop" OpHelp(""), |
| 6230 /* 162 */ "Explain" OpHelp(""), |
| 6231 }; |
| 6232 return azName[i]; |
| 6233 } |
| 6234 #endif |
| 6235 |
| 6236 /************** End of opcodes.c *********************************************/ |
| 6237 /************** Begin file os_unix.c *****************************************/ |
| 6238 /* |
| 6239 ** 2004 May 22 |
| 6240 ** |
| 6241 ** The author disclaims copyright to this source code. In place of |
| 6242 ** a legal notice, here is a blessing: |
| 6243 ** |
| 6244 ** May you do good and not evil. |
| 6245 ** May you find forgiveness for yourself and forgive others. |
| 6246 ** May you share freely, never taking more than you give. |
| 6247 ** |
| 6248 ****************************************************************************** |
| 6249 ** |
| 6250 ** This file contains the VFS implementation for unix-like operating systems |
| 6251 ** include Linux, MacOSX, *BSD, QNX, VxWorks, AIX, HPUX, and others. |
| 6252 ** |
| 6253 ** There are actually several different VFS implementations in this file. |
| 6254 ** The differences are in the way that file locking is done. The default |
| 6255 ** implementation uses Posix Advisory Locks. Alternative implementations |
| 6256 ** use flock(), dot-files, various proprietary locking schemas, or simply |
| 6257 ** skip locking all together. |
| 6258 ** |
| 6259 ** This source file is organized into divisions where the logic for various |
| 6260 ** subfunctions is contained within the appropriate division. PLEASE |
| 6261 ** KEEP THE STRUCTURE OF THIS FILE INTACT. New code should be placed |
| 6262 ** in the correct division and should be clearly labeled. |
| 6263 ** |
| 6264 ** The layout of divisions is as follows: |
| 6265 ** |
| 6266 ** * General-purpose declarations and utility functions. |
| 6267 ** * Unique file ID logic used by VxWorks. |
| 6268 ** * Various locking primitive implementations (all except proxy locking): |
| 6269 ** + for Posix Advisory Locks |
| 6270 ** + for no-op locks |
| 6271 ** + for dot-file locks |
| 6272 ** + for flock() locking |
| 6273 ** + for named semaphore locks (VxWorks only) |
| 6274 ** + for AFP filesystem locks (MacOSX only) |
| 6275 ** * sqlite3_file methods not associated with locking. |
| 6276 ** * Definitions of sqlite3_io_methods objects for all locking |
| 6277 ** methods plus "finder" functions for each locking method. |
| 6278 ** * sqlite3_vfs method implementations. |
| 6279 ** * Locking primitives for the proxy uber-locking-method. (MacOSX only) |
| 6280 ** * Definitions of sqlite3_vfs objects for all locking methods |
| 6281 ** plus implementations of sqlite3_os_init() and sqlite3_os_end(). |
| 6282 */ |
| 6283 /* #include "sqliteInt.h" */ |
| 6284 #if SQLITE_OS_UNIX /* This file is used on unix only */ |
| 6285 |
| 6286 /* |
| 6287 ** There are various methods for file locking used for concurrency |
| 6288 ** control: |
| 6289 ** |
| 6290 ** 1. POSIX locking (the default), |
| 6291 ** 2. No locking, |
| 6292 ** 3. Dot-file locking, |
| 6293 ** 4. flock() locking, |
| 6294 ** 5. AFP locking (OSX only), |
| 6295 ** 6. Named POSIX semaphores (VXWorks only), |
| 6296 ** 7. proxy locking. (OSX only) |
| 6297 ** |
| 6298 ** Styles 4, 5, and 7 are only available of SQLITE_ENABLE_LOCKING_STYLE |
| 6299 ** is defined to 1. The SQLITE_ENABLE_LOCKING_STYLE also enables automatic |
| 6300 ** selection of the appropriate locking style based on the filesystem |
| 6301 ** where the database is located. |
| 6302 */ |
| 6303 #if !defined(SQLITE_ENABLE_LOCKING_STYLE) |
| 6304 # if defined(__APPLE__) |
| 6305 # define SQLITE_ENABLE_LOCKING_STYLE 1 |
| 6306 # else |
| 6307 # define SQLITE_ENABLE_LOCKING_STYLE 0 |
| 6308 # endif |
| 6309 #endif |
| 6310 |
| 6311 /* |
| 6312 ** standard include files. |
| 6313 */ |
| 6314 #include <sys/types.h> |
| 6315 #include <sys/stat.h> |
| 6316 #include <fcntl.h> |
| 6317 #include <unistd.h> |
| 6318 /* #include <time.h> */ |
| 6319 #include <sys/time.h> |
| 6320 #include <errno.h> |
| 6321 #if !defined(SQLITE_OMIT_WAL) || SQLITE_MAX_MMAP_SIZE>0 |
| 6322 # include <sys/mman.h> |
| 6323 #endif |
| 6324 |
| 6325 #if SQLITE_ENABLE_LOCKING_STYLE |
| 6326 # include <sys/ioctl.h> |
| 6327 # include <sys/file.h> |
| 6328 # include <sys/param.h> |
| 6329 #endif /* SQLITE_ENABLE_LOCKING_STYLE */ |
| 6330 |
| 6331 #if defined(__APPLE__) && ((__MAC_OS_X_VERSION_MIN_REQUIRED > 1050) || \ |
| 6332 (__IPHONE_OS_VERSION_MIN_REQUIRED > 2000)) |
| 6333 # if (!defined(TARGET_OS_EMBEDDED) || (TARGET_OS_EMBEDDED==0)) \ |
| 6334 && (!defined(TARGET_IPHONE_SIMULATOR) || (TARGET_IPHONE_SIMULATOR==0)) |
| 6335 # define HAVE_GETHOSTUUID 1 |
| 6336 # else |
| 6337 # warning "gethostuuid() is disabled." |
| 6338 # endif |
| 6339 #endif |
| 6340 |
| 6341 |
| 6342 #if OS_VXWORKS |
| 6343 /* # include <sys/ioctl.h> */ |
| 6344 # include <semaphore.h> |
| 6345 # include <limits.h> |
| 6346 #endif /* OS_VXWORKS */ |
| 6347 |
| 6348 #if defined(__APPLE__) || SQLITE_ENABLE_LOCKING_STYLE |
| 6349 # include <sys/mount.h> |
| 6350 #endif |
| 6351 |
| 6352 #ifdef HAVE_UTIME |
| 6353 # include <utime.h> |
| 6354 #endif |
| 6355 |
| 6356 /* |
| 6357 ** Allowed values of unixFile.fsFlags |
| 6358 */ |
| 6359 #define SQLITE_FSFLAGS_IS_MSDOS 0x1 |
| 6360 |
| 6361 /* |
| 6362 ** If we are to be thread-safe, include the pthreads header and define |
| 6363 ** the SQLITE_UNIX_THREADS macro. |
| 6364 */ |
| 6365 #if SQLITE_THREADSAFE |
| 6366 /* # include <pthread.h> */ |
| 6367 # define SQLITE_UNIX_THREADS 1 |
| 6368 #endif |
| 6369 |
| 6370 /* |
| 6371 ** Default permissions when creating a new file |
| 6372 */ |
| 6373 #ifndef SQLITE_DEFAULT_FILE_PERMISSIONS |
| 6374 # define SQLITE_DEFAULT_FILE_PERMISSIONS 0644 |
| 6375 #endif |
| 6376 |
| 6377 /* |
| 6378 ** Default permissions when creating auto proxy dir |
| 6379 */ |
| 6380 #ifndef SQLITE_DEFAULT_PROXYDIR_PERMISSIONS |
| 6381 # define SQLITE_DEFAULT_PROXYDIR_PERMISSIONS 0755 |
| 6382 #endif |
| 6383 |
| 6384 /* |
| 6385 ** Maximum supported path-length. |
| 6386 */ |
| 6387 #define MAX_PATHNAME 512 |
| 6388 |
| 6389 /* Always cast the getpid() return type for compatibility with |
| 6390 ** kernel modules in VxWorks. */ |
| 6391 #define osGetpid(X) (pid_t)getpid() |
| 6392 |
| 6393 /* |
| 6394 ** Only set the lastErrno if the error code is a real error and not |
| 6395 ** a normal expected return code of SQLITE_BUSY or SQLITE_OK |
| 6396 */ |
| 6397 #define IS_LOCK_ERROR(x) ((x != SQLITE_OK) && (x != SQLITE_BUSY)) |
| 6398 |
| 6399 /* Forward references */ |
| 6400 typedef struct unixShm unixShm; /* Connection shared memory */ |
| 6401 typedef struct unixShmNode unixShmNode; /* Shared memory instance */ |
| 6402 typedef struct unixInodeInfo unixInodeInfo; /* An i-node */ |
| 6403 typedef struct UnixUnusedFd UnixUnusedFd; /* An unused file descriptor */ |
| 6404 |
| 6405 /* |
| 6406 ** Sometimes, after a file handle is closed by SQLite, the file descriptor |
| 6407 ** cannot be closed immediately. In these cases, instances of the following |
| 6408 ** structure are used to store the file descriptor while waiting for an |
| 6409 ** opportunity to either close or reuse it. |
| 6410 */ |
| 6411 struct UnixUnusedFd { |
| 6412 int fd; /* File descriptor to close */ |
| 6413 int flags; /* Flags this file descriptor was opened with */ |
| 6414 UnixUnusedFd *pNext; /* Next unused file descriptor on same file */ |
| 6415 }; |
| 6416 |
| 6417 /* |
| 6418 ** The unixFile structure is subclass of sqlite3_file specific to the unix |
| 6419 ** VFS implementations. |
| 6420 */ |
| 6421 typedef struct unixFile unixFile; |
| 6422 struct unixFile { |
| 6423 sqlite3_io_methods const *pMethod; /* Always the first entry */ |
| 6424 sqlite3_vfs *pVfs; /* The VFS that created this unixFile */ |
| 6425 unixInodeInfo *pInode; /* Info about locks on this inode */ |
| 6426 int h; /* The file descriptor */ |
| 6427 unsigned char eFileLock; /* The type of lock held on this fd */ |
| 6428 unsigned short int ctrlFlags; /* Behavioral bits. UNIXFILE_* flags */ |
| 6429 int lastErrno; /* The unix errno from last I/O error */ |
| 6430 void *lockingContext; /* Locking style specific state */ |
| 6431 UnixUnusedFd *pUnused; /* Pre-allocated UnixUnusedFd */ |
| 6432 const char *zPath; /* Name of the file */ |
| 6433 unixShm *pShm; /* Shared memory segment information */ |
| 6434 int szChunk; /* Configured by FCNTL_CHUNK_SIZE */ |
| 6435 #if SQLITE_MAX_MMAP_SIZE>0 |
| 6436 int nFetchOut; /* Number of outstanding xFetch refs */ |
| 6437 sqlite3_int64 mmapSize; /* Usable size of mapping at pMapRegion */ |
| 6438 sqlite3_int64 mmapSizeActual; /* Actual size of mapping at pMapRegion */ |
| 6439 sqlite3_int64 mmapSizeMax; /* Configured FCNTL_MMAP_SIZE value */ |
| 6440 void *pMapRegion; /* Memory mapped region */ |
| 6441 #endif |
| 6442 #ifdef __QNXNTO__ |
| 6443 int sectorSize; /* Device sector size */ |
| 6444 int deviceCharacteristics; /* Precomputed device characteristics */ |
| 6445 #endif |
| 6446 #if SQLITE_ENABLE_LOCKING_STYLE |
| 6447 int openFlags; /* The flags specified at open() */ |
| 6448 #endif |
| 6449 #if SQLITE_ENABLE_LOCKING_STYLE || defined(__APPLE__) |
| 6450 unsigned fsFlags; /* cached details from statfs() */ |
| 6451 #endif |
| 6452 #if OS_VXWORKS |
| 6453 struct vxworksFileId *pId; /* Unique file ID */ |
| 6454 #endif |
| 6455 #ifdef SQLITE_DEBUG |
| 6456 /* The next group of variables are used to track whether or not the |
| 6457 ** transaction counter in bytes 24-27 of database files are updated |
| 6458 ** whenever any part of the database changes. An assertion fault will |
| 6459 ** occur if a file is updated without also updating the transaction |
| 6460 ** counter. This test is made to avoid new problems similar to the |
| 6461 ** one described by ticket #3584. |
| 6462 */ |
| 6463 unsigned char transCntrChng; /* True if the transaction counter changed */ |
| 6464 unsigned char dbUpdate; /* True if any part of database file changed */ |
| 6465 unsigned char inNormalWrite; /* True if in a normal write operation */ |
| 6466 |
| 6467 #endif |
| 6468 |
| 6469 #ifdef SQLITE_TEST |
| 6470 /* In test mode, increase the size of this structure a bit so that |
| 6471 ** it is larger than the struct CrashFile defined in test6.c. |
| 6472 */ |
| 6473 char aPadding[32]; |
| 6474 #endif |
| 6475 }; |
| 6476 |
| 6477 /* This variable holds the process id (pid) from when the xRandomness() |
| 6478 ** method was called. If xOpen() is called from a different process id, |
| 6479 ** indicating that a fork() has occurred, the PRNG will be reset. |
| 6480 */ |
| 6481 static pid_t randomnessPid = 0; |
| 6482 |
| 6483 /* |
| 6484 ** Allowed values for the unixFile.ctrlFlags bitmask: |
| 6485 */ |
| 6486 #define UNIXFILE_EXCL 0x01 /* Connections from one process only */ |
| 6487 #define UNIXFILE_RDONLY 0x02 /* Connection is read only */ |
| 6488 #define UNIXFILE_PERSIST_WAL 0x04 /* Persistent WAL mode */ |
| 6489 #ifndef SQLITE_DISABLE_DIRSYNC |
| 6490 # define UNIXFILE_DIRSYNC 0x08 /* Directory sync needed */ |
| 6491 #else |
| 6492 # define UNIXFILE_DIRSYNC 0x00 |
| 6493 #endif |
| 6494 #define UNIXFILE_PSOW 0x10 /* SQLITE_IOCAP_POWERSAFE_OVERWRITE */ |
| 6495 #define UNIXFILE_DELETE 0x20 /* Delete on close */ |
| 6496 #define UNIXFILE_URI 0x40 /* Filename might have query parameters */ |
| 6497 #define UNIXFILE_NOLOCK 0x80 /* Do no file locking */ |
| 6498 |
| 6499 /* |
| 6500 ** Include code that is common to all os_*.c files |
| 6501 */ |
| 6502 /************** Include os_common.h in the middle of os_unix.c ***************/ |
| 6503 /************** Begin file os_common.h ***************************************/ |
| 6504 /* |
| 6505 ** 2004 May 22 |
| 6506 ** |
| 6507 ** The author disclaims copyright to this source code. In place of |
| 6508 ** a legal notice, here is a blessing: |
| 6509 ** |
| 6510 ** May you do good and not evil. |
| 6511 ** May you find forgiveness for yourself and forgive others. |
| 6512 ** May you share freely, never taking more than you give. |
| 6513 ** |
| 6514 ****************************************************************************** |
| 6515 ** |
| 6516 ** This file contains macros and a little bit of code that is common to |
| 6517 ** all of the platform-specific files (os_*.c) and is #included into those |
| 6518 ** files. |
| 6519 ** |
| 6520 ** This file should be #included by the os_*.c files only. It is not a |
| 6521 ** general purpose header file. |
| 6522 */ |
| 6523 #ifndef _OS_COMMON_H_ |
| 6524 #define _OS_COMMON_H_ |
| 6525 |
| 6526 /* |
| 6527 ** At least two bugs have slipped in because we changed the MEMORY_DEBUG |
| 6528 ** macro to SQLITE_DEBUG and some older makefiles have not yet made the |
| 6529 ** switch. The following code should catch this problem at compile-time. |
| 6530 */ |
| 6531 #ifdef MEMORY_DEBUG |
| 6532 # error "The MEMORY_DEBUG macro is obsolete. Use SQLITE_DEBUG instead." |
| 6533 #endif |
| 6534 |
| 6535 /* |
| 6536 ** Macros for performance tracing. Normally turned off. Only works |
| 6537 ** on i486 hardware. |
| 6538 */ |
| 6539 #ifdef SQLITE_PERFORMANCE_TRACE |
| 6540 |
| 6541 /* |
| 6542 ** hwtime.h contains inline assembler code for implementing |
| 6543 ** high-performance timing routines. |
| 6544 */ |
| 6545 /************** Include hwtime.h in the middle of os_common.h ****************/ |
| 6546 /************** Begin file hwtime.h ******************************************/ |
| 6547 /* |
| 6548 ** 2008 May 27 |
| 6549 ** |
| 6550 ** The author disclaims copyright to this source code. In place of |
| 6551 ** a legal notice, here is a blessing: |
| 6552 ** |
| 6553 ** May you do good and not evil. |
| 6554 ** May you find forgiveness for yourself and forgive others. |
| 6555 ** May you share freely, never taking more than you give. |
| 6556 ** |
| 6557 ****************************************************************************** |
| 6558 ** |
| 6559 ** This file contains inline asm code for retrieving "high-performance" |
| 6560 ** counters for x86 class CPUs. |
| 6561 */ |
| 6562 #ifndef _HWTIME_H_ |
| 6563 #define _HWTIME_H_ |
| 6564 |
| 6565 /* |
| 6566 ** The following routine only works on pentium-class (or newer) processors. |
| 6567 ** It uses the RDTSC opcode to read the cycle count value out of the |
| 6568 ** processor and returns that value. This can be used for high-res |
| 6569 ** profiling. |
| 6570 */ |
| 6571 #if (defined(__GNUC__) || defined(_MSC_VER)) && \ |
| 6572 (defined(i386) || defined(__i386__) || defined(_M_IX86)) |
| 6573 |
| 6574 #if defined(__GNUC__) |
| 6575 |
| 6576 __inline__ sqlite_uint64 sqlite3Hwtime(void){ |
| 6577 unsigned int lo, hi; |
| 6578 __asm__ __volatile__ ("rdtsc" : "=a" (lo), "=d" (hi)); |
| 6579 return (sqlite_uint64)hi << 32 | lo; |
| 6580 } |
| 6581 |
| 6582 #elif defined(_MSC_VER) |
| 6583 |
| 6584 __declspec(naked) __inline sqlite_uint64 __cdecl sqlite3Hwtime(void){ |
| 6585 __asm { |
| 6586 rdtsc |
| 6587 ret ; return value at EDX:EAX |
| 6588 } |
| 6589 } |
| 6590 |
| 6591 #endif |
| 6592 |
| 6593 #elif (defined(__GNUC__) && defined(__x86_64__)) |
| 6594 |
| 6595 __inline__ sqlite_uint64 sqlite3Hwtime(void){ |
| 6596 unsigned long val; |
| 6597 __asm__ __volatile__ ("rdtsc" : "=A" (val)); |
| 6598 return val; |
| 6599 } |
| 6600 |
| 6601 #elif (defined(__GNUC__) && defined(__ppc__)) |
| 6602 |
| 6603 __inline__ sqlite_uint64 sqlite3Hwtime(void){ |
| 6604 unsigned long long retval; |
| 6605 unsigned long junk; |
| 6606 __asm__ __volatile__ ("\n\ |
| 6607 1: mftbu %1\n\ |
| 6608 mftb %L0\n\ |
| 6609 mftbu %0\n\ |
| 6610 cmpw %0,%1\n\ |
| 6611 bne 1b" |
| 6612 : "=r" (retval), "=r" (junk)); |
| 6613 return retval; |
| 6614 } |
| 6615 |
| 6616 #else |
| 6617 |
| 6618 #error Need implementation of sqlite3Hwtime() for your platform. |
| 6619 |
| 6620 /* |
| 6621 ** To compile without implementing sqlite3Hwtime() for your platform, |
| 6622 ** you can remove the above #error and use the following |
| 6623 ** stub function. You will lose timing support for many |
| 6624 ** of the debugging and testing utilities, but it should at |
| 6625 ** least compile and run. |
| 6626 */ |
| 6627 SQLITE_PRIVATE sqlite_uint64 sqlite3Hwtime(void){ return ((sqlite_uint64)0); } |
| 6628 |
| 6629 #endif |
| 6630 |
| 6631 #endif /* !defined(_HWTIME_H_) */ |
| 6632 |
| 6633 /************** End of hwtime.h **********************************************/ |
| 6634 /************** Continuing where we left off in os_common.h ******************/ |
| 6635 |
| 6636 static sqlite_uint64 g_start; |
| 6637 static sqlite_uint64 g_elapsed; |
| 6638 #define TIMER_START g_start=sqlite3Hwtime() |
| 6639 #define TIMER_END g_elapsed=sqlite3Hwtime()-g_start |
| 6640 #define TIMER_ELAPSED g_elapsed |
| 6641 #else |
| 6642 #define TIMER_START |
| 6643 #define TIMER_END |
| 6644 #define TIMER_ELAPSED ((sqlite_uint64)0) |
| 6645 #endif |
| 6646 |
| 6647 /* |
| 6648 ** If we compile with the SQLITE_TEST macro set, then the following block |
| 6649 ** of code will give us the ability to simulate a disk I/O error. This |
| 6650 ** is used for testing the I/O recovery logic. |
| 6651 */ |
| 6652 #ifdef SQLITE_TEST |
| 6653 SQLITE_API int sqlite3_io_error_hit = 0; /* Total number of I/O Error
s */ |
| 6654 SQLITE_API int sqlite3_io_error_hardhit = 0; /* Number of non-benign erro
rs */ |
| 6655 SQLITE_API int sqlite3_io_error_pending = 0; /* Count down to first I/O e
rror */ |
| 6656 SQLITE_API int sqlite3_io_error_persist = 0; /* True if I/O errors persis
t */ |
| 6657 SQLITE_API int sqlite3_io_error_benign = 0; /* True if errors are benign
*/ |
| 6658 SQLITE_API int sqlite3_diskfull_pending = 0; |
| 6659 SQLITE_API int sqlite3_diskfull = 0; |
| 6660 #define SimulateIOErrorBenign(X) sqlite3_io_error_benign=(X) |
| 6661 #define SimulateIOError(CODE) \ |
| 6662 if( (sqlite3_io_error_persist && sqlite3_io_error_hit) \ |
| 6663 || sqlite3_io_error_pending-- == 1 ) \ |
| 6664 { local_ioerr(); CODE; } |
| 6665 static void local_ioerr(){ |
| 6666 IOTRACE(("IOERR\n")); |
| 6667 sqlite3_io_error_hit++; |
| 6668 if( !sqlite3_io_error_benign ) sqlite3_io_error_hardhit++; |
| 6669 } |
| 6670 #define SimulateDiskfullError(CODE) \ |
| 6671 if( sqlite3_diskfull_pending ){ \ |
| 6672 if( sqlite3_diskfull_pending == 1 ){ \ |
| 6673 local_ioerr(); \ |
| 6674 sqlite3_diskfull = 1; \ |
| 6675 sqlite3_io_error_hit = 1; \ |
| 6676 CODE; \ |
| 6677 }else{ \ |
| 6678 sqlite3_diskfull_pending--; \ |
| 6679 } \ |
| 6680 } |
| 6681 #else |
| 6682 #define SimulateIOErrorBenign(X) |
| 6683 #define SimulateIOError(A) |
| 6684 #define SimulateDiskfullError(A) |
| 6685 #endif |
| 6686 |
| 6687 /* |
| 6688 ** When testing, keep a count of the number of open files. |
| 6689 */ |
| 6690 #ifdef SQLITE_TEST |
| 6691 SQLITE_API int sqlite3_open_file_count = 0; |
| 6692 #define OpenCounter(X) sqlite3_open_file_count+=(X) |
| 6693 #else |
| 6694 #define OpenCounter(X) |
| 6695 #endif |
| 6696 |
| 6697 #endif /* !defined(_OS_COMMON_H_) */ |
| 6698 |
| 6699 /************** End of os_common.h *******************************************/ |
| 6700 /************** Continuing where we left off in os_unix.c ********************/ |
| 6701 |
| 6702 /* |
| 6703 ** Define various macros that are missing from some systems. |
| 6704 */ |
| 6705 #ifndef O_LARGEFILE |
| 6706 # define O_LARGEFILE 0 |
| 6707 #endif |
| 6708 #ifdef SQLITE_DISABLE_LFS |
| 6709 # undef O_LARGEFILE |
| 6710 # define O_LARGEFILE 0 |
| 6711 #endif |
| 6712 #ifndef O_NOFOLLOW |
| 6713 # define O_NOFOLLOW 0 |
| 6714 #endif |
| 6715 #ifndef O_BINARY |
| 6716 # define O_BINARY 0 |
| 6717 #endif |
| 6718 |
| 6719 /* |
| 6720 ** The threadid macro resolves to the thread-id or to 0. Used for |
| 6721 ** testing and debugging only. |
| 6722 */ |
| 6723 #if SQLITE_THREADSAFE |
| 6724 #define threadid pthread_self() |
| 6725 #else |
| 6726 #define threadid 0 |
| 6727 #endif |
| 6728 |
| 6729 /* |
| 6730 ** HAVE_MREMAP defaults to true on Linux and false everywhere else. |
| 6731 */ |
| 6732 #if !defined(HAVE_MREMAP) |
| 6733 # if defined(__linux__) && defined(_GNU_SOURCE) |
| 6734 # define HAVE_MREMAP 1 |
| 6735 # else |
| 6736 # define HAVE_MREMAP 0 |
| 6737 # endif |
| 6738 #endif |
| 6739 |
| 6740 /* |
| 6741 ** Explicitly call the 64-bit version of lseek() on Android. Otherwise, lseek() |
| 6742 ** is the 32-bit version, even if _FILE_OFFSET_BITS=64 is defined. |
| 6743 */ |
| 6744 #ifdef __ANDROID__ |
| 6745 # define lseek lseek64 |
| 6746 #endif |
| 6747 |
| 6748 /* |
| 6749 ** Different Unix systems declare open() in different ways. Same use |
| 6750 ** open(const char*,int,mode_t). Others use open(const char*,int,...). |
| 6751 ** The difference is important when using a pointer to the function. |
| 6752 ** |
| 6753 ** The safest way to deal with the problem is to always use this wrapper |
| 6754 ** which always has the same well-defined interface. |
| 6755 */ |
| 6756 static int posixOpen(const char *zFile, int flags, int mode){ |
| 6757 return open(zFile, flags, mode); |
| 6758 } |
| 6759 |
| 6760 /* Forward reference */ |
| 6761 static int openDirectory(const char*, int*); |
| 6762 static int unixGetpagesize(void); |
| 6763 |
| 6764 /* |
| 6765 ** Many system calls are accessed through pointer-to-functions so that |
| 6766 ** they may be overridden at runtime to facilitate fault injection during |
| 6767 ** testing and sandboxing. The following array holds the names and pointers |
| 6768 ** to all overrideable system calls. |
| 6769 */ |
| 6770 static struct unix_syscall { |
| 6771 const char *zName; /* Name of the system call */ |
| 6772 sqlite3_syscall_ptr pCurrent; /* Current value of the system call */ |
| 6773 sqlite3_syscall_ptr pDefault; /* Default value */ |
| 6774 } aSyscall[] = { |
| 6775 { "open", (sqlite3_syscall_ptr)posixOpen, 0 }, |
| 6776 #define osOpen ((int(*)(const char*,int,int))aSyscall[0].pCurrent) |
| 6777 |
| 6778 { "close", (sqlite3_syscall_ptr)close, 0 }, |
| 6779 #define osClose ((int(*)(int))aSyscall[1].pCurrent) |
| 6780 |
| 6781 { "access", (sqlite3_syscall_ptr)access, 0 }, |
| 6782 #define osAccess ((int(*)(const char*,int))aSyscall[2].pCurrent) |
| 6783 |
| 6784 { "getcwd", (sqlite3_syscall_ptr)getcwd, 0 }, |
| 6785 #define osGetcwd ((char*(*)(char*,size_t))aSyscall[3].pCurrent) |
| 6786 |
| 6787 { "stat", (sqlite3_syscall_ptr)stat, 0 }, |
| 6788 #define osStat ((int(*)(const char*,struct stat*))aSyscall[4].pCurrent) |
| 6789 |
| 6790 /* |
| 6791 ** The DJGPP compiler environment looks mostly like Unix, but it |
| 6792 ** lacks the fcntl() system call. So redefine fcntl() to be something |
| 6793 ** that always succeeds. This means that locking does not occur under |
| 6794 ** DJGPP. But it is DOS - what did you expect? |
| 6795 */ |
| 6796 #ifdef __DJGPP__ |
| 6797 { "fstat", 0, 0 }, |
| 6798 #define osFstat(a,b,c) 0 |
| 6799 #else |
| 6800 { "fstat", (sqlite3_syscall_ptr)fstat, 0 }, |
| 6801 #define osFstat ((int(*)(int,struct stat*))aSyscall[5].pCurrent) |
| 6802 #endif |
| 6803 |
| 6804 { "ftruncate", (sqlite3_syscall_ptr)ftruncate, 0 }, |
| 6805 #define osFtruncate ((int(*)(int,off_t))aSyscall[6].pCurrent) |
| 6806 |
| 6807 { "fcntl", (sqlite3_syscall_ptr)fcntl, 0 }, |
| 6808 #define osFcntl ((int(*)(int,int,...))aSyscall[7].pCurrent) |
| 6809 |
| 6810 { "read", (sqlite3_syscall_ptr)read, 0 }, |
| 6811 #define osRead ((ssize_t(*)(int,void*,size_t))aSyscall[8].pCurrent) |
| 6812 |
| 6813 #if defined(USE_PREAD) || SQLITE_ENABLE_LOCKING_STYLE |
| 6814 { "pread", (sqlite3_syscall_ptr)pread, 0 }, |
| 6815 #else |
| 6816 { "pread", (sqlite3_syscall_ptr)0, 0 }, |
| 6817 #endif |
| 6818 #define osPread ((ssize_t(*)(int,void*,size_t,off_t))aSyscall[9].pCurrent) |
| 6819 |
| 6820 #if defined(USE_PREAD64) |
| 6821 { "pread64", (sqlite3_syscall_ptr)pread64, 0 }, |
| 6822 #else |
| 6823 { "pread64", (sqlite3_syscall_ptr)0, 0 }, |
| 6824 #endif |
| 6825 #define osPread64 ((ssize_t(*)(int,void*,size_t,off_t))aSyscall[10].pCurrent) |
| 6826 |
| 6827 { "write", (sqlite3_syscall_ptr)write, 0 }, |
| 6828 #define osWrite ((ssize_t(*)(int,const void*,size_t))aSyscall[11].pCurrent) |
| 6829 |
| 6830 #if defined(USE_PREAD) || SQLITE_ENABLE_LOCKING_STYLE |
| 6831 { "pwrite", (sqlite3_syscall_ptr)pwrite, 0 }, |
| 6832 #else |
| 6833 { "pwrite", (sqlite3_syscall_ptr)0, 0 }, |
| 6834 #endif |
| 6835 #define osPwrite ((ssize_t(*)(int,const void*,size_t,off_t))\ |
| 6836 aSyscall[12].pCurrent) |
| 6837 |
| 6838 #if defined(USE_PREAD64) |
| 6839 { "pwrite64", (sqlite3_syscall_ptr)pwrite64, 0 }, |
| 6840 #else |
| 6841 { "pwrite64", (sqlite3_syscall_ptr)0, 0 }, |
| 6842 #endif |
| 6843 #define osPwrite64 ((ssize_t(*)(int,const void*,size_t,off_t))\ |
| 6844 aSyscall[13].pCurrent) |
| 6845 |
| 6846 { "fchmod", (sqlite3_syscall_ptr)fchmod, 0 }, |
| 6847 #define osFchmod ((int(*)(int,mode_t))aSyscall[14].pCurrent) |
| 6848 |
| 6849 #if defined(HAVE_POSIX_FALLOCATE) && HAVE_POSIX_FALLOCATE |
| 6850 { "fallocate", (sqlite3_syscall_ptr)posix_fallocate, 0 }, |
| 6851 #else |
| 6852 { "fallocate", (sqlite3_syscall_ptr)0, 0 }, |
| 6853 #endif |
| 6854 #define osFallocate ((int(*)(int,off_t,off_t))aSyscall[15].pCurrent) |
| 6855 |
| 6856 { "unlink", (sqlite3_syscall_ptr)unlink, 0 }, |
| 6857 #define osUnlink ((int(*)(const char*))aSyscall[16].pCurrent) |
| 6858 |
| 6859 { "openDirectory", (sqlite3_syscall_ptr)openDirectory, 0 }, |
| 6860 #define osOpenDirectory ((int(*)(const char*,int*))aSyscall[17].pCurrent) |
| 6861 |
| 6862 { "mkdir", (sqlite3_syscall_ptr)mkdir, 0 }, |
| 6863 #define osMkdir ((int(*)(const char*,mode_t))aSyscall[18].pCurrent) |
| 6864 |
| 6865 { "rmdir", (sqlite3_syscall_ptr)rmdir, 0 }, |
| 6866 #define osRmdir ((int(*)(const char*))aSyscall[19].pCurrent) |
| 6867 |
| 6868 { "fchown", (sqlite3_syscall_ptr)fchown, 0 }, |
| 6869 #define osFchown ((int(*)(int,uid_t,gid_t))aSyscall[20].pCurrent) |
| 6870 |
| 6871 { "geteuid", (sqlite3_syscall_ptr)geteuid, 0 }, |
| 6872 #define osGeteuid ((uid_t(*)(void))aSyscall[21].pCurrent) |
| 6873 |
| 6874 #if !defined(SQLITE_OMIT_WAL) || SQLITE_MAX_MMAP_SIZE>0 |
| 6875 { "mmap", (sqlite3_syscall_ptr)mmap, 0 }, |
| 6876 #define osMmap ((void*(*)(void*,size_t,int,int,int,off_t))aSyscall[22].pCurrent) |
| 6877 |
| 6878 { "munmap", (sqlite3_syscall_ptr)munmap, 0 }, |
| 6879 #define osMunmap ((void*(*)(void*,size_t))aSyscall[23].pCurrent) |
| 6880 |
| 6881 #if HAVE_MREMAP |
| 6882 { "mremap", (sqlite3_syscall_ptr)mremap, 0 }, |
| 6883 #else |
| 6884 { "mremap", (sqlite3_syscall_ptr)0, 0 }, |
| 6885 #endif |
| 6886 #define osMremap ((void*(*)(void*,size_t,size_t,int,...))aSyscall[24].pCurrent) |
| 6887 |
| 6888 { "getpagesize", (sqlite3_syscall_ptr)unixGetpagesize, 0 }, |
| 6889 #define osGetpagesize ((int(*)(void))aSyscall[25].pCurrent) |
| 6890 |
| 6891 { "readlink", (sqlite3_syscall_ptr)readlink, 0 }, |
| 6892 #define osReadlink ((ssize_t(*)(const char*,char*,size_t))aSyscall[26].pCurrent) |
| 6893 |
| 6894 #endif |
| 6895 |
| 6896 }; /* End of the overrideable system calls */ |
| 6897 |
| 6898 |
| 6899 /* |
| 6900 ** On some systems, calls to fchown() will trigger a message in a security |
| 6901 ** log if they come from non-root processes. So avoid calling fchown() if |
| 6902 ** we are not running as root. |
| 6903 */ |
| 6904 static int robustFchown(int fd, uid_t uid, gid_t gid){ |
| 6905 #if OS_VXWORKS |
| 6906 return 0; |
| 6907 #else |
| 6908 return osGeteuid() ? 0 : osFchown(fd,uid,gid); |
| 6909 #endif |
| 6910 } |
| 6911 |
| 6912 /* |
| 6913 ** This is the xSetSystemCall() method of sqlite3_vfs for all of the |
| 6914 ** "unix" VFSes. Return SQLITE_OK opon successfully updating the |
| 6915 ** system call pointer, or SQLITE_NOTFOUND if there is no configurable |
| 6916 ** system call named zName. |
| 6917 */ |
| 6918 static int unixSetSystemCall( |
| 6919 sqlite3_vfs *pNotUsed, /* The VFS pointer. Not used */ |
| 6920 const char *zName, /* Name of system call to override */ |
| 6921 sqlite3_syscall_ptr pNewFunc /* Pointer to new system call value */ |
| 6922 ){ |
| 6923 unsigned int i; |
| 6924 int rc = SQLITE_NOTFOUND; |
| 6925 |
| 6926 UNUSED_PARAMETER(pNotUsed); |
| 6927 if( zName==0 ){ |
| 6928 /* If no zName is given, restore all system calls to their default |
| 6929 ** settings and return NULL |
| 6930 */ |
| 6931 rc = SQLITE_OK; |
| 6932 for(i=0; i<sizeof(aSyscall)/sizeof(aSyscall[0]); i++){ |
| 6933 if( aSyscall[i].pDefault ){ |
| 6934 aSyscall[i].pCurrent = aSyscall[i].pDefault; |
| 6935 } |
| 6936 } |
| 6937 }else{ |
| 6938 /* If zName is specified, operate on only the one system call |
| 6939 ** specified. |
| 6940 */ |
| 6941 for(i=0; i<sizeof(aSyscall)/sizeof(aSyscall[0]); i++){ |
| 6942 if( strcmp(zName, aSyscall[i].zName)==0 ){ |
| 6943 if( aSyscall[i].pDefault==0 ){ |
| 6944 aSyscall[i].pDefault = aSyscall[i].pCurrent; |
| 6945 } |
| 6946 rc = SQLITE_OK; |
| 6947 if( pNewFunc==0 ) pNewFunc = aSyscall[i].pDefault; |
| 6948 aSyscall[i].pCurrent = pNewFunc; |
| 6949 break; |
| 6950 } |
| 6951 } |
| 6952 } |
| 6953 return rc; |
| 6954 } |
| 6955 |
| 6956 /* |
| 6957 ** Return the value of a system call. Return NULL if zName is not a |
| 6958 ** recognized system call name. NULL is also returned if the system call |
| 6959 ** is currently undefined. |
| 6960 */ |
| 6961 static sqlite3_syscall_ptr unixGetSystemCall( |
| 6962 sqlite3_vfs *pNotUsed, |
| 6963 const char *zName |
| 6964 ){ |
| 6965 unsigned int i; |
| 6966 |
| 6967 UNUSED_PARAMETER(pNotUsed); |
| 6968 for(i=0; i<sizeof(aSyscall)/sizeof(aSyscall[0]); i++){ |
| 6969 if( strcmp(zName, aSyscall[i].zName)==0 ) return aSyscall[i].pCurrent; |
| 6970 } |
| 6971 return 0; |
| 6972 } |
| 6973 |
| 6974 /* |
| 6975 ** Return the name of the first system call after zName. If zName==NULL |
| 6976 ** then return the name of the first system call. Return NULL if zName |
| 6977 ** is the last system call or if zName is not the name of a valid |
| 6978 ** system call. |
| 6979 */ |
| 6980 static const char *unixNextSystemCall(sqlite3_vfs *p, const char *zName){ |
| 6981 int i = -1; |
| 6982 |
| 6983 UNUSED_PARAMETER(p); |
| 6984 if( zName ){ |
| 6985 for(i=0; i<ArraySize(aSyscall)-1; i++){ |
| 6986 if( strcmp(zName, aSyscall[i].zName)==0 ) break; |
| 6987 } |
| 6988 } |
| 6989 for(i++; i<ArraySize(aSyscall); i++){ |
| 6990 if( aSyscall[i].pCurrent!=0 ) return aSyscall[i].zName; |
| 6991 } |
| 6992 return 0; |
| 6993 } |
| 6994 |
| 6995 /* |
| 6996 ** Do not accept any file descriptor less than this value, in order to avoid |
| 6997 ** opening database file using file descriptors that are commonly used for |
| 6998 ** standard input, output, and error. |
| 6999 */ |
| 7000 #ifndef SQLITE_MINIMUM_FILE_DESCRIPTOR |
| 7001 # define SQLITE_MINIMUM_FILE_DESCRIPTOR 3 |
| 7002 #endif |
| 7003 |
| 7004 /* |
| 7005 ** Invoke open(). Do so multiple times, until it either succeeds or |
| 7006 ** fails for some reason other than EINTR. |
| 7007 ** |
| 7008 ** If the file creation mode "m" is 0 then set it to the default for |
| 7009 ** SQLite. The default is SQLITE_DEFAULT_FILE_PERMISSIONS (normally |
| 7010 ** 0644) as modified by the system umask. If m is not 0, then |
| 7011 ** make the file creation mode be exactly m ignoring the umask. |
| 7012 ** |
| 7013 ** The m parameter will be non-zero only when creating -wal, -journal, |
| 7014 ** and -shm files. We want those files to have *exactly* the same |
| 7015 ** permissions as their original database, unadulterated by the umask. |
| 7016 ** In that way, if a database file is -rw-rw-rw or -rw-rw-r-, and a |
| 7017 ** transaction crashes and leaves behind hot journals, then any |
| 7018 ** process that is able to write to the database will also be able to |
| 7019 ** recover the hot journals. |
| 7020 */ |
| 7021 static int robust_open(const char *z, int f, mode_t m){ |
| 7022 int fd; |
| 7023 mode_t m2 = m ? m : SQLITE_DEFAULT_FILE_PERMISSIONS; |
| 7024 while(1){ |
| 7025 #if defined(O_CLOEXEC) |
| 7026 fd = osOpen(z,f|O_CLOEXEC,m2); |
| 7027 #else |
| 7028 fd = osOpen(z,f,m2); |
| 7029 #endif |
| 7030 if( fd<0 ){ |
| 7031 if( errno==EINTR ) continue; |
| 7032 break; |
| 7033 } |
| 7034 if( fd>=SQLITE_MINIMUM_FILE_DESCRIPTOR ) break; |
| 7035 osClose(fd); |
| 7036 sqlite3_log(SQLITE_WARNING, |
| 7037 "attempt to open \"%s\" as file descriptor %d", z, fd); |
| 7038 fd = -1; |
| 7039 if( osOpen("/dev/null", f, m)<0 ) break; |
| 7040 } |
| 7041 if( fd>=0 ){ |
| 7042 if( m!=0 ){ |
| 7043 struct stat statbuf; |
| 7044 if( osFstat(fd, &statbuf)==0 |
| 7045 && statbuf.st_size==0 |
| 7046 && (statbuf.st_mode&0777)!=m |
| 7047 ){ |
| 7048 osFchmod(fd, m); |
| 7049 } |
| 7050 } |
| 7051 #if defined(FD_CLOEXEC) && (!defined(O_CLOEXEC) || O_CLOEXEC==0) |
| 7052 osFcntl(fd, F_SETFD, osFcntl(fd, F_GETFD, 0) | FD_CLOEXEC); |
| 7053 #endif |
| 7054 } |
| 7055 return fd; |
| 7056 } |
| 7057 |
| 7058 /* |
| 7059 ** Helper functions to obtain and relinquish the global mutex. The |
| 7060 ** global mutex is used to protect the unixInodeInfo and |
| 7061 ** vxworksFileId objects used by this file, all of which may be |
| 7062 ** shared by multiple threads. |
| 7063 ** |
| 7064 ** Function unixMutexHeld() is used to assert() that the global mutex |
| 7065 ** is held when required. This function is only used as part of assert() |
| 7066 ** statements. e.g. |
| 7067 ** |
| 7068 ** unixEnterMutex() |
| 7069 ** assert( unixMutexHeld() ); |
| 7070 ** unixEnterLeave() |
| 7071 */ |
| 7072 static void unixEnterMutex(void){ |
| 7073 sqlite3_mutex_enter(sqlite3MutexAlloc(SQLITE_MUTEX_STATIC_VFS1)); |
| 7074 } |
| 7075 static void unixLeaveMutex(void){ |
| 7076 sqlite3_mutex_leave(sqlite3MutexAlloc(SQLITE_MUTEX_STATIC_VFS1)); |
| 7077 } |
| 7078 #ifdef SQLITE_DEBUG |
| 7079 static int unixMutexHeld(void) { |
| 7080 return sqlite3_mutex_held(sqlite3MutexAlloc(SQLITE_MUTEX_STATIC_VFS1)); |
| 7081 } |
| 7082 #endif |
| 7083 |
| 7084 |
| 7085 #ifdef SQLITE_HAVE_OS_TRACE |
| 7086 /* |
| 7087 ** Helper function for printing out trace information from debugging |
| 7088 ** binaries. This returns the string representation of the supplied |
| 7089 ** integer lock-type. |
| 7090 */ |
| 7091 static const char *azFileLock(int eFileLock){ |
| 7092 switch( eFileLock ){ |
| 7093 case NO_LOCK: return "NONE"; |
| 7094 case SHARED_LOCK: return "SHARED"; |
| 7095 case RESERVED_LOCK: return "RESERVED"; |
| 7096 case PENDING_LOCK: return "PENDING"; |
| 7097 case EXCLUSIVE_LOCK: return "EXCLUSIVE"; |
| 7098 } |
| 7099 return "ERROR"; |
| 7100 } |
| 7101 #endif |
| 7102 |
| 7103 #ifdef SQLITE_LOCK_TRACE |
| 7104 /* |
| 7105 ** Print out information about all locking operations. |
| 7106 ** |
| 7107 ** This routine is used for troubleshooting locks on multithreaded |
| 7108 ** platforms. Enable by compiling with the -DSQLITE_LOCK_TRACE |
| 7109 ** command-line option on the compiler. This code is normally |
| 7110 ** turned off. |
| 7111 */ |
| 7112 static int lockTrace(int fd, int op, struct flock *p){ |
| 7113 char *zOpName, *zType; |
| 7114 int s; |
| 7115 int savedErrno; |
| 7116 if( op==F_GETLK ){ |
| 7117 zOpName = "GETLK"; |
| 7118 }else if( op==F_SETLK ){ |
| 7119 zOpName = "SETLK"; |
| 7120 }else{ |
| 7121 s = osFcntl(fd, op, p); |
| 7122 sqlite3DebugPrintf("fcntl unknown %d %d %d\n", fd, op, s); |
| 7123 return s; |
| 7124 } |
| 7125 if( p->l_type==F_RDLCK ){ |
| 7126 zType = "RDLCK"; |
| 7127 }else if( p->l_type==F_WRLCK ){ |
| 7128 zType = "WRLCK"; |
| 7129 }else if( p->l_type==F_UNLCK ){ |
| 7130 zType = "UNLCK"; |
| 7131 }else{ |
| 7132 assert( 0 ); |
| 7133 } |
| 7134 assert( p->l_whence==SEEK_SET ); |
| 7135 s = osFcntl(fd, op, p); |
| 7136 savedErrno = errno; |
| 7137 sqlite3DebugPrintf("fcntl %d %d %s %s %d %d %d %d\n", |
| 7138 threadid, fd, zOpName, zType, (int)p->l_start, (int)p->l_len, |
| 7139 (int)p->l_pid, s); |
| 7140 if( s==(-1) && op==F_SETLK && (p->l_type==F_RDLCK || p->l_type==F_WRLCK) ){ |
| 7141 struct flock l2; |
| 7142 l2 = *p; |
| 7143 osFcntl(fd, F_GETLK, &l2); |
| 7144 if( l2.l_type==F_RDLCK ){ |
| 7145 zType = "RDLCK"; |
| 7146 }else if( l2.l_type==F_WRLCK ){ |
| 7147 zType = "WRLCK"; |
| 7148 }else if( l2.l_type==F_UNLCK ){ |
| 7149 zType = "UNLCK"; |
| 7150 }else{ |
| 7151 assert( 0 ); |
| 7152 } |
| 7153 sqlite3DebugPrintf("fcntl-failure-reason: %s %d %d %d\n", |
| 7154 zType, (int)l2.l_start, (int)l2.l_len, (int)l2.l_pid); |
| 7155 } |
| 7156 errno = savedErrno; |
| 7157 return s; |
| 7158 } |
| 7159 #undef osFcntl |
| 7160 #define osFcntl lockTrace |
| 7161 #endif /* SQLITE_LOCK_TRACE */ |
| 7162 |
| 7163 /* |
| 7164 ** Retry ftruncate() calls that fail due to EINTR |
| 7165 ** |
| 7166 ** All calls to ftruncate() within this file should be made through |
| 7167 ** this wrapper. On the Android platform, bypassing the logic below |
| 7168 ** could lead to a corrupt database. |
| 7169 */ |
| 7170 static int robust_ftruncate(int h, sqlite3_int64 sz){ |
| 7171 int rc; |
| 7172 #ifdef __ANDROID__ |
| 7173 /* On Android, ftruncate() always uses 32-bit offsets, even if |
| 7174 ** _FILE_OFFSET_BITS=64 is defined. This means it is unsafe to attempt to |
| 7175 ** truncate a file to any size larger than 2GiB. Silently ignore any |
| 7176 ** such attempts. */ |
| 7177 if( sz>(sqlite3_int64)0x7FFFFFFF ){ |
| 7178 rc = SQLITE_OK; |
| 7179 }else |
| 7180 #endif |
| 7181 do{ rc = osFtruncate(h,sz); }while( rc<0 && errno==EINTR ); |
| 7182 return rc; |
| 7183 } |
| 7184 |
| 7185 /* |
| 7186 ** This routine translates a standard POSIX errno code into something |
| 7187 ** useful to the clients of the sqlite3 functions. Specifically, it is |
| 7188 ** intended to translate a variety of "try again" errors into SQLITE_BUSY |
| 7189 ** and a variety of "please close the file descriptor NOW" errors into |
| 7190 ** SQLITE_IOERR |
| 7191 ** |
| 7192 ** Errors during initialization of locks, or file system support for locks, |
| 7193 ** should handle ENOLCK, ENOTSUP, EOPNOTSUPP separately. |
| 7194 */ |
| 7195 static int sqliteErrorFromPosixError(int posixError, int sqliteIOErr) { |
| 7196 assert( (sqliteIOErr == SQLITE_IOERR_LOCK) || |
| 7197 (sqliteIOErr == SQLITE_IOERR_UNLOCK) || |
| 7198 (sqliteIOErr == SQLITE_IOERR_RDLOCK) || |
| 7199 (sqliteIOErr == SQLITE_IOERR_CHECKRESERVEDLOCK) ); |
| 7200 switch (posixError) { |
| 7201 case EACCES: |
| 7202 case EAGAIN: |
| 7203 case ETIMEDOUT: |
| 7204 case EBUSY: |
| 7205 case EINTR: |
| 7206 case ENOLCK: |
| 7207 /* random NFS retry error, unless during file system support |
| 7208 * introspection, in which it actually means what it says */ |
| 7209 return SQLITE_BUSY; |
| 7210 |
| 7211 case EPERM: |
| 7212 return SQLITE_PERM; |
| 7213 |
| 7214 default: |
| 7215 return sqliteIOErr; |
| 7216 } |
| 7217 } |
| 7218 |
| 7219 |
| 7220 /****************************************************************************** |
| 7221 ****************** Begin Unique File ID Utility Used By VxWorks *************** |
| 7222 ** |
| 7223 ** On most versions of unix, we can get a unique ID for a file by concatenating |
| 7224 ** the device number and the inode number. But this does not work on VxWorks. |
| 7225 ** On VxWorks, a unique file id must be based on the canonical filename. |
| 7226 ** |
| 7227 ** A pointer to an instance of the following structure can be used as a |
| 7228 ** unique file ID in VxWorks. Each instance of this structure contains |
| 7229 ** a copy of the canonical filename. There is also a reference count. |
| 7230 ** The structure is reclaimed when the number of pointers to it drops to |
| 7231 ** zero. |
| 7232 ** |
| 7233 ** There are never very many files open at one time and lookups are not |
| 7234 ** a performance-critical path, so it is sufficient to put these |
| 7235 ** structures on a linked list. |
| 7236 */ |
| 7237 struct vxworksFileId { |
| 7238 struct vxworksFileId *pNext; /* Next in a list of them all */ |
| 7239 int nRef; /* Number of references to this one */ |
| 7240 int nName; /* Length of the zCanonicalName[] string */ |
| 7241 char *zCanonicalName; /* Canonical filename */ |
| 7242 }; |
| 7243 |
| 7244 #if OS_VXWORKS |
| 7245 /* |
| 7246 ** All unique filenames are held on a linked list headed by this |
| 7247 ** variable: |
| 7248 */ |
| 7249 static struct vxworksFileId *vxworksFileList = 0; |
| 7250 |
| 7251 /* |
| 7252 ** Simplify a filename into its canonical form |
| 7253 ** by making the following changes: |
| 7254 ** |
| 7255 ** * removing any trailing and duplicate / |
| 7256 ** * convert /./ into just / |
| 7257 ** * convert /A/../ where A is any simple name into just / |
| 7258 ** |
| 7259 ** Changes are made in-place. Return the new name length. |
| 7260 ** |
| 7261 ** The original filename is in z[0..n-1]. Return the number of |
| 7262 ** characters in the simplified name. |
| 7263 */ |
| 7264 static int vxworksSimplifyName(char *z, int n){ |
| 7265 int i, j; |
| 7266 while( n>1 && z[n-1]=='/' ){ n--; } |
| 7267 for(i=j=0; i<n; i++){ |
| 7268 if( z[i]=='/' ){ |
| 7269 if( z[i+1]=='/' ) continue; |
| 7270 if( z[i+1]=='.' && i+2<n && z[i+2]=='/' ){ |
| 7271 i += 1; |
| 7272 continue; |
| 7273 } |
| 7274 if( z[i+1]=='.' && i+3<n && z[i+2]=='.' && z[i+3]=='/' ){ |
| 7275 while( j>0 && z[j-1]!='/' ){ j--; } |
| 7276 if( j>0 ){ j--; } |
| 7277 i += 2; |
| 7278 continue; |
| 7279 } |
| 7280 } |
| 7281 z[j++] = z[i]; |
| 7282 } |
| 7283 z[j] = 0; |
| 7284 return j; |
| 7285 } |
| 7286 |
| 7287 /* |
| 7288 ** Find a unique file ID for the given absolute pathname. Return |
| 7289 ** a pointer to the vxworksFileId object. This pointer is the unique |
| 7290 ** file ID. |
| 7291 ** |
| 7292 ** The nRef field of the vxworksFileId object is incremented before |
| 7293 ** the object is returned. A new vxworksFileId object is created |
| 7294 ** and added to the global list if necessary. |
| 7295 ** |
| 7296 ** If a memory allocation error occurs, return NULL. |
| 7297 */ |
| 7298 static struct vxworksFileId *vxworksFindFileId(const char *zAbsoluteName){ |
| 7299 struct vxworksFileId *pNew; /* search key and new file ID */ |
| 7300 struct vxworksFileId *pCandidate; /* For looping over existing file IDs */ |
| 7301 int n; /* Length of zAbsoluteName string */ |
| 7302 |
| 7303 assert( zAbsoluteName[0]=='/' ); |
| 7304 n = (int)strlen(zAbsoluteName); |
| 7305 pNew = sqlite3_malloc64( sizeof(*pNew) + (n+1) ); |
| 7306 if( pNew==0 ) return 0; |
| 7307 pNew->zCanonicalName = (char*)&pNew[1]; |
| 7308 memcpy(pNew->zCanonicalName, zAbsoluteName, n+1); |
| 7309 n = vxworksSimplifyName(pNew->zCanonicalName, n); |
| 7310 |
| 7311 /* Search for an existing entry that matching the canonical name. |
| 7312 ** If found, increment the reference count and return a pointer to |
| 7313 ** the existing file ID. |
| 7314 */ |
| 7315 unixEnterMutex(); |
| 7316 for(pCandidate=vxworksFileList; pCandidate; pCandidate=pCandidate->pNext){ |
| 7317 if( pCandidate->nName==n |
| 7318 && memcmp(pCandidate->zCanonicalName, pNew->zCanonicalName, n)==0 |
| 7319 ){ |
| 7320 sqlite3_free(pNew); |
| 7321 pCandidate->nRef++; |
| 7322 unixLeaveMutex(); |
| 7323 return pCandidate; |
| 7324 } |
| 7325 } |
| 7326 |
| 7327 /* No match was found. We will make a new file ID */ |
| 7328 pNew->nRef = 1; |
| 7329 pNew->nName = n; |
| 7330 pNew->pNext = vxworksFileList; |
| 7331 vxworksFileList = pNew; |
| 7332 unixLeaveMutex(); |
| 7333 return pNew; |
| 7334 } |
| 7335 |
| 7336 /* |
| 7337 ** Decrement the reference count on a vxworksFileId object. Free |
| 7338 ** the object when the reference count reaches zero. |
| 7339 */ |
| 7340 static void vxworksReleaseFileId(struct vxworksFileId *pId){ |
| 7341 unixEnterMutex(); |
| 7342 assert( pId->nRef>0 ); |
| 7343 pId->nRef--; |
| 7344 if( pId->nRef==0 ){ |
| 7345 struct vxworksFileId **pp; |
| 7346 for(pp=&vxworksFileList; *pp && *pp!=pId; pp = &((*pp)->pNext)){} |
| 7347 assert( *pp==pId ); |
| 7348 *pp = pId->pNext; |
| 7349 sqlite3_free(pId); |
| 7350 } |
| 7351 unixLeaveMutex(); |
| 7352 } |
| 7353 #endif /* OS_VXWORKS */ |
| 7354 /*************** End of Unique File ID Utility Used By VxWorks **************** |
| 7355 ******************************************************************************/ |
| 7356 |
| 7357 |
| 7358 /****************************************************************************** |
| 7359 *************************** Posix Advisory Locking **************************** |
| 7360 ** |
| 7361 ** POSIX advisory locks are broken by design. ANSI STD 1003.1 (1996) |
| 7362 ** section 6.5.2.2 lines 483 through 490 specify that when a process |
| 7363 ** sets or clears a lock, that operation overrides any prior locks set |
| 7364 ** by the same process. It does not explicitly say so, but this implies |
| 7365 ** that it overrides locks set by the same process using a different |
| 7366 ** file descriptor. Consider this test case: |
| 7367 ** |
| 7368 ** int fd1 = open("./file1", O_RDWR|O_CREAT, 0644); |
| 7369 ** int fd2 = open("./file2", O_RDWR|O_CREAT, 0644); |
| 7370 ** |
| 7371 ** Suppose ./file1 and ./file2 are really the same file (because |
| 7372 ** one is a hard or symbolic link to the other) then if you set |
| 7373 ** an exclusive lock on fd1, then try to get an exclusive lock |
| 7374 ** on fd2, it works. I would have expected the second lock to |
| 7375 ** fail since there was already a lock on the file due to fd1. |
| 7376 ** But not so. Since both locks came from the same process, the |
| 7377 ** second overrides the first, even though they were on different |
| 7378 ** file descriptors opened on different file names. |
| 7379 ** |
| 7380 ** This means that we cannot use POSIX locks to synchronize file access |
| 7381 ** among competing threads of the same process. POSIX locks will work fine |
| 7382 ** to synchronize access for threads in separate processes, but not |
| 7383 ** threads within the same process. |
| 7384 ** |
| 7385 ** To work around the problem, SQLite has to manage file locks internally |
| 7386 ** on its own. Whenever a new database is opened, we have to find the |
| 7387 ** specific inode of the database file (the inode is determined by the |
| 7388 ** st_dev and st_ino fields of the stat structure that fstat() fills in) |
| 7389 ** and check for locks already existing on that inode. When locks are |
| 7390 ** created or removed, we have to look at our own internal record of the |
| 7391 ** locks to see if another thread has previously set a lock on that same |
| 7392 ** inode. |
| 7393 ** |
| 7394 ** (Aside: The use of inode numbers as unique IDs does not work on VxWorks. |
| 7395 ** For VxWorks, we have to use the alternative unique ID system based on |
| 7396 ** canonical filename and implemented in the previous division.) |
| 7397 ** |
| 7398 ** The sqlite3_file structure for POSIX is no longer just an integer file |
| 7399 ** descriptor. It is now a structure that holds the integer file |
| 7400 ** descriptor and a pointer to a structure that describes the internal |
| 7401 ** locks on the corresponding inode. There is one locking structure |
| 7402 ** per inode, so if the same inode is opened twice, both unixFile structures |
| 7403 ** point to the same locking structure. The locking structure keeps |
| 7404 ** a reference count (so we will know when to delete it) and a "cnt" |
| 7405 ** field that tells us its internal lock status. cnt==0 means the |
| 7406 ** file is unlocked. cnt==-1 means the file has an exclusive lock. |
| 7407 ** cnt>0 means there are cnt shared locks on the file. |
| 7408 ** |
| 7409 ** Any attempt to lock or unlock a file first checks the locking |
| 7410 ** structure. The fcntl() system call is only invoked to set a |
| 7411 ** POSIX lock if the internal lock structure transitions between |
| 7412 ** a locked and an unlocked state. |
| 7413 ** |
| 7414 ** But wait: there are yet more problems with POSIX advisory locks. |
| 7415 ** |
| 7416 ** If you close a file descriptor that points to a file that has locks, |
| 7417 ** all locks on that file that are owned by the current process are |
| 7418 ** released. To work around this problem, each unixInodeInfo object |
| 7419 ** maintains a count of the number of pending locks on tha inode. |
| 7420 ** When an attempt is made to close an unixFile, if there are |
| 7421 ** other unixFile open on the same inode that are holding locks, the call |
| 7422 ** to close() the file descriptor is deferred until all of the locks clear. |
| 7423 ** The unixInodeInfo structure keeps a list of file descriptors that need to |
| 7424 ** be closed and that list is walked (and cleared) when the last lock |
| 7425 ** clears. |
| 7426 ** |
| 7427 ** Yet another problem: LinuxThreads do not play well with posix locks. |
| 7428 ** |
| 7429 ** Many older versions of linux use the LinuxThreads library which is |
| 7430 ** not posix compliant. Under LinuxThreads, a lock created by thread |
| 7431 ** A cannot be modified or overridden by a different thread B. |
| 7432 ** Only thread A can modify the lock. Locking behavior is correct |
| 7433 ** if the appliation uses the newer Native Posix Thread Library (NPTL) |
| 7434 ** on linux - with NPTL a lock created by thread A can override locks |
| 7435 ** in thread B. But there is no way to know at compile-time which |
| 7436 ** threading library is being used. So there is no way to know at |
| 7437 ** compile-time whether or not thread A can override locks on thread B. |
| 7438 ** One has to do a run-time check to discover the behavior of the |
| 7439 ** current process. |
| 7440 ** |
| 7441 ** SQLite used to support LinuxThreads. But support for LinuxThreads |
| 7442 ** was dropped beginning with version 3.7.0. SQLite will still work with |
| 7443 ** LinuxThreads provided that (1) there is no more than one connection |
| 7444 ** per database file in the same process and (2) database connections |
| 7445 ** do not move across threads. |
| 7446 */ |
| 7447 |
| 7448 /* |
| 7449 ** An instance of the following structure serves as the key used |
| 7450 ** to locate a particular unixInodeInfo object. |
| 7451 */ |
| 7452 struct unixFileId { |
| 7453 dev_t dev; /* Device number */ |
| 7454 #if OS_VXWORKS |
| 7455 struct vxworksFileId *pId; /* Unique file ID for vxworks. */ |
| 7456 #else |
| 7457 ino_t ino; /* Inode number */ |
| 7458 #endif |
| 7459 }; |
| 7460 |
| 7461 /* |
| 7462 ** An instance of the following structure is allocated for each open |
| 7463 ** inode. Or, on LinuxThreads, there is one of these structures for |
| 7464 ** each inode opened by each thread. |
| 7465 ** |
| 7466 ** A single inode can have multiple file descriptors, so each unixFile |
| 7467 ** structure contains a pointer to an instance of this object and this |
| 7468 ** object keeps a count of the number of unixFile pointing to it. |
| 7469 */ |
| 7470 struct unixInodeInfo { |
| 7471 struct unixFileId fileId; /* The lookup key */ |
| 7472 int nShared; /* Number of SHARED locks held */ |
| 7473 unsigned char eFileLock; /* One of SHARED_LOCK, RESERVED_LOCK etc. */ |
| 7474 unsigned char bProcessLock; /* An exclusive process lock is held */ |
| 7475 int nRef; /* Number of pointers to this structure */ |
| 7476 unixShmNode *pShmNode; /* Shared memory associated with this inode */ |
| 7477 int nLock; /* Number of outstanding file locks */ |
| 7478 UnixUnusedFd *pUnused; /* Unused file descriptors to close */ |
| 7479 unixInodeInfo *pNext; /* List of all unixInodeInfo objects */ |
| 7480 unixInodeInfo *pPrev; /* .... doubly linked */ |
| 7481 #if SQLITE_ENABLE_LOCKING_STYLE |
| 7482 unsigned long long sharedByte; /* for AFP simulated shared lock */ |
| 7483 #endif |
| 7484 #if OS_VXWORKS |
| 7485 sem_t *pSem; /* Named POSIX semaphore */ |
| 7486 char aSemName[MAX_PATHNAME+2]; /* Name of that semaphore */ |
| 7487 #endif |
| 7488 }; |
| 7489 |
| 7490 /* |
| 7491 ** A lists of all unixInodeInfo objects. |
| 7492 */ |
| 7493 static unixInodeInfo *inodeList = 0; |
| 7494 |
| 7495 /* |
| 7496 ** |
| 7497 ** This function - unixLogErrorAtLine(), is only ever called via the macro |
| 7498 ** unixLogError(). |
| 7499 ** |
| 7500 ** It is invoked after an error occurs in an OS function and errno has been |
| 7501 ** set. It logs a message using sqlite3_log() containing the current value of |
| 7502 ** errno and, if possible, the human-readable equivalent from strerror() or |
| 7503 ** strerror_r(). |
| 7504 ** |
| 7505 ** The first argument passed to the macro should be the error code that |
| 7506 ** will be returned to SQLite (e.g. SQLITE_IOERR_DELETE, SQLITE_CANTOPEN). |
| 7507 ** The two subsequent arguments should be the name of the OS function that |
| 7508 ** failed (e.g. "unlink", "open") and the associated file-system path, |
| 7509 ** if any. |
| 7510 */ |
| 7511 #define unixLogError(a,b,c) unixLogErrorAtLine(a,b,c,__LINE__) |
| 7512 static int unixLogErrorAtLine( |
| 7513 int errcode, /* SQLite error code */ |
| 7514 const char *zFunc, /* Name of OS function that failed */ |
| 7515 const char *zPath, /* File path associated with error */ |
| 7516 int iLine /* Source line number where error occurred */ |
| 7517 ){ |
| 7518 char *zErr; /* Message from strerror() or equivalent */ |
| 7519 int iErrno = errno; /* Saved syscall error number */ |
| 7520 |
| 7521 /* If this is not a threadsafe build (SQLITE_THREADSAFE==0), then use |
| 7522 ** the strerror() function to obtain the human-readable error message |
| 7523 ** equivalent to errno. Otherwise, use strerror_r(). |
| 7524 */ |
| 7525 #if SQLITE_THREADSAFE && defined(HAVE_STRERROR_R) |
| 7526 char aErr[80]; |
| 7527 memset(aErr, 0, sizeof(aErr)); |
| 7528 zErr = aErr; |
| 7529 |
| 7530 /* If STRERROR_R_CHAR_P (set by autoconf scripts) or __USE_GNU is defined, |
| 7531 ** assume that the system provides the GNU version of strerror_r() that |
| 7532 ** returns a pointer to a buffer containing the error message. That pointer |
| 7533 ** may point to aErr[], or it may point to some static storage somewhere. |
| 7534 ** Otherwise, assume that the system provides the POSIX version of |
| 7535 ** strerror_r(), which always writes an error message into aErr[]. |
| 7536 ** |
| 7537 ** If the code incorrectly assumes that it is the POSIX version that is |
| 7538 ** available, the error message will often be an empty string. Not a |
| 7539 ** huge problem. Incorrectly concluding that the GNU version is available |
| 7540 ** could lead to a segfault though. |
| 7541 */ |
| 7542 #if defined(STRERROR_R_CHAR_P) || defined(__USE_GNU) |
| 7543 zErr = |
| 7544 # endif |
| 7545 strerror_r(iErrno, aErr, sizeof(aErr)-1); |
| 7546 |
| 7547 #elif SQLITE_THREADSAFE |
| 7548 /* This is a threadsafe build, but strerror_r() is not available. */ |
| 7549 zErr = ""; |
| 7550 #else |
| 7551 /* Non-threadsafe build, use strerror(). */ |
| 7552 zErr = strerror(iErrno); |
| 7553 #endif |
| 7554 |
| 7555 if( zPath==0 ) zPath = ""; |
| 7556 sqlite3_log(errcode, |
| 7557 "os_unix.c:%d: (%d) %s(%s) - %s", |
| 7558 iLine, iErrno, zFunc, zPath, zErr |
| 7559 ); |
| 7560 |
| 7561 return errcode; |
| 7562 } |
| 7563 |
| 7564 /* |
| 7565 ** Close a file descriptor. |
| 7566 ** |
| 7567 ** We assume that close() almost always works, since it is only in a |
| 7568 ** very sick application or on a very sick platform that it might fail. |
| 7569 ** If it does fail, simply leak the file descriptor, but do log the |
| 7570 ** error. |
| 7571 ** |
| 7572 ** Note that it is not safe to retry close() after EINTR since the |
| 7573 ** file descriptor might have already been reused by another thread. |
| 7574 ** So we don't even try to recover from an EINTR. Just log the error |
| 7575 ** and move on. |
| 7576 */ |
| 7577 static void robust_close(unixFile *pFile, int h, int lineno){ |
| 7578 if( osClose(h) ){ |
| 7579 unixLogErrorAtLine(SQLITE_IOERR_CLOSE, "close", |
| 7580 pFile ? pFile->zPath : 0, lineno); |
| 7581 } |
| 7582 } |
| 7583 |
| 7584 /* |
| 7585 ** Set the pFile->lastErrno. Do this in a subroutine as that provides |
| 7586 ** a convenient place to set a breakpoint. |
| 7587 */ |
| 7588 static void storeLastErrno(unixFile *pFile, int error){ |
| 7589 pFile->lastErrno = error; |
| 7590 } |
| 7591 |
| 7592 /* |
| 7593 ** Close all file descriptors accumuated in the unixInodeInfo->pUnused list. |
| 7594 */ |
| 7595 static void closePendingFds(unixFile *pFile){ |
| 7596 unixInodeInfo *pInode = pFile->pInode; |
| 7597 UnixUnusedFd *p; |
| 7598 UnixUnusedFd *pNext; |
| 7599 for(p=pInode->pUnused; p; p=pNext){ |
| 7600 pNext = p->pNext; |
| 7601 robust_close(pFile, p->fd, __LINE__); |
| 7602 sqlite3_free(p); |
| 7603 } |
| 7604 pInode->pUnused = 0; |
| 7605 } |
| 7606 |
| 7607 /* |
| 7608 ** Release a unixInodeInfo structure previously allocated by findInodeInfo(). |
| 7609 ** |
| 7610 ** The mutex entered using the unixEnterMutex() function must be held |
| 7611 ** when this function is called. |
| 7612 */ |
| 7613 static void releaseInodeInfo(unixFile *pFile){ |
| 7614 unixInodeInfo *pInode = pFile->pInode; |
| 7615 assert( unixMutexHeld() ); |
| 7616 if( ALWAYS(pInode) ){ |
| 7617 pInode->nRef--; |
| 7618 if( pInode->nRef==0 ){ |
| 7619 assert( pInode->pShmNode==0 ); |
| 7620 closePendingFds(pFile); |
| 7621 if( pInode->pPrev ){ |
| 7622 assert( pInode->pPrev->pNext==pInode ); |
| 7623 pInode->pPrev->pNext = pInode->pNext; |
| 7624 }else{ |
| 7625 assert( inodeList==pInode ); |
| 7626 inodeList = pInode->pNext; |
| 7627 } |
| 7628 if( pInode->pNext ){ |
| 7629 assert( pInode->pNext->pPrev==pInode ); |
| 7630 pInode->pNext->pPrev = pInode->pPrev; |
| 7631 } |
| 7632 sqlite3_free(pInode); |
| 7633 } |
| 7634 } |
| 7635 } |
| 7636 |
| 7637 /* |
| 7638 ** Given a file descriptor, locate the unixInodeInfo object that |
| 7639 ** describes that file descriptor. Create a new one if necessary. The |
| 7640 ** return value might be uninitialized if an error occurs. |
| 7641 ** |
| 7642 ** The mutex entered using the unixEnterMutex() function must be held |
| 7643 ** when this function is called. |
| 7644 ** |
| 7645 ** Return an appropriate error code. |
| 7646 */ |
| 7647 static int findInodeInfo( |
| 7648 unixFile *pFile, /* Unix file with file desc used in the key */ |
| 7649 unixInodeInfo **ppInode /* Return the unixInodeInfo object here */ |
| 7650 ){ |
| 7651 int rc; /* System call return code */ |
| 7652 int fd; /* The file descriptor for pFile */ |
| 7653 struct unixFileId fileId; /* Lookup key for the unixInodeInfo */ |
| 7654 struct stat statbuf; /* Low-level file information */ |
| 7655 unixInodeInfo *pInode = 0; /* Candidate unixInodeInfo object */ |
| 7656 |
| 7657 assert( unixMutexHeld() ); |
| 7658 |
| 7659 /* Get low-level information about the file that we can used to |
| 7660 ** create a unique name for the file. |
| 7661 */ |
| 7662 fd = pFile->h; |
| 7663 rc = osFstat(fd, &statbuf); |
| 7664 if( rc!=0 ){ |
| 7665 storeLastErrno(pFile, errno); |
| 7666 #if defined(EOVERFLOW) && defined(SQLITE_DISABLE_LFS) |
| 7667 if( pFile->lastErrno==EOVERFLOW ) return SQLITE_NOLFS; |
| 7668 #endif |
| 7669 return SQLITE_IOERR; |
| 7670 } |
| 7671 |
| 7672 #ifdef __APPLE__ |
| 7673 /* On OS X on an msdos filesystem, the inode number is reported |
| 7674 ** incorrectly for zero-size files. See ticket #3260. To work |
| 7675 ** around this problem (we consider it a bug in OS X, not SQLite) |
| 7676 ** we always increase the file size to 1 by writing a single byte |
| 7677 ** prior to accessing the inode number. The one byte written is |
| 7678 ** an ASCII 'S' character which also happens to be the first byte |
| 7679 ** in the header of every SQLite database. In this way, if there |
| 7680 ** is a race condition such that another thread has already populated |
| 7681 ** the first page of the database, no damage is done. |
| 7682 */ |
| 7683 if( statbuf.st_size==0 && (pFile->fsFlags & SQLITE_FSFLAGS_IS_MSDOS)!=0 ){ |
| 7684 do{ rc = osWrite(fd, "S", 1); }while( rc<0 && errno==EINTR ); |
| 7685 if( rc!=1 ){ |
| 7686 storeLastErrno(pFile, errno); |
| 7687 return SQLITE_IOERR; |
| 7688 } |
| 7689 rc = osFstat(fd, &statbuf); |
| 7690 if( rc!=0 ){ |
| 7691 storeLastErrno(pFile, errno); |
| 7692 return SQLITE_IOERR; |
| 7693 } |
| 7694 } |
| 7695 #endif |
| 7696 |
| 7697 memset(&fileId, 0, sizeof(fileId)); |
| 7698 fileId.dev = statbuf.st_dev; |
| 7699 #if OS_VXWORKS |
| 7700 fileId.pId = pFile->pId; |
| 7701 #else |
| 7702 fileId.ino = statbuf.st_ino; |
| 7703 #endif |
| 7704 pInode = inodeList; |
| 7705 while( pInode && memcmp(&fileId, &pInode->fileId, sizeof(fileId)) ){ |
| 7706 pInode = pInode->pNext; |
| 7707 } |
| 7708 if( pInode==0 ){ |
| 7709 pInode = sqlite3_malloc64( sizeof(*pInode) ); |
| 7710 if( pInode==0 ){ |
| 7711 return SQLITE_NOMEM; |
| 7712 } |
| 7713 memset(pInode, 0, sizeof(*pInode)); |
| 7714 memcpy(&pInode->fileId, &fileId, sizeof(fileId)); |
| 7715 pInode->nRef = 1; |
| 7716 pInode->pNext = inodeList; |
| 7717 pInode->pPrev = 0; |
| 7718 if( inodeList ) inodeList->pPrev = pInode; |
| 7719 inodeList = pInode; |
| 7720 }else{ |
| 7721 pInode->nRef++; |
| 7722 } |
| 7723 *ppInode = pInode; |
| 7724 return SQLITE_OK; |
| 7725 } |
| 7726 |
| 7727 /* |
| 7728 ** Return TRUE if pFile has been renamed or unlinked since it was first opened. |
| 7729 */ |
| 7730 static int fileHasMoved(unixFile *pFile){ |
| 7731 #if OS_VXWORKS |
| 7732 return pFile->pInode!=0 && pFile->pId!=pFile->pInode->fileId.pId; |
| 7733 #else |
| 7734 struct stat buf; |
| 7735 |
| 7736 /* TODO(shess): This check doesn't work when the Chromium's WebDB code is |
| 7737 ** running in the sandbox. |
| 7738 */ |
| 7739 return 0; |
| 7740 |
| 7741 return pFile->pInode!=0 && |
| 7742 (osStat(pFile->zPath, &buf)!=0 || buf.st_ino!=pFile->pInode->fileId.ino); |
| 7743 #endif |
| 7744 } |
| 7745 |
| 7746 |
| 7747 /* |
| 7748 ** Check a unixFile that is a database. Verify the following: |
| 7749 ** |
| 7750 ** (1) There is exactly one hard link on the file |
| 7751 ** (2) The file is not a symbolic link |
| 7752 ** (3) The file has not been renamed or unlinked |
| 7753 ** |
| 7754 ** Issue sqlite3_log(SQLITE_WARNING,...) messages if anything is not right. |
| 7755 */ |
| 7756 static void verifyDbFile(unixFile *pFile){ |
| 7757 struct stat buf; |
| 7758 int rc; |
| 7759 rc = osFstat(pFile->h, &buf); |
| 7760 if( rc!=0 ){ |
| 7761 sqlite3_log(SQLITE_WARNING, "cannot fstat db file %s", pFile->zPath); |
| 7762 return; |
| 7763 } |
| 7764 if( buf.st_nlink==0 && (pFile->ctrlFlags & UNIXFILE_DELETE)==0 ){ |
| 7765 sqlite3_log(SQLITE_WARNING, "file unlinked while open: %s", pFile->zPath); |
| 7766 return; |
| 7767 } |
| 7768 if( buf.st_nlink>1 ){ |
| 7769 sqlite3_log(SQLITE_WARNING, "multiple links to file: %s", pFile->zPath); |
| 7770 return; |
| 7771 } |
| 7772 if( fileHasMoved(pFile) ){ |
| 7773 sqlite3_log(SQLITE_WARNING, "file renamed while open: %s", pFile->zPath); |
| 7774 return; |
| 7775 } |
| 7776 } |
| 7777 |
| 7778 |
| 7779 /* |
| 7780 ** This routine checks if there is a RESERVED lock held on the specified |
| 7781 ** file by this or any other process. If such a lock is held, set *pResOut |
| 7782 ** to a non-zero value otherwise *pResOut is set to zero. The return value |
| 7783 ** is set to SQLITE_OK unless an I/O error occurs during lock checking. |
| 7784 */ |
| 7785 static int unixCheckReservedLock(sqlite3_file *id, int *pResOut){ |
| 7786 int rc = SQLITE_OK; |
| 7787 int reserved = 0; |
| 7788 unixFile *pFile = (unixFile*)id; |
| 7789 |
| 7790 SimulateIOError( return SQLITE_IOERR_CHECKRESERVEDLOCK; ); |
| 7791 |
| 7792 assert( pFile ); |
| 7793 assert( pFile->eFileLock<=SHARED_LOCK ); |
| 7794 unixEnterMutex(); /* Because pFile->pInode is shared across threads */ |
| 7795 |
| 7796 /* Check if a thread in this process holds such a lock */ |
| 7797 if( pFile->pInode->eFileLock>SHARED_LOCK ){ |
| 7798 reserved = 1; |
| 7799 } |
| 7800 |
| 7801 /* Otherwise see if some other process holds it. |
| 7802 */ |
| 7803 #ifndef __DJGPP__ |
| 7804 if( !reserved && !pFile->pInode->bProcessLock ){ |
| 7805 struct flock lock; |
| 7806 lock.l_whence = SEEK_SET; |
| 7807 lock.l_start = RESERVED_BYTE; |
| 7808 lock.l_len = 1; |
| 7809 lock.l_type = F_WRLCK; |
| 7810 if( osFcntl(pFile->h, F_GETLK, &lock) ){ |
| 7811 rc = SQLITE_IOERR_CHECKRESERVEDLOCK; |
| 7812 storeLastErrno(pFile, errno); |
| 7813 } else if( lock.l_type!=F_UNLCK ){ |
| 7814 reserved = 1; |
| 7815 } |
| 7816 } |
| 7817 #endif |
| 7818 |
| 7819 unixLeaveMutex(); |
| 7820 OSTRACE(("TEST WR-LOCK %d %d %d (unix)\n", pFile->h, rc, reserved)); |
| 7821 |
| 7822 *pResOut = reserved; |
| 7823 return rc; |
| 7824 } |
| 7825 |
| 7826 /* |
| 7827 ** Attempt to set a system-lock on the file pFile. The lock is |
| 7828 ** described by pLock. |
| 7829 ** |
| 7830 ** If the pFile was opened read/write from unix-excl, then the only lock |
| 7831 ** ever obtained is an exclusive lock, and it is obtained exactly once |
| 7832 ** the first time any lock is attempted. All subsequent system locking |
| 7833 ** operations become no-ops. Locking operations still happen internally, |
| 7834 ** in order to coordinate access between separate database connections |
| 7835 ** within this process, but all of that is handled in memory and the |
| 7836 ** operating system does not participate. |
| 7837 ** |
| 7838 ** This function is a pass-through to fcntl(F_SETLK) if pFile is using |
| 7839 ** any VFS other than "unix-excl" or if pFile is opened on "unix-excl" |
| 7840 ** and is read-only. |
| 7841 ** |
| 7842 ** Zero is returned if the call completes successfully, or -1 if a call |
| 7843 ** to fcntl() fails. In this case, errno is set appropriately (by fcntl()). |
| 7844 */ |
| 7845 static int unixFileLock(unixFile *pFile, struct flock *pLock){ |
| 7846 int rc; |
| 7847 unixInodeInfo *pInode = pFile->pInode; |
| 7848 assert( unixMutexHeld() ); |
| 7849 assert( pInode!=0 ); |
| 7850 if( (pFile->ctrlFlags & (UNIXFILE_EXCL|UNIXFILE_RDONLY))==UNIXFILE_EXCL ){ |
| 7851 if( pInode->bProcessLock==0 ){ |
| 7852 struct flock lock; |
| 7853 assert( pInode->nLock==0 ); |
| 7854 lock.l_whence = SEEK_SET; |
| 7855 lock.l_start = SHARED_FIRST; |
| 7856 lock.l_len = SHARED_SIZE; |
| 7857 lock.l_type = F_WRLCK; |
| 7858 rc = osFcntl(pFile->h, F_SETLK, &lock); |
| 7859 if( rc<0 ) return rc; |
| 7860 pInode->bProcessLock = 1; |
| 7861 pInode->nLock++; |
| 7862 }else{ |
| 7863 rc = 0; |
| 7864 } |
| 7865 }else{ |
| 7866 rc = osFcntl(pFile->h, F_SETLK, pLock); |
| 7867 } |
| 7868 return rc; |
| 7869 } |
| 7870 |
| 7871 /* |
| 7872 ** Lock the file with the lock specified by parameter eFileLock - one |
| 7873 ** of the following: |
| 7874 ** |
| 7875 ** (1) SHARED_LOCK |
| 7876 ** (2) RESERVED_LOCK |
| 7877 ** (3) PENDING_LOCK |
| 7878 ** (4) EXCLUSIVE_LOCK |
| 7879 ** |
| 7880 ** Sometimes when requesting one lock state, additional lock states |
| 7881 ** are inserted in between. The locking might fail on one of the later |
| 7882 ** transitions leaving the lock state different from what it started but |
| 7883 ** still short of its goal. The following chart shows the allowed |
| 7884 ** transitions and the inserted intermediate states: |
| 7885 ** |
| 7886 ** UNLOCKED -> SHARED |
| 7887 ** SHARED -> RESERVED |
| 7888 ** SHARED -> (PENDING) -> EXCLUSIVE |
| 7889 ** RESERVED -> (PENDING) -> EXCLUSIVE |
| 7890 ** PENDING -> EXCLUSIVE |
| 7891 ** |
| 7892 ** This routine will only increase a lock. Use the sqlite3OsUnlock() |
| 7893 ** routine to lower a locking level. |
| 7894 */ |
| 7895 static int unixLock(sqlite3_file *id, int eFileLock){ |
| 7896 /* The following describes the implementation of the various locks and |
| 7897 ** lock transitions in terms of the POSIX advisory shared and exclusive |
| 7898 ** lock primitives (called read-locks and write-locks below, to avoid |
| 7899 ** confusion with SQLite lock names). The algorithms are complicated |
| 7900 ** slightly in order to be compatible with windows systems simultaneously |
| 7901 ** accessing the same database file, in case that is ever required. |
| 7902 ** |
| 7903 ** Symbols defined in os.h indentify the 'pending byte' and the 'reserved |
| 7904 ** byte', each single bytes at well known offsets, and the 'shared byte |
| 7905 ** range', a range of 510 bytes at a well known offset. |
| 7906 ** |
| 7907 ** To obtain a SHARED lock, a read-lock is obtained on the 'pending |
| 7908 ** byte'. If this is successful, a random byte from the 'shared byte |
| 7909 ** range' is read-locked and the lock on the 'pending byte' released. |
| 7910 ** |
| 7911 ** A process may only obtain a RESERVED lock after it has a SHARED lock. |
| 7912 ** A RESERVED lock is implemented by grabbing a write-lock on the |
| 7913 ** 'reserved byte'. |
| 7914 ** |
| 7915 ** A process may only obtain a PENDING lock after it has obtained a |
| 7916 ** SHARED lock. A PENDING lock is implemented by obtaining a write-lock |
| 7917 ** on the 'pending byte'. This ensures that no new SHARED locks can be |
| 7918 ** obtained, but existing SHARED locks are allowed to persist. A process |
| 7919 ** does not have to obtain a RESERVED lock on the way to a PENDING lock. |
| 7920 ** This property is used by the algorithm for rolling back a journal file |
| 7921 ** after a crash. |
| 7922 ** |
| 7923 ** An EXCLUSIVE lock, obtained after a PENDING lock is held, is |
| 7924 ** implemented by obtaining a write-lock on the entire 'shared byte |
| 7925 ** range'. Since all other locks require a read-lock on one of the bytes |
| 7926 ** within this range, this ensures that no other locks are held on the |
| 7927 ** database. |
| 7928 ** |
| 7929 ** The reason a single byte cannot be used instead of the 'shared byte |
| 7930 ** range' is that some versions of windows do not support read-locks. By |
| 7931 ** locking a random byte from a range, concurrent SHARED locks may exist |
| 7932 ** even if the locking primitive used is always a write-lock. |
| 7933 */ |
| 7934 int rc = SQLITE_OK; |
| 7935 unixFile *pFile = (unixFile*)id; |
| 7936 unixInodeInfo *pInode; |
| 7937 struct flock lock; |
| 7938 int tErrno = 0; |
| 7939 |
| 7940 assert( pFile ); |
| 7941 OSTRACE(("LOCK %d %s was %s(%s,%d) pid=%d (unix)\n", pFile->h, |
| 7942 azFileLock(eFileLock), azFileLock(pFile->eFileLock), |
| 7943 azFileLock(pFile->pInode->eFileLock), pFile->pInode->nShared, |
| 7944 osGetpid(0))); |
| 7945 |
| 7946 /* If there is already a lock of this type or more restrictive on the |
| 7947 ** unixFile, do nothing. Don't use the end_lock: exit path, as |
| 7948 ** unixEnterMutex() hasn't been called yet. |
| 7949 */ |
| 7950 if( pFile->eFileLock>=eFileLock ){ |
| 7951 OSTRACE(("LOCK %d %s ok (already held) (unix)\n", pFile->h, |
| 7952 azFileLock(eFileLock))); |
| 7953 return SQLITE_OK; |
| 7954 } |
| 7955 |
| 7956 /* Make sure the locking sequence is correct. |
| 7957 ** (1) We never move from unlocked to anything higher than shared lock. |
| 7958 ** (2) SQLite never explicitly requests a pendig lock. |
| 7959 ** (3) A shared lock is always held when a reserve lock is requested. |
| 7960 */ |
| 7961 assert( pFile->eFileLock!=NO_LOCK || eFileLock==SHARED_LOCK ); |
| 7962 assert( eFileLock!=PENDING_LOCK ); |
| 7963 assert( eFileLock!=RESERVED_LOCK || pFile->eFileLock==SHARED_LOCK ); |
| 7964 |
| 7965 /* This mutex is needed because pFile->pInode is shared across threads |
| 7966 */ |
| 7967 unixEnterMutex(); |
| 7968 pInode = pFile->pInode; |
| 7969 |
| 7970 /* If some thread using this PID has a lock via a different unixFile* |
| 7971 ** handle that precludes the requested lock, return BUSY. |
| 7972 */ |
| 7973 if( (pFile->eFileLock!=pInode->eFileLock && |
| 7974 (pInode->eFileLock>=PENDING_LOCK || eFileLock>SHARED_LOCK)) |
| 7975 ){ |
| 7976 rc = SQLITE_BUSY; |
| 7977 goto end_lock; |
| 7978 } |
| 7979 |
| 7980 /* If a SHARED lock is requested, and some thread using this PID already |
| 7981 ** has a SHARED or RESERVED lock, then increment reference counts and |
| 7982 ** return SQLITE_OK. |
| 7983 */ |
| 7984 if( eFileLock==SHARED_LOCK && |
| 7985 (pInode->eFileLock==SHARED_LOCK || pInode->eFileLock==RESERVED_LOCK) ){ |
| 7986 assert( eFileLock==SHARED_LOCK ); |
| 7987 assert( pFile->eFileLock==0 ); |
| 7988 assert( pInode->nShared>0 ); |
| 7989 pFile->eFileLock = SHARED_LOCK; |
| 7990 pInode->nShared++; |
| 7991 pInode->nLock++; |
| 7992 goto end_lock; |
| 7993 } |
| 7994 |
| 7995 |
| 7996 /* A PENDING lock is needed before acquiring a SHARED lock and before |
| 7997 ** acquiring an EXCLUSIVE lock. For the SHARED lock, the PENDING will |
| 7998 ** be released. |
| 7999 */ |
| 8000 lock.l_len = 1L; |
| 8001 lock.l_whence = SEEK_SET; |
| 8002 if( eFileLock==SHARED_LOCK |
| 8003 || (eFileLock==EXCLUSIVE_LOCK && pFile->eFileLock<PENDING_LOCK) |
| 8004 ){ |
| 8005 lock.l_type = (eFileLock==SHARED_LOCK?F_RDLCK:F_WRLCK); |
| 8006 lock.l_start = PENDING_BYTE; |
| 8007 if( unixFileLock(pFile, &lock) ){ |
| 8008 tErrno = errno; |
| 8009 rc = sqliteErrorFromPosixError(tErrno, SQLITE_IOERR_LOCK); |
| 8010 if( rc!=SQLITE_BUSY ){ |
| 8011 storeLastErrno(pFile, tErrno); |
| 8012 } |
| 8013 goto end_lock; |
| 8014 } |
| 8015 } |
| 8016 |
| 8017 |
| 8018 /* If control gets to this point, then actually go ahead and make |
| 8019 ** operating system calls for the specified lock. |
| 8020 */ |
| 8021 if( eFileLock==SHARED_LOCK ){ |
| 8022 assert( pInode->nShared==0 ); |
| 8023 assert( pInode->eFileLock==0 ); |
| 8024 assert( rc==SQLITE_OK ); |
| 8025 |
| 8026 /* Now get the read-lock */ |
| 8027 lock.l_start = SHARED_FIRST; |
| 8028 lock.l_len = SHARED_SIZE; |
| 8029 if( unixFileLock(pFile, &lock) ){ |
| 8030 tErrno = errno; |
| 8031 rc = sqliteErrorFromPosixError(tErrno, SQLITE_IOERR_LOCK); |
| 8032 } |
| 8033 |
| 8034 /* Drop the temporary PENDING lock */ |
| 8035 lock.l_start = PENDING_BYTE; |
| 8036 lock.l_len = 1L; |
| 8037 lock.l_type = F_UNLCK; |
| 8038 if( unixFileLock(pFile, &lock) && rc==SQLITE_OK ){ |
| 8039 /* This could happen with a network mount */ |
| 8040 tErrno = errno; |
| 8041 rc = SQLITE_IOERR_UNLOCK; |
| 8042 } |
| 8043 |
| 8044 if( rc ){ |
| 8045 if( rc!=SQLITE_BUSY ){ |
| 8046 storeLastErrno(pFile, tErrno); |
| 8047 } |
| 8048 goto end_lock; |
| 8049 }else{ |
| 8050 pFile->eFileLock = SHARED_LOCK; |
| 8051 pInode->nLock++; |
| 8052 pInode->nShared = 1; |
| 8053 } |
| 8054 }else if( eFileLock==EXCLUSIVE_LOCK && pInode->nShared>1 ){ |
| 8055 /* We are trying for an exclusive lock but another thread in this |
| 8056 ** same process is still holding a shared lock. */ |
| 8057 rc = SQLITE_BUSY; |
| 8058 }else{ |
| 8059 /* The request was for a RESERVED or EXCLUSIVE lock. It is |
| 8060 ** assumed that there is a SHARED or greater lock on the file |
| 8061 ** already. |
| 8062 */ |
| 8063 assert( 0!=pFile->eFileLock ); |
| 8064 lock.l_type = F_WRLCK; |
| 8065 |
| 8066 assert( eFileLock==RESERVED_LOCK || eFileLock==EXCLUSIVE_LOCK ); |
| 8067 if( eFileLock==RESERVED_LOCK ){ |
| 8068 lock.l_start = RESERVED_BYTE; |
| 8069 lock.l_len = 1L; |
| 8070 }else{ |
| 8071 lock.l_start = SHARED_FIRST; |
| 8072 lock.l_len = SHARED_SIZE; |
| 8073 } |
| 8074 |
| 8075 if( unixFileLock(pFile, &lock) ){ |
| 8076 tErrno = errno; |
| 8077 rc = sqliteErrorFromPosixError(tErrno, SQLITE_IOERR_LOCK); |
| 8078 if( rc!=SQLITE_BUSY ){ |
| 8079 storeLastErrno(pFile, tErrno); |
| 8080 } |
| 8081 } |
| 8082 } |
| 8083 |
| 8084 |
| 8085 #ifdef SQLITE_DEBUG |
| 8086 /* Set up the transaction-counter change checking flags when |
| 8087 ** transitioning from a SHARED to a RESERVED lock. The change |
| 8088 ** from SHARED to RESERVED marks the beginning of a normal |
| 8089 ** write operation (not a hot journal rollback). |
| 8090 */ |
| 8091 if( rc==SQLITE_OK |
| 8092 && pFile->eFileLock<=SHARED_LOCK |
| 8093 && eFileLock==RESERVED_LOCK |
| 8094 ){ |
| 8095 pFile->transCntrChng = 0; |
| 8096 pFile->dbUpdate = 0; |
| 8097 pFile->inNormalWrite = 1; |
| 8098 } |
| 8099 #endif |
| 8100 |
| 8101 |
| 8102 if( rc==SQLITE_OK ){ |
| 8103 pFile->eFileLock = eFileLock; |
| 8104 pInode->eFileLock = eFileLock; |
| 8105 }else if( eFileLock==EXCLUSIVE_LOCK ){ |
| 8106 pFile->eFileLock = PENDING_LOCK; |
| 8107 pInode->eFileLock = PENDING_LOCK; |
| 8108 } |
| 8109 |
| 8110 end_lock: |
| 8111 unixLeaveMutex(); |
| 8112 OSTRACE(("LOCK %d %s %s (unix)\n", pFile->h, azFileLock(eFileLock), |
| 8113 rc==SQLITE_OK ? "ok" : "failed")); |
| 8114 return rc; |
| 8115 } |
| 8116 |
| 8117 /* |
| 8118 ** Add the file descriptor used by file handle pFile to the corresponding |
| 8119 ** pUnused list. |
| 8120 */ |
| 8121 static void setPendingFd(unixFile *pFile){ |
| 8122 unixInodeInfo *pInode = pFile->pInode; |
| 8123 UnixUnusedFd *p = pFile->pUnused; |
| 8124 p->pNext = pInode->pUnused; |
| 8125 pInode->pUnused = p; |
| 8126 pFile->h = -1; |
| 8127 pFile->pUnused = 0; |
| 8128 } |
| 8129 |
| 8130 /* |
| 8131 ** Lower the locking level on file descriptor pFile to eFileLock. eFileLock |
| 8132 ** must be either NO_LOCK or SHARED_LOCK. |
| 8133 ** |
| 8134 ** If the locking level of the file descriptor is already at or below |
| 8135 ** the requested locking level, this routine is a no-op. |
| 8136 ** |
| 8137 ** If handleNFSUnlock is true, then on downgrading an EXCLUSIVE_LOCK to SHARED |
| 8138 ** the byte range is divided into 2 parts and the first part is unlocked then |
| 8139 ** set to a read lock, then the other part is simply unlocked. This works |
| 8140 ** around a bug in BSD NFS lockd (also seen on MacOSX 10.3+) that fails to |
| 8141 ** remove the write lock on a region when a read lock is set. |
| 8142 */ |
| 8143 static int posixUnlock(sqlite3_file *id, int eFileLock, int handleNFSUnlock){ |
| 8144 unixFile *pFile = (unixFile*)id; |
| 8145 unixInodeInfo *pInode; |
| 8146 struct flock lock; |
| 8147 int rc = SQLITE_OK; |
| 8148 |
| 8149 assert( pFile ); |
| 8150 OSTRACE(("UNLOCK %d %d was %d(%d,%d) pid=%d (unix)\n", pFile->h, eFileLock, |
| 8151 pFile->eFileLock, pFile->pInode->eFileLock, pFile->pInode->nShared, |
| 8152 osGetpid(0))); |
| 8153 |
| 8154 assert( eFileLock<=SHARED_LOCK ); |
| 8155 if( pFile->eFileLock<=eFileLock ){ |
| 8156 return SQLITE_OK; |
| 8157 } |
| 8158 unixEnterMutex(); |
| 8159 pInode = pFile->pInode; |
| 8160 assert( pInode->nShared!=0 ); |
| 8161 if( pFile->eFileLock>SHARED_LOCK ){ |
| 8162 assert( pInode->eFileLock==pFile->eFileLock ); |
| 8163 |
| 8164 #ifdef SQLITE_DEBUG |
| 8165 /* When reducing a lock such that other processes can start |
| 8166 ** reading the database file again, make sure that the |
| 8167 ** transaction counter was updated if any part of the database |
| 8168 ** file changed. If the transaction counter is not updated, |
| 8169 ** other connections to the same file might not realize that |
| 8170 ** the file has changed and hence might not know to flush their |
| 8171 ** cache. The use of a stale cache can lead to database corruption. |
| 8172 */ |
| 8173 pFile->inNormalWrite = 0; |
| 8174 #endif |
| 8175 |
| 8176 /* downgrading to a shared lock on NFS involves clearing the write lock |
| 8177 ** before establishing the readlock - to avoid a race condition we downgrade |
| 8178 ** the lock in 2 blocks, so that part of the range will be covered by a |
| 8179 ** write lock until the rest is covered by a read lock: |
| 8180 ** 1: [WWWWW] |
| 8181 ** 2: [....W] |
| 8182 ** 3: [RRRRW] |
| 8183 ** 4: [RRRR.] |
| 8184 */ |
| 8185 if( eFileLock==SHARED_LOCK ){ |
| 8186 #if !defined(__APPLE__) || !SQLITE_ENABLE_LOCKING_STYLE |
| 8187 (void)handleNFSUnlock; |
| 8188 assert( handleNFSUnlock==0 ); |
| 8189 #endif |
| 8190 #if defined(__APPLE__) && SQLITE_ENABLE_LOCKING_STYLE |
| 8191 if( handleNFSUnlock ){ |
| 8192 int tErrno; /* Error code from system call errors */ |
| 8193 off_t divSize = SHARED_SIZE - 1; |
| 8194 |
| 8195 lock.l_type = F_UNLCK; |
| 8196 lock.l_whence = SEEK_SET; |
| 8197 lock.l_start = SHARED_FIRST; |
| 8198 lock.l_len = divSize; |
| 8199 if( unixFileLock(pFile, &lock)==(-1) ){ |
| 8200 tErrno = errno; |
| 8201 rc = SQLITE_IOERR_UNLOCK; |
| 8202 storeLastErrno(pFile, tErrno); |
| 8203 goto end_unlock; |
| 8204 } |
| 8205 lock.l_type = F_RDLCK; |
| 8206 lock.l_whence = SEEK_SET; |
| 8207 lock.l_start = SHARED_FIRST; |
| 8208 lock.l_len = divSize; |
| 8209 if( unixFileLock(pFile, &lock)==(-1) ){ |
| 8210 tErrno = errno; |
| 8211 rc = sqliteErrorFromPosixError(tErrno, SQLITE_IOERR_RDLOCK); |
| 8212 if( IS_LOCK_ERROR(rc) ){ |
| 8213 storeLastErrno(pFile, tErrno); |
| 8214 } |
| 8215 goto end_unlock; |
| 8216 } |
| 8217 lock.l_type = F_UNLCK; |
| 8218 lock.l_whence = SEEK_SET; |
| 8219 lock.l_start = SHARED_FIRST+divSize; |
| 8220 lock.l_len = SHARED_SIZE-divSize; |
| 8221 if( unixFileLock(pFile, &lock)==(-1) ){ |
| 8222 tErrno = errno; |
| 8223 rc = SQLITE_IOERR_UNLOCK; |
| 8224 storeLastErrno(pFile, tErrno); |
| 8225 goto end_unlock; |
| 8226 } |
| 8227 }else |
| 8228 #endif /* defined(__APPLE__) && SQLITE_ENABLE_LOCKING_STYLE */ |
| 8229 { |
| 8230 lock.l_type = F_RDLCK; |
| 8231 lock.l_whence = SEEK_SET; |
| 8232 lock.l_start = SHARED_FIRST; |
| 8233 lock.l_len = SHARED_SIZE; |
| 8234 if( unixFileLock(pFile, &lock) ){ |
| 8235 /* In theory, the call to unixFileLock() cannot fail because another |
| 8236 ** process is holding an incompatible lock. If it does, this |
| 8237 ** indicates that the other process is not following the locking |
| 8238 ** protocol. If this happens, return SQLITE_IOERR_RDLOCK. Returning |
| 8239 ** SQLITE_BUSY would confuse the upper layer (in practice it causes |
| 8240 ** an assert to fail). */ |
| 8241 rc = SQLITE_IOERR_RDLOCK; |
| 8242 storeLastErrno(pFile, errno); |
| 8243 goto end_unlock; |
| 8244 } |
| 8245 } |
| 8246 } |
| 8247 lock.l_type = F_UNLCK; |
| 8248 lock.l_whence = SEEK_SET; |
| 8249 lock.l_start = PENDING_BYTE; |
| 8250 lock.l_len = 2L; assert( PENDING_BYTE+1==RESERVED_BYTE ); |
| 8251 if( unixFileLock(pFile, &lock)==0 ){ |
| 8252 pInode->eFileLock = SHARED_LOCK; |
| 8253 }else{ |
| 8254 rc = SQLITE_IOERR_UNLOCK; |
| 8255 storeLastErrno(pFile, errno); |
| 8256 goto end_unlock; |
| 8257 } |
| 8258 } |
| 8259 if( eFileLock==NO_LOCK ){ |
| 8260 /* Decrement the shared lock counter. Release the lock using an |
| 8261 ** OS call only when all threads in this same process have released |
| 8262 ** the lock. |
| 8263 */ |
| 8264 pInode->nShared--; |
| 8265 if( pInode->nShared==0 ){ |
| 8266 lock.l_type = F_UNLCK; |
| 8267 lock.l_whence = SEEK_SET; |
| 8268 lock.l_start = lock.l_len = 0L; |
| 8269 if( unixFileLock(pFile, &lock)==0 ){ |
| 8270 pInode->eFileLock = NO_LOCK; |
| 8271 }else{ |
| 8272 rc = SQLITE_IOERR_UNLOCK; |
| 8273 storeLastErrno(pFile, errno); |
| 8274 pInode->eFileLock = NO_LOCK; |
| 8275 pFile->eFileLock = NO_LOCK; |
| 8276 } |
| 8277 } |
| 8278 |
| 8279 /* Decrement the count of locks against this same file. When the |
| 8280 ** count reaches zero, close any other file descriptors whose close |
| 8281 ** was deferred because of outstanding locks. |
| 8282 */ |
| 8283 pInode->nLock--; |
| 8284 assert( pInode->nLock>=0 ); |
| 8285 if( pInode->nLock==0 ){ |
| 8286 closePendingFds(pFile); |
| 8287 } |
| 8288 } |
| 8289 |
| 8290 end_unlock: |
| 8291 unixLeaveMutex(); |
| 8292 if( rc==SQLITE_OK ) pFile->eFileLock = eFileLock; |
| 8293 return rc; |
| 8294 } |
| 8295 |
| 8296 /* |
| 8297 ** Lower the locking level on file descriptor pFile to eFileLock. eFileLock |
| 8298 ** must be either NO_LOCK or SHARED_LOCK. |
| 8299 ** |
| 8300 ** If the locking level of the file descriptor is already at or below |
| 8301 ** the requested locking level, this routine is a no-op. |
| 8302 */ |
| 8303 static int unixUnlock(sqlite3_file *id, int eFileLock){ |
| 8304 #if SQLITE_MAX_MMAP_SIZE>0 |
| 8305 assert( eFileLock==SHARED_LOCK || ((unixFile *)id)->nFetchOut==0 ); |
| 8306 #endif |
| 8307 return posixUnlock(id, eFileLock, 0); |
| 8308 } |
| 8309 |
| 8310 #if SQLITE_MAX_MMAP_SIZE>0 |
| 8311 static int unixMapfile(unixFile *pFd, i64 nByte); |
| 8312 static void unixUnmapfile(unixFile *pFd); |
| 8313 #endif |
| 8314 |
| 8315 /* |
| 8316 ** This function performs the parts of the "close file" operation |
| 8317 ** common to all locking schemes. It closes the directory and file |
| 8318 ** handles, if they are valid, and sets all fields of the unixFile |
| 8319 ** structure to 0. |
| 8320 ** |
| 8321 ** It is *not* necessary to hold the mutex when this routine is called, |
| 8322 ** even on VxWorks. A mutex will be acquired on VxWorks by the |
| 8323 ** vxworksReleaseFileId() routine. |
| 8324 */ |
| 8325 static int closeUnixFile(sqlite3_file *id){ |
| 8326 unixFile *pFile = (unixFile*)id; |
| 8327 #if SQLITE_MAX_MMAP_SIZE>0 |
| 8328 unixUnmapfile(pFile); |
| 8329 #endif |
| 8330 if( pFile->h>=0 ){ |
| 8331 robust_close(pFile, pFile->h, __LINE__); |
| 8332 pFile->h = -1; |
| 8333 } |
| 8334 #if OS_VXWORKS |
| 8335 if( pFile->pId ){ |
| 8336 if( pFile->ctrlFlags & UNIXFILE_DELETE ){ |
| 8337 osUnlink(pFile->pId->zCanonicalName); |
| 8338 } |
| 8339 vxworksReleaseFileId(pFile->pId); |
| 8340 pFile->pId = 0; |
| 8341 } |
| 8342 #endif |
| 8343 #ifdef SQLITE_UNLINK_AFTER_CLOSE |
| 8344 if( pFile->ctrlFlags & UNIXFILE_DELETE ){ |
| 8345 osUnlink(pFile->zPath); |
| 8346 sqlite3_free(*(char**)&pFile->zPath); |
| 8347 pFile->zPath = 0; |
| 8348 } |
| 8349 #endif |
| 8350 OSTRACE(("CLOSE %-3d\n", pFile->h)); |
| 8351 OpenCounter(-1); |
| 8352 sqlite3_free(pFile->pUnused); |
| 8353 memset(pFile, 0, sizeof(unixFile)); |
| 8354 return SQLITE_OK; |
| 8355 } |
| 8356 |
| 8357 /* |
| 8358 ** Close a file. |
| 8359 */ |
| 8360 static int unixClose(sqlite3_file *id){ |
| 8361 int rc = SQLITE_OK; |
| 8362 unixFile *pFile = (unixFile *)id; |
| 8363 verifyDbFile(pFile); |
| 8364 unixUnlock(id, NO_LOCK); |
| 8365 unixEnterMutex(); |
| 8366 |
| 8367 /* unixFile.pInode is always valid here. Otherwise, a different close |
| 8368 ** routine (e.g. nolockClose()) would be called instead. |
| 8369 */ |
| 8370 assert( pFile->pInode->nLock>0 || pFile->pInode->bProcessLock==0 ); |
| 8371 if( ALWAYS(pFile->pInode) && pFile->pInode->nLock ){ |
| 8372 /* If there are outstanding locks, do not actually close the file just |
| 8373 ** yet because that would clear those locks. Instead, add the file |
| 8374 ** descriptor to pInode->pUnused list. It will be automatically closed |
| 8375 ** when the last lock is cleared. |
| 8376 */ |
| 8377 setPendingFd(pFile); |
| 8378 } |
| 8379 releaseInodeInfo(pFile); |
| 8380 rc = closeUnixFile(id); |
| 8381 unixLeaveMutex(); |
| 8382 return rc; |
| 8383 } |
| 8384 |
| 8385 /************** End of the posix advisory lock implementation ***************** |
| 8386 ******************************************************************************/ |
| 8387 |
| 8388 /****************************************************************************** |
| 8389 ****************************** No-op Locking ********************************** |
| 8390 ** |
| 8391 ** Of the various locking implementations available, this is by far the |
| 8392 ** simplest: locking is ignored. No attempt is made to lock the database |
| 8393 ** file for reading or writing. |
| 8394 ** |
| 8395 ** This locking mode is appropriate for use on read-only databases |
| 8396 ** (ex: databases that are burned into CD-ROM, for example.) It can |
| 8397 ** also be used if the application employs some external mechanism to |
| 8398 ** prevent simultaneous access of the same database by two or more |
| 8399 ** database connections. But there is a serious risk of database |
| 8400 ** corruption if this locking mode is used in situations where multiple |
| 8401 ** database connections are accessing the same database file at the same |
| 8402 ** time and one or more of those connections are writing. |
| 8403 */ |
| 8404 |
| 8405 static int nolockCheckReservedLock(sqlite3_file *NotUsed, int *pResOut){ |
| 8406 UNUSED_PARAMETER(NotUsed); |
| 8407 *pResOut = 0; |
| 8408 return SQLITE_OK; |
| 8409 } |
| 8410 static int nolockLock(sqlite3_file *NotUsed, int NotUsed2){ |
| 8411 UNUSED_PARAMETER2(NotUsed, NotUsed2); |
| 8412 return SQLITE_OK; |
| 8413 } |
| 8414 static int nolockUnlock(sqlite3_file *NotUsed, int NotUsed2){ |
| 8415 UNUSED_PARAMETER2(NotUsed, NotUsed2); |
| 8416 return SQLITE_OK; |
| 8417 } |
| 8418 |
| 8419 /* |
| 8420 ** Close the file. |
| 8421 */ |
| 8422 static int nolockClose(sqlite3_file *id) { |
| 8423 return closeUnixFile(id); |
| 8424 } |
| 8425 |
| 8426 /******************* End of the no-op lock implementation ********************* |
| 8427 ******************************************************************************/ |
| 8428 |
| 8429 /****************************************************************************** |
| 8430 ************************* Begin dot-file Locking ****************************** |
| 8431 ** |
| 8432 ** The dotfile locking implementation uses the existence of separate lock |
| 8433 ** files (really a directory) to control access to the database. This works |
| 8434 ** on just about every filesystem imaginable. But there are serious downsides: |
| 8435 ** |
| 8436 ** (1) There is zero concurrency. A single reader blocks all other |
| 8437 ** connections from reading or writing the database. |
| 8438 ** |
| 8439 ** (2) An application crash or power loss can leave stale lock files |
| 8440 ** sitting around that need to be cleared manually. |
| 8441 ** |
| 8442 ** Nevertheless, a dotlock is an appropriate locking mode for use if no |
| 8443 ** other locking strategy is available. |
| 8444 ** |
| 8445 ** Dotfile locking works by creating a subdirectory in the same directory as |
| 8446 ** the database and with the same name but with a ".lock" extension added. |
| 8447 ** The existence of a lock directory implies an EXCLUSIVE lock. All other |
| 8448 ** lock types (SHARED, RESERVED, PENDING) are mapped into EXCLUSIVE. |
| 8449 */ |
| 8450 |
| 8451 /* |
| 8452 ** The file suffix added to the data base filename in order to create the |
| 8453 ** lock directory. |
| 8454 */ |
| 8455 #define DOTLOCK_SUFFIX ".lock" |
| 8456 |
| 8457 /* |
| 8458 ** This routine checks if there is a RESERVED lock held on the specified |
| 8459 ** file by this or any other process. If such a lock is held, set *pResOut |
| 8460 ** to a non-zero value otherwise *pResOut is set to zero. The return value |
| 8461 ** is set to SQLITE_OK unless an I/O error occurs during lock checking. |
| 8462 ** |
| 8463 ** In dotfile locking, either a lock exists or it does not. So in this |
| 8464 ** variation of CheckReservedLock(), *pResOut is set to true if any lock |
| 8465 ** is held on the file and false if the file is unlocked. |
| 8466 */ |
| 8467 static int dotlockCheckReservedLock(sqlite3_file *id, int *pResOut) { |
| 8468 int rc = SQLITE_OK; |
| 8469 int reserved = 0; |
| 8470 unixFile *pFile = (unixFile*)id; |
| 8471 |
| 8472 SimulateIOError( return SQLITE_IOERR_CHECKRESERVEDLOCK; ); |
| 8473 |
| 8474 assert( pFile ); |
| 8475 reserved = osAccess((const char*)pFile->lockingContext, 0)==0; |
| 8476 OSTRACE(("TEST WR-LOCK %d %d %d (dotlock)\n", pFile->h, rc, reserved)); |
| 8477 *pResOut = reserved; |
| 8478 return rc; |
| 8479 } |
| 8480 |
| 8481 /* |
| 8482 ** Lock the file with the lock specified by parameter eFileLock - one |
| 8483 ** of the following: |
| 8484 ** |
| 8485 ** (1) SHARED_LOCK |
| 8486 ** (2) RESERVED_LOCK |
| 8487 ** (3) PENDING_LOCK |
| 8488 ** (4) EXCLUSIVE_LOCK |
| 8489 ** |
| 8490 ** Sometimes when requesting one lock state, additional lock states |
| 8491 ** are inserted in between. The locking might fail on one of the later |
| 8492 ** transitions leaving the lock state different from what it started but |
| 8493 ** still short of its goal. The following chart shows the allowed |
| 8494 ** transitions and the inserted intermediate states: |
| 8495 ** |
| 8496 ** UNLOCKED -> SHARED |
| 8497 ** SHARED -> RESERVED |
| 8498 ** SHARED -> (PENDING) -> EXCLUSIVE |
| 8499 ** RESERVED -> (PENDING) -> EXCLUSIVE |
| 8500 ** PENDING -> EXCLUSIVE |
| 8501 ** |
| 8502 ** This routine will only increase a lock. Use the sqlite3OsUnlock() |
| 8503 ** routine to lower a locking level. |
| 8504 ** |
| 8505 ** With dotfile locking, we really only support state (4): EXCLUSIVE. |
| 8506 ** But we track the other locking levels internally. |
| 8507 */ |
| 8508 static int dotlockLock(sqlite3_file *id, int eFileLock) { |
| 8509 unixFile *pFile = (unixFile*)id; |
| 8510 char *zLockFile = (char *)pFile->lockingContext; |
| 8511 int rc = SQLITE_OK; |
| 8512 |
| 8513 |
| 8514 /* If we have any lock, then the lock file already exists. All we have |
| 8515 ** to do is adjust our internal record of the lock level. |
| 8516 */ |
| 8517 if( pFile->eFileLock > NO_LOCK ){ |
| 8518 pFile->eFileLock = eFileLock; |
| 8519 /* Always update the timestamp on the old file */ |
| 8520 #ifdef HAVE_UTIME |
| 8521 utime(zLockFile, NULL); |
| 8522 #else |
| 8523 utimes(zLockFile, NULL); |
| 8524 #endif |
| 8525 return SQLITE_OK; |
| 8526 } |
| 8527 |
| 8528 /* grab an exclusive lock */ |
| 8529 rc = osMkdir(zLockFile, 0777); |
| 8530 if( rc<0 ){ |
| 8531 /* failed to open/create the lock directory */ |
| 8532 int tErrno = errno; |
| 8533 if( EEXIST == tErrno ){ |
| 8534 rc = SQLITE_BUSY; |
| 8535 } else { |
| 8536 rc = sqliteErrorFromPosixError(tErrno, SQLITE_IOERR_LOCK); |
| 8537 if( rc!=SQLITE_BUSY ){ |
| 8538 storeLastErrno(pFile, tErrno); |
| 8539 } |
| 8540 } |
| 8541 return rc; |
| 8542 } |
| 8543 |
| 8544 /* got it, set the type and return ok */ |
| 8545 pFile->eFileLock = eFileLock; |
| 8546 return rc; |
| 8547 } |
| 8548 |
| 8549 /* |
| 8550 ** Lower the locking level on file descriptor pFile to eFileLock. eFileLock |
| 8551 ** must be either NO_LOCK or SHARED_LOCK. |
| 8552 ** |
| 8553 ** If the locking level of the file descriptor is already at or below |
| 8554 ** the requested locking level, this routine is a no-op. |
| 8555 ** |
| 8556 ** When the locking level reaches NO_LOCK, delete the lock file. |
| 8557 */ |
| 8558 static int dotlockUnlock(sqlite3_file *id, int eFileLock) { |
| 8559 unixFile *pFile = (unixFile*)id; |
| 8560 char *zLockFile = (char *)pFile->lockingContext; |
| 8561 int rc; |
| 8562 |
| 8563 assert( pFile ); |
| 8564 OSTRACE(("UNLOCK %d %d was %d pid=%d (dotlock)\n", pFile->h, eFileLock, |
| 8565 pFile->eFileLock, osGetpid(0))); |
| 8566 assert( eFileLock<=SHARED_LOCK ); |
| 8567 |
| 8568 /* no-op if possible */ |
| 8569 if( pFile->eFileLock==eFileLock ){ |
| 8570 return SQLITE_OK; |
| 8571 } |
| 8572 |
| 8573 /* To downgrade to shared, simply update our internal notion of the |
| 8574 ** lock state. No need to mess with the file on disk. |
| 8575 */ |
| 8576 if( eFileLock==SHARED_LOCK ){ |
| 8577 pFile->eFileLock = SHARED_LOCK; |
| 8578 return SQLITE_OK; |
| 8579 } |
| 8580 |
| 8581 /* To fully unlock the database, delete the lock file */ |
| 8582 assert( eFileLock==NO_LOCK ); |
| 8583 rc = osRmdir(zLockFile); |
| 8584 if( rc<0 ){ |
| 8585 int tErrno = errno; |
| 8586 if( tErrno==ENOENT ){ |
| 8587 rc = SQLITE_OK; |
| 8588 }else{ |
| 8589 rc = SQLITE_IOERR_UNLOCK; |
| 8590 storeLastErrno(pFile, tErrno); |
| 8591 } |
| 8592 return rc; |
| 8593 } |
| 8594 pFile->eFileLock = NO_LOCK; |
| 8595 return SQLITE_OK; |
| 8596 } |
| 8597 |
| 8598 /* |
| 8599 ** Close a file. Make sure the lock has been released before closing. |
| 8600 */ |
| 8601 static int dotlockClose(sqlite3_file *id) { |
| 8602 unixFile *pFile = (unixFile*)id; |
| 8603 assert( id!=0 ); |
| 8604 dotlockUnlock(id, NO_LOCK); |
| 8605 sqlite3_free(pFile->lockingContext); |
| 8606 return closeUnixFile(id); |
| 8607 } |
| 8608 /****************** End of the dot-file lock implementation ******************* |
| 8609 ******************************************************************************/ |
| 8610 |
| 8611 /****************************************************************************** |
| 8612 ************************** Begin flock Locking ******************************** |
| 8613 ** |
| 8614 ** Use the flock() system call to do file locking. |
| 8615 ** |
| 8616 ** flock() locking is like dot-file locking in that the various |
| 8617 ** fine-grain locking levels supported by SQLite are collapsed into |
| 8618 ** a single exclusive lock. In other words, SHARED, RESERVED, and |
| 8619 ** PENDING locks are the same thing as an EXCLUSIVE lock. SQLite |
| 8620 ** still works when you do this, but concurrency is reduced since |
| 8621 ** only a single process can be reading the database at a time. |
| 8622 ** |
| 8623 ** Omit this section if SQLITE_ENABLE_LOCKING_STYLE is turned off |
| 8624 */ |
| 8625 #if SQLITE_ENABLE_LOCKING_STYLE |
| 8626 |
| 8627 /* |
| 8628 ** Retry flock() calls that fail with EINTR |
| 8629 */ |
| 8630 #ifdef EINTR |
| 8631 static int robust_flock(int fd, int op){ |
| 8632 int rc; |
| 8633 do{ rc = flock(fd,op); }while( rc<0 && errno==EINTR ); |
| 8634 return rc; |
| 8635 } |
| 8636 #else |
| 8637 # define robust_flock(a,b) flock(a,b) |
| 8638 #endif |
| 8639 |
| 8640 |
| 8641 /* |
| 8642 ** This routine checks if there is a RESERVED lock held on the specified |
| 8643 ** file by this or any other process. If such a lock is held, set *pResOut |
| 8644 ** to a non-zero value otherwise *pResOut is set to zero. The return value |
| 8645 ** is set to SQLITE_OK unless an I/O error occurs during lock checking. |
| 8646 */ |
| 8647 static int flockCheckReservedLock(sqlite3_file *id, int *pResOut){ |
| 8648 int rc = SQLITE_OK; |
| 8649 int reserved = 0; |
| 8650 unixFile *pFile = (unixFile*)id; |
| 8651 |
| 8652 SimulateIOError( return SQLITE_IOERR_CHECKRESERVEDLOCK; ); |
| 8653 |
| 8654 assert( pFile ); |
| 8655 |
| 8656 /* Check if a thread in this process holds such a lock */ |
| 8657 if( pFile->eFileLock>SHARED_LOCK ){ |
| 8658 reserved = 1; |
| 8659 } |
| 8660 |
| 8661 /* Otherwise see if some other process holds it. */ |
| 8662 if( !reserved ){ |
| 8663 /* attempt to get the lock */ |
| 8664 int lrc = robust_flock(pFile->h, LOCK_EX | LOCK_NB); |
| 8665 if( !lrc ){ |
| 8666 /* got the lock, unlock it */ |
| 8667 lrc = robust_flock(pFile->h, LOCK_UN); |
| 8668 if ( lrc ) { |
| 8669 int tErrno = errno; |
| 8670 /* unlock failed with an error */ |
| 8671 lrc = SQLITE_IOERR_UNLOCK; |
| 8672 storeLastErrno(pFile, tErrno); |
| 8673 rc = lrc; |
| 8674 } |
| 8675 } else { |
| 8676 int tErrno = errno; |
| 8677 reserved = 1; |
| 8678 /* someone else might have it reserved */ |
| 8679 lrc = sqliteErrorFromPosixError(tErrno, SQLITE_IOERR_LOCK); |
| 8680 if( IS_LOCK_ERROR(lrc) ){ |
| 8681 storeLastErrno(pFile, tErrno); |
| 8682 rc = lrc; |
| 8683 } |
| 8684 } |
| 8685 } |
| 8686 OSTRACE(("TEST WR-LOCK %d %d %d (flock)\n", pFile->h, rc, reserved)); |
| 8687 |
| 8688 #ifdef SQLITE_IGNORE_FLOCK_LOCK_ERRORS |
| 8689 if( (rc & SQLITE_IOERR) == SQLITE_IOERR ){ |
| 8690 rc = SQLITE_OK; |
| 8691 reserved=1; |
| 8692 } |
| 8693 #endif /* SQLITE_IGNORE_FLOCK_LOCK_ERRORS */ |
| 8694 *pResOut = reserved; |
| 8695 return rc; |
| 8696 } |
| 8697 |
| 8698 /* |
| 8699 ** Lock the file with the lock specified by parameter eFileLock - one |
| 8700 ** of the following: |
| 8701 ** |
| 8702 ** (1) SHARED_LOCK |
| 8703 ** (2) RESERVED_LOCK |
| 8704 ** (3) PENDING_LOCK |
| 8705 ** (4) EXCLUSIVE_LOCK |
| 8706 ** |
| 8707 ** Sometimes when requesting one lock state, additional lock states |
| 8708 ** are inserted in between. The locking might fail on one of the later |
| 8709 ** transitions leaving the lock state different from what it started but |
| 8710 ** still short of its goal. The following chart shows the allowed |
| 8711 ** transitions and the inserted intermediate states: |
| 8712 ** |
| 8713 ** UNLOCKED -> SHARED |
| 8714 ** SHARED -> RESERVED |
| 8715 ** SHARED -> (PENDING) -> EXCLUSIVE |
| 8716 ** RESERVED -> (PENDING) -> EXCLUSIVE |
| 8717 ** PENDING -> EXCLUSIVE |
| 8718 ** |
| 8719 ** flock() only really support EXCLUSIVE locks. We track intermediate |
| 8720 ** lock states in the sqlite3_file structure, but all locks SHARED or |
| 8721 ** above are really EXCLUSIVE locks and exclude all other processes from |
| 8722 ** access the file. |
| 8723 ** |
| 8724 ** This routine will only increase a lock. Use the sqlite3OsUnlock() |
| 8725 ** routine to lower a locking level. |
| 8726 */ |
| 8727 static int flockLock(sqlite3_file *id, int eFileLock) { |
| 8728 int rc = SQLITE_OK; |
| 8729 unixFile *pFile = (unixFile*)id; |
| 8730 |
| 8731 assert( pFile ); |
| 8732 |
| 8733 /* if we already have a lock, it is exclusive. |
| 8734 ** Just adjust level and punt on outta here. */ |
| 8735 if (pFile->eFileLock > NO_LOCK) { |
| 8736 pFile->eFileLock = eFileLock; |
| 8737 return SQLITE_OK; |
| 8738 } |
| 8739 |
| 8740 /* grab an exclusive lock */ |
| 8741 |
| 8742 if (robust_flock(pFile->h, LOCK_EX | LOCK_NB)) { |
| 8743 int tErrno = errno; |
| 8744 /* didn't get, must be busy */ |
| 8745 rc = sqliteErrorFromPosixError(tErrno, SQLITE_IOERR_LOCK); |
| 8746 if( IS_LOCK_ERROR(rc) ){ |
| 8747 storeLastErrno(pFile, tErrno); |
| 8748 } |
| 8749 } else { |
| 8750 /* got it, set the type and return ok */ |
| 8751 pFile->eFileLock = eFileLock; |
| 8752 } |
| 8753 OSTRACE(("LOCK %d %s %s (flock)\n", pFile->h, azFileLock(eFileLock), |
| 8754 rc==SQLITE_OK ? "ok" : "failed")); |
| 8755 #ifdef SQLITE_IGNORE_FLOCK_LOCK_ERRORS |
| 8756 if( (rc & SQLITE_IOERR) == SQLITE_IOERR ){ |
| 8757 rc = SQLITE_BUSY; |
| 8758 } |
| 8759 #endif /* SQLITE_IGNORE_FLOCK_LOCK_ERRORS */ |
| 8760 return rc; |
| 8761 } |
| 8762 |
| 8763 |
| 8764 /* |
| 8765 ** Lower the locking level on file descriptor pFile to eFileLock. eFileLock |
| 8766 ** must be either NO_LOCK or SHARED_LOCK. |
| 8767 ** |
| 8768 ** If the locking level of the file descriptor is already at or below |
| 8769 ** the requested locking level, this routine is a no-op. |
| 8770 */ |
| 8771 static int flockUnlock(sqlite3_file *id, int eFileLock) { |
| 8772 unixFile *pFile = (unixFile*)id; |
| 8773 |
| 8774 assert( pFile ); |
| 8775 OSTRACE(("UNLOCK %d %d was %d pid=%d (flock)\n", pFile->h, eFileLock, |
| 8776 pFile->eFileLock, osGetpid(0))); |
| 8777 assert( eFileLock<=SHARED_LOCK ); |
| 8778 |
| 8779 /* no-op if possible */ |
| 8780 if( pFile->eFileLock==eFileLock ){ |
| 8781 return SQLITE_OK; |
| 8782 } |
| 8783 |
| 8784 /* shared can just be set because we always have an exclusive */ |
| 8785 if (eFileLock==SHARED_LOCK) { |
| 8786 pFile->eFileLock = eFileLock; |
| 8787 return SQLITE_OK; |
| 8788 } |
| 8789 |
| 8790 /* no, really, unlock. */ |
| 8791 if( robust_flock(pFile->h, LOCK_UN) ){ |
| 8792 #ifdef SQLITE_IGNORE_FLOCK_LOCK_ERRORS |
| 8793 return SQLITE_OK; |
| 8794 #endif /* SQLITE_IGNORE_FLOCK_LOCK_ERRORS */ |
| 8795 return SQLITE_IOERR_UNLOCK; |
| 8796 }else{ |
| 8797 pFile->eFileLock = NO_LOCK; |
| 8798 return SQLITE_OK; |
| 8799 } |
| 8800 } |
| 8801 |
| 8802 /* |
| 8803 ** Close a file. |
| 8804 */ |
| 8805 static int flockClose(sqlite3_file *id) { |
| 8806 assert( id!=0 ); |
| 8807 flockUnlock(id, NO_LOCK); |
| 8808 return closeUnixFile(id); |
| 8809 } |
| 8810 |
| 8811 #endif /* SQLITE_ENABLE_LOCKING_STYLE && !OS_VXWORK */ |
| 8812 |
| 8813 /******************* End of the flock lock implementation ********************* |
| 8814 ******************************************************************************/ |
| 8815 |
| 8816 /****************************************************************************** |
| 8817 ************************ Begin Named Semaphore Locking ************************ |
| 8818 ** |
| 8819 ** Named semaphore locking is only supported on VxWorks. |
| 8820 ** |
| 8821 ** Semaphore locking is like dot-lock and flock in that it really only |
| 8822 ** supports EXCLUSIVE locking. Only a single process can read or write |
| 8823 ** the database file at a time. This reduces potential concurrency, but |
| 8824 ** makes the lock implementation much easier. |
| 8825 */ |
| 8826 #if OS_VXWORKS |
| 8827 |
| 8828 /* |
| 8829 ** This routine checks if there is a RESERVED lock held on the specified |
| 8830 ** file by this or any other process. If such a lock is held, set *pResOut |
| 8831 ** to a non-zero value otherwise *pResOut is set to zero. The return value |
| 8832 ** is set to SQLITE_OK unless an I/O error occurs during lock checking. |
| 8833 */ |
| 8834 static int semXCheckReservedLock(sqlite3_file *id, int *pResOut) { |
| 8835 int rc = SQLITE_OK; |
| 8836 int reserved = 0; |
| 8837 unixFile *pFile = (unixFile*)id; |
| 8838 |
| 8839 SimulateIOError( return SQLITE_IOERR_CHECKRESERVEDLOCK; ); |
| 8840 |
| 8841 assert( pFile ); |
| 8842 |
| 8843 /* Check if a thread in this process holds such a lock */ |
| 8844 if( pFile->eFileLock>SHARED_LOCK ){ |
| 8845 reserved = 1; |
| 8846 } |
| 8847 |
| 8848 /* Otherwise see if some other process holds it. */ |
| 8849 if( !reserved ){ |
| 8850 sem_t *pSem = pFile->pInode->pSem; |
| 8851 |
| 8852 if( sem_trywait(pSem)==-1 ){ |
| 8853 int tErrno = errno; |
| 8854 if( EAGAIN != tErrno ){ |
| 8855 rc = sqliteErrorFromPosixError(tErrno, SQLITE_IOERR_CHECKRESERVEDLOCK); |
| 8856 storeLastErrno(pFile, tErrno); |
| 8857 } else { |
| 8858 /* someone else has the lock when we are in NO_LOCK */ |
| 8859 reserved = (pFile->eFileLock < SHARED_LOCK); |
| 8860 } |
| 8861 }else{ |
| 8862 /* we could have it if we want it */ |
| 8863 sem_post(pSem); |
| 8864 } |
| 8865 } |
| 8866 OSTRACE(("TEST WR-LOCK %d %d %d (sem)\n", pFile->h, rc, reserved)); |
| 8867 |
| 8868 *pResOut = reserved; |
| 8869 return rc; |
| 8870 } |
| 8871 |
| 8872 /* |
| 8873 ** Lock the file with the lock specified by parameter eFileLock - one |
| 8874 ** of the following: |
| 8875 ** |
| 8876 ** (1) SHARED_LOCK |
| 8877 ** (2) RESERVED_LOCK |
| 8878 ** (3) PENDING_LOCK |
| 8879 ** (4) EXCLUSIVE_LOCK |
| 8880 ** |
| 8881 ** Sometimes when requesting one lock state, additional lock states |
| 8882 ** are inserted in between. The locking might fail on one of the later |
| 8883 ** transitions leaving the lock state different from what it started but |
| 8884 ** still short of its goal. The following chart shows the allowed |
| 8885 ** transitions and the inserted intermediate states: |
| 8886 ** |
| 8887 ** UNLOCKED -> SHARED |
| 8888 ** SHARED -> RESERVED |
| 8889 ** SHARED -> (PENDING) -> EXCLUSIVE |
| 8890 ** RESERVED -> (PENDING) -> EXCLUSIVE |
| 8891 ** PENDING -> EXCLUSIVE |
| 8892 ** |
| 8893 ** Semaphore locks only really support EXCLUSIVE locks. We track intermediate |
| 8894 ** lock states in the sqlite3_file structure, but all locks SHARED or |
| 8895 ** above are really EXCLUSIVE locks and exclude all other processes from |
| 8896 ** access the file. |
| 8897 ** |
| 8898 ** This routine will only increase a lock. Use the sqlite3OsUnlock() |
| 8899 ** routine to lower a locking level. |
| 8900 */ |
| 8901 static int semXLock(sqlite3_file *id, int eFileLock) { |
| 8902 unixFile *pFile = (unixFile*)id; |
| 8903 sem_t *pSem = pFile->pInode->pSem; |
| 8904 int rc = SQLITE_OK; |
| 8905 |
| 8906 /* if we already have a lock, it is exclusive. |
| 8907 ** Just adjust level and punt on outta here. */ |
| 8908 if (pFile->eFileLock > NO_LOCK) { |
| 8909 pFile->eFileLock = eFileLock; |
| 8910 rc = SQLITE_OK; |
| 8911 goto sem_end_lock; |
| 8912 } |
| 8913 |
| 8914 /* lock semaphore now but bail out when already locked. */ |
| 8915 if( sem_trywait(pSem)==-1 ){ |
| 8916 rc = SQLITE_BUSY; |
| 8917 goto sem_end_lock; |
| 8918 } |
| 8919 |
| 8920 /* got it, set the type and return ok */ |
| 8921 pFile->eFileLock = eFileLock; |
| 8922 |
| 8923 sem_end_lock: |
| 8924 return rc; |
| 8925 } |
| 8926 |
| 8927 /* |
| 8928 ** Lower the locking level on file descriptor pFile to eFileLock. eFileLock |
| 8929 ** must be either NO_LOCK or SHARED_LOCK. |
| 8930 ** |
| 8931 ** If the locking level of the file descriptor is already at or below |
| 8932 ** the requested locking level, this routine is a no-op. |
| 8933 */ |
| 8934 static int semXUnlock(sqlite3_file *id, int eFileLock) { |
| 8935 unixFile *pFile = (unixFile*)id; |
| 8936 sem_t *pSem = pFile->pInode->pSem; |
| 8937 |
| 8938 assert( pFile ); |
| 8939 assert( pSem ); |
| 8940 OSTRACE(("UNLOCK %d %d was %d pid=%d (sem)\n", pFile->h, eFileLock, |
| 8941 pFile->eFileLock, osGetpid(0))); |
| 8942 assert( eFileLock<=SHARED_LOCK ); |
| 8943 |
| 8944 /* no-op if possible */ |
| 8945 if( pFile->eFileLock==eFileLock ){ |
| 8946 return SQLITE_OK; |
| 8947 } |
| 8948 |
| 8949 /* shared can just be set because we always have an exclusive */ |
| 8950 if (eFileLock==SHARED_LOCK) { |
| 8951 pFile->eFileLock = eFileLock; |
| 8952 return SQLITE_OK; |
| 8953 } |
| 8954 |
| 8955 /* no, really unlock. */ |
| 8956 if ( sem_post(pSem)==-1 ) { |
| 8957 int rc, tErrno = errno; |
| 8958 rc = sqliteErrorFromPosixError(tErrno, SQLITE_IOERR_UNLOCK); |
| 8959 if( IS_LOCK_ERROR(rc) ){ |
| 8960 storeLastErrno(pFile, tErrno); |
| 8961 } |
| 8962 return rc; |
| 8963 } |
| 8964 pFile->eFileLock = NO_LOCK; |
| 8965 return SQLITE_OK; |
| 8966 } |
| 8967 |
| 8968 /* |
| 8969 ** Close a file. |
| 8970 */ |
| 8971 static int semXClose(sqlite3_file *id) { |
| 8972 if( id ){ |
| 8973 unixFile *pFile = (unixFile*)id; |
| 8974 semXUnlock(id, NO_LOCK); |
| 8975 assert( pFile ); |
| 8976 unixEnterMutex(); |
| 8977 releaseInodeInfo(pFile); |
| 8978 unixLeaveMutex(); |
| 8979 closeUnixFile(id); |
| 8980 } |
| 8981 return SQLITE_OK; |
| 8982 } |
| 8983 |
| 8984 #endif /* OS_VXWORKS */ |
| 8985 /* |
| 8986 ** Named semaphore locking is only available on VxWorks. |
| 8987 ** |
| 8988 *************** End of the named semaphore lock implementation **************** |
| 8989 ******************************************************************************/ |
| 8990 |
| 8991 |
| 8992 /****************************************************************************** |
| 8993 *************************** Begin AFP Locking ********************************* |
| 8994 ** |
| 8995 ** AFP is the Apple Filing Protocol. AFP is a network filesystem found |
| 8996 ** on Apple Macintosh computers - both OS9 and OSX. |
| 8997 ** |
| 8998 ** Third-party implementations of AFP are available. But this code here |
| 8999 ** only works on OSX. |
| 9000 */ |
| 9001 |
| 9002 #if defined(__APPLE__) && SQLITE_ENABLE_LOCKING_STYLE |
| 9003 /* |
| 9004 ** The afpLockingContext structure contains all afp lock specific state |
| 9005 */ |
| 9006 typedef struct afpLockingContext afpLockingContext; |
| 9007 struct afpLockingContext { |
| 9008 int reserved; |
| 9009 const char *dbPath; /* Name of the open file */ |
| 9010 }; |
| 9011 |
| 9012 struct ByteRangeLockPB2 |
| 9013 { |
| 9014 unsigned long long offset; /* offset to first byte to lock */ |
| 9015 unsigned long long length; /* nbr of bytes to lock */ |
| 9016 unsigned long long retRangeStart; /* nbr of 1st byte locked if successful */ |
| 9017 unsigned char unLockFlag; /* 1 = unlock, 0 = lock */ |
| 9018 unsigned char startEndFlag; /* 1=rel to end of fork, 0=rel to start */ |
| 9019 int fd; /* file desc to assoc this lock with */ |
| 9020 }; |
| 9021 |
| 9022 #define afpfsByteRangeLock2FSCTL _IOWR('z', 23, struct ByteRangeLockPB2) |
| 9023 |
| 9024 /* |
| 9025 ** This is a utility for setting or clearing a bit-range lock on an |
| 9026 ** AFP filesystem. |
| 9027 ** |
| 9028 ** Return SQLITE_OK on success, SQLITE_BUSY on failure. |
| 9029 */ |
| 9030 static int afpSetLock( |
| 9031 const char *path, /* Name of the file to be locked or unlocked */ |
| 9032 unixFile *pFile, /* Open file descriptor on path */ |
| 9033 unsigned long long offset, /* First byte to be locked */ |
| 9034 unsigned long long length, /* Number of bytes to lock */ |
| 9035 int setLockFlag /* True to set lock. False to clear lock */ |
| 9036 ){ |
| 9037 struct ByteRangeLockPB2 pb; |
| 9038 int err; |
| 9039 |
| 9040 pb.unLockFlag = setLockFlag ? 0 : 1; |
| 9041 pb.startEndFlag = 0; |
| 9042 pb.offset = offset; |
| 9043 pb.length = length; |
| 9044 pb.fd = pFile->h; |
| 9045 |
| 9046 OSTRACE(("AFPSETLOCK [%s] for %d%s in range %llx:%llx\n", |
| 9047 (setLockFlag?"ON":"OFF"), pFile->h, (pb.fd==-1?"[testval-1]":""), |
| 9048 offset, length)); |
| 9049 err = fsctl(path, afpfsByteRangeLock2FSCTL, &pb, 0); |
| 9050 if ( err==-1 ) { |
| 9051 int rc; |
| 9052 int tErrno = errno; |
| 9053 OSTRACE(("AFPSETLOCK failed to fsctl() '%s' %d %s\n", |
| 9054 path, tErrno, strerror(tErrno))); |
| 9055 #ifdef SQLITE_IGNORE_AFP_LOCK_ERRORS |
| 9056 rc = SQLITE_BUSY; |
| 9057 #else |
| 9058 rc = sqliteErrorFromPosixError(tErrno, |
| 9059 setLockFlag ? SQLITE_IOERR_LOCK : SQLITE_IOERR_UNLOCK); |
| 9060 #endif /* SQLITE_IGNORE_AFP_LOCK_ERRORS */ |
| 9061 if( IS_LOCK_ERROR(rc) ){ |
| 9062 storeLastErrno(pFile, tErrno); |
| 9063 } |
| 9064 return rc; |
| 9065 } else { |
| 9066 return SQLITE_OK; |
| 9067 } |
| 9068 } |
| 9069 |
| 9070 /* |
| 9071 ** This routine checks if there is a RESERVED lock held on the specified |
| 9072 ** file by this or any other process. If such a lock is held, set *pResOut |
| 9073 ** to a non-zero value otherwise *pResOut is set to zero. The return value |
| 9074 ** is set to SQLITE_OK unless an I/O error occurs during lock checking. |
| 9075 */ |
| 9076 static int afpCheckReservedLock(sqlite3_file *id, int *pResOut){ |
| 9077 int rc = SQLITE_OK; |
| 9078 int reserved = 0; |
| 9079 unixFile *pFile = (unixFile*)id; |
| 9080 afpLockingContext *context; |
| 9081 |
| 9082 SimulateIOError( return SQLITE_IOERR_CHECKRESERVEDLOCK; ); |
| 9083 |
| 9084 assert( pFile ); |
| 9085 context = (afpLockingContext *) pFile->lockingContext; |
| 9086 if( context->reserved ){ |
| 9087 *pResOut = 1; |
| 9088 return SQLITE_OK; |
| 9089 } |
| 9090 unixEnterMutex(); /* Because pFile->pInode is shared across threads */ |
| 9091 |
| 9092 /* Check if a thread in this process holds such a lock */ |
| 9093 if( pFile->pInode->eFileLock>SHARED_LOCK ){ |
| 9094 reserved = 1; |
| 9095 } |
| 9096 |
| 9097 /* Otherwise see if some other process holds it. |
| 9098 */ |
| 9099 if( !reserved ){ |
| 9100 /* lock the RESERVED byte */ |
| 9101 int lrc = afpSetLock(context->dbPath, pFile, RESERVED_BYTE, 1,1); |
| 9102 if( SQLITE_OK==lrc ){ |
| 9103 /* if we succeeded in taking the reserved lock, unlock it to restore |
| 9104 ** the original state */ |
| 9105 lrc = afpSetLock(context->dbPath, pFile, RESERVED_BYTE, 1, 0); |
| 9106 } else { |
| 9107 /* if we failed to get the lock then someone else must have it */ |
| 9108 reserved = 1; |
| 9109 } |
| 9110 if( IS_LOCK_ERROR(lrc) ){ |
| 9111 rc=lrc; |
| 9112 } |
| 9113 } |
| 9114 |
| 9115 unixLeaveMutex(); |
| 9116 OSTRACE(("TEST WR-LOCK %d %d %d (afp)\n", pFile->h, rc, reserved)); |
| 9117 |
| 9118 *pResOut = reserved; |
| 9119 return rc; |
| 9120 } |
| 9121 |
| 9122 /* |
| 9123 ** Lock the file with the lock specified by parameter eFileLock - one |
| 9124 ** of the following: |
| 9125 ** |
| 9126 ** (1) SHARED_LOCK |
| 9127 ** (2) RESERVED_LOCK |
| 9128 ** (3) PENDING_LOCK |
| 9129 ** (4) EXCLUSIVE_LOCK |
| 9130 ** |
| 9131 ** Sometimes when requesting one lock state, additional lock states |
| 9132 ** are inserted in between. The locking might fail on one of the later |
| 9133 ** transitions leaving the lock state different from what it started but |
| 9134 ** still short of its goal. The following chart shows the allowed |
| 9135 ** transitions and the inserted intermediate states: |
| 9136 ** |
| 9137 ** UNLOCKED -> SHARED |
| 9138 ** SHARED -> RESERVED |
| 9139 ** SHARED -> (PENDING) -> EXCLUSIVE |
| 9140 ** RESERVED -> (PENDING) -> EXCLUSIVE |
| 9141 ** PENDING -> EXCLUSIVE |
| 9142 ** |
| 9143 ** This routine will only increase a lock. Use the sqlite3OsUnlock() |
| 9144 ** routine to lower a locking level. |
| 9145 */ |
| 9146 static int afpLock(sqlite3_file *id, int eFileLock){ |
| 9147 int rc = SQLITE_OK; |
| 9148 unixFile *pFile = (unixFile*)id; |
| 9149 unixInodeInfo *pInode = pFile->pInode; |
| 9150 afpLockingContext *context = (afpLockingContext *) pFile->lockingContext; |
| 9151 |
| 9152 assert( pFile ); |
| 9153 OSTRACE(("LOCK %d %s was %s(%s,%d) pid=%d (afp)\n", pFile->h, |
| 9154 azFileLock(eFileLock), azFileLock(pFile->eFileLock), |
| 9155 azFileLock(pInode->eFileLock), pInode->nShared , osGetpid(0))); |
| 9156 |
| 9157 /* If there is already a lock of this type or more restrictive on the |
| 9158 ** unixFile, do nothing. Don't use the afp_end_lock: exit path, as |
| 9159 ** unixEnterMutex() hasn't been called yet. |
| 9160 */ |
| 9161 if( pFile->eFileLock>=eFileLock ){ |
| 9162 OSTRACE(("LOCK %d %s ok (already held) (afp)\n", pFile->h, |
| 9163 azFileLock(eFileLock))); |
| 9164 return SQLITE_OK; |
| 9165 } |
| 9166 |
| 9167 /* Make sure the locking sequence is correct |
| 9168 ** (1) We never move from unlocked to anything higher than shared lock. |
| 9169 ** (2) SQLite never explicitly requests a pendig lock. |
| 9170 ** (3) A shared lock is always held when a reserve lock is requested. |
| 9171 */ |
| 9172 assert( pFile->eFileLock!=NO_LOCK || eFileLock==SHARED_LOCK ); |
| 9173 assert( eFileLock!=PENDING_LOCK ); |
| 9174 assert( eFileLock!=RESERVED_LOCK || pFile->eFileLock==SHARED_LOCK ); |
| 9175 |
| 9176 /* This mutex is needed because pFile->pInode is shared across threads |
| 9177 */ |
| 9178 unixEnterMutex(); |
| 9179 pInode = pFile->pInode; |
| 9180 |
| 9181 /* If some thread using this PID has a lock via a different unixFile* |
| 9182 ** handle that precludes the requested lock, return BUSY. |
| 9183 */ |
| 9184 if( (pFile->eFileLock!=pInode->eFileLock && |
| 9185 (pInode->eFileLock>=PENDING_LOCK || eFileLock>SHARED_LOCK)) |
| 9186 ){ |
| 9187 rc = SQLITE_BUSY; |
| 9188 goto afp_end_lock; |
| 9189 } |
| 9190 |
| 9191 /* If a SHARED lock is requested, and some thread using this PID already |
| 9192 ** has a SHARED or RESERVED lock, then increment reference counts and |
| 9193 ** return SQLITE_OK. |
| 9194 */ |
| 9195 if( eFileLock==SHARED_LOCK && |
| 9196 (pInode->eFileLock==SHARED_LOCK || pInode->eFileLock==RESERVED_LOCK) ){ |
| 9197 assert( eFileLock==SHARED_LOCK ); |
| 9198 assert( pFile->eFileLock==0 ); |
| 9199 assert( pInode->nShared>0 ); |
| 9200 pFile->eFileLock = SHARED_LOCK; |
| 9201 pInode->nShared++; |
| 9202 pInode->nLock++; |
| 9203 goto afp_end_lock; |
| 9204 } |
| 9205 |
| 9206 /* A PENDING lock is needed before acquiring a SHARED lock and before |
| 9207 ** acquiring an EXCLUSIVE lock. For the SHARED lock, the PENDING will |
| 9208 ** be released. |
| 9209 */ |
| 9210 if( eFileLock==SHARED_LOCK |
| 9211 || (eFileLock==EXCLUSIVE_LOCK && pFile->eFileLock<PENDING_LOCK) |
| 9212 ){ |
| 9213 int failed; |
| 9214 failed = afpSetLock(context->dbPath, pFile, PENDING_BYTE, 1, 1); |
| 9215 if (failed) { |
| 9216 rc = failed; |
| 9217 goto afp_end_lock; |
| 9218 } |
| 9219 } |
| 9220 |
| 9221 /* If control gets to this point, then actually go ahead and make |
| 9222 ** operating system calls for the specified lock. |
| 9223 */ |
| 9224 if( eFileLock==SHARED_LOCK ){ |
| 9225 int lrc1, lrc2, lrc1Errno = 0; |
| 9226 long lk, mask; |
| 9227 |
| 9228 assert( pInode->nShared==0 ); |
| 9229 assert( pInode->eFileLock==0 ); |
| 9230 |
| 9231 mask = (sizeof(long)==8) ? LARGEST_INT64 : 0x7fffffff; |
| 9232 /* Now get the read-lock SHARED_LOCK */ |
| 9233 /* note that the quality of the randomness doesn't matter that much */ |
| 9234 lk = random(); |
| 9235 pInode->sharedByte = (lk & mask)%(SHARED_SIZE - 1); |
| 9236 lrc1 = afpSetLock(context->dbPath, pFile, |
| 9237 SHARED_FIRST+pInode->sharedByte, 1, 1); |
| 9238 if( IS_LOCK_ERROR(lrc1) ){ |
| 9239 lrc1Errno = pFile->lastErrno; |
| 9240 } |
| 9241 /* Drop the temporary PENDING lock */ |
| 9242 lrc2 = afpSetLock(context->dbPath, pFile, PENDING_BYTE, 1, 0); |
| 9243 |
| 9244 if( IS_LOCK_ERROR(lrc1) ) { |
| 9245 storeLastErrno(pFile, lrc1Errno); |
| 9246 rc = lrc1; |
| 9247 goto afp_end_lock; |
| 9248 } else if( IS_LOCK_ERROR(lrc2) ){ |
| 9249 rc = lrc2; |
| 9250 goto afp_end_lock; |
| 9251 } else if( lrc1 != SQLITE_OK ) { |
| 9252 rc = lrc1; |
| 9253 } else { |
| 9254 pFile->eFileLock = SHARED_LOCK; |
| 9255 pInode->nLock++; |
| 9256 pInode->nShared = 1; |
| 9257 } |
| 9258 }else if( eFileLock==EXCLUSIVE_LOCK && pInode->nShared>1 ){ |
| 9259 /* We are trying for an exclusive lock but another thread in this |
| 9260 ** same process is still holding a shared lock. */ |
| 9261 rc = SQLITE_BUSY; |
| 9262 }else{ |
| 9263 /* The request was for a RESERVED or EXCLUSIVE lock. It is |
| 9264 ** assumed that there is a SHARED or greater lock on the file |
| 9265 ** already. |
| 9266 */ |
| 9267 int failed = 0; |
| 9268 assert( 0!=pFile->eFileLock ); |
| 9269 if (eFileLock >= RESERVED_LOCK && pFile->eFileLock < RESERVED_LOCK) { |
| 9270 /* Acquire a RESERVED lock */ |
| 9271 failed = afpSetLock(context->dbPath, pFile, RESERVED_BYTE, 1,1); |
| 9272 if( !failed ){ |
| 9273 context->reserved = 1; |
| 9274 } |
| 9275 } |
| 9276 if (!failed && eFileLock == EXCLUSIVE_LOCK) { |
| 9277 /* Acquire an EXCLUSIVE lock */ |
| 9278 |
| 9279 /* Remove the shared lock before trying the range. we'll need to |
| 9280 ** reestablish the shared lock if we can't get the afpUnlock |
| 9281 */ |
| 9282 if( !(failed = afpSetLock(context->dbPath, pFile, SHARED_FIRST + |
| 9283 pInode->sharedByte, 1, 0)) ){ |
| 9284 int failed2 = SQLITE_OK; |
| 9285 /* now attemmpt to get the exclusive lock range */ |
| 9286 failed = afpSetLock(context->dbPath, pFile, SHARED_FIRST, |
| 9287 SHARED_SIZE, 1); |
| 9288 if( failed && (failed2 = afpSetLock(context->dbPath, pFile, |
| 9289 SHARED_FIRST + pInode->sharedByte, 1, 1)) ){ |
| 9290 /* Can't reestablish the shared lock. Sqlite can't deal, this is |
| 9291 ** a critical I/O error |
| 9292 */ |
| 9293 rc = ((failed & SQLITE_IOERR) == SQLITE_IOERR) ? failed2 : |
| 9294 SQLITE_IOERR_LOCK; |
| 9295 goto afp_end_lock; |
| 9296 } |
| 9297 }else{ |
| 9298 rc = failed; |
| 9299 } |
| 9300 } |
| 9301 if( failed ){ |
| 9302 rc = failed; |
| 9303 } |
| 9304 } |
| 9305 |
| 9306 if( rc==SQLITE_OK ){ |
| 9307 pFile->eFileLock = eFileLock; |
| 9308 pInode->eFileLock = eFileLock; |
| 9309 }else if( eFileLock==EXCLUSIVE_LOCK ){ |
| 9310 pFile->eFileLock = PENDING_LOCK; |
| 9311 pInode->eFileLock = PENDING_LOCK; |
| 9312 } |
| 9313 |
| 9314 afp_end_lock: |
| 9315 unixLeaveMutex(); |
| 9316 OSTRACE(("LOCK %d %s %s (afp)\n", pFile->h, azFileLock(eFileLock), |
| 9317 rc==SQLITE_OK ? "ok" : "failed")); |
| 9318 return rc; |
| 9319 } |
| 9320 |
| 9321 /* |
| 9322 ** Lower the locking level on file descriptor pFile to eFileLock. eFileLock |
| 9323 ** must be either NO_LOCK or SHARED_LOCK. |
| 9324 ** |
| 9325 ** If the locking level of the file descriptor is already at or below |
| 9326 ** the requested locking level, this routine is a no-op. |
| 9327 */ |
| 9328 static int afpUnlock(sqlite3_file *id, int eFileLock) { |
| 9329 int rc = SQLITE_OK; |
| 9330 unixFile *pFile = (unixFile*)id; |
| 9331 unixInodeInfo *pInode; |
| 9332 afpLockingContext *context = (afpLockingContext *) pFile->lockingContext; |
| 9333 int skipShared = 0; |
| 9334 #ifdef SQLITE_TEST |
| 9335 int h = pFile->h; |
| 9336 #endif |
| 9337 |
| 9338 assert( pFile ); |
| 9339 OSTRACE(("UNLOCK %d %d was %d(%d,%d) pid=%d (afp)\n", pFile->h, eFileLock, |
| 9340 pFile->eFileLock, pFile->pInode->eFileLock, pFile->pInode->nShared, |
| 9341 osGetpid(0))); |
| 9342 |
| 9343 assert( eFileLock<=SHARED_LOCK ); |
| 9344 if( pFile->eFileLock<=eFileLock ){ |
| 9345 return SQLITE_OK; |
| 9346 } |
| 9347 unixEnterMutex(); |
| 9348 pInode = pFile->pInode; |
| 9349 assert( pInode->nShared!=0 ); |
| 9350 if( pFile->eFileLock>SHARED_LOCK ){ |
| 9351 assert( pInode->eFileLock==pFile->eFileLock ); |
| 9352 SimulateIOErrorBenign(1); |
| 9353 SimulateIOError( h=(-1) ) |
| 9354 SimulateIOErrorBenign(0); |
| 9355 |
| 9356 #ifdef SQLITE_DEBUG |
| 9357 /* When reducing a lock such that other processes can start |
| 9358 ** reading the database file again, make sure that the |
| 9359 ** transaction counter was updated if any part of the database |
| 9360 ** file changed. If the transaction counter is not updated, |
| 9361 ** other connections to the same file might not realize that |
| 9362 ** the file has changed and hence might not know to flush their |
| 9363 ** cache. The use of a stale cache can lead to database corruption. |
| 9364 */ |
| 9365 assert( pFile->inNormalWrite==0 |
| 9366 || pFile->dbUpdate==0 |
| 9367 || pFile->transCntrChng==1 ); |
| 9368 pFile->inNormalWrite = 0; |
| 9369 #endif |
| 9370 |
| 9371 if( pFile->eFileLock==EXCLUSIVE_LOCK ){ |
| 9372 rc = afpSetLock(context->dbPath, pFile, SHARED_FIRST, SHARED_SIZE, 0); |
| 9373 if( rc==SQLITE_OK && (eFileLock==SHARED_LOCK || pInode->nShared>1) ){ |
| 9374 /* only re-establish the shared lock if necessary */ |
| 9375 int sharedLockByte = SHARED_FIRST+pInode->sharedByte; |
| 9376 rc = afpSetLock(context->dbPath, pFile, sharedLockByte, 1, 1); |
| 9377 } else { |
| 9378 skipShared = 1; |
| 9379 } |
| 9380 } |
| 9381 if( rc==SQLITE_OK && pFile->eFileLock>=PENDING_LOCK ){ |
| 9382 rc = afpSetLock(context->dbPath, pFile, PENDING_BYTE, 1, 0); |
| 9383 } |
| 9384 if( rc==SQLITE_OK && pFile->eFileLock>=RESERVED_LOCK && context->reserved ){ |
| 9385 rc = afpSetLock(context->dbPath, pFile, RESERVED_BYTE, 1, 0); |
| 9386 if( !rc ){ |
| 9387 context->reserved = 0; |
| 9388 } |
| 9389 } |
| 9390 if( rc==SQLITE_OK && (eFileLock==SHARED_LOCK || pInode->nShared>1)){ |
| 9391 pInode->eFileLock = SHARED_LOCK; |
| 9392 } |
| 9393 } |
| 9394 if( rc==SQLITE_OK && eFileLock==NO_LOCK ){ |
| 9395 |
| 9396 /* Decrement the shared lock counter. Release the lock using an |
| 9397 ** OS call only when all threads in this same process have released |
| 9398 ** the lock. |
| 9399 */ |
| 9400 unsigned long long sharedLockByte = SHARED_FIRST+pInode->sharedByte; |
| 9401 pInode->nShared--; |
| 9402 if( pInode->nShared==0 ){ |
| 9403 SimulateIOErrorBenign(1); |
| 9404 SimulateIOError( h=(-1) ) |
| 9405 SimulateIOErrorBenign(0); |
| 9406 if( !skipShared ){ |
| 9407 rc = afpSetLock(context->dbPath, pFile, sharedLockByte, 1, 0); |
| 9408 } |
| 9409 if( !rc ){ |
| 9410 pInode->eFileLock = NO_LOCK; |
| 9411 pFile->eFileLock = NO_LOCK; |
| 9412 } |
| 9413 } |
| 9414 if( rc==SQLITE_OK ){ |
| 9415 pInode->nLock--; |
| 9416 assert( pInode->nLock>=0 ); |
| 9417 if( pInode->nLock==0 ){ |
| 9418 closePendingFds(pFile); |
| 9419 } |
| 9420 } |
| 9421 } |
| 9422 |
| 9423 unixLeaveMutex(); |
| 9424 if( rc==SQLITE_OK ) pFile->eFileLock = eFileLock; |
| 9425 return rc; |
| 9426 } |
| 9427 |
| 9428 /* |
| 9429 ** Close a file & cleanup AFP specific locking context |
| 9430 */ |
| 9431 static int afpClose(sqlite3_file *id) { |
| 9432 int rc = SQLITE_OK; |
| 9433 unixFile *pFile = (unixFile*)id; |
| 9434 assert( id!=0 ); |
| 9435 afpUnlock(id, NO_LOCK); |
| 9436 unixEnterMutex(); |
| 9437 if( pFile->pInode && pFile->pInode->nLock ){ |
| 9438 /* If there are outstanding locks, do not actually close the file just |
| 9439 ** yet because that would clear those locks. Instead, add the file |
| 9440 ** descriptor to pInode->aPending. It will be automatically closed when |
| 9441 ** the last lock is cleared. |
| 9442 */ |
| 9443 setPendingFd(pFile); |
| 9444 } |
| 9445 releaseInodeInfo(pFile); |
| 9446 sqlite3_free(pFile->lockingContext); |
| 9447 rc = closeUnixFile(id); |
| 9448 unixLeaveMutex(); |
| 9449 return rc; |
| 9450 } |
| 9451 |
| 9452 #endif /* defined(__APPLE__) && SQLITE_ENABLE_LOCKING_STYLE */ |
| 9453 /* |
| 9454 ** The code above is the AFP lock implementation. The code is specific |
| 9455 ** to MacOSX and does not work on other unix platforms. No alternative |
| 9456 ** is available. If you don't compile for a mac, then the "unix-afp" |
| 9457 ** VFS is not available. |
| 9458 ** |
| 9459 ********************* End of the AFP lock implementation ********************** |
| 9460 ******************************************************************************/ |
| 9461 |
| 9462 /****************************************************************************** |
| 9463 *************************** Begin NFS Locking ********************************/ |
| 9464 |
| 9465 #if defined(__APPLE__) && SQLITE_ENABLE_LOCKING_STYLE |
| 9466 /* |
| 9467 ** Lower the locking level on file descriptor pFile to eFileLock. eFileLock |
| 9468 ** must be either NO_LOCK or SHARED_LOCK. |
| 9469 ** |
| 9470 ** If the locking level of the file descriptor is already at or below |
| 9471 ** the requested locking level, this routine is a no-op. |
| 9472 */ |
| 9473 static int nfsUnlock(sqlite3_file *id, int eFileLock){ |
| 9474 return posixUnlock(id, eFileLock, 1); |
| 9475 } |
| 9476 |
| 9477 #endif /* defined(__APPLE__) && SQLITE_ENABLE_LOCKING_STYLE */ |
| 9478 /* |
| 9479 ** The code above is the NFS lock implementation. The code is specific |
| 9480 ** to MacOSX and does not work on other unix platforms. No alternative |
| 9481 ** is available. |
| 9482 ** |
| 9483 ********************* End of the NFS lock implementation ********************** |
| 9484 ******************************************************************************/ |
| 9485 |
| 9486 /****************************************************************************** |
| 9487 **************** Non-locking sqlite3_file methods ***************************** |
| 9488 ** |
| 9489 ** The next division contains implementations for all methods of the |
| 9490 ** sqlite3_file object other than the locking methods. The locking |
| 9491 ** methods were defined in divisions above (one locking method per |
| 9492 ** division). Those methods that are common to all locking modes |
| 9493 ** are gather together into this division. |
| 9494 */ |
| 9495 |
| 9496 /* |
| 9497 ** Seek to the offset passed as the second argument, then read cnt |
| 9498 ** bytes into pBuf. Return the number of bytes actually read. |
| 9499 ** |
| 9500 ** NB: If you define USE_PREAD or USE_PREAD64, then it might also |
| 9501 ** be necessary to define _XOPEN_SOURCE to be 500. This varies from |
| 9502 ** one system to another. Since SQLite does not define USE_PREAD |
| 9503 ** in any form by default, we will not attempt to define _XOPEN_SOURCE. |
| 9504 ** See tickets #2741 and #2681. |
| 9505 ** |
| 9506 ** To avoid stomping the errno value on a failed read the lastErrno value |
| 9507 ** is set before returning. |
| 9508 */ |
| 9509 static int seekAndRead(unixFile *id, sqlite3_int64 offset, void *pBuf, int cnt){ |
| 9510 int got; |
| 9511 int prior = 0; |
| 9512 #if (!defined(USE_PREAD) && !defined(USE_PREAD64)) |
| 9513 i64 newOffset; |
| 9514 #endif |
| 9515 TIMER_START; |
| 9516 assert( cnt==(cnt&0x1ffff) ); |
| 9517 assert( id->h>2 ); |
| 9518 do{ |
| 9519 #if defined(USE_PREAD) |
| 9520 got = osPread(id->h, pBuf, cnt, offset); |
| 9521 SimulateIOError( got = -1 ); |
| 9522 #elif defined(USE_PREAD64) |
| 9523 got = osPread64(id->h, pBuf, cnt, offset); |
| 9524 SimulateIOError( got = -1 ); |
| 9525 #else |
| 9526 newOffset = lseek(id->h, offset, SEEK_SET); |
| 9527 SimulateIOError( newOffset = -1 ); |
| 9528 if( newOffset<0 ){ |
| 9529 storeLastErrno((unixFile*)id, errno); |
| 9530 return -1; |
| 9531 } |
| 9532 got = osRead(id->h, pBuf, cnt); |
| 9533 #endif |
| 9534 if( got==cnt ) break; |
| 9535 if( got<0 ){ |
| 9536 if( errno==EINTR ){ got = 1; continue; } |
| 9537 prior = 0; |
| 9538 storeLastErrno((unixFile*)id, errno); |
| 9539 break; |
| 9540 }else if( got>0 ){ |
| 9541 cnt -= got; |
| 9542 offset += got; |
| 9543 prior += got; |
| 9544 pBuf = (void*)(got + (char*)pBuf); |
| 9545 } |
| 9546 }while( got>0 ); |
| 9547 TIMER_END; |
| 9548 OSTRACE(("READ %-3d %5d %7lld %llu\n", |
| 9549 id->h, got+prior, offset-prior, TIMER_ELAPSED)); |
| 9550 return got+prior; |
| 9551 } |
| 9552 |
| 9553 /* |
| 9554 ** Read data from a file into a buffer. Return SQLITE_OK if all |
| 9555 ** bytes were read successfully and SQLITE_IOERR if anything goes |
| 9556 ** wrong. |
| 9557 */ |
| 9558 static int unixRead( |
| 9559 sqlite3_file *id, |
| 9560 void *pBuf, |
| 9561 int amt, |
| 9562 sqlite3_int64 offset |
| 9563 ){ |
| 9564 unixFile *pFile = (unixFile *)id; |
| 9565 int got; |
| 9566 assert( id ); |
| 9567 assert( offset>=0 ); |
| 9568 assert( amt>0 ); |
| 9569 |
| 9570 /* If this is a database file (not a journal, master-journal or temp |
| 9571 ** file), the bytes in the locking range should never be read or written. */ |
| 9572 #if 0 |
| 9573 assert( pFile->pUnused==0 |
| 9574 || offset>=PENDING_BYTE+512 |
| 9575 || offset+amt<=PENDING_BYTE |
| 9576 ); |
| 9577 #endif |
| 9578 |
| 9579 #if defined(SQLITE_MMAP_READWRITE) && SQLITE_MAX_MMAP_SIZE>0 |
| 9580 /* Deal with as much of this read request as possible by transfering |
| 9581 ** data from the memory mapping using memcpy(). */ |
| 9582 if( offset<pFile->mmapSize ){ |
| 9583 if( offset+amt <= pFile->mmapSize ){ |
| 9584 memcpy(pBuf, &((u8 *)(pFile->pMapRegion))[offset], amt); |
| 9585 return SQLITE_OK; |
| 9586 }else{ |
| 9587 int nCopy = pFile->mmapSize - offset; |
| 9588 memcpy(pBuf, &((u8 *)(pFile->pMapRegion))[offset], nCopy); |
| 9589 pBuf = &((u8 *)pBuf)[nCopy]; |
| 9590 amt -= nCopy; |
| 9591 offset += nCopy; |
| 9592 } |
| 9593 } |
| 9594 #endif |
| 9595 |
| 9596 got = seekAndRead(pFile, offset, pBuf, amt); |
| 9597 if( got==amt ){ |
| 9598 return SQLITE_OK; |
| 9599 }else if( got<0 ){ |
| 9600 /* lastErrno set by seekAndRead */ |
| 9601 return SQLITE_IOERR_READ; |
| 9602 }else{ |
| 9603 storeLastErrno(pFile, 0); /* not a system error */ |
| 9604 /* Unread parts of the buffer must be zero-filled */ |
| 9605 memset(&((char*)pBuf)[got], 0, amt-got); |
| 9606 return SQLITE_IOERR_SHORT_READ; |
| 9607 } |
| 9608 } |
| 9609 |
| 9610 /* |
| 9611 ** Attempt to seek the file-descriptor passed as the first argument to |
| 9612 ** absolute offset iOff, then attempt to write nBuf bytes of data from |
| 9613 ** pBuf to it. If an error occurs, return -1 and set *piErrno. Otherwise, |
| 9614 ** return the actual number of bytes written (which may be less than |
| 9615 ** nBuf). |
| 9616 */ |
| 9617 static int seekAndWriteFd( |
| 9618 int fd, /* File descriptor to write to */ |
| 9619 i64 iOff, /* File offset to begin writing at */ |
| 9620 const void *pBuf, /* Copy data from this buffer to the file */ |
| 9621 int nBuf, /* Size of buffer pBuf in bytes */ |
| 9622 int *piErrno /* OUT: Error number if error occurs */ |
| 9623 ){ |
| 9624 int rc = 0; /* Value returned by system call */ |
| 9625 |
| 9626 assert( nBuf==(nBuf&0x1ffff) ); |
| 9627 assert( fd>2 ); |
| 9628 assert( piErrno!=0 ); |
| 9629 nBuf &= 0x1ffff; |
| 9630 TIMER_START; |
| 9631 |
| 9632 #if defined(USE_PREAD) |
| 9633 do{ rc = (int)osPwrite(fd, pBuf, nBuf, iOff); }while( rc<0 && errno==EINTR ); |
| 9634 #elif defined(USE_PREAD64) |
| 9635 do{ rc = (int)osPwrite64(fd, pBuf, nBuf, iOff);}while( rc<0 && errno==EINTR); |
| 9636 #else |
| 9637 do{ |
| 9638 i64 iSeek = lseek(fd, iOff, SEEK_SET); |
| 9639 SimulateIOError( iSeek = -1 ); |
| 9640 if( iSeek<0 ){ |
| 9641 rc = -1; |
| 9642 break; |
| 9643 } |
| 9644 rc = osWrite(fd, pBuf, nBuf); |
| 9645 }while( rc<0 && errno==EINTR ); |
| 9646 #endif |
| 9647 |
| 9648 TIMER_END; |
| 9649 OSTRACE(("WRITE %-3d %5d %7lld %llu\n", fd, rc, iOff, TIMER_ELAPSED)); |
| 9650 |
| 9651 if( rc<0 ) *piErrno = errno; |
| 9652 return rc; |
| 9653 } |
| 9654 |
| 9655 |
| 9656 /* |
| 9657 ** Seek to the offset in id->offset then read cnt bytes into pBuf. |
| 9658 ** Return the number of bytes actually read. Update the offset. |
| 9659 ** |
| 9660 ** To avoid stomping the errno value on a failed write the lastErrno value |
| 9661 ** is set before returning. |
| 9662 */ |
| 9663 static int seekAndWrite(unixFile *id, i64 offset, const void *pBuf, int cnt){ |
| 9664 return seekAndWriteFd(id->h, offset, pBuf, cnt, &id->lastErrno); |
| 9665 } |
| 9666 |
| 9667 |
| 9668 /* |
| 9669 ** Write data from a buffer into a file. Return SQLITE_OK on success |
| 9670 ** or some other error code on failure. |
| 9671 */ |
| 9672 static int unixWrite( |
| 9673 sqlite3_file *id, |
| 9674 const void *pBuf, |
| 9675 int amt, |
| 9676 sqlite3_int64 offset |
| 9677 ){ |
| 9678 unixFile *pFile = (unixFile*)id; |
| 9679 int wrote = 0; |
| 9680 assert( id ); |
| 9681 assert( amt>0 ); |
| 9682 |
| 9683 /* If this is a database file (not a journal, master-journal or temp |
| 9684 ** file), the bytes in the locking range should never be read or written. */ |
| 9685 #if 0 |
| 9686 assert( pFile->pUnused==0 |
| 9687 || offset>=PENDING_BYTE+512 |
| 9688 || offset+amt<=PENDING_BYTE |
| 9689 ); |
| 9690 #endif |
| 9691 |
| 9692 #ifdef SQLITE_DEBUG |
| 9693 /* If we are doing a normal write to a database file (as opposed to |
| 9694 ** doing a hot-journal rollback or a write to some file other than a |
| 9695 ** normal database file) then record the fact that the database |
| 9696 ** has changed. If the transaction counter is modified, record that |
| 9697 ** fact too. |
| 9698 */ |
| 9699 if( pFile->inNormalWrite ){ |
| 9700 pFile->dbUpdate = 1; /* The database has been modified */ |
| 9701 if( offset<=24 && offset+amt>=27 ){ |
| 9702 int rc; |
| 9703 char oldCntr[4]; |
| 9704 SimulateIOErrorBenign(1); |
| 9705 rc = seekAndRead(pFile, 24, oldCntr, 4); |
| 9706 SimulateIOErrorBenign(0); |
| 9707 if( rc!=4 || memcmp(oldCntr, &((char*)pBuf)[24-offset], 4)!=0 ){ |
| 9708 pFile->transCntrChng = 1; /* The transaction counter has changed */ |
| 9709 } |
| 9710 } |
| 9711 } |
| 9712 #endif |
| 9713 |
| 9714 #if defined(SQLITE_MMAP_READWRITE) && SQLITE_MAX_MMAP_SIZE>0 |
| 9715 /* Deal with as much of this write request as possible by transfering |
| 9716 ** data from the memory mapping using memcpy(). */ |
| 9717 if( offset<pFile->mmapSize ){ |
| 9718 if( offset+amt <= pFile->mmapSize ){ |
| 9719 memcpy(&((u8 *)(pFile->pMapRegion))[offset], pBuf, amt); |
| 9720 return SQLITE_OK; |
| 9721 }else{ |
| 9722 int nCopy = pFile->mmapSize - offset; |
| 9723 memcpy(&((u8 *)(pFile->pMapRegion))[offset], pBuf, nCopy); |
| 9724 pBuf = &((u8 *)pBuf)[nCopy]; |
| 9725 amt -= nCopy; |
| 9726 offset += nCopy; |
| 9727 } |
| 9728 } |
| 9729 #endif |
| 9730 |
| 9731 while( (wrote = seekAndWrite(pFile, offset, pBuf, amt))<amt && wrote>0 ){ |
| 9732 amt -= wrote; |
| 9733 offset += wrote; |
| 9734 pBuf = &((char*)pBuf)[wrote]; |
| 9735 } |
| 9736 SimulateIOError(( wrote=(-1), amt=1 )); |
| 9737 SimulateDiskfullError(( wrote=0, amt=1 )); |
| 9738 |
| 9739 if( amt>wrote ){ |
| 9740 if( wrote<0 && pFile->lastErrno!=ENOSPC ){ |
| 9741 /* lastErrno set by seekAndWrite */ |
| 9742 return SQLITE_IOERR_WRITE; |
| 9743 }else{ |
| 9744 storeLastErrno(pFile, 0); /* not a system error */ |
| 9745 return SQLITE_FULL; |
| 9746 } |
| 9747 } |
| 9748 |
| 9749 return SQLITE_OK; |
| 9750 } |
| 9751 |
| 9752 #ifdef SQLITE_TEST |
| 9753 /* |
| 9754 ** Count the number of fullsyncs and normal syncs. This is used to test |
| 9755 ** that syncs and fullsyncs are occurring at the right times. |
| 9756 */ |
| 9757 SQLITE_API int sqlite3_sync_count = 0; |
| 9758 SQLITE_API int sqlite3_fullsync_count = 0; |
| 9759 #endif |
| 9760 |
| 9761 /* |
| 9762 ** We do not trust systems to provide a working fdatasync(). Some do. |
| 9763 ** Others do no. To be safe, we will stick with the (slightly slower) |
| 9764 ** fsync(). If you know that your system does support fdatasync() correctly, |
| 9765 ** then simply compile with -Dfdatasync=fdatasync or -DHAVE_FDATASYNC |
| 9766 */ |
| 9767 #if !defined(fdatasync) && !HAVE_FDATASYNC |
| 9768 # define fdatasync fsync |
| 9769 #endif |
| 9770 |
| 9771 /* |
| 9772 ** Define HAVE_FULLFSYNC to 0 or 1 depending on whether or not |
| 9773 ** the F_FULLFSYNC macro is defined. F_FULLFSYNC is currently |
| 9774 ** only available on Mac OS X. But that could change. |
| 9775 */ |
| 9776 #ifdef F_FULLFSYNC |
| 9777 # define HAVE_FULLFSYNC 1 |
| 9778 #else |
| 9779 # define HAVE_FULLFSYNC 0 |
| 9780 #endif |
| 9781 |
| 9782 |
| 9783 /* |
| 9784 ** The fsync() system call does not work as advertised on many |
| 9785 ** unix systems. The following procedure is an attempt to make |
| 9786 ** it work better. |
| 9787 ** |
| 9788 ** The SQLITE_NO_SYNC macro disables all fsync()s. This is useful |
| 9789 ** for testing when we want to run through the test suite quickly. |
| 9790 ** You are strongly advised *not* to deploy with SQLITE_NO_SYNC |
| 9791 ** enabled, however, since with SQLITE_NO_SYNC enabled, an OS crash |
| 9792 ** or power failure will likely corrupt the database file. |
| 9793 ** |
| 9794 ** SQLite sets the dataOnly flag if the size of the file is unchanged. |
| 9795 ** The idea behind dataOnly is that it should only write the file content |
| 9796 ** to disk, not the inode. We only set dataOnly if the file size is |
| 9797 ** unchanged since the file size is part of the inode. However, |
| 9798 ** Ted Ts'o tells us that fdatasync() will also write the inode if the |
| 9799 ** file size has changed. The only real difference between fdatasync() |
| 9800 ** and fsync(), Ted tells us, is that fdatasync() will not flush the |
| 9801 ** inode if the mtime or owner or other inode attributes have changed. |
| 9802 ** We only care about the file size, not the other file attributes, so |
| 9803 ** as far as SQLite is concerned, an fdatasync() is always adequate. |
| 9804 ** So, we always use fdatasync() if it is available, regardless of |
| 9805 ** the value of the dataOnly flag. |
| 9806 */ |
| 9807 static int full_fsync(int fd, int fullSync, int dataOnly){ |
| 9808 int rc; |
| 9809 |
| 9810 /* The following "ifdef/elif/else/" block has the same structure as |
| 9811 ** the one below. It is replicated here solely to avoid cluttering |
| 9812 ** up the real code with the UNUSED_PARAMETER() macros. |
| 9813 */ |
| 9814 #ifdef SQLITE_NO_SYNC |
| 9815 UNUSED_PARAMETER(fd); |
| 9816 UNUSED_PARAMETER(fullSync); |
| 9817 UNUSED_PARAMETER(dataOnly); |
| 9818 #elif HAVE_FULLFSYNC |
| 9819 UNUSED_PARAMETER(dataOnly); |
| 9820 #else |
| 9821 UNUSED_PARAMETER(fullSync); |
| 9822 UNUSED_PARAMETER(dataOnly); |
| 9823 #endif |
| 9824 |
| 9825 /* Record the number of times that we do a normal fsync() and |
| 9826 ** FULLSYNC. This is used during testing to verify that this procedure |
| 9827 ** gets called with the correct arguments. |
| 9828 */ |
| 9829 #ifdef SQLITE_TEST |
| 9830 if( fullSync ) sqlite3_fullsync_count++; |
| 9831 sqlite3_sync_count++; |
| 9832 #endif |
| 9833 |
| 9834 /* If we compiled with the SQLITE_NO_SYNC flag, then syncing is a |
| 9835 ** no-op. But go ahead and call fstat() to validate the file |
| 9836 ** descriptor as we need a method to provoke a failure during |
| 9837 ** coverate testing. |
| 9838 */ |
| 9839 #ifdef SQLITE_NO_SYNC |
| 9840 { |
| 9841 struct stat buf; |
| 9842 rc = osFstat(fd, &buf); |
| 9843 } |
| 9844 #elif HAVE_FULLFSYNC |
| 9845 if( fullSync ){ |
| 9846 rc = osFcntl(fd, F_FULLFSYNC, 0); |
| 9847 }else{ |
| 9848 rc = 1; |
| 9849 } |
| 9850 /* If the FULLFSYNC failed, fall back to attempting an fsync(). |
| 9851 ** It shouldn't be possible for fullfsync to fail on the local |
| 9852 ** file system (on OSX), so failure indicates that FULLFSYNC |
| 9853 ** isn't supported for this file system. So, attempt an fsync |
| 9854 ** and (for now) ignore the overhead of a superfluous fcntl call. |
| 9855 ** It'd be better to detect fullfsync support once and avoid |
| 9856 ** the fcntl call every time sync is called. |
| 9857 */ |
| 9858 if( rc ) rc = fsync(fd); |
| 9859 |
| 9860 #elif defined(__APPLE__) |
| 9861 /* fdatasync() on HFS+ doesn't yet flush the file size if it changed correctly |
| 9862 ** so currently we default to the macro that redefines fdatasync to fsync |
| 9863 */ |
| 9864 rc = fsync(fd); |
| 9865 #else |
| 9866 rc = fdatasync(fd); |
| 9867 #if OS_VXWORKS |
| 9868 if( rc==-1 && errno==ENOTSUP ){ |
| 9869 rc = fsync(fd); |
| 9870 } |
| 9871 #endif /* OS_VXWORKS */ |
| 9872 #endif /* ifdef SQLITE_NO_SYNC elif HAVE_FULLFSYNC */ |
| 9873 |
| 9874 if( OS_VXWORKS && rc!= -1 ){ |
| 9875 rc = 0; |
| 9876 } |
| 9877 return rc; |
| 9878 } |
| 9879 |
| 9880 /* |
| 9881 ** Open a file descriptor to the directory containing file zFilename. |
| 9882 ** If successful, *pFd is set to the opened file descriptor and |
| 9883 ** SQLITE_OK is returned. If an error occurs, either SQLITE_NOMEM |
| 9884 ** or SQLITE_CANTOPEN is returned and *pFd is set to an undefined |
| 9885 ** value. |
| 9886 ** |
| 9887 ** The directory file descriptor is used for only one thing - to |
| 9888 ** fsync() a directory to make sure file creation and deletion events |
| 9889 ** are flushed to disk. Such fsyncs are not needed on newer |
| 9890 ** journaling filesystems, but are required on older filesystems. |
| 9891 ** |
| 9892 ** This routine can be overridden using the xSetSysCall interface. |
| 9893 ** The ability to override this routine was added in support of the |
| 9894 ** chromium sandbox. Opening a directory is a security risk (we are |
| 9895 ** told) so making it overrideable allows the chromium sandbox to |
| 9896 ** replace this routine with a harmless no-op. To make this routine |
| 9897 ** a no-op, replace it with a stub that returns SQLITE_OK but leaves |
| 9898 ** *pFd set to a negative number. |
| 9899 ** |
| 9900 ** If SQLITE_OK is returned, the caller is responsible for closing |
| 9901 ** the file descriptor *pFd using close(). |
| 9902 */ |
| 9903 static int openDirectory(const char *zFilename, int *pFd){ |
| 9904 int ii; |
| 9905 int fd = -1; |
| 9906 char zDirname[MAX_PATHNAME+1]; |
| 9907 |
| 9908 sqlite3_snprintf(MAX_PATHNAME, zDirname, "%s", zFilename); |
| 9909 for(ii=(int)strlen(zDirname); ii>0 && zDirname[ii]!='/'; ii--); |
| 9910 if( ii>0 ){ |
| 9911 zDirname[ii] = '\0'; |
| 9912 }else{ |
| 9913 if( zDirname[0]!='/' ) zDirname[0] = '.'; |
| 9914 zDirname[1] = 0; |
| 9915 } |
| 9916 fd = robust_open(zDirname, O_RDONLY|O_BINARY, 0); |
| 9917 if( fd>=0 ){ |
| 9918 OSTRACE(("OPENDIR %-3d %s\n", fd, zDirname)); |
| 9919 } |
| 9920 *pFd = fd; |
| 9921 if( fd>=0 ) return SQLITE_OK; |
| 9922 return unixLogError(SQLITE_CANTOPEN_BKPT, "openDirectory", zDirname); |
| 9923 } |
| 9924 |
| 9925 /* |
| 9926 ** Make sure all writes to a particular file are committed to disk. |
| 9927 ** |
| 9928 ** If dataOnly==0 then both the file itself and its metadata (file |
| 9929 ** size, access time, etc) are synced. If dataOnly!=0 then only the |
| 9930 ** file data is synced. |
| 9931 ** |
| 9932 ** Under Unix, also make sure that the directory entry for the file |
| 9933 ** has been created by fsync-ing the directory that contains the file. |
| 9934 ** If we do not do this and we encounter a power failure, the directory |
| 9935 ** entry for the journal might not exist after we reboot. The next |
| 9936 ** SQLite to access the file will not know that the journal exists (because |
| 9937 ** the directory entry for the journal was never created) and the transaction |
| 9938 ** will not roll back - possibly leading to database corruption. |
| 9939 */ |
| 9940 static int unixSync(sqlite3_file *id, int flags){ |
| 9941 int rc; |
| 9942 unixFile *pFile = (unixFile*)id; |
| 9943 |
| 9944 int isDataOnly = (flags&SQLITE_SYNC_DATAONLY); |
| 9945 int isFullsync = (flags&0x0F)==SQLITE_SYNC_FULL; |
| 9946 |
| 9947 /* Check that one of SQLITE_SYNC_NORMAL or FULL was passed */ |
| 9948 assert((flags&0x0F)==SQLITE_SYNC_NORMAL |
| 9949 || (flags&0x0F)==SQLITE_SYNC_FULL |
| 9950 ); |
| 9951 |
| 9952 /* Unix cannot, but some systems may return SQLITE_FULL from here. This |
| 9953 ** line is to test that doing so does not cause any problems. |
| 9954 */ |
| 9955 SimulateDiskfullError( return SQLITE_FULL ); |
| 9956 |
| 9957 assert( pFile ); |
| 9958 OSTRACE(("SYNC %-3d\n", pFile->h)); |
| 9959 rc = full_fsync(pFile->h, isFullsync, isDataOnly); |
| 9960 SimulateIOError( rc=1 ); |
| 9961 if( rc ){ |
| 9962 storeLastErrno(pFile, errno); |
| 9963 return unixLogError(SQLITE_IOERR_FSYNC, "full_fsync", pFile->zPath); |
| 9964 } |
| 9965 |
| 9966 /* Also fsync the directory containing the file if the DIRSYNC flag |
| 9967 ** is set. This is a one-time occurrence. Many systems (examples: AIX) |
| 9968 ** are unable to fsync a directory, so ignore errors on the fsync. |
| 9969 */ |
| 9970 if( pFile->ctrlFlags & UNIXFILE_DIRSYNC ){ |
| 9971 int dirfd; |
| 9972 OSTRACE(("DIRSYNC %s (have_fullfsync=%d fullsync=%d)\n", pFile->zPath, |
| 9973 HAVE_FULLFSYNC, isFullsync)); |
| 9974 rc = osOpenDirectory(pFile->zPath, &dirfd); |
| 9975 if( rc==SQLITE_OK ){ |
| 9976 full_fsync(dirfd, 0, 0); |
| 9977 robust_close(pFile, dirfd, __LINE__); |
| 9978 }else{ |
| 9979 assert( rc==SQLITE_CANTOPEN ); |
| 9980 rc = SQLITE_OK; |
| 9981 } |
| 9982 pFile->ctrlFlags &= ~UNIXFILE_DIRSYNC; |
| 9983 } |
| 9984 return rc; |
| 9985 } |
| 9986 |
| 9987 /* |
| 9988 ** Truncate an open file to a specified size |
| 9989 */ |
| 9990 static int unixTruncate(sqlite3_file *id, i64 nByte){ |
| 9991 unixFile *pFile = (unixFile *)id; |
| 9992 int rc; |
| 9993 assert( pFile ); |
| 9994 SimulateIOError( return SQLITE_IOERR_TRUNCATE ); |
| 9995 |
| 9996 /* If the user has configured a chunk-size for this file, truncate the |
| 9997 ** file so that it consists of an integer number of chunks (i.e. the |
| 9998 ** actual file size after the operation may be larger than the requested |
| 9999 ** size). |
| 10000 */ |
| 10001 if( pFile->szChunk>0 ){ |
| 10002 nByte = ((nByte + pFile->szChunk - 1)/pFile->szChunk) * pFile->szChunk; |
| 10003 } |
| 10004 |
| 10005 rc = robust_ftruncate(pFile->h, nByte); |
| 10006 if( rc ){ |
| 10007 storeLastErrno(pFile, errno); |
| 10008 return unixLogError(SQLITE_IOERR_TRUNCATE, "ftruncate", pFile->zPath); |
| 10009 }else{ |
| 10010 #ifdef SQLITE_DEBUG |
| 10011 /* If we are doing a normal write to a database file (as opposed to |
| 10012 ** doing a hot-journal rollback or a write to some file other than a |
| 10013 ** normal database file) and we truncate the file to zero length, |
| 10014 ** that effectively updates the change counter. This might happen |
| 10015 ** when restoring a database using the backup API from a zero-length |
| 10016 ** source. |
| 10017 */ |
| 10018 if( pFile->inNormalWrite && nByte==0 ){ |
| 10019 pFile->transCntrChng = 1; |
| 10020 } |
| 10021 #endif |
| 10022 |
| 10023 #if SQLITE_MAX_MMAP_SIZE>0 |
| 10024 /* If the file was just truncated to a size smaller than the currently |
| 10025 ** mapped region, reduce the effective mapping size as well. SQLite will |
| 10026 ** use read() and write() to access data beyond this point from now on. |
| 10027 */ |
| 10028 if( nByte<pFile->mmapSize ){ |
| 10029 pFile->mmapSize = nByte; |
| 10030 } |
| 10031 #endif |
| 10032 |
| 10033 return SQLITE_OK; |
| 10034 } |
| 10035 } |
| 10036 |
| 10037 /* |
| 10038 ** Determine the current size of a file in bytes |
| 10039 */ |
| 10040 static int unixFileSize(sqlite3_file *id, i64 *pSize){ |
| 10041 int rc; |
| 10042 struct stat buf; |
| 10043 assert( id ); |
| 10044 rc = osFstat(((unixFile*)id)->h, &buf); |
| 10045 SimulateIOError( rc=1 ); |
| 10046 if( rc!=0 ){ |
| 10047 storeLastErrno((unixFile*)id, errno); |
| 10048 return SQLITE_IOERR_FSTAT; |
| 10049 } |
| 10050 *pSize = buf.st_size; |
| 10051 |
| 10052 /* When opening a zero-size database, the findInodeInfo() procedure |
| 10053 ** writes a single byte into that file in order to work around a bug |
| 10054 ** in the OS-X msdos filesystem. In order to avoid problems with upper |
| 10055 ** layers, we need to report this file size as zero even though it is |
| 10056 ** really 1. Ticket #3260. |
| 10057 */ |
| 10058 if( *pSize==1 ) *pSize = 0; |
| 10059 |
| 10060 |
| 10061 return SQLITE_OK; |
| 10062 } |
| 10063 |
| 10064 #if SQLITE_ENABLE_LOCKING_STYLE && defined(__APPLE__) |
| 10065 /* |
| 10066 ** Handler for proxy-locking file-control verbs. Defined below in the |
| 10067 ** proxying locking division. |
| 10068 */ |
| 10069 static int proxyFileControl(sqlite3_file*,int,void*); |
| 10070 #endif |
| 10071 |
| 10072 /* |
| 10073 ** This function is called to handle the SQLITE_FCNTL_SIZE_HINT |
| 10074 ** file-control operation. Enlarge the database to nBytes in size |
| 10075 ** (rounded up to the next chunk-size). If the database is already |
| 10076 ** nBytes or larger, this routine is a no-op. |
| 10077 */ |
| 10078 static int fcntlSizeHint(unixFile *pFile, i64 nByte){ |
| 10079 if( pFile->szChunk>0 ){ |
| 10080 i64 nSize; /* Required file size */ |
| 10081 struct stat buf; /* Used to hold return values of fstat() */ |
| 10082 |
| 10083 if( osFstat(pFile->h, &buf) ){ |
| 10084 return SQLITE_IOERR_FSTAT; |
| 10085 } |
| 10086 |
| 10087 nSize = ((nByte+pFile->szChunk-1) / pFile->szChunk) * pFile->szChunk; |
| 10088 if( nSize>(i64)buf.st_size ){ |
| 10089 |
| 10090 #if defined(HAVE_POSIX_FALLOCATE) && HAVE_POSIX_FALLOCATE |
| 10091 /* The code below is handling the return value of osFallocate() |
| 10092 ** correctly. posix_fallocate() is defined to "returns zero on success, |
| 10093 ** or an error number on failure". See the manpage for details. */ |
| 10094 int err; |
| 10095 do{ |
| 10096 err = osFallocate(pFile->h, buf.st_size, nSize-buf.st_size); |
| 10097 }while( err==EINTR ); |
| 10098 if( err ) return SQLITE_IOERR_WRITE; |
| 10099 #else |
| 10100 /* If the OS does not have posix_fallocate(), fake it. Write a |
| 10101 ** single byte to the last byte in each block that falls entirely |
| 10102 ** within the extended region. Then, if required, a single byte |
| 10103 ** at offset (nSize-1), to set the size of the file correctly. |
| 10104 ** This is a similar technique to that used by glibc on systems |
| 10105 ** that do not have a real fallocate() call. |
| 10106 */ |
| 10107 int nBlk = buf.st_blksize; /* File-system block size */ |
| 10108 int nWrite = 0; /* Number of bytes written by seekAndWrite */ |
| 10109 i64 iWrite; /* Next offset to write to */ |
| 10110 |
| 10111 iWrite = (buf.st_size/nBlk)*nBlk + nBlk - 1; |
| 10112 assert( iWrite>=buf.st_size ); |
| 10113 assert( ((iWrite+1)%nBlk)==0 ); |
| 10114 for(/*no-op*/; iWrite<nSize+nBlk-1; iWrite+=nBlk ){ |
| 10115 if( iWrite>=nSize ) iWrite = nSize - 1; |
| 10116 nWrite = seekAndWrite(pFile, iWrite, "", 1); |
| 10117 if( nWrite!=1 ) return SQLITE_IOERR_WRITE; |
| 10118 } |
| 10119 #endif |
| 10120 } |
| 10121 } |
| 10122 |
| 10123 #if SQLITE_MAX_MMAP_SIZE>0 |
| 10124 if( pFile->mmapSizeMax>0 && nByte>pFile->mmapSize ){ |
| 10125 int rc; |
| 10126 if( pFile->szChunk<=0 ){ |
| 10127 if( robust_ftruncate(pFile->h, nByte) ){ |
| 10128 storeLastErrno(pFile, errno); |
| 10129 return unixLogError(SQLITE_IOERR_TRUNCATE, "ftruncate", pFile->zPath); |
| 10130 } |
| 10131 } |
| 10132 |
| 10133 rc = unixMapfile(pFile, nByte); |
| 10134 return rc; |
| 10135 } |
| 10136 #endif |
| 10137 |
| 10138 return SQLITE_OK; |
| 10139 } |
| 10140 |
| 10141 /* |
| 10142 ** If *pArg is initially negative then this is a query. Set *pArg to |
| 10143 ** 1 or 0 depending on whether or not bit mask of pFile->ctrlFlags is set. |
| 10144 ** |
| 10145 ** If *pArg is 0 or 1, then clear or set the mask bit of pFile->ctrlFlags. |
| 10146 */ |
| 10147 static void unixModeBit(unixFile *pFile, unsigned char mask, int *pArg){ |
| 10148 if( *pArg<0 ){ |
| 10149 *pArg = (pFile->ctrlFlags & mask)!=0; |
| 10150 }else if( (*pArg)==0 ){ |
| 10151 pFile->ctrlFlags &= ~mask; |
| 10152 }else{ |
| 10153 pFile->ctrlFlags |= mask; |
| 10154 } |
| 10155 } |
| 10156 |
| 10157 /* Forward declaration */ |
| 10158 static int unixGetTempname(int nBuf, char *zBuf); |
| 10159 |
| 10160 /* |
| 10161 ** Information and control of an open file handle. |
| 10162 */ |
| 10163 static int unixFileControl(sqlite3_file *id, int op, void *pArg){ |
| 10164 unixFile *pFile = (unixFile*)id; |
| 10165 switch( op ){ |
| 10166 case SQLITE_FCNTL_LOCKSTATE: { |
| 10167 *(int*)pArg = pFile->eFileLock; |
| 10168 return SQLITE_OK; |
| 10169 } |
| 10170 case SQLITE_FCNTL_LAST_ERRNO: { |
| 10171 *(int*)pArg = pFile->lastErrno; |
| 10172 return SQLITE_OK; |
| 10173 } |
| 10174 case SQLITE_FCNTL_CHUNK_SIZE: { |
| 10175 pFile->szChunk = *(int *)pArg; |
| 10176 return SQLITE_OK; |
| 10177 } |
| 10178 case SQLITE_FCNTL_SIZE_HINT: { |
| 10179 int rc; |
| 10180 SimulateIOErrorBenign(1); |
| 10181 rc = fcntlSizeHint(pFile, *(i64 *)pArg); |
| 10182 SimulateIOErrorBenign(0); |
| 10183 return rc; |
| 10184 } |
| 10185 case SQLITE_FCNTL_PERSIST_WAL: { |
| 10186 unixModeBit(pFile, UNIXFILE_PERSIST_WAL, (int*)pArg); |
| 10187 return SQLITE_OK; |
| 10188 } |
| 10189 case SQLITE_FCNTL_POWERSAFE_OVERWRITE: { |
| 10190 unixModeBit(pFile, UNIXFILE_PSOW, (int*)pArg); |
| 10191 return SQLITE_OK; |
| 10192 } |
| 10193 case SQLITE_FCNTL_VFSNAME: { |
| 10194 *(char**)pArg = sqlite3_mprintf("%s", pFile->pVfs->zName); |
| 10195 return SQLITE_OK; |
| 10196 } |
| 10197 case SQLITE_FCNTL_TEMPFILENAME: { |
| 10198 char *zTFile = sqlite3_malloc64( pFile->pVfs->mxPathname ); |
| 10199 if( zTFile ){ |
| 10200 unixGetTempname(pFile->pVfs->mxPathname, zTFile); |
| 10201 *(char**)pArg = zTFile; |
| 10202 } |
| 10203 return SQLITE_OK; |
| 10204 } |
| 10205 case SQLITE_FCNTL_HAS_MOVED: { |
| 10206 *(int*)pArg = fileHasMoved(pFile); |
| 10207 return SQLITE_OK; |
| 10208 } |
| 10209 #if SQLITE_MAX_MMAP_SIZE>0 |
| 10210 case SQLITE_FCNTL_MMAP_SIZE: { |
| 10211 i64 newLimit = *(i64*)pArg; |
| 10212 int rc = SQLITE_OK; |
| 10213 if( newLimit>sqlite3GlobalConfig.mxMmap ){ |
| 10214 newLimit = sqlite3GlobalConfig.mxMmap; |
| 10215 } |
| 10216 *(i64*)pArg = pFile->mmapSizeMax; |
| 10217 if( newLimit>=0 && newLimit!=pFile->mmapSizeMax && pFile->nFetchOut==0 ){ |
| 10218 pFile->mmapSizeMax = newLimit; |
| 10219 if( pFile->mmapSize>0 ){ |
| 10220 unixUnmapfile(pFile); |
| 10221 rc = unixMapfile(pFile, -1); |
| 10222 } |
| 10223 } |
| 10224 return rc; |
| 10225 } |
| 10226 #endif |
| 10227 #ifdef SQLITE_DEBUG |
| 10228 /* The pager calls this method to signal that it has done |
| 10229 ** a rollback and that the database is therefore unchanged and |
| 10230 ** it hence it is OK for the transaction change counter to be |
| 10231 ** unchanged. |
| 10232 */ |
| 10233 case SQLITE_FCNTL_DB_UNCHANGED: { |
| 10234 ((unixFile*)id)->dbUpdate = 0; |
| 10235 return SQLITE_OK; |
| 10236 } |
| 10237 #endif |
| 10238 #if SQLITE_ENABLE_LOCKING_STYLE && defined(__APPLE__) |
| 10239 case SQLITE_FCNTL_SET_LOCKPROXYFILE: |
| 10240 case SQLITE_FCNTL_GET_LOCKPROXYFILE: { |
| 10241 return proxyFileControl(id,op,pArg); |
| 10242 } |
| 10243 #endif /* SQLITE_ENABLE_LOCKING_STYLE && defined(__APPLE__) */ |
| 10244 } |
| 10245 return SQLITE_NOTFOUND; |
| 10246 } |
| 10247 |
| 10248 /* |
| 10249 ** Return the sector size in bytes of the underlying block device for |
| 10250 ** the specified file. This is almost always 512 bytes, but may be |
| 10251 ** larger for some devices. |
| 10252 ** |
| 10253 ** SQLite code assumes this function cannot fail. It also assumes that |
| 10254 ** if two files are created in the same file-system directory (i.e. |
| 10255 ** a database and its journal file) that the sector size will be the |
| 10256 ** same for both. |
| 10257 */ |
| 10258 #ifndef __QNXNTO__ |
| 10259 static int unixSectorSize(sqlite3_file *NotUsed){ |
| 10260 UNUSED_PARAMETER(NotUsed); |
| 10261 return SQLITE_DEFAULT_SECTOR_SIZE; |
| 10262 } |
| 10263 #endif |
| 10264 |
| 10265 /* |
| 10266 ** The following version of unixSectorSize() is optimized for QNX. |
| 10267 */ |
| 10268 #ifdef __QNXNTO__ |
| 10269 #include <sys/dcmd_blk.h> |
| 10270 #include <sys/statvfs.h> |
| 10271 static int unixSectorSize(sqlite3_file *id){ |
| 10272 unixFile *pFile = (unixFile*)id; |
| 10273 if( pFile->sectorSize == 0 ){ |
| 10274 struct statvfs fsInfo; |
| 10275 |
| 10276 /* Set defaults for non-supported filesystems */ |
| 10277 pFile->sectorSize = SQLITE_DEFAULT_SECTOR_SIZE; |
| 10278 pFile->deviceCharacteristics = 0; |
| 10279 if( fstatvfs(pFile->h, &fsInfo) == -1 ) { |
| 10280 return pFile->sectorSize; |
| 10281 } |
| 10282 |
| 10283 if( !strcmp(fsInfo.f_basetype, "tmp") ) { |
| 10284 pFile->sectorSize = fsInfo.f_bsize; |
| 10285 pFile->deviceCharacteristics = |
| 10286 SQLITE_IOCAP_ATOMIC4K | /* All ram filesystem writes are atomic */ |
| 10287 SQLITE_IOCAP_SAFE_APPEND | /* growing the file does not occur until |
| 10288 ** the write succeeds */ |
| 10289 SQLITE_IOCAP_SEQUENTIAL | /* The ram filesystem has no write behind |
| 10290 ** so it is ordered */ |
| 10291 0; |
| 10292 }else if( strstr(fsInfo.f_basetype, "etfs") ){ |
| 10293 pFile->sectorSize = fsInfo.f_bsize; |
| 10294 pFile->deviceCharacteristics = |
| 10295 /* etfs cluster size writes are atomic */ |
| 10296 (pFile->sectorSize / 512 * SQLITE_IOCAP_ATOMIC512) | |
| 10297 SQLITE_IOCAP_SAFE_APPEND | /* growing the file does not occur until |
| 10298 ** the write succeeds */ |
| 10299 SQLITE_IOCAP_SEQUENTIAL | /* The ram filesystem has no write behind |
| 10300 ** so it is ordered */ |
| 10301 0; |
| 10302 }else if( !strcmp(fsInfo.f_basetype, "qnx6") ){ |
| 10303 pFile->sectorSize = fsInfo.f_bsize; |
| 10304 pFile->deviceCharacteristics = |
| 10305 SQLITE_IOCAP_ATOMIC | /* All filesystem writes are atomic */ |
| 10306 SQLITE_IOCAP_SAFE_APPEND | /* growing the file does not occur until |
| 10307 ** the write succeeds */ |
| 10308 SQLITE_IOCAP_SEQUENTIAL | /* The ram filesystem has no write behind |
| 10309 ** so it is ordered */ |
| 10310 0; |
| 10311 }else if( !strcmp(fsInfo.f_basetype, "qnx4") ){ |
| 10312 pFile->sectorSize = fsInfo.f_bsize; |
| 10313 pFile->deviceCharacteristics = |
| 10314 /* full bitset of atomics from max sector size and smaller */ |
| 10315 ((pFile->sectorSize / 512 * SQLITE_IOCAP_ATOMIC512) << 1) - 2 | |
| 10316 SQLITE_IOCAP_SEQUENTIAL | /* The ram filesystem has no write behind |
| 10317 ** so it is ordered */ |
| 10318 0; |
| 10319 }else if( strstr(fsInfo.f_basetype, "dos") ){ |
| 10320 pFile->sectorSize = fsInfo.f_bsize; |
| 10321 pFile->deviceCharacteristics = |
| 10322 /* full bitset of atomics from max sector size and smaller */ |
| 10323 ((pFile->sectorSize / 512 * SQLITE_IOCAP_ATOMIC512) << 1) - 2 | |
| 10324 SQLITE_IOCAP_SEQUENTIAL | /* The ram filesystem has no write behind |
| 10325 ** so it is ordered */ |
| 10326 0; |
| 10327 }else{ |
| 10328 pFile->deviceCharacteristics = |
| 10329 SQLITE_IOCAP_ATOMIC512 | /* blocks are atomic */ |
| 10330 SQLITE_IOCAP_SAFE_APPEND | /* growing the file does not occur until |
| 10331 ** the write succeeds */ |
| 10332 0; |
| 10333 } |
| 10334 } |
| 10335 /* Last chance verification. If the sector size isn't a multiple of 512 |
| 10336 ** then it isn't valid.*/ |
| 10337 if( pFile->sectorSize % 512 != 0 ){ |
| 10338 pFile->deviceCharacteristics = 0; |
| 10339 pFile->sectorSize = SQLITE_DEFAULT_SECTOR_SIZE; |
| 10340 } |
| 10341 return pFile->sectorSize; |
| 10342 } |
| 10343 #endif /* __QNXNTO__ */ |
| 10344 |
| 10345 /* |
| 10346 ** Return the device characteristics for the file. |
| 10347 ** |
| 10348 ** This VFS is set up to return SQLITE_IOCAP_POWERSAFE_OVERWRITE by default. |
| 10349 ** However, that choice is controversial since technically the underlying |
| 10350 ** file system does not always provide powersafe overwrites. (In other |
| 10351 ** words, after a power-loss event, parts of the file that were never |
| 10352 ** written might end up being altered.) However, non-PSOW behavior is very, |
| 10353 ** very rare. And asserting PSOW makes a large reduction in the amount |
| 10354 ** of required I/O for journaling, since a lot of padding is eliminated. |
| 10355 ** Hence, while POWERSAFE_OVERWRITE is on by default, there is a file-control |
| 10356 ** available to turn it off and URI query parameter available to turn it off. |
| 10357 */ |
| 10358 static int unixDeviceCharacteristics(sqlite3_file *id){ |
| 10359 unixFile *p = (unixFile*)id; |
| 10360 int rc = 0; |
| 10361 #ifdef __QNXNTO__ |
| 10362 if( p->sectorSize==0 ) unixSectorSize(id); |
| 10363 rc = p->deviceCharacteristics; |
| 10364 #endif |
| 10365 if( p->ctrlFlags & UNIXFILE_PSOW ){ |
| 10366 rc |= SQLITE_IOCAP_POWERSAFE_OVERWRITE; |
| 10367 } |
| 10368 return rc; |
| 10369 } |
| 10370 |
| 10371 #if !defined(SQLITE_OMIT_WAL) || SQLITE_MAX_MMAP_SIZE>0 |
| 10372 |
| 10373 /* |
| 10374 ** Return the system page size. |
| 10375 ** |
| 10376 ** This function should not be called directly by other code in this file. |
| 10377 ** Instead, it should be called via macro osGetpagesize(). |
| 10378 */ |
| 10379 static int unixGetpagesize(void){ |
| 10380 #if OS_VXWORKS |
| 10381 return 1024; |
| 10382 #elif defined(_BSD_SOURCE) |
| 10383 return getpagesize(); |
| 10384 #else |
| 10385 return (int)sysconf(_SC_PAGESIZE); |
| 10386 #endif |
| 10387 } |
| 10388 |
| 10389 #endif /* !defined(SQLITE_OMIT_WAL) || SQLITE_MAX_MMAP_SIZE>0 */ |
| 10390 |
| 10391 #ifndef SQLITE_OMIT_WAL |
| 10392 |
| 10393 /* |
| 10394 ** Object used to represent an shared memory buffer. |
| 10395 ** |
| 10396 ** When multiple threads all reference the same wal-index, each thread |
| 10397 ** has its own unixShm object, but they all point to a single instance |
| 10398 ** of this unixShmNode object. In other words, each wal-index is opened |
| 10399 ** only once per process. |
| 10400 ** |
| 10401 ** Each unixShmNode object is connected to a single unixInodeInfo object. |
| 10402 ** We could coalesce this object into unixInodeInfo, but that would mean |
| 10403 ** every open file that does not use shared memory (in other words, most |
| 10404 ** open files) would have to carry around this extra information. So |
| 10405 ** the unixInodeInfo object contains a pointer to this unixShmNode object |
| 10406 ** and the unixShmNode object is created only when needed. |
| 10407 ** |
| 10408 ** unixMutexHeld() must be true when creating or destroying |
| 10409 ** this object or while reading or writing the following fields: |
| 10410 ** |
| 10411 ** nRef |
| 10412 ** |
| 10413 ** The following fields are read-only after the object is created: |
| 10414 ** |
| 10415 ** fid |
| 10416 ** zFilename |
| 10417 ** |
| 10418 ** Either unixShmNode.mutex must be held or unixShmNode.nRef==0 and |
| 10419 ** unixMutexHeld() is true when reading or writing any other field |
| 10420 ** in this structure. |
| 10421 */ |
| 10422 struct unixShmNode { |
| 10423 unixInodeInfo *pInode; /* unixInodeInfo that owns this SHM node */ |
| 10424 sqlite3_mutex *mutex; /* Mutex to access this object */ |
| 10425 char *zFilename; /* Name of the mmapped file */ |
| 10426 int h; /* Open file descriptor */ |
| 10427 int szRegion; /* Size of shared-memory regions */ |
| 10428 u16 nRegion; /* Size of array apRegion */ |
| 10429 u8 isReadonly; /* True if read-only */ |
| 10430 char **apRegion; /* Array of mapped shared-memory regions */ |
| 10431 int nRef; /* Number of unixShm objects pointing to this */ |
| 10432 unixShm *pFirst; /* All unixShm objects pointing to this */ |
| 10433 #ifdef SQLITE_DEBUG |
| 10434 u8 exclMask; /* Mask of exclusive locks held */ |
| 10435 u8 sharedMask; /* Mask of shared locks held */ |
| 10436 u8 nextShmId; /* Next available unixShm.id value */ |
| 10437 #endif |
| 10438 }; |
| 10439 |
| 10440 /* |
| 10441 ** Structure used internally by this VFS to record the state of an |
| 10442 ** open shared memory connection. |
| 10443 ** |
| 10444 ** The following fields are initialized when this object is created and |
| 10445 ** are read-only thereafter: |
| 10446 ** |
| 10447 ** unixShm.pFile |
| 10448 ** unixShm.id |
| 10449 ** |
| 10450 ** All other fields are read/write. The unixShm.pFile->mutex must be held |
| 10451 ** while accessing any read/write fields. |
| 10452 */ |
| 10453 struct unixShm { |
| 10454 unixShmNode *pShmNode; /* The underlying unixShmNode object */ |
| 10455 unixShm *pNext; /* Next unixShm with the same unixShmNode */ |
| 10456 u8 hasMutex; /* True if holding the unixShmNode mutex */ |
| 10457 u8 id; /* Id of this connection within its unixShmNode */ |
| 10458 u16 sharedMask; /* Mask of shared locks held */ |
| 10459 u16 exclMask; /* Mask of exclusive locks held */ |
| 10460 }; |
| 10461 |
| 10462 /* |
| 10463 ** Constants used for locking |
| 10464 */ |
| 10465 #define UNIX_SHM_BASE ((22+SQLITE_SHM_NLOCK)*4) /* first lock byte */ |
| 10466 #define UNIX_SHM_DMS (UNIX_SHM_BASE+SQLITE_SHM_NLOCK) /* deadman switch */ |
| 10467 |
| 10468 /* |
| 10469 ** Apply posix advisory locks for all bytes from ofst through ofst+n-1. |
| 10470 ** |
| 10471 ** Locks block if the mask is exactly UNIX_SHM_C and are non-blocking |
| 10472 ** otherwise. |
| 10473 */ |
| 10474 static int unixShmSystemLock( |
| 10475 unixFile *pFile, /* Open connection to the WAL file */ |
| 10476 int lockType, /* F_UNLCK, F_RDLCK, or F_WRLCK */ |
| 10477 int ofst, /* First byte of the locking range */ |
| 10478 int n /* Number of bytes to lock */ |
| 10479 ){ |
| 10480 unixShmNode *pShmNode; /* Apply locks to this open shared-memory segment */ |
| 10481 struct flock f; /* The posix advisory locking structure */ |
| 10482 int rc = SQLITE_OK; /* Result code form fcntl() */ |
| 10483 |
| 10484 /* Access to the unixShmNode object is serialized by the caller */ |
| 10485 pShmNode = pFile->pInode->pShmNode; |
| 10486 assert( sqlite3_mutex_held(pShmNode->mutex) || pShmNode->nRef==0 ); |
| 10487 |
| 10488 /* Shared locks never span more than one byte */ |
| 10489 assert( n==1 || lockType!=F_RDLCK ); |
| 10490 |
| 10491 /* Locks are within range */ |
| 10492 assert( n>=1 && n<=SQLITE_SHM_NLOCK ); |
| 10493 |
| 10494 if( pShmNode->h>=0 ){ |
| 10495 /* Initialize the locking parameters */ |
| 10496 memset(&f, 0, sizeof(f)); |
| 10497 f.l_type = lockType; |
| 10498 f.l_whence = SEEK_SET; |
| 10499 f.l_start = ofst; |
| 10500 f.l_len = n; |
| 10501 |
| 10502 rc = osFcntl(pShmNode->h, F_SETLK, &f); |
| 10503 rc = (rc!=(-1)) ? SQLITE_OK : SQLITE_BUSY; |
| 10504 } |
| 10505 |
| 10506 /* Update the global lock state and do debug tracing */ |
| 10507 #ifdef SQLITE_DEBUG |
| 10508 { u16 mask; |
| 10509 OSTRACE(("SHM-LOCK ")); |
| 10510 mask = ofst>31 ? 0xffff : (1<<(ofst+n)) - (1<<ofst); |
| 10511 if( rc==SQLITE_OK ){ |
| 10512 if( lockType==F_UNLCK ){ |
| 10513 OSTRACE(("unlock %d ok", ofst)); |
| 10514 pShmNode->exclMask &= ~mask; |
| 10515 pShmNode->sharedMask &= ~mask; |
| 10516 }else if( lockType==F_RDLCK ){ |
| 10517 OSTRACE(("read-lock %d ok", ofst)); |
| 10518 pShmNode->exclMask &= ~mask; |
| 10519 pShmNode->sharedMask |= mask; |
| 10520 }else{ |
| 10521 assert( lockType==F_WRLCK ); |
| 10522 OSTRACE(("write-lock %d ok", ofst)); |
| 10523 pShmNode->exclMask |= mask; |
| 10524 pShmNode->sharedMask &= ~mask; |
| 10525 } |
| 10526 }else{ |
| 10527 if( lockType==F_UNLCK ){ |
| 10528 OSTRACE(("unlock %d failed", ofst)); |
| 10529 }else if( lockType==F_RDLCK ){ |
| 10530 OSTRACE(("read-lock failed")); |
| 10531 }else{ |
| 10532 assert( lockType==F_WRLCK ); |
| 10533 OSTRACE(("write-lock %d failed", ofst)); |
| 10534 } |
| 10535 } |
| 10536 OSTRACE((" - afterwards %03x,%03x\n", |
| 10537 pShmNode->sharedMask, pShmNode->exclMask)); |
| 10538 } |
| 10539 #endif |
| 10540 |
| 10541 return rc; |
| 10542 } |
| 10543 |
| 10544 /* |
| 10545 ** Return the minimum number of 32KB shm regions that should be mapped at |
| 10546 ** a time, assuming that each mapping must be an integer multiple of the |
| 10547 ** current system page-size. |
| 10548 ** |
| 10549 ** Usually, this is 1. The exception seems to be systems that are configured |
| 10550 ** to use 64KB pages - in this case each mapping must cover at least two |
| 10551 ** shm regions. |
| 10552 */ |
| 10553 static int unixShmRegionPerMap(void){ |
| 10554 int shmsz = 32*1024; /* SHM region size */ |
| 10555 int pgsz = osGetpagesize(); /* System page size */ |
| 10556 assert( ((pgsz-1)&pgsz)==0 ); /* Page size must be a power of 2 */ |
| 10557 if( pgsz<shmsz ) return 1; |
| 10558 return pgsz/shmsz; |
| 10559 } |
| 10560 |
| 10561 /* |
| 10562 ** Purge the unixShmNodeList list of all entries with unixShmNode.nRef==0. |
| 10563 ** |
| 10564 ** This is not a VFS shared-memory method; it is a utility function called |
| 10565 ** by VFS shared-memory methods. |
| 10566 */ |
| 10567 static void unixShmPurge(unixFile *pFd){ |
| 10568 unixShmNode *p = pFd->pInode->pShmNode; |
| 10569 assert( unixMutexHeld() ); |
| 10570 if( p && ALWAYS(p->nRef==0) ){ |
| 10571 int nShmPerMap = unixShmRegionPerMap(); |
| 10572 int i; |
| 10573 assert( p->pInode==pFd->pInode ); |
| 10574 sqlite3_mutex_free(p->mutex); |
| 10575 for(i=0; i<p->nRegion; i+=nShmPerMap){ |
| 10576 if( p->h>=0 ){ |
| 10577 osMunmap(p->apRegion[i], p->szRegion); |
| 10578 }else{ |
| 10579 sqlite3_free(p->apRegion[i]); |
| 10580 } |
| 10581 } |
| 10582 sqlite3_free(p->apRegion); |
| 10583 if( p->h>=0 ){ |
| 10584 robust_close(pFd, p->h, __LINE__); |
| 10585 p->h = -1; |
| 10586 } |
| 10587 p->pInode->pShmNode = 0; |
| 10588 sqlite3_free(p); |
| 10589 } |
| 10590 } |
| 10591 |
| 10592 /* |
| 10593 ** Open a shared-memory area associated with open database file pDbFd. |
| 10594 ** This particular implementation uses mmapped files. |
| 10595 ** |
| 10596 ** The file used to implement shared-memory is in the same directory |
| 10597 ** as the open database file and has the same name as the open database |
| 10598 ** file with the "-shm" suffix added. For example, if the database file |
| 10599 ** is "/home/user1/config.db" then the file that is created and mmapped |
| 10600 ** for shared memory will be called "/home/user1/config.db-shm". |
| 10601 ** |
| 10602 ** Another approach to is to use files in /dev/shm or /dev/tmp or an |
| 10603 ** some other tmpfs mount. But if a file in a different directory |
| 10604 ** from the database file is used, then differing access permissions |
| 10605 ** or a chroot() might cause two different processes on the same |
| 10606 ** database to end up using different files for shared memory - |
| 10607 ** meaning that their memory would not really be shared - resulting |
| 10608 ** in database corruption. Nevertheless, this tmpfs file usage |
| 10609 ** can be enabled at compile-time using -DSQLITE_SHM_DIRECTORY="/dev/shm" |
| 10610 ** or the equivalent. The use of the SQLITE_SHM_DIRECTORY compile-time |
| 10611 ** option results in an incompatible build of SQLite; builds of SQLite |
| 10612 ** that with differing SQLITE_SHM_DIRECTORY settings attempt to use the |
| 10613 ** same database file at the same time, database corruption will likely |
| 10614 ** result. The SQLITE_SHM_DIRECTORY compile-time option is considered |
| 10615 ** "unsupported" and may go away in a future SQLite release. |
| 10616 ** |
| 10617 ** When opening a new shared-memory file, if no other instances of that |
| 10618 ** file are currently open, in this process or in other processes, then |
| 10619 ** the file must be truncated to zero length or have its header cleared. |
| 10620 ** |
| 10621 ** If the original database file (pDbFd) is using the "unix-excl" VFS |
| 10622 ** that means that an exclusive lock is held on the database file and |
| 10623 ** that no other processes are able to read or write the database. In |
| 10624 ** that case, we do not really need shared memory. No shared memory |
| 10625 ** file is created. The shared memory will be simulated with heap memory. |
| 10626 */ |
| 10627 static int unixOpenSharedMemory(unixFile *pDbFd){ |
| 10628 struct unixShm *p = 0; /* The connection to be opened */ |
| 10629 struct unixShmNode *pShmNode; /* The underlying mmapped file */ |
| 10630 int rc; /* Result code */ |
| 10631 unixInodeInfo *pInode; /* The inode of fd */ |
| 10632 char *zShmFilename; /* Name of the file used for SHM */ |
| 10633 int nShmFilename; /* Size of the SHM filename in bytes */ |
| 10634 |
| 10635 /* Allocate space for the new unixShm object. */ |
| 10636 p = sqlite3_malloc64( sizeof(*p) ); |
| 10637 if( p==0 ) return SQLITE_NOMEM; |
| 10638 memset(p, 0, sizeof(*p)); |
| 10639 assert( pDbFd->pShm==0 ); |
| 10640 |
| 10641 /* Check to see if a unixShmNode object already exists. Reuse an existing |
| 10642 ** one if present. Create a new one if necessary. |
| 10643 */ |
| 10644 unixEnterMutex(); |
| 10645 pInode = pDbFd->pInode; |
| 10646 pShmNode = pInode->pShmNode; |
| 10647 if( pShmNode==0 ){ |
| 10648 struct stat sStat; /* fstat() info for database file */ |
| 10649 #ifndef SQLITE_SHM_DIRECTORY |
| 10650 const char *zBasePath = pDbFd->zPath; |
| 10651 #endif |
| 10652 |
| 10653 /* Call fstat() to figure out the permissions on the database file. If |
| 10654 ** a new *-shm file is created, an attempt will be made to create it |
| 10655 ** with the same permissions. |
| 10656 */ |
| 10657 if( osFstat(pDbFd->h, &sStat) ){ |
| 10658 rc = SQLITE_IOERR_FSTAT; |
| 10659 goto shm_open_err; |
| 10660 } |
| 10661 |
| 10662 #ifdef SQLITE_SHM_DIRECTORY |
| 10663 nShmFilename = sizeof(SQLITE_SHM_DIRECTORY) + 31; |
| 10664 #else |
| 10665 nShmFilename = 6 + (int)strlen(zBasePath); |
| 10666 #endif |
| 10667 pShmNode = sqlite3_malloc64( sizeof(*pShmNode) + nShmFilename ); |
| 10668 if( pShmNode==0 ){ |
| 10669 rc = SQLITE_NOMEM; |
| 10670 goto shm_open_err; |
| 10671 } |
| 10672 memset(pShmNode, 0, sizeof(*pShmNode)+nShmFilename); |
| 10673 zShmFilename = pShmNode->zFilename = (char*)&pShmNode[1]; |
| 10674 #ifdef SQLITE_SHM_DIRECTORY |
| 10675 sqlite3_snprintf(nShmFilename, zShmFilename, |
| 10676 SQLITE_SHM_DIRECTORY "/sqlite-shm-%x-%x", |
| 10677 (u32)sStat.st_ino, (u32)sStat.st_dev); |
| 10678 #else |
| 10679 sqlite3_snprintf(nShmFilename, zShmFilename, "%s-shm", zBasePath); |
| 10680 sqlite3FileSuffix3(pDbFd->zPath, zShmFilename); |
| 10681 #endif |
| 10682 pShmNode->h = -1; |
| 10683 pDbFd->pInode->pShmNode = pShmNode; |
| 10684 pShmNode->pInode = pDbFd->pInode; |
| 10685 pShmNode->mutex = sqlite3_mutex_alloc(SQLITE_MUTEX_FAST); |
| 10686 if( pShmNode->mutex==0 ){ |
| 10687 rc = SQLITE_NOMEM; |
| 10688 goto shm_open_err; |
| 10689 } |
| 10690 |
| 10691 if( pInode->bProcessLock==0 ){ |
| 10692 int openFlags = O_RDWR | O_CREAT; |
| 10693 if( sqlite3_uri_boolean(pDbFd->zPath, "readonly_shm", 0) ){ |
| 10694 openFlags = O_RDONLY; |
| 10695 pShmNode->isReadonly = 1; |
| 10696 } |
| 10697 pShmNode->h = robust_open(zShmFilename, openFlags, (sStat.st_mode&0777)); |
| 10698 if( pShmNode->h<0 ){ |
| 10699 rc = unixLogError(SQLITE_CANTOPEN_BKPT, "open", zShmFilename); |
| 10700 goto shm_open_err; |
| 10701 } |
| 10702 |
| 10703 /* If this process is running as root, make sure that the SHM file |
| 10704 ** is owned by the same user that owns the original database. Otherwise, |
| 10705 ** the original owner will not be able to connect. |
| 10706 */ |
| 10707 robustFchown(pShmNode->h, sStat.st_uid, sStat.st_gid); |
| 10708 |
| 10709 /* Check to see if another process is holding the dead-man switch. |
| 10710 ** If not, truncate the file to zero length. |
| 10711 */ |
| 10712 rc = SQLITE_OK; |
| 10713 if( unixShmSystemLock(pDbFd, F_WRLCK, UNIX_SHM_DMS, 1)==SQLITE_OK ){ |
| 10714 if( robust_ftruncate(pShmNode->h, 0) ){ |
| 10715 rc = unixLogError(SQLITE_IOERR_SHMOPEN, "ftruncate", zShmFilename); |
| 10716 } |
| 10717 } |
| 10718 if( rc==SQLITE_OK ){ |
| 10719 rc = unixShmSystemLock(pDbFd, F_RDLCK, UNIX_SHM_DMS, 1); |
| 10720 } |
| 10721 if( rc ) goto shm_open_err; |
| 10722 } |
| 10723 } |
| 10724 |
| 10725 /* Make the new connection a child of the unixShmNode */ |
| 10726 p->pShmNode = pShmNode; |
| 10727 #ifdef SQLITE_DEBUG |
| 10728 p->id = pShmNode->nextShmId++; |
| 10729 #endif |
| 10730 pShmNode->nRef++; |
| 10731 pDbFd->pShm = p; |
| 10732 unixLeaveMutex(); |
| 10733 |
| 10734 /* The reference count on pShmNode has already been incremented under |
| 10735 ** the cover of the unixEnterMutex() mutex and the pointer from the |
| 10736 ** new (struct unixShm) object to the pShmNode has been set. All that is |
| 10737 ** left to do is to link the new object into the linked list starting |
| 10738 ** at pShmNode->pFirst. This must be done while holding the pShmNode->mutex |
| 10739 ** mutex. |
| 10740 */ |
| 10741 sqlite3_mutex_enter(pShmNode->mutex); |
| 10742 p->pNext = pShmNode->pFirst; |
| 10743 pShmNode->pFirst = p; |
| 10744 sqlite3_mutex_leave(pShmNode->mutex); |
| 10745 return SQLITE_OK; |
| 10746 |
| 10747 /* Jump here on any error */ |
| 10748 shm_open_err: |
| 10749 unixShmPurge(pDbFd); /* This call frees pShmNode if required */ |
| 10750 sqlite3_free(p); |
| 10751 unixLeaveMutex(); |
| 10752 return rc; |
| 10753 } |
| 10754 |
| 10755 /* |
| 10756 ** This function is called to obtain a pointer to region iRegion of the |
| 10757 ** shared-memory associated with the database file fd. Shared-memory regions |
| 10758 ** are numbered starting from zero. Each shared-memory region is szRegion |
| 10759 ** bytes in size. |
| 10760 ** |
| 10761 ** If an error occurs, an error code is returned and *pp is set to NULL. |
| 10762 ** |
| 10763 ** Otherwise, if the bExtend parameter is 0 and the requested shared-memory |
| 10764 ** region has not been allocated (by any client, including one running in a |
| 10765 ** separate process), then *pp is set to NULL and SQLITE_OK returned. If |
| 10766 ** bExtend is non-zero and the requested shared-memory region has not yet |
| 10767 ** been allocated, it is allocated by this function. |
| 10768 ** |
| 10769 ** If the shared-memory region has already been allocated or is allocated by |
| 10770 ** this call as described above, then it is mapped into this processes |
| 10771 ** address space (if it is not already), *pp is set to point to the mapped |
| 10772 ** memory and SQLITE_OK returned. |
| 10773 */ |
| 10774 static int unixShmMap( |
| 10775 sqlite3_file *fd, /* Handle open on database file */ |
| 10776 int iRegion, /* Region to retrieve */ |
| 10777 int szRegion, /* Size of regions */ |
| 10778 int bExtend, /* True to extend file if necessary */ |
| 10779 void volatile **pp /* OUT: Mapped memory */ |
| 10780 ){ |
| 10781 unixFile *pDbFd = (unixFile*)fd; |
| 10782 unixShm *p; |
| 10783 unixShmNode *pShmNode; |
| 10784 int rc = SQLITE_OK; |
| 10785 int nShmPerMap = unixShmRegionPerMap(); |
| 10786 int nReqRegion; |
| 10787 |
| 10788 /* If the shared-memory file has not yet been opened, open it now. */ |
| 10789 if( pDbFd->pShm==0 ){ |
| 10790 rc = unixOpenSharedMemory(pDbFd); |
| 10791 if( rc!=SQLITE_OK ) return rc; |
| 10792 } |
| 10793 |
| 10794 p = pDbFd->pShm; |
| 10795 pShmNode = p->pShmNode; |
| 10796 sqlite3_mutex_enter(pShmNode->mutex); |
| 10797 assert( szRegion==pShmNode->szRegion || pShmNode->nRegion==0 ); |
| 10798 assert( pShmNode->pInode==pDbFd->pInode ); |
| 10799 assert( pShmNode->h>=0 || pDbFd->pInode->bProcessLock==1 ); |
| 10800 assert( pShmNode->h<0 || pDbFd->pInode->bProcessLock==0 ); |
| 10801 |
| 10802 /* Minimum number of regions required to be mapped. */ |
| 10803 nReqRegion = ((iRegion+nShmPerMap) / nShmPerMap) * nShmPerMap; |
| 10804 |
| 10805 if( pShmNode->nRegion<nReqRegion ){ |
| 10806 char **apNew; /* New apRegion[] array */ |
| 10807 int nByte = nReqRegion*szRegion; /* Minimum required file size */ |
| 10808 struct stat sStat; /* Used by fstat() */ |
| 10809 |
| 10810 pShmNode->szRegion = szRegion; |
| 10811 |
| 10812 if( pShmNode->h>=0 ){ |
| 10813 /* The requested region is not mapped into this processes address space. |
| 10814 ** Check to see if it has been allocated (i.e. if the wal-index file is |
| 10815 ** large enough to contain the requested region). |
| 10816 */ |
| 10817 if( osFstat(pShmNode->h, &sStat) ){ |
| 10818 rc = SQLITE_IOERR_SHMSIZE; |
| 10819 goto shmpage_out; |
| 10820 } |
| 10821 |
| 10822 if( sStat.st_size<nByte ){ |
| 10823 /* The requested memory region does not exist. If bExtend is set to |
| 10824 ** false, exit early. *pp will be set to NULL and SQLITE_OK returned. |
| 10825 */ |
| 10826 if( !bExtend ){ |
| 10827 goto shmpage_out; |
| 10828 } |
| 10829 |
| 10830 /* Alternatively, if bExtend is true, extend the file. Do this by |
| 10831 ** writing a single byte to the end of each (OS) page being |
| 10832 ** allocated or extended. Technically, we need only write to the |
| 10833 ** last page in order to extend the file. But writing to all new |
| 10834 ** pages forces the OS to allocate them immediately, which reduces |
| 10835 ** the chances of SIGBUS while accessing the mapped region later on. |
| 10836 */ |
| 10837 else{ |
| 10838 static const int pgsz = 4096; |
| 10839 int iPg; |
| 10840 |
| 10841 /* Write to the last byte of each newly allocated or extended page */ |
| 10842 assert( (nByte % pgsz)==0 ); |
| 10843 for(iPg=(sStat.st_size/pgsz); iPg<(nByte/pgsz); iPg++){ |
| 10844 int x = 0; |
| 10845 if( seekAndWriteFd(pShmNode->h, iPg*pgsz + pgsz-1, "", 1, &x)!=1 ){ |
| 10846 const char *zFile = pShmNode->zFilename; |
| 10847 rc = unixLogError(SQLITE_IOERR_SHMSIZE, "write", zFile); |
| 10848 goto shmpage_out; |
| 10849 } |
| 10850 } |
| 10851 } |
| 10852 } |
| 10853 } |
| 10854 |
| 10855 /* Map the requested memory region into this processes address space. */ |
| 10856 apNew = (char **)sqlite3_realloc( |
| 10857 pShmNode->apRegion, nReqRegion*sizeof(char *) |
| 10858 ); |
| 10859 if( !apNew ){ |
| 10860 rc = SQLITE_IOERR_NOMEM; |
| 10861 goto shmpage_out; |
| 10862 } |
| 10863 pShmNode->apRegion = apNew; |
| 10864 while( pShmNode->nRegion<nReqRegion ){ |
| 10865 int nMap = szRegion*nShmPerMap; |
| 10866 int i; |
| 10867 void *pMem; |
| 10868 if( pShmNode->h>=0 ){ |
| 10869 pMem = osMmap(0, nMap, |
| 10870 pShmNode->isReadonly ? PROT_READ : PROT_READ|PROT_WRITE, |
| 10871 MAP_SHARED, pShmNode->h, szRegion*(i64)pShmNode->nRegion |
| 10872 ); |
| 10873 if( pMem==MAP_FAILED ){ |
| 10874 rc = unixLogError(SQLITE_IOERR_SHMMAP, "mmap", pShmNode->zFilename); |
| 10875 goto shmpage_out; |
| 10876 } |
| 10877 }else{ |
| 10878 pMem = sqlite3_malloc64(szRegion); |
| 10879 if( pMem==0 ){ |
| 10880 rc = SQLITE_NOMEM; |
| 10881 goto shmpage_out; |
| 10882 } |
| 10883 memset(pMem, 0, szRegion); |
| 10884 } |
| 10885 |
| 10886 for(i=0; i<nShmPerMap; i++){ |
| 10887 pShmNode->apRegion[pShmNode->nRegion+i] = &((char*)pMem)[szRegion*i]; |
| 10888 } |
| 10889 pShmNode->nRegion += nShmPerMap; |
| 10890 } |
| 10891 } |
| 10892 |
| 10893 shmpage_out: |
| 10894 if( pShmNode->nRegion>iRegion ){ |
| 10895 *pp = pShmNode->apRegion[iRegion]; |
| 10896 }else{ |
| 10897 *pp = 0; |
| 10898 } |
| 10899 if( pShmNode->isReadonly && rc==SQLITE_OK ) rc = SQLITE_READONLY; |
| 10900 sqlite3_mutex_leave(pShmNode->mutex); |
| 10901 return rc; |
| 10902 } |
| 10903 |
| 10904 /* |
| 10905 ** Change the lock state for a shared-memory segment. |
| 10906 ** |
| 10907 ** Note that the relationship between SHAREd and EXCLUSIVE locks is a little |
| 10908 ** different here than in posix. In xShmLock(), one can go from unlocked |
| 10909 ** to shared and back or from unlocked to exclusive and back. But one may |
| 10910 ** not go from shared to exclusive or from exclusive to shared. |
| 10911 */ |
| 10912 static int unixShmLock( |
| 10913 sqlite3_file *fd, /* Database file holding the shared memory */ |
| 10914 int ofst, /* First lock to acquire or release */ |
| 10915 int n, /* Number of locks to acquire or release */ |
| 10916 int flags /* What to do with the lock */ |
| 10917 ){ |
| 10918 unixFile *pDbFd = (unixFile*)fd; /* Connection holding shared memory */ |
| 10919 unixShm *p = pDbFd->pShm; /* The shared memory being locked */ |
| 10920 unixShm *pX; /* For looping over all siblings */ |
| 10921 unixShmNode *pShmNode = p->pShmNode; /* The underlying file iNode */ |
| 10922 int rc = SQLITE_OK; /* Result code */ |
| 10923 u16 mask; /* Mask of locks to take or release */ |
| 10924 |
| 10925 assert( pShmNode==pDbFd->pInode->pShmNode ); |
| 10926 assert( pShmNode->pInode==pDbFd->pInode ); |
| 10927 assert( ofst>=0 && ofst+n<=SQLITE_SHM_NLOCK ); |
| 10928 assert( n>=1 ); |
| 10929 assert( flags==(SQLITE_SHM_LOCK | SQLITE_SHM_SHARED) |
| 10930 || flags==(SQLITE_SHM_LOCK | SQLITE_SHM_EXCLUSIVE) |
| 10931 || flags==(SQLITE_SHM_UNLOCK | SQLITE_SHM_SHARED) |
| 10932 || flags==(SQLITE_SHM_UNLOCK | SQLITE_SHM_EXCLUSIVE) ); |
| 10933 assert( n==1 || (flags & SQLITE_SHM_EXCLUSIVE)!=0 ); |
| 10934 assert( pShmNode->h>=0 || pDbFd->pInode->bProcessLock==1 ); |
| 10935 assert( pShmNode->h<0 || pDbFd->pInode->bProcessLock==0 ); |
| 10936 |
| 10937 mask = (1<<(ofst+n)) - (1<<ofst); |
| 10938 assert( n>1 || mask==(1<<ofst) ); |
| 10939 sqlite3_mutex_enter(pShmNode->mutex); |
| 10940 if( flags & SQLITE_SHM_UNLOCK ){ |
| 10941 u16 allMask = 0; /* Mask of locks held by siblings */ |
| 10942 |
| 10943 /* See if any siblings hold this same lock */ |
| 10944 for(pX=pShmNode->pFirst; pX; pX=pX->pNext){ |
| 10945 if( pX==p ) continue; |
| 10946 assert( (pX->exclMask & (p->exclMask|p->sharedMask))==0 ); |
| 10947 allMask |= pX->sharedMask; |
| 10948 } |
| 10949 |
| 10950 /* Unlock the system-level locks */ |
| 10951 if( (mask & allMask)==0 ){ |
| 10952 rc = unixShmSystemLock(pDbFd, F_UNLCK, ofst+UNIX_SHM_BASE, n); |
| 10953 }else{ |
| 10954 rc = SQLITE_OK; |
| 10955 } |
| 10956 |
| 10957 /* Undo the local locks */ |
| 10958 if( rc==SQLITE_OK ){ |
| 10959 p->exclMask &= ~mask; |
| 10960 p->sharedMask &= ~mask; |
| 10961 } |
| 10962 }else if( flags & SQLITE_SHM_SHARED ){ |
| 10963 u16 allShared = 0; /* Union of locks held by connections other than "p" */ |
| 10964 |
| 10965 /* Find out which shared locks are already held by sibling connections. |
| 10966 ** If any sibling already holds an exclusive lock, go ahead and return |
| 10967 ** SQLITE_BUSY. |
| 10968 */ |
| 10969 for(pX=pShmNode->pFirst; pX; pX=pX->pNext){ |
| 10970 if( (pX->exclMask & mask)!=0 ){ |
| 10971 rc = SQLITE_BUSY; |
| 10972 break; |
| 10973 } |
| 10974 allShared |= pX->sharedMask; |
| 10975 } |
| 10976 |
| 10977 /* Get shared locks at the system level, if necessary */ |
| 10978 if( rc==SQLITE_OK ){ |
| 10979 if( (allShared & mask)==0 ){ |
| 10980 rc = unixShmSystemLock(pDbFd, F_RDLCK, ofst+UNIX_SHM_BASE, n); |
| 10981 }else{ |
| 10982 rc = SQLITE_OK; |
| 10983 } |
| 10984 } |
| 10985 |
| 10986 /* Get the local shared locks */ |
| 10987 if( rc==SQLITE_OK ){ |
| 10988 p->sharedMask |= mask; |
| 10989 } |
| 10990 }else{ |
| 10991 /* Make sure no sibling connections hold locks that will block this |
| 10992 ** lock. If any do, return SQLITE_BUSY right away. |
| 10993 */ |
| 10994 for(pX=pShmNode->pFirst; pX; pX=pX->pNext){ |
| 10995 if( (pX->exclMask & mask)!=0 || (pX->sharedMask & mask)!=0 ){ |
| 10996 rc = SQLITE_BUSY; |
| 10997 break; |
| 10998 } |
| 10999 } |
| 11000 |
| 11001 /* Get the exclusive locks at the system level. Then if successful |
| 11002 ** also mark the local connection as being locked. |
| 11003 */ |
| 11004 if( rc==SQLITE_OK ){ |
| 11005 rc = unixShmSystemLock(pDbFd, F_WRLCK, ofst+UNIX_SHM_BASE, n); |
| 11006 if( rc==SQLITE_OK ){ |
| 11007 assert( (p->sharedMask & mask)==0 ); |
| 11008 p->exclMask |= mask; |
| 11009 } |
| 11010 } |
| 11011 } |
| 11012 sqlite3_mutex_leave(pShmNode->mutex); |
| 11013 OSTRACE(("SHM-LOCK shmid-%d, pid-%d got %03x,%03x\n", |
| 11014 p->id, osGetpid(0), p->sharedMask, p->exclMask)); |
| 11015 return rc; |
| 11016 } |
| 11017 |
| 11018 /* |
| 11019 ** Implement a memory barrier or memory fence on shared memory. |
| 11020 ** |
| 11021 ** All loads and stores begun before the barrier must complete before |
| 11022 ** any load or store begun after the barrier. |
| 11023 */ |
| 11024 static void unixShmBarrier( |
| 11025 sqlite3_file *fd /* Database file holding the shared memory */ |
| 11026 ){ |
| 11027 UNUSED_PARAMETER(fd); |
| 11028 sqlite3MemoryBarrier(); /* compiler-defined memory barrier */ |
| 11029 unixEnterMutex(); /* Also mutex, for redundancy */ |
| 11030 unixLeaveMutex(); |
| 11031 } |
| 11032 |
| 11033 /* |
| 11034 ** Close a connection to shared-memory. Delete the underlying |
| 11035 ** storage if deleteFlag is true. |
| 11036 ** |
| 11037 ** If there is no shared memory associated with the connection then this |
| 11038 ** routine is a harmless no-op. |
| 11039 */ |
| 11040 static int unixShmUnmap( |
| 11041 sqlite3_file *fd, /* The underlying database file */ |
| 11042 int deleteFlag /* Delete shared-memory if true */ |
| 11043 ){ |
| 11044 unixShm *p; /* The connection to be closed */ |
| 11045 unixShmNode *pShmNode; /* The underlying shared-memory file */ |
| 11046 unixShm **pp; /* For looping over sibling connections */ |
| 11047 unixFile *pDbFd; /* The underlying database file */ |
| 11048 |
| 11049 pDbFd = (unixFile*)fd; |
| 11050 p = pDbFd->pShm; |
| 11051 if( p==0 ) return SQLITE_OK; |
| 11052 pShmNode = p->pShmNode; |
| 11053 |
| 11054 assert( pShmNode==pDbFd->pInode->pShmNode ); |
| 11055 assert( pShmNode->pInode==pDbFd->pInode ); |
| 11056 |
| 11057 /* Remove connection p from the set of connections associated |
| 11058 ** with pShmNode */ |
| 11059 sqlite3_mutex_enter(pShmNode->mutex); |
| 11060 for(pp=&pShmNode->pFirst; (*pp)!=p; pp = &(*pp)->pNext){} |
| 11061 *pp = p->pNext; |
| 11062 |
| 11063 /* Free the connection p */ |
| 11064 sqlite3_free(p); |
| 11065 pDbFd->pShm = 0; |
| 11066 sqlite3_mutex_leave(pShmNode->mutex); |
| 11067 |
| 11068 /* If pShmNode->nRef has reached 0, then close the underlying |
| 11069 ** shared-memory file, too */ |
| 11070 unixEnterMutex(); |
| 11071 assert( pShmNode->nRef>0 ); |
| 11072 pShmNode->nRef--; |
| 11073 if( pShmNode->nRef==0 ){ |
| 11074 if( deleteFlag && pShmNode->h>=0 ){ |
| 11075 osUnlink(pShmNode->zFilename); |
| 11076 } |
| 11077 unixShmPurge(pDbFd); |
| 11078 } |
| 11079 unixLeaveMutex(); |
| 11080 |
| 11081 return SQLITE_OK; |
| 11082 } |
| 11083 |
| 11084 |
| 11085 #else |
| 11086 # define unixShmMap 0 |
| 11087 # define unixShmLock 0 |
| 11088 # define unixShmBarrier 0 |
| 11089 # define unixShmUnmap 0 |
| 11090 #endif /* #ifndef SQLITE_OMIT_WAL */ |
| 11091 |
| 11092 #if SQLITE_MAX_MMAP_SIZE>0 |
| 11093 /* |
| 11094 ** If it is currently memory mapped, unmap file pFd. |
| 11095 */ |
| 11096 static void unixUnmapfile(unixFile *pFd){ |
| 11097 assert( pFd->nFetchOut==0 ); |
| 11098 if( pFd->pMapRegion ){ |
| 11099 osMunmap(pFd->pMapRegion, pFd->mmapSizeActual); |
| 11100 pFd->pMapRegion = 0; |
| 11101 pFd->mmapSize = 0; |
| 11102 pFd->mmapSizeActual = 0; |
| 11103 } |
| 11104 } |
| 11105 |
| 11106 /* |
| 11107 ** Attempt to set the size of the memory mapping maintained by file |
| 11108 ** descriptor pFd to nNew bytes. Any existing mapping is discarded. |
| 11109 ** |
| 11110 ** If successful, this function sets the following variables: |
| 11111 ** |
| 11112 ** unixFile.pMapRegion |
| 11113 ** unixFile.mmapSize |
| 11114 ** unixFile.mmapSizeActual |
| 11115 ** |
| 11116 ** If unsuccessful, an error message is logged via sqlite3_log() and |
| 11117 ** the three variables above are zeroed. In this case SQLite should |
| 11118 ** continue accessing the database using the xRead() and xWrite() |
| 11119 ** methods. |
| 11120 */ |
| 11121 static void unixRemapfile( |
| 11122 unixFile *pFd, /* File descriptor object */ |
| 11123 i64 nNew /* Required mapping size */ |
| 11124 ){ |
| 11125 const char *zErr = "mmap"; |
| 11126 int h = pFd->h; /* File descriptor open on db file */ |
| 11127 u8 *pOrig = (u8 *)pFd->pMapRegion; /* Pointer to current file mapping */ |
| 11128 i64 nOrig = pFd->mmapSizeActual; /* Size of pOrig region in bytes */ |
| 11129 u8 *pNew = 0; /* Location of new mapping */ |
| 11130 int flags = PROT_READ; /* Flags to pass to mmap() */ |
| 11131 |
| 11132 assert( pFd->nFetchOut==0 ); |
| 11133 assert( nNew>pFd->mmapSize ); |
| 11134 assert( nNew<=pFd->mmapSizeMax ); |
| 11135 assert( nNew>0 ); |
| 11136 assert( pFd->mmapSizeActual>=pFd->mmapSize ); |
| 11137 assert( MAP_FAILED!=0 ); |
| 11138 |
| 11139 #ifdef SQLITE_MMAP_READWRITE |
| 11140 if( (pFd->ctrlFlags & UNIXFILE_RDONLY)==0 ) flags |= PROT_WRITE; |
| 11141 #endif |
| 11142 |
| 11143 if( pOrig ){ |
| 11144 #if HAVE_MREMAP |
| 11145 i64 nReuse = pFd->mmapSize; |
| 11146 #else |
| 11147 const int szSyspage = osGetpagesize(); |
| 11148 i64 nReuse = (pFd->mmapSize & ~(szSyspage-1)); |
| 11149 #endif |
| 11150 u8 *pReq = &pOrig[nReuse]; |
| 11151 |
| 11152 /* Unmap any pages of the existing mapping that cannot be reused. */ |
| 11153 if( nReuse!=nOrig ){ |
| 11154 osMunmap(pReq, nOrig-nReuse); |
| 11155 } |
| 11156 |
| 11157 #if HAVE_MREMAP |
| 11158 pNew = osMremap(pOrig, nReuse, nNew, MREMAP_MAYMOVE); |
| 11159 zErr = "mremap"; |
| 11160 #else |
| 11161 pNew = osMmap(pReq, nNew-nReuse, flags, MAP_SHARED, h, nReuse); |
| 11162 if( pNew!=MAP_FAILED ){ |
| 11163 if( pNew!=pReq ){ |
| 11164 osMunmap(pNew, nNew - nReuse); |
| 11165 pNew = 0; |
| 11166 }else{ |
| 11167 pNew = pOrig; |
| 11168 } |
| 11169 } |
| 11170 #endif |
| 11171 |
| 11172 /* The attempt to extend the existing mapping failed. Free it. */ |
| 11173 if( pNew==MAP_FAILED || pNew==0 ){ |
| 11174 osMunmap(pOrig, nReuse); |
| 11175 } |
| 11176 } |
| 11177 |
| 11178 /* If pNew is still NULL, try to create an entirely new mapping. */ |
| 11179 if( pNew==0 ){ |
| 11180 pNew = osMmap(0, nNew, flags, MAP_SHARED, h, 0); |
| 11181 } |
| 11182 |
| 11183 if( pNew==MAP_FAILED ){ |
| 11184 pNew = 0; |
| 11185 nNew = 0; |
| 11186 unixLogError(SQLITE_OK, zErr, pFd->zPath); |
| 11187 |
| 11188 /* If the mmap() above failed, assume that all subsequent mmap() calls |
| 11189 ** will probably fail too. Fall back to using xRead/xWrite exclusively |
| 11190 ** in this case. */ |
| 11191 pFd->mmapSizeMax = 0; |
| 11192 } |
| 11193 pFd->pMapRegion = (void *)pNew; |
| 11194 pFd->mmapSize = pFd->mmapSizeActual = nNew; |
| 11195 } |
| 11196 |
| 11197 /* |
| 11198 ** Memory map or remap the file opened by file-descriptor pFd (if the file |
| 11199 ** is already mapped, the existing mapping is replaced by the new). Or, if |
| 11200 ** there already exists a mapping for this file, and there are still |
| 11201 ** outstanding xFetch() references to it, this function is a no-op. |
| 11202 ** |
| 11203 ** If parameter nByte is non-negative, then it is the requested size of |
| 11204 ** the mapping to create. Otherwise, if nByte is less than zero, then the |
| 11205 ** requested size is the size of the file on disk. The actual size of the |
| 11206 ** created mapping is either the requested size or the value configured |
| 11207 ** using SQLITE_FCNTL_MMAP_LIMIT, whichever is smaller. |
| 11208 ** |
| 11209 ** SQLITE_OK is returned if no error occurs (even if the mapping is not |
| 11210 ** recreated as a result of outstanding references) or an SQLite error |
| 11211 ** code otherwise. |
| 11212 */ |
| 11213 static int unixMapfile(unixFile *pFd, i64 nMap){ |
| 11214 assert( nMap>=0 || pFd->nFetchOut==0 ); |
| 11215 assert( nMap>0 || (pFd->mmapSize==0 && pFd->pMapRegion==0) ); |
| 11216 if( pFd->nFetchOut>0 ) return SQLITE_OK; |
| 11217 |
| 11218 if( nMap<0 ){ |
| 11219 struct stat statbuf; /* Low-level file information */ |
| 11220 if( osFstat(pFd->h, &statbuf) ){ |
| 11221 return SQLITE_IOERR_FSTAT; |
| 11222 } |
| 11223 nMap = statbuf.st_size; |
| 11224 } |
| 11225 if( nMap>pFd->mmapSizeMax ){ |
| 11226 nMap = pFd->mmapSizeMax; |
| 11227 } |
| 11228 |
| 11229 assert( nMap>0 || (pFd->mmapSize==0 && pFd->pMapRegion==0) ); |
| 11230 if( nMap!=pFd->mmapSize ){ |
| 11231 unixRemapfile(pFd, nMap); |
| 11232 } |
| 11233 |
| 11234 return SQLITE_OK; |
| 11235 } |
| 11236 #endif /* SQLITE_MAX_MMAP_SIZE>0 */ |
| 11237 |
| 11238 /* |
| 11239 ** If possible, return a pointer to a mapping of file fd starting at offset |
| 11240 ** iOff. The mapping must be valid for at least nAmt bytes. |
| 11241 ** |
| 11242 ** If such a pointer can be obtained, store it in *pp and return SQLITE_OK. |
| 11243 ** Or, if one cannot but no error occurs, set *pp to 0 and return SQLITE_OK. |
| 11244 ** Finally, if an error does occur, return an SQLite error code. The final |
| 11245 ** value of *pp is undefined in this case. |
| 11246 ** |
| 11247 ** If this function does return a pointer, the caller must eventually |
| 11248 ** release the reference by calling unixUnfetch(). |
| 11249 */ |
| 11250 static int unixFetch(sqlite3_file *fd, i64 iOff, int nAmt, void **pp){ |
| 11251 #if SQLITE_MAX_MMAP_SIZE>0 |
| 11252 unixFile *pFd = (unixFile *)fd; /* The underlying database file */ |
| 11253 #endif |
| 11254 *pp = 0; |
| 11255 |
| 11256 #if SQLITE_MAX_MMAP_SIZE>0 |
| 11257 if( pFd->mmapSizeMax>0 ){ |
| 11258 if( pFd->pMapRegion==0 ){ |
| 11259 int rc = unixMapfile(pFd, -1); |
| 11260 if( rc!=SQLITE_OK ) return rc; |
| 11261 } |
| 11262 if( pFd->mmapSize >= iOff+nAmt ){ |
| 11263 *pp = &((u8 *)pFd->pMapRegion)[iOff]; |
| 11264 pFd->nFetchOut++; |
| 11265 } |
| 11266 } |
| 11267 #endif |
| 11268 return SQLITE_OK; |
| 11269 } |
| 11270 |
| 11271 /* |
| 11272 ** If the third argument is non-NULL, then this function releases a |
| 11273 ** reference obtained by an earlier call to unixFetch(). The second |
| 11274 ** argument passed to this function must be the same as the corresponding |
| 11275 ** argument that was passed to the unixFetch() invocation. |
| 11276 ** |
| 11277 ** Or, if the third argument is NULL, then this function is being called |
| 11278 ** to inform the VFS layer that, according to POSIX, any existing mapping |
| 11279 ** may now be invalid and should be unmapped. |
| 11280 */ |
| 11281 static int unixUnfetch(sqlite3_file *fd, i64 iOff, void *p){ |
| 11282 #if SQLITE_MAX_MMAP_SIZE>0 |
| 11283 unixFile *pFd = (unixFile *)fd; /* The underlying database file */ |
| 11284 UNUSED_PARAMETER(iOff); |
| 11285 |
| 11286 /* If p==0 (unmap the entire file) then there must be no outstanding |
| 11287 ** xFetch references. Or, if p!=0 (meaning it is an xFetch reference), |
| 11288 ** then there must be at least one outstanding. */ |
| 11289 assert( (p==0)==(pFd->nFetchOut==0) ); |
| 11290 |
| 11291 /* If p!=0, it must match the iOff value. */ |
| 11292 assert( p==0 || p==&((u8 *)pFd->pMapRegion)[iOff] ); |
| 11293 |
| 11294 if( p ){ |
| 11295 pFd->nFetchOut--; |
| 11296 }else{ |
| 11297 unixUnmapfile(pFd); |
| 11298 } |
| 11299 |
| 11300 assert( pFd->nFetchOut>=0 ); |
| 11301 #else |
| 11302 UNUSED_PARAMETER(fd); |
| 11303 UNUSED_PARAMETER(p); |
| 11304 UNUSED_PARAMETER(iOff); |
| 11305 #endif |
| 11306 return SQLITE_OK; |
| 11307 } |
| 11308 |
| 11309 /* |
| 11310 ** Here ends the implementation of all sqlite3_file methods. |
| 11311 ** |
| 11312 ********************** End sqlite3_file Methods ******************************* |
| 11313 ******************************************************************************/ |
| 11314 |
| 11315 /* |
| 11316 ** This division contains definitions of sqlite3_io_methods objects that |
| 11317 ** implement various file locking strategies. It also contains definitions |
| 11318 ** of "finder" functions. A finder-function is used to locate the appropriate |
| 11319 ** sqlite3_io_methods object for a particular database file. The pAppData |
| 11320 ** field of the sqlite3_vfs VFS objects are initialized to be pointers to |
| 11321 ** the correct finder-function for that VFS. |
| 11322 ** |
| 11323 ** Most finder functions return a pointer to a fixed sqlite3_io_methods |
| 11324 ** object. The only interesting finder-function is autolockIoFinder, which |
| 11325 ** looks at the filesystem type and tries to guess the best locking |
| 11326 ** strategy from that. |
| 11327 ** |
| 11328 ** For finder-function F, two objects are created: |
| 11329 ** |
| 11330 ** (1) The real finder-function named "FImpt()". |
| 11331 ** |
| 11332 ** (2) A constant pointer to this function named just "F". |
| 11333 ** |
| 11334 ** |
| 11335 ** A pointer to the F pointer is used as the pAppData value for VFS |
| 11336 ** objects. We have to do this instead of letting pAppData point |
| 11337 ** directly at the finder-function since C90 rules prevent a void* |
| 11338 ** from be cast into a function pointer. |
| 11339 ** |
| 11340 ** |
| 11341 ** Each instance of this macro generates two objects: |
| 11342 ** |
| 11343 ** * A constant sqlite3_io_methods object call METHOD that has locking |
| 11344 ** methods CLOSE, LOCK, UNLOCK, CKRESLOCK. |
| 11345 ** |
| 11346 ** * An I/O method finder function called FINDER that returns a pointer |
| 11347 ** to the METHOD object in the previous bullet. |
| 11348 */ |
| 11349 #define IOMETHODS(FINDER,METHOD,VERSION,CLOSE,LOCK,UNLOCK,CKLOCK,SHMMAP) \ |
| 11350 static const sqlite3_io_methods METHOD = { \ |
| 11351 VERSION, /* iVersion */ \ |
| 11352 CLOSE, /* xClose */ \ |
| 11353 unixRead, /* xRead */ \ |
| 11354 unixWrite, /* xWrite */ \ |
| 11355 unixTruncate, /* xTruncate */ \ |
| 11356 unixSync, /* xSync */ \ |
| 11357 unixFileSize, /* xFileSize */ \ |
| 11358 LOCK, /* xLock */ \ |
| 11359 UNLOCK, /* xUnlock */ \ |
| 11360 CKLOCK, /* xCheckReservedLock */ \ |
| 11361 unixFileControl, /* xFileControl */ \ |
| 11362 unixSectorSize, /* xSectorSize */ \ |
| 11363 unixDeviceCharacteristics, /* xDeviceCapabilities */ \ |
| 11364 SHMMAP, /* xShmMap */ \ |
| 11365 unixShmLock, /* xShmLock */ \ |
| 11366 unixShmBarrier, /* xShmBarrier */ \ |
| 11367 unixShmUnmap, /* xShmUnmap */ \ |
| 11368 unixFetch, /* xFetch */ \ |
| 11369 unixUnfetch, /* xUnfetch */ \ |
| 11370 }; \ |
| 11371 static const sqlite3_io_methods *FINDER##Impl(const char *z, unixFile *p){ \ |
| 11372 UNUSED_PARAMETER(z); UNUSED_PARAMETER(p); \ |
| 11373 return &METHOD; \ |
| 11374 } \ |
| 11375 static const sqlite3_io_methods *(*const FINDER)(const char*,unixFile *p) \ |
| 11376 = FINDER##Impl; |
| 11377 |
| 11378 /* |
| 11379 ** Here are all of the sqlite3_io_methods objects for each of the |
| 11380 ** locking strategies. Functions that return pointers to these methods |
| 11381 ** are also created. |
| 11382 */ |
| 11383 IOMETHODS( |
| 11384 posixIoFinder, /* Finder function name */ |
| 11385 posixIoMethods, /* sqlite3_io_methods object name */ |
| 11386 3, /* shared memory and mmap are enabled */ |
| 11387 unixClose, /* xClose method */ |
| 11388 unixLock, /* xLock method */ |
| 11389 unixUnlock, /* xUnlock method */ |
| 11390 unixCheckReservedLock, /* xCheckReservedLock method */ |
| 11391 unixShmMap /* xShmMap method */ |
| 11392 ) |
| 11393 IOMETHODS( |
| 11394 nolockIoFinder, /* Finder function name */ |
| 11395 nolockIoMethods, /* sqlite3_io_methods object name */ |
| 11396 3, /* shared memory is disabled */ |
| 11397 nolockClose, /* xClose method */ |
| 11398 nolockLock, /* xLock method */ |
| 11399 nolockUnlock, /* xUnlock method */ |
| 11400 nolockCheckReservedLock, /* xCheckReservedLock method */ |
| 11401 0 /* xShmMap method */ |
| 11402 ) |
| 11403 IOMETHODS( |
| 11404 dotlockIoFinder, /* Finder function name */ |
| 11405 dotlockIoMethods, /* sqlite3_io_methods object name */ |
| 11406 1, /* shared memory is disabled */ |
| 11407 dotlockClose, /* xClose method */ |
| 11408 dotlockLock, /* xLock method */ |
| 11409 dotlockUnlock, /* xUnlock method */ |
| 11410 dotlockCheckReservedLock, /* xCheckReservedLock method */ |
| 11411 0 /* xShmMap method */ |
| 11412 ) |
| 11413 |
| 11414 #if SQLITE_ENABLE_LOCKING_STYLE |
| 11415 IOMETHODS( |
| 11416 flockIoFinder, /* Finder function name */ |
| 11417 flockIoMethods, /* sqlite3_io_methods object name */ |
| 11418 1, /* shared memory is disabled */ |
| 11419 flockClose, /* xClose method */ |
| 11420 flockLock, /* xLock method */ |
| 11421 flockUnlock, /* xUnlock method */ |
| 11422 flockCheckReservedLock, /* xCheckReservedLock method */ |
| 11423 0 /* xShmMap method */ |
| 11424 ) |
| 11425 #endif |
| 11426 |
| 11427 #if OS_VXWORKS |
| 11428 IOMETHODS( |
| 11429 semIoFinder, /* Finder function name */ |
| 11430 semIoMethods, /* sqlite3_io_methods object name */ |
| 11431 1, /* shared memory is disabled */ |
| 11432 semXClose, /* xClose method */ |
| 11433 semXLock, /* xLock method */ |
| 11434 semXUnlock, /* xUnlock method */ |
| 11435 semXCheckReservedLock, /* xCheckReservedLock method */ |
| 11436 0 /* xShmMap method */ |
| 11437 ) |
| 11438 #endif |
| 11439 |
| 11440 #if defined(__APPLE__) && SQLITE_ENABLE_LOCKING_STYLE |
| 11441 IOMETHODS( |
| 11442 afpIoFinder, /* Finder function name */ |
| 11443 afpIoMethods, /* sqlite3_io_methods object name */ |
| 11444 1, /* shared memory is disabled */ |
| 11445 afpClose, /* xClose method */ |
| 11446 afpLock, /* xLock method */ |
| 11447 afpUnlock, /* xUnlock method */ |
| 11448 afpCheckReservedLock, /* xCheckReservedLock method */ |
| 11449 0 /* xShmMap method */ |
| 11450 ) |
| 11451 #endif |
| 11452 |
| 11453 /* |
| 11454 ** The proxy locking method is a "super-method" in the sense that it |
| 11455 ** opens secondary file descriptors for the conch and lock files and |
| 11456 ** it uses proxy, dot-file, AFP, and flock() locking methods on those |
| 11457 ** secondary files. For this reason, the division that implements |
| 11458 ** proxy locking is located much further down in the file. But we need |
| 11459 ** to go ahead and define the sqlite3_io_methods and finder function |
| 11460 ** for proxy locking here. So we forward declare the I/O methods. |
| 11461 */ |
| 11462 #if defined(__APPLE__) && SQLITE_ENABLE_LOCKING_STYLE |
| 11463 static int proxyClose(sqlite3_file*); |
| 11464 static int proxyLock(sqlite3_file*, int); |
| 11465 static int proxyUnlock(sqlite3_file*, int); |
| 11466 static int proxyCheckReservedLock(sqlite3_file*, int*); |
| 11467 IOMETHODS( |
| 11468 proxyIoFinder, /* Finder function name */ |
| 11469 proxyIoMethods, /* sqlite3_io_methods object name */ |
| 11470 1, /* shared memory is disabled */ |
| 11471 proxyClose, /* xClose method */ |
| 11472 proxyLock, /* xLock method */ |
| 11473 proxyUnlock, /* xUnlock method */ |
| 11474 proxyCheckReservedLock, /* xCheckReservedLock method */ |
| 11475 0 /* xShmMap method */ |
| 11476 ) |
| 11477 #endif |
| 11478 |
| 11479 /* nfs lockd on OSX 10.3+ doesn't clear write locks when a read lock is set */ |
| 11480 #if defined(__APPLE__) && SQLITE_ENABLE_LOCKING_STYLE |
| 11481 IOMETHODS( |
| 11482 nfsIoFinder, /* Finder function name */ |
| 11483 nfsIoMethods, /* sqlite3_io_methods object name */ |
| 11484 1, /* shared memory is disabled */ |
| 11485 unixClose, /* xClose method */ |
| 11486 unixLock, /* xLock method */ |
| 11487 nfsUnlock, /* xUnlock method */ |
| 11488 unixCheckReservedLock, /* xCheckReservedLock method */ |
| 11489 0 /* xShmMap method */ |
| 11490 ) |
| 11491 #endif |
| 11492 |
| 11493 #if defined(__APPLE__) && SQLITE_ENABLE_LOCKING_STYLE |
| 11494 /* |
| 11495 ** This "finder" function attempts to determine the best locking strategy |
| 11496 ** for the database file "filePath". It then returns the sqlite3_io_methods |
| 11497 ** object that implements that strategy. |
| 11498 ** |
| 11499 ** This is for MacOSX only. |
| 11500 */ |
| 11501 static const sqlite3_io_methods *autolockIoFinderImpl( |
| 11502 const char *filePath, /* name of the database file */ |
| 11503 unixFile *pNew /* open file object for the database file */ |
| 11504 ){ |
| 11505 static const struct Mapping { |
| 11506 const char *zFilesystem; /* Filesystem type name */ |
| 11507 const sqlite3_io_methods *pMethods; /* Appropriate locking method */ |
| 11508 } aMap[] = { |
| 11509 { "hfs", &posixIoMethods }, |
| 11510 { "ufs", &posixIoMethods }, |
| 11511 { "afpfs", &afpIoMethods }, |
| 11512 { "smbfs", &afpIoMethods }, |
| 11513 { "webdav", &nolockIoMethods }, |
| 11514 { 0, 0 } |
| 11515 }; |
| 11516 int i; |
| 11517 struct statfs fsInfo; |
| 11518 struct flock lockInfo; |
| 11519 |
| 11520 if( !filePath ){ |
| 11521 /* If filePath==NULL that means we are dealing with a transient file |
| 11522 ** that does not need to be locked. */ |
| 11523 return &nolockIoMethods; |
| 11524 } |
| 11525 if( statfs(filePath, &fsInfo) != -1 ){ |
| 11526 if( fsInfo.f_flags & MNT_RDONLY ){ |
| 11527 return &nolockIoMethods; |
| 11528 } |
| 11529 for(i=0; aMap[i].zFilesystem; i++){ |
| 11530 if( strcmp(fsInfo.f_fstypename, aMap[i].zFilesystem)==0 ){ |
| 11531 return aMap[i].pMethods; |
| 11532 } |
| 11533 } |
| 11534 } |
| 11535 |
| 11536 /* Default case. Handles, amongst others, "nfs". |
| 11537 ** Test byte-range lock using fcntl(). If the call succeeds, |
| 11538 ** assume that the file-system supports POSIX style locks. |
| 11539 */ |
| 11540 lockInfo.l_len = 1; |
| 11541 lockInfo.l_start = 0; |
| 11542 lockInfo.l_whence = SEEK_SET; |
| 11543 lockInfo.l_type = F_RDLCK; |
| 11544 if( osFcntl(pNew->h, F_GETLK, &lockInfo)!=-1 ) { |
| 11545 if( strcmp(fsInfo.f_fstypename, "nfs")==0 ){ |
| 11546 return &nfsIoMethods; |
| 11547 } else { |
| 11548 return &posixIoMethods; |
| 11549 } |
| 11550 }else{ |
| 11551 return &dotlockIoMethods; |
| 11552 } |
| 11553 } |
| 11554 static const sqlite3_io_methods |
| 11555 *(*const autolockIoFinder)(const char*,unixFile*) = autolockIoFinderImpl; |
| 11556 |
| 11557 #endif /* defined(__APPLE__) && SQLITE_ENABLE_LOCKING_STYLE */ |
| 11558 |
| 11559 #if OS_VXWORKS |
| 11560 /* |
| 11561 ** This "finder" function for VxWorks checks to see if posix advisory |
| 11562 ** locking works. If it does, then that is what is used. If it does not |
| 11563 ** work, then fallback to named semaphore locking. |
| 11564 */ |
| 11565 static const sqlite3_io_methods *vxworksIoFinderImpl( |
| 11566 const char *filePath, /* name of the database file */ |
| 11567 unixFile *pNew /* the open file object */ |
| 11568 ){ |
| 11569 struct flock lockInfo; |
| 11570 |
| 11571 if( !filePath ){ |
| 11572 /* If filePath==NULL that means we are dealing with a transient file |
| 11573 ** that does not need to be locked. */ |
| 11574 return &nolockIoMethods; |
| 11575 } |
| 11576 |
| 11577 /* Test if fcntl() is supported and use POSIX style locks. |
| 11578 ** Otherwise fall back to the named semaphore method. |
| 11579 */ |
| 11580 lockInfo.l_len = 1; |
| 11581 lockInfo.l_start = 0; |
| 11582 lockInfo.l_whence = SEEK_SET; |
| 11583 lockInfo.l_type = F_RDLCK; |
| 11584 if( osFcntl(pNew->h, F_GETLK, &lockInfo)!=-1 ) { |
| 11585 return &posixIoMethods; |
| 11586 }else{ |
| 11587 return &semIoMethods; |
| 11588 } |
| 11589 } |
| 11590 static const sqlite3_io_methods |
| 11591 *(*const vxworksIoFinder)(const char*,unixFile*) = vxworksIoFinderImpl; |
| 11592 |
| 11593 #endif /* OS_VXWORKS */ |
| 11594 |
| 11595 /* |
| 11596 ** An abstract type for a pointer to an IO method finder function: |
| 11597 */ |
| 11598 typedef const sqlite3_io_methods *(*finder_type)(const char*,unixFile*); |
| 11599 |
| 11600 |
| 11601 /**************************************************************************** |
| 11602 **************************** sqlite3_vfs methods **************************** |
| 11603 ** |
| 11604 ** This division contains the implementation of methods on the |
| 11605 ** sqlite3_vfs object. |
| 11606 */ |
| 11607 |
| 11608 /* |
| 11609 ** Initialize the contents of the unixFile structure pointed to by pId. |
| 11610 */ |
| 11611 static int fillInUnixFile( |
| 11612 sqlite3_vfs *pVfs, /* Pointer to vfs object */ |
| 11613 int h, /* Open file descriptor of file being opened */ |
| 11614 sqlite3_file *pId, /* Write to the unixFile structure here */ |
| 11615 const char *zFilename, /* Name of the file being opened */ |
| 11616 int ctrlFlags /* Zero or more UNIXFILE_* values */ |
| 11617 ){ |
| 11618 const sqlite3_io_methods *pLockingStyle; |
| 11619 unixFile *pNew = (unixFile *)pId; |
| 11620 int rc = SQLITE_OK; |
| 11621 |
| 11622 assert( pNew->pInode==NULL ); |
| 11623 |
| 11624 /* Usually the path zFilename should not be a relative pathname. The |
| 11625 ** exception is when opening the proxy "conch" file in builds that |
| 11626 ** include the special Apple locking styles. |
| 11627 */ |
| 11628 #if defined(__APPLE__) && SQLITE_ENABLE_LOCKING_STYLE |
| 11629 assert( zFilename==0 || zFilename[0]=='/' |
| 11630 || pVfs->pAppData==(void*)&autolockIoFinder ); |
| 11631 #else |
| 11632 assert( zFilename==0 || zFilename[0]=='/' ); |
| 11633 #endif |
| 11634 |
| 11635 /* No locking occurs in temporary files */ |
| 11636 assert( zFilename!=0 || (ctrlFlags & UNIXFILE_NOLOCK)!=0 ); |
| 11637 |
| 11638 OSTRACE(("OPEN %-3d %s\n", h, zFilename)); |
| 11639 pNew->h = h; |
| 11640 pNew->pVfs = pVfs; |
| 11641 pNew->zPath = zFilename; |
| 11642 pNew->ctrlFlags = (u8)ctrlFlags; |
| 11643 #if SQLITE_MAX_MMAP_SIZE>0 |
| 11644 pNew->mmapSizeMax = sqlite3GlobalConfig.szMmap; |
| 11645 #endif |
| 11646 if( sqlite3_uri_boolean(((ctrlFlags & UNIXFILE_URI) ? zFilename : 0), |
| 11647 "psow", SQLITE_POWERSAFE_OVERWRITE) ){ |
| 11648 pNew->ctrlFlags |= UNIXFILE_PSOW; |
| 11649 } |
| 11650 if( strcmp(pVfs->zName,"unix-excl")==0 ){ |
| 11651 pNew->ctrlFlags |= UNIXFILE_EXCL; |
| 11652 } |
| 11653 |
| 11654 #if OS_VXWORKS |
| 11655 pNew->pId = vxworksFindFileId(zFilename); |
| 11656 if( pNew->pId==0 ){ |
| 11657 ctrlFlags |= UNIXFILE_NOLOCK; |
| 11658 rc = SQLITE_NOMEM; |
| 11659 } |
| 11660 #endif |
| 11661 |
| 11662 if( ctrlFlags & UNIXFILE_NOLOCK ){ |
| 11663 pLockingStyle = &nolockIoMethods; |
| 11664 }else{ |
| 11665 pLockingStyle = (**(finder_type*)pVfs->pAppData)(zFilename, pNew); |
| 11666 #if SQLITE_ENABLE_LOCKING_STYLE |
| 11667 /* Cache zFilename in the locking context (AFP and dotlock override) for |
| 11668 ** proxyLock activation is possible (remote proxy is based on db name) |
| 11669 ** zFilename remains valid until file is closed, to support */ |
| 11670 pNew->lockingContext = (void*)zFilename; |
| 11671 #endif |
| 11672 } |
| 11673 |
| 11674 if( pLockingStyle == &posixIoMethods |
| 11675 #if defined(__APPLE__) && SQLITE_ENABLE_LOCKING_STYLE |
| 11676 || pLockingStyle == &nfsIoMethods |
| 11677 #endif |
| 11678 ){ |
| 11679 unixEnterMutex(); |
| 11680 rc = findInodeInfo(pNew, &pNew->pInode); |
| 11681 if( rc!=SQLITE_OK ){ |
| 11682 /* If an error occurred in findInodeInfo(), close the file descriptor |
| 11683 ** immediately, before releasing the mutex. findInodeInfo() may fail |
| 11684 ** in two scenarios: |
| 11685 ** |
| 11686 ** (a) A call to fstat() failed. |
| 11687 ** (b) A malloc failed. |
| 11688 ** |
| 11689 ** Scenario (b) may only occur if the process is holding no other |
| 11690 ** file descriptors open on the same file. If there were other file |
| 11691 ** descriptors on this file, then no malloc would be required by |
| 11692 ** findInodeInfo(). If this is the case, it is quite safe to close |
| 11693 ** handle h - as it is guaranteed that no posix locks will be released |
| 11694 ** by doing so. |
| 11695 ** |
| 11696 ** If scenario (a) caused the error then things are not so safe. The |
| 11697 ** implicit assumption here is that if fstat() fails, things are in |
| 11698 ** such bad shape that dropping a lock or two doesn't matter much. |
| 11699 */ |
| 11700 robust_close(pNew, h, __LINE__); |
| 11701 h = -1; |
| 11702 } |
| 11703 unixLeaveMutex(); |
| 11704 } |
| 11705 |
| 11706 #if SQLITE_ENABLE_LOCKING_STYLE && defined(__APPLE__) |
| 11707 else if( pLockingStyle == &afpIoMethods ){ |
| 11708 /* AFP locking uses the file path so it needs to be included in |
| 11709 ** the afpLockingContext. |
| 11710 */ |
| 11711 afpLockingContext *pCtx; |
| 11712 pNew->lockingContext = pCtx = sqlite3_malloc64( sizeof(*pCtx) ); |
| 11713 if( pCtx==0 ){ |
| 11714 rc = SQLITE_NOMEM; |
| 11715 }else{ |
| 11716 /* NB: zFilename exists and remains valid until the file is closed |
| 11717 ** according to requirement F11141. So we do not need to make a |
| 11718 ** copy of the filename. */ |
| 11719 pCtx->dbPath = zFilename; |
| 11720 pCtx->reserved = 0; |
| 11721 srandomdev(); |
| 11722 unixEnterMutex(); |
| 11723 rc = findInodeInfo(pNew, &pNew->pInode); |
| 11724 if( rc!=SQLITE_OK ){ |
| 11725 sqlite3_free(pNew->lockingContext); |
| 11726 robust_close(pNew, h, __LINE__); |
| 11727 h = -1; |
| 11728 } |
| 11729 unixLeaveMutex(); |
| 11730 } |
| 11731 } |
| 11732 #endif |
| 11733 |
| 11734 else if( pLockingStyle == &dotlockIoMethods ){ |
| 11735 /* Dotfile locking uses the file path so it needs to be included in |
| 11736 ** the dotlockLockingContext |
| 11737 */ |
| 11738 char *zLockFile; |
| 11739 int nFilename; |
| 11740 assert( zFilename!=0 ); |
| 11741 nFilename = (int)strlen(zFilename) + 6; |
| 11742 zLockFile = (char *)sqlite3_malloc64(nFilename); |
| 11743 if( zLockFile==0 ){ |
| 11744 rc = SQLITE_NOMEM; |
| 11745 }else{ |
| 11746 sqlite3_snprintf(nFilename, zLockFile, "%s" DOTLOCK_SUFFIX, zFilename); |
| 11747 } |
| 11748 pNew->lockingContext = zLockFile; |
| 11749 } |
| 11750 |
| 11751 #if OS_VXWORKS |
| 11752 else if( pLockingStyle == &semIoMethods ){ |
| 11753 /* Named semaphore locking uses the file path so it needs to be |
| 11754 ** included in the semLockingContext |
| 11755 */ |
| 11756 unixEnterMutex(); |
| 11757 rc = findInodeInfo(pNew, &pNew->pInode); |
| 11758 if( (rc==SQLITE_OK) && (pNew->pInode->pSem==NULL) ){ |
| 11759 char *zSemName = pNew->pInode->aSemName; |
| 11760 int n; |
| 11761 sqlite3_snprintf(MAX_PATHNAME, zSemName, "/%s.sem", |
| 11762 pNew->pId->zCanonicalName); |
| 11763 for( n=1; zSemName[n]; n++ ) |
| 11764 if( zSemName[n]=='/' ) zSemName[n] = '_'; |
| 11765 pNew->pInode->pSem = sem_open(zSemName, O_CREAT, 0666, 1); |
| 11766 if( pNew->pInode->pSem == SEM_FAILED ){ |
| 11767 rc = SQLITE_NOMEM; |
| 11768 pNew->pInode->aSemName[0] = '\0'; |
| 11769 } |
| 11770 } |
| 11771 unixLeaveMutex(); |
| 11772 } |
| 11773 #endif |
| 11774 |
| 11775 storeLastErrno(pNew, 0); |
| 11776 #if OS_VXWORKS |
| 11777 if( rc!=SQLITE_OK ){ |
| 11778 if( h>=0 ) robust_close(pNew, h, __LINE__); |
| 11779 h = -1; |
| 11780 osUnlink(zFilename); |
| 11781 pNew->ctrlFlags |= UNIXFILE_DELETE; |
| 11782 } |
| 11783 #endif |
| 11784 if( rc!=SQLITE_OK ){ |
| 11785 if( h>=0 ) robust_close(pNew, h, __LINE__); |
| 11786 }else{ |
| 11787 pNew->pMethod = pLockingStyle; |
| 11788 OpenCounter(+1); |
| 11789 verifyDbFile(pNew); |
| 11790 } |
| 11791 return rc; |
| 11792 } |
| 11793 |
| 11794 /* |
| 11795 ** Return the name of a directory in which to put temporary files. |
| 11796 ** If no suitable temporary file directory can be found, return NULL. |
| 11797 */ |
| 11798 static const char *unixTempFileDir(void){ |
| 11799 static const char *azDirs[] = { |
| 11800 0, |
| 11801 0, |
| 11802 "/var/tmp", |
| 11803 "/usr/tmp", |
| 11804 "/tmp", |
| 11805 "." |
| 11806 }; |
| 11807 unsigned int i; |
| 11808 struct stat buf; |
| 11809 const char *zDir = sqlite3_temp_directory; |
| 11810 |
| 11811 if( !azDirs[0] ) azDirs[0] = getenv("SQLITE_TMPDIR"); |
| 11812 if( !azDirs[1] ) azDirs[1] = getenv("TMPDIR"); |
| 11813 for(i=0; i<sizeof(azDirs)/sizeof(azDirs[0]); zDir=azDirs[i++]){ |
| 11814 if( zDir==0 ) continue; |
| 11815 if( osStat(zDir, &buf) ) continue; |
| 11816 if( !S_ISDIR(buf.st_mode) ) continue; |
| 11817 if( osAccess(zDir, 07) ) continue; |
| 11818 break; |
| 11819 } |
| 11820 return zDir; |
| 11821 } |
| 11822 |
| 11823 /* |
| 11824 ** Create a temporary file name in zBuf. zBuf must be allocated |
| 11825 ** by the calling process and must be big enough to hold at least |
| 11826 ** pVfs->mxPathname bytes. |
| 11827 */ |
| 11828 static int unixGetTempname(int nBuf, char *zBuf){ |
| 11829 const char *zDir; |
| 11830 int iLimit = 0; |
| 11831 |
| 11832 /* It's odd to simulate an io-error here, but really this is just |
| 11833 ** using the io-error infrastructure to test that SQLite handles this |
| 11834 ** function failing. |
| 11835 */ |
| 11836 SimulateIOError( return SQLITE_IOERR ); |
| 11837 |
| 11838 zDir = unixTempFileDir(); |
| 11839 do{ |
| 11840 u64 r; |
| 11841 sqlite3_randomness(sizeof(r), &r); |
| 11842 assert( nBuf>2 ); |
| 11843 zBuf[nBuf-2] = 0; |
| 11844 sqlite3_snprintf(nBuf, zBuf, "%s/"SQLITE_TEMP_FILE_PREFIX"%llx%c", |
| 11845 zDir, r, 0); |
| 11846 if( zBuf[nBuf-2]!=0 || (iLimit++)>10 ) return SQLITE_ERROR; |
| 11847 }while( osAccess(zBuf,0)==0 ); |
| 11848 return SQLITE_OK; |
| 11849 } |
| 11850 |
| 11851 #if SQLITE_ENABLE_LOCKING_STYLE && defined(__APPLE__) |
| 11852 /* |
| 11853 ** Routine to transform a unixFile into a proxy-locking unixFile. |
| 11854 ** Implementation in the proxy-lock division, but used by unixOpen() |
| 11855 ** if SQLITE_PREFER_PROXY_LOCKING is defined. |
| 11856 */ |
| 11857 static int proxyTransformUnixFile(unixFile*, const char*); |
| 11858 #endif |
| 11859 |
| 11860 /* |
| 11861 ** Search for an unused file descriptor that was opened on the database |
| 11862 ** file (not a journal or master-journal file) identified by pathname |
| 11863 ** zPath with SQLITE_OPEN_XXX flags matching those passed as the second |
| 11864 ** argument to this function. |
| 11865 ** |
| 11866 ** Such a file descriptor may exist if a database connection was closed |
| 11867 ** but the associated file descriptor could not be closed because some |
| 11868 ** other file descriptor open on the same file is holding a file-lock. |
| 11869 ** Refer to comments in the unixClose() function and the lengthy comment |
| 11870 ** describing "Posix Advisory Locking" at the start of this file for |
| 11871 ** further details. Also, ticket #4018. |
| 11872 ** |
| 11873 ** If a suitable file descriptor is found, then it is returned. If no |
| 11874 ** such file descriptor is located, -1 is returned. |
| 11875 */ |
| 11876 static UnixUnusedFd *findReusableFd(const char *zPath, int flags){ |
| 11877 UnixUnusedFd *pUnused = 0; |
| 11878 |
| 11879 /* Do not search for an unused file descriptor on vxworks. Not because |
| 11880 ** vxworks would not benefit from the change (it might, we're not sure), |
| 11881 ** but because no way to test it is currently available. It is better |
| 11882 ** not to risk breaking vxworks support for the sake of such an obscure |
| 11883 ** feature. */ |
| 11884 #if !OS_VXWORKS |
| 11885 struct stat sStat; /* Results of stat() call */ |
| 11886 |
| 11887 /* A stat() call may fail for various reasons. If this happens, it is |
| 11888 ** almost certain that an open() call on the same path will also fail. |
| 11889 ** For this reason, if an error occurs in the stat() call here, it is |
| 11890 ** ignored and -1 is returned. The caller will try to open a new file |
| 11891 ** descriptor on the same path, fail, and return an error to SQLite. |
| 11892 ** |
| 11893 ** Even if a subsequent open() call does succeed, the consequences of |
| 11894 ** not searching for a reusable file descriptor are not dire. */ |
| 11895 if( 0==osStat(zPath, &sStat) ){ |
| 11896 unixInodeInfo *pInode; |
| 11897 |
| 11898 unixEnterMutex(); |
| 11899 pInode = inodeList; |
| 11900 while( pInode && (pInode->fileId.dev!=sStat.st_dev |
| 11901 || pInode->fileId.ino!=sStat.st_ino) ){ |
| 11902 pInode = pInode->pNext; |
| 11903 } |
| 11904 if( pInode ){ |
| 11905 UnixUnusedFd **pp; |
| 11906 for(pp=&pInode->pUnused; *pp && (*pp)->flags!=flags; pp=&((*pp)->pNext)); |
| 11907 pUnused = *pp; |
| 11908 if( pUnused ){ |
| 11909 *pp = pUnused->pNext; |
| 11910 } |
| 11911 } |
| 11912 unixLeaveMutex(); |
| 11913 } |
| 11914 #endif /* if !OS_VXWORKS */ |
| 11915 return pUnused; |
| 11916 } |
| 11917 |
| 11918 /* |
| 11919 ** This function is called by unixOpen() to determine the unix permissions |
| 11920 ** to create new files with. If no error occurs, then SQLITE_OK is returned |
| 11921 ** and a value suitable for passing as the third argument to open(2) is |
| 11922 ** written to *pMode. If an IO error occurs, an SQLite error code is |
| 11923 ** returned and the value of *pMode is not modified. |
| 11924 ** |
| 11925 ** In most cases, this routine sets *pMode to 0, which will become |
| 11926 ** an indication to robust_open() to create the file using |
| 11927 ** SQLITE_DEFAULT_FILE_PERMISSIONS adjusted by the umask. |
| 11928 ** But if the file being opened is a WAL or regular journal file, then |
| 11929 ** this function queries the file-system for the permissions on the |
| 11930 ** corresponding database file and sets *pMode to this value. Whenever |
| 11931 ** possible, WAL and journal files are created using the same permissions |
| 11932 ** as the associated database file. |
| 11933 ** |
| 11934 ** If the SQLITE_ENABLE_8_3_NAMES option is enabled, then the |
| 11935 ** original filename is unavailable. But 8_3_NAMES is only used for |
| 11936 ** FAT filesystems and permissions do not matter there, so just use |
| 11937 ** the default permissions. |
| 11938 */ |
| 11939 static int findCreateFileMode( |
| 11940 const char *zPath, /* Path of file (possibly) being created */ |
| 11941 int flags, /* Flags passed as 4th argument to xOpen() */ |
| 11942 mode_t *pMode, /* OUT: Permissions to open file with */ |
| 11943 uid_t *pUid, /* OUT: uid to set on the file */ |
| 11944 gid_t *pGid /* OUT: gid to set on the file */ |
| 11945 ){ |
| 11946 int rc = SQLITE_OK; /* Return Code */ |
| 11947 *pMode = 0; |
| 11948 *pUid = 0; |
| 11949 *pGid = 0; |
| 11950 if( flags & (SQLITE_OPEN_WAL|SQLITE_OPEN_MAIN_JOURNAL) ){ |
| 11951 char zDb[MAX_PATHNAME+1]; /* Database file path */ |
| 11952 int nDb; /* Number of valid bytes in zDb */ |
| 11953 struct stat sStat; /* Output of stat() on database file */ |
| 11954 |
| 11955 /* zPath is a path to a WAL or journal file. The following block derives |
| 11956 ** the path to the associated database file from zPath. This block handles |
| 11957 ** the following naming conventions: |
| 11958 ** |
| 11959 ** "<path to db>-journal" |
| 11960 ** "<path to db>-wal" |
| 11961 ** "<path to db>-journalNN" |
| 11962 ** "<path to db>-walNN" |
| 11963 ** |
| 11964 ** where NN is a decimal number. The NN naming schemes are |
| 11965 ** used by the test_multiplex.c module. |
| 11966 */ |
| 11967 nDb = sqlite3Strlen30(zPath) - 1; |
| 11968 while( zPath[nDb]!='-' ){ |
| 11969 #ifndef SQLITE_ENABLE_8_3_NAMES |
| 11970 /* In the normal case (8+3 filenames disabled) the journal filename |
| 11971 ** is guaranteed to contain a '-' character. */ |
| 11972 assert( nDb>0 ); |
| 11973 assert( sqlite3Isalnum(zPath[nDb]) ); |
| 11974 #else |
| 11975 /* If 8+3 names are possible, then the journal file might not contain |
| 11976 ** a '-' character. So check for that case and return early. */ |
| 11977 if( nDb==0 || zPath[nDb]=='.' ) return SQLITE_OK; |
| 11978 #endif |
| 11979 nDb--; |
| 11980 } |
| 11981 memcpy(zDb, zPath, nDb); |
| 11982 zDb[nDb] = '\0'; |
| 11983 |
| 11984 if( 0==osStat(zDb, &sStat) ){ |
| 11985 *pMode = sStat.st_mode & 0777; |
| 11986 *pUid = sStat.st_uid; |
| 11987 *pGid = sStat.st_gid; |
| 11988 }else{ |
| 11989 rc = SQLITE_IOERR_FSTAT; |
| 11990 } |
| 11991 }else if( flags & SQLITE_OPEN_DELETEONCLOSE ){ |
| 11992 *pMode = 0600; |
| 11993 } |
| 11994 return rc; |
| 11995 } |
| 11996 |
| 11997 /* |
| 11998 ** Initialize |unixFile| internals of |file| on behalf of chromiumOpen() in |
| 11999 ** WebDatabase SQLiteFileSystemPosix.cpp. Function is a subset of unixOpen(), |
| 12000 ** each duplicated piece is marked by "Duplicated in" comment in unixOpen(). |
| 12001 */ |
| 12002 CHROMIUM_SQLITE_API |
| 12003 int chromium_sqlite3_fill_in_unix_sqlite3_file(sqlite3_vfs* pVfs, |
| 12004 int fd, |
| 12005 sqlite3_file* pFile, |
| 12006 const char* zPath, |
| 12007 int noLock, |
| 12008 int flags) { |
| 12009 unixFile *p = (unixFile *)pFile; |
| 12010 const int eType = flags&0xFFFFFF00; /* Type of file to open */ |
| 12011 const int ctrlFlags = (noLock ? UNIXFILE_NOLOCK : 0); |
| 12012 int rc; |
| 12013 |
| 12014 memset(p, 0, sizeof(unixFile)); |
| 12015 |
| 12016 /* osStat() will not work in the sandbox, so findReusableFd() will always |
| 12017 ** fail, so directly include the failure-case setup then initialize pUnused. |
| 12018 */ |
| 12019 if( eType==SQLITE_OPEN_MAIN_DB ){ |
| 12020 p->pUnused = sqlite3_malloc(sizeof(*p->pUnused)); |
| 12021 if (!p->pUnused) { |
| 12022 return SQLITE_NOMEM; |
| 12023 } |
| 12024 p->pUnused->fd = fd; |
| 12025 p->pUnused->flags = flags; |
| 12026 } |
| 12027 |
| 12028 rc = fillInUnixFile(pVfs, fd, pFile, zPath, ctrlFlags); |
| 12029 if( rc!=SQLITE_OK ){ |
| 12030 sqlite3_free(p->pUnused); |
| 12031 } |
| 12032 return rc; |
| 12033 } |
| 12034 |
| 12035 /* |
| 12036 ** Open the file zPath. |
| 12037 ** |
| 12038 ** Previously, the SQLite OS layer used three functions in place of this |
| 12039 ** one: |
| 12040 ** |
| 12041 ** sqlite3OsOpenReadWrite(); |
| 12042 ** sqlite3OsOpenReadOnly(); |
| 12043 ** sqlite3OsOpenExclusive(); |
| 12044 ** |
| 12045 ** These calls correspond to the following combinations of flags: |
| 12046 ** |
| 12047 ** ReadWrite() -> (READWRITE | CREATE) |
| 12048 ** ReadOnly() -> (READONLY) |
| 12049 ** OpenExclusive() -> (READWRITE | CREATE | EXCLUSIVE) |
| 12050 ** |
| 12051 ** The old OpenExclusive() accepted a boolean argument - "delFlag". If |
| 12052 ** true, the file was configured to be automatically deleted when the |
| 12053 ** file handle closed. To achieve the same effect using this new |
| 12054 ** interface, add the DELETEONCLOSE flag to those specified above for |
| 12055 ** OpenExclusive(). |
| 12056 */ |
| 12057 static int unixOpen( |
| 12058 sqlite3_vfs *pVfs, /* The VFS for which this is the xOpen method */ |
| 12059 const char *zPath, /* Pathname of file to be opened */ |
| 12060 sqlite3_file *pFile, /* The file descriptor to be filled in */ |
| 12061 int flags, /* Input flags to control the opening */ |
| 12062 int *pOutFlags /* Output flags returned to SQLite core */ |
| 12063 ){ |
| 12064 unixFile *p = (unixFile *)pFile; |
| 12065 int fd = -1; /* File descriptor returned by open() */ |
| 12066 int openFlags = 0; /* Flags to pass to open() */ |
| 12067 int eType = flags&0xFFFFFF00; /* Type of file to open */ |
| 12068 int noLock; /* True to omit locking primitives */ |
| 12069 int rc = SQLITE_OK; /* Function Return Code */ |
| 12070 int ctrlFlags = 0; /* UNIXFILE_* flags */ |
| 12071 |
| 12072 int isExclusive = (flags & SQLITE_OPEN_EXCLUSIVE); |
| 12073 int isDelete = (flags & SQLITE_OPEN_DELETEONCLOSE); |
| 12074 int isCreate = (flags & SQLITE_OPEN_CREATE); |
| 12075 int isReadonly = (flags & SQLITE_OPEN_READONLY); |
| 12076 int isReadWrite = (flags & SQLITE_OPEN_READWRITE); |
| 12077 #if SQLITE_ENABLE_LOCKING_STYLE |
| 12078 int isAutoProxy = (flags & SQLITE_OPEN_AUTOPROXY); |
| 12079 #endif |
| 12080 #if defined(__APPLE__) || SQLITE_ENABLE_LOCKING_STYLE |
| 12081 struct statfs fsInfo; |
| 12082 #endif |
| 12083 |
| 12084 /* If creating a master or main-file journal, this function will open |
| 12085 ** a file-descriptor on the directory too. The first time unixSync() |
| 12086 ** is called the directory file descriptor will be fsync()ed and close()d. |
| 12087 */ |
| 12088 int syncDir = (isCreate && ( |
| 12089 eType==SQLITE_OPEN_MASTER_JOURNAL |
| 12090 || eType==SQLITE_OPEN_MAIN_JOURNAL |
| 12091 || eType==SQLITE_OPEN_WAL |
| 12092 )); |
| 12093 |
| 12094 /* If argument zPath is a NULL pointer, this function is required to open |
| 12095 ** a temporary file. Use this buffer to store the file name in. |
| 12096 */ |
| 12097 char zTmpname[MAX_PATHNAME+2]; |
| 12098 const char *zName = zPath; |
| 12099 |
| 12100 /* Check the following statements are true: |
| 12101 ** |
| 12102 ** (a) Exactly one of the READWRITE and READONLY flags must be set, and |
| 12103 ** (b) if CREATE is set, then READWRITE must also be set, and |
| 12104 ** (c) if EXCLUSIVE is set, then CREATE must also be set. |
| 12105 ** (d) if DELETEONCLOSE is set, then CREATE must also be set. |
| 12106 */ |
| 12107 assert((isReadonly==0 || isReadWrite==0) && (isReadWrite || isReadonly)); |
| 12108 assert(isCreate==0 || isReadWrite); |
| 12109 assert(isExclusive==0 || isCreate); |
| 12110 assert(isDelete==0 || isCreate); |
| 12111 |
| 12112 /* The main DB, main journal, WAL file and master journal are never |
| 12113 ** automatically deleted. Nor are they ever temporary files. */ |
| 12114 assert( (!isDelete && zName) || eType!=SQLITE_OPEN_MAIN_DB ); |
| 12115 assert( (!isDelete && zName) || eType!=SQLITE_OPEN_MAIN_JOURNAL ); |
| 12116 assert( (!isDelete && zName) || eType!=SQLITE_OPEN_MASTER_JOURNAL ); |
| 12117 assert( (!isDelete && zName) || eType!=SQLITE_OPEN_WAL ); |
| 12118 |
| 12119 /* Assert that the upper layer has set one of the "file-type" flags. */ |
| 12120 assert( eType==SQLITE_OPEN_MAIN_DB || eType==SQLITE_OPEN_TEMP_DB |
| 12121 || eType==SQLITE_OPEN_MAIN_JOURNAL || eType==SQLITE_OPEN_TEMP_JOURNAL |
| 12122 || eType==SQLITE_OPEN_SUBJOURNAL || eType==SQLITE_OPEN_MASTER_JOURNAL |
| 12123 || eType==SQLITE_OPEN_TRANSIENT_DB || eType==SQLITE_OPEN_WAL |
| 12124 ); |
| 12125 |
| 12126 /* Detect a pid change and reset the PRNG. There is a race condition |
| 12127 ** here such that two or more threads all trying to open databases at |
| 12128 ** the same instant might all reset the PRNG. But multiple resets |
| 12129 ** are harmless. |
| 12130 */ |
| 12131 if( randomnessPid!=osGetpid(0) ){ |
| 12132 randomnessPid = osGetpid(0); |
| 12133 sqlite3_randomness(0,0); |
| 12134 } |
| 12135 |
| 12136 /* Duplicated in chromium_sqlite3_fill_in_unix_sqlite3_file(). */ |
| 12137 memset(p, 0, sizeof(unixFile)); |
| 12138 |
| 12139 if( eType==SQLITE_OPEN_MAIN_DB ){ |
| 12140 UnixUnusedFd *pUnused; |
| 12141 pUnused = findReusableFd(zName, flags); |
| 12142 if( pUnused ){ |
| 12143 fd = pUnused->fd; |
| 12144 }else{ |
| 12145 /* Duplicated in chromium_sqlite3_fill_in_unix_sqlite3_file(). */ |
| 12146 pUnused = sqlite3_malloc64(sizeof(*pUnused)); |
| 12147 if( !pUnused ){ |
| 12148 return SQLITE_NOMEM; |
| 12149 } |
| 12150 } |
| 12151 p->pUnused = pUnused; |
| 12152 |
| 12153 /* Database filenames are double-zero terminated if they are not |
| 12154 ** URIs with parameters. Hence, they can always be passed into |
| 12155 ** sqlite3_uri_parameter(). */ |
| 12156 assert( (flags & SQLITE_OPEN_URI) || zName[strlen(zName)+1]==0 ); |
| 12157 |
| 12158 }else if( !zName ){ |
| 12159 /* If zName is NULL, the upper layer is requesting a temp file. */ |
| 12160 assert(isDelete && !syncDir); |
| 12161 rc = unixGetTempname(pVfs->mxPathname, zTmpname); |
| 12162 if( rc!=SQLITE_OK ){ |
| 12163 return rc; |
| 12164 } |
| 12165 zName = zTmpname; |
| 12166 |
| 12167 /* Generated temporary filenames are always double-zero terminated |
| 12168 ** for use by sqlite3_uri_parameter(). */ |
| 12169 assert( zName[strlen(zName)+1]==0 ); |
| 12170 } |
| 12171 |
| 12172 /* Determine the value of the flags parameter passed to POSIX function |
| 12173 ** open(). These must be calculated even if open() is not called, as |
| 12174 ** they may be stored as part of the file handle and used by the |
| 12175 ** 'conch file' locking functions later on. */ |
| 12176 if( isReadonly ) openFlags |= O_RDONLY; |
| 12177 if( isReadWrite ) openFlags |= O_RDWR; |
| 12178 if( isCreate ) openFlags |= O_CREAT; |
| 12179 if( isExclusive ) openFlags |= (O_EXCL|O_NOFOLLOW); |
| 12180 openFlags |= (O_LARGEFILE|O_BINARY); |
| 12181 |
| 12182 if( fd<0 ){ |
| 12183 mode_t openMode; /* Permissions to create file with */ |
| 12184 uid_t uid; /* Userid for the file */ |
| 12185 gid_t gid; /* Groupid for the file */ |
| 12186 rc = findCreateFileMode(zName, flags, &openMode, &uid, &gid); |
| 12187 if( rc!=SQLITE_OK ){ |
| 12188 assert( !p->pUnused ); |
| 12189 assert( eType==SQLITE_OPEN_WAL || eType==SQLITE_OPEN_MAIN_JOURNAL ); |
| 12190 return rc; |
| 12191 } |
| 12192 fd = robust_open(zName, openFlags, openMode); |
| 12193 OSTRACE(("OPENX %-3d %s 0%o\n", fd, zName, openFlags)); |
| 12194 assert( !isExclusive || (openFlags & O_CREAT)!=0 ); |
| 12195 if( fd<0 && errno!=EISDIR && isReadWrite ){ |
| 12196 /* Failed to open the file for read/write access. Try read-only. */ |
| 12197 flags &= ~(SQLITE_OPEN_READWRITE|SQLITE_OPEN_CREATE); |
| 12198 openFlags &= ~(O_RDWR|O_CREAT); |
| 12199 flags |= SQLITE_OPEN_READONLY; |
| 12200 openFlags |= O_RDONLY; |
| 12201 isReadonly = 1; |
| 12202 fd = robust_open(zName, openFlags, openMode); |
| 12203 } |
| 12204 if( fd<0 ){ |
| 12205 rc = unixLogError(SQLITE_CANTOPEN_BKPT, "open", zName); |
| 12206 goto open_finished; |
| 12207 } |
| 12208 |
| 12209 /* If this process is running as root and if creating a new rollback |
| 12210 ** journal or WAL file, set the ownership of the journal or WAL to be |
| 12211 ** the same as the original database. |
| 12212 */ |
| 12213 if( flags & (SQLITE_OPEN_WAL|SQLITE_OPEN_MAIN_JOURNAL) ){ |
| 12214 robustFchown(fd, uid, gid); |
| 12215 } |
| 12216 } |
| 12217 assert( fd>=0 ); |
| 12218 if( pOutFlags ){ |
| 12219 *pOutFlags = flags; |
| 12220 } |
| 12221 |
| 12222 if( p->pUnused ){ |
| 12223 /* Duplicated in chromium_sqlite3_fill_in_unix_sqlite3_file(). */ |
| 12224 p->pUnused->fd = fd; |
| 12225 p->pUnused->flags = flags; |
| 12226 } |
| 12227 |
| 12228 if( isDelete ){ |
| 12229 #if OS_VXWORKS |
| 12230 zPath = zName; |
| 12231 #elif defined(SQLITE_UNLINK_AFTER_CLOSE) |
| 12232 zPath = sqlite3_mprintf("%s", zName); |
| 12233 if( zPath==0 ){ |
| 12234 robust_close(p, fd, __LINE__); |
| 12235 return SQLITE_NOMEM; |
| 12236 } |
| 12237 #else |
| 12238 osUnlink(zName); |
| 12239 #endif |
| 12240 } |
| 12241 #if SQLITE_ENABLE_LOCKING_STYLE |
| 12242 else{ |
| 12243 p->openFlags = openFlags; |
| 12244 } |
| 12245 #endif |
| 12246 |
| 12247 noLock = eType!=SQLITE_OPEN_MAIN_DB; |
| 12248 |
| 12249 |
| 12250 #if defined(__APPLE__) || SQLITE_ENABLE_LOCKING_STYLE |
| 12251 if( fstatfs(fd, &fsInfo) == -1 ){ |
| 12252 storeLastErrno(p, errno); |
| 12253 robust_close(p, fd, __LINE__); |
| 12254 return SQLITE_IOERR_ACCESS; |
| 12255 } |
| 12256 if (0 == strncmp("msdos", fsInfo.f_fstypename, 5)) { |
| 12257 ((unixFile*)pFile)->fsFlags |= SQLITE_FSFLAGS_IS_MSDOS; |
| 12258 } |
| 12259 if (0 == strncmp("exfat", fsInfo.f_fstypename, 5)) { |
| 12260 ((unixFile*)pFile)->fsFlags |= SQLITE_FSFLAGS_IS_MSDOS; |
| 12261 } |
| 12262 #endif |
| 12263 |
| 12264 /* Set up appropriate ctrlFlags */ |
| 12265 if( isDelete ) ctrlFlags |= UNIXFILE_DELETE; |
| 12266 if( isReadonly ) ctrlFlags |= UNIXFILE_RDONLY; |
| 12267 if( noLock ) ctrlFlags |= UNIXFILE_NOLOCK; |
| 12268 if( syncDir ) ctrlFlags |= UNIXFILE_DIRSYNC; |
| 12269 if( flags & SQLITE_OPEN_URI ) ctrlFlags |= UNIXFILE_URI; |
| 12270 |
| 12271 #if SQLITE_ENABLE_LOCKING_STYLE |
| 12272 #if SQLITE_PREFER_PROXY_LOCKING |
| 12273 isAutoProxy = 1; |
| 12274 #endif |
| 12275 if( isAutoProxy && (zPath!=NULL) && (!noLock) && pVfs->xOpen ){ |
| 12276 char *envforce = getenv("SQLITE_FORCE_PROXY_LOCKING"); |
| 12277 int useProxy = 0; |
| 12278 |
| 12279 /* SQLITE_FORCE_PROXY_LOCKING==1 means force always use proxy, 0 means |
| 12280 ** never use proxy, NULL means use proxy for non-local files only. */ |
| 12281 if( envforce!=NULL ){ |
| 12282 useProxy = atoi(envforce)>0; |
| 12283 }else{ |
| 12284 useProxy = !(fsInfo.f_flags&MNT_LOCAL); |
| 12285 } |
| 12286 if( useProxy ){ |
| 12287 rc = fillInUnixFile(pVfs, fd, pFile, zPath, ctrlFlags); |
| 12288 if( rc==SQLITE_OK ){ |
| 12289 rc = proxyTransformUnixFile((unixFile*)pFile, ":auto:"); |
| 12290 if( rc!=SQLITE_OK ){ |
| 12291 /* Use unixClose to clean up the resources added in fillInUnixFile |
| 12292 ** and clear all the structure's references. Specifically, |
| 12293 ** pFile->pMethods will be NULL so sqlite3OsClose will be a no-op |
| 12294 */ |
| 12295 unixClose(pFile); |
| 12296 return rc; |
| 12297 } |
| 12298 } |
| 12299 goto open_finished; |
| 12300 } |
| 12301 } |
| 12302 #endif |
| 12303 |
| 12304 /* Duplicated in chromium_sqlite3_fill_in_unix_sqlite3_file(). */ |
| 12305 rc = fillInUnixFile(pVfs, fd, pFile, zPath, ctrlFlags); |
| 12306 |
| 12307 open_finished: |
| 12308 if( rc!=SQLITE_OK ){ |
| 12309 /* Duplicated in chromium_sqlite3_fill_in_unix_sqlite3_file(). */ |
| 12310 sqlite3_free(p->pUnused); |
| 12311 } |
| 12312 return rc; |
| 12313 } |
| 12314 |
| 12315 |
| 12316 /* |
| 12317 ** Delete the file at zPath. If the dirSync argument is true, fsync() |
| 12318 ** the directory after deleting the file. |
| 12319 */ |
| 12320 static int unixDelete( |
| 12321 sqlite3_vfs *NotUsed, /* VFS containing this as the xDelete method */ |
| 12322 const char *zPath, /* Name of file to be deleted */ |
| 12323 int dirSync /* If true, fsync() directory after deleting file */ |
| 12324 ){ |
| 12325 int rc = SQLITE_OK; |
| 12326 UNUSED_PARAMETER(NotUsed); |
| 12327 SimulateIOError(return SQLITE_IOERR_DELETE); |
| 12328 if( osUnlink(zPath)==(-1) ){ |
| 12329 if( errno==ENOENT |
| 12330 #if OS_VXWORKS |
| 12331 || osAccess(zPath,0)!=0 |
| 12332 #endif |
| 12333 ){ |
| 12334 rc = SQLITE_IOERR_DELETE_NOENT; |
| 12335 }else{ |
| 12336 rc = unixLogError(SQLITE_IOERR_DELETE, "unlink", zPath); |
| 12337 } |
| 12338 return rc; |
| 12339 } |
| 12340 #ifndef SQLITE_DISABLE_DIRSYNC |
| 12341 if( (dirSync & 1)!=0 ){ |
| 12342 int fd; |
| 12343 rc = osOpenDirectory(zPath, &fd); |
| 12344 if( rc==SQLITE_OK ){ |
| 12345 #if OS_VXWORKS |
| 12346 if( fsync(fd)==-1 ) |
| 12347 #else |
| 12348 if( fsync(fd) ) |
| 12349 #endif |
| 12350 { |
| 12351 rc = unixLogError(SQLITE_IOERR_DIR_FSYNC, "fsync", zPath); |
| 12352 } |
| 12353 robust_close(0, fd, __LINE__); |
| 12354 }else{ |
| 12355 assert( rc==SQLITE_CANTOPEN ); |
| 12356 rc = SQLITE_OK; |
| 12357 } |
| 12358 } |
| 12359 #endif |
| 12360 return rc; |
| 12361 } |
| 12362 |
| 12363 /* |
| 12364 ** Test the existence of or access permissions of file zPath. The |
| 12365 ** test performed depends on the value of flags: |
| 12366 ** |
| 12367 ** SQLITE_ACCESS_EXISTS: Return 1 if the file exists |
| 12368 ** SQLITE_ACCESS_READWRITE: Return 1 if the file is read and writable. |
| 12369 ** SQLITE_ACCESS_READONLY: Return 1 if the file is readable. |
| 12370 ** |
| 12371 ** Otherwise return 0. |
| 12372 */ |
| 12373 static int unixAccess( |
| 12374 sqlite3_vfs *NotUsed, /* The VFS containing this xAccess method */ |
| 12375 const char *zPath, /* Path of the file to examine */ |
| 12376 int flags, /* What do we want to learn about the zPath file? */ |
| 12377 int *pResOut /* Write result boolean here */ |
| 12378 ){ |
| 12379 UNUSED_PARAMETER(NotUsed); |
| 12380 SimulateIOError( return SQLITE_IOERR_ACCESS; ); |
| 12381 assert( pResOut!=0 ); |
| 12382 |
| 12383 /* The spec says there are three possible values for flags. But only |
| 12384 ** two of them are actually used */ |
| 12385 assert( flags==SQLITE_ACCESS_EXISTS || flags==SQLITE_ACCESS_READWRITE ); |
| 12386 |
| 12387 if( flags==SQLITE_ACCESS_EXISTS ){ |
| 12388 struct stat buf; |
| 12389 *pResOut = (0==osStat(zPath, &buf) && buf.st_size>0); |
| 12390 }else{ |
| 12391 *pResOut = osAccess(zPath, W_OK|R_OK)==0; |
| 12392 } |
| 12393 return SQLITE_OK; |
| 12394 } |
| 12395 |
| 12396 |
| 12397 /* |
| 12398 ** Turn a relative pathname into a full pathname. The relative path |
| 12399 ** is stored as a nul-terminated string in the buffer pointed to by |
| 12400 ** zPath. |
| 12401 ** |
| 12402 ** zOut points to a buffer of at least sqlite3_vfs.mxPathname bytes |
| 12403 ** (in this case, MAX_PATHNAME bytes). The full-path is written to |
| 12404 ** this buffer before returning. |
| 12405 */ |
| 12406 static int unixFullPathname( |
| 12407 sqlite3_vfs *pVfs, /* Pointer to vfs object */ |
| 12408 const char *zPath, /* Possibly relative input path */ |
| 12409 int nOut, /* Size of output buffer in bytes */ |
| 12410 char *zOut /* Output buffer */ |
| 12411 ){ |
| 12412 int nByte; |
| 12413 |
| 12414 /* It's odd to simulate an io-error here, but really this is just |
| 12415 ** using the io-error infrastructure to test that SQLite handles this |
| 12416 ** function failing. This function could fail if, for example, the |
| 12417 ** current working directory has been unlinked. |
| 12418 */ |
| 12419 SimulateIOError( return SQLITE_ERROR ); |
| 12420 |
| 12421 assert( pVfs->mxPathname==MAX_PATHNAME ); |
| 12422 UNUSED_PARAMETER(pVfs); |
| 12423 |
| 12424 /* Attempt to resolve the path as if it were a symbolic link. If it is |
| 12425 ** a symbolic link, the resolved path is stored in buffer zOut[]. Or, if |
| 12426 ** the identified file is not a symbolic link or does not exist, then |
| 12427 ** zPath is copied directly into zOut. Either way, nByte is left set to |
| 12428 ** the size of the string copied into zOut[] in bytes. */ |
| 12429 nByte = osReadlink(zPath, zOut, nOut-1); |
| 12430 if( nByte<0 ){ |
| 12431 if( errno!=EINVAL && errno!=ENOENT ){ |
| 12432 return unixLogError(SQLITE_CANTOPEN_BKPT, "readlink", zPath); |
| 12433 } |
| 12434 sqlite3_snprintf(nOut, zOut, "%s", zPath); |
| 12435 nByte = sqlite3Strlen30(zOut); |
| 12436 }else{ |
| 12437 zOut[nByte] = '\0'; |
| 12438 } |
| 12439 |
| 12440 /* If buffer zOut[] now contains an absolute path there is nothing more |
| 12441 ** to do. If it contains a relative path, do the following: |
| 12442 ** |
| 12443 ** * move the relative path string so that it is at the end of th |
| 12444 ** zOut[] buffer. |
| 12445 ** * Call getcwd() to read the path of the current working directory |
| 12446 ** into the start of the zOut[] buffer. |
| 12447 ** * Append a '/' character to the cwd string and move the |
| 12448 ** relative path back within the buffer so that it immediately |
| 12449 ** follows the '/'. |
| 12450 ** |
| 12451 ** This code is written so that if the combination of the CWD and relative |
| 12452 ** path are larger than the allocated size of zOut[] the CWD is silently |
| 12453 ** truncated to make it fit. This is Ok, as SQLite refuses to open any |
| 12454 ** file for which this function returns a full path larger than (nOut-8) |
| 12455 ** bytes in size. */ |
| 12456 testcase( nByte==nOut-5 ); |
| 12457 testcase( nByte==nOut-4 ); |
| 12458 if( zOut[0]!='/' && nByte<nOut-4 ){ |
| 12459 int nCwd; |
| 12460 int nRem = nOut-nByte-1; |
| 12461 memmove(&zOut[nRem], zOut, nByte+1); |
| 12462 zOut[nRem-1] = '\0'; |
| 12463 if( osGetcwd(zOut, nRem-1)==0 ){ |
| 12464 return unixLogError(SQLITE_CANTOPEN_BKPT, "getcwd", zPath); |
| 12465 } |
| 12466 nCwd = sqlite3Strlen30(zOut); |
| 12467 assert( nCwd<=nRem-1 ); |
| 12468 zOut[nCwd] = '/'; |
| 12469 memmove(&zOut[nCwd+1], &zOut[nRem], nByte+1); |
| 12470 } |
| 12471 |
| 12472 return SQLITE_OK; |
| 12473 } |
| 12474 |
| 12475 |
| 12476 #ifndef SQLITE_OMIT_LOAD_EXTENSION |
| 12477 /* |
| 12478 ** Interfaces for opening a shared library, finding entry points |
| 12479 ** within the shared library, and closing the shared library. |
| 12480 */ |
| 12481 #include <dlfcn.h> |
| 12482 static void *unixDlOpen(sqlite3_vfs *NotUsed, const char *zFilename){ |
| 12483 UNUSED_PARAMETER(NotUsed); |
| 12484 return dlopen(zFilename, RTLD_NOW | RTLD_GLOBAL); |
| 12485 } |
| 12486 |
| 12487 /* |
| 12488 ** SQLite calls this function immediately after a call to unixDlSym() or |
| 12489 ** unixDlOpen() fails (returns a null pointer). If a more detailed error |
| 12490 ** message is available, it is written to zBufOut. If no error message |
| 12491 ** is available, zBufOut is left unmodified and SQLite uses a default |
| 12492 ** error message. |
| 12493 */ |
| 12494 static void unixDlError(sqlite3_vfs *NotUsed, int nBuf, char *zBufOut){ |
| 12495 const char *zErr; |
| 12496 UNUSED_PARAMETER(NotUsed); |
| 12497 unixEnterMutex(); |
| 12498 zErr = dlerror(); |
| 12499 if( zErr ){ |
| 12500 sqlite3_snprintf(nBuf, zBufOut, "%s", zErr); |
| 12501 } |
| 12502 unixLeaveMutex(); |
| 12503 } |
| 12504 static void (*unixDlSym(sqlite3_vfs *NotUsed, void *p, const char*zSym))(void){ |
| 12505 /* |
| 12506 ** GCC with -pedantic-errors says that C90 does not allow a void* to be |
| 12507 ** cast into a pointer to a function. And yet the library dlsym() routine |
| 12508 ** returns a void* which is really a pointer to a function. So how do we |
| 12509 ** use dlsym() with -pedantic-errors? |
| 12510 ** |
| 12511 ** Variable x below is defined to be a pointer to a function taking |
| 12512 ** parameters void* and const char* and returning a pointer to a function. |
| 12513 ** We initialize x by assigning it a pointer to the dlsym() function. |
| 12514 ** (That assignment requires a cast.) Then we call the function that |
| 12515 ** x points to. |
| 12516 ** |
| 12517 ** This work-around is unlikely to work correctly on any system where |
| 12518 ** you really cannot cast a function pointer into void*. But then, on the |
| 12519 ** other hand, dlsym() will not work on such a system either, so we have |
| 12520 ** not really lost anything. |
| 12521 */ |
| 12522 void (*(*x)(void*,const char*))(void); |
| 12523 UNUSED_PARAMETER(NotUsed); |
| 12524 x = (void(*(*)(void*,const char*))(void))dlsym; |
| 12525 return (*x)(p, zSym); |
| 12526 } |
| 12527 static void unixDlClose(sqlite3_vfs *NotUsed, void *pHandle){ |
| 12528 UNUSED_PARAMETER(NotUsed); |
| 12529 dlclose(pHandle); |
| 12530 } |
| 12531 #else /* if SQLITE_OMIT_LOAD_EXTENSION is defined: */ |
| 12532 #define unixDlOpen 0 |
| 12533 #define unixDlError 0 |
| 12534 #define unixDlSym 0 |
| 12535 #define unixDlClose 0 |
| 12536 #endif |
| 12537 |
| 12538 /* |
| 12539 ** Write nBuf bytes of random data to the supplied buffer zBuf. |
| 12540 */ |
| 12541 static int unixRandomness(sqlite3_vfs *NotUsed, int nBuf, char *zBuf){ |
| 12542 UNUSED_PARAMETER(NotUsed); |
| 12543 assert((size_t)nBuf>=(sizeof(time_t)+sizeof(int))); |
| 12544 |
| 12545 /* We have to initialize zBuf to prevent valgrind from reporting |
| 12546 ** errors. The reports issued by valgrind are incorrect - we would |
| 12547 ** prefer that the randomness be increased by making use of the |
| 12548 ** uninitialized space in zBuf - but valgrind errors tend to worry |
| 12549 ** some users. Rather than argue, it seems easier just to initialize |
| 12550 ** the whole array and silence valgrind, even if that means less randomness |
| 12551 ** in the random seed. |
| 12552 ** |
| 12553 ** When testing, initializing zBuf[] to zero is all we do. That means |
| 12554 ** that we always use the same random number sequence. This makes the |
| 12555 ** tests repeatable. |
| 12556 */ |
| 12557 memset(zBuf, 0, nBuf); |
| 12558 randomnessPid = osGetpid(0); |
| 12559 #if !defined(SQLITE_TEST) && !defined(SQLITE_OMIT_RANDOMNESS) |
| 12560 { |
| 12561 int fd, got; |
| 12562 fd = robust_open("/dev/urandom", O_RDONLY, 0); |
| 12563 if( fd<0 ){ |
| 12564 time_t t; |
| 12565 time(&t); |
| 12566 memcpy(zBuf, &t, sizeof(t)); |
| 12567 memcpy(&zBuf[sizeof(t)], &randomnessPid, sizeof(randomnessPid)); |
| 12568 assert( sizeof(t)+sizeof(randomnessPid)<=(size_t)nBuf ); |
| 12569 nBuf = sizeof(t) + sizeof(randomnessPid); |
| 12570 }else{ |
| 12571 do{ got = osRead(fd, zBuf, nBuf); }while( got<0 && errno==EINTR ); |
| 12572 robust_close(0, fd, __LINE__); |
| 12573 } |
| 12574 } |
| 12575 #endif |
| 12576 return nBuf; |
| 12577 } |
| 12578 |
| 12579 |
| 12580 /* |
| 12581 ** Sleep for a little while. Return the amount of time slept. |
| 12582 ** The argument is the number of microseconds we want to sleep. |
| 12583 ** The return value is the number of microseconds of sleep actually |
| 12584 ** requested from the underlying operating system, a number which |
| 12585 ** might be greater than or equal to the argument, but not less |
| 12586 ** than the argument. |
| 12587 */ |
| 12588 static int unixSleep(sqlite3_vfs *NotUsed, int microseconds){ |
| 12589 #if OS_VXWORKS |
| 12590 struct timespec sp; |
| 12591 |
| 12592 sp.tv_sec = microseconds / 1000000; |
| 12593 sp.tv_nsec = (microseconds % 1000000) * 1000; |
| 12594 nanosleep(&sp, NULL); |
| 12595 UNUSED_PARAMETER(NotUsed); |
| 12596 return microseconds; |
| 12597 #elif defined(HAVE_USLEEP) && HAVE_USLEEP |
| 12598 usleep(microseconds); |
| 12599 UNUSED_PARAMETER(NotUsed); |
| 12600 return microseconds; |
| 12601 #else |
| 12602 int seconds = (microseconds+999999)/1000000; |
| 12603 sleep(seconds); |
| 12604 UNUSED_PARAMETER(NotUsed); |
| 12605 return seconds*1000000; |
| 12606 #endif |
| 12607 } |
| 12608 |
| 12609 /* |
| 12610 ** The following variable, if set to a non-zero value, is interpreted as |
| 12611 ** the number of seconds since 1970 and is used to set the result of |
| 12612 ** sqlite3OsCurrentTime() during testing. |
| 12613 */ |
| 12614 #ifdef SQLITE_TEST |
| 12615 SQLITE_API int sqlite3_current_time = 0; /* Fake system time in seconds since 1
970. */ |
| 12616 #endif |
| 12617 |
| 12618 /* |
| 12619 ** Find the current time (in Universal Coordinated Time). Write into *piNow |
| 12620 ** the current time and date as a Julian Day number times 86_400_000. In |
| 12621 ** other words, write into *piNow the number of milliseconds since the Julian |
| 12622 ** epoch of noon in Greenwich on November 24, 4714 B.C according to the |
| 12623 ** proleptic Gregorian calendar. |
| 12624 ** |
| 12625 ** On success, return SQLITE_OK. Return SQLITE_ERROR if the time and date |
| 12626 ** cannot be found. |
| 12627 */ |
| 12628 static int unixCurrentTimeInt64(sqlite3_vfs *NotUsed, sqlite3_int64 *piNow){ |
| 12629 static const sqlite3_int64 unixEpoch = 24405875*(sqlite3_int64)8640000; |
| 12630 int rc = SQLITE_OK; |
| 12631 #if defined(NO_GETTOD) |
| 12632 time_t t; |
| 12633 time(&t); |
| 12634 *piNow = ((sqlite3_int64)t)*1000 + unixEpoch; |
| 12635 #elif OS_VXWORKS |
| 12636 struct timespec sNow; |
| 12637 clock_gettime(CLOCK_REALTIME, &sNow); |
| 12638 *piNow = unixEpoch + 1000*(sqlite3_int64)sNow.tv_sec + sNow.tv_nsec/1000000; |
| 12639 #else |
| 12640 struct timeval sNow; |
| 12641 (void)gettimeofday(&sNow, 0); /* Cannot fail given valid arguments */ |
| 12642 *piNow = unixEpoch + 1000*(sqlite3_int64)sNow.tv_sec + sNow.tv_usec/1000; |
| 12643 #endif |
| 12644 |
| 12645 #ifdef SQLITE_TEST |
| 12646 if( sqlite3_current_time ){ |
| 12647 *piNow = 1000*(sqlite3_int64)sqlite3_current_time + unixEpoch; |
| 12648 } |
| 12649 #endif |
| 12650 UNUSED_PARAMETER(NotUsed); |
| 12651 return rc; |
| 12652 } |
| 12653 |
| 12654 #ifndef SQLITE_OMIT_DEPRECATED |
| 12655 /* |
| 12656 ** Find the current time (in Universal Coordinated Time). Write the |
| 12657 ** current time and date as a Julian Day number into *prNow and |
| 12658 ** return 0. Return 1 if the time and date cannot be found. |
| 12659 */ |
| 12660 static int unixCurrentTime(sqlite3_vfs *NotUsed, double *prNow){ |
| 12661 sqlite3_int64 i = 0; |
| 12662 int rc; |
| 12663 UNUSED_PARAMETER(NotUsed); |
| 12664 rc = unixCurrentTimeInt64(0, &i); |
| 12665 *prNow = i/86400000.0; |
| 12666 return rc; |
| 12667 } |
| 12668 #else |
| 12669 # define unixCurrentTime 0 |
| 12670 #endif |
| 12671 |
| 12672 #ifndef SQLITE_OMIT_DEPRECATED |
| 12673 /* |
| 12674 ** We added the xGetLastError() method with the intention of providing |
| 12675 ** better low-level error messages when operating-system problems come up |
| 12676 ** during SQLite operation. But so far, none of that has been implemented |
| 12677 ** in the core. So this routine is never called. For now, it is merely |
| 12678 ** a place-holder. |
| 12679 */ |
| 12680 static int unixGetLastError(sqlite3_vfs *NotUsed, int NotUsed2, char *NotUsed3){ |
| 12681 UNUSED_PARAMETER(NotUsed); |
| 12682 UNUSED_PARAMETER(NotUsed2); |
| 12683 UNUSED_PARAMETER(NotUsed3); |
| 12684 return 0; |
| 12685 } |
| 12686 #else |
| 12687 # define unixGetLastError 0 |
| 12688 #endif |
| 12689 |
| 12690 |
| 12691 /* |
| 12692 ************************ End of sqlite3_vfs methods *************************** |
| 12693 ******************************************************************************/ |
| 12694 |
| 12695 /****************************************************************************** |
| 12696 ************************** Begin Proxy Locking ******************************** |
| 12697 ** |
| 12698 ** Proxy locking is a "uber-locking-method" in this sense: It uses the |
| 12699 ** other locking methods on secondary lock files. Proxy locking is a |
| 12700 ** meta-layer over top of the primitive locking implemented above. For |
| 12701 ** this reason, the division that implements of proxy locking is deferred |
| 12702 ** until late in the file (here) after all of the other I/O methods have |
| 12703 ** been defined - so that the primitive locking methods are available |
| 12704 ** as services to help with the implementation of proxy locking. |
| 12705 ** |
| 12706 **** |
| 12707 ** |
| 12708 ** The default locking schemes in SQLite use byte-range locks on the |
| 12709 ** database file to coordinate safe, concurrent access by multiple readers |
| 12710 ** and writers [http://sqlite.org/lockingv3.html]. The five file locking |
| 12711 ** states (UNLOCKED, PENDING, SHARED, RESERVED, EXCLUSIVE) are implemented |
| 12712 ** as POSIX read & write locks over fixed set of locations (via fsctl), |
| 12713 ** on AFP and SMB only exclusive byte-range locks are available via fsctl |
| 12714 ** with _IOWR('z', 23, struct ByteRangeLockPB2) to track the same 5 states. |
| 12715 ** To simulate a F_RDLCK on the shared range, on AFP a randomly selected |
| 12716 ** address in the shared range is taken for a SHARED lock, the entire |
| 12717 ** shared range is taken for an EXCLUSIVE lock): |
| 12718 ** |
| 12719 ** PENDING_BYTE 0x40000000 |
| 12720 ** RESERVED_BYTE 0x40000001 |
| 12721 ** SHARED_RANGE 0x40000002 -> 0x40000200 |
| 12722 ** |
| 12723 ** This works well on the local file system, but shows a nearly 100x |
| 12724 ** slowdown in read performance on AFP because the AFP client disables |
| 12725 ** the read cache when byte-range locks are present. Enabling the read |
| 12726 ** cache exposes a cache coherency problem that is present on all OS X |
| 12727 ** supported network file systems. NFS and AFP both observe the |
| 12728 ** close-to-open semantics for ensuring cache coherency |
| 12729 ** [http://nfs.sourceforge.net/#faq_a8], which does not effectively |
| 12730 ** address the requirements for concurrent database access by multiple |
| 12731 ** readers and writers |
| 12732 ** [http://www.nabble.com/SQLite-on-NFS-cache-coherency-td15655701.html]. |
| 12733 ** |
| 12734 ** To address the performance and cache coherency issues, proxy file locking |
| 12735 ** changes the way database access is controlled by limiting access to a |
| 12736 ** single host at a time and moving file locks off of the database file |
| 12737 ** and onto a proxy file on the local file system. |
| 12738 ** |
| 12739 ** |
| 12740 ** Using proxy locks |
| 12741 ** ----------------- |
| 12742 ** |
| 12743 ** C APIs |
| 12744 ** |
| 12745 ** sqlite3_file_control(db, dbname, SQLITE_FCNTL_SET_LOCKPROXYFILE, |
| 12746 ** <proxy_path> | ":auto:"); |
| 12747 ** sqlite3_file_control(db, dbname, SQLITE_FCNTL_GET_LOCKPROXYFILE, |
| 12748 ** &<proxy_path>); |
| 12749 ** |
| 12750 ** |
| 12751 ** SQL pragmas |
| 12752 ** |
| 12753 ** PRAGMA [database.]lock_proxy_file=<proxy_path> | :auto: |
| 12754 ** PRAGMA [database.]lock_proxy_file |
| 12755 ** |
| 12756 ** Specifying ":auto:" means that if there is a conch file with a matching |
| 12757 ** host ID in it, the proxy path in the conch file will be used, otherwise |
| 12758 ** a proxy path based on the user's temp dir |
| 12759 ** (via confstr(_CS_DARWIN_USER_TEMP_DIR,...)) will be used and the |
| 12760 ** actual proxy file name is generated from the name and path of the |
| 12761 ** database file. For example: |
| 12762 ** |
| 12763 ** For database path "/Users/me/foo.db" |
| 12764 ** The lock path will be "<tmpdir>/sqliteplocks/_Users_me_foo.db:auto:") |
| 12765 ** |
| 12766 ** Once a lock proxy is configured for a database connection, it can not |
| 12767 ** be removed, however it may be switched to a different proxy path via |
| 12768 ** the above APIs (assuming the conch file is not being held by another |
| 12769 ** connection or process). |
| 12770 ** |
| 12771 ** |
| 12772 ** How proxy locking works |
| 12773 ** ----------------------- |
| 12774 ** |
| 12775 ** Proxy file locking relies primarily on two new supporting files: |
| 12776 ** |
| 12777 ** * conch file to limit access to the database file to a single host |
| 12778 ** at a time |
| 12779 ** |
| 12780 ** * proxy file to act as a proxy for the advisory locks normally |
| 12781 ** taken on the database |
| 12782 ** |
| 12783 ** The conch file - to use a proxy file, sqlite must first "hold the conch" |
| 12784 ** by taking an sqlite-style shared lock on the conch file, reading the |
| 12785 ** contents and comparing the host's unique host ID (see below) and lock |
| 12786 ** proxy path against the values stored in the conch. The conch file is |
| 12787 ** stored in the same directory as the database file and the file name |
| 12788 ** is patterned after the database file name as ".<databasename>-conch". |
| 12789 ** If the conch file does not exist, or its contents do not match the |
| 12790 ** host ID and/or proxy path, then the lock is escalated to an exclusive |
| 12791 ** lock and the conch file contents is updated with the host ID and proxy |
| 12792 ** path and the lock is downgraded to a shared lock again. If the conch |
| 12793 ** is held by another process (with a shared lock), the exclusive lock |
| 12794 ** will fail and SQLITE_BUSY is returned. |
| 12795 ** |
| 12796 ** The proxy file - a single-byte file used for all advisory file locks |
| 12797 ** normally taken on the database file. This allows for safe sharing |
| 12798 ** of the database file for multiple readers and writers on the same |
| 12799 ** host (the conch ensures that they all use the same local lock file). |
| 12800 ** |
| 12801 ** Requesting the lock proxy does not immediately take the conch, it is |
| 12802 ** only taken when the first request to lock database file is made. |
| 12803 ** This matches the semantics of the traditional locking behavior, where |
| 12804 ** opening a connection to a database file does not take a lock on it. |
| 12805 ** The shared lock and an open file descriptor are maintained until |
| 12806 ** the connection to the database is closed. |
| 12807 ** |
| 12808 ** The proxy file and the lock file are never deleted so they only need |
| 12809 ** to be created the first time they are used. |
| 12810 ** |
| 12811 ** Configuration options |
| 12812 ** --------------------- |
| 12813 ** |
| 12814 ** SQLITE_PREFER_PROXY_LOCKING |
| 12815 ** |
| 12816 ** Database files accessed on non-local file systems are |
| 12817 ** automatically configured for proxy locking, lock files are |
| 12818 ** named automatically using the same logic as |
| 12819 ** PRAGMA lock_proxy_file=":auto:" |
| 12820 ** |
| 12821 ** SQLITE_PROXY_DEBUG |
| 12822 ** |
| 12823 ** Enables the logging of error messages during host id file |
| 12824 ** retrieval and creation |
| 12825 ** |
| 12826 ** LOCKPROXYDIR |
| 12827 ** |
| 12828 ** Overrides the default directory used for lock proxy files that |
| 12829 ** are named automatically via the ":auto:" setting |
| 12830 ** |
| 12831 ** SQLITE_DEFAULT_PROXYDIR_PERMISSIONS |
| 12832 ** |
| 12833 ** Permissions to use when creating a directory for storing the |
| 12834 ** lock proxy files, only used when LOCKPROXYDIR is not set. |
| 12835 ** |
| 12836 ** |
| 12837 ** As mentioned above, when compiled with SQLITE_PREFER_PROXY_LOCKING, |
| 12838 ** setting the environment variable SQLITE_FORCE_PROXY_LOCKING to 1 will |
| 12839 ** force proxy locking to be used for every database file opened, and 0 |
| 12840 ** will force automatic proxy locking to be disabled for all database |
| 12841 ** files (explicitly calling the SQLITE_FCNTL_SET_LOCKPROXYFILE pragma or |
| 12842 ** sqlite_file_control API is not affected by SQLITE_FORCE_PROXY_LOCKING). |
| 12843 */ |
| 12844 |
| 12845 /* |
| 12846 ** Proxy locking is only available on MacOSX |
| 12847 */ |
| 12848 #if defined(__APPLE__) && SQLITE_ENABLE_LOCKING_STYLE |
| 12849 |
| 12850 /* |
| 12851 ** The proxyLockingContext has the path and file structures for the remote |
| 12852 ** and local proxy files in it |
| 12853 */ |
| 12854 typedef struct proxyLockingContext proxyLockingContext; |
| 12855 struct proxyLockingContext { |
| 12856 unixFile *conchFile; /* Open conch file */ |
| 12857 char *conchFilePath; /* Name of the conch file */ |
| 12858 unixFile *lockProxy; /* Open proxy lock file */ |
| 12859 char *lockProxyPath; /* Name of the proxy lock file */ |
| 12860 char *dbPath; /* Name of the open file */ |
| 12861 int conchHeld; /* 1 if the conch is held, -1 if lockless */ |
| 12862 int nFails; /* Number of conch taking failures */ |
| 12863 void *oldLockingContext; /* Original lockingcontext to restore on close */ |
| 12864 sqlite3_io_methods const *pOldMethod; /* Original I/O methods for close */ |
| 12865 }; |
| 12866 |
| 12867 /* |
| 12868 ** The proxy lock file path for the database at dbPath is written into lPath, |
| 12869 ** which must point to valid, writable memory large enough for a maxLen length |
| 12870 ** file path. |
| 12871 */ |
| 12872 static int proxyGetLockPath(const char *dbPath, char *lPath, size_t maxLen){ |
| 12873 int len; |
| 12874 int dbLen; |
| 12875 int i; |
| 12876 |
| 12877 #ifdef LOCKPROXYDIR |
| 12878 len = strlcpy(lPath, LOCKPROXYDIR, maxLen); |
| 12879 #else |
| 12880 # ifdef _CS_DARWIN_USER_TEMP_DIR |
| 12881 { |
| 12882 if( !confstr(_CS_DARWIN_USER_TEMP_DIR, lPath, maxLen) ){ |
| 12883 OSTRACE(("GETLOCKPATH failed %s errno=%d pid=%d\n", |
| 12884 lPath, errno, osGetpid(0))); |
| 12885 return SQLITE_IOERR_LOCK; |
| 12886 } |
| 12887 len = strlcat(lPath, "sqliteplocks", maxLen); |
| 12888 } |
| 12889 # else |
| 12890 len = strlcpy(lPath, "/tmp/", maxLen); |
| 12891 # endif |
| 12892 #endif |
| 12893 |
| 12894 if( lPath[len-1]!='/' ){ |
| 12895 len = strlcat(lPath, "/", maxLen); |
| 12896 } |
| 12897 |
| 12898 /* transform the db path to a unique cache name */ |
| 12899 dbLen = (int)strlen(dbPath); |
| 12900 for( i=0; i<dbLen && (i+len+7)<(int)maxLen; i++){ |
| 12901 char c = dbPath[i]; |
| 12902 lPath[i+len] = (c=='/')?'_':c; |
| 12903 } |
| 12904 lPath[i+len]='\0'; |
| 12905 strlcat(lPath, ":auto:", maxLen); |
| 12906 OSTRACE(("GETLOCKPATH proxy lock path=%s pid=%d\n", lPath, osGetpid(0))); |
| 12907 return SQLITE_OK; |
| 12908 } |
| 12909 |
| 12910 /* |
| 12911 ** Creates the lock file and any missing directories in lockPath |
| 12912 */ |
| 12913 static int proxyCreateLockPath(const char *lockPath){ |
| 12914 int i, len; |
| 12915 char buf[MAXPATHLEN]; |
| 12916 int start = 0; |
| 12917 |
| 12918 assert(lockPath!=NULL); |
| 12919 /* try to create all the intermediate directories */ |
| 12920 len = (int)strlen(lockPath); |
| 12921 buf[0] = lockPath[0]; |
| 12922 for( i=1; i<len; i++ ){ |
| 12923 if( lockPath[i] == '/' && (i - start > 0) ){ |
| 12924 /* only mkdir if leaf dir != "." or "/" or ".." */ |
| 12925 if( i-start>2 || (i-start==1 && buf[start] != '.' && buf[start] != '/') |
| 12926 || (i-start==2 && buf[start] != '.' && buf[start+1] != '.') ){ |
| 12927 buf[i]='\0'; |
| 12928 if( osMkdir(buf, SQLITE_DEFAULT_PROXYDIR_PERMISSIONS) ){ |
| 12929 int err=errno; |
| 12930 if( err!=EEXIST ) { |
| 12931 OSTRACE(("CREATELOCKPATH FAILED creating %s, " |
| 12932 "'%s' proxy lock path=%s pid=%d\n", |
| 12933 buf, strerror(err), lockPath, osGetpid(0))); |
| 12934 return err; |
| 12935 } |
| 12936 } |
| 12937 } |
| 12938 start=i+1; |
| 12939 } |
| 12940 buf[i] = lockPath[i]; |
| 12941 } |
| 12942 OSTRACE(("CREATELOCKPATH proxy lock path=%s pid=%d\n",lockPath,osGetpid(0))); |
| 12943 return 0; |
| 12944 } |
| 12945 |
| 12946 /* |
| 12947 ** Create a new VFS file descriptor (stored in memory obtained from |
| 12948 ** sqlite3_malloc) and open the file named "path" in the file descriptor. |
| 12949 ** |
| 12950 ** The caller is responsible not only for closing the file descriptor |
| 12951 ** but also for freeing the memory associated with the file descriptor. |
| 12952 */ |
| 12953 static int proxyCreateUnixFile( |
| 12954 const char *path, /* path for the new unixFile */ |
| 12955 unixFile **ppFile, /* unixFile created and returned by ref */ |
| 12956 int islockfile /* if non zero missing dirs will be created */ |
| 12957 ) { |
| 12958 int fd = -1; |
| 12959 unixFile *pNew; |
| 12960 int rc = SQLITE_OK; |
| 12961 int openFlags = O_RDWR | O_CREAT; |
| 12962 sqlite3_vfs dummyVfs; |
| 12963 int terrno = 0; |
| 12964 UnixUnusedFd *pUnused = NULL; |
| 12965 |
| 12966 /* 1. first try to open/create the file |
| 12967 ** 2. if that fails, and this is a lock file (not-conch), try creating |
| 12968 ** the parent directories and then try again. |
| 12969 ** 3. if that fails, try to open the file read-only |
| 12970 ** otherwise return BUSY (if lock file) or CANTOPEN for the conch file |
| 12971 */ |
| 12972 pUnused = findReusableFd(path, openFlags); |
| 12973 if( pUnused ){ |
| 12974 fd = pUnused->fd; |
| 12975 }else{ |
| 12976 pUnused = sqlite3_malloc64(sizeof(*pUnused)); |
| 12977 if( !pUnused ){ |
| 12978 return SQLITE_NOMEM; |
| 12979 } |
| 12980 } |
| 12981 if( fd<0 ){ |
| 12982 fd = robust_open(path, openFlags, 0); |
| 12983 terrno = errno; |
| 12984 if( fd<0 && errno==ENOENT && islockfile ){ |
| 12985 if( proxyCreateLockPath(path) == SQLITE_OK ){ |
| 12986 fd = robust_open(path, openFlags, 0); |
| 12987 } |
| 12988 } |
| 12989 } |
| 12990 if( fd<0 ){ |
| 12991 openFlags = O_RDONLY; |
| 12992 fd = robust_open(path, openFlags, 0); |
| 12993 terrno = errno; |
| 12994 } |
| 12995 if( fd<0 ){ |
| 12996 if( islockfile ){ |
| 12997 return SQLITE_BUSY; |
| 12998 } |
| 12999 switch (terrno) { |
| 13000 case EACCES: |
| 13001 return SQLITE_PERM; |
| 13002 case EIO: |
| 13003 return SQLITE_IOERR_LOCK; /* even though it is the conch */ |
| 13004 default: |
| 13005 return SQLITE_CANTOPEN_BKPT; |
| 13006 } |
| 13007 } |
| 13008 |
| 13009 pNew = (unixFile *)sqlite3_malloc64(sizeof(*pNew)); |
| 13010 if( pNew==NULL ){ |
| 13011 rc = SQLITE_NOMEM; |
| 13012 goto end_create_proxy; |
| 13013 } |
| 13014 memset(pNew, 0, sizeof(unixFile)); |
| 13015 pNew->openFlags = openFlags; |
| 13016 memset(&dummyVfs, 0, sizeof(dummyVfs)); |
| 13017 dummyVfs.pAppData = (void*)&autolockIoFinder; |
| 13018 dummyVfs.zName = "dummy"; |
| 13019 pUnused->fd = fd; |
| 13020 pUnused->flags = openFlags; |
| 13021 pNew->pUnused = pUnused; |
| 13022 |
| 13023 rc = fillInUnixFile(&dummyVfs, fd, (sqlite3_file*)pNew, path, 0); |
| 13024 if( rc==SQLITE_OK ){ |
| 13025 *ppFile = pNew; |
| 13026 return SQLITE_OK; |
| 13027 } |
| 13028 end_create_proxy: |
| 13029 robust_close(pNew, fd, __LINE__); |
| 13030 sqlite3_free(pNew); |
| 13031 sqlite3_free(pUnused); |
| 13032 return rc; |
| 13033 } |
| 13034 |
| 13035 #ifdef SQLITE_TEST |
| 13036 /* simulate multiple hosts by creating unique hostid file paths */ |
| 13037 SQLITE_API int sqlite3_hostid_num = 0; |
| 13038 #endif |
| 13039 |
| 13040 #define PROXY_HOSTIDLEN 16 /* conch file host id length */ |
| 13041 |
| 13042 #ifdef HAVE_GETHOSTUUID |
| 13043 /* Not always defined in the headers as it ought to be */ |
| 13044 extern int gethostuuid(uuid_t id, const struct timespec *wait); |
| 13045 #endif |
| 13046 |
| 13047 /* get the host ID via gethostuuid(), pHostID must point to PROXY_HOSTIDLEN |
| 13048 ** bytes of writable memory. |
| 13049 */ |
| 13050 static int proxyGetHostID(unsigned char *pHostID, int *pError){ |
| 13051 assert(PROXY_HOSTIDLEN == sizeof(uuid_t)); |
| 13052 memset(pHostID, 0, PROXY_HOSTIDLEN); |
| 13053 #ifdef HAVE_GETHOSTUUID |
| 13054 { |
| 13055 struct timespec timeout = {1, 0}; /* 1 sec timeout */ |
| 13056 if( gethostuuid(pHostID, &timeout) ){ |
| 13057 int err = errno; |
| 13058 if( pError ){ |
| 13059 *pError = err; |
| 13060 } |
| 13061 return SQLITE_IOERR; |
| 13062 } |
| 13063 } |
| 13064 #else |
| 13065 UNUSED_PARAMETER(pError); |
| 13066 #endif |
| 13067 #ifdef SQLITE_TEST |
| 13068 /* simulate multiple hosts by creating unique hostid file paths */ |
| 13069 if( sqlite3_hostid_num != 0){ |
| 13070 pHostID[0] = (char)(pHostID[0] + (char)(sqlite3_hostid_num & 0xFF)); |
| 13071 } |
| 13072 #endif |
| 13073 |
| 13074 return SQLITE_OK; |
| 13075 } |
| 13076 |
| 13077 /* The conch file contains the header, host id and lock file path |
| 13078 */ |
| 13079 #define PROXY_CONCHVERSION 2 /* 1-byte header, 16-byte host id, path */ |
| 13080 #define PROXY_HEADERLEN 1 /* conch file header length */ |
| 13081 #define PROXY_PATHINDEX (PROXY_HEADERLEN+PROXY_HOSTIDLEN) |
| 13082 #define PROXY_MAXCONCHLEN (PROXY_HEADERLEN+PROXY_HOSTIDLEN+MAXPATHLEN) |
| 13083 |
| 13084 /* |
| 13085 ** Takes an open conch file, copies the contents to a new path and then moves |
| 13086 ** it back. The newly created file's file descriptor is assigned to the |
| 13087 ** conch file structure and finally the original conch file descriptor is |
| 13088 ** closed. Returns zero if successful. |
| 13089 */ |
| 13090 static int proxyBreakConchLock(unixFile *pFile, uuid_t myHostID){ |
| 13091 proxyLockingContext *pCtx = (proxyLockingContext *)pFile->lockingContext; |
| 13092 unixFile *conchFile = pCtx->conchFile; |
| 13093 char tPath[MAXPATHLEN]; |
| 13094 char buf[PROXY_MAXCONCHLEN]; |
| 13095 char *cPath = pCtx->conchFilePath; |
| 13096 size_t readLen = 0; |
| 13097 size_t pathLen = 0; |
| 13098 char errmsg[64] = ""; |
| 13099 int fd = -1; |
| 13100 int rc = -1; |
| 13101 UNUSED_PARAMETER(myHostID); |
| 13102 |
| 13103 /* create a new path by replace the trailing '-conch' with '-break' */ |
| 13104 pathLen = strlcpy(tPath, cPath, MAXPATHLEN); |
| 13105 if( pathLen>MAXPATHLEN || pathLen<6 || |
| 13106 (strlcpy(&tPath[pathLen-5], "break", 6) != 5) ){ |
| 13107 sqlite3_snprintf(sizeof(errmsg),errmsg,"path error (len %d)",(int)pathLen); |
| 13108 goto end_breaklock; |
| 13109 } |
| 13110 /* read the conch content */ |
| 13111 readLen = osPread(conchFile->h, buf, PROXY_MAXCONCHLEN, 0); |
| 13112 if( readLen<PROXY_PATHINDEX ){ |
| 13113 sqlite3_snprintf(sizeof(errmsg),errmsg,"read error (len %d)",(int)readLen); |
| 13114 goto end_breaklock; |
| 13115 } |
| 13116 /* write it out to the temporary break file */ |
| 13117 fd = robust_open(tPath, (O_RDWR|O_CREAT|O_EXCL), 0); |
| 13118 if( fd<0 ){ |
| 13119 sqlite3_snprintf(sizeof(errmsg), errmsg, "create failed (%d)", errno); |
| 13120 goto end_breaklock; |
| 13121 } |
| 13122 if( osPwrite(fd, buf, readLen, 0) != (ssize_t)readLen ){ |
| 13123 sqlite3_snprintf(sizeof(errmsg), errmsg, "write failed (%d)", errno); |
| 13124 goto end_breaklock; |
| 13125 } |
| 13126 if( rename(tPath, cPath) ){ |
| 13127 sqlite3_snprintf(sizeof(errmsg), errmsg, "rename failed (%d)", errno); |
| 13128 goto end_breaklock; |
| 13129 } |
| 13130 rc = 0; |
| 13131 fprintf(stderr, "broke stale lock on %s\n", cPath); |
| 13132 robust_close(pFile, conchFile->h, __LINE__); |
| 13133 conchFile->h = fd; |
| 13134 conchFile->openFlags = O_RDWR | O_CREAT; |
| 13135 |
| 13136 end_breaklock: |
| 13137 if( rc ){ |
| 13138 if( fd>=0 ){ |
| 13139 osUnlink(tPath); |
| 13140 robust_close(pFile, fd, __LINE__); |
| 13141 } |
| 13142 fprintf(stderr, "failed to break stale lock on %s, %s\n", cPath, errmsg); |
| 13143 } |
| 13144 return rc; |
| 13145 } |
| 13146 |
| 13147 /* Take the requested lock on the conch file and break a stale lock if the |
| 13148 ** host id matches. |
| 13149 */ |
| 13150 static int proxyConchLock(unixFile *pFile, uuid_t myHostID, int lockType){ |
| 13151 proxyLockingContext *pCtx = (proxyLockingContext *)pFile->lockingContext; |
| 13152 unixFile *conchFile = pCtx->conchFile; |
| 13153 int rc = SQLITE_OK; |
| 13154 int nTries = 0; |
| 13155 struct timespec conchModTime; |
| 13156 |
| 13157 memset(&conchModTime, 0, sizeof(conchModTime)); |
| 13158 do { |
| 13159 rc = conchFile->pMethod->xLock((sqlite3_file*)conchFile, lockType); |
| 13160 nTries ++; |
| 13161 if( rc==SQLITE_BUSY ){ |
| 13162 /* If the lock failed (busy): |
| 13163 * 1st try: get the mod time of the conch, wait 0.5s and try again. |
| 13164 * 2nd try: fail if the mod time changed or host id is different, wait |
| 13165 * 10 sec and try again |
| 13166 * 3rd try: break the lock unless the mod time has changed. |
| 13167 */ |
| 13168 struct stat buf; |
| 13169 if( osFstat(conchFile->h, &buf) ){ |
| 13170 storeLastErrno(pFile, errno); |
| 13171 return SQLITE_IOERR_LOCK; |
| 13172 } |
| 13173 |
| 13174 if( nTries==1 ){ |
| 13175 conchModTime = buf.st_mtimespec; |
| 13176 usleep(500000); /* wait 0.5 sec and try the lock again*/ |
| 13177 continue; |
| 13178 } |
| 13179 |
| 13180 assert( nTries>1 ); |
| 13181 if( conchModTime.tv_sec != buf.st_mtimespec.tv_sec || |
| 13182 conchModTime.tv_nsec != buf.st_mtimespec.tv_nsec ){ |
| 13183 return SQLITE_BUSY; |
| 13184 } |
| 13185 |
| 13186 if( nTries==2 ){ |
| 13187 char tBuf[PROXY_MAXCONCHLEN]; |
| 13188 int len = osPread(conchFile->h, tBuf, PROXY_MAXCONCHLEN, 0); |
| 13189 if( len<0 ){ |
| 13190 storeLastErrno(pFile, errno); |
| 13191 return SQLITE_IOERR_LOCK; |
| 13192 } |
| 13193 if( len>PROXY_PATHINDEX && tBuf[0]==(char)PROXY_CONCHVERSION){ |
| 13194 /* don't break the lock if the host id doesn't match */ |
| 13195 if( 0!=memcmp(&tBuf[PROXY_HEADERLEN], myHostID, PROXY_HOSTIDLEN) ){ |
| 13196 return SQLITE_BUSY; |
| 13197 } |
| 13198 }else{ |
| 13199 /* don't break the lock on short read or a version mismatch */ |
| 13200 return SQLITE_BUSY; |
| 13201 } |
| 13202 usleep(10000000); /* wait 10 sec and try the lock again */ |
| 13203 continue; |
| 13204 } |
| 13205 |
| 13206 assert( nTries==3 ); |
| 13207 if( 0==proxyBreakConchLock(pFile, myHostID) ){ |
| 13208 rc = SQLITE_OK; |
| 13209 if( lockType==EXCLUSIVE_LOCK ){ |
| 13210 rc = conchFile->pMethod->xLock((sqlite3_file*)conchFile, SHARED_LOCK); |
| 13211 } |
| 13212 if( !rc ){ |
| 13213 rc = conchFile->pMethod->xLock((sqlite3_file*)conchFile, lockType); |
| 13214 } |
| 13215 } |
| 13216 } |
| 13217 } while( rc==SQLITE_BUSY && nTries<3 ); |
| 13218 |
| 13219 return rc; |
| 13220 } |
| 13221 |
| 13222 /* Takes the conch by taking a shared lock and read the contents conch, if |
| 13223 ** lockPath is non-NULL, the host ID and lock file path must match. A NULL |
| 13224 ** lockPath means that the lockPath in the conch file will be used if the |
| 13225 ** host IDs match, or a new lock path will be generated automatically |
| 13226 ** and written to the conch file. |
| 13227 */ |
| 13228 static int proxyTakeConch(unixFile *pFile){ |
| 13229 proxyLockingContext *pCtx = (proxyLockingContext *)pFile->lockingContext; |
| 13230 |
| 13231 if( pCtx->conchHeld!=0 ){ |
| 13232 return SQLITE_OK; |
| 13233 }else{ |
| 13234 unixFile *conchFile = pCtx->conchFile; |
| 13235 uuid_t myHostID; |
| 13236 int pError = 0; |
| 13237 char readBuf[PROXY_MAXCONCHLEN]; |
| 13238 char lockPath[MAXPATHLEN]; |
| 13239 char *tempLockPath = NULL; |
| 13240 int rc = SQLITE_OK; |
| 13241 int createConch = 0; |
| 13242 int hostIdMatch = 0; |
| 13243 int readLen = 0; |
| 13244 int tryOldLockPath = 0; |
| 13245 int forceNewLockPath = 0; |
| 13246 |
| 13247 OSTRACE(("TAKECONCH %d for %s pid=%d\n", conchFile->h, |
| 13248 (pCtx->lockProxyPath ? pCtx->lockProxyPath : ":auto:"), |
| 13249 osGetpid(0))); |
| 13250 |
| 13251 rc = proxyGetHostID(myHostID, &pError); |
| 13252 if( (rc&0xff)==SQLITE_IOERR ){ |
| 13253 storeLastErrno(pFile, pError); |
| 13254 goto end_takeconch; |
| 13255 } |
| 13256 rc = proxyConchLock(pFile, myHostID, SHARED_LOCK); |
| 13257 if( rc!=SQLITE_OK ){ |
| 13258 goto end_takeconch; |
| 13259 } |
| 13260 /* read the existing conch file */ |
| 13261 readLen = seekAndRead((unixFile*)conchFile, 0, readBuf, PROXY_MAXCONCHLEN); |
| 13262 if( readLen<0 ){ |
| 13263 /* I/O error: lastErrno set by seekAndRead */ |
| 13264 storeLastErrno(pFile, conchFile->lastErrno); |
| 13265 rc = SQLITE_IOERR_READ; |
| 13266 goto end_takeconch; |
| 13267 }else if( readLen<=(PROXY_HEADERLEN+PROXY_HOSTIDLEN) || |
| 13268 readBuf[0]!=(char)PROXY_CONCHVERSION ){ |
| 13269 /* a short read or version format mismatch means we need to create a new |
| 13270 ** conch file. |
| 13271 */ |
| 13272 createConch = 1; |
| 13273 } |
| 13274 /* if the host id matches and the lock path already exists in the conch |
| 13275 ** we'll try to use the path there, if we can't open that path, we'll |
| 13276 ** retry with a new auto-generated path |
| 13277 */ |
| 13278 do { /* in case we need to try again for an :auto: named lock file */ |
| 13279 |
| 13280 if( !createConch && !forceNewLockPath ){ |
| 13281 hostIdMatch = !memcmp(&readBuf[PROXY_HEADERLEN], myHostID, |
| 13282 PROXY_HOSTIDLEN); |
| 13283 /* if the conch has data compare the contents */ |
| 13284 if( !pCtx->lockProxyPath ){ |
| 13285 /* for auto-named local lock file, just check the host ID and we'll |
| 13286 ** use the local lock file path that's already in there |
| 13287 */ |
| 13288 if( hostIdMatch ){ |
| 13289 size_t pathLen = (readLen - PROXY_PATHINDEX); |
| 13290 |
| 13291 if( pathLen>=MAXPATHLEN ){ |
| 13292 pathLen=MAXPATHLEN-1; |
| 13293 } |
| 13294 memcpy(lockPath, &readBuf[PROXY_PATHINDEX], pathLen); |
| 13295 lockPath[pathLen] = 0; |
| 13296 tempLockPath = lockPath; |
| 13297 tryOldLockPath = 1; |
| 13298 /* create a copy of the lock path if the conch is taken */ |
| 13299 goto end_takeconch; |
| 13300 } |
| 13301 }else if( hostIdMatch |
| 13302 && !strncmp(pCtx->lockProxyPath, &readBuf[PROXY_PATHINDEX], |
| 13303 readLen-PROXY_PATHINDEX) |
| 13304 ){ |
| 13305 /* conch host and lock path match */ |
| 13306 goto end_takeconch; |
| 13307 } |
| 13308 } |
| 13309 |
| 13310 /* if the conch isn't writable and doesn't match, we can't take it */ |
| 13311 if( (conchFile->openFlags&O_RDWR) == 0 ){ |
| 13312 rc = SQLITE_BUSY; |
| 13313 goto end_takeconch; |
| 13314 } |
| 13315 |
| 13316 /* either the conch didn't match or we need to create a new one */ |
| 13317 if( !pCtx->lockProxyPath ){ |
| 13318 proxyGetLockPath(pCtx->dbPath, lockPath, MAXPATHLEN); |
| 13319 tempLockPath = lockPath; |
| 13320 /* create a copy of the lock path _only_ if the conch is taken */ |
| 13321 } |
| 13322 |
| 13323 /* update conch with host and path (this will fail if other process |
| 13324 ** has a shared lock already), if the host id matches, use the big |
| 13325 ** stick. |
| 13326 */ |
| 13327 futimes(conchFile->h, NULL); |
| 13328 if( hostIdMatch && !createConch ){ |
| 13329 if( conchFile->pInode && conchFile->pInode->nShared>1 ){ |
| 13330 /* We are trying for an exclusive lock but another thread in this |
| 13331 ** same process is still holding a shared lock. */ |
| 13332 rc = SQLITE_BUSY; |
| 13333 } else { |
| 13334 rc = proxyConchLock(pFile, myHostID, EXCLUSIVE_LOCK); |
| 13335 } |
| 13336 }else{ |
| 13337 rc = proxyConchLock(pFile, myHostID, EXCLUSIVE_LOCK); |
| 13338 } |
| 13339 if( rc==SQLITE_OK ){ |
| 13340 char writeBuffer[PROXY_MAXCONCHLEN]; |
| 13341 int writeSize = 0; |
| 13342 |
| 13343 writeBuffer[0] = (char)PROXY_CONCHVERSION; |
| 13344 memcpy(&writeBuffer[PROXY_HEADERLEN], myHostID, PROXY_HOSTIDLEN); |
| 13345 if( pCtx->lockProxyPath!=NULL ){ |
| 13346 strlcpy(&writeBuffer[PROXY_PATHINDEX], pCtx->lockProxyPath, |
| 13347 MAXPATHLEN); |
| 13348 }else{ |
| 13349 strlcpy(&writeBuffer[PROXY_PATHINDEX], tempLockPath, MAXPATHLEN); |
| 13350 } |
| 13351 writeSize = PROXY_PATHINDEX + strlen(&writeBuffer[PROXY_PATHINDEX]); |
| 13352 robust_ftruncate(conchFile->h, writeSize); |
| 13353 rc = unixWrite((sqlite3_file *)conchFile, writeBuffer, writeSize, 0); |
| 13354 fsync(conchFile->h); |
| 13355 /* If we created a new conch file (not just updated the contents of a |
| 13356 ** valid conch file), try to match the permissions of the database |
| 13357 */ |
| 13358 if( rc==SQLITE_OK && createConch ){ |
| 13359 struct stat buf; |
| 13360 int err = osFstat(pFile->h, &buf); |
| 13361 if( err==0 ){ |
| 13362 mode_t cmode = buf.st_mode&(S_IRUSR|S_IWUSR | S_IRGRP|S_IWGRP | |
| 13363 S_IROTH|S_IWOTH); |
| 13364 /* try to match the database file R/W permissions, ignore failure */ |
| 13365 #ifndef SQLITE_PROXY_DEBUG |
| 13366 osFchmod(conchFile->h, cmode); |
| 13367 #else |
| 13368 do{ |
| 13369 rc = osFchmod(conchFile->h, cmode); |
| 13370 }while( rc==(-1) && errno==EINTR ); |
| 13371 if( rc!=0 ){ |
| 13372 int code = errno; |
| 13373 fprintf(stderr, "fchmod %o FAILED with %d %s\n", |
| 13374 cmode, code, strerror(code)); |
| 13375 } else { |
| 13376 fprintf(stderr, "fchmod %o SUCCEDED\n",cmode); |
| 13377 } |
| 13378 }else{ |
| 13379 int code = errno; |
| 13380 fprintf(stderr, "STAT FAILED[%d] with %d %s\n", |
| 13381 err, code, strerror(code)); |
| 13382 #endif |
| 13383 } |
| 13384 } |
| 13385 } |
| 13386 conchFile->pMethod->xUnlock((sqlite3_file*)conchFile, SHARED_LOCK); |
| 13387 |
| 13388 end_takeconch: |
| 13389 OSTRACE(("TRANSPROXY: CLOSE %d\n", pFile->h)); |
| 13390 if( rc==SQLITE_OK && pFile->openFlags ){ |
| 13391 int fd; |
| 13392 if( pFile->h>=0 ){ |
| 13393 robust_close(pFile, pFile->h, __LINE__); |
| 13394 } |
| 13395 pFile->h = -1; |
| 13396 fd = robust_open(pCtx->dbPath, pFile->openFlags, 0); |
| 13397 OSTRACE(("TRANSPROXY: OPEN %d\n", fd)); |
| 13398 if( fd>=0 ){ |
| 13399 pFile->h = fd; |
| 13400 }else{ |
| 13401 rc=SQLITE_CANTOPEN_BKPT; /* SQLITE_BUSY? proxyTakeConch called |
| 13402 during locking */ |
| 13403 } |
| 13404 } |
| 13405 if( rc==SQLITE_OK && !pCtx->lockProxy ){ |
| 13406 char *path = tempLockPath ? tempLockPath : pCtx->lockProxyPath; |
| 13407 rc = proxyCreateUnixFile(path, &pCtx->lockProxy, 1); |
| 13408 if( rc!=SQLITE_OK && rc!=SQLITE_NOMEM && tryOldLockPath ){ |
| 13409 /* we couldn't create the proxy lock file with the old lock file path |
| 13410 ** so try again via auto-naming |
| 13411 */ |
| 13412 forceNewLockPath = 1; |
| 13413 tryOldLockPath = 0; |
| 13414 continue; /* go back to the do {} while start point, try again */ |
| 13415 } |
| 13416 } |
| 13417 if( rc==SQLITE_OK ){ |
| 13418 /* Need to make a copy of path if we extracted the value |
| 13419 ** from the conch file or the path was allocated on the stack |
| 13420 */ |
| 13421 if( tempLockPath ){ |
| 13422 pCtx->lockProxyPath = sqlite3DbStrDup(0, tempLockPath); |
| 13423 if( !pCtx->lockProxyPath ){ |
| 13424 rc = SQLITE_NOMEM; |
| 13425 } |
| 13426 } |
| 13427 } |
| 13428 if( rc==SQLITE_OK ){ |
| 13429 pCtx->conchHeld = 1; |
| 13430 |
| 13431 if( pCtx->lockProxy->pMethod == &afpIoMethods ){ |
| 13432 afpLockingContext *afpCtx; |
| 13433 afpCtx = (afpLockingContext *)pCtx->lockProxy->lockingContext; |
| 13434 afpCtx->dbPath = pCtx->lockProxyPath; |
| 13435 } |
| 13436 } else { |
| 13437 conchFile->pMethod->xUnlock((sqlite3_file*)conchFile, NO_LOCK); |
| 13438 } |
| 13439 OSTRACE(("TAKECONCH %d %s\n", conchFile->h, |
| 13440 rc==SQLITE_OK?"ok":"failed")); |
| 13441 return rc; |
| 13442 } while (1); /* in case we need to retry the :auto: lock file - |
| 13443 ** we should never get here except via the 'continue' call. */ |
| 13444 } |
| 13445 } |
| 13446 |
| 13447 /* |
| 13448 ** If pFile holds a lock on a conch file, then release that lock. |
| 13449 */ |
| 13450 static int proxyReleaseConch(unixFile *pFile){ |
| 13451 int rc = SQLITE_OK; /* Subroutine return code */ |
| 13452 proxyLockingContext *pCtx; /* The locking context for the proxy lock */ |
| 13453 unixFile *conchFile; /* Name of the conch file */ |
| 13454 |
| 13455 pCtx = (proxyLockingContext *)pFile->lockingContext; |
| 13456 conchFile = pCtx->conchFile; |
| 13457 OSTRACE(("RELEASECONCH %d for %s pid=%d\n", conchFile->h, |
| 13458 (pCtx->lockProxyPath ? pCtx->lockProxyPath : ":auto:"), |
| 13459 osGetpid(0))); |
| 13460 if( pCtx->conchHeld>0 ){ |
| 13461 rc = conchFile->pMethod->xUnlock((sqlite3_file*)conchFile, NO_LOCK); |
| 13462 } |
| 13463 pCtx->conchHeld = 0; |
| 13464 OSTRACE(("RELEASECONCH %d %s\n", conchFile->h, |
| 13465 (rc==SQLITE_OK ? "ok" : "failed"))); |
| 13466 return rc; |
| 13467 } |
| 13468 |
| 13469 /* |
| 13470 ** Given the name of a database file, compute the name of its conch file. |
| 13471 ** Store the conch filename in memory obtained from sqlite3_malloc64(). |
| 13472 ** Make *pConchPath point to the new name. Return SQLITE_OK on success |
| 13473 ** or SQLITE_NOMEM if unable to obtain memory. |
| 13474 ** |
| 13475 ** The caller is responsible for ensuring that the allocated memory |
| 13476 ** space is eventually freed. |
| 13477 ** |
| 13478 ** *pConchPath is set to NULL if a memory allocation error occurs. |
| 13479 */ |
| 13480 static int proxyCreateConchPathname(char *dbPath, char **pConchPath){ |
| 13481 int i; /* Loop counter */ |
| 13482 int len = (int)strlen(dbPath); /* Length of database filename - dbPath */ |
| 13483 char *conchPath; /* buffer in which to construct conch name */ |
| 13484 |
| 13485 /* Allocate space for the conch filename and initialize the name to |
| 13486 ** the name of the original database file. */ |
| 13487 *pConchPath = conchPath = (char *)sqlite3_malloc64(len + 8); |
| 13488 if( conchPath==0 ){ |
| 13489 return SQLITE_NOMEM; |
| 13490 } |
| 13491 memcpy(conchPath, dbPath, len+1); |
| 13492 |
| 13493 /* now insert a "." before the last / character */ |
| 13494 for( i=(len-1); i>=0; i-- ){ |
| 13495 if( conchPath[i]=='/' ){ |
| 13496 i++; |
| 13497 break; |
| 13498 } |
| 13499 } |
| 13500 conchPath[i]='.'; |
| 13501 while ( i<len ){ |
| 13502 conchPath[i+1]=dbPath[i]; |
| 13503 i++; |
| 13504 } |
| 13505 |
| 13506 /* append the "-conch" suffix to the file */ |
| 13507 memcpy(&conchPath[i+1], "-conch", 7); |
| 13508 assert( (int)strlen(conchPath) == len+7 ); |
| 13509 |
| 13510 return SQLITE_OK; |
| 13511 } |
| 13512 |
| 13513 |
| 13514 /* Takes a fully configured proxy locking-style unix file and switches |
| 13515 ** the local lock file path |
| 13516 */ |
| 13517 static int switchLockProxyPath(unixFile *pFile, const char *path) { |
| 13518 proxyLockingContext *pCtx = (proxyLockingContext*)pFile->lockingContext; |
| 13519 char *oldPath = pCtx->lockProxyPath; |
| 13520 int rc = SQLITE_OK; |
| 13521 |
| 13522 if( pFile->eFileLock!=NO_LOCK ){ |
| 13523 return SQLITE_BUSY; |
| 13524 } |
| 13525 |
| 13526 /* nothing to do if the path is NULL, :auto: or matches the existing path */ |
| 13527 if( !path || path[0]=='\0' || !strcmp(path, ":auto:") || |
| 13528 (oldPath && !strncmp(oldPath, path, MAXPATHLEN)) ){ |
| 13529 return SQLITE_OK; |
| 13530 }else{ |
| 13531 unixFile *lockProxy = pCtx->lockProxy; |
| 13532 pCtx->lockProxy=NULL; |
| 13533 pCtx->conchHeld = 0; |
| 13534 if( lockProxy!=NULL ){ |
| 13535 rc=lockProxy->pMethod->xClose((sqlite3_file *)lockProxy); |
| 13536 if( rc ) return rc; |
| 13537 sqlite3_free(lockProxy); |
| 13538 } |
| 13539 sqlite3_free(oldPath); |
| 13540 pCtx->lockProxyPath = sqlite3DbStrDup(0, path); |
| 13541 } |
| 13542 |
| 13543 return rc; |
| 13544 } |
| 13545 |
| 13546 /* |
| 13547 ** pFile is a file that has been opened by a prior xOpen call. dbPath |
| 13548 ** is a string buffer at least MAXPATHLEN+1 characters in size. |
| 13549 ** |
| 13550 ** This routine find the filename associated with pFile and writes it |
| 13551 ** int dbPath. |
| 13552 */ |
| 13553 static int proxyGetDbPathForUnixFile(unixFile *pFile, char *dbPath){ |
| 13554 #if defined(__APPLE__) |
| 13555 if( pFile->pMethod == &afpIoMethods ){ |
| 13556 /* afp style keeps a reference to the db path in the filePath field |
| 13557 ** of the struct */ |
| 13558 assert( (int)strlen((char*)pFile->lockingContext)<=MAXPATHLEN ); |
| 13559 strlcpy(dbPath, ((afpLockingContext *)pFile->lockingContext)->dbPath, |
| 13560 MAXPATHLEN); |
| 13561 } else |
| 13562 #endif |
| 13563 if( pFile->pMethod == &dotlockIoMethods ){ |
| 13564 /* dot lock style uses the locking context to store the dot lock |
| 13565 ** file path */ |
| 13566 int len = strlen((char *)pFile->lockingContext) - strlen(DOTLOCK_SUFFIX); |
| 13567 memcpy(dbPath, (char *)pFile->lockingContext, len + 1); |
| 13568 }else{ |
| 13569 /* all other styles use the locking context to store the db file path */ |
| 13570 assert( strlen((char*)pFile->lockingContext)<=MAXPATHLEN ); |
| 13571 strlcpy(dbPath, (char *)pFile->lockingContext, MAXPATHLEN); |
| 13572 } |
| 13573 return SQLITE_OK; |
| 13574 } |
| 13575 |
| 13576 /* |
| 13577 ** Takes an already filled in unix file and alters it so all file locking |
| 13578 ** will be performed on the local proxy lock file. The following fields |
| 13579 ** are preserved in the locking context so that they can be restored and |
| 13580 ** the unix structure properly cleaned up at close time: |
| 13581 ** ->lockingContext |
| 13582 ** ->pMethod |
| 13583 */ |
| 13584 static int proxyTransformUnixFile(unixFile *pFile, const char *path) { |
| 13585 proxyLockingContext *pCtx; |
| 13586 char dbPath[MAXPATHLEN+1]; /* Name of the database file */ |
| 13587 char *lockPath=NULL; |
| 13588 int rc = SQLITE_OK; |
| 13589 |
| 13590 if( pFile->eFileLock!=NO_LOCK ){ |
| 13591 return SQLITE_BUSY; |
| 13592 } |
| 13593 proxyGetDbPathForUnixFile(pFile, dbPath); |
| 13594 if( !path || path[0]=='\0' || !strcmp(path, ":auto:") ){ |
| 13595 lockPath=NULL; |
| 13596 }else{ |
| 13597 lockPath=(char *)path; |
| 13598 } |
| 13599 |
| 13600 OSTRACE(("TRANSPROXY %d for %s pid=%d\n", pFile->h, |
| 13601 (lockPath ? lockPath : ":auto:"), osGetpid(0))); |
| 13602 |
| 13603 pCtx = sqlite3_malloc64( sizeof(*pCtx) ); |
| 13604 if( pCtx==0 ){ |
| 13605 return SQLITE_NOMEM; |
| 13606 } |
| 13607 memset(pCtx, 0, sizeof(*pCtx)); |
| 13608 |
| 13609 rc = proxyCreateConchPathname(dbPath, &pCtx->conchFilePath); |
| 13610 if( rc==SQLITE_OK ){ |
| 13611 rc = proxyCreateUnixFile(pCtx->conchFilePath, &pCtx->conchFile, 0); |
| 13612 if( rc==SQLITE_CANTOPEN && ((pFile->openFlags&O_RDWR) == 0) ){ |
| 13613 /* if (a) the open flags are not O_RDWR, (b) the conch isn't there, and |
| 13614 ** (c) the file system is read-only, then enable no-locking access. |
| 13615 ** Ugh, since O_RDONLY==0x0000 we test for !O_RDWR since unixOpen asserts |
| 13616 ** that openFlags will have only one of O_RDONLY or O_RDWR. |
| 13617 */ |
| 13618 struct statfs fsInfo; |
| 13619 struct stat conchInfo; |
| 13620 int goLockless = 0; |
| 13621 |
| 13622 if( osStat(pCtx->conchFilePath, &conchInfo) == -1 ) { |
| 13623 int err = errno; |
| 13624 if( (err==ENOENT) && (statfs(dbPath, &fsInfo) != -1) ){ |
| 13625 goLockless = (fsInfo.f_flags&MNT_RDONLY) == MNT_RDONLY; |
| 13626 } |
| 13627 } |
| 13628 if( goLockless ){ |
| 13629 pCtx->conchHeld = -1; /* read only FS/ lockless */ |
| 13630 rc = SQLITE_OK; |
| 13631 } |
| 13632 } |
| 13633 } |
| 13634 if( rc==SQLITE_OK && lockPath ){ |
| 13635 pCtx->lockProxyPath = sqlite3DbStrDup(0, lockPath); |
| 13636 } |
| 13637 |
| 13638 if( rc==SQLITE_OK ){ |
| 13639 pCtx->dbPath = sqlite3DbStrDup(0, dbPath); |
| 13640 if( pCtx->dbPath==NULL ){ |
| 13641 rc = SQLITE_NOMEM; |
| 13642 } |
| 13643 } |
| 13644 if( rc==SQLITE_OK ){ |
| 13645 /* all memory is allocated, proxys are created and assigned, |
| 13646 ** switch the locking context and pMethod then return. |
| 13647 */ |
| 13648 pCtx->oldLockingContext = pFile->lockingContext; |
| 13649 pFile->lockingContext = pCtx; |
| 13650 pCtx->pOldMethod = pFile->pMethod; |
| 13651 pFile->pMethod = &proxyIoMethods; |
| 13652 }else{ |
| 13653 if( pCtx->conchFile ){ |
| 13654 pCtx->conchFile->pMethod->xClose((sqlite3_file *)pCtx->conchFile); |
| 13655 sqlite3_free(pCtx->conchFile); |
| 13656 } |
| 13657 sqlite3DbFree(0, pCtx->lockProxyPath); |
| 13658 sqlite3_free(pCtx->conchFilePath); |
| 13659 sqlite3_free(pCtx); |
| 13660 } |
| 13661 OSTRACE(("TRANSPROXY %d %s\n", pFile->h, |
| 13662 (rc==SQLITE_OK ? "ok" : "failed"))); |
| 13663 return rc; |
| 13664 } |
| 13665 |
| 13666 |
| 13667 /* |
| 13668 ** This routine handles sqlite3_file_control() calls that are specific |
| 13669 ** to proxy locking. |
| 13670 */ |
| 13671 static int proxyFileControl(sqlite3_file *id, int op, void *pArg){ |
| 13672 switch( op ){ |
| 13673 case SQLITE_FCNTL_GET_LOCKPROXYFILE: { |
| 13674 unixFile *pFile = (unixFile*)id; |
| 13675 if( pFile->pMethod == &proxyIoMethods ){ |
| 13676 proxyLockingContext *pCtx = (proxyLockingContext*)pFile->lockingContext; |
| 13677 proxyTakeConch(pFile); |
| 13678 if( pCtx->lockProxyPath ){ |
| 13679 *(const char **)pArg = pCtx->lockProxyPath; |
| 13680 }else{ |
| 13681 *(const char **)pArg = ":auto: (not held)"; |
| 13682 } |
| 13683 } else { |
| 13684 *(const char **)pArg = NULL; |
| 13685 } |
| 13686 return SQLITE_OK; |
| 13687 } |
| 13688 case SQLITE_FCNTL_SET_LOCKPROXYFILE: { |
| 13689 unixFile *pFile = (unixFile*)id; |
| 13690 int rc = SQLITE_OK; |
| 13691 int isProxyStyle = (pFile->pMethod == &proxyIoMethods); |
| 13692 if( pArg==NULL || (const char *)pArg==0 ){ |
| 13693 if( isProxyStyle ){ |
| 13694 /* turn off proxy locking - not supported. If support is added for |
| 13695 ** switching proxy locking mode off then it will need to fail if |
| 13696 ** the journal mode is WAL mode. |
| 13697 */ |
| 13698 rc = SQLITE_ERROR /*SQLITE_PROTOCOL? SQLITE_MISUSE?*/; |
| 13699 }else{ |
| 13700 /* turn off proxy locking - already off - NOOP */ |
| 13701 rc = SQLITE_OK; |
| 13702 } |
| 13703 }else{ |
| 13704 const char *proxyPath = (const char *)pArg; |
| 13705 if( isProxyStyle ){ |
| 13706 proxyLockingContext *pCtx = |
| 13707 (proxyLockingContext*)pFile->lockingContext; |
| 13708 if( !strcmp(pArg, ":auto:") |
| 13709 || (pCtx->lockProxyPath && |
| 13710 !strncmp(pCtx->lockProxyPath, proxyPath, MAXPATHLEN)) |
| 13711 ){ |
| 13712 rc = SQLITE_OK; |
| 13713 }else{ |
| 13714 rc = switchLockProxyPath(pFile, proxyPath); |
| 13715 } |
| 13716 }else{ |
| 13717 /* turn on proxy file locking */ |
| 13718 rc = proxyTransformUnixFile(pFile, proxyPath); |
| 13719 } |
| 13720 } |
| 13721 return rc; |
| 13722 } |
| 13723 default: { |
| 13724 assert( 0 ); /* The call assures that only valid opcodes are sent */ |
| 13725 } |
| 13726 } |
| 13727 /*NOTREACHED*/ |
| 13728 return SQLITE_ERROR; |
| 13729 } |
| 13730 |
| 13731 /* |
| 13732 ** Within this division (the proxying locking implementation) the procedures |
| 13733 ** above this point are all utilities. The lock-related methods of the |
| 13734 ** proxy-locking sqlite3_io_method object follow. |
| 13735 */ |
| 13736 |
| 13737 |
| 13738 /* |
| 13739 ** This routine checks if there is a RESERVED lock held on the specified |
| 13740 ** file by this or any other process. If such a lock is held, set *pResOut |
| 13741 ** to a non-zero value otherwise *pResOut is set to zero. The return value |
| 13742 ** is set to SQLITE_OK unless an I/O error occurs during lock checking. |
| 13743 */ |
| 13744 static int proxyCheckReservedLock(sqlite3_file *id, int *pResOut) { |
| 13745 unixFile *pFile = (unixFile*)id; |
| 13746 int rc = proxyTakeConch(pFile); |
| 13747 if( rc==SQLITE_OK ){ |
| 13748 proxyLockingContext *pCtx = (proxyLockingContext *)pFile->lockingContext; |
| 13749 if( pCtx->conchHeld>0 ){ |
| 13750 unixFile *proxy = pCtx->lockProxy; |
| 13751 return proxy->pMethod->xCheckReservedLock((sqlite3_file*)proxy, pResOut); |
| 13752 }else{ /* conchHeld < 0 is lockless */ |
| 13753 pResOut=0; |
| 13754 } |
| 13755 } |
| 13756 return rc; |
| 13757 } |
| 13758 |
| 13759 /* |
| 13760 ** Lock the file with the lock specified by parameter eFileLock - one |
| 13761 ** of the following: |
| 13762 ** |
| 13763 ** (1) SHARED_LOCK |
| 13764 ** (2) RESERVED_LOCK |
| 13765 ** (3) PENDING_LOCK |
| 13766 ** (4) EXCLUSIVE_LOCK |
| 13767 ** |
| 13768 ** Sometimes when requesting one lock state, additional lock states |
| 13769 ** are inserted in between. The locking might fail on one of the later |
| 13770 ** transitions leaving the lock state different from what it started but |
| 13771 ** still short of its goal. The following chart shows the allowed |
| 13772 ** transitions and the inserted intermediate states: |
| 13773 ** |
| 13774 ** UNLOCKED -> SHARED |
| 13775 ** SHARED -> RESERVED |
| 13776 ** SHARED -> (PENDING) -> EXCLUSIVE |
| 13777 ** RESERVED -> (PENDING) -> EXCLUSIVE |
| 13778 ** PENDING -> EXCLUSIVE |
| 13779 ** |
| 13780 ** This routine will only increase a lock. Use the sqlite3OsUnlock() |
| 13781 ** routine to lower a locking level. |
| 13782 */ |
| 13783 static int proxyLock(sqlite3_file *id, int eFileLock) { |
| 13784 unixFile *pFile = (unixFile*)id; |
| 13785 int rc = proxyTakeConch(pFile); |
| 13786 if( rc==SQLITE_OK ){ |
| 13787 proxyLockingContext *pCtx = (proxyLockingContext *)pFile->lockingContext; |
| 13788 if( pCtx->conchHeld>0 ){ |
| 13789 unixFile *proxy = pCtx->lockProxy; |
| 13790 rc = proxy->pMethod->xLock((sqlite3_file*)proxy, eFileLock); |
| 13791 pFile->eFileLock = proxy->eFileLock; |
| 13792 }else{ |
| 13793 /* conchHeld < 0 is lockless */ |
| 13794 } |
| 13795 } |
| 13796 return rc; |
| 13797 } |
| 13798 |
| 13799 |
| 13800 /* |
| 13801 ** Lower the locking level on file descriptor pFile to eFileLock. eFileLock |
| 13802 ** must be either NO_LOCK or SHARED_LOCK. |
| 13803 ** |
| 13804 ** If the locking level of the file descriptor is already at or below |
| 13805 ** the requested locking level, this routine is a no-op. |
| 13806 */ |
| 13807 static int proxyUnlock(sqlite3_file *id, int eFileLock) { |
| 13808 unixFile *pFile = (unixFile*)id; |
| 13809 int rc = proxyTakeConch(pFile); |
| 13810 if( rc==SQLITE_OK ){ |
| 13811 proxyLockingContext *pCtx = (proxyLockingContext *)pFile->lockingContext; |
| 13812 if( pCtx->conchHeld>0 ){ |
| 13813 unixFile *proxy = pCtx->lockProxy; |
| 13814 rc = proxy->pMethod->xUnlock((sqlite3_file*)proxy, eFileLock); |
| 13815 pFile->eFileLock = proxy->eFileLock; |
| 13816 }else{ |
| 13817 /* conchHeld < 0 is lockless */ |
| 13818 } |
| 13819 } |
| 13820 return rc; |
| 13821 } |
| 13822 |
| 13823 /* |
| 13824 ** Close a file that uses proxy locks. |
| 13825 */ |
| 13826 static int proxyClose(sqlite3_file *id) { |
| 13827 if( ALWAYS(id) ){ |
| 13828 unixFile *pFile = (unixFile*)id; |
| 13829 proxyLockingContext *pCtx = (proxyLockingContext *)pFile->lockingContext; |
| 13830 unixFile *lockProxy = pCtx->lockProxy; |
| 13831 unixFile *conchFile = pCtx->conchFile; |
| 13832 int rc = SQLITE_OK; |
| 13833 |
| 13834 if( lockProxy ){ |
| 13835 rc = lockProxy->pMethod->xUnlock((sqlite3_file*)lockProxy, NO_LOCK); |
| 13836 if( rc ) return rc; |
| 13837 rc = lockProxy->pMethod->xClose((sqlite3_file*)lockProxy); |
| 13838 if( rc ) return rc; |
| 13839 sqlite3_free(lockProxy); |
| 13840 pCtx->lockProxy = 0; |
| 13841 } |
| 13842 if( conchFile ){ |
| 13843 if( pCtx->conchHeld ){ |
| 13844 rc = proxyReleaseConch(pFile); |
| 13845 if( rc ) return rc; |
| 13846 } |
| 13847 rc = conchFile->pMethod->xClose((sqlite3_file*)conchFile); |
| 13848 if( rc ) return rc; |
| 13849 sqlite3_free(conchFile); |
| 13850 } |
| 13851 sqlite3DbFree(0, pCtx->lockProxyPath); |
| 13852 sqlite3_free(pCtx->conchFilePath); |
| 13853 sqlite3DbFree(0, pCtx->dbPath); |
| 13854 /* restore the original locking context and pMethod then close it */ |
| 13855 pFile->lockingContext = pCtx->oldLockingContext; |
| 13856 pFile->pMethod = pCtx->pOldMethod; |
| 13857 sqlite3_free(pCtx); |
| 13858 return pFile->pMethod->xClose(id); |
| 13859 } |
| 13860 return SQLITE_OK; |
| 13861 } |
| 13862 |
| 13863 |
| 13864 |
| 13865 #endif /* defined(__APPLE__) && SQLITE_ENABLE_LOCKING_STYLE */ |
| 13866 /* |
| 13867 ** The proxy locking style is intended for use with AFP filesystems. |
| 13868 ** And since AFP is only supported on MacOSX, the proxy locking is also |
| 13869 ** restricted to MacOSX. |
| 13870 ** |
| 13871 ** |
| 13872 ******************* End of the proxy lock implementation ********************** |
| 13873 ******************************************************************************/ |
| 13874 |
| 13875 /* |
| 13876 ** Initialize the operating system interface. |
| 13877 ** |
| 13878 ** This routine registers all VFS implementations for unix-like operating |
| 13879 ** systems. This routine, and the sqlite3_os_end() routine that follows, |
| 13880 ** should be the only routines in this file that are visible from other |
| 13881 ** files. |
| 13882 ** |
| 13883 ** This routine is called once during SQLite initialization and by a |
| 13884 ** single thread. The memory allocation and mutex subsystems have not |
| 13885 ** necessarily been initialized when this routine is called, and so they |
| 13886 ** should not be used. |
| 13887 */ |
| 13888 SQLITE_API int SQLITE_STDCALL sqlite3_os_init(void){ |
| 13889 /* |
| 13890 ** The following macro defines an initializer for an sqlite3_vfs object. |
| 13891 ** The name of the VFS is NAME. The pAppData is a pointer to a pointer |
| 13892 ** to the "finder" function. (pAppData is a pointer to a pointer because |
| 13893 ** silly C90 rules prohibit a void* from being cast to a function pointer |
| 13894 ** and so we have to go through the intermediate pointer to avoid problems |
| 13895 ** when compiling with -pedantic-errors on GCC.) |
| 13896 ** |
| 13897 ** The FINDER parameter to this macro is the name of the pointer to the |
| 13898 ** finder-function. The finder-function returns a pointer to the |
| 13899 ** sqlite_io_methods object that implements the desired locking |
| 13900 ** behaviors. See the division above that contains the IOMETHODS |
| 13901 ** macro for addition information on finder-functions. |
| 13902 ** |
| 13903 ** Most finders simply return a pointer to a fixed sqlite3_io_methods |
| 13904 ** object. But the "autolockIoFinder" available on MacOSX does a little |
| 13905 ** more than that; it looks at the filesystem type that hosts the |
| 13906 ** database file and tries to choose an locking method appropriate for |
| 13907 ** that filesystem time. |
| 13908 */ |
| 13909 #define UNIXVFS(VFSNAME, FINDER) { \ |
| 13910 3, /* iVersion */ \ |
| 13911 sizeof(unixFile), /* szOsFile */ \ |
| 13912 MAX_PATHNAME, /* mxPathname */ \ |
| 13913 0, /* pNext */ \ |
| 13914 VFSNAME, /* zName */ \ |
| 13915 (void*)&FINDER, /* pAppData */ \ |
| 13916 unixOpen, /* xOpen */ \ |
| 13917 unixDelete, /* xDelete */ \ |
| 13918 unixAccess, /* xAccess */ \ |
| 13919 unixFullPathname, /* xFullPathname */ \ |
| 13920 unixDlOpen, /* xDlOpen */ \ |
| 13921 unixDlError, /* xDlError */ \ |
| 13922 unixDlSym, /* xDlSym */ \ |
| 13923 unixDlClose, /* xDlClose */ \ |
| 13924 unixRandomness, /* xRandomness */ \ |
| 13925 unixSleep, /* xSleep */ \ |
| 13926 unixCurrentTime, /* xCurrentTime */ \ |
| 13927 unixGetLastError, /* xGetLastError */ \ |
| 13928 unixCurrentTimeInt64, /* xCurrentTimeInt64 */ \ |
| 13929 unixSetSystemCall, /* xSetSystemCall */ \ |
| 13930 unixGetSystemCall, /* xGetSystemCall */ \ |
| 13931 unixNextSystemCall, /* xNextSystemCall */ \ |
| 13932 } |
| 13933 |
| 13934 /* |
| 13935 ** All default VFSes for unix are contained in the following array. |
| 13936 ** |
| 13937 ** Note that the sqlite3_vfs.pNext field of the VFS object is modified |
| 13938 ** by the SQLite core when the VFS is registered. So the following |
| 13939 ** array cannot be const. |
| 13940 */ |
| 13941 static sqlite3_vfs aVfs[] = { |
| 13942 #if SQLITE_ENABLE_LOCKING_STYLE && defined(__APPLE__) |
| 13943 UNIXVFS("unix", autolockIoFinder ), |
| 13944 #elif OS_VXWORKS |
| 13945 UNIXVFS("unix", vxworksIoFinder ), |
| 13946 #else |
| 13947 UNIXVFS("unix", posixIoFinder ), |
| 13948 #endif |
| 13949 UNIXVFS("unix-none", nolockIoFinder ), |
| 13950 UNIXVFS("unix-dotfile", dotlockIoFinder ), |
| 13951 UNIXVFS("unix-excl", posixIoFinder ), |
| 13952 #if OS_VXWORKS |
| 13953 UNIXVFS("unix-namedsem", semIoFinder ), |
| 13954 #endif |
| 13955 #if SQLITE_ENABLE_LOCKING_STYLE || OS_VXWORKS |
| 13956 UNIXVFS("unix-posix", posixIoFinder ), |
| 13957 #endif |
| 13958 #if SQLITE_ENABLE_LOCKING_STYLE |
| 13959 UNIXVFS("unix-flock", flockIoFinder ), |
| 13960 #endif |
| 13961 #if SQLITE_ENABLE_LOCKING_STYLE && defined(__APPLE__) |
| 13962 UNIXVFS("unix-afp", afpIoFinder ), |
| 13963 UNIXVFS("unix-nfs", nfsIoFinder ), |
| 13964 UNIXVFS("unix-proxy", proxyIoFinder ), |
| 13965 #endif |
| 13966 }; |
| 13967 unsigned int i; /* Loop counter */ |
| 13968 |
| 13969 /* Double-check that the aSyscall[] array has been constructed |
| 13970 ** correctly. See ticket [bb3a86e890c8e96ab] */ |
| 13971 assert( ArraySize(aSyscall)==27 ); |
| 13972 |
| 13973 /* Register all VFSes defined in the aVfs[] array */ |
| 13974 for(i=0; i<(sizeof(aVfs)/sizeof(sqlite3_vfs)); i++){ |
| 13975 sqlite3_vfs_register(&aVfs[i], i==0); |
| 13976 } |
| 13977 return SQLITE_OK; |
| 13978 } |
| 13979 |
| 13980 /* |
| 13981 ** Shutdown the operating system interface. |
| 13982 ** |
| 13983 ** Some operating systems might need to do some cleanup in this routine, |
| 13984 ** to release dynamically allocated objects. But not on unix. |
| 13985 ** This routine is a no-op for unix. |
| 13986 */ |
| 13987 SQLITE_API int SQLITE_STDCALL sqlite3_os_end(void){ |
| 13988 return SQLITE_OK; |
| 13989 } |
| 13990 |
| 13991 #endif /* SQLITE_OS_UNIX */ |
| 13992 |
| 13993 /************** End of os_unix.c *********************************************/ |
| 13994 /************** Begin file os_win.c ******************************************/ |
| 13995 /* |
| 13996 ** 2004 May 22 |
| 13997 ** |
| 13998 ** The author disclaims copyright to this source code. In place of |
| 13999 ** a legal notice, here is a blessing: |
| 14000 ** |
| 14001 ** May you do good and not evil. |
| 14002 ** May you find forgiveness for yourself and forgive others. |
| 14003 ** May you share freely, never taking more than you give. |
| 14004 ** |
| 14005 ****************************************************************************** |
| 14006 ** |
| 14007 ** This file contains code that is specific to Windows. |
| 14008 */ |
| 14009 /* #include "sqliteInt.h" */ |
| 14010 #if SQLITE_OS_WIN /* This file is used for Windows only */ |
| 14011 |
| 14012 /* |
| 14013 ** Include code that is common to all os_*.c files |
| 14014 */ |
| 14015 /************** Include os_common.h in the middle of os_win.c ****************/ |
| 14016 /************** Begin file os_common.h ***************************************/ |
| 14017 /* |
| 14018 ** 2004 May 22 |
| 14019 ** |
| 14020 ** The author disclaims copyright to this source code. In place of |
| 14021 ** a legal notice, here is a blessing: |
| 14022 ** |
| 14023 ** May you do good and not evil. |
| 14024 ** May you find forgiveness for yourself and forgive others. |
| 14025 ** May you share freely, never taking more than you give. |
| 14026 ** |
| 14027 ****************************************************************************** |
| 14028 ** |
| 14029 ** This file contains macros and a little bit of code that is common to |
| 14030 ** all of the platform-specific files (os_*.c) and is #included into those |
| 14031 ** files. |
| 14032 ** |
| 14033 ** This file should be #included by the os_*.c files only. It is not a |
| 14034 ** general purpose header file. |
| 14035 */ |
| 14036 #ifndef _OS_COMMON_H_ |
| 14037 #define _OS_COMMON_H_ |
| 14038 |
| 14039 /* |
| 14040 ** At least two bugs have slipped in because we changed the MEMORY_DEBUG |
| 14041 ** macro to SQLITE_DEBUG and some older makefiles have not yet made the |
| 14042 ** switch. The following code should catch this problem at compile-time. |
| 14043 */ |
| 14044 #ifdef MEMORY_DEBUG |
| 14045 # error "The MEMORY_DEBUG macro is obsolete. Use SQLITE_DEBUG instead." |
| 14046 #endif |
| 14047 |
| 14048 /* |
| 14049 ** Macros for performance tracing. Normally turned off. Only works |
| 14050 ** on i486 hardware. |
| 14051 */ |
| 14052 #ifdef SQLITE_PERFORMANCE_TRACE |
| 14053 |
| 14054 /* |
| 14055 ** hwtime.h contains inline assembler code for implementing |
| 14056 ** high-performance timing routines. |
| 14057 */ |
| 14058 /************** Include hwtime.h in the middle of os_common.h ****************/ |
| 14059 /************** Begin file hwtime.h ******************************************/ |
| 14060 /* |
| 14061 ** 2008 May 27 |
| 14062 ** |
| 14063 ** The author disclaims copyright to this source code. In place of |
| 14064 ** a legal notice, here is a blessing: |
| 14065 ** |
| 14066 ** May you do good and not evil. |
| 14067 ** May you find forgiveness for yourself and forgive others. |
| 14068 ** May you share freely, never taking more than you give. |
| 14069 ** |
| 14070 ****************************************************************************** |
| 14071 ** |
| 14072 ** This file contains inline asm code for retrieving "high-performance" |
| 14073 ** counters for x86 class CPUs. |
| 14074 */ |
| 14075 #ifndef _HWTIME_H_ |
| 14076 #define _HWTIME_H_ |
| 14077 |
| 14078 /* |
| 14079 ** The following routine only works on pentium-class (or newer) processors. |
| 14080 ** It uses the RDTSC opcode to read the cycle count value out of the |
| 14081 ** processor and returns that value. This can be used for high-res |
| 14082 ** profiling. |
| 14083 */ |
| 14084 #if (defined(__GNUC__) || defined(_MSC_VER)) && \ |
| 14085 (defined(i386) || defined(__i386__) || defined(_M_IX86)) |
| 14086 |
| 14087 #if defined(__GNUC__) |
| 14088 |
| 14089 __inline__ sqlite_uint64 sqlite3Hwtime(void){ |
| 14090 unsigned int lo, hi; |
| 14091 __asm__ __volatile__ ("rdtsc" : "=a" (lo), "=d" (hi)); |
| 14092 return (sqlite_uint64)hi << 32 | lo; |
| 14093 } |
| 14094 |
| 14095 #elif defined(_MSC_VER) |
| 14096 |
| 14097 __declspec(naked) __inline sqlite_uint64 __cdecl sqlite3Hwtime(void){ |
| 14098 __asm { |
| 14099 rdtsc |
| 14100 ret ; return value at EDX:EAX |
| 14101 } |
| 14102 } |
| 14103 |
| 14104 #endif |
| 14105 |
| 14106 #elif (defined(__GNUC__) && defined(__x86_64__)) |
| 14107 |
| 14108 __inline__ sqlite_uint64 sqlite3Hwtime(void){ |
| 14109 unsigned long val; |
| 14110 __asm__ __volatile__ ("rdtsc" : "=A" (val)); |
| 14111 return val; |
| 14112 } |
| 14113 |
| 14114 #elif (defined(__GNUC__) && defined(__ppc__)) |
| 14115 |
| 14116 __inline__ sqlite_uint64 sqlite3Hwtime(void){ |
| 14117 unsigned long long retval; |
| 14118 unsigned long junk; |
| 14119 __asm__ __volatile__ ("\n\ |
| 14120 1: mftbu %1\n\ |
| 14121 mftb %L0\n\ |
| 14122 mftbu %0\n\ |
| 14123 cmpw %0,%1\n\ |
| 14124 bne 1b" |
| 14125 : "=r" (retval), "=r" (junk)); |
| 14126 return retval; |
| 14127 } |
| 14128 |
| 14129 #else |
| 14130 |
| 14131 #error Need implementation of sqlite3Hwtime() for your platform. |
| 14132 |
| 14133 /* |
| 14134 ** To compile without implementing sqlite3Hwtime() for your platform, |
| 14135 ** you can remove the above #error and use the following |
| 14136 ** stub function. You will lose timing support for many |
| 14137 ** of the debugging and testing utilities, but it should at |
| 14138 ** least compile and run. |
| 14139 */ |
| 14140 SQLITE_PRIVATE sqlite_uint64 sqlite3Hwtime(void){ return ((sqlite_uint64)0); } |
| 14141 |
| 14142 #endif |
| 14143 |
| 14144 #endif /* !defined(_HWTIME_H_) */ |
| 14145 |
| 14146 /************** End of hwtime.h **********************************************/ |
| 14147 /************** Continuing where we left off in os_common.h ******************/ |
| 14148 |
| 14149 static sqlite_uint64 g_start; |
| 14150 static sqlite_uint64 g_elapsed; |
| 14151 #define TIMER_START g_start=sqlite3Hwtime() |
| 14152 #define TIMER_END g_elapsed=sqlite3Hwtime()-g_start |
| 14153 #define TIMER_ELAPSED g_elapsed |
| 14154 #else |
| 14155 #define TIMER_START |
| 14156 #define TIMER_END |
| 14157 #define TIMER_ELAPSED ((sqlite_uint64)0) |
| 14158 #endif |
| 14159 |
| 14160 /* |
| 14161 ** If we compile with the SQLITE_TEST macro set, then the following block |
| 14162 ** of code will give us the ability to simulate a disk I/O error. This |
| 14163 ** is used for testing the I/O recovery logic. |
| 14164 */ |
| 14165 #ifdef SQLITE_TEST |
| 14166 SQLITE_API int sqlite3_io_error_hit = 0; /* Total number of I/O Error
s */ |
| 14167 SQLITE_API int sqlite3_io_error_hardhit = 0; /* Number of non-benign erro
rs */ |
| 14168 SQLITE_API int sqlite3_io_error_pending = 0; /* Count down to first I/O e
rror */ |
| 14169 SQLITE_API int sqlite3_io_error_persist = 0; /* True if I/O errors persis
t */ |
| 14170 SQLITE_API int sqlite3_io_error_benign = 0; /* True if errors are benign
*/ |
| 14171 SQLITE_API int sqlite3_diskfull_pending = 0; |
| 14172 SQLITE_API int sqlite3_diskfull = 0; |
| 14173 #define SimulateIOErrorBenign(X) sqlite3_io_error_benign=(X) |
| 14174 #define SimulateIOError(CODE) \ |
| 14175 if( (sqlite3_io_error_persist && sqlite3_io_error_hit) \ |
| 14176 || sqlite3_io_error_pending-- == 1 ) \ |
| 14177 { local_ioerr(); CODE; } |
| 14178 static void local_ioerr(){ |
| 14179 IOTRACE(("IOERR\n")); |
| 14180 sqlite3_io_error_hit++; |
| 14181 if( !sqlite3_io_error_benign ) sqlite3_io_error_hardhit++; |
| 14182 } |
| 14183 #define SimulateDiskfullError(CODE) \ |
| 14184 if( sqlite3_diskfull_pending ){ \ |
| 14185 if( sqlite3_diskfull_pending == 1 ){ \ |
| 14186 local_ioerr(); \ |
| 14187 sqlite3_diskfull = 1; \ |
| 14188 sqlite3_io_error_hit = 1; \ |
| 14189 CODE; \ |
| 14190 }else{ \ |
| 14191 sqlite3_diskfull_pending--; \ |
| 14192 } \ |
| 14193 } |
| 14194 #else |
| 14195 #define SimulateIOErrorBenign(X) |
| 14196 #define SimulateIOError(A) |
| 14197 #define SimulateDiskfullError(A) |
| 14198 #endif |
| 14199 |
| 14200 /* |
| 14201 ** When testing, keep a count of the number of open files. |
| 14202 */ |
| 14203 #ifdef SQLITE_TEST |
| 14204 SQLITE_API int sqlite3_open_file_count = 0; |
| 14205 #define OpenCounter(X) sqlite3_open_file_count+=(X) |
| 14206 #else |
| 14207 #define OpenCounter(X) |
| 14208 #endif |
| 14209 |
| 14210 #endif /* !defined(_OS_COMMON_H_) */ |
| 14211 |
| 14212 /************** End of os_common.h *******************************************/ |
| 14213 /************** Continuing where we left off in os_win.c *********************/ |
| 14214 |
| 14215 /* |
| 14216 ** Include the header file for the Windows VFS. |
| 14217 */ |
| 14218 /* #include "os_win.h" */ |
| 14219 |
| 14220 /* |
| 14221 ** Compiling and using WAL mode requires several APIs that are only |
| 14222 ** available in Windows platforms based on the NT kernel. |
| 14223 */ |
| 14224 #if !SQLITE_OS_WINNT && !defined(SQLITE_OMIT_WAL) |
| 14225 # error "WAL mode requires support from the Windows NT kernel, compile\ |
| 14226 with SQLITE_OMIT_WAL." |
| 14227 #endif |
| 14228 |
| 14229 #if !SQLITE_OS_WINNT && SQLITE_MAX_MMAP_SIZE>0 |
| 14230 # error "Memory mapped files require support from the Windows NT kernel,\ |
| 14231 compile with SQLITE_MAX_MMAP_SIZE=0." |
| 14232 #endif |
| 14233 |
| 14234 /* |
| 14235 ** Are most of the Win32 ANSI APIs available (i.e. with certain exceptions |
| 14236 ** based on the sub-platform)? |
| 14237 */ |
| 14238 #if !SQLITE_OS_WINCE && !SQLITE_OS_WINRT && !defined(SQLITE_WIN32_NO_ANSI) |
| 14239 # define SQLITE_WIN32_HAS_ANSI |
| 14240 #endif |
| 14241 |
| 14242 /* |
| 14243 ** Are most of the Win32 Unicode APIs available (i.e. with certain exceptions |
| 14244 ** based on the sub-platform)? |
| 14245 */ |
| 14246 #if (SQLITE_OS_WINCE || SQLITE_OS_WINNT || SQLITE_OS_WINRT) && \ |
| 14247 !defined(SQLITE_WIN32_NO_WIDE) |
| 14248 # define SQLITE_WIN32_HAS_WIDE |
| 14249 #endif |
| 14250 |
| 14251 /* |
| 14252 ** Make sure at least one set of Win32 APIs is available. |
| 14253 */ |
| 14254 #if !defined(SQLITE_WIN32_HAS_ANSI) && !defined(SQLITE_WIN32_HAS_WIDE) |
| 14255 # error "At least one of SQLITE_WIN32_HAS_ANSI and SQLITE_WIN32_HAS_WIDE\ |
| 14256 must be defined." |
| 14257 #endif |
| 14258 |
| 14259 /* |
| 14260 ** Define the required Windows SDK version constants if they are not |
| 14261 ** already available. |
| 14262 */ |
| 14263 #ifndef NTDDI_WIN8 |
| 14264 # define NTDDI_WIN8 0x06020000 |
| 14265 #endif |
| 14266 |
| 14267 #ifndef NTDDI_WINBLUE |
| 14268 # define NTDDI_WINBLUE 0x06030000 |
| 14269 #endif |
| 14270 |
| 14271 /* |
| 14272 ** Check to see if the GetVersionEx[AW] functions are deprecated on the |
| 14273 ** target system. GetVersionEx was first deprecated in Win8.1. |
| 14274 */ |
| 14275 #ifndef SQLITE_WIN32_GETVERSIONEX |
| 14276 # if defined(NTDDI_VERSION) && NTDDI_VERSION >= NTDDI_WINBLUE |
| 14277 # define SQLITE_WIN32_GETVERSIONEX 0 /* GetVersionEx() is deprecated */ |
| 14278 # else |
| 14279 # define SQLITE_WIN32_GETVERSIONEX 1 /* GetVersionEx() is current */ |
| 14280 # endif |
| 14281 #endif |
| 14282 |
| 14283 /* |
| 14284 ** This constant should already be defined (in the "WinDef.h" SDK file). |
| 14285 */ |
| 14286 #ifndef MAX_PATH |
| 14287 # define MAX_PATH (260) |
| 14288 #endif |
| 14289 |
| 14290 /* |
| 14291 ** Maximum pathname length (in chars) for Win32. This should normally be |
| 14292 ** MAX_PATH. |
| 14293 */ |
| 14294 #ifndef SQLITE_WIN32_MAX_PATH_CHARS |
| 14295 # define SQLITE_WIN32_MAX_PATH_CHARS (MAX_PATH) |
| 14296 #endif |
| 14297 |
| 14298 /* |
| 14299 ** This constant should already be defined (in the "WinNT.h" SDK file). |
| 14300 */ |
| 14301 #ifndef UNICODE_STRING_MAX_CHARS |
| 14302 # define UNICODE_STRING_MAX_CHARS (32767) |
| 14303 #endif |
| 14304 |
| 14305 /* |
| 14306 ** Maximum pathname length (in chars) for WinNT. This should normally be |
| 14307 ** UNICODE_STRING_MAX_CHARS. |
| 14308 */ |
| 14309 #ifndef SQLITE_WINNT_MAX_PATH_CHARS |
| 14310 # define SQLITE_WINNT_MAX_PATH_CHARS (UNICODE_STRING_MAX_CHARS) |
| 14311 #endif |
| 14312 |
| 14313 /* |
| 14314 ** Maximum pathname length (in bytes) for Win32. The MAX_PATH macro is in |
| 14315 ** characters, so we allocate 4 bytes per character assuming worst-case of |
| 14316 ** 4-bytes-per-character for UTF8. |
| 14317 */ |
| 14318 #ifndef SQLITE_WIN32_MAX_PATH_BYTES |
| 14319 # define SQLITE_WIN32_MAX_PATH_BYTES (SQLITE_WIN32_MAX_PATH_CHARS*4) |
| 14320 #endif |
| 14321 |
| 14322 /* |
| 14323 ** Maximum pathname length (in bytes) for WinNT. This should normally be |
| 14324 ** UNICODE_STRING_MAX_CHARS * sizeof(WCHAR). |
| 14325 */ |
| 14326 #ifndef SQLITE_WINNT_MAX_PATH_BYTES |
| 14327 # define SQLITE_WINNT_MAX_PATH_BYTES \ |
| 14328 (sizeof(WCHAR) * SQLITE_WINNT_MAX_PATH_CHARS) |
| 14329 #endif |
| 14330 |
| 14331 /* |
| 14332 ** Maximum error message length (in chars) for WinRT. |
| 14333 */ |
| 14334 #ifndef SQLITE_WIN32_MAX_ERRMSG_CHARS |
| 14335 # define SQLITE_WIN32_MAX_ERRMSG_CHARS (1024) |
| 14336 #endif |
| 14337 |
| 14338 /* |
| 14339 ** Returns non-zero if the character should be treated as a directory |
| 14340 ** separator. |
| 14341 */ |
| 14342 #ifndef winIsDirSep |
| 14343 # define winIsDirSep(a) (((a) == '/') || ((a) == '\\')) |
| 14344 #endif |
| 14345 |
| 14346 /* |
| 14347 ** This macro is used when a local variable is set to a value that is |
| 14348 ** [sometimes] not used by the code (e.g. via conditional compilation). |
| 14349 */ |
| 14350 #ifndef UNUSED_VARIABLE_VALUE |
| 14351 # define UNUSED_VARIABLE_VALUE(x) (void)(x) |
| 14352 #endif |
| 14353 |
| 14354 /* |
| 14355 ** Returns the character that should be used as the directory separator. |
| 14356 */ |
| 14357 #ifndef winGetDirSep |
| 14358 # define winGetDirSep() '\\' |
| 14359 #endif |
| 14360 |
| 14361 /* |
| 14362 ** Do we need to manually define the Win32 file mapping APIs for use with WAL |
| 14363 ** mode or memory mapped files (e.g. these APIs are available in the Windows |
| 14364 ** CE SDK; however, they are not present in the header file)? |
| 14365 */ |
| 14366 #if SQLITE_WIN32_FILEMAPPING_API && \ |
| 14367 (!defined(SQLITE_OMIT_WAL) || SQLITE_MAX_MMAP_SIZE>0) |
| 14368 /* |
| 14369 ** Two of the file mapping APIs are different under WinRT. Figure out which |
| 14370 ** set we need. |
| 14371 */ |
| 14372 #if SQLITE_OS_WINRT |
| 14373 WINBASEAPI HANDLE WINAPI CreateFileMappingFromApp(HANDLE, \ |
| 14374 LPSECURITY_ATTRIBUTES, ULONG, ULONG64, LPCWSTR); |
| 14375 |
| 14376 WINBASEAPI LPVOID WINAPI MapViewOfFileFromApp(HANDLE, ULONG, ULONG64, SIZE_T); |
| 14377 #else |
| 14378 #if defined(SQLITE_WIN32_HAS_ANSI) |
| 14379 WINBASEAPI HANDLE WINAPI CreateFileMappingA(HANDLE, LPSECURITY_ATTRIBUTES, \ |
| 14380 DWORD, DWORD, DWORD, LPCSTR); |
| 14381 #endif /* defined(SQLITE_WIN32_HAS_ANSI) */ |
| 14382 |
| 14383 #if defined(SQLITE_WIN32_HAS_WIDE) |
| 14384 WINBASEAPI HANDLE WINAPI CreateFileMappingW(HANDLE, LPSECURITY_ATTRIBUTES, \ |
| 14385 DWORD, DWORD, DWORD, LPCWSTR); |
| 14386 #endif /* defined(SQLITE_WIN32_HAS_WIDE) */ |
| 14387 |
| 14388 WINBASEAPI LPVOID WINAPI MapViewOfFile(HANDLE, DWORD, DWORD, DWORD, SIZE_T); |
| 14389 #endif /* SQLITE_OS_WINRT */ |
| 14390 |
| 14391 /* |
| 14392 ** These file mapping APIs are common to both Win32 and WinRT. |
| 14393 */ |
| 14394 |
| 14395 WINBASEAPI BOOL WINAPI FlushViewOfFile(LPCVOID, SIZE_T); |
| 14396 WINBASEAPI BOOL WINAPI UnmapViewOfFile(LPCVOID); |
| 14397 #endif /* SQLITE_WIN32_FILEMAPPING_API */ |
| 14398 |
| 14399 /* |
| 14400 ** Some Microsoft compilers lack this definition. |
| 14401 */ |
| 14402 #ifndef INVALID_FILE_ATTRIBUTES |
| 14403 # define INVALID_FILE_ATTRIBUTES ((DWORD)-1) |
| 14404 #endif |
| 14405 |
| 14406 #ifndef FILE_FLAG_MASK |
| 14407 # define FILE_FLAG_MASK (0xFF3C0000) |
| 14408 #endif |
| 14409 |
| 14410 #ifndef FILE_ATTRIBUTE_MASK |
| 14411 # define FILE_ATTRIBUTE_MASK (0x0003FFF7) |
| 14412 #endif |
| 14413 |
| 14414 #ifndef SQLITE_OMIT_WAL |
| 14415 /* Forward references to structures used for WAL */ |
| 14416 typedef struct winShm winShm; /* A connection to shared-memory */ |
| 14417 typedef struct winShmNode winShmNode; /* A region of shared-memory */ |
| 14418 #endif |
| 14419 |
| 14420 /* |
| 14421 ** WinCE lacks native support for file locking so we have to fake it |
| 14422 ** with some code of our own. |
| 14423 */ |
| 14424 #if SQLITE_OS_WINCE |
| 14425 typedef struct winceLock { |
| 14426 int nReaders; /* Number of reader locks obtained */ |
| 14427 BOOL bPending; /* Indicates a pending lock has been obtained */ |
| 14428 BOOL bReserved; /* Indicates a reserved lock has been obtained */ |
| 14429 BOOL bExclusive; /* Indicates an exclusive lock has been obtained */ |
| 14430 } winceLock; |
| 14431 #endif |
| 14432 |
| 14433 /* |
| 14434 ** The winFile structure is a subclass of sqlite3_file* specific to the win32 |
| 14435 ** portability layer. |
| 14436 */ |
| 14437 typedef struct winFile winFile; |
| 14438 struct winFile { |
| 14439 const sqlite3_io_methods *pMethod; /*** Must be first ***/ |
| 14440 sqlite3_vfs *pVfs; /* The VFS used to open this file */ |
| 14441 HANDLE h; /* Handle for accessing the file */ |
| 14442 u8 locktype; /* Type of lock currently held on this file */ |
| 14443 short sharedLockByte; /* Randomly chosen byte used as a shared lock */ |
| 14444 u8 ctrlFlags; /* Flags. See WINFILE_* below */ |
| 14445 DWORD lastErrno; /* The Windows errno from the last I/O error */ |
| 14446 #ifndef SQLITE_OMIT_WAL |
| 14447 winShm *pShm; /* Instance of shared memory on this file */ |
| 14448 #endif |
| 14449 const char *zPath; /* Full pathname of this file */ |
| 14450 int szChunk; /* Chunk size configured by FCNTL_CHUNK_SIZE */ |
| 14451 #if SQLITE_OS_WINCE |
| 14452 LPWSTR zDeleteOnClose; /* Name of file to delete when closing */ |
| 14453 HANDLE hMutex; /* Mutex used to control access to shared lock */ |
| 14454 HANDLE hShared; /* Shared memory segment used for locking */ |
| 14455 winceLock local; /* Locks obtained by this instance of winFile */ |
| 14456 winceLock *shared; /* Global shared lock memory for the file */ |
| 14457 #endif |
| 14458 #if SQLITE_MAX_MMAP_SIZE>0 |
| 14459 int nFetchOut; /* Number of outstanding xFetch references */ |
| 14460 HANDLE hMap; /* Handle for accessing memory mapping */ |
| 14461 void *pMapRegion; /* Area memory mapped */ |
| 14462 sqlite3_int64 mmapSize; /* Usable size of mapped region */ |
| 14463 sqlite3_int64 mmapSizeActual; /* Actual size of mapped region */ |
| 14464 sqlite3_int64 mmapSizeMax; /* Configured FCNTL_MMAP_SIZE value */ |
| 14465 #endif |
| 14466 }; |
| 14467 |
| 14468 /* |
| 14469 ** Allowed values for winFile.ctrlFlags |
| 14470 */ |
| 14471 #define WINFILE_RDONLY 0x02 /* Connection is read only */ |
| 14472 #define WINFILE_PERSIST_WAL 0x04 /* Persistent WAL mode */ |
| 14473 #define WINFILE_PSOW 0x10 /* SQLITE_IOCAP_POWERSAFE_OVERWRITE */ |
| 14474 |
| 14475 /* |
| 14476 * The size of the buffer used by sqlite3_win32_write_debug(). |
| 14477 */ |
| 14478 #ifndef SQLITE_WIN32_DBG_BUF_SIZE |
| 14479 # define SQLITE_WIN32_DBG_BUF_SIZE ((int)(4096-sizeof(DWORD))) |
| 14480 #endif |
| 14481 |
| 14482 /* |
| 14483 * The value used with sqlite3_win32_set_directory() to specify that |
| 14484 * the data directory should be changed. |
| 14485 */ |
| 14486 #ifndef SQLITE_WIN32_DATA_DIRECTORY_TYPE |
| 14487 # define SQLITE_WIN32_DATA_DIRECTORY_TYPE (1) |
| 14488 #endif |
| 14489 |
| 14490 /* |
| 14491 * The value used with sqlite3_win32_set_directory() to specify that |
| 14492 * the temporary directory should be changed. |
| 14493 */ |
| 14494 #ifndef SQLITE_WIN32_TEMP_DIRECTORY_TYPE |
| 14495 # define SQLITE_WIN32_TEMP_DIRECTORY_TYPE (2) |
| 14496 #endif |
| 14497 |
| 14498 /* |
| 14499 * If compiled with SQLITE_WIN32_MALLOC on Windows, we will use the |
| 14500 * various Win32 API heap functions instead of our own. |
| 14501 */ |
| 14502 #ifdef SQLITE_WIN32_MALLOC |
| 14503 |
| 14504 /* |
| 14505 * If this is non-zero, an isolated heap will be created by the native Win32 |
| 14506 * allocator subsystem; otherwise, the default process heap will be used. This |
| 14507 * setting has no effect when compiling for WinRT. By default, this is enabled |
| 14508 * and an isolated heap will be created to store all allocated data. |
| 14509 * |
| 14510 ****************************************************************************** |
| 14511 * WARNING: It is important to note that when this setting is non-zero and the |
| 14512 * winMemShutdown function is called (e.g. by the sqlite3_shutdown |
| 14513 * function), all data that was allocated using the isolated heap will |
| 14514 * be freed immediately and any attempt to access any of that freed |
| 14515 * data will almost certainly result in an immediate access violation. |
| 14516 ****************************************************************************** |
| 14517 */ |
| 14518 #ifndef SQLITE_WIN32_HEAP_CREATE |
| 14519 # define SQLITE_WIN32_HEAP_CREATE (TRUE) |
| 14520 #endif |
| 14521 |
| 14522 /* |
| 14523 * The initial size of the Win32-specific heap. This value may be zero. |
| 14524 */ |
| 14525 #ifndef SQLITE_WIN32_HEAP_INIT_SIZE |
| 14526 # define SQLITE_WIN32_HEAP_INIT_SIZE ((SQLITE_DEFAULT_CACHE_SIZE) * \ |
| 14527 (SQLITE_DEFAULT_PAGE_SIZE) + 4194304) |
| 14528 #endif |
| 14529 |
| 14530 /* |
| 14531 * The maximum size of the Win32-specific heap. This value may be zero. |
| 14532 */ |
| 14533 #ifndef SQLITE_WIN32_HEAP_MAX_SIZE |
| 14534 # define SQLITE_WIN32_HEAP_MAX_SIZE (0) |
| 14535 #endif |
| 14536 |
| 14537 /* |
| 14538 * The extra flags to use in calls to the Win32 heap APIs. This value may be |
| 14539 * zero for the default behavior. |
| 14540 */ |
| 14541 #ifndef SQLITE_WIN32_HEAP_FLAGS |
| 14542 # define SQLITE_WIN32_HEAP_FLAGS (0) |
| 14543 #endif |
| 14544 |
| 14545 |
| 14546 /* |
| 14547 ** The winMemData structure stores information required by the Win32-specific |
| 14548 ** sqlite3_mem_methods implementation. |
| 14549 */ |
| 14550 typedef struct winMemData winMemData; |
| 14551 struct winMemData { |
| 14552 #ifndef NDEBUG |
| 14553 u32 magic1; /* Magic number to detect structure corruption. */ |
| 14554 #endif |
| 14555 HANDLE hHeap; /* The handle to our heap. */ |
| 14556 BOOL bOwned; /* Do we own the heap (i.e. destroy it on shutdown)? */ |
| 14557 #ifndef NDEBUG |
| 14558 u32 magic2; /* Magic number to detect structure corruption. */ |
| 14559 #endif |
| 14560 }; |
| 14561 |
| 14562 #ifndef NDEBUG |
| 14563 #define WINMEM_MAGIC1 0x42b2830b |
| 14564 #define WINMEM_MAGIC2 0xbd4d7cf4 |
| 14565 #endif |
| 14566 |
| 14567 static struct winMemData win_mem_data = { |
| 14568 #ifndef NDEBUG |
| 14569 WINMEM_MAGIC1, |
| 14570 #endif |
| 14571 NULL, FALSE |
| 14572 #ifndef NDEBUG |
| 14573 ,WINMEM_MAGIC2 |
| 14574 #endif |
| 14575 }; |
| 14576 |
| 14577 #ifndef NDEBUG |
| 14578 #define winMemAssertMagic1() assert( win_mem_data.magic1==WINMEM_MAGIC1 ) |
| 14579 #define winMemAssertMagic2() assert( win_mem_data.magic2==WINMEM_MAGIC2 ) |
| 14580 #define winMemAssertMagic() winMemAssertMagic1(); winMemAssertMagic2(); |
| 14581 #else |
| 14582 #define winMemAssertMagic() |
| 14583 #endif |
| 14584 |
| 14585 #define winMemGetDataPtr() &win_mem_data |
| 14586 #define winMemGetHeap() win_mem_data.hHeap |
| 14587 #define winMemGetOwned() win_mem_data.bOwned |
| 14588 |
| 14589 static void *winMemMalloc(int nBytes); |
| 14590 static void winMemFree(void *pPrior); |
| 14591 static void *winMemRealloc(void *pPrior, int nBytes); |
| 14592 static int winMemSize(void *p); |
| 14593 static int winMemRoundup(int n); |
| 14594 static int winMemInit(void *pAppData); |
| 14595 static void winMemShutdown(void *pAppData); |
| 14596 |
| 14597 SQLITE_PRIVATE const sqlite3_mem_methods *sqlite3MemGetWin32(void); |
| 14598 #endif /* SQLITE_WIN32_MALLOC */ |
| 14599 |
| 14600 /* |
| 14601 ** The following variable is (normally) set once and never changes |
| 14602 ** thereafter. It records whether the operating system is Win9x |
| 14603 ** or WinNT. |
| 14604 ** |
| 14605 ** 0: Operating system unknown. |
| 14606 ** 1: Operating system is Win9x. |
| 14607 ** 2: Operating system is WinNT. |
| 14608 ** |
| 14609 ** In order to facilitate testing on a WinNT system, the test fixture |
| 14610 ** can manually set this value to 1 to emulate Win98 behavior. |
| 14611 */ |
| 14612 #ifdef SQLITE_TEST |
| 14613 SQLITE_API LONG SQLITE_WIN32_VOLATILE sqlite3_os_type = 0; |
| 14614 #else |
| 14615 static LONG SQLITE_WIN32_VOLATILE sqlite3_os_type = 0; |
| 14616 #endif |
| 14617 |
| 14618 #ifndef SYSCALL |
| 14619 # define SYSCALL sqlite3_syscall_ptr |
| 14620 #endif |
| 14621 |
| 14622 /* |
| 14623 ** This function is not available on Windows CE or WinRT. |
| 14624 */ |
| 14625 |
| 14626 #if SQLITE_OS_WINCE || SQLITE_OS_WINRT |
| 14627 # define osAreFileApisANSI() 1 |
| 14628 #endif |
| 14629 |
| 14630 /* |
| 14631 ** Many system calls are accessed through pointer-to-functions so that |
| 14632 ** they may be overridden at runtime to facilitate fault injection during |
| 14633 ** testing and sandboxing. The following array holds the names and pointers |
| 14634 ** to all overrideable system calls. |
| 14635 */ |
| 14636 static struct win_syscall { |
| 14637 const char *zName; /* Name of the system call */ |
| 14638 sqlite3_syscall_ptr pCurrent; /* Current value of the system call */ |
| 14639 sqlite3_syscall_ptr pDefault; /* Default value */ |
| 14640 } aSyscall[] = { |
| 14641 #if !SQLITE_OS_WINCE && !SQLITE_OS_WINRT |
| 14642 { "AreFileApisANSI", (SYSCALL)AreFileApisANSI, 0 }, |
| 14643 #else |
| 14644 { "AreFileApisANSI", (SYSCALL)0, 0 }, |
| 14645 #endif |
| 14646 |
| 14647 #ifndef osAreFileApisANSI |
| 14648 #define osAreFileApisANSI ((BOOL(WINAPI*)(VOID))aSyscall[0].pCurrent) |
| 14649 #endif |
| 14650 |
| 14651 #if SQLITE_OS_WINCE && defined(SQLITE_WIN32_HAS_WIDE) |
| 14652 { "CharLowerW", (SYSCALL)CharLowerW, 0 }, |
| 14653 #else |
| 14654 { "CharLowerW", (SYSCALL)0, 0 }, |
| 14655 #endif |
| 14656 |
| 14657 #define osCharLowerW ((LPWSTR(WINAPI*)(LPWSTR))aSyscall[1].pCurrent) |
| 14658 |
| 14659 #if SQLITE_OS_WINCE && defined(SQLITE_WIN32_HAS_WIDE) |
| 14660 { "CharUpperW", (SYSCALL)CharUpperW, 0 }, |
| 14661 #else |
| 14662 { "CharUpperW", (SYSCALL)0, 0 }, |
| 14663 #endif |
| 14664 |
| 14665 #define osCharUpperW ((LPWSTR(WINAPI*)(LPWSTR))aSyscall[2].pCurrent) |
| 14666 |
| 14667 { "CloseHandle", (SYSCALL)CloseHandle, 0 }, |
| 14668 |
| 14669 #define osCloseHandle ((BOOL(WINAPI*)(HANDLE))aSyscall[3].pCurrent) |
| 14670 |
| 14671 #if defined(SQLITE_WIN32_HAS_ANSI) |
| 14672 { "CreateFileA", (SYSCALL)CreateFileA, 0 }, |
| 14673 #else |
| 14674 { "CreateFileA", (SYSCALL)0, 0 }, |
| 14675 #endif |
| 14676 |
| 14677 #define osCreateFileA ((HANDLE(WINAPI*)(LPCSTR,DWORD,DWORD, \ |
| 14678 LPSECURITY_ATTRIBUTES,DWORD,DWORD,HANDLE))aSyscall[4].pCurrent) |
| 14679 |
| 14680 #if !SQLITE_OS_WINRT && defined(SQLITE_WIN32_HAS_WIDE) |
| 14681 { "CreateFileW", (SYSCALL)CreateFileW, 0 }, |
| 14682 #else |
| 14683 { "CreateFileW", (SYSCALL)0, 0 }, |
| 14684 #endif |
| 14685 |
| 14686 #define osCreateFileW ((HANDLE(WINAPI*)(LPCWSTR,DWORD,DWORD, \ |
| 14687 LPSECURITY_ATTRIBUTES,DWORD,DWORD,HANDLE))aSyscall[5].pCurrent) |
| 14688 |
| 14689 #if (!SQLITE_OS_WINRT && defined(SQLITE_WIN32_HAS_ANSI) && \ |
| 14690 (!defined(SQLITE_OMIT_WAL) || SQLITE_MAX_MMAP_SIZE>0)) |
| 14691 { "CreateFileMappingA", (SYSCALL)CreateFileMappingA, 0 }, |
| 14692 #else |
| 14693 { "CreateFileMappingA", (SYSCALL)0, 0 }, |
| 14694 #endif |
| 14695 |
| 14696 #define osCreateFileMappingA ((HANDLE(WINAPI*)(HANDLE,LPSECURITY_ATTRIBUTES, \ |
| 14697 DWORD,DWORD,DWORD,LPCSTR))aSyscall[6].pCurrent) |
| 14698 |
| 14699 #if SQLITE_OS_WINCE || (!SQLITE_OS_WINRT && defined(SQLITE_WIN32_HAS_WIDE) && \ |
| 14700 (!defined(SQLITE_OMIT_WAL) || SQLITE_MAX_MMAP_SIZE>0)) |
| 14701 { "CreateFileMappingW", (SYSCALL)CreateFileMappingW, 0 }, |
| 14702 #else |
| 14703 { "CreateFileMappingW", (SYSCALL)0, 0 }, |
| 14704 #endif |
| 14705 |
| 14706 #define osCreateFileMappingW ((HANDLE(WINAPI*)(HANDLE,LPSECURITY_ATTRIBUTES, \ |
| 14707 DWORD,DWORD,DWORD,LPCWSTR))aSyscall[7].pCurrent) |
| 14708 |
| 14709 #if !SQLITE_OS_WINRT && defined(SQLITE_WIN32_HAS_WIDE) |
| 14710 { "CreateMutexW", (SYSCALL)CreateMutexW, 0 }, |
| 14711 #else |
| 14712 { "CreateMutexW", (SYSCALL)0, 0 }, |
| 14713 #endif |
| 14714 |
| 14715 #define osCreateMutexW ((HANDLE(WINAPI*)(LPSECURITY_ATTRIBUTES,BOOL, \ |
| 14716 LPCWSTR))aSyscall[8].pCurrent) |
| 14717 |
| 14718 #if defined(SQLITE_WIN32_HAS_ANSI) |
| 14719 { "DeleteFileA", (SYSCALL)DeleteFileA, 0 }, |
| 14720 #else |
| 14721 { "DeleteFileA", (SYSCALL)0, 0 }, |
| 14722 #endif |
| 14723 |
| 14724 #define osDeleteFileA ((BOOL(WINAPI*)(LPCSTR))aSyscall[9].pCurrent) |
| 14725 |
| 14726 #if defined(SQLITE_WIN32_HAS_WIDE) |
| 14727 { "DeleteFileW", (SYSCALL)DeleteFileW, 0 }, |
| 14728 #else |
| 14729 { "DeleteFileW", (SYSCALL)0, 0 }, |
| 14730 #endif |
| 14731 |
| 14732 #define osDeleteFileW ((BOOL(WINAPI*)(LPCWSTR))aSyscall[10].pCurrent) |
| 14733 |
| 14734 #if SQLITE_OS_WINCE |
| 14735 { "FileTimeToLocalFileTime", (SYSCALL)FileTimeToLocalFileTime, 0 }, |
| 14736 #else |
| 14737 { "FileTimeToLocalFileTime", (SYSCALL)0, 0 }, |
| 14738 #endif |
| 14739 |
| 14740 #define osFileTimeToLocalFileTime ((BOOL(WINAPI*)(CONST FILETIME*, \ |
| 14741 LPFILETIME))aSyscall[11].pCurrent) |
| 14742 |
| 14743 #if SQLITE_OS_WINCE |
| 14744 { "FileTimeToSystemTime", (SYSCALL)FileTimeToSystemTime, 0 }, |
| 14745 #else |
| 14746 { "FileTimeToSystemTime", (SYSCALL)0, 0 }, |
| 14747 #endif |
| 14748 |
| 14749 #define osFileTimeToSystemTime ((BOOL(WINAPI*)(CONST FILETIME*, \ |
| 14750 LPSYSTEMTIME))aSyscall[12].pCurrent) |
| 14751 |
| 14752 { "FlushFileBuffers", (SYSCALL)FlushFileBuffers, 0 }, |
| 14753 |
| 14754 #define osFlushFileBuffers ((BOOL(WINAPI*)(HANDLE))aSyscall[13].pCurrent) |
| 14755 |
| 14756 #if defined(SQLITE_WIN32_HAS_ANSI) |
| 14757 { "FormatMessageA", (SYSCALL)FormatMessageA, 0 }, |
| 14758 #else |
| 14759 { "FormatMessageA", (SYSCALL)0, 0 }, |
| 14760 #endif |
| 14761 |
| 14762 #define osFormatMessageA ((DWORD(WINAPI*)(DWORD,LPCVOID,DWORD,DWORD,LPSTR, \ |
| 14763 DWORD,va_list*))aSyscall[14].pCurrent) |
| 14764 |
| 14765 #if defined(SQLITE_WIN32_HAS_WIDE) |
| 14766 { "FormatMessageW", (SYSCALL)FormatMessageW, 0 }, |
| 14767 #else |
| 14768 { "FormatMessageW", (SYSCALL)0, 0 }, |
| 14769 #endif |
| 14770 |
| 14771 #define osFormatMessageW ((DWORD(WINAPI*)(DWORD,LPCVOID,DWORD,DWORD,LPWSTR, \ |
| 14772 DWORD,va_list*))aSyscall[15].pCurrent) |
| 14773 |
| 14774 #if !defined(SQLITE_OMIT_LOAD_EXTENSION) |
| 14775 { "FreeLibrary", (SYSCALL)FreeLibrary, 0 }, |
| 14776 #else |
| 14777 { "FreeLibrary", (SYSCALL)0, 0 }, |
| 14778 #endif |
| 14779 |
| 14780 #define osFreeLibrary ((BOOL(WINAPI*)(HMODULE))aSyscall[16].pCurrent) |
| 14781 |
| 14782 { "GetCurrentProcessId", (SYSCALL)GetCurrentProcessId, 0 }, |
| 14783 |
| 14784 #define osGetCurrentProcessId ((DWORD(WINAPI*)(VOID))aSyscall[17].pCurrent) |
| 14785 |
| 14786 #if !SQLITE_OS_WINCE && defined(SQLITE_WIN32_HAS_ANSI) |
| 14787 { "GetDiskFreeSpaceA", (SYSCALL)GetDiskFreeSpaceA, 0 }, |
| 14788 #else |
| 14789 { "GetDiskFreeSpaceA", (SYSCALL)0, 0 }, |
| 14790 #endif |
| 14791 |
| 14792 #define osGetDiskFreeSpaceA ((BOOL(WINAPI*)(LPCSTR,LPDWORD,LPDWORD,LPDWORD, \ |
| 14793 LPDWORD))aSyscall[18].pCurrent) |
| 14794 |
| 14795 #if !SQLITE_OS_WINCE && !SQLITE_OS_WINRT && defined(SQLITE_WIN32_HAS_WIDE) |
| 14796 { "GetDiskFreeSpaceW", (SYSCALL)GetDiskFreeSpaceW, 0 }, |
| 14797 #else |
| 14798 { "GetDiskFreeSpaceW", (SYSCALL)0, 0 }, |
| 14799 #endif |
| 14800 |
| 14801 #define osGetDiskFreeSpaceW ((BOOL(WINAPI*)(LPCWSTR,LPDWORD,LPDWORD,LPDWORD, \ |
| 14802 LPDWORD))aSyscall[19].pCurrent) |
| 14803 |
| 14804 #if defined(SQLITE_WIN32_HAS_ANSI) |
| 14805 { "GetFileAttributesA", (SYSCALL)GetFileAttributesA, 0 }, |
| 14806 #else |
| 14807 { "GetFileAttributesA", (SYSCALL)0, 0 }, |
| 14808 #endif |
| 14809 |
| 14810 #define osGetFileAttributesA ((DWORD(WINAPI*)(LPCSTR))aSyscall[20].pCurrent) |
| 14811 |
| 14812 #if !SQLITE_OS_WINRT && defined(SQLITE_WIN32_HAS_WIDE) |
| 14813 { "GetFileAttributesW", (SYSCALL)GetFileAttributesW, 0 }, |
| 14814 #else |
| 14815 { "GetFileAttributesW", (SYSCALL)0, 0 }, |
| 14816 #endif |
| 14817 |
| 14818 #define osGetFileAttributesW ((DWORD(WINAPI*)(LPCWSTR))aSyscall[21].pCurrent) |
| 14819 |
| 14820 #if defined(SQLITE_WIN32_HAS_WIDE) |
| 14821 { "GetFileAttributesExW", (SYSCALL)GetFileAttributesExW, 0 }, |
| 14822 #else |
| 14823 { "GetFileAttributesExW", (SYSCALL)0, 0 }, |
| 14824 #endif |
| 14825 |
| 14826 #define osGetFileAttributesExW ((BOOL(WINAPI*)(LPCWSTR,GET_FILEEX_INFO_LEVELS, \ |
| 14827 LPVOID))aSyscall[22].pCurrent) |
| 14828 |
| 14829 #if !SQLITE_OS_WINRT |
| 14830 { "GetFileSize", (SYSCALL)GetFileSize, 0 }, |
| 14831 #else |
| 14832 { "GetFileSize", (SYSCALL)0, 0 }, |
| 14833 #endif |
| 14834 |
| 14835 #define osGetFileSize ((DWORD(WINAPI*)(HANDLE,LPDWORD))aSyscall[23].pCurrent) |
| 14836 |
| 14837 #if !SQLITE_OS_WINCE && defined(SQLITE_WIN32_HAS_ANSI) |
| 14838 { "GetFullPathNameA", (SYSCALL)GetFullPathNameA, 0 }, |
| 14839 #else |
| 14840 { "GetFullPathNameA", (SYSCALL)0, 0 }, |
| 14841 #endif |
| 14842 |
| 14843 #define osGetFullPathNameA ((DWORD(WINAPI*)(LPCSTR,DWORD,LPSTR, \ |
| 14844 LPSTR*))aSyscall[24].pCurrent) |
| 14845 |
| 14846 #if !SQLITE_OS_WINCE && !SQLITE_OS_WINRT && defined(SQLITE_WIN32_HAS_WIDE) |
| 14847 { "GetFullPathNameW", (SYSCALL)GetFullPathNameW, 0 }, |
| 14848 #else |
| 14849 { "GetFullPathNameW", (SYSCALL)0, 0 }, |
| 14850 #endif |
| 14851 |
| 14852 #define osGetFullPathNameW ((DWORD(WINAPI*)(LPCWSTR,DWORD,LPWSTR, \ |
| 14853 LPWSTR*))aSyscall[25].pCurrent) |
| 14854 |
| 14855 { "GetLastError", (SYSCALL)GetLastError, 0 }, |
| 14856 |
| 14857 #define osGetLastError ((DWORD(WINAPI*)(VOID))aSyscall[26].pCurrent) |
| 14858 |
| 14859 #if !defined(SQLITE_OMIT_LOAD_EXTENSION) |
| 14860 #if SQLITE_OS_WINCE |
| 14861 /* The GetProcAddressA() routine is only available on Windows CE. */ |
| 14862 { "GetProcAddressA", (SYSCALL)GetProcAddressA, 0 }, |
| 14863 #else |
| 14864 /* All other Windows platforms expect GetProcAddress() to take |
| 14865 ** an ANSI string regardless of the _UNICODE setting */ |
| 14866 { "GetProcAddressA", (SYSCALL)GetProcAddress, 0 }, |
| 14867 #endif |
| 14868 #else |
| 14869 { "GetProcAddressA", (SYSCALL)0, 0 }, |
| 14870 #endif |
| 14871 |
| 14872 #define osGetProcAddressA ((FARPROC(WINAPI*)(HMODULE, \ |
| 14873 LPCSTR))aSyscall[27].pCurrent) |
| 14874 |
| 14875 #if !SQLITE_OS_WINRT |
| 14876 { "GetSystemInfo", (SYSCALL)GetSystemInfo, 0 }, |
| 14877 #else |
| 14878 { "GetSystemInfo", (SYSCALL)0, 0 }, |
| 14879 #endif |
| 14880 |
| 14881 #define osGetSystemInfo ((VOID(WINAPI*)(LPSYSTEM_INFO))aSyscall[28].pCurrent) |
| 14882 |
| 14883 { "GetSystemTime", (SYSCALL)GetSystemTime, 0 }, |
| 14884 |
| 14885 #define osGetSystemTime ((VOID(WINAPI*)(LPSYSTEMTIME))aSyscall[29].pCurrent) |
| 14886 |
| 14887 #if !SQLITE_OS_WINCE |
| 14888 { "GetSystemTimeAsFileTime", (SYSCALL)GetSystemTimeAsFileTime, 0 }, |
| 14889 #else |
| 14890 { "GetSystemTimeAsFileTime", (SYSCALL)0, 0 }, |
| 14891 #endif |
| 14892 |
| 14893 #define osGetSystemTimeAsFileTime ((VOID(WINAPI*)( \ |
| 14894 LPFILETIME))aSyscall[30].pCurrent) |
| 14895 |
| 14896 #if defined(SQLITE_WIN32_HAS_ANSI) |
| 14897 { "GetTempPathA", (SYSCALL)GetTempPathA, 0 }, |
| 14898 #else |
| 14899 { "GetTempPathA", (SYSCALL)0, 0 }, |
| 14900 #endif |
| 14901 |
| 14902 #define osGetTempPathA ((DWORD(WINAPI*)(DWORD,LPSTR))aSyscall[31].pCurrent) |
| 14903 |
| 14904 #if !SQLITE_OS_WINRT && defined(SQLITE_WIN32_HAS_WIDE) |
| 14905 { "GetTempPathW", (SYSCALL)GetTempPathW, 0 }, |
| 14906 #else |
| 14907 { "GetTempPathW", (SYSCALL)0, 0 }, |
| 14908 #endif |
| 14909 |
| 14910 #define osGetTempPathW ((DWORD(WINAPI*)(DWORD,LPWSTR))aSyscall[32].pCurrent) |
| 14911 |
| 14912 #if !SQLITE_OS_WINRT |
| 14913 { "GetTickCount", (SYSCALL)GetTickCount, 0 }, |
| 14914 #else |
| 14915 { "GetTickCount", (SYSCALL)0, 0 }, |
| 14916 #endif |
| 14917 |
| 14918 #define osGetTickCount ((DWORD(WINAPI*)(VOID))aSyscall[33].pCurrent) |
| 14919 |
| 14920 #if defined(SQLITE_WIN32_HAS_ANSI) && defined(SQLITE_WIN32_GETVERSIONEX) && \ |
| 14921 SQLITE_WIN32_GETVERSIONEX |
| 14922 { "GetVersionExA", (SYSCALL)GetVersionExA, 0 }, |
| 14923 #else |
| 14924 { "GetVersionExA", (SYSCALL)0, 0 }, |
| 14925 #endif |
| 14926 |
| 14927 #define osGetVersionExA ((BOOL(WINAPI*)( \ |
| 14928 LPOSVERSIONINFOA))aSyscall[34].pCurrent) |
| 14929 |
| 14930 #if !SQLITE_OS_WINRT && defined(SQLITE_WIN32_HAS_WIDE) && \ |
| 14931 defined(SQLITE_WIN32_GETVERSIONEX) && SQLITE_WIN32_GETVERSIONEX |
| 14932 { "GetVersionExW", (SYSCALL)GetVersionExW, 0 }, |
| 14933 #else |
| 14934 { "GetVersionExW", (SYSCALL)0, 0 }, |
| 14935 #endif |
| 14936 |
| 14937 #define osGetVersionExW ((BOOL(WINAPI*)( \ |
| 14938 LPOSVERSIONINFOW))aSyscall[35].pCurrent) |
| 14939 |
| 14940 { "HeapAlloc", (SYSCALL)HeapAlloc, 0 }, |
| 14941 |
| 14942 #define osHeapAlloc ((LPVOID(WINAPI*)(HANDLE,DWORD, \ |
| 14943 SIZE_T))aSyscall[36].pCurrent) |
| 14944 |
| 14945 #if !SQLITE_OS_WINRT |
| 14946 { "HeapCreate", (SYSCALL)HeapCreate, 0 }, |
| 14947 #else |
| 14948 { "HeapCreate", (SYSCALL)0, 0 }, |
| 14949 #endif |
| 14950 |
| 14951 #define osHeapCreate ((HANDLE(WINAPI*)(DWORD,SIZE_T, \ |
| 14952 SIZE_T))aSyscall[37].pCurrent) |
| 14953 |
| 14954 #if !SQLITE_OS_WINRT |
| 14955 { "HeapDestroy", (SYSCALL)HeapDestroy, 0 }, |
| 14956 #else |
| 14957 { "HeapDestroy", (SYSCALL)0, 0 }, |
| 14958 #endif |
| 14959 |
| 14960 #define osHeapDestroy ((BOOL(WINAPI*)(HANDLE))aSyscall[38].pCurrent) |
| 14961 |
| 14962 { "HeapFree", (SYSCALL)HeapFree, 0 }, |
| 14963 |
| 14964 #define osHeapFree ((BOOL(WINAPI*)(HANDLE,DWORD,LPVOID))aSyscall[39].pCurrent) |
| 14965 |
| 14966 { "HeapReAlloc", (SYSCALL)HeapReAlloc, 0 }, |
| 14967 |
| 14968 #define osHeapReAlloc ((LPVOID(WINAPI*)(HANDLE,DWORD,LPVOID, \ |
| 14969 SIZE_T))aSyscall[40].pCurrent) |
| 14970 |
| 14971 { "HeapSize", (SYSCALL)HeapSize, 0 }, |
| 14972 |
| 14973 #define osHeapSize ((SIZE_T(WINAPI*)(HANDLE,DWORD, \ |
| 14974 LPCVOID))aSyscall[41].pCurrent) |
| 14975 |
| 14976 #if !SQLITE_OS_WINRT |
| 14977 { "HeapValidate", (SYSCALL)HeapValidate, 0 }, |
| 14978 #else |
| 14979 { "HeapValidate", (SYSCALL)0, 0 }, |
| 14980 #endif |
| 14981 |
| 14982 #define osHeapValidate ((BOOL(WINAPI*)(HANDLE,DWORD, \ |
| 14983 LPCVOID))aSyscall[42].pCurrent) |
| 14984 |
| 14985 #if !SQLITE_OS_WINCE && !SQLITE_OS_WINRT |
| 14986 { "HeapCompact", (SYSCALL)HeapCompact, 0 }, |
| 14987 #else |
| 14988 { "HeapCompact", (SYSCALL)0, 0 }, |
| 14989 #endif |
| 14990 |
| 14991 #define osHeapCompact ((UINT(WINAPI*)(HANDLE,DWORD))aSyscall[43].pCurrent) |
| 14992 |
| 14993 #if defined(SQLITE_WIN32_HAS_ANSI) && !defined(SQLITE_OMIT_LOAD_EXTENSION) |
| 14994 { "LoadLibraryA", (SYSCALL)LoadLibraryA, 0 }, |
| 14995 #else |
| 14996 { "LoadLibraryA", (SYSCALL)0, 0 }, |
| 14997 #endif |
| 14998 |
| 14999 #define osLoadLibraryA ((HMODULE(WINAPI*)(LPCSTR))aSyscall[44].pCurrent) |
| 15000 |
| 15001 #if !SQLITE_OS_WINRT && defined(SQLITE_WIN32_HAS_WIDE) && \ |
| 15002 !defined(SQLITE_OMIT_LOAD_EXTENSION) |
| 15003 { "LoadLibraryW", (SYSCALL)LoadLibraryW, 0 }, |
| 15004 #else |
| 15005 { "LoadLibraryW", (SYSCALL)0, 0 }, |
| 15006 #endif |
| 15007 |
| 15008 #define osLoadLibraryW ((HMODULE(WINAPI*)(LPCWSTR))aSyscall[45].pCurrent) |
| 15009 |
| 15010 #if !SQLITE_OS_WINRT |
| 15011 { "LocalFree", (SYSCALL)LocalFree, 0 }, |
| 15012 #else |
| 15013 { "LocalFree", (SYSCALL)0, 0 }, |
| 15014 #endif |
| 15015 |
| 15016 #define osLocalFree ((HLOCAL(WINAPI*)(HLOCAL))aSyscall[46].pCurrent) |
| 15017 |
| 15018 #if !SQLITE_OS_WINCE && !SQLITE_OS_WINRT |
| 15019 { "LockFile", (SYSCALL)LockFile, 0 }, |
| 15020 #else |
| 15021 { "LockFile", (SYSCALL)0, 0 }, |
| 15022 #endif |
| 15023 |
| 15024 #ifndef osLockFile |
| 15025 #define osLockFile ((BOOL(WINAPI*)(HANDLE,DWORD,DWORD,DWORD, \ |
| 15026 DWORD))aSyscall[47].pCurrent) |
| 15027 #endif |
| 15028 |
| 15029 #if !SQLITE_OS_WINCE |
| 15030 { "LockFileEx", (SYSCALL)LockFileEx, 0 }, |
| 15031 #else |
| 15032 { "LockFileEx", (SYSCALL)0, 0 }, |
| 15033 #endif |
| 15034 |
| 15035 #ifndef osLockFileEx |
| 15036 #define osLockFileEx ((BOOL(WINAPI*)(HANDLE,DWORD,DWORD,DWORD,DWORD, \ |
| 15037 LPOVERLAPPED))aSyscall[48].pCurrent) |
| 15038 #endif |
| 15039 |
| 15040 #if SQLITE_OS_WINCE || (!SQLITE_OS_WINRT && \ |
| 15041 (!defined(SQLITE_OMIT_WAL) || SQLITE_MAX_MMAP_SIZE>0)) |
| 15042 { "MapViewOfFile", (SYSCALL)MapViewOfFile, 0 }, |
| 15043 #else |
| 15044 { "MapViewOfFile", (SYSCALL)0, 0 }, |
| 15045 #endif |
| 15046 |
| 15047 #define osMapViewOfFile ((LPVOID(WINAPI*)(HANDLE,DWORD,DWORD,DWORD, \ |
| 15048 SIZE_T))aSyscall[49].pCurrent) |
| 15049 |
| 15050 { "MultiByteToWideChar", (SYSCALL)MultiByteToWideChar, 0 }, |
| 15051 |
| 15052 #define osMultiByteToWideChar ((int(WINAPI*)(UINT,DWORD,LPCSTR,int,LPWSTR, \ |
| 15053 int))aSyscall[50].pCurrent) |
| 15054 |
| 15055 { "QueryPerformanceCounter", (SYSCALL)QueryPerformanceCounter, 0 }, |
| 15056 |
| 15057 #define osQueryPerformanceCounter ((BOOL(WINAPI*)( \ |
| 15058 LARGE_INTEGER*))aSyscall[51].pCurrent) |
| 15059 |
| 15060 { "ReadFile", (SYSCALL)ReadFile, 0 }, |
| 15061 |
| 15062 #define osReadFile ((BOOL(WINAPI*)(HANDLE,LPVOID,DWORD,LPDWORD, \ |
| 15063 LPOVERLAPPED))aSyscall[52].pCurrent) |
| 15064 |
| 15065 { "SetEndOfFile", (SYSCALL)SetEndOfFile, 0 }, |
| 15066 |
| 15067 #define osSetEndOfFile ((BOOL(WINAPI*)(HANDLE))aSyscall[53].pCurrent) |
| 15068 |
| 15069 #if !SQLITE_OS_WINRT |
| 15070 { "SetFilePointer", (SYSCALL)SetFilePointer, 0 }, |
| 15071 #else |
| 15072 { "SetFilePointer", (SYSCALL)0, 0 }, |
| 15073 #endif |
| 15074 |
| 15075 #define osSetFilePointer ((DWORD(WINAPI*)(HANDLE,LONG,PLONG, \ |
| 15076 DWORD))aSyscall[54].pCurrent) |
| 15077 |
| 15078 #if !SQLITE_OS_WINRT |
| 15079 { "Sleep", (SYSCALL)Sleep, 0 }, |
| 15080 #else |
| 15081 { "Sleep", (SYSCALL)0, 0 }, |
| 15082 #endif |
| 15083 |
| 15084 #define osSleep ((VOID(WINAPI*)(DWORD))aSyscall[55].pCurrent) |
| 15085 |
| 15086 { "SystemTimeToFileTime", (SYSCALL)SystemTimeToFileTime, 0 }, |
| 15087 |
| 15088 #define osSystemTimeToFileTime ((BOOL(WINAPI*)(CONST SYSTEMTIME*, \ |
| 15089 LPFILETIME))aSyscall[56].pCurrent) |
| 15090 |
| 15091 #if !SQLITE_OS_WINCE && !SQLITE_OS_WINRT |
| 15092 { "UnlockFile", (SYSCALL)UnlockFile, 0 }, |
| 15093 #else |
| 15094 { "UnlockFile", (SYSCALL)0, 0 }, |
| 15095 #endif |
| 15096 |
| 15097 #ifndef osUnlockFile |
| 15098 #define osUnlockFile ((BOOL(WINAPI*)(HANDLE,DWORD,DWORD,DWORD, \ |
| 15099 DWORD))aSyscall[57].pCurrent) |
| 15100 #endif |
| 15101 |
| 15102 #if !SQLITE_OS_WINCE |
| 15103 { "UnlockFileEx", (SYSCALL)UnlockFileEx, 0 }, |
| 15104 #else |
| 15105 { "UnlockFileEx", (SYSCALL)0, 0 }, |
| 15106 #endif |
| 15107 |
| 15108 #define osUnlockFileEx ((BOOL(WINAPI*)(HANDLE,DWORD,DWORD,DWORD, \ |
| 15109 LPOVERLAPPED))aSyscall[58].pCurrent) |
| 15110 |
| 15111 #if SQLITE_OS_WINCE || !defined(SQLITE_OMIT_WAL) || SQLITE_MAX_MMAP_SIZE>0 |
| 15112 { "UnmapViewOfFile", (SYSCALL)UnmapViewOfFile, 0 }, |
| 15113 #else |
| 15114 { "UnmapViewOfFile", (SYSCALL)0, 0 }, |
| 15115 #endif |
| 15116 |
| 15117 #define osUnmapViewOfFile ((BOOL(WINAPI*)(LPCVOID))aSyscall[59].pCurrent) |
| 15118 |
| 15119 { "WideCharToMultiByte", (SYSCALL)WideCharToMultiByte, 0 }, |
| 15120 |
| 15121 #define osWideCharToMultiByte ((int(WINAPI*)(UINT,DWORD,LPCWSTR,int,LPSTR,int, \ |
| 15122 LPCSTR,LPBOOL))aSyscall[60].pCurrent) |
| 15123 |
| 15124 { "WriteFile", (SYSCALL)WriteFile, 0 }, |
| 15125 |
| 15126 #define osWriteFile ((BOOL(WINAPI*)(HANDLE,LPCVOID,DWORD,LPDWORD, \ |
| 15127 LPOVERLAPPED))aSyscall[61].pCurrent) |
| 15128 |
| 15129 #if SQLITE_OS_WINRT |
| 15130 { "CreateEventExW", (SYSCALL)CreateEventExW, 0 }, |
| 15131 #else |
| 15132 { "CreateEventExW", (SYSCALL)0, 0 }, |
| 15133 #endif |
| 15134 |
| 15135 #define osCreateEventExW ((HANDLE(WINAPI*)(LPSECURITY_ATTRIBUTES,LPCWSTR, \ |
| 15136 DWORD,DWORD))aSyscall[62].pCurrent) |
| 15137 |
| 15138 #if !SQLITE_OS_WINRT |
| 15139 { "WaitForSingleObject", (SYSCALL)WaitForSingleObject, 0 }, |
| 15140 #else |
| 15141 { "WaitForSingleObject", (SYSCALL)0, 0 }, |
| 15142 #endif |
| 15143 |
| 15144 #define osWaitForSingleObject ((DWORD(WINAPI*)(HANDLE, \ |
| 15145 DWORD))aSyscall[63].pCurrent) |
| 15146 |
| 15147 #if !SQLITE_OS_WINCE |
| 15148 { "WaitForSingleObjectEx", (SYSCALL)WaitForSingleObjectEx, 0 }, |
| 15149 #else |
| 15150 { "WaitForSingleObjectEx", (SYSCALL)0, 0 }, |
| 15151 #endif |
| 15152 |
| 15153 #define osWaitForSingleObjectEx ((DWORD(WINAPI*)(HANDLE,DWORD, \ |
| 15154 BOOL))aSyscall[64].pCurrent) |
| 15155 |
| 15156 #if SQLITE_OS_WINRT |
| 15157 { "SetFilePointerEx", (SYSCALL)SetFilePointerEx, 0 }, |
| 15158 #else |
| 15159 { "SetFilePointerEx", (SYSCALL)0, 0 }, |
| 15160 #endif |
| 15161 |
| 15162 #define osSetFilePointerEx ((BOOL(WINAPI*)(HANDLE,LARGE_INTEGER, \ |
| 15163 PLARGE_INTEGER,DWORD))aSyscall[65].pCurrent) |
| 15164 |
| 15165 #if SQLITE_OS_WINRT |
| 15166 { "GetFileInformationByHandleEx", (SYSCALL)GetFileInformationByHandleEx, 0 }, |
| 15167 #else |
| 15168 { "GetFileInformationByHandleEx", (SYSCALL)0, 0 }, |
| 15169 #endif |
| 15170 |
| 15171 #define osGetFileInformationByHandleEx ((BOOL(WINAPI*)(HANDLE, \ |
| 15172 FILE_INFO_BY_HANDLE_CLASS,LPVOID,DWORD))aSyscall[66].pCurrent) |
| 15173 |
| 15174 #if SQLITE_OS_WINRT && (!defined(SQLITE_OMIT_WAL) || SQLITE_MAX_MMAP_SIZE>0) |
| 15175 { "MapViewOfFileFromApp", (SYSCALL)MapViewOfFileFromApp, 0 }, |
| 15176 #else |
| 15177 { "MapViewOfFileFromApp", (SYSCALL)0, 0 }, |
| 15178 #endif |
| 15179 |
| 15180 #define osMapViewOfFileFromApp ((LPVOID(WINAPI*)(HANDLE,ULONG,ULONG64, \ |
| 15181 SIZE_T))aSyscall[67].pCurrent) |
| 15182 |
| 15183 #if SQLITE_OS_WINRT |
| 15184 { "CreateFile2", (SYSCALL)CreateFile2, 0 }, |
| 15185 #else |
| 15186 { "CreateFile2", (SYSCALL)0, 0 }, |
| 15187 #endif |
| 15188 |
| 15189 #define osCreateFile2 ((HANDLE(WINAPI*)(LPCWSTR,DWORD,DWORD,DWORD, \ |
| 15190 LPCREATEFILE2_EXTENDED_PARAMETERS))aSyscall[68].pCurrent) |
| 15191 |
| 15192 #if SQLITE_OS_WINRT && !defined(SQLITE_OMIT_LOAD_EXTENSION) |
| 15193 { "LoadPackagedLibrary", (SYSCALL)LoadPackagedLibrary, 0 }, |
| 15194 #else |
| 15195 { "LoadPackagedLibrary", (SYSCALL)0, 0 }, |
| 15196 #endif |
| 15197 |
| 15198 #define osLoadPackagedLibrary ((HMODULE(WINAPI*)(LPCWSTR, \ |
| 15199 DWORD))aSyscall[69].pCurrent) |
| 15200 |
| 15201 #if SQLITE_OS_WINRT |
| 15202 { "GetTickCount64", (SYSCALL)GetTickCount64, 0 }, |
| 15203 #else |
| 15204 { "GetTickCount64", (SYSCALL)0, 0 }, |
| 15205 #endif |
| 15206 |
| 15207 #define osGetTickCount64 ((ULONGLONG(WINAPI*)(VOID))aSyscall[70].pCurrent) |
| 15208 |
| 15209 #if SQLITE_OS_WINRT |
| 15210 { "GetNativeSystemInfo", (SYSCALL)GetNativeSystemInfo, 0 }, |
| 15211 #else |
| 15212 { "GetNativeSystemInfo", (SYSCALL)0, 0 }, |
| 15213 #endif |
| 15214 |
| 15215 #define osGetNativeSystemInfo ((VOID(WINAPI*)( \ |
| 15216 LPSYSTEM_INFO))aSyscall[71].pCurrent) |
| 15217 |
| 15218 #if defined(SQLITE_WIN32_HAS_ANSI) |
| 15219 { "OutputDebugStringA", (SYSCALL)OutputDebugStringA, 0 }, |
| 15220 #else |
| 15221 { "OutputDebugStringA", (SYSCALL)0, 0 }, |
| 15222 #endif |
| 15223 |
| 15224 #define osOutputDebugStringA ((VOID(WINAPI*)(LPCSTR))aSyscall[72].pCurrent) |
| 15225 |
| 15226 #if defined(SQLITE_WIN32_HAS_WIDE) |
| 15227 { "OutputDebugStringW", (SYSCALL)OutputDebugStringW, 0 }, |
| 15228 #else |
| 15229 { "OutputDebugStringW", (SYSCALL)0, 0 }, |
| 15230 #endif |
| 15231 |
| 15232 #define osOutputDebugStringW ((VOID(WINAPI*)(LPCWSTR))aSyscall[73].pCurrent) |
| 15233 |
| 15234 { "GetProcessHeap", (SYSCALL)GetProcessHeap, 0 }, |
| 15235 |
| 15236 #define osGetProcessHeap ((HANDLE(WINAPI*)(VOID))aSyscall[74].pCurrent) |
| 15237 |
| 15238 #if SQLITE_OS_WINRT && (!defined(SQLITE_OMIT_WAL) || SQLITE_MAX_MMAP_SIZE>0) |
| 15239 { "CreateFileMappingFromApp", (SYSCALL)CreateFileMappingFromApp, 0 }, |
| 15240 #else |
| 15241 { "CreateFileMappingFromApp", (SYSCALL)0, 0 }, |
| 15242 #endif |
| 15243 |
| 15244 #define osCreateFileMappingFromApp ((HANDLE(WINAPI*)(HANDLE, \ |
| 15245 LPSECURITY_ATTRIBUTES,ULONG,ULONG64,LPCWSTR))aSyscall[75].pCurrent) |
| 15246 |
| 15247 /* |
| 15248 ** NOTE: On some sub-platforms, the InterlockedCompareExchange "function" |
| 15249 ** is really just a macro that uses a compiler intrinsic (e.g. x64). |
| 15250 ** So do not try to make this is into a redefinable interface. |
| 15251 */ |
| 15252 #if defined(InterlockedCompareExchange) |
| 15253 { "InterlockedCompareExchange", (SYSCALL)0, 0 }, |
| 15254 |
| 15255 #define osInterlockedCompareExchange InterlockedCompareExchange |
| 15256 #else |
| 15257 { "InterlockedCompareExchange", (SYSCALL)InterlockedCompareExchange, 0 }, |
| 15258 |
| 15259 #define osInterlockedCompareExchange ((LONG(WINAPI*)(LONG \ |
| 15260 SQLITE_WIN32_VOLATILE*, LONG,LONG))aSyscall[76].pCurrent) |
| 15261 #endif /* defined(InterlockedCompareExchange) */ |
| 15262 |
| 15263 #if !SQLITE_OS_WINCE && !SQLITE_OS_WINRT && SQLITE_WIN32_USE_UUID |
| 15264 { "UuidCreate", (SYSCALL)UuidCreate, 0 }, |
| 15265 #else |
| 15266 { "UuidCreate", (SYSCALL)0, 0 }, |
| 15267 #endif |
| 15268 |
| 15269 #define osUuidCreate ((RPC_STATUS(RPC_ENTRY*)(UUID*))aSyscall[77].pCurrent) |
| 15270 |
| 15271 #if !SQLITE_OS_WINCE && !SQLITE_OS_WINRT && SQLITE_WIN32_USE_UUID |
| 15272 { "UuidCreateSequential", (SYSCALL)UuidCreateSequential, 0 }, |
| 15273 #else |
| 15274 { "UuidCreateSequential", (SYSCALL)0, 0 }, |
| 15275 #endif |
| 15276 |
| 15277 #define osUuidCreateSequential \ |
| 15278 ((RPC_STATUS(RPC_ENTRY*)(UUID*))aSyscall[78].pCurrent) |
| 15279 |
| 15280 #if !defined(SQLITE_NO_SYNC) && SQLITE_MAX_MMAP_SIZE>0 |
| 15281 { "FlushViewOfFile", (SYSCALL)FlushViewOfFile, 0 }, |
| 15282 #else |
| 15283 { "FlushViewOfFile", (SYSCALL)0, 0 }, |
| 15284 #endif |
| 15285 |
| 15286 #define osFlushViewOfFile \ |
| 15287 ((BOOL(WINAPI*)(LPCVOID,SIZE_T))aSyscall[79].pCurrent) |
| 15288 |
| 15289 }; /* End of the overrideable system calls */ |
| 15290 |
| 15291 /* |
| 15292 ** This is the xSetSystemCall() method of sqlite3_vfs for all of the |
| 15293 ** "win32" VFSes. Return SQLITE_OK opon successfully updating the |
| 15294 ** system call pointer, or SQLITE_NOTFOUND if there is no configurable |
| 15295 ** system call named zName. |
| 15296 */ |
| 15297 static int winSetSystemCall( |
| 15298 sqlite3_vfs *pNotUsed, /* The VFS pointer. Not used */ |
| 15299 const char *zName, /* Name of system call to override */ |
| 15300 sqlite3_syscall_ptr pNewFunc /* Pointer to new system call value */ |
| 15301 ){ |
| 15302 unsigned int i; |
| 15303 int rc = SQLITE_NOTFOUND; |
| 15304 |
| 15305 UNUSED_PARAMETER(pNotUsed); |
| 15306 if( zName==0 ){ |
| 15307 /* If no zName is given, restore all system calls to their default |
| 15308 ** settings and return NULL |
| 15309 */ |
| 15310 rc = SQLITE_OK; |
| 15311 for(i=0; i<sizeof(aSyscall)/sizeof(aSyscall[0]); i++){ |
| 15312 if( aSyscall[i].pDefault ){ |
| 15313 aSyscall[i].pCurrent = aSyscall[i].pDefault; |
| 15314 } |
| 15315 } |
| 15316 }else{ |
| 15317 /* If zName is specified, operate on only the one system call |
| 15318 ** specified. |
| 15319 */ |
| 15320 for(i=0; i<sizeof(aSyscall)/sizeof(aSyscall[0]); i++){ |
| 15321 if( strcmp(zName, aSyscall[i].zName)==0 ){ |
| 15322 if( aSyscall[i].pDefault==0 ){ |
| 15323 aSyscall[i].pDefault = aSyscall[i].pCurrent; |
| 15324 } |
| 15325 rc = SQLITE_OK; |
| 15326 if( pNewFunc==0 ) pNewFunc = aSyscall[i].pDefault; |
| 15327 aSyscall[i].pCurrent = pNewFunc; |
| 15328 break; |
| 15329 } |
| 15330 } |
| 15331 } |
| 15332 return rc; |
| 15333 } |
| 15334 |
| 15335 /* |
| 15336 ** Return the value of a system call. Return NULL if zName is not a |
| 15337 ** recognized system call name. NULL is also returned if the system call |
| 15338 ** is currently undefined. |
| 15339 */ |
| 15340 static sqlite3_syscall_ptr winGetSystemCall( |
| 15341 sqlite3_vfs *pNotUsed, |
| 15342 const char *zName |
| 15343 ){ |
| 15344 unsigned int i; |
| 15345 |
| 15346 UNUSED_PARAMETER(pNotUsed); |
| 15347 for(i=0; i<sizeof(aSyscall)/sizeof(aSyscall[0]); i++){ |
| 15348 if( strcmp(zName, aSyscall[i].zName)==0 ) return aSyscall[i].pCurrent; |
| 15349 } |
| 15350 return 0; |
| 15351 } |
| 15352 |
| 15353 /* |
| 15354 ** Return the name of the first system call after zName. If zName==NULL |
| 15355 ** then return the name of the first system call. Return NULL if zName |
| 15356 ** is the last system call or if zName is not the name of a valid |
| 15357 ** system call. |
| 15358 */ |
| 15359 static const char *winNextSystemCall(sqlite3_vfs *p, const char *zName){ |
| 15360 int i = -1; |
| 15361 |
| 15362 UNUSED_PARAMETER(p); |
| 15363 if( zName ){ |
| 15364 for(i=0; i<ArraySize(aSyscall)-1; i++){ |
| 15365 if( strcmp(zName, aSyscall[i].zName)==0 ) break; |
| 15366 } |
| 15367 } |
| 15368 for(i++; i<ArraySize(aSyscall); i++){ |
| 15369 if( aSyscall[i].pCurrent!=0 ) return aSyscall[i].zName; |
| 15370 } |
| 15371 return 0; |
| 15372 } |
| 15373 |
| 15374 #ifdef SQLITE_WIN32_MALLOC |
| 15375 /* |
| 15376 ** If a Win32 native heap has been configured, this function will attempt to |
| 15377 ** compact it. Upon success, SQLITE_OK will be returned. Upon failure, one |
| 15378 ** of SQLITE_NOMEM, SQLITE_ERROR, or SQLITE_NOTFOUND will be returned. The |
| 15379 ** "pnLargest" argument, if non-zero, will be used to return the size of the |
| 15380 ** largest committed free block in the heap, in bytes. |
| 15381 */ |
| 15382 SQLITE_API int SQLITE_STDCALL sqlite3_win32_compact_heap(LPUINT pnLargest){ |
| 15383 int rc = SQLITE_OK; |
| 15384 UINT nLargest = 0; |
| 15385 HANDLE hHeap; |
| 15386 |
| 15387 winMemAssertMagic(); |
| 15388 hHeap = winMemGetHeap(); |
| 15389 assert( hHeap!=0 ); |
| 15390 assert( hHeap!=INVALID_HANDLE_VALUE ); |
| 15391 #if !SQLITE_OS_WINRT && defined(SQLITE_WIN32_MALLOC_VALIDATE) |
| 15392 assert( osHeapValidate(hHeap, SQLITE_WIN32_HEAP_FLAGS, NULL) ); |
| 15393 #endif |
| 15394 #if !SQLITE_OS_WINCE && !SQLITE_OS_WINRT |
| 15395 if( (nLargest=osHeapCompact(hHeap, SQLITE_WIN32_HEAP_FLAGS))==0 ){ |
| 15396 DWORD lastErrno = osGetLastError(); |
| 15397 if( lastErrno==NO_ERROR ){ |
| 15398 sqlite3_log(SQLITE_NOMEM, "failed to HeapCompact (no space), heap=%p", |
| 15399 (void*)hHeap); |
| 15400 rc = SQLITE_NOMEM; |
| 15401 }else{ |
| 15402 sqlite3_log(SQLITE_ERROR, "failed to HeapCompact (%lu), heap=%p", |
| 15403 osGetLastError(), (void*)hHeap); |
| 15404 rc = SQLITE_ERROR; |
| 15405 } |
| 15406 } |
| 15407 #else |
| 15408 sqlite3_log(SQLITE_NOTFOUND, "failed to HeapCompact, heap=%p", |
| 15409 (void*)hHeap); |
| 15410 rc = SQLITE_NOTFOUND; |
| 15411 #endif |
| 15412 if( pnLargest ) *pnLargest = nLargest; |
| 15413 return rc; |
| 15414 } |
| 15415 |
| 15416 /* |
| 15417 ** If a Win32 native heap has been configured, this function will attempt to |
| 15418 ** destroy and recreate it. If the Win32 native heap is not isolated and/or |
| 15419 ** the sqlite3_memory_used() function does not return zero, SQLITE_BUSY will |
| 15420 ** be returned and no changes will be made to the Win32 native heap. |
| 15421 */ |
| 15422 SQLITE_API int SQLITE_STDCALL sqlite3_win32_reset_heap(){ |
| 15423 int rc; |
| 15424 MUTEX_LOGIC( sqlite3_mutex *pMaster; ) /* The main static mutex */ |
| 15425 MUTEX_LOGIC( sqlite3_mutex *pMem; ) /* The memsys static mutex */ |
| 15426 MUTEX_LOGIC( pMaster = sqlite3_mutex_alloc(SQLITE_MUTEX_STATIC_MASTER); ) |
| 15427 MUTEX_LOGIC( pMem = sqlite3_mutex_alloc(SQLITE_MUTEX_STATIC_MEM); ) |
| 15428 sqlite3_mutex_enter(pMaster); |
| 15429 sqlite3_mutex_enter(pMem); |
| 15430 winMemAssertMagic(); |
| 15431 if( winMemGetHeap()!=NULL && winMemGetOwned() && sqlite3_memory_used()==0 ){ |
| 15432 /* |
| 15433 ** At this point, there should be no outstanding memory allocations on |
| 15434 ** the heap. Also, since both the master and memsys locks are currently |
| 15435 ** being held by us, no other function (i.e. from another thread) should |
| 15436 ** be able to even access the heap. Attempt to destroy and recreate our |
| 15437 ** isolated Win32 native heap now. |
| 15438 */ |
| 15439 assert( winMemGetHeap()!=NULL ); |
| 15440 assert( winMemGetOwned() ); |
| 15441 assert( sqlite3_memory_used()==0 ); |
| 15442 winMemShutdown(winMemGetDataPtr()); |
| 15443 assert( winMemGetHeap()==NULL ); |
| 15444 assert( !winMemGetOwned() ); |
| 15445 assert( sqlite3_memory_used()==0 ); |
| 15446 rc = winMemInit(winMemGetDataPtr()); |
| 15447 assert( rc!=SQLITE_OK || winMemGetHeap()!=NULL ); |
| 15448 assert( rc!=SQLITE_OK || winMemGetOwned() ); |
| 15449 assert( rc!=SQLITE_OK || sqlite3_memory_used()==0 ); |
| 15450 }else{ |
| 15451 /* |
| 15452 ** The Win32 native heap cannot be modified because it may be in use. |
| 15453 */ |
| 15454 rc = SQLITE_BUSY; |
| 15455 } |
| 15456 sqlite3_mutex_leave(pMem); |
| 15457 sqlite3_mutex_leave(pMaster); |
| 15458 return rc; |
| 15459 } |
| 15460 #endif /* SQLITE_WIN32_MALLOC */ |
| 15461 |
| 15462 /* |
| 15463 ** This function outputs the specified (ANSI) string to the Win32 debugger |
| 15464 ** (if available). |
| 15465 */ |
| 15466 |
| 15467 SQLITE_API void SQLITE_STDCALL sqlite3_win32_write_debug(const char *zBuf, int n
Buf){ |
| 15468 char zDbgBuf[SQLITE_WIN32_DBG_BUF_SIZE]; |
| 15469 int nMin = MIN(nBuf, (SQLITE_WIN32_DBG_BUF_SIZE - 1)); /* may be negative. */ |
| 15470 if( nMin<-1 ) nMin = -1; /* all negative values become -1. */ |
| 15471 assert( nMin==-1 || nMin==0 || nMin<SQLITE_WIN32_DBG_BUF_SIZE ); |
| 15472 #if defined(SQLITE_WIN32_HAS_ANSI) |
| 15473 if( nMin>0 ){ |
| 15474 memset(zDbgBuf, 0, SQLITE_WIN32_DBG_BUF_SIZE); |
| 15475 memcpy(zDbgBuf, zBuf, nMin); |
| 15476 osOutputDebugStringA(zDbgBuf); |
| 15477 }else{ |
| 15478 osOutputDebugStringA(zBuf); |
| 15479 } |
| 15480 #elif defined(SQLITE_WIN32_HAS_WIDE) |
| 15481 memset(zDbgBuf, 0, SQLITE_WIN32_DBG_BUF_SIZE); |
| 15482 if ( osMultiByteToWideChar( |
| 15483 osAreFileApisANSI() ? CP_ACP : CP_OEMCP, 0, zBuf, |
| 15484 nMin, (LPWSTR)zDbgBuf, SQLITE_WIN32_DBG_BUF_SIZE/sizeof(WCHAR))<=0 ){ |
| 15485 return; |
| 15486 } |
| 15487 osOutputDebugStringW((LPCWSTR)zDbgBuf); |
| 15488 #else |
| 15489 if( nMin>0 ){ |
| 15490 memset(zDbgBuf, 0, SQLITE_WIN32_DBG_BUF_SIZE); |
| 15491 memcpy(zDbgBuf, zBuf, nMin); |
| 15492 fprintf(stderr, "%s", zDbgBuf); |
| 15493 }else{ |
| 15494 fprintf(stderr, "%s", zBuf); |
| 15495 } |
| 15496 #endif |
| 15497 } |
| 15498 |
| 15499 /* |
| 15500 ** The following routine suspends the current thread for at least ms |
| 15501 ** milliseconds. This is equivalent to the Win32 Sleep() interface. |
| 15502 */ |
| 15503 #if SQLITE_OS_WINRT |
| 15504 static HANDLE sleepObj = NULL; |
| 15505 #endif |
| 15506 |
| 15507 SQLITE_API void SQLITE_STDCALL sqlite3_win32_sleep(DWORD milliseconds){ |
| 15508 #if SQLITE_OS_WINRT |
| 15509 if ( sleepObj==NULL ){ |
| 15510 sleepObj = osCreateEventExW(NULL, NULL, CREATE_EVENT_MANUAL_RESET, |
| 15511 SYNCHRONIZE); |
| 15512 } |
| 15513 assert( sleepObj!=NULL ); |
| 15514 osWaitForSingleObjectEx(sleepObj, milliseconds, FALSE); |
| 15515 #else |
| 15516 osSleep(milliseconds); |
| 15517 #endif |
| 15518 } |
| 15519 |
| 15520 #if SQLITE_MAX_WORKER_THREADS>0 && !SQLITE_OS_WINCE && !SQLITE_OS_WINRT && \ |
| 15521 SQLITE_THREADSAFE>0 |
| 15522 SQLITE_PRIVATE DWORD sqlite3Win32Wait(HANDLE hObject){ |
| 15523 DWORD rc; |
| 15524 while( (rc = osWaitForSingleObjectEx(hObject, INFINITE, |
| 15525 TRUE))==WAIT_IO_COMPLETION ){} |
| 15526 return rc; |
| 15527 } |
| 15528 #endif |
| 15529 |
| 15530 /* |
| 15531 ** Return true (non-zero) if we are running under WinNT, Win2K, WinXP, |
| 15532 ** or WinCE. Return false (zero) for Win95, Win98, or WinME. |
| 15533 ** |
| 15534 ** Here is an interesting observation: Win95, Win98, and WinME lack |
| 15535 ** the LockFileEx() API. But we can still statically link against that |
| 15536 ** API as long as we don't call it when running Win95/98/ME. A call to |
| 15537 ** this routine is used to determine if the host is Win95/98/ME or |
| 15538 ** WinNT/2K/XP so that we will know whether or not we can safely call |
| 15539 ** the LockFileEx() API. |
| 15540 */ |
| 15541 |
| 15542 #if !defined(SQLITE_WIN32_GETVERSIONEX) || !SQLITE_WIN32_GETVERSIONEX |
| 15543 # define osIsNT() (1) |
| 15544 #elif SQLITE_OS_WINCE || SQLITE_OS_WINRT || !defined(SQLITE_WIN32_HAS_ANSI) |
| 15545 # define osIsNT() (1) |
| 15546 #elif !defined(SQLITE_WIN32_HAS_WIDE) |
| 15547 # define osIsNT() (0) |
| 15548 #else |
| 15549 # define osIsNT() ((sqlite3_os_type==2) || sqlite3_win32_is_nt()) |
| 15550 #endif |
| 15551 |
| 15552 /* |
| 15553 ** This function determines if the machine is running a version of Windows |
| 15554 ** based on the NT kernel. |
| 15555 */ |
| 15556 SQLITE_API int SQLITE_STDCALL sqlite3_win32_is_nt(void){ |
| 15557 #if SQLITE_OS_WINRT |
| 15558 /* |
| 15559 ** NOTE: The WinRT sub-platform is always assumed to be based on the NT |
| 15560 ** kernel. |
| 15561 */ |
| 15562 return 1; |
| 15563 #elif defined(SQLITE_WIN32_GETVERSIONEX) && SQLITE_WIN32_GETVERSIONEX |
| 15564 if( osInterlockedCompareExchange(&sqlite3_os_type, 0, 0)==0 ){ |
| 15565 #if defined(SQLITE_WIN32_HAS_ANSI) |
| 15566 OSVERSIONINFOA sInfo; |
| 15567 sInfo.dwOSVersionInfoSize = sizeof(sInfo); |
| 15568 osGetVersionExA(&sInfo); |
| 15569 osInterlockedCompareExchange(&sqlite3_os_type, |
| 15570 (sInfo.dwPlatformId == VER_PLATFORM_WIN32_NT) ? 2 : 1, 0); |
| 15571 #elif defined(SQLITE_WIN32_HAS_WIDE) |
| 15572 OSVERSIONINFOW sInfo; |
| 15573 sInfo.dwOSVersionInfoSize = sizeof(sInfo); |
| 15574 osGetVersionExW(&sInfo); |
| 15575 osInterlockedCompareExchange(&sqlite3_os_type, |
| 15576 (sInfo.dwPlatformId == VER_PLATFORM_WIN32_NT) ? 2 : 1, 0); |
| 15577 #endif |
| 15578 } |
| 15579 return osInterlockedCompareExchange(&sqlite3_os_type, 2, 2)==2; |
| 15580 #elif SQLITE_TEST |
| 15581 return osInterlockedCompareExchange(&sqlite3_os_type, 2, 2)==2; |
| 15582 #else |
| 15583 /* |
| 15584 ** NOTE: All sub-platforms where the GetVersionEx[AW] functions are |
| 15585 ** deprecated are always assumed to be based on the NT kernel. |
| 15586 */ |
| 15587 return 1; |
| 15588 #endif |
| 15589 } |
| 15590 |
| 15591 #ifdef SQLITE_WIN32_MALLOC |
| 15592 /* |
| 15593 ** Allocate nBytes of memory. |
| 15594 */ |
| 15595 static void *winMemMalloc(int nBytes){ |
| 15596 HANDLE hHeap; |
| 15597 void *p; |
| 15598 |
| 15599 winMemAssertMagic(); |
| 15600 hHeap = winMemGetHeap(); |
| 15601 assert( hHeap!=0 ); |
| 15602 assert( hHeap!=INVALID_HANDLE_VALUE ); |
| 15603 #if !SQLITE_OS_WINRT && defined(SQLITE_WIN32_MALLOC_VALIDATE) |
| 15604 assert( osHeapValidate(hHeap, SQLITE_WIN32_HEAP_FLAGS, NULL) ); |
| 15605 #endif |
| 15606 assert( nBytes>=0 ); |
| 15607 p = osHeapAlloc(hHeap, SQLITE_WIN32_HEAP_FLAGS, (SIZE_T)nBytes); |
| 15608 if( !p ){ |
| 15609 sqlite3_log(SQLITE_NOMEM, "failed to HeapAlloc %u bytes (%lu), heap=%p", |
| 15610 nBytes, osGetLastError(), (void*)hHeap); |
| 15611 } |
| 15612 return p; |
| 15613 } |
| 15614 |
| 15615 /* |
| 15616 ** Free memory. |
| 15617 */ |
| 15618 static void winMemFree(void *pPrior){ |
| 15619 HANDLE hHeap; |
| 15620 |
| 15621 winMemAssertMagic(); |
| 15622 hHeap = winMemGetHeap(); |
| 15623 assert( hHeap!=0 ); |
| 15624 assert( hHeap!=INVALID_HANDLE_VALUE ); |
| 15625 #if !SQLITE_OS_WINRT && defined(SQLITE_WIN32_MALLOC_VALIDATE) |
| 15626 assert( osHeapValidate(hHeap, SQLITE_WIN32_HEAP_FLAGS, pPrior) ); |
| 15627 #endif |
| 15628 if( !pPrior ) return; /* Passing NULL to HeapFree is undefined. */ |
| 15629 if( !osHeapFree(hHeap, SQLITE_WIN32_HEAP_FLAGS, pPrior) ){ |
| 15630 sqlite3_log(SQLITE_NOMEM, "failed to HeapFree block %p (%lu), heap=%p", |
| 15631 pPrior, osGetLastError(), (void*)hHeap); |
| 15632 } |
| 15633 } |
| 15634 |
| 15635 /* |
| 15636 ** Change the size of an existing memory allocation |
| 15637 */ |
| 15638 static void *winMemRealloc(void *pPrior, int nBytes){ |
| 15639 HANDLE hHeap; |
| 15640 void *p; |
| 15641 |
| 15642 winMemAssertMagic(); |
| 15643 hHeap = winMemGetHeap(); |
| 15644 assert( hHeap!=0 ); |
| 15645 assert( hHeap!=INVALID_HANDLE_VALUE ); |
| 15646 #if !SQLITE_OS_WINRT && defined(SQLITE_WIN32_MALLOC_VALIDATE) |
| 15647 assert( osHeapValidate(hHeap, SQLITE_WIN32_HEAP_FLAGS, pPrior) ); |
| 15648 #endif |
| 15649 assert( nBytes>=0 ); |
| 15650 if( !pPrior ){ |
| 15651 p = osHeapAlloc(hHeap, SQLITE_WIN32_HEAP_FLAGS, (SIZE_T)nBytes); |
| 15652 }else{ |
| 15653 p = osHeapReAlloc(hHeap, SQLITE_WIN32_HEAP_FLAGS, pPrior, (SIZE_T)nBytes); |
| 15654 } |
| 15655 if( !p ){ |
| 15656 sqlite3_log(SQLITE_NOMEM, "failed to %s %u bytes (%lu), heap=%p", |
| 15657 pPrior ? "HeapReAlloc" : "HeapAlloc", nBytes, osGetLastError(), |
| 15658 (void*)hHeap); |
| 15659 } |
| 15660 return p; |
| 15661 } |
| 15662 |
| 15663 /* |
| 15664 ** Return the size of an outstanding allocation, in bytes. |
| 15665 */ |
| 15666 static int winMemSize(void *p){ |
| 15667 HANDLE hHeap; |
| 15668 SIZE_T n; |
| 15669 |
| 15670 winMemAssertMagic(); |
| 15671 hHeap = winMemGetHeap(); |
| 15672 assert( hHeap!=0 ); |
| 15673 assert( hHeap!=INVALID_HANDLE_VALUE ); |
| 15674 #if !SQLITE_OS_WINRT && defined(SQLITE_WIN32_MALLOC_VALIDATE) |
| 15675 assert( osHeapValidate(hHeap, SQLITE_WIN32_HEAP_FLAGS, p) ); |
| 15676 #endif |
| 15677 if( !p ) return 0; |
| 15678 n = osHeapSize(hHeap, SQLITE_WIN32_HEAP_FLAGS, p); |
| 15679 if( n==(SIZE_T)-1 ){ |
| 15680 sqlite3_log(SQLITE_NOMEM, "failed to HeapSize block %p (%lu), heap=%p", |
| 15681 p, osGetLastError(), (void*)hHeap); |
| 15682 return 0; |
| 15683 } |
| 15684 return (int)n; |
| 15685 } |
| 15686 |
| 15687 /* |
| 15688 ** Round up a request size to the next valid allocation size. |
| 15689 */ |
| 15690 static int winMemRoundup(int n){ |
| 15691 return n; |
| 15692 } |
| 15693 |
| 15694 /* |
| 15695 ** Initialize this module. |
| 15696 */ |
| 15697 static int winMemInit(void *pAppData){ |
| 15698 winMemData *pWinMemData = (winMemData *)pAppData; |
| 15699 |
| 15700 if( !pWinMemData ) return SQLITE_ERROR; |
| 15701 assert( pWinMemData->magic1==WINMEM_MAGIC1 ); |
| 15702 assert( pWinMemData->magic2==WINMEM_MAGIC2 ); |
| 15703 |
| 15704 #if !SQLITE_OS_WINRT && SQLITE_WIN32_HEAP_CREATE |
| 15705 if( !pWinMemData->hHeap ){ |
| 15706 DWORD dwInitialSize = SQLITE_WIN32_HEAP_INIT_SIZE; |
| 15707 DWORD dwMaximumSize = (DWORD)sqlite3GlobalConfig.nHeap; |
| 15708 if( dwMaximumSize==0 ){ |
| 15709 dwMaximumSize = SQLITE_WIN32_HEAP_MAX_SIZE; |
| 15710 }else if( dwInitialSize>dwMaximumSize ){ |
| 15711 dwInitialSize = dwMaximumSize; |
| 15712 } |
| 15713 pWinMemData->hHeap = osHeapCreate(SQLITE_WIN32_HEAP_FLAGS, |
| 15714 dwInitialSize, dwMaximumSize); |
| 15715 if( !pWinMemData->hHeap ){ |
| 15716 sqlite3_log(SQLITE_NOMEM, |
| 15717 "failed to HeapCreate (%lu), flags=%u, initSize=%lu, maxSize=%lu", |
| 15718 osGetLastError(), SQLITE_WIN32_HEAP_FLAGS, dwInitialSize, |
| 15719 dwMaximumSize); |
| 15720 return SQLITE_NOMEM; |
| 15721 } |
| 15722 pWinMemData->bOwned = TRUE; |
| 15723 assert( pWinMemData->bOwned ); |
| 15724 } |
| 15725 #else |
| 15726 pWinMemData->hHeap = osGetProcessHeap(); |
| 15727 if( !pWinMemData->hHeap ){ |
| 15728 sqlite3_log(SQLITE_NOMEM, |
| 15729 "failed to GetProcessHeap (%lu)", osGetLastError()); |
| 15730 return SQLITE_NOMEM; |
| 15731 } |
| 15732 pWinMemData->bOwned = FALSE; |
| 15733 assert( !pWinMemData->bOwned ); |
| 15734 #endif |
| 15735 assert( pWinMemData->hHeap!=0 ); |
| 15736 assert( pWinMemData->hHeap!=INVALID_HANDLE_VALUE ); |
| 15737 #if !SQLITE_OS_WINRT && defined(SQLITE_WIN32_MALLOC_VALIDATE) |
| 15738 assert( osHeapValidate(pWinMemData->hHeap, SQLITE_WIN32_HEAP_FLAGS, NULL) ); |
| 15739 #endif |
| 15740 return SQLITE_OK; |
| 15741 } |
| 15742 |
| 15743 /* |
| 15744 ** Deinitialize this module. |
| 15745 */ |
| 15746 static void winMemShutdown(void *pAppData){ |
| 15747 winMemData *pWinMemData = (winMemData *)pAppData; |
| 15748 |
| 15749 if( !pWinMemData ) return; |
| 15750 assert( pWinMemData->magic1==WINMEM_MAGIC1 ); |
| 15751 assert( pWinMemData->magic2==WINMEM_MAGIC2 ); |
| 15752 |
| 15753 if( pWinMemData->hHeap ){ |
| 15754 assert( pWinMemData->hHeap!=INVALID_HANDLE_VALUE ); |
| 15755 #if !SQLITE_OS_WINRT && defined(SQLITE_WIN32_MALLOC_VALIDATE) |
| 15756 assert( osHeapValidate(pWinMemData->hHeap, SQLITE_WIN32_HEAP_FLAGS, NULL) ); |
| 15757 #endif |
| 15758 if( pWinMemData->bOwned ){ |
| 15759 if( !osHeapDestroy(pWinMemData->hHeap) ){ |
| 15760 sqlite3_log(SQLITE_NOMEM, "failed to HeapDestroy (%lu), heap=%p", |
| 15761 osGetLastError(), (void*)pWinMemData->hHeap); |
| 15762 } |
| 15763 pWinMemData->bOwned = FALSE; |
| 15764 } |
| 15765 pWinMemData->hHeap = NULL; |
| 15766 } |
| 15767 } |
| 15768 |
| 15769 /* |
| 15770 ** Populate the low-level memory allocation function pointers in |
| 15771 ** sqlite3GlobalConfig.m with pointers to the routines in this file. The |
| 15772 ** arguments specify the block of memory to manage. |
| 15773 ** |
| 15774 ** This routine is only called by sqlite3_config(), and therefore |
| 15775 ** is not required to be threadsafe (it is not). |
| 15776 */ |
| 15777 SQLITE_PRIVATE const sqlite3_mem_methods *sqlite3MemGetWin32(void){ |
| 15778 static const sqlite3_mem_methods winMemMethods = { |
| 15779 winMemMalloc, |
| 15780 winMemFree, |
| 15781 winMemRealloc, |
| 15782 winMemSize, |
| 15783 winMemRoundup, |
| 15784 winMemInit, |
| 15785 winMemShutdown, |
| 15786 &win_mem_data |
| 15787 }; |
| 15788 return &winMemMethods; |
| 15789 } |
| 15790 |
| 15791 SQLITE_PRIVATE void sqlite3MemSetDefault(void){ |
| 15792 sqlite3_config(SQLITE_CONFIG_MALLOC, sqlite3MemGetWin32()); |
| 15793 } |
| 15794 #endif /* SQLITE_WIN32_MALLOC */ |
| 15795 |
| 15796 /* |
| 15797 ** Convert a UTF-8 string to Microsoft Unicode (UTF-16?). |
| 15798 ** |
| 15799 ** Space to hold the returned string is obtained from malloc. |
| 15800 */ |
| 15801 static LPWSTR winUtf8ToUnicode(const char *zFilename){ |
| 15802 int nChar; |
| 15803 LPWSTR zWideFilename; |
| 15804 |
| 15805 nChar = osMultiByteToWideChar(CP_UTF8, 0, zFilename, -1, NULL, 0); |
| 15806 if( nChar==0 ){ |
| 15807 return 0; |
| 15808 } |
| 15809 zWideFilename = sqlite3MallocZero( nChar*sizeof(zWideFilename[0]) ); |
| 15810 if( zWideFilename==0 ){ |
| 15811 return 0; |
| 15812 } |
| 15813 nChar = osMultiByteToWideChar(CP_UTF8, 0, zFilename, -1, zWideFilename, |
| 15814 nChar); |
| 15815 if( nChar==0 ){ |
| 15816 sqlite3_free(zWideFilename); |
| 15817 zWideFilename = 0; |
| 15818 } |
| 15819 return zWideFilename; |
| 15820 } |
| 15821 |
| 15822 /* |
| 15823 ** Convert Microsoft Unicode to UTF-8. Space to hold the returned string is |
| 15824 ** obtained from sqlite3_malloc(). |
| 15825 */ |
| 15826 static char *winUnicodeToUtf8(LPCWSTR zWideFilename){ |
| 15827 int nByte; |
| 15828 char *zFilename; |
| 15829 |
| 15830 nByte = osWideCharToMultiByte(CP_UTF8, 0, zWideFilename, -1, 0, 0, 0, 0); |
| 15831 if( nByte == 0 ){ |
| 15832 return 0; |
| 15833 } |
| 15834 zFilename = sqlite3MallocZero( nByte ); |
| 15835 if( zFilename==0 ){ |
| 15836 return 0; |
| 15837 } |
| 15838 nByte = osWideCharToMultiByte(CP_UTF8, 0, zWideFilename, -1, zFilename, nByte, |
| 15839 0, 0); |
| 15840 if( nByte == 0 ){ |
| 15841 sqlite3_free(zFilename); |
| 15842 zFilename = 0; |
| 15843 } |
| 15844 return zFilename; |
| 15845 } |
| 15846 |
| 15847 /* |
| 15848 ** Convert an ANSI string to Microsoft Unicode, based on the |
| 15849 ** current codepage settings for file apis. |
| 15850 ** |
| 15851 ** Space to hold the returned string is obtained |
| 15852 ** from sqlite3_malloc. |
| 15853 */ |
| 15854 static LPWSTR winMbcsToUnicode(const char *zFilename){ |
| 15855 int nByte; |
| 15856 LPWSTR zMbcsFilename; |
| 15857 int codepage = osAreFileApisANSI() ? CP_ACP : CP_OEMCP; |
| 15858 |
| 15859 nByte = osMultiByteToWideChar(codepage, 0, zFilename, -1, NULL, |
| 15860 0)*sizeof(WCHAR); |
| 15861 if( nByte==0 ){ |
| 15862 return 0; |
| 15863 } |
| 15864 zMbcsFilename = sqlite3MallocZero( nByte*sizeof(zMbcsFilename[0]) ); |
| 15865 if( zMbcsFilename==0 ){ |
| 15866 return 0; |
| 15867 } |
| 15868 nByte = osMultiByteToWideChar(codepage, 0, zFilename, -1, zMbcsFilename, |
| 15869 nByte); |
| 15870 if( nByte==0 ){ |
| 15871 sqlite3_free(zMbcsFilename); |
| 15872 zMbcsFilename = 0; |
| 15873 } |
| 15874 return zMbcsFilename; |
| 15875 } |
| 15876 |
| 15877 /* |
| 15878 ** Convert Microsoft Unicode to multi-byte character string, based on the |
| 15879 ** user's ANSI codepage. |
| 15880 ** |
| 15881 ** Space to hold the returned string is obtained from |
| 15882 ** sqlite3_malloc(). |
| 15883 */ |
| 15884 static char *winUnicodeToMbcs(LPCWSTR zWideFilename){ |
| 15885 int nByte; |
| 15886 char *zFilename; |
| 15887 int codepage = osAreFileApisANSI() ? CP_ACP : CP_OEMCP; |
| 15888 |
| 15889 nByte = osWideCharToMultiByte(codepage, 0, zWideFilename, -1, 0, 0, 0, 0); |
| 15890 if( nByte == 0 ){ |
| 15891 return 0; |
| 15892 } |
| 15893 zFilename = sqlite3MallocZero( nByte ); |
| 15894 if( zFilename==0 ){ |
| 15895 return 0; |
| 15896 } |
| 15897 nByte = osWideCharToMultiByte(codepage, 0, zWideFilename, -1, zFilename, |
| 15898 nByte, 0, 0); |
| 15899 if( nByte == 0 ){ |
| 15900 sqlite3_free(zFilename); |
| 15901 zFilename = 0; |
| 15902 } |
| 15903 return zFilename; |
| 15904 } |
| 15905 |
| 15906 /* |
| 15907 ** Convert multibyte character string to UTF-8. Space to hold the |
| 15908 ** returned string is obtained from sqlite3_malloc(). |
| 15909 */ |
| 15910 SQLITE_API char *SQLITE_STDCALL sqlite3_win32_mbcs_to_utf8(const char *zFilename
){ |
| 15911 char *zFilenameUtf8; |
| 15912 LPWSTR zTmpWide; |
| 15913 |
| 15914 zTmpWide = winMbcsToUnicode(zFilename); |
| 15915 if( zTmpWide==0 ){ |
| 15916 return 0; |
| 15917 } |
| 15918 zFilenameUtf8 = winUnicodeToUtf8(zTmpWide); |
| 15919 sqlite3_free(zTmpWide); |
| 15920 return zFilenameUtf8; |
| 15921 } |
| 15922 |
| 15923 /* |
| 15924 ** Convert UTF-8 to multibyte character string. Space to hold the |
| 15925 ** returned string is obtained from sqlite3_malloc(). |
| 15926 */ |
| 15927 SQLITE_API char *SQLITE_STDCALL sqlite3_win32_utf8_to_mbcs(const char *zFilename
){ |
| 15928 char *zFilenameMbcs; |
| 15929 LPWSTR zTmpWide; |
| 15930 |
| 15931 zTmpWide = winUtf8ToUnicode(zFilename); |
| 15932 if( zTmpWide==0 ){ |
| 15933 return 0; |
| 15934 } |
| 15935 zFilenameMbcs = winUnicodeToMbcs(zTmpWide); |
| 15936 sqlite3_free(zTmpWide); |
| 15937 return zFilenameMbcs; |
| 15938 } |
| 15939 |
| 15940 /* |
| 15941 ** This function sets the data directory or the temporary directory based on |
| 15942 ** the provided arguments. The type argument must be 1 in order to set the |
| 15943 ** data directory or 2 in order to set the temporary directory. The zValue |
| 15944 ** argument is the name of the directory to use. The return value will be |
| 15945 ** SQLITE_OK if successful. |
| 15946 */ |
| 15947 SQLITE_API int SQLITE_STDCALL sqlite3_win32_set_directory(DWORD type, LPCWSTR zV
alue){ |
| 15948 char **ppDirectory = 0; |
| 15949 #ifndef SQLITE_OMIT_AUTOINIT |
| 15950 int rc = sqlite3_initialize(); |
| 15951 if( rc ) return rc; |
| 15952 #endif |
| 15953 if( type==SQLITE_WIN32_DATA_DIRECTORY_TYPE ){ |
| 15954 ppDirectory = &sqlite3_data_directory; |
| 15955 }else if( type==SQLITE_WIN32_TEMP_DIRECTORY_TYPE ){ |
| 15956 ppDirectory = &sqlite3_temp_directory; |
| 15957 } |
| 15958 assert( !ppDirectory || type==SQLITE_WIN32_DATA_DIRECTORY_TYPE |
| 15959 || type==SQLITE_WIN32_TEMP_DIRECTORY_TYPE |
| 15960 ); |
| 15961 assert( !ppDirectory || sqlite3MemdebugHasType(*ppDirectory, MEMTYPE_HEAP) ); |
| 15962 if( ppDirectory ){ |
| 15963 char *zValueUtf8 = 0; |
| 15964 if( zValue && zValue[0] ){ |
| 15965 zValueUtf8 = winUnicodeToUtf8(zValue); |
| 15966 if ( zValueUtf8==0 ){ |
| 15967 return SQLITE_NOMEM; |
| 15968 } |
| 15969 } |
| 15970 sqlite3_free(*ppDirectory); |
| 15971 *ppDirectory = zValueUtf8; |
| 15972 return SQLITE_OK; |
| 15973 } |
| 15974 return SQLITE_ERROR; |
| 15975 } |
| 15976 |
| 15977 /* |
| 15978 ** The return value of winGetLastErrorMsg |
| 15979 ** is zero if the error message fits in the buffer, or non-zero |
| 15980 ** otherwise (if the message was truncated). |
| 15981 */ |
| 15982 static int winGetLastErrorMsg(DWORD lastErrno, int nBuf, char *zBuf){ |
| 15983 /* FormatMessage returns 0 on failure. Otherwise it |
| 15984 ** returns the number of TCHARs written to the output |
| 15985 ** buffer, excluding the terminating null char. |
| 15986 */ |
| 15987 DWORD dwLen = 0; |
| 15988 char *zOut = 0; |
| 15989 |
| 15990 if( osIsNT() ){ |
| 15991 #if SQLITE_OS_WINRT |
| 15992 WCHAR zTempWide[SQLITE_WIN32_MAX_ERRMSG_CHARS+1]; |
| 15993 dwLen = osFormatMessageW(FORMAT_MESSAGE_FROM_SYSTEM | |
| 15994 FORMAT_MESSAGE_IGNORE_INSERTS, |
| 15995 NULL, |
| 15996 lastErrno, |
| 15997 0, |
| 15998 zTempWide, |
| 15999 SQLITE_WIN32_MAX_ERRMSG_CHARS, |
| 16000 0); |
| 16001 #else |
| 16002 LPWSTR zTempWide = NULL; |
| 16003 dwLen = osFormatMessageW(FORMAT_MESSAGE_ALLOCATE_BUFFER | |
| 16004 FORMAT_MESSAGE_FROM_SYSTEM | |
| 16005 FORMAT_MESSAGE_IGNORE_INSERTS, |
| 16006 NULL, |
| 16007 lastErrno, |
| 16008 0, |
| 16009 (LPWSTR) &zTempWide, |
| 16010 0, |
| 16011 0); |
| 16012 #endif |
| 16013 if( dwLen > 0 ){ |
| 16014 /* allocate a buffer and convert to UTF8 */ |
| 16015 sqlite3BeginBenignMalloc(); |
| 16016 zOut = winUnicodeToUtf8(zTempWide); |
| 16017 sqlite3EndBenignMalloc(); |
| 16018 #if !SQLITE_OS_WINRT |
| 16019 /* free the system buffer allocated by FormatMessage */ |
| 16020 osLocalFree(zTempWide); |
| 16021 #endif |
| 16022 } |
| 16023 } |
| 16024 #ifdef SQLITE_WIN32_HAS_ANSI |
| 16025 else{ |
| 16026 char *zTemp = NULL; |
| 16027 dwLen = osFormatMessageA(FORMAT_MESSAGE_ALLOCATE_BUFFER | |
| 16028 FORMAT_MESSAGE_FROM_SYSTEM | |
| 16029 FORMAT_MESSAGE_IGNORE_INSERTS, |
| 16030 NULL, |
| 16031 lastErrno, |
| 16032 0, |
| 16033 (LPSTR) &zTemp, |
| 16034 0, |
| 16035 0); |
| 16036 if( dwLen > 0 ){ |
| 16037 /* allocate a buffer and convert to UTF8 */ |
| 16038 sqlite3BeginBenignMalloc(); |
| 16039 zOut = sqlite3_win32_mbcs_to_utf8(zTemp); |
| 16040 sqlite3EndBenignMalloc(); |
| 16041 /* free the system buffer allocated by FormatMessage */ |
| 16042 osLocalFree(zTemp); |
| 16043 } |
| 16044 } |
| 16045 #endif |
| 16046 if( 0 == dwLen ){ |
| 16047 sqlite3_snprintf(nBuf, zBuf, "OsError 0x%lx (%lu)", lastErrno, lastErrno); |
| 16048 }else{ |
| 16049 /* copy a maximum of nBuf chars to output buffer */ |
| 16050 sqlite3_snprintf(nBuf, zBuf, "%s", zOut); |
| 16051 /* free the UTF8 buffer */ |
| 16052 sqlite3_free(zOut); |
| 16053 } |
| 16054 return 0; |
| 16055 } |
| 16056 |
| 16057 /* |
| 16058 ** |
| 16059 ** This function - winLogErrorAtLine() - is only ever called via the macro |
| 16060 ** winLogError(). |
| 16061 ** |
| 16062 ** This routine is invoked after an error occurs in an OS function. |
| 16063 ** It logs a message using sqlite3_log() containing the current value of |
| 16064 ** error code and, if possible, the human-readable equivalent from |
| 16065 ** FormatMessage. |
| 16066 ** |
| 16067 ** The first argument passed to the macro should be the error code that |
| 16068 ** will be returned to SQLite (e.g. SQLITE_IOERR_DELETE, SQLITE_CANTOPEN). |
| 16069 ** The two subsequent arguments should be the name of the OS function that |
| 16070 ** failed and the associated file-system path, if any. |
| 16071 */ |
| 16072 #define winLogError(a,b,c,d) winLogErrorAtLine(a,b,c,d,__LINE__) |
| 16073 static int winLogErrorAtLine( |
| 16074 int errcode, /* SQLite error code */ |
| 16075 DWORD lastErrno, /* Win32 last error */ |
| 16076 const char *zFunc, /* Name of OS function that failed */ |
| 16077 const char *zPath, /* File path associated with error */ |
| 16078 int iLine /* Source line number where error occurred */ |
| 16079 ){ |
| 16080 char zMsg[500]; /* Human readable error text */ |
| 16081 int i; /* Loop counter */ |
| 16082 |
| 16083 zMsg[0] = 0; |
| 16084 winGetLastErrorMsg(lastErrno, sizeof(zMsg), zMsg); |
| 16085 assert( errcode!=SQLITE_OK ); |
| 16086 if( zPath==0 ) zPath = ""; |
| 16087 for(i=0; zMsg[i] && zMsg[i]!='\r' && zMsg[i]!='\n'; i++){} |
| 16088 zMsg[i] = 0; |
| 16089 sqlite3_log(errcode, |
| 16090 "os_win.c:%d: (%lu) %s(%s) - %s", |
| 16091 iLine, lastErrno, zFunc, zPath, zMsg |
| 16092 ); |
| 16093 |
| 16094 return errcode; |
| 16095 } |
| 16096 |
| 16097 /* |
| 16098 ** The number of times that a ReadFile(), WriteFile(), and DeleteFile() |
| 16099 ** will be retried following a locking error - probably caused by |
| 16100 ** antivirus software. Also the initial delay before the first retry. |
| 16101 ** The delay increases linearly with each retry. |
| 16102 */ |
| 16103 #ifndef SQLITE_WIN32_IOERR_RETRY |
| 16104 # define SQLITE_WIN32_IOERR_RETRY 10 |
| 16105 #endif |
| 16106 #ifndef SQLITE_WIN32_IOERR_RETRY_DELAY |
| 16107 # define SQLITE_WIN32_IOERR_RETRY_DELAY 25 |
| 16108 #endif |
| 16109 static int winIoerrRetry = SQLITE_WIN32_IOERR_RETRY; |
| 16110 static int winIoerrRetryDelay = SQLITE_WIN32_IOERR_RETRY_DELAY; |
| 16111 |
| 16112 /* |
| 16113 ** The "winIoerrCanRetry1" macro is used to determine if a particular I/O |
| 16114 ** error code obtained via GetLastError() is eligible to be retried. It |
| 16115 ** must accept the error code DWORD as its only argument and should return |
| 16116 ** non-zero if the error code is transient in nature and the operation |
| 16117 ** responsible for generating the original error might succeed upon being |
| 16118 ** retried. The argument to this macro should be a variable. |
| 16119 ** |
| 16120 ** Additionally, a macro named "winIoerrCanRetry2" may be defined. If it |
| 16121 ** is defined, it will be consulted only when the macro "winIoerrCanRetry1" |
| 16122 ** returns zero. The "winIoerrCanRetry2" macro is completely optional and |
| 16123 ** may be used to include additional error codes in the set that should |
| 16124 ** result in the failing I/O operation being retried by the caller. If |
| 16125 ** defined, the "winIoerrCanRetry2" macro must exhibit external semantics |
| 16126 ** identical to those of the "winIoerrCanRetry1" macro. |
| 16127 */ |
| 16128 #if !defined(winIoerrCanRetry1) |
| 16129 #define winIoerrCanRetry1(a) (((a)==ERROR_ACCESS_DENIED) || \ |
| 16130 ((a)==ERROR_SHARING_VIOLATION) || \ |
| 16131 ((a)==ERROR_LOCK_VIOLATION) || \ |
| 16132 ((a)==ERROR_DEV_NOT_EXIST) || \ |
| 16133 ((a)==ERROR_NETNAME_DELETED) || \ |
| 16134 ((a)==ERROR_SEM_TIMEOUT) || \ |
| 16135 ((a)==ERROR_NETWORK_UNREACHABLE)) |
| 16136 #endif |
| 16137 |
| 16138 /* |
| 16139 ** If a ReadFile() or WriteFile() error occurs, invoke this routine |
| 16140 ** to see if it should be retried. Return TRUE to retry. Return FALSE |
| 16141 ** to give up with an error. |
| 16142 */ |
| 16143 static int winRetryIoerr(int *pnRetry, DWORD *pError){ |
| 16144 DWORD e = osGetLastError(); |
| 16145 if( *pnRetry>=winIoerrRetry ){ |
| 16146 if( pError ){ |
| 16147 *pError = e; |
| 16148 } |
| 16149 return 0; |
| 16150 } |
| 16151 if( winIoerrCanRetry1(e) ){ |
| 16152 sqlite3_win32_sleep(winIoerrRetryDelay*(1+*pnRetry)); |
| 16153 ++*pnRetry; |
| 16154 return 1; |
| 16155 } |
| 16156 #if defined(winIoerrCanRetry2) |
| 16157 else if( winIoerrCanRetry2(e) ){ |
| 16158 sqlite3_win32_sleep(winIoerrRetryDelay*(1+*pnRetry)); |
| 16159 ++*pnRetry; |
| 16160 return 1; |
| 16161 } |
| 16162 #endif |
| 16163 if( pError ){ |
| 16164 *pError = e; |
| 16165 } |
| 16166 return 0; |
| 16167 } |
| 16168 |
| 16169 /* |
| 16170 ** Log a I/O error retry episode. |
| 16171 */ |
| 16172 static void winLogIoerr(int nRetry, int lineno){ |
| 16173 if( nRetry ){ |
| 16174 sqlite3_log(SQLITE_NOTICE, |
| 16175 "delayed %dms for lock/sharing conflict at line %d", |
| 16176 winIoerrRetryDelay*nRetry*(nRetry+1)/2, lineno |
| 16177 ); |
| 16178 } |
| 16179 } |
| 16180 |
| 16181 #if SQLITE_OS_WINCE |
| 16182 /************************************************************************* |
| 16183 ** This section contains code for WinCE only. |
| 16184 */ |
| 16185 #if !defined(SQLITE_MSVC_LOCALTIME_API) || !SQLITE_MSVC_LOCALTIME_API |
| 16186 /* |
| 16187 ** The MSVC CRT on Windows CE may not have a localtime() function. So |
| 16188 ** create a substitute. |
| 16189 */ |
| 16190 /* #include <time.h> */ |
| 16191 struct tm *__cdecl localtime(const time_t *t) |
| 16192 { |
| 16193 static struct tm y; |
| 16194 FILETIME uTm, lTm; |
| 16195 SYSTEMTIME pTm; |
| 16196 sqlite3_int64 t64; |
| 16197 t64 = *t; |
| 16198 t64 = (t64 + 11644473600)*10000000; |
| 16199 uTm.dwLowDateTime = (DWORD)(t64 & 0xFFFFFFFF); |
| 16200 uTm.dwHighDateTime= (DWORD)(t64 >> 32); |
| 16201 osFileTimeToLocalFileTime(&uTm,&lTm); |
| 16202 osFileTimeToSystemTime(&lTm,&pTm); |
| 16203 y.tm_year = pTm.wYear - 1900; |
| 16204 y.tm_mon = pTm.wMonth - 1; |
| 16205 y.tm_wday = pTm.wDayOfWeek; |
| 16206 y.tm_mday = pTm.wDay; |
| 16207 y.tm_hour = pTm.wHour; |
| 16208 y.tm_min = pTm.wMinute; |
| 16209 y.tm_sec = pTm.wSecond; |
| 16210 return &y; |
| 16211 } |
| 16212 #endif |
| 16213 |
| 16214 #define HANDLE_TO_WINFILE(a) (winFile*)&((char*)a)[-(int)offsetof(winFile,h)] |
| 16215 |
| 16216 /* |
| 16217 ** Acquire a lock on the handle h |
| 16218 */ |
| 16219 static void winceMutexAcquire(HANDLE h){ |
| 16220 DWORD dwErr; |
| 16221 do { |
| 16222 dwErr = osWaitForSingleObject(h, INFINITE); |
| 16223 } while (dwErr != WAIT_OBJECT_0 && dwErr != WAIT_ABANDONED); |
| 16224 } |
| 16225 /* |
| 16226 ** Release a lock acquired by winceMutexAcquire() |
| 16227 */ |
| 16228 #define winceMutexRelease(h) ReleaseMutex(h) |
| 16229 |
| 16230 /* |
| 16231 ** Create the mutex and shared memory used for locking in the file |
| 16232 ** descriptor pFile |
| 16233 */ |
| 16234 static int winceCreateLock(const char *zFilename, winFile *pFile){ |
| 16235 LPWSTR zTok; |
| 16236 LPWSTR zName; |
| 16237 DWORD lastErrno; |
| 16238 BOOL bLogged = FALSE; |
| 16239 BOOL bInit = TRUE; |
| 16240 |
| 16241 zName = winUtf8ToUnicode(zFilename); |
| 16242 if( zName==0 ){ |
| 16243 /* out of memory */ |
| 16244 return SQLITE_IOERR_NOMEM; |
| 16245 } |
| 16246 |
| 16247 /* Initialize the local lockdata */ |
| 16248 memset(&pFile->local, 0, sizeof(pFile->local)); |
| 16249 |
| 16250 /* Replace the backslashes from the filename and lowercase it |
| 16251 ** to derive a mutex name. */ |
| 16252 zTok = osCharLowerW(zName); |
| 16253 for (;*zTok;zTok++){ |
| 16254 if (*zTok == '\\') *zTok = '_'; |
| 16255 } |
| 16256 |
| 16257 /* Create/open the named mutex */ |
| 16258 pFile->hMutex = osCreateMutexW(NULL, FALSE, zName); |
| 16259 if (!pFile->hMutex){ |
| 16260 pFile->lastErrno = osGetLastError(); |
| 16261 sqlite3_free(zName); |
| 16262 return winLogError(SQLITE_IOERR, pFile->lastErrno, |
| 16263 "winceCreateLock1", zFilename); |
| 16264 } |
| 16265 |
| 16266 /* Acquire the mutex before continuing */ |
| 16267 winceMutexAcquire(pFile->hMutex); |
| 16268 |
| 16269 /* Since the names of named mutexes, semaphores, file mappings etc are |
| 16270 ** case-sensitive, take advantage of that by uppercasing the mutex name |
| 16271 ** and using that as the shared filemapping name. |
| 16272 */ |
| 16273 osCharUpperW(zName); |
| 16274 pFile->hShared = osCreateFileMappingW(INVALID_HANDLE_VALUE, NULL, |
| 16275 PAGE_READWRITE, 0, sizeof(winceLock), |
| 16276 zName); |
| 16277 |
| 16278 /* Set a flag that indicates we're the first to create the memory so it |
| 16279 ** must be zero-initialized */ |
| 16280 lastErrno = osGetLastError(); |
| 16281 if (lastErrno == ERROR_ALREADY_EXISTS){ |
| 16282 bInit = FALSE; |
| 16283 } |
| 16284 |
| 16285 sqlite3_free(zName); |
| 16286 |
| 16287 /* If we succeeded in making the shared memory handle, map it. */ |
| 16288 if( pFile->hShared ){ |
| 16289 pFile->shared = (winceLock*)osMapViewOfFile(pFile->hShared, |
| 16290 FILE_MAP_READ|FILE_MAP_WRITE, 0, 0, sizeof(winceLock)); |
| 16291 /* If mapping failed, close the shared memory handle and erase it */ |
| 16292 if( !pFile->shared ){ |
| 16293 pFile->lastErrno = osGetLastError(); |
| 16294 winLogError(SQLITE_IOERR, pFile->lastErrno, |
| 16295 "winceCreateLock2", zFilename); |
| 16296 bLogged = TRUE; |
| 16297 osCloseHandle(pFile->hShared); |
| 16298 pFile->hShared = NULL; |
| 16299 } |
| 16300 } |
| 16301 |
| 16302 /* If shared memory could not be created, then close the mutex and fail */ |
| 16303 if( pFile->hShared==NULL ){ |
| 16304 if( !bLogged ){ |
| 16305 pFile->lastErrno = lastErrno; |
| 16306 winLogError(SQLITE_IOERR, pFile->lastErrno, |
| 16307 "winceCreateLock3", zFilename); |
| 16308 bLogged = TRUE; |
| 16309 } |
| 16310 winceMutexRelease(pFile->hMutex); |
| 16311 osCloseHandle(pFile->hMutex); |
| 16312 pFile->hMutex = NULL; |
| 16313 return SQLITE_IOERR; |
| 16314 } |
| 16315 |
| 16316 /* Initialize the shared memory if we're supposed to */ |
| 16317 if( bInit ){ |
| 16318 memset(pFile->shared, 0, sizeof(winceLock)); |
| 16319 } |
| 16320 |
| 16321 winceMutexRelease(pFile->hMutex); |
| 16322 return SQLITE_OK; |
| 16323 } |
| 16324 |
| 16325 /* |
| 16326 ** Destroy the part of winFile that deals with wince locks |
| 16327 */ |
| 16328 static void winceDestroyLock(winFile *pFile){ |
| 16329 if (pFile->hMutex){ |
| 16330 /* Acquire the mutex */ |
| 16331 winceMutexAcquire(pFile->hMutex); |
| 16332 |
| 16333 /* The following blocks should probably assert in debug mode, but they |
| 16334 are to cleanup in case any locks remained open */ |
| 16335 if (pFile->local.nReaders){ |
| 16336 pFile->shared->nReaders --; |
| 16337 } |
| 16338 if (pFile->local.bReserved){ |
| 16339 pFile->shared->bReserved = FALSE; |
| 16340 } |
| 16341 if (pFile->local.bPending){ |
| 16342 pFile->shared->bPending = FALSE; |
| 16343 } |
| 16344 if (pFile->local.bExclusive){ |
| 16345 pFile->shared->bExclusive = FALSE; |
| 16346 } |
| 16347 |
| 16348 /* De-reference and close our copy of the shared memory handle */ |
| 16349 osUnmapViewOfFile(pFile->shared); |
| 16350 osCloseHandle(pFile->hShared); |
| 16351 |
| 16352 /* Done with the mutex */ |
| 16353 winceMutexRelease(pFile->hMutex); |
| 16354 osCloseHandle(pFile->hMutex); |
| 16355 pFile->hMutex = NULL; |
| 16356 } |
| 16357 } |
| 16358 |
| 16359 /* |
| 16360 ** An implementation of the LockFile() API of Windows for CE |
| 16361 */ |
| 16362 static BOOL winceLockFile( |
| 16363 LPHANDLE phFile, |
| 16364 DWORD dwFileOffsetLow, |
| 16365 DWORD dwFileOffsetHigh, |
| 16366 DWORD nNumberOfBytesToLockLow, |
| 16367 DWORD nNumberOfBytesToLockHigh |
| 16368 ){ |
| 16369 winFile *pFile = HANDLE_TO_WINFILE(phFile); |
| 16370 BOOL bReturn = FALSE; |
| 16371 |
| 16372 UNUSED_PARAMETER(dwFileOffsetHigh); |
| 16373 UNUSED_PARAMETER(nNumberOfBytesToLockHigh); |
| 16374 |
| 16375 if (!pFile->hMutex) return TRUE; |
| 16376 winceMutexAcquire(pFile->hMutex); |
| 16377 |
| 16378 /* Wanting an exclusive lock? */ |
| 16379 if (dwFileOffsetLow == (DWORD)SHARED_FIRST |
| 16380 && nNumberOfBytesToLockLow == (DWORD)SHARED_SIZE){ |
| 16381 if (pFile->shared->nReaders == 0 && pFile->shared->bExclusive == 0){ |
| 16382 pFile->shared->bExclusive = TRUE; |
| 16383 pFile->local.bExclusive = TRUE; |
| 16384 bReturn = TRUE; |
| 16385 } |
| 16386 } |
| 16387 |
| 16388 /* Want a read-only lock? */ |
| 16389 else if (dwFileOffsetLow == (DWORD)SHARED_FIRST && |
| 16390 nNumberOfBytesToLockLow == 1){ |
| 16391 if (pFile->shared->bExclusive == 0){ |
| 16392 pFile->local.nReaders ++; |
| 16393 if (pFile->local.nReaders == 1){ |
| 16394 pFile->shared->nReaders ++; |
| 16395 } |
| 16396 bReturn = TRUE; |
| 16397 } |
| 16398 } |
| 16399 |
| 16400 /* Want a pending lock? */ |
| 16401 else if (dwFileOffsetLow == (DWORD)PENDING_BYTE |
| 16402 && nNumberOfBytesToLockLow == 1){ |
| 16403 /* If no pending lock has been acquired, then acquire it */ |
| 16404 if (pFile->shared->bPending == 0) { |
| 16405 pFile->shared->bPending = TRUE; |
| 16406 pFile->local.bPending = TRUE; |
| 16407 bReturn = TRUE; |
| 16408 } |
| 16409 } |
| 16410 |
| 16411 /* Want a reserved lock? */ |
| 16412 else if (dwFileOffsetLow == (DWORD)RESERVED_BYTE |
| 16413 && nNumberOfBytesToLockLow == 1){ |
| 16414 if (pFile->shared->bReserved == 0) { |
| 16415 pFile->shared->bReserved = TRUE; |
| 16416 pFile->local.bReserved = TRUE; |
| 16417 bReturn = TRUE; |
| 16418 } |
| 16419 } |
| 16420 |
| 16421 winceMutexRelease(pFile->hMutex); |
| 16422 return bReturn; |
| 16423 } |
| 16424 |
| 16425 /* |
| 16426 ** An implementation of the UnlockFile API of Windows for CE |
| 16427 */ |
| 16428 static BOOL winceUnlockFile( |
| 16429 LPHANDLE phFile, |
| 16430 DWORD dwFileOffsetLow, |
| 16431 DWORD dwFileOffsetHigh, |
| 16432 DWORD nNumberOfBytesToUnlockLow, |
| 16433 DWORD nNumberOfBytesToUnlockHigh |
| 16434 ){ |
| 16435 winFile *pFile = HANDLE_TO_WINFILE(phFile); |
| 16436 BOOL bReturn = FALSE; |
| 16437 |
| 16438 UNUSED_PARAMETER(dwFileOffsetHigh); |
| 16439 UNUSED_PARAMETER(nNumberOfBytesToUnlockHigh); |
| 16440 |
| 16441 if (!pFile->hMutex) return TRUE; |
| 16442 winceMutexAcquire(pFile->hMutex); |
| 16443 |
| 16444 /* Releasing a reader lock or an exclusive lock */ |
| 16445 if (dwFileOffsetLow == (DWORD)SHARED_FIRST){ |
| 16446 /* Did we have an exclusive lock? */ |
| 16447 if (pFile->local.bExclusive){ |
| 16448 assert(nNumberOfBytesToUnlockLow == (DWORD)SHARED_SIZE); |
| 16449 pFile->local.bExclusive = FALSE; |
| 16450 pFile->shared->bExclusive = FALSE; |
| 16451 bReturn = TRUE; |
| 16452 } |
| 16453 |
| 16454 /* Did we just have a reader lock? */ |
| 16455 else if (pFile->local.nReaders){ |
| 16456 assert(nNumberOfBytesToUnlockLow == (DWORD)SHARED_SIZE |
| 16457 || nNumberOfBytesToUnlockLow == 1); |
| 16458 pFile->local.nReaders --; |
| 16459 if (pFile->local.nReaders == 0) |
| 16460 { |
| 16461 pFile->shared->nReaders --; |
| 16462 } |
| 16463 bReturn = TRUE; |
| 16464 } |
| 16465 } |
| 16466 |
| 16467 /* Releasing a pending lock */ |
| 16468 else if (dwFileOffsetLow == (DWORD)PENDING_BYTE |
| 16469 && nNumberOfBytesToUnlockLow == 1){ |
| 16470 if (pFile->local.bPending){ |
| 16471 pFile->local.bPending = FALSE; |
| 16472 pFile->shared->bPending = FALSE; |
| 16473 bReturn = TRUE; |
| 16474 } |
| 16475 } |
| 16476 /* Releasing a reserved lock */ |
| 16477 else if (dwFileOffsetLow == (DWORD)RESERVED_BYTE |
| 16478 && nNumberOfBytesToUnlockLow == 1){ |
| 16479 if (pFile->local.bReserved) { |
| 16480 pFile->local.bReserved = FALSE; |
| 16481 pFile->shared->bReserved = FALSE; |
| 16482 bReturn = TRUE; |
| 16483 } |
| 16484 } |
| 16485 |
| 16486 winceMutexRelease(pFile->hMutex); |
| 16487 return bReturn; |
| 16488 } |
| 16489 /* |
| 16490 ** End of the special code for wince |
| 16491 *****************************************************************************/ |
| 16492 #endif /* SQLITE_OS_WINCE */ |
| 16493 |
| 16494 /* |
| 16495 ** Lock a file region. |
| 16496 */ |
| 16497 static BOOL winLockFile( |
| 16498 LPHANDLE phFile, |
| 16499 DWORD flags, |
| 16500 DWORD offsetLow, |
| 16501 DWORD offsetHigh, |
| 16502 DWORD numBytesLow, |
| 16503 DWORD numBytesHigh |
| 16504 ){ |
| 16505 #if SQLITE_OS_WINCE |
| 16506 /* |
| 16507 ** NOTE: Windows CE is handled differently here due its lack of the Win32 |
| 16508 ** API LockFile. |
| 16509 */ |
| 16510 return winceLockFile(phFile, offsetLow, offsetHigh, |
| 16511 numBytesLow, numBytesHigh); |
| 16512 #else |
| 16513 if( osIsNT() ){ |
| 16514 OVERLAPPED ovlp; |
| 16515 memset(&ovlp, 0, sizeof(OVERLAPPED)); |
| 16516 ovlp.Offset = offsetLow; |
| 16517 ovlp.OffsetHigh = offsetHigh; |
| 16518 return osLockFileEx(*phFile, flags, 0, numBytesLow, numBytesHigh, &ovlp); |
| 16519 }else{ |
| 16520 return osLockFile(*phFile, offsetLow, offsetHigh, numBytesLow, |
| 16521 numBytesHigh); |
| 16522 } |
| 16523 #endif |
| 16524 } |
| 16525 |
| 16526 /* |
| 16527 ** Unlock a file region. |
| 16528 */ |
| 16529 static BOOL winUnlockFile( |
| 16530 LPHANDLE phFile, |
| 16531 DWORD offsetLow, |
| 16532 DWORD offsetHigh, |
| 16533 DWORD numBytesLow, |
| 16534 DWORD numBytesHigh |
| 16535 ){ |
| 16536 #if SQLITE_OS_WINCE |
| 16537 /* |
| 16538 ** NOTE: Windows CE is handled differently here due its lack of the Win32 |
| 16539 ** API UnlockFile. |
| 16540 */ |
| 16541 return winceUnlockFile(phFile, offsetLow, offsetHigh, |
| 16542 numBytesLow, numBytesHigh); |
| 16543 #else |
| 16544 if( osIsNT() ){ |
| 16545 OVERLAPPED ovlp; |
| 16546 memset(&ovlp, 0, sizeof(OVERLAPPED)); |
| 16547 ovlp.Offset = offsetLow; |
| 16548 ovlp.OffsetHigh = offsetHigh; |
| 16549 return osUnlockFileEx(*phFile, 0, numBytesLow, numBytesHigh, &ovlp); |
| 16550 }else{ |
| 16551 return osUnlockFile(*phFile, offsetLow, offsetHigh, numBytesLow, |
| 16552 numBytesHigh); |
| 16553 } |
| 16554 #endif |
| 16555 } |
| 16556 |
| 16557 /***************************************************************************** |
| 16558 ** The next group of routines implement the I/O methods specified |
| 16559 ** by the sqlite3_io_methods object. |
| 16560 ******************************************************************************/ |
| 16561 |
| 16562 /* |
| 16563 ** Some Microsoft compilers lack this definition. |
| 16564 */ |
| 16565 #ifndef INVALID_SET_FILE_POINTER |
| 16566 # define INVALID_SET_FILE_POINTER ((DWORD)-1) |
| 16567 #endif |
| 16568 |
| 16569 /* |
| 16570 ** Move the current position of the file handle passed as the first |
| 16571 ** argument to offset iOffset within the file. If successful, return 0. |
| 16572 ** Otherwise, set pFile->lastErrno and return non-zero. |
| 16573 */ |
| 16574 static int winSeekFile(winFile *pFile, sqlite3_int64 iOffset){ |
| 16575 #if !SQLITE_OS_WINRT |
| 16576 LONG upperBits; /* Most sig. 32 bits of new offset */ |
| 16577 LONG lowerBits; /* Least sig. 32 bits of new offset */ |
| 16578 DWORD dwRet; /* Value returned by SetFilePointer() */ |
| 16579 DWORD lastErrno; /* Value returned by GetLastError() */ |
| 16580 |
| 16581 OSTRACE(("SEEK file=%p, offset=%lld\n", pFile->h, iOffset)); |
| 16582 |
| 16583 upperBits = (LONG)((iOffset>>32) & 0x7fffffff); |
| 16584 lowerBits = (LONG)(iOffset & 0xffffffff); |
| 16585 |
| 16586 /* API oddity: If successful, SetFilePointer() returns a dword |
| 16587 ** containing the lower 32-bits of the new file-offset. Or, if it fails, |
| 16588 ** it returns INVALID_SET_FILE_POINTER. However according to MSDN, |
| 16589 ** INVALID_SET_FILE_POINTER may also be a valid new offset. So to determine |
| 16590 ** whether an error has actually occurred, it is also necessary to call |
| 16591 ** GetLastError(). |
| 16592 */ |
| 16593 dwRet = osSetFilePointer(pFile->h, lowerBits, &upperBits, FILE_BEGIN); |
| 16594 |
| 16595 if( (dwRet==INVALID_SET_FILE_POINTER |
| 16596 && ((lastErrno = osGetLastError())!=NO_ERROR)) ){ |
| 16597 pFile->lastErrno = lastErrno; |
| 16598 winLogError(SQLITE_IOERR_SEEK, pFile->lastErrno, |
| 16599 "winSeekFile", pFile->zPath); |
| 16600 OSTRACE(("SEEK file=%p, rc=SQLITE_IOERR_SEEK\n", pFile->h)); |
| 16601 return 1; |
| 16602 } |
| 16603 |
| 16604 OSTRACE(("SEEK file=%p, rc=SQLITE_OK\n", pFile->h)); |
| 16605 return 0; |
| 16606 #else |
| 16607 /* |
| 16608 ** Same as above, except that this implementation works for WinRT. |
| 16609 */ |
| 16610 |
| 16611 LARGE_INTEGER x; /* The new offset */ |
| 16612 BOOL bRet; /* Value returned by SetFilePointerEx() */ |
| 16613 |
| 16614 x.QuadPart = iOffset; |
| 16615 bRet = osSetFilePointerEx(pFile->h, x, 0, FILE_BEGIN); |
| 16616 |
| 16617 if(!bRet){ |
| 16618 pFile->lastErrno = osGetLastError(); |
| 16619 winLogError(SQLITE_IOERR_SEEK, pFile->lastErrno, |
| 16620 "winSeekFile", pFile->zPath); |
| 16621 OSTRACE(("SEEK file=%p, rc=SQLITE_IOERR_SEEK\n", pFile->h)); |
| 16622 return 1; |
| 16623 } |
| 16624 |
| 16625 OSTRACE(("SEEK file=%p, rc=SQLITE_OK\n", pFile->h)); |
| 16626 return 0; |
| 16627 #endif |
| 16628 } |
| 16629 |
| 16630 #if SQLITE_MAX_MMAP_SIZE>0 |
| 16631 /* Forward references to VFS helper methods used for memory mapped files */ |
| 16632 static int winMapfile(winFile*, sqlite3_int64); |
| 16633 static int winUnmapfile(winFile*); |
| 16634 #endif |
| 16635 |
| 16636 /* |
| 16637 ** Close a file. |
| 16638 ** |
| 16639 ** It is reported that an attempt to close a handle might sometimes |
| 16640 ** fail. This is a very unreasonable result, but Windows is notorious |
| 16641 ** for being unreasonable so I do not doubt that it might happen. If |
| 16642 ** the close fails, we pause for 100 milliseconds and try again. As |
| 16643 ** many as MX_CLOSE_ATTEMPT attempts to close the handle are made before |
| 16644 ** giving up and returning an error. |
| 16645 */ |
| 16646 #define MX_CLOSE_ATTEMPT 3 |
| 16647 static int winClose(sqlite3_file *id){ |
| 16648 int rc, cnt = 0; |
| 16649 winFile *pFile = (winFile*)id; |
| 16650 |
| 16651 assert( id!=0 ); |
| 16652 #ifndef SQLITE_OMIT_WAL |
| 16653 assert( pFile->pShm==0 ); |
| 16654 #endif |
| 16655 assert( pFile->h!=NULL && pFile->h!=INVALID_HANDLE_VALUE ); |
| 16656 OSTRACE(("CLOSE pid=%lu, pFile=%p, file=%p\n", |
| 16657 osGetCurrentProcessId(), pFile, pFile->h)); |
| 16658 |
| 16659 #if SQLITE_MAX_MMAP_SIZE>0 |
| 16660 winUnmapfile(pFile); |
| 16661 #endif |
| 16662 |
| 16663 do{ |
| 16664 rc = osCloseHandle(pFile->h); |
| 16665 /* SimulateIOError( rc=0; cnt=MX_CLOSE_ATTEMPT; ); */ |
| 16666 }while( rc==0 && ++cnt < MX_CLOSE_ATTEMPT && (sqlite3_win32_sleep(100), 1) ); |
| 16667 #if SQLITE_OS_WINCE |
| 16668 #define WINCE_DELETION_ATTEMPTS 3 |
| 16669 winceDestroyLock(pFile); |
| 16670 if( pFile->zDeleteOnClose ){ |
| 16671 int cnt = 0; |
| 16672 while( |
| 16673 osDeleteFileW(pFile->zDeleteOnClose)==0 |
| 16674 && osGetFileAttributesW(pFile->zDeleteOnClose)!=0xffffffff |
| 16675 && cnt++ < WINCE_DELETION_ATTEMPTS |
| 16676 ){ |
| 16677 sqlite3_win32_sleep(100); /* Wait a little before trying again */ |
| 16678 } |
| 16679 sqlite3_free(pFile->zDeleteOnClose); |
| 16680 } |
| 16681 #endif |
| 16682 if( rc ){ |
| 16683 pFile->h = NULL; |
| 16684 } |
| 16685 OpenCounter(-1); |
| 16686 OSTRACE(("CLOSE pid=%lu, pFile=%p, file=%p, rc=%s\n", |
| 16687 osGetCurrentProcessId(), pFile, pFile->h, rc ? "ok" : "failed")); |
| 16688 return rc ? SQLITE_OK |
| 16689 : winLogError(SQLITE_IOERR_CLOSE, osGetLastError(), |
| 16690 "winClose", pFile->zPath); |
| 16691 } |
| 16692 |
| 16693 /* |
| 16694 ** Read data from a file into a buffer. Return SQLITE_OK if all |
| 16695 ** bytes were read successfully and SQLITE_IOERR if anything goes |
| 16696 ** wrong. |
| 16697 */ |
| 16698 static int winRead( |
| 16699 sqlite3_file *id, /* File to read from */ |
| 16700 void *pBuf, /* Write content into this buffer */ |
| 16701 int amt, /* Number of bytes to read */ |
| 16702 sqlite3_int64 offset /* Begin reading at this offset */ |
| 16703 ){ |
| 16704 #if !SQLITE_OS_WINCE && !defined(SQLITE_WIN32_NO_OVERLAPPED) |
| 16705 OVERLAPPED overlapped; /* The offset for ReadFile. */ |
| 16706 #endif |
| 16707 winFile *pFile = (winFile*)id; /* file handle */ |
| 16708 DWORD nRead; /* Number of bytes actually read from file */ |
| 16709 int nRetry = 0; /* Number of retrys */ |
| 16710 |
| 16711 assert( id!=0 ); |
| 16712 assert( amt>0 ); |
| 16713 assert( offset>=0 ); |
| 16714 SimulateIOError(return SQLITE_IOERR_READ); |
| 16715 OSTRACE(("READ pid=%lu, pFile=%p, file=%p, buffer=%p, amount=%d, " |
| 16716 "offset=%lld, lock=%d\n", osGetCurrentProcessId(), pFile, |
| 16717 pFile->h, pBuf, amt, offset, pFile->locktype)); |
| 16718 |
| 16719 #if defined(SQLITE_MMAP_READWRITE) && SQLITE_MAX_MMAP_SIZE>0 |
| 16720 /* Deal with as much of this read request as possible by transfering |
| 16721 ** data from the memory mapping using memcpy(). */ |
| 16722 if( offset<pFile->mmapSize ){ |
| 16723 if( offset+amt <= pFile->mmapSize ){ |
| 16724 memcpy(pBuf, &((u8 *)(pFile->pMapRegion))[offset], amt); |
| 16725 OSTRACE(("READ-MMAP pid=%lu, pFile=%p, file=%p, rc=SQLITE_OK\n", |
| 16726 osGetCurrentProcessId(), pFile, pFile->h)); |
| 16727 return SQLITE_OK; |
| 16728 }else{ |
| 16729 int nCopy = (int)(pFile->mmapSize - offset); |
| 16730 memcpy(pBuf, &((u8 *)(pFile->pMapRegion))[offset], nCopy); |
| 16731 pBuf = &((u8 *)pBuf)[nCopy]; |
| 16732 amt -= nCopy; |
| 16733 offset += nCopy; |
| 16734 } |
| 16735 } |
| 16736 #endif |
| 16737 |
| 16738 #if SQLITE_OS_WINCE || defined(SQLITE_WIN32_NO_OVERLAPPED) |
| 16739 if( winSeekFile(pFile, offset) ){ |
| 16740 OSTRACE(("READ pid=%lu, pFile=%p, file=%p, rc=SQLITE_FULL\n", |
| 16741 osGetCurrentProcessId(), pFile, pFile->h)); |
| 16742 return SQLITE_FULL; |
| 16743 } |
| 16744 while( !osReadFile(pFile->h, pBuf, amt, &nRead, 0) ){ |
| 16745 #else |
| 16746 memset(&overlapped, 0, sizeof(OVERLAPPED)); |
| 16747 overlapped.Offset = (LONG)(offset & 0xffffffff); |
| 16748 overlapped.OffsetHigh = (LONG)((offset>>32) & 0x7fffffff); |
| 16749 while( !osReadFile(pFile->h, pBuf, amt, &nRead, &overlapped) && |
| 16750 osGetLastError()!=ERROR_HANDLE_EOF ){ |
| 16751 #endif |
| 16752 DWORD lastErrno; |
| 16753 if( winRetryIoerr(&nRetry, &lastErrno) ) continue; |
| 16754 pFile->lastErrno = lastErrno; |
| 16755 OSTRACE(("READ pid=%lu, pFile=%p, file=%p, rc=SQLITE_IOERR_READ\n", |
| 16756 osGetCurrentProcessId(), pFile, pFile->h)); |
| 16757 return winLogError(SQLITE_IOERR_READ, pFile->lastErrno, |
| 16758 "winRead", pFile->zPath); |
| 16759 } |
| 16760 winLogIoerr(nRetry, __LINE__); |
| 16761 if( nRead<(DWORD)amt ){ |
| 16762 /* Unread parts of the buffer must be zero-filled */ |
| 16763 memset(&((char*)pBuf)[nRead], 0, amt-nRead); |
| 16764 OSTRACE(("READ pid=%lu, pFile=%p, file=%p, rc=SQLITE_IOERR_SHORT_READ\n", |
| 16765 osGetCurrentProcessId(), pFile, pFile->h)); |
| 16766 return SQLITE_IOERR_SHORT_READ; |
| 16767 } |
| 16768 |
| 16769 OSTRACE(("READ pid=%lu, pFile=%p, file=%p, rc=SQLITE_OK\n", |
| 16770 osGetCurrentProcessId(), pFile, pFile->h)); |
| 16771 return SQLITE_OK; |
| 16772 } |
| 16773 |
| 16774 /* |
| 16775 ** Write data from a buffer into a file. Return SQLITE_OK on success |
| 16776 ** or some other error code on failure. |
| 16777 */ |
| 16778 static int winWrite( |
| 16779 sqlite3_file *id, /* File to write into */ |
| 16780 const void *pBuf, /* The bytes to be written */ |
| 16781 int amt, /* Number of bytes to write */ |
| 16782 sqlite3_int64 offset /* Offset into the file to begin writing at */ |
| 16783 ){ |
| 16784 int rc = 0; /* True if error has occurred, else false */ |
| 16785 winFile *pFile = (winFile*)id; /* File handle */ |
| 16786 int nRetry = 0; /* Number of retries */ |
| 16787 |
| 16788 assert( amt>0 ); |
| 16789 assert( pFile ); |
| 16790 SimulateIOError(return SQLITE_IOERR_WRITE); |
| 16791 SimulateDiskfullError(return SQLITE_FULL); |
| 16792 |
| 16793 OSTRACE(("WRITE pid=%lu, pFile=%p, file=%p, buffer=%p, amount=%d, " |
| 16794 "offset=%lld, lock=%d\n", osGetCurrentProcessId(), pFile, |
| 16795 pFile->h, pBuf, amt, offset, pFile->locktype)); |
| 16796 |
| 16797 #if defined(SQLITE_MMAP_READWRITE) && SQLITE_MAX_MMAP_SIZE>0 |
| 16798 /* Deal with as much of this write request as possible by transfering |
| 16799 ** data from the memory mapping using memcpy(). */ |
| 16800 if( offset<pFile->mmapSize ){ |
| 16801 if( offset+amt <= pFile->mmapSize ){ |
| 16802 memcpy(&((u8 *)(pFile->pMapRegion))[offset], pBuf, amt); |
| 16803 OSTRACE(("WRITE-MMAP pid=%lu, pFile=%p, file=%p, rc=SQLITE_OK\n", |
| 16804 osGetCurrentProcessId(), pFile, pFile->h)); |
| 16805 return SQLITE_OK; |
| 16806 }else{ |
| 16807 int nCopy = (int)(pFile->mmapSize - offset); |
| 16808 memcpy(&((u8 *)(pFile->pMapRegion))[offset], pBuf, nCopy); |
| 16809 pBuf = &((u8 *)pBuf)[nCopy]; |
| 16810 amt -= nCopy; |
| 16811 offset += nCopy; |
| 16812 } |
| 16813 } |
| 16814 #endif |
| 16815 |
| 16816 #if SQLITE_OS_WINCE || defined(SQLITE_WIN32_NO_OVERLAPPED) |
| 16817 rc = winSeekFile(pFile, offset); |
| 16818 if( rc==0 ){ |
| 16819 #else |
| 16820 { |
| 16821 #endif |
| 16822 #if !SQLITE_OS_WINCE && !defined(SQLITE_WIN32_NO_OVERLAPPED) |
| 16823 OVERLAPPED overlapped; /* The offset for WriteFile. */ |
| 16824 #endif |
| 16825 u8 *aRem = (u8 *)pBuf; /* Data yet to be written */ |
| 16826 int nRem = amt; /* Number of bytes yet to be written */ |
| 16827 DWORD nWrite; /* Bytes written by each WriteFile() call */ |
| 16828 DWORD lastErrno = NO_ERROR; /* Value returned by GetLastError() */ |
| 16829 |
| 16830 #if !SQLITE_OS_WINCE && !defined(SQLITE_WIN32_NO_OVERLAPPED) |
| 16831 memset(&overlapped, 0, sizeof(OVERLAPPED)); |
| 16832 overlapped.Offset = (LONG)(offset & 0xffffffff); |
| 16833 overlapped.OffsetHigh = (LONG)((offset>>32) & 0x7fffffff); |
| 16834 #endif |
| 16835 |
| 16836 while( nRem>0 ){ |
| 16837 #if SQLITE_OS_WINCE || defined(SQLITE_WIN32_NO_OVERLAPPED) |
| 16838 if( !osWriteFile(pFile->h, aRem, nRem, &nWrite, 0) ){ |
| 16839 #else |
| 16840 if( !osWriteFile(pFile->h, aRem, nRem, &nWrite, &overlapped) ){ |
| 16841 #endif |
| 16842 if( winRetryIoerr(&nRetry, &lastErrno) ) continue; |
| 16843 break; |
| 16844 } |
| 16845 assert( nWrite==0 || nWrite<=(DWORD)nRem ); |
| 16846 if( nWrite==0 || nWrite>(DWORD)nRem ){ |
| 16847 lastErrno = osGetLastError(); |
| 16848 break; |
| 16849 } |
| 16850 #if !SQLITE_OS_WINCE && !defined(SQLITE_WIN32_NO_OVERLAPPED) |
| 16851 offset += nWrite; |
| 16852 overlapped.Offset = (LONG)(offset & 0xffffffff); |
| 16853 overlapped.OffsetHigh = (LONG)((offset>>32) & 0x7fffffff); |
| 16854 #endif |
| 16855 aRem += nWrite; |
| 16856 nRem -= nWrite; |
| 16857 } |
| 16858 if( nRem>0 ){ |
| 16859 pFile->lastErrno = lastErrno; |
| 16860 rc = 1; |
| 16861 } |
| 16862 } |
| 16863 |
| 16864 if( rc ){ |
| 16865 if( ( pFile->lastErrno==ERROR_HANDLE_DISK_FULL ) |
| 16866 || ( pFile->lastErrno==ERROR_DISK_FULL )){ |
| 16867 OSTRACE(("WRITE pid=%lu, pFile=%p, file=%p, rc=SQLITE_FULL\n", |
| 16868 osGetCurrentProcessId(), pFile, pFile->h)); |
| 16869 return winLogError(SQLITE_FULL, pFile->lastErrno, |
| 16870 "winWrite1", pFile->zPath); |
| 16871 } |
| 16872 OSTRACE(("WRITE pid=%lu, pFile=%p, file=%p, rc=SQLITE_IOERR_WRITE\n", |
| 16873 osGetCurrentProcessId(), pFile, pFile->h)); |
| 16874 return winLogError(SQLITE_IOERR_WRITE, pFile->lastErrno, |
| 16875 "winWrite2", pFile->zPath); |
| 16876 }else{ |
| 16877 winLogIoerr(nRetry, __LINE__); |
| 16878 } |
| 16879 OSTRACE(("WRITE pid=%lu, pFile=%p, file=%p, rc=SQLITE_OK\n", |
| 16880 osGetCurrentProcessId(), pFile, pFile->h)); |
| 16881 return SQLITE_OK; |
| 16882 } |
| 16883 |
| 16884 /* |
| 16885 ** Truncate an open file to a specified size |
| 16886 */ |
| 16887 static int winTruncate(sqlite3_file *id, sqlite3_int64 nByte){ |
| 16888 winFile *pFile = (winFile*)id; /* File handle object */ |
| 16889 int rc = SQLITE_OK; /* Return code for this function */ |
| 16890 DWORD lastErrno; |
| 16891 |
| 16892 assert( pFile ); |
| 16893 SimulateIOError(return SQLITE_IOERR_TRUNCATE); |
| 16894 OSTRACE(("TRUNCATE pid=%lu, pFile=%p, file=%p, size=%lld, lock=%d\n", |
| 16895 osGetCurrentProcessId(), pFile, pFile->h, nByte, pFile->locktype)); |
| 16896 |
| 16897 /* If the user has configured a chunk-size for this file, truncate the |
| 16898 ** file so that it consists of an integer number of chunks (i.e. the |
| 16899 ** actual file size after the operation may be larger than the requested |
| 16900 ** size). |
| 16901 */ |
| 16902 if( pFile->szChunk>0 ){ |
| 16903 nByte = ((nByte + pFile->szChunk - 1)/pFile->szChunk) * pFile->szChunk; |
| 16904 } |
| 16905 |
| 16906 /* SetEndOfFile() returns non-zero when successful, or zero when it fails. */ |
| 16907 if( winSeekFile(pFile, nByte) ){ |
| 16908 rc = winLogError(SQLITE_IOERR_TRUNCATE, pFile->lastErrno, |
| 16909 "winTruncate1", pFile->zPath); |
| 16910 }else if( 0==osSetEndOfFile(pFile->h) && |
| 16911 ((lastErrno = osGetLastError())!=ERROR_USER_MAPPED_FILE) ){ |
| 16912 pFile->lastErrno = lastErrno; |
| 16913 rc = winLogError(SQLITE_IOERR_TRUNCATE, pFile->lastErrno, |
| 16914 "winTruncate2", pFile->zPath); |
| 16915 } |
| 16916 |
| 16917 #if SQLITE_MAX_MMAP_SIZE>0 |
| 16918 /* If the file was truncated to a size smaller than the currently |
| 16919 ** mapped region, reduce the effective mapping size as well. SQLite will |
| 16920 ** use read() and write() to access data beyond this point from now on. |
| 16921 */ |
| 16922 if( pFile->pMapRegion && nByte<pFile->mmapSize ){ |
| 16923 pFile->mmapSize = nByte; |
| 16924 } |
| 16925 #endif |
| 16926 |
| 16927 OSTRACE(("TRUNCATE pid=%lu, pFile=%p, file=%p, rc=%s\n", |
| 16928 osGetCurrentProcessId(), pFile, pFile->h, sqlite3ErrName(rc))); |
| 16929 return rc; |
| 16930 } |
| 16931 |
| 16932 #ifdef SQLITE_TEST |
| 16933 /* |
| 16934 ** Count the number of fullsyncs and normal syncs. This is used to test |
| 16935 ** that syncs and fullsyncs are occuring at the right times. |
| 16936 */ |
| 16937 SQLITE_API int sqlite3_sync_count = 0; |
| 16938 SQLITE_API int sqlite3_fullsync_count = 0; |
| 16939 #endif |
| 16940 |
| 16941 /* |
| 16942 ** Make sure all writes to a particular file are committed to disk. |
| 16943 */ |
| 16944 static int winSync(sqlite3_file *id, int flags){ |
| 16945 #ifndef SQLITE_NO_SYNC |
| 16946 /* |
| 16947 ** Used only when SQLITE_NO_SYNC is not defined. |
| 16948 */ |
| 16949 BOOL rc; |
| 16950 #endif |
| 16951 #if !defined(NDEBUG) || !defined(SQLITE_NO_SYNC) || \ |
| 16952 defined(SQLITE_HAVE_OS_TRACE) |
| 16953 /* |
| 16954 ** Used when SQLITE_NO_SYNC is not defined and by the assert() and/or |
| 16955 ** OSTRACE() macros. |
| 16956 */ |
| 16957 winFile *pFile = (winFile*)id; |
| 16958 #else |
| 16959 UNUSED_PARAMETER(id); |
| 16960 #endif |
| 16961 |
| 16962 assert( pFile ); |
| 16963 /* Check that one of SQLITE_SYNC_NORMAL or FULL was passed */ |
| 16964 assert((flags&0x0F)==SQLITE_SYNC_NORMAL |
| 16965 || (flags&0x0F)==SQLITE_SYNC_FULL |
| 16966 ); |
| 16967 |
| 16968 /* Unix cannot, but some systems may return SQLITE_FULL from here. This |
| 16969 ** line is to test that doing so does not cause any problems. |
| 16970 */ |
| 16971 SimulateDiskfullError( return SQLITE_FULL ); |
| 16972 |
| 16973 OSTRACE(("SYNC pid=%lu, pFile=%p, file=%p, flags=%x, lock=%d\n", |
| 16974 osGetCurrentProcessId(), pFile, pFile->h, flags, |
| 16975 pFile->locktype)); |
| 16976 |
| 16977 #ifndef SQLITE_TEST |
| 16978 UNUSED_PARAMETER(flags); |
| 16979 #else |
| 16980 if( (flags&0x0F)==SQLITE_SYNC_FULL ){ |
| 16981 sqlite3_fullsync_count++; |
| 16982 } |
| 16983 sqlite3_sync_count++; |
| 16984 #endif |
| 16985 |
| 16986 /* If we compiled with the SQLITE_NO_SYNC flag, then syncing is a |
| 16987 ** no-op |
| 16988 */ |
| 16989 #ifdef SQLITE_NO_SYNC |
| 16990 OSTRACE(("SYNC-NOP pid=%lu, pFile=%p, file=%p, rc=SQLITE_OK\n", |
| 16991 osGetCurrentProcessId(), pFile, pFile->h)); |
| 16992 return SQLITE_OK; |
| 16993 #else |
| 16994 #if SQLITE_MAX_MMAP_SIZE>0 |
| 16995 if( pFile->pMapRegion ){ |
| 16996 if( osFlushViewOfFile(pFile->pMapRegion, 0) ){ |
| 16997 OSTRACE(("SYNC-MMAP pid=%lu, pFile=%p, pMapRegion=%p, " |
| 16998 "rc=SQLITE_OK\n", osGetCurrentProcessId(), |
| 16999 pFile, pFile->pMapRegion)); |
| 17000 }else{ |
| 17001 pFile->lastErrno = osGetLastError(); |
| 17002 OSTRACE(("SYNC-MMAP pid=%lu, pFile=%p, pMapRegion=%p, " |
| 17003 "rc=SQLITE_IOERR_MMAP\n", osGetCurrentProcessId(), |
| 17004 pFile, pFile->pMapRegion)); |
| 17005 return winLogError(SQLITE_IOERR_MMAP, pFile->lastErrno, |
| 17006 "winSync1", pFile->zPath); |
| 17007 } |
| 17008 } |
| 17009 #endif |
| 17010 rc = osFlushFileBuffers(pFile->h); |
| 17011 SimulateIOError( rc=FALSE ); |
| 17012 if( rc ){ |
| 17013 OSTRACE(("SYNC pid=%lu, pFile=%p, file=%p, rc=SQLITE_OK\n", |
| 17014 osGetCurrentProcessId(), pFile, pFile->h)); |
| 17015 return SQLITE_OK; |
| 17016 }else{ |
| 17017 pFile->lastErrno = osGetLastError(); |
| 17018 OSTRACE(("SYNC pid=%lu, pFile=%p, file=%p, rc=SQLITE_IOERR_FSYNC\n", |
| 17019 osGetCurrentProcessId(), pFile, pFile->h)); |
| 17020 return winLogError(SQLITE_IOERR_FSYNC, pFile->lastErrno, |
| 17021 "winSync2", pFile->zPath); |
| 17022 } |
| 17023 #endif |
| 17024 } |
| 17025 |
| 17026 /* |
| 17027 ** Determine the current size of a file in bytes |
| 17028 */ |
| 17029 static int winFileSize(sqlite3_file *id, sqlite3_int64 *pSize){ |
| 17030 winFile *pFile = (winFile*)id; |
| 17031 int rc = SQLITE_OK; |
| 17032 |
| 17033 assert( id!=0 ); |
| 17034 assert( pSize!=0 ); |
| 17035 SimulateIOError(return SQLITE_IOERR_FSTAT); |
| 17036 OSTRACE(("SIZE file=%p, pSize=%p\n", pFile->h, pSize)); |
| 17037 |
| 17038 #if SQLITE_OS_WINRT |
| 17039 { |
| 17040 FILE_STANDARD_INFO info; |
| 17041 if( osGetFileInformationByHandleEx(pFile->h, FileStandardInfo, |
| 17042 &info, sizeof(info)) ){ |
| 17043 *pSize = info.EndOfFile.QuadPart; |
| 17044 }else{ |
| 17045 pFile->lastErrno = osGetLastError(); |
| 17046 rc = winLogError(SQLITE_IOERR_FSTAT, pFile->lastErrno, |
| 17047 "winFileSize", pFile->zPath); |
| 17048 } |
| 17049 } |
| 17050 #else |
| 17051 { |
| 17052 DWORD upperBits; |
| 17053 DWORD lowerBits; |
| 17054 DWORD lastErrno; |
| 17055 |
| 17056 lowerBits = osGetFileSize(pFile->h, &upperBits); |
| 17057 *pSize = (((sqlite3_int64)upperBits)<<32) + lowerBits; |
| 17058 if( (lowerBits == INVALID_FILE_SIZE) |
| 17059 && ((lastErrno = osGetLastError())!=NO_ERROR) ){ |
| 17060 pFile->lastErrno = lastErrno; |
| 17061 rc = winLogError(SQLITE_IOERR_FSTAT, pFile->lastErrno, |
| 17062 "winFileSize", pFile->zPath); |
| 17063 } |
| 17064 } |
| 17065 #endif |
| 17066 OSTRACE(("SIZE file=%p, pSize=%p, *pSize=%lld, rc=%s\n", |
| 17067 pFile->h, pSize, *pSize, sqlite3ErrName(rc))); |
| 17068 return rc; |
| 17069 } |
| 17070 |
| 17071 /* |
| 17072 ** LOCKFILE_FAIL_IMMEDIATELY is undefined on some Windows systems. |
| 17073 */ |
| 17074 #ifndef LOCKFILE_FAIL_IMMEDIATELY |
| 17075 # define LOCKFILE_FAIL_IMMEDIATELY 1 |
| 17076 #endif |
| 17077 |
| 17078 #ifndef LOCKFILE_EXCLUSIVE_LOCK |
| 17079 # define LOCKFILE_EXCLUSIVE_LOCK 2 |
| 17080 #endif |
| 17081 |
| 17082 /* |
| 17083 ** Historically, SQLite has used both the LockFile and LockFileEx functions. |
| 17084 ** When the LockFile function was used, it was always expected to fail |
| 17085 ** immediately if the lock could not be obtained. Also, it always expected to |
| 17086 ** obtain an exclusive lock. These flags are used with the LockFileEx function |
| 17087 ** and reflect those expectations; therefore, they should not be changed. |
| 17088 */ |
| 17089 #ifndef SQLITE_LOCKFILE_FLAGS |
| 17090 # define SQLITE_LOCKFILE_FLAGS (LOCKFILE_FAIL_IMMEDIATELY | \ |
| 17091 LOCKFILE_EXCLUSIVE_LOCK) |
| 17092 #endif |
| 17093 |
| 17094 /* |
| 17095 ** Currently, SQLite never calls the LockFileEx function without wanting the |
| 17096 ** call to fail immediately if the lock cannot be obtained. |
| 17097 */ |
| 17098 #ifndef SQLITE_LOCKFILEEX_FLAGS |
| 17099 # define SQLITE_LOCKFILEEX_FLAGS (LOCKFILE_FAIL_IMMEDIATELY) |
| 17100 #endif |
| 17101 |
| 17102 /* |
| 17103 ** Acquire a reader lock. |
| 17104 ** Different API routines are called depending on whether or not this |
| 17105 ** is Win9x or WinNT. |
| 17106 */ |
| 17107 static int winGetReadLock(winFile *pFile){ |
| 17108 int res; |
| 17109 OSTRACE(("READ-LOCK file=%p, lock=%d\n", pFile->h, pFile->locktype)); |
| 17110 if( osIsNT() ){ |
| 17111 #if SQLITE_OS_WINCE |
| 17112 /* |
| 17113 ** NOTE: Windows CE is handled differently here due its lack of the Win32 |
| 17114 ** API LockFileEx. |
| 17115 */ |
| 17116 res = winceLockFile(&pFile->h, SHARED_FIRST, 0, 1, 0); |
| 17117 #else |
| 17118 res = winLockFile(&pFile->h, SQLITE_LOCKFILEEX_FLAGS, SHARED_FIRST, 0, |
| 17119 SHARED_SIZE, 0); |
| 17120 #endif |
| 17121 } |
| 17122 #ifdef SQLITE_WIN32_HAS_ANSI |
| 17123 else{ |
| 17124 int lk; |
| 17125 sqlite3_randomness(sizeof(lk), &lk); |
| 17126 pFile->sharedLockByte = (short)((lk & 0x7fffffff)%(SHARED_SIZE - 1)); |
| 17127 res = winLockFile(&pFile->h, SQLITE_LOCKFILE_FLAGS, |
| 17128 SHARED_FIRST+pFile->sharedLockByte, 0, 1, 0); |
| 17129 } |
| 17130 #endif |
| 17131 if( res == 0 ){ |
| 17132 pFile->lastErrno = osGetLastError(); |
| 17133 /* No need to log a failure to lock */ |
| 17134 } |
| 17135 OSTRACE(("READ-LOCK file=%p, result=%d\n", pFile->h, res)); |
| 17136 return res; |
| 17137 } |
| 17138 |
| 17139 /* |
| 17140 ** Undo a readlock |
| 17141 */ |
| 17142 static int winUnlockReadLock(winFile *pFile){ |
| 17143 int res; |
| 17144 DWORD lastErrno; |
| 17145 OSTRACE(("READ-UNLOCK file=%p, lock=%d\n", pFile->h, pFile->locktype)); |
| 17146 if( osIsNT() ){ |
| 17147 res = winUnlockFile(&pFile->h, SHARED_FIRST, 0, SHARED_SIZE, 0); |
| 17148 } |
| 17149 #ifdef SQLITE_WIN32_HAS_ANSI |
| 17150 else{ |
| 17151 res = winUnlockFile(&pFile->h, SHARED_FIRST+pFile->sharedLockByte, 0, 1, 0); |
| 17152 } |
| 17153 #endif |
| 17154 if( res==0 && ((lastErrno = osGetLastError())!=ERROR_NOT_LOCKED) ){ |
| 17155 pFile->lastErrno = lastErrno; |
| 17156 winLogError(SQLITE_IOERR_UNLOCK, pFile->lastErrno, |
| 17157 "winUnlockReadLock", pFile->zPath); |
| 17158 } |
| 17159 OSTRACE(("READ-UNLOCK file=%p, result=%d\n", pFile->h, res)); |
| 17160 return res; |
| 17161 } |
| 17162 |
| 17163 /* |
| 17164 ** Lock the file with the lock specified by parameter locktype - one |
| 17165 ** of the following: |
| 17166 ** |
| 17167 ** (1) SHARED_LOCK |
| 17168 ** (2) RESERVED_LOCK |
| 17169 ** (3) PENDING_LOCK |
| 17170 ** (4) EXCLUSIVE_LOCK |
| 17171 ** |
| 17172 ** Sometimes when requesting one lock state, additional lock states |
| 17173 ** are inserted in between. The locking might fail on one of the later |
| 17174 ** transitions leaving the lock state different from what it started but |
| 17175 ** still short of its goal. The following chart shows the allowed |
| 17176 ** transitions and the inserted intermediate states: |
| 17177 ** |
| 17178 ** UNLOCKED -> SHARED |
| 17179 ** SHARED -> RESERVED |
| 17180 ** SHARED -> (PENDING) -> EXCLUSIVE |
| 17181 ** RESERVED -> (PENDING) -> EXCLUSIVE |
| 17182 ** PENDING -> EXCLUSIVE |
| 17183 ** |
| 17184 ** This routine will only increase a lock. The winUnlock() routine |
| 17185 ** erases all locks at once and returns us immediately to locking level 0. |
| 17186 ** It is not possible to lower the locking level one step at a time. You |
| 17187 ** must go straight to locking level 0. |
| 17188 */ |
| 17189 static int winLock(sqlite3_file *id, int locktype){ |
| 17190 int rc = SQLITE_OK; /* Return code from subroutines */ |
| 17191 int res = 1; /* Result of a Windows lock call */ |
| 17192 int newLocktype; /* Set pFile->locktype to this value before exiting */ |
| 17193 int gotPendingLock = 0;/* True if we acquired a PENDING lock this time */ |
| 17194 winFile *pFile = (winFile*)id; |
| 17195 DWORD lastErrno = NO_ERROR; |
| 17196 |
| 17197 assert( id!=0 ); |
| 17198 OSTRACE(("LOCK file=%p, oldLock=%d(%d), newLock=%d\n", |
| 17199 pFile->h, pFile->locktype, pFile->sharedLockByte, locktype)); |
| 17200 |
| 17201 /* If there is already a lock of this type or more restrictive on the |
| 17202 ** OsFile, do nothing. Don't use the end_lock: exit path, as |
| 17203 ** sqlite3OsEnterMutex() hasn't been called yet. |
| 17204 */ |
| 17205 if( pFile->locktype>=locktype ){ |
| 17206 OSTRACE(("LOCK-HELD file=%p, rc=SQLITE_OK\n", pFile->h)); |
| 17207 return SQLITE_OK; |
| 17208 } |
| 17209 |
| 17210 /* Do not allow any kind of write-lock on a read-only database |
| 17211 */ |
| 17212 if( (pFile->ctrlFlags & WINFILE_RDONLY)!=0 && locktype>=RESERVED_LOCK ){ |
| 17213 return SQLITE_IOERR_LOCK; |
| 17214 } |
| 17215 |
| 17216 /* Make sure the locking sequence is correct |
| 17217 */ |
| 17218 assert( pFile->locktype!=NO_LOCK || locktype==SHARED_LOCK ); |
| 17219 assert( locktype!=PENDING_LOCK ); |
| 17220 assert( locktype!=RESERVED_LOCK || pFile->locktype==SHARED_LOCK ); |
| 17221 |
| 17222 /* Lock the PENDING_LOCK byte if we need to acquire a PENDING lock or |
| 17223 ** a SHARED lock. If we are acquiring a SHARED lock, the acquisition of |
| 17224 ** the PENDING_LOCK byte is temporary. |
| 17225 */ |
| 17226 newLocktype = pFile->locktype; |
| 17227 if( (pFile->locktype==NO_LOCK) |
| 17228 || ( (locktype==EXCLUSIVE_LOCK) |
| 17229 && (pFile->locktype==RESERVED_LOCK)) |
| 17230 ){ |
| 17231 int cnt = 3; |
| 17232 while( cnt-->0 && (res = winLockFile(&pFile->h, SQLITE_LOCKFILE_FLAGS, |
| 17233 PENDING_BYTE, 0, 1, 0))==0 ){ |
| 17234 /* Try 3 times to get the pending lock. This is needed to work |
| 17235 ** around problems caused by indexing and/or anti-virus software on |
| 17236 ** Windows systems. |
| 17237 ** If you are using this code as a model for alternative VFSes, do not |
| 17238 ** copy this retry logic. It is a hack intended for Windows only. |
| 17239 */ |
| 17240 lastErrno = osGetLastError(); |
| 17241 OSTRACE(("LOCK-PENDING-FAIL file=%p, count=%d, result=%d\n", |
| 17242 pFile->h, cnt, res)); |
| 17243 if( lastErrno==ERROR_INVALID_HANDLE ){ |
| 17244 pFile->lastErrno = lastErrno; |
| 17245 rc = SQLITE_IOERR_LOCK; |
| 17246 OSTRACE(("LOCK-FAIL file=%p, count=%d, rc=%s\n", |
| 17247 pFile->h, cnt, sqlite3ErrName(rc))); |
| 17248 return rc; |
| 17249 } |
| 17250 if( cnt ) sqlite3_win32_sleep(1); |
| 17251 } |
| 17252 gotPendingLock = res; |
| 17253 if( !res ){ |
| 17254 lastErrno = osGetLastError(); |
| 17255 } |
| 17256 } |
| 17257 |
| 17258 /* Acquire a shared lock |
| 17259 */ |
| 17260 if( locktype==SHARED_LOCK && res ){ |
| 17261 assert( pFile->locktype==NO_LOCK ); |
| 17262 res = winGetReadLock(pFile); |
| 17263 if( res ){ |
| 17264 newLocktype = SHARED_LOCK; |
| 17265 }else{ |
| 17266 lastErrno = osGetLastError(); |
| 17267 } |
| 17268 } |
| 17269 |
| 17270 /* Acquire a RESERVED lock |
| 17271 */ |
| 17272 if( locktype==RESERVED_LOCK && res ){ |
| 17273 assert( pFile->locktype==SHARED_LOCK ); |
| 17274 res = winLockFile(&pFile->h, SQLITE_LOCKFILE_FLAGS, RESERVED_BYTE, 0, 1, 0); |
| 17275 if( res ){ |
| 17276 newLocktype = RESERVED_LOCK; |
| 17277 }else{ |
| 17278 lastErrno = osGetLastError(); |
| 17279 } |
| 17280 } |
| 17281 |
| 17282 /* Acquire a PENDING lock |
| 17283 */ |
| 17284 if( locktype==EXCLUSIVE_LOCK && res ){ |
| 17285 newLocktype = PENDING_LOCK; |
| 17286 gotPendingLock = 0; |
| 17287 } |
| 17288 |
| 17289 /* Acquire an EXCLUSIVE lock |
| 17290 */ |
| 17291 if( locktype==EXCLUSIVE_LOCK && res ){ |
| 17292 assert( pFile->locktype>=SHARED_LOCK ); |
| 17293 res = winUnlockReadLock(pFile); |
| 17294 res = winLockFile(&pFile->h, SQLITE_LOCKFILE_FLAGS, SHARED_FIRST, 0, |
| 17295 SHARED_SIZE, 0); |
| 17296 if( res ){ |
| 17297 newLocktype = EXCLUSIVE_LOCK; |
| 17298 }else{ |
| 17299 lastErrno = osGetLastError(); |
| 17300 winGetReadLock(pFile); |
| 17301 } |
| 17302 } |
| 17303 |
| 17304 /* If we are holding a PENDING lock that ought to be released, then |
| 17305 ** release it now. |
| 17306 */ |
| 17307 if( gotPendingLock && locktype==SHARED_LOCK ){ |
| 17308 winUnlockFile(&pFile->h, PENDING_BYTE, 0, 1, 0); |
| 17309 } |
| 17310 |
| 17311 /* Update the state of the lock has held in the file descriptor then |
| 17312 ** return the appropriate result code. |
| 17313 */ |
| 17314 if( res ){ |
| 17315 rc = SQLITE_OK; |
| 17316 }else{ |
| 17317 pFile->lastErrno = lastErrno; |
| 17318 rc = SQLITE_BUSY; |
| 17319 OSTRACE(("LOCK-FAIL file=%p, wanted=%d, got=%d\n", |
| 17320 pFile->h, locktype, newLocktype)); |
| 17321 } |
| 17322 pFile->locktype = (u8)newLocktype; |
| 17323 OSTRACE(("LOCK file=%p, lock=%d, rc=%s\n", |
| 17324 pFile->h, pFile->locktype, sqlite3ErrName(rc))); |
| 17325 return rc; |
| 17326 } |
| 17327 |
| 17328 /* |
| 17329 ** This routine checks if there is a RESERVED lock held on the specified |
| 17330 ** file by this or any other process. If such a lock is held, return |
| 17331 ** non-zero, otherwise zero. |
| 17332 */ |
| 17333 static int winCheckReservedLock(sqlite3_file *id, int *pResOut){ |
| 17334 int res; |
| 17335 winFile *pFile = (winFile*)id; |
| 17336 |
| 17337 SimulateIOError( return SQLITE_IOERR_CHECKRESERVEDLOCK; ); |
| 17338 OSTRACE(("TEST-WR-LOCK file=%p, pResOut=%p\n", pFile->h, pResOut)); |
| 17339 |
| 17340 assert( id!=0 ); |
| 17341 if( pFile->locktype>=RESERVED_LOCK ){ |
| 17342 res = 1; |
| 17343 OSTRACE(("TEST-WR-LOCK file=%p, result=%d (local)\n", pFile->h, res)); |
| 17344 }else{ |
| 17345 res = winLockFile(&pFile->h, SQLITE_LOCKFILEEX_FLAGS,RESERVED_BYTE,0,1,0); |
| 17346 if( res ){ |
| 17347 winUnlockFile(&pFile->h, RESERVED_BYTE, 0, 1, 0); |
| 17348 } |
| 17349 res = !res; |
| 17350 OSTRACE(("TEST-WR-LOCK file=%p, result=%d (remote)\n", pFile->h, res)); |
| 17351 } |
| 17352 *pResOut = res; |
| 17353 OSTRACE(("TEST-WR-LOCK file=%p, pResOut=%p, *pResOut=%d, rc=SQLITE_OK\n", |
| 17354 pFile->h, pResOut, *pResOut)); |
| 17355 return SQLITE_OK; |
| 17356 } |
| 17357 |
| 17358 /* |
| 17359 ** Lower the locking level on file descriptor id to locktype. locktype |
| 17360 ** must be either NO_LOCK or SHARED_LOCK. |
| 17361 ** |
| 17362 ** If the locking level of the file descriptor is already at or below |
| 17363 ** the requested locking level, this routine is a no-op. |
| 17364 ** |
| 17365 ** It is not possible for this routine to fail if the second argument |
| 17366 ** is NO_LOCK. If the second argument is SHARED_LOCK then this routine |
| 17367 ** might return SQLITE_IOERR; |
| 17368 */ |
| 17369 static int winUnlock(sqlite3_file *id, int locktype){ |
| 17370 int type; |
| 17371 winFile *pFile = (winFile*)id; |
| 17372 int rc = SQLITE_OK; |
| 17373 assert( pFile!=0 ); |
| 17374 assert( locktype<=SHARED_LOCK ); |
| 17375 OSTRACE(("UNLOCK file=%p, oldLock=%d(%d), newLock=%d\n", |
| 17376 pFile->h, pFile->locktype, pFile->sharedLockByte, locktype)); |
| 17377 type = pFile->locktype; |
| 17378 if( type>=EXCLUSIVE_LOCK ){ |
| 17379 winUnlockFile(&pFile->h, SHARED_FIRST, 0, SHARED_SIZE, 0); |
| 17380 if( locktype==SHARED_LOCK && !winGetReadLock(pFile) ){ |
| 17381 /* This should never happen. We should always be able to |
| 17382 ** reacquire the read lock */ |
| 17383 rc = winLogError(SQLITE_IOERR_UNLOCK, osGetLastError(), |
| 17384 "winUnlock", pFile->zPath); |
| 17385 } |
| 17386 } |
| 17387 if( type>=RESERVED_LOCK ){ |
| 17388 winUnlockFile(&pFile->h, RESERVED_BYTE, 0, 1, 0); |
| 17389 } |
| 17390 if( locktype==NO_LOCK && type>=SHARED_LOCK ){ |
| 17391 winUnlockReadLock(pFile); |
| 17392 } |
| 17393 if( type>=PENDING_LOCK ){ |
| 17394 winUnlockFile(&pFile->h, PENDING_BYTE, 0, 1, 0); |
| 17395 } |
| 17396 pFile->locktype = (u8)locktype; |
| 17397 OSTRACE(("UNLOCK file=%p, lock=%d, rc=%s\n", |
| 17398 pFile->h, pFile->locktype, sqlite3ErrName(rc))); |
| 17399 return rc; |
| 17400 } |
| 17401 |
| 17402 /* |
| 17403 ** If *pArg is initially negative then this is a query. Set *pArg to |
| 17404 ** 1 or 0 depending on whether or not bit mask of pFile->ctrlFlags is set. |
| 17405 ** |
| 17406 ** If *pArg is 0 or 1, then clear or set the mask bit of pFile->ctrlFlags. |
| 17407 */ |
| 17408 static void winModeBit(winFile *pFile, unsigned char mask, int *pArg){ |
| 17409 if( *pArg<0 ){ |
| 17410 *pArg = (pFile->ctrlFlags & mask)!=0; |
| 17411 }else if( (*pArg)==0 ){ |
| 17412 pFile->ctrlFlags &= ~mask; |
| 17413 }else{ |
| 17414 pFile->ctrlFlags |= mask; |
| 17415 } |
| 17416 } |
| 17417 |
| 17418 /* Forward references to VFS helper methods used for temporary files */ |
| 17419 static int winGetTempname(sqlite3_vfs *, char **); |
| 17420 static int winIsDir(const void *); |
| 17421 static BOOL winIsDriveLetterAndColon(const char *); |
| 17422 |
| 17423 /* |
| 17424 ** Control and query of the open file handle. |
| 17425 */ |
| 17426 static int winFileControl(sqlite3_file *id, int op, void *pArg){ |
| 17427 winFile *pFile = (winFile*)id; |
| 17428 OSTRACE(("FCNTL file=%p, op=%d, pArg=%p\n", pFile->h, op, pArg)); |
| 17429 switch( op ){ |
| 17430 case SQLITE_FCNTL_LOCKSTATE: { |
| 17431 *(int*)pArg = pFile->locktype; |
| 17432 OSTRACE(("FCNTL file=%p, rc=SQLITE_OK\n", pFile->h)); |
| 17433 return SQLITE_OK; |
| 17434 } |
| 17435 case SQLITE_LAST_ERRNO: { |
| 17436 *(int*)pArg = (int)pFile->lastErrno; |
| 17437 OSTRACE(("FCNTL file=%p, rc=SQLITE_OK\n", pFile->h)); |
| 17438 return SQLITE_OK; |
| 17439 } |
| 17440 case SQLITE_FCNTL_CHUNK_SIZE: { |
| 17441 pFile->szChunk = *(int *)pArg; |
| 17442 OSTRACE(("FCNTL file=%p, rc=SQLITE_OK\n", pFile->h)); |
| 17443 return SQLITE_OK; |
| 17444 } |
| 17445 case SQLITE_FCNTL_SIZE_HINT: { |
| 17446 if( pFile->szChunk>0 ){ |
| 17447 sqlite3_int64 oldSz; |
| 17448 int rc = winFileSize(id, &oldSz); |
| 17449 if( rc==SQLITE_OK ){ |
| 17450 sqlite3_int64 newSz = *(sqlite3_int64*)pArg; |
| 17451 if( newSz>oldSz ){ |
| 17452 SimulateIOErrorBenign(1); |
| 17453 rc = winTruncate(id, newSz); |
| 17454 SimulateIOErrorBenign(0); |
| 17455 } |
| 17456 } |
| 17457 OSTRACE(("FCNTL file=%p, rc=%s\n", pFile->h, sqlite3ErrName(rc))); |
| 17458 return rc; |
| 17459 } |
| 17460 OSTRACE(("FCNTL file=%p, rc=SQLITE_OK\n", pFile->h)); |
| 17461 return SQLITE_OK; |
| 17462 } |
| 17463 case SQLITE_FCNTL_PERSIST_WAL: { |
| 17464 winModeBit(pFile, WINFILE_PERSIST_WAL, (int*)pArg); |
| 17465 OSTRACE(("FCNTL file=%p, rc=SQLITE_OK\n", pFile->h)); |
| 17466 return SQLITE_OK; |
| 17467 } |
| 17468 case SQLITE_FCNTL_POWERSAFE_OVERWRITE: { |
| 17469 winModeBit(pFile, WINFILE_PSOW, (int*)pArg); |
| 17470 OSTRACE(("FCNTL file=%p, rc=SQLITE_OK\n", pFile->h)); |
| 17471 return SQLITE_OK; |
| 17472 } |
| 17473 case SQLITE_FCNTL_VFSNAME: { |
| 17474 *(char**)pArg = sqlite3_mprintf("%s", pFile->pVfs->zName); |
| 17475 OSTRACE(("FCNTL file=%p, rc=SQLITE_OK\n", pFile->h)); |
| 17476 return SQLITE_OK; |
| 17477 } |
| 17478 case SQLITE_FCNTL_WIN32_AV_RETRY: { |
| 17479 int *a = (int*)pArg; |
| 17480 if( a[0]>0 ){ |
| 17481 winIoerrRetry = a[0]; |
| 17482 }else{ |
| 17483 a[0] = winIoerrRetry; |
| 17484 } |
| 17485 if( a[1]>0 ){ |
| 17486 winIoerrRetryDelay = a[1]; |
| 17487 }else{ |
| 17488 a[1] = winIoerrRetryDelay; |
| 17489 } |
| 17490 OSTRACE(("FCNTL file=%p, rc=SQLITE_OK\n", pFile->h)); |
| 17491 return SQLITE_OK; |
| 17492 } |
| 17493 #ifdef SQLITE_TEST |
| 17494 case SQLITE_FCNTL_WIN32_SET_HANDLE: { |
| 17495 LPHANDLE phFile = (LPHANDLE)pArg; |
| 17496 HANDLE hOldFile = pFile->h; |
| 17497 pFile->h = *phFile; |
| 17498 *phFile = hOldFile; |
| 17499 OSTRACE(("FCNTL oldFile=%p, newFile=%p, rc=SQLITE_OK\n", |
| 17500 hOldFile, pFile->h)); |
| 17501 return SQLITE_OK; |
| 17502 } |
| 17503 #endif |
| 17504 case SQLITE_FCNTL_TEMPFILENAME: { |
| 17505 char *zTFile = 0; |
| 17506 int rc = winGetTempname(pFile->pVfs, &zTFile); |
| 17507 if( rc==SQLITE_OK ){ |
| 17508 *(char**)pArg = zTFile; |
| 17509 } |
| 17510 OSTRACE(("FCNTL file=%p, rc=%s\n", pFile->h, sqlite3ErrName(rc))); |
| 17511 return rc; |
| 17512 } |
| 17513 #if SQLITE_MAX_MMAP_SIZE>0 |
| 17514 case SQLITE_FCNTL_MMAP_SIZE: { |
| 17515 i64 newLimit = *(i64*)pArg; |
| 17516 int rc = SQLITE_OK; |
| 17517 if( newLimit>sqlite3GlobalConfig.mxMmap ){ |
| 17518 newLimit = sqlite3GlobalConfig.mxMmap; |
| 17519 } |
| 17520 *(i64*)pArg = pFile->mmapSizeMax; |
| 17521 if( newLimit>=0 && newLimit!=pFile->mmapSizeMax && pFile->nFetchOut==0 ){ |
| 17522 pFile->mmapSizeMax = newLimit; |
| 17523 if( pFile->mmapSize>0 ){ |
| 17524 winUnmapfile(pFile); |
| 17525 rc = winMapfile(pFile, -1); |
| 17526 } |
| 17527 } |
| 17528 OSTRACE(("FCNTL file=%p, rc=%s\n", pFile->h, sqlite3ErrName(rc))); |
| 17529 return rc; |
| 17530 } |
| 17531 #endif |
| 17532 } |
| 17533 OSTRACE(("FCNTL file=%p, rc=SQLITE_NOTFOUND\n", pFile->h)); |
| 17534 return SQLITE_NOTFOUND; |
| 17535 } |
| 17536 |
| 17537 /* |
| 17538 ** Return the sector size in bytes of the underlying block device for |
| 17539 ** the specified file. This is almost always 512 bytes, but may be |
| 17540 ** larger for some devices. |
| 17541 ** |
| 17542 ** SQLite code assumes this function cannot fail. It also assumes that |
| 17543 ** if two files are created in the same file-system directory (i.e. |
| 17544 ** a database and its journal file) that the sector size will be the |
| 17545 ** same for both. |
| 17546 */ |
| 17547 static int winSectorSize(sqlite3_file *id){ |
| 17548 (void)id; |
| 17549 return SQLITE_DEFAULT_SECTOR_SIZE; |
| 17550 } |
| 17551 |
| 17552 /* |
| 17553 ** Return a vector of device characteristics. |
| 17554 */ |
| 17555 static int winDeviceCharacteristics(sqlite3_file *id){ |
| 17556 winFile *p = (winFile*)id; |
| 17557 return SQLITE_IOCAP_UNDELETABLE_WHEN_OPEN | |
| 17558 ((p->ctrlFlags & WINFILE_PSOW)?SQLITE_IOCAP_POWERSAFE_OVERWRITE:0); |
| 17559 } |
| 17560 |
| 17561 /* |
| 17562 ** Windows will only let you create file view mappings |
| 17563 ** on allocation size granularity boundaries. |
| 17564 ** During sqlite3_os_init() we do a GetSystemInfo() |
| 17565 ** to get the granularity size. |
| 17566 */ |
| 17567 static SYSTEM_INFO winSysInfo; |
| 17568 |
| 17569 #ifndef SQLITE_OMIT_WAL |
| 17570 |
| 17571 /* |
| 17572 ** Helper functions to obtain and relinquish the global mutex. The |
| 17573 ** global mutex is used to protect the winLockInfo objects used by |
| 17574 ** this file, all of which may be shared by multiple threads. |
| 17575 ** |
| 17576 ** Function winShmMutexHeld() is used to assert() that the global mutex |
| 17577 ** is held when required. This function is only used as part of assert() |
| 17578 ** statements. e.g. |
| 17579 ** |
| 17580 ** winShmEnterMutex() |
| 17581 ** assert( winShmMutexHeld() ); |
| 17582 ** winShmLeaveMutex() |
| 17583 */ |
| 17584 static void winShmEnterMutex(void){ |
| 17585 sqlite3_mutex_enter(sqlite3MutexAlloc(SQLITE_MUTEX_STATIC_VFS1)); |
| 17586 } |
| 17587 static void winShmLeaveMutex(void){ |
| 17588 sqlite3_mutex_leave(sqlite3MutexAlloc(SQLITE_MUTEX_STATIC_VFS1)); |
| 17589 } |
| 17590 #ifndef NDEBUG |
| 17591 static int winShmMutexHeld(void) { |
| 17592 return sqlite3_mutex_held(sqlite3MutexAlloc(SQLITE_MUTEX_STATIC_VFS1)); |
| 17593 } |
| 17594 #endif |
| 17595 |
| 17596 /* |
| 17597 ** Object used to represent a single file opened and mmapped to provide |
| 17598 ** shared memory. When multiple threads all reference the same |
| 17599 ** log-summary, each thread has its own winFile object, but they all |
| 17600 ** point to a single instance of this object. In other words, each |
| 17601 ** log-summary is opened only once per process. |
| 17602 ** |
| 17603 ** winShmMutexHeld() must be true when creating or destroying |
| 17604 ** this object or while reading or writing the following fields: |
| 17605 ** |
| 17606 ** nRef |
| 17607 ** pNext |
| 17608 ** |
| 17609 ** The following fields are read-only after the object is created: |
| 17610 ** |
| 17611 ** fid |
| 17612 ** zFilename |
| 17613 ** |
| 17614 ** Either winShmNode.mutex must be held or winShmNode.nRef==0 and |
| 17615 ** winShmMutexHeld() is true when reading or writing any other field |
| 17616 ** in this structure. |
| 17617 ** |
| 17618 */ |
| 17619 struct winShmNode { |
| 17620 sqlite3_mutex *mutex; /* Mutex to access this object */ |
| 17621 char *zFilename; /* Name of the file */ |
| 17622 winFile hFile; /* File handle from winOpen */ |
| 17623 |
| 17624 int szRegion; /* Size of shared-memory regions */ |
| 17625 int nRegion; /* Size of array apRegion */ |
| 17626 struct ShmRegion { |
| 17627 HANDLE hMap; /* File handle from CreateFileMapping */ |
| 17628 void *pMap; |
| 17629 } *aRegion; |
| 17630 DWORD lastErrno; /* The Windows errno from the last I/O error */ |
| 17631 |
| 17632 int nRef; /* Number of winShm objects pointing to this */ |
| 17633 winShm *pFirst; /* All winShm objects pointing to this */ |
| 17634 winShmNode *pNext; /* Next in list of all winShmNode objects */ |
| 17635 #if defined(SQLITE_DEBUG) || defined(SQLITE_HAVE_OS_TRACE) |
| 17636 u8 nextShmId; /* Next available winShm.id value */ |
| 17637 #endif |
| 17638 }; |
| 17639 |
| 17640 /* |
| 17641 ** A global array of all winShmNode objects. |
| 17642 ** |
| 17643 ** The winShmMutexHeld() must be true while reading or writing this list. |
| 17644 */ |
| 17645 static winShmNode *winShmNodeList = 0; |
| 17646 |
| 17647 /* |
| 17648 ** Structure used internally by this VFS to record the state of an |
| 17649 ** open shared memory connection. |
| 17650 ** |
| 17651 ** The following fields are initialized when this object is created and |
| 17652 ** are read-only thereafter: |
| 17653 ** |
| 17654 ** winShm.pShmNode |
| 17655 ** winShm.id |
| 17656 ** |
| 17657 ** All other fields are read/write. The winShm.pShmNode->mutex must be held |
| 17658 ** while accessing any read/write fields. |
| 17659 */ |
| 17660 struct winShm { |
| 17661 winShmNode *pShmNode; /* The underlying winShmNode object */ |
| 17662 winShm *pNext; /* Next winShm with the same winShmNode */ |
| 17663 u8 hasMutex; /* True if holding the winShmNode mutex */ |
| 17664 u16 sharedMask; /* Mask of shared locks held */ |
| 17665 u16 exclMask; /* Mask of exclusive locks held */ |
| 17666 #if defined(SQLITE_DEBUG) || defined(SQLITE_HAVE_OS_TRACE) |
| 17667 u8 id; /* Id of this connection with its winShmNode */ |
| 17668 #endif |
| 17669 }; |
| 17670 |
| 17671 /* |
| 17672 ** Constants used for locking |
| 17673 */ |
| 17674 #define WIN_SHM_BASE ((22+SQLITE_SHM_NLOCK)*4) /* first lock byte */ |
| 17675 #define WIN_SHM_DMS (WIN_SHM_BASE+SQLITE_SHM_NLOCK) /* deadman switch */ |
| 17676 |
| 17677 /* |
| 17678 ** Apply advisory locks for all n bytes beginning at ofst. |
| 17679 */ |
| 17680 #define _SHM_UNLCK 1 |
| 17681 #define _SHM_RDLCK 2 |
| 17682 #define _SHM_WRLCK 3 |
| 17683 static int winShmSystemLock( |
| 17684 winShmNode *pFile, /* Apply locks to this open shared-memory segment */ |
| 17685 int lockType, /* _SHM_UNLCK, _SHM_RDLCK, or _SHM_WRLCK */ |
| 17686 int ofst, /* Offset to first byte to be locked/unlocked */ |
| 17687 int nByte /* Number of bytes to lock or unlock */ |
| 17688 ){ |
| 17689 int rc = 0; /* Result code form Lock/UnlockFileEx() */ |
| 17690 |
| 17691 /* Access to the winShmNode object is serialized by the caller */ |
| 17692 assert( sqlite3_mutex_held(pFile->mutex) || pFile->nRef==0 ); |
| 17693 |
| 17694 OSTRACE(("SHM-LOCK file=%p, lock=%d, offset=%d, size=%d\n", |
| 17695 pFile->hFile.h, lockType, ofst, nByte)); |
| 17696 |
| 17697 /* Release/Acquire the system-level lock */ |
| 17698 if( lockType==_SHM_UNLCK ){ |
| 17699 rc = winUnlockFile(&pFile->hFile.h, ofst, 0, nByte, 0); |
| 17700 }else{ |
| 17701 /* Initialize the locking parameters */ |
| 17702 DWORD dwFlags = LOCKFILE_FAIL_IMMEDIATELY; |
| 17703 if( lockType == _SHM_WRLCK ) dwFlags |= LOCKFILE_EXCLUSIVE_LOCK; |
| 17704 rc = winLockFile(&pFile->hFile.h, dwFlags, ofst, 0, nByte, 0); |
| 17705 } |
| 17706 |
| 17707 if( rc!= 0 ){ |
| 17708 rc = SQLITE_OK; |
| 17709 }else{ |
| 17710 pFile->lastErrno = osGetLastError(); |
| 17711 rc = SQLITE_BUSY; |
| 17712 } |
| 17713 |
| 17714 OSTRACE(("SHM-LOCK file=%p, func=%s, errno=%lu, rc=%s\n", |
| 17715 pFile->hFile.h, (lockType == _SHM_UNLCK) ? "winUnlockFile" : |
| 17716 "winLockFile", pFile->lastErrno, sqlite3ErrName(rc))); |
| 17717 |
| 17718 return rc; |
| 17719 } |
| 17720 |
| 17721 /* Forward references to VFS methods */ |
| 17722 static int winOpen(sqlite3_vfs*,const char*,sqlite3_file*,int,int*); |
| 17723 static int winDelete(sqlite3_vfs *,const char*,int); |
| 17724 |
| 17725 /* |
| 17726 ** Purge the winShmNodeList list of all entries with winShmNode.nRef==0. |
| 17727 ** |
| 17728 ** This is not a VFS shared-memory method; it is a utility function called |
| 17729 ** by VFS shared-memory methods. |
| 17730 */ |
| 17731 static void winShmPurge(sqlite3_vfs *pVfs, int deleteFlag){ |
| 17732 winShmNode **pp; |
| 17733 winShmNode *p; |
| 17734 assert( winShmMutexHeld() ); |
| 17735 OSTRACE(("SHM-PURGE pid=%lu, deleteFlag=%d\n", |
| 17736 osGetCurrentProcessId(), deleteFlag)); |
| 17737 pp = &winShmNodeList; |
| 17738 while( (p = *pp)!=0 ){ |
| 17739 if( p->nRef==0 ){ |
| 17740 int i; |
| 17741 if( p->mutex ){ sqlite3_mutex_free(p->mutex); } |
| 17742 for(i=0; i<p->nRegion; i++){ |
| 17743 BOOL bRc = osUnmapViewOfFile(p->aRegion[i].pMap); |
| 17744 OSTRACE(("SHM-PURGE-UNMAP pid=%lu, region=%d, rc=%s\n", |
| 17745 osGetCurrentProcessId(), i, bRc ? "ok" : "failed")); |
| 17746 UNUSED_VARIABLE_VALUE(bRc); |
| 17747 bRc = osCloseHandle(p->aRegion[i].hMap); |
| 17748 OSTRACE(("SHM-PURGE-CLOSE pid=%lu, region=%d, rc=%s\n", |
| 17749 osGetCurrentProcessId(), i, bRc ? "ok" : "failed")); |
| 17750 UNUSED_VARIABLE_VALUE(bRc); |
| 17751 } |
| 17752 if( p->hFile.h!=NULL && p->hFile.h!=INVALID_HANDLE_VALUE ){ |
| 17753 SimulateIOErrorBenign(1); |
| 17754 winClose((sqlite3_file *)&p->hFile); |
| 17755 SimulateIOErrorBenign(0); |
| 17756 } |
| 17757 if( deleteFlag ){ |
| 17758 SimulateIOErrorBenign(1); |
| 17759 sqlite3BeginBenignMalloc(); |
| 17760 winDelete(pVfs, p->zFilename, 0); |
| 17761 sqlite3EndBenignMalloc(); |
| 17762 SimulateIOErrorBenign(0); |
| 17763 } |
| 17764 *pp = p->pNext; |
| 17765 sqlite3_free(p->aRegion); |
| 17766 sqlite3_free(p); |
| 17767 }else{ |
| 17768 pp = &p->pNext; |
| 17769 } |
| 17770 } |
| 17771 } |
| 17772 |
| 17773 /* |
| 17774 ** Open the shared-memory area associated with database file pDbFd. |
| 17775 ** |
| 17776 ** When opening a new shared-memory file, if no other instances of that |
| 17777 ** file are currently open, in this process or in other processes, then |
| 17778 ** the file must be truncated to zero length or have its header cleared. |
| 17779 */ |
| 17780 static int winOpenSharedMemory(winFile *pDbFd){ |
| 17781 struct winShm *p; /* The connection to be opened */ |
| 17782 struct winShmNode *pShmNode = 0; /* The underlying mmapped file */ |
| 17783 int rc; /* Result code */ |
| 17784 struct winShmNode *pNew; /* Newly allocated winShmNode */ |
| 17785 int nName; /* Size of zName in bytes */ |
| 17786 |
| 17787 assert( pDbFd->pShm==0 ); /* Not previously opened */ |
| 17788 |
| 17789 /* Allocate space for the new sqlite3_shm object. Also speculatively |
| 17790 ** allocate space for a new winShmNode and filename. |
| 17791 */ |
| 17792 p = sqlite3MallocZero( sizeof(*p) ); |
| 17793 if( p==0 ) return SQLITE_IOERR_NOMEM; |
| 17794 nName = sqlite3Strlen30(pDbFd->zPath); |
| 17795 pNew = sqlite3MallocZero( sizeof(*pShmNode) + nName + 17 ); |
| 17796 if( pNew==0 ){ |
| 17797 sqlite3_free(p); |
| 17798 return SQLITE_IOERR_NOMEM; |
| 17799 } |
| 17800 pNew->zFilename = (char*)&pNew[1]; |
| 17801 sqlite3_snprintf(nName+15, pNew->zFilename, "%s-shm", pDbFd->zPath); |
| 17802 sqlite3FileSuffix3(pDbFd->zPath, pNew->zFilename); |
| 17803 |
| 17804 /* Look to see if there is an existing winShmNode that can be used. |
| 17805 ** If no matching winShmNode currently exists, create a new one. |
| 17806 */ |
| 17807 winShmEnterMutex(); |
| 17808 for(pShmNode = winShmNodeList; pShmNode; pShmNode=pShmNode->pNext){ |
| 17809 /* TBD need to come up with better match here. Perhaps |
| 17810 ** use FILE_ID_BOTH_DIR_INFO Structure. |
| 17811 */ |
| 17812 if( sqlite3StrICmp(pShmNode->zFilename, pNew->zFilename)==0 ) break; |
| 17813 } |
| 17814 if( pShmNode ){ |
| 17815 sqlite3_free(pNew); |
| 17816 }else{ |
| 17817 pShmNode = pNew; |
| 17818 pNew = 0; |
| 17819 ((winFile*)(&pShmNode->hFile))->h = INVALID_HANDLE_VALUE; |
| 17820 pShmNode->pNext = winShmNodeList; |
| 17821 winShmNodeList = pShmNode; |
| 17822 |
| 17823 pShmNode->mutex = sqlite3_mutex_alloc(SQLITE_MUTEX_FAST); |
| 17824 if( pShmNode->mutex==0 ){ |
| 17825 rc = SQLITE_IOERR_NOMEM; |
| 17826 goto shm_open_err; |
| 17827 } |
| 17828 |
| 17829 rc = winOpen(pDbFd->pVfs, |
| 17830 pShmNode->zFilename, /* Name of the file (UTF-8) */ |
| 17831 (sqlite3_file*)&pShmNode->hFile, /* File handle here */ |
| 17832 SQLITE_OPEN_WAL | SQLITE_OPEN_READWRITE | SQLITE_OPEN_CREATE, |
| 17833 0); |
| 17834 if( SQLITE_OK!=rc ){ |
| 17835 goto shm_open_err; |
| 17836 } |
| 17837 |
| 17838 /* Check to see if another process is holding the dead-man switch. |
| 17839 ** If not, truncate the file to zero length. |
| 17840 */ |
| 17841 if( winShmSystemLock(pShmNode, _SHM_WRLCK, WIN_SHM_DMS, 1)==SQLITE_OK ){ |
| 17842 rc = winTruncate((sqlite3_file *)&pShmNode->hFile, 0); |
| 17843 if( rc!=SQLITE_OK ){ |
| 17844 rc = winLogError(SQLITE_IOERR_SHMOPEN, osGetLastError(), |
| 17845 "winOpenShm", pDbFd->zPath); |
| 17846 } |
| 17847 } |
| 17848 if( rc==SQLITE_OK ){ |
| 17849 winShmSystemLock(pShmNode, _SHM_UNLCK, WIN_SHM_DMS, 1); |
| 17850 rc = winShmSystemLock(pShmNode, _SHM_RDLCK, WIN_SHM_DMS, 1); |
| 17851 } |
| 17852 if( rc ) goto shm_open_err; |
| 17853 } |
| 17854 |
| 17855 /* Make the new connection a child of the winShmNode */ |
| 17856 p->pShmNode = pShmNode; |
| 17857 #if defined(SQLITE_DEBUG) || defined(SQLITE_HAVE_OS_TRACE) |
| 17858 p->id = pShmNode->nextShmId++; |
| 17859 #endif |
| 17860 pShmNode->nRef++; |
| 17861 pDbFd->pShm = p; |
| 17862 winShmLeaveMutex(); |
| 17863 |
| 17864 /* The reference count on pShmNode has already been incremented under |
| 17865 ** the cover of the winShmEnterMutex() mutex and the pointer from the |
| 17866 ** new (struct winShm) object to the pShmNode has been set. All that is |
| 17867 ** left to do is to link the new object into the linked list starting |
| 17868 ** at pShmNode->pFirst. This must be done while holding the pShmNode->mutex |
| 17869 ** mutex. |
| 17870 */ |
| 17871 sqlite3_mutex_enter(pShmNode->mutex); |
| 17872 p->pNext = pShmNode->pFirst; |
| 17873 pShmNode->pFirst = p; |
| 17874 sqlite3_mutex_leave(pShmNode->mutex); |
| 17875 return SQLITE_OK; |
| 17876 |
| 17877 /* Jump here on any error */ |
| 17878 shm_open_err: |
| 17879 winShmSystemLock(pShmNode, _SHM_UNLCK, WIN_SHM_DMS, 1); |
| 17880 winShmPurge(pDbFd->pVfs, 0); /* This call frees pShmNode if required */ |
| 17881 sqlite3_free(p); |
| 17882 sqlite3_free(pNew); |
| 17883 winShmLeaveMutex(); |
| 17884 return rc; |
| 17885 } |
| 17886 |
| 17887 /* |
| 17888 ** Close a connection to shared-memory. Delete the underlying |
| 17889 ** storage if deleteFlag is true. |
| 17890 */ |
| 17891 static int winShmUnmap( |
| 17892 sqlite3_file *fd, /* Database holding shared memory */ |
| 17893 int deleteFlag /* Delete after closing if true */ |
| 17894 ){ |
| 17895 winFile *pDbFd; /* Database holding shared-memory */ |
| 17896 winShm *p; /* The connection to be closed */ |
| 17897 winShmNode *pShmNode; /* The underlying shared-memory file */ |
| 17898 winShm **pp; /* For looping over sibling connections */ |
| 17899 |
| 17900 pDbFd = (winFile*)fd; |
| 17901 p = pDbFd->pShm; |
| 17902 if( p==0 ) return SQLITE_OK; |
| 17903 pShmNode = p->pShmNode; |
| 17904 |
| 17905 /* Remove connection p from the set of connections associated |
| 17906 ** with pShmNode */ |
| 17907 sqlite3_mutex_enter(pShmNode->mutex); |
| 17908 for(pp=&pShmNode->pFirst; (*pp)!=p; pp = &(*pp)->pNext){} |
| 17909 *pp = p->pNext; |
| 17910 |
| 17911 /* Free the connection p */ |
| 17912 sqlite3_free(p); |
| 17913 pDbFd->pShm = 0; |
| 17914 sqlite3_mutex_leave(pShmNode->mutex); |
| 17915 |
| 17916 /* If pShmNode->nRef has reached 0, then close the underlying |
| 17917 ** shared-memory file, too */ |
| 17918 winShmEnterMutex(); |
| 17919 assert( pShmNode->nRef>0 ); |
| 17920 pShmNode->nRef--; |
| 17921 if( pShmNode->nRef==0 ){ |
| 17922 winShmPurge(pDbFd->pVfs, deleteFlag); |
| 17923 } |
| 17924 winShmLeaveMutex(); |
| 17925 |
| 17926 return SQLITE_OK; |
| 17927 } |
| 17928 |
| 17929 /* |
| 17930 ** Change the lock state for a shared-memory segment. |
| 17931 */ |
| 17932 static int winShmLock( |
| 17933 sqlite3_file *fd, /* Database file holding the shared memory */ |
| 17934 int ofst, /* First lock to acquire or release */ |
| 17935 int n, /* Number of locks to acquire or release */ |
| 17936 int flags /* What to do with the lock */ |
| 17937 ){ |
| 17938 winFile *pDbFd = (winFile*)fd; /* Connection holding shared memory */ |
| 17939 winShm *p = pDbFd->pShm; /* The shared memory being locked */ |
| 17940 winShm *pX; /* For looping over all siblings */ |
| 17941 winShmNode *pShmNode = p->pShmNode; |
| 17942 int rc = SQLITE_OK; /* Result code */ |
| 17943 u16 mask; /* Mask of locks to take or release */ |
| 17944 |
| 17945 assert( ofst>=0 && ofst+n<=SQLITE_SHM_NLOCK ); |
| 17946 assert( n>=1 ); |
| 17947 assert( flags==(SQLITE_SHM_LOCK | SQLITE_SHM_SHARED) |
| 17948 || flags==(SQLITE_SHM_LOCK | SQLITE_SHM_EXCLUSIVE) |
| 17949 || flags==(SQLITE_SHM_UNLOCK | SQLITE_SHM_SHARED) |
| 17950 || flags==(SQLITE_SHM_UNLOCK | SQLITE_SHM_EXCLUSIVE) ); |
| 17951 assert( n==1 || (flags & SQLITE_SHM_EXCLUSIVE)!=0 ); |
| 17952 |
| 17953 mask = (u16)((1U<<(ofst+n)) - (1U<<ofst)); |
| 17954 assert( n>1 || mask==(1<<ofst) ); |
| 17955 sqlite3_mutex_enter(pShmNode->mutex); |
| 17956 if( flags & SQLITE_SHM_UNLOCK ){ |
| 17957 u16 allMask = 0; /* Mask of locks held by siblings */ |
| 17958 |
| 17959 /* See if any siblings hold this same lock */ |
| 17960 for(pX=pShmNode->pFirst; pX; pX=pX->pNext){ |
| 17961 if( pX==p ) continue; |
| 17962 assert( (pX->exclMask & (p->exclMask|p->sharedMask))==0 ); |
| 17963 allMask |= pX->sharedMask; |
| 17964 } |
| 17965 |
| 17966 /* Unlock the system-level locks */ |
| 17967 if( (mask & allMask)==0 ){ |
| 17968 rc = winShmSystemLock(pShmNode, _SHM_UNLCK, ofst+WIN_SHM_BASE, n); |
| 17969 }else{ |
| 17970 rc = SQLITE_OK; |
| 17971 } |
| 17972 |
| 17973 /* Undo the local locks */ |
| 17974 if( rc==SQLITE_OK ){ |
| 17975 p->exclMask &= ~mask; |
| 17976 p->sharedMask &= ~mask; |
| 17977 } |
| 17978 }else if( flags & SQLITE_SHM_SHARED ){ |
| 17979 u16 allShared = 0; /* Union of locks held by connections other than "p" */ |
| 17980 |
| 17981 /* Find out which shared locks are already held by sibling connections. |
| 17982 ** If any sibling already holds an exclusive lock, go ahead and return |
| 17983 ** SQLITE_BUSY. |
| 17984 */ |
| 17985 for(pX=pShmNode->pFirst; pX; pX=pX->pNext){ |
| 17986 if( (pX->exclMask & mask)!=0 ){ |
| 17987 rc = SQLITE_BUSY; |
| 17988 break; |
| 17989 } |
| 17990 allShared |= pX->sharedMask; |
| 17991 } |
| 17992 |
| 17993 /* Get shared locks at the system level, if necessary */ |
| 17994 if( rc==SQLITE_OK ){ |
| 17995 if( (allShared & mask)==0 ){ |
| 17996 rc = winShmSystemLock(pShmNode, _SHM_RDLCK, ofst+WIN_SHM_BASE, n); |
| 17997 }else{ |
| 17998 rc = SQLITE_OK; |
| 17999 } |
| 18000 } |
| 18001 |
| 18002 /* Get the local shared locks */ |
| 18003 if( rc==SQLITE_OK ){ |
| 18004 p->sharedMask |= mask; |
| 18005 } |
| 18006 }else{ |
| 18007 /* Make sure no sibling connections hold locks that will block this |
| 18008 ** lock. If any do, return SQLITE_BUSY right away. |
| 18009 */ |
| 18010 for(pX=pShmNode->pFirst; pX; pX=pX->pNext){ |
| 18011 if( (pX->exclMask & mask)!=0 || (pX->sharedMask & mask)!=0 ){ |
| 18012 rc = SQLITE_BUSY; |
| 18013 break; |
| 18014 } |
| 18015 } |
| 18016 |
| 18017 /* Get the exclusive locks at the system level. Then if successful |
| 18018 ** also mark the local connection as being locked. |
| 18019 */ |
| 18020 if( rc==SQLITE_OK ){ |
| 18021 rc = winShmSystemLock(pShmNode, _SHM_WRLCK, ofst+WIN_SHM_BASE, n); |
| 18022 if( rc==SQLITE_OK ){ |
| 18023 assert( (p->sharedMask & mask)==0 ); |
| 18024 p->exclMask |= mask; |
| 18025 } |
| 18026 } |
| 18027 } |
| 18028 sqlite3_mutex_leave(pShmNode->mutex); |
| 18029 OSTRACE(("SHM-LOCK pid=%lu, id=%d, sharedMask=%03x, exclMask=%03x, rc=%s\n", |
| 18030 osGetCurrentProcessId(), p->id, p->sharedMask, p->exclMask, |
| 18031 sqlite3ErrName(rc))); |
| 18032 return rc; |
| 18033 } |
| 18034 |
| 18035 /* |
| 18036 ** Implement a memory barrier or memory fence on shared memory. |
| 18037 ** |
| 18038 ** All loads and stores begun before the barrier must complete before |
| 18039 ** any load or store begun after the barrier. |
| 18040 */ |
| 18041 static void winShmBarrier( |
| 18042 sqlite3_file *fd /* Database holding the shared memory */ |
| 18043 ){ |
| 18044 UNUSED_PARAMETER(fd); |
| 18045 sqlite3MemoryBarrier(); /* compiler-defined memory barrier */ |
| 18046 winShmEnterMutex(); /* Also mutex, for redundancy */ |
| 18047 winShmLeaveMutex(); |
| 18048 } |
| 18049 |
| 18050 /* |
| 18051 ** This function is called to obtain a pointer to region iRegion of the |
| 18052 ** shared-memory associated with the database file fd. Shared-memory regions |
| 18053 ** are numbered starting from zero. Each shared-memory region is szRegion |
| 18054 ** bytes in size. |
| 18055 ** |
| 18056 ** If an error occurs, an error code is returned and *pp is set to NULL. |
| 18057 ** |
| 18058 ** Otherwise, if the isWrite parameter is 0 and the requested shared-memory |
| 18059 ** region has not been allocated (by any client, including one running in a |
| 18060 ** separate process), then *pp is set to NULL and SQLITE_OK returned. If |
| 18061 ** isWrite is non-zero and the requested shared-memory region has not yet |
| 18062 ** been allocated, it is allocated by this function. |
| 18063 ** |
| 18064 ** If the shared-memory region has already been allocated or is allocated by |
| 18065 ** this call as described above, then it is mapped into this processes |
| 18066 ** address space (if it is not already), *pp is set to point to the mapped |
| 18067 ** memory and SQLITE_OK returned. |
| 18068 */ |
| 18069 static int winShmMap( |
| 18070 sqlite3_file *fd, /* Handle open on database file */ |
| 18071 int iRegion, /* Region to retrieve */ |
| 18072 int szRegion, /* Size of regions */ |
| 18073 int isWrite, /* True to extend file if necessary */ |
| 18074 void volatile **pp /* OUT: Mapped memory */ |
| 18075 ){ |
| 18076 winFile *pDbFd = (winFile*)fd; |
| 18077 winShm *pShm = pDbFd->pShm; |
| 18078 winShmNode *pShmNode; |
| 18079 int rc = SQLITE_OK; |
| 18080 |
| 18081 if( !pShm ){ |
| 18082 rc = winOpenSharedMemory(pDbFd); |
| 18083 if( rc!=SQLITE_OK ) return rc; |
| 18084 pShm = pDbFd->pShm; |
| 18085 } |
| 18086 pShmNode = pShm->pShmNode; |
| 18087 |
| 18088 sqlite3_mutex_enter(pShmNode->mutex); |
| 18089 assert( szRegion==pShmNode->szRegion || pShmNode->nRegion==0 ); |
| 18090 |
| 18091 if( pShmNode->nRegion<=iRegion ){ |
| 18092 struct ShmRegion *apNew; /* New aRegion[] array */ |
| 18093 int nByte = (iRegion+1)*szRegion; /* Minimum required file size */ |
| 18094 sqlite3_int64 sz; /* Current size of wal-index file */ |
| 18095 |
| 18096 pShmNode->szRegion = szRegion; |
| 18097 |
| 18098 /* The requested region is not mapped into this processes address space. |
| 18099 ** Check to see if it has been allocated (i.e. if the wal-index file is |
| 18100 ** large enough to contain the requested region). |
| 18101 */ |
| 18102 rc = winFileSize((sqlite3_file *)&pShmNode->hFile, &sz); |
| 18103 if( rc!=SQLITE_OK ){ |
| 18104 rc = winLogError(SQLITE_IOERR_SHMSIZE, osGetLastError(), |
| 18105 "winShmMap1", pDbFd->zPath); |
| 18106 goto shmpage_out; |
| 18107 } |
| 18108 |
| 18109 if( sz<nByte ){ |
| 18110 /* The requested memory region does not exist. If isWrite is set to |
| 18111 ** zero, exit early. *pp will be set to NULL and SQLITE_OK returned. |
| 18112 ** |
| 18113 ** Alternatively, if isWrite is non-zero, use ftruncate() to allocate |
| 18114 ** the requested memory region. |
| 18115 */ |
| 18116 if( !isWrite ) goto shmpage_out; |
| 18117 rc = winTruncate((sqlite3_file *)&pShmNode->hFile, nByte); |
| 18118 if( rc!=SQLITE_OK ){ |
| 18119 rc = winLogError(SQLITE_IOERR_SHMSIZE, osGetLastError(), |
| 18120 "winShmMap2", pDbFd->zPath); |
| 18121 goto shmpage_out; |
| 18122 } |
| 18123 } |
| 18124 |
| 18125 /* Map the requested memory region into this processes address space. */ |
| 18126 apNew = (struct ShmRegion *)sqlite3_realloc64( |
| 18127 pShmNode->aRegion, (iRegion+1)*sizeof(apNew[0]) |
| 18128 ); |
| 18129 if( !apNew ){ |
| 18130 rc = SQLITE_IOERR_NOMEM; |
| 18131 goto shmpage_out; |
| 18132 } |
| 18133 pShmNode->aRegion = apNew; |
| 18134 |
| 18135 while( pShmNode->nRegion<=iRegion ){ |
| 18136 HANDLE hMap = NULL; /* file-mapping handle */ |
| 18137 void *pMap = 0; /* Mapped memory region */ |
| 18138 |
| 18139 #if SQLITE_OS_WINRT |
| 18140 hMap = osCreateFileMappingFromApp(pShmNode->hFile.h, |
| 18141 NULL, PAGE_READWRITE, nByte, NULL |
| 18142 ); |
| 18143 #elif defined(SQLITE_WIN32_HAS_WIDE) |
| 18144 hMap = osCreateFileMappingW(pShmNode->hFile.h, |
| 18145 NULL, PAGE_READWRITE, 0, nByte, NULL |
| 18146 ); |
| 18147 #elif defined(SQLITE_WIN32_HAS_ANSI) |
| 18148 hMap = osCreateFileMappingA(pShmNode->hFile.h, |
| 18149 NULL, PAGE_READWRITE, 0, nByte, NULL |
| 18150 ); |
| 18151 #endif |
| 18152 OSTRACE(("SHM-MAP-CREATE pid=%lu, region=%d, size=%d, rc=%s\n", |
| 18153 osGetCurrentProcessId(), pShmNode->nRegion, nByte, |
| 18154 hMap ? "ok" : "failed")); |
| 18155 if( hMap ){ |
| 18156 int iOffset = pShmNode->nRegion*szRegion; |
| 18157 int iOffsetShift = iOffset % winSysInfo.dwAllocationGranularity; |
| 18158 #if SQLITE_OS_WINRT |
| 18159 pMap = osMapViewOfFileFromApp(hMap, FILE_MAP_WRITE | FILE_MAP_READ, |
| 18160 iOffset - iOffsetShift, szRegion + iOffsetShift |
| 18161 ); |
| 18162 #else |
| 18163 pMap = osMapViewOfFile(hMap, FILE_MAP_WRITE | FILE_MAP_READ, |
| 18164 0, iOffset - iOffsetShift, szRegion + iOffsetShift |
| 18165 ); |
| 18166 #endif |
| 18167 OSTRACE(("SHM-MAP-MAP pid=%lu, region=%d, offset=%d, size=%d, rc=%s\n", |
| 18168 osGetCurrentProcessId(), pShmNode->nRegion, iOffset, |
| 18169 szRegion, pMap ? "ok" : "failed")); |
| 18170 } |
| 18171 if( !pMap ){ |
| 18172 pShmNode->lastErrno = osGetLastError(); |
| 18173 rc = winLogError(SQLITE_IOERR_SHMMAP, pShmNode->lastErrno, |
| 18174 "winShmMap3", pDbFd->zPath); |
| 18175 if( hMap ) osCloseHandle(hMap); |
| 18176 goto shmpage_out; |
| 18177 } |
| 18178 |
| 18179 pShmNode->aRegion[pShmNode->nRegion].pMap = pMap; |
| 18180 pShmNode->aRegion[pShmNode->nRegion].hMap = hMap; |
| 18181 pShmNode->nRegion++; |
| 18182 } |
| 18183 } |
| 18184 |
| 18185 shmpage_out: |
| 18186 if( pShmNode->nRegion>iRegion ){ |
| 18187 int iOffset = iRegion*szRegion; |
| 18188 int iOffsetShift = iOffset % winSysInfo.dwAllocationGranularity; |
| 18189 char *p = (char *)pShmNode->aRegion[iRegion].pMap; |
| 18190 *pp = (void *)&p[iOffsetShift]; |
| 18191 }else{ |
| 18192 *pp = 0; |
| 18193 } |
| 18194 sqlite3_mutex_leave(pShmNode->mutex); |
| 18195 return rc; |
| 18196 } |
| 18197 |
| 18198 #else |
| 18199 # define winShmMap 0 |
| 18200 # define winShmLock 0 |
| 18201 # define winShmBarrier 0 |
| 18202 # define winShmUnmap 0 |
| 18203 #endif /* #ifndef SQLITE_OMIT_WAL */ |
| 18204 |
| 18205 /* |
| 18206 ** Cleans up the mapped region of the specified file, if any. |
| 18207 */ |
| 18208 #if SQLITE_MAX_MMAP_SIZE>0 |
| 18209 static int winUnmapfile(winFile *pFile){ |
| 18210 assert( pFile!=0 ); |
| 18211 OSTRACE(("UNMAP-FILE pid=%lu, pFile=%p, hMap=%p, pMapRegion=%p, " |
| 18212 "mmapSize=%lld, mmapSizeActual=%lld, mmapSizeMax=%lld\n", |
| 18213 osGetCurrentProcessId(), pFile, pFile->hMap, pFile->pMapRegion, |
| 18214 pFile->mmapSize, pFile->mmapSizeActual, pFile->mmapSizeMax)); |
| 18215 if( pFile->pMapRegion ){ |
| 18216 if( !osUnmapViewOfFile(pFile->pMapRegion) ){ |
| 18217 pFile->lastErrno = osGetLastError(); |
| 18218 OSTRACE(("UNMAP-FILE pid=%lu, pFile=%p, pMapRegion=%p, " |
| 18219 "rc=SQLITE_IOERR_MMAP\n", osGetCurrentProcessId(), pFile, |
| 18220 pFile->pMapRegion)); |
| 18221 return winLogError(SQLITE_IOERR_MMAP, pFile->lastErrno, |
| 18222 "winUnmapfile1", pFile->zPath); |
| 18223 } |
| 18224 pFile->pMapRegion = 0; |
| 18225 pFile->mmapSize = 0; |
| 18226 pFile->mmapSizeActual = 0; |
| 18227 } |
| 18228 if( pFile->hMap!=NULL ){ |
| 18229 if( !osCloseHandle(pFile->hMap) ){ |
| 18230 pFile->lastErrno = osGetLastError(); |
| 18231 OSTRACE(("UNMAP-FILE pid=%lu, pFile=%p, hMap=%p, rc=SQLITE_IOERR_MMAP\n", |
| 18232 osGetCurrentProcessId(), pFile, pFile->hMap)); |
| 18233 return winLogError(SQLITE_IOERR_MMAP, pFile->lastErrno, |
| 18234 "winUnmapfile2", pFile->zPath); |
| 18235 } |
| 18236 pFile->hMap = NULL; |
| 18237 } |
| 18238 OSTRACE(("UNMAP-FILE pid=%lu, pFile=%p, rc=SQLITE_OK\n", |
| 18239 osGetCurrentProcessId(), pFile)); |
| 18240 return SQLITE_OK; |
| 18241 } |
| 18242 |
| 18243 /* |
| 18244 ** Memory map or remap the file opened by file-descriptor pFd (if the file |
| 18245 ** is already mapped, the existing mapping is replaced by the new). Or, if |
| 18246 ** there already exists a mapping for this file, and there are still |
| 18247 ** outstanding xFetch() references to it, this function is a no-op. |
| 18248 ** |
| 18249 ** If parameter nByte is non-negative, then it is the requested size of |
| 18250 ** the mapping to create. Otherwise, if nByte is less than zero, then the |
| 18251 ** requested size is the size of the file on disk. The actual size of the |
| 18252 ** created mapping is either the requested size or the value configured |
| 18253 ** using SQLITE_FCNTL_MMAP_SIZE, whichever is smaller. |
| 18254 ** |
| 18255 ** SQLITE_OK is returned if no error occurs (even if the mapping is not |
| 18256 ** recreated as a result of outstanding references) or an SQLite error |
| 18257 ** code otherwise. |
| 18258 */ |
| 18259 static int winMapfile(winFile *pFd, sqlite3_int64 nByte){ |
| 18260 sqlite3_int64 nMap = nByte; |
| 18261 int rc; |
| 18262 |
| 18263 assert( nMap>=0 || pFd->nFetchOut==0 ); |
| 18264 OSTRACE(("MAP-FILE pid=%lu, pFile=%p, size=%lld\n", |
| 18265 osGetCurrentProcessId(), pFd, nByte)); |
| 18266 |
| 18267 if( pFd->nFetchOut>0 ) return SQLITE_OK; |
| 18268 |
| 18269 if( nMap<0 ){ |
| 18270 rc = winFileSize((sqlite3_file*)pFd, &nMap); |
| 18271 if( rc ){ |
| 18272 OSTRACE(("MAP-FILE pid=%lu, pFile=%p, rc=SQLITE_IOERR_FSTAT\n", |
| 18273 osGetCurrentProcessId(), pFd)); |
| 18274 return SQLITE_IOERR_FSTAT; |
| 18275 } |
| 18276 } |
| 18277 if( nMap>pFd->mmapSizeMax ){ |
| 18278 nMap = pFd->mmapSizeMax; |
| 18279 } |
| 18280 nMap &= ~(sqlite3_int64)(winSysInfo.dwPageSize - 1); |
| 18281 |
| 18282 if( nMap==0 && pFd->mmapSize>0 ){ |
| 18283 winUnmapfile(pFd); |
| 18284 } |
| 18285 if( nMap!=pFd->mmapSize ){ |
| 18286 void *pNew = 0; |
| 18287 DWORD protect = PAGE_READONLY; |
| 18288 DWORD flags = FILE_MAP_READ; |
| 18289 |
| 18290 winUnmapfile(pFd); |
| 18291 #ifdef SQLITE_MMAP_READWRITE |
| 18292 if( (pFd->ctrlFlags & WINFILE_RDONLY)==0 ){ |
| 18293 protect = PAGE_READWRITE; |
| 18294 flags |= FILE_MAP_WRITE; |
| 18295 } |
| 18296 #endif |
| 18297 #if SQLITE_OS_WINRT |
| 18298 pFd->hMap = osCreateFileMappingFromApp(pFd->h, NULL, protect, nMap, NULL); |
| 18299 #elif defined(SQLITE_WIN32_HAS_WIDE) |
| 18300 pFd->hMap = osCreateFileMappingW(pFd->h, NULL, protect, |
| 18301 (DWORD)((nMap>>32) & 0xffffffff), |
| 18302 (DWORD)(nMap & 0xffffffff), NULL); |
| 18303 #elif defined(SQLITE_WIN32_HAS_ANSI) |
| 18304 pFd->hMap = osCreateFileMappingA(pFd->h, NULL, protect, |
| 18305 (DWORD)((nMap>>32) & 0xffffffff), |
| 18306 (DWORD)(nMap & 0xffffffff), NULL); |
| 18307 #endif |
| 18308 if( pFd->hMap==NULL ){ |
| 18309 pFd->lastErrno = osGetLastError(); |
| 18310 rc = winLogError(SQLITE_IOERR_MMAP, pFd->lastErrno, |
| 18311 "winMapfile1", pFd->zPath); |
| 18312 /* Log the error, but continue normal operation using xRead/xWrite */ |
| 18313 OSTRACE(("MAP-FILE-CREATE pid=%lu, pFile=%p, rc=%s\n", |
| 18314 osGetCurrentProcessId(), pFd, sqlite3ErrName(rc))); |
| 18315 return SQLITE_OK; |
| 18316 } |
| 18317 assert( (nMap % winSysInfo.dwPageSize)==0 ); |
| 18318 assert( sizeof(SIZE_T)==sizeof(sqlite3_int64) || nMap<=0xffffffff ); |
| 18319 #if SQLITE_OS_WINRT |
| 18320 pNew = osMapViewOfFileFromApp(pFd->hMap, flags, 0, (SIZE_T)nMap); |
| 18321 #else |
| 18322 pNew = osMapViewOfFile(pFd->hMap, flags, 0, 0, (SIZE_T)nMap); |
| 18323 #endif |
| 18324 if( pNew==NULL ){ |
| 18325 osCloseHandle(pFd->hMap); |
| 18326 pFd->hMap = NULL; |
| 18327 pFd->lastErrno = osGetLastError(); |
| 18328 rc = winLogError(SQLITE_IOERR_MMAP, pFd->lastErrno, |
| 18329 "winMapfile2", pFd->zPath); |
| 18330 /* Log the error, but continue normal operation using xRead/xWrite */ |
| 18331 OSTRACE(("MAP-FILE-MAP pid=%lu, pFile=%p, rc=%s\n", |
| 18332 osGetCurrentProcessId(), pFd, sqlite3ErrName(rc))); |
| 18333 return SQLITE_OK; |
| 18334 } |
| 18335 pFd->pMapRegion = pNew; |
| 18336 pFd->mmapSize = nMap; |
| 18337 pFd->mmapSizeActual = nMap; |
| 18338 } |
| 18339 |
| 18340 OSTRACE(("MAP-FILE pid=%lu, pFile=%p, rc=SQLITE_OK\n", |
| 18341 osGetCurrentProcessId(), pFd)); |
| 18342 return SQLITE_OK; |
| 18343 } |
| 18344 #endif /* SQLITE_MAX_MMAP_SIZE>0 */ |
| 18345 |
| 18346 /* |
| 18347 ** If possible, return a pointer to a mapping of file fd starting at offset |
| 18348 ** iOff. The mapping must be valid for at least nAmt bytes. |
| 18349 ** |
| 18350 ** If such a pointer can be obtained, store it in *pp and return SQLITE_OK. |
| 18351 ** Or, if one cannot but no error occurs, set *pp to 0 and return SQLITE_OK. |
| 18352 ** Finally, if an error does occur, return an SQLite error code. The final |
| 18353 ** value of *pp is undefined in this case. |
| 18354 ** |
| 18355 ** If this function does return a pointer, the caller must eventually |
| 18356 ** release the reference by calling winUnfetch(). |
| 18357 */ |
| 18358 static int winFetch(sqlite3_file *fd, i64 iOff, int nAmt, void **pp){ |
| 18359 #if SQLITE_MAX_MMAP_SIZE>0 |
| 18360 winFile *pFd = (winFile*)fd; /* The underlying database file */ |
| 18361 #endif |
| 18362 *pp = 0; |
| 18363 |
| 18364 OSTRACE(("FETCH pid=%lu, pFile=%p, offset=%lld, amount=%d, pp=%p\n", |
| 18365 osGetCurrentProcessId(), fd, iOff, nAmt, pp)); |
| 18366 |
| 18367 #if SQLITE_MAX_MMAP_SIZE>0 |
| 18368 if( pFd->mmapSizeMax>0 ){ |
| 18369 if( pFd->pMapRegion==0 ){ |
| 18370 int rc = winMapfile(pFd, -1); |
| 18371 if( rc!=SQLITE_OK ){ |
| 18372 OSTRACE(("FETCH pid=%lu, pFile=%p, rc=%s\n", |
| 18373 osGetCurrentProcessId(), pFd, sqlite3ErrName(rc))); |
| 18374 return rc; |
| 18375 } |
| 18376 } |
| 18377 if( pFd->mmapSize >= iOff+nAmt ){ |
| 18378 *pp = &((u8 *)pFd->pMapRegion)[iOff]; |
| 18379 pFd->nFetchOut++; |
| 18380 } |
| 18381 } |
| 18382 #endif |
| 18383 |
| 18384 OSTRACE(("FETCH pid=%lu, pFile=%p, pp=%p, *pp=%p, rc=SQLITE_OK\n", |
| 18385 osGetCurrentProcessId(), fd, pp, *pp)); |
| 18386 return SQLITE_OK; |
| 18387 } |
| 18388 |
| 18389 /* |
| 18390 ** If the third argument is non-NULL, then this function releases a |
| 18391 ** reference obtained by an earlier call to winFetch(). The second |
| 18392 ** argument passed to this function must be the same as the corresponding |
| 18393 ** argument that was passed to the winFetch() invocation. |
| 18394 ** |
| 18395 ** Or, if the third argument is NULL, then this function is being called |
| 18396 ** to inform the VFS layer that, according to POSIX, any existing mapping |
| 18397 ** may now be invalid and should be unmapped. |
| 18398 */ |
| 18399 static int winUnfetch(sqlite3_file *fd, i64 iOff, void *p){ |
| 18400 #if SQLITE_MAX_MMAP_SIZE>0 |
| 18401 winFile *pFd = (winFile*)fd; /* The underlying database file */ |
| 18402 |
| 18403 /* If p==0 (unmap the entire file) then there must be no outstanding |
| 18404 ** xFetch references. Or, if p!=0 (meaning it is an xFetch reference), |
| 18405 ** then there must be at least one outstanding. */ |
| 18406 assert( (p==0)==(pFd->nFetchOut==0) ); |
| 18407 |
| 18408 /* If p!=0, it must match the iOff value. */ |
| 18409 assert( p==0 || p==&((u8 *)pFd->pMapRegion)[iOff] ); |
| 18410 |
| 18411 OSTRACE(("UNFETCH pid=%lu, pFile=%p, offset=%lld, p=%p\n", |
| 18412 osGetCurrentProcessId(), pFd, iOff, p)); |
| 18413 |
| 18414 if( p ){ |
| 18415 pFd->nFetchOut--; |
| 18416 }else{ |
| 18417 /* FIXME: If Windows truly always prevents truncating or deleting a |
| 18418 ** file while a mapping is held, then the following winUnmapfile() call |
| 18419 ** is unnecessary can be omitted - potentially improving |
| 18420 ** performance. */ |
| 18421 winUnmapfile(pFd); |
| 18422 } |
| 18423 |
| 18424 assert( pFd->nFetchOut>=0 ); |
| 18425 #endif |
| 18426 |
| 18427 OSTRACE(("UNFETCH pid=%lu, pFile=%p, rc=SQLITE_OK\n", |
| 18428 osGetCurrentProcessId(), fd)); |
| 18429 return SQLITE_OK; |
| 18430 } |
| 18431 |
| 18432 /* |
| 18433 ** Here ends the implementation of all sqlite3_file methods. |
| 18434 ** |
| 18435 ********************** End sqlite3_file Methods ******************************* |
| 18436 ******************************************************************************/ |
| 18437 |
| 18438 /* |
| 18439 ** This vector defines all the methods that can operate on an |
| 18440 ** sqlite3_file for win32. |
| 18441 */ |
| 18442 static const sqlite3_io_methods winIoMethod = { |
| 18443 3, /* iVersion */ |
| 18444 winClose, /* xClose */ |
| 18445 winRead, /* xRead */ |
| 18446 winWrite, /* xWrite */ |
| 18447 winTruncate, /* xTruncate */ |
| 18448 winSync, /* xSync */ |
| 18449 winFileSize, /* xFileSize */ |
| 18450 winLock, /* xLock */ |
| 18451 winUnlock, /* xUnlock */ |
| 18452 winCheckReservedLock, /* xCheckReservedLock */ |
| 18453 winFileControl, /* xFileControl */ |
| 18454 winSectorSize, /* xSectorSize */ |
| 18455 winDeviceCharacteristics, /* xDeviceCharacteristics */ |
| 18456 winShmMap, /* xShmMap */ |
| 18457 winShmLock, /* xShmLock */ |
| 18458 winShmBarrier, /* xShmBarrier */ |
| 18459 winShmUnmap, /* xShmUnmap */ |
| 18460 winFetch, /* xFetch */ |
| 18461 winUnfetch /* xUnfetch */ |
| 18462 }; |
| 18463 |
| 18464 /**************************************************************************** |
| 18465 **************************** sqlite3_vfs methods **************************** |
| 18466 ** |
| 18467 ** This division contains the implementation of methods on the |
| 18468 ** sqlite3_vfs object. |
| 18469 */ |
| 18470 |
| 18471 #if defined(__CYGWIN__) |
| 18472 /* |
| 18473 ** Convert a filename from whatever the underlying operating system |
| 18474 ** supports for filenames into UTF-8. Space to hold the result is |
| 18475 ** obtained from malloc and must be freed by the calling function. |
| 18476 */ |
| 18477 static char *winConvertToUtf8Filename(const void *zFilename){ |
| 18478 char *zConverted = 0; |
| 18479 if( osIsNT() ){ |
| 18480 zConverted = winUnicodeToUtf8(zFilename); |
| 18481 } |
| 18482 #ifdef SQLITE_WIN32_HAS_ANSI |
| 18483 else{ |
| 18484 zConverted = sqlite3_win32_mbcs_to_utf8(zFilename); |
| 18485 } |
| 18486 #endif |
| 18487 /* caller will handle out of memory */ |
| 18488 return zConverted; |
| 18489 } |
| 18490 #endif |
| 18491 |
| 18492 /* |
| 18493 ** Convert a UTF-8 filename into whatever form the underlying |
| 18494 ** operating system wants filenames in. Space to hold the result |
| 18495 ** is obtained from malloc and must be freed by the calling |
| 18496 ** function. |
| 18497 */ |
| 18498 static void *winConvertFromUtf8Filename(const char *zFilename){ |
| 18499 void *zConverted = 0; |
| 18500 if( osIsNT() ){ |
| 18501 zConverted = winUtf8ToUnicode(zFilename); |
| 18502 } |
| 18503 #ifdef SQLITE_WIN32_HAS_ANSI |
| 18504 else{ |
| 18505 zConverted = sqlite3_win32_utf8_to_mbcs(zFilename); |
| 18506 } |
| 18507 #endif |
| 18508 /* caller will handle out of memory */ |
| 18509 return zConverted; |
| 18510 } |
| 18511 |
| 18512 /* |
| 18513 ** This function returns non-zero if the specified UTF-8 string buffer |
| 18514 ** ends with a directory separator character or one was successfully |
| 18515 ** added to it. |
| 18516 */ |
| 18517 static int winMakeEndInDirSep(int nBuf, char *zBuf){ |
| 18518 if( zBuf ){ |
| 18519 int nLen = sqlite3Strlen30(zBuf); |
| 18520 if( nLen>0 ){ |
| 18521 if( winIsDirSep(zBuf[nLen-1]) ){ |
| 18522 return 1; |
| 18523 }else if( nLen+1<nBuf ){ |
| 18524 zBuf[nLen] = winGetDirSep(); |
| 18525 zBuf[nLen+1] = '\0'; |
| 18526 return 1; |
| 18527 } |
| 18528 } |
| 18529 } |
| 18530 return 0; |
| 18531 } |
| 18532 |
| 18533 /* |
| 18534 ** Create a temporary file name and store the resulting pointer into pzBuf. |
| 18535 ** The pointer returned in pzBuf must be freed via sqlite3_free(). |
| 18536 */ |
| 18537 static int winGetTempname(sqlite3_vfs *pVfs, char **pzBuf){ |
| 18538 static char zChars[] = |
| 18539 "abcdefghijklmnopqrstuvwxyz" |
| 18540 "ABCDEFGHIJKLMNOPQRSTUVWXYZ" |
| 18541 "0123456789"; |
| 18542 size_t i, j; |
| 18543 int nPre = sqlite3Strlen30(SQLITE_TEMP_FILE_PREFIX); |
| 18544 int nMax, nBuf, nDir, nLen; |
| 18545 char *zBuf; |
| 18546 |
| 18547 /* It's odd to simulate an io-error here, but really this is just |
| 18548 ** using the io-error infrastructure to test that SQLite handles this |
| 18549 ** function failing. |
| 18550 */ |
| 18551 SimulateIOError( return SQLITE_IOERR ); |
| 18552 |
| 18553 /* Allocate a temporary buffer to store the fully qualified file |
| 18554 ** name for the temporary file. If this fails, we cannot continue. |
| 18555 */ |
| 18556 nMax = pVfs->mxPathname; nBuf = nMax + 2; |
| 18557 zBuf = sqlite3MallocZero( nBuf ); |
| 18558 if( !zBuf ){ |
| 18559 OSTRACE(("TEMP-FILENAME rc=SQLITE_IOERR_NOMEM\n")); |
| 18560 return SQLITE_IOERR_NOMEM; |
| 18561 } |
| 18562 |
| 18563 /* Figure out the effective temporary directory. First, check if one |
| 18564 ** has been explicitly set by the application; otherwise, use the one |
| 18565 ** configured by the operating system. |
| 18566 */ |
| 18567 nDir = nMax - (nPre + 15); |
| 18568 assert( nDir>0 ); |
| 18569 if( sqlite3_temp_directory ){ |
| 18570 int nDirLen = sqlite3Strlen30(sqlite3_temp_directory); |
| 18571 if( nDirLen>0 ){ |
| 18572 if( !winIsDirSep(sqlite3_temp_directory[nDirLen-1]) ){ |
| 18573 nDirLen++; |
| 18574 } |
| 18575 if( nDirLen>nDir ){ |
| 18576 sqlite3_free(zBuf); |
| 18577 OSTRACE(("TEMP-FILENAME rc=SQLITE_ERROR\n")); |
| 18578 return winLogError(SQLITE_ERROR, 0, "winGetTempname1", 0); |
| 18579 } |
| 18580 sqlite3_snprintf(nMax, zBuf, "%s", sqlite3_temp_directory); |
| 18581 } |
| 18582 } |
| 18583 #if defined(__CYGWIN__) |
| 18584 else{ |
| 18585 static const char *azDirs[] = { |
| 18586 0, /* getenv("SQLITE_TMPDIR") */ |
| 18587 0, /* getenv("TMPDIR") */ |
| 18588 0, /* getenv("TMP") */ |
| 18589 0, /* getenv("TEMP") */ |
| 18590 0, /* getenv("USERPROFILE") */ |
| 18591 "/var/tmp", |
| 18592 "/usr/tmp", |
| 18593 "/tmp", |
| 18594 ".", |
| 18595 0 /* List terminator */ |
| 18596 }; |
| 18597 unsigned int i; |
| 18598 const char *zDir = 0; |
| 18599 |
| 18600 if( !azDirs[0] ) azDirs[0] = getenv("SQLITE_TMPDIR"); |
| 18601 if( !azDirs[1] ) azDirs[1] = getenv("TMPDIR"); |
| 18602 if( !azDirs[2] ) azDirs[2] = getenv("TMP"); |
| 18603 if( !azDirs[3] ) azDirs[3] = getenv("TEMP"); |
| 18604 if( !azDirs[4] ) azDirs[4] = getenv("USERPROFILE"); |
| 18605 for(i=0; i<sizeof(azDirs)/sizeof(azDirs[0]); zDir=azDirs[i++]){ |
| 18606 void *zConverted; |
| 18607 if( zDir==0 ) continue; |
| 18608 /* If the path starts with a drive letter followed by the colon |
| 18609 ** character, assume it is already a native Win32 path; otherwise, |
| 18610 ** it must be converted to a native Win32 path via the Cygwin API |
| 18611 ** prior to using it. |
| 18612 */ |
| 18613 if( winIsDriveLetterAndColon(zDir) ){ |
| 18614 zConverted = winConvertFromUtf8Filename(zDir); |
| 18615 if( !zConverted ){ |
| 18616 sqlite3_free(zBuf); |
| 18617 OSTRACE(("TEMP-FILENAME rc=SQLITE_IOERR_NOMEM\n")); |
| 18618 return SQLITE_IOERR_NOMEM; |
| 18619 } |
| 18620 if( winIsDir(zConverted) ){ |
| 18621 sqlite3_snprintf(nMax, zBuf, "%s", zDir); |
| 18622 sqlite3_free(zConverted); |
| 18623 break; |
| 18624 } |
| 18625 sqlite3_free(zConverted); |
| 18626 }else{ |
| 18627 zConverted = sqlite3MallocZero( nMax+1 ); |
| 18628 if( !zConverted ){ |
| 18629 sqlite3_free(zBuf); |
| 18630 OSTRACE(("TEMP-FILENAME rc=SQLITE_IOERR_NOMEM\n")); |
| 18631 return SQLITE_IOERR_NOMEM; |
| 18632 } |
| 18633 if( cygwin_conv_path( |
| 18634 osIsNT() ? CCP_POSIX_TO_WIN_W : CCP_POSIX_TO_WIN_A, zDir, |
| 18635 zConverted, nMax+1)<0 ){ |
| 18636 sqlite3_free(zConverted); |
| 18637 sqlite3_free(zBuf); |
| 18638 OSTRACE(("TEMP-FILENAME rc=SQLITE_IOERR_CONVPATH\n")); |
| 18639 return winLogError(SQLITE_IOERR_CONVPATH, (DWORD)errno, |
| 18640 "winGetTempname2", zDir); |
| 18641 } |
| 18642 if( winIsDir(zConverted) ){ |
| 18643 /* At this point, we know the candidate directory exists and should |
| 18644 ** be used. However, we may need to convert the string containing |
| 18645 ** its name into UTF-8 (i.e. if it is UTF-16 right now). |
| 18646 */ |
| 18647 char *zUtf8 = winConvertToUtf8Filename(zConverted); |
| 18648 if( !zUtf8 ){ |
| 18649 sqlite3_free(zConverted); |
| 18650 sqlite3_free(zBuf); |
| 18651 OSTRACE(("TEMP-FILENAME rc=SQLITE_IOERR_NOMEM\n")); |
| 18652 return SQLITE_IOERR_NOMEM; |
| 18653 } |
| 18654 sqlite3_snprintf(nMax, zBuf, "%s", zUtf8); |
| 18655 sqlite3_free(zUtf8); |
| 18656 sqlite3_free(zConverted); |
| 18657 break; |
| 18658 } |
| 18659 sqlite3_free(zConverted); |
| 18660 } |
| 18661 } |
| 18662 } |
| 18663 #elif !SQLITE_OS_WINRT && !defined(__CYGWIN__) |
| 18664 else if( osIsNT() ){ |
| 18665 char *zMulti; |
| 18666 LPWSTR zWidePath = sqlite3MallocZero( nMax*sizeof(WCHAR) ); |
| 18667 if( !zWidePath ){ |
| 18668 sqlite3_free(zBuf); |
| 18669 OSTRACE(("TEMP-FILENAME rc=SQLITE_IOERR_NOMEM\n")); |
| 18670 return SQLITE_IOERR_NOMEM; |
| 18671 } |
| 18672 if( osGetTempPathW(nMax, zWidePath)==0 ){ |
| 18673 sqlite3_free(zWidePath); |
| 18674 sqlite3_free(zBuf); |
| 18675 OSTRACE(("TEMP-FILENAME rc=SQLITE_IOERR_GETTEMPPATH\n")); |
| 18676 return winLogError(SQLITE_IOERR_GETTEMPPATH, osGetLastError(), |
| 18677 "winGetTempname2", 0); |
| 18678 } |
| 18679 zMulti = winUnicodeToUtf8(zWidePath); |
| 18680 if( zMulti ){ |
| 18681 sqlite3_snprintf(nMax, zBuf, "%s", zMulti); |
| 18682 sqlite3_free(zMulti); |
| 18683 sqlite3_free(zWidePath); |
| 18684 }else{ |
| 18685 sqlite3_free(zWidePath); |
| 18686 sqlite3_free(zBuf); |
| 18687 OSTRACE(("TEMP-FILENAME rc=SQLITE_IOERR_NOMEM\n")); |
| 18688 return SQLITE_IOERR_NOMEM; |
| 18689 } |
| 18690 } |
| 18691 #ifdef SQLITE_WIN32_HAS_ANSI |
| 18692 else{ |
| 18693 char *zUtf8; |
| 18694 char *zMbcsPath = sqlite3MallocZero( nMax ); |
| 18695 if( !zMbcsPath ){ |
| 18696 sqlite3_free(zBuf); |
| 18697 OSTRACE(("TEMP-FILENAME rc=SQLITE_IOERR_NOMEM\n")); |
| 18698 return SQLITE_IOERR_NOMEM; |
| 18699 } |
| 18700 if( osGetTempPathA(nMax, zMbcsPath)==0 ){ |
| 18701 sqlite3_free(zBuf); |
| 18702 OSTRACE(("TEMP-FILENAME rc=SQLITE_IOERR_GETTEMPPATH\n")); |
| 18703 return winLogError(SQLITE_IOERR_GETTEMPPATH, osGetLastError(), |
| 18704 "winGetTempname3", 0); |
| 18705 } |
| 18706 zUtf8 = sqlite3_win32_mbcs_to_utf8(zMbcsPath); |
| 18707 if( zUtf8 ){ |
| 18708 sqlite3_snprintf(nMax, zBuf, "%s", zUtf8); |
| 18709 sqlite3_free(zUtf8); |
| 18710 }else{ |
| 18711 sqlite3_free(zBuf); |
| 18712 OSTRACE(("TEMP-FILENAME rc=SQLITE_IOERR_NOMEM\n")); |
| 18713 return SQLITE_IOERR_NOMEM; |
| 18714 } |
| 18715 } |
| 18716 #endif /* SQLITE_WIN32_HAS_ANSI */ |
| 18717 #endif /* !SQLITE_OS_WINRT */ |
| 18718 |
| 18719 /* |
| 18720 ** Check to make sure the temporary directory ends with an appropriate |
| 18721 ** separator. If it does not and there is not enough space left to add |
| 18722 ** one, fail. |
| 18723 */ |
| 18724 if( !winMakeEndInDirSep(nDir+1, zBuf) ){ |
| 18725 sqlite3_free(zBuf); |
| 18726 OSTRACE(("TEMP-FILENAME rc=SQLITE_ERROR\n")); |
| 18727 return winLogError(SQLITE_ERROR, 0, "winGetTempname4", 0); |
| 18728 } |
| 18729 |
| 18730 /* |
| 18731 ** Check that the output buffer is large enough for the temporary file |
| 18732 ** name in the following format: |
| 18733 ** |
| 18734 ** "<temporary_directory>/etilqs_XXXXXXXXXXXXXXX\0\0" |
| 18735 ** |
| 18736 ** If not, return SQLITE_ERROR. The number 17 is used here in order to |
| 18737 ** account for the space used by the 15 character random suffix and the |
| 18738 ** two trailing NUL characters. The final directory separator character |
| 18739 ** has already added if it was not already present. |
| 18740 */ |
| 18741 nLen = sqlite3Strlen30(zBuf); |
| 18742 if( (nLen + nPre + 17) > nBuf ){ |
| 18743 sqlite3_free(zBuf); |
| 18744 OSTRACE(("TEMP-FILENAME rc=SQLITE_ERROR\n")); |
| 18745 return winLogError(SQLITE_ERROR, 0, "winGetTempname5", 0); |
| 18746 } |
| 18747 |
| 18748 sqlite3_snprintf(nBuf-16-nLen, zBuf+nLen, SQLITE_TEMP_FILE_PREFIX); |
| 18749 |
| 18750 j = sqlite3Strlen30(zBuf); |
| 18751 sqlite3_randomness(15, &zBuf[j]); |
| 18752 for(i=0; i<15; i++, j++){ |
| 18753 zBuf[j] = (char)zChars[ ((unsigned char)zBuf[j])%(sizeof(zChars)-1) ]; |
| 18754 } |
| 18755 zBuf[j] = 0; |
| 18756 zBuf[j+1] = 0; |
| 18757 *pzBuf = zBuf; |
| 18758 |
| 18759 OSTRACE(("TEMP-FILENAME name=%s, rc=SQLITE_OK\n", zBuf)); |
| 18760 return SQLITE_OK; |
| 18761 } |
| 18762 |
| 18763 /* |
| 18764 ** Return TRUE if the named file is really a directory. Return false if |
| 18765 ** it is something other than a directory, or if there is any kind of memory |
| 18766 ** allocation failure. |
| 18767 */ |
| 18768 static int winIsDir(const void *zConverted){ |
| 18769 DWORD attr; |
| 18770 int rc = 0; |
| 18771 DWORD lastErrno; |
| 18772 |
| 18773 if( osIsNT() ){ |
| 18774 int cnt = 0; |
| 18775 WIN32_FILE_ATTRIBUTE_DATA sAttrData; |
| 18776 memset(&sAttrData, 0, sizeof(sAttrData)); |
| 18777 while( !(rc = osGetFileAttributesExW((LPCWSTR)zConverted, |
| 18778 GetFileExInfoStandard, |
| 18779 &sAttrData)) && winRetryIoerr(&cnt, &lastErrno) ){} |
| 18780 if( !rc ){ |
| 18781 return 0; /* Invalid name? */ |
| 18782 } |
| 18783 attr = sAttrData.dwFileAttributes; |
| 18784 #if SQLITE_OS_WINCE==0 |
| 18785 }else{ |
| 18786 attr = osGetFileAttributesA((char*)zConverted); |
| 18787 #endif |
| 18788 } |
| 18789 return (attr!=INVALID_FILE_ATTRIBUTES) && (attr&FILE_ATTRIBUTE_DIRECTORY); |
| 18790 } |
| 18791 |
| 18792 /* |
| 18793 ** Open a file. |
| 18794 */ |
| 18795 static int winOpen( |
| 18796 sqlite3_vfs *pVfs, /* Used to get maximum path name length */ |
| 18797 const char *zName, /* Name of the file (UTF-8) */ |
| 18798 sqlite3_file *id, /* Write the SQLite file handle here */ |
| 18799 int flags, /* Open mode flags */ |
| 18800 int *pOutFlags /* Status return flags */ |
| 18801 ){ |
| 18802 HANDLE h; |
| 18803 DWORD lastErrno = 0; |
| 18804 DWORD dwDesiredAccess; |
| 18805 DWORD dwShareMode; |
| 18806 DWORD dwCreationDisposition; |
| 18807 DWORD dwFlagsAndAttributes = 0; |
| 18808 #if SQLITE_OS_WINCE |
| 18809 int isTemp = 0; |
| 18810 #endif |
| 18811 winFile *pFile = (winFile*)id; |
| 18812 void *zConverted; /* Filename in OS encoding */ |
| 18813 const char *zUtf8Name = zName; /* Filename in UTF-8 encoding */ |
| 18814 int cnt = 0; |
| 18815 |
| 18816 /* If argument zPath is a NULL pointer, this function is required to open |
| 18817 ** a temporary file. Use this buffer to store the file name in. |
| 18818 */ |
| 18819 char *zTmpname = 0; /* For temporary filename, if necessary. */ |
| 18820 |
| 18821 int rc = SQLITE_OK; /* Function Return Code */ |
| 18822 #if !defined(NDEBUG) || SQLITE_OS_WINCE |
| 18823 int eType = flags&0xFFFFFF00; /* Type of file to open */ |
| 18824 #endif |
| 18825 |
| 18826 int isExclusive = (flags & SQLITE_OPEN_EXCLUSIVE); |
| 18827 int isDelete = (flags & SQLITE_OPEN_DELETEONCLOSE); |
| 18828 int isCreate = (flags & SQLITE_OPEN_CREATE); |
| 18829 int isReadonly = (flags & SQLITE_OPEN_READONLY); |
| 18830 int isReadWrite = (flags & SQLITE_OPEN_READWRITE); |
| 18831 |
| 18832 #ifndef NDEBUG |
| 18833 int isOpenJournal = (isCreate && ( |
| 18834 eType==SQLITE_OPEN_MASTER_JOURNAL |
| 18835 || eType==SQLITE_OPEN_MAIN_JOURNAL |
| 18836 || eType==SQLITE_OPEN_WAL |
| 18837 )); |
| 18838 #endif |
| 18839 |
| 18840 OSTRACE(("OPEN name=%s, pFile=%p, flags=%x, pOutFlags=%p\n", |
| 18841 zUtf8Name, id, flags, pOutFlags)); |
| 18842 |
| 18843 /* Check the following statements are true: |
| 18844 ** |
| 18845 ** (a) Exactly one of the READWRITE and READONLY flags must be set, and |
| 18846 ** (b) if CREATE is set, then READWRITE must also be set, and |
| 18847 ** (c) if EXCLUSIVE is set, then CREATE must also be set. |
| 18848 ** (d) if DELETEONCLOSE is set, then CREATE must also be set. |
| 18849 */ |
| 18850 assert((isReadonly==0 || isReadWrite==0) && (isReadWrite || isReadonly)); |
| 18851 assert(isCreate==0 || isReadWrite); |
| 18852 assert(isExclusive==0 || isCreate); |
| 18853 assert(isDelete==0 || isCreate); |
| 18854 |
| 18855 /* The main DB, main journal, WAL file and master journal are never |
| 18856 ** automatically deleted. Nor are they ever temporary files. */ |
| 18857 assert( (!isDelete && zName) || eType!=SQLITE_OPEN_MAIN_DB ); |
| 18858 assert( (!isDelete && zName) || eType!=SQLITE_OPEN_MAIN_JOURNAL ); |
| 18859 assert( (!isDelete && zName) || eType!=SQLITE_OPEN_MASTER_JOURNAL ); |
| 18860 assert( (!isDelete && zName) || eType!=SQLITE_OPEN_WAL ); |
| 18861 |
| 18862 /* Assert that the upper layer has set one of the "file-type" flags. */ |
| 18863 assert( eType==SQLITE_OPEN_MAIN_DB || eType==SQLITE_OPEN_TEMP_DB |
| 18864 || eType==SQLITE_OPEN_MAIN_JOURNAL || eType==SQLITE_OPEN_TEMP_JOURNAL |
| 18865 || eType==SQLITE_OPEN_SUBJOURNAL || eType==SQLITE_OPEN_MASTER_JOURNAL |
| 18866 || eType==SQLITE_OPEN_TRANSIENT_DB || eType==SQLITE_OPEN_WAL |
| 18867 ); |
| 18868 |
| 18869 assert( pFile!=0 ); |
| 18870 memset(pFile, 0, sizeof(winFile)); |
| 18871 pFile->h = INVALID_HANDLE_VALUE; |
| 18872 |
| 18873 #if SQLITE_OS_WINRT |
| 18874 if( !zUtf8Name && !sqlite3_temp_directory ){ |
| 18875 sqlite3_log(SQLITE_ERROR, |
| 18876 "sqlite3_temp_directory variable should be set for WinRT"); |
| 18877 } |
| 18878 #endif |
| 18879 |
| 18880 /* If the second argument to this function is NULL, generate a |
| 18881 ** temporary file name to use |
| 18882 */ |
| 18883 if( !zUtf8Name ){ |
| 18884 assert( isDelete && !isOpenJournal ); |
| 18885 rc = winGetTempname(pVfs, &zTmpname); |
| 18886 if( rc!=SQLITE_OK ){ |
| 18887 OSTRACE(("OPEN name=%s, rc=%s", zUtf8Name, sqlite3ErrName(rc))); |
| 18888 return rc; |
| 18889 } |
| 18890 zUtf8Name = zTmpname; |
| 18891 } |
| 18892 |
| 18893 /* Database filenames are double-zero terminated if they are not |
| 18894 ** URIs with parameters. Hence, they can always be passed into |
| 18895 ** sqlite3_uri_parameter(). |
| 18896 */ |
| 18897 assert( (eType!=SQLITE_OPEN_MAIN_DB) || (flags & SQLITE_OPEN_URI) || |
| 18898 zUtf8Name[sqlite3Strlen30(zUtf8Name)+1]==0 ); |
| 18899 |
| 18900 /* Convert the filename to the system encoding. */ |
| 18901 zConverted = winConvertFromUtf8Filename(zUtf8Name); |
| 18902 if( zConverted==0 ){ |
| 18903 sqlite3_free(zTmpname); |
| 18904 OSTRACE(("OPEN name=%s, rc=SQLITE_IOERR_NOMEM", zUtf8Name)); |
| 18905 return SQLITE_IOERR_NOMEM; |
| 18906 } |
| 18907 |
| 18908 if( winIsDir(zConverted) ){ |
| 18909 sqlite3_free(zConverted); |
| 18910 sqlite3_free(zTmpname); |
| 18911 OSTRACE(("OPEN name=%s, rc=SQLITE_CANTOPEN_ISDIR", zUtf8Name)); |
| 18912 return SQLITE_CANTOPEN_ISDIR; |
| 18913 } |
| 18914 |
| 18915 if( isReadWrite ){ |
| 18916 dwDesiredAccess = GENERIC_READ | GENERIC_WRITE; |
| 18917 }else{ |
| 18918 dwDesiredAccess = GENERIC_READ; |
| 18919 } |
| 18920 |
| 18921 /* SQLITE_OPEN_EXCLUSIVE is used to make sure that a new file is |
| 18922 ** created. SQLite doesn't use it to indicate "exclusive access" |
| 18923 ** as it is usually understood. |
| 18924 */ |
| 18925 if( isExclusive ){ |
| 18926 /* Creates a new file, only if it does not already exist. */ |
| 18927 /* If the file exists, it fails. */ |
| 18928 dwCreationDisposition = CREATE_NEW; |
| 18929 }else if( isCreate ){ |
| 18930 /* Open existing file, or create if it doesn't exist */ |
| 18931 dwCreationDisposition = OPEN_ALWAYS; |
| 18932 }else{ |
| 18933 /* Opens a file, only if it exists. */ |
| 18934 dwCreationDisposition = OPEN_EXISTING; |
| 18935 } |
| 18936 |
| 18937 dwShareMode = FILE_SHARE_READ | FILE_SHARE_WRITE; |
| 18938 |
| 18939 if( isDelete ){ |
| 18940 #if SQLITE_OS_WINCE |
| 18941 dwFlagsAndAttributes = FILE_ATTRIBUTE_HIDDEN; |
| 18942 isTemp = 1; |
| 18943 #else |
| 18944 dwFlagsAndAttributes = FILE_ATTRIBUTE_TEMPORARY |
| 18945 | FILE_ATTRIBUTE_HIDDEN |
| 18946 | FILE_FLAG_DELETE_ON_CLOSE; |
| 18947 #endif |
| 18948 }else{ |
| 18949 dwFlagsAndAttributes = FILE_ATTRIBUTE_NORMAL; |
| 18950 } |
| 18951 /* Reports from the internet are that performance is always |
| 18952 ** better if FILE_FLAG_RANDOM_ACCESS is used. Ticket #2699. */ |
| 18953 #if SQLITE_OS_WINCE |
| 18954 dwFlagsAndAttributes |= FILE_FLAG_RANDOM_ACCESS; |
| 18955 #endif |
| 18956 |
| 18957 if( osIsNT() ){ |
| 18958 #if SQLITE_OS_WINRT |
| 18959 CREATEFILE2_EXTENDED_PARAMETERS extendedParameters; |
| 18960 extendedParameters.dwSize = sizeof(CREATEFILE2_EXTENDED_PARAMETERS); |
| 18961 extendedParameters.dwFileAttributes = |
| 18962 dwFlagsAndAttributes & FILE_ATTRIBUTE_MASK; |
| 18963 extendedParameters.dwFileFlags = dwFlagsAndAttributes & FILE_FLAG_MASK; |
| 18964 extendedParameters.dwSecurityQosFlags = SECURITY_ANONYMOUS; |
| 18965 extendedParameters.lpSecurityAttributes = NULL; |
| 18966 extendedParameters.hTemplateFile = NULL; |
| 18967 while( (h = osCreateFile2((LPCWSTR)zConverted, |
| 18968 dwDesiredAccess, |
| 18969 dwShareMode, |
| 18970 dwCreationDisposition, |
| 18971 &extendedParameters))==INVALID_HANDLE_VALUE && |
| 18972 winRetryIoerr(&cnt, &lastErrno) ){ |
| 18973 /* Noop */ |
| 18974 } |
| 18975 #else |
| 18976 while( (h = osCreateFileW((LPCWSTR)zConverted, |
| 18977 dwDesiredAccess, |
| 18978 dwShareMode, NULL, |
| 18979 dwCreationDisposition, |
| 18980 dwFlagsAndAttributes, |
| 18981 NULL))==INVALID_HANDLE_VALUE && |
| 18982 winRetryIoerr(&cnt, &lastErrno) ){ |
| 18983 /* Noop */ |
| 18984 } |
| 18985 #endif |
| 18986 } |
| 18987 #ifdef SQLITE_WIN32_HAS_ANSI |
| 18988 else{ |
| 18989 while( (h = osCreateFileA((LPCSTR)zConverted, |
| 18990 dwDesiredAccess, |
| 18991 dwShareMode, NULL, |
| 18992 dwCreationDisposition, |
| 18993 dwFlagsAndAttributes, |
| 18994 NULL))==INVALID_HANDLE_VALUE && |
| 18995 winRetryIoerr(&cnt, &lastErrno) ){ |
| 18996 /* Noop */ |
| 18997 } |
| 18998 } |
| 18999 #endif |
| 19000 winLogIoerr(cnt, __LINE__); |
| 19001 |
| 19002 OSTRACE(("OPEN file=%p, name=%s, access=%lx, rc=%s\n", h, zUtf8Name, |
| 19003 dwDesiredAccess, (h==INVALID_HANDLE_VALUE) ? "failed" : "ok")); |
| 19004 |
| 19005 if( h==INVALID_HANDLE_VALUE ){ |
| 19006 pFile->lastErrno = lastErrno; |
| 19007 winLogError(SQLITE_CANTOPEN, pFile->lastErrno, "winOpen", zUtf8Name); |
| 19008 sqlite3_free(zConverted); |
| 19009 sqlite3_free(zTmpname); |
| 19010 if( isReadWrite && !isExclusive ){ |
| 19011 return winOpen(pVfs, zName, id, |
| 19012 ((flags|SQLITE_OPEN_READONLY) & |
| 19013 ~(SQLITE_OPEN_CREATE|SQLITE_OPEN_READWRITE)), |
| 19014 pOutFlags); |
| 19015 }else{ |
| 19016 return SQLITE_CANTOPEN_BKPT; |
| 19017 } |
| 19018 } |
| 19019 |
| 19020 if( pOutFlags ){ |
| 19021 if( isReadWrite ){ |
| 19022 *pOutFlags = SQLITE_OPEN_READWRITE; |
| 19023 }else{ |
| 19024 *pOutFlags = SQLITE_OPEN_READONLY; |
| 19025 } |
| 19026 } |
| 19027 |
| 19028 OSTRACE(("OPEN file=%p, name=%s, access=%lx, pOutFlags=%p, *pOutFlags=%d, " |
| 19029 "rc=%s\n", h, zUtf8Name, dwDesiredAccess, pOutFlags, pOutFlags ? |
| 19030 *pOutFlags : 0, (h==INVALID_HANDLE_VALUE) ? "failed" : "ok")); |
| 19031 |
| 19032 #if SQLITE_OS_WINCE |
| 19033 if( isReadWrite && eType==SQLITE_OPEN_MAIN_DB |
| 19034 && (rc = winceCreateLock(zName, pFile))!=SQLITE_OK |
| 19035 ){ |
| 19036 osCloseHandle(h); |
| 19037 sqlite3_free(zConverted); |
| 19038 sqlite3_free(zTmpname); |
| 19039 OSTRACE(("OPEN-CE-LOCK name=%s, rc=%s\n", zName, sqlite3ErrName(rc))); |
| 19040 return rc; |
| 19041 } |
| 19042 if( isTemp ){ |
| 19043 pFile->zDeleteOnClose = zConverted; |
| 19044 }else |
| 19045 #endif |
| 19046 { |
| 19047 sqlite3_free(zConverted); |
| 19048 } |
| 19049 |
| 19050 sqlite3_free(zTmpname); |
| 19051 pFile->pMethod = &winIoMethod; |
| 19052 pFile->pVfs = pVfs; |
| 19053 pFile->h = h; |
| 19054 if( isReadonly ){ |
| 19055 pFile->ctrlFlags |= WINFILE_RDONLY; |
| 19056 } |
| 19057 if( sqlite3_uri_boolean(zName, "psow", SQLITE_POWERSAFE_OVERWRITE) ){ |
| 19058 pFile->ctrlFlags |= WINFILE_PSOW; |
| 19059 } |
| 19060 pFile->lastErrno = NO_ERROR; |
| 19061 pFile->zPath = zName; |
| 19062 #if SQLITE_MAX_MMAP_SIZE>0 |
| 19063 pFile->hMap = NULL; |
| 19064 pFile->pMapRegion = 0; |
| 19065 pFile->mmapSize = 0; |
| 19066 pFile->mmapSizeActual = 0; |
| 19067 pFile->mmapSizeMax = sqlite3GlobalConfig.szMmap; |
| 19068 #endif |
| 19069 |
| 19070 OpenCounter(+1); |
| 19071 return rc; |
| 19072 } |
| 19073 |
| 19074 /* |
| 19075 ** Delete the named file. |
| 19076 ** |
| 19077 ** Note that Windows does not allow a file to be deleted if some other |
| 19078 ** process has it open. Sometimes a virus scanner or indexing program |
| 19079 ** will open a journal file shortly after it is created in order to do |
| 19080 ** whatever it does. While this other process is holding the |
| 19081 ** file open, we will be unable to delete it. To work around this |
| 19082 ** problem, we delay 100 milliseconds and try to delete again. Up |
| 19083 ** to MX_DELETION_ATTEMPTs deletion attempts are run before giving |
| 19084 ** up and returning an error. |
| 19085 */ |
| 19086 static int winDelete( |
| 19087 sqlite3_vfs *pVfs, /* Not used on win32 */ |
| 19088 const char *zFilename, /* Name of file to delete */ |
| 19089 int syncDir /* Not used on win32 */ |
| 19090 ){ |
| 19091 int cnt = 0; |
| 19092 int rc; |
| 19093 DWORD attr; |
| 19094 DWORD lastErrno = 0; |
| 19095 void *zConverted; |
| 19096 UNUSED_PARAMETER(pVfs); |
| 19097 UNUSED_PARAMETER(syncDir); |
| 19098 |
| 19099 SimulateIOError(return SQLITE_IOERR_DELETE); |
| 19100 OSTRACE(("DELETE name=%s, syncDir=%d\n", zFilename, syncDir)); |
| 19101 |
| 19102 zConverted = winConvertFromUtf8Filename(zFilename); |
| 19103 if( zConverted==0 ){ |
| 19104 OSTRACE(("DELETE name=%s, rc=SQLITE_IOERR_NOMEM\n", zFilename)); |
| 19105 return SQLITE_IOERR_NOMEM; |
| 19106 } |
| 19107 if( osIsNT() ){ |
| 19108 do { |
| 19109 #if SQLITE_OS_WINRT |
| 19110 WIN32_FILE_ATTRIBUTE_DATA sAttrData; |
| 19111 memset(&sAttrData, 0, sizeof(sAttrData)); |
| 19112 if ( osGetFileAttributesExW(zConverted, GetFileExInfoStandard, |
| 19113 &sAttrData) ){ |
| 19114 attr = sAttrData.dwFileAttributes; |
| 19115 }else{ |
| 19116 lastErrno = osGetLastError(); |
| 19117 if( lastErrno==ERROR_FILE_NOT_FOUND |
| 19118 || lastErrno==ERROR_PATH_NOT_FOUND ){ |
| 19119 rc = SQLITE_IOERR_DELETE_NOENT; /* Already gone? */ |
| 19120 }else{ |
| 19121 rc = SQLITE_ERROR; |
| 19122 } |
| 19123 break; |
| 19124 } |
| 19125 #else |
| 19126 attr = osGetFileAttributesW(zConverted); |
| 19127 #endif |
| 19128 if ( attr==INVALID_FILE_ATTRIBUTES ){ |
| 19129 lastErrno = osGetLastError(); |
| 19130 if( lastErrno==ERROR_FILE_NOT_FOUND |
| 19131 || lastErrno==ERROR_PATH_NOT_FOUND ){ |
| 19132 rc = SQLITE_IOERR_DELETE_NOENT; /* Already gone? */ |
| 19133 }else{ |
| 19134 rc = SQLITE_ERROR; |
| 19135 } |
| 19136 break; |
| 19137 } |
| 19138 if ( attr&FILE_ATTRIBUTE_DIRECTORY ){ |
| 19139 rc = SQLITE_ERROR; /* Files only. */ |
| 19140 break; |
| 19141 } |
| 19142 if ( osDeleteFileW(zConverted) ){ |
| 19143 rc = SQLITE_OK; /* Deleted OK. */ |
| 19144 break; |
| 19145 } |
| 19146 if ( !winRetryIoerr(&cnt, &lastErrno) ){ |
| 19147 rc = SQLITE_ERROR; /* No more retries. */ |
| 19148 break; |
| 19149 } |
| 19150 } while(1); |
| 19151 } |
| 19152 #ifdef SQLITE_WIN32_HAS_ANSI |
| 19153 else{ |
| 19154 do { |
| 19155 attr = osGetFileAttributesA(zConverted); |
| 19156 if ( attr==INVALID_FILE_ATTRIBUTES ){ |
| 19157 lastErrno = osGetLastError(); |
| 19158 if( lastErrno==ERROR_FILE_NOT_FOUND |
| 19159 || lastErrno==ERROR_PATH_NOT_FOUND ){ |
| 19160 rc = SQLITE_IOERR_DELETE_NOENT; /* Already gone? */ |
| 19161 }else{ |
| 19162 rc = SQLITE_ERROR; |
| 19163 } |
| 19164 break; |
| 19165 } |
| 19166 if ( attr&FILE_ATTRIBUTE_DIRECTORY ){ |
| 19167 rc = SQLITE_ERROR; /* Files only. */ |
| 19168 break; |
| 19169 } |
| 19170 if ( osDeleteFileA(zConverted) ){ |
| 19171 rc = SQLITE_OK; /* Deleted OK. */ |
| 19172 break; |
| 19173 } |
| 19174 if ( !winRetryIoerr(&cnt, &lastErrno) ){ |
| 19175 rc = SQLITE_ERROR; /* No more retries. */ |
| 19176 break; |
| 19177 } |
| 19178 } while(1); |
| 19179 } |
| 19180 #endif |
| 19181 if( rc && rc!=SQLITE_IOERR_DELETE_NOENT ){ |
| 19182 rc = winLogError(SQLITE_IOERR_DELETE, lastErrno, "winDelete", zFilename); |
| 19183 }else{ |
| 19184 winLogIoerr(cnt, __LINE__); |
| 19185 } |
| 19186 sqlite3_free(zConverted); |
| 19187 OSTRACE(("DELETE name=%s, rc=%s\n", zFilename, sqlite3ErrName(rc))); |
| 19188 return rc; |
| 19189 } |
| 19190 |
| 19191 /* |
| 19192 ** Check the existence and status of a file. |
| 19193 */ |
| 19194 static int winAccess( |
| 19195 sqlite3_vfs *pVfs, /* Not used on win32 */ |
| 19196 const char *zFilename, /* Name of file to check */ |
| 19197 int flags, /* Type of test to make on this file */ |
| 19198 int *pResOut /* OUT: Result */ |
| 19199 ){ |
| 19200 DWORD attr; |
| 19201 int rc = 0; |
| 19202 DWORD lastErrno = 0; |
| 19203 void *zConverted; |
| 19204 UNUSED_PARAMETER(pVfs); |
| 19205 |
| 19206 SimulateIOError( return SQLITE_IOERR_ACCESS; ); |
| 19207 OSTRACE(("ACCESS name=%s, flags=%x, pResOut=%p\n", |
| 19208 zFilename, flags, pResOut)); |
| 19209 |
| 19210 zConverted = winConvertFromUtf8Filename(zFilename); |
| 19211 if( zConverted==0 ){ |
| 19212 OSTRACE(("ACCESS name=%s, rc=SQLITE_IOERR_NOMEM\n", zFilename)); |
| 19213 return SQLITE_IOERR_NOMEM; |
| 19214 } |
| 19215 if( osIsNT() ){ |
| 19216 int cnt = 0; |
| 19217 WIN32_FILE_ATTRIBUTE_DATA sAttrData; |
| 19218 memset(&sAttrData, 0, sizeof(sAttrData)); |
| 19219 while( !(rc = osGetFileAttributesExW((LPCWSTR)zConverted, |
| 19220 GetFileExInfoStandard, |
| 19221 &sAttrData)) && winRetryIoerr(&cnt, &lastErrno) ){} |
| 19222 if( rc ){ |
| 19223 /* For an SQLITE_ACCESS_EXISTS query, treat a zero-length file |
| 19224 ** as if it does not exist. |
| 19225 */ |
| 19226 if( flags==SQLITE_ACCESS_EXISTS |
| 19227 && sAttrData.nFileSizeHigh==0 |
| 19228 && sAttrData.nFileSizeLow==0 ){ |
| 19229 attr = INVALID_FILE_ATTRIBUTES; |
| 19230 }else{ |
| 19231 attr = sAttrData.dwFileAttributes; |
| 19232 } |
| 19233 }else{ |
| 19234 winLogIoerr(cnt, __LINE__); |
| 19235 if( lastErrno!=ERROR_FILE_NOT_FOUND && lastErrno!=ERROR_PATH_NOT_FOUND ){ |
| 19236 sqlite3_free(zConverted); |
| 19237 return winLogError(SQLITE_IOERR_ACCESS, lastErrno, "winAccess", |
| 19238 zFilename); |
| 19239 }else{ |
| 19240 attr = INVALID_FILE_ATTRIBUTES; |
| 19241 } |
| 19242 } |
| 19243 } |
| 19244 #ifdef SQLITE_WIN32_HAS_ANSI |
| 19245 else{ |
| 19246 attr = osGetFileAttributesA((char*)zConverted); |
| 19247 } |
| 19248 #endif |
| 19249 sqlite3_free(zConverted); |
| 19250 switch( flags ){ |
| 19251 case SQLITE_ACCESS_READ: |
| 19252 case SQLITE_ACCESS_EXISTS: |
| 19253 rc = attr!=INVALID_FILE_ATTRIBUTES; |
| 19254 break; |
| 19255 case SQLITE_ACCESS_READWRITE: |
| 19256 rc = attr!=INVALID_FILE_ATTRIBUTES && |
| 19257 (attr & FILE_ATTRIBUTE_READONLY)==0; |
| 19258 break; |
| 19259 default: |
| 19260 assert(!"Invalid flags argument"); |
| 19261 } |
| 19262 *pResOut = rc; |
| 19263 OSTRACE(("ACCESS name=%s, pResOut=%p, *pResOut=%d, rc=SQLITE_OK\n", |
| 19264 zFilename, pResOut, *pResOut)); |
| 19265 return SQLITE_OK; |
| 19266 } |
| 19267 |
| 19268 /* |
| 19269 ** Returns non-zero if the specified path name starts with a drive letter |
| 19270 ** followed by a colon character. |
| 19271 */ |
| 19272 static BOOL winIsDriveLetterAndColon( |
| 19273 const char *zPathname |
| 19274 ){ |
| 19275 return ( sqlite3Isalpha(zPathname[0]) && zPathname[1]==':' ); |
| 19276 } |
| 19277 |
| 19278 /* |
| 19279 ** Returns non-zero if the specified path name should be used verbatim. If |
| 19280 ** non-zero is returned from this function, the calling function must simply |
| 19281 ** use the provided path name verbatim -OR- resolve it into a full path name |
| 19282 ** using the GetFullPathName Win32 API function (if available). |
| 19283 */ |
| 19284 static BOOL winIsVerbatimPathname( |
| 19285 const char *zPathname |
| 19286 ){ |
| 19287 /* |
| 19288 ** If the path name starts with a forward slash or a backslash, it is either |
| 19289 ** a legal UNC name, a volume relative path, or an absolute path name in the |
| 19290 ** "Unix" format on Windows. There is no easy way to differentiate between |
| 19291 ** the final two cases; therefore, we return the safer return value of TRUE |
| 19292 ** so that callers of this function will simply use it verbatim. |
| 19293 */ |
| 19294 if ( winIsDirSep(zPathname[0]) ){ |
| 19295 return TRUE; |
| 19296 } |
| 19297 |
| 19298 /* |
| 19299 ** If the path name starts with a letter and a colon it is either a volume |
| 19300 ** relative path or an absolute path. Callers of this function must not |
| 19301 ** attempt to treat it as a relative path name (i.e. they should simply use |
| 19302 ** it verbatim). |
| 19303 */ |
| 19304 if ( winIsDriveLetterAndColon(zPathname) ){ |
| 19305 return TRUE; |
| 19306 } |
| 19307 |
| 19308 /* |
| 19309 ** If we get to this point, the path name should almost certainly be a purely |
| 19310 ** relative one (i.e. not a UNC name, not absolute, and not volume relative). |
| 19311 */ |
| 19312 return FALSE; |
| 19313 } |
| 19314 |
| 19315 /* |
| 19316 ** Turn a relative pathname into a full pathname. Write the full |
| 19317 ** pathname into zOut[]. zOut[] will be at least pVfs->mxPathname |
| 19318 ** bytes in size. |
| 19319 */ |
| 19320 static int winFullPathname( |
| 19321 sqlite3_vfs *pVfs, /* Pointer to vfs object */ |
| 19322 const char *zRelative, /* Possibly relative input path */ |
| 19323 int nFull, /* Size of output buffer in bytes */ |
| 19324 char *zFull /* Output buffer */ |
| 19325 ){ |
| 19326 |
| 19327 #if defined(__CYGWIN__) |
| 19328 SimulateIOError( return SQLITE_ERROR ); |
| 19329 UNUSED_PARAMETER(nFull); |
| 19330 assert( nFull>=pVfs->mxPathname ); |
| 19331 if ( sqlite3_data_directory && !winIsVerbatimPathname(zRelative) ){ |
| 19332 /* |
| 19333 ** NOTE: We are dealing with a relative path name and the data |
| 19334 ** directory has been set. Therefore, use it as the basis |
| 19335 ** for converting the relative path name to an absolute |
| 19336 ** one by prepending the data directory and a slash. |
| 19337 */ |
| 19338 char *zOut = sqlite3MallocZero( pVfs->mxPathname+1 ); |
| 19339 if( !zOut ){ |
| 19340 return SQLITE_IOERR_NOMEM; |
| 19341 } |
| 19342 if( cygwin_conv_path( |
| 19343 (osIsNT() ? CCP_POSIX_TO_WIN_W : CCP_POSIX_TO_WIN_A) | |
| 19344 CCP_RELATIVE, zRelative, zOut, pVfs->mxPathname+1)<0 ){ |
| 19345 sqlite3_free(zOut); |
| 19346 return winLogError(SQLITE_CANTOPEN_CONVPATH, (DWORD)errno, |
| 19347 "winFullPathname1", zRelative); |
| 19348 }else{ |
| 19349 char *zUtf8 = winConvertToUtf8Filename(zOut); |
| 19350 if( !zUtf8 ){ |
| 19351 sqlite3_free(zOut); |
| 19352 return SQLITE_IOERR_NOMEM; |
| 19353 } |
| 19354 sqlite3_snprintf(MIN(nFull, pVfs->mxPathname), zFull, "%s%c%s", |
| 19355 sqlite3_data_directory, winGetDirSep(), zUtf8); |
| 19356 sqlite3_free(zUtf8); |
| 19357 sqlite3_free(zOut); |
| 19358 } |
| 19359 }else{ |
| 19360 char *zOut = sqlite3MallocZero( pVfs->mxPathname+1 ); |
| 19361 if( !zOut ){ |
| 19362 return SQLITE_IOERR_NOMEM; |
| 19363 } |
| 19364 if( cygwin_conv_path( |
| 19365 (osIsNT() ? CCP_POSIX_TO_WIN_W : CCP_POSIX_TO_WIN_A), |
| 19366 zRelative, zOut, pVfs->mxPathname+1)<0 ){ |
| 19367 sqlite3_free(zOut); |
| 19368 return winLogError(SQLITE_CANTOPEN_CONVPATH, (DWORD)errno, |
| 19369 "winFullPathname2", zRelative); |
| 19370 }else{ |
| 19371 char *zUtf8 = winConvertToUtf8Filename(zOut); |
| 19372 if( !zUtf8 ){ |
| 19373 sqlite3_free(zOut); |
| 19374 return SQLITE_IOERR_NOMEM; |
| 19375 } |
| 19376 sqlite3_snprintf(MIN(nFull, pVfs->mxPathname), zFull, "%s", zUtf8); |
| 19377 sqlite3_free(zUtf8); |
| 19378 sqlite3_free(zOut); |
| 19379 } |
| 19380 } |
| 19381 return SQLITE_OK; |
| 19382 #endif |
| 19383 |
| 19384 #if (SQLITE_OS_WINCE || SQLITE_OS_WINRT) && !defined(__CYGWIN__) |
| 19385 SimulateIOError( return SQLITE_ERROR ); |
| 19386 /* WinCE has no concept of a relative pathname, or so I am told. */ |
| 19387 /* WinRT has no way to convert a relative path to an absolute one. */ |
| 19388 if ( sqlite3_data_directory && !winIsVerbatimPathname(zRelative) ){ |
| 19389 /* |
| 19390 ** NOTE: We are dealing with a relative path name and the data |
| 19391 ** directory has been set. Therefore, use it as the basis |
| 19392 ** for converting the relative path name to an absolute |
| 19393 ** one by prepending the data directory and a backslash. |
| 19394 */ |
| 19395 sqlite3_snprintf(MIN(nFull, pVfs->mxPathname), zFull, "%s%c%s", |
| 19396 sqlite3_data_directory, winGetDirSep(), zRelative); |
| 19397 }else{ |
| 19398 sqlite3_snprintf(MIN(nFull, pVfs->mxPathname), zFull, "%s", zRelative); |
| 19399 } |
| 19400 return SQLITE_OK; |
| 19401 #endif |
| 19402 |
| 19403 #if !SQLITE_OS_WINCE && !SQLITE_OS_WINRT && !defined(__CYGWIN__) |
| 19404 DWORD nByte; |
| 19405 void *zConverted; |
| 19406 char *zOut; |
| 19407 |
| 19408 /* If this path name begins with "/X:", where "X" is any alphabetic |
| 19409 ** character, discard the initial "/" from the pathname. |
| 19410 */ |
| 19411 if( zRelative[0]=='/' && winIsDriveLetterAndColon(zRelative+1) ){ |
| 19412 zRelative++; |
| 19413 } |
| 19414 |
| 19415 /* It's odd to simulate an io-error here, but really this is just |
| 19416 ** using the io-error infrastructure to test that SQLite handles this |
| 19417 ** function failing. This function could fail if, for example, the |
| 19418 ** current working directory has been unlinked. |
| 19419 */ |
| 19420 SimulateIOError( return SQLITE_ERROR ); |
| 19421 if ( sqlite3_data_directory && !winIsVerbatimPathname(zRelative) ){ |
| 19422 /* |
| 19423 ** NOTE: We are dealing with a relative path name and the data |
| 19424 ** directory has been set. Therefore, use it as the basis |
| 19425 ** for converting the relative path name to an absolute |
| 19426 ** one by prepending the data directory and a backslash. |
| 19427 */ |
| 19428 sqlite3_snprintf(MIN(nFull, pVfs->mxPathname), zFull, "%s%c%s", |
| 19429 sqlite3_data_directory, winGetDirSep(), zRelative); |
| 19430 return SQLITE_OK; |
| 19431 } |
| 19432 zConverted = winConvertFromUtf8Filename(zRelative); |
| 19433 if( zConverted==0 ){ |
| 19434 return SQLITE_IOERR_NOMEM; |
| 19435 } |
| 19436 if( osIsNT() ){ |
| 19437 LPWSTR zTemp; |
| 19438 nByte = osGetFullPathNameW((LPCWSTR)zConverted, 0, 0, 0); |
| 19439 if( nByte==0 ){ |
| 19440 sqlite3_free(zConverted); |
| 19441 return winLogError(SQLITE_CANTOPEN_FULLPATH, osGetLastError(), |
| 19442 "winFullPathname1", zRelative); |
| 19443 } |
| 19444 nByte += 3; |
| 19445 zTemp = sqlite3MallocZero( nByte*sizeof(zTemp[0]) ); |
| 19446 if( zTemp==0 ){ |
| 19447 sqlite3_free(zConverted); |
| 19448 return SQLITE_IOERR_NOMEM; |
| 19449 } |
| 19450 nByte = osGetFullPathNameW((LPCWSTR)zConverted, nByte, zTemp, 0); |
| 19451 if( nByte==0 ){ |
| 19452 sqlite3_free(zConverted); |
| 19453 sqlite3_free(zTemp); |
| 19454 return winLogError(SQLITE_CANTOPEN_FULLPATH, osGetLastError(), |
| 19455 "winFullPathname2", zRelative); |
| 19456 } |
| 19457 sqlite3_free(zConverted); |
| 19458 zOut = winUnicodeToUtf8(zTemp); |
| 19459 sqlite3_free(zTemp); |
| 19460 } |
| 19461 #ifdef SQLITE_WIN32_HAS_ANSI |
| 19462 else{ |
| 19463 char *zTemp; |
| 19464 nByte = osGetFullPathNameA((char*)zConverted, 0, 0, 0); |
| 19465 if( nByte==0 ){ |
| 19466 sqlite3_free(zConverted); |
| 19467 return winLogError(SQLITE_CANTOPEN_FULLPATH, osGetLastError(), |
| 19468 "winFullPathname3", zRelative); |
| 19469 } |
| 19470 nByte += 3; |
| 19471 zTemp = sqlite3MallocZero( nByte*sizeof(zTemp[0]) ); |
| 19472 if( zTemp==0 ){ |
| 19473 sqlite3_free(zConverted); |
| 19474 return SQLITE_IOERR_NOMEM; |
| 19475 } |
| 19476 nByte = osGetFullPathNameA((char*)zConverted, nByte, zTemp, 0); |
| 19477 if( nByte==0 ){ |
| 19478 sqlite3_free(zConverted); |
| 19479 sqlite3_free(zTemp); |
| 19480 return winLogError(SQLITE_CANTOPEN_FULLPATH, osGetLastError(), |
| 19481 "winFullPathname4", zRelative); |
| 19482 } |
| 19483 sqlite3_free(zConverted); |
| 19484 zOut = sqlite3_win32_mbcs_to_utf8(zTemp); |
| 19485 sqlite3_free(zTemp); |
| 19486 } |
| 19487 #endif |
| 19488 if( zOut ){ |
| 19489 sqlite3_snprintf(MIN(nFull, pVfs->mxPathname), zFull, "%s", zOut); |
| 19490 sqlite3_free(zOut); |
| 19491 return SQLITE_OK; |
| 19492 }else{ |
| 19493 return SQLITE_IOERR_NOMEM; |
| 19494 } |
| 19495 #endif |
| 19496 } |
| 19497 |
| 19498 #ifndef SQLITE_OMIT_LOAD_EXTENSION |
| 19499 /* |
| 19500 ** Interfaces for opening a shared library, finding entry points |
| 19501 ** within the shared library, and closing the shared library. |
| 19502 */ |
| 19503 static void *winDlOpen(sqlite3_vfs *pVfs, const char *zFilename){ |
| 19504 HANDLE h; |
| 19505 #if defined(__CYGWIN__) |
| 19506 int nFull = pVfs->mxPathname+1; |
| 19507 char *zFull = sqlite3MallocZero( nFull ); |
| 19508 void *zConverted = 0; |
| 19509 if( zFull==0 ){ |
| 19510 OSTRACE(("DLOPEN name=%s, handle=%p\n", zFilename, (void*)0)); |
| 19511 return 0; |
| 19512 } |
| 19513 if( winFullPathname(pVfs, zFilename, nFull, zFull)!=SQLITE_OK ){ |
| 19514 sqlite3_free(zFull); |
| 19515 OSTRACE(("DLOPEN name=%s, handle=%p\n", zFilename, (void*)0)); |
| 19516 return 0; |
| 19517 } |
| 19518 zConverted = winConvertFromUtf8Filename(zFull); |
| 19519 sqlite3_free(zFull); |
| 19520 #else |
| 19521 void *zConverted = winConvertFromUtf8Filename(zFilename); |
| 19522 UNUSED_PARAMETER(pVfs); |
| 19523 #endif |
| 19524 if( zConverted==0 ){ |
| 19525 OSTRACE(("DLOPEN name=%s, handle=%p\n", zFilename, (void*)0)); |
| 19526 return 0; |
| 19527 } |
| 19528 if( osIsNT() ){ |
| 19529 #if SQLITE_OS_WINRT |
| 19530 h = osLoadPackagedLibrary((LPCWSTR)zConverted, 0); |
| 19531 #else |
| 19532 h = osLoadLibraryW((LPCWSTR)zConverted); |
| 19533 #endif |
| 19534 } |
| 19535 #ifdef SQLITE_WIN32_HAS_ANSI |
| 19536 else{ |
| 19537 h = osLoadLibraryA((char*)zConverted); |
| 19538 } |
| 19539 #endif |
| 19540 OSTRACE(("DLOPEN name=%s, handle=%p\n", zFilename, (void*)h)); |
| 19541 sqlite3_free(zConverted); |
| 19542 return (void*)h; |
| 19543 } |
| 19544 static void winDlError(sqlite3_vfs *pVfs, int nBuf, char *zBufOut){ |
| 19545 UNUSED_PARAMETER(pVfs); |
| 19546 winGetLastErrorMsg(osGetLastError(), nBuf, zBufOut); |
| 19547 } |
| 19548 static void (*winDlSym(sqlite3_vfs *pVfs,void *pH,const char *zSym))(void){ |
| 19549 FARPROC proc; |
| 19550 UNUSED_PARAMETER(pVfs); |
| 19551 proc = osGetProcAddressA((HANDLE)pH, zSym); |
| 19552 OSTRACE(("DLSYM handle=%p, symbol=%s, address=%p\n", |
| 19553 (void*)pH, zSym, (void*)proc)); |
| 19554 return (void(*)(void))proc; |
| 19555 } |
| 19556 static void winDlClose(sqlite3_vfs *pVfs, void *pHandle){ |
| 19557 UNUSED_PARAMETER(pVfs); |
| 19558 osFreeLibrary((HANDLE)pHandle); |
| 19559 OSTRACE(("DLCLOSE handle=%p\n", (void*)pHandle)); |
| 19560 } |
| 19561 #else /* if SQLITE_OMIT_LOAD_EXTENSION is defined: */ |
| 19562 #define winDlOpen 0 |
| 19563 #define winDlError 0 |
| 19564 #define winDlSym 0 |
| 19565 #define winDlClose 0 |
| 19566 #endif |
| 19567 |
| 19568 |
| 19569 /* |
| 19570 ** Write up to nBuf bytes of randomness into zBuf. |
| 19571 */ |
| 19572 static int winRandomness(sqlite3_vfs *pVfs, int nBuf, char *zBuf){ |
| 19573 int n = 0; |
| 19574 UNUSED_PARAMETER(pVfs); |
| 19575 #if defined(SQLITE_TEST) || defined(SQLITE_OMIT_RANDOMNESS) |
| 19576 n = nBuf; |
| 19577 memset(zBuf, 0, nBuf); |
| 19578 #else |
| 19579 if( sizeof(SYSTEMTIME)<=nBuf-n ){ |
| 19580 SYSTEMTIME x; |
| 19581 osGetSystemTime(&x); |
| 19582 memcpy(&zBuf[n], &x, sizeof(x)); |
| 19583 n += sizeof(x); |
| 19584 } |
| 19585 if( sizeof(DWORD)<=nBuf-n ){ |
| 19586 DWORD pid = osGetCurrentProcessId(); |
| 19587 memcpy(&zBuf[n], &pid, sizeof(pid)); |
| 19588 n += sizeof(pid); |
| 19589 } |
| 19590 #if SQLITE_OS_WINRT |
| 19591 if( sizeof(ULONGLONG)<=nBuf-n ){ |
| 19592 ULONGLONG cnt = osGetTickCount64(); |
| 19593 memcpy(&zBuf[n], &cnt, sizeof(cnt)); |
| 19594 n += sizeof(cnt); |
| 19595 } |
| 19596 #else |
| 19597 if( sizeof(DWORD)<=nBuf-n ){ |
| 19598 DWORD cnt = osGetTickCount(); |
| 19599 memcpy(&zBuf[n], &cnt, sizeof(cnt)); |
| 19600 n += sizeof(cnt); |
| 19601 } |
| 19602 #endif |
| 19603 if( sizeof(LARGE_INTEGER)<=nBuf-n ){ |
| 19604 LARGE_INTEGER i; |
| 19605 osQueryPerformanceCounter(&i); |
| 19606 memcpy(&zBuf[n], &i, sizeof(i)); |
| 19607 n += sizeof(i); |
| 19608 } |
| 19609 #if !SQLITE_OS_WINCE && !SQLITE_OS_WINRT && SQLITE_WIN32_USE_UUID |
| 19610 if( sizeof(UUID)<=nBuf-n ){ |
| 19611 UUID id; |
| 19612 memset(&id, 0, sizeof(UUID)); |
| 19613 osUuidCreate(&id); |
| 19614 memcpy(&zBuf[n], &id, sizeof(UUID)); |
| 19615 n += sizeof(UUID); |
| 19616 } |
| 19617 if( sizeof(UUID)<=nBuf-n ){ |
| 19618 UUID id; |
| 19619 memset(&id, 0, sizeof(UUID)); |
| 19620 osUuidCreateSequential(&id); |
| 19621 memcpy(&zBuf[n], &id, sizeof(UUID)); |
| 19622 n += sizeof(UUID); |
| 19623 } |
| 19624 #endif |
| 19625 #endif /* defined(SQLITE_TEST) || defined(SQLITE_ZERO_PRNG_SEED) */ |
| 19626 return n; |
| 19627 } |
| 19628 |
| 19629 |
| 19630 /* |
| 19631 ** Sleep for a little while. Return the amount of time slept. |
| 19632 */ |
| 19633 static int winSleep(sqlite3_vfs *pVfs, int microsec){ |
| 19634 sqlite3_win32_sleep((microsec+999)/1000); |
| 19635 UNUSED_PARAMETER(pVfs); |
| 19636 return ((microsec+999)/1000)*1000; |
| 19637 } |
| 19638 |
| 19639 /* |
| 19640 ** The following variable, if set to a non-zero value, is interpreted as |
| 19641 ** the number of seconds since 1970 and is used to set the result of |
| 19642 ** sqlite3OsCurrentTime() during testing. |
| 19643 */ |
| 19644 #ifdef SQLITE_TEST |
| 19645 SQLITE_API int sqlite3_current_time = 0; /* Fake system time in seconds since 1
970. */ |
| 19646 #endif |
| 19647 |
| 19648 /* |
| 19649 ** Find the current time (in Universal Coordinated Time). Write into *piNow |
| 19650 ** the current time and date as a Julian Day number times 86_400_000. In |
| 19651 ** other words, write into *piNow the number of milliseconds since the Julian |
| 19652 ** epoch of noon in Greenwich on November 24, 4714 B.C according to the |
| 19653 ** proleptic Gregorian calendar. |
| 19654 ** |
| 19655 ** On success, return SQLITE_OK. Return SQLITE_ERROR if the time and date |
| 19656 ** cannot be found. |
| 19657 */ |
| 19658 static int winCurrentTimeInt64(sqlite3_vfs *pVfs, sqlite3_int64 *piNow){ |
| 19659 /* FILETIME structure is a 64-bit value representing the number of |
| 19660 100-nanosecond intervals since January 1, 1601 (= JD 2305813.5). |
| 19661 */ |
| 19662 FILETIME ft; |
| 19663 static const sqlite3_int64 winFiletimeEpoch = 23058135*(sqlite3_int64)8640000; |
| 19664 #ifdef SQLITE_TEST |
| 19665 static const sqlite3_int64 unixEpoch = 24405875*(sqlite3_int64)8640000; |
| 19666 #endif |
| 19667 /* 2^32 - to avoid use of LL and warnings in gcc */ |
| 19668 static const sqlite3_int64 max32BitValue = |
| 19669 (sqlite3_int64)2000000000 + (sqlite3_int64)2000000000 + |
| 19670 (sqlite3_int64)294967296; |
| 19671 |
| 19672 #if SQLITE_OS_WINCE |
| 19673 SYSTEMTIME time; |
| 19674 osGetSystemTime(&time); |
| 19675 /* if SystemTimeToFileTime() fails, it returns zero. */ |
| 19676 if (!osSystemTimeToFileTime(&time,&ft)){ |
| 19677 return SQLITE_ERROR; |
| 19678 } |
| 19679 #else |
| 19680 osGetSystemTimeAsFileTime( &ft ); |
| 19681 #endif |
| 19682 |
| 19683 *piNow = winFiletimeEpoch + |
| 19684 ((((sqlite3_int64)ft.dwHighDateTime)*max32BitValue) + |
| 19685 (sqlite3_int64)ft.dwLowDateTime)/(sqlite3_int64)10000; |
| 19686 |
| 19687 #ifdef SQLITE_TEST |
| 19688 if( sqlite3_current_time ){ |
| 19689 *piNow = 1000*(sqlite3_int64)sqlite3_current_time + unixEpoch; |
| 19690 } |
| 19691 #endif |
| 19692 UNUSED_PARAMETER(pVfs); |
| 19693 return SQLITE_OK; |
| 19694 } |
| 19695 |
| 19696 /* |
| 19697 ** Find the current time (in Universal Coordinated Time). Write the |
| 19698 ** current time and date as a Julian Day number into *prNow and |
| 19699 ** return 0. Return 1 if the time and date cannot be found. |
| 19700 */ |
| 19701 static int winCurrentTime(sqlite3_vfs *pVfs, double *prNow){ |
| 19702 int rc; |
| 19703 sqlite3_int64 i; |
| 19704 rc = winCurrentTimeInt64(pVfs, &i); |
| 19705 if( !rc ){ |
| 19706 *prNow = i/86400000.0; |
| 19707 } |
| 19708 return rc; |
| 19709 } |
| 19710 |
| 19711 /* |
| 19712 ** The idea is that this function works like a combination of |
| 19713 ** GetLastError() and FormatMessage() on Windows (or errno and |
| 19714 ** strerror_r() on Unix). After an error is returned by an OS |
| 19715 ** function, SQLite calls this function with zBuf pointing to |
| 19716 ** a buffer of nBuf bytes. The OS layer should populate the |
| 19717 ** buffer with a nul-terminated UTF-8 encoded error message |
| 19718 ** describing the last IO error to have occurred within the calling |
| 19719 ** thread. |
| 19720 ** |
| 19721 ** If the error message is too large for the supplied buffer, |
| 19722 ** it should be truncated. The return value of xGetLastError |
| 19723 ** is zero if the error message fits in the buffer, or non-zero |
| 19724 ** otherwise (if the message was truncated). If non-zero is returned, |
| 19725 ** then it is not necessary to include the nul-terminator character |
| 19726 ** in the output buffer. |
| 19727 ** |
| 19728 ** Not supplying an error message will have no adverse effect |
| 19729 ** on SQLite. It is fine to have an implementation that never |
| 19730 ** returns an error message: |
| 19731 ** |
| 19732 ** int xGetLastError(sqlite3_vfs *pVfs, int nBuf, char *zBuf){ |
| 19733 ** assert(zBuf[0]=='\0'); |
| 19734 ** return 0; |
| 19735 ** } |
| 19736 ** |
| 19737 ** However if an error message is supplied, it will be incorporated |
| 19738 ** by sqlite into the error message available to the user using |
| 19739 ** sqlite3_errmsg(), possibly making IO errors easier to debug. |
| 19740 */ |
| 19741 static int winGetLastError(sqlite3_vfs *pVfs, int nBuf, char *zBuf){ |
| 19742 UNUSED_PARAMETER(pVfs); |
| 19743 return winGetLastErrorMsg(osGetLastError(), nBuf, zBuf); |
| 19744 } |
| 19745 |
| 19746 /* |
| 19747 ** Initialize and deinitialize the operating system interface. |
| 19748 */ |
| 19749 SQLITE_API int SQLITE_STDCALL sqlite3_os_init(void){ |
| 19750 static sqlite3_vfs winVfs = { |
| 19751 3, /* iVersion */ |
| 19752 sizeof(winFile), /* szOsFile */ |
| 19753 SQLITE_WIN32_MAX_PATH_BYTES, /* mxPathname */ |
| 19754 0, /* pNext */ |
| 19755 "win32", /* zName */ |
| 19756 0, /* pAppData */ |
| 19757 winOpen, /* xOpen */ |
| 19758 winDelete, /* xDelete */ |
| 19759 winAccess, /* xAccess */ |
| 19760 winFullPathname, /* xFullPathname */ |
| 19761 winDlOpen, /* xDlOpen */ |
| 19762 winDlError, /* xDlError */ |
| 19763 winDlSym, /* xDlSym */ |
| 19764 winDlClose, /* xDlClose */ |
| 19765 winRandomness, /* xRandomness */ |
| 19766 winSleep, /* xSleep */ |
| 19767 winCurrentTime, /* xCurrentTime */ |
| 19768 winGetLastError, /* xGetLastError */ |
| 19769 winCurrentTimeInt64, /* xCurrentTimeInt64 */ |
| 19770 winSetSystemCall, /* xSetSystemCall */ |
| 19771 winGetSystemCall, /* xGetSystemCall */ |
| 19772 winNextSystemCall, /* xNextSystemCall */ |
| 19773 }; |
| 19774 #if defined(SQLITE_WIN32_HAS_WIDE) |
| 19775 static sqlite3_vfs winLongPathVfs = { |
| 19776 3, /* iVersion */ |
| 19777 sizeof(winFile), /* szOsFile */ |
| 19778 SQLITE_WINNT_MAX_PATH_BYTES, /* mxPathname */ |
| 19779 0, /* pNext */ |
| 19780 "win32-longpath", /* zName */ |
| 19781 0, /* pAppData */ |
| 19782 winOpen, /* xOpen */ |
| 19783 winDelete, /* xDelete */ |
| 19784 winAccess, /* xAccess */ |
| 19785 winFullPathname, /* xFullPathname */ |
| 19786 winDlOpen, /* xDlOpen */ |
| 19787 winDlError, /* xDlError */ |
| 19788 winDlSym, /* xDlSym */ |
| 19789 winDlClose, /* xDlClose */ |
| 19790 winRandomness, /* xRandomness */ |
| 19791 winSleep, /* xSleep */ |
| 19792 winCurrentTime, /* xCurrentTime */ |
| 19793 winGetLastError, /* xGetLastError */ |
| 19794 winCurrentTimeInt64, /* xCurrentTimeInt64 */ |
| 19795 winSetSystemCall, /* xSetSystemCall */ |
| 19796 winGetSystemCall, /* xGetSystemCall */ |
| 19797 winNextSystemCall, /* xNextSystemCall */ |
| 19798 }; |
| 19799 #endif |
| 19800 |
| 19801 /* Double-check that the aSyscall[] array has been constructed |
| 19802 ** correctly. See ticket [bb3a86e890c8e96ab] */ |
| 19803 assert( ArraySize(aSyscall)==80 ); |
| 19804 |
| 19805 /* get memory map allocation granularity */ |
| 19806 memset(&winSysInfo, 0, sizeof(SYSTEM_INFO)); |
| 19807 #if SQLITE_OS_WINRT |
| 19808 osGetNativeSystemInfo(&winSysInfo); |
| 19809 #else |
| 19810 osGetSystemInfo(&winSysInfo); |
| 19811 #endif |
| 19812 assert( winSysInfo.dwAllocationGranularity>0 ); |
| 19813 assert( winSysInfo.dwPageSize>0 ); |
| 19814 |
| 19815 sqlite3_vfs_register(&winVfs, 1); |
| 19816 |
| 19817 #if defined(SQLITE_WIN32_HAS_WIDE) |
| 19818 sqlite3_vfs_register(&winLongPathVfs, 0); |
| 19819 #endif |
| 19820 |
| 19821 return SQLITE_OK; |
| 19822 } |
| 19823 |
| 19824 SQLITE_API int SQLITE_STDCALL sqlite3_os_end(void){ |
| 19825 #if SQLITE_OS_WINRT |
| 19826 if( sleepObj!=NULL ){ |
| 19827 osCloseHandle(sleepObj); |
| 19828 sleepObj = NULL; |
| 19829 } |
| 19830 #endif |
| 19831 return SQLITE_OK; |
| 19832 } |
| 19833 |
| 19834 CHROMIUM_SQLITE_API |
| 19835 void chromium_sqlite3_initialize_win_sqlite3_file(sqlite3_file* file, HANDLE han
dle) { |
| 19836 winFile* winSQLite3File = (winFile*)file; |
| 19837 memset(file, 0, sizeof(*file)); |
| 19838 winSQLite3File->pMethod = &winIoMethod; |
| 19839 winSQLite3File->h = handle; |
| 19840 } |
| 19841 |
| 19842 #endif /* SQLITE_OS_WIN */ |
| 19843 |
| 19844 /************** End of os_win.c **********************************************/ |
| 19845 /************** Begin file bitvec.c ******************************************/ |
| 19846 /* |
| 19847 ** 2008 February 16 |
| 19848 ** |
| 19849 ** The author disclaims copyright to this source code. In place of |
| 19850 ** a legal notice, here is a blessing: |
| 19851 ** |
| 19852 ** May you do good and not evil. |
| 19853 ** May you find forgiveness for yourself and forgive others. |
| 19854 ** May you share freely, never taking more than you give. |
| 19855 ** |
| 19856 ************************************************************************* |
| 19857 ** This file implements an object that represents a fixed-length |
| 19858 ** bitmap. Bits are numbered starting with 1. |
| 19859 ** |
| 19860 ** A bitmap is used to record which pages of a database file have been |
| 19861 ** journalled during a transaction, or which pages have the "dont-write" |
| 19862 ** property. Usually only a few pages are meet either condition. |
| 19863 ** So the bitmap is usually sparse and has low cardinality. |
| 19864 ** But sometimes (for example when during a DROP of a large table) most |
| 19865 ** or all of the pages in a database can get journalled. In those cases, |
| 19866 ** the bitmap becomes dense with high cardinality. The algorithm needs |
| 19867 ** to handle both cases well. |
| 19868 ** |
| 19869 ** The size of the bitmap is fixed when the object is created. |
| 19870 ** |
| 19871 ** All bits are clear when the bitmap is created. Individual bits |
| 19872 ** may be set or cleared one at a time. |
| 19873 ** |
| 19874 ** Test operations are about 100 times more common that set operations. |
| 19875 ** Clear operations are exceedingly rare. There are usually between |
| 19876 ** 5 and 500 set operations per Bitvec object, though the number of sets can |
| 19877 ** sometimes grow into tens of thousands or larger. The size of the |
| 19878 ** Bitvec object is the number of pages in the database file at the |
| 19879 ** start of a transaction, and is thus usually less than a few thousand, |
| 19880 ** but can be as large as 2 billion for a really big database. |
| 19881 */ |
| 19882 /* #include "sqliteInt.h" */ |
| 19883 |
| 19884 /* Size of the Bitvec structure in bytes. */ |
| 19885 #define BITVEC_SZ 512 |
| 19886 |
| 19887 /* Round the union size down to the nearest pointer boundary, since that's how |
| 19888 ** it will be aligned within the Bitvec struct. */ |
| 19889 #define BITVEC_USIZE \ |
| 19890 (((BITVEC_SZ-(3*sizeof(u32)))/sizeof(Bitvec*))*sizeof(Bitvec*)) |
| 19891 |
| 19892 /* Type of the array "element" for the bitmap representation. |
| 19893 ** Should be a power of 2, and ideally, evenly divide into BITVEC_USIZE. |
| 19894 ** Setting this to the "natural word" size of your CPU may improve |
| 19895 ** performance. */ |
| 19896 #define BITVEC_TELEM u8 |
| 19897 /* Size, in bits, of the bitmap element. */ |
| 19898 #define BITVEC_SZELEM 8 |
| 19899 /* Number of elements in a bitmap array. */ |
| 19900 #define BITVEC_NELEM (BITVEC_USIZE/sizeof(BITVEC_TELEM)) |
| 19901 /* Number of bits in the bitmap array. */ |
| 19902 #define BITVEC_NBIT (BITVEC_NELEM*BITVEC_SZELEM) |
| 19903 |
| 19904 /* Number of u32 values in hash table. */ |
| 19905 #define BITVEC_NINT (BITVEC_USIZE/sizeof(u32)) |
| 19906 /* Maximum number of entries in hash table before |
| 19907 ** sub-dividing and re-hashing. */ |
| 19908 #define BITVEC_MXHASH (BITVEC_NINT/2) |
| 19909 /* Hashing function for the aHash representation. |
| 19910 ** Empirical testing showed that the *37 multiplier |
| 19911 ** (an arbitrary prime)in the hash function provided |
| 19912 ** no fewer collisions than the no-op *1. */ |
| 19913 #define BITVEC_HASH(X) (((X)*1)%BITVEC_NINT) |
| 19914 |
| 19915 #define BITVEC_NPTR (BITVEC_USIZE/sizeof(Bitvec *)) |
| 19916 |
| 19917 |
| 19918 /* |
| 19919 ** A bitmap is an instance of the following structure. |
| 19920 ** |
| 19921 ** This bitmap records the existence of zero or more bits |
| 19922 ** with values between 1 and iSize, inclusive. |
| 19923 ** |
| 19924 ** There are three possible representations of the bitmap. |
| 19925 ** If iSize<=BITVEC_NBIT, then Bitvec.u.aBitmap[] is a straight |
| 19926 ** bitmap. The least significant bit is bit 1. |
| 19927 ** |
| 19928 ** If iSize>BITVEC_NBIT and iDivisor==0 then Bitvec.u.aHash[] is |
| 19929 ** a hash table that will hold up to BITVEC_MXHASH distinct values. |
| 19930 ** |
| 19931 ** Otherwise, the value i is redirected into one of BITVEC_NPTR |
| 19932 ** sub-bitmaps pointed to by Bitvec.u.apSub[]. Each subbitmap |
| 19933 ** handles up to iDivisor separate values of i. apSub[0] holds |
| 19934 ** values between 1 and iDivisor. apSub[1] holds values between |
| 19935 ** iDivisor+1 and 2*iDivisor. apSub[N] holds values between |
| 19936 ** N*iDivisor+1 and (N+1)*iDivisor. Each subbitmap is normalized |
| 19937 ** to hold deal with values between 1 and iDivisor. |
| 19938 */ |
| 19939 struct Bitvec { |
| 19940 u32 iSize; /* Maximum bit index. Max iSize is 4,294,967,296. */ |
| 19941 u32 nSet; /* Number of bits that are set - only valid for aHash |
| 19942 ** element. Max is BITVEC_NINT. For BITVEC_SZ of 512, |
| 19943 ** this would be 125. */ |
| 19944 u32 iDivisor; /* Number of bits handled by each apSub[] entry. */ |
| 19945 /* Should >=0 for apSub element. */ |
| 19946 /* Max iDivisor is max(u32) / BITVEC_NPTR + 1. */ |
| 19947 /* For a BITVEC_SZ of 512, this would be 34,359,739. */ |
| 19948 union { |
| 19949 BITVEC_TELEM aBitmap[BITVEC_NELEM]; /* Bitmap representation */ |
| 19950 u32 aHash[BITVEC_NINT]; /* Hash table representation */ |
| 19951 Bitvec *apSub[BITVEC_NPTR]; /* Recursive representation */ |
| 19952 } u; |
| 19953 }; |
| 19954 |
| 19955 /* |
| 19956 ** Create a new bitmap object able to handle bits between 0 and iSize, |
| 19957 ** inclusive. Return a pointer to the new object. Return NULL if |
| 19958 ** malloc fails. |
| 19959 */ |
| 19960 SQLITE_PRIVATE Bitvec *sqlite3BitvecCreate(u32 iSize){ |
| 19961 Bitvec *p; |
| 19962 assert( sizeof(*p)==BITVEC_SZ ); |
| 19963 p = sqlite3MallocZero( sizeof(*p) ); |
| 19964 if( p ){ |
| 19965 p->iSize = iSize; |
| 19966 } |
| 19967 return p; |
| 19968 } |
| 19969 |
| 19970 /* |
| 19971 ** Check to see if the i-th bit is set. Return true or false. |
| 19972 ** If p is NULL (if the bitmap has not been created) or if |
| 19973 ** i is out of range, then return false. |
| 19974 */ |
| 19975 SQLITE_PRIVATE int sqlite3BitvecTestNotNull(Bitvec *p, u32 i){ |
| 19976 assert( p!=0 ); |
| 19977 i--; |
| 19978 if( i>=p->iSize ) return 0; |
| 19979 while( p->iDivisor ){ |
| 19980 u32 bin = i/p->iDivisor; |
| 19981 i = i%p->iDivisor; |
| 19982 p = p->u.apSub[bin]; |
| 19983 if (!p) { |
| 19984 return 0; |
| 19985 } |
| 19986 } |
| 19987 if( p->iSize<=BITVEC_NBIT ){ |
| 19988 return (p->u.aBitmap[i/BITVEC_SZELEM] & (1<<(i&(BITVEC_SZELEM-1))))!=0; |
| 19989 } else{ |
| 19990 u32 h = BITVEC_HASH(i++); |
| 19991 while( p->u.aHash[h] ){ |
| 19992 if( p->u.aHash[h]==i ) return 1; |
| 19993 h = (h+1) % BITVEC_NINT; |
| 19994 } |
| 19995 return 0; |
| 19996 } |
| 19997 } |
| 19998 SQLITE_PRIVATE int sqlite3BitvecTest(Bitvec *p, u32 i){ |
| 19999 return p!=0 && sqlite3BitvecTestNotNull(p,i); |
| 20000 } |
| 20001 |
| 20002 /* |
| 20003 ** Set the i-th bit. Return 0 on success and an error code if |
| 20004 ** anything goes wrong. |
| 20005 ** |
| 20006 ** This routine might cause sub-bitmaps to be allocated. Failing |
| 20007 ** to get the memory needed to hold the sub-bitmap is the only |
| 20008 ** that can go wrong with an insert, assuming p and i are valid. |
| 20009 ** |
| 20010 ** The calling function must ensure that p is a valid Bitvec object |
| 20011 ** and that the value for "i" is within range of the Bitvec object. |
| 20012 ** Otherwise the behavior is undefined. |
| 20013 */ |
| 20014 SQLITE_PRIVATE int sqlite3BitvecSet(Bitvec *p, u32 i){ |
| 20015 u32 h; |
| 20016 if( p==0 ) return SQLITE_OK; |
| 20017 assert( i>0 ); |
| 20018 assert( i<=p->iSize ); |
| 20019 i--; |
| 20020 while((p->iSize > BITVEC_NBIT) && p->iDivisor) { |
| 20021 u32 bin = i/p->iDivisor; |
| 20022 i = i%p->iDivisor; |
| 20023 if( p->u.apSub[bin]==0 ){ |
| 20024 p->u.apSub[bin] = sqlite3BitvecCreate( p->iDivisor ); |
| 20025 if( p->u.apSub[bin]==0 ) return SQLITE_NOMEM; |
| 20026 } |
| 20027 p = p->u.apSub[bin]; |
| 20028 } |
| 20029 if( p->iSize<=BITVEC_NBIT ){ |
| 20030 p->u.aBitmap[i/BITVEC_SZELEM] |= 1 << (i&(BITVEC_SZELEM-1)); |
| 20031 return SQLITE_OK; |
| 20032 } |
| 20033 h = BITVEC_HASH(i++); |
| 20034 /* if there wasn't a hash collision, and this doesn't */ |
| 20035 /* completely fill the hash, then just add it without */ |
| 20036 /* worring about sub-dividing and re-hashing. */ |
| 20037 if( !p->u.aHash[h] ){ |
| 20038 if (p->nSet<(BITVEC_NINT-1)) { |
| 20039 goto bitvec_set_end; |
| 20040 } else { |
| 20041 goto bitvec_set_rehash; |
| 20042 } |
| 20043 } |
| 20044 /* there was a collision, check to see if it's already */ |
| 20045 /* in hash, if not, try to find a spot for it */ |
| 20046 do { |
| 20047 if( p->u.aHash[h]==i ) return SQLITE_OK; |
| 20048 h++; |
| 20049 if( h>=BITVEC_NINT ) h = 0; |
| 20050 } while( p->u.aHash[h] ); |
| 20051 /* we didn't find it in the hash. h points to the first */ |
| 20052 /* available free spot. check to see if this is going to */ |
| 20053 /* make our hash too "full". */ |
| 20054 bitvec_set_rehash: |
| 20055 if( p->nSet>=BITVEC_MXHASH ){ |
| 20056 unsigned int j; |
| 20057 int rc; |
| 20058 u32 *aiValues = sqlite3StackAllocRaw(0, sizeof(p->u.aHash)); |
| 20059 if( aiValues==0 ){ |
| 20060 return SQLITE_NOMEM; |
| 20061 }else{ |
| 20062 memcpy(aiValues, p->u.aHash, sizeof(p->u.aHash)); |
| 20063 memset(p->u.apSub, 0, sizeof(p->u.apSub)); |
| 20064 p->iDivisor = (p->iSize + BITVEC_NPTR - 1)/BITVEC_NPTR; |
| 20065 rc = sqlite3BitvecSet(p, i); |
| 20066 for(j=0; j<BITVEC_NINT; j++){ |
| 20067 if( aiValues[j] ) rc |= sqlite3BitvecSet(p, aiValues[j]); |
| 20068 } |
| 20069 sqlite3StackFree(0, aiValues); |
| 20070 return rc; |
| 20071 } |
| 20072 } |
| 20073 bitvec_set_end: |
| 20074 p->nSet++; |
| 20075 p->u.aHash[h] = i; |
| 20076 return SQLITE_OK; |
| 20077 } |
| 20078 |
| 20079 /* |
| 20080 ** Clear the i-th bit. |
| 20081 ** |
| 20082 ** pBuf must be a pointer to at least BITVEC_SZ bytes of temporary storage |
| 20083 ** that BitvecClear can use to rebuilt its hash table. |
| 20084 */ |
| 20085 SQLITE_PRIVATE void sqlite3BitvecClear(Bitvec *p, u32 i, void *pBuf){ |
| 20086 if( p==0 ) return; |
| 20087 assert( i>0 ); |
| 20088 i--; |
| 20089 while( p->iDivisor ){ |
| 20090 u32 bin = i/p->iDivisor; |
| 20091 i = i%p->iDivisor; |
| 20092 p = p->u.apSub[bin]; |
| 20093 if (!p) { |
| 20094 return; |
| 20095 } |
| 20096 } |
| 20097 if( p->iSize<=BITVEC_NBIT ){ |
| 20098 p->u.aBitmap[i/BITVEC_SZELEM] &= ~(1 << (i&(BITVEC_SZELEM-1))); |
| 20099 }else{ |
| 20100 unsigned int j; |
| 20101 u32 *aiValues = pBuf; |
| 20102 memcpy(aiValues, p->u.aHash, sizeof(p->u.aHash)); |
| 20103 memset(p->u.aHash, 0, sizeof(p->u.aHash)); |
| 20104 p->nSet = 0; |
| 20105 for(j=0; j<BITVEC_NINT; j++){ |
| 20106 if( aiValues[j] && aiValues[j]!=(i+1) ){ |
| 20107 u32 h = BITVEC_HASH(aiValues[j]-1); |
| 20108 p->nSet++; |
| 20109 while( p->u.aHash[h] ){ |
| 20110 h++; |
| 20111 if( h>=BITVEC_NINT ) h = 0; |
| 20112 } |
| 20113 p->u.aHash[h] = aiValues[j]; |
| 20114 } |
| 20115 } |
| 20116 } |
| 20117 } |
| 20118 |
| 20119 /* |
| 20120 ** Destroy a bitmap object. Reclaim all memory used. |
| 20121 */ |
| 20122 SQLITE_PRIVATE void sqlite3BitvecDestroy(Bitvec *p){ |
| 20123 if( p==0 ) return; |
| 20124 if( p->iDivisor ){ |
| 20125 unsigned int i; |
| 20126 for(i=0; i<BITVEC_NPTR; i++){ |
| 20127 sqlite3BitvecDestroy(p->u.apSub[i]); |
| 20128 } |
| 20129 } |
| 20130 sqlite3_free(p); |
| 20131 } |
| 20132 |
| 20133 /* |
| 20134 ** Return the value of the iSize parameter specified when Bitvec *p |
| 20135 ** was created. |
| 20136 */ |
| 20137 SQLITE_PRIVATE u32 sqlite3BitvecSize(Bitvec *p){ |
| 20138 return p->iSize; |
| 20139 } |
| 20140 |
| 20141 #ifndef SQLITE_OMIT_BUILTIN_TEST |
| 20142 /* |
| 20143 ** Let V[] be an array of unsigned characters sufficient to hold |
| 20144 ** up to N bits. Let I be an integer between 0 and N. 0<=I<N. |
| 20145 ** Then the following macros can be used to set, clear, or test |
| 20146 ** individual bits within V. |
| 20147 */ |
| 20148 #define SETBIT(V,I) V[I>>3] |= (1<<(I&7)) |
| 20149 #define CLEARBIT(V,I) V[I>>3] &= ~(1<<(I&7)) |
| 20150 #define TESTBIT(V,I) (V[I>>3]&(1<<(I&7)))!=0 |
| 20151 |
| 20152 /* |
| 20153 ** This routine runs an extensive test of the Bitvec code. |
| 20154 ** |
| 20155 ** The input is an array of integers that acts as a program |
| 20156 ** to test the Bitvec. The integers are opcodes followed |
| 20157 ** by 0, 1, or 3 operands, depending on the opcode. Another |
| 20158 ** opcode follows immediately after the last operand. |
| 20159 ** |
| 20160 ** There are 6 opcodes numbered from 0 through 5. 0 is the |
| 20161 ** "halt" opcode and causes the test to end. |
| 20162 ** |
| 20163 ** 0 Halt and return the number of errors |
| 20164 ** 1 N S X Set N bits beginning with S and incrementing by X |
| 20165 ** 2 N S X Clear N bits beginning with S and incrementing by X |
| 20166 ** 3 N Set N randomly chosen bits |
| 20167 ** 4 N Clear N randomly chosen bits |
| 20168 ** 5 N S X Set N bits from S increment X in array only, not in bitvec |
| 20169 ** |
| 20170 ** The opcodes 1 through 4 perform set and clear operations are performed |
| 20171 ** on both a Bitvec object and on a linear array of bits obtained from malloc. |
| 20172 ** Opcode 5 works on the linear array only, not on the Bitvec. |
| 20173 ** Opcode 5 is used to deliberately induce a fault in order to |
| 20174 ** confirm that error detection works. |
| 20175 ** |
| 20176 ** At the conclusion of the test the linear array is compared |
| 20177 ** against the Bitvec object. If there are any differences, |
| 20178 ** an error is returned. If they are the same, zero is returned. |
| 20179 ** |
| 20180 ** If a memory allocation error occurs, return -1. |
| 20181 */ |
| 20182 SQLITE_PRIVATE int sqlite3BitvecBuiltinTest(int sz, int *aOp){ |
| 20183 Bitvec *pBitvec = 0; |
| 20184 unsigned char *pV = 0; |
| 20185 int rc = -1; |
| 20186 int i, nx, pc, op; |
| 20187 void *pTmpSpace; |
| 20188 |
| 20189 /* Allocate the Bitvec to be tested and a linear array of |
| 20190 ** bits to act as the reference */ |
| 20191 pBitvec = sqlite3BitvecCreate( sz ); |
| 20192 pV = sqlite3MallocZero( (sz+7)/8 + 1 ); |
| 20193 pTmpSpace = sqlite3_malloc64(BITVEC_SZ); |
| 20194 if( pBitvec==0 || pV==0 || pTmpSpace==0 ) goto bitvec_end; |
| 20195 |
| 20196 /* NULL pBitvec tests */ |
| 20197 sqlite3BitvecSet(0, 1); |
| 20198 sqlite3BitvecClear(0, 1, pTmpSpace); |
| 20199 |
| 20200 /* Run the program */ |
| 20201 pc = 0; |
| 20202 while( (op = aOp[pc])!=0 ){ |
| 20203 switch( op ){ |
| 20204 case 1: |
| 20205 case 2: |
| 20206 case 5: { |
| 20207 nx = 4; |
| 20208 i = aOp[pc+2] - 1; |
| 20209 aOp[pc+2] += aOp[pc+3]; |
| 20210 break; |
| 20211 } |
| 20212 case 3: |
| 20213 case 4: |
| 20214 default: { |
| 20215 nx = 2; |
| 20216 sqlite3_randomness(sizeof(i), &i); |
| 20217 break; |
| 20218 } |
| 20219 } |
| 20220 if( (--aOp[pc+1]) > 0 ) nx = 0; |
| 20221 pc += nx; |
| 20222 i = (i & 0x7fffffff)%sz; |
| 20223 if( (op & 1)!=0 ){ |
| 20224 SETBIT(pV, (i+1)); |
| 20225 if( op!=5 ){ |
| 20226 if( sqlite3BitvecSet(pBitvec, i+1) ) goto bitvec_end; |
| 20227 } |
| 20228 }else{ |
| 20229 CLEARBIT(pV, (i+1)); |
| 20230 sqlite3BitvecClear(pBitvec, i+1, pTmpSpace); |
| 20231 } |
| 20232 } |
| 20233 |
| 20234 /* Test to make sure the linear array exactly matches the |
| 20235 ** Bitvec object. Start with the assumption that they do |
| 20236 ** match (rc==0). Change rc to non-zero if a discrepancy |
| 20237 ** is found. |
| 20238 */ |
| 20239 rc = sqlite3BitvecTest(0,0) + sqlite3BitvecTest(pBitvec, sz+1) |
| 20240 + sqlite3BitvecTest(pBitvec, 0) |
| 20241 + (sqlite3BitvecSize(pBitvec) - sz); |
| 20242 for(i=1; i<=sz; i++){ |
| 20243 if( (TESTBIT(pV,i))!=sqlite3BitvecTest(pBitvec,i) ){ |
| 20244 rc = i; |
| 20245 break; |
| 20246 } |
| 20247 } |
| 20248 |
| 20249 /* Free allocated structure */ |
| 20250 bitvec_end: |
| 20251 sqlite3_free(pTmpSpace); |
| 20252 sqlite3_free(pV); |
| 20253 sqlite3BitvecDestroy(pBitvec); |
| 20254 return rc; |
| 20255 } |
| 20256 #endif /* SQLITE_OMIT_BUILTIN_TEST */ |
| 20257 |
| 20258 /************** End of bitvec.c **********************************************/ |
| 20259 /************** Begin file pcache.c ******************************************/ |
| 20260 /* |
| 20261 ** 2008 August 05 |
| 20262 ** |
| 20263 ** The author disclaims copyright to this source code. In place of |
| 20264 ** a legal notice, here is a blessing: |
| 20265 ** |
| 20266 ** May you do good and not evil. |
| 20267 ** May you find forgiveness for yourself and forgive others. |
| 20268 ** May you share freely, never taking more than you give. |
| 20269 ** |
| 20270 ************************************************************************* |
| 20271 ** This file implements that page cache. |
| 20272 */ |
| 20273 /* #include "sqliteInt.h" */ |
| 20274 |
| 20275 /* |
| 20276 ** A complete page cache is an instance of this structure. |
| 20277 */ |
| 20278 struct PCache { |
| 20279 PgHdr *pDirty, *pDirtyTail; /* List of dirty pages in LRU order */ |
| 20280 PgHdr *pSynced; /* Last synced page in dirty page list */ |
| 20281 int nRefSum; /* Sum of ref counts over all pages */ |
| 20282 int szCache; /* Configured cache size */ |
| 20283 int szSpill; /* Size before spilling occurs */ |
| 20284 int szPage; /* Size of every page in this cache */ |
| 20285 int szExtra; /* Size of extra space for each page */ |
| 20286 u8 bPurgeable; /* True if pages are on backing store */ |
| 20287 u8 eCreate; /* eCreate value for for xFetch() */ |
| 20288 int (*xStress)(void*,PgHdr*); /* Call to try make a page clean */ |
| 20289 void *pStress; /* Argument to xStress */ |
| 20290 sqlite3_pcache *pCache; /* Pluggable cache module */ |
| 20291 }; |
| 20292 |
| 20293 /********************************** Linked List Management ********************/ |
| 20294 |
| 20295 /* Allowed values for second argument to pcacheManageDirtyList() */ |
| 20296 #define PCACHE_DIRTYLIST_REMOVE 1 /* Remove pPage from dirty list */ |
| 20297 #define PCACHE_DIRTYLIST_ADD 2 /* Add pPage to the dirty list */ |
| 20298 #define PCACHE_DIRTYLIST_FRONT 3 /* Move pPage to the front of the list */ |
| 20299 |
| 20300 /* |
| 20301 ** Manage pPage's participation on the dirty list. Bits of the addRemove |
| 20302 ** argument determines what operation to do. The 0x01 bit means first |
| 20303 ** remove pPage from the dirty list. The 0x02 means add pPage back to |
| 20304 ** the dirty list. Doing both moves pPage to the front of the dirty list. |
| 20305 */ |
| 20306 static void pcacheManageDirtyList(PgHdr *pPage, u8 addRemove){ |
| 20307 PCache *p = pPage->pCache; |
| 20308 |
| 20309 if( addRemove & PCACHE_DIRTYLIST_REMOVE ){ |
| 20310 assert( pPage->pDirtyNext || pPage==p->pDirtyTail ); |
| 20311 assert( pPage->pDirtyPrev || pPage==p->pDirty ); |
| 20312 |
| 20313 /* Update the PCache1.pSynced variable if necessary. */ |
| 20314 if( p->pSynced==pPage ){ |
| 20315 PgHdr *pSynced = pPage->pDirtyPrev; |
| 20316 while( pSynced && (pSynced->flags&PGHDR_NEED_SYNC) ){ |
| 20317 pSynced = pSynced->pDirtyPrev; |
| 20318 } |
| 20319 p->pSynced = pSynced; |
| 20320 } |
| 20321 |
| 20322 if( pPage->pDirtyNext ){ |
| 20323 pPage->pDirtyNext->pDirtyPrev = pPage->pDirtyPrev; |
| 20324 }else{ |
| 20325 assert( pPage==p->pDirtyTail ); |
| 20326 p->pDirtyTail = pPage->pDirtyPrev; |
| 20327 } |
| 20328 if( pPage->pDirtyPrev ){ |
| 20329 pPage->pDirtyPrev->pDirtyNext = pPage->pDirtyNext; |
| 20330 }else{ |
| 20331 assert( pPage==p->pDirty ); |
| 20332 p->pDirty = pPage->pDirtyNext; |
| 20333 if( p->pDirty==0 && p->bPurgeable ){ |
| 20334 assert( p->eCreate==1 ); |
| 20335 p->eCreate = 2; |
| 20336 } |
| 20337 } |
| 20338 pPage->pDirtyNext = 0; |
| 20339 pPage->pDirtyPrev = 0; |
| 20340 } |
| 20341 if( addRemove & PCACHE_DIRTYLIST_ADD ){ |
| 20342 assert( pPage->pDirtyNext==0 && pPage->pDirtyPrev==0 && p->pDirty!=pPage ); |
| 20343 |
| 20344 pPage->pDirtyNext = p->pDirty; |
| 20345 if( pPage->pDirtyNext ){ |
| 20346 assert( pPage->pDirtyNext->pDirtyPrev==0 ); |
| 20347 pPage->pDirtyNext->pDirtyPrev = pPage; |
| 20348 }else{ |
| 20349 p->pDirtyTail = pPage; |
| 20350 if( p->bPurgeable ){ |
| 20351 assert( p->eCreate==2 ); |
| 20352 p->eCreate = 1; |
| 20353 } |
| 20354 } |
| 20355 p->pDirty = pPage; |
| 20356 if( !p->pSynced && 0==(pPage->flags&PGHDR_NEED_SYNC) ){ |
| 20357 p->pSynced = pPage; |
| 20358 } |
| 20359 } |
| 20360 } |
| 20361 |
| 20362 /* |
| 20363 ** Wrapper around the pluggable caches xUnpin method. If the cache is |
| 20364 ** being used for an in-memory database, this function is a no-op. |
| 20365 */ |
| 20366 static void pcacheUnpin(PgHdr *p){ |
| 20367 if( p->pCache->bPurgeable ){ |
| 20368 sqlite3GlobalConfig.pcache2.xUnpin(p->pCache->pCache, p->pPage, 0); |
| 20369 } |
| 20370 } |
| 20371 |
| 20372 /* |
| 20373 ** Compute the number of pages of cache requested. p->szCache is the |
| 20374 ** cache size requested by the "PRAGMA cache_size" statement. |
| 20375 */ |
| 20376 static int numberOfCachePages(PCache *p){ |
| 20377 if( p->szCache>=0 ){ |
| 20378 /* IMPLEMENTATION-OF: R-42059-47211 If the argument N is positive then the |
| 20379 ** suggested cache size is set to N. */ |
| 20380 return p->szCache; |
| 20381 }else{ |
| 20382 /* IMPLEMENTATION-OF: R-61436-13639 If the argument N is negative, then |
| 20383 ** the number of cache pages is adjusted to use approximately abs(N*1024) |
| 20384 ** bytes of memory. */ |
| 20385 return (int)((-1024*(i64)p->szCache)/(p->szPage+p->szExtra)); |
| 20386 } |
| 20387 } |
| 20388 |
| 20389 /*************************************************** General Interfaces ****** |
| 20390 ** |
| 20391 ** Initialize and shutdown the page cache subsystem. Neither of these |
| 20392 ** functions are threadsafe. |
| 20393 */ |
| 20394 SQLITE_PRIVATE int sqlite3PcacheInitialize(void){ |
| 20395 if( sqlite3GlobalConfig.pcache2.xInit==0 ){ |
| 20396 /* IMPLEMENTATION-OF: R-26801-64137 If the xInit() method is NULL, then the |
| 20397 ** built-in default page cache is used instead of the application defined |
| 20398 ** page cache. */ |
| 20399 sqlite3PCacheSetDefault(); |
| 20400 } |
| 20401 return sqlite3GlobalConfig.pcache2.xInit(sqlite3GlobalConfig.pcache2.pArg); |
| 20402 } |
| 20403 SQLITE_PRIVATE void sqlite3PcacheShutdown(void){ |
| 20404 if( sqlite3GlobalConfig.pcache2.xShutdown ){ |
| 20405 /* IMPLEMENTATION-OF: R-26000-56589 The xShutdown() method may be NULL. */ |
| 20406 sqlite3GlobalConfig.pcache2.xShutdown(sqlite3GlobalConfig.pcache2.pArg); |
| 20407 } |
| 20408 } |
| 20409 |
| 20410 /* |
| 20411 ** Return the size in bytes of a PCache object. |
| 20412 */ |
| 20413 SQLITE_PRIVATE int sqlite3PcacheSize(void){ return sizeof(PCache); } |
| 20414 |
| 20415 /* |
| 20416 ** Create a new PCache object. Storage space to hold the object |
| 20417 ** has already been allocated and is passed in as the p pointer. |
| 20418 ** The caller discovers how much space needs to be allocated by |
| 20419 ** calling sqlite3PcacheSize(). |
| 20420 */ |
| 20421 SQLITE_PRIVATE int sqlite3PcacheOpen( |
| 20422 int szPage, /* Size of every page */ |
| 20423 int szExtra, /* Extra space associated with each page */ |
| 20424 int bPurgeable, /* True if pages are on backing store */ |
| 20425 int (*xStress)(void*,PgHdr*),/* Call to try to make pages clean */ |
| 20426 void *pStress, /* Argument to xStress */ |
| 20427 PCache *p /* Preallocated space for the PCache */ |
| 20428 ){ |
| 20429 memset(p, 0, sizeof(PCache)); |
| 20430 p->szPage = 1; |
| 20431 p->szExtra = szExtra; |
| 20432 p->bPurgeable = bPurgeable; |
| 20433 p->eCreate = 2; |
| 20434 p->xStress = xStress; |
| 20435 p->pStress = pStress; |
| 20436 p->szCache = 100; |
| 20437 p->szSpill = 1; |
| 20438 return sqlite3PcacheSetPageSize(p, szPage); |
| 20439 } |
| 20440 |
| 20441 /* |
| 20442 ** Change the page size for PCache object. The caller must ensure that there |
| 20443 ** are no outstanding page references when this function is called. |
| 20444 */ |
| 20445 SQLITE_PRIVATE int sqlite3PcacheSetPageSize(PCache *pCache, int szPage){ |
| 20446 assert( pCache->nRefSum==0 && pCache->pDirty==0 ); |
| 20447 if( pCache->szPage ){ |
| 20448 sqlite3_pcache *pNew; |
| 20449 pNew = sqlite3GlobalConfig.pcache2.xCreate( |
| 20450 szPage, pCache->szExtra + ROUND8(sizeof(PgHdr)), |
| 20451 pCache->bPurgeable |
| 20452 ); |
| 20453 if( pNew==0 ) return SQLITE_NOMEM; |
| 20454 sqlite3GlobalConfig.pcache2.xCachesize(pNew, numberOfCachePages(pCache)); |
| 20455 if( pCache->pCache ){ |
| 20456 sqlite3GlobalConfig.pcache2.xDestroy(pCache->pCache); |
| 20457 } |
| 20458 pCache->pCache = pNew; |
| 20459 pCache->szPage = szPage; |
| 20460 } |
| 20461 return SQLITE_OK; |
| 20462 } |
| 20463 |
| 20464 /* |
| 20465 ** Try to obtain a page from the cache. |
| 20466 ** |
| 20467 ** This routine returns a pointer to an sqlite3_pcache_page object if |
| 20468 ** such an object is already in cache, or if a new one is created. |
| 20469 ** This routine returns a NULL pointer if the object was not in cache |
| 20470 ** and could not be created. |
| 20471 ** |
| 20472 ** The createFlags should be 0 to check for existing pages and should |
| 20473 ** be 3 (not 1, but 3) to try to create a new page. |
| 20474 ** |
| 20475 ** If the createFlag is 0, then NULL is always returned if the page |
| 20476 ** is not already in the cache. If createFlag is 1, then a new page |
| 20477 ** is created only if that can be done without spilling dirty pages |
| 20478 ** and without exceeding the cache size limit. |
| 20479 ** |
| 20480 ** The caller needs to invoke sqlite3PcacheFetchFinish() to properly |
| 20481 ** initialize the sqlite3_pcache_page object and convert it into a |
| 20482 ** PgHdr object. The sqlite3PcacheFetch() and sqlite3PcacheFetchFinish() |
| 20483 ** routines are split this way for performance reasons. When separated |
| 20484 ** they can both (usually) operate without having to push values to |
| 20485 ** the stack on entry and pop them back off on exit, which saves a |
| 20486 ** lot of pushing and popping. |
| 20487 */ |
| 20488 SQLITE_PRIVATE sqlite3_pcache_page *sqlite3PcacheFetch( |
| 20489 PCache *pCache, /* Obtain the page from this cache */ |
| 20490 Pgno pgno, /* Page number to obtain */ |
| 20491 int createFlag /* If true, create page if it does not exist already */ |
| 20492 ){ |
| 20493 int eCreate; |
| 20494 |
| 20495 assert( pCache!=0 ); |
| 20496 assert( pCache->pCache!=0 ); |
| 20497 assert( createFlag==3 || createFlag==0 ); |
| 20498 assert( pgno>0 ); |
| 20499 |
| 20500 /* eCreate defines what to do if the page does not exist. |
| 20501 ** 0 Do not allocate a new page. (createFlag==0) |
| 20502 ** 1 Allocate a new page if doing so is inexpensive. |
| 20503 ** (createFlag==1 AND bPurgeable AND pDirty) |
| 20504 ** 2 Allocate a new page even it doing so is difficult. |
| 20505 ** (createFlag==1 AND !(bPurgeable AND pDirty) |
| 20506 */ |
| 20507 eCreate = createFlag & pCache->eCreate; |
| 20508 assert( eCreate==0 || eCreate==1 || eCreate==2 ); |
| 20509 assert( createFlag==0 || pCache->eCreate==eCreate ); |
| 20510 assert( createFlag==0 || eCreate==1+(!pCache->bPurgeable||!pCache->pDirty) ); |
| 20511 return sqlite3GlobalConfig.pcache2.xFetch(pCache->pCache, pgno, eCreate); |
| 20512 } |
| 20513 |
| 20514 /* |
| 20515 ** If the sqlite3PcacheFetch() routine is unable to allocate a new |
| 20516 ** page because new clean pages are available for reuse and the cache |
| 20517 ** size limit has been reached, then this routine can be invoked to |
| 20518 ** try harder to allocate a page. This routine might invoke the stress |
| 20519 ** callback to spill dirty pages to the journal. It will then try to |
| 20520 ** allocate the new page and will only fail to allocate a new page on |
| 20521 ** an OOM error. |
| 20522 ** |
| 20523 ** This routine should be invoked only after sqlite3PcacheFetch() fails. |
| 20524 */ |
| 20525 SQLITE_PRIVATE int sqlite3PcacheFetchStress( |
| 20526 PCache *pCache, /* Obtain the page from this cache */ |
| 20527 Pgno pgno, /* Page number to obtain */ |
| 20528 sqlite3_pcache_page **ppPage /* Write result here */ |
| 20529 ){ |
| 20530 PgHdr *pPg; |
| 20531 if( pCache->eCreate==2 ) return 0; |
| 20532 |
| 20533 if( sqlite3PcachePagecount(pCache)>pCache->szSpill ){ |
| 20534 /* Find a dirty page to write-out and recycle. First try to find a |
| 20535 ** page that does not require a journal-sync (one with PGHDR_NEED_SYNC |
| 20536 ** cleared), but if that is not possible settle for any other |
| 20537 ** unreferenced dirty page. |
| 20538 */ |
| 20539 for(pPg=pCache->pSynced; |
| 20540 pPg && (pPg->nRef || (pPg->flags&PGHDR_NEED_SYNC)); |
| 20541 pPg=pPg->pDirtyPrev |
| 20542 ); |
| 20543 pCache->pSynced = pPg; |
| 20544 if( !pPg ){ |
| 20545 for(pPg=pCache->pDirtyTail; pPg && pPg->nRef; pPg=pPg->pDirtyPrev); |
| 20546 } |
| 20547 if( pPg ){ |
| 20548 int rc; |
| 20549 #ifdef SQLITE_LOG_CACHE_SPILL |
| 20550 sqlite3_log(SQLITE_FULL, |
| 20551 "spill page %d making room for %d - cache used: %d/%d", |
| 20552 pPg->pgno, pgno, |
| 20553 sqlite3GlobalConfig.pcache.xPagecount(pCache->pCache), |
| 20554 numberOfCachePages(pCache)); |
| 20555 #endif |
| 20556 rc = pCache->xStress(pCache->pStress, pPg); |
| 20557 if( rc!=SQLITE_OK && rc!=SQLITE_BUSY ){ |
| 20558 return rc; |
| 20559 } |
| 20560 } |
| 20561 } |
| 20562 *ppPage = sqlite3GlobalConfig.pcache2.xFetch(pCache->pCache, pgno, 2); |
| 20563 return *ppPage==0 ? SQLITE_NOMEM : SQLITE_OK; |
| 20564 } |
| 20565 |
| 20566 /* |
| 20567 ** This is a helper routine for sqlite3PcacheFetchFinish() |
| 20568 ** |
| 20569 ** In the uncommon case where the page being fetched has not been |
| 20570 ** initialized, this routine is invoked to do the initialization. |
| 20571 ** This routine is broken out into a separate function since it |
| 20572 ** requires extra stack manipulation that can be avoided in the common |
| 20573 ** case. |
| 20574 */ |
| 20575 static SQLITE_NOINLINE PgHdr *pcacheFetchFinishWithInit( |
| 20576 PCache *pCache, /* Obtain the page from this cache */ |
| 20577 Pgno pgno, /* Page number obtained */ |
| 20578 sqlite3_pcache_page *pPage /* Page obtained by prior PcacheFetch() call */ |
| 20579 ){ |
| 20580 PgHdr *pPgHdr; |
| 20581 assert( pPage!=0 ); |
| 20582 pPgHdr = (PgHdr*)pPage->pExtra; |
| 20583 assert( pPgHdr->pPage==0 ); |
| 20584 memset(pPgHdr, 0, sizeof(PgHdr)); |
| 20585 pPgHdr->pPage = pPage; |
| 20586 pPgHdr->pData = pPage->pBuf; |
| 20587 pPgHdr->pExtra = (void *)&pPgHdr[1]; |
| 20588 memset(pPgHdr->pExtra, 0, pCache->szExtra); |
| 20589 pPgHdr->pCache = pCache; |
| 20590 pPgHdr->pgno = pgno; |
| 20591 pPgHdr->flags = PGHDR_CLEAN; |
| 20592 return sqlite3PcacheFetchFinish(pCache,pgno,pPage); |
| 20593 } |
| 20594 |
| 20595 /* |
| 20596 ** This routine converts the sqlite3_pcache_page object returned by |
| 20597 ** sqlite3PcacheFetch() into an initialized PgHdr object. This routine |
| 20598 ** must be called after sqlite3PcacheFetch() in order to get a usable |
| 20599 ** result. |
| 20600 */ |
| 20601 SQLITE_PRIVATE PgHdr *sqlite3PcacheFetchFinish( |
| 20602 PCache *pCache, /* Obtain the page from this cache */ |
| 20603 Pgno pgno, /* Page number obtained */ |
| 20604 sqlite3_pcache_page *pPage /* Page obtained by prior PcacheFetch() call */ |
| 20605 ){ |
| 20606 PgHdr *pPgHdr; |
| 20607 |
| 20608 assert( pPage!=0 ); |
| 20609 pPgHdr = (PgHdr *)pPage->pExtra; |
| 20610 |
| 20611 if( !pPgHdr->pPage ){ |
| 20612 return pcacheFetchFinishWithInit(pCache, pgno, pPage); |
| 20613 } |
| 20614 pCache->nRefSum++; |
| 20615 pPgHdr->nRef++; |
| 20616 return pPgHdr; |
| 20617 } |
| 20618 |
| 20619 /* |
| 20620 ** Decrement the reference count on a page. If the page is clean and the |
| 20621 ** reference count drops to 0, then it is made eligible for recycling. |
| 20622 */ |
| 20623 SQLITE_PRIVATE void SQLITE_NOINLINE sqlite3PcacheRelease(PgHdr *p){ |
| 20624 assert( p->nRef>0 ); |
| 20625 p->pCache->nRefSum--; |
| 20626 if( (--p->nRef)==0 ){ |
| 20627 if( p->flags&PGHDR_CLEAN ){ |
| 20628 pcacheUnpin(p); |
| 20629 }else if( p->pDirtyPrev!=0 ){ |
| 20630 /* Move the page to the head of the dirty list. */ |
| 20631 pcacheManageDirtyList(p, PCACHE_DIRTYLIST_FRONT); |
| 20632 } |
| 20633 } |
| 20634 } |
| 20635 |
| 20636 /* |
| 20637 ** Increase the reference count of a supplied page by 1. |
| 20638 */ |
| 20639 SQLITE_PRIVATE void sqlite3PcacheRef(PgHdr *p){ |
| 20640 assert(p->nRef>0); |
| 20641 p->nRef++; |
| 20642 p->pCache->nRefSum++; |
| 20643 } |
| 20644 |
| 20645 /* |
| 20646 ** Drop a page from the cache. There must be exactly one reference to the |
| 20647 ** page. This function deletes that reference, so after it returns the |
| 20648 ** page pointed to by p is invalid. |
| 20649 */ |
| 20650 SQLITE_PRIVATE void sqlite3PcacheDrop(PgHdr *p){ |
| 20651 assert( p->nRef==1 ); |
| 20652 if( p->flags&PGHDR_DIRTY ){ |
| 20653 pcacheManageDirtyList(p, PCACHE_DIRTYLIST_REMOVE); |
| 20654 } |
| 20655 p->pCache->nRefSum--; |
| 20656 sqlite3GlobalConfig.pcache2.xUnpin(p->pCache->pCache, p->pPage, 1); |
| 20657 } |
| 20658 |
| 20659 /* |
| 20660 ** Make sure the page is marked as dirty. If it isn't dirty already, |
| 20661 ** make it so. |
| 20662 */ |
| 20663 SQLITE_PRIVATE void sqlite3PcacheMakeDirty(PgHdr *p){ |
| 20664 assert( p->nRef>0 ); |
| 20665 if( p->flags & (PGHDR_CLEAN|PGHDR_DONT_WRITE) ){ |
| 20666 p->flags &= ~PGHDR_DONT_WRITE; |
| 20667 if( p->flags & PGHDR_CLEAN ){ |
| 20668 p->flags ^= (PGHDR_DIRTY|PGHDR_CLEAN); |
| 20669 assert( (p->flags & (PGHDR_DIRTY|PGHDR_CLEAN))==PGHDR_DIRTY ); |
| 20670 pcacheManageDirtyList(p, PCACHE_DIRTYLIST_ADD); |
| 20671 } |
| 20672 } |
| 20673 } |
| 20674 |
| 20675 /* |
| 20676 ** Make sure the page is marked as clean. If it isn't clean already, |
| 20677 ** make it so. |
| 20678 */ |
| 20679 SQLITE_PRIVATE void sqlite3PcacheMakeClean(PgHdr *p){ |
| 20680 if( (p->flags & PGHDR_DIRTY) ){ |
| 20681 assert( (p->flags & PGHDR_CLEAN)==0 ); |
| 20682 pcacheManageDirtyList(p, PCACHE_DIRTYLIST_REMOVE); |
| 20683 p->flags &= ~(PGHDR_DIRTY|PGHDR_NEED_SYNC|PGHDR_WRITEABLE); |
| 20684 p->flags |= PGHDR_CLEAN; |
| 20685 if( p->nRef==0 ){ |
| 20686 pcacheUnpin(p); |
| 20687 } |
| 20688 } |
| 20689 } |
| 20690 |
| 20691 /* |
| 20692 ** Make every page in the cache clean. |
| 20693 */ |
| 20694 SQLITE_PRIVATE void sqlite3PcacheCleanAll(PCache *pCache){ |
| 20695 PgHdr *p; |
| 20696 while( (p = pCache->pDirty)!=0 ){ |
| 20697 sqlite3PcacheMakeClean(p); |
| 20698 } |
| 20699 } |
| 20700 |
| 20701 /* |
| 20702 ** Clear the PGHDR_NEED_SYNC flag from all dirty pages. |
| 20703 */ |
| 20704 SQLITE_PRIVATE void sqlite3PcacheClearSyncFlags(PCache *pCache){ |
| 20705 PgHdr *p; |
| 20706 for(p=pCache->pDirty; p; p=p->pDirtyNext){ |
| 20707 p->flags &= ~PGHDR_NEED_SYNC; |
| 20708 } |
| 20709 pCache->pSynced = pCache->pDirtyTail; |
| 20710 } |
| 20711 |
| 20712 /* |
| 20713 ** Change the page number of page p to newPgno. |
| 20714 */ |
| 20715 SQLITE_PRIVATE void sqlite3PcacheMove(PgHdr *p, Pgno newPgno){ |
| 20716 PCache *pCache = p->pCache; |
| 20717 assert( p->nRef>0 ); |
| 20718 assert( newPgno>0 ); |
| 20719 sqlite3GlobalConfig.pcache2.xRekey(pCache->pCache, p->pPage, p->pgno,newPgno); |
| 20720 p->pgno = newPgno; |
| 20721 if( (p->flags&PGHDR_DIRTY) && (p->flags&PGHDR_NEED_SYNC) ){ |
| 20722 pcacheManageDirtyList(p, PCACHE_DIRTYLIST_FRONT); |
| 20723 } |
| 20724 } |
| 20725 |
| 20726 /* |
| 20727 ** Drop every cache entry whose page number is greater than "pgno". The |
| 20728 ** caller must ensure that there are no outstanding references to any pages |
| 20729 ** other than page 1 with a page number greater than pgno. |
| 20730 ** |
| 20731 ** If there is a reference to page 1 and the pgno parameter passed to this |
| 20732 ** function is 0, then the data area associated with page 1 is zeroed, but |
| 20733 ** the page object is not dropped. |
| 20734 */ |
| 20735 SQLITE_PRIVATE void sqlite3PcacheTruncate(PCache *pCache, Pgno pgno){ |
| 20736 if( pCache->pCache ){ |
| 20737 PgHdr *p; |
| 20738 PgHdr *pNext; |
| 20739 for(p=pCache->pDirty; p; p=pNext){ |
| 20740 pNext = p->pDirtyNext; |
| 20741 /* This routine never gets call with a positive pgno except right |
| 20742 ** after sqlite3PcacheCleanAll(). So if there are dirty pages, |
| 20743 ** it must be that pgno==0. |
| 20744 */ |
| 20745 assert( p->pgno>0 ); |
| 20746 if( ALWAYS(p->pgno>pgno) ){ |
| 20747 assert( p->flags&PGHDR_DIRTY ); |
| 20748 sqlite3PcacheMakeClean(p); |
| 20749 } |
| 20750 } |
| 20751 if( pgno==0 && pCache->nRefSum ){ |
| 20752 sqlite3_pcache_page *pPage1; |
| 20753 pPage1 = sqlite3GlobalConfig.pcache2.xFetch(pCache->pCache,1,0); |
| 20754 if( ALWAYS(pPage1) ){ /* Page 1 is always available in cache, because |
| 20755 ** pCache->nRefSum>0 */ |
| 20756 memset(pPage1->pBuf, 0, pCache->szPage); |
| 20757 pgno = 1; |
| 20758 } |
| 20759 } |
| 20760 sqlite3GlobalConfig.pcache2.xTruncate(pCache->pCache, pgno+1); |
| 20761 } |
| 20762 } |
| 20763 |
| 20764 /* |
| 20765 ** Close a cache. |
| 20766 */ |
| 20767 SQLITE_PRIVATE void sqlite3PcacheClose(PCache *pCache){ |
| 20768 assert( pCache->pCache!=0 ); |
| 20769 sqlite3GlobalConfig.pcache2.xDestroy(pCache->pCache); |
| 20770 } |
| 20771 |
| 20772 /* |
| 20773 ** Discard the contents of the cache. |
| 20774 */ |
| 20775 SQLITE_PRIVATE void sqlite3PcacheClear(PCache *pCache){ |
| 20776 sqlite3PcacheTruncate(pCache, 0); |
| 20777 } |
| 20778 |
| 20779 /* |
| 20780 ** Merge two lists of pages connected by pDirty and in pgno order. |
| 20781 ** Do not both fixing the pDirtyPrev pointers. |
| 20782 */ |
| 20783 static PgHdr *pcacheMergeDirtyList(PgHdr *pA, PgHdr *pB){ |
| 20784 PgHdr result, *pTail; |
| 20785 pTail = &result; |
| 20786 while( pA && pB ){ |
| 20787 if( pA->pgno<pB->pgno ){ |
| 20788 pTail->pDirty = pA; |
| 20789 pTail = pA; |
| 20790 pA = pA->pDirty; |
| 20791 }else{ |
| 20792 pTail->pDirty = pB; |
| 20793 pTail = pB; |
| 20794 pB = pB->pDirty; |
| 20795 } |
| 20796 } |
| 20797 if( pA ){ |
| 20798 pTail->pDirty = pA; |
| 20799 }else if( pB ){ |
| 20800 pTail->pDirty = pB; |
| 20801 }else{ |
| 20802 pTail->pDirty = 0; |
| 20803 } |
| 20804 return result.pDirty; |
| 20805 } |
| 20806 |
| 20807 /* |
| 20808 ** Sort the list of pages in accending order by pgno. Pages are |
| 20809 ** connected by pDirty pointers. The pDirtyPrev pointers are |
| 20810 ** corrupted by this sort. |
| 20811 ** |
| 20812 ** Since there cannot be more than 2^31 distinct pages in a database, |
| 20813 ** there cannot be more than 31 buckets required by the merge sorter. |
| 20814 ** One extra bucket is added to catch overflow in case something |
| 20815 ** ever changes to make the previous sentence incorrect. |
| 20816 */ |
| 20817 #define N_SORT_BUCKET 32 |
| 20818 static PgHdr *pcacheSortDirtyList(PgHdr *pIn){ |
| 20819 PgHdr *a[N_SORT_BUCKET], *p; |
| 20820 int i; |
| 20821 memset(a, 0, sizeof(a)); |
| 20822 while( pIn ){ |
| 20823 p = pIn; |
| 20824 pIn = p->pDirty; |
| 20825 p->pDirty = 0; |
| 20826 for(i=0; ALWAYS(i<N_SORT_BUCKET-1); i++){ |
| 20827 if( a[i]==0 ){ |
| 20828 a[i] = p; |
| 20829 break; |
| 20830 }else{ |
| 20831 p = pcacheMergeDirtyList(a[i], p); |
| 20832 a[i] = 0; |
| 20833 } |
| 20834 } |
| 20835 if( NEVER(i==N_SORT_BUCKET-1) ){ |
| 20836 /* To get here, there need to be 2^(N_SORT_BUCKET) elements in |
| 20837 ** the input list. But that is impossible. |
| 20838 */ |
| 20839 a[i] = pcacheMergeDirtyList(a[i], p); |
| 20840 } |
| 20841 } |
| 20842 p = a[0]; |
| 20843 for(i=1; i<N_SORT_BUCKET; i++){ |
| 20844 p = pcacheMergeDirtyList(p, a[i]); |
| 20845 } |
| 20846 return p; |
| 20847 } |
| 20848 |
| 20849 /* |
| 20850 ** Return a list of all dirty pages in the cache, sorted by page number. |
| 20851 */ |
| 20852 SQLITE_PRIVATE PgHdr *sqlite3PcacheDirtyList(PCache *pCache){ |
| 20853 PgHdr *p; |
| 20854 for(p=pCache->pDirty; p; p=p->pDirtyNext){ |
| 20855 p->pDirty = p->pDirtyNext; |
| 20856 } |
| 20857 return pcacheSortDirtyList(pCache->pDirty); |
| 20858 } |
| 20859 |
| 20860 /* |
| 20861 ** Return the total number of references to all pages held by the cache. |
| 20862 ** |
| 20863 ** This is not the total number of pages referenced, but the sum of the |
| 20864 ** reference count for all pages. |
| 20865 */ |
| 20866 SQLITE_PRIVATE int sqlite3PcacheRefCount(PCache *pCache){ |
| 20867 return pCache->nRefSum; |
| 20868 } |
| 20869 |
| 20870 /* |
| 20871 ** Return the number of references to the page supplied as an argument. |
| 20872 */ |
| 20873 SQLITE_PRIVATE int sqlite3PcachePageRefcount(PgHdr *p){ |
| 20874 return p->nRef; |
| 20875 } |
| 20876 |
| 20877 /* |
| 20878 ** Return the total number of pages in the cache. |
| 20879 */ |
| 20880 SQLITE_PRIVATE int sqlite3PcachePagecount(PCache *pCache){ |
| 20881 assert( pCache->pCache!=0 ); |
| 20882 return sqlite3GlobalConfig.pcache2.xPagecount(pCache->pCache); |
| 20883 } |
| 20884 |
| 20885 #ifdef SQLITE_TEST |
| 20886 /* |
| 20887 ** Get the suggested cache-size value. |
| 20888 */ |
| 20889 SQLITE_PRIVATE int sqlite3PcacheGetCachesize(PCache *pCache){ |
| 20890 return numberOfCachePages(pCache); |
| 20891 } |
| 20892 #endif |
| 20893 |
| 20894 /* |
| 20895 ** Set the suggested cache-size value. |
| 20896 */ |
| 20897 SQLITE_PRIVATE void sqlite3PcacheSetCachesize(PCache *pCache, int mxPage){ |
| 20898 assert( pCache->pCache!=0 ); |
| 20899 pCache->szCache = mxPage; |
| 20900 sqlite3GlobalConfig.pcache2.xCachesize(pCache->pCache, |
| 20901 numberOfCachePages(pCache)); |
| 20902 } |
| 20903 |
| 20904 /* |
| 20905 ** Set the suggested cache-spill value. Make no changes if if the |
| 20906 ** argument is zero. Return the effective cache-spill size, which will |
| 20907 ** be the larger of the szSpill and szCache. |
| 20908 */ |
| 20909 SQLITE_PRIVATE int sqlite3PcacheSetSpillsize(PCache *p, int mxPage){ |
| 20910 int res; |
| 20911 assert( p->pCache!=0 ); |
| 20912 if( mxPage ){ |
| 20913 if( mxPage<0 ){ |
| 20914 mxPage = (int)((-1024*(i64)mxPage)/(p->szPage+p->szExtra)); |
| 20915 } |
| 20916 p->szSpill = mxPage; |
| 20917 } |
| 20918 res = numberOfCachePages(p); |
| 20919 if( res<p->szSpill ) res = p->szSpill; |
| 20920 return res; |
| 20921 } |
| 20922 |
| 20923 /* |
| 20924 ** Free up as much memory as possible from the page cache. |
| 20925 */ |
| 20926 SQLITE_PRIVATE void sqlite3PcacheShrink(PCache *pCache){ |
| 20927 assert( pCache->pCache!=0 ); |
| 20928 sqlite3GlobalConfig.pcache2.xShrink(pCache->pCache); |
| 20929 } |
| 20930 |
| 20931 /* |
| 20932 ** Return the size of the header added by this middleware layer |
| 20933 ** in the page-cache hierarchy. |
| 20934 */ |
| 20935 SQLITE_PRIVATE int sqlite3HeaderSizePcache(void){ return ROUND8(sizeof(PgHdr));
} |
| 20936 |
| 20937 |
| 20938 #if defined(SQLITE_CHECK_PAGES) || defined(SQLITE_DEBUG) |
| 20939 /* |
| 20940 ** For all dirty pages currently in the cache, invoke the specified |
| 20941 ** callback. This is only used if the SQLITE_CHECK_PAGES macro is |
| 20942 ** defined. |
| 20943 */ |
| 20944 SQLITE_PRIVATE void sqlite3PcacheIterateDirty(PCache *pCache, void (*xIter)(PgHd
r *)){ |
| 20945 PgHdr *pDirty; |
| 20946 for(pDirty=pCache->pDirty; pDirty; pDirty=pDirty->pDirtyNext){ |
| 20947 xIter(pDirty); |
| 20948 } |
| 20949 } |
| 20950 #endif |
| 20951 |
| 20952 /************** End of pcache.c **********************************************/ |
| 20953 /************** Begin file pcache1.c *****************************************/ |
| 20954 /* |
| 20955 ** 2008 November 05 |
| 20956 ** |
| 20957 ** The author disclaims copyright to this source code. In place of |
| 20958 ** a legal notice, here is a blessing: |
| 20959 ** |
| 20960 ** May you do good and not evil. |
| 20961 ** May you find forgiveness for yourself and forgive others. |
| 20962 ** May you share freely, never taking more than you give. |
| 20963 ** |
| 20964 ************************************************************************* |
| 20965 ** |
| 20966 ** This file implements the default page cache implementation (the |
| 20967 ** sqlite3_pcache interface). It also contains part of the implementation |
| 20968 ** of the SQLITE_CONFIG_PAGECACHE and sqlite3_release_memory() features. |
| 20969 ** If the default page cache implementation is overridden, then neither of |
| 20970 ** these two features are available. |
| 20971 ** |
| 20972 ** A Page cache line looks like this: |
| 20973 ** |
| 20974 ** ------------------------------------------------------------- |
| 20975 ** | database page content | PgHdr1 | MemPage | PgHdr | |
| 20976 ** ------------------------------------------------------------- |
| 20977 ** |
| 20978 ** The database page content is up front (so that buffer overreads tend to |
| 20979 ** flow harmlessly into the PgHdr1, MemPage, and PgHdr extensions). MemPage |
| 20980 ** is the extension added by the btree.c module containing information such |
| 20981 ** as the database page number and how that database page is used. PgHdr |
| 20982 ** is added by the pcache.c layer and contains information used to keep track |
| 20983 ** of which pages are "dirty". PgHdr1 is an extension added by this |
| 20984 ** module (pcache1.c). The PgHdr1 header is a subclass of sqlite3_pcache_page. |
| 20985 ** PgHdr1 contains information needed to look up a page by its page number. |
| 20986 ** The superclass sqlite3_pcache_page.pBuf points to the start of the |
| 20987 ** database page content and sqlite3_pcache_page.pExtra points to PgHdr. |
| 20988 ** |
| 20989 ** The size of the extension (MemPage+PgHdr+PgHdr1) can be determined at |
| 20990 ** runtime using sqlite3_config(SQLITE_CONFIG_PCACHE_HDRSZ, &size). The |
| 20991 ** sizes of the extensions sum to 272 bytes on x64 for 3.8.10, but this |
| 20992 ** size can vary according to architecture, compile-time options, and |
| 20993 ** SQLite library version number. |
| 20994 ** |
| 20995 ** If SQLITE_PCACHE_SEPARATE_HEADER is defined, then the extension is obtained |
| 20996 ** using a separate memory allocation from the database page content. This |
| 20997 ** seeks to overcome the "clownshoe" problem (also called "internal |
| 20998 ** fragmentation" in academic literature) of allocating a few bytes more |
| 20999 ** than a power of two with the memory allocator rounding up to the next |
| 21000 ** power of two, and leaving the rounded-up space unused. |
| 21001 ** |
| 21002 ** This module tracks pointers to PgHdr1 objects. Only pcache.c communicates |
| 21003 ** with this module. Information is passed back and forth as PgHdr1 pointers. |
| 21004 ** |
| 21005 ** The pcache.c and pager.c modules deal pointers to PgHdr objects. |
| 21006 ** The btree.c module deals with pointers to MemPage objects. |
| 21007 ** |
| 21008 ** SOURCE OF PAGE CACHE MEMORY: |
| 21009 ** |
| 21010 ** Memory for a page might come from any of three sources: |
| 21011 ** |
| 21012 ** (1) The general-purpose memory allocator - sqlite3Malloc() |
| 21013 ** (2) Global page-cache memory provided using sqlite3_config() with |
| 21014 ** SQLITE_CONFIG_PAGECACHE. |
| 21015 ** (3) PCache-local bulk allocation. |
| 21016 ** |
| 21017 ** The third case is a chunk of heap memory (defaulting to 100 pages worth) |
| 21018 ** that is allocated when the page cache is created. The size of the local |
| 21019 ** bulk allocation can be adjusted using |
| 21020 ** |
| 21021 ** sqlite3_config(SQLITE_CONFIG_PAGECACHE, (void*)0, 0, N). |
| 21022 ** |
| 21023 ** If N is positive, then N pages worth of memory are allocated using a single |
| 21024 ** sqlite3Malloc() call and that memory is used for the first N pages allocated. |
| 21025 ** Or if N is negative, then -1024*N bytes of memory are allocated and used |
| 21026 ** for as many pages as can be accomodated. |
| 21027 ** |
| 21028 ** Only one of (2) or (3) can be used. Once the memory available to (2) or |
| 21029 ** (3) is exhausted, subsequent allocations fail over to the general-purpose |
| 21030 ** memory allocator (1). |
| 21031 ** |
| 21032 ** Earlier versions of SQLite used only methods (1) and (2). But experiments |
| 21033 ** show that method (3) with N==100 provides about a 5% performance boost for |
| 21034 ** common workloads. |
| 21035 */ |
| 21036 /* #include "sqliteInt.h" */ |
| 21037 |
| 21038 typedef struct PCache1 PCache1; |
| 21039 typedef struct PgHdr1 PgHdr1; |
| 21040 typedef struct PgFreeslot PgFreeslot; |
| 21041 typedef struct PGroup PGroup; |
| 21042 |
| 21043 /* |
| 21044 ** Each cache entry is represented by an instance of the following |
| 21045 ** structure. Unless SQLITE_PCACHE_SEPARATE_HEADER is defined, a buffer of |
| 21046 ** PgHdr1.pCache->szPage bytes is allocated directly before this structure |
| 21047 ** in memory. |
| 21048 */ |
| 21049 struct PgHdr1 { |
| 21050 sqlite3_pcache_page page; /* Base class. Must be first. pBuf & pExtra */ |
| 21051 unsigned int iKey; /* Key value (page number) */ |
| 21052 u8 isPinned; /* Page in use, not on the LRU list */ |
| 21053 u8 isBulkLocal; /* This page from bulk local storage */ |
| 21054 u8 isAnchor; /* This is the PGroup.lru element */ |
| 21055 PgHdr1 *pNext; /* Next in hash table chain */ |
| 21056 PCache1 *pCache; /* Cache that currently owns this page */ |
| 21057 PgHdr1 *pLruNext; /* Next in LRU list of unpinned pages */ |
| 21058 PgHdr1 *pLruPrev; /* Previous in LRU list of unpinned pages */ |
| 21059 }; |
| 21060 |
| 21061 /* Each page cache (or PCache) belongs to a PGroup. A PGroup is a set |
| 21062 ** of one or more PCaches that are able to recycle each other's unpinned |
| 21063 ** pages when they are under memory pressure. A PGroup is an instance of |
| 21064 ** the following object. |
| 21065 ** |
| 21066 ** This page cache implementation works in one of two modes: |
| 21067 ** |
| 21068 ** (1) Every PCache is the sole member of its own PGroup. There is |
| 21069 ** one PGroup per PCache. |
| 21070 ** |
| 21071 ** (2) There is a single global PGroup that all PCaches are a member |
| 21072 ** of. |
| 21073 ** |
| 21074 ** Mode 1 uses more memory (since PCache instances are not able to rob |
| 21075 ** unused pages from other PCaches) but it also operates without a mutex, |
| 21076 ** and is therefore often faster. Mode 2 requires a mutex in order to be |
| 21077 ** threadsafe, but recycles pages more efficiently. |
| 21078 ** |
| 21079 ** For mode (1), PGroup.mutex is NULL. For mode (2) there is only a single |
| 21080 ** PGroup which is the pcache1.grp global variable and its mutex is |
| 21081 ** SQLITE_MUTEX_STATIC_LRU. |
| 21082 */ |
| 21083 struct PGroup { |
| 21084 sqlite3_mutex *mutex; /* MUTEX_STATIC_LRU or NULL */ |
| 21085 unsigned int nMaxPage; /* Sum of nMax for purgeable caches */ |
| 21086 unsigned int nMinPage; /* Sum of nMin for purgeable caches */ |
| 21087 unsigned int mxPinned; /* nMaxpage + 10 - nMinPage */ |
| 21088 unsigned int nCurrentPage; /* Number of purgeable pages allocated */ |
| 21089 PgHdr1 lru; /* The beginning and end of the LRU list */ |
| 21090 }; |
| 21091 |
| 21092 /* Each page cache is an instance of the following object. Every |
| 21093 ** open database file (including each in-memory database and each |
| 21094 ** temporary or transient database) has a single page cache which |
| 21095 ** is an instance of this object. |
| 21096 ** |
| 21097 ** Pointers to structures of this type are cast and returned as |
| 21098 ** opaque sqlite3_pcache* handles. |
| 21099 */ |
| 21100 struct PCache1 { |
| 21101 /* Cache configuration parameters. Page size (szPage) and the purgeable |
| 21102 ** flag (bPurgeable) are set when the cache is created. nMax may be |
| 21103 ** modified at any time by a call to the pcache1Cachesize() method. |
| 21104 ** The PGroup mutex must be held when accessing nMax. |
| 21105 */ |
| 21106 PGroup *pGroup; /* PGroup this cache belongs to */ |
| 21107 int szPage; /* Size of database content section */ |
| 21108 int szExtra; /* sizeof(MemPage)+sizeof(PgHdr) */ |
| 21109 int szAlloc; /* Total size of one pcache line */ |
| 21110 int bPurgeable; /* True if cache is purgeable */ |
| 21111 unsigned int nMin; /* Minimum number of pages reserved */ |
| 21112 unsigned int nMax; /* Configured "cache_size" value */ |
| 21113 unsigned int n90pct; /* nMax*9/10 */ |
| 21114 unsigned int iMaxKey; /* Largest key seen since xTruncate() */ |
| 21115 |
| 21116 /* Hash table of all pages. The following variables may only be accessed |
| 21117 ** when the accessor is holding the PGroup mutex. |
| 21118 */ |
| 21119 unsigned int nRecyclable; /* Number of pages in the LRU list */ |
| 21120 unsigned int nPage; /* Total number of pages in apHash */ |
| 21121 unsigned int nHash; /* Number of slots in apHash[] */ |
| 21122 PgHdr1 **apHash; /* Hash table for fast lookup by key */ |
| 21123 PgHdr1 *pFree; /* List of unused pcache-local pages */ |
| 21124 void *pBulk; /* Bulk memory used by pcache-local */ |
| 21125 }; |
| 21126 |
| 21127 /* |
| 21128 ** Free slots in the allocator used to divide up the global page cache |
| 21129 ** buffer provided using the SQLITE_CONFIG_PAGECACHE mechanism. |
| 21130 */ |
| 21131 struct PgFreeslot { |
| 21132 PgFreeslot *pNext; /* Next free slot */ |
| 21133 }; |
| 21134 |
| 21135 /* |
| 21136 ** Global data used by this cache. |
| 21137 */ |
| 21138 static SQLITE_WSD struct PCacheGlobal { |
| 21139 PGroup grp; /* The global PGroup for mode (2) */ |
| 21140 |
| 21141 /* Variables related to SQLITE_CONFIG_PAGECACHE settings. The |
| 21142 ** szSlot, nSlot, pStart, pEnd, nReserve, and isInit values are all |
| 21143 ** fixed at sqlite3_initialize() time and do not require mutex protection. |
| 21144 ** The nFreeSlot and pFree values do require mutex protection. |
| 21145 */ |
| 21146 int isInit; /* True if initialized */ |
| 21147 int separateCache; /* Use a new PGroup for each PCache */ |
| 21148 int nInitPage; /* Initial bulk allocation size */ |
| 21149 int szSlot; /* Size of each free slot */ |
| 21150 int nSlot; /* The number of pcache slots */ |
| 21151 int nReserve; /* Try to keep nFreeSlot above this */ |
| 21152 void *pStart, *pEnd; /* Bounds of global page cache memory */ |
| 21153 /* Above requires no mutex. Use mutex below for variable that follow. */ |
| 21154 sqlite3_mutex *mutex; /* Mutex for accessing the following: */ |
| 21155 PgFreeslot *pFree; /* Free page blocks */ |
| 21156 int nFreeSlot; /* Number of unused pcache slots */ |
| 21157 /* The following value requires a mutex to change. We skip the mutex on |
| 21158 ** reading because (1) most platforms read a 32-bit integer atomically and |
| 21159 ** (2) even if an incorrect value is read, no great harm is done since this |
| 21160 ** is really just an optimization. */ |
| 21161 int bUnderPressure; /* True if low on PAGECACHE memory */ |
| 21162 } pcache1_g; |
| 21163 |
| 21164 /* |
| 21165 ** All code in this file should access the global structure above via the |
| 21166 ** alias "pcache1". This ensures that the WSD emulation is used when |
| 21167 ** compiling for systems that do not support real WSD. |
| 21168 */ |
| 21169 #define pcache1 (GLOBAL(struct PCacheGlobal, pcache1_g)) |
| 21170 |
| 21171 /* |
| 21172 ** Macros to enter and leave the PCache LRU mutex. |
| 21173 */ |
| 21174 #if !defined(SQLITE_ENABLE_MEMORY_MANAGEMENT) || SQLITE_THREADSAFE==0 |
| 21175 # define pcache1EnterMutex(X) assert((X)->mutex==0) |
| 21176 # define pcache1LeaveMutex(X) assert((X)->mutex==0) |
| 21177 # define PCACHE1_MIGHT_USE_GROUP_MUTEX 0 |
| 21178 #else |
| 21179 # define pcache1EnterMutex(X) sqlite3_mutex_enter((X)->mutex) |
| 21180 # define pcache1LeaveMutex(X) sqlite3_mutex_leave((X)->mutex) |
| 21181 # define PCACHE1_MIGHT_USE_GROUP_MUTEX 1 |
| 21182 #endif |
| 21183 |
| 21184 /******************************************************************************/ |
| 21185 /******** Page Allocation/SQLITE_CONFIG_PCACHE Related Functions **************/ |
| 21186 |
| 21187 |
| 21188 /* |
| 21189 ** This function is called during initialization if a static buffer is |
| 21190 ** supplied to use for the page-cache by passing the SQLITE_CONFIG_PAGECACHE |
| 21191 ** verb to sqlite3_config(). Parameter pBuf points to an allocation large |
| 21192 ** enough to contain 'n' buffers of 'sz' bytes each. |
| 21193 ** |
| 21194 ** This routine is called from sqlite3_initialize() and so it is guaranteed |
| 21195 ** to be serialized already. There is no need for further mutexing. |
| 21196 */ |
| 21197 SQLITE_PRIVATE void sqlite3PCacheBufferSetup(void *pBuf, int sz, int n){ |
| 21198 if( pcache1.isInit ){ |
| 21199 PgFreeslot *p; |
| 21200 if( pBuf==0 ) sz = n = 0; |
| 21201 sz = ROUNDDOWN8(sz); |
| 21202 pcache1.szSlot = sz; |
| 21203 pcache1.nSlot = pcache1.nFreeSlot = n; |
| 21204 pcache1.nReserve = n>90 ? 10 : (n/10 + 1); |
| 21205 pcache1.pStart = pBuf; |
| 21206 pcache1.pFree = 0; |
| 21207 pcache1.bUnderPressure = 0; |
| 21208 while( n-- ){ |
| 21209 p = (PgFreeslot*)pBuf; |
| 21210 p->pNext = pcache1.pFree; |
| 21211 pcache1.pFree = p; |
| 21212 pBuf = (void*)&((char*)pBuf)[sz]; |
| 21213 } |
| 21214 pcache1.pEnd = pBuf; |
| 21215 } |
| 21216 } |
| 21217 |
| 21218 /* |
| 21219 ** Try to initialize the pCache->pFree and pCache->pBulk fields. Return |
| 21220 ** true if pCache->pFree ends up containing one or more free pages. |
| 21221 */ |
| 21222 static int pcache1InitBulk(PCache1 *pCache){ |
| 21223 i64 szBulk; |
| 21224 char *zBulk; |
| 21225 if( pcache1.nInitPage==0 ) return 0; |
| 21226 /* Do not bother with a bulk allocation if the cache size very small */ |
| 21227 if( pCache->nMax<3 ) return 0; |
| 21228 sqlite3BeginBenignMalloc(); |
| 21229 if( pcache1.nInitPage>0 ){ |
| 21230 szBulk = pCache->szAlloc * (i64)pcache1.nInitPage; |
| 21231 }else{ |
| 21232 szBulk = -1024 * (i64)pcache1.nInitPage; |
| 21233 } |
| 21234 if( szBulk > pCache->szAlloc*(i64)pCache->nMax ){ |
| 21235 szBulk = pCache->szAlloc*pCache->nMax; |
| 21236 } |
| 21237 zBulk = pCache->pBulk = sqlite3Malloc( szBulk ); |
| 21238 sqlite3EndBenignMalloc(); |
| 21239 if( zBulk ){ |
| 21240 int nBulk = sqlite3MallocSize(zBulk)/pCache->szAlloc; |
| 21241 int i; |
| 21242 for(i=0; i<nBulk; i++){ |
| 21243 PgHdr1 *pX = (PgHdr1*)&zBulk[pCache->szPage]; |
| 21244 pX->page.pBuf = zBulk; |
| 21245 pX->page.pExtra = &pX[1]; |
| 21246 pX->isBulkLocal = 1; |
| 21247 pX->isAnchor = 0; |
| 21248 pX->pNext = pCache->pFree; |
| 21249 pCache->pFree = pX; |
| 21250 zBulk += pCache->szAlloc; |
| 21251 } |
| 21252 } |
| 21253 return pCache->pFree!=0; |
| 21254 } |
| 21255 |
| 21256 /* |
| 21257 ** Malloc function used within this file to allocate space from the buffer |
| 21258 ** configured using sqlite3_config(SQLITE_CONFIG_PAGECACHE) option. If no |
| 21259 ** such buffer exists or there is no space left in it, this function falls |
| 21260 ** back to sqlite3Malloc(). |
| 21261 ** |
| 21262 ** Multiple threads can run this routine at the same time. Global variables |
| 21263 ** in pcache1 need to be protected via mutex. |
| 21264 */ |
| 21265 static void *pcache1Alloc(int nByte){ |
| 21266 void *p = 0; |
| 21267 assert( sqlite3_mutex_notheld(pcache1.grp.mutex) ); |
| 21268 if( nByte<=pcache1.szSlot ){ |
| 21269 sqlite3_mutex_enter(pcache1.mutex); |
| 21270 p = (PgHdr1 *)pcache1.pFree; |
| 21271 if( p ){ |
| 21272 pcache1.pFree = pcache1.pFree->pNext; |
| 21273 pcache1.nFreeSlot--; |
| 21274 pcache1.bUnderPressure = pcache1.nFreeSlot<pcache1.nReserve; |
| 21275 assert( pcache1.nFreeSlot>=0 ); |
| 21276 sqlite3StatusHighwater(SQLITE_STATUS_PAGECACHE_SIZE, nByte); |
| 21277 sqlite3StatusUp(SQLITE_STATUS_PAGECACHE_USED, 1); |
| 21278 } |
| 21279 sqlite3_mutex_leave(pcache1.mutex); |
| 21280 } |
| 21281 if( p==0 ){ |
| 21282 /* Memory is not available in the SQLITE_CONFIG_PAGECACHE pool. Get |
| 21283 ** it from sqlite3Malloc instead. |
| 21284 */ |
| 21285 p = sqlite3Malloc(nByte); |
| 21286 #ifndef SQLITE_DISABLE_PAGECACHE_OVERFLOW_STATS |
| 21287 if( p ){ |
| 21288 int sz = sqlite3MallocSize(p); |
| 21289 sqlite3_mutex_enter(pcache1.mutex); |
| 21290 sqlite3StatusHighwater(SQLITE_STATUS_PAGECACHE_SIZE, nByte); |
| 21291 sqlite3StatusUp(SQLITE_STATUS_PAGECACHE_OVERFLOW, sz); |
| 21292 sqlite3_mutex_leave(pcache1.mutex); |
| 21293 } |
| 21294 #endif |
| 21295 sqlite3MemdebugSetType(p, MEMTYPE_PCACHE); |
| 21296 } |
| 21297 return p; |
| 21298 } |
| 21299 |
| 21300 /* |
| 21301 ** Free an allocated buffer obtained from pcache1Alloc(). |
| 21302 */ |
| 21303 static void pcache1Free(void *p){ |
| 21304 int nFreed = 0; |
| 21305 if( p==0 ) return; |
| 21306 if( SQLITE_WITHIN(p, pcache1.pStart, pcache1.pEnd) ){ |
| 21307 PgFreeslot *pSlot; |
| 21308 sqlite3_mutex_enter(pcache1.mutex); |
| 21309 sqlite3StatusDown(SQLITE_STATUS_PAGECACHE_USED, 1); |
| 21310 pSlot = (PgFreeslot*)p; |
| 21311 pSlot->pNext = pcache1.pFree; |
| 21312 pcache1.pFree = pSlot; |
| 21313 pcache1.nFreeSlot++; |
| 21314 pcache1.bUnderPressure = pcache1.nFreeSlot<pcache1.nReserve; |
| 21315 assert( pcache1.nFreeSlot<=pcache1.nSlot ); |
| 21316 sqlite3_mutex_leave(pcache1.mutex); |
| 21317 }else{ |
| 21318 assert( sqlite3MemdebugHasType(p, MEMTYPE_PCACHE) ); |
| 21319 sqlite3MemdebugSetType(p, MEMTYPE_HEAP); |
| 21320 #ifndef SQLITE_DISABLE_PAGECACHE_OVERFLOW_STATS |
| 21321 nFreed = sqlite3MallocSize(p); |
| 21322 sqlite3_mutex_enter(pcache1.mutex); |
| 21323 sqlite3StatusDown(SQLITE_STATUS_PAGECACHE_OVERFLOW, nFreed); |
| 21324 sqlite3_mutex_leave(pcache1.mutex); |
| 21325 #endif |
| 21326 sqlite3_free(p); |
| 21327 } |
| 21328 } |
| 21329 |
| 21330 #ifdef SQLITE_ENABLE_MEMORY_MANAGEMENT |
| 21331 /* |
| 21332 ** Return the size of a pcache allocation |
| 21333 */ |
| 21334 static int pcache1MemSize(void *p){ |
| 21335 if( p>=pcache1.pStart && p<pcache1.pEnd ){ |
| 21336 return pcache1.szSlot; |
| 21337 }else{ |
| 21338 int iSize; |
| 21339 assert( sqlite3MemdebugHasType(p, MEMTYPE_PCACHE) ); |
| 21340 sqlite3MemdebugSetType(p, MEMTYPE_HEAP); |
| 21341 iSize = sqlite3MallocSize(p); |
| 21342 sqlite3MemdebugSetType(p, MEMTYPE_PCACHE); |
| 21343 return iSize; |
| 21344 } |
| 21345 } |
| 21346 #endif /* SQLITE_ENABLE_MEMORY_MANAGEMENT */ |
| 21347 |
| 21348 /* |
| 21349 ** Allocate a new page object initially associated with cache pCache. |
| 21350 */ |
| 21351 static PgHdr1 *pcache1AllocPage(PCache1 *pCache, int benignMalloc){ |
| 21352 PgHdr1 *p = 0; |
| 21353 void *pPg; |
| 21354 |
| 21355 assert( sqlite3_mutex_held(pCache->pGroup->mutex) ); |
| 21356 if( pCache->pFree || (pCache->nPage==0 && pcache1InitBulk(pCache)) ){ |
| 21357 p = pCache->pFree; |
| 21358 pCache->pFree = p->pNext; |
| 21359 p->pNext = 0; |
| 21360 }else{ |
| 21361 #ifdef SQLITE_ENABLE_MEMORY_MANAGEMENT |
| 21362 /* The group mutex must be released before pcache1Alloc() is called. This |
| 21363 ** is because it might call sqlite3_release_memory(), which assumes that |
| 21364 ** this mutex is not held. */ |
| 21365 assert( pcache1.separateCache==0 ); |
| 21366 assert( pCache->pGroup==&pcache1.grp ); |
| 21367 pcache1LeaveMutex(pCache->pGroup); |
| 21368 #endif |
| 21369 if( benignMalloc ){ sqlite3BeginBenignMalloc(); } |
| 21370 #ifdef SQLITE_PCACHE_SEPARATE_HEADER |
| 21371 pPg = pcache1Alloc(pCache->szPage); |
| 21372 p = sqlite3Malloc(sizeof(PgHdr1) + pCache->szExtra); |
| 21373 if( !pPg || !p ){ |
| 21374 pcache1Free(pPg); |
| 21375 sqlite3_free(p); |
| 21376 pPg = 0; |
| 21377 } |
| 21378 #else |
| 21379 pPg = pcache1Alloc(pCache->szAlloc); |
| 21380 p = (PgHdr1 *)&((u8 *)pPg)[pCache->szPage]; |
| 21381 #endif |
| 21382 if( benignMalloc ){ sqlite3EndBenignMalloc(); } |
| 21383 #ifdef SQLITE_ENABLE_MEMORY_MANAGEMENT |
| 21384 pcache1EnterMutex(pCache->pGroup); |
| 21385 #endif |
| 21386 if( pPg==0 ) return 0; |
| 21387 p->page.pBuf = pPg; |
| 21388 p->page.pExtra = &p[1]; |
| 21389 p->isBulkLocal = 0; |
| 21390 p->isAnchor = 0; |
| 21391 } |
| 21392 if( pCache->bPurgeable ){ |
| 21393 pCache->pGroup->nCurrentPage++; |
| 21394 } |
| 21395 return p; |
| 21396 } |
| 21397 |
| 21398 /* |
| 21399 ** Free a page object allocated by pcache1AllocPage(). |
| 21400 */ |
| 21401 static void pcache1FreePage(PgHdr1 *p){ |
| 21402 PCache1 *pCache; |
| 21403 assert( p!=0 ); |
| 21404 pCache = p->pCache; |
| 21405 assert( sqlite3_mutex_held(p->pCache->pGroup->mutex) ); |
| 21406 if( p->isBulkLocal ){ |
| 21407 p->pNext = pCache->pFree; |
| 21408 pCache->pFree = p; |
| 21409 }else{ |
| 21410 pcache1Free(p->page.pBuf); |
| 21411 #ifdef SQLITE_PCACHE_SEPARATE_HEADER |
| 21412 sqlite3_free(p); |
| 21413 #endif |
| 21414 } |
| 21415 if( pCache->bPurgeable ){ |
| 21416 pCache->pGroup->nCurrentPage--; |
| 21417 } |
| 21418 } |
| 21419 |
| 21420 /* |
| 21421 ** Malloc function used by SQLite to obtain space from the buffer configured |
| 21422 ** using sqlite3_config(SQLITE_CONFIG_PAGECACHE) option. If no such buffer |
| 21423 ** exists, this function falls back to sqlite3Malloc(). |
| 21424 */ |
| 21425 SQLITE_PRIVATE void *sqlite3PageMalloc(int sz){ |
| 21426 return pcache1Alloc(sz); |
| 21427 } |
| 21428 |
| 21429 /* |
| 21430 ** Free an allocated buffer obtained from sqlite3PageMalloc(). |
| 21431 */ |
| 21432 SQLITE_PRIVATE void sqlite3PageFree(void *p){ |
| 21433 pcache1Free(p); |
| 21434 } |
| 21435 |
| 21436 |
| 21437 /* |
| 21438 ** Return true if it desirable to avoid allocating a new page cache |
| 21439 ** entry. |
| 21440 ** |
| 21441 ** If memory was allocated specifically to the page cache using |
| 21442 ** SQLITE_CONFIG_PAGECACHE but that memory has all been used, then |
| 21443 ** it is desirable to avoid allocating a new page cache entry because |
| 21444 ** presumably SQLITE_CONFIG_PAGECACHE was suppose to be sufficient |
| 21445 ** for all page cache needs and we should not need to spill the |
| 21446 ** allocation onto the heap. |
| 21447 ** |
| 21448 ** Or, the heap is used for all page cache memory but the heap is |
| 21449 ** under memory pressure, then again it is desirable to avoid |
| 21450 ** allocating a new page cache entry in order to avoid stressing |
| 21451 ** the heap even further. |
| 21452 */ |
| 21453 static int pcache1UnderMemoryPressure(PCache1 *pCache){ |
| 21454 if( pcache1.nSlot && (pCache->szPage+pCache->szExtra)<=pcache1.szSlot ){ |
| 21455 return pcache1.bUnderPressure; |
| 21456 }else{ |
| 21457 return sqlite3HeapNearlyFull(); |
| 21458 } |
| 21459 } |
| 21460 |
| 21461 /******************************************************************************/ |
| 21462 /******** General Implementation Functions ************************************/ |
| 21463 |
| 21464 /* |
| 21465 ** This function is used to resize the hash table used by the cache passed |
| 21466 ** as the first argument. |
| 21467 ** |
| 21468 ** The PCache mutex must be held when this function is called. |
| 21469 */ |
| 21470 static void pcache1ResizeHash(PCache1 *p){ |
| 21471 PgHdr1 **apNew; |
| 21472 unsigned int nNew; |
| 21473 unsigned int i; |
| 21474 |
| 21475 assert( sqlite3_mutex_held(p->pGroup->mutex) ); |
| 21476 |
| 21477 nNew = p->nHash*2; |
| 21478 if( nNew<256 ){ |
| 21479 nNew = 256; |
| 21480 } |
| 21481 |
| 21482 pcache1LeaveMutex(p->pGroup); |
| 21483 if( p->nHash ){ sqlite3BeginBenignMalloc(); } |
| 21484 apNew = (PgHdr1 **)sqlite3MallocZero(sizeof(PgHdr1 *)*nNew); |
| 21485 if( p->nHash ){ sqlite3EndBenignMalloc(); } |
| 21486 pcache1EnterMutex(p->pGroup); |
| 21487 if( apNew ){ |
| 21488 for(i=0; i<p->nHash; i++){ |
| 21489 PgHdr1 *pPage; |
| 21490 PgHdr1 *pNext = p->apHash[i]; |
| 21491 while( (pPage = pNext)!=0 ){ |
| 21492 unsigned int h = pPage->iKey % nNew; |
| 21493 pNext = pPage->pNext; |
| 21494 pPage->pNext = apNew[h]; |
| 21495 apNew[h] = pPage; |
| 21496 } |
| 21497 } |
| 21498 sqlite3_free(p->apHash); |
| 21499 p->apHash = apNew; |
| 21500 p->nHash = nNew; |
| 21501 } |
| 21502 } |
| 21503 |
| 21504 /* |
| 21505 ** This function is used internally to remove the page pPage from the |
| 21506 ** PGroup LRU list, if is part of it. If pPage is not part of the PGroup |
| 21507 ** LRU list, then this function is a no-op. |
| 21508 ** |
| 21509 ** The PGroup mutex must be held when this function is called. |
| 21510 */ |
| 21511 static PgHdr1 *pcache1PinPage(PgHdr1 *pPage){ |
| 21512 PCache1 *pCache; |
| 21513 |
| 21514 assert( pPage!=0 ); |
| 21515 assert( pPage->isPinned==0 ); |
| 21516 pCache = pPage->pCache; |
| 21517 assert( pPage->pLruNext ); |
| 21518 assert( pPage->pLruPrev ); |
| 21519 assert( sqlite3_mutex_held(pCache->pGroup->mutex) ); |
| 21520 pPage->pLruPrev->pLruNext = pPage->pLruNext; |
| 21521 pPage->pLruNext->pLruPrev = pPage->pLruPrev; |
| 21522 pPage->pLruNext = 0; |
| 21523 pPage->pLruPrev = 0; |
| 21524 pPage->isPinned = 1; |
| 21525 assert( pPage->isAnchor==0 ); |
| 21526 assert( pCache->pGroup->lru.isAnchor==1 ); |
| 21527 pCache->nRecyclable--; |
| 21528 return pPage; |
| 21529 } |
| 21530 |
| 21531 |
| 21532 /* |
| 21533 ** Remove the page supplied as an argument from the hash table |
| 21534 ** (PCache1.apHash structure) that it is currently stored in. |
| 21535 ** Also free the page if freePage is true. |
| 21536 ** |
| 21537 ** The PGroup mutex must be held when this function is called. |
| 21538 */ |
| 21539 static void pcache1RemoveFromHash(PgHdr1 *pPage, int freeFlag){ |
| 21540 unsigned int h; |
| 21541 PCache1 *pCache = pPage->pCache; |
| 21542 PgHdr1 **pp; |
| 21543 |
| 21544 assert( sqlite3_mutex_held(pCache->pGroup->mutex) ); |
| 21545 h = pPage->iKey % pCache->nHash; |
| 21546 for(pp=&pCache->apHash[h]; (*pp)!=pPage; pp=&(*pp)->pNext); |
| 21547 *pp = (*pp)->pNext; |
| 21548 |
| 21549 pCache->nPage--; |
| 21550 if( freeFlag ) pcache1FreePage(pPage); |
| 21551 } |
| 21552 |
| 21553 /* |
| 21554 ** If there are currently more than nMaxPage pages allocated, try |
| 21555 ** to recycle pages to reduce the number allocated to nMaxPage. |
| 21556 */ |
| 21557 static void pcache1EnforceMaxPage(PCache1 *pCache){ |
| 21558 PGroup *pGroup = pCache->pGroup; |
| 21559 PgHdr1 *p; |
| 21560 assert( sqlite3_mutex_held(pGroup->mutex) ); |
| 21561 while( pGroup->nCurrentPage>pGroup->nMaxPage |
| 21562 && (p=pGroup->lru.pLruPrev)->isAnchor==0 |
| 21563 ){ |
| 21564 assert( p->pCache->pGroup==pGroup ); |
| 21565 assert( p->isPinned==0 ); |
| 21566 pcache1PinPage(p); |
| 21567 pcache1RemoveFromHash(p, 1); |
| 21568 } |
| 21569 if( pCache->nPage==0 && pCache->pBulk ){ |
| 21570 sqlite3_free(pCache->pBulk); |
| 21571 pCache->pBulk = pCache->pFree = 0; |
| 21572 } |
| 21573 } |
| 21574 |
| 21575 /* |
| 21576 ** Discard all pages from cache pCache with a page number (key value) |
| 21577 ** greater than or equal to iLimit. Any pinned pages that meet this |
| 21578 ** criteria are unpinned before they are discarded. |
| 21579 ** |
| 21580 ** The PCache mutex must be held when this function is called. |
| 21581 */ |
| 21582 static void pcache1TruncateUnsafe( |
| 21583 PCache1 *pCache, /* The cache to truncate */ |
| 21584 unsigned int iLimit /* Drop pages with this pgno or larger */ |
| 21585 ){ |
| 21586 TESTONLY( unsigned int nPage = 0; ) /* To assert pCache->nPage is correct */ |
| 21587 unsigned int h; |
| 21588 assert( sqlite3_mutex_held(pCache->pGroup->mutex) ); |
| 21589 for(h=0; h<pCache->nHash; h++){ |
| 21590 PgHdr1 **pp = &pCache->apHash[h]; |
| 21591 PgHdr1 *pPage; |
| 21592 while( (pPage = *pp)!=0 ){ |
| 21593 if( pPage->iKey>=iLimit ){ |
| 21594 pCache->nPage--; |
| 21595 *pp = pPage->pNext; |
| 21596 if( !pPage->isPinned ) pcache1PinPage(pPage); |
| 21597 pcache1FreePage(pPage); |
| 21598 }else{ |
| 21599 pp = &pPage->pNext; |
| 21600 TESTONLY( nPage++; ) |
| 21601 } |
| 21602 } |
| 21603 } |
| 21604 assert( pCache->nPage==nPage ); |
| 21605 } |
| 21606 |
| 21607 /******************************************************************************/ |
| 21608 /******** sqlite3_pcache Methods **********************************************/ |
| 21609 |
| 21610 /* |
| 21611 ** Implementation of the sqlite3_pcache.xInit method. |
| 21612 */ |
| 21613 static int pcache1Init(void *NotUsed){ |
| 21614 UNUSED_PARAMETER(NotUsed); |
| 21615 assert( pcache1.isInit==0 ); |
| 21616 memset(&pcache1, 0, sizeof(pcache1)); |
| 21617 |
| 21618 |
| 21619 /* |
| 21620 ** The pcache1.separateCache variable is true if each PCache has its own |
| 21621 ** private PGroup (mode-1). pcache1.separateCache is false if the single |
| 21622 ** PGroup in pcache1.grp is used for all page caches (mode-2). |
| 21623 ** |
| 21624 ** * Always use separate caches (mode-1) if SQLITE_SEPARATE_CACHE_POOLS |
| 21625 ** |
| 21626 ** * Always use a unified cache (mode-2) if ENABLE_MEMORY_MANAGEMENT |
| 21627 ** |
| 21628 ** * Use a unified cache in single-threaded applications that have |
| 21629 ** configured a start-time buffer for use as page-cache memory using |
| 21630 ** sqlite3_config(SQLITE_CONFIG_PAGECACHE, pBuf, sz, N) with non-NULL |
| 21631 ** pBuf argument. |
| 21632 ** |
| 21633 ** * Otherwise use separate caches (mode-1) |
| 21634 */ |
| 21635 #ifdef SQLITE_SEPARATE_CACHE_POOLS |
| 21636 const int separateCache = 1; |
| 21637 #elif defined(SQLITE_ENABLE_MEMORY_MANAGEMENT) |
| 21638 pcache1.separateCache = 0; |
| 21639 #elif SQLITE_THREADSAFE |
| 21640 pcache1.separateCache = sqlite3GlobalConfig.pPage==0 |
| 21641 || sqlite3GlobalConfig.bCoreMutex>0; |
| 21642 #else |
| 21643 pcache1.separateCache = sqlite3GlobalConfig.pPage==0; |
| 21644 #endif |
| 21645 |
| 21646 #if SQLITE_THREADSAFE |
| 21647 if( sqlite3GlobalConfig.bCoreMutex ){ |
| 21648 pcache1.grp.mutex = sqlite3_mutex_alloc(SQLITE_MUTEX_STATIC_LRU); |
| 21649 pcache1.mutex = sqlite3_mutex_alloc(SQLITE_MUTEX_STATIC_PMEM); |
| 21650 } |
| 21651 #endif |
| 21652 if( pcache1.separateCache |
| 21653 && sqlite3GlobalConfig.nPage!=0 |
| 21654 && sqlite3GlobalConfig.pPage==0 |
| 21655 ){ |
| 21656 pcache1.nInitPage = sqlite3GlobalConfig.nPage; |
| 21657 }else{ |
| 21658 pcache1.nInitPage = 0; |
| 21659 } |
| 21660 pcache1.grp.mxPinned = 10; |
| 21661 pcache1.isInit = 1; |
| 21662 return SQLITE_OK; |
| 21663 } |
| 21664 |
| 21665 /* |
| 21666 ** Implementation of the sqlite3_pcache.xShutdown method. |
| 21667 ** Note that the static mutex allocated in xInit does |
| 21668 ** not need to be freed. |
| 21669 */ |
| 21670 static void pcache1Shutdown(void *NotUsed){ |
| 21671 UNUSED_PARAMETER(NotUsed); |
| 21672 assert( pcache1.isInit!=0 ); |
| 21673 memset(&pcache1, 0, sizeof(pcache1)); |
| 21674 } |
| 21675 |
| 21676 /* forward declaration */ |
| 21677 static void pcache1Destroy(sqlite3_pcache *p); |
| 21678 |
| 21679 /* |
| 21680 ** Implementation of the sqlite3_pcache.xCreate method. |
| 21681 ** |
| 21682 ** Allocate a new cache. |
| 21683 */ |
| 21684 static sqlite3_pcache *pcache1Create(int szPage, int szExtra, int bPurgeable){ |
| 21685 PCache1 *pCache; /* The newly created page cache */ |
| 21686 PGroup *pGroup; /* The group the new page cache will belong to */ |
| 21687 int sz; /* Bytes of memory required to allocate the new cache */ |
| 21688 |
| 21689 assert( (szPage & (szPage-1))==0 && szPage>=512 && szPage<=65536 ); |
| 21690 assert( szExtra < 300 ); |
| 21691 |
| 21692 sz = sizeof(PCache1) + sizeof(PGroup)*pcache1.separateCache; |
| 21693 pCache = (PCache1 *)sqlite3MallocZero(sz); |
| 21694 if( pCache ){ |
| 21695 if( pcache1.separateCache ){ |
| 21696 pGroup = (PGroup*)&pCache[1]; |
| 21697 pGroup->mxPinned = 10; |
| 21698 }else{ |
| 21699 pGroup = &pcache1.grp; |
| 21700 } |
| 21701 if( pGroup->lru.isAnchor==0 ){ |
| 21702 pGroup->lru.isAnchor = 1; |
| 21703 pGroup->lru.pLruPrev = pGroup->lru.pLruNext = &pGroup->lru; |
| 21704 } |
| 21705 pCache->pGroup = pGroup; |
| 21706 pCache->szPage = szPage; |
| 21707 pCache->szExtra = szExtra; |
| 21708 pCache->szAlloc = szPage + szExtra + ROUND8(sizeof(PgHdr1)); |
| 21709 pCache->bPurgeable = (bPurgeable ? 1 : 0); |
| 21710 pcache1EnterMutex(pGroup); |
| 21711 pcache1ResizeHash(pCache); |
| 21712 if( bPurgeable ){ |
| 21713 pCache->nMin = 10; |
| 21714 pGroup->nMinPage += pCache->nMin; |
| 21715 pGroup->mxPinned = pGroup->nMaxPage + 10 - pGroup->nMinPage; |
| 21716 } |
| 21717 pcache1LeaveMutex(pGroup); |
| 21718 if( pCache->nHash==0 ){ |
| 21719 pcache1Destroy((sqlite3_pcache*)pCache); |
| 21720 pCache = 0; |
| 21721 } |
| 21722 } |
| 21723 return (sqlite3_pcache *)pCache; |
| 21724 } |
| 21725 |
| 21726 /* |
| 21727 ** Implementation of the sqlite3_pcache.xCachesize method. |
| 21728 ** |
| 21729 ** Configure the cache_size limit for a cache. |
| 21730 */ |
| 21731 static void pcache1Cachesize(sqlite3_pcache *p, int nMax){ |
| 21732 PCache1 *pCache = (PCache1 *)p; |
| 21733 if( pCache->bPurgeable ){ |
| 21734 PGroup *pGroup = pCache->pGroup; |
| 21735 pcache1EnterMutex(pGroup); |
| 21736 pGroup->nMaxPage += (nMax - pCache->nMax); |
| 21737 pGroup->mxPinned = pGroup->nMaxPage + 10 - pGroup->nMinPage; |
| 21738 pCache->nMax = nMax; |
| 21739 pCache->n90pct = pCache->nMax*9/10; |
| 21740 pcache1EnforceMaxPage(pCache); |
| 21741 pcache1LeaveMutex(pGroup); |
| 21742 } |
| 21743 } |
| 21744 |
| 21745 /* |
| 21746 ** Implementation of the sqlite3_pcache.xShrink method. |
| 21747 ** |
| 21748 ** Free up as much memory as possible. |
| 21749 */ |
| 21750 static void pcache1Shrink(sqlite3_pcache *p){ |
| 21751 PCache1 *pCache = (PCache1*)p; |
| 21752 if( pCache->bPurgeable ){ |
| 21753 PGroup *pGroup = pCache->pGroup; |
| 21754 int savedMaxPage; |
| 21755 pcache1EnterMutex(pGroup); |
| 21756 savedMaxPage = pGroup->nMaxPage; |
| 21757 pGroup->nMaxPage = 0; |
| 21758 pcache1EnforceMaxPage(pCache); |
| 21759 pGroup->nMaxPage = savedMaxPage; |
| 21760 pcache1LeaveMutex(pGroup); |
| 21761 } |
| 21762 } |
| 21763 |
| 21764 /* |
| 21765 ** Implementation of the sqlite3_pcache.xPagecount method. |
| 21766 */ |
| 21767 static int pcache1Pagecount(sqlite3_pcache *p){ |
| 21768 int n; |
| 21769 PCache1 *pCache = (PCache1*)p; |
| 21770 pcache1EnterMutex(pCache->pGroup); |
| 21771 n = pCache->nPage; |
| 21772 pcache1LeaveMutex(pCache->pGroup); |
| 21773 return n; |
| 21774 } |
| 21775 |
| 21776 |
| 21777 /* |
| 21778 ** Implement steps 3, 4, and 5 of the pcache1Fetch() algorithm described |
| 21779 ** in the header of the pcache1Fetch() procedure. |
| 21780 ** |
| 21781 ** This steps are broken out into a separate procedure because they are |
| 21782 ** usually not needed, and by avoiding the stack initialization required |
| 21783 ** for these steps, the main pcache1Fetch() procedure can run faster. |
| 21784 */ |
| 21785 static SQLITE_NOINLINE PgHdr1 *pcache1FetchStage2( |
| 21786 PCache1 *pCache, |
| 21787 unsigned int iKey, |
| 21788 int createFlag |
| 21789 ){ |
| 21790 unsigned int nPinned; |
| 21791 PGroup *pGroup = pCache->pGroup; |
| 21792 PgHdr1 *pPage = 0; |
| 21793 |
| 21794 /* Step 3: Abort if createFlag is 1 but the cache is nearly full */ |
| 21795 assert( pCache->nPage >= pCache->nRecyclable ); |
| 21796 nPinned = pCache->nPage - pCache->nRecyclable; |
| 21797 assert( pGroup->mxPinned == pGroup->nMaxPage + 10 - pGroup->nMinPage ); |
| 21798 assert( pCache->n90pct == pCache->nMax*9/10 ); |
| 21799 if( createFlag==1 && ( |
| 21800 nPinned>=pGroup->mxPinned |
| 21801 || nPinned>=pCache->n90pct |
| 21802 || (pcache1UnderMemoryPressure(pCache) && pCache->nRecyclable<nPinned) |
| 21803 )){ |
| 21804 return 0; |
| 21805 } |
| 21806 |
| 21807 if( pCache->nPage>=pCache->nHash ) pcache1ResizeHash(pCache); |
| 21808 assert( pCache->nHash>0 && pCache->apHash ); |
| 21809 |
| 21810 /* Step 4. Try to recycle a page. */ |
| 21811 if( pCache->bPurgeable |
| 21812 && !pGroup->lru.pLruPrev->isAnchor |
| 21813 && ((pCache->nPage+1>=pCache->nMax) || pcache1UnderMemoryPressure(pCache)) |
| 21814 ){ |
| 21815 PCache1 *pOther; |
| 21816 pPage = pGroup->lru.pLruPrev; |
| 21817 assert( pPage->isPinned==0 ); |
| 21818 pcache1RemoveFromHash(pPage, 0); |
| 21819 pcache1PinPage(pPage); |
| 21820 pOther = pPage->pCache; |
| 21821 if( pOther->szAlloc != pCache->szAlloc ){ |
| 21822 pcache1FreePage(pPage); |
| 21823 pPage = 0; |
| 21824 }else{ |
| 21825 pGroup->nCurrentPage -= (pOther->bPurgeable - pCache->bPurgeable); |
| 21826 } |
| 21827 } |
| 21828 |
| 21829 /* Step 5. If a usable page buffer has still not been found, |
| 21830 ** attempt to allocate a new one. |
| 21831 */ |
| 21832 if( !pPage ){ |
| 21833 pPage = pcache1AllocPage(pCache, createFlag==1); |
| 21834 } |
| 21835 |
| 21836 if( pPage ){ |
| 21837 unsigned int h = iKey % pCache->nHash; |
| 21838 pCache->nPage++; |
| 21839 pPage->iKey = iKey; |
| 21840 pPage->pNext = pCache->apHash[h]; |
| 21841 pPage->pCache = pCache; |
| 21842 pPage->pLruPrev = 0; |
| 21843 pPage->pLruNext = 0; |
| 21844 pPage->isPinned = 1; |
| 21845 *(void **)pPage->page.pExtra = 0; |
| 21846 pCache->apHash[h] = pPage; |
| 21847 if( iKey>pCache->iMaxKey ){ |
| 21848 pCache->iMaxKey = iKey; |
| 21849 } |
| 21850 } |
| 21851 return pPage; |
| 21852 } |
| 21853 |
| 21854 /* |
| 21855 ** Implementation of the sqlite3_pcache.xFetch method. |
| 21856 ** |
| 21857 ** Fetch a page by key value. |
| 21858 ** |
| 21859 ** Whether or not a new page may be allocated by this function depends on |
| 21860 ** the value of the createFlag argument. 0 means do not allocate a new |
| 21861 ** page. 1 means allocate a new page if space is easily available. 2 |
| 21862 ** means to try really hard to allocate a new page. |
| 21863 ** |
| 21864 ** For a non-purgeable cache (a cache used as the storage for an in-memory |
| 21865 ** database) there is really no difference between createFlag 1 and 2. So |
| 21866 ** the calling function (pcache.c) will never have a createFlag of 1 on |
| 21867 ** a non-purgeable cache. |
| 21868 ** |
| 21869 ** There are three different approaches to obtaining space for a page, |
| 21870 ** depending on the value of parameter createFlag (which may be 0, 1 or 2). |
| 21871 ** |
| 21872 ** 1. Regardless of the value of createFlag, the cache is searched for a |
| 21873 ** copy of the requested page. If one is found, it is returned. |
| 21874 ** |
| 21875 ** 2. If createFlag==0 and the page is not already in the cache, NULL is |
| 21876 ** returned. |
| 21877 ** |
| 21878 ** 3. If createFlag is 1, and the page is not already in the cache, then |
| 21879 ** return NULL (do not allocate a new page) if any of the following |
| 21880 ** conditions are true: |
| 21881 ** |
| 21882 ** (a) the number of pages pinned by the cache is greater than |
| 21883 ** PCache1.nMax, or |
| 21884 ** |
| 21885 ** (b) the number of pages pinned by the cache is greater than |
| 21886 ** the sum of nMax for all purgeable caches, less the sum of |
| 21887 ** nMin for all other purgeable caches, or |
| 21888 ** |
| 21889 ** 4. If none of the first three conditions apply and the cache is marked |
| 21890 ** as purgeable, and if one of the following is true: |
| 21891 ** |
| 21892 ** (a) The number of pages allocated for the cache is already |
| 21893 ** PCache1.nMax, or |
| 21894 ** |
| 21895 ** (b) The number of pages allocated for all purgeable caches is |
| 21896 ** already equal to or greater than the sum of nMax for all |
| 21897 ** purgeable caches, |
| 21898 ** |
| 21899 ** (c) The system is under memory pressure and wants to avoid |
| 21900 ** unnecessary pages cache entry allocations |
| 21901 ** |
| 21902 ** then attempt to recycle a page from the LRU list. If it is the right |
| 21903 ** size, return the recycled buffer. Otherwise, free the buffer and |
| 21904 ** proceed to step 5. |
| 21905 ** |
| 21906 ** 5. Otherwise, allocate and return a new page buffer. |
| 21907 ** |
| 21908 ** There are two versions of this routine. pcache1FetchWithMutex() is |
| 21909 ** the general case. pcache1FetchNoMutex() is a faster implementation for |
| 21910 ** the common case where pGroup->mutex is NULL. The pcache1Fetch() wrapper |
| 21911 ** invokes the appropriate routine. |
| 21912 */ |
| 21913 static PgHdr1 *pcache1FetchNoMutex( |
| 21914 sqlite3_pcache *p, |
| 21915 unsigned int iKey, |
| 21916 int createFlag |
| 21917 ){ |
| 21918 PCache1 *pCache = (PCache1 *)p; |
| 21919 PgHdr1 *pPage = 0; |
| 21920 |
| 21921 /* Step 1: Search the hash table for an existing entry. */ |
| 21922 pPage = pCache->apHash[iKey % pCache->nHash]; |
| 21923 while( pPage && pPage->iKey!=iKey ){ pPage = pPage->pNext; } |
| 21924 |
| 21925 /* Step 2: If the page was found in the hash table, then return it. |
| 21926 ** If the page was not in the hash table and createFlag is 0, abort. |
| 21927 ** Otherwise (page not in hash and createFlag!=0) continue with |
| 21928 ** subsequent steps to try to create the page. */ |
| 21929 if( pPage ){ |
| 21930 if( !pPage->isPinned ){ |
| 21931 return pcache1PinPage(pPage); |
| 21932 }else{ |
| 21933 return pPage; |
| 21934 } |
| 21935 }else if( createFlag ){ |
| 21936 /* Steps 3, 4, and 5 implemented by this subroutine */ |
| 21937 return pcache1FetchStage2(pCache, iKey, createFlag); |
| 21938 }else{ |
| 21939 return 0; |
| 21940 } |
| 21941 } |
| 21942 #if PCACHE1_MIGHT_USE_GROUP_MUTEX |
| 21943 static PgHdr1 *pcache1FetchWithMutex( |
| 21944 sqlite3_pcache *p, |
| 21945 unsigned int iKey, |
| 21946 int createFlag |
| 21947 ){ |
| 21948 PCache1 *pCache = (PCache1 *)p; |
| 21949 PgHdr1 *pPage; |
| 21950 |
| 21951 pcache1EnterMutex(pCache->pGroup); |
| 21952 pPage = pcache1FetchNoMutex(p, iKey, createFlag); |
| 21953 assert( pPage==0 || pCache->iMaxKey>=iKey ); |
| 21954 pcache1LeaveMutex(pCache->pGroup); |
| 21955 return pPage; |
| 21956 } |
| 21957 #endif |
| 21958 static sqlite3_pcache_page *pcache1Fetch( |
| 21959 sqlite3_pcache *p, |
| 21960 unsigned int iKey, |
| 21961 int createFlag |
| 21962 ){ |
| 21963 #if PCACHE1_MIGHT_USE_GROUP_MUTEX || defined(SQLITE_DEBUG) |
| 21964 PCache1 *pCache = (PCache1 *)p; |
| 21965 #endif |
| 21966 |
| 21967 assert( offsetof(PgHdr1,page)==0 ); |
| 21968 assert( pCache->bPurgeable || createFlag!=1 ); |
| 21969 assert( pCache->bPurgeable || pCache->nMin==0 ); |
| 21970 assert( pCache->bPurgeable==0 || pCache->nMin==10 ); |
| 21971 assert( pCache->nMin==0 || pCache->bPurgeable ); |
| 21972 assert( pCache->nHash>0 ); |
| 21973 #if PCACHE1_MIGHT_USE_GROUP_MUTEX |
| 21974 if( pCache->pGroup->mutex ){ |
| 21975 return (sqlite3_pcache_page*)pcache1FetchWithMutex(p, iKey, createFlag); |
| 21976 }else |
| 21977 #endif |
| 21978 { |
| 21979 return (sqlite3_pcache_page*)pcache1FetchNoMutex(p, iKey, createFlag); |
| 21980 } |
| 21981 } |
| 21982 |
| 21983 |
| 21984 /* |
| 21985 ** Implementation of the sqlite3_pcache.xUnpin method. |
| 21986 ** |
| 21987 ** Mark a page as unpinned (eligible for asynchronous recycling). |
| 21988 */ |
| 21989 static void pcache1Unpin( |
| 21990 sqlite3_pcache *p, |
| 21991 sqlite3_pcache_page *pPg, |
| 21992 int reuseUnlikely |
| 21993 ){ |
| 21994 PCache1 *pCache = (PCache1 *)p; |
| 21995 PgHdr1 *pPage = (PgHdr1 *)pPg; |
| 21996 PGroup *pGroup = pCache->pGroup; |
| 21997 |
| 21998 assert( pPage->pCache==pCache ); |
| 21999 pcache1EnterMutex(pGroup); |
| 22000 |
| 22001 /* It is an error to call this function if the page is already |
| 22002 ** part of the PGroup LRU list. |
| 22003 */ |
| 22004 assert( pPage->pLruPrev==0 && pPage->pLruNext==0 ); |
| 22005 assert( pPage->isPinned==1 ); |
| 22006 |
| 22007 if( reuseUnlikely || pGroup->nCurrentPage>pGroup->nMaxPage ){ |
| 22008 pcache1RemoveFromHash(pPage, 1); |
| 22009 }else{ |
| 22010 /* Add the page to the PGroup LRU list. */ |
| 22011 PgHdr1 **ppFirst = &pGroup->lru.pLruNext; |
| 22012 pPage->pLruPrev = &pGroup->lru; |
| 22013 (pPage->pLruNext = *ppFirst)->pLruPrev = pPage; |
| 22014 *ppFirst = pPage; |
| 22015 pCache->nRecyclable++; |
| 22016 pPage->isPinned = 0; |
| 22017 } |
| 22018 |
| 22019 pcache1LeaveMutex(pCache->pGroup); |
| 22020 } |
| 22021 |
| 22022 /* |
| 22023 ** Implementation of the sqlite3_pcache.xRekey method. |
| 22024 */ |
| 22025 static void pcache1Rekey( |
| 22026 sqlite3_pcache *p, |
| 22027 sqlite3_pcache_page *pPg, |
| 22028 unsigned int iOld, |
| 22029 unsigned int iNew |
| 22030 ){ |
| 22031 PCache1 *pCache = (PCache1 *)p; |
| 22032 PgHdr1 *pPage = (PgHdr1 *)pPg; |
| 22033 PgHdr1 **pp; |
| 22034 unsigned int h; |
| 22035 assert( pPage->iKey==iOld ); |
| 22036 assert( pPage->pCache==pCache ); |
| 22037 |
| 22038 pcache1EnterMutex(pCache->pGroup); |
| 22039 |
| 22040 h = iOld%pCache->nHash; |
| 22041 pp = &pCache->apHash[h]; |
| 22042 while( (*pp)!=pPage ){ |
| 22043 pp = &(*pp)->pNext; |
| 22044 } |
| 22045 *pp = pPage->pNext; |
| 22046 |
| 22047 h = iNew%pCache->nHash; |
| 22048 pPage->iKey = iNew; |
| 22049 pPage->pNext = pCache->apHash[h]; |
| 22050 pCache->apHash[h] = pPage; |
| 22051 if( iNew>pCache->iMaxKey ){ |
| 22052 pCache->iMaxKey = iNew; |
| 22053 } |
| 22054 |
| 22055 pcache1LeaveMutex(pCache->pGroup); |
| 22056 } |
| 22057 |
| 22058 /* |
| 22059 ** Implementation of the sqlite3_pcache.xTruncate method. |
| 22060 ** |
| 22061 ** Discard all unpinned pages in the cache with a page number equal to |
| 22062 ** or greater than parameter iLimit. Any pinned pages with a page number |
| 22063 ** equal to or greater than iLimit are implicitly unpinned. |
| 22064 */ |
| 22065 static void pcache1Truncate(sqlite3_pcache *p, unsigned int iLimit){ |
| 22066 PCache1 *pCache = (PCache1 *)p; |
| 22067 pcache1EnterMutex(pCache->pGroup); |
| 22068 if( iLimit<=pCache->iMaxKey ){ |
| 22069 pcache1TruncateUnsafe(pCache, iLimit); |
| 22070 pCache->iMaxKey = iLimit-1; |
| 22071 } |
| 22072 pcache1LeaveMutex(pCache->pGroup); |
| 22073 } |
| 22074 |
| 22075 /* |
| 22076 ** Implementation of the sqlite3_pcache.xDestroy method. |
| 22077 ** |
| 22078 ** Destroy a cache allocated using pcache1Create(). |
| 22079 */ |
| 22080 static void pcache1Destroy(sqlite3_pcache *p){ |
| 22081 PCache1 *pCache = (PCache1 *)p; |
| 22082 PGroup *pGroup = pCache->pGroup; |
| 22083 assert( pCache->bPurgeable || (pCache->nMax==0 && pCache->nMin==0) ); |
| 22084 pcache1EnterMutex(pGroup); |
| 22085 pcache1TruncateUnsafe(pCache, 0); |
| 22086 assert( pGroup->nMaxPage >= pCache->nMax ); |
| 22087 pGroup->nMaxPage -= pCache->nMax; |
| 22088 assert( pGroup->nMinPage >= pCache->nMin ); |
| 22089 pGroup->nMinPage -= pCache->nMin; |
| 22090 pGroup->mxPinned = pGroup->nMaxPage + 10 - pGroup->nMinPage; |
| 22091 pcache1EnforceMaxPage(pCache); |
| 22092 pcache1LeaveMutex(pGroup); |
| 22093 sqlite3_free(pCache->pBulk); |
| 22094 sqlite3_free(pCache->apHash); |
| 22095 sqlite3_free(pCache); |
| 22096 } |
| 22097 |
| 22098 /* |
| 22099 ** This function is called during initialization (sqlite3_initialize()) to |
| 22100 ** install the default pluggable cache module, assuming the user has not |
| 22101 ** already provided an alternative. |
| 22102 */ |
| 22103 SQLITE_PRIVATE void sqlite3PCacheSetDefault(void){ |
| 22104 static const sqlite3_pcache_methods2 defaultMethods = { |
| 22105 1, /* iVersion */ |
| 22106 0, /* pArg */ |
| 22107 pcache1Init, /* xInit */ |
| 22108 pcache1Shutdown, /* xShutdown */ |
| 22109 pcache1Create, /* xCreate */ |
| 22110 pcache1Cachesize, /* xCachesize */ |
| 22111 pcache1Pagecount, /* xPagecount */ |
| 22112 pcache1Fetch, /* xFetch */ |
| 22113 pcache1Unpin, /* xUnpin */ |
| 22114 pcache1Rekey, /* xRekey */ |
| 22115 pcache1Truncate, /* xTruncate */ |
| 22116 pcache1Destroy, /* xDestroy */ |
| 22117 pcache1Shrink /* xShrink */ |
| 22118 }; |
| 22119 sqlite3_config(SQLITE_CONFIG_PCACHE2, &defaultMethods); |
| 22120 } |
| 22121 |
| 22122 /* |
| 22123 ** Return the size of the header on each page of this PCACHE implementation. |
| 22124 */ |
| 22125 SQLITE_PRIVATE int sqlite3HeaderSizePcache1(void){ return ROUND8(sizeof(PgHdr1))
; } |
| 22126 |
| 22127 /* |
| 22128 ** Return the global mutex used by this PCACHE implementation. The |
| 22129 ** sqlite3_status() routine needs access to this mutex. |
| 22130 */ |
| 22131 SQLITE_PRIVATE sqlite3_mutex *sqlite3Pcache1Mutex(void){ |
| 22132 return pcache1.mutex; |
| 22133 } |
| 22134 |
| 22135 #ifdef SQLITE_ENABLE_MEMORY_MANAGEMENT |
| 22136 /* |
| 22137 ** This function is called to free superfluous dynamically allocated memory |
| 22138 ** held by the pager system. Memory in use by any SQLite pager allocated |
| 22139 ** by the current thread may be sqlite3_free()ed. |
| 22140 ** |
| 22141 ** nReq is the number of bytes of memory required. Once this much has |
| 22142 ** been released, the function returns. The return value is the total number |
| 22143 ** of bytes of memory released. |
| 22144 */ |
| 22145 SQLITE_PRIVATE int sqlite3PcacheReleaseMemory(int nReq){ |
| 22146 int nFree = 0; |
| 22147 assert( sqlite3_mutex_notheld(pcache1.grp.mutex) ); |
| 22148 assert( sqlite3_mutex_notheld(pcache1.mutex) ); |
| 22149 if( sqlite3GlobalConfig.nPage==0 ){ |
| 22150 PgHdr1 *p; |
| 22151 pcache1EnterMutex(&pcache1.grp); |
| 22152 while( (nReq<0 || nFree<nReq) |
| 22153 && (p=pcache1.grp.lru.pLruPrev)!=0 |
| 22154 && p->isAnchor==0 |
| 22155 ){ |
| 22156 nFree += pcache1MemSize(p->page.pBuf); |
| 22157 #ifdef SQLITE_PCACHE_SEPARATE_HEADER |
| 22158 nFree += sqlite3MemSize(p); |
| 22159 #endif |
| 22160 assert( p->isPinned==0 ); |
| 22161 pcache1PinPage(p); |
| 22162 pcache1RemoveFromHash(p, 1); |
| 22163 } |
| 22164 pcache1LeaveMutex(&pcache1.grp); |
| 22165 } |
| 22166 return nFree; |
| 22167 } |
| 22168 #endif /* SQLITE_ENABLE_MEMORY_MANAGEMENT */ |
| 22169 |
| 22170 #ifdef SQLITE_TEST |
| 22171 /* |
| 22172 ** This function is used by test procedures to inspect the internal state |
| 22173 ** of the global cache. |
| 22174 */ |
| 22175 SQLITE_PRIVATE void sqlite3PcacheStats( |
| 22176 int *pnCurrent, /* OUT: Total number of pages cached */ |
| 22177 int *pnMax, /* OUT: Global maximum cache size */ |
| 22178 int *pnMin, /* OUT: Sum of PCache1.nMin for purgeable caches */ |
| 22179 int *pnRecyclable /* OUT: Total number of pages available for recycling */ |
| 22180 ){ |
| 22181 PgHdr1 *p; |
| 22182 int nRecyclable = 0; |
| 22183 for(p=pcache1.grp.lru.pLruNext; p && !p->isAnchor; p=p->pLruNext){ |
| 22184 assert( p->isPinned==0 ); |
| 22185 nRecyclable++; |
| 22186 } |
| 22187 *pnCurrent = pcache1.grp.nCurrentPage; |
| 22188 *pnMax = (int)pcache1.grp.nMaxPage; |
| 22189 *pnMin = (int)pcache1.grp.nMinPage; |
| 22190 *pnRecyclable = nRecyclable; |
| 22191 } |
| 22192 #endif |
| 22193 |
| 22194 /************** End of pcache1.c *********************************************/ |
| 22195 /************** Begin file rowset.c ******************************************/ |
| 22196 /* |
| 22197 ** 2008 December 3 |
| 22198 ** |
| 22199 ** The author disclaims copyright to this source code. In place of |
| 22200 ** a legal notice, here is a blessing: |
| 22201 ** |
| 22202 ** May you do good and not evil. |
| 22203 ** May you find forgiveness for yourself and forgive others. |
| 22204 ** May you share freely, never taking more than you give. |
| 22205 ** |
| 22206 ************************************************************************* |
| 22207 ** |
| 22208 ** This module implements an object we call a "RowSet". |
| 22209 ** |
| 22210 ** The RowSet object is a collection of rowids. Rowids |
| 22211 ** are inserted into the RowSet in an arbitrary order. Inserts |
| 22212 ** can be intermixed with tests to see if a given rowid has been |
| 22213 ** previously inserted into the RowSet. |
| 22214 ** |
| 22215 ** After all inserts are finished, it is possible to extract the |
| 22216 ** elements of the RowSet in sorted order. Once this extraction |
| 22217 ** process has started, no new elements may be inserted. |
| 22218 ** |
| 22219 ** Hence, the primitive operations for a RowSet are: |
| 22220 ** |
| 22221 ** CREATE |
| 22222 ** INSERT |
| 22223 ** TEST |
| 22224 ** SMALLEST |
| 22225 ** DESTROY |
| 22226 ** |
| 22227 ** The CREATE and DESTROY primitives are the constructor and destructor, |
| 22228 ** obviously. The INSERT primitive adds a new element to the RowSet. |
| 22229 ** TEST checks to see if an element is already in the RowSet. SMALLEST |
| 22230 ** extracts the least value from the RowSet. |
| 22231 ** |
| 22232 ** The INSERT primitive might allocate additional memory. Memory is |
| 22233 ** allocated in chunks so most INSERTs do no allocation. There is an |
| 22234 ** upper bound on the size of allocated memory. No memory is freed |
| 22235 ** until DESTROY. |
| 22236 ** |
| 22237 ** The TEST primitive includes a "batch" number. The TEST primitive |
| 22238 ** will only see elements that were inserted before the last change |
| 22239 ** in the batch number. In other words, if an INSERT occurs between |
| 22240 ** two TESTs where the TESTs have the same batch nubmer, then the |
| 22241 ** value added by the INSERT will not be visible to the second TEST. |
| 22242 ** The initial batch number is zero, so if the very first TEST contains |
| 22243 ** a non-zero batch number, it will see all prior INSERTs. |
| 22244 ** |
| 22245 ** No INSERTs may occurs after a SMALLEST. An assertion will fail if |
| 22246 ** that is attempted. |
| 22247 ** |
| 22248 ** The cost of an INSERT is roughly constant. (Sometimes new memory |
| 22249 ** has to be allocated on an INSERT.) The cost of a TEST with a new |
| 22250 ** batch number is O(NlogN) where N is the number of elements in the RowSet. |
| 22251 ** The cost of a TEST using the same batch number is O(logN). The cost |
| 22252 ** of the first SMALLEST is O(NlogN). Second and subsequent SMALLEST |
| 22253 ** primitives are constant time. The cost of DESTROY is O(N). |
| 22254 ** |
| 22255 ** There is an added cost of O(N) when switching between TEST and |
| 22256 ** SMALLEST primitives. |
| 22257 */ |
| 22258 /* #include "sqliteInt.h" */ |
| 22259 |
| 22260 |
| 22261 /* |
| 22262 ** Target size for allocation chunks. |
| 22263 */ |
| 22264 #define ROWSET_ALLOCATION_SIZE 1024 |
| 22265 |
| 22266 /* |
| 22267 ** The number of rowset entries per allocation chunk. |
| 22268 */ |
| 22269 #define ROWSET_ENTRY_PER_CHUNK \ |
| 22270 ((ROWSET_ALLOCATION_SIZE-8)/sizeof(struct RowSetEntry)) |
| 22271 |
| 22272 /* |
| 22273 ** Each entry in a RowSet is an instance of the following object. |
| 22274 ** |
| 22275 ** This same object is reused to store a linked list of trees of RowSetEntry |
| 22276 ** objects. In that alternative use, pRight points to the next entry |
| 22277 ** in the list, pLeft points to the tree, and v is unused. The |
| 22278 ** RowSet.pForest value points to the head of this forest list. |
| 22279 */ |
| 22280 struct RowSetEntry { |
| 22281 i64 v; /* ROWID value for this entry */ |
| 22282 struct RowSetEntry *pRight; /* Right subtree (larger entries) or list */ |
| 22283 struct RowSetEntry *pLeft; /* Left subtree (smaller entries) */ |
| 22284 }; |
| 22285 |
| 22286 /* |
| 22287 ** RowSetEntry objects are allocated in large chunks (instances of the |
| 22288 ** following structure) to reduce memory allocation overhead. The |
| 22289 ** chunks are kept on a linked list so that they can be deallocated |
| 22290 ** when the RowSet is destroyed. |
| 22291 */ |
| 22292 struct RowSetChunk { |
| 22293 struct RowSetChunk *pNextChunk; /* Next chunk on list of them all */ |
| 22294 struct RowSetEntry aEntry[ROWSET_ENTRY_PER_CHUNK]; /* Allocated entries */ |
| 22295 }; |
| 22296 |
| 22297 /* |
| 22298 ** A RowSet in an instance of the following structure. |
| 22299 ** |
| 22300 ** A typedef of this structure if found in sqliteInt.h. |
| 22301 */ |
| 22302 struct RowSet { |
| 22303 struct RowSetChunk *pChunk; /* List of all chunk allocations */ |
| 22304 sqlite3 *db; /* The database connection */ |
| 22305 struct RowSetEntry *pEntry; /* List of entries using pRight */ |
| 22306 struct RowSetEntry *pLast; /* Last entry on the pEntry list */ |
| 22307 struct RowSetEntry *pFresh; /* Source of new entry objects */ |
| 22308 struct RowSetEntry *pForest; /* List of binary trees of entries */ |
| 22309 u16 nFresh; /* Number of objects on pFresh */ |
| 22310 u16 rsFlags; /* Various flags */ |
| 22311 int iBatch; /* Current insert batch */ |
| 22312 }; |
| 22313 |
| 22314 /* |
| 22315 ** Allowed values for RowSet.rsFlags |
| 22316 */ |
| 22317 #define ROWSET_SORTED 0x01 /* True if RowSet.pEntry is sorted */ |
| 22318 #define ROWSET_NEXT 0x02 /* True if sqlite3RowSetNext() has been called */ |
| 22319 |
| 22320 /* |
| 22321 ** Turn bulk memory into a RowSet object. N bytes of memory |
| 22322 ** are available at pSpace. The db pointer is used as a memory context |
| 22323 ** for any subsequent allocations that need to occur. |
| 22324 ** Return a pointer to the new RowSet object. |
| 22325 ** |
| 22326 ** It must be the case that N is sufficient to make a Rowset. If not |
| 22327 ** an assertion fault occurs. |
| 22328 ** |
| 22329 ** If N is larger than the minimum, use the surplus as an initial |
| 22330 ** allocation of entries available to be filled. |
| 22331 */ |
| 22332 SQLITE_PRIVATE RowSet *sqlite3RowSetInit(sqlite3 *db, void *pSpace, unsigned int
N){ |
| 22333 RowSet *p; |
| 22334 assert( N >= ROUND8(sizeof(*p)) ); |
| 22335 p = pSpace; |
| 22336 p->pChunk = 0; |
| 22337 p->db = db; |
| 22338 p->pEntry = 0; |
| 22339 p->pLast = 0; |
| 22340 p->pForest = 0; |
| 22341 p->pFresh = (struct RowSetEntry*)(ROUND8(sizeof(*p)) + (char*)p); |
| 22342 p->nFresh = (u16)((N - ROUND8(sizeof(*p)))/sizeof(struct RowSetEntry)); |
| 22343 p->rsFlags = ROWSET_SORTED; |
| 22344 p->iBatch = 0; |
| 22345 return p; |
| 22346 } |
| 22347 |
| 22348 /* |
| 22349 ** Deallocate all chunks from a RowSet. This frees all memory that |
| 22350 ** the RowSet has allocated over its lifetime. This routine is |
| 22351 ** the destructor for the RowSet. |
| 22352 */ |
| 22353 SQLITE_PRIVATE void sqlite3RowSetClear(RowSet *p){ |
| 22354 struct RowSetChunk *pChunk, *pNextChunk; |
| 22355 for(pChunk=p->pChunk; pChunk; pChunk = pNextChunk){ |
| 22356 pNextChunk = pChunk->pNextChunk; |
| 22357 sqlite3DbFree(p->db, pChunk); |
| 22358 } |
| 22359 p->pChunk = 0; |
| 22360 p->nFresh = 0; |
| 22361 p->pEntry = 0; |
| 22362 p->pLast = 0; |
| 22363 p->pForest = 0; |
| 22364 p->rsFlags = ROWSET_SORTED; |
| 22365 } |
| 22366 |
| 22367 /* |
| 22368 ** Allocate a new RowSetEntry object that is associated with the |
| 22369 ** given RowSet. Return a pointer to the new and completely uninitialized |
| 22370 ** objected. |
| 22371 ** |
| 22372 ** In an OOM situation, the RowSet.db->mallocFailed flag is set and this |
| 22373 ** routine returns NULL. |
| 22374 */ |
| 22375 static struct RowSetEntry *rowSetEntryAlloc(RowSet *p){ |
| 22376 assert( p!=0 ); |
| 22377 if( p->nFresh==0 ){ |
| 22378 struct RowSetChunk *pNew; |
| 22379 pNew = sqlite3DbMallocRaw(p->db, sizeof(*pNew)); |
| 22380 if( pNew==0 ){ |
| 22381 return 0; |
| 22382 } |
| 22383 pNew->pNextChunk = p->pChunk; |
| 22384 p->pChunk = pNew; |
| 22385 p->pFresh = pNew->aEntry; |
| 22386 p->nFresh = ROWSET_ENTRY_PER_CHUNK; |
| 22387 } |
| 22388 p->nFresh--; |
| 22389 return p->pFresh++; |
| 22390 } |
| 22391 |
| 22392 /* |
| 22393 ** Insert a new value into a RowSet. |
| 22394 ** |
| 22395 ** The mallocFailed flag of the database connection is set if a |
| 22396 ** memory allocation fails. |
| 22397 */ |
| 22398 SQLITE_PRIVATE void sqlite3RowSetInsert(RowSet *p, i64 rowid){ |
| 22399 struct RowSetEntry *pEntry; /* The new entry */ |
| 22400 struct RowSetEntry *pLast; /* The last prior entry */ |
| 22401 |
| 22402 /* This routine is never called after sqlite3RowSetNext() */ |
| 22403 assert( p!=0 && (p->rsFlags & ROWSET_NEXT)==0 ); |
| 22404 |
| 22405 pEntry = rowSetEntryAlloc(p); |
| 22406 if( pEntry==0 ) return; |
| 22407 pEntry->v = rowid; |
| 22408 pEntry->pRight = 0; |
| 22409 pLast = p->pLast; |
| 22410 if( pLast ){ |
| 22411 if( (p->rsFlags & ROWSET_SORTED)!=0 && rowid<=pLast->v ){ |
| 22412 p->rsFlags &= ~ROWSET_SORTED; |
| 22413 } |
| 22414 pLast->pRight = pEntry; |
| 22415 }else{ |
| 22416 p->pEntry = pEntry; |
| 22417 } |
| 22418 p->pLast = pEntry; |
| 22419 } |
| 22420 |
| 22421 /* |
| 22422 ** Merge two lists of RowSetEntry objects. Remove duplicates. |
| 22423 ** |
| 22424 ** The input lists are connected via pRight pointers and are |
| 22425 ** assumed to each already be in sorted order. |
| 22426 */ |
| 22427 static struct RowSetEntry *rowSetEntryMerge( |
| 22428 struct RowSetEntry *pA, /* First sorted list to be merged */ |
| 22429 struct RowSetEntry *pB /* Second sorted list to be merged */ |
| 22430 ){ |
| 22431 struct RowSetEntry head; |
| 22432 struct RowSetEntry *pTail; |
| 22433 |
| 22434 pTail = &head; |
| 22435 while( pA && pB ){ |
| 22436 assert( pA->pRight==0 || pA->v<=pA->pRight->v ); |
| 22437 assert( pB->pRight==0 || pB->v<=pB->pRight->v ); |
| 22438 if( pA->v<pB->v ){ |
| 22439 pTail->pRight = pA; |
| 22440 pA = pA->pRight; |
| 22441 pTail = pTail->pRight; |
| 22442 }else if( pB->v<pA->v ){ |
| 22443 pTail->pRight = pB; |
| 22444 pB = pB->pRight; |
| 22445 pTail = pTail->pRight; |
| 22446 }else{ |
| 22447 pA = pA->pRight; |
| 22448 } |
| 22449 } |
| 22450 if( pA ){ |
| 22451 assert( pA->pRight==0 || pA->v<=pA->pRight->v ); |
| 22452 pTail->pRight = pA; |
| 22453 }else{ |
| 22454 assert( pB==0 || pB->pRight==0 || pB->v<=pB->pRight->v ); |
| 22455 pTail->pRight = pB; |
| 22456 } |
| 22457 return head.pRight; |
| 22458 } |
| 22459 |
| 22460 /* |
| 22461 ** Sort all elements on the list of RowSetEntry objects into order of |
| 22462 ** increasing v. |
| 22463 */ |
| 22464 static struct RowSetEntry *rowSetEntrySort(struct RowSetEntry *pIn){ |
| 22465 unsigned int i; |
| 22466 struct RowSetEntry *pNext, *aBucket[40]; |
| 22467 |
| 22468 memset(aBucket, 0, sizeof(aBucket)); |
| 22469 while( pIn ){ |
| 22470 pNext = pIn->pRight; |
| 22471 pIn->pRight = 0; |
| 22472 for(i=0; aBucket[i]; i++){ |
| 22473 pIn = rowSetEntryMerge(aBucket[i], pIn); |
| 22474 aBucket[i] = 0; |
| 22475 } |
| 22476 aBucket[i] = pIn; |
| 22477 pIn = pNext; |
| 22478 } |
| 22479 pIn = 0; |
| 22480 for(i=0; i<sizeof(aBucket)/sizeof(aBucket[0]); i++){ |
| 22481 pIn = rowSetEntryMerge(pIn, aBucket[i]); |
| 22482 } |
| 22483 return pIn; |
| 22484 } |
| 22485 |
| 22486 |
| 22487 /* |
| 22488 ** The input, pIn, is a binary tree (or subtree) of RowSetEntry objects. |
| 22489 ** Convert this tree into a linked list connected by the pRight pointers |
| 22490 ** and return pointers to the first and last elements of the new list. |
| 22491 */ |
| 22492 static void rowSetTreeToList( |
| 22493 struct RowSetEntry *pIn, /* Root of the input tree */ |
| 22494 struct RowSetEntry **ppFirst, /* Write head of the output list here */ |
| 22495 struct RowSetEntry **ppLast /* Write tail of the output list here */ |
| 22496 ){ |
| 22497 assert( pIn!=0 ); |
| 22498 if( pIn->pLeft ){ |
| 22499 struct RowSetEntry *p; |
| 22500 rowSetTreeToList(pIn->pLeft, ppFirst, &p); |
| 22501 p->pRight = pIn; |
| 22502 }else{ |
| 22503 *ppFirst = pIn; |
| 22504 } |
| 22505 if( pIn->pRight ){ |
| 22506 rowSetTreeToList(pIn->pRight, &pIn->pRight, ppLast); |
| 22507 }else{ |
| 22508 *ppLast = pIn; |
| 22509 } |
| 22510 assert( (*ppLast)->pRight==0 ); |
| 22511 } |
| 22512 |
| 22513 |
| 22514 /* |
| 22515 ** Convert a sorted list of elements (connected by pRight) into a binary |
| 22516 ** tree with depth of iDepth. A depth of 1 means the tree contains a single |
| 22517 ** node taken from the head of *ppList. A depth of 2 means a tree with |
| 22518 ** three nodes. And so forth. |
| 22519 ** |
| 22520 ** Use as many entries from the input list as required and update the |
| 22521 ** *ppList to point to the unused elements of the list. If the input |
| 22522 ** list contains too few elements, then construct an incomplete tree |
| 22523 ** and leave *ppList set to NULL. |
| 22524 ** |
| 22525 ** Return a pointer to the root of the constructed binary tree. |
| 22526 */ |
| 22527 static struct RowSetEntry *rowSetNDeepTree( |
| 22528 struct RowSetEntry **ppList, |
| 22529 int iDepth |
| 22530 ){ |
| 22531 struct RowSetEntry *p; /* Root of the new tree */ |
| 22532 struct RowSetEntry *pLeft; /* Left subtree */ |
| 22533 if( *ppList==0 ){ |
| 22534 return 0; |
| 22535 } |
| 22536 if( iDepth==1 ){ |
| 22537 p = *ppList; |
| 22538 *ppList = p->pRight; |
| 22539 p->pLeft = p->pRight = 0; |
| 22540 return p; |
| 22541 } |
| 22542 pLeft = rowSetNDeepTree(ppList, iDepth-1); |
| 22543 p = *ppList; |
| 22544 if( p==0 ){ |
| 22545 return pLeft; |
| 22546 } |
| 22547 p->pLeft = pLeft; |
| 22548 *ppList = p->pRight; |
| 22549 p->pRight = rowSetNDeepTree(ppList, iDepth-1); |
| 22550 return p; |
| 22551 } |
| 22552 |
| 22553 /* |
| 22554 ** Convert a sorted list of elements into a binary tree. Make the tree |
| 22555 ** as deep as it needs to be in order to contain the entire list. |
| 22556 */ |
| 22557 static struct RowSetEntry *rowSetListToTree(struct RowSetEntry *pList){ |
| 22558 int iDepth; /* Depth of the tree so far */ |
| 22559 struct RowSetEntry *p; /* Current tree root */ |
| 22560 struct RowSetEntry *pLeft; /* Left subtree */ |
| 22561 |
| 22562 assert( pList!=0 ); |
| 22563 p = pList; |
| 22564 pList = p->pRight; |
| 22565 p->pLeft = p->pRight = 0; |
| 22566 for(iDepth=1; pList; iDepth++){ |
| 22567 pLeft = p; |
| 22568 p = pList; |
| 22569 pList = p->pRight; |
| 22570 p->pLeft = pLeft; |
| 22571 p->pRight = rowSetNDeepTree(&pList, iDepth); |
| 22572 } |
| 22573 return p; |
| 22574 } |
| 22575 |
| 22576 /* |
| 22577 ** Take all the entries on p->pEntry and on the trees in p->pForest and |
| 22578 ** sort them all together into one big ordered list on p->pEntry. |
| 22579 ** |
| 22580 ** This routine should only be called once in the life of a RowSet. |
| 22581 */ |
| 22582 static void rowSetToList(RowSet *p){ |
| 22583 |
| 22584 /* This routine is called only once */ |
| 22585 assert( p!=0 && (p->rsFlags & ROWSET_NEXT)==0 ); |
| 22586 |
| 22587 if( (p->rsFlags & ROWSET_SORTED)==0 ){ |
| 22588 p->pEntry = rowSetEntrySort(p->pEntry); |
| 22589 } |
| 22590 |
| 22591 /* While this module could theoretically support it, sqlite3RowSetNext() |
| 22592 ** is never called after sqlite3RowSetText() for the same RowSet. So |
| 22593 ** there is never a forest to deal with. Should this change, simply |
| 22594 ** remove the assert() and the #if 0. */ |
| 22595 assert( p->pForest==0 ); |
| 22596 #if 0 |
| 22597 while( p->pForest ){ |
| 22598 struct RowSetEntry *pTree = p->pForest->pLeft; |
| 22599 if( pTree ){ |
| 22600 struct RowSetEntry *pHead, *pTail; |
| 22601 rowSetTreeToList(pTree, &pHead, &pTail); |
| 22602 p->pEntry = rowSetEntryMerge(p->pEntry, pHead); |
| 22603 } |
| 22604 p->pForest = p->pForest->pRight; |
| 22605 } |
| 22606 #endif |
| 22607 p->rsFlags |= ROWSET_NEXT; /* Verify this routine is never called again */ |
| 22608 } |
| 22609 |
| 22610 /* |
| 22611 ** Extract the smallest element from the RowSet. |
| 22612 ** Write the element into *pRowid. Return 1 on success. Return |
| 22613 ** 0 if the RowSet is already empty. |
| 22614 ** |
| 22615 ** After this routine has been called, the sqlite3RowSetInsert() |
| 22616 ** routine may not be called again. |
| 22617 */ |
| 22618 SQLITE_PRIVATE int sqlite3RowSetNext(RowSet *p, i64 *pRowid){ |
| 22619 assert( p!=0 ); |
| 22620 |
| 22621 /* Merge the forest into a single sorted list on first call */ |
| 22622 if( (p->rsFlags & ROWSET_NEXT)==0 ) rowSetToList(p); |
| 22623 |
| 22624 /* Return the next entry on the list */ |
| 22625 if( p->pEntry ){ |
| 22626 *pRowid = p->pEntry->v; |
| 22627 p->pEntry = p->pEntry->pRight; |
| 22628 if( p->pEntry==0 ){ |
| 22629 sqlite3RowSetClear(p); |
| 22630 } |
| 22631 return 1; |
| 22632 }else{ |
| 22633 return 0; |
| 22634 } |
| 22635 } |
| 22636 |
| 22637 /* |
| 22638 ** Check to see if element iRowid was inserted into the rowset as |
| 22639 ** part of any insert batch prior to iBatch. Return 1 or 0. |
| 22640 ** |
| 22641 ** If this is the first test of a new batch and if there exist entries |
| 22642 ** on pRowSet->pEntry, then sort those entries into the forest at |
| 22643 ** pRowSet->pForest so that they can be tested. |
| 22644 */ |
| 22645 SQLITE_PRIVATE int sqlite3RowSetTest(RowSet *pRowSet, int iBatch, sqlite3_int64
iRowid){ |
| 22646 struct RowSetEntry *p, *pTree; |
| 22647 |
| 22648 /* This routine is never called after sqlite3RowSetNext() */ |
| 22649 assert( pRowSet!=0 && (pRowSet->rsFlags & ROWSET_NEXT)==0 ); |
| 22650 |
| 22651 /* Sort entries into the forest on the first test of a new batch |
| 22652 */ |
| 22653 if( iBatch!=pRowSet->iBatch ){ |
| 22654 p = pRowSet->pEntry; |
| 22655 if( p ){ |
| 22656 struct RowSetEntry **ppPrevTree = &pRowSet->pForest; |
| 22657 if( (pRowSet->rsFlags & ROWSET_SORTED)==0 ){ |
| 22658 p = rowSetEntrySort(p); |
| 22659 } |
| 22660 for(pTree = pRowSet->pForest; pTree; pTree=pTree->pRight){ |
| 22661 ppPrevTree = &pTree->pRight; |
| 22662 if( pTree->pLeft==0 ){ |
| 22663 pTree->pLeft = rowSetListToTree(p); |
| 22664 break; |
| 22665 }else{ |
| 22666 struct RowSetEntry *pAux, *pTail; |
| 22667 rowSetTreeToList(pTree->pLeft, &pAux, &pTail); |
| 22668 pTree->pLeft = 0; |
| 22669 p = rowSetEntryMerge(pAux, p); |
| 22670 } |
| 22671 } |
| 22672 if( pTree==0 ){ |
| 22673 *ppPrevTree = pTree = rowSetEntryAlloc(pRowSet); |
| 22674 if( pTree ){ |
| 22675 pTree->v = 0; |
| 22676 pTree->pRight = 0; |
| 22677 pTree->pLeft = rowSetListToTree(p); |
| 22678 } |
| 22679 } |
| 22680 pRowSet->pEntry = 0; |
| 22681 pRowSet->pLast = 0; |
| 22682 pRowSet->rsFlags |= ROWSET_SORTED; |
| 22683 } |
| 22684 pRowSet->iBatch = iBatch; |
| 22685 } |
| 22686 |
| 22687 /* Test to see if the iRowid value appears anywhere in the forest. |
| 22688 ** Return 1 if it does and 0 if not. |
| 22689 */ |
| 22690 for(pTree = pRowSet->pForest; pTree; pTree=pTree->pRight){ |
| 22691 p = pTree->pLeft; |
| 22692 while( p ){ |
| 22693 if( p->v<iRowid ){ |
| 22694 p = p->pRight; |
| 22695 }else if( p->v>iRowid ){ |
| 22696 p = p->pLeft; |
| 22697 }else{ |
| 22698 return 1; |
| 22699 } |
| 22700 } |
| 22701 } |
| 22702 return 0; |
| 22703 } |
| 22704 |
| 22705 /************** End of rowset.c **********************************************/ |
| 22706 |
| 22707 /* Chain include. */ |
| 22708 #include "sqlite3.02.c" |
OLD | NEW |