1、实验二 文件系统模拟设计 ———————————————————————————————— 作者: ———————————————————————————————— 日期: 15 个人收集整理 勿做商业用途 实验二 文件系统模拟设计
2、 一、实验目的 通过一个简单多用户文件系统的设计,加深理解文件系统的内部功能及内部实现。 二、实验内容 为linux系统设计一个简单的二级文件系统。要求做到以下几点: (1)可以实现下列几条命令(至少4条); login 用户登录 dir 列文件目录 create 创建文件 delete 删除文件 open 打开文件 close 关闭文件 read 读文件 write 写文件 (2)列目录时要列出文件名、物理地址、保护码和文件长度; (3)源文件可以进
3、行读写保 三、实验内容指导提示 (1)首先应确定文件系统的数据结构:主目录、子目录及活动文件等。主目录和子目录都以文件的形式存放于磁盘,这样便于查找和修改。 (2)用户创建的文件,可以编号存储于磁盘上.如file0,file1,file2.。.并以编号作为物理地址,在目录中进行登记。 ★★★★★《程序设计思想参考》 〈程序设计> (1)设计思想 本系统是模拟实现多用户多目录的文件系统(8个用户),在系统出现登录后 ,输入用户与口令,在用户登录系统后,可建立文件卷,将用户输入的文件保存在指定的文件中.系统的命令与其命令的具体实现,此模拟系统共提供了上述命令,并根据命令的含义
4、与要求,用C++编程来完成所有具体操作。该系统可以模拟完成用户的登陆和验证,列出文件和目录,新建目录,改变目录,创立和编写文件,删除文件和退出系统等功能。【注:在linux下用g++编译C++】 (2)主要数据结构 用户结构:账号与密码结构 typedef struct users { char name[8]; char pwd[10]; }users; 本系统有8个默认的用户名,前面是用户名,后面为密码,用户登陆时只要输入正确便可进入系统,否则提示失败要求重新输入。 users usrarray[8] = { "usr1”,"usr1",
5、 ”usr2",”usr2”, "usr3”,”usr3”, ”usr4”,”usr4", ”usr5”,”usr5", "usr6”,"usr6”, "usr7",”usr7", "usr8”,"usr8", }; (3)数据结构说明 a)文件结构链表 struct fnode { char filename[FILENAME_LENGTH]; int isdir; int isopen; char content[255]; fnode *parent; fnode *child; fnode *prev; fno
6、de *next; }; b)函数介绍 fnode *initfile(char filename[],int isdir);//初始化文件或目录 void createroot();//建立系统根目录 int run();系统运行 int findpara(char *topara);对参数进行处理 bool chklogin(char *users, char *pwd);检查账号与口令 void help();命令列表 int mkdir();建立目录 int create();建立文件 int read();读取文件 int write();
7、写入文件 int del();删除文件 int cd();切换目录 int dir();文件与目录列表 (4)各模块流程图 (5)、模拟文件系统参考程序清单 #include "stdio。h” #include ”iostream.h" #include ”string。h" #include ”iomanip.h” #define FILENAME_LENGTH 10 //文件名称长度 #define COMMAND_LENGTH 10 //命令行长度 #define PARA_LENGTH 30 //参数长度
8、 //账号结构 typedef struct users { char name[8]; char pwd[10]; }users; //文件结构 struct fnode { char filename[FILENAME_LENGTH]; int isdir; int isopen; char content[255]; fnode *parent; fnode *child; fnode *prev; fnode *next; }; //账号 users usrarray[8] = { ”
9、usr1",”usr1”, "usr2”,”usr2", "usr3","usr3", "usr4",”usr4”, "usr5",”usr5”, ”usr6”,”usr6”, "usr7”,”usr7", ”usr8","usr8”, }; fnode *initfile(char filename[],int isdir); void createroot(); int run(); int findpara(char *topara); bool chklogin(char *users, char *pwd); void help(
10、); int mkdir(); int create(); int read(); int write(); int del(); int cd(); int dir(); fnode *root,*recent,*temp,*ttemp; char para[PARA_LENGTH],command[COMMAND_LENGTH],temppara[PARA_LENGTH],recentpara[PARA_LENGTH]; //创建文件与目录结点 fnode* initfile(char filename[],int isdir) { f
11、node *node=new fnode; strcpy(node-〉filename,filename); node—>isdir=isdir; node—〉isopen=0; node->parent=NULL; node-〉child=NULL; node-〉prev=NULL; node—>next=NULL; return node; } //创建文件存储结点 void createroot () { recent=root=initfile(”/",1); root—>parent=NULL; roo
12、t—〉child=NULL; root—〉prev=root->next=NULL; strcpy(para,”/”); } int mkdir() { temp=initfile(” ”,1); cin>〉temp—〉filename; if(recent-〉child==NULL) { temp—>parent=recent; temp->child=NULL; recent-〉child=temp; temp—〉prev=temp—〉next=NULL; } else
13、 { ttemp=recent-〉child; while(ttemp—〉next) { ttemp=ttemp—〉next; if(strcmp(ttemp->filename,temp—>filename)==0&&ttemp->isdir==1) { printf(”对不起,目录已存在!”); return 1; } } ttemp->next=temp; temp->parent=NULL; temp—〉child=NULL; temp-〉prev
14、ttemp;
temp—>next=NULL;
}
return 1;
}
int create()
{
temp=initfile(” ”,0);
cin>〉temp->filename;
cin>>temp—〉content;
if(recent->child==NULL)
{
temp->parent=recent;
temp—〉child=NULL;
recent—>child=temp;
temp—〉prev=temp->next=NULL;
cout〈〈”文件建立成功!”< 15、l;
}
else
{
ttemp=recent—〉child;
while(ttemp->next)
{
ttemp=ttemp—>next;
if(strcmp(ttemp-〉filename,temp->filename)==0&&ttemp->isdir==0)
{
printf(”对不起,文件已存在!”);
return 1;
}
}
ttemp—〉next=temp;
temp—〉parent=NULL;
temp->child=NULL 16、
temp->prev=ttemp;
temp—>next=NULL;
cout〈〈”文件建立成功!”〈 17、 ” 〈isdir)
{cout<〈” 18、temp=temp->next;
}
cout<〈"Total: ”〈〈" directors ” 〈>filename;
if(recent->child==NULL)
{
cout<〈"文件不存在!"〈 19、lename,filename)==0)
{
cout〈〈recent—>child-〉content〈〈endl;
return 1;
}
else
{
temp=recent—〉child;
while(temp—>next)
{
if(strcmp(temp->next-〉filename,filename)==0)
{cout〈 20、
}
int write()
{
char filename[FILENAME_LENGTH];
cin〉>filename;
if(recent—〉child==NULL)
{
cout<<”文件不存在!”< 21、—〉isopen=0;//设置文件标记为关闭
cout<<”文件写入成功!”〈〈endl;
return 1;
}
else
{
temp=recent—〉child;
while(temp->next)
{
if(strcmp(temp->next—>filename,filename)==0)
{
recent—〉child-〉isopen=1;//设置文件标记为打开
cin〉>temp->next->content;
recent—〉child-〉isopen=0;//设 22、置文件标记为关闭
cout<<”文件写入成功!"〈〈endl;
return 1;}
}
cout〈〈”文件不存在!"〈 23、parent;
}
i=strlen(para);
while(para[i]!='/’ && i>0) i-—;
if(i!=0)
para[i]=’\0’;
else
para[i+1]=’\0’;
}
else
{
findpara(topara);
}
return 1;
}
int findpara(char *topara)
{
int i=0;
int sign=1;
if(strcmp(topara,”/”)==0)
{
recent=r 24、oot;
strcpy(para,”/”);
return 1;
}
temp=recent;
strcpy(temppara,para);
if(topara[0]==’/’)
{
recent=root—〉child;
i++;
strcpy(para,”/”);
}
else
{
if(recent!=NULL && recent!=root)
strcat(para,"/");
if(recent && recent—> 25、child)
{
if(recent-〉isdir)
recent=recent—>child;
else
{
printf(”路径错误!\n”);
return 1;
}
}
}
while(i<=strlen(topara) && recent)
{
int j=0;
if(topara[i]==’/' && recent-〉child)
{
i++;
if(recent->isdir)
recent=re 26、cent->child;
else
{printf(”路径错误\n”);
return 0;
}
strcat(para,”/");
}
while(topara[i]!=’/’ && i〈=strlen(topara))
{
recentpara[j]=topara[i];
i++;j++;
}
recentpara[j]='\0';
while((strcmp(recent—>filename,recentpara)!=0 || (recent-〉isdir!=1)) 27、 && recent—〉next!=NULL)
{
recent=recent->next;
}
if(strcmp(recent-〉filename,recentpara)==0)
{
if(recent—>isdir==0)
{strcpy(para,temppara);
recent=temp;
printf("是文件不是目录。\n");
return 0;
}
strcat(para,recent-〉filename);
}
if(strcmp(recent- 28、>filename,recentpara)!=0 || recent==NULL)
{
strcpy(para,temppara);
recent=temp;
printf("输入路径错误\n");
return 0;
}
}
return 1;
}
int del()
{
char filename[FILENAME_LENGTH];
cin〉>filename;
temp=new fnode;
if(recent-〉child)
{
temp=recent-〉chil 29、d;
while(temp->next && (strcmp(temp->filename,filename)!=0 || temp—〉isdir!=0))
temp=temp->next;
if(strcmp(temp->filename,filename)!=0)
{
cout〈〈”不存在该文件!”〈〈endl;
return 0;
}
}
else
{
cout<〈"不存在该文件!"<〈endl;
return 0;
}
if(temp-〉parent==NULL)
{
t 30、emp—〉prev—〉next=temp—〉next;
if(temp->next)
temp—>next—>prev=temp-〉prev;
temp—>prev=temp—〉next=NULL;
}
else
{
if(temp—>next)
temp—〉next->parent=temp->parent;
temp-〉parent-〉child=temp-〉next;
}
delete temp;
cout<〈”文件已删除!"〈〈endl;
}
bool chklogin(char 31、*users, char *pwd)
{
int i;
for(i=0; i<8; i++)
{
if( (strcmp(users,usrarray[i].name)==0) && (strcmp(pwd,usrarray[i].pwd)==0))
return true;
}
return false;
}
void help(void)
{
cout<〈" 命 令 一 览 ”<〈endl;
cout〈〈endl;
cout〈〈"create: 32、建立文件。 ”〈 33、换目录。 ”<〈endl;
cout<〈"logout: 退出登录。 ”〈〈endl;
}
int run()
{
cout<<”linux:”<〈para〈〈"〉";
cin>>command;
if(strcmp(command,”mkdir”)==0)
mkdir();
else if(strcmp(command,"dir")==0)
dir();
else if(strcmp(command,"cd")==0)
cd();
els 34、e if(strcmp(command,"create”)==0)
create();
else if(strcmp(command,”read")==0)
read();
else if(strcmp(command,"write")==0)
write();
else if(strcmp(command,”del")==0)
del();
else if(strcmp(command,"help”)==0)
help();
else if(strcmp(command,”logout”)==0)
return 0;
e 35、lse
cout<〈"请参考help提供的命令列表!"<〈endl;
}
int main()
{
int i=0;
bool in=false;
char users[8],pwd[12];
cout〈〈”|———-——---——---—-—————---——-—---—---—————---—-—-————-----—---——-—-|”< 36、usr1-usr8 密码:usr1-usr8 |"〈






