操作系统实验报告 文件管理系统 源程序 下载本文

内容发布更新时间 : 2024/5/6 19:46:57星期一 下面是文章的全部内容请认真阅读。

操作系统实验报告

};

/*———————————————————————— 账户信息

————————————————————————*/ struct user{

unsigned short user_id; //用户ID unsigned short user_access; //权限 1为管理员 0为平民 string username; //用户名 string password; //密码 };

/*———————————————————————— 文件、目录项结构

————————————————————————*/ struct directory{

string name; /*目录、文件名*/ string content;/*文件数据, 目录则没有数据*/ unsigned short d_ino; /*目录、文件号*/ };

/*----变量--------*/

unsigned short di_bitmap[DINODE_NUM]; // 硬盘inode节点位图 1表示已使用 0表示未使用

unsigned short bk_bitmap[DATA_BLOCK_NUM]; // 数据块block位图 struct super_block superBlock; //超级块 struct user account[ACCOUNT_NUM]; //共创建ACCOUNT_NUM个账户 FILE *f_stream; //文件指针 struct inode *cur_inode; //inode当前目录指针 struct inode *inode_temp; //inode临时指针 const char diskName[30]=\//模拟硬盘的文件名

struct directory dir_buf[DIRECTORY_NUM]; //目录数组,每个目录最多允许有12个项(子目录或者文件)

string cur_Path;//cmd 的头 表示所在哪个文件夹 //int i_lock=0;//inode位图锁 可能会多线程 //int b_lock=0;//block位图锁 struct user *cur_user; //当前用户

struct directory cur_dir[DIRECTORY_NUM];// 当前目录

/********************** 函数声明

********************/

bool Format(void); //此函数用于格式化

bool install(void); //此函数用于装载虚拟硬盘的数据

int FindFile(string filename);//次函数用于查找当前文件夹是否有该文件 bool access(struct inode *pinode);//权限判断

16

操作系统实验报告

int main(){ void login(void); //此函数用于用户登陆 void showMenu(void);//此函数用于显示功能菜单 bool onAction(void);//此函数用于用户选择功能并执行 char format_bool;

/**************初始化************/

cout<<\系统已启动,是否初始化所有数据?\\tY/N\while(true){ cin>>format_bool; if(format_bool=='y'||format_bool=='Y'){ if(!Format()) return 0; break; } else if(format_bool=='n'||format_bool=='N'){ }

cout<<\不初始化不能开始!\ continue; } else{ cout<<\请输入Y或者N\ continue; }

cout<<\初始化成功!\

/********转载虚拟硬盘数据***********/ if(!install()) { cout<<\加载失败,无效的硬盘格式\ //main(); return 0; }

else cout<<\加载disk成功!\cur_Path=cur_dir[1].name+\/*******登陆**********/ login();

/**显示菜单**/ showMenu();

/**显示当前路径**/ cout<

17

操作系统实验报告

}

/*此函数用于格式化*/ bool Format(){

//创建文件

f_stream=fopen(diskName,\if(f_stream==NULL){ }

cout<<\创建文件失败\return false;

//初始化超级块 superBlock.s_inodes_count=DINODE_NUM; /* 文件系统中inode的总数 */ superBlock.s_free_inodes_count=DATA_BLOCK_NUM-2-ACCOUNT_NUM; /* 空闲的inode总数 初始化时,主目录和账户信息各占一块,10个用户10块*/ superBlock.s_blocks_count=DATA_BLOCK_NUM; /* 块总数 */ superBlock.s_free_blocks_count=DATA_BLOCK_NUM-2-ACCOUNT_NUM; // 空闲块总数 主目录/10个用户/账户信息共占用12个 superBlock.s_log_block_size=BLOCK_SIZE; /* block 的大小 */ //超级块放第1个物理块,第0个为引导 fseek(f_stream,BLOCK_SIZE,SEEK_SET); fwrite(&superBlock,BLOCK_SIZE,1,f_stream); // // //

fprintf(f_stream,\测试 //初始化dinode位图 block位图 for(int i=0;i

//初始化block位图

for(int i=0;i

//位示图存放与第2.3块

fseek(f_stream,BLOCK_SIZE*2,SEEK_SET); fwrite(&di_bitmap,BLOCK_SIZE,1,f_stream); fprintf(f_stream,\测试 fseek(f_stream,BLOCK_SIZE*3,SEEK_SET); fwrite(&bk_bitmap,BLOCK_SIZE,1,f_stream); fprintf(f_stream,\测试 //初始化inode表

18

操作系统实验报告

struct inode *node_temp; node_temp=new inode; if(!node_temp) {

printf(\内存分配失败!\ return false; }

//主目录的inode

node_temp->di_tag=0;//i节点标志

node_temp->di_number=2+ACCOUNT_NUM;//关联12个文件夹 node_temp->di_mode=0;//0为目录

node_temp->di_userID=0;//用户id 第一个用户 node_temp->di_access=0;//0为公共可以访问 node_temp->di_size=0;//目录无size

node_temp->di_ctime=0; /* 创建时间 */ node_temp->di_mtime=0; /* 修改时间 */ node_temp->di_block=0;

fseek(f_stream,DINODE_START,SEEK_SET);

fwrite(node_temp,sizeof(struct inode), 1,f_stream);

//账户信息的inode node_temp->di_tag=1;

node_temp->di_number=0;//无文件关联,此项等于0表明是账户信息,可用作区别于文件、目录的标识 node_temp->di_mode=0;//0为目录 node_temp->di_userID=0;//用户id 第一个用户 node_temp->di_access=0;//0为公共可以访问 node_temp->di_size=0;//无size node_temp->di_ctime=0; /* 创建时间 */ node_temp->di_mtime=0; /* 修改时间 */ node_temp->di_block=1; /**账户信息存在数据块的第1块**/ fseek(f_stream,DINODE_START+BLOCK_SIZE,SEEK_SET); fwrite(node_temp,sizeof(struct inode), 1,f_stream); //管理员和9个普通用户的inode for(int i=1;i<=ACCOUNT_NUM;i++){ node_temp->di_tag=i+1;//inode标志 node_temp->di_number=2;//关联2个文件夹 node_temp->di_access=1;//1为用户私有 node_temp->di_mode=0;//0为目录 node_temp->di_userID=i;//用户id 第一个用户 node_temp->di_size=0;//目录无size

19

操作系统实验报告

node_temp->di_ctime=0; /* 创建时间 */ node_temp->di_mtime=0; /* 修改时间 */

node_temp->di_block=i+1;//所占物理块号.从管理员到user9占用的位置为2~11. fseek(f_stream,DINODE_START+BLOCK_SIZE*(i+1),SEEK_SET); fwrite(node_temp,sizeof(struct inode), 1,f_stream); }

/*******初始化主目录**********/ dir_buf[0].name=\dir_buf[0].d_ino=0; dir_buf[1].name=\dir_buf[1].d_ino=0;

dir_buf[2].name=\

dir_buf[2].d_ino=2;//管理员的inode号为2 //user1~user9的目录项

for(int i=0;i

fseek(f_stream,DATA_START,SEEK_SET); fwrite(dir_buf,BLOCK_SIZE,1,f_stream); /**初始化账户信息**/

struct user account_temp[ACCOUNT_NUM]; //管理员账户

account_temp[0].username=\account_temp[0].password=\

account_temp[0].user_access=1;//1为管理员 account_temp[0].user_id=2; //管理员目录的块号 //user1~user9

for(int i=1;i

fseek(f_stream,DATA_START+BLOCK_SIZE,SEEK_SET); fwrite(account_temp,BLOCK_SIZE,1,f_stream); /**初始化admin目录以及user1~user9**/

//清空dir_buf

for(int i=0;i

20