~drizzle-trunk/drizzle/development

« back to all changes in this revision

Viewing changes to extra/innochecksum.c

  • Committer: Monty Taylor
  • Date: 2008-07-05 18:10:38 UTC
  • mto: This revision was merged to the branch mainline in revision 63.
  • Revision ID: monty@inaugust.com-20080705181038-0ih0nnamu5qrut0y
Fixed prototypes. Cleaned define a little bit.

Show diffs side-by-side

added added

removed removed

Lines of Context:
27
27
#define _LARGEFILE_SOURCE
28
28
#define _LARGEFILE64_SOURCE
29
29
 
30
 
#ifndef __sun
31
30
#define _XOPEN_SOURCE 500 /* needed to include getopt.h on some platforms. */
32
 
#endif
33
31
 
34
32
#include <stdio.h>
35
33
#include <stdlib.h>
37
35
#include <sys/types.h>
38
36
#include <sys/stat.h>
39
37
#include <unistd.h>
40
 
#include <stdint.h>
41
 
#include <inttypes.h>
42
38
 
43
39
/* all of these ripped from InnoDB code from MySQL 4.0.22 */
44
40
#define UT_HASH_RANDOM_MASK     1463735687
54
50
/* command line argument to do page checks (that's it) */
55
51
/* another argument to specify page ranges... seek to right spot and go from there */
56
52
 
 
53
typedef unsigned long int ulint;
 
54
typedef unsigned char uchar;
 
55
 
57
56
/* innodb function in name; modified slightly to not have the ASM version (lots of #ifs that didn't apply) */
58
 
static uint32_t mach_read_from_4(unsigned char *b)
 
57
ulint mach_read_from_4(uchar *b)
59
58
{
60
 
  return( ((uint32_t)(b[0]) << 24)
61
 
          + ((uint32_t)(b[1]) << 16)
62
 
          + ((uint32_t)(b[2]) << 8)
63
 
          + (uint32_t)(b[3])
 
59
  return( ((ulint)(b[0]) << 24)
 
60
          + ((ulint)(b[1]) << 16)
 
61
          + ((ulint)(b[2]) << 8)
 
62
          + (ulint)(b[3])
64
63
          );
65
64
}
66
65
 
67
 
static uint32_t
68
 
ut_fold_uint32_t_pair(
 
66
ulint
 
67
ut_fold_ulint_pair(
69
68
/*===============*/
70
69
            /* out: folded value */
71
 
    uint32_t   n1, /* in: uint32_t */
72
 
    uint32_t   n2) /* in: uint32_t */
 
70
    ulint   n1, /* in: ulint */
 
71
    ulint   n2) /* in: ulint */
73
72
{
74
73
    return(((((n1 ^ n2 ^ UT_HASH_RANDOM_MASK2) << 8) + n1)
75
74
                        ^ UT_HASH_RANDOM_MASK) + n2);
76
75
}
77
76
 
78
 
static uint32_t
 
77
ulint
79
78
ut_fold_binary(
80
79
/*===========*/
81
80
            /* out: folded value */
82
 
    unsigned char*   str,    /* in: string of bytes */
83
 
    uint32_t   len)    /* in: length */
 
81
    uchar*   str,    /* in: string of bytes */
 
82
    ulint   len)    /* in: length */
84
83
{
85
 
    uint32_t   i;
86
 
    uint32_t   fold= 0;
 
84
    ulint   i;
 
85
    ulint   fold= 0;
87
86
 
88
87
    for (i= 0; i < len; i++)
89
88
    {
90
 
      fold= ut_fold_uint32_t_pair(fold, (uint32_t)(*str));
 
89
      fold= ut_fold_ulint_pair(fold, (ulint)(*str));
91
90
 
92
91
      str++;
93
92
    }
95
94
    return(fold);
96
95
}
97
96
 
98
 
static uint32_t
 
97
ulint
99
98
buf_calc_page_new_checksum(
100
99
/*=======================*/
101
100
               /* out: checksum */
102
 
    unsigned char*    page) /* in: buffer page */
 
101
    uchar*    page) /* in: buffer page */
103
102
{
104
 
    uint32_t checksum;
 
103
    ulint checksum;
105
104
 
106
105
    /* Since the fields FIL_PAGE_FILE_FLUSH_LSN and ..._ARCH_LOG_NO
107
106
    are written outside the buffer pool to the first pages of data
120
119
    return(checksum);
121
120
}
122
121
 
123
 
static uint32_t
 
122
ulint
124
123
buf_calc_page_old_checksum(
125
124
/*=======================*/
126
125
               /* out: checksum */
127
 
    unsigned char*    page) /* in: buffer page */
 
126
    uchar*    page) /* in: buffer page */
128
127
{
129
 
    uint32_t checksum;
 
128
    ulint checksum;
130
129
 
131
130
    checksum= ut_fold_binary(page, FIL_PAGE_FILE_FLUSH_LSN);
132
131
 
139
138
int main(int argc, char **argv)
140
139
{
141
140
  FILE *f;                     /* our input file */
142
 
  unsigned char *p;                     /* storage of pages read */
 
141
  uchar *p;                     /* storage of pages read */
143
142
  int bytes;                   /* bytes read count */
144
 
  uint32_t ct;                    /* current page number (0 based) */
 
143
  ulint ct;                    /* current page number (0 based) */
145
144
  int now;                     /* current time */
146
145
  int lastt;                   /* last time */
147
 
  uint32_t oldcsum, oldcsumfield, csum, csumfield, logseq, logseqfield; /* uint32_ts for checksum storage */
 
146
  ulint oldcsum, oldcsumfield, csum, csumfield, logseq, logseqfield; /* ulints for checksum storage */
148
147
  struct stat st;              /* for stat, if you couldn't guess */
149
 
  uint64_t size; /* size of file (has to be 64 bits) */
150
 
  uint32_t pages;                 /* number of pages in file */
151
 
  uint32_t start_page= 0, end_page= 0, use_end_page= 0; /* for starting and ending at certain pages */
 
148
  unsigned long long int size; /* size of file (has to be 64 bits) */
 
149
  ulint pages;                 /* number of pages in file */
 
150
  ulint start_page= 0, end_page= 0, use_end_page= 0; /* for starting and ending at certain pages */
152
151
  off_t offset= 0;
153
152
  int just_count= 0;          /* if true, just print page count */
154
153
  int verbose= 0;
220
219
  pages= size / UNIV_PAGE_SIZE;
221
220
  if (just_count)
222
221
  {
223
 
    printf("%u\n", pages);
 
222
    printf("%lu\n", pages);
224
223
    return 0;
225
224
  }
226
225
  else if (verbose)
227
226
  {
228
 
    printf("file %s= %"PRIu64" bytes (%u pages)...\n", argv[1], size, pages);
229
 
    printf("checking pages in range %u to %u\n", start_page, use_end_page ? end_page : (pages - 1));
 
227
    printf("file %s= %llu bytes (%lu pages)...\n", argv[1], size, pages);
 
228
    printf("checking pages in range %lu to %lu\n", start_page, use_end_page ? end_page : (pages - 1));
230
229
  }
231
230
 
232
231
  /* open the file for reading */
257
256
  }
258
257
 
259
258
  /* allocate buffer for reading (so we don't realloc every time) */
260
 
  p= (unsigned char *)malloc(UNIV_PAGE_SIZE);
 
259
  p= (uchar *)malloc(UNIV_PAGE_SIZE);
261
260
 
262
261
  /* main checksumming loop */
263
262
  ct= start_page;
276
275
    logseq= mach_read_from_4(p + FIL_PAGE_LSN + 4);
277
276
    logseqfield= mach_read_from_4(p + UNIV_PAGE_SIZE - FIL_PAGE_END_LSN_OLD_CHKSUM + 4);
278
277
    if (debug)
279
 
      printf("page %u: log sequence number: first = %u; second = %u\n", ct, logseq, logseqfield);
 
278
      printf("page %lu: log sequence number: first = %lu; second = %lu\n", ct, logseq, logseqfield);
280
279
    if (logseq != logseqfield)
281
280
    {
282
 
      fprintf(stderr, "page %u invalid (fails log sequence number check)\n", ct);
 
281
      fprintf(stderr, "page %lu invalid (fails log sequence number check)\n", ct);
283
282
      return 1;
284
283
    }
285
284
 
287
286
    oldcsum= buf_calc_page_old_checksum(p);
288
287
    oldcsumfield= mach_read_from_4(p + UNIV_PAGE_SIZE - FIL_PAGE_END_LSN_OLD_CHKSUM);
289
288
    if (debug)
290
 
      printf("page %u: old style: calculated = %u; recorded = %u\n", ct, oldcsum, oldcsumfield);
 
289
      printf("page %lu: old style: calculated = %lu; recorded = %lu\n", ct, oldcsum, oldcsumfield);
291
290
    if (oldcsumfield != mach_read_from_4(p + FIL_PAGE_LSN) && oldcsumfield != oldcsum)
292
291
    {
293
 
      fprintf(stderr, "page %u invalid (fails old style checksum)\n", ct);
 
292
      fprintf(stderr, "page %lu invalid (fails old style checksum)\n", ct);
294
293
      return 1;
295
294
    }
296
295
 
298
297
    csum= buf_calc_page_new_checksum(p);
299
298
    csumfield= mach_read_from_4(p + FIL_PAGE_SPACE_OR_CHKSUM);
300
299
    if (debug)
301
 
      printf("page %u: new style: calculated = %u; recorded = %u\n", ct, csum, csumfield);
 
300
      printf("page %lu: new style: calculated = %lu; recorded = %lu\n", ct, csum, csumfield);
302
301
    if (csumfield != 0 && csum != csumfield)
303
302
    {
304
 
      fprintf(stderr, "page %u invalid (fails new style checksum)\n", ct);
 
303
      fprintf(stderr, "page %lu invalid (fails new style checksum)\n", ct);
305
304
      return 1;
306
305
    }
307
306
 
319
318
        if (!lastt) lastt= now;
320
319
        if (now - lastt >= 1)
321
320
        {
322
 
          printf("page %u okay: %.3f%% done\n", (ct - 1), (float) ct / pages * 100);
 
321
          printf("page %lu okay: %.3f%% done\n", (ct - 1), (float) ct / pages * 100);
323
322
          lastt= now;
324
323
        }
325
324
      }