《基于Linux的模拟文件系统的设计与实现.doc》由会员分享,可在线阅读,更多相关《基于Linux的模拟文件系统的设计与实现.doc(28页珍藏版)》请在三一办公上搜索。
1、中北大学操作系统课程设计说 明 书学 院、系:软件学院专 业:软件工程学 生 姓 名:学 号:0921010414设 计 题 目:基于Linux的模拟文件系统的设计与实现 起 迄 日 期: 2011年12月22日- 2012年1月7日指 导 教 师:2012 年 1月 7 日1 需求分析 所要实现的功能: (1) 设计一个10个用户的文件系统。每个用户最多可以保存10个文件,一次运行用户可打开多个文件。(2) 程序采用二级文件目录。(即设置主目录(MFD)和用户文件目录(UFD)。另外,可打开文件设置指针。(3) 为了方便实现,对文件的读写作了简化。在执行读写命令时,只需改读写指针。并不进行实
2、际的读写操作。(4) 实现的基本功能主要包括:改变目录(CD),创建目录(MD),显示目录(DIR),删除目录(RD),打开全部文件(openall),打开单个文件(open),建立一个文件(create),删除一个文件(delete),写文件(write),读文件(read),改文件的保护码(change),退出(exit)等。考虑特殊情况如:各个命令对全路径和相对路径的支持、目录不存在时,给出错误信息、不能用cd进入文件、命令之中不能有空格(如 ex it,给出错误提示)、相对路径的解析、路径中的空格剔除、新建目录或文件时的问题、重名问题、目录或文件的名字长度限制、目录或文件的名字中包含不
3、合法字符(注意空格)、删除目录或文件时的问题、删除不存在的文件或目录给出错误提示、删除目录时目录不为空(如果该目录为空,则可删除,否则给出是否做删除提示,删除操作将该目录下的全部文件和子目录都删除)、进入到某个目录下,却要删除本目录或上级目录、不能用delete删除目录、不能用RD删除文件等都要考虑在内。最终获得的成果就是:在系统中用一个文件来模拟一个磁盘;此系统至少有:Create、delete、open、close、read、write等和部分文件属性的功能。实现这个文件系统。能实际演示这个文件系统。基本上是进入一个界面(此界面就是该文件系统的界面)后,可以实现设计的操作要求。2 总体设计
4、 2.1系统活动图 用户管理系统文件目录管理系统新建目录删除目录进入目录打开文件路径控制登陆退出 目录内文件管理系统新建文件读文件写文件 删除 移动登陆退出 注册 登陆 退出 2.2 头文件:#include #include #include 2.3结构体:typedef struct file char name10; struct file *next;FILE *fp;File;typedef struct contentchar name10;File *fileHead;int f_num;struct content *next;Content;typedef struct us
5、er char name10; char psw10; Content *conHead;int c_num; struct user *next;User;2.4 全局变量和函数:void registe()int login()void MenuUser()void FolderCreate()void FolderDelete()void FolderViewAllFiles()void FolderIntoFolder()void MenuFolder()void FileCreate()void FileRead()void FileWrite()void FileDelete()v
6、oid FileMove()3 详细设计用户管理系统:registe():用户注册login():用户登录目录管理系统:FolderCreate():新建目录FolderDelete():删除目录FolderIntoFolder():进入目录FolderViewAllFiles():显示所有目录内的所有文件absolutePathFileController():通过绝对路径直接读文件文件管理系统:FileCreate():新建文件FileDelete():删除文件FileMove():跨目录移动文件FileRead():读文件FileWrite():写文件3.1 用户管理系统: 截图3-1相
7、关核心代码:/step0void registe()if(user_num=10)printf(Has 10 users.You have no access to registern);return;/allocate new memory block for new userUser *add,*last;add=(User *)malloc(sizeof(User);add-next=NULL;if(user_head=NULL)user_head=add;elselast=user_head;while(last-next!=NULL)last=last-next;last-next=
8、add;user_num+;/fulfill the new userchar name10,psw10;User *check;char buffer2;fgets(buffer,2,stdin);unr:printf(now create a new user:nuser name(9,only alphabet and number can be used):n);scanf(%s,name);check=user_head;while(check!=add)if(strcmp(name,check-name)=0)printf(username exist.please re-inpu
9、t:n);goto unr;check=check-next;strcpy(add-name,name);fgets(buffer,2,stdin);printf(password(10):n);fgets(psw,10,stdin);strcpy(add-psw,psw);add-conHead=NULL;add-c_num=0;/successprintf(successn);return;/step0int login()if(user_head=NULL)printf(system has no usern);return 0;char buffer2;fgets(buffer,2,s
10、tdin);char name10,psw10;User *check;printf(press # if you wanna quitn);unl:printf(username:n);scanf(%s,name);if(name0=#)return 0;check=user_head;while(check!=NULL)if(!strcmp(check-name,name)break;elsecheck=check-next;if(check=NULL)printf(no such user.nplease re-input:n);goto unl;/match passwordfgets
11、(buffer,2,stdin);printf(password:n);fgets(psw,11,stdin);if(strcmp(psw,check-psw)!=0)printf(wrong passwordn);goto unl;printf(nn*welcome back!%s*nn,check-name);cur_user=check;return 1;void main(int argc, char* argv)user_num=0;user_head=NULL;cur_user=NULL;int choice;for(;)printf(*MAIN MENU*n);printf(1.
12、 New usern);printf(2. Loginn);printf(0. Exitn);printf(*n);printf(%d user existn,user_num);printf(Input Your Choice:);scanf(%d,&choice);switch(choice)case 1:registe();continue;case 2:if(login()MenuUser();continue;case 0:printf(Thanks for usingn);exit(0);default:printf(wrong input!n);3.2 目录管理系统: 截图3-2
13、相关核心代码:/step0-1void MenuUser()void FolderCreate();void FolderDelete();void FolderViewAllFiles();void FolderIntoFolder();void absolutePathFileController();User *current;Content *goThrough;current=cur_user;goThrough=current-conHead;for(;)printf(*%ss Content*n,cur_user-name);printf(1.create a foldern);
14、printf(2.get in to a foldern);printf(3.delete a foldern);printf(4.view all filesn);printf(5.read a file directly by absolute pathn);printf(6.log outn);printf(0.shut down FILE_SYSTEMn);printf(view all your folders below:%d folders totaln,current-c_num);if(current-conHead=NULL)printf(|nno folder.n);el
15、segoThrough=current-conHead;while(goThrough!=NULL)printf(|n%sn,goThrough-name);goThrough=goThrough-next;printf(*);printf(ninput your choice:n);int choice;scanf(%d,&choice);switch(choice)case 1:FolderCreate();continue;case 2:FolderIntoFolder();continue;case 3:FolderDelete();continue;case 4:FolderView
16、AllFiles();continue;case 5:absolutePathFileController();continue;case 6:printf(nThanks for usingnn);return;case 0:printf(nThanks for usingn);cur_user=NULL;cur_con=NULL;exit(0);default:printf(wrong input!n);/step1void FolderCreate()User *user;user=cur_user;/allocate new memory block for new folderCon
17、tent *add,*last;add=(Content *)malloc(sizeof(Content);add-next=NULL;if(user-conHead=NULL)user-conHead=add;elselast=user-conHead;while(last-next!=NULL)last=last-next;last-next=add;user-c_num+;add-fileHead=NULL;add-f_num=0;/fulfill the new folder namechar name10;Content *check;char buffer2;fgets(buffe
18、r,2,stdin);con_r:printf(now create a new folder:nfolder name(9,only alphabet and number can be used):n);scanf(%s,name);check=user-conHead;while(check!=add)if(strcmp(name,check-name)=0)printf(folder name exist.please re-input:n);goto con_r;check=check-next;/check legality of the nameint i=0;for(;i=97
19、&namei=48&namei=65&namei=90)/uppercontinue;elseif(namei=0)break;elseprintf(is a illegal input!nplease re-input:n,namei);goto con_r;strcpy(add-name,name);printf(DONEnn);return;/step1void FolderDelete()if(cur_user-conHead=NULL)printf(NO Foldernn);return;Content *front,*current;char name10;printf(press
20、 # if you wanna quitn);con_d:printf(folder name:n);scanf(%s,name);if(name0=#)return;/seek folder namecurrent=cur_user-conHead;while(current!=NULL)if(!strcmp(current-name,name)break;elsecurrent=current-next;if(current=NULL)printf(no such folder.nplease re-input:n);goto con_d;/if has any fileswhile(cu
21、rrent-fileHead!=NULL)char buffer2;fgets(buffer,2,stdin);printf(folder not empty!IF you sure you wanna delete?(Y/N)n);char judge;scanf(%c,&judge);if(judge=n|judge=y|judge=N|judge=Y)if(judge=n|judge=N)return;elsebreak;elseprintf(Please input Y OR Nn);continue;/user recognisechar psw10;printf(input you
22、r user password to recognise:);char buffer2;fgets(buffer,2,stdin);fgets(psw,10,stdin);if(strcmp(cur_user-psw,psw)!=0)printf(WRONG PASSWORD!nDELETE FUNCTION FAILLEDn);return;/execute delete operationfront=cur_user-conHead;if(front=current)/if curren is the head filecur_user-conHead=current-next;free(
23、current);printf(Has been deletedn1n);return;while(front-next!=current)front=front-next;front-next=current-next;free(current);cur_user-c_num-;printf(Has been deletedn2n);return;/step1void FolderViewAllFiles()printf(nscan all your file:n);Content *con;con=cur_user-conHead;int fi_num=0;while(con!=NULL)
24、printf(|%s-n,con-name);File *fi;fi=con-fileHead;while(fi!=NULL)printf(|%sn,fi-name);printf(|n);fi=fi-next;fi_num+=con-f_num;con=con-next;printf(total:%d Folders,%d Filesnn,cur_user-c_num,fi_num);return;/step1void FolderIntoFolder()void MenuFolder();if(cur_user-conHead=NULL)printf(You has no Foldern)
25、;return;printf(now get into a Folder:nFolder name(9):n);char name10;char buffer2;fgets(buffer,2,stdin);Content *check;printf(type # if you wanna quitn);con_g:printf(folder name:ncd );scanf(%s,name);if(name0=#)return;check=cur_user-conHead;while(check!=NULL)if(!strcmp(check-name,name)break;elsecheck=
26、check-next;if(check=NULL)printf(no such folder.nplease re-input:n);goto con_g;/successcur_con=check;MenuFolder();return;/step void absolutePathFileController()printf(n_n);printf(format:/n);printf(_n);char name28;printf(press # if you wanna quitn);absolute:printf(absolute file path:n);scanf(%s,name);
27、if(name0=#)return;char folder_name10;char file_name14;/analyse the absolute path/get folder nameif(name0=/)printf(no folder name!please re-input:n);goto absolute;int i=0,j=0;while(namei!=/&i=9)if(namei=0)printf(WRONG FORMATn);goto absolute;folder_namei=namei;i+;folder_namei=0;i+;/get file nameif(nam
28、ei=0)printf(NO ,PLEASE RE-INPUTn);goto absolute;while(namei+j!=0&jconHead;while(folder_checker!=NULL)if(!strcmp(folder_checker-name,folder_name)break;elsefolder_checker=folder_checker-next;if(folder_checker=NULL)printf(no such folder.nplease re-input:n);goto absolute;/seek fileFile *file_checker;fil
29、e_checker=folder_checker-fileHead;while(file_checker!=NULL)if(!strcmp(file_checker-name,file_name)break;elsefile_checker=file_checker-next;if(file_checker=NULL)printf(no such file.nplease re-input:n);goto absolute;/read the filechar fullName20=PRO/;strcat(fullName,file_name);printf(=ndetails:nn);fil
30、e_checker-fp=fopen(fullName,r);char ch;while(ch!=EOF)putchar(ch=getc(file_checker-fp);printf(n=n);fclose(file_checker-fp);return;3.2 文件管理系统: 截图3-3相关核心代码:/step1-2void MenuFolder()void FileCreate();void FileRead();void FileWrite();void FileDelete();void FileMove();File *goThrough;Content *current;curr
31、ent=cur_con;goThrough=current-fileHead;char buffer2;int choice;for(;)fgets(buffer,2,stdin);printf(nFolder:%sn,current-name);printf(1.create a new filen);printf(2.read a filen);printf(3.write to a filen);printf(4.delete a filen);printf(5.move a filen);printf(6.back to user-menun);printf(0.shut down F
32、ILE_SYSTEMnn);printf(view all your files below:%d files totaln,current-f_num);if(current-fileHead=NULL)printf(|nno file.n);elsegoThrough=current-fileHead;while(goThrough!=NULL)printf(|n%sn,goThrough-name);goThrough=goThrough-next;printf();printf(ninput your choice:n);choice=-1;scanf(%d,&choice);swit
33、ch(choice)case 1:FileCreate();continue;case 2:FileRead();continue;case 3:FileWrite();continue;case 4:FileDelete();continue;case 5:FileMove();continue;case 6:printf(nnow in your UFD:nn);cur_con=NULL;return;case 0:printf(nThanks for usingn);cur_user=NULL;cur_con=NULL;exit(0);default:printf(wrong input
34、!n);/step2void FileCreate()Content *cur_content;cur_content=cur_con;/allocate new memory block for new files indexFile *add,*last;add=(File *)malloc(sizeof(File);add-next=NULL;add-fp=NULL;if(cur_content-fileHead=NULL)cur_content-fileHead=add;elselast=cur_content-fileHead;while(last-next!=NULL)last=l
35、ast-next;last-next=add;cur_content-f_num+;/fulfill the new folder namechar name10;File *check;char buffer2;fgets(buffer,2,stdin);file_r:printf(now create a new File:nfile name(9,only alphabet and number can be used):n);scanf(%s,name);check=cur_content-fileHead;while(check!=add)if(strcmp(name,check-n
36、ame)=0)printf(file name exist.please re-input:n);goto file_r;check=check-next;/check legality of the nameint i=0;for(;i=97&namei=48&namei=65&namei=90)/uppercontinue;elseif(namei=0)break;elseprintf(is a illegal input!nplease re-input:n,namei);goto file_r;strcpy(add-name,name);char fullName20=PRO/;str
37、cat(fullName,name);add-fp=fopen(fullName,w);if(add-fp=NULL)printf(ERROR WHEN CREATE A FILE IN DESKnPEASE CONTACT SYSTEM MANAGERn);goto file_r;fclose(add-fp);printf(DONEnn);return;/step2void FileRead()if(cur_con-fileHead=NULL)printf(NO Filenn);return;printf(nnow read a file:n);File *current;char name10;printf(press # if you wanna quitn);file_r:printf(file name:n);scanf(%s,name);if(n