Can you please implement the read functiononly? We need to use Lbread function.
// b_read functions just like its Linux counterpart read. Theuser passes in
// the file descriptor (index into fcbArray), a buffer wherethay want you to
// place the data, and a count of how many bytes they want fromthe file.
// The return value is the number of bytes you have copied intotheir buffer.
// The return value can never be greater then the requestedcount, but it can
// be less only when you have run out of bytes to read. i.e. Endof File
int b_read (b_io_fd fd, char * buffer, int count)
{
//*** TODO ***:
// Write buffered read function to return the data and # bytesread
// You must use LBAread and you must buffer the data inB_CHUNK_SIZE byte chunks.
if (startup == 0) b_init(); //Initialize our system
// check that fd is between 0 and (MAXFCBS-1)
if ((fd < 0) || (fd >= MAXFCBS))
{
return (-1); //invalid file descriptor
}
// and check that the specified FCB is actually in use
if (fcbArray[fd].fi == NULL) //File not open for thisdescriptor
{
return -1;
}
// Your Read code here - the only function you call to get datais LBAread.
// Track which byte in the buffer you are at, and which block inthe file
}
This is the code given .
#include <stdio.h>
#include <unistd.h>
#include <stdlib.h>
#include <string.h>
#include <fcntl.h>
#include "b_io.h"
#include "fsLowSmall.h"
#define MAXFCBS 20 //The maximum number of files open at onetime
// This structure is all the information needed to maintain anopen file
// It contains a pointer to a fileInfo strucutre and any otherinformation
// that you need to maintain your open file.
typedef struct b_fcb
{
fileInfo * fi; //holds the low level systems file info
// Add any other needed variables here to track the individualopen file
} b_fcb;
//static array of file control blocks
b_fcb fcbArray[MAXFCBS];
// Indicates that the file control block array has not beeninitialized
int startup = 0;
// Method to initialize our file system / file controlblocks
// Anything else that needs one time initialization can go inthis routine
void b_init ()
{
if (startup)
return; //already initialized
//init fcbArray to all free
for (int i = 0; i < MAXFCBS; i++)
{
fcbArray.fi = NULL; //indicates a free fcbArray
}
startup = 1;
}
//Method to get a free File Control Block FCB element
b_io_fd b_getFCB ()
{
for (int i = 0; i < MAXFCBS; i++)
{
if (fcbArray.fi == NULL)
{
fcbArray.fi = (fileInfo *)-2; // used but not assigned
return i; //Not thread safe but okay for this project
}
}
return (-1); //all in use
}
// b_open is called by the "user application" to open a file.This routine is
// similar to the Linux open function.
// You will create your own file descriptor which is just aninteger index into an
// array of file control blocks (fcbArray) that you maintain foreach open file.
b_io_fd b_open (char * filename, int flags)
{
if (startup == 0) b_init(); //Initialize our system
//*** TODO ***: Write open function to return your filedescriptor
// You may want to allocate the buffer here as well
// But make sure every file has its own buffer
// This is where you are going to want to call GetFileInfo andb_getFCB
}
// b_read functions just like its Linux counterpart read. Theuser passes in
// the file descriptor (index into fcbArray), a buffer wherethay want you to
// place the data, and a count of how many bytes they want fromthe file.
// The return value is the number of bytes you have copied intotheir buffer.
// The return value can never be greater then the requestedcount, but it can
// be less only when you have run out of bytes to read. i.e. Endof File
int b_read (b_io_fd fd, char * buffer, int count)
{
//*** TODO ***:
// Write buffered read function to return the data and # bytesread
// You must use LBAread and you must buffer the data inB_CHUNK_SIZE byte chunks.
if (startup == 0) b_init(); //Initialize our system
// check that fd is between 0 and (MAXFCBS-1)
if ((fd < 0) || (fd >= MAXFCBS))
{
return (-1); //invalid file descriptor
}
// and check that the specified FCB is actually in use
if (fcbArray[fd].fi == NULL) //File not open for thisdescriptor
{
return -1;
}
// Your Read code here - the only function you call to get datais LBAread.
// Track which byte in the buffer you are at, and which block inthe file
}
// b_close frees and allocated memory and places the filecontrol block back
// into the unused pool of file control blocks.
int b_close (b_io_fd fd)
{
//*** TODO ***: Release any resources
}
Can you please implement the read function only? We need to use Lbread function. // b_read functions just like its Linu
-
answerhappygod
- Site Admin
- Posts: 899604
- Joined: Mon Aug 02, 2021 8:13 am
Can you please implement the read function only? We need to use Lbread function. // b_read functions just like its Linu
Join a community of subject matter experts. Register for FREE to view solutions, replies, and use search function. Request answer by replying!