我是靠谱客的博主 威武大神,最近开发中收集的这篇文章主要介绍磁盘格式化程序——测试程序,觉得挺不错的,现在分享给大家,希望可以做个参考。

概述

/*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 = %dn",(*(struct rt_super_block *)super_block_buffer).s_magic);
 printf("s_total_blocks_count = %dn",(*(struct rt_super_block *)super_block_buffer).s_total_blocks_count);
 printf("s_ipoint_start_position = %dn",(*(struct rt_super_block *)super_block_buffer).s_ipoint_start_position);
 printf("s_ipoints_count = %dn",(*(struct rt_super_block *)super_block_buffer).s_ipoints_count);
 printf("s_ipoint_size = %dn",(*(struct rt_super_block *)super_block_buffer).s_ipoint_size);

 printf("s_cluster_map_start_position = %dn",(*(struct rt_super_block *)super_block_buffer).s_cluster_map_start_position);
 printf("s_cluster_map_count = %dn",(*(struct rt_super_block *)super_block_buffer).s_cluster_map_count);
 printf("s_cluster_start_position = %dn",(*(struct rt_super_block *)super_block_buffer).s_cluster_start_position);
 printf("s_cluster_size = %dn",(*(struct rt_super_block *)super_block_buffer).s_cluster_size);
 printf("s_cluster_count = %dn",(*(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] = %dn",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] = %cn",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

最后

以上就是威武大神为你收集整理的磁盘格式化程序——测试程序的全部内容,希望文章能够帮你解决磁盘格式化程序——测试程序所遇到的程序开发问题。

如果觉得靠谱客网站的内容还不错,欢迎将靠谱客网站推荐给程序员好友。

本图文内容来源于网友提供,作为学习参考使用,或来自网络收集整理,版权属于原作者所有。
点赞(38)

评论列表共有 0 条评论

立即
投稿
返回
顶部