已阅读5页,还剩28页未读, 继续免费阅读
版权说明:本文档由用户提供并上传,收益归属内容提供方,若内容存在侵权,请进行举报或认领
文档简介
昆明理工大学信息工程与自动化学院学生实验报告( 201 201 学年 第 二 学期 )课程名称:操作系统 开课实验室: 年 月 日年级、专业、班 学号 姓名 成绩实验项目名称文件管理指导教师 教师评语 教师签名: 年 月 日一、实验目的用C或C+语言编写和调试一个简单的文件系统,模拟文件管理的基本功能。从而对各种文件操作命令的实质内容和执行过程有比较深入的了解。二、实验原理及基本技术路线图(方框原理图)用C模拟实现文件系统的管理;要求设计一个多级目录结构的文件系统,能正确描述文件控制块,采用合理的外存分配方式,能实现基本的目录及文件的操作,包括创建、删除、重命名、复制、移动等功能,并对文件有一定的存取权限控制。功能设计 :Help显示命令帮助dir显示当前目录下的文件和文件夹exit退出系统create 文件名创建文本文件cdir 目录名创建文件夹read 文件名读取一个文件最多可同时读取五个close文件名关闭一个文件edit 文件名编辑一个文件cd 目录名进子目录或者上级目录attr 文件名显示该文件的属性del 文件名删除文件rename 文件名重命名 编辑功能流程图 删除文件流程图 创建文件流程图核心算法:bool Format(void);/格式化bool install(void);/装载虚拟硬盘的数据void login(void);/用户登陆void showMenu(void);/显示功能菜单bool onAction(void);/用户选择功能并执行void createFile(string str);/创建文件bool read(string str);/读取文件void editFile(string str);/编辑文件void Delete(string str);/删除一个文件数据结构:/*-常变量-*/const unsigned int BLOCK_SIZE=512;/块长const unsigned int DATA_BLOCK_NUM=512;/数据块数量const unsigned int DINODE_START=4*BLOCK_SIZE;/inode起始位置const unsigned int DINODE_SIZE=512;/inode大小const unsigned int DINODE_NUM=32;/inode数量const unsigned int DATASTART=(2+DINODE_NUM)*BLOCK_SIZE;/数据区的开始地址const unsigned int ACCOUNT_NUM=10;/用户数量/*inode结构体*/struct inodeunsigned short di_tag;/*inode标识*/ unsigned short di_number;/*关联文件数,当为0时表示删除文件,如一个目录至少包含两个文件:.和.*/unsigned short di_mode;/*存取模式:0为目录,1为文件*/ unsigned short di_userID;/*当前inode所属用户 0为根目录ID,一次下去是管理员目录、用户目录*/unsigned short di_access;/*访问权限 0为不允许普通用户访问(公共目录),1为允许普通用户访问*/unsigned short di_size;/*文件大小,目录没有大小,值为0*/unsigned short di_ctime; /* 创建时间 */unsigned short di_mtime; /* 最后一次修改时间*/unsigned short di_blockDATA_BLOCK_NUM; /* 数据块块地址编号 */;/*超级块*/struct super_blockunsigned short s_inodes_count; /* 文件系统中inode的总数 */unsigned short s_blocks_count; /* 数据块总数 */unsigned short s_r_blocks_count; /* 保留块总数 */unsigned short s_free_blocks_count; / 空闲块总数 unsigned short s_free_inodes_count; /* 空闲的inode总数 */unsigned short s_log_block_size; /* block 的大小 */;/*账户信息*/struct userunsigned short user_id;/用户IDunsigned short user_access;/权限string username;/用户名string password;/密码;/*文件/目录结构*/struct directorystring name;/*目录名*/unsigned short d_ino;/*目录号*/;三、所用仪器、材料(设备名称、型号、规格等)。计算机一台四、实验方法、步骤#include #include#include#includestruct OpenFileTable /打开文件表数据结构long offset; / 当前文件读写指针char file_name10; / 文件名数组long int file_start; / 文件起始块号long int file_length; / 文件长度(字节);struct FCB_Block /FCB数据结构int flag; / 标志,-1表示未用,1表示文件用char file_name10; / 文件名数组long int file_date; / 文件建立日期 long int file_time; / 文件建立时间long int file_start; / 文件起始块号long int file_length; / 文件长度(字节);struct Super_Block / 超级块数据结构, 文件系统的分区信息,存放在0#物理块中 unsigned long int fs_totalsize; / 整个分区的总磁盘物理块数 unsigned long int fs_freesize; / 分区的所有空闲磁盘物理块数unsigned int fs_blocksize; / 文件系统的物理块大小(字节)unsigned int fs_fat_start; / FAT的起始磁盘物理块号 unsigned int fs_fat_size; / FAT占用的磁盘物理块数unsigned int fs_dir_start; / 根目录的起始磁盘物理块号unsigned int fs_dir_size; / 根目录占用的磁盘物理块数unsigned int fs_data_start; / 数据区起始磁盘物理块号unsigned long int fs_data_size; / 数据区的磁盘物理块数 ;const char DiskName=FileSys.dat; /磁盘文件名char rw_buffer512; / 读写使用的缓冲区struct FCB_Block filefcb130; / 读写目录使用的数据结构struct Super_Block FsSupBlk; / 读写超级块使用的数据结构long int fat_buffer5000; / 读写FAT使用的缓冲区,为简化在系统启动时全部装入内存,0为空闲struct OpenFileTable OFT16; / 打开文件表,当前只使用OFT0unsigned int block_size; / 物理块大小(字节)unsigned long int total_disk_size; / 磁盘总容量(物理块数)unsigned int total_dir_size; / 目录占有的物理块数unsigned int total_fat_size; / FAT占有的物理块数long int find_fcb; / 记录读FCB块的次数FILE *fsPtr; / 模拟磁盘的文件指针/* 磁盘块的申请*/ unsigned long int Get_Block(unsigned long int count) /分配count个物理快,返回首块指针,其它已经连接unsigned long int tmp,firstblk,tmpcount;unsigned long int i; int flag=1;if (count FsSupBlk.fs_freesize) printf( = 没有足够磁盘容量,不能分配!= n); return 0; tmpcount=0; for(i=FsSupBlk.fs_data_start;iFsSupBlk.fs_totalsize) printf( = 超出磁盘容量,不能读!= n); return; fseek(fsPtr,FsSupBlk.fs_blocksize*addr,SEEK_SET); fread(buf,512,1,fsPtr); return;/* 写磁盘块*/ void Write_Block(unsigned long int addr,char *buf) if (addrFsSupBlk.fs_totalsize) printf( = 超出磁盘容量,不能写!= n); return; fseek(fsPtr,FsSupBlk.fs_blocksize*addr,SEEK_SET); fwrite(buf,512,1,fsPtr); return;/* 格式化磁盘*/ void Real_Format() unsigned long int bcount; long int fatval,i;char *c;/更改系统超级块信息 FsSupBlk.fs_totalsize=total_disk_size; FsSupBlk.fs_blocksize=block_size; FsSupBlk.fs_dir_start=1; FsSupBlk.fs_dir_size=total_dir_size; FsSupBlk.fs_fat_start=total_dir_size+1; FsSupBlk.fs_fat_size=total_fat_size; FsSupBlk.fs_data_start=FsSupBlk.fs_fat_start+FsSupBlk.fs_fat_size; FsSupBlk.fs_data_size = FsSupBlk.fs_totalsize - FsSupBlk.fs_dir_size - FsSupBlk.fs_fat_size-1; FsSupBlk.fs_freesize= FsSupBlk.fs_data_size; /初始化目录 for(i=0;i128;i+) filefcbi.flag=-1; /为-1表示FCB未使用fseek(fsPtr,512L,SEEK_SET);fwrite(&filefcb0,sizeof(struct FCB_Block),128,fsPtr); /初始化FAT fatval=FsSupBlk.fs_fat_start*512; fseek(fsPtr,fatval,SEEK_SET); /定位文件指针 bcount=FsSupBlk.fs_fat_size+FsSupBlk.fs_dir_size+1; for(i=0;ibcount;i+) fat_bufferi=-1; /标记已经使用的磁盘数据块,即FAT区、目录区和启动区 for(;iFsSupBlk.fs_totalsize;i+) fat_bufferi=0; /为0表示为空的物理快 fwrite(&fat_buffer0,sizeof(long int),FsSupBlk.fs_totalsize,fsPtr); /初始化数据区 for(i=0;i512;i+) rw_bufferi= ;/缓冲区清空 for(i=FsSupBlk.fs_data_start;iFsSupBlk.fs_totalsize;i+) Write_Block(i,rw_buffer); /缓冲区写入第i块 /*新建系统磁盘文件*/ void Create_Disk() long int i;unsigned long int total;fsPtr=fopen(DiskName,wb+);if(fsPtr=NULL)printf( 不能建立磁盘所需的文件 !n);exit(0); / 建立磁盘文件 total=total_disk_size; for(i=0;itotal;i+) /建立大小为total的磁盘文件 fwrite(rw_buffer,512,1,fsPtr); fclose(fsPtr); fsPtr=fopen(DiskName,rb+); Real_Format(); return;/*读写系统超级块信息*/ void Read_Boot() /读取磁盘超级块数据信息 rewind(fsPtr);fread(&FsSupBlk,sizeof(struct Super_Block),1,fsPtr);return;void FileBoot() /超级块数据信息存盘 rewind(fsPtr);fwrite(&FsSupBlk,sizeof(struct Super_Block),1,fsPtr);return;/*FAT操作*/ void LoadFat() /装载全部FAT到内存 fseek(fsPtr,FsSupBlk.fs_fat_start*512,SEEK_SET);fread(fat_buffer,sizeof(long int),FsSupBlk.fs_totalsize ,fsPtr);return;void SaveFat() /FAT到文件FAT区 fseek(fsPtr,FsSupBlk.fs_fat_start*512,SEEK_SET);fwrite(fat_buffer,sizeof(long int),FsSupBlk.fs_totalsize,fsPtr);return;/*显示超级块信息*/ void boot_dis() printf(FsSupBlk.fs_totalsize=%ldn,FsSupBlk.fs_totalsize); printf(FsSupBlk.fs_blocksize=%dn,FsSupBlk.fs_blocksize); printf(FsSupBlk.fs_dir_start=%dn,FsSupBlk.fs_dir_start); printf(FsSupBlk.fs_dir_size=%dn,FsSupBlk.fs_dir_size); printf(FsSupBlk.fs_fat_start=%dn,FsSupBlk.fs_fat_start); printf(FsSupBlk.fs_fat_size=%dn,FsSupBlk.fs_fat_size); printf(FsSupBlk.fs_data_start=%dn,FsSupBlk.fs_data_start); printf(FsSupBlk.fs_data_size=%ldn,FsSupBlk.fs_data_size); printf(FsSupBlk.fs_freesize=%ldn,FsSupBlk.fs_freesize); /*系统初始化*/ void Sys_Init() /初始化fsPtr=fopen(DiskName,rb+);if(fsPtr = NULL) Create_Disk(); Read_Boot();/boot_dis(); LoadFat();return;/*显示操作*/ void dir() /显示目录下的文件 int i,countFile=0; char str16; long int n,pos_dir,pos_fat;coutendl; pos_dir=FsSupBlk.fs_dir_start*512; pos_fat=FsSupBlk.fs_fat_start*512;fseek(fsPtr,pos_dir,SEEK_SET);while(ftell(fsPtr)pos_fat) fread(&filefcb0,sizeof(struct FCB_Block),16 ,fsPtr); for(i=0;i16;i+) if(filefcbi.flag = 1) /文件占有 countFile+; n = filefcbi.file_length; printf( %-15s%15d bytesn, filefcbi.file_name,file,n); coutendl;printf( 总共有 %d 个文件n,countFile);printf( 系统总共有 %ld 个物理块可用nn,FsSupBlk.fs_freesize); /*查找文件*/ /查找文件,文件存在返回当前FCB数组下标,否则返回-1int Find_File(char *filename) int i;long int pos_dir,pos_fat; pos_dir=FsSupBlk.fs_dir_start*512; pos_fat=FsSupBlk.fs_fat_start*512; find_fcb=0;fseek(fsPtr,pos_dir,SEEK_SET);while(ftell(fsPtr)pos_fat) find_fcb+; fread(&filefcb0,sizeof(struct FCB_Block),16 ,fsPtr); for(i=0;i16;i+) if(filefcbi.flag!=-1) if(strcmp(filename,filefcbi.file_name) = 0) return i; /文件存在 return -1;/*创建文件*/ void create(char *fname,long int num) /在当前目录下创建一个名字为str的文件,长度为num int i,j; /true表示没有与该名字重名的文件 int tempnode; long int pos_dir,getnum=0;unsigned long int blkcount;blkcount= num/512+1; /计算需要的物理块 if(FsSupBlk.fs_freesize blkcount) /磁盘没有足够空间 printf(n 磁盘没有足够空间,不能建立 !nn); return;tempnode=Find_File(fname);if (tempnode!=-1) /表示文件存在 printf(n 文件已经存在,不需要建立 !nn); return;/ 建立文件的处理 pos_dir=FsSupBlk.fs_dir_start*FsSupBlk.fs_blocksize;fseek(fsPtr,pos_dir,SEEK_SET); /定位到目录区 for(i=0; iFsSupBlk.fs_dir_size; i+) /Read_Block(i+FsSupBlk.fs_dir_start,(char *)filefcb);fread(&filefcb0,sizeof(struct FCB_Block),16 ,fsPtr); for(j=0;j16;j+)if(filefcbj.flag = -1) /找到空目录项/ 分配空间, 标记FCB数据项,并将FCB写磁盘getnum=Get_Block(blkcount);if(getnum=-1) printf(不能分配存储空间 n);return;filefcbj.file_start=getnum;filefcbj.flag = 1;filefcbj.file_length=num;strcpy(filefcbj.file_name,fname);/filefcb.file_time=/filefcb.file_date=/ 改变磁盘FCB值 pos_dir=pos_dir+sizeof(struct FCB_Block)*(i*16+j);fseek(fsPtr,pos_dir,SEEK_SET); /定位到目录区的FCB项fwrite(&filefcbj,sizeof(struct FCB_Block),1 ,fsPtr); /Write_Block(i+FsSupBlk.fs_dir_start,(char *)filefcb); printf( 文件占用了 %d 个物理块n,blkcount); printf( 系统还有 %ld 个物理块可用nn,FsSupBlk.fs_freesize);return; /没有FCB项,不能建立文件 cout当前没有足够的目录区,不能建立文件 ! endl; return; /*格式化*/ void format()char ch; coutch; if(ch=y|ch=Y) Real_Format(); printf(n Format Successful!nn);/*删除文件操作*/ void del(char *fname) long int tempnode;long int pos_dir;tempnode=Find_File(fname);if (tempnode=-1) /表示文件不存在 printf(n 文件不存在,不能删除 !nn); return;/ 删除文件的处理 Put_Block(filefcbtempnode.file_start); / 释放文件存储空间 filefcbtempnode.flag =-1; / 标记FCB项可用 / 修改的FCB项写回磁盘pos_dir=FsSupBlk.fs_dir_start*FsSupBlk.fs_blocksize+(find_fcb-1)*16+tempnode)*sizeof(struct FCB_Block);fseek(fsPtr,pos_dir,SEEK_SET); /定位到目录区fwrite(&filefcbtempnode,sizeof(struct FCB_Block),1 ,fsPtr);return; /*写文件*/ void write(char *fname ,int num)/写文件,仅实现在文件尾部填加内容,并限制在512个字节内 int tempnode; int i,j,x;int rescount;long int pos_dir; if (num 512) printf(n 不能写大于512字节的数据 !nn); return; tempnode=Find_File(fname); if (tempnode=-1) /表示文件不存在 printf(n 文件不存在,不能写 !nn); return; / 写文件的处理char *string=new char num; /申请空间for(i=0;istringi; rescount=filefcbtempnode.file_length % FsSupBlk.fs_blocksize; if (num FsSupBlk.fs_blocksize-rescount)if (FsSupBlk.fs_freesize1) printf(n 文件系统没有足够空间,不能写 !nn);return;for(j=filefcbtempnode.file_start;fat_bufferj!=-1;j=fat_bufferj); Read_Block(j,rw_buffer); if (num= FsSupBlk.fs_blocksize-rescount)for(i=0;inum;i+) rw_bufferrescount+i=stringi;Write_Block(j,rw_buffer); else for(i=0;iFsSupBlk.fs_blocksize-rescount;i+) rw_bufferrescount+i=stringi; Write_Block(j,rw_buffer); fat_bufferj=Get_Block(1); j=fat_bufferj; for(x=0;xFsSupBlk.fs_blocksize;x+) rw_bufferx= ; for(x=0;xnum-(FsSupBlk.fs_blocksize-rescount);x+) rw_bufferx=stringi+x; Write_Block(j,rw_buffer); /delete string;/ 修改FCB项并写回磁盘 filefcbtempnode.file_length+=num; / 增加文件的长度pos_dir=FsSupBlk.fs_dir_start*FsSupBlk.fs_blocksize+(find_fcb-1)*16+tempnode)*sizeof(struct FCB_Block);fseek(fsPtr,pos_dir,SEEK_SET); /定位到目录区fwrite(&filefcbtempnode,sizeof(struct FCB_Block),1 ,fsPtr); cin.ignore(10000,n); /清除输入流缓冲区coutendl= 写文件完成!= filefcbtempnode.file_length) /读的数据超出文件范围 printf(n 超出文件范围,不能读 !nn); return; / 读文件的处理 printf(n=n); stblknum=start/FsSupBlk.fs_blocksize+1; / 起始物理块 offset=start%FsSupBlk.fs_blocksize; / 起始物理块的偏移量if(start+countfilefcbtempnode.file_length ) count= filefcbtempnode.file_length-start;for(i=filefcbtempnode.file_start,j=1;jfilefcbtempnode.file_length ) count= filefcbtempnode.file_length-start;if(count=FsSupBlk.fs_blocksize-offset) for(j=0;jcount;j+) coutrw_bufferj+offset;else for(j=0;jFsSupBlk.fs_blocksize-offset;j+) coutrw_bufferj+offset; dspnum=(count-(FsSupBlk.fs_blocksize-offset)/FsSupBlk.fs_blocksize+1; for(j=0;jdspnum-1;j+) i=fat_bufferi; Read_Block(i,rw_buffer); for(x=0;xFsSupBlk.fs_blocksize;x+) coutrw_bufferx; i=fat_bufferi; Read_Block(i,rw_buffer); x=(count-(FsSupBlk.fs_blocksize-offset)%FsSupBlk.fs_blocksize; for(j=0;jx;j+) coutrw_bufferx;coutendl= 读文件完成!=endl;return;/*显示帮助*/ void display_help()printf( 写文件 wf filename
温馨提示
- 1. 本站所有资源如无特殊说明,都需要本地电脑安装OFFICE2007和PDF阅读器。图纸软件为CAD,CAXA,PROE,UG,SolidWorks等.压缩文件请下载最新的WinRAR软件解压。
- 2. 本站的文档不包含任何第三方提供的附件图纸等,如果需要附件,请联系上传者。文件的所有权益归上传用户所有。
- 3. 本站RAR压缩包中若带图纸,网页内容里面会有图纸预览,若没有图纸预览就没有图纸。
- 4. 未经权益所有人同意不得将文件中的内容挪作商业或盈利用途。
- 5. 人人文库网仅提供信息存储空间,仅对用户上传内容的表现方式做保护处理,对用户上传分享的文档内容本身不做任何修改或编辑,并不能对任何下载内容负责。
- 6. 下载文件中如有侵权或不适当内容,请与我们联系,我们立即纠正。
- 7. 本站不保证下载资源的准确性、安全性和完整性, 同时也不承担用户因使用这些下载资源对自己和他人造成任何形式的伤害或损失。
最新文档
- 建立健全盐油糖采购制度
- 山西科技学院《人因工程学》2025-2026学年期末试卷
- 上海农林职业技术学院《中国古代文学史补充题》2025-2026学年期末试卷
- 上海震旦职业学院《劳动与社会保障法》2025-2026学年期末试卷
- 上海邦德职业技术学院《新石器时代》2025-2026学年期末试卷
- 山西铁道职业技术学院《市场调查理论与研究课程》2025-2026学年期末试卷
- 上海音乐学院《国际经济法》2025-2026学年期末试卷
- 上海闵行职业技术学院《临床诊断》2025-2026学年期末试卷
- 太原理工大学《企业管理与技术经济分析》2025-2026学年期末试卷
- 2026年部编版语文四年级下册期末测试题附答案(二)
- 2026贵州贵阳经济开发区招聘工作人员20名考试参考题库及答案解析
- 2026年山西运城农业职业技术学院单招综合素质考试题库含答案详细解析
- T-GFIA 006-2026 金毛狗种苗繁育及林下生态种植技术规范
- 第8课 北宋的政治 课件(27张内嵌视频)-七年级 历史下册(统编版)
- 2026年宁波卫生职业技术学院单招职业倾向性测试题库附答案详解(巩固)
- 窦性心律失常护理方案
- 湖北省武汉市2026届高三下学期三月调研考试语文试题(含答案)
- 三级 模块二 项目六 功能促进 任务三 指导或协助老年人使用安全防护性辅助器具
- 2026年上半年新疆维吾尔自治区招聘事业单位人员考试参考试题及答案解析
- 肿瘤姑息治疗2025年CSCO指南
- 门窗销售考核制度
评论
0/150
提交评论