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

📄 myfs.txt

📁 这是我写的一个用于测试的文件系统
💻 TXT
📖 第 1 页 / 共 4 页
字号:
	save_dir(dir_id);                              //立即写回磁盘
}

void open_file_system()                            //打开文件系统                     
{
	int flag=0;
	fp=fopen(file_system_name,"rb");               //以读方式打开,如果不存在,则创建文件系统
	if(fp==NULL)                            
	{
		create_file_system();
		flag=1;
	}
	fp=fopen(file_system_name,"rb+");              //以读写方式打开
	if(fp==NULL)
	{
		printf("Open file system error!\n");
		exit(1);
	}
	if(flag==0)                                    //如果该文件系统早就创建,则将其信息读入主存                          
	{
		fseek(fp,0,SEEK_SET);                      //读超级块信息
		fread(&used_dir,sizeof(int),1,fp);
		fread(&used_file,sizeof(int),1,fp);
		fread(&used_block,sizeof(int),1,fp);

		for(int i=0;i<DIR_NUM;i++)
			fread(&dir_flag[i],sizeof(int),1,fp);
		for(i=0;i<FILE_NUM;i++)
			fread(&file_flag[i],sizeof(int),1,fp);
		for(i=0;i<BLOCK_NUM;i++)
			fread(&block_flag[i],sizeof(int),1,fp);

		for(i=0;i<DIR_NUM;i++)                     //读目录及文件的控制信息
			if(dir_flag[i]==1)
			{
				fseek(fp,sizeof(int)*(DIR_NUM+FILE_NUM+BLOCK_NUM+3)+sizeof(struct dir_node)*i,SEEK_SET);			
				fread(&dir[i],sizeof(struct dir_node),1,fp);
			}
		for(i=0;i<FILE_NUM;i++)
			if(file_flag[i]==1)
			{
				fseek(fp,sizeof(int)*(DIR_NUM+FILE_NUM+BLOCK_NUM+3)+sizeof(struct dir_node)*DIR_NUM+sizeof(struct file_node)*i,SEEK_SET);					
				fread(&file[i],sizeof(struct file_node),1,fp);
			}
		collect(0);
	}
 	fflush(fp);

	curr=0;
	curr_dir=&dir[curr];                           //设置当前目录为根目录
	strcpy(curr_path,curr_dir->dir_name);
	strcat(curr_path,"/");
}

void close_file_system()                           //关闭文件系统,并保存相应信息,这是系统
{                                                  //隐式保存文件及目录的方法
	save(0);
	for(int i=0;i<DIR_NUM;i++)
		if(al_dflag[i]%2!=0)
			write_bit(i,0);
	for(i=0;i<FILE_NUM;i++)
		if(al_fflag[i]%2!=0)
			write_bit(i,1);
	for(i=0;i<BLOCK_NUM;i++)
		if(al_bflag[i]%2!=0)
			write_bit(i,2);

	fseek(fp,0,SEEK_SET);
	fwrite(&used_dir,sizeof(int),1,fp);
	fwrite(&used_file,sizeof(int),1,fp);
	fwrite(&used_block,sizeof(int),1,fp);

	fclose(fp);
}

void help()                               //打印命令及解释
{
	printf("Commands:                 Explanation:\n\n");
	printf("    exit:                 Exit!\n");
	printf("     dir:                 Show files and directories!\n");
	printf("    tree:                 Show directory tree!\n");
	printf("     mkf:                 Create a new file!\n");
	printf("     mkd:                 Create a new directory!\n");
	printf("    delf:                 Delete a file!\n");
	printf("    deld:                 Delete a directory!\n");
	printf("      cd:                 Change the current directory!\n");
	printf("   write:                 Write a string to a file!\n");
	printf("    read:                 Read a string from a file!\n");
	printf("    edit:                 Edit a file!\n");
	printf("     cpf:                 Copy a file!\n");
	printf("     cpd:                 Copy a directory!\n");
	printf("  format:                 Format the disk!\n");
	printf("    help:                 Print commands!\n");
	printf("   savef:                 Save a file!\n");
	printf("   saved:                 Save a directory!\n");
	printf("     rnf:                 Rename a file!\n");
	printf("     rnd:                 Rename a directory!\n");
}

void run()                               //命令接受及解析,转入相应函数运行
{
	char *command[19]={"exit","dir","mkf","mkd","delf","deld","cd","write","read","cpf","cpd","savef","saved","format","help","tree","rnf","rnd","edit"};
	char buf1[256];
	char buf2[2048];
	char buf3[128];
	char buf4[128];
	int key,num,offset,index,del_pos,parent,buf_id,file_id,p;
	char *s,flag='\0';
	printf("Input help to print commands!\n");
	while(1)
	{
		strcpy(buf1,"");
		strcpy(buf2,"");
		strcpy(buf3,"");
		strcpy(buf4,"");
		p=0;
		printf("\n%s\n",curr_path);
		cin.getline(buf1,256,'\n');
		key=-1;
		for(int i=0;i<=18;i++)                     //命令匹配
			if(strncmp(buf1,command[i],(int)strlen(command[i]))==0)
			{
				key=i;
				break;
			}
		if(key!=-1)                                //参数分解
		{
			s=strchr(buf1,' ');
			if(s!=NULL)
			{
				index=(int)(s-buf1);
				while(buf1[index]==' ')
					index++;
				strcpy(buf3,buf1+index);
				s=strchr(buf3,' ');
				if(s!=NULL)
				{
					num=(int)(s-buf3);		
					buf3[num]='\0';
				}
				s=strrchr(buf1+index,' ');
				if(s!=NULL)
				{
					index=(int)(s-buf1);
					strcpy(buf4,buf1+index+1);
				}
			}
		}
		switch(key)
		{
		case -1:
			printf("The command you input not exist!\n");
			break;;
		case 0:
			return;
		case 1:
			list();
			break;
		case 2:
			if((int)strlen(buf3)==0)
			{			
				printf("please input the name of the file:\n");			
				cin.getline(buf3,128,'\n');			
			}
			if(create(buf3)!=-1)				
				printf("Succeed!\n");
			break;
		case 3:
			if((int)strlen(buf3)==0)			
			{			
				printf("please input the name of the directory:\n");	
				cin.getline(buf3,128,'\n');			
			}
			if(md(buf3)!=-1)				
				printf("Succeed!\n");
			break;
		case 4:
			if((int)strlen(buf3)==0)
			{			
				printf("please input the name of the file:\n");			
				cin.getline(buf3,128,'\n');			
			}
			if(delfile(buf3)!=-1)				
				printf("Succeed!\n");
			break;
		case 5:
			if((int)strlen(buf3)==0)
			{
				printf("please input the name of the directory:\n");			
				cin.getline(buf3,128,'\n');
			}			
			parent=get_parent(buf3,p);
			if(parent==-1)
			{		
				printf("Path name error!\n");		
				break;	
			}
			if((del_pos=search(parent,buf3+p,0,index))==-1)
			{	
				printf("The directory to delete not exist in current directory!\n");
				break;
			}
			if(dir[del_pos].dir_count>0 || dir[del_pos].file_count>0)
			{
				printf("The directory is not empty!\nAre you sure to delete it?('Y' or 'N')\n");			
				cin.getline(&flag,32,'\n');			
				if(flag!='Y' && flag!='y')			
					break;
			}
			del_dir(parent,del_pos,index);
			printf("Succeed!\n");
			break;
		case 6:
			if((int)strlen(buf3)==0)
			{
				printf("please input the name of the diectory:\n");	
				cin.getline(buf3,128,'\n');
			}
			if(change_dir(buf3)!=-1)
				printf("Succeed!\n");	
			break;
		case 7:
			if((int)strlen(buf3)==0)
			{			
				printf("please input the file name:\n");			
				cin.getline(buf3,128,'\n');
			}
			printf("please input the offset:\n");
			scanf("%d",&offset);
			printf("please input the string:\n");
			cin.getline(buf2,256,'\n');
			if(write_file(buf3,offset,strlen(buf2),buf2)!=-1)
				printf("Succeed!\n");
			break;
		case 8:
			if((int)strlen(buf3)==0)
			{
				printf("please input the file name:\n");		
				cin.getline(buf3,128,'\n');
			}
			printf("please input the offset:\n");
			scanf("%d",&offset);
			printf("please input the number of characters:\n");
			scanf("%d",&num);
			if(read_file(buf3,offset,num,buf2)!=-1)
			{
				printf("Succeed!\n");
				printf("%s\n",buf2);
			}
			break;
		case 9:
			if((int)strlen(buf3)==0)
			{
				printf("please input the name of the dest dir:\n");	
				cin.getline(buf3,128,'\n');
			}
			if((int)strlen(buf4)==0)
			{
				printf("please input the name of the source file:\n");	
				cin.getline(buf4,128,'\n');
			}
			if(copy_file(buf3,buf4)!=-1)
				printf("Succeed!\n");
			break;
		case 10:
			if((int)strlen(buf3)==0)
			{
				printf("please input the name of the dest dir:\n");			
				cin.getline(buf3,128,'\n');
			}
			if((int)strlen(buf4)==0)
			{
				printf("please input the name of the source dir:\n");	
				cin.getline(buf4,128,'\n');
			}
			if(copy_dir(buf3,buf4)!=-1)
				printf("Succeed!\n");
			break;
		case 11:
			if((int)strlen(buf3)==0)
			{			
				printf("please input the name of the file:\n");	
				cin.getline(buf3,128,'\n');
			}
			if(savefile(buf3)!=-1)
				printf("Succeed!\n");
			break;
		case 12:
			if((int)strlen(buf3)==0)
			{
				printf("please input the name of the directory:\n");		
				cin.getline(buf3,128,'\n');
			}
			if(saved(buf1)!=-1)
				printf("Succeed!\n");
			break;
		case 13:
			format();
			printf("Succeed!\n");
			break;
		case 14:
			help();
			break;
		case 15:
			show_tree();
			break;
		case 16:
			if((int)strlen(buf3)==0)
			{
				printf("please input the name of the file:\n");			
				cin.getline(buf3,128,'\n');
			}
			if((int)strlen(buf4)==0)
			{
				printf("please input the new name:\n");	
				cin.getline(buf4,128,'\n');
			}
			if(rename_file(buf3,buf4)!=-1)
				printf("Succeed!\n");
			break;
		case 17:
			if((int)strlen(buf3)==0)
			{
				printf("please input the name of the dir:\n");			
				cin.getline(buf3,128,'\n');
			}
			if((int)strlen(buf4)==0)
			{
				printf("please input the new name:\n");	
				cin.getline(buf4,128,'\n');
			}
			if(rename_dir(buf3,buf4)!=-1)
				printf("Succeed!\n");
			break;
		case 18:
			if((int)strlen(buf3)==0)
			{
				printf("please input the name of the file:\n");			
				cin.getline(buf3,128,'\n');
			}	
			parent=get_parent(buf3,p);	
			if(parent==-1)	
			{		
				printf("Path name error!\n");	
				break;	
			}
			file_id=search(parent,buf3+p,1,index);
			if(file_id==-1)	
			{		
				printf("The file not exist!\n");		
				break;	
			}
			buf_id=open_file(file_id,2);
			printf("\n%s\n\n",buffer[buf_id].buf);
			printf("Please select:\nD(delete)  I(insert)   Q(cancel)\n");
			cin.getline(&flag,10,'\n');
			if(flag=='D' || flag=='d')
			{
				printf("Please input the start position and the finish position:\n");
				scanf("%d %d",&offset,&index);
				if(offset>=index)
				{
					printf("Position error!\n");
					break;
				}
				edit(buf_id,2,offset,index,NULL);
			}
			else if(flag=='I' || flag=='i')
			{
				printf("Please input the insert position:\n");
				scanf("%d",&offset);
				printf("Please input the string:\n");
				cin.getline(buf1,256,'\n');
				edit(buf_id,1,offset,0,buf1);
			}
			else
				break;
			printf("Succeed!\n");
			break;
		}
	}
}

void main()
{
	open_file_system();
	run();
	close_file_system();
}

⌨️ 快捷键说明

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