write a c program showblock which displays the disk blocks of a file in an ext2 file system

write a c program showblock which displays the disk blocks of a file in an ext2 file system

the website link

https://www.eecs.wsu.edu/~cs360/LAB6.html

 
 Write a C program, showblock, which displays the disk blocks of a file 
 in an EXT2 file system. The program runs as follows

       showblock  DEVICE    PATHNAME
       ---------  -------   ----------
e.g.   showblock  diskimage  /a/b/c/d  (diskimage contains an EXT2 FS)

 It locates the file named PATHNAME and prints the disk blocks (direct, 
 indirect, double-indirect) of the file.
**************************************************************************/ 

               HOWTO Traverse EXT2 File System Tree

1. How to traverse the EXT2 FS tree:

   Given a device, mydisk, containing an ext2 FS, and a pathname, e.g.
                   /cs360/is/fun
   of a file, find the file.

   NOTE!!! To find a file amounts to finding its INODE. 
           From its inode, you have ALL the information of a file.
  
2. ALGORITHM:

(1). Open disk for READ ==> file descriptor fd as dev number
          You already have a    get_block(fd, blk, buf[  ]) function.
 
(2). Tokenize pathname into components, e.g. /cs360/is/fun ==>

     char *name[0]  name[1]  name[2];  with n = 3
            |        |        |
           "cs360"  "is"     "fun"

(3). The following C code prints the entries of a DIRectory INODE:

   INODE *ip -> INODE structure of a DIRectory

   char dbuf[BLKSIZE], temp[256];
   DIR *dp;
   char *cp;
   int i;
   int dev = opened fd of disk

   for (i=0; i < 12; i++){  // assume at most 12 direct blocks
       if (ip->i_block[i] == 0)
          break;
       get_block(dev, ip->i_block[i], dbuf);
       dp = (DIR *)dbuf;
       cp = dbuf;

       while (cp < dbuf + BLKSIZE){
          strncpy(temp, dp->name, dp->name_len);
          temp[dp->name_len] = 0;
          printf("%4d %4d %4d %sn", 
	          dp->inode, dp->rec_len, dp->name_len, temp);

          cp += dp->rec_len;
          dp = (DIR *)cp;
       }
   }

   Modify it to wrtie a search() function

          int search(INODE *ip, char *name)
  
   which searches the DIrectory's data blocks for a name string; 
         return its inode number if found; 0 if not.

(4). Start from the root inode #2: (YOU should already know HOW to do this) 

         INODE *ip->root inode;

         int ino, blk, offset;
         int iblk = inodes_start_block number (YOU should also know HOW)
         char ibuf[BLKSIZE];

         for (i=0; i < n; i++){
             ino = search(ip, name[i]);
             if (ino==0){
                printf("can't find %sn", name[i]); exit(1);
             }
  
             // Mailman's algorithm: Convert (dev, ino) to inode pointer
             blk    = (ino - 1) / 8 + iblk;  // disk block contain this INODE 
             offset = (ino - 1) % 8;         // offset of INODE in this block
             get_block(dev, blk, ibuf[ ]);
             ip = (INODE *)ibuf + offset;    // ip -> new INODE
         }
  
(5). When the above for loop ends, ip MUST point at the INODE of pathname.

(6). Extract information from ip-> as required:

       Print direct block numbers;
       Print indirect block numbers; 
       Print double indirect block numbers, if any

     Pat yourself on the back and say: Good Job!
(7). SAMPLES SOLUTION in samples/LAB6/:
             showblock.bin  diskimage

 
Do you need a similar assignment done for you from scratch? We have qualified writers to help you. We assure you an A+ quality paper that is free from plagiarism. Order now for an Amazing Discount!
Use Discount Code "Newclient" for a 15% Discount!

NB: We do not resell papers. Upon ordering, we do an original paper exclusively for you.