多用户多级目录文件系统的实现课程设计_第1页
多用户多级目录文件系统的实现课程设计_第2页
多用户多级目录文件系统的实现课程设计_第3页
多用户多级目录文件系统的实现课程设计_第4页
多用户多级目录文件系统的实现课程设计_第5页
已阅读5页,还剩23页未读 继续免费阅读

下载本文档

版权说明:本文档由用户提供并上传,收益归属内容提供方,若内容存在侵权,请进行举报或认领

文档简介

1、课 程 设 计 课程名称_操作系统 _ 题目名称_多用户多级目录文件系统的实现 _ 学生学院_计算机学院_文件管理系统一 设计目的:编写并调试一个树型目录结构的文件系统,模拟文件管理工作流程。二 设计要求: 设计多用户文件系统,采用多级文件目录。至少要有十个以上的实用命令,应设置文件保护措施。设计一个较实用的用户界面,方便用户使用,界面要为用户提供足够的选择信息,不需用户打入冗长的命令。三 设计内容本程序设计了一个树形目录结构的文件管理系统,初始化系统默认根目录为root,根目录下为11个用户的注册目录,包括一个超级用户su和十个普通用户stud01stud10,初始化的树形分支结构如下:ro

2、ot su stud01 stud02 stud03 stud04 stud05 stud06 stud07 stud08 stud09stud10初始化后各个用户注册目录下均为空,用户可进行创建编辑文件,显示文件内容,删除文件,复制文件,粘贴文件,创建目录,删除目录,更改工作目录,显示当前工作目录的全路径,显示当前目录下的全部内容,寻求帮助及用户切换等操作,当只有目录可以为分支节点创建其目录下的子目录或者文件(这里指的文件为除目录外的终端文件),也可以为叶子节点以待必要的时候在其目录下创建子目录或者文件使其成为分支节点,而文件只能是叶子节点。程序运行时要求输入用户名及其密码,初始化时su和s

3、tud01stud10相应的密码分别为su,01stud,02stud,03stud,04stud,05stud,06stud,07stud,08stud,09stud,10stud,系统将验证输入的用户名及其密码,并提示是否输入正确,错误要求重新输入。用户密码只有超级用户su才能更改,普通用户没有权限进行此操作。登录后系统在每个命令提示符前加【用户名】,表示当前的登录用户,若登录用户为su时,命令提示符为#符号,若为普通用户,命令提示符为$符号,而且工作目录也指向当前登录用户的注册目录下。而只有登录用户为su时才可以访问根目录及其所有用户的注册目录下的所有目录以及文件,登录用户为普通用户时只

4、能访问根目录以及当前用户注册目录下的所有子目录和文件,如当前用户为stud01,就无权访问su以及stud02stud10目录下及其目录下的所有子目录和文件。进入系统后,根据系统各命令的功能进行相应的操作,系统所有的命令参数及其功能如下:su,stud01,stud02,stud03,stud04,stud05,stud06,stud07,stud08,stud09,stud10:输入用户名进行用户切换,输入用户名后系统会要求输入用户密码,只有输入正确,用户切换才能成功;pwd:显示当前工作目录的全路径名; ls:列出当前目录下的内容,包括所有子目录以及文件,列出时在目录名后面标记“/”,在终

5、端文件后面标记“*”; mkdir:在当前目录下建立子目录,输入此命令后,系统提示“directory name:”,要求输入要建立的子目录名,倘若当前目录下已经存在同名子目录,系统会提示“directory already exist”; rmdir:删除当前目录下已经存在的子目录,输入此命令后,系统提示“directory name:”,即输入要删除的子目录名,倘若你要删除的子目录在当前目录下不存在,系统提示“directory not exist”;vi:在当前目录下建立编辑文件,输入此命令后,系统提示“file name:”,要求输入要编辑的文件名,倘若当前目录下已经存在该文件,系统会

6、显示该文件内容,进入编辑界面,若当前目录下没有该文件创建进入编辑界面;vi编辑状态下,提示符为“:”,编辑命令及其功能为:i进行文本行的输入,系统提示输入要输入文本的行,若该行已存在则清空重新等待输入;d删除指定的行,系统提示“linenum:”,输入行数进行删除;list显示当前编辑文本的内容;wq对文本修改进行存储并退出,系统同时提示“now saving the file!press any key and exit.”;cat:显示当前目录下文件的内容,输入此命令后,系统提示“file name:”,倘若你要查看的文件不在当前目录下,系统提示“file not exist”; rm:删

7、除当前目录下已经存在的文件,输入此命令后,系统提示“file name:”,倘若你要删除的文件不在当前目录下,系统提示“file not exist”;cd:改变当前目录,输入此命令后,系统提示“directory name or symbol:”,要求输入子目录名字或者父目录标识,当输入子目录名时,若存在,当前的工作目录会改到子目录,若不存在,系统提示“subdirectory not exist!”,当输入“.”时,当前目录会返回到上一级父目录,若当前目录已经是根目录,系统会提示“that is already root directory!”,当输入“/”,工作目录返回到根目录root,

8、当输入“.”,当前的工作目录保持不变,当输入“”,即直接按回车而不输入任何字符,工作目录将返回用户注册目录下;cp:复制文件内容到缓冲区以备粘贴,输入此命令后,系统提示“file name:”,倘若你要复制的文件不在当前目录下,系统提示“file not exist”;paste:创建新的文件并将缓冲区的内容写进或者用缓冲区的内容覆盖已存在文件的内容,输入此命令后,系统提示“file name:”,若当前目录下不存在此文件,则创建并写将缓冲区的内容,若存在,系统提示“file already exist!cover it or not?y/n:”,输入y或y覆盖原有文件,输入n或n拒绝此操作。

9、chpw:在超级用户状态下更改用户密码,如当前用户不为su运行此命令,系统会提示“you have no right to change user password!”; help:查看系统命令参数的功能;exit:退出文件管理系统。四 程序运行举例程序运行时,当输入用户名su和密码su后系统提示登录成功,按任意键进入文件管理界面。进入用户操作界面后,由于当前的用户为su,故命令提示符为#,提示符前也会显示当前用户名,输入pwd确定当前的工作目录,登录后系统默认目录为用户注册目录/root/su。在当前目录下创建新的目录chaoyang和新的文件qq,并编辑文件qq,最后用wq保存并退出,用l

10、s命令查看当前目录下的所有内容,目录名的后缀为/,文件为*。当输入命令为copy时,系统提示“command not found”,即不存在此命令参数,当输入cp而输入要复制的文件名为chaoyang,系统提示要复制的文件不存在“file not exist”,随后复制文件qq并粘贴创建新的文件ye,用cat命令查看文件内容,可确定复制成功。用ls查看当前目录下的全部内容并用rm命令删除文件qq,用ls确定是否已删除该文件。进行工作目录更改,运行命令cd,根据提示“directory name or symbol:”输入chaoyang,进入chaoyang目录下,用pwd查看目录路径确认,用

11、cd加标志/返回根目录,用cd返回用户注册目录下。用命令cd加标识“.”返回当前工作目录的上一级目录,如/root/su返回到/root,在根目录下用cd命令,根据要求输入用户名进入该用户的注册目录访问,若当前的登录用户不为超级用户,无权访问其他用户的注册目录及其以下所有内容。输入用户名stud01,系统要求输入相应的密码01stud,若验证成功命令提示符变为$,其左边为stud01,表明当前的登录用户已切换到stud01,返回到root目录下运行cd加目录名stud02,企图进入目录stud02进行访问,系统提示“current user don't access other use

12、rs' files or dirrctory!”,即普通用户不能访问其他用户注册目录及其一下所有文件和子目录。在登录用户为stud01的情况下,输入命令chpw,系统提示你没有权力更改用户密码“you have no right to change user password!”,重新切换到用户su,将stud01用户的登录密码改为01,运用新的密码重新登录。返回工作目录到根目录下,进行创建目录,创建文件,删除目录,删除文件,系统会警告“don't allow this operation in root directory!”,在根目录下不允许这些操作,即使在超级用户下也不行

13、。运行help参看命令参数及其功能。五 设计思想创建节点的结构属性为:struct nodechar name256;/*节点名称*/char code256;/*若节点为用户注册目录,保存相应的用户密码*/int attribute;/*attribute为1时表示节点为目录,为0表示节点为文件*/struct node *parent,*firstchild,*nextsibling;/*定义双亲节点,孩子节点,兄弟节点*/struct line *start,*last;*p;/*定义节点的结构属性*/指针p时刻指向当前的工作目录,fistchild指向节点的第一个孩子,nextsibl

14、ing链接相邻的节点以便进行访问。文件以及目录的创建,查看,复制,粘贴,删除等所有操作都是基于结构体以及链表的操作,同时编辑文件的操作也是采用链表对编辑内容进行记忆,所以所有操作并不是实际的磁盘操作,只有程序运行完才释放所有的内存空间。因此只要程序运行完重新运行,原先在初始化目录下进行的相关操作结果都不会存在,这也是该程序局限性所在,有待改善。六 程序运行环境本程序是在turboc2.0下设计和调试的,设计语言为c语言。七 设计感悟在熟悉unix文件系统结构的基础上,通过所学的c语言和数据结构知识设计此文件管理系统,深化了自己对c语言以及链表结构体等数据结构的理解和应用,同时加深了对操作系统之

15、文件系统中文件和目录相关操作与内存磁盘分配和释放的关系。八 源程序及其注释#include"string.h"#include"stdio.h"#include"stdlib.h"#include"math.h"#include"conio.h"#define getnode(type)(type*)malloc(sizeof(type)char sign;/*命令提示符*/char title256;/*当前用户名*/int num=255;int symbol=0;int show=0;ch

16、ar str256;struct linechar text81;int num;/*行号*/struct line *next;/*指向下一个输入项目的指针*/struct line *prior;/*指向前一项目的指针*/;/*定义文件编辑行的结构属性*/struct line *start;/*指向表中第一项目的指针*/struct line *last;/*指向表中最后一个项目的指针*/struct line *begin;/*指向缓冲区第一项目的指针*/struct line *end;/*指向缓冲区最后一个项目的指针*/struct line *find(),*dls_store(

17、);struct nodechar name256;/*节点名称*/char code256;/*若节点为用户注册目录,保存相应的用户密码*/int attribute;/*attribute为1时表示节点为目录,为0表示节点为文件*/struct node *parent,*firstchild,*nextsibling;/*定义双亲节点,孩子节点,兄弟节点*/struct line *start,*last;*p;/*定义节点的结构属性*/typedef struct node node;node *root,*g,*su,*user1,*user2,*user3,*user4,*user

18、5,*user6,*user7,*user8,*user9,*user10;main()p=getnode(node);initialization();entry();while(symbol=0)clrscr();entry();clrscr();printf("%s%c",title,sign);while(num!=0)gets(str);operation();switch(num)case 1:pwd();printf("n%s%c",title,sign);num=255;break;case 2:ls();printf("n%s

19、%c",title,sign);num=255;break;case 3:mkdir();printf("n%s%c",title,sign);num=255;break;case 4:cd();printf("n%s%c",title,sign);num=255;break;case 5:vi();printf("n%s%c",title,sign);num=255;break;case 6:rm();printf("n%s%c",title,sign);num=255;break;case 7:rmd

20、ir();printf("n%s%c",title,sign);num=255;break; case 8:help();printf("n%s%c",title,sign);num=255;break;case 9:cat();printf("n%s%c",title,sign);num=255;break;case 10:change();printf("n%s%c",title,sign);num=255;break;case 11:copy();printf("n%s%c",title,

21、sign);num=255;break;case 12:paste();printf("n%s%c",title,sign);num=255;break;case 13:chpw();printf("n%s%c",title,sign);num=255;break;default:printf("ncommand not foundn%s%c",title,sign);initialization()/*初始化根目录和用户目录*/root=getnode(node);strcpy(root->name,"root&qu

22、ot;);root->attribute=1;root->parent=null;user10=getnode(node);strcpy(user10->name,"stud10");strcpy(user10->code,"10stud");user10->attribute=1;user10->parent=root;user10->firstchild=null;user10->nextsibling=null;user9=getnode(node);strcpy(user9->name,&qu

23、ot;stud09");strcpy(user9->code,"09stud");user9->attribute=1;user9->parent=root;user9->firstchild=null;user9->nextsibling=user10;user8=getnode(node);strcpy(user8->name,"stud08");strcpy(user8->code,"08stud");user8->attribute=1;user8->parent

24、=root;user8->firstchild=null;user8->nextsibling=user9;user7=getnode(node);strcpy(user7->name,"stud07");strcpy(user7->code,"07stud");user7->attribute=1;user7->parent=root;user7->firstchild=null;user7->nextsibling=user8;user6=getnode(node);strcpy(user6->n

25、ame,"stud06");strcpy(user6->code,"06stud");user6->attribute=1;user6->parent=root;user6->firstchild=null;user6->nextsibling=user7;user5=getnode(node);strcpy(user5->name,"stud05");strcpy(user5->code,"05stud");user5->attribute=1;user5->

26、parent=root;user5->firstchild=null;user5->nextsibling=user6;user4=getnode(node);strcpy(user4->name,"stud04");strcpy(user4->code,"04stud");user4->attribute=1;user4->parent=root;user4->firstchild=null;user4->nextsibling=user5;user3=getnode(node);strcpy(user3

27、->name,"stud03");strcpy(user3->code,"03stud");user3->attribute=1;user3->parent=root;user3->firstchild=null;user3->nextsibling=user4;user2=getnode(node);strcpy(user2->name,"stud02");strcpy(user2->code,"02stud");user2->attribute=1;user

28、2->parent=root;user2->firstchild=null;user2->nextsibling=user3;user1=getnode(node);strcpy(user1->name,"stud01");strcpy(user1->code,"01stud");user1->attribute=1;user1->parent=root;user1->firstchild=null;user1->nextsibling=user2;su=getnode(node);strcpy(su

29、->name,"su");strcpy(su->code,"su");su->attribute=1;su->parent=root;su->firstchild=null;su->nextsibling=user1;root->firstchild=su;root->nextsibling=null;entry()/*系统登录界面,验证用户及密码*/int h;char username256,password256;h=0;g=getnode(node);g=su;printf("nn&quo

30、t;);printf("tt*file manage system*n");printf("nnnnnnn");printf("ttuser:");gets(username);printf("n");printf("ttpassword:");gets(password);while(g!=null)if(strcmp(username,g->name)=0)h=1;if(strcmp(password,g->code)=0)symbol=1;printf("nttsuc

31、ceed in logging on!press any key and enter.");p=g;if(strcmp(username,"su")=0) sign='#'else sign='$'strcpy(title,username);else printf("nttincorrect password!");break;else g=g->nextsibling;if(h=0) printf("nttincorrect username!");getch();change()/

32、*切换用户*/node *u;char password256;printf("npassword:");gets(password);u=root->firstchild;while(strcmp(str,u->name)!=0)u=u->nextsibling;if(strcmp(password,u->code)!=0) printf("nincorrect password!");elsep=u;g=u;strcpy(title,str);if(strcmp(str,"su")=0) sign='

33、;#'else sign='$'begin=null;end=null;show=0;operation()/*验证输入的命令参数*/node *u;u=root->firstchild;if(strcmp(str,"exit")=0)num=0;else if(strcmp(str,"pwd")=0)num=1;else if(strcmp(str,"ls")=0)num=2;else if(strcmp(str,"mkdir")=0)num=3;else if(strcmp(str

34、,"cd")=0)num=4;else if(strcmp(str,"vi")=0)num=5;else if(strcmp(str,"rm")=0)num=6;else if(strcmp(str,"rmdir")=0)num=7;else if(strcmp(str,"help")=0)num=8;else if(strcmp(str,"cat")=0)num=9;else if(strcmp(str,"cp")=0)num=11;else if(st

35、rcmp(str,"paste")=0)num=12;else if(strcmp(str,"chpw")=0) num=13;else while(u!=null&&strcmp(str,u->name)!=0)u=u->nextsibling;if(u!=null)num=10;pwd()/*显示当前的工作路径*/node *t,*l,*k;l=getnode(node);l->firstchild=null;t=p;dok=getnode(node);strcpy(k->name,p->name);k-

36、>firstchild=l->firstchild;l->firstchild=k;p=p->parent;while(p!=null);for(k=l->firstchild;k!=null;k=k->firstchild)printf("/%s",k->name);printf("n");p=t;mkdir()/*创建子目录*/if(p!=root)node *q,*t,*w;t=p;w=p;q=getnode(node);printf("ndirectory name:");scanf(

37、"%s",q->name);q->attribute=1;w=w->firstchild;while(strcmp(w->name,q->name)!=0|w->attribute!=1)&&w!=null)w=w->nextsibling;if(w!=null) printf("ndirectory already exist");elseq->parent=p;q->firstchild=null;q->nextsibling=p->firstchild;p->f

38、irstchild=q;p=t;gets(str);else printf("ndon't allow this operation in root directory!");ls()/*显示当前目录下的所有内容,后缀为/的表示目录,*的表示文件*/node *t;t=p;p=p->firstchild;printf("n");while(p!=null)if(p->attribute=1) printf("%s/t",p->name);else printf("%s*t",p->n

39、ame);p=p->nextsibling;p=t;cd()/*改变当前的工作目录*/node *t;int flag=1;printf("ndirectory name or symbol:");gets(str);if(strcmp(str,".")=0)/*返回当前目录的上一级目录*/if(p->parent!=null) p=p->parent;else printf("nthat is already root directory!");else if(strcmp(str,"")=0)

40、/*返回用户注册子目录*/p=g;else if(strcmp(str,"/")=0)/*返回根目录*/while(p->parent!=null) p=p->parent;else if(strcmp(str,".")=0)/*保持当前目录不变*/else/*当前目录改变为指定的子目录*/if(p!=root|sign!='$')t=p;p=p->firstchild;while(p!=null&&flag)if(strcmp(str,p->name)=0&&p->attri

41、bute=1) flag=0;else p=p->nextsibling;if(p=null)printf("nsubdirectory not exist!");p=t;elseif(strcmp(str,g->name)!=0)printf("ncurrent user don't access other users' files or dirrctory!");else p=g;rm()/*删除文件*/if(p!=root)node *t,*s;t=p;printf("nfile name:");g

42、ets(str);p=p->firstchild;if(strcmp(str,p->name)=0&&p->attribute=0)t->firstchild=p->nextsibling;p=t;elsewhile(strcmp(str,p->name)!=0|p->attribute!=0)&&p!=null)s=p;p=p->nextsibling;if(p!=null)s->nextsibling=p->nextsibling;free(p);else printf("nfile no

43、t exist");p=t;else printf("ndon't allow this operation in root directory!");rmdir()/*删除子目录*/if(p!=root)node *t,*s;t=p;printf("ndirectory name:");gets(str);p=p->firstchild;if(strcmp(str,p->name)=0&&p->attribute=1)t->firstchild=p->nextsibling;p=t;els

44、ewhile(strcmp(str,p->name)!=0|p->attribute!=1)&&p!=null)s=p;p=p->nextsibling;if(p!=null)s->nextsibling=p->nextsibling;free(p);else printf("ndirectory not exist");p=t;else printf("ndon't allow this operation in root directory!");help()/*提示命令参数功能*/printf(

45、"n*command parameter*n");printf("su,stud01,stud02,stud03,stud04,stud05,stud06,stud07,stud08,stud09,stud10:n");printf(" help you switch to other user;n");printf("pwd:display the path of current directory or file;n");printf("ls:display all subdirectory and

46、file under current directory,symbol * n");printf(" denotes file name,symbol / denotes directory name;n"); printf("mkdir:create a subdirectory under current directory;n");printf("rmdir:delete a subdirectory under current directory;n");printf("vi:create a file u

47、nder current directory and edit;n");printf("rm:create a file under current directory;n");printf("cat:display file content;n");printf("cp:copy file content into buffer and save;n");printf("paste:creat a file and overwrite with buffer content;n");printf(&qu

48、ot;cd:choose the path and enter into subdurectory or parent directory,n");printf("tfor enample,when system requests to input the subdirectory name or then");printf(" parent directory symbol,if you input subdirectory name,current directoryn");printf(" will point to the s

49、ubdirectory,if you input .,current directory willn");printf(" point to father directory,if you input /,current directory will pointn");printf(" to root directory,if you input .,current directory will not alter,if inputn");printf(" nothing and enter,current directory wil

50、l point to registered user directory;n");printf("help:if you have any doubt,input help and seek system helpn");printf("exit:end the file system operationn");edit()/*对文件内容编辑*/int choice;char s80,fname80;struct line *info;int linenum=1;int guide=0;dochoice=menu_select();switch

51、(choice)case 1:printf("linenum:");gets(s);linenum=atoi(s);enter(linenum);break;case 2:delete();break;case 3:list();break;case 4:save();start=null;last=null;guide=1;break;while(guide=0);menu_select()/*验证文件编辑命令*/char s80;int c=0;printf("n");doprintf(":");gets(s);if(strcmp

52、(s,"i")=0)c=1;else if(strcmp(s,"d")=0) c=2;else if(strcmp(s,"list")=0) c=3;else if(strcmp(s,"wq")=0) c=4;while(c<1|c>6);return(c);enter(int linenum)/*将文本插在指定行端部*/struct line *info;char t81;for(;)info=(struct line *)malloc(sizeof(struct line);if(!info)pri

53、ntf("trun out of memory!n");return(null);printf("%d:",linenum);gets(info->text);info->num=linenum;if(*info->text)if(find(linenum)patchup(linenum,1);if(*info->text) start=dls_store(info);else break;linenum+;return(linenum);patchup(int n,int incr)/*在插入和删除操作之后重新对文本各行编号*/

54、struct line *i;i=find(n);while(i)i->num=i->num+incr;i=i->next;struct line *dls_store(struct line *i)/*按行号排序后存入*/struct line *old,*p;if(last=null)i->next=null;i->prior=null;last=i;return(i);p=start;old=null;while(p)if(p->num<i->num)old=p;p=p->next;elseif(p->prior)p->p

55、rior->next=i;i->next=p;p->prior=i;return(start);i->next=p;i->prior=null;p->prior=i;return(i);old->next=i;i->next=null;i->prior=old;last=i;return(start);delete()/*删除指定行*/struct line *info;char s80;int linenum;printf("linenum:");gets(s);linenum=atoi(s);info=find(li

56、nenum);if(info)if(start=info)start=info->next;if(start) start->prior=null;else last=null;elseinfo->prior->next=info->next;if(info!=last)info->next->prior=info->prior;else last=info->prior;free(info);patchup(linenum+1,-1);struct line *find(int linenum)/*查找一行文本*/struct line *info;info=start;while(info)if(linenum=info->num) return(info);info=info->next;return(null);cat()/*显示文件内容*/if(p!=root)node *t;t=p;printf("nfile name:");gets(str);p=p->firstchild;while(strcmp(str,p->name)!=0|

温馨提示

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

评论

0/150

提交评论