




版权说明:本文档由用户提供并上传,收益归属内容提供方,若内容存在侵权,请进行举报或认领
文档简介
1、华南理工大学软件学院2013级操作系统大作业实验报告课程名称:操作系统任课老师:作业题目:简单二级文件系统班级:学生姓名:学号:目录1 .实验目的2 .实验环境3 .实验内容4 .程序中使用的数据结构及符号说明5 .源程序及注释6 .程序运行时的初值和运行结果一、实验目的通过一个简单多用户文件系统的设计,加深理解文件系统的内部功能及内部实现。二、实验环境操作系统:Ubuntu12.04(Ubuntu/Linaro4.6.3-1ubuntu5)编译器:gcc4.6.3语言:C语言三、实验内容FMSV1.0系统模拟了原生的文件系统,使用文本来当作磁盘,并使用了空闲块记录表,目录,i-node等数据
2、结构来辅助文件系统的运作。磁盘:一个名为blockDisk的文件,里面含有256个数据块。每个数据块的长度为1024,可存储1024个字符。I节点:包含文件名,文件保护码,文件长度,文件存放位置。目录:一个链表,记录了i节点。空闲块记录表FBT:用于记录磁盘块使用情况,0表示未使用,1表示使用。数据块:保存了文件的数据,一个数据块至多存放一个文件。数据块使用情况:用于记录文件使用数据块的情况。存放在I节点的fat数组中。文件:一个文件最多占用两个数据块,即长度至多为4028.FMSV1.0系统支持的命令行:login:登录register:注册open:打开文件read:读取文件数据到屏幕wr
3、ite:写文件,分为cover和append两种类型;cover是覆盖原数据,append是在不改变原始数据的基础上进行增加。close:关闭文件create:新增文件delete:删除文件exit:退出程序help:帮助四、程序中使用的数据结构及符号说明/*结构定义*/*用户*/typedefstructusercharaccount15;账户最长为10charpassword15;密码最长为10,多出来的是为了方便运算user;/*数据节点*/typedefstructdatanode数据块的哪个范围(intnum;用于记录文件数据保存在哪个intbegin;intend;datanode
4、;/*i节点*/typedefstructinode(Charfilename30;Intnum;charcode30;数据块号数据开始位置数据结束位置保护码intsize;datanodefatMAX_DATANODE_NUM;intnode_num;inode;typedefstructdirEntry用链表来记录I节点inodeind;structdirEntry*next;dirEntry;/*数据块*/typedefstructblock(charcontentMAX_BLOCK_SIZE;数据块内容最大长度为1025预留最后一位用来存储'0',intnum;into
5、ffset;记录当前数据的数量block;五、源程序及注释head.h头文件#include<string.h>#include<stdio.h>#include<stdlib.h>最大数据块数量是数据块数据容量,一#include<stdbool.h>#defineMAX_BLOCKS_NUM256256,则数据块总大小是256KB#defineMAX_BLOCK_SIZE1025个数据块的大小是1KB,最有1位用来存储'0',表示字符串结尾!#defineMAX_DATANODE_NUM2每个文件最多占用的数据块的数量#def
6、ineMAX_INODE_NUM512/i节点的最大数目,亦即系统允许容纳文件的最大数量#defineMAX_CACHE_NUM32允许缓存的最大数据块数量为32个数据块,缓存为32KB/*结构定义*/*用户*/typedefstructusercharaccount15;账户最长为10charpassword15;密码最长为10,多出来的是为了方便运算user;/*数据节点*/用于记录文件数据保存在哪typedefstructdatanode个数据块的哪个范围(数据块号数据开始位置数据结束位置intnum;intbegin;intend;datanode;/*i节点*/typedefstru
7、ctinode(charfilename30;intnum;charcode30;保护码intsize;datanodefatMAX_DATANODE_NUM;intnode_num;inode;typedefstructdirEntry用链表来记录I节点inodeind;structdirEntry*next;dirEntry;/*数据块*/typedefstructblock数据块内容用于记录数据块记录当前数据的(charcontentMAX_BLOCK_SIZE;最大长度为1024intnum;boolisRevised;是否进行数据修改intoffset;数量block;/*定义全局变
8、量*/intislogin=0;/0meansthecharblockspath30="userdata/blocksDISK.disk"/所有数据(即虚拟磁盘)地址charuserspath30="userdata/users.us"staticusercurrUser;当前用户staticdirEntry*DIR;staticintcurr_inode_num=0;当前i节点数量,亦即文件数量staticblock*cache;intmax_node=0;最大的inode编号staticintFBTMAX_BLOCKS_NUM;staticchar*
9、dirpath;staticcharfbtpath30="userdata/FBT.disk"staticdirEntry*selectEntry;当selectEntry=NULL时,证明没有打开文件staticdirEntry*currEntry;命令行staticcharcm_help10="help"staticcharcm_lg10="login"staticcharcm_rg10="register"staticcharcm_dir10="dir"staticcharcm_creat
10、e10="create"staticcharcm_delete10="delete"staticcharcm_open10="open"staticcharcm_read10="read"staticcharcm_write10="write"staticcharcm_close10="close"staticcharcm_exit10="exit"staticcharcm_cancle10="cancle"/*函数声明*/voidcr
11、eateStyle(int,char);voidcreateWrap(int);intcreateSystem();intdiskUpdate(block);voidstart();voidgetUser();intlogin();intregist();charlgOrRg();voidcreateBlocksDisk();FILE*createDir();voidinitDir(char*);intgetAction();voidinitFBT();初始化数据空闲块记录表intgetCode();intgetFreeBlock(bool);voidsaveDir();voidsaveBlo
12、ck(blockbk);voidsaveFBT();intcreateHelp(char*filename);dirEntry*delHelp(char*filename);voidcreateDataDir();登录或注册响应指令的函数intLgRg();voidhelp();voiddir();voidcreate();voiddel();voidopen();voidclose();voidread();voidwrite();voidcoverHelp();voidappendHelp();/Linuxdoesn'tsupportthefunctionstrcmpi,soIne
13、strcmpi(char*p1,char*p2)if(strlen(p1)!=strlen(p2)return-1;inti;for(i=0;i<strlen(p1);i+)if(p1i!=p2i)return-1;)return0;)voidcreateDataDir()(char*datapath="userdata"printf("NodataDir!nNowcreateitn");if(mkdir(datapath,0777)(printf("CannotcreateDataDirn
14、PleasecontactQQ878631510forhelpnCrtl+Ctoexit.n");while(1);)printf("SucceedcreatingdataDir!n");)voidshowInode(inodeind)printf("num:%dn",ind.num);printf("filename:%sn",ind.filename);printf("size:%dn",ind.size);)界面美化函数voidcreateStyle(intnum,charstyle)(while(
15、num-)(printf("%c",style);)voidcreateWrap(intn)(while(n-)(printf("n");辅助函数charlgOrRg()while(1)charcom10;printf("loginorregister:");scanf("%s",com);if(!strcmpi(com,cm_lg)(returnT;if(!strcmp(com,cm_rg)(return'r'voidhelp()printf("欢迎您使用FMS文件系统模拟系统V1.0&
16、quot;);printf("以下是本系统支持的指令:n");printf("exit:退出n");printf("help:帮助n");printf("dir:查看目录中的所有文件n");printf("create:新建文件n");printf("delete:删除文件n");printf("open:打开文件n");printf("read:读文件(必须先打开文件)n");printf("write:写文件(必须先打开文
17、件)n");printf("close:关闭文件n");voidcreateBlocksDisk()初始化磁盘块FILE*fp=fopen(blockspath,"w");if(fp=NULL)printf("CannotcreateDisk!nCtrl+Ctoquitn");while(1);else(inti,j;for(i=0;i<MAX_BLOCKS_NUM;i+)(for(j=0;j<MAX_BLOCK_SIZE;j+)(fputc('$',fp);)fclose(fp);)FILE*p
18、=fopen(fbtpath,"w");if(p=NULL)(printf("FBTCreatedERROR!n");while(1);)elseinti;for(i=0;i<MAX_BLOCKS_NUM;i+)(FBTi=0;fprintf(p,"%d",0);fclose(p);dirEntry*isInDir(char*filename)(inti;dirEntry*pt=DIR;while(pt!=NULL)(if(!strcmpi(pt->ind.filename,filename)(returnpt;pt=pt
19、->next;returnNULL;dirEntry*delHelp(char*filename)(dirEntry*res=DIR;if(res=NULL)(printf("Nofiles!n");returnres;)if(res->next=NULL)(if(!strcmpi(res->ind.filename,filename)(DIR=NULL;currEntry=NULL;printf("删除成功!n");returnres;)elsereturnNULL;if(!strcmpi(res->ind.filename,f
20、ilename)(DIR=res->next;printf("删除成功!n");returnres;)while(res->next!=NULL)(if(!strcmpi(res->next->ind.filename,filename)(dirEntry*r=res->next;res->next=r->next;printf("删除成功!n");returnr;)res=res->next;)printf("删除失败!n");returnNULL;voidcoverHelp()(释放
21、原数据块,更新FBTintf;if(selectEntry->ind.size!=0)要size>0才会分配数据块(for(f=0;f<selectEntry->ind.node_num;f+)(FBTselectEntry->ind.fatf.num=0;charcontentMAX_DATANODE_NUMMAX_BLOCK_SIZE;chartmp;inodeind;printf("Endwith'$''n");inti=0;while(tmp=getchar()!='$')(if(i=0&
22、&tmp='n')(continue;contenti/MAX_BLOCK_SIZEi+=tmp;)ind.size=i;此时已经结束输入if(i>(MAX_BLOCK_SIZE-1)*MAX_DATANODE_NUM)printf("文件过大,无法存储,创建失败!n");return;)intk;for(k=0;k<=i/(MAX_BLOCK_SIZE-1);k+)blockbk;intbkn;for(bkn=0;bkn<MAX_BLOCK_SIZE-1;bkn+)bk.contentbkn='$')bk.cont
23、entMAX_BLOCK_SIZE-1='0'/printf("bk.content:%sn",bk.content);char*tmp;inttp=0;intlen=0;if(k=0)(if(i<MAX_BLOCK_SIZE-1)(len=i;if(k=1)(len=i%(MAX_BLOCK_SIZE-1)+1;for(tp=0;tp<len;tp+)(bk.contenttp=contentktp;bk.isRevised=true;if(k=0)bk.num=getFreeBlock(false);)else(bk.num=getFreeB
24、lock(true);if(bk.num=-1)(printf("数据块已用完,内存不足!n");return;)saveBlock(bk);ind.fatk.num=bk.num;ind.fatk.begin=0;ind.fatk.end=len;)ind.node_num=k;strcpy(ind.code,selectEntry->ind.code);strcpy(ind.filename,selectEntry->ind.filename);ind.num=selectEntry->ind.node_num;selectEntry->ind=
25、ind;saveDir();saveFBT();printf("文件已保存!n");)voidappendHelp()chartmpMAX_BLOCK_SIZE*2;charch;printf("Endwith'$':n");inti=0;while(ch=getchar()!='$')if(i=0&&ch='n')continue;)tmpi+=ch;)tmpi='0'此时已经完成输入if(i+selectEntry->ind.size)>(MAX_BLOCK_
26、SIZE-1)*MAX_DATANODE_NUM)printf("文件过大,无法存储,创建失败!n");return;else(if(selectEntry->ind.size>MAX_BLOCK_SIZE-1)已经占用了两个block(intoffset=selectEntry->ind.size-MAX_BLOCK_SIZE+1;FILE*bfp=fopen(blockspath,"r+");if(bfp=NULL)(printf("DISKERROR!nFromappendFile.n");return;)el
27、se(fseek(bfp,(selectEntry->ind.fat1.num*(MAX_BLOCK_SIZE-1)+offset),SEEK_SET);fwrite(tmp,sizeof(char),i,bfp);fclose(bfp);selectEntry->ind.size=selectEntry->ind.size+i;selectEntry->ind.fat1.end=selectEntry->ind.fat1.end+i;saveDir();printf("文件保存成功!n");else只占用了一个blockif(i<(MA
28、X_BLOCK_SIZE-1-selectEntry->ind.size)不会占用新的blockFILE*bfp=fopen(blockspath,"r+");if(bfp=NULL)printf("DISKERROR!nFromappendFile.n");return;else/if(selectEntry->ind.size=0)/fseek(bfp,0,SEEK_SET);/selectEntry->ind.fat0=0;/else/fseek(bfp,(selectEntry->ind.fat0.num*(MAX_BLO
29、CK_SIZE-1)+selectEntry->ind.size),SEEK_SET);/printf("ftell=%l",ftell(bfp);fseek(bfp,(selectEntry->ind.fat0.num*(MAX_BLOCK_SIZE-1)+selectEntry->ind.size),SEEK_SET);fwrite(tmp,sizeof(char),i,bfp);fclose(bfp);selectEntry->ind.size=selectEntry->ind.size+i;selectEntry->ind.fat
30、0.endselectEntry->ind.fat0.end+i;saveDir();printf("文件保存成功!n");else要占用新的block(intbkNum=getFreeBlock(true);if(bkNum=-1)(printf("数据块已用完,内存不足!n");*p1return;char*p2(char*)malloc(MAX_BLOCK_SIZE-1-selectEntry->ind.size)*sizeof(char);char(char*)malloc(i-(MAX_BLOCK_SIZE-1-selectEntr
31、y->ind.size)*sizeof(char);intpi;intpn1=0,pn2=0;for(pi=0;pi<i;pi+)(if(pi<MAX_BLOCK_SIZE-1-selectEntry->ind.size)p1pn1+=tmppi;)else(p2pn2+=tmppi;)p1pn1='0'p2pn2='0'存储FILE*bfp=fopen(blockspath,"r+");if(bfp=NULL)(printf("DISKERROR!nFromappendFilen");return
32、;)else(fseek(bfp,(MAX_BLOCK_SIZE-1)*selectEntry->ind.fat0.num+selectEntry->ind.fat0.end),SEEK_SET);fwrite(p1,sizeof(char),pn1,bfp);printf("linenear481n");fseek(bfp,(MAX_BLOCK_SIZE-1)*bkNum),SEEK_SET);fwrite(p2,sizeof(char),pn2,bfp);fclose(bfp);FBTbkNum=1;selectEntry->ind.node_num=
33、2;selectEntry->ind.size=selectEntry->ind.size+i;selectEntry->ind.fat0.endMAX_BLOCK_SIZE-2;selectEntry->ind.fat1.num=bkNum;selectEntry->ind.fat1.begin=0;selectEntry->ind.fat1.end=pn2;saveFBT();saveDir();printf("文件保存成功!n");核心函数voidcreate()intbkNum=getFreeBlock(false);if(bkN
34、um=-1)printf("数据块已用完,内存不足!n");return;chartmp;dirEntry*pt=(dirEntry*)malloc(sizeof(dirEntry);pt->next=NULL;while(1)printf("filename:");scanf("%s”,pt->ind.filename);if(isInDir(pt->ind.filename)!=NULL)(printf("文件名已存在!n请重新输入:n");elsebreak;)while(1)(printf(&quo
35、t;Doyouwanttowritethefile?y/n:");scanf("%c”,&tmp);if(tmp='y')|(tmp='Y')|(tmp='n')|(tmp='N')(break;)pt->ind.num=curr_inode_num+;文件保护码不是很懂,因此默认为"rrrwwwxxx"charcode10="rrrwwwxxx"strcpy(pt->ind.code,code);pt->ind.size=0;pt->in
36、d.node_num=0;初始化,跟存储要相符合boolisNoBk=false;if(tmp='y'|tmp='Y')(charcontentMAX_DATANODE_NUMMAX_BLOCK_SIZE;chartmp;printf("Endwith'$''n");inti=0;while(tmp=getchar()!='$')(if(i=0&&tmp='n')(continue;contenti/MAX_BLOCK_SIZEi+=tmp;pt->ind.size
37、=i;此时已经结束输入if(i>(MAX_BLOCK_SIZE-1)*MAX_DATANODE_NUM)(printf("文件过大,无法存储,创建失败!n");return;intk;for(k=0;k<=i/(MAX_BLOCK_SIZE-1);k+)(blockbk;intbkn;for(bkn=0;bkn<MAX_BLOCK_SIZE-1;bkn+)(bk.contentbkn='$'bk.contentMAX_BLOCK_SIZE-1=''0'printf("bk.content:%sn"
38、,bk.content);char*tmp;inttp=0;intlen=0;if(k=0)(if(i<MAX_BLOCK_SIZE-1)(len=i;if(k=1)len=i%(MAX_BLOCK_SIZE-1)+1;)for(tp=0;tp<len;tp+)(bk.contenttp=contentktp;)bk.isRevised=true;if(k=0)(bk.num=bkNum;)else(bk.num=getFreeBlock(true);if(bk.num=-1)(printf("数据块已用完,内存不足!n");return;)saveBlock(
39、bk);pt->ind.fatk.num=bk.num;pt->ind.fatk.begin=0;pt->ind.fatk.end=len;)pt->ind.node_num=k;)if(currEntry=NULL)DIR=pt;)elsecurrEntry->next=pt;)currEntry=pt;saveDir();saveFBT();printf("Succeedcreatefile%s!",pt->ind.filename);)voiddel()chartmp30;printf("请输入要删除的文件名:"
40、);scanf("%s",tmp);if(isInDir(tmp)=NULL)(printf("不存在这个文件。n");return;else(dirEntry*dle=delHelp(tmp);if(dle!=NULL)(inti;for(i=0;i<dle->ind.node_num;i+)(FBTdle->ind.fati.num=0;saveDir();saveFBT();voidopen()(charfile50;printf("请输入文件名:");scanf("%s",file);se
41、lectEntry=isInDir(file);if(selectEntry=NULL)(printf("没有这个文件!n");)else(printf("文件%s已打开,输入close关闭.n",file);intc=0;while(1)(if(c=1)(break;switch(getCode()(case 5:read();break;case 6:write();break;case 7:close();c=1;break;default:printf("无效的指令n");voidread()(FILE*bfp=fopen(b
42、lockspath,"r");if(bfp=NULL)(printf("不存在磁盘文件!n");while(1);)else打开磁盘文件(inti;chartmp=''printf("文件%s中的内容如下:n",selectEntry->ind.filename);if(selectEntry->ind.size=0)(printf(”内容为空。n");)else(for(i=0;i<selectEntry->ind.node_num;i+)(fseek(bfp,(selectEntr
43、y->ind.fati.num*(MAX_BLOCK_SIZE-1),SEEK_SET);这个offset彳艮重要intj;for(j=selectEntry->ind.fati.begin;j<selectEntry->ind.fati.end;j+)tmp=fgetc(bfp);printf("%c",tmp);printf("n");fclose(bfp);voidclose()selectEntry=NULL;printf("文件已关闭!n");voidwrite()charsel10;charcm_cover10=&
温馨提示
- 1. 本站所有资源如无特殊说明,都需要本地电脑安装OFFICE2007和PDF阅读器。图纸软件为CAD,CAXA,PROE,UG,SolidWorks等.压缩文件请下载最新的WinRAR软件解压。
- 2. 本站的文档不包含任何第三方提供的附件图纸等,如果需要附件,请联系上传者。文件的所有权益归上传用户所有。
- 3. 本站RAR压缩包中若带图纸,网页内容里面会有图纸预览,若没有图纸预览就没有图纸。
- 4. 未经权益所有人同意不得将文件中的内容挪作商业或盈利用途。
- 5. 人人文库网仅提供信息存储空间,仅对用户上传内容的表现方式做保护处理,对用户上传分享的文档内容本身不做任何修改或编辑,并不能对任何下载内容负责。
- 6. 下载文件中如有侵权或不适当内容,请与我们联系,我们立即纠正。
- 7. 本站不保证下载资源的准确性、安全性和完整性, 同时也不承担用户因使用这些下载资源对自己和他人造成任何形式的伤害或损失。
最新文档
- 2025年艺术生联考专项考试试卷及答案重点
- 2025年心理学入门知识测试题及答案
- 2025年甘肃省中考语文试卷真题(含标准答案)
- 2025年舞蹈艺术与表演技巧期末考试试题及答案
- 2025年无人机技术应用与管理考试试卷及答案
- 2025年数字媒体艺术专业考试试卷及答案
- 2025年农村经济与管理考试试卷及答案
- 2025年编程语言与软件开发能力评估试题及答案
- 2025年电气工程及其自动化专业考试试卷及答案
- 2025年甘肃省武威市民勤县收成镇选聘专业化管理村文书笔试参考题库及答案详解一套
- 复合性溃疡的健康宣教
- 山东电动伸缩雨棚施工方案
- 新媒体营销技术与应用PPT完整全套教学课件
- 第5章红外教学课件
- 卡氏肺孢子虫肺炎
- 大足县某水库除险加固工程施工组织设计
- 基于单片机数字电压表电路设计外文文献原稿和译文
- JJG 1149-2022电动汽车非车载充电机(试行)
- 2023版浙江评审卫生高级专业技术资格医学卫生刊物名录
- GB/T 1689-1998硫化橡胶耐磨性能的测定(用阿克隆磨耗机)
- GB/T 16823.3-2010紧固件扭矩-夹紧力试验
评论
0/150
提交评论