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

📄 main.cpp

📁 模拟unix文件系统的功能
💻 CPP
📖 第 1 页 / 共 3 页
字号:
	cout<<"打开文件失败!"<<endl;
    return NULL;
}

/*******************************************************************************************
                     检测并恢复文件系统check主函数
				        将异常Inode区进行初始化
*******************************************************************************************/
void check()
{
	for(int i=0;i<Inode_Num;i++)
	{
		//若Inode区中decide值出现异常,则进行修复
		if((Inode[i].decide!=0)&&(Inode[i].decide!=1)&&(Inode[i].decide!=-1))
		{
			Inode[i].decide=-1;
            Inode[i].index=i;
		    for(int j=0;j<15;j++)                    //将每个目录的子节点赋为-1
			{
				Inode[i].child[j]=-1;
			}
        }
	}
}

/*******************************************************************************************
                            copy函数的子功能函数
                    实现路径的转换,内外部文件都可实现
*******************************************************************************************/
char *Trans_Path(char *path,int decide)
{
	char *Real_Path=new char;
	char *temp=new char;
	if(decide==0)                               //内部文件
    {
		if(path[2]=='/')                        //完整路径
        {
			return path;
		}
		else                                    //相对路径
		{
			Real_Path=path+2;
			return Real_Path;
		}
	}
    if(decide==1)                               //外部文件
	{
		temp=strchr(path,'\\');                  
		if(temp)                                //完整路径
		{
			return path;
		}
		else                                    //相对路径
		{
			Real_Path=path+2;
			return Real_Path;
		}
	}
	return NULL;
}



/*******************************************************************************************
                         拷贝命令copy函数
		    支持模拟文件系统内部的文件拷贝,还支持内部文件与外部文件间拷贝
			         外部文件只支持纯文本文件
*******************************************************************************************/

int copy(char *source,char *dest)
{
	//记录源文件与目标文件是内部文件还是外部文件,内部为0,外部为1,不存在为-1
	int parent;
	int decide;
	int Source_Decide=-1;                                  
	int Dest_Decide=-1;
	long i;
	char *content=new char;
	char *Content_Temp=new char;
	char *Source_Path=new char;
    char *Dest_Path=new char;
	if(source[0]=='L')
	Source_Decide=0;
	else Source_Decide=1;
	if(dest[0]=='L')
	Dest_Decide=0;
	else Dest_Decide=1;

	if((Source_Decide==0)&&(Dest_Decide==0))                 //内部文件相互拷贝
	{
		strcpy(Source_Path,Trans_Path(source,0));
		strcpy(Dest_Path,Trans_Path(dest,0));
        strcpy(content,cat(Source_Path));
		if(content!=NULL)                                    //读出了内部文件内容
		{
			parent=Get_Parent(Dest_Path);
			if(parent!=-1&&parent!=-2)
			{
				decide=search(Get_Name(Dest_Path),parent);
				if(decide==-1)
				{
					File_Init(parent,Get_Name(Dest_Path),content);//写入内部文件
				    return 1;
				}
			}
		}
	}

	if((Source_Decide==1)&&(Dest_Decide==0))                 //外部文件到内部文件拷贝
	{
		strcpy(Source_Path,Trans_Path(source,1));
		strcpy(Dest_Path,Trans_Path(dest,0));
		if(access(Source_Path,0)==0)                         //该外部文件确实存在
		{
			fstream iof(Source_Path,ios::binary|ios::in|ios::out);
            streampos begin=iof.tellg();
		    iof.seekg(0,ios::end);
            streampos end=iof.tellg();
	        i=long(end-begin);
	        iof.seekg(0,ios::beg);
            while(!iof.eof())
			{
				iof.get(*Content_Temp);                     
				Content_Temp++;
			}
			strncpy(content,Content_Temp-i-1,i+1);
			content[i+1]='\0';                                //读出外部文件内容 
			parent=Get_Parent(Dest_Path);
			if(parent!=-1&&parent!=-2)
			{
				decide=search(Get_Name(Dest_Path),parent);
				if(decide==-1)
				{
					File_Init(parent,Get_Name(Dest_Path),content);//写入内部文件
				    return 1;
				}
			}
		}
	}
	if((Source_Decide==0)&&(Dest_Decide==1))                 //内部文件到外部文件拷贝
	{
		strcpy(Source_Path,Trans_Path(source,0));
		strcpy(Dest_Path,Trans_Path(dest,1));
        strcpy(content,cat(Source_Path));
		if(content!=NULL)                                    //读出了内部文件内容
		{
			fstream iof(Dest_Path,ios::binary|ios::in|ios::out);
		 	if(!iof)
			{
				return -1;
			}
			ofstream out(Dest_Path);                         //清空外部文件
            while(*content!='\0')
			{
				iof.write((char*)&(*content),sizeof(char));  //写入外部文件 
				content++;
			}
			return 1;
		}
	}
	cout<<"拷贝失败!"<<endl;
    return -1;
}



/*******************************************************************************************
                        删除文件命令del主函数
		    删除指定文件,成功时返回被删文件索引号,失败时返回-1
*******************************************************************************************/

int del(char *path)
{
	int parent,Req_Block_Num,First_Block,Index_Other;
	parent=Get_Parent(path);
	if((parent!=-2)&&(parent!=-1))
	{
		int decide;
		decide=search(Get_Name(path),parent);
	    if((decide!=-1)&&Inode[decide].decide==1)          //要删除的文件存在      
		{
			Index_Other=Search_Other(Get_Name(path),parent);
			Req_Block_Num=(Inode[decide].length)/Block_Size+1;
			First_Block=Inode[decide].First_Block;
			//修改superblock及两个bitmap
			super.Free_Inode++;
			super.Free_Block=super.Free_Block+Req_Block_Num;
            Inode_Bitmap[decide]=0;
            for(int num=0;num<Req_Block_Num;num++)
			{
				Block_Bitmap[First_Block+num]=0;
			}
			
            //修改inode节点内容
            Inode[decide].decide=-1;                
            Inode[decide].First_Block=-1;            
            Inode[decide].length=0;
            Inode[decide].index=decide;
            
            //修改被删除文件的父节点
			Inode[parent].Child_Num--;
			Inode[parent].child[Index_Other]=-1;
			return decide;
		}
	}
	cout<<"删除文件失败!"<<endl;
	return -1;
}

/*******************************************************************************************
                         列出命令,提供帮助,help函数
*******************************************************************************************/
void help()
{
	cout<<" "<<endl;
	cout<<"  *************************************************************************"<<endl;
	cout<<"  ***********************The    List    Of    Commands ********************"<<endl;
    cout<<"  *************************************************************************"<<endl;
	cout<<"  1                info"<<endl;
	cout<<"  2                cd"<<endl;
	cout<<"  3                dir"<<endl;
	cout<<"  4                md"<<endl;
	cout<<"  5                rd"<<endl;
	cout<<"  6                newfile"<<endl;
	cout<<"  7                cat"<<endl;
	cout<<"  8                copy"<<endl;
	cout<<"  9                del"<<endl;
	cout<<"  10               check"<<endl;
	cout<<"  11               exit"<<endl;
	cout<<"  12               help"<<endl;
}

/*******************************************************************************************
                  初始化文件系统时,创建四个目录:home,bin,boot,usr
*******************************************************************************************/
void Init_System()
{
	md("home");
	md("bin");
	md("boot");
	md("usr");
}

/*******************************************************************************************
                      使用exit命令退出时,保存文件系统信息
*******************************************************************************************/
void Save_System()
{
    fstream iof("C:\\LinuxOs.dat",ios::binary|ios::in|ios::out);//打开磁盘空间LinuxOs.dat
	int i;
    iof.seekp(0,ios::beg);
    iof.write((char*)&super,sizeof(Super_Block)); 
    iof.seekp(1016,ios::cur);
    for(i=0;i<Inode_Num;i++)                 //保存inode位图信息
	{
        iof.write((char*)&Inode_Bitmap[i],sizeof(int));
	}
    for(i=0;i<533;i++)                       //保存block位图信息
    {
        iof.write((char*)&Block_Bitmap[i],sizeof(int));
    }
    for(i=0;i<Inode_Num;i++)                 //保存Inode区信息
	{
	    iof.write((char*)&Inode[i],sizeof(inode));
	}

	iof.close();
}    

/*******************************************************************************************
                        实现一个简单的shell
            根据输入的命令及参数,利用上面的函数来进行操作
*******************************************************************************************/

void shell()
{
	char *command[12]={"info","cd","dir","md","rd","newfile","cat",
		               "copy","del","check","exit","help"};
	while(1)
	{
		cout<<current<<endl;
		char *Temp_Command=new char;
		char *Par_A=new char;
		char *Par_B=new char;
		char *A=new char;
		char *B=new char;
		char *C=new char;
		int decide=-1;
		int length;
        gets(Temp_Command);
        
		for(int j=0;j<12;j++)
		{
			length=strlen(command[j]);
			if(!strncmp(Temp_Command,command[j],length))
			{
				decide=j;
			}
		}                                               //获取命令的序号
        
		if((decide!=0)&&(decide!=7)&&(decide!=9)&&(decide!=-1)&&(decide!=10)&&(decide!=11))
		{
			int i=0;
			length=strlen(command[decide]);
			Temp_Command=Temp_Command+length;
			while(*Temp_Command!='\0')                   //将参数赋给字符串Par_A
			{
				if(*Temp_Command!=' ')
				{
					Par_A[i]=*Temp_Command;
					i++;
				}
				Temp_Command++;
			}
			Par_A[i]='\0';
		}

		if(decide==7)                                     //copy命令有两个参数
		{
			int i=0;
			int j=0;
			length=strlen(command[decide]);
			Temp_Command=Temp_Command+length;
            A=strchr(Temp_Command,' ');                    //分别将两个参数赋给Par_A与Par_B
			if(A!=NULL)
			{
				while(*A==' ')
				{
					A++;
				}
			    B=strchr(A,' ');
				if(B!=NULL)
				{
					i=int(B-A);
                    strncpy(Par_A,A,i);
				    Par_A[i]='\0';
                    while(*B==' ')
					{
					   B++;
					}
                    C=strchr(B,' ');
                    if(C==NULL)
					{
						strcpy(Par_B,B);
					}
					else
					{
						j=int(C-B);
						strncpy(Par_B,B,j);
						Par_B[j]='\0';
					}
				}
			}
		}
        
		//使用switch,case函数,选择要执行的命令
        switch(decide)
		{
		case -1:
			cout<<"The command is not exist!"<<endl;
			break;
		case 0:
			info();
			cout<<"Succeed!"<<endl;
			break;
		case 1:
            if(cd(Par_A)!=-1)
			cout<<"Succeed!"<<endl;
			break;
		case 2:
            if(dir(Par_A)!=-1)
			cout<<"Succeed!"<<endl;
			break;
        case 3:
			if(md(Par_A)!=-1)
			cout<<"Succeed!"<<endl;
			break;
		case 4:
			if(rd(Par_A)!=-1)
			cout<<"Succeed!"<<endl;
			break;
		case 5:
            if(newfile(Par_A)!=-1)
			cout<<"Succeed!"<<endl;
			break;
        case 6:
			if(cat(Par_A)!=NULL)
		    cout<<cat(Par_A)<<endl;
			cout<<"Succeed!"<<endl;
			break;
		case 7:
			if(copy(Par_A,Par_B)!=-1)
			cout<<"Succeed!"<<endl;
			break;
        case 8:
            if(del(Par_A)!=-1)
			cout<<"Succeed!"<<endl;
			break;
		case 9:
            check();
			cout<<"Succeed!"<<endl;
            break;
		case 10:
			return;
		case 11:
			help();
			break;
		}   
	}
}

void main()
{
    if(access("C:\\LinuxOs.dat",0)==0)                          //文件系统已创建过
	{
		Get_System_Info();                                      //将信息从磁盘读至内存
	}
	else                                                        //文件系统尚未创建
	{
	    Creat_System();                                         //初始化文件系统
		Init_System();
	}
    shell();                                                    //命令解释器,对命令进行解释
	Save_System();                                              //保存信息
}

⌨️ 快捷键说明

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