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 | */ |
danielk1977 | f55b899 | 2007-08-24 08:15:53 +0000 | [diff] [blame] | 17 | #if SQLITE_TEST /* This file is used for testing only */ |
drh | 9c06c95 | 2005-11-26 00:25:00 +0000 | [diff] [blame] | 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 | f8940ae | 2007-08-23 11:07:10 +0000 | [diff] [blame] | 23 | /* #define TRACE_CRASHTEST */ |
| 24 | |
danielk1977 | 6207906 | 2007-08-15 17:08:46 +0000 | [diff] [blame] | 25 | typedef struct CrashFile CrashFile; |
| 26 | typedef struct CrashGlobal CrashGlobal; |
danielk1977 | 0d24e6b | 2007-08-14 17:42:05 +0000 | [diff] [blame] | 27 | typedef 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 |
| 90 | ** logic governs the behaviour if any of the other ATOMICXXX flags |
| 91 | ** 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 | */ |
danielk1977 | 6207906 | 2007-08-15 17:08:46 +0000 | [diff] [blame] | 108 | |
| 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 | */ |
danielk1977 | 0d24e6b | 2007-08-14 17:42:05 +0000 | [diff] [blame] | 117 | struct WriteBuffer { |
danielk1977 | 6207906 | 2007-08-15 17:08:46 +0000 | [diff] [blame] | 118 | 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 */ |
danielk1977 | 0d24e6b | 2007-08-14 17:42:05 +0000 | [diff] [blame] | 124 | }; |
| 125 | |
danielk1977 | 6207906 | 2007-08-15 17:08:46 +0000 | [diff] [blame] | 126 | struct CrashFile { |
| 127 | const sqlite3_io_methods *pMethod; /* Must be first */ |
| 128 | sqlite3_file *pRealFile; /* Underlying "real" file handle */ |
danielk1977 | 1e53695 | 2007-08-16 10:09:01 +0000 | [diff] [blame] | 129 | char *zName; |
danielk1977 | 967a4a1 | 2007-08-20 14:23:44 +0000 | [diff] [blame] | 130 | |
danielk1977 | f1da17a | 2007-08-21 13:07:46 +0000 | [diff] [blame] | 131 | /* Cache of the entire file. This is used to speed up OsRead() and |
| 132 | ** OsFileSize() calls. Although both could be done by traversing the |
| 133 | ** write-list, in practice this is impractically slow. |
| 134 | */ |
danielk1977 | 967a4a1 | 2007-08-20 14:23:44 +0000 | [diff] [blame] | 135 | int iSize; /* Size of file in bytes */ |
| 136 | int nData; /* Size of buffer allocated at zData */ |
| 137 | u8 *zData; /* Buffer containing file contents */ |
drh | 9c06c95 | 2005-11-26 00:25:00 +0000 | [diff] [blame] | 138 | }; |
| 139 | |
danielk1977 | 6207906 | 2007-08-15 17:08:46 +0000 | [diff] [blame] | 140 | struct CrashGlobal { |
| 141 | WriteBuffer *pWriteList; /* Head of write-list */ |
danielk1977 | 967a4a1 | 2007-08-20 14:23:44 +0000 | [diff] [blame] | 142 | WriteBuffer *pWriteListEnd; /* End of write-list */ |
drh | 9c06c95 | 2005-11-26 00:25:00 +0000 | [diff] [blame] | 143 | |
danielk1977 | 6207906 | 2007-08-15 17:08:46 +0000 | [diff] [blame] | 144 | int iSectorSize; /* Value of simulated sector size */ |
| 145 | int iDeviceCharacteristics; /* Value of simulated device characteristics */ |
drh | 9c06c95 | 2005-11-26 00:25:00 +0000 | [diff] [blame] | 146 | |
danielk1977 | 6207906 | 2007-08-15 17:08:46 +0000 | [diff] [blame] | 147 | int iCrash; /* Crash on the iCrash'th call to xSync() */ |
| 148 | char zCrashFile[500]; /* Crash during an xSync() on this file */ |
| 149 | }; |
drh | 9c06c95 | 2005-11-26 00:25:00 +0000 | [diff] [blame] | 150 | |
danielk1977 | 967a4a1 | 2007-08-20 14:23:44 +0000 | [diff] [blame] | 151 | static CrashGlobal g = {0, 0, SQLITE_DEFAULT_SECTOR_SIZE, 0, 0}; |
drh | 9c06c95 | 2005-11-26 00:25:00 +0000 | [diff] [blame] | 152 | |
| 153 | /* |
drh | 66560ad | 2006-01-06 14:32:19 +0000 | [diff] [blame] | 154 | ** Set this global variable to 1 to enable crash testing. |
| 155 | */ |
danielk1977 | 967a4a1 | 2007-08-20 14:23:44 +0000 | [diff] [blame] | 156 | static int sqlite3CrashTestEnable = 0; |
drh | 66560ad | 2006-01-06 14:32:19 +0000 | [diff] [blame] | 157 | |
| 158 | /* |
danielk1977 | 6207906 | 2007-08-15 17:08:46 +0000 | [diff] [blame] | 159 | ** Flush the write-list as if xSync() had been called on file handle |
| 160 | ** pFile. If isCrash is true, simulate a crash. |
drh | 9c06c95 | 2005-11-26 00:25:00 +0000 | [diff] [blame] | 161 | */ |
danielk1977 | 6207906 | 2007-08-15 17:08:46 +0000 | [diff] [blame] | 162 | static int writeListSync(CrashFile *pFile, int isCrash){ |
| 163 | int rc = SQLITE_OK; |
| 164 | int iDc = g.iDeviceCharacteristics; |
drh | 66560ad | 2006-01-06 14:32:19 +0000 | [diff] [blame] | 165 | |
danielk1977 | 6207906 | 2007-08-15 17:08:46 +0000 | [diff] [blame] | 166 | WriteBuffer *pWrite; |
| 167 | WriteBuffer **ppPtr; |
drh | 66560ad | 2006-01-06 14:32:19 +0000 | [diff] [blame] | 168 | |
danielk1977 | f55b899 | 2007-08-24 08:15:53 +0000 | [diff] [blame] | 169 | /* If this is not a crash simulation, set pFinal to point to the |
| 170 | ** last element of the write-list that is associated with file handle |
| 171 | ** pFile. |
| 172 | ** |
| 173 | ** If this is a crash simulation, set pFinal to an arbitrarily selected |
| 174 | ** element of the write-list. |
danielk1977 | 6207906 | 2007-08-15 17:08:46 +0000 | [diff] [blame] | 175 | */ |
| 176 | WriteBuffer *pFinal = 0; |
| 177 | if( !isCrash ){ |
| 178 | for(pWrite=g.pWriteList; pWrite; pWrite=pWrite->pNext){ |
| 179 | if( pWrite->pFile==pFile ){ |
| 180 | pFinal = pWrite; |
| 181 | } |
| 182 | } |
danielk1977 | f55b899 | 2007-08-24 08:15:53 +0000 | [diff] [blame] | 183 | }else if( iDc&(SQLITE_IOCAP_SEQUENTIAL|SQLITE_IOCAP_SAFE_APPEND) ){ |
| 184 | int nWrite = 0; |
| 185 | int iFinal; |
| 186 | for(pWrite=g.pWriteList; pWrite; pWrite=pWrite->pNext) nWrite++; |
| 187 | sqlite3Randomness(sizeof(int), &iFinal); |
| 188 | iFinal = ((iFinal<0)?-1*iFinal:iFinal)%nWrite; |
| 189 | for(pWrite=g.pWriteList; iFinal>0; pWrite=pWrite->pNext) iFinal--; |
| 190 | pFinal = pWrite; |
drh | 1a23593 | 2005-11-29 18:37:15 +0000 | [diff] [blame] | 191 | } |
drh | 66560ad | 2006-01-06 14:32:19 +0000 | [diff] [blame] | 192 | |
danielk1977 | f8940ae | 2007-08-23 11:07:10 +0000 | [diff] [blame] | 193 | #ifdef TRACE_CRASHTEST |
| 194 | printf("Sync %s (is %s crash)\n", pFile->zName, (isCrash?"a":"not a")); |
| 195 | #endif |
| 196 | |
danielk1977 | 6207906 | 2007-08-15 17:08:46 +0000 | [diff] [blame] | 197 | ppPtr = &g.pWriteList; |
| 198 | for(pWrite=*ppPtr; rc==SQLITE_OK && pWrite; pWrite=*ppPtr){ |
danielk1977 | 967a4a1 | 2007-08-20 14:23:44 +0000 | [diff] [blame] | 199 | sqlite3_file *pRealFile = pWrite->pFile->pRealFile; |
danielk1977 | 6207906 | 2007-08-15 17:08:46 +0000 | [diff] [blame] | 200 | |
| 201 | /* (eAction==1) -> write block out normally, |
| 202 | ** (eAction==2) -> do nothing, |
| 203 | ** (eAction==3) -> trash sectors. |
| 204 | */ |
| 205 | int eAction = 0; |
| 206 | if( !isCrash ){ |
| 207 | eAction = 2; |
| 208 | if( (pWrite->pFile==pFile || iDc&SQLITE_IOCAP_SEQUENTIAL) ){ |
| 209 | eAction = 1; |
| 210 | } |
| 211 | }else{ |
| 212 | char random; |
| 213 | sqlite3Randomness(1, &random); |
| 214 | |
danielk1977 | f55b899 | 2007-08-24 08:15:53 +0000 | [diff] [blame] | 215 | /* Do not select option 3 (sector trashing) if the IOCAP_ATOMIC flag |
| 216 | ** is set or this is an OsTruncate(), not an Oswrite(). |
| 217 | */ |
| 218 | if( (iDc&SQLITE_IOCAP_ATOMIC) || (pWrite->zBuf==0) ){ |
danielk1977 | 6207906 | 2007-08-15 17:08:46 +0000 | [diff] [blame] | 219 | random &= 0x01; |
| 220 | } |
| 221 | |
danielk1977 | f55b899 | 2007-08-24 08:15:53 +0000 | [diff] [blame] | 222 | /* If IOCAP_SEQUENTIAL is set and this is not the final entry |
| 223 | ** in the truncated write-list, always select option 1 (write |
| 224 | ** out correctly). |
| 225 | */ |
| 226 | if( (iDc&SQLITE_IOCAP_SEQUENTIAL && pWrite!=pFinal) ){ |
| 227 | random = 0; |
| 228 | } |
| 229 | |
| 230 | /* If IOCAP_SAFE_APPEND is set and this OsWrite() operation is |
| 231 | ** an append (first byte of the written region is 1 byte past the |
| 232 | ** current EOF), always select option 1 (write out correctly). |
| 233 | */ |
| 234 | if( iDc&SQLITE_IOCAP_SAFE_APPEND && pWrite->zBuf ){ |
| 235 | i64 iSize; |
| 236 | sqlite3OsFileSize(pRealFile, &iSize); |
| 237 | if( iSize==pWrite->iOffset ){ |
| 238 | random = 0; |
| 239 | } |
| 240 | } |
| 241 | |
danielk1977 | 6207906 | 2007-08-15 17:08:46 +0000 | [diff] [blame] | 242 | if( (random&0x06)==0x06 ){ |
| 243 | eAction = 3; |
| 244 | }else{ |
| 245 | eAction = ((random&0x01)?2:1); |
| 246 | } |
| 247 | } |
| 248 | |
| 249 | switch( eAction ){ |
| 250 | case 1: { /* Write out correctly */ |
| 251 | if( pWrite->zBuf ){ |
| 252 | rc = sqlite3OsWrite( |
danielk1977 | 967a4a1 | 2007-08-20 14:23:44 +0000 | [diff] [blame] | 253 | pRealFile, pWrite->zBuf, pWrite->nBuf, pWrite->iOffset |
danielk1977 | 6207906 | 2007-08-15 17:08:46 +0000 | [diff] [blame] | 254 | ); |
| 255 | }else{ |
danielk1977 | 967a4a1 | 2007-08-20 14:23:44 +0000 | [diff] [blame] | 256 | rc = sqlite3OsTruncate(pRealFile, pWrite->iOffset); |
danielk1977 | 6207906 | 2007-08-15 17:08:46 +0000 | [diff] [blame] | 257 | } |
| 258 | *ppPtr = pWrite->pNext; |
danielk1977 | f8940ae | 2007-08-23 11:07:10 +0000 | [diff] [blame] | 259 | #ifdef TRACE_CRASHTEST |
| 260 | if( isCrash ){ |
danielk1977 | f55b899 | 2007-08-24 08:15:53 +0000 | [diff] [blame] | 261 | printf("Writing %d bytes @ %d (%s)\n", |
| 262 | pWrite->nBuf, (int)pWrite->iOffset, pWrite->pFile->zName |
| 263 | ); |
danielk1977 | f8940ae | 2007-08-23 11:07:10 +0000 | [diff] [blame] | 264 | } |
| 265 | #endif |
drh | 1743575 | 2007-08-16 04:30:38 +0000 | [diff] [blame] | 266 | sqlite3_free(pWrite); |
danielk1977 | 6207906 | 2007-08-15 17:08:46 +0000 | [diff] [blame] | 267 | break; |
| 268 | } |
| 269 | case 2: { /* Do nothing */ |
| 270 | ppPtr = &pWrite->pNext; |
danielk1977 | f8940ae | 2007-08-23 11:07:10 +0000 | [diff] [blame] | 271 | #ifdef TRACE_CRASHTEST |
| 272 | if( isCrash ){ |
danielk1977 | f55b899 | 2007-08-24 08:15:53 +0000 | [diff] [blame] | 273 | printf("Omiting %d bytes @ %d (%s)\n", |
| 274 | pWrite->nBuf, (int)pWrite->iOffset, pWrite->pFile->zName |
| 275 | ); |
danielk1977 | f8940ae | 2007-08-23 11:07:10 +0000 | [diff] [blame] | 276 | } |
| 277 | #endif |
danielk1977 | 6207906 | 2007-08-15 17:08:46 +0000 | [diff] [blame] | 278 | break; |
| 279 | } |
| 280 | case 3: { /* Trash sectors */ |
| 281 | u8 *zGarbage; |
danielk1977 | 967a4a1 | 2007-08-20 14:23:44 +0000 | [diff] [blame] | 282 | int iFirst = (pWrite->iOffset/g.iSectorSize); |
| 283 | int iLast = (pWrite->iOffset+pWrite->nBuf-1)/g.iSectorSize; |
| 284 | |
| 285 | assert(pWrite->zBuf); |
danielk1977 | 6207906 | 2007-08-15 17:08:46 +0000 | [diff] [blame] | 286 | |
danielk1977 | f8940ae | 2007-08-23 11:07:10 +0000 | [diff] [blame] | 287 | #ifdef TRACE_CRASHTEST |
danielk1977 | f55b899 | 2007-08-24 08:15:53 +0000 | [diff] [blame] | 288 | printf("Trashing %d sectors @ sector %d (%s)\n", |
| 289 | 1+iLast-iFirst, iFirst, pWrite->pFile->zName |
| 290 | ); |
danielk1977 | f8940ae | 2007-08-23 11:07:10 +0000 | [diff] [blame] | 291 | #endif |
| 292 | |
drh | 1743575 | 2007-08-16 04:30:38 +0000 | [diff] [blame] | 293 | zGarbage = sqlite3_malloc(g.iSectorSize); |
danielk1977 | 6207906 | 2007-08-15 17:08:46 +0000 | [diff] [blame] | 294 | if( zGarbage ){ |
| 295 | sqlite3_int64 i; |
| 296 | for(i=iFirst; rc==SQLITE_OK && i<=iLast; i++){ |
| 297 | sqlite3Randomness(g.iSectorSize, zGarbage); |
| 298 | rc = sqlite3OsWrite( |
danielk1977 | 967a4a1 | 2007-08-20 14:23:44 +0000 | [diff] [blame] | 299 | pRealFile, zGarbage, g.iSectorSize, i*g.iSectorSize |
danielk1977 | 6207906 | 2007-08-15 17:08:46 +0000 | [diff] [blame] | 300 | ); |
| 301 | } |
drh | 1743575 | 2007-08-16 04:30:38 +0000 | [diff] [blame] | 302 | sqlite3_free(zGarbage); |
danielk1977 | 6207906 | 2007-08-15 17:08:46 +0000 | [diff] [blame] | 303 | }else{ |
| 304 | rc = SQLITE_NOMEM; |
| 305 | } |
| 306 | |
| 307 | ppPtr = &pWrite->pNext; |
| 308 | break; |
| 309 | } |
| 310 | |
| 311 | default: |
| 312 | assert(!"Cannot happen"); |
| 313 | } |
| 314 | |
| 315 | if( pWrite==pFinal ) break; |
drh | 1a23593 | 2005-11-29 18:37:15 +0000 | [diff] [blame] | 316 | } |
danielk1977 | 6207906 | 2007-08-15 17:08:46 +0000 | [diff] [blame] | 317 | |
| 318 | if( rc==SQLITE_OK && isCrash ){ |
| 319 | exit(-1); |
| 320 | } |
| 321 | |
danielk1977 | 967a4a1 | 2007-08-20 14:23:44 +0000 | [diff] [blame] | 322 | for(pWrite=g.pWriteList; pWrite && pWrite->pNext; pWrite=pWrite->pNext); |
| 323 | g.pWriteListEnd = pWrite; |
| 324 | |
drh | 1a23593 | 2005-11-29 18:37:15 +0000 | [diff] [blame] | 325 | return rc; |
drh | 9c06c95 | 2005-11-26 00:25:00 +0000 | [diff] [blame] | 326 | } |
| 327 | |
| 328 | /* |
danielk1977 | 6207906 | 2007-08-15 17:08:46 +0000 | [diff] [blame] | 329 | ** Add an entry to the end of the write-list. |
drh | 1883921 | 2005-11-26 03:43:23 +0000 | [diff] [blame] | 330 | */ |
danielk1977 | 6207906 | 2007-08-15 17:08:46 +0000 | [diff] [blame] | 331 | static int writeListAppend( |
| 332 | sqlite3_file *pFile, |
| 333 | sqlite3_int64 iOffset, |
| 334 | const u8 *zBuf, |
| 335 | int nBuf |
| 336 | ){ |
| 337 | WriteBuffer *pNew; |
| 338 | |
| 339 | assert((zBuf && nBuf) || (!nBuf && !zBuf)); |
| 340 | |
danielk1977 | b4b4741 | 2007-08-17 15:53:36 +0000 | [diff] [blame] | 341 | pNew = (WriteBuffer *)sqlite3MallocZero(sizeof(WriteBuffer) + nBuf); |
danielk1977 | 6207906 | 2007-08-15 17:08:46 +0000 | [diff] [blame] | 342 | pNew->iOffset = iOffset; |
| 343 | pNew->nBuf = nBuf; |
| 344 | pNew->pFile = (CrashFile *)pFile; |
| 345 | if( zBuf ){ |
| 346 | pNew->zBuf = (u8 *)&pNew[1]; |
| 347 | memcpy(pNew->zBuf, zBuf, nBuf); |
| 348 | } |
| 349 | |
| 350 | if( g.pWriteList ){ |
danielk1977 | 967a4a1 | 2007-08-20 14:23:44 +0000 | [diff] [blame] | 351 | assert(g.pWriteListEnd); |
| 352 | g.pWriteListEnd->pNext = pNew; |
danielk1977 | 6207906 | 2007-08-15 17:08:46 +0000 | [diff] [blame] | 353 | }else{ |
| 354 | g.pWriteList = pNew; |
| 355 | } |
danielk1977 | 967a4a1 | 2007-08-20 14:23:44 +0000 | [diff] [blame] | 356 | g.pWriteListEnd = pNew; |
danielk1977 | 6207906 | 2007-08-15 17:08:46 +0000 | [diff] [blame] | 357 | |
drh | 1a23593 | 2005-11-29 18:37:15 +0000 | [diff] [blame] | 358 | return SQLITE_OK; |
| 359 | } |
| 360 | |
| 361 | /* |
danielk1977 | 6207906 | 2007-08-15 17:08:46 +0000 | [diff] [blame] | 362 | ** Close a crash-file. |
drh | 1a23593 | 2005-11-29 18:37:15 +0000 | [diff] [blame] | 363 | */ |
danielk1977 | 1e53695 | 2007-08-16 10:09:01 +0000 | [diff] [blame] | 364 | static int cfClose(sqlite3_file *pFile){ |
danielk1977 | 6207906 | 2007-08-15 17:08:46 +0000 | [diff] [blame] | 365 | CrashFile *pCrash = (CrashFile *)pFile; |
| 366 | writeListSync(pCrash, 0); |
danielk1977 | f1da17a | 2007-08-21 13:07:46 +0000 | [diff] [blame] | 367 | sqlite3OsClose(pCrash->pRealFile); |
danielk1977 | 6207906 | 2007-08-15 17:08:46 +0000 | [diff] [blame] | 368 | return SQLITE_OK; |
drh | 1a23593 | 2005-11-29 18:37:15 +0000 | [diff] [blame] | 369 | } |
| 370 | |
| 371 | /* |
danielk1977 | 6207906 | 2007-08-15 17:08:46 +0000 | [diff] [blame] | 372 | ** Read data from a crash-file. |
drh | 1a23593 | 2005-11-29 18:37:15 +0000 | [diff] [blame] | 373 | */ |
danielk1977 | 1e53695 | 2007-08-16 10:09:01 +0000 | [diff] [blame] | 374 | static int cfRead( |
| 375 | sqlite3_file *pFile, |
| 376 | void *zBuf, |
| 377 | int iAmt, |
| 378 | sqlite_int64 iOfst |
| 379 | ){ |
danielk1977 | 6207906 | 2007-08-15 17:08:46 +0000 | [diff] [blame] | 380 | CrashFile *pCrash = (CrashFile *)pFile; |
danielk1977 | 6207906 | 2007-08-15 17:08:46 +0000 | [diff] [blame] | 381 | |
| 382 | /* Check the file-size to see if this is a short-read */ |
danielk1977 | 967a4a1 | 2007-08-20 14:23:44 +0000 | [diff] [blame] | 383 | if( pCrash->iSize<(iOfst+iAmt) ){ |
danielk1977 | 6207906 | 2007-08-15 17:08:46 +0000 | [diff] [blame] | 384 | return SQLITE_IOERR_SHORT_READ; |
| 385 | } |
| 386 | |
danielk1977 | 967a4a1 | 2007-08-20 14:23:44 +0000 | [diff] [blame] | 387 | memcpy(zBuf, &pCrash->zData[iOfst], iAmt); |
| 388 | return SQLITE_OK; |
drh | 1883921 | 2005-11-26 03:43:23 +0000 | [diff] [blame] | 389 | } |
| 390 | |
| 391 | /* |
danielk1977 | 6207906 | 2007-08-15 17:08:46 +0000 | [diff] [blame] | 392 | ** Write data to a crash-file. |
danielk1977 | b472117 | 2007-03-19 05:54:48 +0000 | [diff] [blame] | 393 | */ |
danielk1977 | 1e53695 | 2007-08-16 10:09:01 +0000 | [diff] [blame] | 394 | static int cfWrite( |
| 395 | sqlite3_file *pFile, |
| 396 | const void *zBuf, |
| 397 | int iAmt, |
| 398 | sqlite_int64 iOfst |
| 399 | ){ |
danielk1977 | 967a4a1 | 2007-08-20 14:23:44 +0000 | [diff] [blame] | 400 | CrashFile *pCrash = (CrashFile *)pFile; |
| 401 | if( iAmt+iOfst>pCrash->iSize ){ |
| 402 | pCrash->iSize = iAmt+iOfst; |
| 403 | } |
| 404 | while( pCrash->iSize>pCrash->nData ){ |
drh | 4a50aac | 2007-08-23 02:47:53 +0000 | [diff] [blame] | 405 | u8 *zNew; |
danielk1977 | 967a4a1 | 2007-08-20 14:23:44 +0000 | [diff] [blame] | 406 | int nNew = (pCrash->nData*2) + 4096; |
drh | 4a50aac | 2007-08-23 02:47:53 +0000 | [diff] [blame] | 407 | zNew = sqlite3_realloc(pCrash->zData, nNew); |
danielk1977 | 967a4a1 | 2007-08-20 14:23:44 +0000 | [diff] [blame] | 408 | if( !zNew ){ |
| 409 | return SQLITE_NOMEM; |
| 410 | } |
| 411 | memset(&zNew[pCrash->nData], 0, nNew-pCrash->nData); |
| 412 | pCrash->nData = nNew; |
| 413 | pCrash->zData = zNew; |
| 414 | } |
| 415 | memcpy(&pCrash->zData[iOfst], zBuf, iAmt); |
danielk1977 | 6207906 | 2007-08-15 17:08:46 +0000 | [diff] [blame] | 416 | return writeListAppend(pFile, iOfst, zBuf, iAmt); |
danielk1977 | b472117 | 2007-03-19 05:54:48 +0000 | [diff] [blame] | 417 | } |
| 418 | |
| 419 | /* |
danielk1977 | 6207906 | 2007-08-15 17:08:46 +0000 | [diff] [blame] | 420 | ** Truncate a crash-file. |
drh | 054889e | 2005-11-30 03:20:31 +0000 | [diff] [blame] | 421 | */ |
danielk1977 | 1e53695 | 2007-08-16 10:09:01 +0000 | [diff] [blame] | 422 | static int cfTruncate(sqlite3_file *pFile, sqlite_int64 size){ |
danielk1977 | 967a4a1 | 2007-08-20 14:23:44 +0000 | [diff] [blame] | 423 | CrashFile *pCrash = (CrashFile *)pFile; |
| 424 | assert(size>=0); |
| 425 | if( pCrash->iSize>size ){ |
| 426 | pCrash->iSize = size; |
| 427 | } |
danielk1977 | 6207906 | 2007-08-15 17:08:46 +0000 | [diff] [blame] | 428 | return writeListAppend(pFile, size, 0, 0); |
| 429 | } |
| 430 | |
| 431 | /* |
| 432 | ** Sync a crash-file. |
| 433 | */ |
danielk1977 | 1e53695 | 2007-08-16 10:09:01 +0000 | [diff] [blame] | 434 | static int cfSync(sqlite3_file *pFile, int flags){ |
danielk1977 | 6207906 | 2007-08-15 17:08:46 +0000 | [diff] [blame] | 435 | CrashFile *pCrash = (CrashFile *)pFile; |
| 436 | int isCrash = 0; |
| 437 | |
danielk1977 | 967a4a1 | 2007-08-20 14:23:44 +0000 | [diff] [blame] | 438 | const char *zName = pCrash->zName; |
| 439 | const char *zCrashFile = g.zCrashFile; |
| 440 | int nName = strlen(zName); |
| 441 | int nCrashFile = strlen(zCrashFile); |
| 442 | |
| 443 | if( nCrashFile>0 && zCrashFile[nCrashFile-1]=='*' ){ |
| 444 | nCrashFile--; |
| 445 | if( nName>nCrashFile ) nName = nCrashFile; |
| 446 | } |
| 447 | |
| 448 | if( nName==nCrashFile && 0==memcmp(zName, zCrashFile, nName) ){ |
| 449 | if( (--g.iCrash)==0 ) isCrash = 1; |
danielk1977 | 6207906 | 2007-08-15 17:08:46 +0000 | [diff] [blame] | 450 | } |
| 451 | |
| 452 | return writeListSync(pCrash, isCrash); |
| 453 | } |
| 454 | |
| 455 | /* |
| 456 | ** Return the current file-size of the crash-file. |
| 457 | */ |
danielk1977 | 1e53695 | 2007-08-16 10:09:01 +0000 | [diff] [blame] | 458 | static int cfFileSize(sqlite3_file *pFile, sqlite_int64 *pSize){ |
danielk1977 | 6207906 | 2007-08-15 17:08:46 +0000 | [diff] [blame] | 459 | CrashFile *pCrash = (CrashFile *)pFile; |
danielk1977 | 967a4a1 | 2007-08-20 14:23:44 +0000 | [diff] [blame] | 460 | *pSize = (i64)pCrash->iSize; |
danielk1977 | 6207906 | 2007-08-15 17:08:46 +0000 | [diff] [blame] | 461 | return SQLITE_OK; |
| 462 | } |
| 463 | |
| 464 | /* |
| 465 | ** Calls related to file-locks are passed on to the real file handle. |
| 466 | */ |
danielk1977 | 1e53695 | 2007-08-16 10:09:01 +0000 | [diff] [blame] | 467 | static int cfLock(sqlite3_file *pFile, int eLock){ |
danielk1977 | 6207906 | 2007-08-15 17:08:46 +0000 | [diff] [blame] | 468 | return sqlite3OsLock(((CrashFile *)pFile)->pRealFile, eLock); |
| 469 | } |
danielk1977 | 1e53695 | 2007-08-16 10:09:01 +0000 | [diff] [blame] | 470 | static int cfUnlock(sqlite3_file *pFile, int eLock){ |
danielk1977 | 6207906 | 2007-08-15 17:08:46 +0000 | [diff] [blame] | 471 | return sqlite3OsUnlock(((CrashFile *)pFile)->pRealFile, eLock); |
| 472 | } |
danielk1977 | 1e53695 | 2007-08-16 10:09:01 +0000 | [diff] [blame] | 473 | static int cfCheckReservedLock(sqlite3_file *pFile){ |
danielk1977 | 6207906 | 2007-08-15 17:08:46 +0000 | [diff] [blame] | 474 | return sqlite3OsCheckReservedLock(((CrashFile *)pFile)->pRealFile); |
| 475 | } |
danielk1977 | 967a4a1 | 2007-08-20 14:23:44 +0000 | [diff] [blame] | 476 | static int cfLockState(sqlite3_file *pFile){ |
| 477 | return sqlite3OsLockState(((CrashFile *)pFile)->pRealFile); |
| 478 | } |
danielk1977 | 1e53695 | 2007-08-16 10:09:01 +0000 | [diff] [blame] | 479 | static int cfBreakLock(sqlite3_file *pFile){ |
danielk1977 | 6207906 | 2007-08-15 17:08:46 +0000 | [diff] [blame] | 480 | return sqlite3OsBreakLock(((CrashFile *)pFile)->pRealFile); |
| 481 | } |
| 482 | |
| 483 | /* |
| 484 | ** The xSectorSize() and xDeviceCharacteristics() functions return |
| 485 | ** the global values configured by the [sqlite_crashparams] tcl |
| 486 | * interface. |
| 487 | */ |
danielk1977 | 1e53695 | 2007-08-16 10:09:01 +0000 | [diff] [blame] | 488 | static int cfSectorSize(sqlite3_file *pFile){ |
danielk1977 | 6207906 | 2007-08-15 17:08:46 +0000 | [diff] [blame] | 489 | return g.iSectorSize; |
| 490 | } |
danielk1977 | 1e53695 | 2007-08-16 10:09:01 +0000 | [diff] [blame] | 491 | static int cfDeviceCharacteristics(sqlite3_file *pFile){ |
danielk1977 | 6207906 | 2007-08-15 17:08:46 +0000 | [diff] [blame] | 492 | return g.iDeviceCharacteristics; |
| 493 | } |
| 494 | |
| 495 | static const sqlite3_io_methods CrashFileVtab = { |
| 496 | 1, /* iVersion */ |
| 497 | cfClose, /* xClose */ |
| 498 | cfRead, /* xRead */ |
| 499 | cfWrite, /* xWrite */ |
| 500 | cfTruncate, /* xTruncate */ |
| 501 | cfSync, /* xSync */ |
| 502 | cfFileSize, /* xFileSize */ |
| 503 | cfLock, /* xLock */ |
| 504 | cfUnlock, /* xUnlock */ |
| 505 | cfCheckReservedLock, /* xCheckReservedLock */ |
| 506 | cfBreakLock, /* xBreakLock */ |
danielk1977 | 967a4a1 | 2007-08-20 14:23:44 +0000 | [diff] [blame] | 507 | cfLockState, /* xLockState */ |
danielk1977 | 6207906 | 2007-08-15 17:08:46 +0000 | [diff] [blame] | 508 | cfSectorSize, /* xSectorSize */ |
| 509 | cfDeviceCharacteristics /* xDeviceCharacteristics */ |
drh | 054889e | 2005-11-30 03:20:31 +0000 | [diff] [blame] | 510 | }; |
| 511 | |
drh | 054889e | 2005-11-30 03:20:31 +0000 | [diff] [blame] | 512 | /* |
drh | d677b3d | 2007-08-20 22:48:41 +0000 | [diff] [blame] | 513 | ** Application data for the crash VFS |
| 514 | */ |
| 515 | struct crashAppData { |
danielk1977 | f1da17a | 2007-08-21 13:07:46 +0000 | [diff] [blame] | 516 | sqlite3_vfs *pOrig; /* Wrapped vfs structure */ |
drh | d677b3d | 2007-08-20 22:48:41 +0000 | [diff] [blame] | 517 | }; |
| 518 | |
| 519 | /* |
danielk1977 | 0e87b70 | 2007-08-25 12:29:30 +0000 | [diff] [blame^] | 520 | ** Open a crash-file file handle. |
danielk1977 | 967a4a1 | 2007-08-20 14:23:44 +0000 | [diff] [blame] | 521 | ** |
| 522 | ** The caller will have allocated pVfs->szOsFile bytes of space |
| 523 | ** at pFile. This file uses this space for the CrashFile structure |
| 524 | ** and allocates space for the "real" file structure using |
| 525 | ** sqlite3_malloc(). The assumption here is (pVfs->szOsFile) is |
| 526 | ** equal or greater than sizeof(CrashFile). |
drh | 054889e | 2005-11-30 03:20:31 +0000 | [diff] [blame] | 527 | */ |
danielk1977 | f1da17a | 2007-08-21 13:07:46 +0000 | [diff] [blame] | 528 | static int cfOpen( |
danielk1977 | f55b899 | 2007-08-24 08:15:53 +0000 | [diff] [blame] | 529 | sqlite3_vfs *pCfVfs, |
danielk1977 | 6207906 | 2007-08-15 17:08:46 +0000 | [diff] [blame] | 530 | const char *zName, |
| 531 | sqlite3_file *pFile, |
| 532 | int flags, |
| 533 | int *pOutFlags |
| 534 | ){ |
danielk1977 | f55b899 | 2007-08-24 08:15:53 +0000 | [diff] [blame] | 535 | sqlite3_vfs *pVfs = (sqlite3_vfs *)pCfVfs->pAppData; |
danielk1977 | 967a4a1 | 2007-08-20 14:23:44 +0000 | [diff] [blame] | 536 | int rc; |
danielk1977 | f1da17a | 2007-08-21 13:07:46 +0000 | [diff] [blame] | 537 | CrashFile *pWrapper = (CrashFile *)pFile; |
drh | 4a50aac | 2007-08-23 02:47:53 +0000 | [diff] [blame] | 538 | sqlite3_file *pReal = (sqlite3_file*)&pWrapper[1]; |
danielk1977 | f1da17a | 2007-08-21 13:07:46 +0000 | [diff] [blame] | 539 | |
| 540 | memset(pWrapper, 0, sizeof(CrashFile)); |
| 541 | rc = sqlite3OsOpen(pVfs, zName, pReal, flags, pOutFlags); |
| 542 | |
| 543 | if( rc==SQLITE_OK ){ |
| 544 | i64 iSize; |
| 545 | pWrapper->pMethod = &CrashFileVtab; |
| 546 | pWrapper->zName = (char *)zName; |
| 547 | pWrapper->pRealFile = pReal; |
| 548 | rc = sqlite3OsFileSize(pReal, &iSize); |
| 549 | pWrapper->iSize = (int)iSize; |
| 550 | } |
| 551 | if( rc==SQLITE_OK ){ |
| 552 | pWrapper->nData = (4096 + pWrapper->iSize); |
drh | 4a50aac | 2007-08-23 02:47:53 +0000 | [diff] [blame] | 553 | pWrapper->zData = sqlite3_malloc(pWrapper->nData); |
danielk1977 | f1da17a | 2007-08-21 13:07:46 +0000 | [diff] [blame] | 554 | if( pWrapper->zData ){ |
| 555 | memset(pWrapper->zData, 0, pWrapper->nData); |
| 556 | rc = sqlite3OsRead(pReal, pWrapper->zData, pWrapper->iSize, 0); |
| 557 | }else{ |
| 558 | rc = SQLITE_NOMEM; |
danielk1977 | 6207906 | 2007-08-15 17:08:46 +0000 | [diff] [blame] | 559 | } |
danielk1977 | f1da17a | 2007-08-21 13:07:46 +0000 | [diff] [blame] | 560 | } |
| 561 | if( rc!=SQLITE_OK && pWrapper->pMethod ){ |
| 562 | sqlite3OsClose(pFile); |
danielk1977 | 6207906 | 2007-08-15 17:08:46 +0000 | [diff] [blame] | 563 | } |
| 564 | return rc; |
| 565 | } |
| 566 | |
danielk1977 | f55b899 | 2007-08-24 08:15:53 +0000 | [diff] [blame] | 567 | static int cfDelete(sqlite3_vfs *pCfVfs, const char *zPath, int dirSync){ |
| 568 | sqlite3_vfs *pVfs = (sqlite3_vfs *)pCfVfs->pAppData; |
| 569 | return pVfs->xDelete(pVfs, zPath, dirSync); |
danielk1977 | f1da17a | 2007-08-21 13:07:46 +0000 | [diff] [blame] | 570 | } |
danielk1977 | f55b899 | 2007-08-24 08:15:53 +0000 | [diff] [blame] | 571 | static int cfAccess(sqlite3_vfs *pCfVfs, const char *zPath, int flags){ |
| 572 | sqlite3_vfs *pVfs = (sqlite3_vfs *)pCfVfs->pAppData; |
| 573 | return pVfs->xAccess(pVfs, zPath, flags); |
danielk1977 | f1da17a | 2007-08-21 13:07:46 +0000 | [diff] [blame] | 574 | } |
danielk1977 | f55b899 | 2007-08-24 08:15:53 +0000 | [diff] [blame] | 575 | static int cfGetTempName(sqlite3_vfs *pCfVfs, char *zBufOut){ |
| 576 | sqlite3_vfs *pVfs = (sqlite3_vfs *)pCfVfs->pAppData; |
| 577 | return pVfs->xGetTempName(pVfs, zBufOut); |
danielk1977 | f1da17a | 2007-08-21 13:07:46 +0000 | [diff] [blame] | 578 | } |
danielk1977 | f55b899 | 2007-08-24 08:15:53 +0000 | [diff] [blame] | 579 | static int cfFullPathname(sqlite3_vfs *pCfVfs, const char *zPath, char *zPathOut){ |
| 580 | sqlite3_vfs *pVfs = (sqlite3_vfs *)pCfVfs->pAppData; |
| 581 | return pVfs->xFullPathname(pVfs, zPath, zPathOut); |
danielk1977 | f1da17a | 2007-08-21 13:07:46 +0000 | [diff] [blame] | 582 | } |
danielk1977 | f55b899 | 2007-08-24 08:15:53 +0000 | [diff] [blame] | 583 | static void *cfDlOpen(sqlite3_vfs *pCfVfs, const char *zPath){ |
| 584 | sqlite3_vfs *pVfs = (sqlite3_vfs *)pCfVfs->pAppData; |
| 585 | return pVfs->xDlOpen(pVfs, zPath); |
danielk1977 | f1da17a | 2007-08-21 13:07:46 +0000 | [diff] [blame] | 586 | } |
danielk1977 | 0e87b70 | 2007-08-25 12:29:30 +0000 | [diff] [blame^] | 587 | static void cfDlError(sqlite3_vfs *pCfVfs, int nByte, char *zErrMsg){ |
| 588 | sqlite3_vfs *pVfs = (sqlite3_vfs *)pCfVfs->pAppData; |
| 589 | pVfs->xDlError(pVfs, nByte, zErrMsg); |
| 590 | } |
| 591 | static void *cfDlSym(sqlite3_vfs *pCfVfs, void *pHandle, const char *zSymbol){ |
| 592 | sqlite3_vfs *pVfs = (sqlite3_vfs *)pCfVfs->pAppData; |
| 593 | return pVfs->xDlSym(pVfs, pHandle, zSymbol); |
| 594 | } |
| 595 | static void cfDlClose(sqlite3_vfs *pCfVfs, void *pHandle){ |
| 596 | sqlite3_vfs *pVfs = (sqlite3_vfs *)pCfVfs->pAppData; |
| 597 | pVfs->xDlClose(pVfs, pHandle); |
| 598 | } |
danielk1977 | f55b899 | 2007-08-24 08:15:53 +0000 | [diff] [blame] | 599 | static int cfRandomness(sqlite3_vfs *pCfVfs, int nByte, char *zBufOut){ |
| 600 | sqlite3_vfs *pVfs = (sqlite3_vfs *)pCfVfs->pAppData; |
| 601 | return pVfs->xRandomness(pVfs, nByte, zBufOut); |
danielk1977 | f1da17a | 2007-08-21 13:07:46 +0000 | [diff] [blame] | 602 | } |
danielk1977 | f55b899 | 2007-08-24 08:15:53 +0000 | [diff] [blame] | 603 | static int cfSleep(sqlite3_vfs *pCfVfs, int nMicro){ |
| 604 | sqlite3_vfs *pVfs = (sqlite3_vfs *)pCfVfs->pAppData; |
| 605 | return pVfs->xSleep(pVfs, nMicro); |
danielk1977 | f1da17a | 2007-08-21 13:07:46 +0000 | [diff] [blame] | 606 | } |
danielk1977 | f55b899 | 2007-08-24 08:15:53 +0000 | [diff] [blame] | 607 | static int cfCurrentTime(sqlite3_vfs *pCfVfs, double *pTimeOut){ |
| 608 | sqlite3_vfs *pVfs = (sqlite3_vfs *)pCfVfs->pAppData; |
| 609 | return pVfs->xCurrentTime(pVfs, pTimeOut); |
danielk1977 | f1da17a | 2007-08-21 13:07:46 +0000 | [diff] [blame] | 610 | } |
| 611 | |
danielk1977 | 2ca0f86 | 2007-08-23 08:06:44 +0000 | [diff] [blame] | 612 | static int processDevSymArgs( |
| 613 | Tcl_Interp *interp, |
| 614 | int objc, |
| 615 | Tcl_Obj *CONST objv[], |
| 616 | int *piDeviceChar, |
| 617 | int *piSectorSize |
| 618 | ){ |
| 619 | struct DeviceFlag { |
| 620 | char *zName; |
| 621 | int iValue; |
| 622 | } aFlag[] = { |
| 623 | { "atomic", SQLITE_IOCAP_ATOMIC }, |
| 624 | { "atomic512", SQLITE_IOCAP_ATOMIC512 }, |
| 625 | { "atomic1k", SQLITE_IOCAP_ATOMIC1K }, |
| 626 | { "atomic2k", SQLITE_IOCAP_ATOMIC2K }, |
| 627 | { "atomic4k", SQLITE_IOCAP_ATOMIC4K }, |
| 628 | { "atomic8k", SQLITE_IOCAP_ATOMIC8K }, |
| 629 | { "atomic16k", SQLITE_IOCAP_ATOMIC16K }, |
| 630 | { "atomic32k", SQLITE_IOCAP_ATOMIC32K }, |
| 631 | { "atomic64k", SQLITE_IOCAP_ATOMIC64K }, |
| 632 | { "sequential", SQLITE_IOCAP_SEQUENTIAL }, |
| 633 | { "safe_append", SQLITE_IOCAP_SAFE_APPEND }, |
| 634 | { 0, 0 } |
| 635 | }; |
| 636 | |
| 637 | int i; |
| 638 | int iDc = 0; |
| 639 | int iSectorSize = 0; |
| 640 | int setSectorsize = 0; |
| 641 | int setDeviceChar = 0; |
| 642 | |
| 643 | for(i=0; i<objc; i+=2){ |
| 644 | int nOpt; |
| 645 | char *zOpt = Tcl_GetStringFromObj(objv[i], &nOpt); |
| 646 | |
| 647 | if( (nOpt>11 || nOpt<2 || strncmp("-sectorsize", zOpt, nOpt)) |
| 648 | && (nOpt>16 || nOpt<2 || strncmp("-characteristics", zOpt, nOpt)) |
| 649 | ){ |
| 650 | Tcl_AppendResult(interp, |
| 651 | "Bad option: \"", zOpt, |
| 652 | "\" - must be \"-characteristics\" or \"-sectorsize\"", 0 |
| 653 | ); |
| 654 | return TCL_ERROR; |
| 655 | } |
| 656 | if( i==objc-1 ){ |
| 657 | Tcl_AppendResult(interp, "Option requires an argument: \"", zOpt, "\"",0); |
| 658 | return TCL_ERROR; |
| 659 | } |
| 660 | |
| 661 | if( zOpt[1]=='s' ){ |
| 662 | if( Tcl_GetIntFromObj(interp, objv[i+1], &iSectorSize) ){ |
| 663 | return TCL_ERROR; |
| 664 | } |
| 665 | setSectorsize = 1; |
| 666 | }else{ |
| 667 | int j; |
| 668 | Tcl_Obj **apObj; |
| 669 | int nObj; |
| 670 | if( Tcl_ListObjGetElements(interp, objv[i+1], &nObj, &apObj) ){ |
| 671 | return TCL_ERROR; |
| 672 | } |
| 673 | for(j=0; j<nObj; j++){ |
| 674 | int rc; |
| 675 | int iChoice; |
| 676 | Tcl_Obj *pFlag = Tcl_DuplicateObj(apObj[j]); |
| 677 | Tcl_IncrRefCount(pFlag); |
| 678 | Tcl_UtfToLower(Tcl_GetString(pFlag)); |
| 679 | |
| 680 | rc = Tcl_GetIndexFromObjStruct( |
| 681 | interp, pFlag, aFlag, sizeof(aFlag[0]), "no such flag", 0, &iChoice |
| 682 | ); |
| 683 | Tcl_DecrRefCount(pFlag); |
| 684 | if( rc ){ |
| 685 | return TCL_ERROR; |
| 686 | } |
| 687 | |
| 688 | iDc |= aFlag[iChoice].iValue; |
| 689 | } |
| 690 | setDeviceChar = 1; |
| 691 | } |
| 692 | } |
| 693 | |
| 694 | if( setDeviceChar ){ |
| 695 | *piDeviceChar = iDc; |
| 696 | } |
| 697 | if( setSectorsize ){ |
| 698 | *piSectorSize = iSectorSize; |
| 699 | } |
| 700 | |
| 701 | return TCL_OK; |
| 702 | } |
| 703 | |
drh | 054889e | 2005-11-30 03:20:31 +0000 | [diff] [blame] | 704 | /* |
danielk1977 | 6207906 | 2007-08-15 17:08:46 +0000 | [diff] [blame] | 705 | ** tclcmd: sqlite_crashparams ?OPTIONS? DELAY CRASHFILE |
drh | 9c06c95 | 2005-11-26 00:25:00 +0000 | [diff] [blame] | 706 | ** |
| 707 | ** This procedure implements a TCL command that enables crash testing |
| 708 | ** in testfixture. Once enabled, crash testing cannot be disabled. |
danielk1977 | 6207906 | 2007-08-15 17:08:46 +0000 | [diff] [blame] | 709 | ** |
| 710 | ** Available options are "-characteristics" and "-sectorsize". Both require |
| 711 | ** an argument. For -sectorsize, this is the simulated sector size in |
| 712 | ** bytes. For -characteristics, the argument must be a list of io-capability |
| 713 | ** flags to simulate. Valid flags are "atomic", "atomic512", "atomic1K", |
| 714 | ** "atomic2K", "atomic4K", "atomic8K", "atomic16K", "atomic32K", |
| 715 | ** "atomic64K", "sequential" and "safe_append". |
| 716 | ** |
| 717 | ** Example: |
| 718 | ** |
| 719 | ** sqlite_crashparams -sect 1024 -char {atomic sequential} ./test.db 1 |
| 720 | ** |
drh | 9c06c95 | 2005-11-26 00:25:00 +0000 | [diff] [blame] | 721 | */ |
| 722 | static int crashParamsObjCmd( |
| 723 | void * clientData, |
| 724 | Tcl_Interp *interp, |
| 725 | int objc, |
| 726 | Tcl_Obj *CONST objv[] |
| 727 | ){ |
danielk1977 | 6207906 | 2007-08-15 17:08:46 +0000 | [diff] [blame] | 728 | int iDelay; |
| 729 | const char *zCrashFile; |
drh | 153c62c | 2007-08-24 03:51:33 +0000 | [diff] [blame] | 730 | int nCrashFile, iDc, iSectorSize; |
drh | d677b3d | 2007-08-20 22:48:41 +0000 | [diff] [blame] | 731 | |
danielk1977 | f1da17a | 2007-08-21 13:07:46 +0000 | [diff] [blame] | 732 | static sqlite3_vfs crashVfs = { |
| 733 | 1, /* iVersion */ |
| 734 | 0, /* szOsFile */ |
| 735 | 0, /* mxPathname */ |
danielk1977 | f1da17a | 2007-08-21 13:07:46 +0000 | [diff] [blame] | 736 | 0, /* pNext */ |
| 737 | "crash", /* zName */ |
| 738 | 0, /* pAppData */ |
| 739 | |
| 740 | cfOpen, /* xOpen */ |
| 741 | cfDelete, /* xDelete */ |
| 742 | cfAccess, /* xAccess */ |
| 743 | cfGetTempName, /* xGetTempName */ |
| 744 | cfFullPathname, /* xFullPathname */ |
| 745 | cfDlOpen, /* xDlOpen */ |
danielk1977 | 0e87b70 | 2007-08-25 12:29:30 +0000 | [diff] [blame^] | 746 | cfDlError, /* xDlError */ |
| 747 | cfDlSym, /* xDlSym */ |
| 748 | cfDlClose, /* xDlClose */ |
danielk1977 | f1da17a | 2007-08-21 13:07:46 +0000 | [diff] [blame] | 749 | cfRandomness, /* xRandomness */ |
| 750 | cfSleep, /* xSleep */ |
| 751 | cfCurrentTime /* xCurrentTime */ |
| 752 | }; |
| 753 | |
drh | 153c62c | 2007-08-24 03:51:33 +0000 | [diff] [blame] | 754 | |
danielk1977 | f1da17a | 2007-08-21 13:07:46 +0000 | [diff] [blame] | 755 | if( crashVfs.pAppData==0 ){ |
| 756 | sqlite3_vfs *pOriginalVfs = sqlite3_vfs_find(0); |
danielk1977 | f1da17a | 2007-08-21 13:07:46 +0000 | [diff] [blame] | 757 | crashVfs.mxPathname = pOriginalVfs->mxPathname; |
| 758 | crashVfs.pAppData = (void *)pOriginalVfs; |
| 759 | crashVfs.szOsFile = sizeof(CrashFile) + pOriginalVfs->szOsFile; |
danielk1977 | f1da17a | 2007-08-21 13:07:46 +0000 | [diff] [blame] | 760 | /* sqlite3_vfs_unregister(pOriginalVfs); */ |
drh | d677b3d | 2007-08-20 22:48:41 +0000 | [diff] [blame] | 761 | sqlite3_vfs_register(&crashVfs, 1); |
| 762 | } |
danielk1977 | 6207906 | 2007-08-15 17:08:46 +0000 | [diff] [blame] | 763 | |
drh | 153c62c | 2007-08-24 03:51:33 +0000 | [diff] [blame] | 764 | iDc = -1; |
| 765 | iSectorSize = -1; |
danielk1977 | 6207906 | 2007-08-15 17:08:46 +0000 | [diff] [blame] | 766 | |
danielk1977 | 6207906 | 2007-08-15 17:08:46 +0000 | [diff] [blame] | 767 | if( objc<3 ){ |
| 768 | Tcl_WrongNumArgs(interp, 1, objv, "?OPTIONS? DELAY CRASHFILE"); |
danielk1977 | b4b4741 | 2007-08-17 15:53:36 +0000 | [diff] [blame] | 769 | goto error; |
drh | 9c06c95 | 2005-11-26 00:25:00 +0000 | [diff] [blame] | 770 | } |
danielk1977 | 6207906 | 2007-08-15 17:08:46 +0000 | [diff] [blame] | 771 | |
danielk1977 | 967a4a1 | 2007-08-20 14:23:44 +0000 | [diff] [blame] | 772 | zCrashFile = Tcl_GetStringFromObj(objv[objc-1], &nCrashFile); |
danielk1977 | 6207906 | 2007-08-15 17:08:46 +0000 | [diff] [blame] | 773 | if( nCrashFile>=sizeof(g.zCrashFile) ){ |
| 774 | Tcl_AppendResult(interp, "Filename is too long: \"", zCrashFile, "\"", 0); |
danielk1977 | b4b4741 | 2007-08-17 15:53:36 +0000 | [diff] [blame] | 775 | goto error; |
drh | 9c06c95 | 2005-11-26 00:25:00 +0000 | [diff] [blame] | 776 | } |
danielk1977 | 6207906 | 2007-08-15 17:08:46 +0000 | [diff] [blame] | 777 | if( Tcl_GetIntFromObj(interp, objv[objc-2], &iDelay) ){ |
danielk1977 | b4b4741 | 2007-08-17 15:53:36 +0000 | [diff] [blame] | 778 | goto error; |
danielk1977 | 6207906 | 2007-08-15 17:08:46 +0000 | [diff] [blame] | 779 | } |
| 780 | |
danielk1977 | 2ca0f86 | 2007-08-23 08:06:44 +0000 | [diff] [blame] | 781 | if( processDevSymArgs(interp, objc-3, &objv[1], &iDc, &iSectorSize) ){ |
| 782 | return TCL_ERROR; |
danielk1977 | 59a33f9 | 2007-03-17 10:26:59 +0000 | [diff] [blame] | 783 | } |
danielk1977 | 6207906 | 2007-08-15 17:08:46 +0000 | [diff] [blame] | 784 | |
danielk1977 | 2ca0f86 | 2007-08-23 08:06:44 +0000 | [diff] [blame] | 785 | if( iDc>=0 ){ |
danielk1977 | 6207906 | 2007-08-15 17:08:46 +0000 | [diff] [blame] | 786 | g.iDeviceCharacteristics = iDc; |
| 787 | } |
danielk1977 | 2ca0f86 | 2007-08-23 08:06:44 +0000 | [diff] [blame] | 788 | if( iSectorSize>=0 ){ |
danielk1977 | 6207906 | 2007-08-15 17:08:46 +0000 | [diff] [blame] | 789 | g.iSectorSize = iSectorSize; |
| 790 | } |
danielk1977 | 2ca0f86 | 2007-08-23 08:06:44 +0000 | [diff] [blame] | 791 | |
danielk1977 | 6207906 | 2007-08-15 17:08:46 +0000 | [diff] [blame] | 792 | g.iCrash = iDelay; |
| 793 | memcpy(g.zCrashFile, zCrashFile, nCrashFile+1); |
drh | 66560ad | 2006-01-06 14:32:19 +0000 | [diff] [blame] | 794 | sqlite3CrashTestEnable = 1; |
drh | 9c06c95 | 2005-11-26 00:25:00 +0000 | [diff] [blame] | 795 | return TCL_OK; |
danielk1977 | b4b4741 | 2007-08-17 15:53:36 +0000 | [diff] [blame] | 796 | |
| 797 | error: |
danielk1977 | b4b4741 | 2007-08-17 15:53:36 +0000 | [diff] [blame] | 798 | return TCL_ERROR; |
drh | 9c06c95 | 2005-11-26 00:25:00 +0000 | [diff] [blame] | 799 | } |
| 800 | |
danielk1977 | 2ca0f86 | 2007-08-23 08:06:44 +0000 | [diff] [blame] | 801 | static int devSymObjCmd( |
| 802 | void * clientData, |
| 803 | Tcl_Interp *interp, |
| 804 | int objc, |
| 805 | Tcl_Obj *CONST objv[] |
| 806 | ){ |
| 807 | |
| 808 | extern int sqlite3_test_device_characteristics; |
| 809 | extern int sqlite3_test_sector_size; |
| 810 | |
| 811 | int iDc = -1; |
| 812 | int iSectorSize = -1; |
| 813 | if( processDevSymArgs(interp, objc-1, &objv[1], &iDc, &iSectorSize) ){ |
| 814 | return TCL_ERROR; |
| 815 | } |
| 816 | |
| 817 | if( iDc>=0 ){ |
| 818 | sqlite3_test_device_characteristics = iDc; |
| 819 | } |
| 820 | if( iSectorSize>=0 ){ |
| 821 | sqlite3_test_sector_size = iSectorSize; |
| 822 | } |
| 823 | |
| 824 | return TCL_OK; |
| 825 | } |
| 826 | |
drh | 198bf39 | 2006-01-06 21:52:49 +0000 | [diff] [blame] | 827 | #endif /* SQLITE_OMIT_DISKIO */ |
| 828 | |
drh | 9c06c95 | 2005-11-26 00:25:00 +0000 | [diff] [blame] | 829 | /* |
| 830 | ** This procedure registers the TCL procedures defined in this file. |
| 831 | */ |
| 832 | int Sqlitetest6_Init(Tcl_Interp *interp){ |
drh | 198bf39 | 2006-01-06 21:52:49 +0000 | [diff] [blame] | 833 | #ifndef SQLITE_OMIT_DISKIO |
drh | 9c06c95 | 2005-11-26 00:25:00 +0000 | [diff] [blame] | 834 | Tcl_CreateObjCommand(interp, "sqlite3_crashparams", crashParamsObjCmd, 0, 0); |
danielk1977 | 2ca0f86 | 2007-08-23 08:06:44 +0000 | [diff] [blame] | 835 | Tcl_CreateObjCommand(interp, "sqlite3_simulate_device", devSymObjCmd, 0, 0); |
drh | 198bf39 | 2006-01-06 21:52:49 +0000 | [diff] [blame] | 836 | #endif |
drh | 9c06c95 | 2005-11-26 00:25:00 +0000 | [diff] [blame] | 837 | return TCL_OK; |
| 838 | } |
| 839 | |
| 840 | #endif /* SQLITE_TEST */ |