blob: c151ea42989b0be24ee4b9a4be378b4f636db641 [file] [log] [blame]
drh9c06c952005-11-26 00:25:00 +00001/*
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*/
danielk1977f55b8992007-08-24 08:15:53 +000017#if SQLITE_TEST /* This file is used for testing only */
drh9c06c952005-11-26 00:25:00 +000018#include "sqliteInt.h"
drh9c06c952005-11-26 00:25:00 +000019#include "tcl.h"
20
drh198bf392006-01-06 21:52:49 +000021#ifndef SQLITE_OMIT_DISKIO /* This file is a no-op if disk I/O is disabled */
22
danielk1977f8940ae2007-08-23 11:07:10 +000023/* #define TRACE_CRASHTEST */
24
danielk197762079062007-08-15 17:08:46 +000025typedef struct CrashFile CrashFile;
26typedef struct CrashGlobal CrashGlobal;
danielk19770d24e6b2007-08-14 17:42:05 +000027typedef 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
mistachkin48864df2013-03-21 21:20:32 +000090** logic governs the behavior if any of the other ATOMICXXX flags
danielk19770d24e6b2007-08-14 17:42:05 +000091** 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*/
danielk197762079062007-08-15 17:08:46 +0000108
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*/
danielk19770d24e6b2007-08-14 17:42:05 +0000117struct WriteBuffer {
danielk197762079062007-08-15 17:08:46 +0000118 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 */
danielk19770d24e6b2007-08-14 17:42:05 +0000124};
125
danielk197762079062007-08-15 17:08:46 +0000126struct CrashFile {
127 const sqlite3_io_methods *pMethod; /* Must be first */
128 sqlite3_file *pRealFile; /* Underlying "real" file handle */
danielk19771e536952007-08-16 10:09:01 +0000129 char *zName;
danielk1977d6846d72009-02-11 14:27:04 +0000130 int flags; /* Flags the file was opened with */
danielk1977967a4a12007-08-20 14:23:44 +0000131
danielk1977f1da17a2007-08-21 13:07:46 +0000132 /* Cache of the entire file. This is used to speed up OsRead() and
133 ** OsFileSize() calls. Although both could be done by traversing the
134 ** write-list, in practice this is impractically slow.
135 */
danielk1977967a4a12007-08-20 14:23:44 +0000136 int iSize; /* Size of file in bytes */
137 int nData; /* Size of buffer allocated at zData */
138 u8 *zData; /* Buffer containing file contents */
drh9c06c952005-11-26 00:25:00 +0000139};
140
danielk197762079062007-08-15 17:08:46 +0000141struct CrashGlobal {
142 WriteBuffer *pWriteList; /* Head of write-list */
danielk1977967a4a12007-08-20 14:23:44 +0000143 WriteBuffer *pWriteListEnd; /* End of write-list */
drh9c06c952005-11-26 00:25:00 +0000144
danielk197762079062007-08-15 17:08:46 +0000145 int iSectorSize; /* Value of simulated sector size */
146 int iDeviceCharacteristics; /* Value of simulated device characteristics */
drh9c06c952005-11-26 00:25:00 +0000147
danielk197762079062007-08-15 17:08:46 +0000148 int iCrash; /* Crash on the iCrash'th call to xSync() */
149 char zCrashFile[500]; /* Crash during an xSync() on this file */
150};
drh9c06c952005-11-26 00:25:00 +0000151
danielk1977967a4a12007-08-20 14:23:44 +0000152static CrashGlobal g = {0, 0, SQLITE_DEFAULT_SECTOR_SIZE, 0, 0};
drh9c06c952005-11-26 00:25:00 +0000153
154/*
drh66560ad2006-01-06 14:32:19 +0000155** Set this global variable to 1 to enable crash testing.
156*/
danielk1977967a4a12007-08-20 14:23:44 +0000157static int sqlite3CrashTestEnable = 0;
drh66560ad2006-01-06 14:32:19 +0000158
danielk1977a98d7b42008-01-18 13:42:54 +0000159static void *crash_malloc(int nByte){
160 return (void *)Tcl_Alloc((size_t)nByte);
161}
162static void crash_free(void *p){
163 Tcl_Free(p);
164}
165static void *crash_realloc(void *p, int n){
166 return (void *)Tcl_Realloc(p, (size_t)n);
167}
168
drh66560ad2006-01-06 14:32:19 +0000169/*
danielk1977d6846d72009-02-11 14:27:04 +0000170** Wrapper around the sqlite3OsWrite() function that avoids writing to the
171** 512 byte block begining at offset PENDING_BYTE.
172*/
173static int writeDbFile(CrashFile *p, u8 *z, i64 iAmt, i64 iOff){
shanehebffe412010-07-08 16:22:51 +0000174 int rc = SQLITE_OK;
danielk1977d6846d72009-02-11 14:27:04 +0000175 int iSkip = 0;
176 if( iOff==PENDING_BYTE && (p->flags&SQLITE_OPEN_MAIN_DB) ){
177 iSkip = 512;
178 }
179 if( (iAmt-iSkip)>0 ){
drh7da5fcb2012-03-30 14:59:43 +0000180 rc = sqlite3OsWrite(p->pRealFile, &z[iSkip], (int)(iAmt-iSkip), iOff+iSkip);
danielk1977d6846d72009-02-11 14:27:04 +0000181 }
182 return rc;
183}
184
185/*
danielk197762079062007-08-15 17:08:46 +0000186** Flush the write-list as if xSync() had been called on file handle
187** pFile. If isCrash is true, simulate a crash.
drh9c06c952005-11-26 00:25:00 +0000188*/
danielk197762079062007-08-15 17:08:46 +0000189static int writeListSync(CrashFile *pFile, int isCrash){
190 int rc = SQLITE_OK;
191 int iDc = g.iDeviceCharacteristics;
drh66560ad2006-01-06 14:32:19 +0000192
danielk197762079062007-08-15 17:08:46 +0000193 WriteBuffer *pWrite;
194 WriteBuffer **ppPtr;
drh66560ad2006-01-06 14:32:19 +0000195
danielk1977f55b8992007-08-24 08:15:53 +0000196 /* If this is not a crash simulation, set pFinal to point to the
197 ** last element of the write-list that is associated with file handle
198 ** pFile.
199 **
200 ** If this is a crash simulation, set pFinal to an arbitrarily selected
201 ** element of the write-list.
danielk197762079062007-08-15 17:08:46 +0000202 */
203 WriteBuffer *pFinal = 0;
204 if( !isCrash ){
205 for(pWrite=g.pWriteList; pWrite; pWrite=pWrite->pNext){
206 if( pWrite->pFile==pFile ){
207 pFinal = pWrite;
208 }
209 }
danielk1977f55b8992007-08-24 08:15:53 +0000210 }else if( iDc&(SQLITE_IOCAP_SEQUENTIAL|SQLITE_IOCAP_SAFE_APPEND) ){
211 int nWrite = 0;
212 int iFinal;
213 for(pWrite=g.pWriteList; pWrite; pWrite=pWrite->pNext) nWrite++;
drh2fa18682008-03-19 14:15:34 +0000214 sqlite3_randomness(sizeof(int), &iFinal);
danielk1977f55b8992007-08-24 08:15:53 +0000215 iFinal = ((iFinal<0)?-1*iFinal:iFinal)%nWrite;
216 for(pWrite=g.pWriteList; iFinal>0; pWrite=pWrite->pNext) iFinal--;
217 pFinal = pWrite;
drh1a235932005-11-29 18:37:15 +0000218 }
drh66560ad2006-01-06 14:32:19 +0000219
danielk1977f8940ae2007-08-23 11:07:10 +0000220#ifdef TRACE_CRASHTEST
221 printf("Sync %s (is %s crash)\n", pFile->zName, (isCrash?"a":"not a"));
222#endif
223
danielk197762079062007-08-15 17:08:46 +0000224 ppPtr = &g.pWriteList;
225 for(pWrite=*ppPtr; rc==SQLITE_OK && pWrite; pWrite=*ppPtr){
danielk1977967a4a12007-08-20 14:23:44 +0000226 sqlite3_file *pRealFile = pWrite->pFile->pRealFile;
danielk197762079062007-08-15 17:08:46 +0000227
228 /* (eAction==1) -> write block out normally,
229 ** (eAction==2) -> do nothing,
230 ** (eAction==3) -> trash sectors.
231 */
232 int eAction = 0;
233 if( !isCrash ){
234 eAction = 2;
235 if( (pWrite->pFile==pFile || iDc&SQLITE_IOCAP_SEQUENTIAL) ){
236 eAction = 1;
237 }
238 }else{
239 char random;
drh2fa18682008-03-19 14:15:34 +0000240 sqlite3_randomness(1, &random);
danielk197762079062007-08-15 17:08:46 +0000241
danielk1977f55b8992007-08-24 08:15:53 +0000242 /* Do not select option 3 (sector trashing) if the IOCAP_ATOMIC flag
243 ** is set or this is an OsTruncate(), not an Oswrite().
244 */
245 if( (iDc&SQLITE_IOCAP_ATOMIC) || (pWrite->zBuf==0) ){
danielk197762079062007-08-15 17:08:46 +0000246 random &= 0x01;
247 }
248
danielk1977f55b8992007-08-24 08:15:53 +0000249 /* If IOCAP_SEQUENTIAL is set and this is not the final entry
250 ** in the truncated write-list, always select option 1 (write
251 ** out correctly).
252 */
253 if( (iDc&SQLITE_IOCAP_SEQUENTIAL && pWrite!=pFinal) ){
254 random = 0;
255 }
256
257 /* If IOCAP_SAFE_APPEND is set and this OsWrite() operation is
258 ** an append (first byte of the written region is 1 byte past the
259 ** current EOF), always select option 1 (write out correctly).
260 */
261 if( iDc&SQLITE_IOCAP_SAFE_APPEND && pWrite->zBuf ){
262 i64 iSize;
263 sqlite3OsFileSize(pRealFile, &iSize);
264 if( iSize==pWrite->iOffset ){
265 random = 0;
266 }
267 }
268
danielk197762079062007-08-15 17:08:46 +0000269 if( (random&0x06)==0x06 ){
270 eAction = 3;
271 }else{
272 eAction = ((random&0x01)?2:1);
273 }
274 }
275
276 switch( eAction ){
277 case 1: { /* Write out correctly */
278 if( pWrite->zBuf ){
danielk1977d6846d72009-02-11 14:27:04 +0000279 rc = writeDbFile(
280 pWrite->pFile, pWrite->zBuf, pWrite->nBuf, pWrite->iOffset
danielk197762079062007-08-15 17:08:46 +0000281 );
282 }else{
danielk1977967a4a12007-08-20 14:23:44 +0000283 rc = sqlite3OsTruncate(pRealFile, pWrite->iOffset);
danielk197762079062007-08-15 17:08:46 +0000284 }
285 *ppPtr = pWrite->pNext;
danielk1977f8940ae2007-08-23 11:07:10 +0000286#ifdef TRACE_CRASHTEST
287 if( isCrash ){
danielk1977f55b8992007-08-24 08:15:53 +0000288 printf("Writing %d bytes @ %d (%s)\n",
289 pWrite->nBuf, (int)pWrite->iOffset, pWrite->pFile->zName
290 );
danielk1977f8940ae2007-08-23 11:07:10 +0000291 }
292#endif
danielk1977a98d7b42008-01-18 13:42:54 +0000293 crash_free(pWrite);
danielk197762079062007-08-15 17:08:46 +0000294 break;
295 }
296 case 2: { /* Do nothing */
297 ppPtr = &pWrite->pNext;
danielk1977f8940ae2007-08-23 11:07:10 +0000298#ifdef TRACE_CRASHTEST
299 if( isCrash ){
danielk1977f55b8992007-08-24 08:15:53 +0000300 printf("Omiting %d bytes @ %d (%s)\n",
301 pWrite->nBuf, (int)pWrite->iOffset, pWrite->pFile->zName
302 );
danielk1977f8940ae2007-08-23 11:07:10 +0000303 }
304#endif
danielk197762079062007-08-15 17:08:46 +0000305 break;
306 }
307 case 3: { /* Trash sectors */
308 u8 *zGarbage;
drh7da5fcb2012-03-30 14:59:43 +0000309 int iFirst = (int)(pWrite->iOffset/g.iSectorSize);
310 int iLast = (int)((pWrite->iOffset+pWrite->nBuf-1)/g.iSectorSize);
danielk1977967a4a12007-08-20 14:23:44 +0000311
312 assert(pWrite->zBuf);
danielk197762079062007-08-15 17:08:46 +0000313
danielk1977f8940ae2007-08-23 11:07:10 +0000314#ifdef TRACE_CRASHTEST
mistachkine1b461b2012-10-18 09:39:16 +0000315 printf("Trashing %d sectors @ %lld (sector %d) (%s)\n",
dan1e3e4182012-10-17 16:20:36 +0000316 1+iLast-iFirst, pWrite->iOffset, iFirst, pWrite->pFile->zName
danielk1977f55b8992007-08-24 08:15:53 +0000317 );
danielk1977f8940ae2007-08-23 11:07:10 +0000318#endif
319
danielk1977a98d7b42008-01-18 13:42:54 +0000320 zGarbage = crash_malloc(g.iSectorSize);
danielk197762079062007-08-15 17:08:46 +0000321 if( zGarbage ){
322 sqlite3_int64 i;
323 for(i=iFirst; rc==SQLITE_OK && i<=iLast; i++){
drh2fa18682008-03-19 14:15:34 +0000324 sqlite3_randomness(g.iSectorSize, zGarbage);
danielk1977d6846d72009-02-11 14:27:04 +0000325 rc = writeDbFile(
326 pWrite->pFile, zGarbage, g.iSectorSize, i*g.iSectorSize
danielk197762079062007-08-15 17:08:46 +0000327 );
328 }
danielk1977a98d7b42008-01-18 13:42:54 +0000329 crash_free(zGarbage);
danielk197762079062007-08-15 17:08:46 +0000330 }else{
331 rc = SQLITE_NOMEM;
332 }
333
334 ppPtr = &pWrite->pNext;
335 break;
336 }
337
338 default:
339 assert(!"Cannot happen");
340 }
341
342 if( pWrite==pFinal ) break;
drh1a235932005-11-29 18:37:15 +0000343 }
danielk197762079062007-08-15 17:08:46 +0000344
345 if( rc==SQLITE_OK && isCrash ){
346 exit(-1);
347 }
348
danielk1977967a4a12007-08-20 14:23:44 +0000349 for(pWrite=g.pWriteList; pWrite && pWrite->pNext; pWrite=pWrite->pNext);
350 g.pWriteListEnd = pWrite;
351
drh1a235932005-11-29 18:37:15 +0000352 return rc;
drh9c06c952005-11-26 00:25:00 +0000353}
354
355/*
danielk197762079062007-08-15 17:08:46 +0000356** Add an entry to the end of the write-list.
drh18839212005-11-26 03:43:23 +0000357*/
danielk197762079062007-08-15 17:08:46 +0000358static int writeListAppend(
359 sqlite3_file *pFile,
360 sqlite3_int64 iOffset,
361 const u8 *zBuf,
362 int nBuf
363){
364 WriteBuffer *pNew;
365
366 assert((zBuf && nBuf) || (!nBuf && !zBuf));
367
danielk1977a98d7b42008-01-18 13:42:54 +0000368 pNew = (WriteBuffer *)crash_malloc(sizeof(WriteBuffer) + nBuf);
drha018dc72007-12-14 17:22:23 +0000369 if( pNew==0 ){
370 fprintf(stderr, "out of memory in the crash simulator\n");
371 }
danielk1977a98d7b42008-01-18 13:42:54 +0000372 memset(pNew, 0, sizeof(WriteBuffer)+nBuf);
danielk197762079062007-08-15 17:08:46 +0000373 pNew->iOffset = iOffset;
374 pNew->nBuf = nBuf;
375 pNew->pFile = (CrashFile *)pFile;
376 if( zBuf ){
377 pNew->zBuf = (u8 *)&pNew[1];
378 memcpy(pNew->zBuf, zBuf, nBuf);
379 }
380
381 if( g.pWriteList ){
danielk1977967a4a12007-08-20 14:23:44 +0000382 assert(g.pWriteListEnd);
383 g.pWriteListEnd->pNext = pNew;
danielk197762079062007-08-15 17:08:46 +0000384 }else{
385 g.pWriteList = pNew;
386 }
danielk1977967a4a12007-08-20 14:23:44 +0000387 g.pWriteListEnd = pNew;
danielk197762079062007-08-15 17:08:46 +0000388
drh1a235932005-11-29 18:37:15 +0000389 return SQLITE_OK;
390}
391
392/*
danielk197762079062007-08-15 17:08:46 +0000393** Close a crash-file.
drh1a235932005-11-29 18:37:15 +0000394*/
danielk19771e536952007-08-16 10:09:01 +0000395static int cfClose(sqlite3_file *pFile){
danielk197762079062007-08-15 17:08:46 +0000396 CrashFile *pCrash = (CrashFile *)pFile;
397 writeListSync(pCrash, 0);
danielk1977f1da17a2007-08-21 13:07:46 +0000398 sqlite3OsClose(pCrash->pRealFile);
danielk197762079062007-08-15 17:08:46 +0000399 return SQLITE_OK;
drh1a235932005-11-29 18:37:15 +0000400}
401
402/*
danielk197762079062007-08-15 17:08:46 +0000403** Read data from a crash-file.
drh1a235932005-11-29 18:37:15 +0000404*/
danielk19771e536952007-08-16 10:09:01 +0000405static int cfRead(
406 sqlite3_file *pFile,
407 void *zBuf,
408 int iAmt,
409 sqlite_int64 iOfst
410){
danielk197762079062007-08-15 17:08:46 +0000411 CrashFile *pCrash = (CrashFile *)pFile;
danielk197762079062007-08-15 17:08:46 +0000412
413 /* Check the file-size to see if this is a short-read */
danielk1977967a4a12007-08-20 14:23:44 +0000414 if( pCrash->iSize<(iOfst+iAmt) ){
danielk197762079062007-08-15 17:08:46 +0000415 return SQLITE_IOERR_SHORT_READ;
416 }
417
danielk1977967a4a12007-08-20 14:23:44 +0000418 memcpy(zBuf, &pCrash->zData[iOfst], iAmt);
419 return SQLITE_OK;
drh18839212005-11-26 03:43:23 +0000420}
421
422/*
danielk197762079062007-08-15 17:08:46 +0000423** Write data to a crash-file.
danielk1977b4721172007-03-19 05:54:48 +0000424*/
danielk19771e536952007-08-16 10:09:01 +0000425static int cfWrite(
426 sqlite3_file *pFile,
427 const void *zBuf,
428 int iAmt,
429 sqlite_int64 iOfst
430){
danielk1977967a4a12007-08-20 14:23:44 +0000431 CrashFile *pCrash = (CrashFile *)pFile;
432 if( iAmt+iOfst>pCrash->iSize ){
drh7da5fcb2012-03-30 14:59:43 +0000433 pCrash->iSize = (int)(iAmt+iOfst);
danielk1977967a4a12007-08-20 14:23:44 +0000434 }
435 while( pCrash->iSize>pCrash->nData ){
drh4a50aac2007-08-23 02:47:53 +0000436 u8 *zNew;
danielk1977967a4a12007-08-20 14:23:44 +0000437 int nNew = (pCrash->nData*2) + 4096;
danielk1977a98d7b42008-01-18 13:42:54 +0000438 zNew = crash_realloc(pCrash->zData, nNew);
danielk1977967a4a12007-08-20 14:23:44 +0000439 if( !zNew ){
440 return SQLITE_NOMEM;
441 }
442 memset(&zNew[pCrash->nData], 0, nNew-pCrash->nData);
443 pCrash->nData = nNew;
444 pCrash->zData = zNew;
445 }
446 memcpy(&pCrash->zData[iOfst], zBuf, iAmt);
danielk197762079062007-08-15 17:08:46 +0000447 return writeListAppend(pFile, iOfst, zBuf, iAmt);
danielk1977b4721172007-03-19 05:54:48 +0000448}
449
450/*
danielk197762079062007-08-15 17:08:46 +0000451** Truncate a crash-file.
drh054889e2005-11-30 03:20:31 +0000452*/
danielk19771e536952007-08-16 10:09:01 +0000453static int cfTruncate(sqlite3_file *pFile, sqlite_int64 size){
danielk1977967a4a12007-08-20 14:23:44 +0000454 CrashFile *pCrash = (CrashFile *)pFile;
455 assert(size>=0);
456 if( pCrash->iSize>size ){
drh7da5fcb2012-03-30 14:59:43 +0000457 pCrash->iSize = (int)size;
danielk1977967a4a12007-08-20 14:23:44 +0000458 }
danielk197762079062007-08-15 17:08:46 +0000459 return writeListAppend(pFile, size, 0, 0);
460}
461
462/*
463** Sync a crash-file.
464*/
danielk19771e536952007-08-16 10:09:01 +0000465static int cfSync(sqlite3_file *pFile, int flags){
danielk197762079062007-08-15 17:08:46 +0000466 CrashFile *pCrash = (CrashFile *)pFile;
467 int isCrash = 0;
468
danielk1977967a4a12007-08-20 14:23:44 +0000469 const char *zName = pCrash->zName;
470 const char *zCrashFile = g.zCrashFile;
drh83cc1392012-04-19 18:04:28 +0000471 int nName = (int)strlen(zName);
472 int nCrashFile = (int)strlen(zCrashFile);
danielk1977967a4a12007-08-20 14:23:44 +0000473
474 if( nCrashFile>0 && zCrashFile[nCrashFile-1]=='*' ){
475 nCrashFile--;
476 if( nName>nCrashFile ) nName = nCrashFile;
477 }
478
mistachkinf8a78462012-03-08 20:00:36 +0000479#ifdef TRACE_CRASHTEST
480 printf("cfSync(): nName = %d, nCrashFile = %d, zName = %s, zCrashFile = %s\n",
481 nName, nCrashFile, zName, zCrashFile);
482#endif
483
danielk1977967a4a12007-08-20 14:23:44 +0000484 if( nName==nCrashFile && 0==memcmp(zName, zCrashFile, nName) ){
mistachkinf8a78462012-03-08 20:00:36 +0000485#ifdef TRACE_CRASHTEST
486 printf("cfSync(): name matched, g.iCrash = %d\n", g.iCrash);
487#endif
danielk1977967a4a12007-08-20 14:23:44 +0000488 if( (--g.iCrash)==0 ) isCrash = 1;
danielk197762079062007-08-15 17:08:46 +0000489 }
490
491 return writeListSync(pCrash, isCrash);
492}
493
494/*
495** Return the current file-size of the crash-file.
496*/
danielk19771e536952007-08-16 10:09:01 +0000497static int cfFileSize(sqlite3_file *pFile, sqlite_int64 *pSize){
danielk197762079062007-08-15 17:08:46 +0000498 CrashFile *pCrash = (CrashFile *)pFile;
danielk1977967a4a12007-08-20 14:23:44 +0000499 *pSize = (i64)pCrash->iSize;
danielk197762079062007-08-15 17:08:46 +0000500 return SQLITE_OK;
501}
502
503/*
504** Calls related to file-locks are passed on to the real file handle.
505*/
danielk19771e536952007-08-16 10:09:01 +0000506static int cfLock(sqlite3_file *pFile, int eLock){
danielk197762079062007-08-15 17:08:46 +0000507 return sqlite3OsLock(((CrashFile *)pFile)->pRealFile, eLock);
508}
danielk19771e536952007-08-16 10:09:01 +0000509static int cfUnlock(sqlite3_file *pFile, int eLock){
danielk197762079062007-08-15 17:08:46 +0000510 return sqlite3OsUnlock(((CrashFile *)pFile)->pRealFile, eLock);
511}
danielk1977861f7452008-06-05 11:39:11 +0000512static int cfCheckReservedLock(sqlite3_file *pFile, int *pResOut){
513 return sqlite3OsCheckReservedLock(((CrashFile *)pFile)->pRealFile, pResOut);
danielk197762079062007-08-15 17:08:46 +0000514}
drhcc6bb3e2007-08-31 16:11:35 +0000515static int cfFileControl(sqlite3_file *pFile, int op, void *pArg){
dan67e10d12011-08-23 16:41:06 +0000516 if( op==SQLITE_FCNTL_SIZE_HINT ){
517 CrashFile *pCrash = (CrashFile *)pFile;
518 i64 nByte = *(i64 *)pArg;
519 if( nByte>pCrash->iSize ){
dan422faae2011-08-23 19:46:02 +0000520 if( SQLITE_OK==writeListAppend(pFile, nByte, 0, 0) ){
drh7da5fcb2012-03-30 14:59:43 +0000521 pCrash->iSize = (int)nByte;
dan422faae2011-08-23 19:46:02 +0000522 }
dan67e10d12011-08-23 16:41:06 +0000523 }
dan422faae2011-08-23 19:46:02 +0000524 return SQLITE_OK;
dan67e10d12011-08-23 16:41:06 +0000525 }
drhcc6bb3e2007-08-31 16:11:35 +0000526 return sqlite3OsFileControl(((CrashFile *)pFile)->pRealFile, op, pArg);
danielk197762079062007-08-15 17:08:46 +0000527}
528
529/*
530** The xSectorSize() and xDeviceCharacteristics() functions return
531** the global values configured by the [sqlite_crashparams] tcl
532* interface.
533*/
danielk19771e536952007-08-16 10:09:01 +0000534static int cfSectorSize(sqlite3_file *pFile){
danielk197762079062007-08-15 17:08:46 +0000535 return g.iSectorSize;
536}
danielk19771e536952007-08-16 10:09:01 +0000537static int cfDeviceCharacteristics(sqlite3_file *pFile){
danielk197762079062007-08-15 17:08:46 +0000538 return g.iDeviceCharacteristics;
539}
540
drhd9e5c4f2010-05-12 18:01:39 +0000541/*
542** Pass-throughs for WAL support.
543*/
drh73b64e42010-05-30 19:55:15 +0000544static int cfShmLock(sqlite3_file *pFile, int ofst, int n, int flags){
545 return sqlite3OsShmLock(((CrashFile*)pFile)->pRealFile, ofst, n, flags);
drhd9e5c4f2010-05-12 18:01:39 +0000546}
drh286a2882010-05-20 23:51:06 +0000547static void cfShmBarrier(sqlite3_file *pFile){
548 sqlite3OsShmBarrier(((CrashFile*)pFile)->pRealFile);
549}
drhe11fedc2010-07-14 00:14:30 +0000550static int cfShmUnmap(sqlite3_file *pFile, int delFlag){
551 return sqlite3OsShmUnmap(((CrashFile*)pFile)->pRealFile, delFlag);
drhd9e5c4f2010-05-12 18:01:39 +0000552}
dan18801912010-06-14 14:07:50 +0000553static int cfShmMap(
dan067f3162010-06-14 10:30:12 +0000554 sqlite3_file *pFile, /* Handle open on database file */
dan18801912010-06-14 14:07:50 +0000555 int iRegion, /* Region to retrieve */
556 int sz, /* Size of regions */
dan067f3162010-06-14 10:30:12 +0000557 int w, /* True to extend file if necessary */
558 void volatile **pp /* OUT: Mapped memory */
559){
dan18801912010-06-14 14:07:50 +0000560 return sqlite3OsShmMap(((CrashFile*)pFile)->pRealFile, iRegion, sz, w, pp);
dan067f3162010-06-14 10:30:12 +0000561}
drhd9e5c4f2010-05-12 18:01:39 +0000562
danielk197762079062007-08-15 17:08:46 +0000563static const sqlite3_io_methods CrashFileVtab = {
drhd9e5c4f2010-05-12 18:01:39 +0000564 2, /* iVersion */
danielk197762079062007-08-15 17:08:46 +0000565 cfClose, /* xClose */
566 cfRead, /* xRead */
567 cfWrite, /* xWrite */
568 cfTruncate, /* xTruncate */
569 cfSync, /* xSync */
570 cfFileSize, /* xFileSize */
571 cfLock, /* xLock */
572 cfUnlock, /* xUnlock */
573 cfCheckReservedLock, /* xCheckReservedLock */
drhcc6bb3e2007-08-31 16:11:35 +0000574 cfFileControl, /* xFileControl */
danielk197762079062007-08-15 17:08:46 +0000575 cfSectorSize, /* xSectorSize */
drhd9e5c4f2010-05-12 18:01:39 +0000576 cfDeviceCharacteristics, /* xDeviceCharacteristics */
drh6b017cc2010-06-14 18:01:46 +0000577 cfShmMap, /* xShmMap */
danda9fe0c2010-07-13 18:44:03 +0000578 cfShmLock, /* xShmLock */
drh286a2882010-05-20 23:51:06 +0000579 cfShmBarrier, /* xShmBarrier */
drhe11fedc2010-07-14 00:14:30 +0000580 cfShmUnmap /* xShmUnmap */
drh054889e2005-11-30 03:20:31 +0000581};
582
drh054889e2005-11-30 03:20:31 +0000583/*
drhd677b3d2007-08-20 22:48:41 +0000584** Application data for the crash VFS
585*/
586struct crashAppData {
danielk1977f1da17a2007-08-21 13:07:46 +0000587 sqlite3_vfs *pOrig; /* Wrapped vfs structure */
drhd677b3d2007-08-20 22:48:41 +0000588};
589
590/*
danielk19770e87b702007-08-25 12:29:30 +0000591** Open a crash-file file handle.
danielk1977967a4a12007-08-20 14:23:44 +0000592**
593** The caller will have allocated pVfs->szOsFile bytes of space
594** at pFile. This file uses this space for the CrashFile structure
595** and allocates space for the "real" file structure using
596** sqlite3_malloc(). The assumption here is (pVfs->szOsFile) is
597** equal or greater than sizeof(CrashFile).
drh054889e2005-11-30 03:20:31 +0000598*/
danielk1977f1da17a2007-08-21 13:07:46 +0000599static int cfOpen(
danielk1977f55b8992007-08-24 08:15:53 +0000600 sqlite3_vfs *pCfVfs,
danielk197762079062007-08-15 17:08:46 +0000601 const char *zName,
602 sqlite3_file *pFile,
603 int flags,
604 int *pOutFlags
605){
danielk1977f55b8992007-08-24 08:15:53 +0000606 sqlite3_vfs *pVfs = (sqlite3_vfs *)pCfVfs->pAppData;
danielk1977967a4a12007-08-20 14:23:44 +0000607 int rc;
danielk1977f1da17a2007-08-21 13:07:46 +0000608 CrashFile *pWrapper = (CrashFile *)pFile;
drh4a50aac2007-08-23 02:47:53 +0000609 sqlite3_file *pReal = (sqlite3_file*)&pWrapper[1];
danielk1977f1da17a2007-08-21 13:07:46 +0000610
611 memset(pWrapper, 0, sizeof(CrashFile));
612 rc = sqlite3OsOpen(pVfs, zName, pReal, flags, pOutFlags);
613
614 if( rc==SQLITE_OK ){
615 i64 iSize;
616 pWrapper->pMethod = &CrashFileVtab;
617 pWrapper->zName = (char *)zName;
618 pWrapper->pRealFile = pReal;
619 rc = sqlite3OsFileSize(pReal, &iSize);
620 pWrapper->iSize = (int)iSize;
danielk1977d6846d72009-02-11 14:27:04 +0000621 pWrapper->flags = flags;
danielk1977f1da17a2007-08-21 13:07:46 +0000622 }
623 if( rc==SQLITE_OK ){
624 pWrapper->nData = (4096 + pWrapper->iSize);
danielk1977a98d7b42008-01-18 13:42:54 +0000625 pWrapper->zData = crash_malloc(pWrapper->nData);
danielk1977f1da17a2007-08-21 13:07:46 +0000626 if( pWrapper->zData ){
danielk19772d42c2b2009-02-10 14:28:57 +0000627 /* os_unix.c contains an assert() that fails if the caller attempts
628 ** to read data from the 512-byte locking region of a file opened
629 ** with the SQLITE_OPEN_MAIN_DB flag. This region of a database file
630 ** never contains valid data anyhow. So avoid doing such a read here.
dance5c42b2012-10-17 15:28:26 +0000631 **
632 ** UPDATE: It also contains an assert() verifying that each call
633 ** to the xRead() method reads less than 128KB of data.
danielk19772d42c2b2009-02-10 14:28:57 +0000634 */
635 const int isDb = (flags&SQLITE_OPEN_MAIN_DB);
dance5c42b2012-10-17 15:28:26 +0000636 i64 iOff;
637
danielk1977f1da17a2007-08-21 13:07:46 +0000638 memset(pWrapper->zData, 0, pWrapper->nData);
dance5c42b2012-10-17 15:28:26 +0000639 for(iOff=0; iOff<pWrapper->iSize; iOff += 512){
mistachkin5f070c72012-10-18 10:35:19 +0000640 int nRead = pWrapper->iSize - (int)iOff;
dance5c42b2012-10-17 15:28:26 +0000641 if( nRead>512 ) nRead = 512;
642 if( isDb && iOff==PENDING_BYTE ) continue;
643 rc = sqlite3OsRead(pReal, &pWrapper->zData[iOff], nRead, iOff);
danielk19772d42c2b2009-02-10 14:28:57 +0000644 }
danielk1977f1da17a2007-08-21 13:07:46 +0000645 }else{
646 rc = SQLITE_NOMEM;
danielk197762079062007-08-15 17:08:46 +0000647 }
danielk1977f1da17a2007-08-21 13:07:46 +0000648 }
649 if( rc!=SQLITE_OK && pWrapper->pMethod ){
650 sqlite3OsClose(pFile);
danielk197762079062007-08-15 17:08:46 +0000651 }
652 return rc;
653}
654
danielk1977f55b8992007-08-24 08:15:53 +0000655static int cfDelete(sqlite3_vfs *pCfVfs, const char *zPath, int dirSync){
656 sqlite3_vfs *pVfs = (sqlite3_vfs *)pCfVfs->pAppData;
657 return pVfs->xDelete(pVfs, zPath, dirSync);
danielk1977f1da17a2007-08-21 13:07:46 +0000658}
danielk1977861f7452008-06-05 11:39:11 +0000659static int cfAccess(
660 sqlite3_vfs *pCfVfs,
661 const char *zPath,
662 int flags,
663 int *pResOut
664){
danielk1977f55b8992007-08-24 08:15:53 +0000665 sqlite3_vfs *pVfs = (sqlite3_vfs *)pCfVfs->pAppData;
danielk1977861f7452008-06-05 11:39:11 +0000666 return pVfs->xAccess(pVfs, zPath, flags, pResOut);
danielk1977f1da17a2007-08-21 13:07:46 +0000667}
danielk1977adfb9b02007-09-17 07:02:56 +0000668static int cfFullPathname(
669 sqlite3_vfs *pCfVfs,
670 const char *zPath,
671 int nPathOut,
672 char *zPathOut
673){
danielk1977f55b8992007-08-24 08:15:53 +0000674 sqlite3_vfs *pVfs = (sqlite3_vfs *)pCfVfs->pAppData;
danielk1977adfb9b02007-09-17 07:02:56 +0000675 return pVfs->xFullPathname(pVfs, zPath, nPathOut, zPathOut);
danielk1977f1da17a2007-08-21 13:07:46 +0000676}
danielk1977f55b8992007-08-24 08:15:53 +0000677static void *cfDlOpen(sqlite3_vfs *pCfVfs, const char *zPath){
678 sqlite3_vfs *pVfs = (sqlite3_vfs *)pCfVfs->pAppData;
679 return pVfs->xDlOpen(pVfs, zPath);
danielk1977f1da17a2007-08-21 13:07:46 +0000680}
danielk19770e87b702007-08-25 12:29:30 +0000681static void cfDlError(sqlite3_vfs *pCfVfs, int nByte, char *zErrMsg){
682 sqlite3_vfs *pVfs = (sqlite3_vfs *)pCfVfs->pAppData;
683 pVfs->xDlError(pVfs, nByte, zErrMsg);
684}
drhec1724e2008-12-09 01:32:03 +0000685static void (*cfDlSym(sqlite3_vfs *pCfVfs, void *pH, const char *zSym))(void){
danielk19770e87b702007-08-25 12:29:30 +0000686 sqlite3_vfs *pVfs = (sqlite3_vfs *)pCfVfs->pAppData;
drhec1724e2008-12-09 01:32:03 +0000687 return pVfs->xDlSym(pVfs, pH, zSym);
danielk19770e87b702007-08-25 12:29:30 +0000688}
689static void cfDlClose(sqlite3_vfs *pCfVfs, void *pHandle){
690 sqlite3_vfs *pVfs = (sqlite3_vfs *)pCfVfs->pAppData;
691 pVfs->xDlClose(pVfs, pHandle);
692}
danielk1977f55b8992007-08-24 08:15:53 +0000693static int cfRandomness(sqlite3_vfs *pCfVfs, int nByte, char *zBufOut){
694 sqlite3_vfs *pVfs = (sqlite3_vfs *)pCfVfs->pAppData;
695 return pVfs->xRandomness(pVfs, nByte, zBufOut);
danielk1977f1da17a2007-08-21 13:07:46 +0000696}
danielk1977f55b8992007-08-24 08:15:53 +0000697static int cfSleep(sqlite3_vfs *pCfVfs, int nMicro){
698 sqlite3_vfs *pVfs = (sqlite3_vfs *)pCfVfs->pAppData;
699 return pVfs->xSleep(pVfs, nMicro);
danielk1977f1da17a2007-08-21 13:07:46 +0000700}
danielk1977f55b8992007-08-24 08:15:53 +0000701static int cfCurrentTime(sqlite3_vfs *pCfVfs, double *pTimeOut){
702 sqlite3_vfs *pVfs = (sqlite3_vfs *)pCfVfs->pAppData;
703 return pVfs->xCurrentTime(pVfs, pTimeOut);
danielk1977f1da17a2007-08-21 13:07:46 +0000704}
705
danielk19772ca0f862007-08-23 08:06:44 +0000706static int processDevSymArgs(
707 Tcl_Interp *interp,
708 int objc,
709 Tcl_Obj *CONST objv[],
710 int *piDeviceChar,
711 int *piSectorSize
712){
713 struct DeviceFlag {
714 char *zName;
715 int iValue;
716 } aFlag[] = {
drhcb15f352011-12-23 01:04:17 +0000717 { "atomic", SQLITE_IOCAP_ATOMIC },
718 { "atomic512", SQLITE_IOCAP_ATOMIC512 },
719 { "atomic1k", SQLITE_IOCAP_ATOMIC1K },
720 { "atomic2k", SQLITE_IOCAP_ATOMIC2K },
721 { "atomic4k", SQLITE_IOCAP_ATOMIC4K },
722 { "atomic8k", SQLITE_IOCAP_ATOMIC8K },
723 { "atomic16k", SQLITE_IOCAP_ATOMIC16K },
724 { "atomic32k", SQLITE_IOCAP_ATOMIC32K },
725 { "atomic64k", SQLITE_IOCAP_ATOMIC64K },
726 { "sequential", SQLITE_IOCAP_SEQUENTIAL },
727 { "safe_append", SQLITE_IOCAP_SAFE_APPEND },
728 { "powersafe_overwrite", SQLITE_IOCAP_POWERSAFE_OVERWRITE },
danielk19772ca0f862007-08-23 08:06:44 +0000729 { 0, 0 }
730 };
731
732 int i;
733 int iDc = 0;
734 int iSectorSize = 0;
735 int setSectorsize = 0;
736 int setDeviceChar = 0;
737
738 for(i=0; i<objc; i+=2){
739 int nOpt;
740 char *zOpt = Tcl_GetStringFromObj(objv[i], &nOpt);
741
742 if( (nOpt>11 || nOpt<2 || strncmp("-sectorsize", zOpt, nOpt))
743 && (nOpt>16 || nOpt<2 || strncmp("-characteristics", zOpt, nOpt))
744 ){
745 Tcl_AppendResult(interp,
746 "Bad option: \"", zOpt,
747 "\" - must be \"-characteristics\" or \"-sectorsize\"", 0
748 );
749 return TCL_ERROR;
750 }
751 if( i==objc-1 ){
752 Tcl_AppendResult(interp, "Option requires an argument: \"", zOpt, "\"",0);
753 return TCL_ERROR;
754 }
755
756 if( zOpt[1]=='s' ){
757 if( Tcl_GetIntFromObj(interp, objv[i+1], &iSectorSize) ){
758 return TCL_ERROR;
759 }
760 setSectorsize = 1;
761 }else{
762 int j;
763 Tcl_Obj **apObj;
764 int nObj;
765 if( Tcl_ListObjGetElements(interp, objv[i+1], &nObj, &apObj) ){
766 return TCL_ERROR;
767 }
768 for(j=0; j<nObj; j++){
769 int rc;
770 int iChoice;
771 Tcl_Obj *pFlag = Tcl_DuplicateObj(apObj[j]);
772 Tcl_IncrRefCount(pFlag);
773 Tcl_UtfToLower(Tcl_GetString(pFlag));
774
775 rc = Tcl_GetIndexFromObjStruct(
776 interp, pFlag, aFlag, sizeof(aFlag[0]), "no such flag", 0, &iChoice
777 );
778 Tcl_DecrRefCount(pFlag);
779 if( rc ){
780 return TCL_ERROR;
781 }
782
783 iDc |= aFlag[iChoice].iValue;
784 }
785 setDeviceChar = 1;
786 }
787 }
788
789 if( setDeviceChar ){
790 *piDeviceChar = iDc;
791 }
792 if( setSectorsize ){
793 *piSectorSize = iSectorSize;
794 }
795
796 return TCL_OK;
797}
798
drh054889e2005-11-30 03:20:31 +0000799/*
danielk1977ca0c8972007-09-01 09:02:53 +0000800** tclcmd: sqlite_crash_enable ENABLE
801**
802** Parameter ENABLE must be a boolean value. If true, then the "crash"
803** vfs is added to the system. If false, it is removed.
804*/
805static int crashEnableCmd(
806 void * clientData,
807 Tcl_Interp *interp,
808 int objc,
809 Tcl_Obj *CONST objv[]
810){
811 int isEnable;
812 static sqlite3_vfs crashVfs = {
drh4c846bb2010-05-03 16:36:55 +0000813 2, /* iVersion */
danielk1977ca0c8972007-09-01 09:02:53 +0000814 0, /* szOsFile */
815 0, /* mxPathname */
816 0, /* pNext */
817 "crash", /* zName */
818 0, /* pAppData */
819
820 cfOpen, /* xOpen */
821 cfDelete, /* xDelete */
822 cfAccess, /* xAccess */
danielk1977ca0c8972007-09-01 09:02:53 +0000823 cfFullPathname, /* xFullPathname */
824 cfDlOpen, /* xDlOpen */
825 cfDlError, /* xDlError */
826 cfDlSym, /* xDlSym */
827 cfDlClose, /* xDlClose */
828 cfRandomness, /* xRandomness */
829 cfSleep, /* xSleep */
drhf2424c52010-04-26 00:04:55 +0000830 cfCurrentTime, /* xCurrentTime */
drh4c846bb2010-05-03 16:36:55 +0000831 0, /* xGetlastError */
drh4c846bb2010-05-03 16:36:55 +0000832 0, /* xCurrentTimeInt64 */
danielk1977ca0c8972007-09-01 09:02:53 +0000833 };
834
835 if( objc!=2 ){
836 Tcl_WrongNumArgs(interp, 1, objv, "ENABLE");
837 return TCL_ERROR;
838 }
839
840 if( Tcl_GetBooleanFromObj(interp, objv[1], &isEnable) ){
841 return TCL_ERROR;
842 }
843
844 if( (isEnable && crashVfs.pAppData) || (!isEnable && !crashVfs.pAppData) ){
845 return TCL_OK;
846 }
847
848 if( crashVfs.pAppData==0 ){
849 sqlite3_vfs *pOriginalVfs = sqlite3_vfs_find(0);
850 crashVfs.mxPathname = pOriginalVfs->mxPathname;
851 crashVfs.pAppData = (void *)pOriginalVfs;
852 crashVfs.szOsFile = sizeof(CrashFile) + pOriginalVfs->szOsFile;
853 sqlite3_vfs_register(&crashVfs, 0);
854 }else{
855 crashVfs.pAppData = 0;
856 sqlite3_vfs_unregister(&crashVfs);
857 }
858
859 return TCL_OK;
860}
861
862/*
danielk197762079062007-08-15 17:08:46 +0000863** tclcmd: sqlite_crashparams ?OPTIONS? DELAY CRASHFILE
drh9c06c952005-11-26 00:25:00 +0000864**
865** This procedure implements a TCL command that enables crash testing
866** in testfixture. Once enabled, crash testing cannot be disabled.
danielk197762079062007-08-15 17:08:46 +0000867**
868** Available options are "-characteristics" and "-sectorsize". Both require
869** an argument. For -sectorsize, this is the simulated sector size in
870** bytes. For -characteristics, the argument must be a list of io-capability
871** flags to simulate. Valid flags are "atomic", "atomic512", "atomic1K",
872** "atomic2K", "atomic4K", "atomic8K", "atomic16K", "atomic32K",
873** "atomic64K", "sequential" and "safe_append".
874**
875** Example:
876**
877** sqlite_crashparams -sect 1024 -char {atomic sequential} ./test.db 1
878**
drh9c06c952005-11-26 00:25:00 +0000879*/
880static int crashParamsObjCmd(
881 void * clientData,
882 Tcl_Interp *interp,
883 int objc,
884 Tcl_Obj *CONST objv[]
885){
danielk197762079062007-08-15 17:08:46 +0000886 int iDelay;
887 const char *zCrashFile;
drh153c62c2007-08-24 03:51:33 +0000888 int nCrashFile, iDc, iSectorSize;
drhd677b3d2007-08-20 22:48:41 +0000889
drh153c62c2007-08-24 03:51:33 +0000890 iDc = -1;
891 iSectorSize = -1;
danielk197762079062007-08-15 17:08:46 +0000892
danielk197762079062007-08-15 17:08:46 +0000893 if( objc<3 ){
894 Tcl_WrongNumArgs(interp, 1, objv, "?OPTIONS? DELAY CRASHFILE");
danielk1977b4b47412007-08-17 15:53:36 +0000895 goto error;
drh9c06c952005-11-26 00:25:00 +0000896 }
danielk197762079062007-08-15 17:08:46 +0000897
danielk1977967a4a12007-08-20 14:23:44 +0000898 zCrashFile = Tcl_GetStringFromObj(objv[objc-1], &nCrashFile);
danielk197762079062007-08-15 17:08:46 +0000899 if( nCrashFile>=sizeof(g.zCrashFile) ){
900 Tcl_AppendResult(interp, "Filename is too long: \"", zCrashFile, "\"", 0);
danielk1977b4b47412007-08-17 15:53:36 +0000901 goto error;
drh9c06c952005-11-26 00:25:00 +0000902 }
danielk197762079062007-08-15 17:08:46 +0000903 if( Tcl_GetIntFromObj(interp, objv[objc-2], &iDelay) ){
danielk1977b4b47412007-08-17 15:53:36 +0000904 goto error;
danielk197762079062007-08-15 17:08:46 +0000905 }
906
danielk19772ca0f862007-08-23 08:06:44 +0000907 if( processDevSymArgs(interp, objc-3, &objv[1], &iDc, &iSectorSize) ){
908 return TCL_ERROR;
danielk197759a33f92007-03-17 10:26:59 +0000909 }
danielk197762079062007-08-15 17:08:46 +0000910
danielk19772ca0f862007-08-23 08:06:44 +0000911 if( iDc>=0 ){
danielk197762079062007-08-15 17:08:46 +0000912 g.iDeviceCharacteristics = iDc;
913 }
danielk19772ca0f862007-08-23 08:06:44 +0000914 if( iSectorSize>=0 ){
danielk197762079062007-08-15 17:08:46 +0000915 g.iSectorSize = iSectorSize;
916 }
danielk19772ca0f862007-08-23 08:06:44 +0000917
danielk197762079062007-08-15 17:08:46 +0000918 g.iCrash = iDelay;
919 memcpy(g.zCrashFile, zCrashFile, nCrashFile+1);
drh66560ad2006-01-06 14:32:19 +0000920 sqlite3CrashTestEnable = 1;
drh9c06c952005-11-26 00:25:00 +0000921 return TCL_OK;
danielk1977b4b47412007-08-17 15:53:36 +0000922
923error:
danielk1977b4b47412007-08-17 15:53:36 +0000924 return TCL_ERROR;
drh9c06c952005-11-26 00:25:00 +0000925}
926
danielk19772ca0f862007-08-23 08:06:44 +0000927static int devSymObjCmd(
928 void * clientData,
929 Tcl_Interp *interp,
930 int objc,
931 Tcl_Obj *CONST objv[]
932){
danielk1977bf260972008-01-22 11:50:13 +0000933 void devsym_register(int iDeviceChar, int iSectorSize);
danielk19772ca0f862007-08-23 08:06:44 +0000934
935 int iDc = -1;
936 int iSectorSize = -1;
danielk1977bf260972008-01-22 11:50:13 +0000937
danielk19772ca0f862007-08-23 08:06:44 +0000938 if( processDevSymArgs(interp, objc-1, &objv[1], &iDc, &iSectorSize) ){
939 return TCL_ERROR;
940 }
danielk1977bf260972008-01-22 11:50:13 +0000941 devsym_register(iDc, iSectorSize);
danielk19772ca0f862007-08-23 08:06:44 +0000942
943 return TCL_OK;
944}
945
danielk1977a0fc7292008-12-20 18:33:59 +0000946/*
947** tclcmd: register_jt_vfs ?-default? PARENT-VFS
948*/
949static int jtObjCmd(
950 void * clientData,
951 Tcl_Interp *interp,
952 int objc,
953 Tcl_Obj *CONST objv[]
954){
955 int jt_register(char *, int);
956 char *zParent = 0;
957
958 if( objc!=2 && objc!=3 ){
959 Tcl_WrongNumArgs(interp, 1, objv, "?-default? PARENT-VFS");
960 return TCL_ERROR;
961 }
962 zParent = Tcl_GetString(objv[1]);
963 if( objc==3 ){
964 if( strcmp(zParent, "-default") ){
965 Tcl_AppendResult(interp,
966 "bad option \"", zParent, "\": must be -default", 0
967 );
968 return TCL_ERROR;
969 }
970 zParent = Tcl_GetString(objv[2]);
971 }
972
973 if( !(*zParent) ){
974 zParent = 0;
975 }
976 if( jt_register(zParent, objc==3) ){
977 Tcl_AppendResult(interp, "Error in jt_register", 0);
978 return TCL_ERROR;
979 }
980
981 return TCL_OK;
982}
983
984/*
985** tclcmd: unregister_jt_vfs
986*/
987static int jtUnregisterObjCmd(
988 void * clientData,
989 Tcl_Interp *interp,
990 int objc,
991 Tcl_Obj *CONST objv[]
992){
993 void jt_unregister(void);
994
995 if( objc!=1 ){
996 Tcl_WrongNumArgs(interp, 1, objv, "");
997 return TCL_ERROR;
998 }
999
1000 jt_unregister();
1001 return TCL_OK;
1002}
1003
drh198bf392006-01-06 21:52:49 +00001004#endif /* SQLITE_OMIT_DISKIO */
1005
drh9c06c952005-11-26 00:25:00 +00001006/*
1007** This procedure registers the TCL procedures defined in this file.
1008*/
1009int Sqlitetest6_Init(Tcl_Interp *interp){
drh198bf392006-01-06 21:52:49 +00001010#ifndef SQLITE_OMIT_DISKIO
danielk1977ca0c8972007-09-01 09:02:53 +00001011 Tcl_CreateObjCommand(interp, "sqlite3_crash_enable", crashEnableCmd, 0, 0);
drh9c06c952005-11-26 00:25:00 +00001012 Tcl_CreateObjCommand(interp, "sqlite3_crashparams", crashParamsObjCmd, 0, 0);
danielk19772ca0f862007-08-23 08:06:44 +00001013 Tcl_CreateObjCommand(interp, "sqlite3_simulate_device", devSymObjCmd, 0, 0);
danielk1977a0fc7292008-12-20 18:33:59 +00001014 Tcl_CreateObjCommand(interp, "register_jt_vfs", jtObjCmd, 0, 0);
1015 Tcl_CreateObjCommand(interp, "unregister_jt_vfs", jtUnregisterObjCmd, 0, 0);
drh198bf392006-01-06 21:52:49 +00001016#endif
drh9c06c952005-11-26 00:25:00 +00001017 return TCL_OK;
1018}
1019
1020#endif /* SQLITE_TEST */