sqlite
[Ported from github.com/sqlite/sqlite] Fix bugs in and improve the interface to the ".check" command of the CLI.
Brought by drakestapleton
| 1 | /* |
| 2 | ** A utility for printing content from a write-ahead log file. |
| 3 | */ |
| 4 | #include <stdio.h> |
| 5 | #include <ctype.h> |
| 6 | #include <sys/types.h> |
| 7 | #include <sys/stat.h> |
| 8 | #include <fcntl.h> |
| 9 | |
| 10 | #define ISDIGIT(X) isdigit((unsigned char)(X)) |
| 11 | #define ISPRINT(X) isprint((unsigned char)(X)) |
| 12 | |
| 13 | #if !defined(_MSC_VER) |
| 14 | #include <unistd.h> |
| 15 | #include <sys/types.h> |
| 16 | #else |
| 17 | #include <io.h> |
| 18 | #endif |
| 19 | |
| 20 | #include <stdlib.h> |
| 21 | #include <string.h> |
| 22 | |
| 23 | |
| 24 | static int pagesize = 1024; /* Size of a database page */ |
| 25 | static int fd = -1; /* File descriptor for reading the WAL file */ |
| 26 | static int mxFrame = 0; /* Last frame */ |
| 27 | static int perLine = 16; /* HEX elements to print per line */ |
| 28 | |
| 29 | typedef long long int i64; /* Datatype for 64-bit integers */ |
| 30 | |
| 31 | /* Information for computing the checksum */ |
| 32 | typedef struct Cksum Cksum; |
| 33 | struct Cksum { |
| 34 | int bSwap; /* True to do byte swapping on 32-bit words */ |
| 35 | unsigned s0, s1; /* Current checksum value */ |
| 36 | }; |
| 37 | |
| 38 | /* |
| 39 | ** extract a 32-bit big-endian integer |
| 40 | */ |
| 41 | static unsigned int getInt32(const unsigned char *a){ |
| 42 | unsigned int x = (a[0]<<24) + (a[1]<<16) + (a[2]<<8) + a[3]; |
| 43 | return x; |
| 44 | } |
| 45 | |
| 46 | /* |
| 47 | ** Swap bytes on a 32-bit unsigned integer |
| 48 | */ |
| 49 | static unsigned int swab32(unsigned int x){ |
| 50 | return (((x)&0x000000FF)<<24) + (((x)&0x0000FF00)<<8) |
| 51 | + (((x)&0x00FF0000)>>8) + (((x)&0xFF000000)>>24); |
| 52 | } |
| 53 | |
| 54 | /* Extend the checksum. Reinitialize the checksum if bInit is true. |
| 55 | */ |
| 56 | static void extendCksum( |
| 57 | Cksum *pCksum, |
| 58 | unsigned char *aData, |
| 59 | unsigned int nByte, |
| 60 | int bInit |
| 61 | ){ |
| 62 | unsigned int *a32; |
| 63 | if( bInit ){ |
| 64 | int a = 0; |
| 65 | *((char*)&a) = 1; |
| 66 | if( a==1 ){ |
| 67 | /* Host is little-endian */ |
| 68 | pCksum->bSwap = getInt32(aData)!=0x377f0682; |
| 69 | }else{ |
| 70 | /* Host is big-endian */ |
| 71 | pCksum->bSwap = getInt32(aData)!=0x377f0683; |
| 72 | } |
| 73 | pCksum->s0 = 0; |
| 74 | pCksum->s1 = 0; |
| 75 | } |
| 76 | a32 = (unsigned int*)aData; |
| 77 | while( nByte>0 ){ |
| 78 | unsigned int x0 = a32[0]; |
| 79 | unsigned int x1 = a32[1]; |
| 80 | if( pCksum->bSwap ){ |
| 81 | x0 = swab32(x0); |
| 82 | x1 = swab32(x1); |
| 83 | } |
| 84 | pCksum->s0 += x0 + pCksum->s1; |
| 85 | pCksum->s1 += x1 + pCksum->s0; |
| 86 | nByte -= 8; |
| 87 | a32 += 2; |
| 88 | } |
| 89 | } |
| 90 | |
| 91 | /* |
| 92 | ** Convert the var-int format into i64. Return the number of bytes |
| 93 | ** in the var-int. Write the var-int value into *pVal. |
| 94 | */ |
| 95 | static int decodeVarint(const unsigned char *z, i64 *pVal){ |
| 96 | i64 v = 0; |
| 97 | int i; |
| 98 | for(i=0; i<8; i++){ |
| 99 | v = (v<<7) + (z[i]&0x7f); |
| 100 | if( (z[i]&0x80)==0 ){ *pVal = v; return i+1; } |
| 101 | } |
| 102 | v = (v<<8) + (z[i]&0xff); |
| 103 | *pVal = v; |
| 104 | return 9; |
| 105 | } |
| 106 | |
| 107 | /* Report an out-of-memory error and die. |
| 108 | */ |
| 109 | static void out_of_memory(void){ |
| 110 | fprintf(stderr,"Out of memory...\n"); |
| 111 | exit(1); |
| 112 | } |
| 113 | |
| 114 | /* |
| 115 | ** Read content from the file. |
| 116 | ** |
| 117 | ** Space to hold the content is obtained from malloc() and needs to be |
| 118 | ** freed by the caller. |
| 119 | */ |
| 120 | static unsigned char *getContent(i64 ofst, int nByte){ |
| 121 | unsigned char *aData; |
| 122 | aData = malloc(nByte); |
| 123 | if( aData==0 ) out_of_memory(); |
| 124 | lseek(fd, ofst, SEEK_SET); |
| 125 | read(fd, aData, nByte); |
| 126 | return aData; |
| 127 | } |
| 128 | |
| 129 | /* |
| 130 | ** Print a range of bytes as hex and as ascii. |
| 131 | */ |
| 132 | static void print_byte_range( |
| 133 | int ofst, /* First byte in the range of bytes to print */ |
| 134 | int nByte, /* Number of bytes to print */ |
| 135 | unsigned char *aData, /* Content to print */ |
| 136 | int printOfst /* Add this amount to the index on the left column */ |
| 137 | ){ |
| 138 | int i, j; |
| 139 | const char *zOfstFmt; |
| 140 | |
| 141 | if( ((printOfst+nByte)&~0xfff)==0 ){ |
| 142 | zOfstFmt = " %03x: "; |
| 143 | }else if( ((printOfst+nByte)&~0xffff)==0 ){ |
| 144 | zOfstFmt = " %04x: "; |
| 145 | }else if( ((printOfst+nByte)&~0xfffff)==0 ){ |
| 146 | zOfstFmt = " %05x: "; |
| 147 | }else if( ((printOfst+nByte)&~0xffffff)==0 ){ |
| 148 | zOfstFmt = " %06x: "; |
| 149 | }else{ |
| 150 | zOfstFmt = " %08x: "; |
| 151 | } |
| 152 | |
| 153 | for(i=0; i<nByte; i += perLine){ |
| 154 | fprintf(stdout, zOfstFmt, i+printOfst); |
| 155 | for(j=0; j<perLine; j++){ |
| 156 | if( i+j>nByte ){ |
| 157 | fprintf(stdout, " "); |
| 158 | }else{ |
| 159 | fprintf(stdout,"%02x ", aData[i+j]); |
| 160 | } |
| 161 | } |
| 162 | for(j=0; j<perLine; j++){ |
| 163 | if( i+j>nByte ){ |
| 164 | fprintf(stdout, " "); |
| 165 | }else{ |
| 166 | fprintf(stdout,"%c", ISPRINT(aData[i+j]) ? aData[i+j] : '.'); |
| 167 | } |
| 168 | } |
| 169 | fprintf(stdout,"\n"); |
| 170 | } |
| 171 | } |
| 172 | |
| 173 | /* Print a line of decode output showing a 4-byte integer. |
| 174 | */ |
| 175 | static void print_decode_line( |
| 176 | unsigned char *aData, /* Content being decoded */ |
| 177 | int ofst, int nByte, /* Start and size of decode */ |
| 178 | int asHex, /* If true, output value as hex */ |
| 179 | const char *zMsg /* Message to append */ |
| 180 | ){ |
| 181 | int i, j; |
| 182 | int val = aData[ofst]; |
| 183 | char zBuf[100]; |
| 184 | sprintf(zBuf, " %03x: %02x", ofst, aData[ofst]); |
| 185 | i = (int)strlen(zBuf); |
| 186 | for(j=1; j<4; j++){ |
| 187 | if( j>=nByte ){ |
| 188 | sprintf(&zBuf[i], " "); |
| 189 | }else{ |
| 190 | sprintf(&zBuf[i], " %02x", aData[ofst+j]); |
| 191 | val = val*256 + aData[ofst+j]; |
| 192 | } |
| 193 | i += (int)strlen(&zBuf[i]); |
| 194 | } |
| 195 | if( asHex ){ |
| 196 | sprintf(&zBuf[i], " 0x%08x", val); |
| 197 | }else{ |
| 198 | sprintf(&zBuf[i], " %9d", val); |
| 199 | } |
| 200 | printf("%s %s\n", zBuf, zMsg); |
| 201 | } |
| 202 | |
| 203 | /* |
| 204 | ** Print an entire page of content as hex |
| 205 | */ |
| 206 | static void print_frame(int iFrame){ |
| 207 | i64 iStart; |
| 208 | unsigned char *aData; |
| 209 | iStart = 32 + (i64)(iFrame-1)*(pagesize+24); |
| 210 | fprintf(stdout, "Frame %d: (offsets 0x%llx..0x%llx)\n", |
| 211 | iFrame, iStart, iStart+pagesize+24); |
| 212 | aData = getContent(iStart, pagesize+24); |
| 213 | print_decode_line(aData, 0, 4, 0, "Page number"); |
| 214 | print_decode_line(aData, 4, 4, 0, "DB size, or 0 for non-commit"); |
| 215 | print_decode_line(aData, 8, 4, 1, "Salt-1"); |
| 216 | print_decode_line(aData,12, 4, 1, "Salt-2"); |
| 217 | print_decode_line(aData,16, 4, 1, "Checksum-1"); |
| 218 | print_decode_line(aData,20, 4, 1, "Checksum-2"); |
| 219 | print_byte_range(iStart+24, pagesize, aData+24, 0); |
| 220 | free(aData); |
| 221 | } |
| 222 | |
| 223 | /* |
| 224 | ** Summarize a single frame on a single line. |
| 225 | */ |
| 226 | static void print_oneline_frame(int iFrame, Cksum *pCksum){ |
| 227 | i64 iStart; |
| 228 | unsigned char *aData; |
| 229 | unsigned int s0, s1; |
| 230 | iStart = 32 + (i64)(iFrame-1)*(pagesize+24); |
| 231 | aData = getContent(iStart, 24); |
| 232 | extendCksum(pCksum, aData, 8, 0); |
| 233 | extendCksum(pCksum, getContent(iStart+24, pagesize), pagesize, 0); |
| 234 | s0 = getInt32(aData+16); |
| 235 | s1 = getInt32(aData+20); |
| 236 | fprintf(stdout, "Frame %4d: %6d %6d 0x%08x,%08x 0x%08x,%08x", |
| 237 | iFrame, |
| 238 | getInt32(aData), |
| 239 | getInt32(aData+4), |
| 240 | getInt32(aData+8), |
| 241 | getInt32(aData+12), |
| 242 | s0, |
| 243 | s1 |
| 244 | ); |
| 245 | if( s0==pCksum->s0 && s1==pCksum->s1 ){ |
| 246 | fprintf(stdout, "\n"); |
| 247 | }else{ |
| 248 | fprintf(stdout, " should be 0x%08x,%08x\n", |
| 249 | pCksum->s0, pCksum->s1); |
| 250 | } |
| 251 | |
| 252 | /* Reset the checksum so that a single frame checksum failure will not |
| 253 | ** cause all subsequent frames to also show a failure. */ |
| 254 | pCksum->s0 = s0; |
| 255 | pCksum->s1 = s1; |
| 256 | free(aData); |
| 257 | } |
| 258 | |
| 259 | /* |
| 260 | ** Decode the WAL header. |
| 261 | */ |
| 262 | static void print_wal_header(Cksum *pCksum){ |
| 263 | unsigned char *aData; |
| 264 | aData = getContent(0, 32); |
| 265 | if( pCksum ){ |
| 266 | extendCksum(pCksum, aData, 24, 1); |
| 267 | printf("Checksum byte order: %s\n", pCksum->bSwap ? "swapped" : "native"); |
| 268 | } |
| 269 | printf("WAL Header:\n"); |
| 270 | print_decode_line(aData, 0, 4,1,"Magic. 0x377f0682 (le) or 0x377f0683 (be)"); |
| 271 | print_decode_line(aData, 4, 4, 0, "File format"); |
| 272 | print_decode_line(aData, 8, 4, 0, "Database page size"); |
| 273 | print_decode_line(aData, 12,4, 0, "Checkpoint sequence number"); |
| 274 | print_decode_line(aData, 16,4, 1, "Salt-1"); |
| 275 | print_decode_line(aData, 20,4, 1, "Salt-2"); |
| 276 | print_decode_line(aData, 24,4, 1, "Checksum-1"); |
| 277 | print_decode_line(aData, 28,4, 1, "Checksum-2"); |
| 278 | if( pCksum ){ |
| 279 | if( pCksum->s0!=getInt32(aData+24) ){ |
| 280 | printf("**** cksum-1 mismatch: 0x%08x\n", pCksum->s0); |
| 281 | } |
| 282 | if( pCksum->s1!=getInt32(aData+28) ){ |
| 283 | printf("**** cksum-2 mismatch: 0x%08x\n", pCksum->s1); |
| 284 | } |
| 285 | } |
| 286 | free(aData); |
| 287 | } |
| 288 | /* |
| 289 | ** Describe cell content. |
| 290 | */ |
| 291 | static i64 describeContent( |
| 292 | unsigned char *a, /* Cell content */ |
| 293 | i64 nLocal, /* Bytes in a[] */ |
| 294 | int nMax, /* Maximum number of bytes to write to zDesc */ |
| 295 | char *zDesc /* Write description here */ |
| 296 | ){ |
| 297 | int nDesc = 0; |
| 298 | int n, j; |
| 299 | i64 i, x, v; |
| 300 | const unsigned char *pData; |
| 301 | const unsigned char *pLimit; |
| 302 | char sep = ' '; |
| 303 | |
| 304 | pLimit = &a[nLocal]; |
| 305 | n = decodeVarint(a, &x); |
| 306 | pData = &a[x]; |
| 307 | a += n; |
| 308 | i = x - n; |
| 309 | while( i>0 && pData<=pLimit && nDesc<nMax ){ |
| 310 | n = decodeVarint(a, &x); |
| 311 | a += n; |
| 312 | i -= n; |
| 313 | nLocal -= n; |
| 314 | zDesc[0] = sep; |
| 315 | sep = ','; |
| 316 | nDesc++; |
| 317 | zDesc++; |
| 318 | if( x==0 ){ |
| 319 | snprintf(zDesc, nMax-nDesc, "*"); /* NULL is a "*" */ |
| 320 | }else if( x>=1 && x<=6 ){ |
| 321 | v = (signed char)pData[0]; |
| 322 | pData++; |
| 323 | switch( x ){ |
| 324 | case 6: v = (v<<16) + (pData[0]<<8) + pData[1]; pData += 2; |
| 325 | case 5: v = (v<<16) + (pData[0]<<8) + pData[1]; pData += 2; |
| 326 | case 4: v = (v<<8) + pData[0]; pData++; |
| 327 | case 3: v = (v<<8) + pData[0]; pData++; |
| 328 | case 2: v = (v<<8) + pData[0]; pData++; |
| 329 | } |
| 330 | snprintf(zDesc, nMax-nDesc, "%lld", v); |
| 331 | }else if( x==7 ){ |
| 332 | snprintf(zDesc, nMax-nDesc, "real"); |
| 333 | pData += 8; |
| 334 | }else if( x==8 ){ |
| 335 | snprintf(zDesc, nMax-nDesc, "0"); |
| 336 | }else if( x==9 ){ |
| 337 | snprintf(zDesc, nMax-nDesc, "1"); |
| 338 | }else if( x>=12 ){ |
| 339 | i64 size = (x-12)/2; |
| 340 | if( (x&1)==0 ){ |
| 341 | snprintf(zDesc, nMax-nDesc, "blob(%lld)", size); |
| 342 | }else{ |
| 343 | snprintf(zDesc, nMax-nDesc, "txt(%lld)", size); |
| 344 | } |
| 345 | pData += size; |
| 346 | } |
| 347 | j = (int)strlen(zDesc); |
| 348 | zDesc += j; |
| 349 | nDesc += j; |
| 350 | } |
| 351 | return nDesc; |
| 352 | } |
| 353 | |
| 354 | /* |
| 355 | ** Compute the local payload size given the total payload size and |
| 356 | ** the page size. |
| 357 | */ |
| 358 | static i64 localPayload(i64 nPayload, char cType){ |
| 359 | i64 maxLocal; |
| 360 | i64 minLocal; |
| 361 | i64 surplus; |
| 362 | i64 nLocal; |
| 363 | if( cType==13 ){ |
| 364 | /* Table leaf */ |
| 365 | maxLocal = pagesize-35; |
| 366 | minLocal = (pagesize-12)*32/255-23; |
| 367 | }else{ |
| 368 | maxLocal = (pagesize-12)*64/255-23; |
| 369 | minLocal = (pagesize-12)*32/255-23; |
| 370 | } |
| 371 | if( nPayload>maxLocal ){ |
| 372 | surplus = minLocal + (nPayload-minLocal)%(pagesize-4); |
| 373 | if( surplus<=maxLocal ){ |
| 374 | nLocal = surplus; |
| 375 | }else{ |
| 376 | nLocal = minLocal; |
| 377 | } |
| 378 | }else{ |
| 379 | nLocal = nPayload; |
| 380 | } |
| 381 | return nLocal; |
| 382 | } |
| 383 | |
| 384 | /* |
| 385 | ** Create a description for a single cell. |
| 386 | ** |
| 387 | ** The return value is the local cell size. |
| 388 | */ |
| 389 | static i64 describeCell( |
| 390 | unsigned char cType, /* Page type */ |
| 391 | unsigned char *a, /* Cell content */ |
| 392 | int showCellContent, /* Show cell content if true */ |
| 393 | char **pzDesc /* Store description here */ |
| 394 | ){ |
| 395 | int i; |
| 396 | i64 nDesc = 0; |
| 397 | int n = 0; |
| 398 | int leftChild; |
| 399 | i64 nPayload; |
| 400 | i64 rowid; |
| 401 | i64 nLocal; |
| 402 | static char zDesc[1000]; |
| 403 | i = 0; |
| 404 | if( cType<=5 ){ |
| 405 | leftChild = ((a[0]*256 + a[1])*256 + a[2])*256 + a[3]; |
| 406 | a += 4; |
| 407 | n += 4; |
| 408 | sprintf(zDesc, "lx: %d ", leftChild); |
| 409 | nDesc = strlen(zDesc); |
| 410 | } |
| 411 | if( cType!=5 ){ |
| 412 | i = decodeVarint(a, &nPayload); |
| 413 | a += i; |
| 414 | n += i; |
| 415 | sprintf(&zDesc[nDesc], "n: %lld ", nPayload); |
| 416 | nDesc += strlen(&zDesc[nDesc]); |
| 417 | nLocal = localPayload(nPayload, cType); |
| 418 | }else{ |
| 419 | nPayload = nLocal = 0; |
| 420 | } |
| 421 | if( cType==5 || cType==13 ){ |
| 422 | i = decodeVarint(a, &rowid); |
| 423 | a += i; |
| 424 | n += i; |
| 425 | sprintf(&zDesc[nDesc], "r: %lld ", rowid); |
| 426 | nDesc += strlen(&zDesc[nDesc]); |
| 427 | } |
| 428 | if( nLocal<nPayload ){ |
| 429 | int ovfl; |
| 430 | unsigned char *b = &a[nLocal]; |
| 431 | ovfl = ((b[0]*256 + b[1])*256 + b[2])*256 + b[3]; |
| 432 | sprintf(&zDesc[nDesc], "ov: %d ", ovfl); |
| 433 | nDesc += strlen(&zDesc[nDesc]); |
| 434 | n += 4; |
| 435 | } |
| 436 | if( showCellContent && cType!=5 ){ |
| 437 | zDesc[sizeof(zDesc)-1] = '\0'; |
| 438 | nDesc += describeContent(a, nLocal, sizeof(zDesc)-nDesc, &zDesc[nDesc-1]); |
| 439 | } |
| 440 | *pzDesc = zDesc; |
| 441 | return nLocal+n; |
| 442 | } |
| 443 | |
| 444 | /* |
| 445 | ** Decode a btree page |
| 446 | */ |
| 447 | static void decode_btree_page( |
| 448 | unsigned char *a, /* Content of the btree page to be decoded */ |
| 449 | int pgno, /* Page number */ |
| 450 | int hdrSize, /* Size of the page1-header in bytes */ |
| 451 | const char *zArgs /* Flags to control formatting */ |
| 452 | ){ |
| 453 | const char *zType = "unknown"; |
| 454 | int nCell; |
| 455 | int i, j; |
| 456 | int iCellPtr; |
| 457 | int showCellContent = 0; |
| 458 | int showMap = 0; |
| 459 | char *zMap = 0; |
| 460 | switch( a[0] ){ |
| 461 | case 2: zType = "index interior node"; break; |
| 462 | case 5: zType = "table interior node"; break; |
| 463 | case 10: zType = "index leaf"; break; |
| 464 | case 13: zType = "table leaf"; break; |
| 465 | } |
| 466 | while( zArgs[0] ){ |
| 467 | switch( zArgs[0] ){ |
| 468 | case 'c': showCellContent = 1; break; |
| 469 | case 'm': showMap = 1; break; |
| 470 | } |
| 471 | zArgs++; |
| 472 | } |
| 473 | printf("Decode of btree page %d:\n", pgno); |
| 474 | print_decode_line(a, 0, 1, 0, zType); |
| 475 | print_decode_line(a, 1, 2, 0, "Offset to first freeblock"); |
| 476 | print_decode_line(a, 3, 2, 0, "Number of cells on this page"); |
| 477 | nCell = a[3]*256 + a[4]; |
| 478 | print_decode_line(a, 5, 2, 0, "Offset to cell content area"); |
| 479 | print_decode_line(a, 7, 1, 0, "Fragmented byte count"); |
| 480 | if( a[0]==2 || a[0]==5 ){ |
| 481 | print_decode_line(a, 8, 4, 0, "Right child"); |
| 482 | iCellPtr = 12; |
| 483 | }else{ |
| 484 | iCellPtr = 8; |
| 485 | } |
| 486 | if( nCell>0 ){ |
| 487 | printf(" key: lx=left-child n=payload-size r=rowid\n"); |
| 488 | } |
| 489 | if( showMap ){ |
| 490 | zMap = malloc(pagesize); |
| 491 | memset(zMap, '.', pagesize); |
| 492 | memset(zMap, '1', hdrSize); |
| 493 | memset(&zMap[hdrSize], 'H', iCellPtr); |
| 494 | memset(&zMap[hdrSize+iCellPtr], 'P', 2*nCell); |
| 495 | } |
| 496 | for(i=0; i<nCell; i++){ |
| 497 | int cofst = iCellPtr + i*2; |
| 498 | char *zDesc; |
| 499 | i64 n; |
| 500 | |
| 501 | cofst = a[cofst]*256 + a[cofst+1]; |
| 502 | n = describeCell(a[0], &a[cofst-hdrSize], showCellContent, &zDesc); |
| 503 | if( showMap ){ |
| 504 | char zBuf[30]; |
| 505 | memset(&zMap[cofst], '*', (size_t)n); |
| 506 | zMap[cofst] = '['; |
| 507 | zMap[cofst+n-1] = ']'; |
| 508 | sprintf(zBuf, "%d", i); |
| 509 | j = (int)strlen(zBuf); |
| 510 | if( j<=n-2 ) memcpy(&zMap[cofst+1], zBuf, j); |
| 511 | } |
| 512 | printf(" %03x: cell[%d] %s\n", cofst, i, zDesc); |
| 513 | } |
| 514 | if( showMap ){ |
| 515 | for(i=0; i<pagesize; i+=64){ |
| 516 | printf(" %03x: %.64s\n", i, &zMap[i]); |
| 517 | } |
| 518 | free(zMap); |
| 519 | } |
| 520 | } |
| 521 | |
| 522 | /* |
| 523 | ** Check the range validity for a page number. Print an error and |
| 524 | ** exit if the page is out of range. |
| 525 | */ |
| 526 | static void checkPageValidity(int iPage, int mxPage){ |
| 527 | if( iPage<1 || iPage>mxPage ){ |
| 528 | fprintf(stderr, "Invalid page number %d: valid range is 1..%d\n", |
| 529 | iPage, mxPage); |
| 530 | exit(1); |
| 531 | } |
| 532 | } |
| 533 | |
| 534 | int main(int argc, char **argv){ |
| 535 | struct stat sbuf; |
| 536 | unsigned char zPgSz[4]; |
| 537 | if( argc<2 ){ |
| 538 | fprintf(stderr,"Usage: %s FILENAME ?PAGE? ...\n", argv[0]); |
| 539 | exit(1); |
| 540 | } |
| 541 | fd = open(argv[1], O_RDONLY); |
| 542 | if( fd<0 ){ |
| 543 | fprintf(stderr,"%s: can't open %s\n", argv[0], argv[1]); |
| 544 | exit(1); |
| 545 | } |
| 546 | zPgSz[0] = 0; |
| 547 | zPgSz[1] = 0; |
| 548 | fstat(fd, &sbuf); |
| 549 | if( sbuf.st_size<32 ){ |
| 550 | printf("%s: file too small to be a WAL - only %d bytes\n", |
| 551 | argv[1], (int)sbuf.st_size); |
| 552 | return 0; |
| 553 | } |
| 554 | if( lseek(fd, 8, SEEK_SET)!=8 ){ |
| 555 | printf("\"%s\" seems to not be a valid WAL file\n", argv[1]); |
| 556 | return 1; |
| 557 | } |
| 558 | if( read(fd, zPgSz, 4)!=4 ){ |
| 559 | printf("\"%s\": cannot read the page size\n", argv[1]); |
| 560 | return 1; |
| 561 | } |
| 562 | pagesize = zPgSz[1]*65536 + zPgSz[2]*256 + zPgSz[3]; |
| 563 | if( pagesize==0 ) pagesize = 1024; |
| 564 | printf("Pagesize: %d\n", pagesize); |
| 565 | if( (pagesize & (pagesize-1))!=0 || pagesize<512 || pagesize>65536 ){ |
| 566 | printf("\"%s\": invalid page size.\n", argv[1]); |
| 567 | return 1; |
| 568 | } |
| 569 | mxFrame = (sbuf.st_size - 32)/(pagesize + 24); |
| 570 | printf("Available pages: 1..%d\n", mxFrame); |
| 571 | if( argc==2 ){ |
| 572 | int i; |
| 573 | Cksum x; |
| 574 | print_wal_header(&x); |
| 575 | for(i=1; i<=mxFrame; i++){ |
| 576 | print_oneline_frame(i, &x); |
| 577 | } |
| 578 | }else{ |
| 579 | int i; |
| 580 | for(i=2; i<argc; i++){ |
| 581 | int iStart, iEnd; |
| 582 | char *zLeft; |
| 583 | if( strcmp(argv[i], "header")==0 ){ |
| 584 | print_wal_header(0); |
| 585 | continue; |
| 586 | } |
| 587 | if( !ISDIGIT(argv[i][0]) ){ |
| 588 | fprintf(stderr, "%s: unknown option: [%s]\n", argv[0], argv[i]); |
| 589 | continue; |
| 590 | } |
| 591 | iStart = strtol(argv[i], &zLeft, 0); |
| 592 | checkPageValidity(iStart, mxFrame); |
| 593 | if( zLeft && strcmp(zLeft,"..end")==0 ){ |
| 594 | iEnd = mxFrame; |
| 595 | }else if( zLeft && zLeft[0]=='.' && zLeft[1]=='.' ){ |
| 596 | iEnd = strtol(&zLeft[2], 0, 0); |
| 597 | checkPageValidity(iEnd, mxFrame); |
| 598 | }else if( zLeft && zLeft[0]=='b' ){ |
| 599 | i64 ofst; |
| 600 | int nByte, hdrSize; |
| 601 | unsigned char *a; |
| 602 | if( iStart==1 ){ |
| 603 | hdrSize = 100; |
| 604 | ofst = hdrSize = 100; |
| 605 | nByte = pagesize-100; |
| 606 | }else{ |
| 607 | hdrSize = 0; |
| 608 | ofst = (i64)(iStart-1)*pagesize; |
| 609 | nByte = pagesize; |
| 610 | } |
| 611 | ofst = 32 + hdrSize + (i64)(iStart-1)*(pagesize+24) + 24; |
| 612 | a = getContent(ofst, nByte); |
| 613 | decode_btree_page(a, iStart, hdrSize, zLeft+1); |
| 614 | free(a); |
| 615 | continue; |
| 616 | #if !defined(_MSC_VER) |
| 617 | }else if( zLeft && strcmp(zLeft,"truncate")==0 ){ |
| 618 | /* Frame number followed by "truncate" truncates the WAL file |
| 619 | ** after that frame */ |
| 620 | off_t newSize = 32 + iStart*(pagesize+24); |
| 621 | truncate(argv[1], newSize); |
| 622 | continue; |
| 623 | #endif |
| 624 | }else{ |
| 625 | iEnd = iStart; |
| 626 | } |
| 627 | if( iStart<1 || iEnd<iStart || iEnd>mxFrame ){ |
| 628 | fprintf(stderr, |
| 629 | "Page argument should be LOWER?..UPPER?. Range 1 to %d\n", |
| 630 | mxFrame); |
| 631 | exit(1); |
| 632 | } |
| 633 | while( iStart<=iEnd ){ |
| 634 | print_frame(iStart); |
| 635 | iStart++; |
| 636 | } |
| 637 | } |
| 638 | } |
| 639 | close(fd); |
| 640 | return 0; |
| 641 | } |