Question: please help me solve this code The ds 3 cat utility prints the contents of a file to standard output. It takes the name of

please help me solve this code
The ds3cat utility prints the contents of a file to standard output. It takes the name of the disk image file and an inode number as the only arguments. It prints the contents of the file that is specified by the inode number.
For this utility, first print the string File blocks with a newline at the end and then print each of the disk block numbers for the file to standard out, one disk block number per line. After printing the file blocks, print an empty line with only a newline.
Next, print the string File data with a newline at the end and then print the full contents of the file to standard out. You do not need to differentiate between files and directories, for this utility everything is considered to be data and should be printed to standard out.
#include
#include
#include
#include
#include
#include "LocalFileSystem.h"
#include "ufs.h"
using namespace std;
LocalFileSystem::LocalFileSystem(Disk *disk){
this->disk = disk;
}
void LocalFileSystem::readDataBitmap(super_t *super, unsigned char *dataBitmap){
for (int i =0; i < super->data_bitmap_len; i++){
disk->readBlock(super->data_bitmap_addr + i, dataBitmap + i * UFS_BLOCK_SIZE);
}
}
void LocalFileSystem::readInodeBitmap(super_t *super, unsigned char *inodeBitmap){
for (int i =0; i < super->inode_bitmap_len; i++){
disk->readBlock(super->inode_bitmap_addr + i, inodeBitmap + i * UFS_BLOCK_SIZE);
}
}
void LocalFileSystem::readSuperBlock(super_t *super){
unsigned char buffer[UFS_BLOCK_SIZE];
disk->readBlock(0, buffer);
memcpy(super, buffer, sizeof(super_t));
}
void LocalFileSystem::readInodeRegion(super_t *super, inode_t *inodes){
int numBlocks = super->inode_region_len;
for (int i =0; i < numBlocks; i++){
int blockIndex = super->inode_region_addr + i;
char buffer[UFS_BLOCK_SIZE];
disk->readBlock(blockIndex, buffer);
memcpy(inodes + i * UFS_BLOCK_SIZE / sizeof(inode_t), buffer, UFS_BLOCK_SIZE);
}
}
int LocalFileSystem::read(int inodeNumber, void *buffer, int size){
if (size <=0) return -EINVALIDSIZE;
inode_t inode;
if (stat(inodeNumber, &inode)!=0) return -EINVALIDINODE;
if (inode.type != UFS_REGULAR_FILE) return -EINVALIDTYPE;
int bytesRead =0;
int remainingSize = min(size, inode.size);
int blockIndex =0;
while (remainingSize >0 && blockIndex < DIRECT_PTRS){
if (inode.direct[blockIndex]== static_cast(-1)){
break;
}
int blockSize = min(remainingSize, UFS_BLOCK_SIZE);
disk->readBlock(inode.direct[blockIndex], static_cast(buffer)+ bytesRead);
bytesRead += blockSize;
remainingSize -= blockSize;
blockIndex++;
}
return bytesRead;
}
int LocalFileSystem::lookup(int parentInodeNumber, string name){
return 0;
}
int LocalFileSystem::stat(int inodeNumber, inode_t *inode){
super_t super;
readSuperBlock(&super);
if (inodeNumber <0|| inodeNumber >= super.num_inodes){
return -EINVALIDINODE;
}
inode_t inodes[super.num_inodes];
readInodeRegion(&super, inodes);
if (inodes[inodeNumber].type ==0){// Assuming 0 represents an invalid inode
return -ENOTALLOCATED;
}
*inode = inodes[inodeNumber];
return 0;
}
int LocalFileSystem::create(int parentInodeNumber, int type, string name){
return 0;
}
int LocalFileSystem::write(int inodeNumber, const void *buffer, int size){
return 0;
}
int LocalFileSystem::unlink(int parentInodeNumber, string name){
return 0;
}

Step by Step Solution

There are 3 Steps involved in it

1 Expert Approved Answer
Step: 1 Unlock blur-text-image
Question Has Been Solved by an Expert!

Get step-by-step solutions from verified subject matter experts

Step: 2 Unlock
Step: 3 Unlock

Students Have Also Explored These Related Databases Questions!