xref: /illumos-gate/usr/src/lib/libsqlite/tool/showjournal.c (revision 20a7641f9918de8574b8b3b47dbe35c4bfc78df1)
1 /*
2 ** A utility for printing an SQLite database journal.
3 */
4 #include <stdio.h>
5 #include <ctype.h>
6 #include <sys/types.h>
7 #include <sys/stat.h>
8 #include <fcntl.h>
9 #include <unistd.h>
10 #include <stdlib.h>
11 
12 
13 static int pagesize = 1024;
14 static int db = -1;
15 static int mxPage = 0;
16 
17 static void out_of_memory(void){
18   fprintf(stderr,"Out of memory...\n");
19   exit(1);
20 }
21 
22 static print_page(int iPg){
23   unsigned char *aData;
24   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]);
33     }
34     for(j=0; j<16; j++){
35       fprintf(stdout,"%c", isprint(aData[i+j]) ? aData[i+j] : '.');
36     }
37     fprintf(stdout,"\n");
38   }
39   free(aData);
40 }
41 
42 int main(int argc, char **argv){
43   struct stat sbuf;
44   unsigned int u;
45   int rc;
46   unsigned char zBuf[10];
47   unsigned char zBuf2[sizeof(u)];
48   if( argc!=2 ){
49     fprintf(stderr,"Usage: %s FILENAME\n", argv[0]);
50     exit(1);
51   }
52   db = open(argv[1], O_RDONLY);
53   if( db<0 ){
54     fprintf(stderr,"%s: can't open %s\n", argv[0], argv[1]);
55     exit(1);
56   }
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);
73     }
74   }
75   close(db);
76 }
77