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

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

操作系统实验报告

cout<<\}

/**访问目录**/ void visit(string str){ cout<<\}

/**显示文件属性**/ void attrib(string str){ cout<<\}

/**文件或者目录重命名**/ void rename(string str){ cout<<\}

/**此函数判断对要访问的文件夹是否有访问权限**/

bool access(struct inode *pinode)//调用的时候 要把当前文件的inode指针传送过来 这里0表示普通用户 1表示管理员 { if(pinode->di_userID==cur_user->user_id)return true;//该文件是当前用户拥有 if(cur_user->user_access==1)return true;//该用户为管理员 return false; }

/**此函数用于读取相应inode号下面的inode块**/ struct inode * getInode(unsigned int n){ inode *temp; temp=new inode; fseek(f_stream,DINODE_START+BLOCK_SIZE*n,SEEK_SET); fread(temp,sizeof(struct inode),1,f_stream); // cout<<\mode:\

/**此函数将指定编号下的数据块的目录或者文件读入cur_dir**/ bool getDataBlock(unsigned int n){ //directory *temp;

26

操作系统实验报告

//temp=new directory;

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

fread(cur_dir,sizeof(struct directory)*cur_inode->di_number,1,f_stream);

// cout<<\函数:\ return true; }

/**此函数用于判断当前文件夹有没有查找的文件 没有返回-1 有返回该文件的inod号**/ int FindFile(string filename) { int i; if(filename.length()>FILE_NAME_LENGTH) {

cout<<\您输入的文件名长度大于14,文件查找失败!\ return -1; }

if( filename==\ { cout<<\自身文件夹不允许操作!\ return -1; }

if(\ {

cout<<\父目录不允许操作!\ return -1; }

for(i=0;idi_number;i++) {

if(dir_buf[i].name==filename) {

cout<<\当前文件存在!\ return dir_buf[i].d_ino; } } cout<<\未找到此文件\ return -1;

27

操作系统实验报告

}

/**此函数由于读取一个文件,将文件内容输出**/ bool read(string filename) { struct inode *temp; if(FindFile(filename)==-1) { return false; } temp=getInode(FindFile(filename)); if(!access(temp)) {

}

cout<<\您没有此文件的访问权限\return false;

if(temp->di_mode!=1) {

cout<di_size/BLOCK_SIZE+1)); fseek(f_stream, DATA_START+BLOCK_SIZE*temp->di_block, SEEK_SET); fread (buf, BLOCK_SIZE*(temp->di_size/BLOCK_SIZE+1),1,f_stream); return true; }

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

if(str.length()>FILE_NAME_LENGTH) {

28

操作系统实验报告

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;

fflush(stdin);

for(j=0;j

for(m=0;m

if(bk_bitmap[m]==0) {

bk_bitmap[m]=1; break; } }

char *buf; int k=0,tag=0;

29

操作系统实验报告

buf = (char *)malloc(BLOCK_SIZE*sizeof(char));

cout<<\请输入文件的内容,以“###”结束!\ for(i=0;i

buf[i] = getchar(); if(tag==0) k=0; if(buf[i] == '#') { tag=1; k++; if(k == 3) break; } else { k=0; tag=0; } }

buf[i-2]='\\0';

cout<<\文件长度为:\

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

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

node_temp->di_tag=j;

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

node_temp->di_mode=1; /*存取模式: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=strlen(buf); /*文件大小,目录没有大小,值为0*/ node_temp->di_ctime; /* 创建时间 */

node_temp->di_mtime; /* 最后一次修改时间*/

30

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