磁盘格式化程序——测试程序

来源:互联网 发布:锐准电子狗软件 编辑:程序博客网 时间:2024/05/18 13:45
/*testfs.c*/ 

#include <stdio.h>
#include <string.h>
#include <fcntl.h>
#include "rt.h"

static char *program_name="testfs";
static int DEV=-1;
static char *device_name=NULL;
static char super_block_buffer[1024];
static char i_point_buffer[4096];
static char zone_map[10240];
static long got;

static void die(char *str){
 fprintf(stderr,"%s:",program_name);
 fprintf(stderr,str,device_name);
 fprintf(stderr,"\n");
 exit(8);
}

main()
{
 device_name="/dev/sdb1";
 DEV=open(device_name,O_RDWR);
 if(DEV<0)
  die("unable to open %s");
 
 //读超级块
 if(lseek(DEV,1024,SEEK_SET)!=1024)
  die("seek to super block failed!");
 got=read(DEV,super_block_buffer,1024);
 printf("s_magic = %d\n",(*(struct rt_super_block *)super_block_buffer).s_magic);
 printf("s_total_blocks_count = %d\n",(*(struct rt_super_block *)super_block_buffer).s_total_blocks_count);
 printf("s_ipoint_start_position = %d\n",(*(struct rt_super_block *)super_block_buffer).s_ipoint_start_position);
 printf("s_ipoints_count = %d\n",(*(struct rt_super_block *)super_block_buffer).s_ipoints_count);
 printf("s_ipoint_size = %d\n",(*(struct rt_super_block *)super_block_buffer).s_ipoint_size);

 printf("s_cluster_map_start_position = %d\n",(*(struct rt_super_block *)super_block_buffer).s_cluster_map_start_position);
 printf("s_cluster_map_count = %d\n",(*(struct rt_super_block *)super_block_buffer).s_cluster_map_count);
 printf("s_cluster_start_position = %d\n",(*(struct rt_super_block *)super_block_buffer).s_cluster_start_position);
 printf("s_cluster_size = %d\n",(*(struct rt_super_block *)super_block_buffer).s_cluster_size);
 printf("s_cluster_count = %d\n",(*(struct rt_super_block *)super_block_buffer).s_cluster_count);
 
 //读点索引区
 if(lseek(DEV,2048,SEEK_SET)!=2048)
  die("seek to ipoint zone failed!");
 got=read(DEV,i_point_buffer,4096);
 int i;
 for(i=0;i<1024;i++)
 {
  printf("Point[%d] = %d\n",i,(*(struct rt_ipoint *)i_point_buffer).i_latest_cluster[i]);
 }

 //读数据簇位图
 if(lseek(DEV,6144,SEEK_SET)!=6144)
  die("seek to cluster zone map failed!");
 got=read(DEV,zone_map,10240);
 for(i=0;i<130;i++)
 {
  printf("Byte[%d] = %c\n",i,zone_map[i]);
 }
 

}

 

 

 

/*rt.h*/

#include <linux/fs.h>
#include <linux/types.h>
#include <linux/magic.h>


#define RT_SUPER_MAGIC 0x0920
#define RT_BLOCK_SIZE 1024

#define CLUSTER_SIZE    50
#define DATAZONEBASE    16

#define RT_IPOINT_SIZE   4
#define RT_IPOINT_BLOCKS  4
#define RT_IPOINT_START_POSITION 2

#define RT_CLUSTERMAP_BLOCKS  10
#define RT_CLUSTERMAP_START_POSITION 6

#define RT_BLOCKS_PER_CLUSTER  50
#define RT_CLUSTER_START_POSITION 16

#define RT_IPOINTS_PER_BLOCK ((RT_BLOCK_SIZE)/RT_IPOINT_SIZE)
#define RT_IPOINTS_COUNT (RT_IPOINTS_PER_BLOCK*RT_IPOINT_BLOCKS)

struct rt_ipoint{
 __le32 i_latest_cluster[RT_IPOINTS_COUNT];
};

struct rt_super_block{
 __le64 s_total_blocks_count;         //the total blocks of the partation
 __le16 s_ipoint_start_position;   //the point index start position default 2
 __le16 s_ipoints_count;        //points count default 1024
 __le16 s_ipoint_num;              //point num default 0
 __u8   s_ipoint_size;             //point size default 4 Byte
 __le32 s_cluster_map_start_position;   //cluster bit map start position default 6(block) block=1024  byte;
 __le16 s_cluster_map_count;           //default 10 blocks
 __le16 s_cluster_start_position;    //cluster start position  default 16 (block)
 __u8   s_cluster_size;             //cluster size =50  blocks
 __le64 s_cluster_num;            //current cluster number
 __le64 s_cluster_count;          //clusters count
 __le64 s_create_time;      //rtfs create time
 __le64 s_write_time;       //the recent write time
 __le64 s_read_time;       //the read time
 __le16 s_magic;            //the magic of rtfs default  2336
};

 

///////////////////////////////////////////////////

编译命令:cc -g testfs.c -o testfs

调试命令:gdb testfs