Commit 1a77ebd1 authored by unknown's avatar unknown

Maria: a stronger checksum in the control file, to test integrity.

4 bytes, using my_checksum() (the old checksum was one byte and just
a sum of the bytes - that was before I saw we have my_checksum :)


storage/maria/ma_control_file.c:
  stronger checksum (4 bytes instead of 1, and using CRC instead of
  simple byte sum).
storage/maria/unittest/ma_control_file-t.c:
  Checksum is now 4 bytes (total length of control file is now 23),
  so LSN and LAST_LOGNO move.
parent 46922b51
...@@ -33,7 +33,7 @@ ...@@ -33,7 +33,7 @@
#define CONTROL_FILE_MAGIC_STRING_OFFSET 0 #define CONTROL_FILE_MAGIC_STRING_OFFSET 0
#define CONTROL_FILE_MAGIC_STRING_SIZE (sizeof(CONTROL_FILE_MAGIC_STRING)-1) #define CONTROL_FILE_MAGIC_STRING_SIZE (sizeof(CONTROL_FILE_MAGIC_STRING)-1)
#define CONTROL_FILE_CHECKSUM_OFFSET (CONTROL_FILE_MAGIC_STRING_OFFSET + CONTROL_FILE_MAGIC_STRING_SIZE) #define CONTROL_FILE_CHECKSUM_OFFSET (CONTROL_FILE_MAGIC_STRING_OFFSET + CONTROL_FILE_MAGIC_STRING_SIZE)
#define CONTROL_FILE_CHECKSUM_SIZE 1 #define CONTROL_FILE_CHECKSUM_SIZE 4
#define CONTROL_FILE_LSN_OFFSET (CONTROL_FILE_CHECKSUM_OFFSET + CONTROL_FILE_CHECKSUM_SIZE) #define CONTROL_FILE_LSN_OFFSET (CONTROL_FILE_CHECKSUM_OFFSET + CONTROL_FILE_CHECKSUM_SIZE)
#define CONTROL_FILE_LSN_SIZE LSN_STORE_SIZE #define CONTROL_FILE_LSN_SIZE LSN_STORE_SIZE
#define CONTROL_FILE_FILENO_OFFSET (CONTROL_FILE_LSN_OFFSET + CONTROL_FILE_LSN_SIZE) #define CONTROL_FILE_FILENO_OFFSET (CONTROL_FILE_LSN_OFFSET + CONTROL_FILE_LSN_SIZE)
...@@ -57,16 +57,6 @@ uint32 last_logno; ...@@ -57,16 +57,6 @@ uint32 last_logno;
*/ */
static int control_file_fd= -1; static int control_file_fd= -1;
static char simple_checksum(char *buffer, uint size)
{
/* TODO: improve this sum if we want */
char s= 0;
uint i;
for (i= 0; i<size; i++)
s+= buffer[i];
return s;
}
/* /*
Initialize control file subsystem Initialize control file subsystem
...@@ -80,7 +70,7 @@ static char simple_checksum(char *buffer, uint size) ...@@ -80,7 +70,7 @@ static char simple_checksum(char *buffer, uint size)
The format of the control file is: The format of the control file is:
4 bytes: magic string 4 bytes: magic string
1 byte: checksum of the following bytes 4 bytes: checksum of the following bytes
4 bytes: number of log where last checkpoint is 4 bytes: number of log where last checkpoint is
4 bytes: offset in log where last checkpoint is 4 bytes: offset in log where last checkpoint is
4 bytes: number of last log 4 bytes: number of last log
...@@ -190,9 +180,9 @@ CONTROL_FILE_ERROR ma_control_file_create_or_open() ...@@ -190,9 +180,9 @@ CONTROL_FILE_ERROR ma_control_file_create_or_open()
error= CONTROL_FILE_BAD_MAGIC_STRING; error= CONTROL_FILE_BAD_MAGIC_STRING;
goto err; goto err;
} }
if (simple_checksum(buffer + CONTROL_FILE_LSN_OFFSET, if (my_checksum(0, buffer + CONTROL_FILE_LSN_OFFSET,
CONTROL_FILE_SIZE - CONTROL_FILE_LSN_OFFSET) != CONTROL_FILE_SIZE - CONTROL_FILE_LSN_OFFSET) !=
buffer[CONTROL_FILE_CHECKSUM_OFFSET]) uint4korr(buffer + CONTROL_FILE_CHECKSUM_OFFSET))
{ {
/* TODO: store message "checksum mismatch" somewhere */ /* TODO: store message "checksum mismatch" somewhere */
error= CONTROL_FILE_BAD_CHECKSUM; error= CONTROL_FILE_BAD_CHECKSUM;
...@@ -268,9 +258,12 @@ int ma_control_file_write_and_force(const LSN checkpoint_lsn, uint32 logno, ...@@ -268,9 +258,12 @@ int ma_control_file_write_and_force(const LSN checkpoint_lsn, uint32 logno,
else else
int4store(buffer + CONTROL_FILE_FILENO_OFFSET, last_logno); int4store(buffer + CONTROL_FILE_FILENO_OFFSET, last_logno);
buffer[CONTROL_FILE_CHECKSUM_OFFSET]= {
simple_checksum(buffer + CONTROL_FILE_LSN_OFFSET, uint32 sum= (uint32)
CONTROL_FILE_SIZE - CONTROL_FILE_LSN_OFFSET); my_checksum(0, buffer + CONTROL_FILE_LSN_OFFSET,
CONTROL_FILE_SIZE - CONTROL_FILE_LSN_OFFSET);
int4store(buffer + CONTROL_FILE_CHECKSUM_OFFSET, sum);
}
if (my_pwrite(control_file_fd, buffer, sizeof(buffer), if (my_pwrite(control_file_fd, buffer, sizeof(buffer),
0, MYF(MY_FNABP | MY_WME)) || 0, MYF(MY_FNABP | MY_WME)) ||
......
...@@ -268,14 +268,14 @@ static int test_binary_content() ...@@ -268,14 +268,14 @@ static int test_binary_content()
RET_ERR_UNLESS((fd= my_open(file_name, RET_ERR_UNLESS((fd= my_open(file_name,
O_BINARY | O_RDWR, O_BINARY | O_RDWR,
MYF(MY_WME))) >= 0); MYF(MY_WME))) >= 0);
RET_ERR_UNLESS(my_read(fd, buffer, 20, MYF(MY_FNABP | MY_WME)) == 0); RET_ERR_UNLESS(my_read(fd, buffer, 23, MYF(MY_FNABP | MY_WME)) == 0);
RET_ERR_UNLESS(my_close(fd, MYF(MY_WME)) == 0); RET_ERR_UNLESS(my_close(fd, MYF(MY_WME)) == 0);
RET_ERR_UNLESS(create_or_open_file() == CONTROL_FILE_OK); RET_ERR_UNLESS(create_or_open_file() == CONTROL_FILE_OK);
i= uint3korr(buffer+9); i= uint3korr(buffer+12);
RET_ERR_UNLESS(i == LSN_FILE_NO(last_checkpoint_lsn)); RET_ERR_UNLESS(i == LSN_FILE_NO(last_checkpoint_lsn));
i= uint4korr(buffer+12); i= uint4korr(buffer+15);
RET_ERR_UNLESS(i == LSN_OFFSET(last_checkpoint_lsn)); RET_ERR_UNLESS(i == LSN_OFFSET(last_checkpoint_lsn));
i= uint4korr(buffer+16); i= uint4korr(buffer+19);
RET_ERR_UNLESS(i == last_logno); RET_ERR_UNLESS(i == last_logno);
RET_ERR_UNLESS(close_file() == 0); RET_ERR_UNLESS(close_file() == 0);
return 0; return 0;
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment