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 |
mistachkin | 48864df | 2013-03-21 21:20:32 +0000 | [diff] [blame] | 90 | ** logic governs the behavior if any of the other ATOMICXXX flags |
danielk1977 | 0d24e6b | 2007-08-14 17:42:05 +0000 | [diff] [blame] | 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 | d6846d7 | 2009-02-11 14:27:04 +0000 | [diff] [blame] | 130 | int flags; /* Flags the file was opened with */ |
danielk1977 | 967a4a1 | 2007-08-20 14:23:44 +0000 | [diff] [blame] | 131 | |
danielk1977 | f1da17a | 2007-08-21 13:07:46 +0000 | [diff] [blame] | 132 | /* Cache of the entire file. This is used to speed up OsRead() and |
| 133 | ** OsFileSize() calls. Although both could be done by traversing the |
| 134 | ** write-list, in practice this is impractically slow. |
| 135 | */ |
danielk1977 | 967a4a1 | 2007-08-20 14:23:44 +0000 | [diff] [blame] | 136 | u8 *zData; /* Buffer containing file contents */ |
drh | a703f50 | 2013-10-15 14:29:32 +0000 | [diff] [blame] | 137 | int nData; /* Size of buffer allocated at zData */ |
| 138 | i64 iSize; /* Size of file in bytes */ |
drh | 9c06c95 | 2005-11-26 00:25:00 +0000 | [diff] [blame] | 139 | }; |
| 140 | |
danielk1977 | 6207906 | 2007-08-15 17:08:46 +0000 | [diff] [blame] | 141 | struct CrashGlobal { |
| 142 | WriteBuffer *pWriteList; /* Head of write-list */ |
danielk1977 | 967a4a1 | 2007-08-20 14:23:44 +0000 | [diff] [blame] | 143 | WriteBuffer *pWriteListEnd; /* End of write-list */ |
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 iSectorSize; /* Value of simulated sector size */ |
| 146 | int iDeviceCharacteristics; /* Value of simulated device characteristics */ |
drh | 9c06c95 | 2005-11-26 00:25:00 +0000 | [diff] [blame] | 147 | |
danielk1977 | 6207906 | 2007-08-15 17:08:46 +0000 | [diff] [blame] | 148 | int iCrash; /* Crash on the iCrash'th call to xSync() */ |
| 149 | char zCrashFile[500]; /* Crash during an xSync() on this file */ |
| 150 | }; |
drh | 9c06c95 | 2005-11-26 00:25:00 +0000 | [diff] [blame] | 151 | |
danielk1977 | 967a4a1 | 2007-08-20 14:23:44 +0000 | [diff] [blame] | 152 | static CrashGlobal g = {0, 0, SQLITE_DEFAULT_SECTOR_SIZE, 0, 0}; |
drh | 9c06c95 | 2005-11-26 00:25:00 +0000 | [diff] [blame] | 153 | |
| 154 | /* |
drh | 66560ad | 2006-01-06 14:32:19 +0000 | [diff] [blame] | 155 | ** Set this global variable to 1 to enable crash testing. |
| 156 | */ |
danielk1977 | 967a4a1 | 2007-08-20 14:23:44 +0000 | [diff] [blame] | 157 | static int sqlite3CrashTestEnable = 0; |
drh | 66560ad | 2006-01-06 14:32:19 +0000 | [diff] [blame] | 158 | |
danielk1977 | a98d7b4 | 2008-01-18 13:42:54 +0000 | [diff] [blame] | 159 | static void *crash_malloc(int nByte){ |
| 160 | return (void *)Tcl_Alloc((size_t)nByte); |
| 161 | } |
| 162 | static void crash_free(void *p){ |
| 163 | Tcl_Free(p); |
| 164 | } |
| 165 | static void *crash_realloc(void *p, int n){ |
| 166 | return (void *)Tcl_Realloc(p, (size_t)n); |
| 167 | } |
| 168 | |
drh | 66560ad | 2006-01-06 14:32:19 +0000 | [diff] [blame] | 169 | /* |
danielk1977 | d6846d7 | 2009-02-11 14:27:04 +0000 | [diff] [blame] | 170 | ** Wrapper around the sqlite3OsWrite() function that avoids writing to the |
| 171 | ** 512 byte block begining at offset PENDING_BYTE. |
| 172 | */ |
| 173 | static int writeDbFile(CrashFile *p, u8 *z, i64 iAmt, i64 iOff){ |
shaneh | ebffe41 | 2010-07-08 16:22:51 +0000 | [diff] [blame] | 174 | int rc = SQLITE_OK; |
danielk1977 | d6846d7 | 2009-02-11 14:27:04 +0000 | [diff] [blame] | 175 | int iSkip = 0; |
danielk1977 | d6846d7 | 2009-02-11 14:27:04 +0000 | [diff] [blame] | 176 | if( (iAmt-iSkip)>0 ){ |
drh | 7da5fcb | 2012-03-30 14:59:43 +0000 | [diff] [blame] | 177 | rc = sqlite3OsWrite(p->pRealFile, &z[iSkip], (int)(iAmt-iSkip), iOff+iSkip); |
danielk1977 | d6846d7 | 2009-02-11 14:27:04 +0000 | [diff] [blame] | 178 | } |
| 179 | return rc; |
| 180 | } |
| 181 | |
| 182 | /* |
danielk1977 | 6207906 | 2007-08-15 17:08:46 +0000 | [diff] [blame] | 183 | ** Flush the write-list as if xSync() had been called on file handle |
| 184 | ** pFile. If isCrash is true, simulate a crash. |
drh | 9c06c95 | 2005-11-26 00:25:00 +0000 | [diff] [blame] | 185 | */ |
danielk1977 | 6207906 | 2007-08-15 17:08:46 +0000 | [diff] [blame] | 186 | static int writeListSync(CrashFile *pFile, int isCrash){ |
| 187 | int rc = SQLITE_OK; |
| 188 | int iDc = g.iDeviceCharacteristics; |
drh | 66560ad | 2006-01-06 14:32:19 +0000 | [diff] [blame] | 189 | |
danielk1977 | 6207906 | 2007-08-15 17:08:46 +0000 | [diff] [blame] | 190 | WriteBuffer *pWrite; |
| 191 | WriteBuffer **ppPtr; |
drh | 66560ad | 2006-01-06 14:32:19 +0000 | [diff] [blame] | 192 | |
danielk1977 | f55b899 | 2007-08-24 08:15:53 +0000 | [diff] [blame] | 193 | /* If this is not a crash simulation, set pFinal to point to the |
| 194 | ** last element of the write-list that is associated with file handle |
| 195 | ** pFile. |
| 196 | ** |
| 197 | ** If this is a crash simulation, set pFinal to an arbitrarily selected |
| 198 | ** element of the write-list. |
danielk1977 | 6207906 | 2007-08-15 17:08:46 +0000 | [diff] [blame] | 199 | */ |
| 200 | WriteBuffer *pFinal = 0; |
| 201 | if( !isCrash ){ |
| 202 | for(pWrite=g.pWriteList; pWrite; pWrite=pWrite->pNext){ |
| 203 | if( pWrite->pFile==pFile ){ |
| 204 | pFinal = pWrite; |
| 205 | } |
| 206 | } |
danielk1977 | f55b899 | 2007-08-24 08:15:53 +0000 | [diff] [blame] | 207 | }else if( iDc&(SQLITE_IOCAP_SEQUENTIAL|SQLITE_IOCAP_SAFE_APPEND) ){ |
| 208 | int nWrite = 0; |
| 209 | int iFinal; |
| 210 | for(pWrite=g.pWriteList; pWrite; pWrite=pWrite->pNext) nWrite++; |
drh | 2fa1868 | 2008-03-19 14:15:34 +0000 | [diff] [blame] | 211 | sqlite3_randomness(sizeof(int), &iFinal); |
danielk1977 | f55b899 | 2007-08-24 08:15:53 +0000 | [diff] [blame] | 212 | iFinal = ((iFinal<0)?-1*iFinal:iFinal)%nWrite; |
| 213 | for(pWrite=g.pWriteList; iFinal>0; pWrite=pWrite->pNext) iFinal--; |
| 214 | pFinal = pWrite; |
drh | 1a23593 | 2005-11-29 18:37:15 +0000 | [diff] [blame] | 215 | } |
drh | 66560ad | 2006-01-06 14:32:19 +0000 | [diff] [blame] | 216 | |
danielk1977 | f8940ae | 2007-08-23 11:07:10 +0000 | [diff] [blame] | 217 | #ifdef TRACE_CRASHTEST |
dan | fe91251 | 2016-05-24 16:20:51 +0000 | [diff] [blame] | 218 | if( pFile ){ |
| 219 | printf("Sync %s (is %s crash)\n", pFile->zName, (isCrash?"a":"not a")); |
| 220 | } |
danielk1977 | f8940ae | 2007-08-23 11:07:10 +0000 | [diff] [blame] | 221 | #endif |
| 222 | |
danielk1977 | 6207906 | 2007-08-15 17:08:46 +0000 | [diff] [blame] | 223 | ppPtr = &g.pWriteList; |
| 224 | for(pWrite=*ppPtr; rc==SQLITE_OK && pWrite; pWrite=*ppPtr){ |
danielk1977 | 967a4a1 | 2007-08-20 14:23:44 +0000 | [diff] [blame] | 225 | sqlite3_file *pRealFile = pWrite->pFile->pRealFile; |
danielk1977 | 6207906 | 2007-08-15 17:08:46 +0000 | [diff] [blame] | 226 | |
| 227 | /* (eAction==1) -> write block out normally, |
| 228 | ** (eAction==2) -> do nothing, |
| 229 | ** (eAction==3) -> trash sectors. |
| 230 | */ |
| 231 | int eAction = 0; |
| 232 | if( !isCrash ){ |
| 233 | eAction = 2; |
| 234 | if( (pWrite->pFile==pFile || iDc&SQLITE_IOCAP_SEQUENTIAL) ){ |
| 235 | eAction = 1; |
| 236 | } |
| 237 | }else{ |
| 238 | char random; |
drh | 2fa1868 | 2008-03-19 14:15:34 +0000 | [diff] [blame] | 239 | sqlite3_randomness(1, &random); |
danielk1977 | 6207906 | 2007-08-15 17:08:46 +0000 | [diff] [blame] | 240 | |
danielk1977 | f55b899 | 2007-08-24 08:15:53 +0000 | [diff] [blame] | 241 | /* Do not select option 3 (sector trashing) if the IOCAP_ATOMIC flag |
| 242 | ** is set or this is an OsTruncate(), not an Oswrite(). |
| 243 | */ |
| 244 | if( (iDc&SQLITE_IOCAP_ATOMIC) || (pWrite->zBuf==0) ){ |
danielk1977 | 6207906 | 2007-08-15 17:08:46 +0000 | [diff] [blame] | 245 | random &= 0x01; |
| 246 | } |
| 247 | |
danielk1977 | f55b899 | 2007-08-24 08:15:53 +0000 | [diff] [blame] | 248 | /* If IOCAP_SEQUENTIAL is set and this is not the final entry |
| 249 | ** in the truncated write-list, always select option 1 (write |
| 250 | ** out correctly). |
| 251 | */ |
| 252 | if( (iDc&SQLITE_IOCAP_SEQUENTIAL && pWrite!=pFinal) ){ |
| 253 | random = 0; |
| 254 | } |
| 255 | |
| 256 | /* If IOCAP_SAFE_APPEND is set and this OsWrite() operation is |
| 257 | ** an append (first byte of the written region is 1 byte past the |
| 258 | ** current EOF), always select option 1 (write out correctly). |
| 259 | */ |
| 260 | if( iDc&SQLITE_IOCAP_SAFE_APPEND && pWrite->zBuf ){ |
| 261 | i64 iSize; |
| 262 | sqlite3OsFileSize(pRealFile, &iSize); |
| 263 | if( iSize==pWrite->iOffset ){ |
| 264 | random = 0; |
| 265 | } |
| 266 | } |
| 267 | |
danielk1977 | 6207906 | 2007-08-15 17:08:46 +0000 | [diff] [blame] | 268 | if( (random&0x06)==0x06 ){ |
| 269 | eAction = 3; |
| 270 | }else{ |
| 271 | eAction = ((random&0x01)?2:1); |
| 272 | } |
| 273 | } |
| 274 | |
| 275 | switch( eAction ){ |
| 276 | case 1: { /* Write out correctly */ |
| 277 | if( pWrite->zBuf ){ |
danielk1977 | d6846d7 | 2009-02-11 14:27:04 +0000 | [diff] [blame] | 278 | rc = writeDbFile( |
| 279 | pWrite->pFile, pWrite->zBuf, pWrite->nBuf, pWrite->iOffset |
danielk1977 | 6207906 | 2007-08-15 17:08:46 +0000 | [diff] [blame] | 280 | ); |
| 281 | }else{ |
danielk1977 | 967a4a1 | 2007-08-20 14:23:44 +0000 | [diff] [blame] | 282 | rc = sqlite3OsTruncate(pRealFile, pWrite->iOffset); |
danielk1977 | 6207906 | 2007-08-15 17:08:46 +0000 | [diff] [blame] | 283 | } |
| 284 | *ppPtr = pWrite->pNext; |
danielk1977 | f8940ae | 2007-08-23 11:07:10 +0000 | [diff] [blame] | 285 | #ifdef TRACE_CRASHTEST |
| 286 | if( isCrash ){ |
danielk1977 | f55b899 | 2007-08-24 08:15:53 +0000 | [diff] [blame] | 287 | printf("Writing %d bytes @ %d (%s)\n", |
| 288 | pWrite->nBuf, (int)pWrite->iOffset, pWrite->pFile->zName |
| 289 | ); |
danielk1977 | f8940ae | 2007-08-23 11:07:10 +0000 | [diff] [blame] | 290 | } |
| 291 | #endif |
danielk1977 | a98d7b4 | 2008-01-18 13:42:54 +0000 | [diff] [blame] | 292 | crash_free(pWrite); |
danielk1977 | 6207906 | 2007-08-15 17:08:46 +0000 | [diff] [blame] | 293 | break; |
| 294 | } |
| 295 | case 2: { /* Do nothing */ |
| 296 | ppPtr = &pWrite->pNext; |
danielk1977 | f8940ae | 2007-08-23 11:07:10 +0000 | [diff] [blame] | 297 | #ifdef TRACE_CRASHTEST |
| 298 | if( isCrash ){ |
danielk1977 | f55b899 | 2007-08-24 08:15:53 +0000 | [diff] [blame] | 299 | printf("Omiting %d bytes @ %d (%s)\n", |
| 300 | pWrite->nBuf, (int)pWrite->iOffset, pWrite->pFile->zName |
| 301 | ); |
danielk1977 | f8940ae | 2007-08-23 11:07:10 +0000 | [diff] [blame] | 302 | } |
| 303 | #endif |
danielk1977 | 6207906 | 2007-08-15 17:08:46 +0000 | [diff] [blame] | 304 | break; |
| 305 | } |
| 306 | case 3: { /* Trash sectors */ |
| 307 | u8 *zGarbage; |
drh | 7da5fcb | 2012-03-30 14:59:43 +0000 | [diff] [blame] | 308 | int iFirst = (int)(pWrite->iOffset/g.iSectorSize); |
| 309 | int iLast = (int)((pWrite->iOffset+pWrite->nBuf-1)/g.iSectorSize); |
danielk1977 | 967a4a1 | 2007-08-20 14:23:44 +0000 | [diff] [blame] | 310 | |
| 311 | assert(pWrite->zBuf); |
danielk1977 | 6207906 | 2007-08-15 17:08:46 +0000 | [diff] [blame] | 312 | |
danielk1977 | f8940ae | 2007-08-23 11:07:10 +0000 | [diff] [blame] | 313 | #ifdef TRACE_CRASHTEST |
mistachkin | e1b461b | 2012-10-18 09:39:16 +0000 | [diff] [blame] | 314 | printf("Trashing %d sectors @ %lld (sector %d) (%s)\n", |
dan | 1e3e418 | 2012-10-17 16:20:36 +0000 | [diff] [blame] | 315 | 1+iLast-iFirst, pWrite->iOffset, iFirst, pWrite->pFile->zName |
danielk1977 | f55b899 | 2007-08-24 08:15:53 +0000 | [diff] [blame] | 316 | ); |
danielk1977 | f8940ae | 2007-08-23 11:07:10 +0000 | [diff] [blame] | 317 | #endif |
| 318 | |
danielk1977 | a98d7b4 | 2008-01-18 13:42:54 +0000 | [diff] [blame] | 319 | zGarbage = crash_malloc(g.iSectorSize); |
danielk1977 | 6207906 | 2007-08-15 17:08:46 +0000 | [diff] [blame] | 320 | if( zGarbage ){ |
| 321 | sqlite3_int64 i; |
| 322 | for(i=iFirst; rc==SQLITE_OK && i<=iLast; i++){ |
drh | 2fa1868 | 2008-03-19 14:15:34 +0000 | [diff] [blame] | 323 | sqlite3_randomness(g.iSectorSize, zGarbage); |
danielk1977 | d6846d7 | 2009-02-11 14:27:04 +0000 | [diff] [blame] | 324 | rc = writeDbFile( |
| 325 | pWrite->pFile, zGarbage, g.iSectorSize, i*g.iSectorSize |
danielk1977 | 6207906 | 2007-08-15 17:08:46 +0000 | [diff] [blame] | 326 | ); |
| 327 | } |
danielk1977 | a98d7b4 | 2008-01-18 13:42:54 +0000 | [diff] [blame] | 328 | crash_free(zGarbage); |
danielk1977 | 6207906 | 2007-08-15 17:08:46 +0000 | [diff] [blame] | 329 | }else{ |
| 330 | rc = SQLITE_NOMEM; |
| 331 | } |
| 332 | |
| 333 | ppPtr = &pWrite->pNext; |
| 334 | break; |
| 335 | } |
| 336 | |
| 337 | default: |
| 338 | assert(!"Cannot happen"); |
| 339 | } |
| 340 | |
| 341 | if( pWrite==pFinal ) break; |
drh | 1a23593 | 2005-11-29 18:37:15 +0000 | [diff] [blame] | 342 | } |
danielk1977 | 6207906 | 2007-08-15 17:08:46 +0000 | [diff] [blame] | 343 | |
| 344 | if( rc==SQLITE_OK && isCrash ){ |
| 345 | exit(-1); |
| 346 | } |
| 347 | |
danielk1977 | 967a4a1 | 2007-08-20 14:23:44 +0000 | [diff] [blame] | 348 | for(pWrite=g.pWriteList; pWrite && pWrite->pNext; pWrite=pWrite->pNext); |
| 349 | g.pWriteListEnd = pWrite; |
| 350 | |
drh | 1a23593 | 2005-11-29 18:37:15 +0000 | [diff] [blame] | 351 | return rc; |
drh | 9c06c95 | 2005-11-26 00:25:00 +0000 | [diff] [blame] | 352 | } |
| 353 | |
| 354 | /* |
danielk1977 | 6207906 | 2007-08-15 17:08:46 +0000 | [diff] [blame] | 355 | ** Add an entry to the end of the write-list. |
drh | 1883921 | 2005-11-26 03:43:23 +0000 | [diff] [blame] | 356 | */ |
danielk1977 | 6207906 | 2007-08-15 17:08:46 +0000 | [diff] [blame] | 357 | static int writeListAppend( |
| 358 | sqlite3_file *pFile, |
| 359 | sqlite3_int64 iOffset, |
| 360 | const u8 *zBuf, |
| 361 | int nBuf |
| 362 | ){ |
| 363 | WriteBuffer *pNew; |
| 364 | |
| 365 | assert((zBuf && nBuf) || (!nBuf && !zBuf)); |
| 366 | |
danielk1977 | a98d7b4 | 2008-01-18 13:42:54 +0000 | [diff] [blame] | 367 | pNew = (WriteBuffer *)crash_malloc(sizeof(WriteBuffer) + nBuf); |
drh | a018dc7 | 2007-12-14 17:22:23 +0000 | [diff] [blame] | 368 | if( pNew==0 ){ |
| 369 | fprintf(stderr, "out of memory in the crash simulator\n"); |
| 370 | } |
danielk1977 | a98d7b4 | 2008-01-18 13:42:54 +0000 | [diff] [blame] | 371 | memset(pNew, 0, sizeof(WriteBuffer)+nBuf); |
danielk1977 | 6207906 | 2007-08-15 17:08:46 +0000 | [diff] [blame] | 372 | pNew->iOffset = iOffset; |
| 373 | pNew->nBuf = nBuf; |
| 374 | pNew->pFile = (CrashFile *)pFile; |
| 375 | if( zBuf ){ |
| 376 | pNew->zBuf = (u8 *)&pNew[1]; |
| 377 | memcpy(pNew->zBuf, zBuf, nBuf); |
| 378 | } |
| 379 | |
| 380 | if( g.pWriteList ){ |
danielk1977 | 967a4a1 | 2007-08-20 14:23:44 +0000 | [diff] [blame] | 381 | assert(g.pWriteListEnd); |
| 382 | g.pWriteListEnd->pNext = pNew; |
danielk1977 | 6207906 | 2007-08-15 17:08:46 +0000 | [diff] [blame] | 383 | }else{ |
| 384 | g.pWriteList = pNew; |
| 385 | } |
danielk1977 | 967a4a1 | 2007-08-20 14:23:44 +0000 | [diff] [blame] | 386 | g.pWriteListEnd = pNew; |
danielk1977 | 6207906 | 2007-08-15 17:08:46 +0000 | [diff] [blame] | 387 | |
drh | 1a23593 | 2005-11-29 18:37:15 +0000 | [diff] [blame] | 388 | return SQLITE_OK; |
| 389 | } |
| 390 | |
| 391 | /* |
danielk1977 | 6207906 | 2007-08-15 17:08:46 +0000 | [diff] [blame] | 392 | ** Close a crash-file. |
drh | 1a23593 | 2005-11-29 18:37:15 +0000 | [diff] [blame] | 393 | */ |
danielk1977 | 1e53695 | 2007-08-16 10:09:01 +0000 | [diff] [blame] | 394 | static int cfClose(sqlite3_file *pFile){ |
danielk1977 | 6207906 | 2007-08-15 17:08:46 +0000 | [diff] [blame] | 395 | CrashFile *pCrash = (CrashFile *)pFile; |
| 396 | writeListSync(pCrash, 0); |
danielk1977 | f1da17a | 2007-08-21 13:07:46 +0000 | [diff] [blame] | 397 | sqlite3OsClose(pCrash->pRealFile); |
danielk1977 | 6207906 | 2007-08-15 17:08:46 +0000 | [diff] [blame] | 398 | return SQLITE_OK; |
drh | 1a23593 | 2005-11-29 18:37:15 +0000 | [diff] [blame] | 399 | } |
| 400 | |
| 401 | /* |
danielk1977 | 6207906 | 2007-08-15 17:08:46 +0000 | [diff] [blame] | 402 | ** Read data from a crash-file. |
drh | 1a23593 | 2005-11-29 18:37:15 +0000 | [diff] [blame] | 403 | */ |
danielk1977 | 1e53695 | 2007-08-16 10:09:01 +0000 | [diff] [blame] | 404 | static int cfRead( |
| 405 | sqlite3_file *pFile, |
| 406 | void *zBuf, |
| 407 | int iAmt, |
| 408 | sqlite_int64 iOfst |
| 409 | ){ |
danielk1977 | 6207906 | 2007-08-15 17:08:46 +0000 | [diff] [blame] | 410 | CrashFile *pCrash = (CrashFile *)pFile; |
dan | 999cd08 | 2013-12-09 20:42:03 +0000 | [diff] [blame] | 411 | int nCopy = (int)MIN((i64)iAmt, (pCrash->iSize - iOfst)); |
| 412 | |
| 413 | if( nCopy>0 ){ |
| 414 | memcpy(zBuf, &pCrash->zData[iOfst], nCopy); |
| 415 | } |
danielk1977 | 6207906 | 2007-08-15 17:08:46 +0000 | [diff] [blame] | 416 | |
| 417 | /* Check the file-size to see if this is a short-read */ |
dan | 999cd08 | 2013-12-09 20:42:03 +0000 | [diff] [blame] | 418 | if( nCopy<iAmt ){ |
danielk1977 | 6207906 | 2007-08-15 17:08:46 +0000 | [diff] [blame] | 419 | return SQLITE_IOERR_SHORT_READ; |
| 420 | } |
| 421 | |
danielk1977 | 967a4a1 | 2007-08-20 14:23:44 +0000 | [diff] [blame] | 422 | return SQLITE_OK; |
drh | 1883921 | 2005-11-26 03:43:23 +0000 | [diff] [blame] | 423 | } |
| 424 | |
| 425 | /* |
danielk1977 | 6207906 | 2007-08-15 17:08:46 +0000 | [diff] [blame] | 426 | ** Write data to a crash-file. |
danielk1977 | b472117 | 2007-03-19 05:54:48 +0000 | [diff] [blame] | 427 | */ |
danielk1977 | 1e53695 | 2007-08-16 10:09:01 +0000 | [diff] [blame] | 428 | static int cfWrite( |
| 429 | sqlite3_file *pFile, |
| 430 | const void *zBuf, |
| 431 | int iAmt, |
| 432 | sqlite_int64 iOfst |
| 433 | ){ |
danielk1977 | 967a4a1 | 2007-08-20 14:23:44 +0000 | [diff] [blame] | 434 | CrashFile *pCrash = (CrashFile *)pFile; |
| 435 | if( iAmt+iOfst>pCrash->iSize ){ |
drh | 7da5fcb | 2012-03-30 14:59:43 +0000 | [diff] [blame] | 436 | pCrash->iSize = (int)(iAmt+iOfst); |
danielk1977 | 967a4a1 | 2007-08-20 14:23:44 +0000 | [diff] [blame] | 437 | } |
| 438 | while( pCrash->iSize>pCrash->nData ){ |
drh | 4a50aac | 2007-08-23 02:47:53 +0000 | [diff] [blame] | 439 | u8 *zNew; |
danielk1977 | 967a4a1 | 2007-08-20 14:23:44 +0000 | [diff] [blame] | 440 | int nNew = (pCrash->nData*2) + 4096; |
danielk1977 | a98d7b4 | 2008-01-18 13:42:54 +0000 | [diff] [blame] | 441 | zNew = crash_realloc(pCrash->zData, nNew); |
danielk1977 | 967a4a1 | 2007-08-20 14:23:44 +0000 | [diff] [blame] | 442 | if( !zNew ){ |
| 443 | return SQLITE_NOMEM; |
| 444 | } |
| 445 | memset(&zNew[pCrash->nData], 0, nNew-pCrash->nData); |
| 446 | pCrash->nData = nNew; |
| 447 | pCrash->zData = zNew; |
| 448 | } |
| 449 | memcpy(&pCrash->zData[iOfst], zBuf, iAmt); |
danielk1977 | 6207906 | 2007-08-15 17:08:46 +0000 | [diff] [blame] | 450 | return writeListAppend(pFile, iOfst, zBuf, iAmt); |
danielk1977 | b472117 | 2007-03-19 05:54:48 +0000 | [diff] [blame] | 451 | } |
| 452 | |
| 453 | /* |
danielk1977 | 6207906 | 2007-08-15 17:08:46 +0000 | [diff] [blame] | 454 | ** Truncate a crash-file. |
drh | 054889e | 2005-11-30 03:20:31 +0000 | [diff] [blame] | 455 | */ |
danielk1977 | 1e53695 | 2007-08-16 10:09:01 +0000 | [diff] [blame] | 456 | static int cfTruncate(sqlite3_file *pFile, sqlite_int64 size){ |
danielk1977 | 967a4a1 | 2007-08-20 14:23:44 +0000 | [diff] [blame] | 457 | CrashFile *pCrash = (CrashFile *)pFile; |
| 458 | assert(size>=0); |
| 459 | if( pCrash->iSize>size ){ |
drh | 7da5fcb | 2012-03-30 14:59:43 +0000 | [diff] [blame] | 460 | pCrash->iSize = (int)size; |
danielk1977 | 967a4a1 | 2007-08-20 14:23:44 +0000 | [diff] [blame] | 461 | } |
danielk1977 | 6207906 | 2007-08-15 17:08:46 +0000 | [diff] [blame] | 462 | return writeListAppend(pFile, size, 0, 0); |
| 463 | } |
| 464 | |
| 465 | /* |
| 466 | ** Sync a crash-file. |
| 467 | */ |
danielk1977 | 1e53695 | 2007-08-16 10:09:01 +0000 | [diff] [blame] | 468 | static int cfSync(sqlite3_file *pFile, int flags){ |
danielk1977 | 6207906 | 2007-08-15 17:08:46 +0000 | [diff] [blame] | 469 | CrashFile *pCrash = (CrashFile *)pFile; |
| 470 | int isCrash = 0; |
| 471 | |
danielk1977 | 967a4a1 | 2007-08-20 14:23:44 +0000 | [diff] [blame] | 472 | const char *zName = pCrash->zName; |
| 473 | const char *zCrashFile = g.zCrashFile; |
drh | 83cc139 | 2012-04-19 18:04:28 +0000 | [diff] [blame] | 474 | int nName = (int)strlen(zName); |
| 475 | int nCrashFile = (int)strlen(zCrashFile); |
danielk1977 | 967a4a1 | 2007-08-20 14:23:44 +0000 | [diff] [blame] | 476 | |
| 477 | if( nCrashFile>0 && zCrashFile[nCrashFile-1]=='*' ){ |
| 478 | nCrashFile--; |
| 479 | if( nName>nCrashFile ) nName = nCrashFile; |
| 480 | } |
| 481 | |
mistachkin | f8a7846 | 2012-03-08 20:00:36 +0000 | [diff] [blame] | 482 | #ifdef TRACE_CRASHTEST |
| 483 | printf("cfSync(): nName = %d, nCrashFile = %d, zName = %s, zCrashFile = %s\n", |
| 484 | nName, nCrashFile, zName, zCrashFile); |
| 485 | #endif |
| 486 | |
danielk1977 | 967a4a1 | 2007-08-20 14:23:44 +0000 | [diff] [blame] | 487 | if( nName==nCrashFile && 0==memcmp(zName, zCrashFile, nName) ){ |
mistachkin | f8a7846 | 2012-03-08 20:00:36 +0000 | [diff] [blame] | 488 | #ifdef TRACE_CRASHTEST |
| 489 | printf("cfSync(): name matched, g.iCrash = %d\n", g.iCrash); |
| 490 | #endif |
danielk1977 | 967a4a1 | 2007-08-20 14:23:44 +0000 | [diff] [blame] | 491 | if( (--g.iCrash)==0 ) isCrash = 1; |
danielk1977 | 6207906 | 2007-08-15 17:08:46 +0000 | [diff] [blame] | 492 | } |
| 493 | |
| 494 | return writeListSync(pCrash, isCrash); |
| 495 | } |
| 496 | |
| 497 | /* |
| 498 | ** Return the current file-size of the crash-file. |
| 499 | */ |
danielk1977 | 1e53695 | 2007-08-16 10:09:01 +0000 | [diff] [blame] | 500 | static int cfFileSize(sqlite3_file *pFile, sqlite_int64 *pSize){ |
danielk1977 | 6207906 | 2007-08-15 17:08:46 +0000 | [diff] [blame] | 501 | CrashFile *pCrash = (CrashFile *)pFile; |
danielk1977 | 967a4a1 | 2007-08-20 14:23:44 +0000 | [diff] [blame] | 502 | *pSize = (i64)pCrash->iSize; |
danielk1977 | 6207906 | 2007-08-15 17:08:46 +0000 | [diff] [blame] | 503 | return SQLITE_OK; |
| 504 | } |
| 505 | |
| 506 | /* |
| 507 | ** Calls related to file-locks are passed on to the real file handle. |
| 508 | */ |
danielk1977 | 1e53695 | 2007-08-16 10:09:01 +0000 | [diff] [blame] | 509 | static int cfLock(sqlite3_file *pFile, int eLock){ |
danielk1977 | 6207906 | 2007-08-15 17:08:46 +0000 | [diff] [blame] | 510 | return sqlite3OsLock(((CrashFile *)pFile)->pRealFile, eLock); |
| 511 | } |
danielk1977 | 1e53695 | 2007-08-16 10:09:01 +0000 | [diff] [blame] | 512 | static int cfUnlock(sqlite3_file *pFile, int eLock){ |
danielk1977 | 6207906 | 2007-08-15 17:08:46 +0000 | [diff] [blame] | 513 | return sqlite3OsUnlock(((CrashFile *)pFile)->pRealFile, eLock); |
| 514 | } |
danielk1977 | 861f745 | 2008-06-05 11:39:11 +0000 | [diff] [blame] | 515 | static int cfCheckReservedLock(sqlite3_file *pFile, int *pResOut){ |
| 516 | return sqlite3OsCheckReservedLock(((CrashFile *)pFile)->pRealFile, pResOut); |
danielk1977 | 6207906 | 2007-08-15 17:08:46 +0000 | [diff] [blame] | 517 | } |
drh | cc6bb3e | 2007-08-31 16:11:35 +0000 | [diff] [blame] | 518 | static int cfFileControl(sqlite3_file *pFile, int op, void *pArg){ |
dan | 67e10d1 | 2011-08-23 16:41:06 +0000 | [diff] [blame] | 519 | if( op==SQLITE_FCNTL_SIZE_HINT ){ |
| 520 | CrashFile *pCrash = (CrashFile *)pFile; |
| 521 | i64 nByte = *(i64 *)pArg; |
| 522 | if( nByte>pCrash->iSize ){ |
dan | 422faae | 2011-08-23 19:46:02 +0000 | [diff] [blame] | 523 | if( SQLITE_OK==writeListAppend(pFile, nByte, 0, 0) ){ |
drh | 7da5fcb | 2012-03-30 14:59:43 +0000 | [diff] [blame] | 524 | pCrash->iSize = (int)nByte; |
dan | 422faae | 2011-08-23 19:46:02 +0000 | [diff] [blame] | 525 | } |
dan | 67e10d1 | 2011-08-23 16:41:06 +0000 | [diff] [blame] | 526 | } |
dan | 422faae | 2011-08-23 19:46:02 +0000 | [diff] [blame] | 527 | return SQLITE_OK; |
dan | 67e10d1 | 2011-08-23 16:41:06 +0000 | [diff] [blame] | 528 | } |
drh | cc6bb3e | 2007-08-31 16:11:35 +0000 | [diff] [blame] | 529 | return sqlite3OsFileControl(((CrashFile *)pFile)->pRealFile, op, pArg); |
danielk1977 | 6207906 | 2007-08-15 17:08:46 +0000 | [diff] [blame] | 530 | } |
| 531 | |
| 532 | /* |
| 533 | ** The xSectorSize() and xDeviceCharacteristics() functions return |
| 534 | ** the global values configured by the [sqlite_crashparams] tcl |
| 535 | * interface. |
| 536 | */ |
danielk1977 | 1e53695 | 2007-08-16 10:09:01 +0000 | [diff] [blame] | 537 | static int cfSectorSize(sqlite3_file *pFile){ |
danielk1977 | 6207906 | 2007-08-15 17:08:46 +0000 | [diff] [blame] | 538 | return g.iSectorSize; |
| 539 | } |
danielk1977 | 1e53695 | 2007-08-16 10:09:01 +0000 | [diff] [blame] | 540 | static int cfDeviceCharacteristics(sqlite3_file *pFile){ |
danielk1977 | 6207906 | 2007-08-15 17:08:46 +0000 | [diff] [blame] | 541 | return g.iDeviceCharacteristics; |
| 542 | } |
| 543 | |
drh | d9e5c4f | 2010-05-12 18:01:39 +0000 | [diff] [blame] | 544 | /* |
| 545 | ** Pass-throughs for WAL support. |
| 546 | */ |
drh | 73b64e4 | 2010-05-30 19:55:15 +0000 | [diff] [blame] | 547 | static int cfShmLock(sqlite3_file *pFile, int ofst, int n, int flags){ |
| 548 | return sqlite3OsShmLock(((CrashFile*)pFile)->pRealFile, ofst, n, flags); |
drh | d9e5c4f | 2010-05-12 18:01:39 +0000 | [diff] [blame] | 549 | } |
drh | 286a288 | 2010-05-20 23:51:06 +0000 | [diff] [blame] | 550 | static void cfShmBarrier(sqlite3_file *pFile){ |
| 551 | sqlite3OsShmBarrier(((CrashFile*)pFile)->pRealFile); |
| 552 | } |
drh | e11fedc | 2010-07-14 00:14:30 +0000 | [diff] [blame] | 553 | static int cfShmUnmap(sqlite3_file *pFile, int delFlag){ |
| 554 | return sqlite3OsShmUnmap(((CrashFile*)pFile)->pRealFile, delFlag); |
drh | d9e5c4f | 2010-05-12 18:01:39 +0000 | [diff] [blame] | 555 | } |
dan | 1880191 | 2010-06-14 14:07:50 +0000 | [diff] [blame] | 556 | static int cfShmMap( |
dan | 067f316 | 2010-06-14 10:30:12 +0000 | [diff] [blame] | 557 | sqlite3_file *pFile, /* Handle open on database file */ |
dan | 1880191 | 2010-06-14 14:07:50 +0000 | [diff] [blame] | 558 | int iRegion, /* Region to retrieve */ |
| 559 | int sz, /* Size of regions */ |
dan | 067f316 | 2010-06-14 10:30:12 +0000 | [diff] [blame] | 560 | int w, /* True to extend file if necessary */ |
| 561 | void volatile **pp /* OUT: Mapped memory */ |
| 562 | ){ |
dan | 1880191 | 2010-06-14 14:07:50 +0000 | [diff] [blame] | 563 | return sqlite3OsShmMap(((CrashFile*)pFile)->pRealFile, iRegion, sz, w, pp); |
dan | 067f316 | 2010-06-14 10:30:12 +0000 | [diff] [blame] | 564 | } |
drh | d9e5c4f | 2010-05-12 18:01:39 +0000 | [diff] [blame] | 565 | |
danielk1977 | 6207906 | 2007-08-15 17:08:46 +0000 | [diff] [blame] | 566 | static const sqlite3_io_methods CrashFileVtab = { |
drh | d9e5c4f | 2010-05-12 18:01:39 +0000 | [diff] [blame] | 567 | 2, /* iVersion */ |
danielk1977 | 6207906 | 2007-08-15 17:08:46 +0000 | [diff] [blame] | 568 | cfClose, /* xClose */ |
| 569 | cfRead, /* xRead */ |
| 570 | cfWrite, /* xWrite */ |
| 571 | cfTruncate, /* xTruncate */ |
| 572 | cfSync, /* xSync */ |
| 573 | cfFileSize, /* xFileSize */ |
| 574 | cfLock, /* xLock */ |
| 575 | cfUnlock, /* xUnlock */ |
| 576 | cfCheckReservedLock, /* xCheckReservedLock */ |
drh | cc6bb3e | 2007-08-31 16:11:35 +0000 | [diff] [blame] | 577 | cfFileControl, /* xFileControl */ |
danielk1977 | 6207906 | 2007-08-15 17:08:46 +0000 | [diff] [blame] | 578 | cfSectorSize, /* xSectorSize */ |
drh | d9e5c4f | 2010-05-12 18:01:39 +0000 | [diff] [blame] | 579 | cfDeviceCharacteristics, /* xDeviceCharacteristics */ |
drh | 6b017cc | 2010-06-14 18:01:46 +0000 | [diff] [blame] | 580 | cfShmMap, /* xShmMap */ |
dan | da9fe0c | 2010-07-13 18:44:03 +0000 | [diff] [blame] | 581 | cfShmLock, /* xShmLock */ |
drh | 286a288 | 2010-05-20 23:51:06 +0000 | [diff] [blame] | 582 | cfShmBarrier, /* xShmBarrier */ |
drh | e11fedc | 2010-07-14 00:14:30 +0000 | [diff] [blame] | 583 | cfShmUnmap /* xShmUnmap */ |
drh | 054889e | 2005-11-30 03:20:31 +0000 | [diff] [blame] | 584 | }; |
| 585 | |
drh | 054889e | 2005-11-30 03:20:31 +0000 | [diff] [blame] | 586 | /* |
drh | d677b3d | 2007-08-20 22:48:41 +0000 | [diff] [blame] | 587 | ** Application data for the crash VFS |
| 588 | */ |
| 589 | struct crashAppData { |
danielk1977 | f1da17a | 2007-08-21 13:07:46 +0000 | [diff] [blame] | 590 | sqlite3_vfs *pOrig; /* Wrapped vfs structure */ |
drh | d677b3d | 2007-08-20 22:48:41 +0000 | [diff] [blame] | 591 | }; |
| 592 | |
| 593 | /* |
danielk1977 | 0e87b70 | 2007-08-25 12:29:30 +0000 | [diff] [blame] | 594 | ** Open a crash-file file handle. |
danielk1977 | 967a4a1 | 2007-08-20 14:23:44 +0000 | [diff] [blame] | 595 | ** |
| 596 | ** The caller will have allocated pVfs->szOsFile bytes of space |
| 597 | ** at pFile. This file uses this space for the CrashFile structure |
| 598 | ** and allocates space for the "real" file structure using |
| 599 | ** sqlite3_malloc(). The assumption here is (pVfs->szOsFile) is |
| 600 | ** equal or greater than sizeof(CrashFile). |
drh | 054889e | 2005-11-30 03:20:31 +0000 | [diff] [blame] | 601 | */ |
danielk1977 | f1da17a | 2007-08-21 13:07:46 +0000 | [diff] [blame] | 602 | static int cfOpen( |
danielk1977 | f55b899 | 2007-08-24 08:15:53 +0000 | [diff] [blame] | 603 | sqlite3_vfs *pCfVfs, |
danielk1977 | 6207906 | 2007-08-15 17:08:46 +0000 | [diff] [blame] | 604 | const char *zName, |
| 605 | sqlite3_file *pFile, |
| 606 | int flags, |
| 607 | int *pOutFlags |
| 608 | ){ |
danielk1977 | f55b899 | 2007-08-24 08:15:53 +0000 | [diff] [blame] | 609 | sqlite3_vfs *pVfs = (sqlite3_vfs *)pCfVfs->pAppData; |
danielk1977 | 967a4a1 | 2007-08-20 14:23:44 +0000 | [diff] [blame] | 610 | int rc; |
danielk1977 | f1da17a | 2007-08-21 13:07:46 +0000 | [diff] [blame] | 611 | CrashFile *pWrapper = (CrashFile *)pFile; |
drh | 4a50aac | 2007-08-23 02:47:53 +0000 | [diff] [blame] | 612 | sqlite3_file *pReal = (sqlite3_file*)&pWrapper[1]; |
danielk1977 | f1da17a | 2007-08-21 13:07:46 +0000 | [diff] [blame] | 613 | |
| 614 | memset(pWrapper, 0, sizeof(CrashFile)); |
| 615 | rc = sqlite3OsOpen(pVfs, zName, pReal, flags, pOutFlags); |
| 616 | |
| 617 | if( rc==SQLITE_OK ){ |
| 618 | i64 iSize; |
| 619 | pWrapper->pMethod = &CrashFileVtab; |
| 620 | pWrapper->zName = (char *)zName; |
| 621 | pWrapper->pRealFile = pReal; |
| 622 | rc = sqlite3OsFileSize(pReal, &iSize); |
| 623 | pWrapper->iSize = (int)iSize; |
danielk1977 | d6846d7 | 2009-02-11 14:27:04 +0000 | [diff] [blame] | 624 | pWrapper->flags = flags; |
danielk1977 | f1da17a | 2007-08-21 13:07:46 +0000 | [diff] [blame] | 625 | } |
| 626 | if( rc==SQLITE_OK ){ |
mistachkin | 2744938 | 2013-10-31 06:11:10 +0000 | [diff] [blame] | 627 | pWrapper->nData = (int)(4096 + pWrapper->iSize); |
danielk1977 | a98d7b4 | 2008-01-18 13:42:54 +0000 | [diff] [blame] | 628 | pWrapper->zData = crash_malloc(pWrapper->nData); |
danielk1977 | f1da17a | 2007-08-21 13:07:46 +0000 | [diff] [blame] | 629 | if( pWrapper->zData ){ |
danielk1977 | 2d42c2b | 2009-02-10 14:28:57 +0000 | [diff] [blame] | 630 | /* os_unix.c contains an assert() that fails if the caller attempts |
| 631 | ** to read data from the 512-byte locking region of a file opened |
| 632 | ** with the SQLITE_OPEN_MAIN_DB flag. This region of a database file |
| 633 | ** never contains valid data anyhow. So avoid doing such a read here. |
dan | ce5c42b | 2012-10-17 15:28:26 +0000 | [diff] [blame] | 634 | ** |
| 635 | ** UPDATE: It also contains an assert() verifying that each call |
| 636 | ** to the xRead() method reads less than 128KB of data. |
danielk1977 | 2d42c2b | 2009-02-10 14:28:57 +0000 | [diff] [blame] | 637 | */ |
dan | ce5c42b | 2012-10-17 15:28:26 +0000 | [diff] [blame] | 638 | i64 iOff; |
| 639 | |
danielk1977 | f1da17a | 2007-08-21 13:07:46 +0000 | [diff] [blame] | 640 | memset(pWrapper->zData, 0, pWrapper->nData); |
dan | ce5c42b | 2012-10-17 15:28:26 +0000 | [diff] [blame] | 641 | for(iOff=0; iOff<pWrapper->iSize; iOff += 512){ |
mistachkin | 2744938 | 2013-10-31 06:11:10 +0000 | [diff] [blame] | 642 | int nRead = (int)(pWrapper->iSize - iOff); |
dan | ce5c42b | 2012-10-17 15:28:26 +0000 | [diff] [blame] | 643 | if( nRead>512 ) nRead = 512; |
dan | ce5c42b | 2012-10-17 15:28:26 +0000 | [diff] [blame] | 644 | rc = sqlite3OsRead(pReal, &pWrapper->zData[iOff], nRead, iOff); |
danielk1977 | 2d42c2b | 2009-02-10 14:28:57 +0000 | [diff] [blame] | 645 | } |
danielk1977 | f1da17a | 2007-08-21 13:07:46 +0000 | [diff] [blame] | 646 | }else{ |
| 647 | rc = SQLITE_NOMEM; |
danielk1977 | 6207906 | 2007-08-15 17:08:46 +0000 | [diff] [blame] | 648 | } |
danielk1977 | f1da17a | 2007-08-21 13:07:46 +0000 | [diff] [blame] | 649 | } |
| 650 | if( rc!=SQLITE_OK && pWrapper->pMethod ){ |
| 651 | sqlite3OsClose(pFile); |
danielk1977 | 6207906 | 2007-08-15 17:08:46 +0000 | [diff] [blame] | 652 | } |
| 653 | return rc; |
| 654 | } |
| 655 | |
danielk1977 | f55b899 | 2007-08-24 08:15:53 +0000 | [diff] [blame] | 656 | static int cfDelete(sqlite3_vfs *pCfVfs, const char *zPath, int dirSync){ |
| 657 | sqlite3_vfs *pVfs = (sqlite3_vfs *)pCfVfs->pAppData; |
| 658 | return pVfs->xDelete(pVfs, zPath, dirSync); |
danielk1977 | f1da17a | 2007-08-21 13:07:46 +0000 | [diff] [blame] | 659 | } |
danielk1977 | 861f745 | 2008-06-05 11:39:11 +0000 | [diff] [blame] | 660 | static int cfAccess( |
| 661 | sqlite3_vfs *pCfVfs, |
| 662 | const char *zPath, |
| 663 | int flags, |
| 664 | int *pResOut |
| 665 | ){ |
danielk1977 | f55b899 | 2007-08-24 08:15:53 +0000 | [diff] [blame] | 666 | sqlite3_vfs *pVfs = (sqlite3_vfs *)pCfVfs->pAppData; |
danielk1977 | 861f745 | 2008-06-05 11:39:11 +0000 | [diff] [blame] | 667 | return pVfs->xAccess(pVfs, zPath, flags, pResOut); |
danielk1977 | f1da17a | 2007-08-21 13:07:46 +0000 | [diff] [blame] | 668 | } |
danielk1977 | adfb9b0 | 2007-09-17 07:02:56 +0000 | [diff] [blame] | 669 | static int cfFullPathname( |
| 670 | sqlite3_vfs *pCfVfs, |
| 671 | const char *zPath, |
| 672 | int nPathOut, |
| 673 | char *zPathOut |
| 674 | ){ |
danielk1977 | f55b899 | 2007-08-24 08:15:53 +0000 | [diff] [blame] | 675 | sqlite3_vfs *pVfs = (sqlite3_vfs *)pCfVfs->pAppData; |
danielk1977 | adfb9b0 | 2007-09-17 07:02:56 +0000 | [diff] [blame] | 676 | return pVfs->xFullPathname(pVfs, zPath, nPathOut, zPathOut); |
danielk1977 | f1da17a | 2007-08-21 13:07:46 +0000 | [diff] [blame] | 677 | } |
danielk1977 | f55b899 | 2007-08-24 08:15:53 +0000 | [diff] [blame] | 678 | static void *cfDlOpen(sqlite3_vfs *pCfVfs, const char *zPath){ |
| 679 | sqlite3_vfs *pVfs = (sqlite3_vfs *)pCfVfs->pAppData; |
| 680 | return pVfs->xDlOpen(pVfs, zPath); |
danielk1977 | f1da17a | 2007-08-21 13:07:46 +0000 | [diff] [blame] | 681 | } |
danielk1977 | 0e87b70 | 2007-08-25 12:29:30 +0000 | [diff] [blame] | 682 | static void cfDlError(sqlite3_vfs *pCfVfs, int nByte, char *zErrMsg){ |
| 683 | sqlite3_vfs *pVfs = (sqlite3_vfs *)pCfVfs->pAppData; |
| 684 | pVfs->xDlError(pVfs, nByte, zErrMsg); |
| 685 | } |
drh | ec1724e | 2008-12-09 01:32:03 +0000 | [diff] [blame] | 686 | static void (*cfDlSym(sqlite3_vfs *pCfVfs, void *pH, const char *zSym))(void){ |
danielk1977 | 0e87b70 | 2007-08-25 12:29:30 +0000 | [diff] [blame] | 687 | sqlite3_vfs *pVfs = (sqlite3_vfs *)pCfVfs->pAppData; |
drh | ec1724e | 2008-12-09 01:32:03 +0000 | [diff] [blame] | 688 | return pVfs->xDlSym(pVfs, pH, zSym); |
danielk1977 | 0e87b70 | 2007-08-25 12:29:30 +0000 | [diff] [blame] | 689 | } |
| 690 | static void cfDlClose(sqlite3_vfs *pCfVfs, void *pHandle){ |
| 691 | sqlite3_vfs *pVfs = (sqlite3_vfs *)pCfVfs->pAppData; |
| 692 | pVfs->xDlClose(pVfs, pHandle); |
| 693 | } |
danielk1977 | f55b899 | 2007-08-24 08:15:53 +0000 | [diff] [blame] | 694 | static int cfRandomness(sqlite3_vfs *pCfVfs, int nByte, char *zBufOut){ |
| 695 | sqlite3_vfs *pVfs = (sqlite3_vfs *)pCfVfs->pAppData; |
| 696 | return pVfs->xRandomness(pVfs, nByte, zBufOut); |
danielk1977 | f1da17a | 2007-08-21 13:07:46 +0000 | [diff] [blame] | 697 | } |
danielk1977 | f55b899 | 2007-08-24 08:15:53 +0000 | [diff] [blame] | 698 | static int cfSleep(sqlite3_vfs *pCfVfs, int nMicro){ |
| 699 | sqlite3_vfs *pVfs = (sqlite3_vfs *)pCfVfs->pAppData; |
| 700 | return pVfs->xSleep(pVfs, nMicro); |
danielk1977 | f1da17a | 2007-08-21 13:07:46 +0000 | [diff] [blame] | 701 | } |
danielk1977 | f55b899 | 2007-08-24 08:15:53 +0000 | [diff] [blame] | 702 | static int cfCurrentTime(sqlite3_vfs *pCfVfs, double *pTimeOut){ |
| 703 | sqlite3_vfs *pVfs = (sqlite3_vfs *)pCfVfs->pAppData; |
| 704 | return pVfs->xCurrentTime(pVfs, pTimeOut); |
danielk1977 | f1da17a | 2007-08-21 13:07:46 +0000 | [diff] [blame] | 705 | } |
dan | 05accd2 | 2016-04-27 18:54:49 +0000 | [diff] [blame] | 706 | static int cfGetLastError(sqlite3_vfs *pCfVfs, int n, char *z){ |
| 707 | sqlite3_vfs *pVfs = (sqlite3_vfs *)pCfVfs->pAppData; |
| 708 | return pVfs->xGetLastError(pVfs, n, z); |
| 709 | } |
danielk1977 | f1da17a | 2007-08-21 13:07:46 +0000 | [diff] [blame] | 710 | |
danielk1977 | 2ca0f86 | 2007-08-23 08:06:44 +0000 | [diff] [blame] | 711 | static int processDevSymArgs( |
| 712 | Tcl_Interp *interp, |
| 713 | int objc, |
| 714 | Tcl_Obj *CONST objv[], |
| 715 | int *piDeviceChar, |
| 716 | int *piSectorSize |
| 717 | ){ |
| 718 | struct DeviceFlag { |
| 719 | char *zName; |
| 720 | int iValue; |
| 721 | } aFlag[] = { |
drh | cb15f35 | 2011-12-23 01:04:17 +0000 | [diff] [blame] | 722 | { "atomic", SQLITE_IOCAP_ATOMIC }, |
| 723 | { "atomic512", SQLITE_IOCAP_ATOMIC512 }, |
| 724 | { "atomic1k", SQLITE_IOCAP_ATOMIC1K }, |
| 725 | { "atomic2k", SQLITE_IOCAP_ATOMIC2K }, |
| 726 | { "atomic4k", SQLITE_IOCAP_ATOMIC4K }, |
| 727 | { "atomic8k", SQLITE_IOCAP_ATOMIC8K }, |
| 728 | { "atomic16k", SQLITE_IOCAP_ATOMIC16K }, |
| 729 | { "atomic32k", SQLITE_IOCAP_ATOMIC32K }, |
| 730 | { "atomic64k", SQLITE_IOCAP_ATOMIC64K }, |
| 731 | { "sequential", SQLITE_IOCAP_SEQUENTIAL }, |
| 732 | { "safe_append", SQLITE_IOCAP_SAFE_APPEND }, |
| 733 | { "powersafe_overwrite", SQLITE_IOCAP_POWERSAFE_OVERWRITE }, |
danielk1977 | 2ca0f86 | 2007-08-23 08:06:44 +0000 | [diff] [blame] | 734 | { 0, 0 } |
| 735 | }; |
| 736 | |
| 737 | int i; |
| 738 | int iDc = 0; |
| 739 | int iSectorSize = 0; |
| 740 | int setSectorsize = 0; |
| 741 | int setDeviceChar = 0; |
| 742 | |
| 743 | for(i=0; i<objc; i+=2){ |
| 744 | int nOpt; |
| 745 | char *zOpt = Tcl_GetStringFromObj(objv[i], &nOpt); |
| 746 | |
| 747 | if( (nOpt>11 || nOpt<2 || strncmp("-sectorsize", zOpt, nOpt)) |
| 748 | && (nOpt>16 || nOpt<2 || strncmp("-characteristics", zOpt, nOpt)) |
| 749 | ){ |
| 750 | Tcl_AppendResult(interp, |
| 751 | "Bad option: \"", zOpt, |
| 752 | "\" - must be \"-characteristics\" or \"-sectorsize\"", 0 |
| 753 | ); |
| 754 | return TCL_ERROR; |
| 755 | } |
| 756 | if( i==objc-1 ){ |
| 757 | Tcl_AppendResult(interp, "Option requires an argument: \"", zOpt, "\"",0); |
| 758 | return TCL_ERROR; |
| 759 | } |
| 760 | |
| 761 | if( zOpt[1]=='s' ){ |
| 762 | if( Tcl_GetIntFromObj(interp, objv[i+1], &iSectorSize) ){ |
| 763 | return TCL_ERROR; |
| 764 | } |
| 765 | setSectorsize = 1; |
| 766 | }else{ |
| 767 | int j; |
| 768 | Tcl_Obj **apObj; |
| 769 | int nObj; |
| 770 | if( Tcl_ListObjGetElements(interp, objv[i+1], &nObj, &apObj) ){ |
| 771 | return TCL_ERROR; |
| 772 | } |
| 773 | for(j=0; j<nObj; j++){ |
| 774 | int rc; |
| 775 | int iChoice; |
| 776 | Tcl_Obj *pFlag = Tcl_DuplicateObj(apObj[j]); |
| 777 | Tcl_IncrRefCount(pFlag); |
| 778 | Tcl_UtfToLower(Tcl_GetString(pFlag)); |
| 779 | |
| 780 | rc = Tcl_GetIndexFromObjStruct( |
| 781 | interp, pFlag, aFlag, sizeof(aFlag[0]), "no such flag", 0, &iChoice |
| 782 | ); |
| 783 | Tcl_DecrRefCount(pFlag); |
| 784 | if( rc ){ |
| 785 | return TCL_ERROR; |
| 786 | } |
| 787 | |
| 788 | iDc |= aFlag[iChoice].iValue; |
| 789 | } |
| 790 | setDeviceChar = 1; |
| 791 | } |
| 792 | } |
| 793 | |
| 794 | if( setDeviceChar ){ |
| 795 | *piDeviceChar = iDc; |
| 796 | } |
| 797 | if( setSectorsize ){ |
| 798 | *piSectorSize = iSectorSize; |
| 799 | } |
| 800 | |
| 801 | return TCL_OK; |
| 802 | } |
| 803 | |
drh | 054889e | 2005-11-30 03:20:31 +0000 | [diff] [blame] | 804 | /* |
dan | fe91251 | 2016-05-24 16:20:51 +0000 | [diff] [blame] | 805 | ** tclcmd: sqlite3_crash_now |
| 806 | ** |
| 807 | ** Simulate a crash immediately. This function does not return |
| 808 | ** (writeListSync() calls exit(-1)). |
| 809 | */ |
| 810 | static int crashNowCmd( |
| 811 | void * clientData, |
| 812 | Tcl_Interp *interp, |
| 813 | int objc, |
| 814 | Tcl_Obj *CONST objv[] |
| 815 | ){ |
| 816 | if( objc!=1 ){ |
| 817 | Tcl_WrongNumArgs(interp, 1, objv, ""); |
| 818 | return TCL_ERROR; |
| 819 | } |
| 820 | writeListSync(0, 1); |
| 821 | assert( 0 ); |
| 822 | return TCL_OK; |
| 823 | } |
| 824 | |
| 825 | /* |
danielk1977 | ca0c897 | 2007-09-01 09:02:53 +0000 | [diff] [blame] | 826 | ** tclcmd: sqlite_crash_enable ENABLE |
| 827 | ** |
| 828 | ** Parameter ENABLE must be a boolean value. If true, then the "crash" |
| 829 | ** vfs is added to the system. If false, it is removed. |
| 830 | */ |
| 831 | static int crashEnableCmd( |
| 832 | void * clientData, |
| 833 | Tcl_Interp *interp, |
| 834 | int objc, |
| 835 | Tcl_Obj *CONST objv[] |
| 836 | ){ |
| 837 | int isEnable; |
| 838 | static sqlite3_vfs crashVfs = { |
drh | 4c846bb | 2010-05-03 16:36:55 +0000 | [diff] [blame] | 839 | 2, /* iVersion */ |
danielk1977 | ca0c897 | 2007-09-01 09:02:53 +0000 | [diff] [blame] | 840 | 0, /* szOsFile */ |
| 841 | 0, /* mxPathname */ |
| 842 | 0, /* pNext */ |
| 843 | "crash", /* zName */ |
| 844 | 0, /* pAppData */ |
| 845 | |
| 846 | cfOpen, /* xOpen */ |
| 847 | cfDelete, /* xDelete */ |
| 848 | cfAccess, /* xAccess */ |
danielk1977 | ca0c897 | 2007-09-01 09:02:53 +0000 | [diff] [blame] | 849 | cfFullPathname, /* xFullPathname */ |
| 850 | cfDlOpen, /* xDlOpen */ |
| 851 | cfDlError, /* xDlError */ |
| 852 | cfDlSym, /* xDlSym */ |
| 853 | cfDlClose, /* xDlClose */ |
| 854 | cfRandomness, /* xRandomness */ |
| 855 | cfSleep, /* xSleep */ |
drh | f2424c5 | 2010-04-26 00:04:55 +0000 | [diff] [blame] | 856 | cfCurrentTime, /* xCurrentTime */ |
dan | 05accd2 | 2016-04-27 18:54:49 +0000 | [diff] [blame] | 857 | cfGetLastError, /* xGetLastError */ |
drh | 4c846bb | 2010-05-03 16:36:55 +0000 | [diff] [blame] | 858 | 0, /* xCurrentTimeInt64 */ |
danielk1977 | ca0c897 | 2007-09-01 09:02:53 +0000 | [diff] [blame] | 859 | }; |
| 860 | |
| 861 | if( objc!=2 ){ |
| 862 | Tcl_WrongNumArgs(interp, 1, objv, "ENABLE"); |
| 863 | return TCL_ERROR; |
| 864 | } |
| 865 | |
| 866 | if( Tcl_GetBooleanFromObj(interp, objv[1], &isEnable) ){ |
| 867 | return TCL_ERROR; |
| 868 | } |
| 869 | |
| 870 | if( (isEnable && crashVfs.pAppData) || (!isEnable && !crashVfs.pAppData) ){ |
| 871 | return TCL_OK; |
| 872 | } |
| 873 | |
| 874 | if( crashVfs.pAppData==0 ){ |
| 875 | sqlite3_vfs *pOriginalVfs = sqlite3_vfs_find(0); |
| 876 | crashVfs.mxPathname = pOriginalVfs->mxPathname; |
| 877 | crashVfs.pAppData = (void *)pOriginalVfs; |
| 878 | crashVfs.szOsFile = sizeof(CrashFile) + pOriginalVfs->szOsFile; |
| 879 | sqlite3_vfs_register(&crashVfs, 0); |
| 880 | }else{ |
| 881 | crashVfs.pAppData = 0; |
| 882 | sqlite3_vfs_unregister(&crashVfs); |
| 883 | } |
| 884 | |
| 885 | return TCL_OK; |
| 886 | } |
| 887 | |
| 888 | /* |
danielk1977 | 6207906 | 2007-08-15 17:08:46 +0000 | [diff] [blame] | 889 | ** tclcmd: sqlite_crashparams ?OPTIONS? DELAY CRASHFILE |
drh | 9c06c95 | 2005-11-26 00:25:00 +0000 | [diff] [blame] | 890 | ** |
| 891 | ** This procedure implements a TCL command that enables crash testing |
| 892 | ** in testfixture. Once enabled, crash testing cannot be disabled. |
danielk1977 | 6207906 | 2007-08-15 17:08:46 +0000 | [diff] [blame] | 893 | ** |
| 894 | ** Available options are "-characteristics" and "-sectorsize". Both require |
| 895 | ** an argument. For -sectorsize, this is the simulated sector size in |
| 896 | ** bytes. For -characteristics, the argument must be a list of io-capability |
| 897 | ** flags to simulate. Valid flags are "atomic", "atomic512", "atomic1K", |
| 898 | ** "atomic2K", "atomic4K", "atomic8K", "atomic16K", "atomic32K", |
| 899 | ** "atomic64K", "sequential" and "safe_append". |
| 900 | ** |
| 901 | ** Example: |
| 902 | ** |
| 903 | ** sqlite_crashparams -sect 1024 -char {atomic sequential} ./test.db 1 |
| 904 | ** |
drh | 9c06c95 | 2005-11-26 00:25:00 +0000 | [diff] [blame] | 905 | */ |
| 906 | static int crashParamsObjCmd( |
| 907 | void * clientData, |
| 908 | Tcl_Interp *interp, |
| 909 | int objc, |
| 910 | Tcl_Obj *CONST objv[] |
| 911 | ){ |
danielk1977 | 6207906 | 2007-08-15 17:08:46 +0000 | [diff] [blame] | 912 | int iDelay; |
| 913 | const char *zCrashFile; |
drh | 153c62c | 2007-08-24 03:51:33 +0000 | [diff] [blame] | 914 | int nCrashFile, iDc, iSectorSize; |
drh | d677b3d | 2007-08-20 22:48:41 +0000 | [diff] [blame] | 915 | |
drh | 153c62c | 2007-08-24 03:51:33 +0000 | [diff] [blame] | 916 | iDc = -1; |
| 917 | iSectorSize = -1; |
danielk1977 | 6207906 | 2007-08-15 17:08:46 +0000 | [diff] [blame] | 918 | |
danielk1977 | 6207906 | 2007-08-15 17:08:46 +0000 | [diff] [blame] | 919 | if( objc<3 ){ |
| 920 | Tcl_WrongNumArgs(interp, 1, objv, "?OPTIONS? DELAY CRASHFILE"); |
danielk1977 | b4b4741 | 2007-08-17 15:53:36 +0000 | [diff] [blame] | 921 | goto error; |
drh | 9c06c95 | 2005-11-26 00:25:00 +0000 | [diff] [blame] | 922 | } |
danielk1977 | 6207906 | 2007-08-15 17:08:46 +0000 | [diff] [blame] | 923 | |
danielk1977 | 967a4a1 | 2007-08-20 14:23:44 +0000 | [diff] [blame] | 924 | zCrashFile = Tcl_GetStringFromObj(objv[objc-1], &nCrashFile); |
danielk1977 | 6207906 | 2007-08-15 17:08:46 +0000 | [diff] [blame] | 925 | if( nCrashFile>=sizeof(g.zCrashFile) ){ |
| 926 | Tcl_AppendResult(interp, "Filename is too long: \"", zCrashFile, "\"", 0); |
danielk1977 | b4b4741 | 2007-08-17 15:53:36 +0000 | [diff] [blame] | 927 | goto error; |
drh | 9c06c95 | 2005-11-26 00:25:00 +0000 | [diff] [blame] | 928 | } |
danielk1977 | 6207906 | 2007-08-15 17:08:46 +0000 | [diff] [blame] | 929 | if( Tcl_GetIntFromObj(interp, objv[objc-2], &iDelay) ){ |
danielk1977 | b4b4741 | 2007-08-17 15:53:36 +0000 | [diff] [blame] | 930 | goto error; |
danielk1977 | 6207906 | 2007-08-15 17:08:46 +0000 | [diff] [blame] | 931 | } |
| 932 | |
danielk1977 | 2ca0f86 | 2007-08-23 08:06:44 +0000 | [diff] [blame] | 933 | if( processDevSymArgs(interp, objc-3, &objv[1], &iDc, &iSectorSize) ){ |
| 934 | return TCL_ERROR; |
danielk1977 | 59a33f9 | 2007-03-17 10:26:59 +0000 | [diff] [blame] | 935 | } |
danielk1977 | 6207906 | 2007-08-15 17:08:46 +0000 | [diff] [blame] | 936 | |
danielk1977 | 2ca0f86 | 2007-08-23 08:06:44 +0000 | [diff] [blame] | 937 | if( iDc>=0 ){ |
danielk1977 | 6207906 | 2007-08-15 17:08:46 +0000 | [diff] [blame] | 938 | g.iDeviceCharacteristics = iDc; |
| 939 | } |
danielk1977 | 2ca0f86 | 2007-08-23 08:06:44 +0000 | [diff] [blame] | 940 | if( iSectorSize>=0 ){ |
danielk1977 | 6207906 | 2007-08-15 17:08:46 +0000 | [diff] [blame] | 941 | g.iSectorSize = iSectorSize; |
| 942 | } |
danielk1977 | 2ca0f86 | 2007-08-23 08:06:44 +0000 | [diff] [blame] | 943 | |
danielk1977 | 6207906 | 2007-08-15 17:08:46 +0000 | [diff] [blame] | 944 | g.iCrash = iDelay; |
| 945 | memcpy(g.zCrashFile, zCrashFile, nCrashFile+1); |
drh | 66560ad | 2006-01-06 14:32:19 +0000 | [diff] [blame] | 946 | sqlite3CrashTestEnable = 1; |
drh | 9c06c95 | 2005-11-26 00:25:00 +0000 | [diff] [blame] | 947 | return TCL_OK; |
danielk1977 | b4b4741 | 2007-08-17 15:53:36 +0000 | [diff] [blame] | 948 | |
| 949 | error: |
danielk1977 | b4b4741 | 2007-08-17 15:53:36 +0000 | [diff] [blame] | 950 | return TCL_ERROR; |
drh | 9c06c95 | 2005-11-26 00:25:00 +0000 | [diff] [blame] | 951 | } |
| 952 | |
danielk1977 | 2ca0f86 | 2007-08-23 08:06:44 +0000 | [diff] [blame] | 953 | static int devSymObjCmd( |
| 954 | void * clientData, |
| 955 | Tcl_Interp *interp, |
| 956 | int objc, |
| 957 | Tcl_Obj *CONST objv[] |
| 958 | ){ |
danielk1977 | bf26097 | 2008-01-22 11:50:13 +0000 | [diff] [blame] | 959 | void devsym_register(int iDeviceChar, int iSectorSize); |
danielk1977 | 2ca0f86 | 2007-08-23 08:06:44 +0000 | [diff] [blame] | 960 | |
| 961 | int iDc = -1; |
| 962 | int iSectorSize = -1; |
danielk1977 | bf26097 | 2008-01-22 11:50:13 +0000 | [diff] [blame] | 963 | |
danielk1977 | 2ca0f86 | 2007-08-23 08:06:44 +0000 | [diff] [blame] | 964 | if( processDevSymArgs(interp, objc-1, &objv[1], &iDc, &iSectorSize) ){ |
| 965 | return TCL_ERROR; |
| 966 | } |
danielk1977 | bf26097 | 2008-01-22 11:50:13 +0000 | [diff] [blame] | 967 | devsym_register(iDc, iSectorSize); |
danielk1977 | 2ca0f86 | 2007-08-23 08:06:44 +0000 | [diff] [blame] | 968 | |
| 969 | return TCL_OK; |
dan | 05accd2 | 2016-04-27 18:54:49 +0000 | [diff] [blame] | 970 | |
| 971 | } |
| 972 | |
| 973 | /* |
| 974 | ** tclcmd: unregister_devsim |
| 975 | */ |
| 976 | static int dsUnregisterObjCmd( |
| 977 | void * clientData, |
| 978 | Tcl_Interp *interp, |
| 979 | int objc, |
| 980 | Tcl_Obj *CONST objv[] |
| 981 | ){ |
| 982 | void devsym_unregister(void); |
| 983 | |
| 984 | if( objc!=1 ){ |
| 985 | Tcl_WrongNumArgs(interp, 1, objv, ""); |
| 986 | return TCL_ERROR; |
| 987 | } |
| 988 | |
| 989 | devsym_unregister(); |
| 990 | return TCL_OK; |
danielk1977 | 2ca0f86 | 2007-08-23 08:06:44 +0000 | [diff] [blame] | 991 | } |
| 992 | |
danielk1977 | a0fc729 | 2008-12-20 18:33:59 +0000 | [diff] [blame] | 993 | /* |
| 994 | ** tclcmd: register_jt_vfs ?-default? PARENT-VFS |
| 995 | */ |
| 996 | static int jtObjCmd( |
| 997 | void * clientData, |
| 998 | Tcl_Interp *interp, |
| 999 | int objc, |
| 1000 | Tcl_Obj *CONST objv[] |
| 1001 | ){ |
| 1002 | int jt_register(char *, int); |
| 1003 | char *zParent = 0; |
| 1004 | |
| 1005 | if( objc!=2 && objc!=3 ){ |
| 1006 | Tcl_WrongNumArgs(interp, 1, objv, "?-default? PARENT-VFS"); |
| 1007 | return TCL_ERROR; |
| 1008 | } |
| 1009 | zParent = Tcl_GetString(objv[1]); |
| 1010 | if( objc==3 ){ |
| 1011 | if( strcmp(zParent, "-default") ){ |
| 1012 | Tcl_AppendResult(interp, |
| 1013 | "bad option \"", zParent, "\": must be -default", 0 |
| 1014 | ); |
| 1015 | return TCL_ERROR; |
| 1016 | } |
| 1017 | zParent = Tcl_GetString(objv[2]); |
| 1018 | } |
| 1019 | |
| 1020 | if( !(*zParent) ){ |
| 1021 | zParent = 0; |
| 1022 | } |
| 1023 | if( jt_register(zParent, objc==3) ){ |
| 1024 | Tcl_AppendResult(interp, "Error in jt_register", 0); |
| 1025 | return TCL_ERROR; |
| 1026 | } |
| 1027 | |
| 1028 | return TCL_OK; |
| 1029 | } |
| 1030 | |
| 1031 | /* |
| 1032 | ** tclcmd: unregister_jt_vfs |
| 1033 | */ |
| 1034 | static int jtUnregisterObjCmd( |
| 1035 | void * clientData, |
| 1036 | Tcl_Interp *interp, |
| 1037 | int objc, |
| 1038 | Tcl_Obj *CONST objv[] |
| 1039 | ){ |
| 1040 | void jt_unregister(void); |
| 1041 | |
| 1042 | if( objc!=1 ){ |
| 1043 | Tcl_WrongNumArgs(interp, 1, objv, ""); |
| 1044 | return TCL_ERROR; |
| 1045 | } |
| 1046 | |
| 1047 | jt_unregister(); |
| 1048 | return TCL_OK; |
| 1049 | } |
| 1050 | |
drh | 198bf39 | 2006-01-06 21:52:49 +0000 | [diff] [blame] | 1051 | #endif /* SQLITE_OMIT_DISKIO */ |
| 1052 | |
drh | 9c06c95 | 2005-11-26 00:25:00 +0000 | [diff] [blame] | 1053 | /* |
| 1054 | ** This procedure registers the TCL procedures defined in this file. |
| 1055 | */ |
| 1056 | int Sqlitetest6_Init(Tcl_Interp *interp){ |
drh | 198bf39 | 2006-01-06 21:52:49 +0000 | [diff] [blame] | 1057 | #ifndef SQLITE_OMIT_DISKIO |
danielk1977 | ca0c897 | 2007-09-01 09:02:53 +0000 | [diff] [blame] | 1058 | Tcl_CreateObjCommand(interp, "sqlite3_crash_enable", crashEnableCmd, 0, 0); |
drh | 9c06c95 | 2005-11-26 00:25:00 +0000 | [diff] [blame] | 1059 | Tcl_CreateObjCommand(interp, "sqlite3_crashparams", crashParamsObjCmd, 0, 0); |
dan | fe91251 | 2016-05-24 16:20:51 +0000 | [diff] [blame] | 1060 | Tcl_CreateObjCommand(interp, "sqlite3_crash_now", crashNowCmd, 0, 0); |
danielk1977 | 2ca0f86 | 2007-08-23 08:06:44 +0000 | [diff] [blame] | 1061 | Tcl_CreateObjCommand(interp, "sqlite3_simulate_device", devSymObjCmd, 0, 0); |
dan | 05accd2 | 2016-04-27 18:54:49 +0000 | [diff] [blame] | 1062 | Tcl_CreateObjCommand(interp, "unregister_devsim", dsUnregisterObjCmd, 0, 0); |
danielk1977 | a0fc729 | 2008-12-20 18:33:59 +0000 | [diff] [blame] | 1063 | Tcl_CreateObjCommand(interp, "register_jt_vfs", jtObjCmd, 0, 0); |
| 1064 | Tcl_CreateObjCommand(interp, "unregister_jt_vfs", jtUnregisterObjCmd, 0, 0); |
drh | 198bf39 | 2006-01-06 21:52:49 +0000 | [diff] [blame] | 1065 | #endif |
drh | 9c06c95 | 2005-11-26 00:25:00 +0000 | [diff] [blame] | 1066 | return TCL_OK; |
| 1067 | } |
| 1068 | |
| 1069 | #endif /* SQLITE_TEST */ |