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

📄 fs.c

📁 模拟linux操作系统文件系统资源管理的C程序
💻 C
📖 第 1 页 / 共 4 页
字号:
		}
		idnum=i;
		nodnum=pdir->rectables[i].rec_idnum;
		//get ready for  free block area of all records in the dir 
		ptemnod=(struct inode *)malloc(sizeof(struct inode));
		if(ptemnod==NULL){
			printf("Malloc inode struct error!\n");
			return MAL_ERR;
		}
		offset=BLOCK_SIZE+sizeof(struct inode)*nodnum;
		/* read out itself inode */
		fseek(fd,offset,SEEK_SET);                
		fread((char *)ptemnod,sizeof(struct inode),1,fd); 
		/* read out its dir content from disk*/
		pbasedir=(struct dir *)malloc(sizeof(struct dir));
		if(pbasedir==NULL){
			printf("Malloc dir struct error!\n");
			return MAL_ERR;
		}
		blklen=pnd->f_blktotal;
		for(i=0;i<blklen;i++){
			blknum=pnd->f_blknums[i];
			offset=BLOCK_SIZE*(blknum+1);
			/* read out dictionary dir content to a dir struct point*/
			fseek(fd,offset,SEEK_SET);                
			fread((char *)pbasedir+offset-BLOCK_SIZE,sizeof(struct dir),1,fd); 
		}
		// free block area of all records in the dir 
		for(j=0,i=0;(i<MAX_RECTOTAL)&&(j<pbasedir->rec_len);i++){
			if(pbasedir->rectables[i].flag==0)
				continue;
			else {  j++;
					if(pnd->i_mode.typ==0)			//if sub record is dir
						trmdir(dname,pbasedir->rectables[i].rec_name);	//delet dir record
					if(ptemnod->i_mode.typ==1){			//if sub record is file 
						nodnum=pbasedir->rectables[i].rec_idnum;
						subpnd=(struct inode *)malloc(sizeof(struct inode));
						if(subpnd==NULL){
							printf("Malloc inode struct error!\n");
							return MAL_ERR;
						}
						fnum=pnode->i_num;
						offset=BLOCK_SIZE+sizeof(struct inode)*fnum;
						/* read out dictionary inode */
						fseek(fd,offset,SEEK_SET);                
						fread((char *)pnd,sizeof(struct inode),1,fd); 
						tdelet(subpnd);				//delet file record
					}
				}
		}
		/* update father dictionary  uopdate this dir's father dictionary */
		pdir->rectables[idnum].flag=0;
		/* now rewrite father dir file content to its disk */
		blklen=pnd->f_blktotal;
		for(i=0;i<blklen;i++){
			blknum=pnd->f_blknums[i];
			offset=BLOCK_SIZE*(blknum+1);
			/* read out dictionary dir content to a dir struct point*/
			fseek(fd,offset,SEEK_SET);                
			fwrite((char *)pdir+offset-BLOCK_SIZE,sizeof(struct dir),1,fd); 
		}

		/* free sup_block  struct */		
		for(i=0;i<ptemnod->f_blktotal;i++){
			blknum=ptemnod->f_blknums[i];
			temblk=psp->freblkstart;
			psp->free_blks[blknum].next_blknum=psp->free_blks[temblk].next_blknum;
			psp->free_blks[temblk].next_blknum=blknum;
			ptemnod->f_blknums[i]=psp->free_blks[temblk].cur_blknum;
		}

		/*  free the i_node of file to delet update psp->free_nod[] */
 
		nodnum=pbasedir->rectables[i].rec_idnum;
		temnod=psp->freenod_start;
		psp->free_inodes[nodnum].next_nodnum=psp->free_inodes[temnod].next_nodnum;
		psp->free_inodes[temnod].next_nodnum=nodnum;
		psp->freenod_start=nodnum;

		printf("\n Success!");
		return OK;
	}

	/****************************************************
	 ** 函数
	 **
	 **
	 *******************************************/
	 unsigned int tls(const char * dname )
	{
	
		 unsigned int i,j;
		 unsigned int nodnum;
		 unsigned int blknum,blklen,fnum;
		 unsigned int offset;
		struct inode * pnode,* pnd;
		struct dir * pdir;

		/** read out super  block area at first **/
		if(psp==NULL){	//第一次进入则需读出 super block 结构
			psp=(struct super_block *)malloc(sizeof(struct super_block));
			if(psp==NULL){
				printf("Initiate global variable psp error! \n");
				return MAL_ERR;
			}
			/* read out super block */
			fread(psp,sizeof(struct super_block),1,fd);
		}

		pnode=(struct inode *)malloc(sizeof(struct inode));
		if(pnode==NULL){
			printf("malloc error \n");
			return MAL_ERR;
		}

		/* Notice: getinode(fname) return inode index of file's dictionary  with name fname */
		/* possibility check */
		if((nodnum=getinode(dname))<0){
			printf("Dictionary not exit!");
			return NOD_ERR;	
		}

		/* read out cordirate i_niode content from disk */
		offset=BLOCK_SIZE+nodnum*sizeof(struct inode);
		fseek(fd,offset,SEEK_SET);                
		fread((char *)pnode,sizeof(struct inode),1,fd);
		/* user priority check */
		i=cur_usernum;
		if(pnode->i_uid!=psp->users[i].uid){
			printf("Vist without priority!");
			return PRI_ERR;		
		}

		/*  dir mode check */
		if(pnode->i_mode.rwx<4){
			printf("Read only!");
			return MOD_ERR;
		}
		
	
		/* 1. read out the dictionary of the file
		 * 2. search the record number of the file 
		 * 3. update the dictionary record
		 */

		pnd=(struct inode *)malloc(sizeof(struct inode));
		if(pnd==NULL){
			printf("Malloc inode struct error!\n");
			return MAL_ERR;
		}
		fnum=pnode->i_father;
		offset=BLOCK_SIZE+sizeof(struct inode)*fnum;
		/* read out dictionary inode */
		fseek(fd,offset,SEEK_SET);                
		fread((char *)pnd,sizeof(struct inode),1,fd); 
		/* read out dictionary content from disk to dir struct */
		pdir=(struct dir *)malloc(sizeof(struct dir));
		if(pdir==NULL){
			printf("Malloc dir struct error!\n");
			return MAL_ERR;
		}
		blklen=pnd->f_blktotal;
		for(i=0;i<blklen;i++){
			blknum=pnd->f_blknums[i];
			offset=BLOCK_SIZE*(blknum+1);
			/* read out dictionary dir content to a dir struct point*/
			fseek(fd,offset,SEEK_SET);                
			fread((char *)pdir+offset-BLOCK_SIZE,sizeof(struct dir),1,fd); 
		}

		/* now display the dictionary of the dir
		 * 1. search the rectables to find out all record 
		 * 2. display all records in this dir 
		 */		
		for(j=0,i=0;(i<MAX_RECTOTAL)&&(j<pdir->rec_len);i++){
			if(pdir->rectables[i].flag==0)
				continue;
			else { j++;
				   printf(" %s \t",pdir->rectables[i].rec_name);
				}
		}
		if(pdir->rec_len==0)
			printf("Dictionary is empty \n");

		printf("\n Success!");
		return OK;
	}

/***************************************
 ** declare global variable
 **
struct super_block * psp;
unsigned int maxftotal;
unsigned int UID[MAX_USERTOTAL]={UID_USR1,UID_USR2,UID_USR3,UID_USR4};
unsigned int cur_usernum; */

/*********************************************************************
 *********************************************************************
 ** now enter main test 
 **/
 int main(void)
{
	unsigned int buflen;
	char buf[MAX_IOBUF],ch;
	const char * basefile="d:\\t.dat";
	char dirname[MAX_FILENM]="dirname";
	char filename[MAX_FILENM]="filename";
	struct inode * pnode;

	/* intiate global variable at first */
	cur_usernum=0;
	//basefile="d:\expr.c";
	psp=NULL;			//进入标志

	pnode=(struct inode *)malloc(sizeof(struct inode));
	if(pnode==NULL){
		printf("Initiate global variable pnode error! \n");
		return MAL_ERR;
	}

	if((fd=fopen(basefile,"r+b"))==0){
		printf("Can't open %s!",basefile);
		return OPEN_ERR;
	}
	/* initiate variable in main founctiion */
	/*basefile="basefile";
	dirname="dirname";
	filename="filename";*/

	printf("============================================================\n");
	printf("~~~~~~~~~    Welcom to vitual file system   ~~~~~~~~~~~~~~~~\n"); 
	printf("============================================================\n");
	printf("*********************************************************\n");
	printf("*** NOTICE: you should choose a free file big enough   **\n");
	printf("***	as the vitual disk space at first,then you must    **\n");
	printf("***	fornmat the file.After that you can log in the     **\n");
	printf("*** system with right usre name and key,might you need **\n"); 
	printf("*** refered the readme text to get manager id and user **\n");
	printf("*** secrat.if youget into succsess,you can makd dir and**\n");
	printf("*** dir with commal command as usual.                  **\n");
	printf("*********************************************************\n \n");
	printf("~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~\n");
	printf("************************************************************\n");
	printf("~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~\n");

CONU:
	printf("\n @@@@@@  Now  please  choose  @@@@@@@ \n");
	printf(" Format ------------------------- F/f \n");
	printf(" Login  ------------------------- I/i \n");
	printf(" Logout ------------------------- O/o \n");
	printf(" Creat  ------------------------- C/c \n");
	printf(" Open   ------------------------- P/p \n");
	printf(" Write  ------------------------- W/w \n");
	printf(" Read   ------------------------- R/r \n");
	printf(" Close   ------------------------ Z/z \n");
	printf(" Delet  ------------------------- D/d \n");
	printf(" Makdir ------------------------- M/m \n");
	printf(" Rmdir  ------------------------- K/k \n");
	printf(" Shwdir ------------------------- S/s \n");
	printf(" Exit --------------------------- X/x \n \n");

	getchar();
	printf("\n @ Enter your choose : ");
	scanf("%c",&ch);
	switch(ch){
		case 'f':
		case 'F':
				  format();
				  break;
		case 'i':
		case 'I': login();
				  break;
		case 'o':
		case 'O': logout();
				  break;
		case 'c':
		case 'C': printf("\n@Enter dictionary name please: ");
				  scanf("%s",dirname);
				  printf("\n@Enter file name please: ");
				  scanf("%s",filename);
				  tcreat(dirname,filename);
				  break;
		case 'p':
		case 'P': printf("\n@Enter file name please: ");
				  scanf("%s",filename);
				  topen(filename);
				  break;
		case 'w':
		case 'W': printf("\n@Enter file name please: ");
				  scanf("%s",filename);
				  pnode=topen(filename);
				  printf("\n@write to file please: ");
				  scanf("%s",buf);
				  buflen=sizeof(buf);
				  twrite(pnode,buf,buflen,011);
				  break;
		case 'r':
		case 'R': printf("\n@Enter file name please: ");
				  scanf("%s",filename);
				  pnode=topen(filename);				  
				  buflen=BLOCK_SIZE;
				  tread(pnode,buf,buflen);
				  //clsrc();
				  puts(buf);
				  break;
		case 'z':
		case 'Z': tclose(pnode);
				  break;
		case 'd':
		case 'D': printf("\n@Enter file name please: ");
				  scanf("%s",filename);
				  pnode=topen(filename);
				  tdelet(pnode);	
				  break;
		case 'm': 
		case 'M': printf("\n@Enter path name please: ");
				  scanf("%s",dirname);
				  printf("\n@Enter dir name please: ");
				  scanf("%s",filename);
				  tmkdir(dirname,filename);
				  break;
		case 'k':
		case 'K': printf("\n@Enter path name please: ");
				  scanf("%s",dirname);
				  printf("\n@Enter dir name please: ");
				  scanf("%s",filename);
				  trmdir(dirname,filename);
				  break;
		case 's':
		case 'S': printf("\n@Enter path name please: ");
				  scanf("%s",dirname);
				  tls(dirname);
				  break;
		case 'x':
		case 'X':
				free(psp);
				free(pnode);
				fclose(fd);
				printf("\n See you !");		
				 //exit(-1);
				return OK;
		default : printf("\n @Choose error! ");
	}
	//getchar();
	//printf("\n@ Continue?Y/N  ");
	//scanf("%c",&ch);
	//scanf("%c",&con);
	///con=getchar();
	//ch=getchar();
	//putchar(ch);	
	//if(ch!='n'||ch!='N'){
		// clrsc();
	goto CONU;
	//}
	/*else{
		free(psp);
		free(pnode);
		fclose(fd);
		printf("\n See you !");
		getchar();
		return OK;
	}*/
}				  









	





		

	
		






		
		


			

		





	
		
		

		

⌨️ 快捷键说明

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