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

操作系统实验报告

node_temp->di_block=m; /* 数据块块地址编号 */

fseek(f_stream,DINODE_START+j*BLOCK_SIZE,SEEK_SET);

fwrite(node_temp,BLOCK_SIZE,1,f_stream); //将新建文件inode写入磁盘

fseek(f_stream,DATA_START+m*BLOCK_SIZE,SEEK_SET);

fwrite(buf,BLOCK_SIZE,1,f_stream);//将新建文件的内容写入磁盘

dir_buf[cur_inode->di_number].d_ino=j;//修改当前目录的结构 dir_buf[cur_inode->di_number].name=str;

fseek(f_stream,DATA_START+BLOCK_SIZE*cur_inode->di_block,SEEK_SET);//将当前目录信息写入文件的block区

fwrite(dir_buf,BLOCK_SIZE,1,f_stream);

cur_inode->di_number++;//当前目录关联文件数++

superBlock.s_free_inodes_count--;//超级块中空闲的inode数目减一 superBlock.s_free_blocks_count--; //超级块中空闲的block数目减一 fseek(f_stream,BLOCK_SIZE,SEEK_SET);//修改超级块 fwrite(&superBlock,BLOCK_SIZE,1,f_stream);

fseek(f_stream,2*BLOCK_SIZE,SEEK_SET);// fwrite(di_bitmap,BLOCK_SIZE,1,f_stream); fwrite(bk_bitmap,BLOCK_SIZE,1,f_stream); cout<<\文件已成功添加\ free(buf);

return; }

void Delete(string str) { int i,j,m; int numb; //numb用来存储需要删除的文件inode号 char C;

if(str.length ()>FILE_NAME_LENGTH)//判断输入的文件名是否超过长度 {

31

操作系统实验报告

cout<<\您输入的文件名长度大于20,操作失败!\ return ; }

numb=FindFile(str);//判断是否存在该文件或文件夹 if(numb==-1) { cout<<\该文件或文件夹不存在!\ return; }

if(!access(getInode(numb)))//判断是否有权限删除文件 {

cout<<\您没有权限删除该文件或文件夹!\ return; }

struct inode *node_temp;//node_temp指向需要删除文件inode结构体 node_temp=getInode(numb);

if(node_temp->di_mode==1)//如果是文件的话就直接删除 {

di_bitmap[numb]=0;//将文件inode释放,即将相应位示图位置置为0

bk_bitmap[node_temp->di_block]=0;//将文件block释放 ,即将相应位示图位置置为0

cur_inode->di_number--;//当前文件夹关联文件减一

for(i=0;idi_number;i++)//改变当前文件夹的关联文件 { if(dir_buf[i].name==str) break; }

for(j=i;jdi_number-1;j++) {

dir_buf[j]=dir_buf[j+1]; }

fseek(f_stream,DATA_START+BLOCK_SIZE*cur_inode->di_block,SEEK_SET);//将当前目录信息写入文件的block区

fwrite(dir_buf,BLOCK_SIZE,1,f_stream);

fseek(f_stream,2*BLOCK_SIZE,SEEK_SET);//修改位示图 fwrite(di_bitmap,BLOCK_SIZE,1,f_stream);

32

操作系统实验报告

fseek(f_stream,3*BLOCK_SIZE,SEEK_SET); fwrite(bk_bitmap,BLOCK_SIZE,1,f_stream);

superBlock.s_free_inodes_count--;//超级块中空闲的inode数目加一 superBlock.s_free_blocks_count--; //超级块中空闲的block数目加一 fseek(f_stream,BLOCK_SIZE,SEEK_SET);//修改超级块 fwrite(&superBlock,BLOCK_SIZE,1,f_stream); }

else if(node_temp->di_mode==0)//是文件夹的话则判断该文件夹中是否有文件 {

if(node_temp->di_number>2)//如果有文件则不能删除 {

cout<<\当前文件夹内包含文件,不允许被删除!\ return; }

cout<<\是否真的要删除该文件夹? (Y/N)\若没有文件则判断是否要删除该文件夹

cin>>C;

if(C=='N'||C=='n') return;

else if(C=='Y'||C=='y') {

di_bitmap[numb]=0;

bk_bitmap[node_temp->di_block]=0; cur_inode->di_number--; for(i=0;idi_number;i++) { if(dir_buf[i].name==str) break; }

for(j=i;jdi_number-1;j++) {

dir_buf[j]=dir_buf[j+1]; }

fseek(f_stream,DATA_START+BLOCK_SIZE*cur_inode->di_block,SEEK_SET);//将当前目录信息写入文件的block区

fwrite(dir_buf,BLOCK_SIZE,1,f_stream);

33

操作系统实验报告

fseek(f_stream,2*BLOCK_SIZE,SEEK_SET);//修改位示图 fwrite(di_bitmap,BLOCK_SIZE,1,f_stream); fwrite(bk_bitmap,BLOCK_SIZE,1,f_stream);

superBlock.s_free_inodes_count--;//超级块中空闲的inode数目加一 superBlock.s_free_blocks_count--; //超级块中空闲的block数目加一 fseek(f_stream,BLOCK_SIZE,SEEK_SET);//修改超级块 fwrite(&superBlock,BLOCK_SIZE,1,f_stream);

return; } } }

void createDir(string str) { int i,j,m,file_access;

if(str.length()>FILE_NAME_LENGTH) {

cout<<\您输入的文件夹名长度大于20,文件夹创建失败!\ return ; }

if(FindFile(str)!=-1) { cout<<\您输入的文件夹名已存在,文件夹名不能重复,创建失败!\ return; }

if(superBlock.s_free_inodes_count==0||superBlock.s_free_blocks_count==0) {

cout<<\当前存储空间已满!请删除一些文件后再存储。\ return ; }

cout<<\请输入文件夹的访问权限。1表示共享文件夹,0表示私有文件夹: \ cin>>file_access;

34

操作系统实验报告

fflush(stdin);

for(j=0;j

for(m=0;m

if(bk_bitmap[m]==0) {

bk_bitmap[m]=1; break; } }

struct directory buf_dir[DIRECTORY_NUM];

buf_dir[0].name=\ buf_dir[0].d_ino=0; buf_dir[1].name=\ buf_dir[1].d_ino=0;

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

cout<<\内存分配失败!文件创建不成功!\ return; }

node_temp->di_tag=j;

node_temp->di_number=2; /*关联文件数,当为0时表示删除文件,如一个目录至少包含两个文件:\和\

node_temp->di_mode=0; /*存取模式:0为目录,1为文件*/

node_temp->di_userID=cur_user->user_id; /*当前inode所属用户 0为根目录ID,一次下去是管理员目录、用户目录*/

node_temp->di_access=file_access; /*访问权限 0为不允许普通用户访问(公共目录),1为允许普通用户访问*/

node_temp->di_size=0; /*文件大小,目录没有大小,值为0*/

35

操作系统实验报告

node_temp->di_ctime; /* 创建时间 */

node_temp->di_mtime; /* 最后一次修改时间*/ node_temp->di_block=m; /* 数据块块地址编号 */

fseek(f_stream,DINODE_START+j*BLOCK_SIZE,SEEK_SET);

fwrite(node_temp,BLOCK_SIZE,1,f_stream); //将新建文件inode写入磁盘

dir_buf[cur_inode->di_number].d_ino=j;//修改当前目录的结构 dir_buf[cur_inode->di_number].name=str;

fseek(f_stream,DATA_START+BLOCK_SIZE*cur_inode->di_block,SEEK_SET);//将当前目录信息写入文件的block区

fwrite(dir_buf,BLOCK_SIZE,1,f_stream);

fseek(f_stream,DATA_START+m*BLOCK_SIZE,SEEK_SET);//将创建的文件夹的关联文件写入磁盘

fwrite(buf_dir,BLOCK_SIZE,1,f_stream);

cur_inode->di_number++;//当前目录关联文件数加一

superBlock.s_free_inodes_count--;//超级块中空闲的inode数目减一 superBlock.s_free_blocks_count--; //超级块中空闲的block数目减一 fseek(f_stream,BLOCK_SIZE,SEEK_SET);//修改超级块 fwrite(&superBlock,BLOCK_SIZE,1,f_stream);

fseek(f_stream,2*BLOCK_SIZE,SEEK_SET);//将修改后的inode位示图和block位示图写回磁盘

fwrite(di_bitmap,BLOCK_SIZE,1,f_stream); fwrite(bk_bitmap,BLOCK_SIZE,1,f_stream); cout<<\文件夹已成功添加\

return; }

36

联系客服:779662525#qq.com(#替换为@) 苏ICP备20003344号-4