版權(quán)說明:本文檔由用戶提供并上傳,收益歸屬內(nèi)容提供方,若內(nèi)容存在侵權(quán),請進行舉報或認領(lǐng)
文檔簡介
實驗5實驗內(nèi)容及要求實驗內(nèi)容:為Linux系統(tǒng)設(shè)計一個簡單的二級文件系統(tǒng)。要求做到以下幾點:(1)可以實現(xiàn)下列幾條命令(至少4條);login 用戶登錄dir 列文件目錄create 創(chuàng)建文件delete 刪除文件open 打開文件close 關(guān)閉文件read 讀文件write 寫文件(2)列目錄時要列出文件名、物理地址、保護碼和文件長度;(3)源文件可以進行讀寫保護。要求:實驗室內(nèi)的實驗環(huán)境與系統(tǒng)是共用設(shè)施,請不要在系統(tǒng)內(nèi)做對系統(tǒng)或?qū)ζ渌脩舨话踩氖虑?。要求認真讀懂程序,分析程序,并根據(jù)執(zhí)行結(jié)果,分析原因。用戶要按通常實驗要認真書寫實驗報告。實驗提示:(1)首先應(yīng)確定文件系統(tǒng)的數(shù)據(jù)結(jié)構(gòu):主目錄、子目錄及活動文件等。主目錄和子目錄都以文件的形式存放于磁盤,這樣便于查找和修改。(2>用戶創(chuàng)建的文件,可以編號存儲于磁盤上。如file0,file1,file2...并以編號作為物理地址,在目錄中進行登記。實驗過程1.設(shè)計思想本文件系統(tǒng)采用兩級目錄,其中第一級對應(yīng)于用戶賬號,第二級對應(yīng)于用戶賬號下的文件。另外,為了簡單本文件系統(tǒng)未考慮文件共享、文件系統(tǒng)安全以及管道文件與設(shè)備文件等特殊內(nèi)容。對這些內(nèi)容感興趣的讀者,可以在本系統(tǒng)的程序基礎(chǔ)上進行擴充。2.主要數(shù)據(jù)結(jié)構(gòu)(1)i節(jié)點structinode{structinode *i_forw;structinode *i_back;char i_flag;unsignedint I_tmp //磁盤i節(jié)點標號unsignedint I_count; //引用計數(shù)unsignedshort di_number; //關(guān)聯(lián)文件數(shù),當為0時,則刪除該文件unsignedshort di_mode; //存取權(quán)限unsignedshort di_uid; //磁盤i節(jié)點用戶idunsignedshort di_gid; //磁盤i節(jié)忠組idunsignedint di_addr[NADDR]; //物理塊號}(2)磁盤i節(jié)點//關(guān)聯(lián)文件數(shù)structdinode{unsignedshort di_number; //關(guān)聯(lián)文件數(shù)unsignedshort di_mode; //存取權(quán)限椅unsignedshort di_uid;unsignedshort di_gid;unsignedlong di_size; //文件大小unsignedint di_addr[NADDR]; //物理塊號}(3)目錄項結(jié)構(gòu)structdirect{ chard_name[DIRSIZ]; //目錄名 unsignedintd_ino; //目錄號}(4)超級塊structfilsys{ unsignedshort s_isize; //i節(jié),點塊塊數(shù) unsignedlong s_fsize; //數(shù)據(jù)塊塊數(shù) unsignedint s_nfree; //空閑塊塊數(shù) unsignedshort s_pfree; //空閑塊指針 unsignedint s_free[NICFREE]; //空閑塊堆棧 unsignedint s_ninode; //空閑i節(jié)點數(shù) unsignedshort s_pinode; //空閑i節(jié)點指針 unsignedint s_inode[NICINOD]; //空閑i節(jié)點數(shù)組 unsignedint s_rinodepr; //銘記i節(jié)點 char s_fmod; //超級塊修改標志}(5)用戶密碼structpwd{ unsignedshort p_uid; unsignedshort p_gid; char passward[PW0SIZ];}(6)目錄structdir{ structdirect direct[DIRNUM]; int size;}(7)查找內(nèi)存i節(jié)點的hash表structhinode{ structinode *i_forw;}(8)系統(tǒng)打開表structfile{ char f_flag;//文件操作標志 unsignedint f_count; //引用計數(shù) structinode *f_inode; //指向內(nèi)存i節(jié)點 unsignedlong f_off$; //讀/寫指針}(9)用戶打開表structuser{ unsignedshort u_default_mode; unsignedshort u_uidid; //用戶標志 unsignedshort u_gid; //用戶組標志 unsignedshort u_ofile[NOFILE]; //用戶打開文件表}3.主要函數(shù)(1)i節(jié)點內(nèi)容獲取函數(shù)iget()(詳細描述略)。(2)i節(jié)點內(nèi)容釋放函數(shù)iput()(詳細描述略)。(3)目錄創(chuàng)建函數(shù)mkdir()(詳細描述略)。(4)目錄搜索函數(shù)namei()(詳細描述略)。(5)磁盤塊分配函數(shù)balloc()(詳細描述略)。(6)磁盤塊釋放函數(shù)bfree()(詳細描述略)。(7)分配i節(jié)點區(qū)函數(shù)ialloc()(詳細描述略)。(8)釋放i節(jié)點區(qū)函數(shù)ifree()(詳細描述略)。(9)搜索當前目錄下文件的函數(shù)iname()(詳細描述略)(10)訪問控制函數(shù)access()(詳細描述略)。(11)顯示目錄和文件用函數(shù)dir()(詳細描述略)。(12)改變當前目錄用函數(shù)chdir()(詳細描述略)。(13)打開文件函數(shù)open()(詳細描述略)。(14)創(chuàng)建文件函數(shù)create()(詳細描述略)。(15)讀文件用函數(shù)read()(詳細描述略)。(16)寫文件用函數(shù)write()(詳細描述略)。(17)用戶登錄函數(shù)login()(詳細描述略)。(18)用戶退出函數(shù)logout()(詳細描述略)。(19)文件系統(tǒng)格式化函數(shù)format()(詳細描述略)。(20)進入文件系統(tǒng)函數(shù)install()(詳細描述略)。(21)關(guān)閉文件函數(shù)close()(詳細描述略)。(22)退出文件系統(tǒng)函數(shù)halt()(詳細描述略)。(23)文件刪除函數(shù)delete()(詳細描述略)。4.主程序說明Begin Step1對磁盤進行格式化 Step2調(diào)用install(),進入文件系統(tǒng) Step3調(diào)用dir(),顯示當前目錄 Step4調(diào)用login(),用戶注冊 Step5調(diào)用mkdir()和chdir()創(chuàng)建目錄 Step6調(diào)用create(),創(chuàng)建文件 Step7分配緩沖區(qū) Step8寫文件 Step9關(guān)閉文件0和釋放緩沖 Step10調(diào)用mkdir()和chdir()創(chuàng)建子目錄 Step11調(diào)用create(),創(chuàng)建文件1 Step12分配緩沖區(qū) Step13寫文件1 Step14關(guān)閉文件l和釋放緩沖 Step15調(diào)用chdir將當前目錄移到上一級 Step16調(diào)用creat(),創(chuàng)建文件2 Step17分配緩沖區(qū) Step18調(diào)用write(),寫文件2 Step19關(guān)閉文件2和釋放緩沖 Step20調(diào)用delete(),刪除文件。 Step21調(diào)用create(),創(chuàng)建文件3 Step22為文件3分配緩沖區(qū) Step23調(diào)用write(),寫文件3 Step24關(guān)閉文件3并釋放緩沖區(qū) Step25調(diào)用open(),打開文件2 Step26為文件2分配緩沖 Step27寫文件3后關(guān)閉文件3 Step28釋放緩沖 Step29用戶退出(logout) Step30關(guān)閉(halt)end由上述描述過程可知,該文件系統(tǒng)實際是為用戶提供一個解釋執(zhí)行相關(guān)命令的環(huán)境。主程序中的大部分語句都被用來執(zhí)行相應(yīng)的命令。下面,我們給出每個過程的相關(guān)C語言程序。讀者也可以使用這些子過程,編寫出一個shell控制的文件系統(tǒng)界面。<程序>1.編程管理文件makefile本文件系統(tǒng)程序用makefile編程管理工具進行管理。其內(nèi)容如下。###makefile###filesys:main.oigetput.oiallfre.oballfre.oname.oaccess.olog.oclose.ocreat.odelete.odir.oopen.ordwt.oformat.oinstall.ohalt.o cc-ofilsysmain.oigetput.oiallfre.oballfre.oname.oaccess.olog.oclose.ocreat.odelete.odir.oopen.ordwt.oformat.oinstall.ohalt.omain.o:main.cfilesys.h cc-cmain.cigetput.o: igetput.cfilesys.h cc-cigetput.ciallfre.o: iallfre.cfilesys.h cc-ciallfre.cballfre.o: ballfre.cfilesys.h ame.o: name.cfilesys.h cc-cname.caccess.o:access.cfilesys.h; cc-caccess.clog.o: log.cfilesys.h cc-clog.cclose.o: close.cfilesys.h cc-cclose.ccreat.o: creat.cfilesys.h cc-ccreat.cdelete.o: delete.cfilesys.h cc-cdelete.cdir.o: dir.cfilesys.h cc-cdir.cdirlt.o: dirlt.cfilesys.h cc-cdirlt.copen.o: open.cfilesys.h cc-copen.crdwt.o:rdwt.cfilesys.h cc-crdwt.cformat.o:format.cfilesys.h cc-cformat.cinstall.o:install.cfilesys.h cc-cinstall.chalt.o: halt.c cc-chalt.c2.頭文件filesys.h頭文件fiiesp.h用來定義本文件系統(tǒng)中所使用的各種數(shù)據(jù)結(jié)構(gòu)和常數(shù)符號。//filesys.h定義本文件系統(tǒng)中的數(shù)據(jù)結(jié)構(gòu)和常數(shù)//filesys.h定義本文件系統(tǒng)中的數(shù)據(jù)結(jié)構(gòu)和常數(shù)#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 32//filsys#define DINODEBLK 32#define FILEBLK 512#define NICFREE 50#define NICINOD 50#define DINODESTART 2*BLOCKSIZ#defineDATASTART (2+DINODEBLK)*BLOCKSIZ//di_mode#define DIEMPTY 0000#define DIFILE 01000#define DIDIR 02000#define UDIREAD 00001 //user#define UDIWRITE 00002#define UDIEXICUTE 00004#define GDIREAD 00010 //group#define GDIWRITE 00020#define GDIEXICUTE 00040#define ODIREAD 00100 //other#define ODIWRITE 00200#define ODIEXECUTE 00400#define READ 1#define WRITE 2#define EXECUTE 4#define DEFAULTMODE 00777//I_flag#define IUPDATE 00002//s_fmod#define SUPDATE 00001//f_flag#define FREAD 00001#define FWRITE 00002#define FAPPEND 00004//error#define DISKFULL 65535//fseekorigin#define SEEK_SET 0//文件系統(tǒng)數(shù)據(jù)結(jié)構(gòu)structinode{ structinode *i_forw; structinode *i_back; char i_flag; unsignedint i_ino; //磁盤i節(jié)點標志 unsignedint i_count; //引用計數(shù) unsignedshort di_number; //關(guān)聯(lián)文件數(shù),當為0 unsignedshort di_mode; //存取權(quán)限 unsignedshort di_uid; unsignedshort di_gid; unsignedint di_size; //文件大小 unsignedint di_addr[NADDR]; //物理塊號};structdinode{ unsignedshort di_number; //善關(guān)聯(lián)文件數(shù)鈴/ unsignedshort di_mode; //存取權(quán)限禱/ unsignedshort di_uid; unsignedshort di_gidp; unsignedlong di_size; //文件大小 unsignedint di_addr[NADDR]; //禱物理塊號費/};structdirect{ char d_name[DIRSIZ]; unsignedint d_ino;};structfilsys{ unsignedshorts_isize; //i節(jié)點塊塊數(shù) unsignedlongs_fsize; //數(shù)據(jù)塊塊數(shù) unsignedints_nfree; //空閑塊塊數(shù) unsignedshorts_pfree; //空閑塊指針 unsignedints_free[NICFREE]; //空閑塊堆錢 unsignedints_ninode; //空閑i節(jié)點數(shù) unsignedshorts_pinode;//空閑i節(jié)點指針 unsignedints_inode[NICINOD]; //空閑i節(jié)點數(shù)組 unsignedints_rinode; //銘記i節(jié)點 chars_fmod; //超級塊修改標志};structpwd{ unsignedshortp_uid; unsignedshortp_gid; charpassword[PWDSIZ];};structdir{ structdirectdirect[DIRNUM]; intsize; //當前目錄大小};structhinode{ structinode *i_forw; //hash表指針};structfile{ charf_flag; //文件操作標志 unsignedint f_count; //引用計數(shù) structinode *f_inode; //指向內(nèi)存i節(jié)點 unsignedlong f_off; //讀/寫指針};structuser{ unsignedshort u_default_mode; unsignedshort u_uid; unsignedshort u_gid; unsignedshort u_ofile[NOFILE]; //用戶打開文件表};//下為全局變量 externstructhinodehinode[NHINO]; externstructdirdir; //當前目錄(在內(nèi)存中全部讀入) externstructfilesys_ofile[SYSOPENFILE]; externstructfilsysfilsys;//內(nèi)存中的超級塊 externstructpwdpwd[PWDNUM]; externstructuseruser[USERNUM]; externFILE*fd; //thefilesystemcolumnofallthesystem externstruct inode*cur_path_inode; externintuser_id,file_block;//proptypeofthesubroutionusedinthefilesystem externstructinode *iget(); externiput(); externunsignedintballoc(); externbfree(); externstructinode*ialloc(); externifree(); externunsignedintnamei(); externunsignedintiname(); externunsignedintaccess(); extern_dir(); externmkdir(); externchdir(); externdirlt(); externunsignedshortopen(); externcreate(); externunsignedintread(); externunsignedwrite(); externintlogin(); externlogout(); externinstall(); externformat(); externclose(); externhalt();3.主程序main()(文件名main.c)主程序main.c用來測試文件系統(tǒng)的各種設(shè)計功能主要功能描述如程序設(shè)計的第4部分。程序:#include <string.h>#include <stdio.h>#include "filesys.h"structhinodehinode[NHINO];structdir dir;structfile sys_ofile[SYSOPENFILE];structfilsys filsys;structpwd pwd[PWDNUM];structuser user[USERNUM];FILE *fd;structinode *cur_path_inode;int user_id,file_block;main(){ unsignedshort ab_fd1,ab_fd2,ab_fd3,ab_fd4; unsignedshort bhy_fd1; char *buf; printf("\nDoyouwanttoformatthedisk\n"); if(getchar()=='y'){ printf("Formatwilleraseallcontextonthedisk.Areyousure?\n"); getchar(); } elsereturn; if(getchar()=='y')format(); elsereturn; install(); _dir(); login(2118,"abcd"); user_id=0; mkdir("a2118"); chdir("a2118"); ab_fd1=create(user_id,"file0.c",01777); file_block=BLOCKSIZ*6+5; buf=(char*)malloc(BLOCKSIZ*6+5); write(ab_fd1,buf,BLOCKSIZ*6+5); close(user_id,ab_fd1); free(buf); mkdir("subdir"); chdir("subdir"); ab_fd2=create(user_id,"file1.c",01777); file_block=BLOCKSIZ*4+20; buf=(char*)malloc(BLOCKSIZ*4+20); write(ab_fd2,buf,BLOCKSIZ*4+20); close(user_id,ab_fd2); free(buf); chdir(".."); ab_fd3=create(user_id,"file2.c",01777); file_block=BLOCKSIZ*3+255; buf=(char*)malloc(BLOCKSIZ*3+255); write(ab_fd3,buf,BLOCKSIZ*3+255); close(user_id,ab_fd3); free(buf); _dir(); delete("ab_file0.c"); ab_fd4=creat(user_id,"file3.c",01777); file_block=BLOCKSIZ*8+300; buf=(char*)malloc(BLOCKSIZ*8+300); write(ab_fd4,buf,BLOCKSIZ*8+300); close(user_id,ab_fd4); free(buf); _dir(); ab_fd3=open(user_id,"file2.c",FAPPEND); file_block=BLOCKSIZ*3+100; buf=(char*)malloc(BLOCKSIZ*3+100); write(ab_fd3,buf,BLOCKSIZ*3+100); close(user_id,ab_fd3); free(buf); _dir(); chdir(".."); logout(); halt();}4.初始化磁盤格式程序format()(文件名format.c)#include <stdio.h>#include "filesys.h"format(){ structinode *inode; structdirectdir_buf[BLOCKSIZ/(DIRSIZ+2)]; structfilsys; unsignedintblock_buf[BLOCKSIZ/sizeof(int)]; char *buf; int i,j;//creatthefilesystem fd=fopen("filesystern","r+w+b"); buf=(char*)malloc((DINODEBLK+FILEBLK+2)*BLOCKSIZ*sizeof(char)); if(buf==NULL) { printf("\nfilesystemfilecEeatfailed!\n"); exit(0); } fseek(fd,0,SEEK_SET); fwrite(buf,1,(DINODEBLK+FILEBLK+2)*BLOCKSIZ*sizeof(char),fd);//0.initializethepassword pwd[0].p_uid=2116; pwd[0].p_gid=03; strcpy(pwd[0].password,"dddd"); pwd[1].p_uid=2117; pwd[1].p_gid=03; strcpy(pwd[1].password,"bbbb"); pwd[2].p_uid=2118; pwd[2].p_gid=04; strcpy(pwd[2].password,"abcd"); pwd[3].p_uid=2119; pwd[3].p_gid=04; strcpy(pwd[3].password,"cccc"); pwd[4].p_uid=2220; pwd[4].p_gid=05; strcpy(pwd[4].password,"eeee");//1Creatthemaindirectory&itssubdiretcandthefilepassword inode=iget(0); //0emptydinodeid inode->di_mode=DIEMPTY; iput(inode); inode=iget(1); //1maindirid inode->di_number=1; inode->di_mode=DEFAULTMODE|DIDIR; inode->di_size=3*(DIRSIZ+2); inode->di_addr[0]=0; //block0#isusedbythemaindirectory strcpy(dir_buf[0].d_name,".."); dir_buf[0].d_ino=1; strcpy(dir_buf[1].d_name,"."); dir_buf[1].d_ino=1; strcpy(dir_buf[2].d_name,"etc"); dir_buf[2].d_ino=2; fseek(fd,DATASTART,SEEK_SET); fwrite(dir_buf,1,3*(DIRSIZ+2),fd); iput(inode); inode=iget(2); //2etcdirid inode->di_number=1; inode->di_mode=DEFAULTMODE|DIDIR; inode->di_size=3*(DIRSIZ+2); inode->di_addr[0]=1; //block0#isusedbytheetcdirectory strcpy(dir_buf[0].d_name,".."); dir_buf[0].d_ino=1; strcpy(dir_buf[1].d_name,".."); dir_buf[1].d_ino=2; strcpy(dir_buf[2].d_name,"password"); dir_buf[2].d_ino=3; fseek(fd,DATASTART+BLOCKSIZ*1,SEEK_SET); fwrite(dir_buf,1,3*(DIRSIZ+2),fd); iput(inode); inode=iget(3);//3passwordid inode->di_number=1; inode->di_mode=DEFAULTMODE|DIDIR; inode->di_size=BLOCKSIZ; inode->di_addr[0]=2; //block2#isusedbythepasswordfile for(i=5;i<PWDNUM;i++){ pwd[i].p_uid=0; pwd[i].p_gid=0; strcpy(pwd[i].password,""); } fseek(fd,DATASTART+2*BLOCKSIZ,SEEK_SET); fwrite(pwd,1,BLOCKSIZ,fd); iput(inode); //2.initializethesuperblock filsys.s_isize=DINODEBLK; filsys.s_fsize=FILEBLK; filsys.s_ninode=DINODEBLK*BLOCKSIZ/DINODESIZ-4; filsys.s_nfree=FILEBLK-3; for(i=0;i<NICINOD;i++){//beginwith4,0,1,2,3,isusedbymain,etc,password filsys.s_inode[i]=4+i; } filsys.s_pinode=0; filsys.s_rinode=NICINOD+4; block_buf[NICFREE-1]=FILEBLK+1; //FILEBLK+1isaflagofend for(i=0;i<NICFREE-1;i++) block_buf[NICFREE-2-i]=FILEBLK-i; fseek(fd,DATASTART+BLOCKSIZ*(FILEBLK-NICFREE-1),SEEK_SET); fwrite(block_buf,1,BLOCKSIZ,fd); for(i=FILEBLK-NICFREE-1;i>2;i-=NICFREE){ for(j=0;j<NICFREE;j++){ block_buf[j]=i-j; } block_buf[j]=50; fseek(fd,DATASTART+BLOCKSIZ*(i-1),SEEK_SET); fwrite(block_buf,1,BLOCKSIZ,fd); } j=i+NICFREE; for(i=j;i>2;i--){ filsys.s_free[NICFREE-1+i-j]=i; } filsys.s_pfree=NICFREE-1-j+3; filsys.s_pinode=0; fseek(fd,BLOCKSIZ,SEEK_SET); fwrite(&filsys.s_isize,1,sizeof(filsys),fd); fseek(fd,BLOCKSIZ,SEEK_SET); fread(&filsys.s_isize,1,sizeof(filsys),fd);}5.進入文件系統(tǒng)程序install()(文件名install.c)#include <stdio.h>#include <string.h>#include "filesys.h"install(){ int i,j;//1.readthefilsysfromthesuperblock fseek(fd,BLOCKSIZ,SEEK_SET); fread(&filsys,1,sizeof(structfilsys),fd);//2.initializetheinodehashchain for(i=0;i<NHINO;i++){ hinode[i].i_forw=NULL; }//3.initializethesys_file for(i=0;i<SYSOPENFILE;i++){ sys_ofile[i].f_count=0; sys_ofile[i].f_inode=NULL; }//4.initializetheuser for(i=0;i<USERNUM;i++){ user[i].u_uid=0; user[i].u_gid=0; for(j=0;j<NOFILE;j++)user[i].u_ofile[j]=SYSOPENFILE+1; }//5readthemaindirectorytoinitializethedir cur_path_inode=iget(1); dir.size=cur_path_inode->di_size/(DIRSIZ+2); for(i=0;i<DIRNUM;i++){ strcpy(dir.direct[i].d_name,""); dir.direct[i].d_ino=0; } for(i=0;i<dir.size/(BLOCKSIZ/(DIRSIZ+2));i++){ fseek(fd,DATASTART+BLOCKSIZ*cur_path_inode->di_addr[i],SEEK_SET); fread(&dir.direct[(BLOCKSIZ/(DIRSIZ+2))*i],1,BLOCKSIZ,fd); } fseek(fd,DATASTART+BLOCKSIZ*cur_path_inode->di_addr[i],SEEK_SET); fread(&dir.direct[(BLOCKSIZ)/(DIRSIZ+2)*i],1,cur_path_inode->di_size%BLOCKSIZ,fd);}6.退出程序halt()(文件名halt.c)#include <stdio.h>#include "filesys.h"halt(){ structinode*inode; int i,j;//1.writebackthecurrentdir chdir(".."); iput(cur_path_inode);//2.freetheu_ofileandsys_ofileandinode for(i=0;i<USERNUM;i++){ if(user[i].u_uid!=0){ for(j=0;j<NOFILE;j++){ if(user[i].u_ofile[j]!=SYSOPENFILE+1){ close(i,j); user[i].u_ofile[j]=SYSOPENFILE+1; } } } }//3.writebackthefilesystothedisk fseek(fd,BLOCKSIZ,SEEK_SET); fwrite(&filsys,1,sizeof(structfilsys),fd);//4.closethefilesystemcolumn fclose(fd);//5.sayGOODBYEtoalltheuser printf("\nGoodbye.SeeyouNextTime.\n"); exit(0);}7.獲取釋放i節(jié)點內(nèi)容程序iget()/iput()(文件名igetput.c)#include <stdio.h>#include "filesys.h"structinode*iget(dinodeid) //iget()unsignedintdinodeid;{ intexisted=0,inodeid; longaddr; structinode*temp,*newinode; inodeid=dinodeid%NHINO; if(hinode[inodeid].i_forw==NULL)existed=0; else{ temp=hinode[inodeid].i_forw; while(temp){ if(temp->i_ino==inodeid){ //existed existed=1; temp->i_count++; returntemp; } Else{ //notexisted temp=temp->i_forw; } } }//notexisted//1.calculatetheaddrofthedinodeinthefilesyscolumn addr=DINODESTART+dinodeid*DINODESIZ;//2.mallocthenewinode newinode=(structinode*)malloc(sizeof(structinode));//3.readthedinodetotheinode fseek(fd,addr,SEEK_SET); fread(&(newinode->di_number),DINODESIZ,1,fd);//4.putitintohinode[inodeid]queue newinode->i_forw=hinode[inodeid].i_forw; newinode->i_back=newinode; if(newinode->i_forw!=NULL) newinode->i_forw->i_back=newinode; hinode[inodeid].i_forw=newinode;//5.initializetheirlode newinode->i_count=1; newinode->i_flag=0; newinode->i_ino=dinodeid; newinode->di_size=3*(DIRSIZ+2); if(dinodeid==3)newinode->di_size=BLOCKSIZ; returnnewinode;}iput(pinode) //iput()structinode*pinode;{ longaddr; unsignedintblock_num; int i; if(pinode->i_count>1){ pinode->i_count--; return; } else{ if(pinode->di_number!=0){ //writebacktheinode addr=DINODESTART+pinode->i_ino*DINODESIZ; fseek(fd,addr,SEEK_SET); fwrite(&pinode->di_number,DINODESIZ,1,fd); } else{ //rmtheinode&theblockofthefileinthedisk block_num=pinode->di_size/BLOCKSIZ; for(i=0;i<block_num;i++) bfree(pinode->di_addr[i]); ifree(pinode->i_ino); }//freetheinodeinthememory if(pinode->i_forw==NULL) pinode->i_back->i_forw=NULL; else{ pinode->i_forw->i_back=pinode->i_back; pinode->i_back->i_forw=pinode->i_forw; } ifree(pinode); }}8.i節(jié)點分配和釋放函數(shù)ialloc()和ifree()(文件名iallfre.c)#include <stdio.h>#include "filesys.h"staticstruct dinodeblock_buf[BLOCKSIZ/DINODESIZ];structinode *ialloc() //ialloc{ structinode *temp_inod; unsignedintcur_di; inti,count,block_end_flag; if(filsys.s_pinode==NICINOD){ //s_inodeempty i=0; count=0; block_end_flag=1; filsys.s_pinode=NICINOD-1; cur_di=filsys.s_rinode; while((count<NICINOD)||(count<=filsys.s_ninode)){ if(block_end_flag){ fseek(fd,DINODESTART+cur_di*DINODESIZ,SEEK_SET); fread(block_buf,1,BLOCKSIZ,fd); block_end_flag=0; i=0; } while(block_buf[i].di_mode==DIEMPTY){ cur_di++; i++; } if(i==NICINOD) block_end_flag=1; else{ filsys.s_inode[filsys.s_pinode--]=cur_di; count++; } } filsys.s_rinode=cur_di; } temp_inod=iget(filsys.s_inode[filsys.s_pinode]); fseek(fd,DINODESTART+filsys.s_inode[filsys.s_pinode]*DINODESIZ,SEEK_SET); fwrite(&temp_inod->di_number,1,sizeof(structdinode),fd); filsys.s_pinode++; filsys.s_ninode--; filsys.s_fmod=SUPDATE; returntemp_inod;}ifree(dinodeid) //ifreeunsigneddinodeid;{ filsys.s_ninode++; if(filsys.s_pinode!=NICINOD){ //notfull filsys.s_inode[filsys.s_pinode]=dinodeid; filsys.s_pinode++; } else{ //full if(dinodeid<filsys.s_rinode){ filsys.s_inode[NICINOD]=dinodeid; filsys.s_rinode=dinodeid; } }}9.磁盤塊分配與釋放函數(shù)balloc()與bfree()(文件名ballfre.c)#include <stdio.h>#include "filesys.h"staticunsignedintblock_buf[BLOCKSIZ];unsignedint balloc(){ unsignedint free_block,free_block_num; int i; if(filsys.s_nfree==0){ printf("\nDiskFull!\n"); returnDISKFULL; } free_block=filsys.s_free[filsys.s_pfree]; if(filsys.s_pfree==NICFREE-1){ fseek(fd,DATASTART+(562-filsys.s_nfree)*BLOCKSIZ,SEEK_SET); fread(block_buf,1,BLOCKSIZ,fd); free_block_num=block_buf[NICFREE]; //thetotalnumberinthegroup for(i=0;i<free_block_num;i++){ filsys.s_free[NICFREE-1-i]=block_buf[i]; } filsys.s_pfree=NICFREE-free_block_num; } else filsys.s_pfree++; filsys.s_nfree--; filsys.s_fmod=SUPDATE; returnfree_block;}bfree(block_num)unsignedintblock_num;{ int i; if(filsys.s_free==0){ //s_freefull block_buf[NICFREE]=NICFREE; for(i=0;i<NICFREE;i++){ block_buf[i]=filsys.s_free[NICFREE-1-i]; } filsys.s_pfree=NICFREE-1; } fseek(fd,BLOCKSIZ,SEEK_SET); fwrite(block_buf,1,BLOCKSIZ,fd); filsys.s_nfree++; filsys.s_fmod=SUPDATE;}10.搜索函數(shù)namei()和iname()(文件名name.c)#include <string.h>#include <stdio.h>#include "filesys.h"unsignedintnamei(name) //nameichar *name;{ inti,notfound=1; for(i=0;((i<dir.size)&&(notfound));i++) if((!strcmp(dir.direct[i].d_name,name))&&(dir.direct[i].d_ino!=0)) returndir.direct[i].d_ino; //find return0; //notfind}unsignedintiname(name) //inamechar *name;{ inti,notfound=1; for(i=0;((i<DIRNUM)&&(notfound));i++) if(dir.direct[i].d_ino==0){ notfound=0; break; } if(notfound){ printf("\nThecurrentdirectoryisfull!\n"); return0; } else { strcpy(dir.direct[i].d_name,name); dir.direct[i].d_ino=1; returni; }}11.訪問控制函數(shù)access()(文件名access.c)#include <stdio.h>#include "filesys.h"unsignedintaccess(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; return0; caseEXECUTE: if(inode->di_mode&ODIEXECUTE)return1; if((inode->di_mode&GDIEXICUTE) &&(user[user_id].u_gid==inode->di_gid))return1; if((inode->di_mode&UDIEXICUTE) &&(user[user_id].u_uid==inode->di_uid))return1; return0; caseDEFAULTMODE: return1; default: return0; }}12.顯示列表函數(shù)_dir()和目錄創(chuàng)建函數(shù)mkdir()等(文件名dir.c)#include <stdio.h>#include <string.h>#include "filesys.h"_dir() //_dir{ unsignedshortdi_mode; inti,j,k,one; structinode*temp_inode; printf("\nCURRENTDIRECTORYzdir.size=%d\n",dir.size); for(i=0;i<dir.size;i++){ if(dir.direct[i].d_ino!=DIEMPTY){ printf("%20s",dir.direct[i].d_name); temp_inode=iget(dir.direct[i].d_ino); di_mode=temp_inode->di_mode; if(temp_inode->di_mode&DIFILE)printf("f"); elseprintf("d"); for(j=0;j<9;j++){ one=di_mode%2; di_mode=di_mode/2; if(one)printf("x");elseprintf("-"); } if(temp_inode->di_mode&DIFILE){ printf("%ld",temp_inode->di_size); printf("blockchain:"); for(k=0;k<temp_inode->di_size/BLOCKSIZ+1;k++) printf("%d",temp_inode->di_addr[k]); printf("\n"); } else printf("<dir>blockchain:%d\n",dir.direct[i].d_ino); iput(temp_inode); } }}mkdir(dirname) //mkdirchar *dirname;{ intdirid,dirpos; structinode*inode; structdirectbuf[BLOCKSIZ/(DIRSIZ+2)]; unsignedintblock; dirid=namei(dirname); if(dirid!=0){ inode=iget(dirid); if(inode->di_mode&DIDIR) printf("\n%sdirectoryalreadyexisted!\n",dirname); else printf("\n%sisafilename,&can'tcreatadirthesamename\n",dirname); iput(inode); return; } dirpos=iname(dirname); inode=ialloc(); dirid=inode->i_ino; dir.direct[dirpos].d_ino=inode->i_ino; dir.size++;//fillthenewdirbuf strcpy(buf[0].d_name,"."); buf[0].d_ino=dirid; strcpy(buf[1].d_name,".."); buf[1].d_ino=cur_path_inode->i_ino; buf[2].d_ino=0; block=balloc(); fseek(fd,DATASTART+block*BLOCKSIZ,SEEK_SET); fwrite(buf,1,BLOCKSIZ,fd); inode->di_size=2*(DIRSIZ+2); inode->di_number=1; inode->di_mode=user[user_id].u_default_mode|DIDIR; inode->di_uid=user[user_id].u_uid; inode->di_gid=user[user_id].u_gid; inode->di_addr[0]=block; iput(inode); return;}chdir(dirname) //chdirchar *dirname;{ unsignedintdirid; structinode *inod; unsignedshortblock; inti,j,low=0,high=0; dirid=namei(dirname); if(dirid==0){ printf("\n%sdoesnotexist!\n",dirname); return; } inod=iget(dirid); if(!access(user_id,inod,user[user_id].u_default_mode)){ printf("\nhasnotaccesstothedirectory%s\n",dirname); iput(inod); return; }//packthecurrentdirectory for(i=0;i<dir.size;i++){ for(j=0;j<DIRNUM;j++) if(dir.direct[j].d_ino=0)break; memcpy(&dir.direct[j],&dir.direct[i],DIRSIZ+2); dir.direct[j].d_ino=0; }//writebackthecurrentdirectory for(i=0;i<cur_path_inode->di_size/BLOCKSIZ+1;i++){ bfree(cur_path_inode->di_addr[i]); for(i==0;i<dir.size;i+=BLOCKSIZ/(DIRSIZ+2)) block=balloc(); cur_path_inode->di_addr[i]=block; fseek(fd,DATASTART+block*BLOCKSIZ,SEEK_SET); fwrite(&dir.direct[0],1,BLOCKSIZ,fd); cur_path_inode->di_size=dir.size*(DIRSIZ+2); iput(cur_path_inode); cur_path_inode=inod; dir.size=inod->di_size/(DIRSIZ+2);//readthechangedirfromdisk j=0; for(i=0;i<inod->di_size/BLOCKSIZ+1;i++){ fseek(fd,DATASTART+inod->di_addr[i]*BLOCKSIZ,SEEK_SET); fread(&dir.direct[0],1,BLOCKSIZ,fd); j+=BLOCKSIZ/(DIRSIZ+2); } return; }}13.文件創(chuàng)建函數(shù)create()(文件名create.c)#include <stdio.h>#include "filesys.h"create(user_id,filename,mode)unsignedintuser_id;char *filename;unsignedshortmode;{ unsignedintdi_ino,di_ith; struct inode*inode; int i,j; di_ino=namei(filename); if(di_ino!=0){ //alreadyexited inode=iget(di_ino); if(access(user_id,inode,mode)==0){ iput(mode); printf("\ncreateaccessnotallowed"); return; }//freealltheblockoftheoldfile for(i=0;i<inode->di_size/BLOCKSIZ+1;i++){ bfree(inode->di_addr[i]); }//todo:addcodeheretoupdatethepointerofthesys_file for(i=0;i<SYSOPENFILE;i++) if(sys_ofile[i].f_inode==inode) sys_ofile[i].f_off=0; for(i=0;i<NOFILE;i++) if(user[user_id].u_ofile[i]==SYSOPENFILE+1){ user[user_id].u_uid=inode->di_uid; user[user_id].u_gid=inode->di_gid; for(j=0;j<SYSOPENFILE;j++) if(sys_ofile[j].f_count==0){ user[user_id].u_ofile[i]=j; sys_ofile[j].f_flag=mode; } returni; } } else{ //notexistedbefore inode=ialloc(); di_ith=iname(filename); dir.size++; dir.direct[di_ith].d_ino=inode->i_ino; dir.direct[di_ith+1].d_ino=0; inode->di_mode=user[user_id].u_default_mode|DIFILE; inode->di_uid=user[user_id].u_uid; inode->di_gid=user[user_id].u_gid; inode->di_size=file_block; inode->di_number=1; for(i=0;i<SYSOPENFILE;i++) if(sys_ofile[i].f_count==0) break; for(j=0;j<NOFILE;j++) if(user[user_id].u_ofile[j]==SYSOPENFILE+1) break; user[user_id].u_ofile[j]=i; sys_ofile[i].f_flag=mode; sys_ofile[i].f_count=0; sys_ofile[i].f_off=0; sys_ofile[i].f_inode=inode; returnj; } }14.打開文件函數(shù)Open()(文件名open.c)#include <stdio.h>#include "filesys.h"unsignedshortopen(user_id,filename,openmode)intuser_id;char *filename;unsignedshortopenmode;{ unsignedintdinodeid; struct inode *inode; int i,j; dinodeid=namei(filename); if(dinodeid==0){ //nosuchfile printf("\nfiledoesnotexist!\n"); return0; } inode=iget(dinodeid); if(!access(user_id,inode,openmode)){ //accessdenied printf("\nfileopenhasnotaccess!\n"); iput(inode); return0; }//allocthesys_ofileitem for(i=0;i<SYSOPENFILE;i++) if(sys_ofile[i].f_count==0)break; if(i==SYSOPENFILE){ printf("\nsysternopenfiletoomuch\n"); iput(inode); return0; } sys_ofile[i].f_inode=inode; sys_ofile[i].f_flag=openmode; sys_ofile[i].f_count=1; if(openmode&FAPPEND) sys_ofile[i].f_off=inode->di_size; else sys_ofile[i].f_off=0;//alloctheuseropenfileitem for(j=0;j<NOFILE;j++) if(user[user_id].u_ofile[j]==SYSOPENFILE+1) break; if(j==NOFILE){ printf("\nuseropenfiletoomuch!\n"); sys_ofile[i].f_count=0; iput(inode); return0; } user[user_id].u_ofile[j]=1;//ifHAPPEND,freetheblockofthefiiebefore if(!(openmode&FAPPEND)){ for(i=0;i<inode->di_size/BLOCKSIZ+1;i++) bfree(inode->di_addr[i]); inode->di_size=0; } returnj;}15.關(guān)閉文件系統(tǒng)函數(shù)close()(文件名close.c)#include <stdio.h>#include "filesys.h"close(user_id,cfd) //closeunsignedintuser_id;unsignedshortcfd;{ structinode *inode; inode=sys_ofile[user[user_id].u_ofile[cfd]].f_inode; iput(inode); sys_ofile[user[user_id].u_ofile[cfd]].f_count--; user[user_id].u_ofile[cfd]=SYSOPENFILE+1;}16.刪除文件函數(shù)delete()(文件名delete.c)#include <stdio.h>#include "filesys.h"delete(filename)char *filename;{ unsignedintdinodeid,i; structinode*inode; dinodeid=namei(filename); if(dinodeid!=0) inode=iget(dinodeid); inode->di_number--; for(i=0;i<dir.size;i++) if(dir.direct[i].d_ino==dinodeid)break; i++; while(dir.direct[i].d_ino!=0){ strcpy(dir.direct[i-1].d_name,dir.direct[i].d_name); dir.direct[i-1].d_ino=dir.direct[i].d_ino; i++; } dir.direct[i+1].d_ino=0; dir.size=i-1; iput(inode); printf("\ndir.size=%d\n",dir.size);}17.讀寫文件函數(shù)read()與write()(文件名rdwt.c)#include <stdio.h>#include "filesys.h"#include "dirent.h"unsignedintread(fd1,buf,size)int fd1;char *buf;unsignedintsize;{ unsignedlongoff; intblock,block_off,i,j; stru
溫馨提示
- 1. 本站所有資源如無特殊說明,都需要本地電腦安裝OFFICE2007和PDF閱讀器。圖紙軟件為CAD,CAXA,PROE,UG,SolidWorks等.壓縮文件請下載最新的WinRAR軟件解壓。
- 2. 本站的文檔不包含任何第三方提供的附件圖紙等,如果需要附件,請聯(lián)系上傳者。文件的所有權(quán)益歸上傳用戶所有。
- 3. 本站RAR壓縮包中若帶圖紙,網(wǎng)頁內(nèi)容里面會有圖紙預(yù)覽,若沒有圖紙預(yù)覽就沒有圖紙。
- 4. 未經(jīng)權(quán)益所有人同意不得將文件中的內(nèi)容挪作商業(yè)或盈利用途。
- 5. 人人文庫網(wǎng)僅提供信息存儲空間,僅對用戶上傳內(nèi)容的表現(xiàn)方式做保護處理,對用戶上傳分享的文檔內(nèi)容本身不做任何修改或編輯,并不能對任何下載內(nèi)容負責。
- 6. 下載文件中如有侵權(quán)或不適當內(nèi)容,請與我們聯(lián)系,我們立即糾正。
- 7. 本站不保證下載資源的準確性、安全性和完整性, 同時也不承擔用戶因使用這些下載資源對自己和他人造成任何形式的傷害或損失。
最新文檔
- 2024辣椒購銷合同爭議的解決方式
- 2025年度智能化廚房設(shè)備采購與安裝一體化合同4篇
- 2025年投標采購心得體會總結(jié)與合同管理創(chuàng)新合同3篇
- 個人房屋轉(zhuǎn)讓協(xié)議書合同范本
- 2024年駕校場地使用權(quán)益轉(zhuǎn)讓合同
- 2025年度煤礦廢棄資源煤矸石回收利用合同4篇
- 2025年度油氣田鉆井工程合同執(zhí)行監(jiān)督合同范本4篇
- 全新2025年度醫(yī)療設(shè)備采購與安裝合同5篇
- 2025版污水處理廠智能化改造與運營維護協(xié)議3篇
- 2025版領(lǐng)隊與紀念品供應(yīng)商合作協(xié)議范本4篇
- 2024-2030年中國護肝解酒市場營銷策略分析與未來銷售渠道調(diào)研研究報告
- 人教版高中數(shù)學必修二《第十章 概率》單元同步練習及答案
- 智慧校園信息化建設(shè)項目組織人員安排方案
- 浙教版七年級上冊數(shù)學第4章代數(shù)式單元測試卷(含答案)
- 一病一品成果護理匯報
- AQ-T 1009-2021礦山救護隊標準化考核規(guī)范
- 鹽酸??颂婺崤R床療效、不良反應(yīng)與藥代動力學的相關(guān)性分析的開題報告
- 消防設(shè)施安全檢查表
- 組合結(jié)構(gòu)設(shè)計原理 第2版 課件 第6、7章 鋼-混凝土組合梁、鋼-混凝土組合剪力墻
- 建筑公司資質(zhì)常識培訓課件
- GB/T 26316-2023市場、民意和社會調(diào)查(包括洞察與數(shù)據(jù)分析)術(shù)語和服務(wù)要求
評論
0/150
提交評論