blob: 24fe725f78b44d6e7410261ff00d92f3045f22b2 [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 u8 *zData; /* Buffer containing file contents */
drha703f502013-10-15 14:29:32 +0000137 int nData; /* Size of buffer allocated at zData */
138 i64 iSize; /* Size of file in bytes */
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;
danielk1977d6846d72009-02-11 14:27:04 +0000176 if( (iAmt-iSkip)>0 ){
drh7da5fcb2012-03-30 14:59:43 +0000177 rc = sqlite3OsWrite(p->pRealFile, &z[iSkip], (int)(iAmt-iSkip), iOff+iSkip);
danielk1977d6846d72009-02-11 14:27:04 +0000178 }
179 return rc;
180}
181
182/*
danielk197762079062007-08-15 17:08:46 +0000183** Flush the write-list as if xSync() had been called on file handle
184** pFile. If isCrash is true, simulate a crash.
drh9c06c952005-11-26 00:25:00 +0000185*/
danielk197762079062007-08-15 17:08:46 +0000186static int writeListSync(CrashFile *pFile, int isCrash){
187 int rc = SQLITE_OK;
188 int iDc = g.iDeviceCharacteristics;
drh66560ad2006-01-06 14:32:19 +0000189
danielk197762079062007-08-15 17:08:46 +0000190 WriteBuffer *pWrite;
191 WriteBuffer **ppPtr;
drh66560ad2006-01-06 14:32:19 +0000192
danielk1977f55b8992007-08-24 08:15:53 +0000193 /* If this is not a crash simulation, set pFinal to point to the
194 ** last element of the write-list that is associated with file handle
195 ** pFile.
196 **
197 ** If this is a crash simulation, set pFinal to an arbitrarily selected
198 ** element of the write-list.
danielk197762079062007-08-15 17:08:46 +0000199 */
200 WriteBuffer *pFinal = 0;
201 if( !isCrash ){
202 for(pWrite=g.pWriteList; pWrite; pWrite=pWrite->pNext){
203 if( pWrite->pFile==pFile ){
204 pFinal = pWrite;
205 }
206 }
danielk1977f55b8992007-08-24 08:15:53 +0000207 }else if( iDc&(SQLITE_IOCAP_SEQUENTIAL|SQLITE_IOCAP_SAFE_APPEND) ){
208 int nWrite = 0;
209 int iFinal;
210 for(pWrite=g.pWriteList; pWrite; pWrite=pWrite->pNext) nWrite++;
drh2fa18682008-03-19 14:15:34 +0000211 sqlite3_randomness(sizeof(int), &iFinal);
danielk1977f55b8992007-08-24 08:15:53 +0000212 iFinal = ((iFinal<0)?-1*iFinal:iFinal)%nWrite;
213 for(pWrite=g.pWriteList; iFinal>0; pWrite=pWrite->pNext) iFinal--;
214 pFinal = pWrite;
drh1a235932005-11-29 18:37:15 +0000215 }
drh66560ad2006-01-06 14:32:19 +0000216
danielk1977f8940ae2007-08-23 11:07:10 +0000217#ifdef TRACE_CRASHTEST
danfe912512016-05-24 16:20:51 +0000218 if( pFile ){
219 printf("Sync %s (is %s crash)\n", pFile->zName, (isCrash?"a":"not a"));
220 }
danielk1977f8940ae2007-08-23 11:07:10 +0000221#endif
222
danielk197762079062007-08-15 17:08:46 +0000223 ppPtr = &g.pWriteList;
224 for(pWrite=*ppPtr; rc==SQLITE_OK && pWrite; pWrite=*ppPtr){
danielk1977967a4a12007-08-20 14:23:44 +0000225 sqlite3_file *pRealFile = pWrite->pFile->pRealFile;
danielk197762079062007-08-15 17:08:46 +0000226
227 /* (eAction==1) -> write block out normally,
228 ** (eAction==2) -> do nothing,
229 ** (eAction==3) -> trash sectors.
230 */
231 int eAction = 0;
232 if( !isCrash ){
233 eAction = 2;
234 if( (pWrite->pFile==pFile || iDc&SQLITE_IOCAP_SEQUENTIAL) ){
235 eAction = 1;
236 }
237 }else{
238 char random;
drh2fa18682008-03-19 14:15:34 +0000239 sqlite3_randomness(1, &random);
danielk197762079062007-08-15 17:08:46 +0000240
danielk1977f55b8992007-08-24 08:15:53 +0000241 /* Do not select option 3 (sector trashing) if the IOCAP_ATOMIC flag
242 ** is set or this is an OsTruncate(), not an Oswrite().
243 */
244 if( (iDc&SQLITE_IOCAP_ATOMIC) || (pWrite->zBuf==0) ){
danielk197762079062007-08-15 17:08:46 +0000245 random &= 0x01;
246 }
247
danielk1977f55b8992007-08-24 08:15:53 +0000248 /* If IOCAP_SEQUENTIAL is set and this is not the final entry
249 ** in the truncated write-list, always select option 1 (write
250 ** out correctly).
251 */
252 if( (iDc&SQLITE_IOCAP_SEQUENTIAL && pWrite!=pFinal) ){
253 random = 0;
254 }
255
256 /* If IOCAP_SAFE_APPEND is set and this OsWrite() operation is
257 ** an append (first byte of the written region is 1 byte past the
258 ** current EOF), always select option 1 (write out correctly).
259 */
260 if( iDc&SQLITE_IOCAP_SAFE_APPEND && pWrite->zBuf ){
261 i64 iSize;
262 sqlite3OsFileSize(pRealFile, &iSize);
263 if( iSize==pWrite->iOffset ){
264 random = 0;
265 }
266 }
267
danielk197762079062007-08-15 17:08:46 +0000268 if( (random&0x06)==0x06 ){
269 eAction = 3;
270 }else{
271 eAction = ((random&0x01)?2:1);
272 }
273 }
274
275 switch( eAction ){
276 case 1: { /* Write out correctly */
277 if( pWrite->zBuf ){
danielk1977d6846d72009-02-11 14:27:04 +0000278 rc = writeDbFile(
279 pWrite->pFile, pWrite->zBuf, pWrite->nBuf, pWrite->iOffset
danielk197762079062007-08-15 17:08:46 +0000280 );
281 }else{
danielk1977967a4a12007-08-20 14:23:44 +0000282 rc = sqlite3OsTruncate(pRealFile, pWrite->iOffset);
danielk197762079062007-08-15 17:08:46 +0000283 }
284 *ppPtr = pWrite->pNext;
danielk1977f8940ae2007-08-23 11:07:10 +0000285#ifdef TRACE_CRASHTEST
286 if( isCrash ){
danielk1977f55b8992007-08-24 08:15:53 +0000287 printf("Writing %d bytes @ %d (%s)\n",
288 pWrite->nBuf, (int)pWrite->iOffset, pWrite->pFile->zName
289 );
danielk1977f8940ae2007-08-23 11:07:10 +0000290 }
291#endif
danielk1977a98d7b42008-01-18 13:42:54 +0000292 crash_free(pWrite);
danielk197762079062007-08-15 17:08:46 +0000293 break;
294 }
295 case 2: { /* Do nothing */
296 ppPtr = &pWrite->pNext;
danielk1977f8940ae2007-08-23 11:07:10 +0000297#ifdef TRACE_CRASHTEST
298 if( isCrash ){
danielk1977f55b8992007-08-24 08:15:53 +0000299 printf("Omiting %d bytes @ %d (%s)\n",
300 pWrite->nBuf, (int)pWrite->iOffset, pWrite->pFile->zName
301 );
danielk1977f8940ae2007-08-23 11:07:10 +0000302 }
303#endif
danielk197762079062007-08-15 17:08:46 +0000304 break;
305 }
306 case 3: { /* Trash sectors */
307 u8 *zGarbage;
drh7da5fcb2012-03-30 14:59:43 +0000308 int iFirst = (int)(pWrite->iOffset/g.iSectorSize);
309 int iLast = (int)((pWrite->iOffset+pWrite->nBuf-1)/g.iSectorSize);
danielk1977967a4a12007-08-20 14:23:44 +0000310
311 assert(pWrite->zBuf);
danielk197762079062007-08-15 17:08:46 +0000312
danielk1977f8940ae2007-08-23 11:07:10 +0000313#ifdef TRACE_CRASHTEST
mistachkine1b461b2012-10-18 09:39:16 +0000314 printf("Trashing %d sectors @ %lld (sector %d) (%s)\n",
dan1e3e4182012-10-17 16:20:36 +0000315 1+iLast-iFirst, pWrite->iOffset, iFirst, pWrite->pFile->zName
danielk1977f55b8992007-08-24 08:15:53 +0000316 );
danielk1977f8940ae2007-08-23 11:07:10 +0000317#endif
318
danielk1977a98d7b42008-01-18 13:42:54 +0000319 zGarbage = crash_malloc(g.iSectorSize);
danielk197762079062007-08-15 17:08:46 +0000320 if( zGarbage ){
321 sqlite3_int64 i;
322 for(i=iFirst; rc==SQLITE_OK && i<=iLast; i++){
drh2fa18682008-03-19 14:15:34 +0000323 sqlite3_randomness(g.iSectorSize, zGarbage);
danielk1977d6846d72009-02-11 14:27:04 +0000324 rc = writeDbFile(
325 pWrite->pFile, zGarbage, g.iSectorSize, i*g.iSectorSize
danielk197762079062007-08-15 17:08:46 +0000326 );
327 }
danielk1977a98d7b42008-01-18 13:42:54 +0000328 crash_free(zGarbage);
danielk197762079062007-08-15 17:08:46 +0000329 }else{
330 rc = SQLITE_NOMEM;
331 }
332
333 ppPtr = &pWrite->pNext;
334 break;
335 }
336
337 default:
338 assert(!"Cannot happen");
339 }
340
341 if( pWrite==pFinal ) break;
drh1a235932005-11-29 18:37:15 +0000342 }
danielk197762079062007-08-15 17:08:46 +0000343
344 if( rc==SQLITE_OK && isCrash ){
345 exit(-1);
346 }
347
danielk1977967a4a12007-08-20 14:23:44 +0000348 for(pWrite=g.pWriteList; pWrite && pWrite->pNext; pWrite=pWrite->pNext);
349 g.pWriteListEnd = pWrite;
350
drh1a235932005-11-29 18:37:15 +0000351 return rc;
drh9c06c952005-11-26 00:25:00 +0000352}
353
354/*
danielk197762079062007-08-15 17:08:46 +0000355** Add an entry to the end of the write-list.
drh18839212005-11-26 03:43:23 +0000356*/
danielk197762079062007-08-15 17:08:46 +0000357static int writeListAppend(
358 sqlite3_file *pFile,
359 sqlite3_int64 iOffset,
360 const u8 *zBuf,
361 int nBuf
362){
363 WriteBuffer *pNew;
364
365 assert((zBuf && nBuf) || (!nBuf && !zBuf));
366
danielk1977a98d7b42008-01-18 13:42:54 +0000367 pNew = (WriteBuffer *)crash_malloc(sizeof(WriteBuffer) + nBuf);
drha018dc72007-12-14 17:22:23 +0000368 if( pNew==0 ){
369 fprintf(stderr, "out of memory in the crash simulator\n");
370 }
danielk1977a98d7b42008-01-18 13:42:54 +0000371 memset(pNew, 0, sizeof(WriteBuffer)+nBuf);
danielk197762079062007-08-15 17:08:46 +0000372 pNew->iOffset = iOffset;
373 pNew->nBuf = nBuf;
374 pNew->pFile = (CrashFile *)pFile;
375 if( zBuf ){
376 pNew->zBuf = (u8 *)&pNew[1];
377 memcpy(pNew->zBuf, zBuf, nBuf);
378 }
379
380 if( g.pWriteList ){
danielk1977967a4a12007-08-20 14:23:44 +0000381 assert(g.pWriteListEnd);
382 g.pWriteListEnd->pNext = pNew;
danielk197762079062007-08-15 17:08:46 +0000383 }else{
384 g.pWriteList = pNew;
385 }
danielk1977967a4a12007-08-20 14:23:44 +0000386 g.pWriteListEnd = pNew;
danielk197762079062007-08-15 17:08:46 +0000387
drh1a235932005-11-29 18:37:15 +0000388 return SQLITE_OK;
389}
390
391/*
danielk197762079062007-08-15 17:08:46 +0000392** Close a crash-file.
drh1a235932005-11-29 18:37:15 +0000393*/
danielk19771e536952007-08-16 10:09:01 +0000394static int cfClose(sqlite3_file *pFile){
danielk197762079062007-08-15 17:08:46 +0000395 CrashFile *pCrash = (CrashFile *)pFile;
396 writeListSync(pCrash, 0);
danielk1977f1da17a2007-08-21 13:07:46 +0000397 sqlite3OsClose(pCrash->pRealFile);
danielk197762079062007-08-15 17:08:46 +0000398 return SQLITE_OK;
drh1a235932005-11-29 18:37:15 +0000399}
400
401/*
danielk197762079062007-08-15 17:08:46 +0000402** Read data from a crash-file.
drh1a235932005-11-29 18:37:15 +0000403*/
danielk19771e536952007-08-16 10:09:01 +0000404static int cfRead(
405 sqlite3_file *pFile,
406 void *zBuf,
407 int iAmt,
408 sqlite_int64 iOfst
409){
danielk197762079062007-08-15 17:08:46 +0000410 CrashFile *pCrash = (CrashFile *)pFile;
dan999cd082013-12-09 20:42:03 +0000411 int nCopy = (int)MIN((i64)iAmt, (pCrash->iSize - iOfst));
412
413 if( nCopy>0 ){
414 memcpy(zBuf, &pCrash->zData[iOfst], nCopy);
415 }
danielk197762079062007-08-15 17:08:46 +0000416
417 /* Check the file-size to see if this is a short-read */
dan999cd082013-12-09 20:42:03 +0000418 if( nCopy<iAmt ){
danielk197762079062007-08-15 17:08:46 +0000419 return SQLITE_IOERR_SHORT_READ;
420 }
421
danielk1977967a4a12007-08-20 14:23:44 +0000422 return SQLITE_OK;
drh18839212005-11-26 03:43:23 +0000423}
424
425/*
danielk197762079062007-08-15 17:08:46 +0000426** Write data to a crash-file.
danielk1977b4721172007-03-19 05:54:48 +0000427*/
danielk19771e536952007-08-16 10:09:01 +0000428static int cfWrite(
429 sqlite3_file *pFile,
430 const void *zBuf,
431 int iAmt,
432 sqlite_int64 iOfst
433){
danielk1977967a4a12007-08-20 14:23:44 +0000434 CrashFile *pCrash = (CrashFile *)pFile;
435 if( iAmt+iOfst>pCrash->iSize ){
drh7da5fcb2012-03-30 14:59:43 +0000436 pCrash->iSize = (int)(iAmt+iOfst);
danielk1977967a4a12007-08-20 14:23:44 +0000437 }
438 while( pCrash->iSize>pCrash->nData ){
drh4a50aac2007-08-23 02:47:53 +0000439 u8 *zNew;
danielk1977967a4a12007-08-20 14:23:44 +0000440 int nNew = (pCrash->nData*2) + 4096;
danielk1977a98d7b42008-01-18 13:42:54 +0000441 zNew = crash_realloc(pCrash->zData, nNew);
danielk1977967a4a12007-08-20 14:23:44 +0000442 if( !zNew ){
443 return SQLITE_NOMEM;
444 }
445 memset(&zNew[pCrash->nData], 0, nNew-pCrash->nData);
446 pCrash->nData = nNew;
447 pCrash->zData = zNew;
448 }
449 memcpy(&pCrash->zData[iOfst], zBuf, iAmt);
danielk197762079062007-08-15 17:08:46 +0000450 return writeListAppend(pFile, iOfst, zBuf, iAmt);
danielk1977b4721172007-03-19 05:54:48 +0000451}
452
453/*
danielk197762079062007-08-15 17:08:46 +0000454** Truncate a crash-file.
drh054889e2005-11-30 03:20:31 +0000455*/
danielk19771e536952007-08-16 10:09:01 +0000456static int cfTruncate(sqlite3_file *pFile, sqlite_int64 size){
danielk1977967a4a12007-08-20 14:23:44 +0000457 CrashFile *pCrash = (CrashFile *)pFile;
458 assert(size>=0);
459 if( pCrash->iSize>size ){
drh7da5fcb2012-03-30 14:59:43 +0000460 pCrash->iSize = (int)size;
danielk1977967a4a12007-08-20 14:23:44 +0000461 }
danielk197762079062007-08-15 17:08:46 +0000462 return writeListAppend(pFile, size, 0, 0);
463}
464
465/*
466** Sync a crash-file.
467*/
danielk19771e536952007-08-16 10:09:01 +0000468static int cfSync(sqlite3_file *pFile, int flags){
danielk197762079062007-08-15 17:08:46 +0000469 CrashFile *pCrash = (CrashFile *)pFile;
470 int isCrash = 0;
471
danielk1977967a4a12007-08-20 14:23:44 +0000472 const char *zName = pCrash->zName;
473 const char *zCrashFile = g.zCrashFile;
drh83cc1392012-04-19 18:04:28 +0000474 int nName = (int)strlen(zName);
475 int nCrashFile = (int)strlen(zCrashFile);
danielk1977967a4a12007-08-20 14:23:44 +0000476
477 if( nCrashFile>0 && zCrashFile[nCrashFile-1]=='*' ){
478 nCrashFile--;
479 if( nName>nCrashFile ) nName = nCrashFile;
480 }
481
mistachkinf8a78462012-03-08 20:00:36 +0000482#ifdef TRACE_CRASHTEST
483 printf("cfSync(): nName = %d, nCrashFile = %d, zName = %s, zCrashFile = %s\n",
484 nName, nCrashFile, zName, zCrashFile);
485#endif
486
danielk1977967a4a12007-08-20 14:23:44 +0000487 if( nName==nCrashFile && 0==memcmp(zName, zCrashFile, nName) ){
mistachkinf8a78462012-03-08 20:00:36 +0000488#ifdef TRACE_CRASHTEST
489 printf("cfSync(): name matched, g.iCrash = %d\n", g.iCrash);
490#endif
danielk1977967a4a12007-08-20 14:23:44 +0000491 if( (--g.iCrash)==0 ) isCrash = 1;
danielk197762079062007-08-15 17:08:46 +0000492 }
493
494 return writeListSync(pCrash, isCrash);
495}
496
497/*
498** Return the current file-size of the crash-file.
499*/
danielk19771e536952007-08-16 10:09:01 +0000500static int cfFileSize(sqlite3_file *pFile, sqlite_int64 *pSize){
danielk197762079062007-08-15 17:08:46 +0000501 CrashFile *pCrash = (CrashFile *)pFile;
danielk1977967a4a12007-08-20 14:23:44 +0000502 *pSize = (i64)pCrash->iSize;
danielk197762079062007-08-15 17:08:46 +0000503 return SQLITE_OK;
504}
505
506/*
507** Calls related to file-locks are passed on to the real file handle.
508*/
danielk19771e536952007-08-16 10:09:01 +0000509static int cfLock(sqlite3_file *pFile, int eLock){
danielk197762079062007-08-15 17:08:46 +0000510 return sqlite3OsLock(((CrashFile *)pFile)->pRealFile, eLock);
511}
danielk19771e536952007-08-16 10:09:01 +0000512static int cfUnlock(sqlite3_file *pFile, int eLock){
danielk197762079062007-08-15 17:08:46 +0000513 return sqlite3OsUnlock(((CrashFile *)pFile)->pRealFile, eLock);
514}
danielk1977861f7452008-06-05 11:39:11 +0000515static int cfCheckReservedLock(sqlite3_file *pFile, int *pResOut){
516 return sqlite3OsCheckReservedLock(((CrashFile *)pFile)->pRealFile, pResOut);
danielk197762079062007-08-15 17:08:46 +0000517}
drhcc6bb3e2007-08-31 16:11:35 +0000518static int cfFileControl(sqlite3_file *pFile, int op, void *pArg){
dan67e10d12011-08-23 16:41:06 +0000519 if( op==SQLITE_FCNTL_SIZE_HINT ){
520 CrashFile *pCrash = (CrashFile *)pFile;
521 i64 nByte = *(i64 *)pArg;
522 if( nByte>pCrash->iSize ){
dan422faae2011-08-23 19:46:02 +0000523 if( SQLITE_OK==writeListAppend(pFile, nByte, 0, 0) ){
drh7da5fcb2012-03-30 14:59:43 +0000524 pCrash->iSize = (int)nByte;
dan422faae2011-08-23 19:46:02 +0000525 }
dan67e10d12011-08-23 16:41:06 +0000526 }
dan422faae2011-08-23 19:46:02 +0000527 return SQLITE_OK;
dan67e10d12011-08-23 16:41:06 +0000528 }
drhcc6bb3e2007-08-31 16:11:35 +0000529 return sqlite3OsFileControl(((CrashFile *)pFile)->pRealFile, op, pArg);
danielk197762079062007-08-15 17:08:46 +0000530}
531
532/*
533** The xSectorSize() and xDeviceCharacteristics() functions return
534** the global values configured by the [sqlite_crashparams] tcl
535* interface.
536*/
danielk19771e536952007-08-16 10:09:01 +0000537static int cfSectorSize(sqlite3_file *pFile){
danielk197762079062007-08-15 17:08:46 +0000538 return g.iSectorSize;
539}
danielk19771e536952007-08-16 10:09:01 +0000540static int cfDeviceCharacteristics(sqlite3_file *pFile){
danielk197762079062007-08-15 17:08:46 +0000541 return g.iDeviceCharacteristics;
542}
543
drhd9e5c4f2010-05-12 18:01:39 +0000544/*
545** Pass-throughs for WAL support.
546*/
drh73b64e42010-05-30 19:55:15 +0000547static int cfShmLock(sqlite3_file *pFile, int ofst, int n, int flags){
548 return sqlite3OsShmLock(((CrashFile*)pFile)->pRealFile, ofst, n, flags);
drhd9e5c4f2010-05-12 18:01:39 +0000549}
drh286a2882010-05-20 23:51:06 +0000550static void cfShmBarrier(sqlite3_file *pFile){
551 sqlite3OsShmBarrier(((CrashFile*)pFile)->pRealFile);
552}
drhe11fedc2010-07-14 00:14:30 +0000553static int cfShmUnmap(sqlite3_file *pFile, int delFlag){
554 return sqlite3OsShmUnmap(((CrashFile*)pFile)->pRealFile, delFlag);
drhd9e5c4f2010-05-12 18:01:39 +0000555}
dan18801912010-06-14 14:07:50 +0000556static int cfShmMap(
dan067f3162010-06-14 10:30:12 +0000557 sqlite3_file *pFile, /* Handle open on database file */
dan18801912010-06-14 14:07:50 +0000558 int iRegion, /* Region to retrieve */
559 int sz, /* Size of regions */
dan067f3162010-06-14 10:30:12 +0000560 int w, /* True to extend file if necessary */
561 void volatile **pp /* OUT: Mapped memory */
562){
dan18801912010-06-14 14:07:50 +0000563 return sqlite3OsShmMap(((CrashFile*)pFile)->pRealFile, iRegion, sz, w, pp);
dan067f3162010-06-14 10:30:12 +0000564}
drhd9e5c4f2010-05-12 18:01:39 +0000565
danielk197762079062007-08-15 17:08:46 +0000566static const sqlite3_io_methods CrashFileVtab = {
drhd9e5c4f2010-05-12 18:01:39 +0000567 2, /* iVersion */
danielk197762079062007-08-15 17:08:46 +0000568 cfClose, /* xClose */
569 cfRead, /* xRead */
570 cfWrite, /* xWrite */
571 cfTruncate, /* xTruncate */
572 cfSync, /* xSync */
573 cfFileSize, /* xFileSize */
574 cfLock, /* xLock */
575 cfUnlock, /* xUnlock */
576 cfCheckReservedLock, /* xCheckReservedLock */
drhcc6bb3e2007-08-31 16:11:35 +0000577 cfFileControl, /* xFileControl */
danielk197762079062007-08-15 17:08:46 +0000578 cfSectorSize, /* xSectorSize */
drhd9e5c4f2010-05-12 18:01:39 +0000579 cfDeviceCharacteristics, /* xDeviceCharacteristics */
drh6b017cc2010-06-14 18:01:46 +0000580 cfShmMap, /* xShmMap */
danda9fe0c2010-07-13 18:44:03 +0000581 cfShmLock, /* xShmLock */
drh286a2882010-05-20 23:51:06 +0000582 cfShmBarrier, /* xShmBarrier */
drhe11fedc2010-07-14 00:14:30 +0000583 cfShmUnmap /* xShmUnmap */
drh054889e2005-11-30 03:20:31 +0000584};
585
drh054889e2005-11-30 03:20:31 +0000586/*
drhd677b3d2007-08-20 22:48:41 +0000587** Application data for the crash VFS
588*/
589struct crashAppData {
danielk1977f1da17a2007-08-21 13:07:46 +0000590 sqlite3_vfs *pOrig; /* Wrapped vfs structure */
drhd677b3d2007-08-20 22:48:41 +0000591};
592
593/*
danielk19770e87b702007-08-25 12:29:30 +0000594** Open a crash-file file handle.
danielk1977967a4a12007-08-20 14:23:44 +0000595**
596** The caller will have allocated pVfs->szOsFile bytes of space
597** at pFile. This file uses this space for the CrashFile structure
598** and allocates space for the "real" file structure using
599** sqlite3_malloc(). The assumption here is (pVfs->szOsFile) is
600** equal or greater than sizeof(CrashFile).
drh054889e2005-11-30 03:20:31 +0000601*/
danielk1977f1da17a2007-08-21 13:07:46 +0000602static int cfOpen(
danielk1977f55b8992007-08-24 08:15:53 +0000603 sqlite3_vfs *pCfVfs,
danielk197762079062007-08-15 17:08:46 +0000604 const char *zName,
605 sqlite3_file *pFile,
606 int flags,
607 int *pOutFlags
608){
danielk1977f55b8992007-08-24 08:15:53 +0000609 sqlite3_vfs *pVfs = (sqlite3_vfs *)pCfVfs->pAppData;
danielk1977967a4a12007-08-20 14:23:44 +0000610 int rc;
danielk1977f1da17a2007-08-21 13:07:46 +0000611 CrashFile *pWrapper = (CrashFile *)pFile;
drh4a50aac2007-08-23 02:47:53 +0000612 sqlite3_file *pReal = (sqlite3_file*)&pWrapper[1];
danielk1977f1da17a2007-08-21 13:07:46 +0000613
614 memset(pWrapper, 0, sizeof(CrashFile));
615 rc = sqlite3OsOpen(pVfs, zName, pReal, flags, pOutFlags);
616
617 if( rc==SQLITE_OK ){
618 i64 iSize;
619 pWrapper->pMethod = &CrashFileVtab;
620 pWrapper->zName = (char *)zName;
621 pWrapper->pRealFile = pReal;
622 rc = sqlite3OsFileSize(pReal, &iSize);
623 pWrapper->iSize = (int)iSize;
danielk1977d6846d72009-02-11 14:27:04 +0000624 pWrapper->flags = flags;
danielk1977f1da17a2007-08-21 13:07:46 +0000625 }
626 if( rc==SQLITE_OK ){
mistachkin27449382013-10-31 06:11:10 +0000627 pWrapper->nData = (int)(4096 + pWrapper->iSize);
danielk1977a98d7b42008-01-18 13:42:54 +0000628 pWrapper->zData = crash_malloc(pWrapper->nData);
danielk1977f1da17a2007-08-21 13:07:46 +0000629 if( pWrapper->zData ){
danielk19772d42c2b2009-02-10 14:28:57 +0000630 /* os_unix.c contains an assert() that fails if the caller attempts
631 ** to read data from the 512-byte locking region of a file opened
632 ** with the SQLITE_OPEN_MAIN_DB flag. This region of a database file
633 ** never contains valid data anyhow. So avoid doing such a read here.
dance5c42b2012-10-17 15:28:26 +0000634 **
635 ** UPDATE: It also contains an assert() verifying that each call
636 ** to the xRead() method reads less than 128KB of data.
danielk19772d42c2b2009-02-10 14:28:57 +0000637 */
dance5c42b2012-10-17 15:28:26 +0000638 i64 iOff;
639
danielk1977f1da17a2007-08-21 13:07:46 +0000640 memset(pWrapper->zData, 0, pWrapper->nData);
dance5c42b2012-10-17 15:28:26 +0000641 for(iOff=0; iOff<pWrapper->iSize; iOff += 512){
mistachkin27449382013-10-31 06:11:10 +0000642 int nRead = (int)(pWrapper->iSize - iOff);
dance5c42b2012-10-17 15:28:26 +0000643 if( nRead>512 ) nRead = 512;
dance5c42b2012-10-17 15:28:26 +0000644 rc = sqlite3OsRead(pReal, &pWrapper->zData[iOff], nRead, iOff);
danielk19772d42c2b2009-02-10 14:28:57 +0000645 }
danielk1977f1da17a2007-08-21 13:07:46 +0000646 }else{
647 rc = SQLITE_NOMEM;
danielk197762079062007-08-15 17:08:46 +0000648 }
danielk1977f1da17a2007-08-21 13:07:46 +0000649 }
650 if( rc!=SQLITE_OK && pWrapper->pMethod ){
651 sqlite3OsClose(pFile);
danielk197762079062007-08-15 17:08:46 +0000652 }
653 return rc;
654}
655
danielk1977f55b8992007-08-24 08:15:53 +0000656static int cfDelete(sqlite3_vfs *pCfVfs, const char *zPath, int dirSync){
657 sqlite3_vfs *pVfs = (sqlite3_vfs *)pCfVfs->pAppData;
658 return pVfs->xDelete(pVfs, zPath, dirSync);
danielk1977f1da17a2007-08-21 13:07:46 +0000659}
danielk1977861f7452008-06-05 11:39:11 +0000660static int cfAccess(
661 sqlite3_vfs *pCfVfs,
662 const char *zPath,
663 int flags,
664 int *pResOut
665){
danielk1977f55b8992007-08-24 08:15:53 +0000666 sqlite3_vfs *pVfs = (sqlite3_vfs *)pCfVfs->pAppData;
danielk1977861f7452008-06-05 11:39:11 +0000667 return pVfs->xAccess(pVfs, zPath, flags, pResOut);
danielk1977f1da17a2007-08-21 13:07:46 +0000668}
danielk1977adfb9b02007-09-17 07:02:56 +0000669static int cfFullPathname(
670 sqlite3_vfs *pCfVfs,
671 const char *zPath,
672 int nPathOut,
673 char *zPathOut
674){
danielk1977f55b8992007-08-24 08:15:53 +0000675 sqlite3_vfs *pVfs = (sqlite3_vfs *)pCfVfs->pAppData;
danielk1977adfb9b02007-09-17 07:02:56 +0000676 return pVfs->xFullPathname(pVfs, zPath, nPathOut, zPathOut);
danielk1977f1da17a2007-08-21 13:07:46 +0000677}
danielk1977f55b8992007-08-24 08:15:53 +0000678static void *cfDlOpen(sqlite3_vfs *pCfVfs, const char *zPath){
679 sqlite3_vfs *pVfs = (sqlite3_vfs *)pCfVfs->pAppData;
680 return pVfs->xDlOpen(pVfs, zPath);
danielk1977f1da17a2007-08-21 13:07:46 +0000681}
danielk19770e87b702007-08-25 12:29:30 +0000682static void cfDlError(sqlite3_vfs *pCfVfs, int nByte, char *zErrMsg){
683 sqlite3_vfs *pVfs = (sqlite3_vfs *)pCfVfs->pAppData;
684 pVfs->xDlError(pVfs, nByte, zErrMsg);
685}
drhec1724e2008-12-09 01:32:03 +0000686static void (*cfDlSym(sqlite3_vfs *pCfVfs, void *pH, const char *zSym))(void){
danielk19770e87b702007-08-25 12:29:30 +0000687 sqlite3_vfs *pVfs = (sqlite3_vfs *)pCfVfs->pAppData;
drhec1724e2008-12-09 01:32:03 +0000688 return pVfs->xDlSym(pVfs, pH, zSym);
danielk19770e87b702007-08-25 12:29:30 +0000689}
690static void cfDlClose(sqlite3_vfs *pCfVfs, void *pHandle){
691 sqlite3_vfs *pVfs = (sqlite3_vfs *)pCfVfs->pAppData;
692 pVfs->xDlClose(pVfs, pHandle);
693}
danielk1977f55b8992007-08-24 08:15:53 +0000694static int cfRandomness(sqlite3_vfs *pCfVfs, int nByte, char *zBufOut){
695 sqlite3_vfs *pVfs = (sqlite3_vfs *)pCfVfs->pAppData;
696 return pVfs->xRandomness(pVfs, nByte, zBufOut);
danielk1977f1da17a2007-08-21 13:07:46 +0000697}
danielk1977f55b8992007-08-24 08:15:53 +0000698static int cfSleep(sqlite3_vfs *pCfVfs, int nMicro){
699 sqlite3_vfs *pVfs = (sqlite3_vfs *)pCfVfs->pAppData;
700 return pVfs->xSleep(pVfs, nMicro);
danielk1977f1da17a2007-08-21 13:07:46 +0000701}
danielk1977f55b8992007-08-24 08:15:53 +0000702static int cfCurrentTime(sqlite3_vfs *pCfVfs, double *pTimeOut){
703 sqlite3_vfs *pVfs = (sqlite3_vfs *)pCfVfs->pAppData;
704 return pVfs->xCurrentTime(pVfs, pTimeOut);
danielk1977f1da17a2007-08-21 13:07:46 +0000705}
dan05accd22016-04-27 18:54:49 +0000706static int cfGetLastError(sqlite3_vfs *pCfVfs, int n, char *z){
707 sqlite3_vfs *pVfs = (sqlite3_vfs *)pCfVfs->pAppData;
708 return pVfs->xGetLastError(pVfs, n, z);
709}
danielk1977f1da17a2007-08-21 13:07:46 +0000710
danielk19772ca0f862007-08-23 08:06:44 +0000711static int processDevSymArgs(
712 Tcl_Interp *interp,
713 int objc,
714 Tcl_Obj *CONST objv[],
715 int *piDeviceChar,
716 int *piSectorSize
717){
718 struct DeviceFlag {
719 char *zName;
720 int iValue;
721 } aFlag[] = {
drhcb15f352011-12-23 01:04:17 +0000722 { "atomic", SQLITE_IOCAP_ATOMIC },
723 { "atomic512", SQLITE_IOCAP_ATOMIC512 },
724 { "atomic1k", SQLITE_IOCAP_ATOMIC1K },
725 { "atomic2k", SQLITE_IOCAP_ATOMIC2K },
726 { "atomic4k", SQLITE_IOCAP_ATOMIC4K },
727 { "atomic8k", SQLITE_IOCAP_ATOMIC8K },
728 { "atomic16k", SQLITE_IOCAP_ATOMIC16K },
729 { "atomic32k", SQLITE_IOCAP_ATOMIC32K },
730 { "atomic64k", SQLITE_IOCAP_ATOMIC64K },
731 { "sequential", SQLITE_IOCAP_SEQUENTIAL },
732 { "safe_append", SQLITE_IOCAP_SAFE_APPEND },
733 { "powersafe_overwrite", SQLITE_IOCAP_POWERSAFE_OVERWRITE },
danielk19772ca0f862007-08-23 08:06:44 +0000734 { 0, 0 }
735 };
736
737 int i;
738 int iDc = 0;
739 int iSectorSize = 0;
740 int setSectorsize = 0;
741 int setDeviceChar = 0;
742
743 for(i=0; i<objc; i+=2){
744 int nOpt;
745 char *zOpt = Tcl_GetStringFromObj(objv[i], &nOpt);
746
747 if( (nOpt>11 || nOpt<2 || strncmp("-sectorsize", zOpt, nOpt))
748 && (nOpt>16 || nOpt<2 || strncmp("-characteristics", zOpt, nOpt))
749 ){
750 Tcl_AppendResult(interp,
751 "Bad option: \"", zOpt,
752 "\" - must be \"-characteristics\" or \"-sectorsize\"", 0
753 );
754 return TCL_ERROR;
755 }
756 if( i==objc-1 ){
757 Tcl_AppendResult(interp, "Option requires an argument: \"", zOpt, "\"",0);
758 return TCL_ERROR;
759 }
760
761 if( zOpt[1]=='s' ){
762 if( Tcl_GetIntFromObj(interp, objv[i+1], &iSectorSize) ){
763 return TCL_ERROR;
764 }
765 setSectorsize = 1;
766 }else{
767 int j;
768 Tcl_Obj **apObj;
769 int nObj;
770 if( Tcl_ListObjGetElements(interp, objv[i+1], &nObj, &apObj) ){
771 return TCL_ERROR;
772 }
773 for(j=0; j<nObj; j++){
774 int rc;
775 int iChoice;
776 Tcl_Obj *pFlag = Tcl_DuplicateObj(apObj[j]);
777 Tcl_IncrRefCount(pFlag);
778 Tcl_UtfToLower(Tcl_GetString(pFlag));
779
780 rc = Tcl_GetIndexFromObjStruct(
781 interp, pFlag, aFlag, sizeof(aFlag[0]), "no such flag", 0, &iChoice
782 );
783 Tcl_DecrRefCount(pFlag);
784 if( rc ){
785 return TCL_ERROR;
786 }
787
788 iDc |= aFlag[iChoice].iValue;
789 }
790 setDeviceChar = 1;
791 }
792 }
793
794 if( setDeviceChar ){
795 *piDeviceChar = iDc;
796 }
797 if( setSectorsize ){
798 *piSectorSize = iSectorSize;
799 }
800
801 return TCL_OK;
802}
803
drh054889e2005-11-30 03:20:31 +0000804/*
danfe912512016-05-24 16:20:51 +0000805** tclcmd: sqlite3_crash_now
806**
807** Simulate a crash immediately. This function does not return
808** (writeListSync() calls exit(-1)).
809*/
810static int crashNowCmd(
811 void * clientData,
812 Tcl_Interp *interp,
813 int objc,
814 Tcl_Obj *CONST objv[]
815){
816 if( objc!=1 ){
817 Tcl_WrongNumArgs(interp, 1, objv, "");
818 return TCL_ERROR;
819 }
820 writeListSync(0, 1);
821 assert( 0 );
822 return TCL_OK;
823}
824
825/*
danielk1977ca0c8972007-09-01 09:02:53 +0000826** tclcmd: sqlite_crash_enable ENABLE
827**
828** Parameter ENABLE must be a boolean value. If true, then the "crash"
829** vfs is added to the system. If false, it is removed.
830*/
831static int crashEnableCmd(
832 void * clientData,
833 Tcl_Interp *interp,
834 int objc,
835 Tcl_Obj *CONST objv[]
836){
837 int isEnable;
838 static sqlite3_vfs crashVfs = {
drh4c846bb2010-05-03 16:36:55 +0000839 2, /* iVersion */
danielk1977ca0c8972007-09-01 09:02:53 +0000840 0, /* szOsFile */
841 0, /* mxPathname */
842 0, /* pNext */
843 "crash", /* zName */
844 0, /* pAppData */
845
846 cfOpen, /* xOpen */
847 cfDelete, /* xDelete */
848 cfAccess, /* xAccess */
danielk1977ca0c8972007-09-01 09:02:53 +0000849 cfFullPathname, /* xFullPathname */
850 cfDlOpen, /* xDlOpen */
851 cfDlError, /* xDlError */
852 cfDlSym, /* xDlSym */
853 cfDlClose, /* xDlClose */
854 cfRandomness, /* xRandomness */
855 cfSleep, /* xSleep */
drhf2424c52010-04-26 00:04:55 +0000856 cfCurrentTime, /* xCurrentTime */
dan05accd22016-04-27 18:54:49 +0000857 cfGetLastError, /* xGetLastError */
drh4c846bb2010-05-03 16:36:55 +0000858 0, /* xCurrentTimeInt64 */
danielk1977ca0c8972007-09-01 09:02:53 +0000859 };
860
861 if( objc!=2 ){
862 Tcl_WrongNumArgs(interp, 1, objv, "ENABLE");
863 return TCL_ERROR;
864 }
865
866 if( Tcl_GetBooleanFromObj(interp, objv[1], &isEnable) ){
867 return TCL_ERROR;
868 }
869
870 if( (isEnable && crashVfs.pAppData) || (!isEnable && !crashVfs.pAppData) ){
871 return TCL_OK;
872 }
873
874 if( crashVfs.pAppData==0 ){
875 sqlite3_vfs *pOriginalVfs = sqlite3_vfs_find(0);
876 crashVfs.mxPathname = pOriginalVfs->mxPathname;
877 crashVfs.pAppData = (void *)pOriginalVfs;
878 crashVfs.szOsFile = sizeof(CrashFile) + pOriginalVfs->szOsFile;
879 sqlite3_vfs_register(&crashVfs, 0);
880 }else{
881 crashVfs.pAppData = 0;
882 sqlite3_vfs_unregister(&crashVfs);
883 }
884
885 return TCL_OK;
886}
887
888/*
danielk197762079062007-08-15 17:08:46 +0000889** tclcmd: sqlite_crashparams ?OPTIONS? DELAY CRASHFILE
drh9c06c952005-11-26 00:25:00 +0000890**
891** This procedure implements a TCL command that enables crash testing
892** in testfixture. Once enabled, crash testing cannot be disabled.
danielk197762079062007-08-15 17:08:46 +0000893**
894** Available options are "-characteristics" and "-sectorsize". Both require
895** an argument. For -sectorsize, this is the simulated sector size in
896** bytes. For -characteristics, the argument must be a list of io-capability
897** flags to simulate. Valid flags are "atomic", "atomic512", "atomic1K",
898** "atomic2K", "atomic4K", "atomic8K", "atomic16K", "atomic32K",
899** "atomic64K", "sequential" and "safe_append".
900**
901** Example:
902**
903** sqlite_crashparams -sect 1024 -char {atomic sequential} ./test.db 1
904**
drh9c06c952005-11-26 00:25:00 +0000905*/
906static int crashParamsObjCmd(
907 void * clientData,
908 Tcl_Interp *interp,
909 int objc,
910 Tcl_Obj *CONST objv[]
911){
danielk197762079062007-08-15 17:08:46 +0000912 int iDelay;
913 const char *zCrashFile;
drh153c62c2007-08-24 03:51:33 +0000914 int nCrashFile, iDc, iSectorSize;
drhd677b3d2007-08-20 22:48:41 +0000915
drh153c62c2007-08-24 03:51:33 +0000916 iDc = -1;
917 iSectorSize = -1;
danielk197762079062007-08-15 17:08:46 +0000918
danielk197762079062007-08-15 17:08:46 +0000919 if( objc<3 ){
920 Tcl_WrongNumArgs(interp, 1, objv, "?OPTIONS? DELAY CRASHFILE");
danielk1977b4b47412007-08-17 15:53:36 +0000921 goto error;
drh9c06c952005-11-26 00:25:00 +0000922 }
danielk197762079062007-08-15 17:08:46 +0000923
danielk1977967a4a12007-08-20 14:23:44 +0000924 zCrashFile = Tcl_GetStringFromObj(objv[objc-1], &nCrashFile);
danielk197762079062007-08-15 17:08:46 +0000925 if( nCrashFile>=sizeof(g.zCrashFile) ){
926 Tcl_AppendResult(interp, "Filename is too long: \"", zCrashFile, "\"", 0);
danielk1977b4b47412007-08-17 15:53:36 +0000927 goto error;
drh9c06c952005-11-26 00:25:00 +0000928 }
danielk197762079062007-08-15 17:08:46 +0000929 if( Tcl_GetIntFromObj(interp, objv[objc-2], &iDelay) ){
danielk1977b4b47412007-08-17 15:53:36 +0000930 goto error;
danielk197762079062007-08-15 17:08:46 +0000931 }
932
danielk19772ca0f862007-08-23 08:06:44 +0000933 if( processDevSymArgs(interp, objc-3, &objv[1], &iDc, &iSectorSize) ){
934 return TCL_ERROR;
danielk197759a33f92007-03-17 10:26:59 +0000935 }
danielk197762079062007-08-15 17:08:46 +0000936
danielk19772ca0f862007-08-23 08:06:44 +0000937 if( iDc>=0 ){
danielk197762079062007-08-15 17:08:46 +0000938 g.iDeviceCharacteristics = iDc;
939 }
danielk19772ca0f862007-08-23 08:06:44 +0000940 if( iSectorSize>=0 ){
danielk197762079062007-08-15 17:08:46 +0000941 g.iSectorSize = iSectorSize;
942 }
danielk19772ca0f862007-08-23 08:06:44 +0000943
danielk197762079062007-08-15 17:08:46 +0000944 g.iCrash = iDelay;
945 memcpy(g.zCrashFile, zCrashFile, nCrashFile+1);
drh66560ad2006-01-06 14:32:19 +0000946 sqlite3CrashTestEnable = 1;
drh9c06c952005-11-26 00:25:00 +0000947 return TCL_OK;
danielk1977b4b47412007-08-17 15:53:36 +0000948
949error:
danielk1977b4b47412007-08-17 15:53:36 +0000950 return TCL_ERROR;
drh9c06c952005-11-26 00:25:00 +0000951}
952
danielk19772ca0f862007-08-23 08:06:44 +0000953static int devSymObjCmd(
954 void * clientData,
955 Tcl_Interp *interp,
956 int objc,
957 Tcl_Obj *CONST objv[]
958){
danielk1977bf260972008-01-22 11:50:13 +0000959 void devsym_register(int iDeviceChar, int iSectorSize);
danielk19772ca0f862007-08-23 08:06:44 +0000960
961 int iDc = -1;
962 int iSectorSize = -1;
danielk1977bf260972008-01-22 11:50:13 +0000963
danielk19772ca0f862007-08-23 08:06:44 +0000964 if( processDevSymArgs(interp, objc-1, &objv[1], &iDc, &iSectorSize) ){
965 return TCL_ERROR;
966 }
danielk1977bf260972008-01-22 11:50:13 +0000967 devsym_register(iDc, iSectorSize);
danielk19772ca0f862007-08-23 08:06:44 +0000968
969 return TCL_OK;
dan05accd22016-04-27 18:54:49 +0000970
971}
972
973/*
974** tclcmd: unregister_devsim
975*/
976static int dsUnregisterObjCmd(
977 void * clientData,
978 Tcl_Interp *interp,
979 int objc,
980 Tcl_Obj *CONST objv[]
981){
982 void devsym_unregister(void);
983
984 if( objc!=1 ){
985 Tcl_WrongNumArgs(interp, 1, objv, "");
986 return TCL_ERROR;
987 }
988
989 devsym_unregister();
990 return TCL_OK;
danielk19772ca0f862007-08-23 08:06:44 +0000991}
992
danielk1977a0fc7292008-12-20 18:33:59 +0000993/*
994** tclcmd: register_jt_vfs ?-default? PARENT-VFS
995*/
996static int jtObjCmd(
997 void * clientData,
998 Tcl_Interp *interp,
999 int objc,
1000 Tcl_Obj *CONST objv[]
1001){
1002 int jt_register(char *, int);
1003 char *zParent = 0;
1004
1005 if( objc!=2 && objc!=3 ){
1006 Tcl_WrongNumArgs(interp, 1, objv, "?-default? PARENT-VFS");
1007 return TCL_ERROR;
1008 }
1009 zParent = Tcl_GetString(objv[1]);
1010 if( objc==3 ){
1011 if( strcmp(zParent, "-default") ){
1012 Tcl_AppendResult(interp,
1013 "bad option \"", zParent, "\": must be -default", 0
1014 );
1015 return TCL_ERROR;
1016 }
1017 zParent = Tcl_GetString(objv[2]);
1018 }
1019
1020 if( !(*zParent) ){
1021 zParent = 0;
1022 }
1023 if( jt_register(zParent, objc==3) ){
1024 Tcl_AppendResult(interp, "Error in jt_register", 0);
1025 return TCL_ERROR;
1026 }
1027
1028 return TCL_OK;
1029}
1030
1031/*
1032** tclcmd: unregister_jt_vfs
1033*/
1034static int jtUnregisterObjCmd(
1035 void * clientData,
1036 Tcl_Interp *interp,
1037 int objc,
1038 Tcl_Obj *CONST objv[]
1039){
1040 void jt_unregister(void);
1041
1042 if( objc!=1 ){
1043 Tcl_WrongNumArgs(interp, 1, objv, "");
1044 return TCL_ERROR;
1045 }
1046
1047 jt_unregister();
1048 return TCL_OK;
1049}
1050
drh198bf392006-01-06 21:52:49 +00001051#endif /* SQLITE_OMIT_DISKIO */
1052
drh9c06c952005-11-26 00:25:00 +00001053/*
1054** This procedure registers the TCL procedures defined in this file.
1055*/
1056int Sqlitetest6_Init(Tcl_Interp *interp){
drh198bf392006-01-06 21:52:49 +00001057#ifndef SQLITE_OMIT_DISKIO
danielk1977ca0c8972007-09-01 09:02:53 +00001058 Tcl_CreateObjCommand(interp, "sqlite3_crash_enable", crashEnableCmd, 0, 0);
drh9c06c952005-11-26 00:25:00 +00001059 Tcl_CreateObjCommand(interp, "sqlite3_crashparams", crashParamsObjCmd, 0, 0);
danfe912512016-05-24 16:20:51 +00001060 Tcl_CreateObjCommand(interp, "sqlite3_crash_now", crashNowCmd, 0, 0);
danielk19772ca0f862007-08-23 08:06:44 +00001061 Tcl_CreateObjCommand(interp, "sqlite3_simulate_device", devSymObjCmd, 0, 0);
dan05accd22016-04-27 18:54:49 +00001062 Tcl_CreateObjCommand(interp, "unregister_devsim", dsUnregisterObjCmd, 0, 0);
danielk1977a0fc7292008-12-20 18:33:59 +00001063 Tcl_CreateObjCommand(interp, "register_jt_vfs", jtObjCmd, 0, 0);
1064 Tcl_CreateObjCommand(interp, "unregister_jt_vfs", jtUnregisterObjCmd, 0, 0);
drh198bf392006-01-06 21:52:49 +00001065#endif
drh9c06c952005-11-26 00:25:00 +00001066 return TCL_OK;
1067}
1068
1069#endif /* SQLITE_TEST */