Advertisement
Guest User

Untitled

a guest
Apr 28th, 2017
64
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 2.37 KB | None | 0 0
  1. #include "type.h"
  2.  
  3. int read_file(int fd, int bytes)
  4. {
  5.     //Handlers for incorrect fd
  6.     if (fd < 0 || fd >= 10) {printf("FD value invalid: 0 to 10 are valid values\n"); return -1;}
  7.  
  8.     if (running->fd[fd] == NULL) {printf("Given FD is not in open.\n"); return -1;}
  9.  
  10.     return myread(fd,read_buff,bytes);
  11. }
  12.  
  13. // read nbytes from fd to buf[ ], and returns the actual number of bytes read.
  14. int myread(int fd,char* m_buff,long nbytes)
  15. {
  16.     int is_indirect = 0, is_double_indirect = 0, count = 0;
  17.     char indir_buff[1024], dblindir_buff[1024];
  18.     long * indirect, * dblindirect;
  19.     long indblk, dblindblk, eloop = 0;
  20.  
  21.     // The number of bytes still available in file.
  22.     long avil = running->fd[fd]->inodeptr->INODE.i_size - running->fd[fd]->offset;
  23.     long lbk, startByte = 0, blk;
  24.  
  25.     while (nbytes && avil)  // While there are still bytes in the file
  26.     {
  27.         // 1. Compute LOGICAL BLOCK number lbk and startByte in that block from offset
  28.         lbk = running->fd[fd]->offset / BLOCK_SIZE;
  29.         startByte = running->fd[fd]->offset % BLOCK_SIZE;
  30.  
  31.         // 2. Get block from 1 of 3 cases:
  32.         if (lbk < 12) // CASE 1: lbk is a direct block
  33.         {
  34.             blk = running->fd[fd]->inodeptr->INODE.i_block[lbk];
  35.         }
  36.         else if (lbk >= 12 && lbk < 256+12) // CASE 2: indirect blocks if over number that an indirect block, indirect.
  37.         {
  38.             if (!is_indirect)
  39.             {
  40.                 //set offset for indirect
  41.                 get_block(running->fd[fd]->inodeptr->dev,running->fd[fd]->inodeptr->INODE.i_block[12],indir_buff);
  42.                 is_indirect = 1;
  43.             }
  44.             indirect = (long *)buf;
  45.             blk = * (indirect+(lbk-12)); //indirect offset
  46.  
  47.         }
  48.         else // CASE 3: double indirect blocks
  49.         {
  50.             printf("Double indirect\n");
  51.         }
  52.  
  53.         // 3. get the data block into readbuf[BLKSIZE]
  54.         get_block(running->fd[fd]->inodeptr->dev,blk,read_buff);
  55.  
  56.         char *cq = buf;
  57.         // 4. copy from startByte to buf[ ], <= amount of remaining bytes in this block
  58.         char *cp = read_buff + startByte; //start of buff
  59.         int remain = BLOCK_SIZE - startByte; // number of bytes remain in readbuf[]
  60.  
  61.         while (remain > 0)
  62.         {
  63.             printf("%c", *cp);
  64.             *cq++ = *cp++;   // copy byte from readbuf[] into buf[]
  65.             running->fd[fd]->offset++;  // advance offset
  66.             count++;            // inc count as number of bytes read
  67.             avil--; nbytes--; remain--;
  68.             if (nbytes <= 0 || avil <= 0)
  69.                 printf("\n");
  70.                 break;
  71.         }
  72.     }
  73.     printf("\n");
  74.     return count; // count is the actual number of bytes read
  75. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement