課程名: 計算機操作系統地點: 姓名: 班級: 項目: 文件操作 實驗目的 1、 掌握文件系統的工作機理 2、 理解文件系統的主要數據結構 3、 學習較為復雜的 Linux 下的編程 學號: 成績 實驗內容 一、 設計并實現一個一級(單用戶) 文件系統程序 1、 提供以下操作 文件創建/刪除接口命令 / 目錄創建/刪除接口命令 /rmdir 顯示目錄內容命令 ls 2、 創建的文件不要求格式和內容 二、 在以上基礎上設計并實現一個二級文件系統 1、 具備提供用戶登錄 2、 文件、 目錄要有權限 程序主文件代碼: # # # # # "。 h" # "creat。 h" # "。 h" # "。 h" # "close。 h" # "。 h" # "dir。 h" # "。 h" # "halt。
h" # "。 h" # "。 h" # "log。 h" # "name。 h" # "open。 h" # "rdwt。 h" # "。 h" [NHINO] ; dir dir; file [] ; ; pwd pwd[]; user user[]; FILE *fd; inode *; int ; short ; char usr_p[12]; char sel; char [12]; main() { short , , , , i, j; char *buf; int done=1; ("\nDo you want to the disk(y or n) ?\n") ; if(() ==' y' ) { ("\ will erase all on the disk \n") ; ("。
。 。 \n") ; () ; ("\nNow will the , wait。 。 。 \n") ; () ; ("\n----Login----\ input your :") ; scanf("%u", &) ; ("\ input your :") ; scanf("%s", &usr_p) ; if(!login(, usr_p) ) ; /*login(2118, "abcd") ;*/ while(done) { ("\n Your \n") ; (" -1----ls\n -2----mkdir\n -3---- dir\n -4---- file\n -0----\n") ; sel=() ; sel=() ; (sel) { for help。
\n") ; break; } } } else ("User \nGood Bye\n") ; } 索引節點的數據結構: inode { inode *; inode *; char ; int i_ino; int ;case ' 1' : _dir() ; break; case ' 2' : (" input dir name: ") ; scanf("%s", ) ; mkdir() ; break; case ' 3' : (" input dir name: ") ; scanf("%s", ) ; chdir() ; break; case ' 4' : (" input file name:") ; scanf("%s", ) ; =creat(2118, , 01777) ; buf=(char *) (*6+5) ; write(, buf, *6+5) ; close(0, ) ; free(buf) ; break; case ' 0' : () ; halt() ; done = 0; : ("error!\nNo such , try again。
\nOr you can int [NADDR] ; short ; short ; short ; short ; short ; } ; 超級塊的數據結構 { short ; long ; int ; short ; int []; int ; short ; int []; int ; char ; } ; 用戶和口令的數據結構 user { short ; short u_uid; short u_gid; short [] ; } ; pwd { short p_uid; short p_gid; char []; } ; 有關目錄的數據結構 { short ; short ; short ; short ; long ; int [NADDR] ; } ; { char [] ; int d_ino; } ; dir { []; int size; } ; 初始化數據塊 for(i=+2; &) 1; if((inode->&) && (user[]。
u_gid==inode->) ) 1; if((inode->&) && (user[]。 u_uid==inode->) ) 1; 0; case WRITE: 。 。 。 。 。 。 。 。 。 case : : 1; 創建文件 while(--; iput(inode) ; } 讀文件 inode=[user[]。 [cfd]]。 ; if(!([user[]。 [cfd] ]。 &FREAD) ) { ("\nthe file is not for read\n") ; 0; } if((off+size) >inode->) size=inode->-off; =off%; block=off/; if(+[j+i] *, ) ; fread(, 1, ,fd) ; +=; } } 寫文件 if(!([user[]。
[cfd] ]。 &) ) { ("\nthe file is not for write\n") ; 0; } if(+[block]*+成都信息工程學院操作系統實驗報告四文件操作, ) ; (buf, 1, size, fd) ; size; } fseek(fd, +inode->[block]*+, ) ; (, 1, -, fd) ; +=-; for(i=0;[block+1+i]=() ; fseek(fd, +inode->[block+1+i]*, ) ; (,1成都信息工程學院操作系統實驗報告四文件操作, , fd) ; 打開文件 if(! =(int) NULL) { ("\nfile does not !!! \n") ; (int) NULL;} inode=iget() ; if(!(, inode, ) ) { ("\nfile open has not !!!\n") ; iput(inode) ; (int) NULL;} for(i=1;i