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