drh | 9c06c95 | 2005-11-26 00:25:00 +0000 | [diff] [blame] | 1 | /* |
| 2 | ** 2004 May 22 |
| 3 | ** |
| 4 | ** The author disclaims copyright to this source code. In place of |
| 5 | ** a legal notice, here is a blessing: |
| 6 | ** |
| 7 | ** May you do good and not evil. |
| 8 | ** May you find forgiveness for yourself and forgive others. |
| 9 | ** May you share freely, never taking more than you give. |
| 10 | ** |
| 11 | ****************************************************************************** |
| 12 | ** |
| 13 | ** This file contains code that modified the OS layer in order to simulate |
| 14 | ** the effect on the database file of an OS crash or power failure. This |
| 15 | ** is used to test the ability of SQLite to recover from those situations. |
| 16 | */ |
| 17 | #if SQLITE_TEST /* This file is used for the testing only */ |
| 18 | #include "sqliteInt.h" |
| 19 | #include "os.h" |
| 20 | #include "tcl.h" |
| 21 | |
| 22 | /* |
drh | 054889e | 2005-11-30 03:20:31 +0000 | [diff] [blame^] | 23 | ** A copy of the original sqlite3Os structure |
drh | 9c06c95 | 2005-11-26 00:25:00 +0000 | [diff] [blame] | 24 | */ |
drh | 054889e | 2005-11-30 03:20:31 +0000 | [diff] [blame^] | 25 | static struct sqlite3OsVtbl origOs; |
drh | 9c06c95 | 2005-11-26 00:25:00 +0000 | [diff] [blame] | 26 | |
| 27 | /* |
drh | 054889e | 2005-11-30 03:20:31 +0000 | [diff] [blame^] | 28 | ** crashFile is a subclass of OsFile that is taylored for the |
| 29 | ** crash test module. |
drh | 9c06c95 | 2005-11-26 00:25:00 +0000 | [diff] [blame] | 30 | */ |
drh | 054889e | 2005-11-30 03:20:31 +0000 | [diff] [blame^] | 31 | typedef struct crashFile crashFile; |
| 32 | struct crashFile { |
| 33 | IoMethod const *pMethod; /* Must be first */ |
| 34 | u8 **apBlk; /* Array of blocks that have been written to. */ |
| 35 | int nBlk; /* Size of apBlock. */ |
| 36 | i64 offset; /* Next character to be read from the file */ |
| 37 | int nMaxWrite; /* Largest offset written to. */ |
| 38 | char *zName; /* File name */ |
| 39 | OsFile *pBase; /* The real file */ |
| 40 | crashFile *pNext; /* Next in a list of them all */ |
drh | 9c06c95 | 2005-11-26 00:25:00 +0000 | [diff] [blame] | 41 | }; |
| 42 | |
| 43 | /* |
| 44 | ** Size of a simulated disk block |
| 45 | */ |
| 46 | #define BLOCKSIZE 512 |
| 47 | #define BLOCK_OFFSET(x) ((x) * BLOCKSIZE) |
| 48 | |
| 49 | |
| 50 | /* |
| 51 | ** The following variables control when a simulated crash occurs. |
| 52 | ** |
| 53 | ** If iCrashDelay is non-zero, then zCrashFile contains (full path) name of |
| 54 | ** a file that SQLite will call sqlite3OsSync() on. Each time this happens |
| 55 | ** iCrashDelay is decremented. If iCrashDelay is zero after being |
| 56 | ** decremented, a "crash" occurs during the sync() operation. |
| 57 | ** |
| 58 | ** In other words, a crash occurs the iCrashDelay'th time zCrashFile is |
| 59 | ** synced. |
| 60 | */ |
| 61 | static int iCrashDelay = 0; |
| 62 | static char zCrashFile[500]; |
| 63 | |
| 64 | /* |
| 65 | ** Set the value of the two crash parameters. |
| 66 | */ |
| 67 | static void setCrashParams(int iDelay, char const *zFile){ |
drh | 054889e | 2005-11-30 03:20:31 +0000 | [diff] [blame^] | 68 | sqlite3Os.xEnterMutex(); |
drh | 9c06c95 | 2005-11-26 00:25:00 +0000 | [diff] [blame] | 69 | assert( strlen(zFile)<sizeof(zCrashFile) ); |
| 70 | strcpy(zCrashFile, zFile); |
| 71 | iCrashDelay = iDelay; |
drh | 054889e | 2005-11-30 03:20:31 +0000 | [diff] [blame^] | 72 | sqlite3Os.xLeaveMutex(); |
drh | 9c06c95 | 2005-11-26 00:25:00 +0000 | [diff] [blame] | 73 | } |
| 74 | |
| 75 | /* |
| 76 | ** File zPath is being sync()ed. Return non-zero if this should |
| 77 | ** cause a crash. |
| 78 | */ |
| 79 | static int crashRequired(char const *zPath){ |
| 80 | int r; |
| 81 | int n; |
drh | 054889e | 2005-11-30 03:20:31 +0000 | [diff] [blame^] | 82 | sqlite3Os.xEnterMutex(); |
drh | 9c06c95 | 2005-11-26 00:25:00 +0000 | [diff] [blame] | 83 | n = strlen(zCrashFile); |
| 84 | if( zCrashFile[n-1]=='*' ){ |
| 85 | n--; |
| 86 | }else if( strlen(zPath)>n ){ |
| 87 | n = strlen(zPath); |
| 88 | } |
| 89 | r = 0; |
| 90 | if( iCrashDelay>0 && strncmp(zPath, zCrashFile, n)==0 ){ |
| 91 | iCrashDelay--; |
| 92 | if( iCrashDelay<=0 ){ |
| 93 | r = 1; |
| 94 | } |
| 95 | } |
drh | 054889e | 2005-11-30 03:20:31 +0000 | [diff] [blame^] | 96 | sqlite3Os.xLeaveMutex(); |
drh | 9c06c95 | 2005-11-26 00:25:00 +0000 | [diff] [blame] | 97 | return r; |
| 98 | } |
| 99 | |
| 100 | /* |
| 101 | ** A list of all open files. |
| 102 | */ |
drh | 054889e | 2005-11-30 03:20:31 +0000 | [diff] [blame^] | 103 | static crashFile *pAllFiles = 0; |
drh | 9c06c95 | 2005-11-26 00:25:00 +0000 | [diff] [blame] | 104 | |
drh | 054889e | 2005-11-30 03:20:31 +0000 | [diff] [blame^] | 105 | /* Forward reference */ |
| 106 | static void initFile(OsFile **pId, char const *zName, OsFile *pBase); |
drh | 9c06c95 | 2005-11-26 00:25:00 +0000 | [diff] [blame] | 107 | |
| 108 | /* |
drh | 1a23593 | 2005-11-29 18:37:15 +0000 | [diff] [blame] | 109 | ** Undo the work done by initFile. Delete the OsFile structure |
drh | 9c06c95 | 2005-11-26 00:25:00 +0000 | [diff] [blame] | 110 | ** and unlink the structure from the pAllFiles list. |
| 111 | */ |
drh | 054889e | 2005-11-30 03:20:31 +0000 | [diff] [blame^] | 112 | static void closeFile(crashFile **pId){ |
| 113 | crashFile *pFile = *pId; |
drh | 9c06c95 | 2005-11-26 00:25:00 +0000 | [diff] [blame] | 114 | if( pFile==pAllFiles ){ |
| 115 | pAllFiles = pFile->pNext; |
| 116 | }else{ |
drh | 054889e | 2005-11-30 03:20:31 +0000 | [diff] [blame^] | 117 | crashFile *p; |
drh | 9c06c95 | 2005-11-26 00:25:00 +0000 | [diff] [blame] | 118 | for(p=pAllFiles; p->pNext!=pFile; p=p->pNext ){ |
| 119 | assert( p ); |
| 120 | } |
| 121 | p->pNext = pFile->pNext; |
| 122 | } |
drh | 1a23593 | 2005-11-29 18:37:15 +0000 | [diff] [blame] | 123 | sqliteFree(*pId); |
| 124 | *pId = 0; |
drh | 9c06c95 | 2005-11-26 00:25:00 +0000 | [diff] [blame] | 125 | } |
| 126 | |
| 127 | /* |
drh | 1a23593 | 2005-11-29 18:37:15 +0000 | [diff] [blame] | 128 | ** Read block 'blk' off of the real disk file and into the cache of pFile. |
drh | 9c06c95 | 2005-11-26 00:25:00 +0000 | [diff] [blame] | 129 | */ |
drh | 054889e | 2005-11-30 03:20:31 +0000 | [diff] [blame^] | 130 | static int readBlockIntoCache(crashFile *pFile, int blk){ |
drh | 9c06c95 | 2005-11-26 00:25:00 +0000 | [diff] [blame] | 131 | if( blk>=pFile->nBlk ){ |
| 132 | int n = ((pFile->nBlk * 2) + 100 + blk); |
| 133 | /* if( pFile->nBlk==0 ){ printf("DIRTY %s\n", pFile->zName); } */ |
| 134 | pFile->apBlk = (u8 **)sqliteRealloc(pFile->apBlk, n * sizeof(u8*)); |
| 135 | if( !pFile->apBlk ) return SQLITE_NOMEM; |
| 136 | memset(&pFile->apBlk[pFile->nBlk], 0, (n - pFile->nBlk)*sizeof(u8*)); |
| 137 | pFile->nBlk = n; |
| 138 | } |
| 139 | |
| 140 | if( !pFile->apBlk[blk] ){ |
| 141 | i64 filesize; |
| 142 | int rc; |
| 143 | |
| 144 | u8 *p = sqliteMalloc(BLOCKSIZE); |
| 145 | if( !p ) return SQLITE_NOMEM; |
| 146 | pFile->apBlk[blk] = p; |
| 147 | |
drh | 054889e | 2005-11-30 03:20:31 +0000 | [diff] [blame^] | 148 | rc = sqlite3OsFileSize(pFile->pBase, &filesize); |
drh | 9c06c95 | 2005-11-26 00:25:00 +0000 | [diff] [blame] | 149 | if( rc!=SQLITE_OK ) return rc; |
| 150 | |
| 151 | if( BLOCK_OFFSET(blk)<filesize ){ |
| 152 | int len = BLOCKSIZE; |
drh | 054889e | 2005-11-30 03:20:31 +0000 | [diff] [blame^] | 153 | rc = sqlite3OsSeek(pFile->pBase, blk*BLOCKSIZE); |
drh | 9c06c95 | 2005-11-26 00:25:00 +0000 | [diff] [blame] | 154 | if( BLOCK_OFFSET(blk+1)>filesize ){ |
| 155 | len = filesize - BLOCK_OFFSET(blk); |
| 156 | } |
| 157 | if( rc!=SQLITE_OK ) return rc; |
drh | 054889e | 2005-11-30 03:20:31 +0000 | [diff] [blame^] | 158 | rc = sqlite3OsRead(pFile->pBase, p, len); |
drh | 9c06c95 | 2005-11-26 00:25:00 +0000 | [diff] [blame] | 159 | if( rc!=SQLITE_OK ) return rc; |
| 160 | } |
| 161 | } |
| 162 | |
| 163 | return SQLITE_OK; |
| 164 | } |
| 165 | |
| 166 | /* |
| 167 | ** Write the cache of pFile to disk. If crash is non-zero, randomly |
| 168 | ** skip blocks when writing. The cache is deleted before returning. |
| 169 | */ |
drh | 054889e | 2005-11-30 03:20:31 +0000 | [diff] [blame^] | 170 | static int writeCache2(crashFile *pFile, int crash){ |
drh | 9c06c95 | 2005-11-26 00:25:00 +0000 | [diff] [blame] | 171 | int i; |
| 172 | int nMax = pFile->nMaxWrite; |
drh | 9c06c95 | 2005-11-26 00:25:00 +0000 | [diff] [blame] | 173 | int rc = SQLITE_OK; |
| 174 | |
drh | 9c06c95 | 2005-11-26 00:25:00 +0000 | [diff] [blame] | 175 | for(i=0; i<pFile->nBlk; i++){ |
| 176 | u8 *p = pFile->apBlk[i]; |
| 177 | if( p ){ |
| 178 | int skip = 0; |
| 179 | int trash = 0; |
| 180 | if( crash ){ |
| 181 | char random; |
| 182 | sqlite3Randomness(1, &random); |
| 183 | if( random & 0x01 ){ |
| 184 | if( random & 0x02 ){ |
| 185 | trash = 1; |
| 186 | #ifdef TRACE_WRITECACHE |
| 187 | printf("Trashing block %d of %s\n", i, pFile->zName); |
| 188 | #endif |
| 189 | }else{ |
| 190 | skip = 1; |
| 191 | #ifdef TRACE_WRITECACHE |
| 192 | printf("Skiping block %d of %s\n", i, pFile->zName); |
| 193 | #endif |
| 194 | } |
| 195 | }else{ |
| 196 | #ifdef TRACE_WRITECACHE |
| 197 | printf("Writing block %d of %s\n", i, pFile->zName); |
| 198 | #endif |
| 199 | } |
| 200 | } |
| 201 | if( rc==SQLITE_OK ){ |
drh | 054889e | 2005-11-30 03:20:31 +0000 | [diff] [blame^] | 202 | rc = sqlite3OsSeek(pFile->pBase, BLOCK_OFFSET(i)); |
drh | 9c06c95 | 2005-11-26 00:25:00 +0000 | [diff] [blame] | 203 | } |
| 204 | if( rc==SQLITE_OK && !skip ){ |
| 205 | int len = BLOCKSIZE; |
| 206 | if( BLOCK_OFFSET(i+1)>nMax ){ |
| 207 | len = nMax-BLOCK_OFFSET(i); |
| 208 | } |
| 209 | if( len>0 ){ |
| 210 | if( trash ){ |
| 211 | sqlite3Randomness(len, p); |
| 212 | } |
drh | 054889e | 2005-11-30 03:20:31 +0000 | [diff] [blame^] | 213 | rc = sqlite3OsWrite(pFile->pBase, p, len); |
drh | 9c06c95 | 2005-11-26 00:25:00 +0000 | [diff] [blame] | 214 | } |
| 215 | } |
| 216 | sqliteFree(p); |
| 217 | } |
| 218 | } |
| 219 | sqliteFree(pFile->apBlk); |
| 220 | pFile->nBlk = 0; |
| 221 | pFile->apBlk = 0; |
| 222 | pFile->nMaxWrite = 0; |
drh | 9c06c95 | 2005-11-26 00:25:00 +0000 | [diff] [blame] | 223 | return rc; |
| 224 | } |
| 225 | |
| 226 | /* |
| 227 | ** Write the cache to disk. |
| 228 | */ |
drh | 054889e | 2005-11-30 03:20:31 +0000 | [diff] [blame^] | 229 | static int writeCache(crashFile *pFile){ |
drh | 9c06c95 | 2005-11-26 00:25:00 +0000 | [diff] [blame] | 230 | if( pFile->apBlk ){ |
| 231 | int c = crashRequired(pFile->zName); |
| 232 | if( c ){ |
drh | 054889e | 2005-11-30 03:20:31 +0000 | [diff] [blame^] | 233 | crashFile *p; |
drh | 9c06c95 | 2005-11-26 00:25:00 +0000 | [diff] [blame] | 234 | #ifdef TRACE_WRITECACHE |
| 235 | printf("\nCrash during sync of %s\n", pFile->zName); |
| 236 | #endif |
| 237 | for(p=pAllFiles; p; p=p->pNext){ |
| 238 | writeCache2(p, 1); |
| 239 | } |
| 240 | exit(-1); |
| 241 | }else{ |
| 242 | return writeCache2(pFile, 0); |
| 243 | } |
| 244 | } |
| 245 | return SQLITE_OK; |
| 246 | } |
| 247 | |
| 248 | /* |
| 249 | ** Close the file. |
| 250 | */ |
drh | 1a23593 | 2005-11-29 18:37:15 +0000 | [diff] [blame] | 251 | static int crashClose(OsFile **pId){ |
drh | 054889e | 2005-11-30 03:20:31 +0000 | [diff] [blame^] | 252 | crashFile *pFile = (crashFile*)*pId; |
drh | 1a23593 | 2005-11-29 18:37:15 +0000 | [diff] [blame] | 253 | if( pFile ){ |
| 254 | /* printf("CLOSE %s (%d blocks)\n", pFile->zName, pFile->nBlk); */ |
| 255 | writeCache(pFile); |
drh | 054889e | 2005-11-30 03:20:31 +0000 | [diff] [blame^] | 256 | sqlite3OsClose(&pFile->pBase); |
drh | 9c06c95 | 2005-11-26 00:25:00 +0000 | [diff] [blame] | 257 | } |
drh | 054889e | 2005-11-30 03:20:31 +0000 | [diff] [blame^] | 258 | closeFile(&pFile); |
| 259 | *pId = 0; |
drh | 1a23593 | 2005-11-29 18:37:15 +0000 | [diff] [blame] | 260 | return SQLITE_OK; |
| 261 | } |
| 262 | |
| 263 | static int crashSeek(OsFile *id, i64 offset){ |
drh | 054889e | 2005-11-30 03:20:31 +0000 | [diff] [blame^] | 264 | ((crashFile*)id)->offset = offset; |
drh | 9c06c95 | 2005-11-26 00:25:00 +0000 | [diff] [blame] | 265 | return SQLITE_OK; |
| 266 | } |
| 267 | |
| 268 | static int crashRead(OsFile *id, void *pBuf, int amt){ |
| 269 | i64 offset; /* The current offset from the start of the file */ |
| 270 | i64 end; /* The byte just past the last byte read */ |
| 271 | int blk; /* Block number the read starts on */ |
| 272 | int i; |
| 273 | u8 *zCsr; |
| 274 | int rc = SQLITE_OK; |
drh | 054889e | 2005-11-30 03:20:31 +0000 | [diff] [blame^] | 275 | crashFile *pFile = (crashFile*)id; |
drh | 9c06c95 | 2005-11-26 00:25:00 +0000 | [diff] [blame] | 276 | |
drh | 1a23593 | 2005-11-29 18:37:15 +0000 | [diff] [blame] | 277 | offset = pFile->offset; |
drh | 9c06c95 | 2005-11-26 00:25:00 +0000 | [diff] [blame] | 278 | end = offset+amt; |
| 279 | blk = (offset/BLOCKSIZE); |
| 280 | |
| 281 | zCsr = (u8 *)pBuf; |
| 282 | for(i=blk; i*BLOCKSIZE<end; i++){ |
| 283 | int off = 0; |
| 284 | int len = 0; |
| 285 | |
| 286 | |
| 287 | if( BLOCK_OFFSET(i) < offset ){ |
| 288 | off = offset-BLOCK_OFFSET(i); |
| 289 | } |
| 290 | len = BLOCKSIZE - off; |
| 291 | if( BLOCK_OFFSET(i+1) > end ){ |
| 292 | len = len - (BLOCK_OFFSET(i+1)-end); |
| 293 | } |
| 294 | |
| 295 | if( i<pFile->nBlk && pFile->apBlk[i]){ |
| 296 | u8 *pBlk = pFile->apBlk[i]; |
| 297 | memcpy(zCsr, &pBlk[off], len); |
| 298 | }else{ |
drh | 054889e | 2005-11-30 03:20:31 +0000 | [diff] [blame^] | 299 | rc = sqlite3OsSeek(pFile->pBase, BLOCK_OFFSET(i) + off); |
drh | 9c06c95 | 2005-11-26 00:25:00 +0000 | [diff] [blame] | 300 | if( rc!=SQLITE_OK ) return rc; |
drh | 054889e | 2005-11-30 03:20:31 +0000 | [diff] [blame^] | 301 | rc = sqlite3OsRead(pFile->pBase, zCsr, len); |
drh | 9c06c95 | 2005-11-26 00:25:00 +0000 | [diff] [blame] | 302 | if( rc!=SQLITE_OK ) return rc; |
| 303 | } |
| 304 | |
| 305 | zCsr += len; |
| 306 | } |
| 307 | assert( zCsr==&((u8 *)pBuf)[amt] ); |
| 308 | |
drh | 054889e | 2005-11-30 03:20:31 +0000 | [diff] [blame^] | 309 | pFile->offset = end; |
drh | 9c06c95 | 2005-11-26 00:25:00 +0000 | [diff] [blame] | 310 | return rc; |
| 311 | } |
| 312 | |
| 313 | static int crashWrite(OsFile *id, const void *pBuf, int amt){ |
| 314 | i64 offset; /* The current offset from the start of the file */ |
| 315 | i64 end; /* The byte just past the last byte written */ |
| 316 | int blk; /* Block number the write starts on */ |
| 317 | int i; |
| 318 | const u8 *zCsr; |
| 319 | int rc = SQLITE_OK; |
drh | 054889e | 2005-11-30 03:20:31 +0000 | [diff] [blame^] | 320 | crashFile *pFile = (crashFile*)id; |
drh | 9c06c95 | 2005-11-26 00:25:00 +0000 | [diff] [blame] | 321 | |
drh | 054889e | 2005-11-30 03:20:31 +0000 | [diff] [blame^] | 322 | offset = pFile->offset; |
drh | 9c06c95 | 2005-11-26 00:25:00 +0000 | [diff] [blame] | 323 | end = offset+amt; |
| 324 | blk = (offset/BLOCKSIZE); |
| 325 | |
| 326 | zCsr = (u8 *)pBuf; |
| 327 | for(i=blk; i*BLOCKSIZE<end; i++){ |
| 328 | u8 *pBlk; |
| 329 | int off = 0; |
| 330 | int len = 0; |
| 331 | |
| 332 | /* Make sure the block is in the cache */ |
drh | 054889e | 2005-11-30 03:20:31 +0000 | [diff] [blame^] | 333 | rc = readBlockIntoCache(pFile, i); |
drh | 9c06c95 | 2005-11-26 00:25:00 +0000 | [diff] [blame] | 334 | if( rc!=SQLITE_OK ) return rc; |
| 335 | |
| 336 | /* Write into the cache */ |
drh | 054889e | 2005-11-30 03:20:31 +0000 | [diff] [blame^] | 337 | pBlk = pFile->apBlk[i]; |
drh | 9c06c95 | 2005-11-26 00:25:00 +0000 | [diff] [blame] | 338 | assert( pBlk ); |
| 339 | |
| 340 | if( BLOCK_OFFSET(i) < offset ){ |
| 341 | off = offset-BLOCK_OFFSET(i); |
| 342 | } |
| 343 | len = BLOCKSIZE - off; |
| 344 | if( BLOCK_OFFSET(i+1) > end ){ |
| 345 | len = len - (BLOCK_OFFSET(i+1)-end); |
| 346 | } |
| 347 | memcpy(&pBlk[off], zCsr, len); |
| 348 | zCsr += len; |
| 349 | } |
drh | 054889e | 2005-11-30 03:20:31 +0000 | [diff] [blame^] | 350 | if( pFile->nMaxWrite<end ){ |
| 351 | pFile->nMaxWrite = end; |
drh | 9c06c95 | 2005-11-26 00:25:00 +0000 | [diff] [blame] | 352 | } |
| 353 | assert( zCsr==&((u8 *)pBuf)[amt] ); |
drh | 054889e | 2005-11-30 03:20:31 +0000 | [diff] [blame^] | 354 | pFile->offset = end; |
drh | 9c06c95 | 2005-11-26 00:25:00 +0000 | [diff] [blame] | 355 | return rc; |
| 356 | } |
| 357 | |
| 358 | /* |
| 359 | ** Sync the file. First flush the write-cache to disk, then call the |
| 360 | ** real sync() function. |
| 361 | */ |
| 362 | static int crashSync(OsFile *id, int dataOnly){ |
drh | 054889e | 2005-11-30 03:20:31 +0000 | [diff] [blame^] | 363 | return writeCache((crashFile*)id); |
drh | 9c06c95 | 2005-11-26 00:25:00 +0000 | [diff] [blame] | 364 | } |
| 365 | |
| 366 | /* |
| 367 | ** Truncate the file. Set the internal OsFile.nMaxWrite variable to the new |
| 368 | ** file size to ensure that nothing in the write-cache past this point |
| 369 | ** is written to disk. |
| 370 | */ |
| 371 | static int crashTruncate(OsFile *id, i64 nByte){ |
drh | 054889e | 2005-11-30 03:20:31 +0000 | [diff] [blame^] | 372 | crashFile *pFile = (crashFile*)id; |
| 373 | pFile->nMaxWrite = nByte; |
| 374 | return sqlite3OsTruncate(pFile->pBase, nByte); |
drh | 9c06c95 | 2005-11-26 00:25:00 +0000 | [diff] [blame] | 375 | } |
| 376 | |
| 377 | /* |
| 378 | ** Return the size of the file. If the cache contains a write that extended |
| 379 | ** the file, then return this size instead of the on-disk size. |
| 380 | */ |
| 381 | static int crashFileSize(OsFile *id, i64 *pSize){ |
drh | 054889e | 2005-11-30 03:20:31 +0000 | [diff] [blame^] | 382 | crashFile *pFile = (crashFile*)id; |
| 383 | int rc = sqlite3OsFileSize(pFile->pBase, pSize); |
| 384 | if( rc==SQLITE_OK && pSize && *pSize<pFile->nMaxWrite ){ |
| 385 | *pSize = pFile->nMaxWrite; |
drh | 9c06c95 | 2005-11-26 00:25:00 +0000 | [diff] [blame] | 386 | } |
| 387 | return rc; |
| 388 | } |
| 389 | |
| 390 | /* |
| 391 | ** The three functions used to open files. All that is required is to |
| 392 | ** initialise the os_test.c specific fields and then call the corresponding |
| 393 | ** os_unix.c function to really open the file. |
| 394 | */ |
drh | 1a23593 | 2005-11-29 18:37:15 +0000 | [diff] [blame] | 395 | static int crashOpenReadWrite(const char *zFilename, OsFile **pId,int *pRdonly){ |
| 396 | OsFile *pBase = 0; |
drh | 054889e | 2005-11-30 03:20:31 +0000 | [diff] [blame^] | 397 | int rc = origOs.xOpenReadWrite(zFilename, &pBase, pRdonly); |
drh | 1a23593 | 2005-11-29 18:37:15 +0000 | [diff] [blame] | 398 | if( !rc ){ |
| 399 | initFile(pId, zFilename, pBase); |
| 400 | } |
| 401 | return rc; |
drh | 9c06c95 | 2005-11-26 00:25:00 +0000 | [diff] [blame] | 402 | } |
drh | 1a23593 | 2005-11-29 18:37:15 +0000 | [diff] [blame] | 403 | static int crashOpenExclusive(const char *zFilename, OsFile **pId, int delFlag){ |
| 404 | OsFile *pBase = 0; |
drh | 054889e | 2005-11-30 03:20:31 +0000 | [diff] [blame^] | 405 | int rc = origOs.xOpenExclusive(zFilename, &pBase, delFlag); |
drh | 1a23593 | 2005-11-29 18:37:15 +0000 | [diff] [blame] | 406 | if( !rc ){ |
| 407 | initFile(pId, zFilename, pBase); |
| 408 | } |
| 409 | return rc; |
drh | 9c06c95 | 2005-11-26 00:25:00 +0000 | [diff] [blame] | 410 | } |
drh | 1a23593 | 2005-11-29 18:37:15 +0000 | [diff] [blame] | 411 | static int crashOpenReadOnly(const char *zFilename, OsFile **pId){ |
| 412 | OsFile *pBase = 0; |
drh | 054889e | 2005-11-30 03:20:31 +0000 | [diff] [blame^] | 413 | int rc = origOs.xOpenReadOnly(zFilename, &pBase); |
drh | 1a23593 | 2005-11-29 18:37:15 +0000 | [diff] [blame] | 414 | if( !rc ){ |
| 415 | initFile(pId, zFilename, pBase); |
| 416 | } |
| 417 | return rc; |
drh | 9c06c95 | 2005-11-26 00:25:00 +0000 | [diff] [blame] | 418 | } |
| 419 | |
| 420 | /* |
drh | 054889e | 2005-11-30 03:20:31 +0000 | [diff] [blame^] | 421 | ** OpenDirectory is a no-op |
drh | 1883921 | 2005-11-26 03:43:23 +0000 | [diff] [blame] | 422 | */ |
drh | 054889e | 2005-11-30 03:20:31 +0000 | [diff] [blame^] | 423 | static int crashOpenDir(OsFile *id, const char *zName){ |
drh | 1a23593 | 2005-11-29 18:37:15 +0000 | [diff] [blame] | 424 | return SQLITE_OK; |
| 425 | } |
| 426 | |
| 427 | /* |
| 428 | ** Locking primitives are passed through into the underlying |
| 429 | ** file descriptor. |
| 430 | */ |
| 431 | int crashLock(OsFile *id, int lockType){ |
drh | 054889e | 2005-11-30 03:20:31 +0000 | [diff] [blame^] | 432 | return sqlite3OsLock(((crashFile*)id)->pBase, lockType); |
drh | 1a23593 | 2005-11-29 18:37:15 +0000 | [diff] [blame] | 433 | } |
| 434 | int crashUnlock(OsFile *id, int lockType){ |
drh | 054889e | 2005-11-30 03:20:31 +0000 | [diff] [blame^] | 435 | return sqlite3OsUnlock(((crashFile*)id)->pBase, lockType); |
drh | 1a23593 | 2005-11-29 18:37:15 +0000 | [diff] [blame] | 436 | } |
| 437 | int crashCheckReservedLock(OsFile *id){ |
drh | 054889e | 2005-11-30 03:20:31 +0000 | [diff] [blame^] | 438 | return sqlite3OsCheckReservedLock(((crashFile*)id)->pBase); |
drh | 1a23593 | 2005-11-29 18:37:15 +0000 | [diff] [blame] | 439 | } |
| 440 | void crashSetFullSync(OsFile *id, int setting){ |
| 441 | return; /* This is a no-op */ |
| 442 | } |
| 443 | int crashLockState(OsFile *id){ |
drh | 054889e | 2005-11-30 03:20:31 +0000 | [diff] [blame^] | 444 | return sqlite3OsLockState(((crashFile*)id)->pBase); |
drh | 1a23593 | 2005-11-29 18:37:15 +0000 | [diff] [blame] | 445 | } |
| 446 | |
| 447 | /* |
| 448 | ** Return the underlying file handle. |
| 449 | */ |
| 450 | int crashFileHandle(OsFile *id){ |
drh | 054889e | 2005-11-30 03:20:31 +0000 | [diff] [blame^] | 451 | return sqlite3OsFileHandle(((crashFile*)id)->pBase); |
drh | 1883921 | 2005-11-26 03:43:23 +0000 | [diff] [blame] | 452 | } |
| 453 | |
| 454 | /* |
drh | 054889e | 2005-11-30 03:20:31 +0000 | [diff] [blame^] | 455 | ** This vector defines all the methods that can operate on an OsFile |
| 456 | ** for the crash tester. |
| 457 | */ |
| 458 | static const IoMethod crashIoMethod = { |
| 459 | crashClose, |
| 460 | crashOpenDir, |
| 461 | crashRead, |
| 462 | crashWrite, |
| 463 | crashSeek, |
| 464 | crashTruncate, |
| 465 | crashSync, |
| 466 | crashSetFullSync, |
| 467 | crashFileHandle, |
| 468 | crashFileSize, |
| 469 | crashLock, |
| 470 | crashUnlock, |
| 471 | crashLockState, |
| 472 | crashCheckReservedLock, |
| 473 | }; |
| 474 | |
| 475 | |
| 476 | /* |
| 477 | ** Initialise the os_test.c specific fields of pFile. |
| 478 | */ |
| 479 | static void initFile(OsFile **pId, char const *zName, OsFile *pBase){ |
| 480 | crashFile *pFile = sqliteMalloc(sizeof(crashFile) + strlen(zName)+1); |
| 481 | pFile->pMethod = &crashIoMethod; |
| 482 | pFile->nMaxWrite = 0; |
| 483 | pFile->offset = 0; |
| 484 | pFile->nBlk = 0; |
| 485 | pFile->apBlk = 0; |
| 486 | pFile->zName = (char *)(&pFile[1]); |
| 487 | strcpy(pFile->zName, zName); |
| 488 | pFile->pBase = pBase; |
| 489 | pFile->pNext = pAllFiles; |
| 490 | pAllFiles = pFile; |
| 491 | *pId = (OsFile*)pFile; |
| 492 | } |
| 493 | |
| 494 | |
| 495 | /* |
drh | 9c06c95 | 2005-11-26 00:25:00 +0000 | [diff] [blame] | 496 | ** tclcmd: sqlite_crashparams DELAY CRASHFILE |
| 497 | ** |
| 498 | ** This procedure implements a TCL command that enables crash testing |
| 499 | ** in testfixture. Once enabled, crash testing cannot be disabled. |
| 500 | */ |
| 501 | static int crashParamsObjCmd( |
| 502 | void * clientData, |
| 503 | Tcl_Interp *interp, |
| 504 | int objc, |
| 505 | Tcl_Obj *CONST objv[] |
| 506 | ){ |
| 507 | int delay; |
| 508 | const char *zFile; |
| 509 | int nFile; |
| 510 | if( objc!=3 ){ |
| 511 | Tcl_WrongNumArgs(interp, 1, objv, "DELAY CRASHFILE"); |
| 512 | return TCL_ERROR; |
| 513 | } |
| 514 | if( Tcl_GetIntFromObj(interp, objv[1], &delay) ) return TCL_ERROR; |
| 515 | zFile = Tcl_GetStringFromObj(objv[2], &nFile); |
| 516 | if( nFile>=sizeof(zCrashFile)-1 ){ |
| 517 | Tcl_AppendResult(interp, "crash file name too big", 0); |
| 518 | return TCL_ERROR; |
| 519 | } |
| 520 | setCrashParams(delay, zFile); |
drh | 054889e | 2005-11-30 03:20:31 +0000 | [diff] [blame^] | 521 | if( origOs.xOpenReadWrite==0 ){ |
| 522 | origOs = sqlite3Os; |
| 523 | sqlite3Os.xOpenReadWrite = crashOpenReadWrite; |
| 524 | sqlite3Os.xOpenExclusive = crashOpenExclusive; |
| 525 | sqlite3Os.xOpenReadOnly = crashOpenReadOnly; |
| 526 | } |
drh | 9c06c95 | 2005-11-26 00:25:00 +0000 | [diff] [blame] | 527 | return TCL_OK; |
| 528 | } |
| 529 | |
| 530 | /* |
| 531 | ** This procedure registers the TCL procedures defined in this file. |
| 532 | */ |
| 533 | int Sqlitetest6_Init(Tcl_Interp *interp){ |
| 534 | Tcl_CreateObjCommand(interp, "sqlite3_crashparams", crashParamsObjCmd, 0, 0); |
drh | 9c06c95 | 2005-11-26 00:25:00 +0000 | [diff] [blame] | 535 | return TCL_OK; |
| 536 | } |
| 537 | |
| 538 | #endif /* SQLITE_TEST */ |