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