⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 hd.c

📁 一个完整的SHELL实现源代码
💻 C
字号:
#include "fs.h"

char *filename="filesys.hd";

//s_int a;
filsys g_sblock;

FILE *fp_img_hd=NULL;

/* hd_init
   =======
   open filesystem, the image file
*/
s_int hd_init()
{
//	char *p;
//	char c;
	if(fp_img_hd==NULL)
	{
		printf("Loading image file [%s].\n",filename);
		fp_img_hd=fopen(filename,"rb+");
		if(fp_img_hd==NULL)
		{
			printf("File not exist. Creating [%s].\n",filename);
			fp_img_hd=fopen(filename,"wb+");
			printf("Process format.\n");
			hd_format();
		}
	}
	/* read super block */
	if(NULL==hd_read(1,&g_sblock))
	{
		printf("Fatal error read file [%s].\n",filename);
		panic("hd_init():Error read image file.");
	}
	return 0;
}

/* hd_leave
   ========
   close the image file
*/
s_int hd_leave()
{
//	filsys *buf;
	if(g_sblock.fmod)
	{
		g_sblock.fmod=0;
		hd_write(1,&g_sblock);
	}
	if( fp_img_hd == NULL )return 1;
	fclose(fp_img_hd);
	fp_img_hd = NULL;
	return 0;
}

/* hd_read 
   =======
   read a block from filesystem to buf
   return the buf address
   or when failed to read return NULL.
*/
void* hd_read(s_int blkno,void *buf)
{
#ifdef DEBUG_MODE
	if(fp_img_hd==NULL)
		panic("hd_read():You must hd_init() first.");
#endif
	fseek(fp_img_hd,(long)blkno*512,SEEK_SET);
	if(1==fread(buf,512,1,fp_img_hd))return buf;
	else return NULL;
}

/* hd_write
   ========
   write buf block in to filesystem
   return true when success.
*/
s_int hd_write(s_int blkno,void *buf)
{
#ifdef DEBUG_MODE
	if(fp_img_hd==NULL)
		panic("hd_write():You must hd_init() first.");
#endif
	fseek(fp_img_hd,(long)blkno*512,SEEK_SET);
	return (1==fwrite(buf,512,1,fp_img_hd));
}

s_int hd_format()
{
	s_int i,j;
	inode *ip;
	filsys tfs;
	memset(&tfs,0,sizeof(tfs));
	for(i=2;i<2+10;i++)  /* clear inode_o block area */
		hd_write(i,&tfs);
	hd_write(NBLOCK-1,&tfs); /* set hdd size to NBLOCK*512 bytes */
	tfs.isize=10; /* inode area has 10 blocks */
	tfs.fsize=NBLOCK;/*-tfs.isize-2;*/
	tfs.nfree=0;
	tfs.ninode=0;
	tfs.flock=0;
	tfs.ilock=0;
	tfs.fmod=0;
	tfs.ronly=0;
	tfs.time[0]=0;
	tfs.time[1]=0;
	tfs.free[0]=0;	
	for(i=1,j=tfs.isize+2;j<NBLOCK;j++)
	{
		tfs.free[i++]=j;
		if(i==100)
		{
			tfs.nfree=100;
			hd_write(++j,&tfs);
			tfs.free[0]=j;
			i=1;
		}
	}
	tfs.nfree=i;
	hd_write(1,&tfs);
	/* create root inode */
	/* ip->number must be 0 */
	ip=iget(0);
	if(ip==NULL)	panic("hd_format(): root inode alloc failed.");
	ip->mode |= IALLOC; /* not use ialloc, but alloc it */
	if(ip->number != 0 ) panic("hd_format(): root inode No. not 0.");
	ip->nlink=1;
	ip->gid=0;
	ip->uid=0;
	ip->mode |=IFDIR;
	ip->size0=0;
	ip->size1=0;
	ip->adr[0]=balloc();
	iput(ip);
	printf("Format completed.\n");
	return 0;
}

/* 盘块分配函数*/
s_int balloc()
{
	s_int i,tbno;
	filsys p;
	if(g_sblock.nfree>1)
	{
		g_sblock.fmod=1; 
		return g_sblock.free[--g_sblock.nfree];
	}
	tbno=g_sblock.free[0];

	/* no free blocks */
	if(tbno==0)return 0;
	g_sblock.fmod=1; 
	hd_read(tbno,&p); /* or use bread instead */
	for(i=0;i<100;i++)
		g_sblock.free[i]=p.free[i];
	return tbno;
}

/* 盘块释放函数 */
s_int bfree(s_int bno)
{
	if(bno<g_sblock.isize+2)return 0;
	if(g_sblock.nfree<99)
	{
		g_sblock.free[g_sblock.nfree++]=bno;
	}
	else
	{
		hd_write(bno,&g_sblock);
		g_sblock.nfree=1;
		g_sblock.free[0]=bno;
	}
	g_sblock.fmod=1; 
	return 0;
}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -