《操作系统磁盘文件管理源码.doc》由会员分享,可在线阅读,更多相关《操作系统磁盘文件管理源码.doc(45页珍藏版)》请在taowenge.com淘文阁网|工程机械CAD图纸|机械工程制图|CAD装配图下载|SolidWorks_CaTia_CAD_UG_PROE_设计图分享下载上搜索。
1、. .#include #include #include #define MEM_D_SIZE 1024*1024 /总磁盘空间为1M#define DISKSIZE 1024/磁盘块的大小1K#define DISK_NUM 1024/磁盘块数目1K#define FATSIZE DISK_NUM*sizeof(struct fatitem)/FAT表大小#define ROOT_DISK_NO FATSIZE/DISKSIZE+1/根目录起始盘块号#define ROOT_DISK_SIZE sizeof(struct direct)/根目录大小#define DIR_MAXSIZE 1
2、024/路径最大长度为1KB#define MSD 5/最大子目录数5#define MOFN 5/最大文件深度为5#define MAX_WRITE 1024*128/最大写入文字长度128KB struct fatitem /* size 8*/ int item; /*存放文件下一个磁盘的指针*/ char em_disk; /*磁盘块是否空闲标志位 0 空闲*/ ; struct direct /*-文件控制快信息-*/ struct FCB char name9; /*文件/目录名 8位*/ char property; /*属性 1位目录 0位普通文件*/ int size; /*
3、文件/目录字节数、盘块数)*/ int firstdisk; /*文件/目录起始盘块号*/ int next; /*子目录起始盘块号*/ int sign; /*1是根目录 0不是根目录*/ directitemMSD+2; ; struct opentable struct openttableitem char name9; /*文件名*/ int firstdisk; /*起始盘块号*/ int size; /*文件的大小*/ openitemMOFN; int cur_size; /*当前打文件的数目*/ ; struct fatitem *fat; /*FAT表*/ struct d
4、irect *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
5、); 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(); void format() int i;
6、 FILE *fp; fat = (struct fatitem *)(fdisk+DISKSIZE); /*计算FAT表地址,引导区向后偏移 1k)*/ /*-初始化FAT表-*/ fat0.item=-1; /*引导块*/ fat0.em_disk=1; for(i=1;iROOT_DISK_NO-1;i+) /*存放 FAT表的磁盘块号*/ fati.item=i+1; fati.em_disk=1; fatROOT_DISK_NO.item=-1; /*存放根目录的磁盘块号*/ fatROOT_DISK_NO.em_disk=1; for(i=ROOT_DISK_NO+1;idirec
7、titem0.sign = 1; root-directitem0.firstdisk = ROOT_DISK_NO; strcpy(root-directitem0.name,.); root-directitem0.next = root-directitem0.firstdisk; root-directitem0.property = 1; root-directitem0.size = ROOT_DISK_SIZE; /*-指向上一级目录的目录项-*/ root-directitem1.sign = 1; root-directitem1.firstdisk = ROOT_DISK_
8、NO; strcpy(root-directitem1.name,.); root-directitem1.next = root-directitem0.firstdisk; root-directitem1.property = 1; root-directitem1.size = ROOT_DISK_SIZE; if(fp = fopen(disk.dat,wb)=NULL) printf(Error:n Cannot open file n); return; for(i=2;idirectitemi.sign = 0; root-directitemi.firstdisk = -1;
9、 strcpy(root-directitemi.name,); root-directitemi.next = -1; root-directitemi.property = 0; root-directitemi.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)
10、; 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 filen); return; if(!fread(fdisk,MEM_D_SIZE,1,fp) /*把磁盘文件disk.dat 读入虚拟磁盘空间(存)*/ printf(Error:nCannot read filen); exit(0); fat = (struct fatitem *)(fdisk
11、+DISKSIZE); /*找到FAT表地址*/ root = (struct direct *)(fdisk+DISKSIZE+FATSIZE);/*找到根目录地址*/ fclose(fp); /*-初始化用户翻开表-*/ for(i=0;i8) /*文件名大于 8位*/ return(-1); for(j=2;jdirectitemj.name,name) break; if(jMSD+2) /*文件已经存在*/ return(-4); for(i=2;idirectitemi.firstdisk=-1) break; if(i=MSD+2) /*无空目录项*/ return(-2); i
12、f(u_opentable.cur_size=MOFN) /*翻开文件太多*/ return(-3); for(j=ROOT_DISK_NO+1;j=DISK_NUM) return(-5); fatj.em_disk = 1; /*将空闲块置为已经分配*/ /*-填写目录项-*/ strcpy(cur_dir-directitemi.name,name); cur_dir-directitemi.firstdisk = j; cur_dir-directitemi.size = 0; cur_dir-directitemi.next = j; cur_dir-directitemi.prop
13、erty = 0; /*-*/ fd = open(name); return 0; int open(char *name) int i, j; for(i=2;idirectitemi.name,name) break; if(i=MSD+2) return(-1); /*-是文件还是目录-*/ if(cur_dir-directitemi.property=1)return(-4); /*-文件是否翻开-*/ for(j=0;jMOFN;j+) if(!strcmp(u_opentable.openitemj.name,name) break; if(j=MOFN) /*文件翻开太多*/
14、 return(-3); /*-查找一个空闲用户翻开表项-*/ for(j=0;jdirectitemi.firstdisk; strcpy(u_opentable.openitemj.name,name); u_opentable.openitemj.size = cur_dir-directitemi.size; u_opentable.cur_size+; /*-返回用户翻开表表项的序号-*/ return(j); int close(char *name) int i; for(i=0;i=MOFN) return(-1); /*-清空该文件的用户翻开表项的容-*/ strcpy(u_
15、opentable.openitemi.name,); u_opentable.openitemi.firstdisk = -1; u_opentable.openitemi.size = 0; u_opentable.cur_size-; 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; char Endter= n; for(i=0
16、;ilen;i+) if(bufi = $) bufi = Space; else if(bufi = #) bufi = Endter; /*-读取用户翻开表对应表项第一个盘块号-*/ item = u_opentable.openitemfd.firstdisk; /*-找到当前目录所对应表项的序号-*/ for(i=2;idirectitemi.firstdisk=item) break; temp = i; /*-存放当前目录项的下标-*/ /*-找到的item 是该文件的最后一块磁盘块-*/while(fatitem.item!=-1) item =fatitem.item; /*-
17、查找该文件的下一盘块-*/ /*-计算出该文件的最末地址-*/ first = fdisk+item*DISKSIZE+u_opentable.openitemfd.size%DISKSIZE; /*-如果最后磁盘块剩余的大小大于要写入的文件的大小-*/ if(DISKSIZE-u_opentable.openitemfd.size%DISKSIZElen) strcpy(first,buf); u_opentable.openitemfd.size = u_opentable.openitemfd.size+len; cur_dir-directitemtemp.size = cur_dir
18、-directitemtemp.size+len; else for(i=0;i0) ilen2 = ilen2+1; /*-还需要多少块磁盘块-*/ for(j=0;jilen2;j+) for(i=ROOT_DISK_NO+1;i=DISK_NUM) /*-如果磁盘块已经分配完了-*/ return(-1); first = fdisk+i*DISKSIZE; /*-找到的那块空闲磁盘块的起始地址-*/ if(j=ilen2-1) /*-如果是最后要分配的一块-*/ for(k=0;klen-(DISKSIZE-u_opentable.openitemfd.size%DISKSIZE)-j
19、*DISKSIZE;k+) firstk = bufk; else/*-如果不是要最后分配的一块-*/ for(k=0;kdirectitemtemp.size = cur_dir-directitemtemp.size+len; return 0; int read(int fd, char *buf) int len = u_opentable.openitemfd.size; char *first; int i, j, item; int ilen1, modlen; item = u_opentable.openitemfd.firstdisk; ilen1 = len/DISKSI
20、ZE; modlen = len%DISKSIZE; if(modlen!=0) ilen1 = ilen1+1; /*-计算文件所占磁盘的块数-*/ first = fdisk+item*DISKSIZE; /*-计算文件的起始位置-*/ for(i=0;iilen1;i+) if(i=ilen1-1) /*-如果在最后一个磁盘块-*/ for(j=0;jlen-i*DISKSIZE;j+) bufi*DISKSIZE+j = firstj; else /*-不在最后一块磁盘块-*/ for(j=0;jlen-i*DISKSIZE;j+) bufi*DISKSIZE+j = firstj;
21、item = fatitem.item; /*-查找下一盘块-*/ first = fdisk+item*DISKSIZE; return 0; int del(char *name) int i,cur_item,item,temp; for(i=2;idirectitemi.name,name) break; cur_item = i; /*-用来保存目录项的序号,供释放目录中-*/ if(i=MSD+2) /*-如果不在当前目录中-*/ return(-1); if(cur_dir-directitemcur_item.property!=0) /*-如果删除的(不)是目录-*/ ret
22、urn(-3); for(i=0;idirectitemcur_item.firstdisk;/*-该文件的起始盘块号-*/ while(item!=-1) /*-释放空间,将FAT表对应项进展修改-*/ temp = fatitem.item; fatitem.item = -1; fatitem.em_disk = 0; item = temp; /*-释放目录项-*/ cur_dir-directitemcur_item.sign = 0; cur_dir-directitemcur_item.firstdisk = -1; strcpy(u_opentable.openitemcur_
23、item.name,); cur_dir-directitemcur_item.next = -1; cur_dir-directitemcur_item.property = 0; cur_dir-directitemcur_item.size = 0; return 0; int mkdir(char *name) int i,j; struct direct *cur_mkdir; if(!strcmp(name,.) return(-4); if(!strcmp(name,.) return(-4); if(strlen(name)8) /*-如果目录名长度大于 8位-*/ retur
24、n(-1); for(i=2;idirectitemi.firstdisk=-1) break; if(i=MSD+2) /*-目录/文件已满-*/ return(-2); for(j=2;jdirectitemj.name,name) break; if(jMSD+2) /*-如果有重名-*/ return(-3); for(j=ROOT_DISK_NO+1;j=DISK_NUM) return(-5); fatj.em_disk=1; /*-将该空闲块设置为已分配-*/ /*-填写目录项-*/ strcpy(cur_dir-directitemi.name,name); cur_dir-d
25、irectitemi.firstdisk=j; cur_dir-directitemi.size=ROOT_DISK_SIZE; cur_dir-directitemi.next=j; cur_dir-directitemi.property=1; /*-所创目录在虚拟磁盘上的地址(存物理地址)-*/ cur_mkdir=(struct direct *)(fdisk+cur_dir-directitemi.firstdisk*DISKSIZE); /*-初始化目录-*/ /*-指向当前目录的目录项-*/ cur_mkdir-directitem0.sign=0; cur_mkdir-dire
26、ctitem0.firstdisk=cur_dir-directitemi.firstdisk; strcpy(cur_mkdir-directitem0.name,.); cur_mkdir-directitem0.next=cur_mkdir-directitem0.firstdisk; cur_mkdir-directitem0.property=1; cur_mkdir-directitem0.size=ROOT_DISK_SIZE; /*-指向上一级目录的目录项-*/ cur_mkdir-directitem1.sign=cur_dir-directitem0.sign;cur_mk
27、dir-directitem1.firstdisk=cur_dir-directitem0.firstdisk; strcpy(cur_mkdir-directitem1.name,.); cur_mkdir-directitem1.next=cur_mkdir-directitem1.firstdisk; cur_mkdir-directitem1.property=1; cur_mkdir-directitem1.size=ROOT_DISK_SIZE; for(i=2;idirectitemi.sign=0; cur_mkdir-directitemi.firstdisk=-1; str
28、cpy(cur_mkdir-directitemi.name,); cur_mkdir-directitemi.next=-1; cur_mkdir-directitemi.property=0; cur_mkdir-directitemi.size=0; return 0; int rmdir(char *name) int i,j,item; struct direct *temp_dir; /*-检查当前目录项中有无该目录-*/ for(i=2;idirectitemi.name,name) break; if(i=MSD+2) /*-没有这个文件或目录-*/ return(-1);if
29、(cur_dir-directitemi.property!=1)/*-删除的不是目录-*/ return(-3); /*-判断要删除的目录有无子目录-*/ temp_dir=(struct direct *)(fdisk+cur_dir-directitemi.next*DISKSIZE); for(j=2;jdirectitemj.next!=-1) break; if(jdirectitemi.firstdisk; fatitem.em_disk=0; /*-修改目录项-*/ cur_dir-directitemi.sign=0; cur_dir-directitemi.firstdisk=-1; strcpy(cur_dir-directitemi.name,); cur_dir-directitemi.next=-1; cur_dir-directitemi.property=0; cur_dir-directitemi.size=0; return 0; void dir() int i; for(i=2;iMSD+2;i+) if(cur_dir