实验四_文件系统实验
实验四 文件系统 实验报告

文件系统实验报告一、实验目的了解操作系统中文件系统的原理以及实现方法。
二、实验方法通过FAT12文件系统的解读,了解文件系统的原理和实现。
三、实验任务通过对FAT12文件系统的了解,编写程序,读取并列出一个虚拟软盘中文件信息(文件名、属性、修改时间等),以及读取其中的文件内容四、实验要点FAT12文件系统的了解,Linux系统下文件读写相关系统调用。
五、实验过程1. FAT12 文件系统分析簇是操作系统分配文件空间的基本单位,簇由若干个扇区组成。
在FAT12文件系统中,簇号的有效位是12位,所以这种文件系统就被称为FAT12。
FAT12其中,引导区中储存着一些基本的信息。
例如,0x0000000B和0x0000000C 两个字节保存着每个扇区的大小,0x0000000D保存着每个簇占用多少个扇区。
FAT区中储存着簇号。
在0x00000200开始的三个字节,分别储存设备类型标记(0xF0为软盘);第二个第三个字节均为0xFF,是FAT标识符。
在FAT12文件系统中,每个簇占用12位,即1.5个字节。
簇号与地址的对应关系如下表:然后对读出的两个字节进行位运算处理,得到下一簇的簇序号。
注意,这里同样需要对高低位进行处理,即使用位计算的方式提取相应的簇号信息。
根据上述的原理,可以得出一个函数,以一个簇号为参数,返回值为文件下一个簇号。
代码如下:int getNextClutserId(FILE *fp, short clusterId){unsigned short tmp, low = 0, high = 0;;int address = (clusterId * 3 / 2) + 0x0000200;fseek(fp, address, SEEK_SET);fread((void *)(&tmp), 1, sizeof(unsigned short), fp);low = ((tmp & 0xFFF0) >> 4);high = tmp & 0x0FFF;return (clusterId % 2 == 0 ? high : low);}其中,fp 是用于读取文件系统的文件流,clusterID是当前簇号,返回值是下一个簇号。
实验四文件系统

•
参考代码
• • • • HashFile.h HashFile.c 测试程序jtRecord.h 测试程序jtRecord.c
HashFile.h
#include <unistd.h> #define COLLISIONFACTOR 0.5 //Hash函数冲突 因子 struct HashFileHeader { int sig; //Hash文件印鉴 int reclen; //记录长度 int total_rec_num; //总记录数 int current_rec_num; //当前记录数 };
int hashfile_write(int fd,int keyoffset,int keylen,void *buf) { return hashfile_saverec(fd,keyoffset,keylen,buf); //return -1; } int hashfile_delrec(int fd,int keyoffset,int keylen,void *buf) { int offset; offset=hashfile_findrec(fd,keyoffset,keylen,buf); if(offset!=-1) { struct CFTag tag; read(fd,&tag,sizeof(struct CFTag)); tag.free=0; //置空闲标志 lseek(fd,offset,SEEK_SET); write(fd,&tag,sizeof(struct CFTag)); struct HashFileHeader hfh; readHashFileHeader(fd,&hfh); int addr=hash(keyoffset,keylen,buf,hfh.total_rec_num); offset=sizeof(struct HashFileHeader)+addr*(hfh.reclen+sizeof(struct CFTag)); if(lseek(fd,offset,SEEK_SET)==-1) return -1;
操作系统实验_文件系统

广州大学学生实验报告开课学院及实验室:计算机科学与工程实验室 2015 年12 月8日实验课操作系统实验成绩程名称实验项实验4 文件系统指导老师陈康民目名称一、实验目的1. 对文件管理有进一步了解2. 利用文件备份实验加深对文件管理的了解3. 熟悉fopen()、fread()、 fwrite()、fclose() 的使用二、实验内容利用函数fopen(), fread(), fwrite(), fclose() 来实现简单的文件备份,即将一个文件的内容拷贝到另一个文件中去。
三、实验原理利用函数fopen(), fread(), fwrite(), fclose() 来实现简单的文件备份四、实验设备PC(操作系统:Fedora,含GCC)五、实验要求完成利用函数fopen(), fread(), fwrite(), fclose() 来实现简单的文件备份,即将一个文件的内容拷贝到另一个文件中去。
并进行分析参考程序提供错误解决方案。
六、实验程序9.c:#include <sys/types.h>#include <stdio.h>#include <stdlib.h>int main(void){char buf; //建立缓冲区FILE *source, *backup; //设立FILE结构指针if ((source = fopen("source.dat", "r")) == NULL) //若以只读形式source.dat成功打开,则fopen()返回FILE指针source{printf("Error in opening file.\n");exit(1);}if ((backup = fopen("backup.dat", "w")) == NULL) //若以只写形式backup.dat无法打开,则创建文件不成功{printf("Error in creating file.\n");exit(1);}while (fread(&buf, sizeof(buf), 1, source) == 1) //用fread函数读取source的一个buf大小的数据到缓冲区{if (fwrite(&buf, sizeof(buf), 1, backup) == 0) //用fwrite函数把一个buf大小的缓冲区数据写入backup{//若写入错误,则打印“写入文件错误”printf("Error in wrinting file.\n");exit(1);}}if (ferror(source)) //读取文件source出现错误{printf("Error in reading file.\n");exit(1);}if (fclose(source)) //source流的关闭出现错误{printf("Error in close file.\n");exit(1);}if (fclose(backup)) //backup流的关闭出现错误{printf("Error in close file.\n");exit(1);}return 0;}9.c结果截图实验结果1:源文件source.dat内容如下:编译运行后多了个Backup.dat文件实验分析1:此方案1,用char buf;重新定义缓冲区,程序中if (!(source = fopen("source.dat", "r")))和if (!(backup = fopen("backup.dat", "w")))中的表达式虽然没有错误,但这种写法不太好,因fopen()返回的是一个地址或者空值,这样写会影响程序的可读性,同时在某些机器上可能引发错误,所以改为if ((source = fopen("source.dat", "r")) == NULL)和if ((backup = fopen("backup.dat", "w")) == NULL)。
南邮操作系统实验4.文件系统模拟实现

关闭文件close close (fd);
实验内容及原理说明——实验4
读文件read
写文件write
nr = read (fd, buf, count); buf 为读出信息所应送入的用户数据区首地址 count 为要求传送字节数 nr 为实际读入字节数 一旦读到文件末尾,系统调用就返回,故nr可能小于 count,甚至为0 nw = write (fd, buf, count); 参数类似 read 其中 buf 是信息传送的源地址,即将buf指向的用户数 据区中的信息写入文件中
具体功能:
#include <unistd.h> #include <stdio.h> #include <fcntl.h>
实验内容及原理说明——实验4
创建文件creat(兼具打开功能)
fd = creat (filenamep, mode);
filenamep指向所要创建的文件路径名
实验4:文件系统的模拟实现
实验基本信息 实验内容说明
实验基本信息
实验名称
文件系统的模拟实现
实验类型:综合 学时:2+2 实验时间:13.5.30(下午3-4节) 地点:教5-501 实验环境:
VMWare + Ubuntu
实验内容及原理说明——实验4
实验目link、open、close、 read、write的使用,编程模拟实现文件系统基本功能 运行终端给出命令提示,请用户选择创建、删除、读、 写、退出等操作(可用数字表示不同命令) 根据用户选择命令,提示用户输入文件名、文件内容 等信息 执行用户命令
国开 操作系统 实验4:文件管理实验

实验4:文件管理实验
一、目的:
1、加深对文件、目录、文件系统等概念的理解。
2、掌握Linux 文件系统的目录结构。
3、掌握有关Linux 文件系统操作的常用命令。
4、了解有关文件安全性方面的知识。
二、条件:
需要一个Linux的环境
1、在Win10系统启用Linux子系统
2、在Win10应用商店安装Ubuntu应用
3、
三、过程:
1、浏览文件系统
用到pwd、ls、mkdir、cd命令
不带参数的cd命令,工作目录回到了用户的默认目录
cd ../.. 执行后,工作目录回到了/根目录
2、查看文件
用到data、head、tail、ls、man、date、cp、mv、rm命令
man date
建立链接后第2个字段从1变成2
3、文件查找和目录
用到了find、grep命令
4、修改文件存取权限
chmod命令
四、总结:Linux是一个多用户的现代操作系统,提供了丰富和强大的文件管理的命令。
掌握这些命令对于我们深入Linux学习是必需的。
实验四 文件管理实验

实验四文件管理实验◆实验名称:文件管理实验◆仪器、设备:计算机◆参考资料:操作系统实验指导书◆实验目的:设计一个n个用户的文件系统,用户在一次运行中只能打开一个文件,有Create、delete、open、close、read、write等命令。
◆实验内容:为DOS系统设计一个简单的二级文件系统。
要求可以实现下列几条命令CREATE 创建文件DELETE 删除文件OPEN 打开文件CLOSE 关闭文件READ 读文件WRITE 写文件◆实验原理、数据(程序)记录:#define MAXNAME 25 /*the largest length of mfdname,ufdname,filename*/#define MAXCHILD 50 /*the largest child*/#define MAX (MAXCHILD*MAXCHILD) /*the size of fpaddrno*/void CreateF() /*Create File*/{int fpaddrno,flag=1,i;char fname[MAXNAME],str[50],str1[50],strtext[255],a[25];char fmode[25];int FindPANo(); /*find out physical address num*/int WriteF1(); /*write file*/int ExistF(char *filename); /*Whether FileName Exist,Exist-i,Not Exist-0*/int ExistD(char *dirname);if (strcmp(strupr(dirname),strupr(username))!=0){printf("\nError. You must create file in your own dir.\n");wgetchar=1;}else{ printf("\nPlease input FileName:");gets(fname);ltrim(rtrim(fname));if (ExistF(fname)>=0){printf("\nError. Name \'%s\' has already existed.\n",fname);wgetchar=1;} else{printf("Please input FileMode(0-Read Only, 1-Write Only, 2-Read and Write, 3-Protect):");gets(fmode);ltrim(rtrim(fmode));if((strcmp(fmode,"0")==0)||(strcmp(fmode,"1")==0)||(strcmp(fmode,"2")==0)||(strcmp(fmode ,"3")==0)){fpaddrno=FindPANo();if (fpaddrno>=0){i=ExistD(username);strcpy(ufd[i]->ufdfile[fcount[i]].fname,fname);ufd[i]->ufdfile[fcount[i]].fpaddr=fpaddrno;ufd[i]->ufdfile[fcount[i]].fmode=atoi(fmode);ifopen[i][fcount[i]].ifopen=0;ifopen[i][fcount[i]].openmode=4;strcpy(str,"c:\\osfile\\file\\file");itoa(fpaddrno,str1,10);strcat(str,str1);fp_file=fopen(str,"wb");fclose(fp_file);fcount[i]++;while(flag){printf("Input text now(Y/N):");gets(a);ltrim(rtrim(a));ufd[i]->ufdfile[fcount[i]-1].flength=0;if(strcmp(strupr(a),"Y")==0){fp_file=fopen(str,"wb+");ufd[i]->ufdfile[fcount[i]-1].flength=WriteF1();flag=0;} else if(strcmp(strupr(a),"N")==0){flag=0;wgetchar=1;}} printf("\n\'%s\' has been created successfully!\n",fname);} else{printf("\nFail!No Disk Space. Please format your disk.\n");wgetchar=1;}} else {printf("\nError. FileMode\'s Range is 0-3\n");wgetchar=1;}}}}int ExistF(char *filename) /*Whether FileName Exist,Exist-i,Not Exist-0*/{int i,j;int exist=0;int ExistD(char *dirname);j=ExistD(dirname);for(i=0;i<fcount[j];i++)if (strcmp(strupr(ufd[j]->ufdfile[i].fname),strupr(filename))==0){exist=1;break;}if (exist) return(i);else return(-1);}int FindPANo() /*find out physical address num*/{int i;for(i=0;i<MAX;i++)if (fpaddrno[i]==0) {fpaddrno[i]=1;break;}if (i<MAX) return(i);else return(-1);}int WriteF1() /*write file*/{int length=0;char c;printf("Please input text(\'#\' stands for end):\n");while((c=getchar())!='#'){fprintf(fp_file,"%c",c);if (c!='\n') length++;} fprintf(fp_file,"\n");fclose(fp_file);return(length);}******************************************************************************* void DeleteF() /*Delete File*/{char fname[MAXNAME];char str[50],str1[50];int i,j,k,flag=1;char a[25]; /*whether delete*/char *rtrim(char *str); /*remove the trailing blanks.*/char *ltrim(char *str); /*remove the heading blanks.*/int ExistF(char *filename); /*Whether FileName Exist,Exist-i,Not Exist-0*/int ExistD(char *dirname);if (strcmp(strupr(dirname),strupr(username))!=0){printf("\nError. You can only delete file in your own dir.\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("\nError. \'%s\' is in open status. Close it before delete.\n",fname);wgetchar=1;}else{while(flag){printf("\'%s\' will be deleted. Are you sure(Y/N):",fname);gets(a);ltrim(rtrim(a));if(strcmp(strupr(a),"Y")==0){fpaddrno[ufd[k]->ufdfile[i].fpaddr]=0;itoa(ufd[k]->ufdfile[i].fpaddr,str,10);for(j=i;j<fcount[k]-1;j++){strcpy(ufd[k]->ufdfile[j].fname,ufd[k]->ufdfile[j+1].fname);ufd[k]->ufdfile[j].fpaddr=ufd[k]->ufdfile[j+1].fpaddr;ufd[k]->ufdfile[j].flength=ufd[k]->ufdfile[j+1].flength;ufd[k]->ufdfile[j].fmode=ufd[k]->ufdfile[j+1].fmode;ifopen[k][j]=ifopen[k][j+1];}fcount[k]--;strcpy(str1,"c:\\osfile\\file\\file");strcat(str1,str);remove(str1);flag=0;printf("\n\'%s\' has been deleted successfully.\n",fname);wgetchar=1;}else if(strcmp(strupr(a),"N")==0){printf("\nError. \'%s\' hasn\'t been deleted.\n",fname);wgetchar=1;flag=0;}}}}else {printf("\nError. \'%s\' does not exist.\n",fname);wgetchar=1;}}}******************************************************************************* void OpenF() /*Open File*/{char fname[MAXNAME];char str[25],str1[25],fmode[25];int i,k;char *rtrim(char *str); /*remove the trailing blanks.*/char *ltrim(char *str); /*remove the heading blanks.*/int ExistF(char *filename); /*Whether FileName Exist,Exist-i,Not Exist-0*/int ExistD(char *dirname);if (strcmp(strupr(ltrim(rtrim(dirname))),"")==0){printf("\nError. Please change to ufd dir before open.\n");wgetchar=1;return;}printf("\nPlease input FileName:");gets(fname);ltrim(rtrim(fname));i=ExistF(fname);if (i>=0){k=ExistD(dirname);if(!ifopen[k][i].ifopen){if (ufd[k]->ufdfile[i].fmode==3){printf("\nError. The file\'s mode is FORBID. Can not open.\n");wgetchar=1;}else{printf("Please input FileOpenMode(0-Read Only,1-Write Only,2-Read and Write):");gets(fmode);ltrim(rtrim(fmode));if((strcmp(fmode,"0")==0)||(strcmp(fmode,"1")==0)||(strcmp(fmode,"2")==0)){if(fmode[0]=='0') /*open file with read only mode*/{strcpy(str,"read only");if((ufd[k]->ufdfile[i].fmode==0)||(ufd[k]->ufdfile[i].fmode==2)) ifopen[k][i].ifopen=1;}else if(fmode[0]=='1') /*open file with write only mode*/{strcpy(str,"write only");if((ufd[k]->ufdfile[i].fmode==1)||(ufd[k]->ufdfile[i].fmode==2)) ifopen[k][i].ifopen=1;}else if(fmode[0]=='2') /*open file with read and write mode*/{strcpy(str,"read and write");if(ufd[k]->ufdfile[i].fmode==2) ifopen[k][i].ifopen=1;}if(ufd[k]->ufdfile[i].fmode==0) strcpy(str1,"read only"); /*FileMode*/else if(ufd[k]->ufdfile[i].fmode==1) strcpy(str1,"write only");else if(ufd[k]->ufdfile[i].fmode==2) strcpy(str1,"read and write");if(ifopen[k][i].ifopen==1){ifopen[k][i].openmode=atoi(fmode);if (ifopen[k][i].openmode==0) strcpy(str,"read only");else if(ifopen[k][i].openmode==1) strcpy(str,"write only");else if(ifopen[k][i].openmode==2) strcpy(str,"read and write");printf("\n\'%s\' has been opened. OpenMode is %s,FileModeis %s\n",fname,strupr(str),strupr(str1));wgetchar=1;} else{printf("\nError. \'%s\' hasn\'t been opened. OpenMode Error. OpenMode is %s,but FileMode is %s\n",fname,strupr(str),strupr(str1));wgetchar=1;}} else {printf("\nError. FileOpenMode\'s Range is 0-2\n");wgetchar=1;}}} else {printf("\nError. \'%s\' is in open status.\n",fname);wgetchar=1;}} else{printf("\nError. \'%s\' does not exist.\n",fname);wgetchar=1;}}******************************************************************************* void CloseF() /*Close File*/{int i,k,n=0;char fname[MAXNAME];char *rtrim(char *str); /*remove the trailing blanks.*/char *ltrim(char *str); /*remove the heading blanks.*/int ExistF(char *filename); /*Whether FileName Exist,Exist-i,Not Exist-0*/int ExistD(char *dirname);if (strcmp(strupr(ltrim(rtrim(dirname))),"")==0){printf("\nError. Please convert to ufd dir before close.\n");wgetchar=1;return;}k=ExistD(dirname);printf("\nOpen File(s) In This Ufd:\n"); /*display openned file*/for(i=0;i<fcount[k];i++){if (ifopen[k][i].ifopen==1) {printf("%15s",ufd[k]->ufdfile[i].fname);n++;}if((n%4==0)&&(n!=0)) printf("\n");} printf("\n%d files openned.\n",n);if (n==0) wgetchar=1;if(n!=0){printf("\nPlease input FileName:");gets(fname);ltrim(rtrim(fname));i=ExistF(fname);if(i>=0){if(ifopen[k][i].ifopen==1){ifopen[k][i].ifopen=0;ifopen[k][i].openmode=4;printf("\n\'%s\' has been closed successfully.\n",fname);wgetchar=1;} else {printf("\nError.\'%s\' is in closing status.\n",fname);wgetchar=1;}} else {printf("\nError. \'%s\' is not exist.\n",fname);wgetchar=1;}}}******************************************************************************* void ReadF() /*Read File*/{int i,k,n=0;char fname[MAXNAME];char str[255],str1[255],c;char *rtrim(char *str); /*remove the trailing blanks.*/char *ltrim(char *str); /*remove the heading blanks.*/int ExistF(char *filename); /*Whether FileName Exist,Exist-i,Not Exist-0*/int ExistD(char *dirname);if (strcmp(strupr(ltrim(rtrim(dirname))),"")==0) {printf("\nError.Please convert to ufd dir before read.\n");wgetchar=1;return;}printf("\nCaution:Open file first\n");printf("Opened 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 files openned.\n",n);if (n==0) wgetchar=1;if(n!=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==0) ||(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);fp_file=fopen(str,"rb");fseek(fp_file,0,0);printf("\nThe text is:\n\n");printf(" ");while(fscanf(fp_file,"%c",&c)!=EOF)if (c=='\n') printf("\n ");else printf("%c",c);printf("\n\n%d Length.\n",ufd[k]->ufdfile[i].flength);fclose(fp_file);wgetchar=1;}else{printf("\nError.\'%s\' has been opened with WRITE ONLY mode. It isn\'tread.\n",fname);wgetchar=1;}}else {printf("\nError.\'%s\' is in closing status. Please open it beforeread\n",fname);wgetchar=1;}}else {printf("\nError. \'%s\' does not exist.\n",fname);wgetchar=1;}}}******************************************************************************* void WriteF() /*Write File*/{int i,k,n=0;char fname[MAXNAME];char str[50],str1[50],a[50];char *rtrim(char *str); /*remove the trailing blanks.*/char *ltrim(char *str); /*remove the heading blanks.*/int ExistF(char *filename); /*Whether FileName Exist,Exist-i,Not Exist-0*/int ExistD(char *dirname);int WriteF1(); /*write file*/if (strcmp(strupr(ltrim(rtrim(dirname))),"")==0) {printf("\nError. Please convert to ufd dir before write.\n");wgetchar=1;return;}k=ExistD(dirname);printf("\nOpen File(s) with write only mode or read and write mode:\n");/*display openned files with writable mode*/for(i=0;i<fcount[k];i++){if (ifopen[k][i].ifopen==1)if ((ifopen[k][i].openmode==1) ||(ifopen[k][i].openmode==2)){printf("%15s",ufd[k]->ufdfile[i].fname);n++;}if((n%4==0)&&(n!=0)) printf("\n");}printf("\n%d files open.\n",n);if (n==0) wgetchar=1;if(n!=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);if (ufd[k]->ufdfile[i].flength!=0){printf("\n\'%s\' has text. Overwrite or Append(O-overwrite,A-Append,else-not write):",fname);gets(a);ltrim(rtrim(a));if (fp_file!=NULL) fclose(fp_file);if (strcmp(strupr(a),"O")==0){printf("\nOverwrite\n");fp_file=fopen(str,"wb");ufd[k]->ufdfile[i].flength=0;ufd[k]->ufdfile[i].flength=WriteF1();}else if(strcmp(strupr(a),"A")==0){printf("\nAppend\n");fp_file=fopen(str,"ab");ufd[k]->ufdfile[i].flength=ufd[k]->ufdfile[i].flength+WriteF1();}else{printf("\nError.\'%s\' has not been written.\n",fname);fclose(fp_file);wgetchar=1;} }else{fp_file=fopen(str,"wb");ufd[k]->ufdfile[i].flength=WriteF1();}}else{printf("\nError. \'%s\' has been opened with read only mode.It isn\'twrited.\n",fname);wgetchar=1;}}else{printf("\nError. \'%s\' is in closing status. Please open it beforewrite\n",fname);wgetchar=1;}}else{printf("\nError. \'%s\' does not exist.\n",fname);wgetchar=1;}}}◆实验结果及分析通过实验实现了有Create、delete、open、close、read、write等命令的简单的文件系统。
实验四文件操作

实验四文件操作一、实验目的1.熟悉和理解文件系统的概念和文件系统的类型。
2.掌握文件系统的挂载方法。
3.学会Linux中数据备份的方法。
4.了解Linux系统中文件系统的管理机制。
二、实验内容1.挂载文件系统。
2.监视文件系统状态。
3.维护文件系统。
4.软盘的使用。
5.硬盘的使用。
6.数据备份和压缩。
7.在GNOME下管理磁盘三、实验环境1.装有Linux系统的计算机。
2.软磁盘和光盘各一张。
四、实验步骤(一)挂载文件系统1.手工挂载文件系统①用mount命令挂载文件系统。
命令格式:mount [-fnrvw] [-t type] device dir其中:-t type:指定文件系统类型;device:待安装文件系统的块设备名;dir:安装点(目录);-f: 模拟一个文件系统的挂装过程,用它可以检查一个文件系统是否可以正确挂装。
-n:挂装一个文件系统,但不在fstab文件中生成与之对应的设置项。
-v:命令进展注释,给出mount命令每个操作步骤的注释。
-r:将文件系统挂载为只读模式;-w:将文件系统挂载为读写模式。
操作:在软驱中插入一张磁盘,然后将软驱挂载为msdos文件类型。
②挂载软磁盘的命令:mount -t msdos /dev/fd0 /mnt/floppy挂载完成后,用ls命令查看软磁盘中包括的文件。
③用mount命令还可以显示所有已安装的文件系统。
命令格式:mount [-hV]2.手工卸载文件系统用umount命令卸载文件系统。
命令格式:umount [-hV]umount [-a] [-n] [-v] [-t types]其中:-h:显示帮助信息。
-V:显示版本信息。
-a:表示要卸载/etc/fstab中的所有文件系统;-n:卸除时不要将信息存入/etc/mtab文件中。
-v:执行时显示详细的信息。
-t types:指定文件系统的类型。
操作:卸载软盘的msdos文件系统。
①命令:umount -t msdos /dev/fd0卸载完成后,从软盘驱动器中取出软盘。
实验四 文件系统实验

实验四文件系统实验实验目的1)掌握文件系统的工作机理。
2)理解文件系统的主要数据结构。
3)学习较为复杂的LINUX下的编程实验内容1)设计并实现一个一级(单用户)文件系统程序a.提供以下操作:➢文件创建/删除接口命令create/delete➢目录创建/删除接口命令mkdir/rmdir➢显示目录内容命令lsb.创建的文件不要求格式和内容2)设计并实现一个二级文件系统程序a.提供用户登录;b.文件、目录要有权限实验结果及其解释说明基本上实现了简单文件系统的实现。
创建并读写文件如下图所示:打开文件、关闭文件、创建并显示子目录、删除子目录如下图所示:实验中的问题及解决首先应确定文件系统的数据结构:主目录、子目录及活动文件等。
主目录和子目录都应以文件的形式存放于磁盘,这样便于查找和修改。
由于对文件系统理解不够深刻,导致程序在一些原理上的迷糊不清,而时间较短,因此采取了简化的方法。
在写程序的开始也没有做详细的规划,因此在程序的结构上有些混乱。
在后期做了一些调整,但是整体还是有缺陷的。
另外程序只是很简单的实现了文件管理系统,只有一些非常简单的功能,而且功能考虑的也不尽全面、严谨,难免会出现一些Bug。
必须对写好的程序进行反复的调试和优化。
实验体会通过学习基本掌握了文件系统的存储结构,当遇到困难的时候,认真思考和查阅资料可以有很大的帮助。
经过这次实验,锻炼了自己的动手的能力和分析问题的能力。
在构造函数的时候可以开阔思维,同时加深自己对文件系统实现的理解。
在进行理论知识的学习时,我们一定要注意动手应用,这样可以让我们更好地掌握理论知识。
程序清单#include<stdio.h>#include<string.h>#include<stdlib.h>#define DIR_LENGTH 1024 /*路径最长可达100字节*/#define MAX_WRITE 1024*128 /*写入文字可达128k字节*/#define MEM_D_SIZE 1024*1024 /*1M磁盘空间*/#define DISKSIZE 1024 /*磁盘快的大小1K*/#define MSD 5 /*最大子目录数5 (类似五叉树)*/#define DISK_NUM MEM_D_SIZE/DISKSIZE /*磁盘快数目1024=1M/1K*/#define FATSIZE DISK_NUM*sizeof(struct fatitem) /*FAT表大小8K=8192B (理想应该是1.5K)*/#define MOFN 5 /*最大文件打开数5 (即除根以外最大深度为5)*/#define ROOT_DISK_NO FATSIZE/DISKSIZE+1 /*根目录起始盘快号9*/#define ROOT_DISK_SIZE sizeof(struct direct)/*根目录大小196*//*---------------FAT表项结构-----------------------*/struct fatitem /* size 8*/{int item; /*存放文件下一个磁盘的指针*/char em_disk; /*磁盘块是否空闲标志位0 空闲*/};/*-------------------目录项结构------------------------*/struct direct /* size 196*/{/*-----文件控制快信息-----*/struct FCB{char name[9]; /*文件/目录名8位*/char property; /*属性1位目录0位普通文件*/int size; /*文件/目录字节数(原注释位盘块数)*/int firstdisk; /*文件/目录起始盘块号*/int next; /*子目录起始盘块号*/int sign; /*1是根目录0不是根目录*/}directitem[MSD+2];};/*------------------文件打开表项结构--------------------------*/ struct opentable /* size 104*/{struct openttableitem /* size 20*/{char name[9]; /*文件名*/int firstdisk; /*起始盘块号*/int size; /*文件的大小*/}openitem[MOFN];int cur_size; /*当前打文件的数目*/};/*-------------------------------------------------------------------*/ struct fatitem *fat; /*FAT表*/struct direct *root; /*根目录*/struct direct *cur_dir; /*当前目录*/struct opentable u_opentable; /*文件打开表*/int fd=-1; /*文件打开表的序号*/char *bufferdir; /*记录当前路径的名称*/char *fdisk; /*虚拟磁盘起始地址*/void initfile();void format();void enter();void halt();int create(char *name);int open(char *name);int close(char *name);int write(int fd,char *buf,int len);int read(int fd,char *buf);int del(char *name);int mkdir(char *name);int rmdir(char *name);void dir();int cd(char *name);void print();void show();/*----------------------------------------------------------------------------------------------*//*------------------------------------------初始化文件系统--------------------------------------*/ void initfile(){fdisk = (char *)malloc(MEM_D_SIZE*sizeof(char)); /*申请1M空间*/format();free(fdisk);}/*----------------------------------------------------------------------------------------------*//*------------------------------------------格式化----------------------------------------------*/ void format(){int i;FILE *fp;fat = (struct fatitem *)(fdisk+DISKSIZE); /*计算FAT表地址(为什么向后偏移1k)*/ /*-----初始化FAT表------------*/fat[0].item=-1; /*引导块*/fat[0].em_disk='1';for(i=1;i<ROOT_DISK_NO-1;i++) /*存放FAT表的磁盘块号*/{fat[i].item=i+1;fat[i].em_disk='1';}fat[ROOT_DISK_NO-1].item=-1;fat[ROOT_DISK_NO-1].em_disk='1';fat[ROOT_DISK_NO].item=-1; /*存放根目录的磁盘块号*/fat[ROOT_DISK_NO].em_disk='1';for(i=ROOT_DISK_NO+1;i<DISK_NUM;i++){fat[i].item = -1;fat[i].em_disk = '0';}/*-----------------------------------------------*/root = (struct direct *)(fdisk+DISKSIZE+FATSIZE); /*根目录的地址*//*初始化目录*//*---------指向当前目录的目录项---------*/root->directitem[0].sign = 1;root->directitem[0].firstdisk = ROOT_DISK_NO;strcpy(root->directitem[0].name,".");root->directitem[0].next = root->directitem[0].firstdisk;root->directitem[0].property = '1';root->directitem[0].size = ROOT_DISK_SIZE;/*-------指向上一级目录的目录项---------*/root->directitem[1].sign = 1;root->directitem[1].firstdisk = ROOT_DISK_NO;strcpy(root->directitem[1].name,"..");root->directitem[1].next = root->directitem[0].firstdisk;root->directitem[1].property = '1';root->directitem[1].size = ROOT_DISK_SIZE;for(i=2;i<MSD+2;i++) /*-子目录初始化为空-*/{root->directitem[i].sign = 0;root->directitem[i].firstdisk = -1;strcpy(root->directitem[i].name,"");root->directitem[i].next = -1;root->directitem[i].property = '0';root->directitem[i].size = 0;}if((fp = fopen("disk.dat","wb"))==NULL){printf("Error:\n Cannot open file \n");return;}if(fwrite(fdisk,MEM_D_SIZE,1,fp)!=1) /*把虚拟磁盘空间保存到磁盘文件中*/ {printf("Error:\n File write error! \n");}fclose(fp);}/*----------------------------------------------------------------------------------------------*//*--------------------------------进入文件系统--------------------------------------------------*/void enter(){FILE *fp;int i;fdisk = (char *)malloc(MEM_D_SIZE*sizeof(char)); /*申请1M空间*/if((fp=fopen("disk.dat","rb"))==NULL){printf("Error:\nCannot open file\n");return;}if(!fread(fdisk,MEM_D_SIZE,1,fp)) /*把磁盘文件disk.dat 读入虚拟磁盘空间(内存)*/ {printf("Error:\nCannot read file\n");exit(0);}fat = (struct fatitem *)(fdisk+DISKSIZE); /*找到FAT表地址*/root = (struct direct *)(fdisk+DISKSIZE+FATSIZE);/*找到根目录地址*/fclose(fp);/*--------------初始化用户打开表------------------*/for(i=0;i<MOFN;i++){strcpy(u_opentable.openitem[i].name,"");u_opentable.openitem[i].firstdisk = -1;u_opentable.openitem[i].size = 0;}u_opentable.cur_size = 0;cur_dir = root; /*当前目录为根目录*/bufferdir = (char *)malloc(DIR_LENGTH*sizeof(char));strcpy(bufferdir,"Root:"); /*显示根目录为E:*/}/*----------------------------------------------------------------------------------------------*//*------------------------------------退出文件系统----------------------------------------------*/void halt(){FILE *fp;int i;if((fp=fopen("disk.dat","wb"))==NULL)printf("Error:\nCannot open file\n");return;}if(!fwrite(fdisk,MEM_D_SIZE,1,fp)) /*把虚拟磁盘空间(内存)内容读入磁盘文件disk.dat */{printf("Error:\nFile write error!\n");}fclose(fp);free(fdisk);free(bufferdir);for(i=0;i<MOFN;i++) /*撤销用户打开表(好像没有必要,系统自动会回收)*/{strcpy(u_opentable.openitem[i].name,"");u_opentable.openitem[i].firstdisk = 0;u_opentable.openitem[i].size = 0;}u_opentable.cur_size = 0; /*用户打开文件数清零*/return;}/*----------------------------------------------------------------------------------------------*//*----------------------------------------创建文件----------------------------------------------*/int create(char *name){int i,j;if(strlen(name)>8) /*文件名大于8位*/return(-1);for(i=2;i<MSD+2;i++) /*找到第一个空闲子目录*/{if(cur_dir->directitem[i].firstdisk==-1)break;}for(j=2;j<MSD+2;j++) /*检查创建文件是否与已存在的文件重名*/if(!strcmp(cur_dir->directitem[j].name,name))break;}if(i>=MSD+2) /*无空目录项*/return(-2);if(u_opentable.cur_size>=MOFN) /*打开文件太多(第五层)*/return(-3);if(j<MSD+2) /*文件已经存在*/return(-4);for(j=ROOT_DISK_NO+1;j<DISK_NUM;j++) /*找到空闲盘块j 后退出*/ {if(fat[j].em_disk=='0')break;}if(j>=DISK_NUM)return(-5);fat[j].em_disk = '1'; /*将空闲块置为已经分配*//*-----------填写目录项-----------------*/strcpy(cur_dir->directitem[i].name,name);cur_dir->directitem[i].firstdisk = j;cur_dir->directitem[i].size = 0;cur_dir->directitem[i].next = j;cur_dir->directitem[i].property = '0';/*cur_dir->directitem[i].sign 丢失*//*---------------------------------*/fd = open(name); /*打开所创建的文件*/return 0;}/*----------------------------------------------------------------------------------------------*//*----------------------------------------打开文件----------------------------------------------*/ int open(char *name){int i, j;for(i=2;i<MSD+2;i++) /*文件是否存在*/{if(!strcmp(cur_dir->directitem[i].name,name))break;if(i>=MSD+2) /*文件不存在*/return(-1);/*--------是文件还是目录-----------------------*/if(cur_dir->directitem[i].property=='1')/*是目录,不可打开读写*/return(-4);/*--------文件是否打开-----------------------*/for(j=0;j<MOFN;j++){if(!strcmp(u_opentable.openitem[j].name,name))break;}if(j<MOFN) /*文件已经打开*/return(-2);if(u_opentable.cur_size>=MOFN) /*文件打开太多*/return(-3);/*--------查找一个空闲用户打开表项-----------------------*/for(j=0;j<MOFN;j++){if(u_opentable.openitem[j].firstdisk==-1)break;}/*--------------填写表项的相关信息------------------------*/u_opentable.openitem[j].firstdisk = cur_dir->directitem[i].firstdisk;strcpy(u_opentable.openitem[j].name,name);u_opentable.openitem[j].size = cur_dir->directitem[i].size;u_opentable.cur_size++;/*----------返回用户打开表表项的序号--------------------------*/return(j);}/*----------------------------------------------------------------------------------------------*//*----------------------------------------关闭文件----------------------------------------------*/ int close(char *name){int i;for(i=0;i<MOFN;i++){if(!strcmp(u_opentable.openitem[i].name,name))break;}if(i>=MOFN) /*--文件没有打开-*/return(-1);/*-----------清空该文件的用户打开表项的内容---------------------*/strcpy(u_opentable.openitem[i].name,"");u_opentable.openitem[i].firstdisk = -1;u_opentable.openitem[i].size = 0;u_opentable.cur_size--;fd = -1; /*文件打开表的序号为-1 */return 0;}/*----------------------------------------------------------------------------------------------*/ /*----------------------------------------写文件------------------------------------------------*/ int write(int fd, char *buf, int len){char *first;int item, i, j, k;int ilen1, ilen2, modlen, temp;/*----------用$ 字符作为空格# 字符作为换行符-----------------------*/char Space = 32; /*SPACE的ASCII码值*/char Endter= '\n';for(i=0;i<len;i++){if(buf[i] == '$') /*用$ 字符作为空格*/buf[i] = Space;else if(buf[i] == '#')buf[i] = Endter;}/*----------读取用户打开表对应表项第一个盘块号-----------------------*/item = u_opentable.openitem[fd].firstdisk;/*-------------找到当前目录所对应表项的序号-------------------------*/for(i=2;i<MSD+2;i++){if(cur_dir->directitem[i].firstdisk==item)break;}temp = i; /*-存放当前目录项的下标-*//*------找到的item 是该文件的最后一块磁盘块-------------------*/while(fat[item].item!=-1){item =fat[item].item; /*-查找该文件的下一盘块--*/}/*-----计算除该文件的最末地址-------*/first = fdisk+item*DISKSIZE+u_opentable.openitem[fd].size%DISKSIZE;/*-----如果最后磁盘块剩余的大小大于要写入的文件的大小-------*/if(DISKSIZE-u_opentable.openitem[fd].size%DISKSIZE>len){strcpy(first,buf);u_opentable.openitem[fd].size = u_opentable.openitem[fd].size+len;cur_dir->directitem[temp].size = cur_dir->directitem[temp].size+len;}else{for(i=0;i<(DISKSIZE-u_opentable.openitem[fd].size%DISKSIZE);i++){/*写一部分内容到最后一块磁盘块的剩余空间(字节)*/first[i] = buf [i];}/*-----计算分配完最后一块磁盘的剩余空间(字节) 还剩下多少字节未存储-------*/ ilen1 = len-(DISKSIZE-u_opentable.openitem[fd].size%DISKSIZE);ilen2 = ilen1/DISKSIZE;modlen = ilen1%DISKSIZE;if(modlen>0)ilen2 = ilen2+1; /*--还需要多少块磁盘块-*//*调试时特别注意*/for(j=0;j<ilen2;j++){for(i=ROOT_DISK_NO+1;i<DISK_NUM;i++)/*寻找空闲磁盘块*/{if(fat[i].em_disk=='0')break;}if(i>=DISK_NUM) /*--如果磁盘块已经分配完了-*/return(-1);first = fdisk+i*DISKSIZE; /*--找到的那块空闲磁盘块的起始地址-*/if(j==ilen2-1) /*--如果是最后要分配的一块-*/{for(k=0;k<len-(DISKSIZE-u_opentable.openitem[fd].size%DISKSIZE)-j*DISKSIZE;k++) first[k] = buf[k];}else/*-如果不是要最后分配的一块--*/{for(k=0;k<DISKSIZE;k++)first[k] =buf[k];}fat[item].item = i; /*--找到一块后将它的序号存放在上一块的指针中-*/fat[i].em_disk = '1'; /*--置找到的磁盘快的空闲标志位为已分配-*/fat[i].item = -1; /*--它的指针为-1 (即没有下一块)-*/}/*--修改文件打开表用户的长度-*/u_opentable.openitem[fd].size = u_opentable.openitem[fd].size+len;/*--修改目录项的文件长度-*/cur_dir->directitem[temp].size = cur_dir->directitem[temp].size+len;}return 0;}/*----------------------------------------------------------------------------------------------*//*----------------------------------------读文件------------------------------------------------*/int read(int fd, char *buf){int len = u_opentable.openitem[fd].size;char *first;int i, j, item;int ilen1, modlen;item = u_opentable.openitem[fd].firstdisk;if(len>u_opentable.openitem[fd].size) /*--欲读出的文件长度比实际文件长度长-*/ return(-1);ilen1 = len/DISKSIZE;modlen = len%DISKSIZE;if(modlen!=0)ilen1 = ilen1+1; /*--计算文件所占磁盘的块数-*/first = fdisk+item*DISKSIZE; /*--计算文件的起始位置-*/for(i=0;i<ilen1;i++)if(i==ilen1-1) /*--如果在最后一个磁盘块-*/{for(j=0;j<len-i*DISKSIZE;j++)buf[i*DISKSIZE+j] = first[j];}else /*--不在最后一块磁盘块-*/{for(j=0;j<len-i*DISKSIZE;j++)buf[i*DISKSIZE+j] = first[j];item = fat[item].item; /*-查找下一盘块-*/first = fdisk+item*DISKSIZE;}}return 0;}/*----------------------------------------------------------------------------------------------*//*----------------------------------------删除文件----------------------------------------------*/ int del(char *name){int i,cur_item,item,temp;for(i=2;i<MSD+2;i++) /*--查找要删除文件是否在当前目录中-*/{if(!strcmp(cur_dir->directitem[i].name,name))break;}cur_item = i; /*--用来保存目录项的序号,供释放目录中-*/if(i>=MSD+2) /*--如果不在当前目录中-*/return(-1);if(cur_dir->directitem[cur_item].property!='0') /*--如果删除的(不)是目录-*/return(-3);for(i=0;i<MOFN;i++) /*--如果文件打开,则不能删除,退出-*/{if(!strcmp(u_opentable.openitem[i].name,name))return(-2);}item = cur_dir->directitem[cur_item].firstdisk;/*--该文件的起始盘块号-*/while(item!=-1) /*--释放空间,将FAT表对应项进行修改-*/temp = fat[item].item;fat[item].item = -1;fat[item].em_disk = '0';item = temp;}/*-----------------释放目录项-----------------------*/cur_dir->directitem[cur_item].sign = 0;cur_dir->directitem[cur_item].firstdisk = -1;strcpy(u_opentable.openitem[cur_item].name,"");cur_dir->directitem[cur_item].next = -1;cur_dir->directitem[cur_item].property = '0';cur_dir->directitem[cur_item].size = 0;return 0;}/*----------------------------------------------------------------------------------------------*//*---------------------------------------创建子目录---------------------------------------------*/ int mkdir(char *name){int i,j;struct direct *cur_mkdir;if(strchr(name,'\\'))/*如果目录名中有'\'字符*/return(-4);if(!strcmp(name,"."))return(-6);if(!strcmp(name,".."))return(-6);if(strlen(name)>8) /*-如果目录名长度大于8位-*/return(-1);for(i=2;i<MSD+2;i++) /*-如果有空闲目录项退出-*/{if(cur_dir->directitem[i].firstdisk==-1)break;}if(i>=MSD+2) /*-目录/文件已满-*/return(-2);for(j=2;j<MSD+2;j++) /*-判断是否有重名-*/{if(!strcmp(cur_dir->directitem[j].name,name))break;if(j<MSD+2) /*-如果有重名-*/return(-3);for(j=ROOT_DISK_NO+1;j<DISK_NUM;j++) /*-找到空闲磁盘块j 后退出-*/{if(fat[j].em_disk=='0')break;}if(j>=DISK_NUM)return(-5);fat[j].em_disk='1'; /*-将该空闲块设置为已分配-*//*-------------填写目录项----------*/strcpy(cur_dir->directitem[i].name,name);cur_dir->directitem[i].firstdisk=j;cur_dir->directitem[i].size=ROOT_DISK_SIZE;cur_dir->directitem[i].next=j; /*-指向子目录(其实就是其本身)的起始盘块号-*/cur_dir->directitem[i].property='1';/*-sign=1为根标志,这里可以省略-*//*-所创目录在虚拟磁盘上的地址(内存物理地址)-*/cur_mkdir=(struct direct *)(fdisk+cur_dir->directitem[i].firstdisk*DISKSIZE);/*-初始化目录-*//*-指向当前目录的目录项-*/cur_mkdir->directitem[0].sign=0;cur_mkdir->directitem[0].firstdisk=cur_dir->directitem[i].firstdisk;strcpy(cur_mkdir->directitem[0].name,".");cur_mkdir->directitem[0].next=cur_mkdir->directitem[0].firstdisk;cur_mkdir->directitem[0].property='1';cur_mkdir->directitem[0].size=ROOT_DISK_SIZE;/*-指向上一级目录的目录项-*/cur_mkdir->directitem[1].sign=cur_dir->directitem[0].sign;/*-指向上一级目录的目录项-*/ cur_mkdir->directitem[1].firstdisk=cur_dir->directitem[0].firstdisk;strcpy(cur_mkdir->directitem[1].name,"..");cur_mkdir->directitem[1].next=cur_mkdir->directitem[1].firstdisk;cur_mkdir->directitem[1].property='1';cur_mkdir->directitem[1].size=ROOT_DISK_SIZE;for(i=2;i<MSD+2;i++) /*-子目录都初始化为空-*/{cur_mkdir->directitem[i].sign=0;cur_mkdir->directitem[i].firstdisk=-1;strcpy(cur_mkdir->directitem[i].name,"");cur_mkdir->directitem[i].next=-1;cur_mkdir->directitem[i].property='0';cur_mkdir->directitem[i].size=0;}return 0;}/*----------------------------------------------------------------------------------------------*//*---------------------------------------删除子目录---------------------------------------------*/ int rmdir(char *name){int i,j,item;struct direct *temp_dir;/*-检查当前目录项中有无该目录-*/for(i=2;i<MSD+2;i++){if(!strcmp(cur_dir->directitem[i].name,name))break;}if(cur_dir->directitem[i].property!='1')/*-删除的不是目录-*/return(-3);if(i>=MSD+2) /*-没有这个文件或目录-*/return(-1);/*-判断要删除的目录有无子目录-*//*-要删除的目录起始地址-*/temp_dir=(struct direct *)(fdisk+cur_dir->directitem[i].next*DISKSIZE);for(j=2;j<MSD+2;j++){if(temp_dir->directitem[j].next!=-1)break;}if(j<MSD+2) /*-有子目录或文件-*/return(-2); /*-有关联则报错,也可以采取级联删除,像Windows-*//*------------找到起始盘块号,并将其释放----------------*/item=cur_dir->directitem[i].firstdisk;fat[item].em_disk='0';/*-修改目录项-*/cur_dir->directitem[i].sign=0;cur_dir->directitem[i].firstdisk=-1;strcpy(cur_dir->directitem[i].name,"");cur_dir->directitem[i].next=-1;cur_dir->directitem[i].property='0';cur_dir->directitem[i].size=0;return 0;}/*----------------------------------------------------------------------------------------------*//*-------------------------------显示当前目录的子目录-------------------------------------------*/ void dir(){int i;for(i=0;i<MSD+2;i++){if(cur_dir->directitem[i].firstdisk!=-1) /*-如果存在子目录-*/{ /*-其本身和父目录也算?-*/printf("%s\t",cur_dir->directitem[i].name);if(cur_dir->directitem[i].property=='0') /*-文件-*/printf("%d\t\t\n",cur_dir->directitem[i].size);else /*-目录-*/printf("\t<DIR>\t\n");}}}/*----------------------------------------------------------------------------------------------*//*---------------------------------------更改当前目录-------------------------------------------*/int cd(char *name){int i,j,item;char *str,*str1;char *temp,*point,*point1;struct direct *temp_dir;temp_dir=cur_dir; /*-先用临时目录代替当前目录-*/str=name; /*-str用来记录下次查找的起始地址-*/if(!strcmp("\\",name)) /*如果输入"\" ,回根目录*/{cur_dir = root;strcpy(bufferdir,"Root:");return 0;}j=0;for(i=0;i<(int)strlen(str);i++)/*查找有两个连续是"\",即"\\",退出*/{if(name[i]=='\\')j++;if(j>=2){return -3;}}elsej=0;}if(name[0]=='\\') /*如果最后一个是"\" ,去掉这个"\"*/{temp_dir = root;strcpy(bufferdir,"Root:");str++;}if(str[strlen(str)-1] == '\\'){str[strlen(str)-1] = '\0';}str1=strchr(str,'\\'); /*-找到'\'字符的位置-*/temp = (char *)malloc(DIR_LENGTH*sizeof(char));/*-为子目录的名字分配空间-*/ while(str1!=NULL) /*-找到-*/{for(i=0;i<str1-str;i++){temp[i]=str[i];}temp[i]='\0';for(j=2;j<MSD+2;j++) /*-查找该子目录是否在当前目录中-*/{if(!strcmp(temp_dir->directitem[j].name,temp))break;}if(j>=MSD+2) /*-不在当前目录-*/return(-1);item=temp_dir->directitem[j].firstdisk;temp_dir=(struct direct *)(fdisk+item*DISKSIZE); /*-计算当前目录物理位置-*/str=str1+1;str1=strchr(str,'\\');//free(temp);}str1=str1+strlen(str);for(i=0;i<(int)strlen(str);i++)temp[i]=str[i];temp[i]='\0';for(j=0;j<MSD+2;j++) /*-查找该子目录是否在当前目录中-*/{if(!strcmp(temp_dir->directitem[j].name,temp))break;}free(temp);/*释放申请的临时空间*/if(temp_dir->directitem[j].property!='1') /*-打开的不是目录-*/return(-2);if(j>=MSD+2) /*-不在当前目录-*/return(-1);item=temp_dir->directitem[j].firstdisk;/*-当前目录在磁盘中位置-*/temp_dir=(struct direct *)(fdisk+item*DISKSIZE);if(!strcmp("..",name)){if(cur_dir->directitem[j-1].sign!=1) /*-如果子目录不是根目录-*/ {point=strchr(bufferdir,'\\');while(point!=NULL){point1=point+1; /*-减去'\'所占的空间,记录下次查找的起始地址-*/ point=strchr(point1,'\\');}*(point1-1)='\0'; /*-将上一级目录删除-*/}else{}}else if(!strcmp(".",name)){bufferdir=bufferdir; /*-如果是当前目录则不变-*/}else{if(name[0] !='\\')bufferdir = strcat(bufferdir,"\\"); /*-修改当前目录-*/bufferdir = strcat(bufferdir,name);}cur_dir=temp_dir; /*-将当前目录确定下来-*/return 0;}/*----------------------------------------------------------------------------------------------*//*---------------------------------------显示当前路径-------------------------------------------*/ void show(){printf("%s>",bufferdir);}/*----------------------------------------------------------------------------------------------*//*--------------------------------------输出提示信息--------------------------------------------*/ void print(){printf("\t\t 退出文件系统halt\n");printf("\t\t 创建文件create 文件名\n");printf("\t\t 删除文件del 文件名\n");printf("\t\t 打开文件open 文件名\n");printf("\t\t 关闭文件close 文件名\n");printf("\t\t 写文件write\n");printf("\t\t 读文件read\n\n");printf("\t\t 创建子目录mkdir 目录名\n");printf("\t\t 删除子目录rmdir 目录名\n");printf("\t\t 显示当前目录的子目录dir\n");printf("\t\t 更改当前目录cd 目录名\n");}/*----------------------------------------------------------------------------------------------*//*------------------------------------------主函数----------------------------------------------*/{FILE *fp;char ch;char a[100];char code[11][10];char name[10];int i,flag,r_size;char *contect;contect = (char *)malloc(MAX_WRITE*sizeof(char));if((fp=fopen("disk.dat","rb"))==NULL)/*如果还没有进行格式化,则要格式化*/ {printf("You have not format,Do you want format?(y/n)");scanf("%c",&ch);if(ch=='y'){initfile();printf("Successfully format! \n");}else{//return;}}enter();print();show();/*将命令全部保存在CODE数组中*/strcpy(code[0],"halt");strcpy(code[1],"create");strcpy(code[2],"open");strcpy(code[3],"close");strcpy(code[4],"write");strcpy(code[5],"read");strcpy(code[6],"del");strcpy(code[7],"mkdir");strcpy(code[8],"rmdir");strcpy(code[9],"dir");strcpy(code[10],"cd");{scanf("%s",a);for(i=0;i<11;i++){if(!strcmp(code[i],a))break;}switch(i){case 0: //*--退出文件系统--//free(contect);halt();//return;case 1: //*--创建文件--//scanf("%s",name);flag = create(name);if(flag==-1){printf("Error: \n The length is too long !\n");}else if(flag==-2){printf("Error: \n The direct item is already full !\n");}else if(flag==-3){printf("Error: \n The number of openfile is too much !\n");}else if(flag==-4){printf("Error: \n The name is already in the direct !\n");}else if(flag==-5){printf("Error: \n The disk space is full!\n");}else{printf("Successfully create a file! \n");}show();case 2://--打开文件--//scanf("%s",name);fd = open(name);if(fd == -1){printf("Error: \n The open file not exit! \n");}else if(fd == -2){printf("Error: \n The file have already opened! \n");}else if(fd == -3){printf("Error: \n The number of open file is too much! \n");}else if(fd == -4){printf("Error: \n It is a direct,can not open for read or write! \n"); }else{printf("Successfully opened! \n");}show();break;case 3://--关闭文件--//scanf("%s",name);flag = close(name);if(flag == -1){printf("Error:\n The file is not opened ! \n");}else{printf("Successfully closed! \n");}show();break;case 4:/*--写文件--*/if(fd ==-1){printf("Error:\n The file is not opened ! \n");}else{printf("Please input the file contect:");scanf("%s",contect);flag=write(fd,contect,strlen(contect));if(flag == 0){printf("Successfully write! \n");}else{printf("Error:\n The disk size is not enough! \n");}}show();break;case 5:/*--读文件--*/if(fd ==-1){printf("Error:\n The file is not opened ! \n");}else{flag = read(fd,contect);if(flag == -1){printf("Error: \n The size is over the length of the file! \n"); }else{//printf("Successfully read! \n The contect is :");for(i=0;i<u_opentable.openitem[fd].size;i++){printf("%c",contect[i]);}printf("\t\n");}}show();break;case 6://*--删除文件--scanf("%s",name);flag = del(name);if(flag == -1){printf("Error:\n The file not exit! \n");}else if(flag == -2){printf("Error:\n The file is opened,please first close it ! \n"); }else if(flag == -3){printf("Error:\n The delete is not file ! \n");}else{printf("Successfully delete! \n");}show();break;case 7://*--创建子目录--/scanf("%s",name);flag = mkdir(name);if(flag == -1){printf("Error:\n The length of name is to long! \n");}else if(flag == -2){printf("Error:\n The direct item is already full ! \n");}else if(flag == -3){printf("Error:\n The name is already in the direct ! \n");}else if(flag == -4){printf("Error:\n \\ can not in the name of a direct ! \n");}else if(flag == -5){。
- 1、下载文档前请自行甄别文档内容的完整性,平台不提供额外的编辑、内容补充、找答案等附加服务。
- 2、"仅部分预览"的文档,不可在线预览部分如存在完整性等问题,可反馈申请退款(可完整预览的文档不适用该条件!)。
- 3、如文档侵犯您的权益,请联系客服反馈,我们会尽快为您处理(人工客服工作时间:9:00-18:30)。
实验四文件系统实验一 .目的要求1、用高级语言编写和调试一个简单的文件系统,模拟文件管理的工作过程。
从而对各种文件操作命令的实质内容和执行过程有比较深入的了解。
2、要求设计一个 n个用户的文件系统,每次用户可保存m个文件,用户在一次运行中只能打开一个文件,对文件必须设置保护措施,且至少有Create、delete、open、close、read、write等命令。
二 .例题:1、设计一个10个用户的文件系统,每次用户可保存10个文件,一次运行用户可以打开5个文件。
2、程序采用二级文件目录(即设置主目录[MFD])和用户文件目录(UED)。
另外,为打开文件设置了运行文件目录(AFD)。
3、为了便于实现,对文件的读写作了简化,在执行读写命令时,只需改读写指针,并不进行实际的读写操作。
4、算法与框图:①因系统小,文件目录的检索使用了简单的线性搜索。
②文件保护简单使用了三位保护码:允许读写执行、对应位为 1,对应位为0,则表示不允许读写、执行。
③程序中使用的主要设计结构如下:主文件目录和用户文件目录( MFD、UFD)打开文件目录( AFD)(即运行文件目录)文件系统算法的流程图如下:三 .实验题:1、增加 2~3个文件操作命令,并加以实现。
(如移动读写指针,改变文件属性,更换文件名,改变文件保护级别)。
#include<iostream>#include<string>#include<conio.h>using namespace std;struct TYPE_UFD{string File_Name;bool Read;bool Write;bool Execute;int Length_File;};struct TYPE_MFD{string User_Name;TYPE_UFD *Pointer;};struct TYPE_AFD{int File_ID;bool Read;bool Write;bool Execute;int Pointer;};class TYPE_FILE_SYSTEM{public:void Initial( void );void Start( void );private:int _Number_Users;int _Number_Files;int _MaxNumber_Open_Files;TYPE_MFD *_MFD;TYPE_UFD *_UFD;TYPE_AFD *_AFD;};void TYPE_FILE_SYSTEM::Initial( void ){_Number_Users = 10;_Number_Files = 10;_MaxNumber_Open_Files = 5;_UFD = new TYPE_UFD [_Number_Users*_Number_Files]; _MFD = new TYPE_MFD [_Number_Users];int i=0;for( i=0 ; i<_Number_Users ; i++ ){_MFD[i].Pointer = &(_UFD[i*_Number_Files]);}_AFD = new TYPE_AFD [_MaxNumber_Open_Files];_MFD[0].User_Name = "chaochao";_UFD[0].File_Name = "chaochao1.txt";_UFD[0].Length_File = 10;_UFD[0].Read = true;_UFD[0].Write = false;_UFD[0].Execute = true;_UFD[1].File_Name = "chaochao2.txt";_UFD[1].Length_File = 20;_UFD[1].Read = true;_UFD[1].Write = false;_UFD[1].Execute = false;for( i=2 ; i<_Number_Files ; i++ ){_UFD[i].File_Name = "";_UFD[i].Length_File = -1;_UFD[i].Read = false;_UFD[i].Write = false;_UFD[i].Execute = false;}}void TYPE_FILE_SYSTEM::Start( void ){int User_ID;int i,temp_int;string temp;char choice;int Number_Open_Files;string User_Name;string Command;TYPE_UFD *UFD;do{do{cout << "User name:";cin >> User_Name;for( User_ID=0 ; User_ID<_Number_Users ; User_ID++ ){if( _MFD[User_ID].User_Name == User_Name )break;}if( User_ID == _Number_Users )cout << "Bad user name , please try again ." << endl; }while( User_ID == _Number_Users );cout << "Ok , welcome to login , " << User_Name << " !" << endl; UFD = _MFD[User_ID].Pointer;for( i=0 ; i<_MaxNumber_Open_Files ; i++ ){_AFD[i].File_ID = -1;}Number_Open_Files = 0;do{cout << "C:\\" << User_Name << ">" ;cin >> Command;if( Command == "dir" ){cout << endl;cout << "Files of user " << User_Name << endl;cout << "\t" << "State\t" << "Length\t" << "File name" << endl;for( i=0 ; i<_Number_Files ; i++ ){if( UFD[i].Length_File != -1 ){cout << "\t" ;if( UFD[i].Read == true )cout << "R";elsecout << "-";if( UFD[i].Write == true )cout << "W";elsecout << "-";if( UFD[i].Execute == true )cout << "E";elsecout << "-";cout << "\t";cout << UFD[i].Length_File;cout << "\t";cout << UFD[i].File_Name << endl;}}cout << endl;}else if( Command == "diropen" ){cout << endl;cout << "Opening Files of user " << User_Name << endl; cout << "\t" << "State\t" << "Open File name" << endl; for( i=0 ; i<_MaxNumber_Open_Files ; i++ ){if( _AFD[i].File_ID != -1 ){cout << "\t" ;if( _AFD[i].Read == true )cout << "R";elsecout << "-";if( _AFD[i].Write == true )cout << "W";elsecout << "-";if( _AFD[i].Execute == true )cout << "E";elsecout << "-";cout << "\t";cout << UFD[_AFD[i].File_ID].File_Name << endl; }}cout << endl;}else if( Command == "create" ){for( i=0 ; i<_Number_Files ; i++ )if( UFD[i].Length_File == -1 )break;if( i == _Number_Files )cout << "Error: you have already had " <<_Number_Files << " files ." << endl;else{cout << "Please enter the information of the new file:" << endl;cout << "File name : ";cin >> temp;UFD[i].File_Name = temp;cout << "Read (y/n):";do{choice = getch();}while( choice!='y' && choice!='n' );if( choice == 'y' )UFD[i].Read = true;elseUFD[i].Read = false;cout << endl;cout << "Write (y/n):";do{choice = getch();}while( choice!='y' && choice!='n' );if( choice == 'y' )UFD[i].Write = true;elseUFD[i].Write = false;cout << endl;cout << "Execute (y/n):";do{choice = getch();}while( choice!='y' && choice!='n' );if( choice == 'y' )UFD[i].Execute = true;elseUFD[i].Execute = false;cout << endl;cout << "Length :";cin >> temp_int;if( temp_int > 0 )UFD[i].Length_File = temp_int;cout << "Ok , the new file " << UFD[i].File_Name << " is created!" << endl;}}else if( Command == "delete" ){cout << "Please enter the file name :";cin >> temp;for( i=0 ; i<_Number_Files ; i++ )if( (UFD[i].Length_File!=-1)&&(UFD[i].File_Name==temp) )break;if( i == _Number_Files )cout << "Bad file name , please try again ." << endl; else{UFD[i].Length_File = -1;cout << "Ok , the file " << UFD[i].File_Name << " is deleted ." << endl;}}else if( Command == "open" ){if( Number_Open_Files == _MaxNumber_Open_Files )cout << "Error: you have already opened " << Number_Open_Files << " files." << endl;else{cout << "Please enter the file name :";cin >> temp;for( i=0 ; i<_Number_Files ; i++ )if( (UFD[i].Length_File!=-1)&&(UFD[i].File_Name==temp) )break;if( i == _Number_Files )cout << "Bad file name , please try again ." << endl;else{Number_Open_Files++;for( temp_int=0 ;temp_int<_MaxNumber_Open_Files ; temp_int++ )if( _AFD[temp_int].File_ID == -1 )break;_AFD[temp_int].File_ID = i;_AFD[temp_int].Pointer = 0;cout << "Please configure the open mode :" << endl;if( UFD[i].Read == true ){cout << "Read (y/n):";do{choice = getch();}while( choice!='y' && choice!='n' );if( choice == 'y' )_AFD[temp_int].Read = true;else_AFD[temp_int].Read = false;cout << endl;}else_AFD[temp_int].Read = false;if( UFD[i].Write == true ){cout << "Write (y/n):";do{choice = getch();}while( choice!='y' && choice!='n' );if( choice == 'y' )_AFD[temp_int].Write = true;else_AFD[temp_int].Write = false;cout << endl;}else_AFD[temp_int].Write = false;if( UFD[i].Execute == true ){cout << "Execute (y/n):";do{choice = getch();}while( choice!='y' && choice!='n' );if( choice == 'y' )_AFD[temp_int].Execute = true;else_AFD[temp_int].Execute = false;cout << endl;}else_AFD[temp_int].Execute;cout << "Ok , the file " << temp << " is open ." << endl;}}}else if( Command == "logout" ){cout << "Ok , see you later , " << User_Name << " !" << endl;break;}else if( Command == "close" ){cout << "Please enter the file name :";cin >> temp;for( i=0 ; i<_Number_Files ; i++ )if( (UFD[i].Length_File!=-1)&&(UFD[i].File_Name==temp) )break;if( i == _Number_Files )cout << "Bad file name , please try again ." << endl; else{for( temp_int=0 ; temp_int<_MaxNumber_Open_Files ; temp_int++ )if( _AFD[temp_int].File_ID == i )break;if( temp_int == _MaxNumber_Open_Files )cout << "The file " << temp << " isn't open ." << endl;else{_AFD[temp_int].File_ID = -1;Number_Open_Files--;cout << "Ok , the file " << temp << " is closed ." << endl;}}}else if( Command == "read" ){cout << "Please enter the file name :";cin >> temp;for( i=0 ; i<_Number_Files ; i++ )if( (UFD[i].Length_File!=-1)&&(UFD[i].File_Name==temp) )break;if( i == _Number_Files )cout << "Bad file name , please try again ." << endl; else{for( temp_int=0 ; temp_int<_MaxNumber_Open_Files ; temp_int++ )if( _AFD[temp_int].File_ID == i )break;if( temp_int == _MaxNumber_Open_Files )cout << "The file " << temp << " isn't open ." << endl;else{if( _AFD[temp_int].Read == true )cout << "Ok , read the file " << temp << " successfully." << endl;elsecout << "Error: the open mode of the file doesn't allow you to read it ." << endl;}}}else if( Command == "write" ){cout << "Please enter the file name :";cin >> temp;for( i=0 ; i<_Number_Files ; i++ )if( (UFD[i].Length_File!=-1)&&(UFD[i].File_Name==temp) )break;if( i == _Number_Files )cout << "Bad file name , please try again ." << endl; else{for( temp_int=0 ; temp_int<_MaxNumber_Open_Files ; temp_int++ )if( _AFD[temp_int].File_ID == i )break;if( temp_int == _MaxNumber_Open_Files )cout << "The file " << temp << " isn't open ." << endl;else{if( _AFD[temp_int].Write == true )cout << "Ok , write the file " << temp << " successfully." << endl;elsecout << "Error: the open mode of the file doesn't allow you to write it ." << endl;}}}else if( Command == "shutdown" ){cout << "Loging out........" << endl;cout << "Ok , see you later , " << User_Name << " !" << endl;cout << "The power of the computer is shutting down.........." << endl;break;}else{cout << "Bad commands , please try again ." << endl; }}while( Command != "logout" && Command != "shutdown" );}while( Command != "shutdown" );}main(){TYPE_FILE_SYSTEM FS;FS.Initial();FS.Start();return 0;}截图如下所示:四、实验总结我觉得这次的实验真的好难啊,不会写,而且没有完整的思路,想要把自己的想法转换成代码来表示也是写不出来,在做实验的时候就参考了网上的代码,看着代码一个一个都很长,而且也不容易理解,经常就是要翻来覆去的看,正看下面的代码的时候还要往上翻,看相应的变量和函数是什么。