Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include "type.h"
- int read_file(int fd, int bytes)
- {
- //Handlers for incorrect fd
- if (fd < 0 || fd >= 10) {printf("FD value invalid: 0 to 10 are valid values\n"); return -1;}
- if (running->fd[fd] == NULL) {printf("Given FD is not in open.\n"); return -1;}
- return myread(fd,read_buff,bytes);
- }
- // read nbytes from fd to buf[ ], and returns the actual number of bytes read.
- int myread(int fd,char* m_buff,long nbytes)
- {
- int is_indirect = 0, is_double_indirect = 0, count = 0;
- char indir_buff[1024], dblindir_buff[1024];
- long * indirect, * dblindirect;
- long indblk, dblindblk, eloop = 0;
- // The number of bytes still available in file.
- long avil = running->fd[fd]->inodeptr->INODE.i_size - running->fd[fd]->offset;
- long lbk, startByte = 0, blk;
- while (nbytes && avil) // While there are still bytes in the file
- {
- // 1. Compute LOGICAL BLOCK number lbk and startByte in that block from offset
- lbk = running->fd[fd]->offset / BLOCK_SIZE;
- startByte = running->fd[fd]->offset % BLOCK_SIZE;
- // 2. Get block from 1 of 3 cases:
- if (lbk < 12) // CASE 1: lbk is a direct block
- {
- blk = running->fd[fd]->inodeptr->INODE.i_block[lbk];
- }
- else if (lbk >= 12 && lbk < 256+12) // CASE 2: indirect blocks if over number that an indirect block, indirect.
- {
- if (!is_indirect)
- {
- //set offset for indirect
- get_block(running->fd[fd]->inodeptr->dev,running->fd[fd]->inodeptr->INODE.i_block[12],indir_buff);
- is_indirect = 1;
- }
- indirect = (long *)buf;
- blk = * (indirect+(lbk-12)); //indirect offset
- }
- else // CASE 3: double indirect blocks
- {
- printf("Double indirect\n");
- }
- // 3. get the data block into readbuf[BLKSIZE]
- get_block(running->fd[fd]->inodeptr->dev,blk,read_buff);
- char *cq = buf;
- // 4. copy from startByte to buf[ ], <= amount of remaining bytes in this block
- char *cp = read_buff + startByte; //start of buff
- int remain = BLOCK_SIZE - startByte; // number of bytes remain in readbuf[]
- while (remain > 0)
- {
- printf("%c", *cp);
- *cq++ = *cp++; // copy byte from readbuf[] into buf[]
- running->fd[fd]->offset++; // advance offset
- count++; // inc count as number of bytes read
- avil--; nbytes--; remain--;
- if (nbytes <= 0 || avil <= 0)
- printf("\n");
- break;
- }
- }
- printf("\n");
- return count; // count is the actual number of bytes read
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement