Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

Please help me solve this question The ds 3 ls utility prints the names for all of the files and directories in a disk image.

Please help me solve this question
The ds3ls utility prints the names for all of the files and directories in a disk image. This utility takes a single command line argument: the name of the disk image file to use. This program will start at the root of the file system, print the contents of that directory in full, and then traverse to each directory contained within. This process repeats in a depth-first fasion until all file and directory names have been printed.
When printing the contents of a directory, first, print the word Directory followed by a space and then the full path for the directory you're printing, and ending it with a newline. Second, print each of the entries in that directory. Sorted each entry using std::strcmp and print them in that order. Each entry will include the inode number, a tab, the name of the entry, and finishing it off with a newline. Third, print a empty line consisting of only a newline to finish printing the contents of the directory.
Make sure that your solution does not print the contents of . and .. subdirectories as this action would lead to an infinitely loop.
After printing a directory, traverse into each subdirectory and repeat the process recursively in a depth-first fashion.
LocalFileSystem.cpp
#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;
}
ds3ls.cpp
#include
#include
#include
#include
#include
#include "Disk.h"
#include "LocalFileSystem.h"
#include "ufs.h"
using namespace std;
int main(int argc, char* argv[]){
if (argc !=2){
cout << "Usage: "<< argv[0]<<" diskImageFile" << endl;
return 1;
}
}

Step by Step Solution

There are 3 Steps involved in it

Step: 1

blur-text-image

Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image

Step: 3

blur-text-image

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Recommended Textbook for

Beginning Apache Cassandra Development

Authors: Vivek Mishra

1st Edition

1484201426, 9781484201428

More Books

Students also viewed these Databases questions