Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(204)

Side by Side Diff: third_party/sqlite/sqlite-src-3080704/tool/showjournal.c

Issue 2363173002: [sqlite] Remove obsolete reference version 3.8.7.4. (Closed)
Patch Set: Created 4 years, 2 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
OLDNEW
(Empty)
1 /*
2 ** A utility for printing an SQLite database journal.
3 */
4 #include <stdio.h>
5 #include <ctype.h>
6 #include <stdlib.h>
7 #include <string.h>
8
9 /*
10 ** state information
11 */
12 static int pageSize = 1024;
13 static int sectorSize = 512;
14 static FILE *db = 0;
15 static int fileSize = 0;
16 static unsigned cksumNonce = 0;
17
18 /* Report a memory allocation error */
19 static void out_of_memory(void){
20 fprintf(stderr,"Out of memory...\n");
21 exit(1);
22 }
23
24 /*
25 ** Read N bytes of memory starting at iOfst into space obtained
26 ** from malloc().
27 */
28 static unsigned char *read_content(int N, int iOfst){
29 int got;
30 unsigned char *pBuf = malloc(N);
31 if( pBuf==0 ) out_of_memory();
32 fseek(db, iOfst, SEEK_SET);
33 got = fread(pBuf, 1, N, db);
34 if( got<0 ){
35 fprintf(stderr, "I/O error reading %d bytes from %d\n", N, iOfst);
36 memset(pBuf, 0, N);
37 }else if( got<N ){
38 fprintf(stderr, "Short read: got only %d of %d bytes from %d\n",
39 got, N, iOfst);
40 memset(&pBuf[got], 0, N-got);
41 }
42 return pBuf;
43 }
44
45 /* Print a line of decode output showing a 4-byte integer.
46 */
47 static unsigned print_decode_line(
48 const unsigned char *aData, /* Content being decoded */
49 int ofst, int nByte, /* Start and size of decode */
50 const char *zMsg /* Message to append */
51 ){
52 int i, j;
53 unsigned val = aData[ofst];
54 char zBuf[100];
55 sprintf(zBuf, " %05x: %02x", ofst, aData[ofst]);
56 i = strlen(zBuf);
57 for(j=1; j<4; j++){
58 if( j>=nByte ){
59 sprintf(&zBuf[i], " ");
60 }else{
61 sprintf(&zBuf[i], " %02x", aData[ofst+j]);
62 val = val*256 + aData[ofst+j];
63 }
64 i += strlen(&zBuf[i]);
65 }
66 sprintf(&zBuf[i], " %10u", val);
67 printf("%s %s\n", zBuf, zMsg);
68 return val;
69 }
70
71 /*
72 ** Read and print a journal header. Store key information (page size, etc)
73 ** in global variables.
74 */
75 static unsigned decode_journal_header(int iOfst){
76 unsigned char *pHdr = read_content(64, iOfst);
77 unsigned nPage;
78 printf("Header at offset %d:\n", iOfst);
79 print_decode_line(pHdr, 0, 4, "Header part 1 (3654616569)");
80 print_decode_line(pHdr, 4, 4, "Header part 2 (547447767)");
81 nPage =
82 print_decode_line(pHdr, 8, 4, "page count");
83 cksumNonce =
84 print_decode_line(pHdr, 12, 4, "chksum nonce");
85 print_decode_line(pHdr, 16, 4, "initial database size in pages");
86 sectorSize =
87 print_decode_line(pHdr, 20, 4, "sector size");
88 pageSize =
89 print_decode_line(pHdr, 24, 4, "page size");
90 print_decode_line(pHdr, 28, 4, "zero");
91 print_decode_line(pHdr, 32, 4, "zero");
92 print_decode_line(pHdr, 36, 4, "zero");
93 print_decode_line(pHdr, 40, 4, "zero");
94 free(pHdr);
95 return nPage;
96 }
97
98 static void print_page(int iOfst){
99 unsigned char *aData;
100 char zTitle[50];
101 aData = read_content(pageSize+8, iOfst);
102 sprintf(zTitle, "page number for page at offset %d", iOfst);
103 print_decode_line(aData-iOfst, iOfst, 4, zTitle);
104 free(aData);
105 }
106
107 int main(int argc, char **argv){
108 int nPage, cnt;
109 int iOfst;
110 if( argc!=2 ){
111 fprintf(stderr,"Usage: %s FILENAME\n", argv[0]);
112 exit(1);
113 }
114 db = fopen(argv[1], "rb");
115 if( db==0 ){
116 fprintf(stderr,"%s: can't open %s\n", argv[0], argv[1]);
117 exit(1);
118 }
119 fseek(db, 0, SEEK_END);
120 fileSize = ftell(db);
121 printf("journal file size: %d bytes\n", fileSize);
122 fseek(db, 0, SEEK_SET);
123 iOfst = 0;
124 while( iOfst<fileSize ){
125 cnt = nPage = (int)decode_journal_header(iOfst);
126 if( cnt==0 ){
127 cnt = (fileSize - sectorSize)/(pageSize+8);
128 }
129 iOfst += sectorSize;
130 while( cnt && iOfst<fileSize ){
131 print_page(iOfst);
132 iOfst += pageSize+8;
133 }
134 iOfst = (iOfst/sectorSize + 1)*sectorSize;
135 }
136 fclose(db);
137 return 0;
138 }
OLDNEW
« no previous file with comments | « third_party/sqlite/sqlite-src-3080704/tool/showdb.c ('k') | third_party/sqlite/sqlite-src-3080704/tool/showstat4.c » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698