blob: 9a3aa093f06e632154f49b87d57da16bc2c63c1a [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"
mistachkin52b1dbb2016-07-28 14:37:04 +000019#if defined(INCLUDE_SQLITE_TCL_H)
20# include "sqlite_tcl.h"
21#else
22# include "tcl.h"
23#endif
drh9c06c952005-11-26 00:25:00 +000024
drh198bf392006-01-06 21:52:49 +000025#ifndef SQLITE_OMIT_DISKIO /* This file is a no-op if disk I/O is disabled */
26
danielk1977f8940ae2007-08-23 11:07:10 +000027/* #define TRACE_CRASHTEST */
28
danielk197762079062007-08-15 17:08:46 +000029typedef struct CrashFile CrashFile;
30typedef struct CrashGlobal CrashGlobal;
danielk19770d24e6b2007-08-14 17:42:05 +000031typedef struct WriteBuffer WriteBuffer;
32
33/*
34** Method:
35**
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).
42**
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.
49**
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.
57**
58** The exact subset of the write-list written and/or corrupted is
59** determined by the simulated device characteristics and sector-size.
60**
61** "Normal" mode:
62**
63** Normal mode is used when the simulated device has none of the
64** SQLITE_IOCAP_XXX flags set.
65**
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.
70**
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:
74**
75** 1. The buffer is correctly written to the file, just as if
76** a crash were not being simulated.
77**
78** 2. Nothing is done.
79**
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.
84**
85** Device Characteristic flag handling:
86**
87** If the IOCAP_ATOMIC flag is set, then option (3) above is
88** never selected.
89**
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
mistachkin48864df2013-03-21 21:20:32 +000094** logic governs the behavior if any of the other ATOMICXXX flags
danielk19770d24e6b2007-08-14 17:42:05 +000095** is set.
96**
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.
101**
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.
107**
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.
111*/
danielk197762079062007-08-15 17:08:46 +0000112
113/*
114** Each write operation in the write-list is represented by an instance
115** of the following structure.
116**
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
119** truncated to.
120*/
danielk19770d24e6b2007-08-14 17:42:05 +0000121struct WriteBuffer {
danielk197762079062007-08-15 17:08:46 +0000122 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 */
126
127 WriteBuffer *pNext; /* Next in CrashGlobal.pWriteList */
danielk19770d24e6b2007-08-14 17:42:05 +0000128};
129
danielk197762079062007-08-15 17:08:46 +0000130struct CrashFile {
131 const sqlite3_io_methods *pMethod; /* Must be first */
132 sqlite3_file *pRealFile; /* Underlying "real" file handle */
danielk19771e536952007-08-16 10:09:01 +0000133 char *zName;
danielk1977d6846d72009-02-11 14:27:04 +0000134 int flags; /* Flags the file was opened with */
danielk1977967a4a12007-08-20 14:23:44 +0000135
danielk1977f1da17a2007-08-21 13:07:46 +0000136 /* 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.
139 */
danielk1977967a4a12007-08-20 14:23:44 +0000140 u8 *zData; /* Buffer containing file contents */
drha703f502013-10-15 14:29:32 +0000141 int nData; /* Size of buffer allocated at zData */
142 i64 iSize; /* Size of file in bytes */
drh9c06c952005-11-26 00:25:00 +0000143};
144
danielk197762079062007-08-15 17:08:46 +0000145struct CrashGlobal {
146 WriteBuffer *pWriteList; /* Head of write-list */
danielk1977967a4a12007-08-20 14:23:44 +0000147 WriteBuffer *pWriteListEnd; /* End of write-list */
drh9c06c952005-11-26 00:25:00 +0000148
danielk197762079062007-08-15 17:08:46 +0000149 int iSectorSize; /* Value of simulated sector size */
150 int iDeviceCharacteristics; /* Value of simulated device characteristics */
drh9c06c952005-11-26 00:25:00 +0000151
danielk197762079062007-08-15 17:08:46 +0000152 int iCrash; /* Crash on the iCrash'th call to xSync() */
153 char zCrashFile[500]; /* Crash during an xSync() on this file */
154};
drh9c06c952005-11-26 00:25:00 +0000155
danielk1977967a4a12007-08-20 14:23:44 +0000156static CrashGlobal g = {0, 0, SQLITE_DEFAULT_SECTOR_SIZE, 0, 0};
drh9c06c952005-11-26 00:25:00 +0000157
158/*
drh66560ad2006-01-06 14:32:19 +0000159** Set this global variable to 1 to enable crash testing.
160*/
danielk1977967a4a12007-08-20 14:23:44 +0000161static int sqlite3CrashTestEnable = 0;
drh66560ad2006-01-06 14:32:19 +0000162
danielk1977a98d7b42008-01-18 13:42:54 +0000163static void *crash_malloc(int nByte){
mistachkind7423672017-02-15 04:16:56 +0000164 return (void *)Tcl_AttemptAlloc((size_t)nByte);
danielk1977a98d7b42008-01-18 13:42:54 +0000165}
166static void crash_free(void *p){
167 Tcl_Free(p);
168}
169static void *crash_realloc(void *p, int n){
mistachkind7423672017-02-15 04:16:56 +0000170 return (void *)Tcl_AttemptRealloc(p, (size_t)n);
danielk1977a98d7b42008-01-18 13:42:54 +0000171}
172
drh66560ad2006-01-06 14:32:19 +0000173/*
danielk1977d6846d72009-02-11 14:27:04 +0000174** Wrapper around the sqlite3OsWrite() function that avoids writing to the
175** 512 byte block begining at offset PENDING_BYTE.
176*/
177static int writeDbFile(CrashFile *p, u8 *z, i64 iAmt, i64 iOff){
shanehebffe412010-07-08 16:22:51 +0000178 int rc = SQLITE_OK;
danielk1977d6846d72009-02-11 14:27:04 +0000179 int iSkip = 0;
danielk1977d6846d72009-02-11 14:27:04 +0000180 if( (iAmt-iSkip)>0 ){
drh7da5fcb2012-03-30 14:59:43 +0000181 rc = sqlite3OsWrite(p->pRealFile, &z[iSkip], (int)(iAmt-iSkip), iOff+iSkip);
danielk1977d6846d72009-02-11 14:27:04 +0000182 }
183 return rc;
184}
185
186/*
danielk197762079062007-08-15 17:08:46 +0000187** Flush the write-list as if xSync() had been called on file handle
188** pFile. If isCrash is true, simulate a crash.
drh9c06c952005-11-26 00:25:00 +0000189*/
danielk197762079062007-08-15 17:08:46 +0000190static int writeListSync(CrashFile *pFile, int isCrash){
191 int rc = SQLITE_OK;
192 int iDc = g.iDeviceCharacteristics;
drh66560ad2006-01-06 14:32:19 +0000193
danielk197762079062007-08-15 17:08:46 +0000194 WriteBuffer *pWrite;
195 WriteBuffer **ppPtr;
drh66560ad2006-01-06 14:32:19 +0000196
danielk1977f55b8992007-08-24 08:15:53 +0000197 /* 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
199 ** pFile.
200 **
201 ** If this is a crash simulation, set pFinal to an arbitrarily selected
202 ** element of the write-list.
danielk197762079062007-08-15 17:08:46 +0000203 */
204 WriteBuffer *pFinal = 0;
205 if( !isCrash ){
206 for(pWrite=g.pWriteList; pWrite; pWrite=pWrite->pNext){
207 if( pWrite->pFile==pFile ){
208 pFinal = pWrite;
209 }
210 }
danielk1977f55b8992007-08-24 08:15:53 +0000211 }else if( iDc&(SQLITE_IOCAP_SEQUENTIAL|SQLITE_IOCAP_SAFE_APPEND) ){
212 int nWrite = 0;
213 int iFinal;
214 for(pWrite=g.pWriteList; pWrite; pWrite=pWrite->pNext) nWrite++;
drh2fa18682008-03-19 14:15:34 +0000215 sqlite3_randomness(sizeof(int), &iFinal);
danielk1977f55b8992007-08-24 08:15:53 +0000216 iFinal = ((iFinal<0)?-1*iFinal:iFinal)%nWrite;
217 for(pWrite=g.pWriteList; iFinal>0; pWrite=pWrite->pNext) iFinal--;
218 pFinal = pWrite;
drh1a235932005-11-29 18:37:15 +0000219 }
drh66560ad2006-01-06 14:32:19 +0000220
danielk1977f8940ae2007-08-23 11:07:10 +0000221#ifdef TRACE_CRASHTEST
danfe912512016-05-24 16:20:51 +0000222 if( pFile ){
223 printf("Sync %s (is %s crash)\n", pFile->zName, (isCrash?"a":"not a"));
224 }
danielk1977f8940ae2007-08-23 11:07:10 +0000225#endif
226
danielk197762079062007-08-15 17:08:46 +0000227 ppPtr = &g.pWriteList;
228 for(pWrite=*ppPtr; rc==SQLITE_OK && pWrite; pWrite=*ppPtr){
danielk1977967a4a12007-08-20 14:23:44 +0000229 sqlite3_file *pRealFile = pWrite->pFile->pRealFile;
danielk197762079062007-08-15 17:08:46 +0000230
231 /* (eAction==1) -> write block out normally,
232 ** (eAction==2) -> do nothing,
233 ** (eAction==3) -> trash sectors.
234 */
235 int eAction = 0;
236 if( !isCrash ){
237 eAction = 2;
238 if( (pWrite->pFile==pFile || iDc&SQLITE_IOCAP_SEQUENTIAL) ){
239 eAction = 1;
240 }
241 }else{
242 char random;
drh2fa18682008-03-19 14:15:34 +0000243 sqlite3_randomness(1, &random);
danielk197762079062007-08-15 17:08:46 +0000244
danielk1977f55b8992007-08-24 08:15:53 +0000245 /* Do not select option 3 (sector trashing) if the IOCAP_ATOMIC flag
246 ** is set or this is an OsTruncate(), not an Oswrite().
247 */
248 if( (iDc&SQLITE_IOCAP_ATOMIC) || (pWrite->zBuf==0) ){
danielk197762079062007-08-15 17:08:46 +0000249 random &= 0x01;
250 }
251
danielk1977f55b8992007-08-24 08:15:53 +0000252 /* If IOCAP_SEQUENTIAL is set and this is not the final entry
253 ** in the truncated write-list, always select option 1 (write
254 ** out correctly).
255 */
256 if( (iDc&SQLITE_IOCAP_SEQUENTIAL && pWrite!=pFinal) ){
257 random = 0;
258 }
259
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).
263 */
264 if( iDc&SQLITE_IOCAP_SAFE_APPEND && pWrite->zBuf ){
265 i64 iSize;
266 sqlite3OsFileSize(pRealFile, &iSize);
267 if( iSize==pWrite->iOffset ){
268 random = 0;
269 }
270 }
271
danielk197762079062007-08-15 17:08:46 +0000272 if( (random&0x06)==0x06 ){
273 eAction = 3;
274 }else{
275 eAction = ((random&0x01)?2:1);
276 }
277 }
278
279 switch( eAction ){
280 case 1: { /* Write out correctly */
281 if( pWrite->zBuf ){
danielk1977d6846d72009-02-11 14:27:04 +0000282 rc = writeDbFile(
283 pWrite->pFile, pWrite->zBuf, pWrite->nBuf, pWrite->iOffset
danielk197762079062007-08-15 17:08:46 +0000284 );
285 }else{
danielk1977967a4a12007-08-20 14:23:44 +0000286 rc = sqlite3OsTruncate(pRealFile, pWrite->iOffset);
danielk197762079062007-08-15 17:08:46 +0000287 }
288 *ppPtr = pWrite->pNext;
danielk1977f8940ae2007-08-23 11:07:10 +0000289#ifdef TRACE_CRASHTEST
290 if( isCrash ){
danielk1977f55b8992007-08-24 08:15:53 +0000291 printf("Writing %d bytes @ %d (%s)\n",
292 pWrite->nBuf, (int)pWrite->iOffset, pWrite->pFile->zName
293 );
danielk1977f8940ae2007-08-23 11:07:10 +0000294 }
295#endif
danielk1977a98d7b42008-01-18 13:42:54 +0000296 crash_free(pWrite);
danielk197762079062007-08-15 17:08:46 +0000297 break;
298 }
299 case 2: { /* Do nothing */
300 ppPtr = &pWrite->pNext;
danielk1977f8940ae2007-08-23 11:07:10 +0000301#ifdef TRACE_CRASHTEST
302 if( isCrash ){
danielk1977f55b8992007-08-24 08:15:53 +0000303 printf("Omiting %d bytes @ %d (%s)\n",
304 pWrite->nBuf, (int)pWrite->iOffset, pWrite->pFile->zName
305 );
danielk1977f8940ae2007-08-23 11:07:10 +0000306 }
307#endif
danielk197762079062007-08-15 17:08:46 +0000308 break;
309 }
310 case 3: { /* Trash sectors */
311 u8 *zGarbage;
drh7da5fcb2012-03-30 14:59:43 +0000312 int iFirst = (int)(pWrite->iOffset/g.iSectorSize);
313 int iLast = (int)((pWrite->iOffset+pWrite->nBuf-1)/g.iSectorSize);
danielk1977967a4a12007-08-20 14:23:44 +0000314
315 assert(pWrite->zBuf);
danielk197762079062007-08-15 17:08:46 +0000316
danielk1977f8940ae2007-08-23 11:07:10 +0000317#ifdef TRACE_CRASHTEST
dancb1b0a62017-03-02 14:51:47 +0000318 printf("Trashing %d sectors (%d bytes) @ %lld (sector %d) (%s)\n",
319 1+iLast-iFirst, (1+iLast-iFirst)*g.iSectorSize,
320 pWrite->iOffset, iFirst, pWrite->pFile->zName
danielk1977f55b8992007-08-24 08:15:53 +0000321 );
danielk1977f8940ae2007-08-23 11:07:10 +0000322#endif
323
danielk1977a98d7b42008-01-18 13:42:54 +0000324 zGarbage = crash_malloc(g.iSectorSize);
danielk197762079062007-08-15 17:08:46 +0000325 if( zGarbage ){
326 sqlite3_int64 i;
327 for(i=iFirst; rc==SQLITE_OK && i<=iLast; i++){
drh2fa18682008-03-19 14:15:34 +0000328 sqlite3_randomness(g.iSectorSize, zGarbage);
danielk1977d6846d72009-02-11 14:27:04 +0000329 rc = writeDbFile(
330 pWrite->pFile, zGarbage, g.iSectorSize, i*g.iSectorSize
danielk197762079062007-08-15 17:08:46 +0000331 );
332 }
danielk1977a98d7b42008-01-18 13:42:54 +0000333 crash_free(zGarbage);
danielk197762079062007-08-15 17:08:46 +0000334 }else{
335 rc = SQLITE_NOMEM;
336 }
337
338 ppPtr = &pWrite->pNext;
339 break;
340 }
341
342 default:
343 assert(!"Cannot happen");
344 }
345
346 if( pWrite==pFinal ) break;
drh1a235932005-11-29 18:37:15 +0000347 }
danielk197762079062007-08-15 17:08:46 +0000348
349 if( rc==SQLITE_OK && isCrash ){
350 exit(-1);
351 }
352
danielk1977967a4a12007-08-20 14:23:44 +0000353 for(pWrite=g.pWriteList; pWrite && pWrite->pNext; pWrite=pWrite->pNext);
354 g.pWriteListEnd = pWrite;
355
drh1a235932005-11-29 18:37:15 +0000356 return rc;
drh9c06c952005-11-26 00:25:00 +0000357}
358
359/*
danielk197762079062007-08-15 17:08:46 +0000360** Add an entry to the end of the write-list.
drh18839212005-11-26 03:43:23 +0000361*/
danielk197762079062007-08-15 17:08:46 +0000362static int writeListAppend(
363 sqlite3_file *pFile,
364 sqlite3_int64 iOffset,
365 const u8 *zBuf,
366 int nBuf
367){
368 WriteBuffer *pNew;
369
370 assert((zBuf && nBuf) || (!nBuf && !zBuf));
371
danielk1977a98d7b42008-01-18 13:42:54 +0000372 pNew = (WriteBuffer *)crash_malloc(sizeof(WriteBuffer) + nBuf);
drha018dc72007-12-14 17:22:23 +0000373 if( pNew==0 ){
374 fprintf(stderr, "out of memory in the crash simulator\n");
375 }
danielk1977a98d7b42008-01-18 13:42:54 +0000376 memset(pNew, 0, sizeof(WriteBuffer)+nBuf);
danielk197762079062007-08-15 17:08:46 +0000377 pNew->iOffset = iOffset;
378 pNew->nBuf = nBuf;
379 pNew->pFile = (CrashFile *)pFile;
380 if( zBuf ){
381 pNew->zBuf = (u8 *)&pNew[1];
382 memcpy(pNew->zBuf, zBuf, nBuf);
383 }
384
385 if( g.pWriteList ){
danielk1977967a4a12007-08-20 14:23:44 +0000386 assert(g.pWriteListEnd);
387 g.pWriteListEnd->pNext = pNew;
danielk197762079062007-08-15 17:08:46 +0000388 }else{
389 g.pWriteList = pNew;
390 }
danielk1977967a4a12007-08-20 14:23:44 +0000391 g.pWriteListEnd = pNew;
danielk197762079062007-08-15 17:08:46 +0000392
drh1a235932005-11-29 18:37:15 +0000393 return SQLITE_OK;
394}
395
396/*
danielk197762079062007-08-15 17:08:46 +0000397** Close a crash-file.
drh1a235932005-11-29 18:37:15 +0000398*/
danielk19771e536952007-08-16 10:09:01 +0000399static int cfClose(sqlite3_file *pFile){
danielk197762079062007-08-15 17:08:46 +0000400 CrashFile *pCrash = (CrashFile *)pFile;
401 writeListSync(pCrash, 0);
danielk1977f1da17a2007-08-21 13:07:46 +0000402 sqlite3OsClose(pCrash->pRealFile);
danielk197762079062007-08-15 17:08:46 +0000403 return SQLITE_OK;
drh1a235932005-11-29 18:37:15 +0000404}
405
406/*
danielk197762079062007-08-15 17:08:46 +0000407** Read data from a crash-file.
drh1a235932005-11-29 18:37:15 +0000408*/
danielk19771e536952007-08-16 10:09:01 +0000409static int cfRead(
410 sqlite3_file *pFile,
411 void *zBuf,
412 int iAmt,
413 sqlite_int64 iOfst
414){
danielk197762079062007-08-15 17:08:46 +0000415 CrashFile *pCrash = (CrashFile *)pFile;
dan999cd082013-12-09 20:42:03 +0000416 int nCopy = (int)MIN((i64)iAmt, (pCrash->iSize - iOfst));
417
418 if( nCopy>0 ){
419 memcpy(zBuf, &pCrash->zData[iOfst], nCopy);
420 }
danielk197762079062007-08-15 17:08:46 +0000421
422 /* Check the file-size to see if this is a short-read */
dan999cd082013-12-09 20:42:03 +0000423 if( nCopy<iAmt ){
danielk197762079062007-08-15 17:08:46 +0000424 return SQLITE_IOERR_SHORT_READ;
425 }
426
danielk1977967a4a12007-08-20 14:23:44 +0000427 return SQLITE_OK;
drh18839212005-11-26 03:43:23 +0000428}
429
430/*
danielk197762079062007-08-15 17:08:46 +0000431** Write data to a crash-file.
danielk1977b4721172007-03-19 05:54:48 +0000432*/
danielk19771e536952007-08-16 10:09:01 +0000433static int cfWrite(
434 sqlite3_file *pFile,
435 const void *zBuf,
436 int iAmt,
437 sqlite_int64 iOfst
438){
danielk1977967a4a12007-08-20 14:23:44 +0000439 CrashFile *pCrash = (CrashFile *)pFile;
440 if( iAmt+iOfst>pCrash->iSize ){
drh7da5fcb2012-03-30 14:59:43 +0000441 pCrash->iSize = (int)(iAmt+iOfst);
danielk1977967a4a12007-08-20 14:23:44 +0000442 }
443 while( pCrash->iSize>pCrash->nData ){
drh4a50aac2007-08-23 02:47:53 +0000444 u8 *zNew;
danielk1977967a4a12007-08-20 14:23:44 +0000445 int nNew = (pCrash->nData*2) + 4096;
danielk1977a98d7b42008-01-18 13:42:54 +0000446 zNew = crash_realloc(pCrash->zData, nNew);
danielk1977967a4a12007-08-20 14:23:44 +0000447 if( !zNew ){
448 return SQLITE_NOMEM;
449 }
450 memset(&zNew[pCrash->nData], 0, nNew-pCrash->nData);
451 pCrash->nData = nNew;
452 pCrash->zData = zNew;
453 }
454 memcpy(&pCrash->zData[iOfst], zBuf, iAmt);
danielk197762079062007-08-15 17:08:46 +0000455 return writeListAppend(pFile, iOfst, zBuf, iAmt);
danielk1977b4721172007-03-19 05:54:48 +0000456}
457
458/*
danielk197762079062007-08-15 17:08:46 +0000459** Truncate a crash-file.
drh054889e2005-11-30 03:20:31 +0000460*/
danielk19771e536952007-08-16 10:09:01 +0000461static int cfTruncate(sqlite3_file *pFile, sqlite_int64 size){
danielk1977967a4a12007-08-20 14:23:44 +0000462 CrashFile *pCrash = (CrashFile *)pFile;
463 assert(size>=0);
464 if( pCrash->iSize>size ){
drh7da5fcb2012-03-30 14:59:43 +0000465 pCrash->iSize = (int)size;
danielk1977967a4a12007-08-20 14:23:44 +0000466 }
danielk197762079062007-08-15 17:08:46 +0000467 return writeListAppend(pFile, size, 0, 0);
468}
469
470/*
471** Sync a crash-file.
472*/
danielk19771e536952007-08-16 10:09:01 +0000473static int cfSync(sqlite3_file *pFile, int flags){
danielk197762079062007-08-15 17:08:46 +0000474 CrashFile *pCrash = (CrashFile *)pFile;
475 int isCrash = 0;
476
danielk1977967a4a12007-08-20 14:23:44 +0000477 const char *zName = pCrash->zName;
478 const char *zCrashFile = g.zCrashFile;
drh83cc1392012-04-19 18:04:28 +0000479 int nName = (int)strlen(zName);
480 int nCrashFile = (int)strlen(zCrashFile);
danielk1977967a4a12007-08-20 14:23:44 +0000481
482 if( nCrashFile>0 && zCrashFile[nCrashFile-1]=='*' ){
483 nCrashFile--;
484 if( nName>nCrashFile ) nName = nCrashFile;
485 }
486
mistachkinf8a78462012-03-08 20:00:36 +0000487#ifdef TRACE_CRASHTEST
488 printf("cfSync(): nName = %d, nCrashFile = %d, zName = %s, zCrashFile = %s\n",
489 nName, nCrashFile, zName, zCrashFile);
490#endif
491
danielk1977967a4a12007-08-20 14:23:44 +0000492 if( nName==nCrashFile && 0==memcmp(zName, zCrashFile, nName) ){
mistachkinf8a78462012-03-08 20:00:36 +0000493#ifdef TRACE_CRASHTEST
494 printf("cfSync(): name matched, g.iCrash = %d\n", g.iCrash);
495#endif
danielk1977967a4a12007-08-20 14:23:44 +0000496 if( (--g.iCrash)==0 ) isCrash = 1;
danielk197762079062007-08-15 17:08:46 +0000497 }
498
499 return writeListSync(pCrash, isCrash);
500}
501
502/*
503** Return the current file-size of the crash-file.
504*/
danielk19771e536952007-08-16 10:09:01 +0000505static int cfFileSize(sqlite3_file *pFile, sqlite_int64 *pSize){
danielk197762079062007-08-15 17:08:46 +0000506 CrashFile *pCrash = (CrashFile *)pFile;
danielk1977967a4a12007-08-20 14:23:44 +0000507 *pSize = (i64)pCrash->iSize;
danielk197762079062007-08-15 17:08:46 +0000508 return SQLITE_OK;
509}
510
511/*
512** Calls related to file-locks are passed on to the real file handle.
513*/
danielk19771e536952007-08-16 10:09:01 +0000514static int cfLock(sqlite3_file *pFile, int eLock){
danielk197762079062007-08-15 17:08:46 +0000515 return sqlite3OsLock(((CrashFile *)pFile)->pRealFile, eLock);
516}
danielk19771e536952007-08-16 10:09:01 +0000517static int cfUnlock(sqlite3_file *pFile, int eLock){
danielk197762079062007-08-15 17:08:46 +0000518 return sqlite3OsUnlock(((CrashFile *)pFile)->pRealFile, eLock);
519}
danielk1977861f7452008-06-05 11:39:11 +0000520static int cfCheckReservedLock(sqlite3_file *pFile, int *pResOut){
521 return sqlite3OsCheckReservedLock(((CrashFile *)pFile)->pRealFile, pResOut);
danielk197762079062007-08-15 17:08:46 +0000522}
drhcc6bb3e2007-08-31 16:11:35 +0000523static int cfFileControl(sqlite3_file *pFile, int op, void *pArg){
dan67e10d12011-08-23 16:41:06 +0000524 if( op==SQLITE_FCNTL_SIZE_HINT ){
525 CrashFile *pCrash = (CrashFile *)pFile;
526 i64 nByte = *(i64 *)pArg;
527 if( nByte>pCrash->iSize ){
dan422faae2011-08-23 19:46:02 +0000528 if( SQLITE_OK==writeListAppend(pFile, nByte, 0, 0) ){
drh7da5fcb2012-03-30 14:59:43 +0000529 pCrash->iSize = (int)nByte;
dan422faae2011-08-23 19:46:02 +0000530 }
dan67e10d12011-08-23 16:41:06 +0000531 }
dan422faae2011-08-23 19:46:02 +0000532 return SQLITE_OK;
dan67e10d12011-08-23 16:41:06 +0000533 }
drhcc6bb3e2007-08-31 16:11:35 +0000534 return sqlite3OsFileControl(((CrashFile *)pFile)->pRealFile, op, pArg);
danielk197762079062007-08-15 17:08:46 +0000535}
536
537/*
538** The xSectorSize() and xDeviceCharacteristics() functions return
539** the global values configured by the [sqlite_crashparams] tcl
540* interface.
541*/
danielk19771e536952007-08-16 10:09:01 +0000542static int cfSectorSize(sqlite3_file *pFile){
danielk197762079062007-08-15 17:08:46 +0000543 return g.iSectorSize;
544}
danielk19771e536952007-08-16 10:09:01 +0000545static int cfDeviceCharacteristics(sqlite3_file *pFile){
danielk197762079062007-08-15 17:08:46 +0000546 return g.iDeviceCharacteristics;
547}
548
drhd9e5c4f2010-05-12 18:01:39 +0000549/*
550** Pass-throughs for WAL support.
551*/
drh73b64e42010-05-30 19:55:15 +0000552static int cfShmLock(sqlite3_file *pFile, int ofst, int n, int flags){
553 return sqlite3OsShmLock(((CrashFile*)pFile)->pRealFile, ofst, n, flags);
drhd9e5c4f2010-05-12 18:01:39 +0000554}
drh286a2882010-05-20 23:51:06 +0000555static void cfShmBarrier(sqlite3_file *pFile){
556 sqlite3OsShmBarrier(((CrashFile*)pFile)->pRealFile);
557}
drhe11fedc2010-07-14 00:14:30 +0000558static int cfShmUnmap(sqlite3_file *pFile, int delFlag){
559 return sqlite3OsShmUnmap(((CrashFile*)pFile)->pRealFile, delFlag);
drhd9e5c4f2010-05-12 18:01:39 +0000560}
dan18801912010-06-14 14:07:50 +0000561static int cfShmMap(
dan067f3162010-06-14 10:30:12 +0000562 sqlite3_file *pFile, /* Handle open on database file */
dan18801912010-06-14 14:07:50 +0000563 int iRegion, /* Region to retrieve */
564 int sz, /* Size of regions */
dan067f3162010-06-14 10:30:12 +0000565 int w, /* True to extend file if necessary */
566 void volatile **pp /* OUT: Mapped memory */
567){
dan18801912010-06-14 14:07:50 +0000568 return sqlite3OsShmMap(((CrashFile*)pFile)->pRealFile, iRegion, sz, w, pp);
dan067f3162010-06-14 10:30:12 +0000569}
drhd9e5c4f2010-05-12 18:01:39 +0000570
danielk197762079062007-08-15 17:08:46 +0000571static const sqlite3_io_methods CrashFileVtab = {
drhd9e5c4f2010-05-12 18:01:39 +0000572 2, /* iVersion */
danielk197762079062007-08-15 17:08:46 +0000573 cfClose, /* xClose */
574 cfRead, /* xRead */
575 cfWrite, /* xWrite */
576 cfTruncate, /* xTruncate */
577 cfSync, /* xSync */
578 cfFileSize, /* xFileSize */
579 cfLock, /* xLock */
580 cfUnlock, /* xUnlock */
581 cfCheckReservedLock, /* xCheckReservedLock */
drhcc6bb3e2007-08-31 16:11:35 +0000582 cfFileControl, /* xFileControl */
danielk197762079062007-08-15 17:08:46 +0000583 cfSectorSize, /* xSectorSize */
drhd9e5c4f2010-05-12 18:01:39 +0000584 cfDeviceCharacteristics, /* xDeviceCharacteristics */
drh6b017cc2010-06-14 18:01:46 +0000585 cfShmMap, /* xShmMap */
danda9fe0c2010-07-13 18:44:03 +0000586 cfShmLock, /* xShmLock */
drh286a2882010-05-20 23:51:06 +0000587 cfShmBarrier, /* xShmBarrier */
drhe11fedc2010-07-14 00:14:30 +0000588 cfShmUnmap /* xShmUnmap */
drh054889e2005-11-30 03:20:31 +0000589};
590
drh054889e2005-11-30 03:20:31 +0000591/*
drhd677b3d2007-08-20 22:48:41 +0000592** Application data for the crash VFS
593*/
594struct crashAppData {
danielk1977f1da17a2007-08-21 13:07:46 +0000595 sqlite3_vfs *pOrig; /* Wrapped vfs structure */
drhd677b3d2007-08-20 22:48:41 +0000596};
597
598/*
danielk19770e87b702007-08-25 12:29:30 +0000599** Open a crash-file file handle.
danielk1977967a4a12007-08-20 14:23:44 +0000600**
601** The caller will have allocated pVfs->szOsFile bytes of space
602** at pFile. This file uses this space for the CrashFile structure
603** and allocates space for the "real" file structure using
604** sqlite3_malloc(). The assumption here is (pVfs->szOsFile) is
605** equal or greater than sizeof(CrashFile).
drh054889e2005-11-30 03:20:31 +0000606*/
danielk1977f1da17a2007-08-21 13:07:46 +0000607static int cfOpen(
danielk1977f55b8992007-08-24 08:15:53 +0000608 sqlite3_vfs *pCfVfs,
danielk197762079062007-08-15 17:08:46 +0000609 const char *zName,
610 sqlite3_file *pFile,
611 int flags,
612 int *pOutFlags
613){
danielk1977f55b8992007-08-24 08:15:53 +0000614 sqlite3_vfs *pVfs = (sqlite3_vfs *)pCfVfs->pAppData;
danielk1977967a4a12007-08-20 14:23:44 +0000615 int rc;
danielk1977f1da17a2007-08-21 13:07:46 +0000616 CrashFile *pWrapper = (CrashFile *)pFile;
drh4a50aac2007-08-23 02:47:53 +0000617 sqlite3_file *pReal = (sqlite3_file*)&pWrapper[1];
danielk1977f1da17a2007-08-21 13:07:46 +0000618
619 memset(pWrapper, 0, sizeof(CrashFile));
620 rc = sqlite3OsOpen(pVfs, zName, pReal, flags, pOutFlags);
621
622 if( rc==SQLITE_OK ){
623 i64 iSize;
624 pWrapper->pMethod = &CrashFileVtab;
625 pWrapper->zName = (char *)zName;
626 pWrapper->pRealFile = pReal;
627 rc = sqlite3OsFileSize(pReal, &iSize);
628 pWrapper->iSize = (int)iSize;
danielk1977d6846d72009-02-11 14:27:04 +0000629 pWrapper->flags = flags;
danielk1977f1da17a2007-08-21 13:07:46 +0000630 }
631 if( rc==SQLITE_OK ){
mistachkin27449382013-10-31 06:11:10 +0000632 pWrapper->nData = (int)(4096 + pWrapper->iSize);
danielk1977a98d7b42008-01-18 13:42:54 +0000633 pWrapper->zData = crash_malloc(pWrapper->nData);
danielk1977f1da17a2007-08-21 13:07:46 +0000634 if( pWrapper->zData ){
danielk19772d42c2b2009-02-10 14:28:57 +0000635 /* os_unix.c contains an assert() that fails if the caller attempts
636 ** to read data from the 512-byte locking region of a file opened
637 ** with the SQLITE_OPEN_MAIN_DB flag. This region of a database file
638 ** never contains valid data anyhow. So avoid doing such a read here.
dance5c42b2012-10-17 15:28:26 +0000639 **
640 ** UPDATE: It also contains an assert() verifying that each call
641 ** to the xRead() method reads less than 128KB of data.
danielk19772d42c2b2009-02-10 14:28:57 +0000642 */
dance5c42b2012-10-17 15:28:26 +0000643 i64 iOff;
644
danielk1977f1da17a2007-08-21 13:07:46 +0000645 memset(pWrapper->zData, 0, pWrapper->nData);
dance5c42b2012-10-17 15:28:26 +0000646 for(iOff=0; iOff<pWrapper->iSize; iOff += 512){
mistachkin27449382013-10-31 06:11:10 +0000647 int nRead = (int)(pWrapper->iSize - iOff);
dance5c42b2012-10-17 15:28:26 +0000648 if( nRead>512 ) nRead = 512;
dance5c42b2012-10-17 15:28:26 +0000649 rc = sqlite3OsRead(pReal, &pWrapper->zData[iOff], nRead, iOff);
danielk19772d42c2b2009-02-10 14:28:57 +0000650 }
danielk1977f1da17a2007-08-21 13:07:46 +0000651 }else{
652 rc = SQLITE_NOMEM;
danielk197762079062007-08-15 17:08:46 +0000653 }
danielk1977f1da17a2007-08-21 13:07:46 +0000654 }
655 if( rc!=SQLITE_OK && pWrapper->pMethod ){
656 sqlite3OsClose(pFile);
danielk197762079062007-08-15 17:08:46 +0000657 }
658 return rc;
659}
660
danielk1977f55b8992007-08-24 08:15:53 +0000661static int cfDelete(sqlite3_vfs *pCfVfs, const char *zPath, int dirSync){
662 sqlite3_vfs *pVfs = (sqlite3_vfs *)pCfVfs->pAppData;
663 return pVfs->xDelete(pVfs, zPath, dirSync);
danielk1977f1da17a2007-08-21 13:07:46 +0000664}
danielk1977861f7452008-06-05 11:39:11 +0000665static int cfAccess(
666 sqlite3_vfs *pCfVfs,
667 const char *zPath,
668 int flags,
669 int *pResOut
670){
danielk1977f55b8992007-08-24 08:15:53 +0000671 sqlite3_vfs *pVfs = (sqlite3_vfs *)pCfVfs->pAppData;
danielk1977861f7452008-06-05 11:39:11 +0000672 return pVfs->xAccess(pVfs, zPath, flags, pResOut);
danielk1977f1da17a2007-08-21 13:07:46 +0000673}
danielk1977adfb9b02007-09-17 07:02:56 +0000674static int cfFullPathname(
675 sqlite3_vfs *pCfVfs,
676 const char *zPath,
677 int nPathOut,
678 char *zPathOut
679){
danielk1977f55b8992007-08-24 08:15:53 +0000680 sqlite3_vfs *pVfs = (sqlite3_vfs *)pCfVfs->pAppData;
danielk1977adfb9b02007-09-17 07:02:56 +0000681 return pVfs->xFullPathname(pVfs, zPath, nPathOut, zPathOut);
danielk1977f1da17a2007-08-21 13:07:46 +0000682}
danielk1977f55b8992007-08-24 08:15:53 +0000683static void *cfDlOpen(sqlite3_vfs *pCfVfs, const char *zPath){
684 sqlite3_vfs *pVfs = (sqlite3_vfs *)pCfVfs->pAppData;
685 return pVfs->xDlOpen(pVfs, zPath);
danielk1977f1da17a2007-08-21 13:07:46 +0000686}
danielk19770e87b702007-08-25 12:29:30 +0000687static void cfDlError(sqlite3_vfs *pCfVfs, int nByte, char *zErrMsg){
688 sqlite3_vfs *pVfs = (sqlite3_vfs *)pCfVfs->pAppData;
689 pVfs->xDlError(pVfs, nByte, zErrMsg);
690}
drhec1724e2008-12-09 01:32:03 +0000691static void (*cfDlSym(sqlite3_vfs *pCfVfs, void *pH, const char *zSym))(void){
danielk19770e87b702007-08-25 12:29:30 +0000692 sqlite3_vfs *pVfs = (sqlite3_vfs *)pCfVfs->pAppData;
drhec1724e2008-12-09 01:32:03 +0000693 return pVfs->xDlSym(pVfs, pH, zSym);
danielk19770e87b702007-08-25 12:29:30 +0000694}
695static void cfDlClose(sqlite3_vfs *pCfVfs, void *pHandle){
696 sqlite3_vfs *pVfs = (sqlite3_vfs *)pCfVfs->pAppData;
697 pVfs->xDlClose(pVfs, pHandle);
698}
danielk1977f55b8992007-08-24 08:15:53 +0000699static int cfRandomness(sqlite3_vfs *pCfVfs, int nByte, char *zBufOut){
700 sqlite3_vfs *pVfs = (sqlite3_vfs *)pCfVfs->pAppData;
701 return pVfs->xRandomness(pVfs, nByte, zBufOut);
danielk1977f1da17a2007-08-21 13:07:46 +0000702}
danielk1977f55b8992007-08-24 08:15:53 +0000703static int cfSleep(sqlite3_vfs *pCfVfs, int nMicro){
704 sqlite3_vfs *pVfs = (sqlite3_vfs *)pCfVfs->pAppData;
705 return pVfs->xSleep(pVfs, nMicro);
danielk1977f1da17a2007-08-21 13:07:46 +0000706}
danielk1977f55b8992007-08-24 08:15:53 +0000707static int cfCurrentTime(sqlite3_vfs *pCfVfs, double *pTimeOut){
708 sqlite3_vfs *pVfs = (sqlite3_vfs *)pCfVfs->pAppData;
709 return pVfs->xCurrentTime(pVfs, pTimeOut);
danielk1977f1da17a2007-08-21 13:07:46 +0000710}
dan05accd22016-04-27 18:54:49 +0000711static int cfGetLastError(sqlite3_vfs *pCfVfs, int n, char *z){
712 sqlite3_vfs *pVfs = (sqlite3_vfs *)pCfVfs->pAppData;
713 return pVfs->xGetLastError(pVfs, n, z);
714}
danielk1977f1da17a2007-08-21 13:07:46 +0000715
danielk19772ca0f862007-08-23 08:06:44 +0000716static int processDevSymArgs(
717 Tcl_Interp *interp,
718 int objc,
719 Tcl_Obj *CONST objv[],
720 int *piDeviceChar,
721 int *piSectorSize
722){
723 struct DeviceFlag {
724 char *zName;
725 int iValue;
726 } aFlag[] = {
drhcb15f352011-12-23 01:04:17 +0000727 { "atomic", SQLITE_IOCAP_ATOMIC },
728 { "atomic512", SQLITE_IOCAP_ATOMIC512 },
729 { "atomic1k", SQLITE_IOCAP_ATOMIC1K },
730 { "atomic2k", SQLITE_IOCAP_ATOMIC2K },
731 { "atomic4k", SQLITE_IOCAP_ATOMIC4K },
732 { "atomic8k", SQLITE_IOCAP_ATOMIC8K },
733 { "atomic16k", SQLITE_IOCAP_ATOMIC16K },
734 { "atomic32k", SQLITE_IOCAP_ATOMIC32K },
735 { "atomic64k", SQLITE_IOCAP_ATOMIC64K },
736 { "sequential", SQLITE_IOCAP_SEQUENTIAL },
737 { "safe_append", SQLITE_IOCAP_SAFE_APPEND },
738 { "powersafe_overwrite", SQLITE_IOCAP_POWERSAFE_OVERWRITE },
dan33447e72017-07-22 20:12:31 +0000739 { "batch-atomic", SQLITE_IOCAP_BATCH_ATOMIC },
danielk19772ca0f862007-08-23 08:06:44 +0000740 { 0, 0 }
741 };
742
743 int i;
744 int iDc = 0;
745 int iSectorSize = 0;
746 int setSectorsize = 0;
747 int setDeviceChar = 0;
748
749 for(i=0; i<objc; i+=2){
750 int nOpt;
751 char *zOpt = Tcl_GetStringFromObj(objv[i], &nOpt);
752
753 if( (nOpt>11 || nOpt<2 || strncmp("-sectorsize", zOpt, nOpt))
754 && (nOpt>16 || nOpt<2 || strncmp("-characteristics", zOpt, nOpt))
755 ){
756 Tcl_AppendResult(interp,
757 "Bad option: \"", zOpt,
758 "\" - must be \"-characteristics\" or \"-sectorsize\"", 0
759 );
760 return TCL_ERROR;
761 }
762 if( i==objc-1 ){
763 Tcl_AppendResult(interp, "Option requires an argument: \"", zOpt, "\"",0);
764 return TCL_ERROR;
765 }
766
767 if( zOpt[1]=='s' ){
768 if( Tcl_GetIntFromObj(interp, objv[i+1], &iSectorSize) ){
769 return TCL_ERROR;
770 }
771 setSectorsize = 1;
772 }else{
773 int j;
774 Tcl_Obj **apObj;
775 int nObj;
776 if( Tcl_ListObjGetElements(interp, objv[i+1], &nObj, &apObj) ){
777 return TCL_ERROR;
778 }
779 for(j=0; j<nObj; j++){
780 int rc;
781 int iChoice;
782 Tcl_Obj *pFlag = Tcl_DuplicateObj(apObj[j]);
783 Tcl_IncrRefCount(pFlag);
784 Tcl_UtfToLower(Tcl_GetString(pFlag));
785
786 rc = Tcl_GetIndexFromObjStruct(
787 interp, pFlag, aFlag, sizeof(aFlag[0]), "no such flag", 0, &iChoice
788 );
789 Tcl_DecrRefCount(pFlag);
790 if( rc ){
791 return TCL_ERROR;
792 }
793
794 iDc |= aFlag[iChoice].iValue;
795 }
796 setDeviceChar = 1;
797 }
798 }
799
800 if( setDeviceChar ){
801 *piDeviceChar = iDc;
802 }
803 if( setSectorsize ){
804 *piSectorSize = iSectorSize;
805 }
806
807 return TCL_OK;
808}
809
drh054889e2005-11-30 03:20:31 +0000810/*
danfe912512016-05-24 16:20:51 +0000811** tclcmd: sqlite3_crash_now
812**
813** Simulate a crash immediately. This function does not return
814** (writeListSync() calls exit(-1)).
815*/
mistachkin7617e4a2016-07-28 17:11:20 +0000816static int SQLITE_TCLAPI crashNowCmd(
danfe912512016-05-24 16:20:51 +0000817 void * clientData,
818 Tcl_Interp *interp,
819 int objc,
820 Tcl_Obj *CONST objv[]
821){
822 if( objc!=1 ){
823 Tcl_WrongNumArgs(interp, 1, objv, "");
824 return TCL_ERROR;
825 }
826 writeListSync(0, 1);
827 assert( 0 );
828 return TCL_OK;
829}
830
831/*
dancb1b0a62017-03-02 14:51:47 +0000832** tclcmd: sqlite_crash_enable ENABLE ?DEFAULT?
danielk1977ca0c8972007-09-01 09:02:53 +0000833**
834** Parameter ENABLE must be a boolean value. If true, then the "crash"
835** vfs is added to the system. If false, it is removed.
836*/
mistachkin7617e4a2016-07-28 17:11:20 +0000837static int SQLITE_TCLAPI crashEnableCmd(
danielk1977ca0c8972007-09-01 09:02:53 +0000838 void * clientData,
839 Tcl_Interp *interp,
840 int objc,
841 Tcl_Obj *CONST objv[]
842){
843 int isEnable;
dancb1b0a62017-03-02 14:51:47 +0000844 int isDefault = 0;
danielk1977ca0c8972007-09-01 09:02:53 +0000845 static sqlite3_vfs crashVfs = {
drh4c846bb2010-05-03 16:36:55 +0000846 2, /* iVersion */
danielk1977ca0c8972007-09-01 09:02:53 +0000847 0, /* szOsFile */
848 0, /* mxPathname */
849 0, /* pNext */
850 "crash", /* zName */
851 0, /* pAppData */
852
853 cfOpen, /* xOpen */
854 cfDelete, /* xDelete */
855 cfAccess, /* xAccess */
danielk1977ca0c8972007-09-01 09:02:53 +0000856 cfFullPathname, /* xFullPathname */
857 cfDlOpen, /* xDlOpen */
858 cfDlError, /* xDlError */
859 cfDlSym, /* xDlSym */
860 cfDlClose, /* xDlClose */
861 cfRandomness, /* xRandomness */
862 cfSleep, /* xSleep */
drhf2424c52010-04-26 00:04:55 +0000863 cfCurrentTime, /* xCurrentTime */
dan05accd22016-04-27 18:54:49 +0000864 cfGetLastError, /* xGetLastError */
drh4c846bb2010-05-03 16:36:55 +0000865 0, /* xCurrentTimeInt64 */
danielk1977ca0c8972007-09-01 09:02:53 +0000866 };
867
dancb1b0a62017-03-02 14:51:47 +0000868 if( objc!=2 && objc!=3 ){
869 Tcl_WrongNumArgs(interp, 1, objv, "ENABLE ?DEFAULT?");
danielk1977ca0c8972007-09-01 09:02:53 +0000870 return TCL_ERROR;
871 }
872
873 if( Tcl_GetBooleanFromObj(interp, objv[1], &isEnable) ){
874 return TCL_ERROR;
875 }
dancb1b0a62017-03-02 14:51:47 +0000876 if( objc==3 && Tcl_GetBooleanFromObj(interp, objv[2], &isDefault) ){
877 return TCL_ERROR;
878 }
danielk1977ca0c8972007-09-01 09:02:53 +0000879
880 if( (isEnable && crashVfs.pAppData) || (!isEnable && !crashVfs.pAppData) ){
881 return TCL_OK;
882 }
883
884 if( crashVfs.pAppData==0 ){
885 sqlite3_vfs *pOriginalVfs = sqlite3_vfs_find(0);
886 crashVfs.mxPathname = pOriginalVfs->mxPathname;
887 crashVfs.pAppData = (void *)pOriginalVfs;
888 crashVfs.szOsFile = sizeof(CrashFile) + pOriginalVfs->szOsFile;
dancb1b0a62017-03-02 14:51:47 +0000889 sqlite3_vfs_register(&crashVfs, isDefault);
danielk1977ca0c8972007-09-01 09:02:53 +0000890 }else{
891 crashVfs.pAppData = 0;
892 sqlite3_vfs_unregister(&crashVfs);
893 }
894
895 return TCL_OK;
896}
897
898/*
danielk197762079062007-08-15 17:08:46 +0000899** tclcmd: sqlite_crashparams ?OPTIONS? DELAY CRASHFILE
drh9c06c952005-11-26 00:25:00 +0000900**
901** This procedure implements a TCL command that enables crash testing
902** in testfixture. Once enabled, crash testing cannot be disabled.
danielk197762079062007-08-15 17:08:46 +0000903**
904** Available options are "-characteristics" and "-sectorsize". Both require
905** an argument. For -sectorsize, this is the simulated sector size in
906** bytes. For -characteristics, the argument must be a list of io-capability
907** flags to simulate. Valid flags are "atomic", "atomic512", "atomic1K",
908** "atomic2K", "atomic4K", "atomic8K", "atomic16K", "atomic32K",
909** "atomic64K", "sequential" and "safe_append".
910**
911** Example:
912**
913** sqlite_crashparams -sect 1024 -char {atomic sequential} ./test.db 1
914**
drh9c06c952005-11-26 00:25:00 +0000915*/
mistachkin7617e4a2016-07-28 17:11:20 +0000916static int SQLITE_TCLAPI crashParamsObjCmd(
drh9c06c952005-11-26 00:25:00 +0000917 void * clientData,
918 Tcl_Interp *interp,
919 int objc,
920 Tcl_Obj *CONST objv[]
921){
danielk197762079062007-08-15 17:08:46 +0000922 int iDelay;
923 const char *zCrashFile;
drh153c62c2007-08-24 03:51:33 +0000924 int nCrashFile, iDc, iSectorSize;
drhd677b3d2007-08-20 22:48:41 +0000925
drh153c62c2007-08-24 03:51:33 +0000926 iDc = -1;
927 iSectorSize = -1;
danielk197762079062007-08-15 17:08:46 +0000928
danielk197762079062007-08-15 17:08:46 +0000929 if( objc<3 ){
930 Tcl_WrongNumArgs(interp, 1, objv, "?OPTIONS? DELAY CRASHFILE");
danielk1977b4b47412007-08-17 15:53:36 +0000931 goto error;
drh9c06c952005-11-26 00:25:00 +0000932 }
danielk197762079062007-08-15 17:08:46 +0000933
danielk1977967a4a12007-08-20 14:23:44 +0000934 zCrashFile = Tcl_GetStringFromObj(objv[objc-1], &nCrashFile);
danielk197762079062007-08-15 17:08:46 +0000935 if( nCrashFile>=sizeof(g.zCrashFile) ){
936 Tcl_AppendResult(interp, "Filename is too long: \"", zCrashFile, "\"", 0);
danielk1977b4b47412007-08-17 15:53:36 +0000937 goto error;
drh9c06c952005-11-26 00:25:00 +0000938 }
danielk197762079062007-08-15 17:08:46 +0000939 if( Tcl_GetIntFromObj(interp, objv[objc-2], &iDelay) ){
danielk1977b4b47412007-08-17 15:53:36 +0000940 goto error;
danielk197762079062007-08-15 17:08:46 +0000941 }
942
danielk19772ca0f862007-08-23 08:06:44 +0000943 if( processDevSymArgs(interp, objc-3, &objv[1], &iDc, &iSectorSize) ){
944 return TCL_ERROR;
danielk197759a33f92007-03-17 10:26:59 +0000945 }
danielk197762079062007-08-15 17:08:46 +0000946
danielk19772ca0f862007-08-23 08:06:44 +0000947 if( iDc>=0 ){
danielk197762079062007-08-15 17:08:46 +0000948 g.iDeviceCharacteristics = iDc;
949 }
danielk19772ca0f862007-08-23 08:06:44 +0000950 if( iSectorSize>=0 ){
danielk197762079062007-08-15 17:08:46 +0000951 g.iSectorSize = iSectorSize;
952 }
danielk19772ca0f862007-08-23 08:06:44 +0000953
danielk197762079062007-08-15 17:08:46 +0000954 g.iCrash = iDelay;
955 memcpy(g.zCrashFile, zCrashFile, nCrashFile+1);
drh66560ad2006-01-06 14:32:19 +0000956 sqlite3CrashTestEnable = 1;
drh9c06c952005-11-26 00:25:00 +0000957 return TCL_OK;
danielk1977b4b47412007-08-17 15:53:36 +0000958
959error:
danielk1977b4b47412007-08-17 15:53:36 +0000960 return TCL_ERROR;
drh9c06c952005-11-26 00:25:00 +0000961}
962
mistachkin7617e4a2016-07-28 17:11:20 +0000963static int SQLITE_TCLAPI devSymObjCmd(
danielk19772ca0f862007-08-23 08:06:44 +0000964 void * clientData,
965 Tcl_Interp *interp,
966 int objc,
967 Tcl_Obj *CONST objv[]
968){
danielk1977bf260972008-01-22 11:50:13 +0000969 void devsym_register(int iDeviceChar, int iSectorSize);
danielk19772ca0f862007-08-23 08:06:44 +0000970
971 int iDc = -1;
972 int iSectorSize = -1;
danielk1977bf260972008-01-22 11:50:13 +0000973
danielk19772ca0f862007-08-23 08:06:44 +0000974 if( processDevSymArgs(interp, objc-1, &objv[1], &iDc, &iSectorSize) ){
975 return TCL_ERROR;
976 }
danielk1977bf260972008-01-22 11:50:13 +0000977 devsym_register(iDc, iSectorSize);
danielk19772ca0f862007-08-23 08:06:44 +0000978
979 return TCL_OK;
dan33447e72017-07-22 20:12:31 +0000980}
dan05accd22016-04-27 18:54:49 +0000981
dan33447e72017-07-22 20:12:31 +0000982/*
983** tclcmd: sqlite3_crash_on_write N
984*/
985static int SQLITE_TCLAPI writeCrashObjCmd(
986 void * clientData,
987 Tcl_Interp *interp,
988 int objc,
989 Tcl_Obj *CONST objv[]
990){
991 void devsym_crash_on_write(int);
992 int nWrite = 0;
993
994 if( objc!=2 ){
995 Tcl_WrongNumArgs(interp, 1, objv, "NWRITE");
996 return TCL_ERROR;
997 }
998 if( Tcl_GetIntFromObj(interp, objv[1], &nWrite) ){
999 return TCL_ERROR;
1000 }
1001
1002 devsym_crash_on_write(nWrite);
1003 return TCL_OK;
dan05accd22016-04-27 18:54:49 +00001004}
1005
1006/*
1007** tclcmd: unregister_devsim
1008*/
mistachkin7617e4a2016-07-28 17:11:20 +00001009static int SQLITE_TCLAPI dsUnregisterObjCmd(
dan05accd22016-04-27 18:54:49 +00001010 void * clientData,
1011 Tcl_Interp *interp,
1012 int objc,
1013 Tcl_Obj *CONST objv[]
1014){
1015 void devsym_unregister(void);
1016
1017 if( objc!=1 ){
1018 Tcl_WrongNumArgs(interp, 1, objv, "");
1019 return TCL_ERROR;
1020 }
1021
1022 devsym_unregister();
1023 return TCL_OK;
danielk19772ca0f862007-08-23 08:06:44 +00001024}
1025
danielk1977a0fc7292008-12-20 18:33:59 +00001026/*
1027** tclcmd: register_jt_vfs ?-default? PARENT-VFS
1028*/
mistachkin7617e4a2016-07-28 17:11:20 +00001029static int SQLITE_TCLAPI jtObjCmd(
danielk1977a0fc7292008-12-20 18:33:59 +00001030 void * clientData,
1031 Tcl_Interp *interp,
1032 int objc,
1033 Tcl_Obj *CONST objv[]
1034){
1035 int jt_register(char *, int);
1036 char *zParent = 0;
1037
1038 if( objc!=2 && objc!=3 ){
1039 Tcl_WrongNumArgs(interp, 1, objv, "?-default? PARENT-VFS");
1040 return TCL_ERROR;
1041 }
1042 zParent = Tcl_GetString(objv[1]);
1043 if( objc==3 ){
1044 if( strcmp(zParent, "-default") ){
1045 Tcl_AppendResult(interp,
1046 "bad option \"", zParent, "\": must be -default", 0
1047 );
1048 return TCL_ERROR;
1049 }
1050 zParent = Tcl_GetString(objv[2]);
1051 }
1052
1053 if( !(*zParent) ){
1054 zParent = 0;
1055 }
1056 if( jt_register(zParent, objc==3) ){
1057 Tcl_AppendResult(interp, "Error in jt_register", 0);
1058 return TCL_ERROR;
1059 }
1060
1061 return TCL_OK;
1062}
1063
1064/*
1065** tclcmd: unregister_jt_vfs
1066*/
mistachkin7617e4a2016-07-28 17:11:20 +00001067static int SQLITE_TCLAPI jtUnregisterObjCmd(
danielk1977a0fc7292008-12-20 18:33:59 +00001068 void * clientData,
1069 Tcl_Interp *interp,
1070 int objc,
1071 Tcl_Obj *CONST objv[]
1072){
1073 void jt_unregister(void);
1074
1075 if( objc!=1 ){
1076 Tcl_WrongNumArgs(interp, 1, objv, "");
1077 return TCL_ERROR;
1078 }
1079
1080 jt_unregister();
1081 return TCL_OK;
1082}
1083
drh198bf392006-01-06 21:52:49 +00001084#endif /* SQLITE_OMIT_DISKIO */
1085
drh9c06c952005-11-26 00:25:00 +00001086/*
1087** This procedure registers the TCL procedures defined in this file.
1088*/
1089int Sqlitetest6_Init(Tcl_Interp *interp){
drh198bf392006-01-06 21:52:49 +00001090#ifndef SQLITE_OMIT_DISKIO
danielk1977ca0c8972007-09-01 09:02:53 +00001091 Tcl_CreateObjCommand(interp, "sqlite3_crash_enable", crashEnableCmd, 0, 0);
drh9c06c952005-11-26 00:25:00 +00001092 Tcl_CreateObjCommand(interp, "sqlite3_crashparams", crashParamsObjCmd, 0, 0);
danfe912512016-05-24 16:20:51 +00001093 Tcl_CreateObjCommand(interp, "sqlite3_crash_now", crashNowCmd, 0, 0);
danielk19772ca0f862007-08-23 08:06:44 +00001094 Tcl_CreateObjCommand(interp, "sqlite3_simulate_device", devSymObjCmd, 0, 0);
dan33447e72017-07-22 20:12:31 +00001095 Tcl_CreateObjCommand(interp, "sqlite3_crash_on_write", writeCrashObjCmd,0,0);
dan05accd22016-04-27 18:54:49 +00001096 Tcl_CreateObjCommand(interp, "unregister_devsim", dsUnregisterObjCmd, 0, 0);
danielk1977a0fc7292008-12-20 18:33:59 +00001097 Tcl_CreateObjCommand(interp, "register_jt_vfs", jtObjCmd, 0, 0);
1098 Tcl_CreateObjCommand(interp, "unregister_jt_vfs", jtUnregisterObjCmd, 0, 0);
drh198bf392006-01-06 21:52:49 +00001099#endif
drh9c06c952005-11-26 00:25:00 +00001100 return TCL_OK;
1101}
1102
1103#endif /* SQLITE_TEST */