文件管理文档格式.docx
《文件管理文档格式.docx》由会员分享,可在线阅读,更多相关《文件管理文档格式.docx(46页珍藏版)》请在冰豆网上搜索。
(6)删除目录。
RD<
删除当前目录下的指定目录,如果该目录为空,则可删除,否则应提示是否作删除,删除则将该目录下的全部文件和子目录都删除。
程序源代码如下:
#include<
iostream>
string>
stdio.h>
usingnamespacestd;
/*---------常变量------*/
constunsignedintBLOCK_SIZE=512;
//块长
constunsignedintDATA_BLOCK_NUM=512;
//数据块数量
constunsignedintDINODE_START=4*BLOCK_SIZE;
//inode起始位置
constunsignedintDINODE_SIZE=512;
//inode大小
constunsignedintDINODE_NUM=512;
//inode数量
constunsignedintDATA_START=(2+DINODE_NUM)*BLOCK_SIZE;
//数据区的开始地址
constunsignedintACCOUNT_NUM=10;
//用户数量
constunsignedintDIRECTORY_NUM=12;
//每个目录最多允许拥有的子目录和文件
constunsignedshortFILE_NAME_LENGTH=20;
//最大文件名长度
/**************************
inode结构体
**************************/
/*inode结构体*/
structinode{
unsignedshortdi_tag;
/*inode标识*/
unsignedshortdi_number;
/*关联文件数,当为0时表示删除文件,如一个目录至少包含两个文件:
"
."
和"
.."
*/
unsignedshortdi_mode;
/*存取模式:
0为目录,1为文件*/
unsignedshortdi_userID;
/*当前inode所属用户0为根目录ID,一次下去是管理员目录、用户目录*/
unsignedshortdi_access;
/*访问权限0为不允许普通用户访问(公共目录),1为允许普通用户访问*/
unsignedshortdi_size;
/*文件大小,目录没有大小,值为0*/
unsignedshortdi_ctime;
/*创建时间*/
unsignedshortdi_mtime;
/*最后一次修改时间*/
unsignedshortdi_block;
/*数据块块地址编号*/
};
/*————————————————————————
超级块结构
————————————————————————*/
structsuper_block{
unsignedshorts_inodes_count;
/*文件系统中inode的总数*/
unsignedshorts_free_inodes_count;
/*空闲的inode总数*/
unsignedshorts_blocks_count;
/*块总数*/
unsignedshorts_r_blocks_count;
/*保留块总数*/
unsignedshorts_free_blocks_count;
//空闲块总数
unsignedshorts_log_block_size;
/*block的大小*/
//unsignedshorts_free_blocks_group[GROUPNUM];
//新增一个数组来记录每个数据块组中的空闲数据块计数
//unsignedshorts_first_data_block;
/*第一个数据block*/
//unsignedshorts_blocks_per_group;
/*每blockgroup的block数量*/
//unsignedshorts_inodes_per_group;
/*每blockgroup的inode数量*/
账户信息
structuser{
unsignedshortuser_id;
//用户ID
unsignedshortuser_access;
//权限1为管理员0为平民
stringusername;
//用户名
stringpassword;
//密码
文件、目录项结构
structdirectory{
stringname;
/*目录、文件名*/
stringcontent;
/*文件数据,目录则没有数据*/
unsignedshortd_ino;
/*目录、文件号*/
/*----变量--------*/
unsignedshortdi_bitmap[DINODE_NUM];
//硬盘inode节点位图1表示已使用0表示未使用
unsignedshortbk_bitmap[DATA_BLOCK_NUM];
//数据块block位图
structsuper_blocksuperBlock;
//超级块
structuseraccount[ACCOUNT_NUM];
//共创建ACCOUNT_NUM个账户
FILE*f_stream;
//文件指针
structinode*cur_inode;
//inode当前目录指针
structinode*inode_temp;
//inode临时指针
constchardiskName[30]="
ext2forlinux.disk"
;
//模拟硬盘的文件名
structdirectorydir_buf[DIRECTORY_NUM];
//目录数组,每个目录最多允许有12个项(子目录或者文件)
stringcur_Path;
//cmd的头表示所在哪个文件夹
//inti_lock=0;
//inode位图锁可能会多线程
//intb_lock=0;
//block位图锁
structuser*cur_user;
//当前用户
structdirectorycur_dir[DIRECTORY_NUM];
//当前目录
/**********************
函数声明
********************/
boolFormat(void);
//此函数用于格式化
boolinstall(void);
//此函数用于装载虚拟硬盘的数据
intFindFile(stringfilename);
//次函数用于查找当前文件夹是否有该文件
boolaccess(structinode*pinode);
//权限判断
intmain(){
voidlogin(void);
//此函数用于用户登陆
voidshowMenu(void);
//此函数用于显示功能菜单
boolonAction(void);
//此函数用于用户选择功能并执行
charformat_bool;
/**************初始化************/
cout<
<
系统已启动,是否初始化所有数据?
\tY/N"
endl;
while(true){
cin>
>
format_bool;
if(format_bool=='
y'
||format_bool=='
Y'
){
if(!
Format())return0;
break;
}
elseif(format_bool=='
n'
N'
cout<
不初始化不能开始!
continue;
else{
请输入Y或者N"
}
}
初始化成功!
欢迎使用本程序,学号:
20134012004姓名:
李辉文"
/********转载虚拟硬盘数据***********/
if(!
install()){
cout<
加载失败,无效的硬盘格式"
//main();
return0;
elsecout<
加载disk成功!
cur_Path=cur_dir[1].name+"
\\root"
/*******登陆**********/
login();
/**显示菜单**/
showMenu();
/**显示当前路径**/
cur_Path;
while(onAction());
return0;
}
/*此函数用于格式化*/
boolFormat(){
//创建文件
f_stream=fopen(diskName,"
wb+"
);
if(f_stream==NULL){
创建文件失败"
returnfalse;
//初始化超级块
superBlock.s_inodes_count=DINODE_NUM;
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;
//超级块放第1个物理块,第0个为引导
fseek(f_stream,BLOCK_SIZE,SEEK_SET);
fwrite(&
superBlock,BLOCK_SIZE,1,f_stream);
//fprintf(f_stream,"
thisissuperblock"
//测试
//初始化dinode位图block位图
for(inti=0;
i<
DATA_BLOCK_NUM;
i++){
if(i<
2+ACCOUNT_NUM)bk_bitmap[i]=1;
elsebk_bitmap[i]=0;
//初始化block位图
for(i=0;
DINODE_NUM;
2+ACCOUNT_NUM)di_bitmap[i]=1;
elsedi_bitmap[i]=0;
//位示图存放与第2.3块
fseek(f_stream,BLOCK_SIZE*2,SEEK_SET);
di_bitmap,BLOCK_SIZE,1,f_stream);
fseek(f_stream,BLOCK_SIZE*3,SEEK_SET);
bk_bitmap,BLOCK_SIZE,1,f_stream);
//初始化inode表
structinode*node_temp;
node_temp=newinode;
node_temp)
{
printf("
node_temp内存分配失败!
//主目录的inode
node_temp->
di_tag=0;
//i节点标志
di_number=2+ACCOUNT_NUM;
//关联12个文件夹
di_mode=0;
//0为目录
di_userID=0;
//用户id第一个用户
di_access=0;
//0为公共可以访问
di_size=0;
//目录无size
di_ctime=0;
di_mtime=0;
/*修改时间*/
di_block=0;
fseek(f_stream,DINODE_START,SEEK_SET);
fwrite(node_temp,sizeof(structinode),1,f_stream);
//账户信息的inode
di_tag=1;
di_number=0;
//无文件关联,此项等于0表明是账户信息,可用作区别于文件、目录的标识
//无size
di_block=1;
/**账户信息存在数据块的第1块**/
fseek(f_stream,DINODE_START+BLOCK_SIZE,SEEK_SET);
//管理员和9个普通用户的inode
for(i=1;
=ACCOUNT_NUM;
di_tag=i+1;
//inode标志
di_number=2;
//关联2个文件夹
di_access=1;
//1为用户私有
di_userID=i;
di_block=i+1;
//所占物理块号.从管理员到user9占用的位置为2~11.
fseek(f_stream,DINODE_START+BLOCK_SIZE*(i+1),SEEK_SET);
/*******初始化主目录**********/
dir_buf[0].name="
dir_buf[0].d_ino=0;
dir_buf[1].name="
dir_buf[1].d_ino=0;
dir_buf[2].name="
admin"
dir_buf[2].d_ino=2;
//管理员的inode号为2
//user1~user9的目录项
ACCOUNT_NUM-1;
dir_buf[i+3].name="
user"
+i;
dir_buf[i+3].d_ino=i+3;
fseek(f_stream,DATA_START,SEEK_SET);
fwrite(dir_buf,BLOCK_SIZE,1,f_stream);
/**初始化账户信息**/
structuseraccount_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
ACCOUNT_NUM;
account_temp[i].username="
account_temp[i].password="
account_temp[i].user_access=0;
account_temp[i].user_id=i+2;
//user[i]的物理块号
fseek(f_stream,DATA_START+BLOCK_SIZE,SEEK_SET);
fwrite(account_temp,BLOCK_SIZE,1,f_stream);
/**初始化admin目录以及user1~user9**/
//清空dir_buf
DIRECTORY_NUM;
dir_buf[i].d_ino=0;
dir_buf[i].name="
\0"
dir_buf[0].d_ino=2;
dir_buf[0].name="
dir_buf[1].d_ino=0;
dir_buf[1].name="
fseek(f_stream,DATA_START+BLOCK_SIZE*(i+2),SEEK_SET);
//管理员在第二块物理空间
returntrue;
/*此函数用于装载虚拟硬盘的数据*/
boolinstall(void){
/**函数声明**/
inode*getInode(unsignedint);
//获取指定inode号的inode单元
boolgetDataBlock(unsignedint);
//获取指定数据块号下的目录或文件
inti;
installing..."
f_stream=fopen(diskName,"
rb+"
//只读方式打开硬盘模拟文件
if(f_stream==NULL)
文件打开失败"
//读超级块
fread(&
superBlock,sizeof(structsuper_block),1,f_stream);
//////
加载超级块:
superBlock.s_blocks_count<
inode_temp=newinode;
inode_temp)
cur_inode内存分配失败!
cur_inode=newinode;
cur_inode)
//读取inode位示图
fread(di_bitmap,BLOCK_SIZE,1,f_stream);
加载inode位示图:
di_bitmap[0]<
//读取block位示图
fread(bk_bitmap,BLOCK_SIZE,1,f_stream);
加载block位示图:
bk_bitmap[0]<
//读取账户信息的inode
inode_temp=getInode
(1);
if(inode_temp==NULL)
加载账户信息失败!
//changeinode();
//交换指针后cur_inode指向当前目录的inode
//读取账户信息
账户信息块号:
inode_temp->
di_block<
//fseek(f_stream,DATA_START+BLOCK_SIZE*inode_temp->
di_block,SEEK_SET);
fread(account,BLOCK_SIZE,1,f_stream);
//cout<
加载账户信息:
account[0].username<
'
\t'
acc