C语言版Mysql存储块(page)格式读取工具(源代码)

本文原创为freas_1990,,转载请标明出处:

C版本的Mysql存储块格式读取工具源代码如下:

其中mysql块存储采用大端字节,所以需要做一定的转换,C语言指针强力转换方式如下:

#include <stdio.h>#include <stdlib.h>int readpage(unsigned char * page_hdr,int page_off,FILE *fd) {int ret = -1;if((ret = fseek(fd,page_off,SEEK_SET)) != 0) {//fseek error may not be caught successfully hereprintf("fseek failed\n");exit(-1);}printf("now from our calclation page_off is %d\n",page_off);if(!(ret = fread(page_hdr,62,1,fd))) {printf("read page failed! ret is %d\n",ret);return -1;}return 1;}int main() {unsigned char page_hdr_tmp[62];unsigned char * tmp;unsigned int page_offset = 0;unsigned short page_type = 0;unsigned int tab_space_id = 0;unsigned short page_level = 0;int ret = -1;int page_count = 0;int filesize = 0;FILE *fd;if(!freopen("C:\\MinGW\\bin\\log.txt", "w", stdout)) {printf("log file created failed!\n");}if(!(fd = fopen("C:\\MinGW\\bin\\payment.ibd","rb"))) {printf("fopen failed!\n");return -1;} if((ret = fseek(fd,0,SEEK_END)) != 0) {printf("fseek failed\n");exit(-1);}filesize = ftell(fd);printf("filesize is %d\n",filesize);while (page_count < filesize/1024/16) {printf("processing the %d [rd] page\n",page_count);if(ret = readpage(page_hdr_tmp, page_count*16*1024, fd)) {page_count++;} else {printf("readpage failed!\n");system("pause");exit -1;}tmp = (unsigned char *)&page_offset;tmp[0] = page_hdr_tmp[7];tmp[1] = page_hdr_tmp[6];tmp[2] = page_hdr_tmp[5];tmp[3] = page_hdr_tmp[4];tmp = (unsigned char *)&page_type;tmp[0] = page_hdr_tmp[25];tmp[1] = page_hdr_tmp[24];tmp = (unsigned char *)&tab_space_id;tmp[0] = page_hdr_tmp[37];tmp[1] = page_hdr_tmp[36];tmp[2] = page_hdr_tmp[35];tmp[3] = page_hdr_tmp[34];tmp = (unsigned char *)&page_level;tmp[0] = page_hdr_tmp[61];tmp[1] = page_hdr_tmp[60];printf("page_offset is %d\n",page_offset);printf("page_type is %x\n",page_type);printf("tab_space_id is %x\n",tab_space_id);printf("page_level is %x\n\n",page_level);}fclose(fd);system("pause");return 0;}

我们打开程序输出的log来看,信息如下:

filesize is 10485760processing the 0 [rd] pagenow from our calclation page_off is 0page_offset is 0page_type is 8tab_space_id is 11page_level is bprocessing the 1 [rd] pagenow from our calclation page_off is 16384page_offset is 1page_type is 5tab_space_id is 11page_level is 0processing the 2 [rd] pagenow from our calclation page_off is 32768page_offset is 2page_type is 3tab_space_id is 11page_level is 0processing the 3 [rd] pagenow from our calclation page_off is 49152page_offset is 3page_type is 45bftab_space_id is 11page_level is 0

其中page_type的45bf代表的是索引节点。而page_level是0,意思是,索引节点的层级为0。

到了第75 [rd]块的时候,所有块都是空白块了。

processing the 74 [rd] pagenow from our calclation page_off is 1212416page_offset is 74page_type is 45bftab_space_id is 11page_level is 0processing the 75 [rd] pagenow from our calclation page_off is 1228800page_offset is 0page_type is 0tab_space_id is 0page_level is 0processing the 76 [rd] pagenow from our calclation page_off is 1245184page_offset is 0page_type is 0tab_space_id is 0page_level is 0测试的ibd文件是mysql 5.6自带的sakila库下的payment.ibd。

只能昏昏沉沉地沿着青草和泥土的气息前进。

C语言版Mysql存储块(page)格式读取工具(源代码)

相关文章:

你感兴趣的文章:

标签云: