当前位置:文档之家› 操作系统课程设计-Linux二级文件系统设计

操作系统课程设计-Linux二级文件系统设计

操作系统课程设计报告专业:软件工程学号:姓名:马提交日期:2017/1/10【设计目的】1、通过一个简单多用户文件系统的设计,加深理解文件系统的内部功能和内部实现2、结合数据结构、程序设计、计算机原理等课程的知识,设计一个二级文件系统,进一步理解操作系统3、通过对实际问题的分析、设计、编程实现,提高学生实际应用、编程的能力【设计内容】为Linux系统设计一个简单的二级文件系统。

要求做到以下几点:1.可以实现下列几条命令:login 用户登录dir 列目录create 创建文件delete 删除文件open 打开文件close 关闭文件read 读文件write 写文件cd 进出目录2.列目录时要列出文件名,物理地址,保护码和文件长度3.源文件可以进行读写保护【实验环境】C++DevCpp【设计思路】本文件系统采用两级目录,其中第一级对应于用户账号,第二级对应于用户帐号下的文件。

另外,为了简便文件系统未考虑文件共享,文件系统安全以及管道文件与设备文件等特殊内容。

首先应确定文件系统的数据结构:主目录、子目录及活动文件等。

主目录和子目录都以文件的形式存放于磁盘,这样便于查找和修改。

用户创建的文件,可以编号存储于磁盘上。

如:file0,file1,file2…并以编号作为物理地址,在目录中进行登记。

结构体:typedef struct /*the structure of OSFILE*/{int fpaddr; /*file physical address*/int flength; /*file length*/int fmode; /*file mode:0-Read Only;1-Write Only;2-Read and Write; 3-Protect;*/char fname[MAXNAME]; /*file name*/} OSFILE; //存放重要信息typedef struct /*the structure of OSUFD*/{char ufdname[MAXNAME]; /*ufd name*/OSFILE ufdfile[MAXCHILD]; /*ufd own file*/}OSUFD; //用户下面的文件typedef struct /*the structure of OSUFD'LOGIN*/{char ufdname[MAXNAME]; /*ufd name*/char ufdpword[8]; /*ufd password*/} OSUFD_LOGIN;typedef struct /*file open mode*/{int ifopen; /*ifopen:0-close,1-open*/int openmode; /*0-read only,1-write only,2-read and write,3-initial*/ }OSUFD_OPENMODE;主要的函数说明:void LoginF(); /*LOGIN FileSystem用户登录*/void DirF(); /*Dir FileSystem列目录*/void CdF(); /*Change Dir改变目录*/void CreateF(); /*Create File创建文件*/void DeleteF(); /*Delete File删除文件*/void ModifyFM(); /*Modify FileMode修改*/void OpenF(); /*Open File打开文件*/void CloseF(); /*Close File关闭文件*/void ReadF(); /*Read File读文件*/void WriteF(); /*Write File写文件*/void QuitF(); /*Quit FileSystem离开文件系统*/void help();其他重要函数:void clrscr() //清屏int ExistD(char *dirname) /*Whether DirName Exist,Exist-i,Not Exist-0*/ int ExistF(char *filename) /*Whether FileName Exist,Exist-i,Not Exist-0*/ int FindPANo() /*find out physical address num*/void SetPANo(int RorW) /*Set physical address num,0-read,1-write*/ void InputPW(char *password) /*input password,use '*' replace*/char *ltrim(char *str) /*remove the heading blanks.去除左空白*/char *rtrim(char *str) /*remove the trailing blanks.去除右空白*/int WriteF1() /*write file相当于置换文件*/程序流程说明:整体流程:各部分功能流程:Open :N NY开始Open 获取文件名文件是否存在? 文件名不存在获取文件置为打开状态并获取文件模式打开文件成功结束【源程序清单】Open:void OpenF() /*Open File*/{printf("\n\nC:\\%s>",strupr(dirname)); //显示当前路径int fcoun, i; //定义两个整形变量char fname[MAXNAME], fmode[25]; //定义两个字符串变量int fmod; //文件模式printf("\nPlease input FileName:");gets(fname); //接收打开文件的文件名ltrim(rtrim(fname)); //去除左右空白if(ExistF(fname)<0) //判断文件是否存在{//不存在printf("\nError.文件名\'%s\'不存在\n", fname);wgetchar=1;} else {//存在i=ExistD(username); //获取用户物理信息for(int a=0; a < fcount[i]; a++) //遍历用户文件{if(strcmp(fname, ufd[i]->ufdfile[a].fname)==0) //找到文件{fcoun=a;break;}}ifopen[i][fcoun].ifopen=1; //将文件状态置为打开状态printf("Please input OpenMode(0-Read Only, 1-Write Only, 2-Read and Write, 3-Protect):");//打开文件模式gets(fmode); //获取模式fmod=atoi(fmode); //将字符串转换为整型ifopen[i][fcoun].openmode=fmod; //将文件的模式置为OpenModeprintf("\nOpen Successed");wgetchar=1;}}Delete:void DeleteF() /*Delete File*/{printf("\n\nC:\\%s>",strupr(dirname)); //显示路径char fname[MAXNAME], str[50], str1[50]; //定义三个字符串变量int i, k, j;int fpaddrno1; //记录文件物理地址块号if(strcmp(strupr(ltrim(rtrim(dirname))), "")==0){ //判断主目录是否为空printf("\nError.请确认您要删除的是否在用户目录下!\n");wgetchar=1;}if(strcmp(strupr(dirname), strupr(username))!=0){ //判断用户是否在用户目录下printf("\nError.您只能删除修改自己用户目录下的文件哦!\n");wgetchar=1;} else {printf("\nPlease input FileName:");gets(fname); //接收删除的文件名ltrim(rtrim(fname)); //去除文件名的左右空白i=ExistF(fname); //用户文件位置if(i>=0){k=ExistD(username); //获取用户所在存储位置if(ifopen[k][i].ifopen==1){//文件状态处于打开状态,不许删除printf("\n Error.\'%s\' 处于打开状态!请先关闭哟!\n", fname);wgetchar=1;} else {if(ufd[k]->ufdfile[i].fmode==3){ //保护文件,不可删除printf("\nError.\'%s\'处于被保护状态!请先关闭哟!\n", fname);wgetchar=1;} else {fpaddrno1=ufd[k]->ufdfile[i].fpaddr; //获取文件的物理地址块号fpaddrno[fpaddrno1]=0; //回收物理地址块号for(j=i; j<fcount[k]; j++) //将文件都向前移动{ufd[k]->ufdfile[j]=ufd[k]->ufdfile[j+1]; //将j+1位置为j}strcpy(str , "c:\\osfile\\file\\");itoa(fpaddrno1, str1, 10); //将整数转化为字符串strcat(str, str1);strcat(str, ".txt");//连接remove(str); //删除物理文件fcount[k--]; //文件个数减一printf("\n\'%s\'is deleted successfully.\n", fname);wgetchar=1;}}} else {printf("\nError.\'%s\'文件不存在!\n", fname); //文件不存在wgetchar=1;}}}Write:void WriteF() /*Write File*/{printf("\n\nC:\\%s>",strupr(dirname)); //显示用户路径int i, k, m=0; //定义整形变量int length; //定义长度整形变量char fname[MAXNAME]; //定义文件名字符串char str[255], str1[255]; //定义两个字符串变量if(strcmp(strupr(dirname), strupr(username))!=0) { //判断用户是否在用户目录下printf("\nError!请确认您要写的在用户目录下!\n");wgetchar=1;return;}printf("\n请先打开文件!\n");printf("Opened File(s) List:\n");k=ExistD(dirname); //获取用户文件信息for(i=0; i<fcount[k]; i++) //遍历用户下的文件{if(ifopen[k][i].ifopen==1) { //文件处于打开状态printf("%15s", ufd[k]->ufdfile[i].fname);m++;}if(m%4 == 0 && m!=0) //每创建4个文件换一行printf("\n");}printf("\n%d 文件已经打开啦!\n", m);if(m == 0)wgetchar=1;if(m!=0) //创建文件{printf("\nPlease input FileName:");gets(fname); //接收文件名ltrim(rtrim(fname)); //去除左右空白i=ExistF(fname); //获取文件物理地址if(i>=0) { //文件存在if(ifopen[k][i].ifopen==1) { //文件处于打开状态if(ifopen[k][i].openmode==1 || ifopen[k][i].openmode==2) {//文件权限是只写或读写itoa(ufd[k]->ufdfile[i].fpaddr, str, 10); //获取文件路径strcpy(str1, "file");strcat(str1, str);strcpy(str, "c:\\osfile\\file\\");strcat(str, str1);strcat(str, ".txt"); //文件路径char str2[3];int choice=3;strcpy(str2,"ab");printf("You can choise [0-Covered W] [1-Additonal W]:");//选择追加还是覆盖scanf(" %d", &choice);if(choice == 0) //0-覆盖strcpy(str2, "wb");fp_file=fopen(str, str2); //打开文件length=WriteF1();ufd[k]->ufdfile[i].flength=ufd[k]->ufdfile[i].flength+length;//修改文件长度if(choice == 0)ufd[k]->ufdfile[i].flength=length;printf("\n\nYou have write file successfully");fclose(fp_file); //关闭文件wgetchar=0;} else if(ifopen[k][i].openmode==0) {//文件处于只读状态,不允许写printf("\nError.\'%s\' 文件以只读状态打开,不允许写!\n", fname);wgetchar=1;} else {printf("\nError.\'%s\' 文件处于关闭状态,请先打开!\n", fname);wgetchar=1;}}} else {printf("\nError.\'%s\' 文件不存在!\n", fname); //文件不存在wgetchar=1;}}}Close:void CloseF() /*Close File*/{printf("\n\nC:\\%s>",strupr(dirname)); //显示路径char fname[MAXNAME]; //定义字符串变量int i, k, n=0;if(strcmp(strupr(dirname), strupr(username))!=0) //关闭用户文件需在用户目录下{printf("\nError!请确认您要关闭的是在用户目录下!\n");} else {printf("\n\nOpened File(s) List:\n"); //罗列已处于打开的文件k=ExistD(dirname);for(i=0;i<fcount[k];i++){if (ifopen[k][i].ifopen==1)//文件处于开启状态if ((ifopen[k][i].openmode==0) ||(ifopen[k][i].openmode==2))//只读或者读写状态{printf("%15s",ufd[k]->ufdfile[i].fname);n++;}if((n%4==0)&&(n!=0)) printf("\n");}printf("\n%d 文件已被打开!\n",n);if (n==0) wgetchar=1;printf("\nPlease input FileName:");gets(fname); //接收关闭文件的文件名ltrim(rtrim(fname)); //除去首尾空格i=ExistF(fname); //获取文件物理地址if(i >= 0){k=ExistD(username); //获取用户文件信息if(ifopen[k][i].ifopen==0) //文件处于关闭状态{printf("\nError!\'%s\'文件已经被关闭!\n", fname);} else { //将文件关闭ifopen[k][i].ifopen=0;ifopen[k][i].openmode=4;printf("\'%s\' has been closed successfully!", fname);}} else {printf("\nError.\'%s\'文件不存在\n", fname);}}}【测试结果】(此部分请同学们自己动手操作)Login:Help:Create:Open:Read:Write:1—additional添加后变为:0—covered覆盖后变为:Close:Attrib:Delete:Dir:Cls:(清屏)Exit:【设计总结】首先通过这次的操作系统课程设计,让我认识到了实际的编程操作并不难,难的是对过程的设计,功能的定义以及最后的程序流程图的设计。

相关主题