/ Check-in [fa97d895]
Login

Many hyperlinks are disabled.
Use anonymous login to enable hyperlinks.

Overview
Comment:Updates to the showjournal.c utility in order to bring it up to version 3.
Downloads: Tarball | ZIP archive | SQL archive
Timelines: family | ancestors | descendants | both | trunk
Files: files | file ages | folders
SHA1: fa97d895463be6fd963c29b4525c2664193e36ec
User & Date: drh 2010-10-01 13:28:43
Context
2010-10-01
17:23
Add #ifndef USE_SYSTEM_SQLITE...#endif around the bundled SQLite library for the TCL bindings. check-in: 8d85584a user: drh tags: trunk
15:11
Not true: For a zeroblob, the Mem.z pointer can be null: The Mem.z pointer can never been NULL for a string or blob, even a zero-length string or blob. Assert this fact. Closed-Leaf check-in: efda3104 user: drh tags: mistake
13:28
Updates to the showjournal.c utility in order to bring it up to version 3. check-in: fa97d895 user: drh tags: trunk
2010-09-30
20:33
Merge experimental into trunk: Refactor the text-to-numeric conversion routines to work without zero-terminators and in UTF16 as well as UTF8. Avoid invalidating strings with doing affinity conversions. check-in: 07ee080e user: drh tags: trunk
Changes
Hide Diffs Side-by-Side Diffs Ignore Whitespace Patch

Changes to tool/showjournal.c.

     1      1   /*
     2      2   ** A utility for printing an SQLite database journal.
     3      3   */
     4      4   #include <stdio.h>
     5      5   #include <ctype.h>
     6         -#include <sys/types.h>
     7         -#include <sys/stat.h>
     8         -#include <fcntl.h>
     9         -#include <unistd.h>
    10      6   #include <stdlib.h>
    11         -
            7  +#include <string.h>
    12      8   
    13         -static int pagesize = 1024;
    14         -static int db = -1;
    15         -static int mxPage = 0;
            9  +/*
           10  +** state information
           11  +*/
           12  +static int pageSize = 1024;
           13  +static int sectorSize = 512;
           14  +static FILE *db = 0;
           15  +static int showPageContent = 0;
           16  +static int fileSize = 0;
           17  +static unsigned cksumNonce = 0;
    16     18   
           19  +/* Report a memory allocation error */
    17     20   static void out_of_memory(void){
    18     21     fprintf(stderr,"Out of memory...\n");
    19     22     exit(1);
    20     23   }
    21     24   
    22         -static print_page(int iPg){
    23         -  unsigned char *aData;
           25  +/*
           26  +** Read N bytes of memory starting at iOfst into space obtained
           27  +** from malloc().
           28  +*/
           29  +static char *read_content(int N, int iOfst){
           30  +  int got;
           31  +  char *pBuf = malloc(N);
           32  +  if( pBuf==0 ) out_of_memory();
           33  +  fseek(db, iOfst, SEEK_SET);
           34  +  got = fread(pBuf, 1, N, db);
           35  +  if( got<0 ){
           36  +    fprintf(stderr, "I/O error reading %d bytes from %d\n", N, iOfst);
           37  +    memset(pBuf, 0, N);
           38  +  }else if( got<N ){
           39  +    fprintf(stderr, "Short read: got only %d of %d bytes from %d\n",
           40  +                     got, N, iOfst);
           41  +    memset(&pBuf[got], 0, N-got);
           42  +  }
           43  +  return pBuf;
           44  +}
           45  +
           46  +/* Print a line of decode output showing a 4-byte integer.
           47  +*/
           48  +static unsigned print_decode_line(
           49  +  unsigned char *aData,      /* Content being decoded */
           50  +  int ofst, int nByte,       /* Start and size of decode */
           51  +  const char *zMsg           /* Message to append */
           52  +){
    24     53     int i, j;
    25         -  aData = malloc(pagesize);
    26         -  if( aData==0 ) out_of_memory();
    27         -  read(db, aData, pagesize);
    28         -  fprintf(stdout, "Page %d:\n", iPg);
    29         -  for(i=0; i<pagesize; i += 16){
    30         -    fprintf(stdout, " %03x: ",i);
    31         -    for(j=0; j<16; j++){
    32         -      fprintf(stdout,"%02x ", aData[i+j]);
           54  +  unsigned val = aData[ofst];
           55  +  char zBuf[100];
           56  +  sprintf(zBuf, " %03x: %02x", ofst, aData[ofst]);
           57  +  i = strlen(zBuf);
           58  +  for(j=1; j<4; j++){
           59  +    if( j>=nByte ){
           60  +      sprintf(&zBuf[i], "   ");
           61  +    }else{
           62  +      sprintf(&zBuf[i], " %02x", aData[ofst+j]);
           63  +      val = val*256 + aData[ofst+j];
    33     64       }
    34         -    for(j=0; j<16; j++){
    35         -      fprintf(stdout,"%c", isprint(aData[i+j]) ? aData[i+j] : '.');
    36         -    }
    37         -    fprintf(stdout,"\n");
           65  +    i += strlen(&zBuf[i]);
    38     66     }
           67  +  sprintf(&zBuf[i], "   %10u", val);
           68  +  printf("%s  %s\n", zBuf, zMsg);
           69  +  return val;
           70  +}
           71  +
           72  +/*
           73  +** Read and print a journal header.  Store key information (page size, etc)
           74  +** in global variables.
           75  +*/
           76  +static unsigned decode_journal_header(int iOfst){
           77  +  char *pHdr = read_content(64, iOfst);
           78  +  unsigned nPage;
           79  +  printf("Header at offset %d:\n", iOfst);
           80  +  print_decode_line(pHdr, 0, 4, "Header part 1 (3654616569)");
           81  +  print_decode_line(pHdr, 4, 4, "Header part 2 (547447767)");
           82  +  nPage =
           83  +  print_decode_line(pHdr, 8, 4, "page count");
           84  +  cksumNonce =
           85  +  print_decode_line(pHdr, 12, 4, "chksum nonce");
           86  +  print_decode_line(pHdr, 16, 4, "initial database size in pages");
           87  +  sectorSize =
           88  +  print_decode_line(pHdr, 20, 4, "sector size");
           89  +  pageSize =
           90  +  print_decode_line(pHdr, 24, 4, "page size");
           91  +  print_decode_line(pHdr, 28, 4, "zero");
           92  +  print_decode_line(pHdr, 32, 4, "zero");
           93  +  print_decode_line(pHdr, 36, 4, "zero");
           94  +  print_decode_line(pHdr, 40, 4, "zero");
           95  +  free(pHdr);
           96  +  return nPage;
           97  +}
           98  +
           99  +static void print_page(int iOfst){
          100  +  unsigned char *aData;
          101  +  char zTitle[50];
          102  +  aData = read_content(pageSize+8, iOfst);
          103  +  sprintf(zTitle, "page number for page at offset %d", iOfst);
          104  +  print_decode_line(aData, 0, 4, zTitle);
    39    105     free(aData);
    40    106   }
    41    107   
    42    108   int main(int argc, char **argv){
    43         -  struct stat sbuf;
    44         -  unsigned int u;
    45    109     int rc;
    46         -  unsigned char zBuf[10];
    47         -  unsigned char zBuf2[sizeof(u)];
          110  +  int nPage, cnt;
          111  +  int iOfst;
    48    112     if( argc!=2 ){
    49    113       fprintf(stderr,"Usage: %s FILENAME\n", argv[0]);
    50    114       exit(1);
    51    115     }
    52         -  db = open(argv[1], O_RDONLY);
    53         -  if( db<0 ){
          116  +  db = fopen(argv[1], "rb");
          117  +  if( db==0 ){
    54    118       fprintf(stderr,"%s: can't open %s\n", argv[0], argv[1]);
    55    119       exit(1);
    56    120     }
    57         -  read(db, zBuf, 8);
    58         -  if( zBuf[7]==0xd6 ){
    59         -    read(db, &u, sizeof(u));
    60         -    printf("Records in Journal: %u\n", u);
    61         -    read(db, &u, sizeof(u));
    62         -    printf("Magic Number: 0x%08x\n", u);
    63         -  }
    64         -  read(db, zBuf2, sizeof(zBuf2));
    65         -  u = zBuf2[0]<<24 | zBuf2[1]<<16 | zBuf2[2]<<8 | zBuf2[3];
    66         -  printf("Database Size: %u\n", u);
    67         -  while( read(db, zBuf2, sizeof(zBuf2))==sizeof(zBuf2) ){
    68         -    u = zBuf2[0]<<24 | zBuf2[1]<<16 | zBuf2[2]<<8 | zBuf2[3];
    69         -    print_page(u);
    70         -    if( zBuf[7]==0xd6 ){
    71         -      read(db, &u, sizeof(u));
    72         -      printf("Checksum: 0x%08x\n", u);
          121  +  fseek(db, 0, SEEK_END);
          122  +  fileSize = ftell(db);
          123  +  printf("journal file size: %d bytes\n", fileSize);
          124  +  fseek(db, 0, SEEK_SET);
          125  +  iOfst = 0;
          126  +  while( iOfst<fileSize ){
          127  +    cnt = nPage = (int)decode_journal_header(iOfst);
          128  +    if( cnt==0 ){
          129  +      cnt = (fileSize - sectorSize)/(pageSize+8);
          130  +    }
          131  +    iOfst += sectorSize;
          132  +    while( cnt && iOfst<fileSize ){
          133  +      print_page(iOfst);
          134  +      iOfst += pageSize+8;
    73    135       }
          136  +    iOfst = (iOfst/sectorSize + 1)*sectorSize;
    74    137     }
    75         -  close(db);
          138  +  fclose(db);
    76    139   }