实验5Linux文件管理一、实验目的1.掌握Linux文件系统的基本原理、结构和实现方法2.掌握Linux文件系统中文件的建立、打开、读/写、执行、属性等系统调用的使用3.学会设计简单的文件系统并实现一组操作4.通过实验学习文件系统的系统调用命令,提高对文件系统实现功能的理解和掌握二、实验内容设计并实现一个一级文件系统程序,要求实现以下功能:1.提供文件创建/删除接口命令create/delete、目录创建/删除接口命令mkdir/rmdir、显示目录内容命令ls等。2.创建的文件不要求格式和内容。三、实验结果1.程序源代码main.cpp#includestdio.h#includemalloc.h#includestdlib.h#includestring.h#includestructure.h#includecreat.h#includeaccess.h#includeballfre.h#includeclose.h#includedelete.h#includedir.h#includeformat.h#includehalt.h#includeiallfre.h#includeinstall.h#includelog.h#includename.h#includeopen.h#includerdwt.h#includeigetput.hstructhinodehinode[NHINO];structdirdir;structfilesys_ofile[SYSOPENFILE];structfilsysfilsys;structpwdpwd[PWDNUM];structuseruser[USERNUM];FILE*fd;structinode*cur_path_inode;intuser_id;/*kkkkk*/unsignedshortusr_id;charusr_p[12];charsel;chartemp_dir[12];main(){unsignedshortab_fd1,ab_fd2,ab_fd3,ab_fd4,i,j;char*buf;intdone=1;printf(\nDoyouwanttoformatthedisk(yorn)?\n);if(getchar()=='y'){printf(\nFormatwilleraseallcontextonthedisk\n);printf(Formating...\n);format();printf(\nNowwillinstallthefillsystem,pleasewait...\n);install();printf(\n----Login----\nPleaseinputyouruserid:);scanf(%u,&usr_id);printf(\nPleaseinputyourpassword:);scanf(%s,&usr_p);/*printf(\nsuccess\n);*/if(!login(usr_id,usr_p))return;while(done){printf(\nPleaseSelectYourOperating\n);printf(-1----ls\n-2----mkdir\n-3----changedir\n-4----createfile\n-0----Logout\n);/*зЂвт*/sel=getchar();sel=getchar();switch(sel){case'1':_dir();break;case'2':printf(pleaseinputdirname:);scanf(%s,temp_dir);mkdir(temp_dir);break;case'3':printf(pleaseinputdirname:);scanf(%s,temp_dir);chdir(temp_dir);break;case'4':printf(pleaseinputfilename:);scanf(%s,temp_dir);ab_fd1=creat(2118,temp_dir,01777);buf=(char*)malloc(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!\nNosuchcommand,pleasetryagain.\nOryoucanaskyourteacherforhelp.\n);break;}}}elseprintf(Usercanseled\nGoodBye\n);}structure.h#ifndef_STRUCTURE_H#define_STRUCTURE_H#defineMAX32767#defineBLOCKSIZ512#defineSYSOPENFILE40#defineDIRNUM128#defineDIRSIZ14#definePWDSIZ12#definePWDNUM32#defineNOFILE20#defineNADDR10#defineNHINO128#defineUSERNUM10#defineDINODESIZ72#defineDINODEBLK32#defineFILEBLK512#defineNICFREE50#defineNICINOD50#defineDINODESTART1024#defineDATASTART(2+DINODEBLK)*BLOCKSIZ#defineDIEMPTY00000#defineDIFILE01000#defineDIDIR02000#defineUDIREAD00001#defineUDIWRITE00002#defineUDIEXICUTE00004#defineGDIREAD00010#defineGDIWRITE00020#defineGDIEXICUTE00040#defineODIREAD00100#defineODIWRITE00200#defineODIEXICUTE00400#defineREAD1#defineWRITE2#defineEXICUTE3#defineDEFAULTMODE00777#defineIUPDATE00002#defineSUPDATE00001#defineFREAD00001#defineFWRITE00002#defineFAPPEND00004#defineDISKFULL65535#defineSEEK_SET0structinode{structinode*i_forw;structinode*i_back;chari_flag;unsignedinti_ino;unsignedinti_count;unsignedintdi_addr[NADDR];unsignedshortdi_number;unsignedshortdi_mode;unsignedshortdi_uid;unsignedshortdi_gid;unsignedshortdi_size;};structdinode{unsignedshortdi_number;unsignedshortdi_mode;unsignedshortdi_uid;unsignedshortdi_gid;unsignedlongdi_size;unsignedintdi_addr[NADDR];};structdirect{chard_name[DIRSIZ];unsignedintd_ino;};structfilsys{unsignedshorts_isize;unsignedlongs_fsize;unsignedints_nfree;unsignedshorts_pfree;unsignedints_free[NICFREE];unsignedints_ninode;unsignedshorts_pinode;unsignedints_inode[NICINOD];unsignedints_rinode;chars_fmod;};structpwd{unsignedshortp_uid;unsignedshortp_gid;charpassword[PWDSIZ];};structdir{structdirectdirect[DIRNUM];intsize;};structhinode{structinode*i_forw;};structfile{charf_flag;unsignedintf_count;structinode*f_inode;unsignedlongf_off;};structuser{unsignedshortu_default_mode;unsignedshortu_uid;unsignedshortu_gid;unsignedshortu_ofile[NOFILE];};externstructinode*aaa;externstructhinodehinode[NHINO];externstructdirdir;externstructfilesys_ofile[SYSOPENFILE];externstructfilsysfilsys;externstructpwdpwd[PWDNUM];externstructuseruser[USERNUM];externstructinode*cur_path_inode;externFILE*fd;externintuser_id;externstructinode*iget();externiput();externunsignedintballoc();externbfree();externstructinode*ialloc();externifree();externintnamei();externshortiname();externunsignedintaccess();extern_dir();externmkdir();externchdir();externunsignedshortopen();externcreate();externunsignedintread();externunsignedintwrite();externintlogin();externlogout();externinstall();externformat();externclose();externhalt();#endifaccess.h#includestdio.h#includestructure.hunsignedintaccess(user_id,inode,mode)unsignedintuser_id;structinode*inode;unsignedshortmode;{switch(mode){caseREAD:if(inode-di_mode&ODIREAD)return1;if((inode-di_mode&GDIREAD)&&(user[user_id].u_gid==inode-di_gid))return1;if((inode-di_mode&UDIREAD)&&(user[user_id].u_uid==inode-di_uid))return1;return0;caseWRITE:if(inode-di_mode&ODIWRITE)return1;if((inode-di_mode&GDIWRITE)&&(user[user_id].u_gid==inode-di_gid))return1;if((inode-di_mode&UDIWRITE)&&(user[user_id].u_uid==inode-di_uid))return1;re