1 /* 2 ** 2004 May 22 3 ** 4 ** The author disclaims copyright to this source code. In place of 5 ** a legal notice, here is a blessing: 6 ** 7 ** May you do good and not evil. 8 ** May you find forgiveness for yourself and forgive others. 9 ** May you share freely, never taking more than you give. 10 ** 11 ****************************************************************************** 12 ** 13 ** This file contains code that modified the OS layer in order to simulate 14 ** the effect on the database file of an OS crash or power failure. This 15 ** is used to test the ability of SQLite to recover from those situations. 16 */ 17 #if SQLITE_TEST /* This file is used for testing only */ 18 #include "sqliteInt.h" 19 #include "tcl.h" 20 21 #ifndef SQLITE_OMIT_DISKIO /* This file is a no-op if disk I/O is disabled */ 22 23 /* #define TRACE_CRASHTEST */ 24 25 typedef struct CrashFile CrashFile; 26 typedef struct CrashGlobal CrashGlobal; 27 typedef struct WriteBuffer WriteBuffer; 28 29 /* 30 ** Method: 31 ** 32 ** This layer is implemented as a wrapper around the "real" 33 ** sqlite3_file object for the host system. Each time data is 34 ** written to the file object, instead of being written to the 35 ** underlying file, the write operation is stored in an in-memory 36 ** structure (type WriteBuffer). This structure is placed at the 37 ** end of a global ordered list (the write-list). 38 ** 39 ** When data is read from a file object, the requested region is 40 ** first retrieved from the real file. The write-list is then 41 ** traversed and data copied from any overlapping WriteBuffer 42 ** structures to the output buffer. i.e. a read() operation following 43 ** one or more write() operations works as expected, even if no 44 ** data has actually been written out to the real file. 45 ** 46 ** When a fsync() operation is performed, an operating system crash 47 ** may be simulated, in which case exit(-1) is called (the call to 48 ** xSync() never returns). Whether or not a crash is simulated, 49 ** the data associated with a subset of the WriteBuffer structures 50 ** stored in the write-list is written to the real underlying files 51 ** and the entries removed from the write-list. If a crash is simulated, 52 ** a subset of the buffers may be corrupted before the data is written. 53 ** 54 ** The exact subset of the write-list written and/or corrupted is 55 ** determined by the simulated device characteristics and sector-size. 56 ** 57 ** "Normal" mode: 58 ** 59 ** Normal mode is used when the simulated device has none of the 60 ** SQLITE_IOCAP_XXX flags set. 61 ** 62 ** In normal mode, if the fsync() is not a simulated crash, the 63 ** write-list is traversed from beginning to end. Each WriteBuffer 64 ** structure associated with the file handle used to call xSync() 65 ** is written to the real file and removed from the write-list. 66 ** 67 ** If a crash is simulated, one of the following takes place for 68 ** each WriteBuffer in the write-list, regardless of which 69 ** file-handle it is associated with: 70 ** 71 ** 1. The buffer is correctly written to the file, just as if 72 ** a crash were not being simulated. 73 ** 74 ** 2. Nothing is done. 75 ** 76 ** 3. Garbage data is written to all sectors of the file that 77 ** overlap the region specified by the WriteBuffer. Or garbage 78 ** data is written to some contiguous section within the 79 ** overlapped sectors. 80 ** 81 ** Device Characteristic flag handling: 82 ** 83 ** If the IOCAP_ATOMIC flag is set, then option (3) above is 84 ** never selected. 85 ** 86 ** If the IOCAP_ATOMIC512 flag is set, and the WriteBuffer represents 87 ** an aligned write() of an integer number of 512 byte regions, then 88 ** option (3) above is never selected. Instead, each 512 byte region 89 ** is either correctly written or left completely untouched. Similar 90 ** logic governs the behaviour if any of the other ATOMICXXX flags 91 ** is set. 92 ** 93 ** If either the IOCAP_SAFEAPPEND or IOCAP_SEQUENTIAL flags are set 94 ** and a crash is being simulated, then an entry of the write-list is 95 ** selected at random. Everything in the list after the selected entry 96 ** is discarded before processing begins. 97 ** 98 ** If IOCAP_SEQUENTIAL is set and a crash is being simulated, option 99 ** (1) is selected for all write-list entries except the last. If a 100 ** crash is not being simulated, then all entries in the write-list 101 ** that occur before at least one write() on the file-handle specified 102 ** as part of the xSync() are written to their associated real files. 103 ** 104 ** If IOCAP_SAFEAPPEND is set and the first byte written by the write() 105 ** operation is one byte past the current end of the file, then option 106 ** (1) is always selected. 107 */ 108 109 /* 110 ** Each write operation in the write-list is represented by an instance 111 ** of the following structure. 112 ** 113 ** If zBuf is 0, then this structure represents a call to xTruncate(), 114 ** not xWrite(). In that case, iOffset is the size that the file is 115 ** truncated to. 116 */ 117 struct WriteBuffer { 118 i64 iOffset; /* Byte offset of the start of this write() */ 119 int nBuf; /* Number of bytes written */ 120 u8 *zBuf; /* Pointer to copy of written data */ 121 CrashFile *pFile; /* File this write() applies to */ 122 123 WriteBuffer *pNext; /* Next in CrashGlobal.pWriteList */ 124 }; 125 126 struct CrashFile { 127 const sqlite3_io_methods *pMethod; /* Must be first */ 128 sqlite3_file *pRealFile; /* Underlying "real" file handle */ 129 char *zName; 130 131 /* Cache of the entire file. This is used to speed up OsRead() and 132 ** OsFileSize() calls. Although both could be done by traversing the 133 ** write-list, in practice this is impractically slow. 134 */ 135 int iSize; /* Size of file in bytes */ 136 int nData; /* Size of buffer allocated at zData */ 137 u8 *zData; /* Buffer containing file contents */ 138 }; 139 140 struct CrashGlobal { 141 WriteBuffer *pWriteList; /* Head of write-list */ 142 WriteBuffer *pWriteListEnd; /* End of write-list */ 143 144 int iSectorSize; /* Value of simulated sector size */ 145 int iDeviceCharacteristics; /* Value of simulated device characteristics */ 146 147 int iCrash; /* Crash on the iCrash'th call to xSync() */ 148 char zCrashFile[500]; /* Crash during an xSync() on this file */ 149 }; 150 151 static CrashGlobal g = {0, 0, SQLITE_DEFAULT_SECTOR_SIZE, 0, 0}; 152 153 /* 154 ** Set this global variable to 1 to enable crash testing. 155 */ 156 static int sqlite3CrashTestEnable = 0; 157 158 /* 159 ** Flush the write-list as if xSync() had been called on file handle 160 ** pFile. If isCrash is true, simulate a crash. 161 */ 162 static int writeListSync(CrashFile *pFile, int isCrash){ 163 int rc = SQLITE_OK; 164 int iDc = g.iDeviceCharacteristics; 165 166 WriteBuffer *pWrite; 167 WriteBuffer **ppPtr; 168 169 /* If this is not a crash simulation, set pFinal to point to the 170 ** last element of the write-list that is associated with file handle 171 ** pFile. 172 ** 173 ** If this is a crash simulation, set pFinal to an arbitrarily selected 174 ** element of the write-list. 175 */ 176 WriteBuffer *pFinal = 0; 177 if( !isCrash ){ 178 for(pWrite=g.pWriteList; pWrite; pWrite=pWrite->pNext){ 179 if( pWrite->pFile==pFile ){ 180 pFinal = pWrite; 181 } 182 } 183 }else if( iDc&(SQLITE_IOCAP_SEQUENTIAL|SQLITE_IOCAP_SAFE_APPEND) ){ 184 int nWrite = 0; 185 int iFinal; 186 for(pWrite=g.pWriteList; pWrite; pWrite=pWrite->pNext) nWrite++; 187 sqlite3Randomness(sizeof(int), &iFinal); 188 iFinal = ((iFinal<0)?-1*iFinal:iFinal)%nWrite; 189 for(pWrite=g.pWriteList; iFinal>0; pWrite=pWrite->pNext) iFinal--; 190 pFinal = pWrite; 191 } 192 193 #ifdef TRACE_CRASHTEST 194 printf("Sync %s (is %s crash)\n", pFile->zName, (isCrash?"a":"not a")); 195 #endif 196 197 ppPtr = &g.pWriteList; 198 for(pWrite=*ppPtr; rc==SQLITE_OK && pWrite; pWrite=*ppPtr){ 199 sqlite3_file *pRealFile = pWrite->pFile->pRealFile; 200 201 /* (eAction==1) -> write block out normally, 202 ** (eAction==2) -> do nothing, 203 ** (eAction==3) -> trash sectors. 204 */ 205 int eAction = 0; 206 if( !isCrash ){ 207 eAction = 2; 208 if( (pWrite->pFile==pFile || iDc&SQLITE_IOCAP_SEQUENTIAL) ){ 209 eAction = 1; 210 } 211 }else{ 212 char random; 213 sqlite3Randomness(1, &random); 214 215 /* Do not select option 3 (sector trashing) if the IOCAP_ATOMIC flag 216 ** is set or this is an OsTruncate(), not an Oswrite(). 217 */ 218 if( (iDc&SQLITE_IOCAP_ATOMIC) || (pWrite->zBuf==0) ){ 219 random &= 0x01; 220 } 221 222 /* If IOCAP_SEQUENTIAL is set and this is not the final entry 223 ** in the truncated write-list, always select option 1 (write 224 ** out correctly). 225 */ 226 if( (iDc&SQLITE_IOCAP_SEQUENTIAL && pWrite!=pFinal) ){ 227 random = 0; 228 } 229 230 /* If IOCAP_SAFE_APPEND is set and this OsWrite() operation is 231 ** an append (first byte of the written region is 1 byte past the 232 ** current EOF), always select option 1 (write out correctly). 233 */ 234 if( iDc&SQLITE_IOCAP_SAFE_APPEND && pWrite->zBuf ){ 235 i64 iSize; 236 sqlite3OsFileSize(pRealFile, &iSize); 237 if( iSize==pWrite->iOffset ){ 238 random = 0; 239 } 240 } 241 242 if( (random&0x06)==0x06 ){ 243 eAction = 3; 244 }else{ 245 eAction = ((random&0x01)?2:1); 246 } 247 } 248 249 switch( eAction ){ 250 case 1: { /* Write out correctly */ 251 if( pWrite->zBuf ){ 252 rc = sqlite3OsWrite( 253 pRealFile, pWrite->zBuf, pWrite->nBuf, pWrite->iOffset 254 ); 255 }else{ 256 rc = sqlite3OsTruncate(pRealFile, pWrite->iOffset); 257 } 258 *ppPtr = pWrite->pNext; 259 #ifdef TRACE_CRASHTEST 260 if( isCrash ){ 261 printf("Writing %d bytes @ %d (%s)\n", 262 pWrite->nBuf, (int)pWrite->iOffset, pWrite->pFile->zName 263 ); 264 } 265 #endif 266 sqlite3_free(pWrite); 267 break; 268 } 269 case 2: { /* Do nothing */ 270 ppPtr = &pWrite->pNext; 271 #ifdef TRACE_CRASHTEST 272 if( isCrash ){ 273 printf("Omiting %d bytes @ %d (%s)\n", 274 pWrite->nBuf, (int)pWrite->iOffset, pWrite->pFile->zName 275 ); 276 } 277 #endif 278 break; 279 } 280 case 3: { /* Trash sectors */ 281 u8 *zGarbage; 282 int iFirst = (pWrite->iOffset/g.iSectorSize); 283 int iLast = (pWrite->iOffset+pWrite->nBuf-1)/g.iSectorSize; 284 285 assert(pWrite->zBuf); 286 287 #ifdef TRACE_CRASHTEST 288 printf("Trashing %d sectors @ sector %d (%s)\n", 289 1+iLast-iFirst, iFirst, pWrite->pFile->zName 290 ); 291 #endif 292 293 zGarbage = sqlite3_malloc(g.iSectorSize); 294 if( zGarbage ){ 295 sqlite3_int64 i; 296 for(i=iFirst; rc==SQLITE_OK && i<=iLast; i++){ 297 sqlite3Randomness(g.iSectorSize, zGarbage); 298 rc = sqlite3OsWrite( 299 pRealFile, zGarbage, g.iSectorSize, i*g.iSectorSize 300 ); 301 } 302 sqlite3_free(zGarbage); 303 }else{ 304 rc = SQLITE_NOMEM; 305 } 306 307 ppPtr = &pWrite->pNext; 308 break; 309 } 310 311 default: 312 assert(!"Cannot happen"); 313 } 314 315 if( pWrite==pFinal ) break; 316 } 317 318 if( rc==SQLITE_OK && isCrash ){ 319 exit(-1); 320 } 321 322 for(pWrite=g.pWriteList; pWrite && pWrite->pNext; pWrite=pWrite->pNext); 323 g.pWriteListEnd = pWrite; 324 325 return rc; 326 } 327 328 /* 329 ** Add an entry to the end of the write-list. 330 */ 331 static int writeListAppend( 332 sqlite3_file *pFile, 333 sqlite3_int64 iOffset, 334 const u8 *zBuf, 335 int nBuf 336 ){ 337 WriteBuffer *pNew; 338 339 assert((zBuf && nBuf) || (!nBuf && !zBuf)); 340 341 pNew = (WriteBuffer *)sqlite3MallocZero(sizeof(WriteBuffer) + nBuf); 342 pNew->iOffset = iOffset; 343 pNew->nBuf = nBuf; 344 pNew->pFile = (CrashFile *)pFile; 345 if( zBuf ){ 346 pNew->zBuf = (u8 *)&pNew[1]; 347 memcpy(pNew->zBuf, zBuf, nBuf); 348 } 349 350 if( g.pWriteList ){ 351 assert(g.pWriteListEnd); 352 g.pWriteListEnd->pNext = pNew; 353 }else{ 354 g.pWriteList = pNew; 355 } 356 g.pWriteListEnd = pNew; 357 358 return SQLITE_OK; 359 } 360 361 /* 362 ** Close a crash-file. 363 */ 364 static int cfClose(sqlite3_file *pFile){ 365 CrashFile *pCrash = (CrashFile *)pFile; 366 writeListSync(pCrash, 0); 367 sqlite3OsClose(pCrash->pRealFile); 368 return SQLITE_OK; 369 } 370 371 /* 372 ** Read data from a crash-file. 373 */ 374 static int cfRead( 375 sqlite3_file *pFile, 376 void *zBuf, 377 int iAmt, 378 sqlite_int64 iOfst 379 ){ 380 CrashFile *pCrash = (CrashFile *)pFile; 381 382 /* Check the file-size to see if this is a short-read */ 383 if( pCrash->iSize<(iOfst+iAmt) ){ 384 return SQLITE_IOERR_SHORT_READ; 385 } 386 387 memcpy(zBuf, &pCrash->zData[iOfst], iAmt); 388 return SQLITE_OK; 389 } 390 391 /* 392 ** Write data to a crash-file. 393 */ 394 static int cfWrite( 395 sqlite3_file *pFile, 396 const void *zBuf, 397 int iAmt, 398 sqlite_int64 iOfst 399 ){ 400 CrashFile *pCrash = (CrashFile *)pFile; 401 if( iAmt+iOfst>pCrash->iSize ){ 402 pCrash->iSize = iAmt+iOfst; 403 } 404 while( pCrash->iSize>pCrash->nData ){ 405 u8 *zNew; 406 int nNew = (pCrash->nData*2) + 4096; 407 zNew = sqlite3_realloc(pCrash->zData, nNew); 408 if( !zNew ){ 409 return SQLITE_NOMEM; 410 } 411 memset(&zNew[pCrash->nData], 0, nNew-pCrash->nData); 412 pCrash->nData = nNew; 413 pCrash->zData = zNew; 414 } 415 memcpy(&pCrash->zData[iOfst], zBuf, iAmt); 416 return writeListAppend(pFile, iOfst, zBuf, iAmt); 417 } 418 419 /* 420 ** Truncate a crash-file. 421 */ 422 static int cfTruncate(sqlite3_file *pFile, sqlite_int64 size){ 423 CrashFile *pCrash = (CrashFile *)pFile; 424 assert(size>=0); 425 if( pCrash->iSize>size ){ 426 pCrash->iSize = size; 427 } 428 return writeListAppend(pFile, size, 0, 0); 429 } 430 431 /* 432 ** Sync a crash-file. 433 */ 434 static int cfSync(sqlite3_file *pFile, int flags){ 435 CrashFile *pCrash = (CrashFile *)pFile; 436 int isCrash = 0; 437 438 const char *zName = pCrash->zName; 439 const char *zCrashFile = g.zCrashFile; 440 int nName = strlen(zName); 441 int nCrashFile = strlen(zCrashFile); 442 443 if( nCrashFile>0 && zCrashFile[nCrashFile-1]=='*' ){ 444 nCrashFile--; 445 if( nName>nCrashFile ) nName = nCrashFile; 446 } 447 448 if( nName==nCrashFile && 0==memcmp(zName, zCrashFile, nName) ){ 449 if( (--g.iCrash)==0 ) isCrash = 1; 450 } 451 452 return writeListSync(pCrash, isCrash); 453 } 454 455 /* 456 ** Return the current file-size of the crash-file. 457 */ 458 static int cfFileSize(sqlite3_file *pFile, sqlite_int64 *pSize){ 459 CrashFile *pCrash = (CrashFile *)pFile; 460 *pSize = (i64)pCrash->iSize; 461 return SQLITE_OK; 462 } 463 464 /* 465 ** Calls related to file-locks are passed on to the real file handle. 466 */ 467 static int cfLock(sqlite3_file *pFile, int eLock){ 468 return sqlite3OsLock(((CrashFile *)pFile)->pRealFile, eLock); 469 } 470 static int cfUnlock(sqlite3_file *pFile, int eLock){ 471 return sqlite3OsUnlock(((CrashFile *)pFile)->pRealFile, eLock); 472 } 473 static int cfCheckReservedLock(sqlite3_file *pFile){ 474 return sqlite3OsCheckReservedLock(((CrashFile *)pFile)->pRealFile); 475 } 476 static int cfLockState(sqlite3_file *pFile){ 477 return sqlite3OsLockState(((CrashFile *)pFile)->pRealFile); 478 } 479 static int cfBreakLock(sqlite3_file *pFile){ 480 return sqlite3OsBreakLock(((CrashFile *)pFile)->pRealFile); 481 } 482 483 /* 484 ** The xSectorSize() and xDeviceCharacteristics() functions return 485 ** the global values configured by the [sqlite_crashparams] tcl 486 * interface. 487 */ 488 static int cfSectorSize(sqlite3_file *pFile){ 489 return g.iSectorSize; 490 } 491 static int cfDeviceCharacteristics(sqlite3_file *pFile){ 492 return g.iDeviceCharacteristics; 493 } 494 495 static const sqlite3_io_methods CrashFileVtab = { 496 1, /* iVersion */ 497 cfClose, /* xClose */ 498 cfRead, /* xRead */ 499 cfWrite, /* xWrite */ 500 cfTruncate, /* xTruncate */ 501 cfSync, /* xSync */ 502 cfFileSize, /* xFileSize */ 503 cfLock, /* xLock */ 504 cfUnlock, /* xUnlock */ 505 cfCheckReservedLock, /* xCheckReservedLock */ 506 cfBreakLock, /* xBreakLock */ 507 cfLockState, /* xLockState */ 508 cfSectorSize, /* xSectorSize */ 509 cfDeviceCharacteristics /* xDeviceCharacteristics */ 510 }; 511 512 /* 513 ** Application data for the crash VFS 514 */ 515 struct crashAppData { 516 sqlite3_vfs *pOrig; /* Wrapped vfs structure */ 517 }; 518 519 /* 520 ** Open a crash-file file handle. 521 ** 522 ** The caller will have allocated pVfs->szOsFile bytes of space 523 ** at pFile. This file uses this space for the CrashFile structure 524 ** and allocates space for the "real" file structure using 525 ** sqlite3_malloc(). The assumption here is (pVfs->szOsFile) is 526 ** equal or greater than sizeof(CrashFile). 527 */ 528 static int cfOpen( 529 sqlite3_vfs *pCfVfs, 530 const char *zName, 531 sqlite3_file *pFile, 532 int flags, 533 int *pOutFlags 534 ){ 535 sqlite3_vfs *pVfs = (sqlite3_vfs *)pCfVfs->pAppData; 536 int rc; 537 CrashFile *pWrapper = (CrashFile *)pFile; 538 sqlite3_file *pReal = (sqlite3_file*)&pWrapper[1]; 539 540 memset(pWrapper, 0, sizeof(CrashFile)); 541 rc = sqlite3OsOpen(pVfs, zName, pReal, flags, pOutFlags); 542 543 if( rc==SQLITE_OK ){ 544 i64 iSize; 545 pWrapper->pMethod = &CrashFileVtab; 546 pWrapper->zName = (char *)zName; 547 pWrapper->pRealFile = pReal; 548 rc = sqlite3OsFileSize(pReal, &iSize); 549 pWrapper->iSize = (int)iSize; 550 } 551 if( rc==SQLITE_OK ){ 552 pWrapper->nData = (4096 + pWrapper->iSize); 553 pWrapper->zData = sqlite3_malloc(pWrapper->nData); 554 if( pWrapper->zData ){ 555 memset(pWrapper->zData, 0, pWrapper->nData); 556 rc = sqlite3OsRead(pReal, pWrapper->zData, pWrapper->iSize, 0); 557 }else{ 558 rc = SQLITE_NOMEM; 559 } 560 } 561 if( rc!=SQLITE_OK && pWrapper->pMethod ){ 562 sqlite3OsClose(pFile); 563 } 564 return rc; 565 } 566 567 static int cfDelete(sqlite3_vfs *pCfVfs, const char *zPath, int dirSync){ 568 sqlite3_vfs *pVfs = (sqlite3_vfs *)pCfVfs->pAppData; 569 return pVfs->xDelete(pVfs, zPath, dirSync); 570 } 571 static int cfAccess(sqlite3_vfs *pCfVfs, const char *zPath, int flags){ 572 sqlite3_vfs *pVfs = (sqlite3_vfs *)pCfVfs->pAppData; 573 return pVfs->xAccess(pVfs, zPath, flags); 574 } 575 static int cfGetTempName(sqlite3_vfs *pCfVfs, char *zBufOut){ 576 sqlite3_vfs *pVfs = (sqlite3_vfs *)pCfVfs->pAppData; 577 return pVfs->xGetTempName(pVfs, zBufOut); 578 } 579 static int cfFullPathname(sqlite3_vfs *pCfVfs, const char *zPath, char *zPathOut){ 580 sqlite3_vfs *pVfs = (sqlite3_vfs *)pCfVfs->pAppData; 581 return pVfs->xFullPathname(pVfs, zPath, zPathOut); 582 } 583 static void *cfDlOpen(sqlite3_vfs *pCfVfs, const char *zPath){ 584 sqlite3_vfs *pVfs = (sqlite3_vfs *)pCfVfs->pAppData; 585 return pVfs->xDlOpen(pVfs, zPath); 586 } 587 static void cfDlError(sqlite3_vfs *pCfVfs, int nByte, char *zErrMsg){ 588 sqlite3_vfs *pVfs = (sqlite3_vfs *)pCfVfs->pAppData; 589 pVfs->xDlError(pVfs, nByte, zErrMsg); 590 } 591 static void *cfDlSym(sqlite3_vfs *pCfVfs, void *pHandle, const char *zSymbol){ 592 sqlite3_vfs *pVfs = (sqlite3_vfs *)pCfVfs->pAppData; 593 return pVfs->xDlSym(pVfs, pHandle, zSymbol); 594 } 595 static void cfDlClose(sqlite3_vfs *pCfVfs, void *pHandle){ 596 sqlite3_vfs *pVfs = (sqlite3_vfs *)pCfVfs->pAppData; 597 pVfs->xDlClose(pVfs, pHandle); 598 } 599 static int cfRandomness(sqlite3_vfs *pCfVfs, int nByte, char *zBufOut){ 600 sqlite3_vfs *pVfs = (sqlite3_vfs *)pCfVfs->pAppData; 601 return pVfs->xRandomness(pVfs, nByte, zBufOut); 602 } 603 static int cfSleep(sqlite3_vfs *pCfVfs, int nMicro){ 604 sqlite3_vfs *pVfs = (sqlite3_vfs *)pCfVfs->pAppData; 605 return pVfs->xSleep(pVfs, nMicro); 606 } 607 static int cfCurrentTime(sqlite3_vfs *pCfVfs, double *pTimeOut){ 608 sqlite3_vfs *pVfs = (sqlite3_vfs *)pCfVfs->pAppData; 609 return pVfs->xCurrentTime(pVfs, pTimeOut); 610 } 611 612 static int processDevSymArgs( 613 Tcl_Interp *interp, 614 int objc, 615 Tcl_Obj *CONST objv[], 616 int *piDeviceChar, 617 int *piSectorSize 618 ){ 619 struct DeviceFlag { 620 char *zName; 621 int iValue; 622 } aFlag[] = { 623 { "atomic", SQLITE_IOCAP_ATOMIC }, 624 { "atomic512", SQLITE_IOCAP_ATOMIC512 }, 625 { "atomic1k", SQLITE_IOCAP_ATOMIC1K }, 626 { "atomic2k", SQLITE_IOCAP_ATOMIC2K }, 627 { "atomic4k", SQLITE_IOCAP_ATOMIC4K }, 628 { "atomic8k", SQLITE_IOCAP_ATOMIC8K }, 629 { "atomic16k", SQLITE_IOCAP_ATOMIC16K }, 630 { "atomic32k", SQLITE_IOCAP_ATOMIC32K }, 631 { "atomic64k", SQLITE_IOCAP_ATOMIC64K }, 632 { "sequential", SQLITE_IOCAP_SEQUENTIAL }, 633 { "safe_append", SQLITE_IOCAP_SAFE_APPEND }, 634 { 0, 0 } 635 }; 636 637 int i; 638 int iDc = 0; 639 int iSectorSize = 0; 640 int setSectorsize = 0; 641 int setDeviceChar = 0; 642 643 for(i=0; i<objc; i+=2){ 644 int nOpt; 645 char *zOpt = Tcl_GetStringFromObj(objv[i], &nOpt); 646 647 if( (nOpt>11 || nOpt<2 || strncmp("-sectorsize", zOpt, nOpt)) 648 && (nOpt>16 || nOpt<2 || strncmp("-characteristics", zOpt, nOpt)) 649 ){ 650 Tcl_AppendResult(interp, 651 "Bad option: \"", zOpt, 652 "\" - must be \"-characteristics\" or \"-sectorsize\"", 0 653 ); 654 return TCL_ERROR; 655 } 656 if( i==objc-1 ){ 657 Tcl_AppendResult(interp, "Option requires an argument: \"", zOpt, "\"",0); 658 return TCL_ERROR; 659 } 660 661 if( zOpt[1]=='s' ){ 662 if( Tcl_GetIntFromObj(interp, objv[i+1], &iSectorSize) ){ 663 return TCL_ERROR; 664 } 665 setSectorsize = 1; 666 }else{ 667 int j; 668 Tcl_Obj **apObj; 669 int nObj; 670 if( Tcl_ListObjGetElements(interp, objv[i+1], &nObj, &apObj) ){ 671 return TCL_ERROR; 672 } 673 for(j=0; j<nObj; j++){ 674 int rc; 675 int iChoice; 676 Tcl_Obj *pFlag = Tcl_DuplicateObj(apObj[j]); 677 Tcl_IncrRefCount(pFlag); 678 Tcl_UtfToLower(Tcl_GetString(pFlag)); 679 680 rc = Tcl_GetIndexFromObjStruct( 681 interp, pFlag, aFlag, sizeof(aFlag[0]), "no such flag", 0, &iChoice 682 ); 683 Tcl_DecrRefCount(pFlag); 684 if( rc ){ 685 return TCL_ERROR; 686 } 687 688 iDc |= aFlag[iChoice].iValue; 689 } 690 setDeviceChar = 1; 691 } 692 } 693 694 if( setDeviceChar ){ 695 *piDeviceChar = iDc; 696 } 697 if( setSectorsize ){ 698 *piSectorSize = iSectorSize; 699 } 700 701 return TCL_OK; 702 } 703 704 /* 705 ** tclcmd: sqlite_crashparams ?OPTIONS? DELAY CRASHFILE 706 ** 707 ** This procedure implements a TCL command that enables crash testing 708 ** in testfixture. Once enabled, crash testing cannot be disabled. 709 ** 710 ** Available options are "-characteristics" and "-sectorsize". Both require 711 ** an argument. For -sectorsize, this is the simulated sector size in 712 ** bytes. For -characteristics, the argument must be a list of io-capability 713 ** flags to simulate. Valid flags are "atomic", "atomic512", "atomic1K", 714 ** "atomic2K", "atomic4K", "atomic8K", "atomic16K", "atomic32K", 715 ** "atomic64K", "sequential" and "safe_append". 716 ** 717 ** Example: 718 ** 719 ** sqlite_crashparams -sect 1024 -char {atomic sequential} ./test.db 1 720 ** 721 */ 722 static int crashParamsObjCmd( 723 void * clientData, 724 Tcl_Interp *interp, 725 int objc, 726 Tcl_Obj *CONST objv[] 727 ){ 728 int iDelay; 729 const char *zCrashFile; 730 int nCrashFile, iDc, iSectorSize; 731 732 static sqlite3_vfs crashVfs = { 733 1, /* iVersion */ 734 0, /* szOsFile */ 735 0, /* mxPathname */ 736 0, /* pNext */ 737 "crash", /* zName */ 738 0, /* pAppData */ 739 740 cfOpen, /* xOpen */ 741 cfDelete, /* xDelete */ 742 cfAccess, /* xAccess */ 743 cfGetTempName, /* xGetTempName */ 744 cfFullPathname, /* xFullPathname */ 745 cfDlOpen, /* xDlOpen */ 746 cfDlError, /* xDlError */ 747 cfDlSym, /* xDlSym */ 748 cfDlClose, /* xDlClose */ 749 cfRandomness, /* xRandomness */ 750 cfSleep, /* xSleep */ 751 cfCurrentTime /* xCurrentTime */ 752 }; 753 754 755 if( crashVfs.pAppData==0 ){ 756 sqlite3_vfs *pOriginalVfs = sqlite3_vfs_find(0); 757 crashVfs.mxPathname = pOriginalVfs->mxPathname; 758 crashVfs.pAppData = (void *)pOriginalVfs; 759 crashVfs.szOsFile = sizeof(CrashFile) + pOriginalVfs->szOsFile; 760 /* sqlite3_vfs_unregister(pOriginalVfs); */ 761 sqlite3_vfs_register(&crashVfs, 1); 762 } 763 764 iDc = -1; 765 iSectorSize = -1; 766 767 if( objc<3 ){ 768 Tcl_WrongNumArgs(interp, 1, objv, "?OPTIONS? DELAY CRASHFILE"); 769 goto error; 770 } 771 772 zCrashFile = Tcl_GetStringFromObj(objv[objc-1], &nCrashFile); 773 if( nCrashFile>=sizeof(g.zCrashFile) ){ 774 Tcl_AppendResult(interp, "Filename is too long: \"", zCrashFile, "\"", 0); 775 goto error; 776 } 777 if( Tcl_GetIntFromObj(interp, objv[objc-2], &iDelay) ){ 778 goto error; 779 } 780 781 if( processDevSymArgs(interp, objc-3, &objv[1], &iDc, &iSectorSize) ){ 782 return TCL_ERROR; 783 } 784 785 if( iDc>=0 ){ 786 g.iDeviceCharacteristics = iDc; 787 } 788 if( iSectorSize>=0 ){ 789 g.iSectorSize = iSectorSize; 790 } 791 792 g.iCrash = iDelay; 793 memcpy(g.zCrashFile, zCrashFile, nCrashFile+1); 794 sqlite3CrashTestEnable = 1; 795 return TCL_OK; 796 797 error: 798 return TCL_ERROR; 799 } 800 801 static int devSymObjCmd( 802 void * clientData, 803 Tcl_Interp *interp, 804 int objc, 805 Tcl_Obj *CONST objv[] 806 ){ 807 808 extern int sqlite3_test_device_characteristics; 809 extern int sqlite3_test_sector_size; 810 811 int iDc = -1; 812 int iSectorSize = -1; 813 if( processDevSymArgs(interp, objc-1, &objv[1], &iDc, &iSectorSize) ){ 814 return TCL_ERROR; 815 } 816 817 if( iDc>=0 ){ 818 sqlite3_test_device_characteristics = iDc; 819 } 820 if( iSectorSize>=0 ){ 821 sqlite3_test_sector_size = iSectorSize; 822 } 823 824 return TCL_OK; 825 } 826 827 #endif /* SQLITE_OMIT_DISKIO */ 828 829 /* 830 ** This procedure registers the TCL procedures defined in this file. 831 */ 832 int Sqlitetest6_Init(Tcl_Interp *interp){ 833 #ifndef SQLITE_OMIT_DISKIO 834 Tcl_CreateObjCommand(interp, "sqlite3_crashparams", crashParamsObjCmd, 0, 0); 835 Tcl_CreateObjCommand(interp, "sqlite3_simulate_device", devSymObjCmd, 0, 0); 836 #endif 837 return TCL_OK; 838 } 839 840 #endif /* SQLITE_TEST */ 841