⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 myfilelib.c

📁 题目:文件系统的模拟实现 目的:深入理解文件系统的实现机制
💻 C
字号:

#include "myfilelib.h"
#include <errno.h>

void initfilesys(void)
{
    fd = open("myfilesys.dat", O_RDWR);
    open_flag = 1;
}

int Dblock_BtoP(dblock_addr num)
{
    return (SP_bsize + DI_bsize + (num-1) * 1024);
}

int Dinode_BtoP(dinode_addr num)
{   
    return (SP_bsize + (num-1) * 64); 
}

inode Communicate_GetInode1(const char *pathname, int flags, int mode)
{

     int server_fifo_fd, client_fifo_fd;
    struct data_to_pass my_request;
    char client_fifo_name[200],message[200];
    pid_t mypid;

    inode Inode;  

    server_fifo_fd = open(SERVER_FIFO_NAME,O_WRONLY);
    if (server_fifo_fd == -1 ) {
        printf("Can not open SERVER_FIFO\n");
          exit(2);
    }
    
     mypid=getpid();
     sprintf(client_fifo_name,CLIENT_FIFO_NAME,mypid);
     if (mkfifo (client_fifo_name,0777) == -1)
        { printf ("Cann.t mkfifo client_fifo\n");
	  exit(3);
	}

     my_request.index = 1;
     strcpy(my_request.pathname, pathname);
     my_request.flag = flags;
     my_request.mode = mode;
     // sprintf(my_request.text_data,"/root/snake,%d",mypid);

     my_request.client_pid = mypid;
     write(server_fifo_fd,&my_request,sizeof(my_request));
     
     
     
     client_fifo_fd =open(client_fifo_name,O_RDONLY);
     if (client_fifo_fd!=-1) 
        read(client_fifo_fd,&Inode,sizeof(inode));
     
     
     close(client_fifo_fd);   
     unlink(client_fifo_name);

     return Inode;

}

inode Communicate_GetInode2(const char *pathname, int flags)
{
    int server_fifo_fd, client_fifo_fd;
    struct data_to_pass my_request;
    char client_fifo_name[200],message[200];
    pid_t mypid;

    inode Inode;  

    server_fifo_fd = open(SERVER_FIFO_NAME,O_WRONLY);
    if (server_fifo_fd == -1 ) {
        printf("Can not open SERVER_FIFO\n");
	exit(2);
    }
    
    mypid=getpid();
    sprintf(client_fifo_name,CLIENT_FIFO_NAME,mypid);
    if (mkfifo (client_fifo_name,0777) == -1) { 
      printf ("Cann.t mkfifo client_fifo\n");
      exit(3);
    }
    
    my_request.index = 2;
    strcpy(my_request.pathname, pathname);
    my_request.flag = flags;
    
    my_request.client_pid = mypid;
    write(server_fifo_fd,&my_request,sizeof(my_request));
    
    
    
    client_fifo_fd =open(client_fifo_name,O_RDONLY);
    if (client_fifo_fd!=-1) 
        read(client_fifo_fd,&Inode,sizeof(inode));
    
    
    close(client_fifo_fd); 
    unlink(client_fifo_name);
    return Inode;
    
}

int Open(const char *pathname, int flags, ...)
{
    va_list ap;
    inode *p;

    if (open_flag == 0)
      initfilesys();

    p = (inode *) malloc(sizeof(inode));


    if ((flags&O_CREAT) > 0) {
    va_start(ap, flags);

    //    *p = GetInode(pathname, flags,  va_arg(ap, int));
    *p = Communicate_GetInode1(pathname, flags, va_arg(ap, int));

    u_ofile[++FD] = (struct file *) malloc(sizeof(struct file));
    u_ofile[FD]->f_uinode = p;
    u_ofile[FD]->f_off = 0; 

    va_end(ap); 
    } else {
      //   *p = GetInode(pathname, flags);
        *p = Communicate_GetInode2(pathname, flags);
	
	if (p->inumber == 0) 
	    return -1;

	u_ofile[++FD] = malloc(sizeof(struct file));
	u_ofile[FD]->f_uinode = p;
	u_ofile[FD]->f_off = 0; 
    }
    
    
    return FD;
}

int Read(int _fd, char *buf, unsigned int nbytes)
{
    inode *p;
    int realbytes, nblocks, i;
    
    p = u_ofile[_fd]->f_uinode;
    
    if (u_ofile[_fd]->f_off == p->di_size)
        return 0;
    else {
        if (nbytes <= p->di_size)
	    realbytes = nbytes;
	else 
	    realbytes = p->di_size;
	
	nblocks = (realbytes + 1023) / 1024;
	
	for (i = 0; i < nblocks; i++) {
	    lseek(fd, Dblock_BtoP(p->i_a[i]), 0);
	    read(fd, buf, 1024);
	    buf += 1024;
	    u_ofile[_fd]->f_off = 1024;
	}
	
	if (realbytes%1024 != 0) {
	    lseek(fd, Dblock_BtoP(p->i_a[nblocks]), 0);
	    read(fd, buf, realbytes%1024);
	    u_ofile[_fd]->f_off = realbytes%1024;
	}
	return realbytes;
    }
} 

inode Communicate_AdjustDinode(dinode_addr DinodeAddr, unsigned int nbytes)
{

    int server_fifo_fd, client_fifo_fd;
    struct data_to_pass my_request;
    char client_fifo_name[200],message[200];
    pid_t mypid;
    
    inode Inode;  
    
    server_fifo_fd = open(SERVER_FIFO_NAME,O_WRONLY);
    if (server_fifo_fd == -1 ) {
        printf("Can not open SERVER_FIFO\n");
          exit(2);
    }
    
     mypid=getpid();
     sprintf(client_fifo_name,CLIENT_FIFO_NAME,mypid);
     if (mkfifo (client_fifo_name,0777) == -1)
        { printf ("Cann.t mkfifo client_fifo\n");
	  exit(3);
	}

     my_request.index = 3;
     my_request.DinodeAddr = DinodeAddr;
     my_request.nbytes = nbytes;
     
     // sprintf(my_request.text_data,"/root/snake,%d",mypid);
     
     my_request.client_pid = mypid;
     write(server_fifo_fd,&my_request,sizeof(my_request));
     
     client_fifo_fd =open(client_fifo_name,O_RDONLY);
     if (client_fifo_fd!=-1) 
	  read(client_fifo_fd,&Inode,sizeof(inode));
         
     close(client_fifo_fd);   
     unlink(client_fifo_name);
     
     return Inode;

}

int Write(int _fd, char *buf, unsigned int nbytes)
{
    inode *p;
    inode Inode;
    int i, nblocks;
    
    p = u_ofile[_fd]->f_uinode;
    
    //  Inode =  AdjustDinode(p->inumber, nbytes);
    Inode = Communicate_AdjustDinode(p->inumber, nbytes);
    
    free(p);
    p = malloc(sizeof(inode));
    *p = Inode;
    u_ofile[_fd]->f_uinode = p;
    
    nblocks = (nbytes + 1023) / 1024;
    
    for (i = 0; i < nblocks; i++) {
        lseek(fd, Dblock_BtoP(p->i_a[i]), 0);
	write(fd, buf, 1024);
	buf += 1024;
	
    }
    if (nbytes%1024 != 0) {
        lseek(fd, Dblock_BtoP(p->i_a[nblocks]), 0);
	write(fd, buf, nbytes%1024);
    }	  
    
    return nbytes;
}

dinode_addr  Communicate_MakeDir( const char *pathname, unsigned int mode)
{

    int server_fifo_fd, client_fifo_fd;
    struct data_to_pass my_request;
    char client_fifo_name[200],message[200];
    pid_t mypid;
    dinode_addr DinodeAddr;
    

    server_fifo_fd = open(SERVER_FIFO_NAME,O_WRONLY);
    if (server_fifo_fd == -1 ) {
        printf("Can not open SERVER_FIFO\n");
	exit(2);
    }
    
     mypid=getpid();
     sprintf(client_fifo_name,CLIENT_FIFO_NAME,mypid);
     if (mkfifo (client_fifo_name,0777) == -1)
        { printf ("Mkdir():Cann.t mkfifo client_fifo\n");
	  exit(3);
	}

     my_request.index = 4;
     strcpy(my_request.pathname , pathname);

     my_request.mode = mode;

     // sprintf(my_request.text_data,"/root/snake,%d",mypid);
     
     my_request.client_pid = mypid;
     write(server_fifo_fd,&my_request,sizeof(my_request));
     
     client_fifo_fd =open(client_fifo_name,O_RDONLY);
     if (client_fifo_fd!=-1) 
	  read(client_fifo_fd,&DinodeAddr,sizeof(dinode_addr));
         
     close(client_fifo_fd);   
     unlink(client_fifo_name);
     return DinodeAddr;
}

void mymkdir( const char *pathname)
{
    dinode_addr DinodeAddr;
    if (open_flag == 0)
        initfilesys();
    
    // MAKEDIR(pathname, 0144644);
    DinodeAddr =  Communicate_MakeDir(pathname, 0144644);
}


dinode_addr Communicate_LookUp(const char *pathname)
{

    int server_fifo_fd, client_fifo_fd;
    struct data_to_pass my_request;
    char client_fifo_name[200],message[200];
    pid_t mypid;
    dinode_addr DinodeAddr;
    
    my_request.index = 5;
    strcpy(my_request.pathname, pathname);
    
    //  DinodeAddr = LookUp(pathname, 0, 0);     // pathname always exist
    
    server_fifo_fd = open(SERVER_FIFO_NAME,O_WRONLY);
    if (server_fifo_fd == -1 ) {
        printf("Can not open SERVER_FIFO\n");
	exit(2);
    }
    
     mypid=getpid();
     sprintf(client_fifo_name,CLIENT_FIFO_NAME,mypid);
     
     if (mkfifo (client_fifo_name,0777) == -1)
       { printf ("LookUp():Cann.t mkfifo client_fifo\n");
         exit(3);
	}
     
     my_request.client_pid = mypid;
     write(server_fifo_fd,&my_request,sizeof(my_request));
     
     
     
     client_fifo_fd =open(client_fifo_name,O_RDONLY);
     if (client_fifo_fd!=-1) 
	  read(client_fifo_fd,&DinodeAddr,sizeof(dinode_addr));

     close(client_fifo_fd);
     
     close(server_fifo_fd);
     unlink(client_fifo_name);
     
     return DinodeAddr;

}

dinode_addr GetDirInode(const char *pathname)
{
     return Communicate_LookUp(pathname);
}

int SearchDir(const char *pathname)
{
  //  return  LookUp(pathname, 0, 0);    //don't exist return 0
    if (open_flag == 0)
        initfilesys();

    return Communicate_LookUp(pathname);
}

void List(const char *pathname, int flag)
{
    dinode Dinode;
    dinode_addr DinodeAddr;
    direct Direct[64];
    int i, j;
    
    if (open_flag == 0)
        initfilesys();
    
    DinodeAddr = GetDirInode(pathname);
    
    
    lseek(fd, Dinode_BtoP(DinodeAddr), 0);
    read(fd, &Dinode, sizeof(dinode));
    for (i = 0; Dinode.di_addr[i] != 0; i++) {
          lseek(fd, Dblock_BtoP(Dinode.di_addr[i]), 0);
	  read(fd, Direct, 64*sizeof(direct));
	  for (j = 0; Direct[j].DinodeAddr != 0; j++) {
	      lseek(fd, Dinode_BtoP(Direct[j].DinodeAddr), 0);
	      read(fd, &Dinode, sizeof(dinode));
	      if (flag == 0) {        //ls 
		printf("%s\n", Direct[j].d_name);
	      } else {                //ls -l
		printf("%s\t\t\t\t size: %u\t", Direct[j].d_name, Dinode.di_size);
	      if (Dinode.di_mode == 0000644)
		printf("-rw-r--r--\n");
	      else 
		printf("drwxr-xr-x\n");    //0144644

		//	    List(Direct[j].DinodeAddr);
	      }
	  }
    }
    
    
}



void Close(int _fd)
{
  if (_fd != -1) {
      free( u_ofile[_fd]->f_uinode);
      free( u_ofile[_fd] );
  }
}


void Over(void)
{

    int server_fifo_fd;
    struct data_to_pass my_request;
    char client_fifo_name[200],message[200];
    pid_t mypid;
    
    my_request.index = 9;
     
    server_fifo_fd = open(SERVER_FIFO_NAME,O_WRONLY);
    if (server_fifo_fd == -1 ) {
        printf("Can not open SERVER_FIFO\n");
	exit(2);
    }
    
     mypid=getpid();
     sprintf(client_fifo_name,CLIENT_FIFO_NAME,mypid);
     
     if (mkfifo (client_fifo_name,0777) == -1)
       { printf ("LookUp():Cann.t mkfifo client_fifo\n");
         exit(3);
	}
     
     my_request.client_pid = mypid;
     write(server_fifo_fd,&my_request,sizeof(my_request));

     close(server_fifo_fd);

}



⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -