Question
Simple Linux File System Implementation using C. a) write the first inode structure (for the root) to the file (sfs.bin), b) jump to the beginning
Simple Linux File System Implementation using C.
a) write the first inode structure (for the root) to the file (sfs.bin),
b) jump to the beginning of the data block to write the two directory entries (dot and dotdot) the first data block,
c) close the file (sfs.bin)
d) open the file (sfs.bin) and read the directory entries dot and dotdot, print their names.
The code I wrote is below. May you please help me to fix mistakes for these parts?
#include #include #include
const int REG_FILE = 0; const int DIRECTORY = 1;
struct super_block{ int inode_bitmap; int data_bitmap[10]; }; struct inode_st{ int type; int size; int data_block_indices[10]; };
struct dir_ent{ char name [28]; unsigned int inode_no; }; void initialize_sb(struct super_block sb){ sb.inode_bitmap = 0; for(int i = 0; i < 10; i++) sb.data_bitmap[i] = 0; } int main() { struct super_block sb; initialize_sb(sb);
struct inode_st root; root.type = DIRECTORY; root.size = sizeof(struct dir_ent)*2; for(int i = 0; i < 10; i++) root.data_block_indices[i] = 0;
sb.inode_bitmap = 1; sb.data_bitmap[0] = 1;
printf("size of dir. entry: %lu ",sizeof(struct dir_ent));
struct dir_ent dot; strcpy(dot.name, "."); dot.inode_no = 0; struct dir_ent dotdot; strcpy(dotdot.name, ".."); dotdot.inode_no = 0;
FILE *sfs = fopen("sfs.bin", "w+"); fwrite(&sb,sizeof(sb),1, sfs); fwrite(&root, sizeof(root.size), 1, sfs); //first inode structure (for the root) to the file fseek(sfs, 0, SEEK_SET); //jump to the beginning of the file fwrite(&dot, sizeof(dot), 1, sfs); //write dot to the first data block fwrite(&dotdot, sizeof(dotdot), 1, sfs); //write dotdot to the first data block fclose(sfs); //close the file fopen("sfs.bin","r+"); //open the existing file again char buff[500]; while(!feof(sfs)) //while it's not the end of the file fgets(buff, 500, sfs); //read the directory entries dot and dotdot puts(buff); //print their names
return 0; }
Step by Step Solution
There are 3 Steps involved in it
Step: 1
Get Instant Access to Expert-Tailored Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started