实验5 Linux文件管理

更新时间:2024-03-08 15:20:01 阅读量: 综合文库 文档下载

说明:文章内容仅供预览,部分内容可能不全。下载后的文档,内容与下面显示的完全一致。下载之前请确认下面内容是否您想要的,是否完整无缺。

实验5 Linux文件管理

一、实验目的

1. 掌握Linux文件系统的基本原理、结构和实现方法

2. 掌握Linux文件系统中文件的建立、打开、读/写、执行、属性等系统调用的使用 3. 学会设计简单的文件系统并实现一组操作

4. 通过实验学习文件系统的系统调用命令,提高对文件系统实现功能的理解和掌握 二、实验内容

设计并实现一个一级文件系统程序,要求实现以下功能: 1. 提供文件创建/删除接口命令create / delete、目录创建/删除接口命令mkdir / rmdir、显示目录内容命令ls等。

2. 创建的文件不要求格式和内容。 三、实验结果

1. 程序源代码

main.cpp

#include #include #include #include

#include \#include \#include \#include \#include \#include \#include \#include \#include \#include \#include \#include \#include \#include \#include \

#include \

struct hinode hinode[NHINO];

struct dir dir;

struct file sys_ofile[SYSOPENFILE]; struct filsys filsys;

struct pwd pwd[PWDNUM];

struct user user[USERNUM]; FILE *fd;

struct inode *cur_path_inode; int user_id; /*kkkkk*/

unsigned short usr_id; char usr_p[12]; char sel;

char temp_dir[12]; main() {

unsigned short ab_fd1,ab_fd2,ab_fd3,ab_fd4,i,j; char *buf; int done=1;

printf(\ if(getchar()=='y') {

printf(\ printf(\ format();

printf(\ install();

printf(\ scanf(\

printf(\ scanf(\

/* printf(\ if(!login(usr_id,usr_p)) return; while(done) {

printf(\

printf(\-1----ls\\n -2----mkdir\\n -3----change dir\\n -4----create file\\n -0----Logout\\n\ sel=getchar(); sel=getchar(); switch(sel) {

case '1': _dir(); break; case '2':

printf(\

scanf(\ mkdir(temp_dir); break; case '3':

printf(\ scanf(\ chdir(temp_dir); break; case '4':

printf(\ scanf(\

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(\your teacher for help.\\n\ break; } } } else

printf(\}

structure.h

#ifndef _STRUCTURE_H #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 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 ODIWRITE 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 0

struct inode {

struct inode *i_forw; struct inode *i_back; char i_flag;

unsigned int i_ino; unsigned int i_count;

unsigned int di_addr[NADDR]; 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; unsigned short di_gid; unsigned long di_size;

unsigned int di_addr[NADDR]; };

struct direct {

char d_name[DIRSIZ]; 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_free[NICFREE];

unsigned int s_ninode; unsigned short s_pinode;

unsigned int s_inode[NICINOD]; unsigned int s_rinode;

{

inode=iget(di_ino);

if(access(user_id,inode,mode)==0) {

iput(inode);

printf(\ return 0; }

for(i=0; idi_size/BLOCKSIZ+1; i++) {

bfree(inode->di_addr[i]); }

for(i=0; i

if(sys_ofile[i].f_inode==inode) {

sys_ofile[i].f_off=0; }

for(i=0; 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

user[user_id].u_ofile[i]=j;

sys_ofile[j].f_flag=(char)mode; } return i; } } else {

inode=ialloc();

di_ith=iname(filename);

dir.size++;

dir.direct[di_ith].d_ino=inode->i_ino;

inode->di_mode=user[user_id].u_default_mode; inode->di_uid=user[user_id].u_uid; inode->di_gid=user[user_id].u_gid; inode->di_size=0; inode->di_number=0;

for(i=0; i

break; }

for(j=0; j

if(user[user_id].u_ofile[j]==SYSOPENFILE+1) {

break; }

user[user_id].u_ofile[j]=i;

sys_ofile[i].f_flag=(char)mode; sys_ofile[i].f_count=0; sys_ofile[i].f_off=0;

sys_ofile[i].f_inode=inode; return j; }

return 0; }

delete.h

#include #include \

delete(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(\ for(i=0;idi_mode; for(j=0;j<9;j++) { one=di_mode%2; di_mode=di_mode/2; if(one) printf(\ else printf(\ } if(temp_inode->di_mode&&DIFILE==1) { printf(\ printf(\ for(i=0;idi_size/BLOCKSIZ+1;i++) printf(\ printf(\ } else printf(\ iput(temp_inode); } } }

mkdir(char *dirname) { int dirid,dirpos; struct inode *inode; struct direct buf[BLOCKSIZ/(DIRSIZ+2)]; unsigned int block; dirid=namei(dirname); if(dirid!=-1)//dirid==-1表示没有该目录名存在; { inode=iget(dirid); if(inode->di_mode&DIDIR)

printf(\ else printf(\is a file name&can not creat a dir the same name\ iput(inode); return 0; } dirpos=iname(dirname); inode=ialloc(); inode->i_ino=dirpos; dir.direct[dirpos].d_ino=inode->i_ino; dir.size++; strcpy(buf[0].d_name,\ buf[0].d_ino=dirid; strcpy(buf[1].d_name,\ buf[1].d_ino=cur_path_inode->i_ino; 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; inode->di_uid=user[user_id].u_uid; inode->di_gid=user[user_id].u_gid; inode->di_addr[0]=block; iput(inode); return 0; }

chdir(char *dirname) { int dirid; int temp; struct inode *inode; short block; int i,j,low=0,high=0; dirid=namei(dirname); if(dirid=#include

#include \

delete(char *filename) { unsigned int dinodeid; struct inode *inode; dinodeid=namei(filename); if(dinodeid!=(int)NULL) inode=iget(dinodeid); inode->di_number--; iput(inode); } =-1) { printf(\ return 0; } inode=iget(dirid); if(!access(user_id,inode,user[user_id].u_default_mode)) { printf(\ iput(inode); return 0; } for(i=0;iMAX) break; } dir.direct[j].d_ino=0; } for(i=0;idi_size/BLOCKSIZ+1;i++) { bfree(cur_path_inode->di_addr[i]); } i=dir.size; for(i=0;i

{ block=balloc(); cur_path_inode->di_addr[i]=block; fseek(fd,DATASTART+block*BLOCKSIZ,SEEK_SET); fwrite(&dir.direct[i],1,BLOCKSIZ,fd); } cur_path_inode->di_size=dir.size*(DIRSIZ+2); iput(cur_path_inode); cur_path_inode=inode; i=inode->di_size/BLOCKSIZ+1; j=0; for(i=0;idi_size/BLOCKSIZ+1;i++) { fseek(fd,DATASTART+inode->di_addr[i]*BLOCKSIZ,SEEK_SET); fread(&dir.direct[j],1,BLOCKSIZ,fd); j+=BLOCKSIZ/(DIRSIZ+2); } return 0; }

format.h format() {

struct inode *inode; struct direct dir_buf[BLOCKSIZ/(DIRSIZ+2)]; struct pwd passwd[BLOCKSIZ/(PWDSIZ+4)]; struct filsys filsys; unsigned int block_buf[BLOCKSIZ/sizeof(int)]; char *buf; char *empty; int i,j,k; fd=fopen(\ buf=(char*)malloc((DINODEBLK+FILEBLK+2)*BLOCKSIZ*sizeof(char)); if(fd==NULL) { printf(\ exit(0); } fseek(fd,0,SEEK_SET); fwrite(buf,1,(DINODEBLK+FILEBLK+2)*BLOCKSIZ*sizeof(char),fd);

free(buf);

passwd[0].p_uid=2116; passwd[0].p_gid=03;

strcpy(passwd[0].password,\passwd[1].p_uid=2117; passwd[1].p_gid=03;

strcpy(passwd[1].password,\passwd[2].p_uid=2118; passwd[2].p_gid=04;

strcpy(passwd[2].password,\passwd[3].p_uid=2119; passwd[3].p_gid=04;

strcpy(passwd[3].password,\passwd[4].p_uid=2220; passwd[4].p_gid=05;

strcpy(passwd[4].password,\

inode=iget(0);

inode->di_mode=DIEMPTY; iput(inode);

inode=iget(1);

inode->di_number=1;

inode->di_mode=DEFAULTMODE|DIDIR; inode->di_size=3*(DIRSIZ+2); inode->di_addr[0]=0;

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,\dir_buf[2].d_ino=2;

fseek(fd,DATASTART,SEEK_SET); fwrite(dir_buf,1,3*(DIRSIZ+2),fd);

iput(inode); fseek(fd,1056,SEEK_SET); fread(inode,DINODESIZ,1,fd); inode=iget(1);

iput(inode);

inode=iget(2);

inode->di_number=1;

inode->di_mode=DEFAULTMODE|DIDIR; inode->di_size=3*(DIRSIZ+2); inode->di_addr[0]=1;

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,\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);

inode->di_number=1;

inode->di_mode=DEFAULTMODE|DIFILE; inode->di_size=BLOCKSIZ; inode->di_addr[0]=2; for(i=5;i

fseek(fd,DATASTART+2*BLOCKSIZ,SEEK_SET); fwrite(passwd,1,BLOCKSIZ,fd); iput(inode);

filsys.s_isize=DINODEBLK; filsys.s_fsize=FILEBLK;

filsys.s_ninode=DINODEBLK*BLOCKSIZ/DINODESIZ-4; filsys.s_nfree=FILEBLK-3; for(i=0;i

filsys.s_pinode=0;

filsys.s_rinode=NICINOD+4;

for(i=NICFREE+2;i

{ for(j=0;j

}//当i=502之后,完成文件块502~453的写入; //之后文件块512~503不能进行,需要特殊处理 for(i=503;i<512;i++) block_buf[i-503]=i;

fseek(fd,DATASTART+BLOCKSIZ*503,SEEK_SET);

fwrite(block_buf,1,BLOCKSIZ,fd);//至此,才完成512块文件块的定位

for(i=0;i

filsys.s_free[i]=i+3;//从DATASTART的第一个BLOCK作为MAIN DIRECTORY //第二个BLOCK作为etc目录 //第三个BLOCK作为password文 //故此i要加3

//每50个BLOCK成组,在每个BLOCK组当中的第一个BLOCK(称为地址块)放有整个BLOCK //组的地址。这样,filsys.s_free[0]指向该组的地址块,而filsys.s_free[49]+1 //则指向下一个组的地址块。 j=1; filsys.s_pfree=NICFREE-j; filsys.s_pinode=0; fseek(fd,BLOCKSIZ,SEEK_SET); fwrite(&filsys,1,sizeof(struct filsys),fd); fclose(fd); }

halt.h

#include #include \

halt() { int i,j; for(i=0;i

if(user[i].u_uid!=0) { for(j=0;j

iallfre.h

#include #include \

static struct dinode block_buf1[BLOCKSIZ/DINODESIZ];

struct inode *ialloc() { struct inode* temp_inode; unsigned int cur_di; int i,count,block_end_flag; i=filsys.s_pinode; if(filsys.s_pinode==NICINOD) { i=0; count=0; block_end_flag=1; filsys.s_pinode=NICINOD-1; cur_di=filsys.s_rinode; while((count

fread(block_buf1,1,BLOCKSIZ,fd); block_end_flag=0; i=0; } while(block_buf1[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; } i=filsys.s_inode[filsys.s_pinode]; temp_inode=iget(filsys.s_inode[filsys.s_pinode]); fseek(fd,DINODESTART+filsys.s_inode[filsys.s_pinode]*DINODESIZ,SEEK_SET); fwrite(temp_inode,1,DINODESIZ,fd); filsys.s_pinode++; filsys.s_ninode--; filsys.s_fmod=SUPDATE; return temp_inode; }

ifree(unsigned dinodeid) { filsys.s_ninode++; if(filsys.s_pinode!=NICINOD) { filsys.s_inode[filsys.s_pinode]=dinodeid; filsys.s_pinode++; } else { if(dinodeid

} }

igetput.h

struct inode *iget(unsigned int dinodeid) { int existed=0,inodeid; long addr; struct inode *temp,*newinode; inodeid=dinodeid%NHINO; if(hinode[inodeid].i_forw==NULL) temp=&hinode[inodeid]; else { temp=hinode[inodeid].i_forw; while(temp->i_forw) { if((int)temp->i_ino==inodeid) { existed=1; temp->i_count++; return temp; } else temp=temp->i_forw; }; } addr=DINODESTART+dinodeid*DINODESIZ; newinode=(struct inode*)malloc(sizeof(struct inode)); fseek(fd,addr,SEEK_SET); fread(newinode,DINODESIZ,1,fd); if(hinode[inodeid].i_forw==NULL) { newinode->i_forw=hinode[inodeid].i_forw; hinode[inodeid].i_forw=newinode; newinode->i_back=&(hinode[inodeid]); } else { newinode->i_forw=hinode[inodeid].i_forw; hinode[inodeid].i_forw=newinode; newinode->i_back=newinode->i_forw->i_back;

newinode->i_forw->i_back=newinode; } newinode->i_count=1; newinode->i_flag=0; newinode->i_ino=dinodeid; fseek(fd,addr,SEEK_SET); fwrite(newinode,DINODESIZ,1,fd); return newinode; }

iput(struct inode *pinode) { long addr; unsigned int block_num; int i; if(pinode->i_count>1) { pinode->i_count--; } else { if(pinode->di_number!=0) { addr=DINODESTART+pinode->i_ino*DINODESIZ; fseek(fd,addr,SEEK_SET); fwrite(pinode,DINODESIZ,1,fd); } else { block_num=pinode->di_size/BLOCKSIZ; for(i=0;i<(int)block_num;i++) { balloc(pinode->di_addr[i]); } ifree(pinode->i_ino); }; 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;

}; free(pinode); }; }

install.h install() { int i,j; printf(\ fd=fopen(\ if(fd==NULL) { printf(\ exit(0); } fseek(fd,BLOCKSIZ,SEEK_SET); fread(&filsys,1,sizeof(struct filsys),fd); i=filsys.s_free[49]; for(i=0;idi_size/(DIRSIZ+2); for(i=0;i

} fseek(fd,DATASTART+BLOCKSIZ,SEEK_SET); fread(&dir.direct[BLOCKSIZ/(DIRSIZ+2)],1,3*(DIRSIZ+2),fd); for(i=0;idi_addr[i],SEEK_SET); fread(&dir.direct[(BLOCKSIZ/(DIRSIZ+2))*i],1,cur_path_inode->di_size%BLOCKSIZ,fd); } }

log.h

install() { int i,j; printf(\ fd=fopen(\ if(fd==NULL) { printf(\ exit(0); } fseek(fd,BLOCKSIZ,SEEK_SET); fread(&filsys,1,sizeof(struct filsys),fd); i=filsys.s_free[49]; for(i=0;i

} cur_path_inode=iget(1); dir.size=cur_path_inode->di_size/(DIRSIZ+2); for(i=0;idi_addr[i],SEEK_SET); fread(&dir.direct[(BLOCKSIZ/(DIRSIZ+2))*i],1,cur_path_inode->di_size%BLOCKSIZ,fd); } }

name.h

#include #include #include \

int namei(char *name) { int i,notfound=1; for(i=0;i

short iname(char *name) { int i,notfound=1; for(i=0;((iMAX)

{ notfound=0; break; } if(notfound) { printf(\ return -1; } else { strcpy(dir.direct[i].d_name,name); dir.direct[i].d_ino=1; return i; } }

open.h

#include #include \

unsigned short open(user_id,filename,openmode) int user_id; char *filename;

unsigned short openmode; { unsigned int dinodeid; struct inode *inode; int i,j; dinodeid=namei(filename); if(dinodeid!=(int)NULL) { printf(\ return (int)NULL; } inode=iget(dinodeid); if(!access(user_id,inode,openmode)) { printf(\ iput(inode); return (int)NULL; }

for(i=1;idi_size; else sys_ofile[i].f_off=0; for(j=0;jdi_size/BLOCKSIZ+1;i++) bfree(inode->di_addr[i]); inode->di_size=0; } return j; }

rdwt.h

#include #include \

unsigned int read(int cfd,char *buf,unsigned int size) { unsigned long off; int block,block_off,i,j;

struct inode *inode; char *temp_buf;

inode=sys_ofile[user[user_id].u_ofile[cfd]].f_inode; if(!(sys_ofile[user[user_id].u_ofile[cfd]].f_flag&FREAD)) { printf(\ return 0; }

temp_buf=buf;

off=sys_ofile[user[user_id].u_ofile[cfd]].f_off;

if((off+size)>inode->di_size) size=inode->di_size-off; block_off=off%BLOCKSIZ; block=off/BLOCKSIZ;

if(block_off+sizedi_addr[block]*BLOCKSIZ+block_off,SEEK_SET); fread(buf,1,size,fd); return size; }

fseek(fd,DATASTART+inode->di_addr[block]*BLOCKSIZ+block_off,SEEK_SET); fread(temp_buf,1,BLOCKSIZ-block_off,fd);

temp_buf+=BLOCKSIZ-block_off;

j=(inode->di_size-off-block_off)/BLOCKSIZ; for(i=0;i<(int)(size-block_off)/BLOCKSIZ;i++) { fseek(fd,DATASTART+inode->di_addr[j+i]*BLOCKSIZ,SEEK_SET); fread(temp_buf,1,BLOCKSIZ,fd); temp_buf+=BLOCKSIZ; }

block_off=(size-block_off)%BLOCKSIZ;

block=inode->di_addr[off+size/BLOCKSIZ+1]; fseek(fd,DATASTART+block*BLOCKSIZ,SEEK_SET); fread(temp_buf,1,block_off,fd);

sys_ofile[user[user_id].u_ofile[cfd]].f_off+=size; return size;

}

unsigned int write(int cfd,char *buf,unsigned int size) { unsigned long off; int block,block_off,i; struct inode *inode; char *temp_buf; i=user[user_id].u_ofile[cfd]; inode=sys_ofile[user[user_id].u_ofile[cfd]].f_inode; if(!(sys_ofile[user[user_id].u_ofile[cfd]].f_flag&FWRITE)) { printf(\ return 0; } temp_buf=buf; off=sys_ofile[user[user_id].u_ofile[cfd]].f_off; block_off=off%BLOCKSIZ; block=off/BLOCKSIZ; inode->di_addr[block]=balloc(); if(block_off+sizedi_addr[block]*BLOCKSIZ+block_off,SEEK_SET); fwrite(buf,1,size,fd); return size; } fseek(fd,DATASTART+inode->di_addr[block]*BLOCKSIZ+block_off,SEEK_SET); fwrite(temp_buf,1,BLOCKSIZ-block_off,fd); temp_buf+=BLOCKSIZ-block_off; for(i=0;i<(int)(size-block_off)/BLOCKSIZ-1;i++) { inode->di_addr[block+1+i]=balloc(); fseek(fd,DATASTART+inode->di_addr[block+1+i]*BLOCKSIZ,SEEK_SET); fwrite(temp_buf,1,BLOCKSIZ,fd); temp_buf+=BLOCKSIZ; } //下面是对不够一个block的数据进行处理 block_off=(size-block_off)%BLOCKSIZ; block=inode->di_addr[off+size/BLOCKSIZ+1]=balloc(); fseek(fd,DATASTART+block*BLOCKSIZ,SEEK_SET); fwrite(temp_buf,1,block_off,fd);

2.

sys_ofile[user[user_id].u_ofile[cfd]].f_off+=size; return size; }

程序截图

A. 文件创建(有权限):

B. 文件创建(无权限):

C. 目录创建:

D. 目录删除:

E.

显示目录内容:

本文来源:https://www.bwwdw.com/article/1k6a.html

Top