4 ** The author disclaims copyright to this source code. In place of
5 ** a legal notice, here is a blessing:
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.
11 ******************************************************************************
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.
17 #if SQLITE_TEST /* This file is used for testing only */
18 #include "sqliteInt.h"
19 #if defined(INCLUDE_SQLITE_TCL_H)
20 # include "sqlite_tcl.h"
25 #ifndef SQLITE_OMIT_DISKIO /* This file is a no-op if disk I/O is disabled */
27 /* #define TRACE_CRASHTEST */
29 typedef struct CrashFile CrashFile
;
30 typedef struct CrashGlobal CrashGlobal
;
31 typedef struct WriteBuffer WriteBuffer
;
36 ** This layer is implemented as a wrapper around the "real"
37 ** sqlite3_file object for the host system. Each time data is
38 ** written to the file object, instead of being written to the
39 ** underlying file, the write operation is stored in an in-memory
40 ** structure (type WriteBuffer). This structure is placed at the
41 ** end of a global ordered list (the write-list).
43 ** When data is read from a file object, the requested region is
44 ** first retrieved from the real file. The write-list is then
45 ** traversed and data copied from any overlapping WriteBuffer
46 ** structures to the output buffer. i.e. a read() operation following
47 ** one or more write() operations works as expected, even if no
48 ** data has actually been written out to the real file.
50 ** When a fsync() operation is performed, an operating system crash
51 ** may be simulated, in which case exit(-1) is called (the call to
52 ** xSync() never returns). Whether or not a crash is simulated,
53 ** the data associated with a subset of the WriteBuffer structures
54 ** stored in the write-list is written to the real underlying files
55 ** and the entries removed from the write-list. If a crash is simulated,
56 ** a subset of the buffers may be corrupted before the data is written.
58 ** The exact subset of the write-list written and/or corrupted is
59 ** determined by the simulated device characteristics and sector-size.
63 ** Normal mode is used when the simulated device has none of the
64 ** SQLITE_IOCAP_XXX flags set.
66 ** In normal mode, if the fsync() is not a simulated crash, the
67 ** write-list is traversed from beginning to end. Each WriteBuffer
68 ** structure associated with the file handle used to call xSync()
69 ** is written to the real file and removed from the write-list.
71 ** If a crash is simulated, one of the following takes place for
72 ** each WriteBuffer in the write-list, regardless of which
73 ** file-handle it is associated with:
75 ** 1. The buffer is correctly written to the file, just as if
76 ** a crash were not being simulated.
78 ** 2. Nothing is done.
80 ** 3. Garbage data is written to all sectors of the file that
81 ** overlap the region specified by the WriteBuffer. Or garbage
82 ** data is written to some contiguous section within the
83 ** overlapped sectors.
85 ** Device Characteristic flag handling:
87 ** If the IOCAP_ATOMIC flag is set, then option (3) above is
90 ** If the IOCAP_ATOMIC512 flag is set, and the WriteBuffer represents
91 ** an aligned write() of an integer number of 512 byte regions, then
92 ** option (3) above is never selected. Instead, each 512 byte region
93 ** is either correctly written or left completely untouched. Similar
94 ** logic governs the behavior if any of the other ATOMICXXX flags
97 ** If either the IOCAP_SAFEAPPEND or IOCAP_SEQUENTIAL flags are set
98 ** and a crash is being simulated, then an entry of the write-list is
99 ** selected at random. Everything in the list after the selected entry
100 ** is discarded before processing begins.
102 ** If IOCAP_SEQUENTIAL is set and a crash is being simulated, option
103 ** (1) is selected for all write-list entries except the last. If a
104 ** crash is not being simulated, then all entries in the write-list
105 ** that occur before at least one write() on the file-handle specified
106 ** as part of the xSync() are written to their associated real files.
108 ** If IOCAP_SAFEAPPEND is set and the first byte written by the write()
109 ** operation is one byte past the current end of the file, then option
110 ** (1) is always selected.
114 ** Each write operation in the write-list is represented by an instance
115 ** of the following structure.
117 ** If zBuf is 0, then this structure represents a call to xTruncate(),
118 ** not xWrite(). In that case, iOffset is the size that the file is
122 i64 iOffset
; /* Byte offset of the start of this write() */
123 int nBuf
; /* Number of bytes written */
124 u8
*zBuf
; /* Pointer to copy of written data */
125 CrashFile
*pFile
; /* File this write() applies to */
127 WriteBuffer
*pNext
; /* Next in CrashGlobal.pWriteList */
131 const sqlite3_io_methods
*pMethod
; /* Must be first */
132 sqlite3_file
*pRealFile
; /* Underlying "real" file handle */
134 int flags
; /* Flags the file was opened with */
136 /* Cache of the entire file. This is used to speed up OsRead() and
137 ** OsFileSize() calls. Although both could be done by traversing the
138 ** write-list, in practice this is impractically slow.
140 u8
*zData
; /* Buffer containing file contents */
141 int nData
; /* Size of buffer allocated at zData */
142 i64 iSize
; /* Size of file in bytes */
146 WriteBuffer
*pWriteList
; /* Head of write-list */
147 WriteBuffer
*pWriteListEnd
; /* End of write-list */
149 int iSectorSize
; /* Value of simulated sector size */
150 int iDeviceCharacteristics
; /* Value of simulated device characteristics */
152 int iCrash
; /* Crash on the iCrash'th call to xSync() */
153 char zCrashFile
[500]; /* Crash during an xSync() on this file */
156 static CrashGlobal g
= {0, 0, SQLITE_DEFAULT_SECTOR_SIZE
, 0, 0};
159 ** Set this global variable to 1 to enable crash testing.
161 static int sqlite3CrashTestEnable
= 0;
163 static void *crash_malloc(int nByte
){
164 return (void *)Tcl_AttemptAlloc((size_t)nByte
);
166 static void crash_free(void *p
){
169 static void *crash_realloc(void *p
, int n
){
170 return (void *)Tcl_AttemptRealloc(p
, (size_t)n
);
174 ** Wrapper around the sqlite3OsWrite() function that avoids writing to the
175 ** 512 byte block begining at offset PENDING_BYTE.
177 static int writeDbFile(CrashFile
*p
, u8
*z
, i64 iAmt
, i64 iOff
){
180 if( (iAmt
-iSkip
)>0 ){
181 rc
= sqlite3OsWrite(p
->pRealFile
, &z
[iSkip
], (int)(iAmt
-iSkip
), iOff
+iSkip
);
187 ** Flush the write-list as if xSync() had been called on file handle
188 ** pFile. If isCrash is true, simulate a crash.
190 static int writeListSync(CrashFile
*pFile
, int isCrash
){
192 int iDc
= g
.iDeviceCharacteristics
;
197 /* If this is not a crash simulation, set pFinal to point to the
198 ** last element of the write-list that is associated with file handle
201 ** If this is a crash simulation, set pFinal to an arbitrarily selected
202 ** element of the write-list.
204 WriteBuffer
*pFinal
= 0;
206 for(pWrite
=g
.pWriteList
; pWrite
; pWrite
=pWrite
->pNext
){
207 if( pWrite
->pFile
==pFile
){
211 }else if( iDc
&(SQLITE_IOCAP_SEQUENTIAL
|SQLITE_IOCAP_SAFE_APPEND
) ){
214 for(pWrite
=g
.pWriteList
; pWrite
; pWrite
=pWrite
->pNext
) nWrite
++;
215 sqlite3_randomness(sizeof(int), &iFinal
);
216 iFinal
= ((iFinal
<0)?-1*iFinal
:iFinal
)%nWrite
;
217 for(pWrite
=g
.pWriteList
; iFinal
>0; pWrite
=pWrite
->pNext
) iFinal
--;
221 #ifdef TRACE_CRASHTEST
223 printf("Sync %s (is %s crash)\n", pFile
->zName
, (isCrash
?"a":"not a"));
227 ppPtr
= &g
.pWriteList
;
228 for(pWrite
=*ppPtr
; rc
==SQLITE_OK
&& pWrite
; pWrite
=*ppPtr
){
229 sqlite3_file
*pRealFile
= pWrite
->pFile
->pRealFile
;
231 /* (eAction==1) -> write block out normally,
232 ** (eAction==2) -> do nothing,
233 ** (eAction==3) -> trash sectors.
238 if( (pWrite
->pFile
==pFile
|| iDc
&SQLITE_IOCAP_SEQUENTIAL
) ){
243 sqlite3_randomness(1, &random
);
245 /* Do not select option 3 (sector trashing) if the IOCAP_ATOMIC flag
246 ** is set or this is an OsTruncate(), not an Oswrite().
248 if( (iDc
&SQLITE_IOCAP_ATOMIC
) || (pWrite
->zBuf
==0) ){
252 /* If IOCAP_SEQUENTIAL is set and this is not the final entry
253 ** in the truncated write-list, always select option 1 (write
256 if( (iDc
&SQLITE_IOCAP_SEQUENTIAL
&& pWrite
!=pFinal
) ){
260 /* If IOCAP_SAFE_APPEND is set and this OsWrite() operation is
261 ** an append (first byte of the written region is 1 byte past the
262 ** current EOF), always select option 1 (write out correctly).
264 if( iDc
&SQLITE_IOCAP_SAFE_APPEND
&& pWrite
->zBuf
){
266 sqlite3OsFileSize(pRealFile
, &iSize
);
267 if( iSize
==pWrite
->iOffset
){
272 if( (random
&0x06)==0x06 ){
275 eAction
= ((random
&0x01)?2:1);
280 case 1: { /* Write out correctly */
283 pWrite
->pFile
, pWrite
->zBuf
, pWrite
->nBuf
, pWrite
->iOffset
286 rc
= sqlite3OsTruncate(pRealFile
, pWrite
->iOffset
);
288 *ppPtr
= pWrite
->pNext
;
289 #ifdef TRACE_CRASHTEST
291 printf("Writing %d bytes @ %d (%s)\n",
292 pWrite
->nBuf
, (int)pWrite
->iOffset
, pWrite
->pFile
->zName
299 case 2: { /* Do nothing */
300 ppPtr
= &pWrite
->pNext
;
301 #ifdef TRACE_CRASHTEST
303 printf("Omiting %d bytes @ %d (%s)\n",
304 pWrite
->nBuf
, (int)pWrite
->iOffset
, pWrite
->pFile
->zName
310 case 3: { /* Trash sectors */
312 int iFirst
= (int)(pWrite
->iOffset
/g
.iSectorSize
);
313 int iLast
= (int)((pWrite
->iOffset
+pWrite
->nBuf
-1)/g
.iSectorSize
);
315 assert(pWrite
->zBuf
);
317 #ifdef TRACE_CRASHTEST
318 printf("Trashing %d sectors @ %lld (sector %d) (%s)\n",
319 1+iLast
-iFirst
, pWrite
->iOffset
, iFirst
, pWrite
->pFile
->zName
323 zGarbage
= crash_malloc(g
.iSectorSize
);
326 for(i
=iFirst
; rc
==SQLITE_OK
&& i
<=iLast
; i
++){
327 sqlite3_randomness(g
.iSectorSize
, zGarbage
);
329 pWrite
->pFile
, zGarbage
, g
.iSectorSize
, i
*g
.iSectorSize
332 crash_free(zGarbage
);
337 ppPtr
= &pWrite
->pNext
;
342 assert(!"Cannot happen");
345 if( pWrite
==pFinal
) break;
348 if( rc
==SQLITE_OK
&& isCrash
){
352 for(pWrite
=g
.pWriteList
; pWrite
&& pWrite
->pNext
; pWrite
=pWrite
->pNext
);
353 g
.pWriteListEnd
= pWrite
;
359 ** Add an entry to the end of the write-list.
361 static int writeListAppend(
363 sqlite3_int64 iOffset
,
369 assert((zBuf
&& nBuf
) || (!nBuf
&& !zBuf
));
371 pNew
= (WriteBuffer
*)crash_malloc(sizeof(WriteBuffer
) + nBuf
);
373 fprintf(stderr
, "out of memory in the crash simulator\n");
375 memset(pNew
, 0, sizeof(WriteBuffer
)+nBuf
);
376 pNew
->iOffset
= iOffset
;
378 pNew
->pFile
= (CrashFile
*)pFile
;
380 pNew
->zBuf
= (u8
*)&pNew
[1];
381 memcpy(pNew
->zBuf
, zBuf
, nBuf
);
385 assert(g
.pWriteListEnd
);
386 g
.pWriteListEnd
->pNext
= pNew
;
390 g
.pWriteListEnd
= pNew
;
396 ** Close a crash-file.
398 static int cfClose(sqlite3_file
*pFile
){
399 CrashFile
*pCrash
= (CrashFile
*)pFile
;
400 writeListSync(pCrash
, 0);
401 sqlite3OsClose(pCrash
->pRealFile
);
406 ** Read data from a crash-file.
414 CrashFile
*pCrash
= (CrashFile
*)pFile
;
415 int nCopy
= (int)MIN((i64
)iAmt
, (pCrash
->iSize
- iOfst
));
418 memcpy(zBuf
, &pCrash
->zData
[iOfst
], nCopy
);
421 /* Check the file-size to see if this is a short-read */
423 return SQLITE_IOERR_SHORT_READ
;
430 ** Write data to a crash-file.
438 CrashFile
*pCrash
= (CrashFile
*)pFile
;
439 if( iAmt
+iOfst
>pCrash
->iSize
){
440 pCrash
->iSize
= (int)(iAmt
+iOfst
);
442 while( pCrash
->iSize
>pCrash
->nData
){
444 int nNew
= (pCrash
->nData
*2) + 4096;
445 zNew
= crash_realloc(pCrash
->zData
, nNew
);
449 memset(&zNew
[pCrash
->nData
], 0, nNew
-pCrash
->nData
);
450 pCrash
->nData
= nNew
;
451 pCrash
->zData
= zNew
;
453 memcpy(&pCrash
->zData
[iOfst
], zBuf
, iAmt
);
454 return writeListAppend(pFile
, iOfst
, zBuf
, iAmt
);
458 ** Truncate a crash-file.
460 static int cfTruncate(sqlite3_file
*pFile
, sqlite_int64 size
){
461 CrashFile
*pCrash
= (CrashFile
*)pFile
;
463 if( pCrash
->iSize
>size
){
464 pCrash
->iSize
= (int)size
;
466 return writeListAppend(pFile
, size
, 0, 0);
470 ** Sync a crash-file.
472 static int cfSync(sqlite3_file
*pFile
, int flags
){
473 CrashFile
*pCrash
= (CrashFile
*)pFile
;
476 const char *zName
= pCrash
->zName
;
477 const char *zCrashFile
= g
.zCrashFile
;
478 int nName
= (int)strlen(zName
);
479 int nCrashFile
= (int)strlen(zCrashFile
);
481 if( nCrashFile
>0 && zCrashFile
[nCrashFile
-1]=='*' ){
483 if( nName
>nCrashFile
) nName
= nCrashFile
;
486 #ifdef TRACE_CRASHTEST
487 printf("cfSync(): nName = %d, nCrashFile = %d, zName = %s, zCrashFile = %s\n",
488 nName
, nCrashFile
, zName
, zCrashFile
);
491 if( nName
==nCrashFile
&& 0==memcmp(zName
, zCrashFile
, nName
) ){
492 #ifdef TRACE_CRASHTEST
493 printf("cfSync(): name matched, g.iCrash = %d\n", g
.iCrash
);
495 if( (--g
.iCrash
)==0 ) isCrash
= 1;
498 return writeListSync(pCrash
, isCrash
);
502 ** Return the current file-size of the crash-file.
504 static int cfFileSize(sqlite3_file
*pFile
, sqlite_int64
*pSize
){
505 CrashFile
*pCrash
= (CrashFile
*)pFile
;
506 *pSize
= (i64
)pCrash
->iSize
;
511 ** Calls related to file-locks are passed on to the real file handle.
513 static int cfLock(sqlite3_file
*pFile
, int eLock
){
514 return sqlite3OsLock(((CrashFile
*)pFile
)->pRealFile
, eLock
);
516 static int cfUnlock(sqlite3_file
*pFile
, int eLock
){
517 return sqlite3OsUnlock(((CrashFile
*)pFile
)->pRealFile
, eLock
);
519 static int cfCheckReservedLock(sqlite3_file
*pFile
, int *pResOut
){
520 return sqlite3OsCheckReservedLock(((CrashFile
*)pFile
)->pRealFile
, pResOut
);
522 static int cfFileControl(sqlite3_file
*pFile
, int op
, void *pArg
){
523 if( op
==SQLITE_FCNTL_SIZE_HINT
){
524 CrashFile
*pCrash
= (CrashFile
*)pFile
;
525 i64 nByte
= *(i64
*)pArg
;
526 if( nByte
>pCrash
->iSize
){
527 if( SQLITE_OK
==writeListAppend(pFile
, nByte
, 0, 0) ){
528 pCrash
->iSize
= (int)nByte
;
533 return sqlite3OsFileControl(((CrashFile
*)pFile
)->pRealFile
, op
, pArg
);
537 ** The xSectorSize() and xDeviceCharacteristics() functions return
538 ** the global values configured by the [sqlite_crashparams] tcl
541 static int cfSectorSize(sqlite3_file
*pFile
){
542 return g
.iSectorSize
;
544 static int cfDeviceCharacteristics(sqlite3_file
*pFile
){
545 return g
.iDeviceCharacteristics
;
549 ** Pass-throughs for WAL support.
551 static int cfShmLock(sqlite3_file
*pFile
, int ofst
, int n
, int flags
){
552 return sqlite3OsShmLock(((CrashFile
*)pFile
)->pRealFile
, ofst
, n
, flags
);
554 static void cfShmBarrier(sqlite3_file
*pFile
){
555 sqlite3OsShmBarrier(((CrashFile
*)pFile
)->pRealFile
);
557 static int cfShmUnmap(sqlite3_file
*pFile
, int delFlag
){
558 return sqlite3OsShmUnmap(((CrashFile
*)pFile
)->pRealFile
, delFlag
);
561 sqlite3_file
*pFile
, /* Handle open on database file */
562 int iRegion
, /* Region to retrieve */
563 int sz
, /* Size of regions */
564 int w
, /* True to extend file if necessary */
565 void volatile **pp
/* OUT: Mapped memory */
567 return sqlite3OsShmMap(((CrashFile
*)pFile
)->pRealFile
, iRegion
, sz
, w
, pp
);
570 static const sqlite3_io_methods CrashFileVtab
= {
572 cfClose
, /* xClose */
574 cfWrite
, /* xWrite */
575 cfTruncate
, /* xTruncate */
577 cfFileSize
, /* xFileSize */
579 cfUnlock
, /* xUnlock */
580 cfCheckReservedLock
, /* xCheckReservedLock */
581 cfFileControl
, /* xFileControl */
582 cfSectorSize
, /* xSectorSize */
583 cfDeviceCharacteristics
, /* xDeviceCharacteristics */
584 cfShmMap
, /* xShmMap */
585 cfShmLock
, /* xShmLock */
586 cfShmBarrier
, /* xShmBarrier */
587 cfShmUnmap
/* xShmUnmap */
591 ** Application data for the crash VFS
593 struct crashAppData
{
594 sqlite3_vfs
*pOrig
; /* Wrapped vfs structure */
598 ** Open a crash-file file handle.
600 ** The caller will have allocated pVfs->szOsFile bytes of space
601 ** at pFile. This file uses this space for the CrashFile structure
602 ** and allocates space for the "real" file structure using
603 ** sqlite3_malloc(). The assumption here is (pVfs->szOsFile) is
604 ** equal or greater than sizeof(CrashFile).
613 sqlite3_vfs
*pVfs
= (sqlite3_vfs
*)pCfVfs
->pAppData
;
615 CrashFile
*pWrapper
= (CrashFile
*)pFile
;
616 sqlite3_file
*pReal
= (sqlite3_file
*)&pWrapper
[1];
618 memset(pWrapper
, 0, sizeof(CrashFile
));
619 rc
= sqlite3OsOpen(pVfs
, zName
, pReal
, flags
, pOutFlags
);
623 pWrapper
->pMethod
= &CrashFileVtab
;
624 pWrapper
->zName
= (char *)zName
;
625 pWrapper
->pRealFile
= pReal
;
626 rc
= sqlite3OsFileSize(pReal
, &iSize
);
627 pWrapper
->iSize
= (int)iSize
;
628 pWrapper
->flags
= flags
;
631 pWrapper
->nData
= (int)(4096 + pWrapper
->iSize
);
632 pWrapper
->zData
= crash_malloc(pWrapper
->nData
);
633 if( pWrapper
->zData
){
634 /* os_unix.c contains an assert() that fails if the caller attempts
635 ** to read data from the 512-byte locking region of a file opened
636 ** with the SQLITE_OPEN_MAIN_DB flag. This region of a database file
637 ** never contains valid data anyhow. So avoid doing such a read here.
639 ** UPDATE: It also contains an assert() verifying that each call
640 ** to the xRead() method reads less than 128KB of data.
644 memset(pWrapper
->zData
, 0, pWrapper
->nData
);
645 for(iOff
=0; iOff
<pWrapper
->iSize
; iOff
+= 512){
646 int nRead
= (int)(pWrapper
->iSize
- iOff
);
647 if( nRead
>512 ) nRead
= 512;
648 rc
= sqlite3OsRead(pReal
, &pWrapper
->zData
[iOff
], nRead
, iOff
);
654 if( rc
!=SQLITE_OK
&& pWrapper
->pMethod
){
655 sqlite3OsClose(pFile
);
660 static int cfDelete(sqlite3_vfs
*pCfVfs
, const char *zPath
, int dirSync
){
661 sqlite3_vfs
*pVfs
= (sqlite3_vfs
*)pCfVfs
->pAppData
;
662 return pVfs
->xDelete(pVfs
, zPath
, dirSync
);
670 sqlite3_vfs
*pVfs
= (sqlite3_vfs
*)pCfVfs
->pAppData
;
671 return pVfs
->xAccess(pVfs
, zPath
, flags
, pResOut
);
673 static int cfFullPathname(
679 sqlite3_vfs
*pVfs
= (sqlite3_vfs
*)pCfVfs
->pAppData
;
680 return pVfs
->xFullPathname(pVfs
, zPath
, nPathOut
, zPathOut
);
682 static void *cfDlOpen(sqlite3_vfs
*pCfVfs
, const char *zPath
){
683 sqlite3_vfs
*pVfs
= (sqlite3_vfs
*)pCfVfs
->pAppData
;
684 return pVfs
->xDlOpen(pVfs
, zPath
);
686 static void cfDlError(sqlite3_vfs
*pCfVfs
, int nByte
, char *zErrMsg
){
687 sqlite3_vfs
*pVfs
= (sqlite3_vfs
*)pCfVfs
->pAppData
;
688 pVfs
->xDlError(pVfs
, nByte
, zErrMsg
);
690 static void (*cfDlSym(sqlite3_vfs
*pCfVfs
, void *pH
, const char *zSym
))(void){
691 sqlite3_vfs
*pVfs
= (sqlite3_vfs
*)pCfVfs
->pAppData
;
692 return pVfs
->xDlSym(pVfs
, pH
, zSym
);
694 static void cfDlClose(sqlite3_vfs
*pCfVfs
, void *pHandle
){
695 sqlite3_vfs
*pVfs
= (sqlite3_vfs
*)pCfVfs
->pAppData
;
696 pVfs
->xDlClose(pVfs
, pHandle
);
698 static int cfRandomness(sqlite3_vfs
*pCfVfs
, int nByte
, char *zBufOut
){
699 sqlite3_vfs
*pVfs
= (sqlite3_vfs
*)pCfVfs
->pAppData
;
700 return pVfs
->xRandomness(pVfs
, nByte
, zBufOut
);
702 static int cfSleep(sqlite3_vfs
*pCfVfs
, int nMicro
){
703 sqlite3_vfs
*pVfs
= (sqlite3_vfs
*)pCfVfs
->pAppData
;
704 return pVfs
->xSleep(pVfs
, nMicro
);
706 static int cfCurrentTime(sqlite3_vfs
*pCfVfs
, double *pTimeOut
){
707 sqlite3_vfs
*pVfs
= (sqlite3_vfs
*)pCfVfs
->pAppData
;
708 return pVfs
->xCurrentTime(pVfs
, pTimeOut
);
710 static int cfGetLastError(sqlite3_vfs
*pCfVfs
, int n
, char *z
){
711 sqlite3_vfs
*pVfs
= (sqlite3_vfs
*)pCfVfs
->pAppData
;
712 return pVfs
->xGetLastError(pVfs
, n
, z
);
715 static int processDevSymArgs(
718 Tcl_Obj
*CONST objv
[],
726 { "atomic", SQLITE_IOCAP_ATOMIC
},
727 { "atomic512", SQLITE_IOCAP_ATOMIC512
},
728 { "atomic1k", SQLITE_IOCAP_ATOMIC1K
},
729 { "atomic2k", SQLITE_IOCAP_ATOMIC2K
},
730 { "atomic4k", SQLITE_IOCAP_ATOMIC4K
},
731 { "atomic8k", SQLITE_IOCAP_ATOMIC8K
},
732 { "atomic16k", SQLITE_IOCAP_ATOMIC16K
},
733 { "atomic32k", SQLITE_IOCAP_ATOMIC32K
},
734 { "atomic64k", SQLITE_IOCAP_ATOMIC64K
},
735 { "sequential", SQLITE_IOCAP_SEQUENTIAL
},
736 { "safe_append", SQLITE_IOCAP_SAFE_APPEND
},
737 { "powersafe_overwrite", SQLITE_IOCAP_POWERSAFE_OVERWRITE
},
744 int setSectorsize
= 0;
745 int setDeviceChar
= 0;
747 for(i
=0; i
<objc
; i
+=2){
749 char *zOpt
= Tcl_GetStringFromObj(objv
[i
], &nOpt
);
751 if( (nOpt
>11 || nOpt
<2 || strncmp("-sectorsize", zOpt
, nOpt
))
752 && (nOpt
>16 || nOpt
<2 || strncmp("-characteristics", zOpt
, nOpt
))
754 Tcl_AppendResult(interp
,
755 "Bad option: \"", zOpt
,
756 "\" - must be \"-characteristics\" or \"-sectorsize\"", 0
761 Tcl_AppendResult(interp
, "Option requires an argument: \"", zOpt
, "\"",0);
766 if( Tcl_GetIntFromObj(interp
, objv
[i
+1], &iSectorSize
) ){
774 if( Tcl_ListObjGetElements(interp
, objv
[i
+1], &nObj
, &apObj
) ){
777 for(j
=0; j
<nObj
; j
++){
780 Tcl_Obj
*pFlag
= Tcl_DuplicateObj(apObj
[j
]);
781 Tcl_IncrRefCount(pFlag
);
782 Tcl_UtfToLower(Tcl_GetString(pFlag
));
784 rc
= Tcl_GetIndexFromObjStruct(
785 interp
, pFlag
, aFlag
, sizeof(aFlag
[0]), "no such flag", 0, &iChoice
787 Tcl_DecrRefCount(pFlag
);
792 iDc
|= aFlag
[iChoice
].iValue
;
802 *piSectorSize
= iSectorSize
;
809 ** tclcmd: sqlite3_crash_now
811 ** Simulate a crash immediately. This function does not return
812 ** (writeListSync() calls exit(-1)).
814 static int SQLITE_TCLAPI
crashNowCmd(
818 Tcl_Obj
*CONST objv
[]
821 Tcl_WrongNumArgs(interp
, 1, objv
, "");
830 ** tclcmd: sqlite_crash_enable ENABLE
832 ** Parameter ENABLE must be a boolean value. If true, then the "crash"
833 ** vfs is added to the system. If false, it is removed.
835 static int SQLITE_TCLAPI
crashEnableCmd(
839 Tcl_Obj
*CONST objv
[]
842 static sqlite3_vfs crashVfs
= {
851 cfDelete
, /* xDelete */
852 cfAccess
, /* xAccess */
853 cfFullPathname
, /* xFullPathname */
854 cfDlOpen
, /* xDlOpen */
855 cfDlError
, /* xDlError */
856 cfDlSym
, /* xDlSym */
857 cfDlClose
, /* xDlClose */
858 cfRandomness
, /* xRandomness */
859 cfSleep
, /* xSleep */
860 cfCurrentTime
, /* xCurrentTime */
861 cfGetLastError
, /* xGetLastError */
862 0, /* xCurrentTimeInt64 */
866 Tcl_WrongNumArgs(interp
, 1, objv
, "ENABLE");
870 if( Tcl_GetBooleanFromObj(interp
, objv
[1], &isEnable
) ){
874 if( (isEnable
&& crashVfs
.pAppData
) || (!isEnable
&& !crashVfs
.pAppData
) ){
878 if( crashVfs
.pAppData
==0 ){
879 sqlite3_vfs
*pOriginalVfs
= sqlite3_vfs_find(0);
880 crashVfs
.mxPathname
= pOriginalVfs
->mxPathname
;
881 crashVfs
.pAppData
= (void *)pOriginalVfs
;
882 crashVfs
.szOsFile
= sizeof(CrashFile
) + pOriginalVfs
->szOsFile
;
883 sqlite3_vfs_register(&crashVfs
, 0);
885 crashVfs
.pAppData
= 0;
886 sqlite3_vfs_unregister(&crashVfs
);
893 ** tclcmd: sqlite_crashparams ?OPTIONS? DELAY CRASHFILE
895 ** This procedure implements a TCL command that enables crash testing
896 ** in testfixture. Once enabled, crash testing cannot be disabled.
898 ** Available options are "-characteristics" and "-sectorsize". Both require
899 ** an argument. For -sectorsize, this is the simulated sector size in
900 ** bytes. For -characteristics, the argument must be a list of io-capability
901 ** flags to simulate. Valid flags are "atomic", "atomic512", "atomic1K",
902 ** "atomic2K", "atomic4K", "atomic8K", "atomic16K", "atomic32K",
903 ** "atomic64K", "sequential" and "safe_append".
907 ** sqlite_crashparams -sect 1024 -char {atomic sequential} ./test.db 1
910 static int SQLITE_TCLAPI
crashParamsObjCmd(
914 Tcl_Obj
*CONST objv
[]
917 const char *zCrashFile
;
918 int nCrashFile
, iDc
, iSectorSize
;
924 Tcl_WrongNumArgs(interp
, 1, objv
, "?OPTIONS? DELAY CRASHFILE");
928 zCrashFile
= Tcl_GetStringFromObj(objv
[objc
-1], &nCrashFile
);
929 if( nCrashFile
>=sizeof(g
.zCrashFile
) ){
930 Tcl_AppendResult(interp
, "Filename is too long: \"", zCrashFile
, "\"", 0);
933 if( Tcl_GetIntFromObj(interp
, objv
[objc
-2], &iDelay
) ){
937 if( processDevSymArgs(interp
, objc
-3, &objv
[1], &iDc
, &iSectorSize
) ){
942 g
.iDeviceCharacteristics
= iDc
;
944 if( iSectorSize
>=0 ){
945 g
.iSectorSize
= iSectorSize
;
949 memcpy(g
.zCrashFile
, zCrashFile
, nCrashFile
+1);
950 sqlite3CrashTestEnable
= 1;
957 static int SQLITE_TCLAPI
devSymObjCmd(
961 Tcl_Obj
*CONST objv
[]
963 void devsym_register(int iDeviceChar
, int iSectorSize
);
966 int iSectorSize
= -1;
968 if( processDevSymArgs(interp
, objc
-1, &objv
[1], &iDc
, &iSectorSize
) ){
971 devsym_register(iDc
, iSectorSize
);
978 ** tclcmd: unregister_devsim
980 static int SQLITE_TCLAPI
dsUnregisterObjCmd(
984 Tcl_Obj
*CONST objv
[]
986 void devsym_unregister(void);
989 Tcl_WrongNumArgs(interp
, 1, objv
, "");
998 ** tclcmd: register_jt_vfs ?-default? PARENT-VFS
1000 static int SQLITE_TCLAPI
jtObjCmd(
1004 Tcl_Obj
*CONST objv
[]
1006 int jt_register(char *, int);
1009 if( objc
!=2 && objc
!=3 ){
1010 Tcl_WrongNumArgs(interp
, 1, objv
, "?-default? PARENT-VFS");
1013 zParent
= Tcl_GetString(objv
[1]);
1015 if( strcmp(zParent
, "-default") ){
1016 Tcl_AppendResult(interp
,
1017 "bad option \"", zParent
, "\": must be -default", 0
1021 zParent
= Tcl_GetString(objv
[2]);
1027 if( jt_register(zParent
, objc
==3) ){
1028 Tcl_AppendResult(interp
, "Error in jt_register", 0);
1036 ** tclcmd: unregister_jt_vfs
1038 static int SQLITE_TCLAPI
jtUnregisterObjCmd(
1042 Tcl_Obj
*CONST objv
[]
1044 void jt_unregister(void);
1047 Tcl_WrongNumArgs(interp
, 1, objv
, "");
1055 #endif /* SQLITE_OMIT_DISKIO */
1058 ** This procedure registers the TCL procedures defined in this file.
1060 int Sqlitetest6_Init(Tcl_Interp
*interp
){
1061 #ifndef SQLITE_OMIT_DISKIO
1062 Tcl_CreateObjCommand(interp
, "sqlite3_crash_enable", crashEnableCmd
, 0, 0);
1063 Tcl_CreateObjCommand(interp
, "sqlite3_crashparams", crashParamsObjCmd
, 0, 0);
1064 Tcl_CreateObjCommand(interp
, "sqlite3_crash_now", crashNowCmd
, 0, 0);
1065 Tcl_CreateObjCommand(interp
, "sqlite3_simulate_device", devSymObjCmd
, 0, 0);
1066 Tcl_CreateObjCommand(interp
, "unregister_devsim", dsUnregisterObjCmd
, 0, 0);
1067 Tcl_CreateObjCommand(interp
, "register_jt_vfs", jtObjCmd
, 0, 0);
1068 Tcl_CreateObjCommand(interp
, "unregister_jt_vfs", jtUnregisterObjCmd
, 0, 0);
1073 #endif /* SQLITE_TEST */