1、实验5Linux文件管理实验5 Linux文件管理一、实验目的1. 掌握Linux文件系统的基本原理、结构和实现方法2. 掌握Linux文件系统中文件的建立、打开、读/写、执行、属性等系统调用的使用3. 学会设计简单的文件系统并实现一组操作4. 通过实验学习文件系统的系统调用命令,提高对文件系统实现功能的理解和掌握二、实验内容设计并实现一个一级文件系统程序,要求实现以下功能:1. 提供文件创建/删除接口命令create / delete、目录创建/删除接口命令mkdir / rmdir、显示目录内容命令ls等。2. 创建的文件不要求格式和内容。三、实验结果1. 程序源代码main.cpp#in
2、clude #include #include #include #include structure.h#include creat.h#include access.h#include ballfre.h#include close.h#include delete.h#include dir.h#include format.h#include halt.h#include iallfre.h#include install.h#include log.h#include name.h#include open.h#include rdwt.h#include igetput.hstru
3、ct hinode hinodeNHINO;struct dir dir;struct file sys_ofileSYSOPENFILE;struct filsys filsys;struct pwd pwdPWDNUM;struct user userUSERNUM;FILE *fd;struct inode *cur_path_inode;int user_id;/*kkkkk*/unsigned short usr_id;char usr_p12;char sel;char temp_dir12;main() unsigned short ab_fd1,ab_fd2,ab_fd3,ab
4、_fd4,i,j; char *buf; int done=1; printf(nDo you want to format the disk(y or n)?n); if(getchar()=y) printf(nFormat will erase all context on the disk n); printf(Formating.n); format(); printf(nNow will install the fillsystem,please wait.n); install(); printf(n-Login-nPlease input your userid:); scan
5、f(%u,&usr_id); printf(nPlease input your password:); scanf(%s,&usr_p); /* printf(nsuccessn);*/ if(!login(usr_id,usr_p) return; while(done) printf(n Please Select Your Operatingn); printf( -1-lsn -2-mkdirn -3-change dirn -4-create filen -0-Logoutn);/* */ sel=getchar(); sel=getchar(); switch(sel) case
6、 1: _dir(); break; case 2: printf(please input dir name:); scanf(%s,temp_dir); mkdir(temp_dir); break; case 3: printf(please input dir name:); scanf(%s,temp_dir); chdir(temp_dir); break; case 4: printf(please input file name:); scanf(%s,temp_dir); ab_fd1=creat(2118,temp_dir,01777); buf=(char *)mallo
7、c(BLOCKSIZ*6+5); write(ab_fd1,buf,BLOCKSIZ*6+5); close(0,ab_fd1); free(buf); break; case 0: logout(usr_id); halt(); done = 0; default: printf(error!nNo such command,please try again.nOr you can ask your teacher for help.n); break; else printf(User canselednGood Byen);structure.h#ifndef _STRUCTURE_H#
8、define _STRUCTURE_H#define MAX 32767#define BLOCKSIZ 512#define SYSOPENFILE 40#define DIRNUM 128#define DIRSIZ 14#define PWDSIZ 12#define PWDNUM 32#define NOFILE 20#define NADDR 10#define NHINO 128#define USERNUM 10#define DINODESIZ 72#define DINODEBLK 32#define FILEBLK 512#define NICFREE 50#define
9、NICINOD 50#define DINODESTART 1024#define DATASTART (2+DINODEBLK)*BLOCKSIZ#define DIEMPTY 00000#define DIFILE 01000#define DIDIR 02000#define UDIREAD 00001#define UDIWRITE 00002#define UDIEXICUTE 00004#define GDIREAD 00010#define GDIWRITE 00020#define GDIEXICUTE 00040#define ODIREAD 00100#define ODI
10、WRITE 00200#define ODIEXICUTE 00400#define READ 1#define WRITE 2#define EXICUTE 3#define DEFAULTMODE 00777#define IUPDATE 00002#define SUPDATE 00001#define FREAD 00001#define FWRITE 00002#define FAPPEND 00004#define DISKFULL 65535#define SEEK_SET 0struct inode struct inode *i_forw; struct inode *i_b
11、ack; char i_flag; unsigned int i_ino; unsigned int i_count; unsigned int di_addrNADDR; unsigned short di_number; unsigned short di_mode; unsigned short di_uid; unsigned short di_gid; unsigned short di_size;struct dinode unsigned short di_number; unsigned short di_mode; unsigned short di_uid; unsigne
12、d short di_gid; unsigned long di_size; unsigned int di_addrNADDR;struct direct char d_nameDIRSIZ; unsigned int d_ino;struct filsys unsigned short s_isize; unsigned long s_fsize; unsigned int s_nfree; unsigned short s_pfree; unsigned int s_freeNICFREE; unsigned int s_ninode; unsigned short s_pinode;
13、unsigned int s_inodeNICINOD; unsigned int s_rinode; char s_fmod;struct pwd unsigned short p_uid; unsigned short p_gid; char passwordPWDSIZ;struct dir struct direct directDIRNUM; int size;struct hinode struct inode *i_forw;struct file char f_flag; unsigned int f_count; struct inode *f_inode; unsigned
14、 long f_off;struct user unsigned short u_default_mode; unsigned short u_uid; unsigned short u_gid; unsigned short u_ofileNOFILE;extern struct inode* aaa;extern struct hinode hinodeNHINO;extern struct dir dir;extern struct file sys_ofileSYSOPENFILE;extern struct filsys filsys;extern struct pwd pwdPWD
15、NUM;extern struct user userUSERNUM;extern struct inode *cur_path_inode;extern FILE *fd;extern int user_id;extern struct inode *iget();extern iput();extern unsigned int balloc();extern bfree();extern struct inode *ialloc();extern ifree();extern int namei();extern short iname();extern unsigned int acc
16、ess();extern _dir();extern mkdir();extern chdir();extern unsigned short open();extern create();extern unsigned int read();extern unsigned int write();extern int login();extern logout();extern install();extern format();extern close();extern halt();#endifaccess.h#include #include structure.hunsigned i
17、nt access(user_id,inode,mode)unsigned int user_id;struct inode *inode;unsigned short mode; switch(mode) case READ: if(inode-di_mode&ODIREAD) return 1; if(inode-di_mode&GDIREAD)& (useruser_id.u_gid=inode-di_gid) return 1; if(inode-di_mode&UDIREAD)& (useruser_id.u_uid=inode-di_uid) return 1; return 0;
18、 case WRITE: if(inode-di_mode&ODIWRITE) return 1; if(inode-di_mode&GDIWRITE)& (useruser_id.u_gid=inode-di_gid) return 1; if(inode-di_mode&UDIWRITE)& (useruser_id.u_uid=inode-di_uid) return 1; return 0; case EXICUTE: if(inode-di_mode&ODIEXICUTE) return 1; if(inode-di_mode&GDIEXICUTE)& (useruser_id.u_
19、gid=inode-di_gid) return 1; if(inode-di_mode&UDIEXICUTE)& (useruser_id.u_uid=inode-di_uid) return 1; return 0; default: return 1; ballfre.h#include #include structure.hstatic unsigned int block_bufBLOCKSIZ/sizeof(int);unsigned int balloc() unsigned int free_block; int i,flag; if(filsys.s_nfree=0) pr
20、intf(nDisk Full!n); return DISKFULL; i=filsys.s_pfree; flag=(i=0); if(flag) /该BLOCK组全部用了 fseek(fd,DATASTART+BLOCKSIZ*(filsys.s_freeNICFREE-1+1),SEEK_SET); /filsys.s_freeNICFREE-1+1指向下一个block组的地址块 fread(block_buf,1,BLOCKSIZ,fd); for(i=0; iNICFREE; i+) filsys.s_freei=block_bufi; /将待用block组的地址读入超级块 fil
21、sys.s_pfree=NICFREE-1; free_block=filsys.s_freefilsys.s_pfree; else free_block=filsys.s_freefilsys.s_pfree; filsys.s_pfree-; filsys.s_nfree-; filsys.s_fmod=SUPDATE; return free_block;bfree (unsigned int block_num) int i; if(filsys.s_pfree=NICFREE-1) /表示回收的block已经可以组成一个block组了 for(i=0; iNICFREE; i+)
22、block_bufi=filsys.s_freeNICFREE-1-i; filsys.s_pfree=0; fseek(fd,DATASTART+BLOCKSIZ*(filsys.s_free0),SEEK_SET); /filsys.s_free0为当前BLOCK组的地址块 fwrite(block_buf,1,BLOCKSIZ,fd); else filsys.s_pfree+; filsys.s_nfree+; filsys.s_fmod=SUPDATE;close.h#include #include structure.hclose(user_id,cfd)unsigned int
23、 user_id;unsigned short cfd; struct inode *inode; inode=sys_ofileuseruser_id.u_ofilecfd.f_inode; iput(inode); sys_ofileuseruser_id.u_ofilecfd.f_count-; useruser_id.u_ofilecfd=SYSOPENFILE+1;creat.hcreat(uid,filename,mode)unsigned int uid;char *filename;unsigned short mode; int di_ith,di_ino; struct i
24、node *inode; int i,j; i=0; while(iUSERNUM)/user的值由函数login()注册,参看文件log.h if(useri.u_uid=uid) user_id=i; break; i+; if(i=USERNUM) printf(the user id is wrong.n); exit(1); di_ino=namei(filename); if(di_ino!=-1)/文件已经存在 inode=iget(di_ino); if(access(user_id,inode,mode)=0) iput(inode); printf(ncreat acces
25、s not allowedn); return 0; for(i=0; idi_size/BLOCKSIZ+1; i+) bfree(inode-di_addri); for(i=0; iSYSOPENFILE; i+) if(sys_ofilei.f_inode=inode) sys_ofilei.f_off=0; for(i=0; idi_uid; useruser_id.u_gid=inode-di_gid; for(j=0; ji_ino; inode-di_mode=useruser_id.u_default_mode; inode-di_uid=useruser_id.u_uid;
26、 inode-di_gid=useruser_id.u_gid; inode-di_size=0; inode-di_number=0; for(i=0; iSYSOPENFILE; i+) if(sys_ofilei.f_count=0) break; for(j=0; jNOFILE; i+) if(useruser_id.u_ofilej=SYSOPENFILE+1) break; useruser_id.u_ofilej=i; sys_ofilei.f_flag=(char)mode; sys_ofilei.f_count=0; sys_ofilei.f_off=0; sys_ofil
27、ei.f_inode=inode; return j; return 0;delete.h#include #include structure.hdelete(char *filename) unsigned int dinodeid; struct inode *inode; dinodeid=namei(filename); if(dinodeid!=(int)NULL) inode=iget(dinodeid); inode-di_number-; iput(inode);dir.h_dir() unsigned int di_mode; int i,j,one; struct inode *temp_inode; printf(CURRENT DIRECTORY:n); for(i=0;idir.size;i+) if(dir
copyright@ 2008-2022 冰豆网网站版权所有
经营许可证编号:鄂ICP备2022015515号-1