2022年操作系统-文件系统源代码 2.pdf
《2022年操作系统-文件系统源代码 2.pdf》由会员分享,可在线阅读,更多相关《2022年操作系统-文件系统源代码 2.pdf(21页珍藏版)》请在淘文阁 - 分享文档赚钱的网站上搜索。
1、#include#include#include#include#define BLOCKSIZE 1024#define SIZE 1024000#define END 65535#define FREE 0#define ROOTBLOCKNUM 2#define MAXOPENFILE 10 typedef struct FCB/unsigned char filename8;char exname3;unsigned char attribute;char reserve10;/me:保留区(图 3-2 MS-DOS的文件控制块)unsigned short time;unsigned
2、 short data;unsigned short first;unsigned long length;fcb;typedef struct FAT unsigned short id;fat;typedef struct USEROPEN fcb openfileMAXOPENFILE;char dirMAXOPENFILE80;/me:用来记录每个打开文件所在的目录名,以方便用户打开不同目录下具有相同文件名的不同文件。/me:即,相应打开文件所在的目录名,这样方便快速检查出指定文件是否已经打开int count;useropen;typedef struct BLOCK0/*引导块内容
3、*/char information200;/赋值 50 时本身已经越界unsigned char*startblock;/me:虚拟磁盘上数据区开始位置block0;名师资料总结-精品资料欢迎下载-名师精心整理-第 1 页,共 21 页 -unsigned char*myvhard;useropen*openfilelist;char currentdir80;/me:记录当前目录的目录名(包括目录的路径)unsigned char*startp;/me:记录虚拟磁盘上数据区开始位置void initsys();void format();void startsys();void exits
4、ys();void create();int open(char*filename);void close(int fd);void mywrite(int fd,char*buff);void myread(int fd,char*buff);void deletefile(char*filename);void ls();void listopenfile();void cd(char*dirname);/系统初始化:创建虚盘和格式化虚盘void initsys()myvhard=(unsigned char*)malloc(SIZE);memset(myvhard,0,SIZE);for
5、mat();void format()FILE*fp;time_t*now;struct tm*nowtime;unsigned char*p;fat*fat1,*fat2;fcb*root;int i,a;block0*b0;now=(time_t*)malloc(sizeof(time_t);p=myvhard;b0=(block0*)p;strcpy(b0-information,My filesystem,version0.01,blocksize is 1K,whole size is 1000K,emulate FAT16,Max open file number is 10);名
6、师资料总结-精品资料欢迎下载-名师精心整理-第 2 页,共 21 页 -/printf(n%s,b0-information);p+=BLOCKSIZE;/me:将虚拟磁盘的第一块分配给了引导块/me:给两个 FAT各分配两个盘块fat1=(fat*)(p);fat2=(fat*)(p+BLOCKSIZE*2);/me:对于每个 FAT 中,前面 5 个块设置为已分配,后面 995个块设置为空闲;/me:即,将两个 fat 表的前 5 个表项的 id 赋值 END,第 6 项为的 id 赋值 6(指向虚拟磁盘的第 6 块(即根目录文件所在块)fat1-id=END;fat2-id=END;fa
7、t1+;fat2+;fat1-id=END;fat2-id=END;fat1+;fat2+;fat1-id=END;fat2-id=END;fat1+;fat2+;fat1-id=END;fat2-id=END;fat1+;fat2+;fat1-id=END;fat2-id=END;fat1+;fat2+;fat1-id=6;fat2-id=6;fat1+;fat2+;fat1-id=END;fat2-id=END;fat1+;fat2+;/me:从第 7 个表项开始,初始化全部是空闲的for(i=7;ifilename,.);strcpy(root-exname,di);root-attri
8、bute=40;/me:表示为目录文件time(now);nowtime=localtime(now);root-time=nowtime-tm_hour*2048+nowtime-tm_min*32+nowtime-tm_sec/2;root-data=(nowtime-tm_year-80)*512+(nowtime-tm_mon+1)*32+nowtime-tm_mday;root-first=5;root-length=2*sizeof(fcb);root+;strcpy(root-filename,.);strcpy(root-exname,di);root-attribute=40
9、;time(now);nowtime=localtime(now);root-time=nowtime-tm_hour*2048+nowtime-tm_min*32+nowtime-tm_sec/2;root-data=(nowtime-tm_year-80)*512+(nowtime-tm_mon+1)*32+nowtime-tm_mday;root-first=5;root-length=2*sizeof(fcb);root+;for(i=2;ifilename0=0;a=BLOCKSIZE*2/sizeof(fcb);fp=fopen(c:myfsys,w);b0-startblock=
10、p+BLOCKSIZE*4;/me:虚拟磁盘上数据区开始位置fwrite(myvhard,SIZE,1,fp);fclose(fp);free(myvhard);名师资料总结-精品资料欢迎下载-名师精心整理-第 4 页,共 21 页 -/格式化之后填充初始内容void startsys()FILE*fp;int i;openfilelist=(useropen*)malloc(sizeof(useropen);memset(openfilelist,0,sizeof(useropen);fp=fopen(c:myfsys,r);myvhard=(unsigned char*)malloc(SI
11、ZE);memset(myvhard,0,SIZE);fread(myvhard,SIZE,1,fp);strcpy(openfilelist-openfile0.filename,root);strcpy(openfilelist-openfile0.exname,di);openfilelist-openfile0.attribute=168;openfilelist-openfile0.time=(fcb*)(myvhard+5*BLOCKSIZE)-time;openfilelist-openfile0.data=(fcb*)(myvhard+5*BLOCKSIZE)-data;ope
12、nfilelist-openfile0.first=(fcb*)(myvhard+5*BLOCKSIZE)-first;openfilelist-openfile0.length=(fcb*)(myvhard+5*BLOCKSIZE)-length;openfilelist-count=1;strcpy(currentdir,root);strcpy(openfilelist-dir0,currentdir);for(i=6;istartblock;fclose(fp);void exitsys()FILE*fp;fp=fopen(c:myfsys,w);fwrite(myvhard,SIZE
13、,1,fp);free(myvhard);free(openfilelist);fclose(fp);/创建文件或目录void create()名师资料总结-精品资料欢迎下载-名师精心整理-第 5 页,共 21 页 -time_t*now;struct tm*nowtime;char current8;fcb*freefcb,*updir,*p;/me:前者指向分配的空闲FCB,后者指向自身在其父目录的 fcb 位置int i,j,k,m,flag,b;unsigned short index;fat*fat1,*fat2,*fatcurrent1,*fatcurrent2;now=(time
14、_t*)malloc(sizeof(time_t);if(openfilelist-count=MAXOPENFILE)printf(open too many files!n);return;flag=0;/me:找到当前所在目录for(i=80;i=0;i-)if(currentdiri=)flag+;if(flag=2)break;i+;/me:找到当前目录的上一目录for(j=0;currentdiri!=;i+,j+)currentj=currentdiri;currentj=0;/me:通过检查 attribute 位的末三位是不是8 来确定文件类型(目录文件还是数据文件)for(
15、i=0;iopenfilei.attribute&8)=8)break;fat1=(fat*)(myvhard+BLOCKSIZE);index=openfilelist-openfilei.first;freefcb=(fcb*)(myvhard+openfilelist-openfilei.first*BLOCKSIZE);for(m=0;mopenfilei.length/BLOCKSIZE+1;m+)名师资料总结-精品资料欢迎下载-名师精心整理-第 6 页,共 21 页 -for(j=0;jfilename0=0)break;freefcb+;if(j=32)freefcb=(fcb*
16、)(myvhard+fat1index.id);index=fat1index.id;if(strcmp(currentdir,root)!=0)updir=(fcb*)(myvhard+openfilelist-openfilei.first*BLOCKSIZE);if(j=openfilelist-openfilei.length/32)for(k=0;kopenfilei.length/32;j+)if(strcmp(updir-filename,.)=0)break;updir+;fat1=(fat*)(myvhard+BLOCKSIZE);fat2=(fat*)(myvhard+BL
17、OCKSIZE*3);for(m=7;mid=FREE)break;fat1+;fat2+;fatcurrent1=(fat*)(myvhard+BLOCKSIZE);fatcurrent2=(fat*)(myvhard+BLOCKSIZE*3);index=openfilelist-openfilei.first;while(fatcurrent1index.id!=END)index=fatcurrent1index.id;if(m=SIZE/BLOCKSIZE)名师资料总结-精品资料欢迎下载-名师精心整理-第 7 页,共 21 页 -printf(cant creat filen);re
18、turn;fatcurrent1index.id=m;fatcurrent2index.id=m;fatcurrent1m.id=END;fatcurrent2m.id=END;for(k=0;klength/32;k+)if(strcmp(updir-filename,currentdir)=0)break;updir+;fat1=(fat*)(myvhard+BLOCKSIZE);fat2=(fat*)(myvhard+BLOCKSIZE*3);for(m=0;mid=FREE)break;fat1+;fat2+;fat1-id=END;fat2-id=END;updir=(fcb*)(m
19、yvhard+openfilelist-openfilei.first*BLOCKSIZE);updir-length+=sizeof(fcb);updir+;updir-length+=sizeof(fcb);updir-;for(k=0;kopenfilei.length/32;j+)if(strcmp(updir-filename,.)=0)break;updir+;updir=(fcb*)(myvhard+updir-first*BLOCKSIZE);for(k=0;klength/32;k+)if(strcmp(updir-filename,currentdir)=0)名师资料总结-
20、精品资料欢迎下载-名师精心整理-第 8 页,共 21 页 -break;updir+;updir-length+=sizeof(fcb);else updir=(fcb*)(myvhard+openfilelist-openfilei.first*BLOCKSIZE);updir-length+=sizeof(fcb);updir+;updir-length+=sizeof(fcb);fat1=(fat*)(myvhard+BLOCKSIZE);fat2=(fat*)(myvhard+BLOCKSIZE*3);for(m=0;mid=FREE)break;fat1+;fat2+;fat1-id
21、=END;fat2-id=END;openfilelist-openfilei.length+=sizeof(fcb);printf(please input filename:);scanf(%s,freefcb-filename);printf(please input filetype:);scanf(%s,freefcb-exname);freefcb-attribute=0;if(strcmp(freefcb-exname,di)=0)freefcb-attribute+=8;p=(fcb*)(myvhard+m*BLOCKSIZE);strcpy(p-filename,.);str
22、cpy(p-exname,di);p-attribute=40;time(now);nowtime=localtime(now);p-time=nowtime-tm_hour*2048+nowtime-tm_min*32+nowtime-tm_sec/2;名师资料总结-精品资料欢迎下载-名师精心整理-第 9 页,共 21 页 -p-data=(nowtime-tm_year-80)*512+(nowtime-tm_mon+1)*32+nowtime-tm_mday;p-first=m;p-length=2*sizeof(fcb);p+;strcpy(p-filename,.);strcpy(p
23、-exname,di);p-attribute=40;time(now);nowtime=localtime(now);p-time=nowtime-tm_hour*2048+nowtime-tm_min*32+nowtime-tm_sec/2;p-data=(nowtime-tm_year-80)*512+(nowtime-tm_mon+1)*32+nowtime-tm_mday;p-first=openfilelist-openfilei.first;p-length=2*sizeof(fcb);freefcb-length=2*sizeof(fcb);printf(is this fil
24、e readonly?(1 or 0);scanf(%d,&b);if(b=1)freefcb-attribute+=128;printf(is this file hide?(1 or 0);scanf(%d,&b);if(b=1)freefcb-attribute+=64;printf(is this file system?(1 or 0);scanf(%d,&b);if(b=1)freefcb-attribute+=32;time(now);nowtime=localtime(now);freefcb-time=nowtime-tm_hour*2048+nowtime-tm_min*3
25、2+nowtime-tm_sec/2;freefcb-data=(nowtime-tm_year-80)*512+(nowtime-tm_mon+1)*32+nowtime-tm_mday;名师资料总结-精品资料欢迎下载-名师精心整理-第 10 页,共 21 页 -freefcb-first=m;if(freefcb-attribute&8)!=8)freefcb-length=0;open(freefcb-filename);void ls()int i,l;fcb*current;for(i=0;iopenfilei.attribute&8)=8)break;current=(fcb*)(
- 配套讲稿:
如PPT文件的首页显示word图标,表示该PPT已包含配套word讲稿。双击word图标可打开word文档。
- 特殊限制:
部分文档作品中含有的国旗、国徽等图片,仅作为作品整体效果示例展示,禁止商用。设计者仅对作品中独创性部分享有著作权。
- 关 键 词:
- 2022年操作系统-文件系统源代码 2022 操作系统 文件系统 源代码
限制150内