#include <stdio.h>
#include <string.h>
#include "init.h"
static void update_super_block(void) //写超级块
{
fp=fopen("./Ext2","r+");
fseek(fp,DISK_START,SEEK_SET);
fwrite(sb_block,SB_SIZE,1,fp);
fflush(fp); //立刻将缓冲区的内容输出,保证磁盘内存数据的一致性
}
static void reload_super_block(void) //读超级块
{
fseek(fp,DISK_START,SEEK_SET);
fread(sb_block,SB_SIZE,1,fp);//读取内容到超级块缓冲区中
}
static void update_group_desc(void) //写组描述符
{
fp=fopen("./Ext2","r+");
fseek(fp,GDT_START,SEEK_SET);
fwrite(gdt,GD_SIZE,1,fp);
fflush(fp);
}
static void reload_group_desc(void) // 读组描述符
{
fseek(fp,GDT_START,SEEK_SET);
fread(gdt,GD_SIZE,1,fp);
}
static void update_inode_entry(unsigned short i) // 写第i个inode
{
fp=fopen("./Ext2","r+");
fseek(fp,INODE_TABLE+(i-1)*INODE_SIZE,SEEK_SET);
fwrite(inode_area,INODE_SIZE,1,fp);
fflush(fp);
}
static void reload_inode_entry(unsigned short i) // 读第i个inode
{
fseek(fp,INODE_TABLE+(i-1)*INODE_SIZE,SEEK_SET);
fread(inode_area,INODE_SIZE,1,fp);
}
static void update_dir(unsigned short i) // 写第i个 数据块
{
fp=fopen("./Ext2","r+");
fseek(fp,DATA_BLOCK+i*BLOCK_SIZE,SEEK_SET);
fwrite(dir,BLOCK_SIZE,1,fp);
fflush(fp);
}
static void reload_dir(unsigned short i) // 读第i个 数据块
{
fseek(fp,DATA_BLOCK+i*BLOCK_SIZE,SEEK_SET);
fread(dir,BLOCK_SIZE,1,fp);
//fclose(fp);
}
static void update_block_bitmap(void) //写block位图
{
fp=fopen("./Ext2","r+");
fseek(fp,BLOCK_BITMAP,SEEK_SET);
fwrite(bitbuf,BLOCK_SIZE,1,fp);
fflush(fp);
}
static void reload_block_bitmap(void) //读block位图
{
fseek(fp,BLOCK_BITMAP,SEEK_SET);
fread(bitbuf,BLOCK_SIZE,1,fp);
}
static void update_inode_bitmap(void) //写inode位图
{
fp=fopen("./Ext2","r+");
fseek(fp,INODE_BITMAP,SEEK_SET);
fwrite(ibuf,BLOCK_SIZE,1,fp);
fflush(fp);
}
static void reload_inode_bitmap(void) // 读inode位图
{
fseek(fp,INODE_BITMAP,SEEK_SET);
fread(ibuf,BLOCK_SIZE,1,fp);
}
static void update_block(unsigned short i) // 写第i个数据块
{
fp=fopen("./Ext2","r+");
fseek(fp,DATA_BLOCK+i*BLOCK_SIZE,SEEK_SET);
//fseek(fp,0,SEEK_SET);
fwrite(Buffer,BLOCK_SIZE,1,fp);
fflush(fp);
}
static void reload_block(unsigned short i) // 读第i个数据块
{
fseek(fp,DATA_BLOCK+i*BLOCK_SIZE,SEEK_SET);
fread(Buffer,BLOCK_SIZE,1,fp);
}
static int alloc_block(void) // 分配一个数据块,返回数据块号
{
//bitbuf共有512个字节,表示4096个数据块。根据last_alloc_block/8计算它在bitbuf的哪一个字节
unsigned short cur=last_alloc_block;
//printf("cur: %d\n",cur);
unsigned char con=128; // 1000 0000b
int flag=0;
if(gdt[0].bg_free_blocks_count==0)
{
printf("There is no block to be alloced!\n");
return(0);
}
reload_block_bitmap();
cur/=8;
while(bitbuf[cur]==255)//该字节的8个bit都已有数据
{
if(cur==511)cur=0; //最后一个字节也已经满,从头开始寻找
else cur++;
}
while(bitbuf[cur]&con) //在一个字节中找具体的某一个bit
{
con=con/2;
flag++;
}
bitbuf[cur]=bitbuf[cur]+con;
last_alloc_block=cur*8+flag;
update_block_bitmap();
gdt[0].bg_free_blocks_count--;
update_group_desc();
return last_alloc_block;
}
static int get_inode(void) // 分配一个inode
{
unsigned short cur=last_alloc_inode;
unsigned char con=128;
int flag=0;
if(gdt[0].bg_free_inodes_count==0)
{
printf("There is no Inode to be alloced!\n");
return 0;
}
reload_inode_bitmap();
cur=(cur-1)/8; //第一个标号是1,但是存储是从0开始的
//printf("%s",)
while(ibuf[cur]==255) //先看该字节的8个位是否已经填满
{
if(cur==511)cur=0;
else cur++;
}
while(ibuf[cur]&con) //再看某个字节的具体哪一位没有被占用
{
con=con/2;
flag++;
}
ibuf[cur]=ibuf[cur]+con;
last_alloc_inode=cur*8+flag+1;
update_inode_bitmap();
gdt[0].bg_free_inodes_count--;
update_group_desc();
//printf("%d\n",last_alloc_inode);
return last_alloc_inode;
}
//当前目录中查找文件或目录为tmp,并得到该文件的 inode 号,它在上级目录中的数据块号以及数据块中目录的项号
static unsigned short reserch_file(char tmp[9],int file_type,unsigned short *inode_num,
unsigned short *block_num,unsigned short *dir_num)
{
unsigned short j,k;
reload_inode_entry(current_dir); //进入当前目录
j=0;
while(j<inode_area[0].i_blocks)
{
reload_dir(inode_area[0].i_block[j]);
k=0;
while(k<32)
{
if(!dir[k].inode||dir[k].file_type!=file_type||strcmp(dir[k].name,tmp))
{
k++;
}
else
{
*inode_num=dir[k].inode;
*block_num=j;
*dir_num=k;
return 1;
}
}
j++;
}
return 0;
}
/*为新增目录或文件分配 dir_entry
对于新增文件,只需分配一个inode号
对于新增目录,除了inode号外,还需要分配数据区存储.和..两个目录项*/
static void dir_prepare(unsigned short tmp,unsigned short len,int type)
{
reload_inode_entry(tmp);
if(type==2) // 目录
{
inode_area[0].i_size=32;
inode_area[0].i_blocks=1;
inode_area[0].i_block[0]=alloc_block();
//printf("alloc %d\n",inode_area[0].i_block[0]);
dir[0].inode=tmp;
dir[1].inode=current_dir;
dir[0].name_len=len;
dir[1].name_len=current_dirlen;
dir[0].file_type=dir[1].file_type=2;
for(type=2;type<32;type++)
dir[type].inode=0;
strcpy(dir[0].name,".");
strcpy(dir[1].name,"..");
update_dir(inode_area[0].i_block[0]);
inode_area[0].i_mode=01006;
}
else
{
inode_area[0].i_size=0;
inode_area[0].i_blocks=0;
inode_area[0].i_mode=0407;
}
update_inode_entry(tmp);
}
//删除一个块号
static void remove_block(unsigned short del_num)
{
unsigned short tmp;
tmp=del_num/8;
reload_block_bitmap();
switch(del_num%8) // 更新block位图 将具体的位置为0
{
case 0:bitbuf[tmp]=bitbuf[tmp]&127;break; // bitbuf[tmp] & 0111 1111b
case 1:bitbuf[tmp]=bitbuf[tmp]&191;break; //bitbuf[tmp] & 1011 1111b
case 2:bitbuf[tmp]=bitbuf[tmp]&223;break; //bitbuf[tmp] & 1101 1111b
case 3:bitbuf[tmp]=bitbuf[tmp]&239;break; //bitbbuf[tmp] & 1110 1111b
case 4:bitbuf[tmp]=bitbuf[tmp]&247;break; //bitbuf[tmp] & 1111 0111b
case 5:bitbuf[tmp]=bitbuf[tmp]&251;break; //bitbuf[tmp] & 1111 1011b
case 6:bitbuf[tmp]=bitbuf[tmp]&253;break; //bitbuf[tmp] & 1111 1101b
case 7:bitbuf[tmp]=bitbuf[tmp]&254;break; // bitbuf[tmp] & 1111 1110b
}
update_block_bitmap();
gdt[0].bg_free_blocks_count++;
update_group_desc();
}
//删除一个inode 号
static void remove_inode(unsigned short del_num)
{
unsigned short tmp;
tmp=(del_num-1)/8;
reload_inode_bitmap();
switch((del_num-1)%8)//更改block位图
{
case 0:bitbuf[tmp]=bitbuf[tmp]&127;break;
case 1:bitbuf[tmp]=bitbuf[tmp]&191;break;
case 2:bitbuf[tmp]=bitbuf[tmp]&223;break;
case 3:bitbuf[tmp]=bitbuf[tmp]&239;break;
case 4:bitbuf[tmp]=bitbuf[tmp]&247;break;
case 5:bitbuf[tmp]=bitbuf[tmp]&251;break;
case 6:bitbuf[tmp]=bitbuf[tmp]&253;break;
case 7:bitbuf[tmp]=bitbuf[tmp]&254;break;
}
update_inode_bitmap();
gdt[0].bg_free_inodes_count++;
update_group_desc();
}
//在打开文件表中查找是否已打开文件
static unsigned short search_file(unsigned short Inode)
{
unsigned short fopen_table_point=0;
while(fopen_table_point<16&&fopen_table[fopen_table_point++]!=Inode);
if(fopen_table_point==16)
{
return 0;
}
return 1;
}
void initialize_disk(void) //初始化磁盘
{
int i=0;
printf("Creating the ext2 file system\n");
printf("Please wait ");
while(i<1)
{
printf("... ");
++i;
没有合适的资源?快使用搜索试试~ 我知道了~
温馨提示
软件开发设计:应用软件开发、系统软件开发、移动应用开发、网站开发C++、Java、python、web、C#等语言的项目开发与学习资料 硬件与设备:单片机、EDA、proteus、RTOS、包括计算机硬件、服务器、网络设备、存储设备、移动设备等 操作系统:LInux、树莓派、安卓开发、微机操作系统、网络操作系统、分布式操作系统等。此外,还有嵌入式操作系统、智能操作系统等。 网络与通信:数据传输、信号处理、网络协议、网络与通信硬件、网络安全网络与通信是一个非常广泛的领域,它涉及到计算机科学、电子工程、数学等多个学科的知识。 云计算与大数据:包括云计算平台、大数据分析、人工智能、机器学习等,云计算是一种基于互联网的计算方式,通过这种方式,共享的软硬件资源和信息可以按需提供给计算机和其他设备
资源推荐
资源详情
资源评论
收起资源包目录
本项目用C语言在linux下实现ext2文件系统的基本功能.zip (5个子文件)
cm
Ext2 2.25MB
main.h 530B
init.h 4KB
init.c 29KB
main.c 6KB
共 5 条
- 1
资源评论
妄北y
- 粉丝: 1w+
- 资源: 1万+
上传资源 快速赚钱
- 我的内容管理 展开
- 我的资源 快来上传第一个资源
- 我的收益 登录查看自己的收益
- 我的积分 登录查看自己的积分
- 我的C币 登录后查看C币余额
- 我的收藏
- 我的下载
- 下载帮助
最新资源
- 基于MFC的校园导航程序(使用最短路径dijkstra算法).rar
- Android Studio android APP 视频作为视图背景需要源代码或想了解其实现原理的可以私心我
- com.ZeroneGames.GreenProject.apk
- Python自动化开发入门教程
- 4399GameSem_116_13955_207551_6.apk
- python 3.9.19源码编译包
- php-8.2.18-Win32-vs16-x64.rar
- 字节跳动青训营-抖音项目
- SQL资料手册,语句教程,高级查询语句语法
- 上位机和串口建立 Modbus 协议进行数据传输,并使用 Mysql 数据库存储,能够实现实时温湿度显示和动态变化曲线,历史数据
资源上传下载、课程学习等过程中有任何疑问或建议,欢迎提出宝贵意见哦~我们会及时处理!
点击此处反馈
安全验证
文档复制为VIP权益,开通VIP直接复制
信息提交成功