基于Linux的模拟文件系统的设计与实现

上传人:仙*** 文档编号:28329242 上传时间:2021-08-26 格式:DOC 页数:29 大小:156.50KB
收藏 版权申诉 举报 下载
基于Linux的模拟文件系统的设计与实现_第1页
第1页 / 共29页
基于Linux的模拟文件系统的设计与实现_第2页
第2页 / 共29页
基于Linux的模拟文件系统的设计与实现_第3页
第3页 / 共29页
资源描述:

《基于Linux的模拟文件系统的设计与实现》由会员分享,可在线阅读,更多相关《基于Linux的模拟文件系统的设计与实现(29页珍藏版)》请在装配图网上搜索。

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 name

38、10;printf(press # if you wanna quitn);file_r:printf(file name:n);scanf(%s,name);if(name0=#)return;/seek file namecurrent=cur_con-fileHead;while(current!=NULL)if(!strcmp(current-name,name)break;elsecurrent=current-next;if(current=NULL)printf(no such file.nplease re-input:n);goto file_r;char fullName2

39、0=PRO/;strcat(fullName,name);printf(=ndetails:nn);current-fp=fopen(fullName,r);char ch;while(ch!=EOF)putchar(ch=getc(current-fp);printf(n=n);fclose(current-fp);return;/step2void FileWrite()if(cur_con-fileHead=NULL)printf(NO Filenn);return;printf(nnow read a file:n);File *current;char name10;printf(p

40、ress # if you wanna quitn);file_r:printf(file name:n);scanf(%s,name);if(name0=#)return;/seek file namecurrent=cur_con-fileHead;while(current!=NULL)if(!strcmp(current-name,name)break;elsecurrent=current-next;if(current=NULL)printf(no such file.nplease re-input:n);goto file_r;char fullName20=PRO/;strc

41、at(fullName,name);printf(=nType details:(tpye in # to finish the text)nn);current-fp=fopen(fullName,a+);char ch;ch=getchar();while(ch!=#)putc(ch,current-fp);ch=getchar();fclose(current-fp);printf(=);return;/step2void FileDelete()if(cur_con-fileHead=NULL)printf(NO Filenn);return;File *front,*current;

42、char name10;printf(press # if you wanna quitn);file_d:printf(file name:n);scanf(%s,name);if(name0=#)return;/seek file namecurrent=cur_con-fileHead;while(current!=NULL)if(!strcmp(current-name,name)break;elsecurrent=current-next;if(current=NULL)printf(no such file.nplease re-input:n);goto file_d;/exec

43、ute delete operationfront=cur_con-fileHead;if(front=current)/if curren is the head filecur_con-fileHead=current-next;free(current);printf(Has been deletedn1n);return;while(front-next!=current)front=front-next;front-next=current-next;free(current);cur_con-f_num-;printf(Has been deletedn2n);return;/st

44、ep2void FileMove()printf(n=nnow move a file from Folder %s to another Foldern=n,cur_con-name);/move indexContent *target_con,*current_con;if(cur_con-fileHead=NULL)printf(NO Filenn);return;File *front_f,*current_f,*tar_f;char name10;printf(press # if you wanna quitn);file_m:printf(file name:n);scanf(

45、%s,name);if(name0=#)return;/seek file namecurrent_f=cur_con-fileHead;while(current_f!=NULL)if(!strcmp(current_f-name,name)break;elsecurrent_f=current_f-next;if(current_f=NULL)printf(no such file.nplease re-input:n);goto file_m;/get target folder valuechar buffer2;fgets(buffer,2,stdin);char tar_name1

46、0;printf(ninput target Folder name:n);printf(press # if you wanna quit)n);file_tar:printf(target folder name:n);scanf(%s,tar_name);if(tar_name0=#)return;/seek folder nametarget_con=cur_user-conHead;while(current_con!=NULL)if(!strcmp(target_con-name,tar_name)break;elsetarget_con=target_con-next;if(ta

47、rget_con=NULL)printf(no such folder.nplease re-input:n);goto file_tar;/seek target insert position then execute move operationtar_f=target_con-fileHead;front_f=cur_con-fileHead;if(tar_f=NULL)/if target folder emptytarget_con-fileHead=current_f;if(current_f=front_f)/if moving file is the first file o

48、f pre-blongcur_con-fileHead=current_f-next;else/moving file is not the firstwhile(front_f-next!=current_f)front_f=front_f-next;front_f-next=current_f-next;current_f-next=NULL;return;/if target folder is not emptywhile(tar_f-next!=NULL)tar_f=tar_f-next;tar_f-next=current_f;if(current_f=front_f)/if mo

49、ving file is the first file of pre-blongcur_con-fileHead=current_f-next;else/moving file is not the firstwhile(front_f-next!=current_f)front_f=front_f-next;front_f-next=current_f-next;current_f-next=NULL;return;4. 心得体会 操作系统课程设计是本课程重要的实践教学环节。课程设计的目的,一方面使学生更透彻地理解操作系统的基本概念和原理,使之由抽象到具体;另一方面,通过课程设计加强学生的实验手段与实践技能,培养学生独立分析问题、解决问题、应用知识的能力和创新精神。与本课程的实验教学相比,课程设计独立设课,具有更多的学时,给学生更多自行设计、自主实验的机会,充分放手让学生真正培养学生的实践动手能力,全面提高学生的综合素质。在设计的过程中遇到问题,可以说得是困难重重,难免会遇到过各种各样的问题,同时在设计的过程中发现了自己的不足之处,对以前所学过的知识理解得不够深刻,掌握得不够牢固,不过设计终于顺利完成了,在设计中遇到了很多编程问题,最后在老师的辛勤指导下,终于迎刃而解。28

展开阅读全文
温馨提示:
1: 本站所有资源如无特殊说明,都需要本地电脑安装OFFICE2007和PDF阅读器。图纸软件为CAD,CAXA,PROE,UG,SolidWorks等.压缩文件请下载最新的WinRAR软件解压。
2: 本站的文档不包含任何第三方提供的附件图纸等,如果需要附件,请联系上传者。文件的所有权益归上传用户所有。
3.本站RAR压缩包中若带图纸,网页内容里面会有图纸预览,若没有图纸预览就没有图纸。
4. 未经权益所有人同意不得将文件中的内容挪作商业或盈利用途。
5. 装配图网仅提供信息存储空间,仅对用户上传内容的表现方式做保护处理,对用户上传分享的文档内容本身不做任何修改或编辑,并不能对任何下载内容负责。
6. 下载文件中如有侵权或不适当内容,请与我们联系,我们立即纠正。
7. 本站不保证下载资源的准确性、安全性和完整性, 同时也不承担用户因使用这些下载资源对自己和他人造成任何形式的伤害或损失。
关于我们 - 网站声明 - 网站地图 - 资源地图 - 友情链接 - 网站客服 - 联系我们

copyright@ 2023-2025  zhuangpeitu.com 装配图网版权所有   联系电话:18123376007

备案号:ICP2024067431-1 川公网安备51140202000466号


本站为文档C2C交易模式,即用户上传的文档直接被用户下载,本站只是中间服务平台,本站所有文档下载所得的收益归上传人(含作者)所有。装配图网仅提供信息存储空间,仅对用户上传内容的表现方式做保护处理,对上载内容本身不做任何修改或编辑。若文档所含内容侵犯了您的版权或隐私,请立即通知装配图网,我们立即给予删除!