📄 alloc.h
字号:
void update_group_desc()//更新组描述符,超级块super_block和组描述符group_desc重合
{
fseek(fp,DISK_START,SEEK_SET);
fwrite(super_block,BLOCK_SIZE,1,fp);
}
void reload_group_desc()//载入组描述符
{
fseek(fp,DISK_START,SEEK_SET);
fread(super_block,BLOCK_SIZE,1,fp);
}
void update_inode_bitmap()//更新inode位图
{
fseek(fp,INODE_BITMAP,SEEK_SET);
fwrite(bitbuf,BLOCK_SIZE,1,fp);
}
void reload_inode_bitmap()//载入inode位图
{
fseek(fp,INODE_BITMAP,SEEK_SET);
fread(bitbuf,BLOCK_SIZE,1,fp);
}
void update_block_bitmap()//更新block位图
{
fseek(fp,BLOCK_BITMAP,SEEK_SET);
fwrite(bitbuf,BLOCK_SIZE,1,fp);
}
void reload_block_bitmap()//载入block位图
{
fseek(fp,BLOCK_BITMAP,SEEK_SET);
fread(bitbuf,BLOCK_SIZE,1,fp);
}
void update_inode_entry(unsigned short i)//更新第i个inode入口
{
fseek(fp,INODE_TABLE+(i-1)*INODE_SIZE,SEEK_SET);
fwrite(inode_area,INODE_SIZE,1,fp);
}
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);
}
void reload_dir(unsigned short i)//更新第i个目录
{
fseek(fp,DATA_BLOCK+i*BLOCK_SIZE,SEEK_SET);
fread(dir,BLOCK_SIZE,1,fp);
}
void update_dir(unsigned short i)//载入第i个目录
{
fseek(fp,DATA_BLOCK+i*BLOCK_SIZE,SEEK_SET);
fwrite(dir,BLOCK_SIZE,1,fp);
}
void reload_block(unsigned short i)//载入第i个数据块
{
fseek(fp,DATA_BLOCK+i*BLOCK_SIZE,SEEK_SET);
fread(Buffer,BLOCK_SIZE,1,fp);
}
void update_block(unsigned short i)//更新第i个数据块
{
fseek(fp,DATA_BLOCK+i*BLOCK_SIZE,SEEK_SET);
fwrite(Buffer,BLOCK_SIZE,1,fp);
}
void reload_index_block(unsigned short i)//载入索引数据块
{
fseek(fp,DATA_BLOCK+i*BLOCK_SIZE,SEEK_SET);
fread(index_buf,BLOCK_SIZE,1,fp);
}
void update_index_block(unsigned short i)//更新索引数据块
{
fseek(fp,DATA_BLOCK+i*BLOCK_SIZE,SEEK_SET);
fwrite(index_buf,BLOCK_SIZE,1,fp);
}
void reload_index1_block(unsigned short i)//载入二级索引数据块
{
fseek(fp,DATA_BLOCK+i*BLOCK_SIZE,SEEK_SET);
fread(index_buf1,BLOCK_SIZE,1,fp);
}
void update_index1_block(unsigned short i)//更新二级索引数据块
{
fseek(fp,DATA_BLOCK+i*BLOCK_SIZE,SEEK_SET);
fwrite(index_buf1,BLOCK_SIZE,1,fp);
}
unsigned short alloc_block()//分配一个数据块,返回数据块号;
{
unsigned short cur=last_alloc_block;
unsigned char con=128;
int flag=0;
if(super_block[0].bg_free_blocks_count==0)
{
printf("There is no block to be alloced!\n");
return(0);
}
reload_block_bitmap();
cur=cur/8;
while(bitbuf[cur]==255)
{
if(cur==511)cur=0;
else cur++;
}
//printf("block: %x %d\n",bitbuf[cur],cur);
while(bitbuf[cur]&con)
{
con=con/2;
flag++;
}
bitbuf[cur]=bitbuf[cur]+con;
last_alloc_block=cur*8+flag;
//printf(" %x %d\n",bitbuf[cur],last_alloc_block);
update_block_bitmap();
super_block[0].bg_free_blocks_count--;
update_group_desc();
return last_alloc_block;
}
void remove_block(unsigned short del_num)//删除一个block
{
unsigned short tmp;
tmp=del_num/8;
reload_block_bitmap();
switch(del_num%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;
}
//printf("%x \n",bitbuf[tmp]);
update_block_bitmap();
super_block[0].bg_free_blocks_count++;
update_group_desc();
}
//
unsigned short get_inode()//分配一个inode,返回序号
{
unsigned short cur=last_alloc_inode;
unsigned char con=128;
int flag=0;
if(super_block[0].bg_free_inodes_count==0)
{
printf("There is no Inode to be alloced!\n");
return 0;
}
reload_inode_bitmap();
cur=(cur-1)/8;
while(bitbuf[cur]==255)//比较结果永远为假--------------
{
if(cur==511)cur=0;
else cur++;
}
//printf("inode %x %d\n",bitbuf[cur],cur);
while(bitbuf[cur]&con)
{
con=con/2;
flag++;
}
bitbuf[cur]=bitbuf[cur]+con;
last_alloc_inode=cur*8+flag+1;
//printf(" %x %d\n",bitbuf[cur],last_alloc_inode);
update_inode_bitmap();
super_block[0].bg_free_inodes_count--;
update_group_desc();
return last_alloc_inode;
}
//
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();
super_block[0].bg_free_inodes_count++;
update_group_desc();
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -