0

**the image has some files on it and I have been able to read the names but I couldn't get the content of all files at once , I have been able to find the root directory content with the inode number but what if the directory changed ? NOTE: I am working on vmware work station and in c language

**

this part shows that I can read root i-node which it's number is 2 I want to read the rest of i-nodes not based on it's number although



    /* show entries in the root directory */

    read_inode(fd, 2, &group, &inode);   /* read inode 2 (root directory) */
    read_dir(fd, &inode, &group);

    close(fd);
    exit(0);
} /* main() */

static 
void read_inode(int fd, int inode_no, const struct ext2_group_desc *group, struct ext2_inode *inode)
{
    lseek(fd, BLOCK_OFFSET(group->bg_inode_table)+(inode_no-1)*sizeof(struct ext2_inode), 
          SEEK_SET);
    read(fd, inode, sizeof(struct ext2_inode));
} /* read_inode() */


static void read_dir(int fd, const struct ext2_inode *inode, const struct ext2_group_desc *group)
{
    void *block;

    if (S_ISDIR(inode->i_mode)) {
        struct ext2_dir_entry_2 *entry;
        unsigned int size = 0;

        if ((block = malloc(block_size)) == NULL) { /* allocate memory for the data block */
            fprintf(stderr, "Memory error\n");
            close(fd);
            exit(1);
        }

        lseek(fd, BLOCK_OFFSET(inode->i_block[0]), SEEK_SET);
        read(fd, block, block_size);                /* read block from disk*/

        entry = (struct ext2_dir_entry_2 *) block;  /* first entry in the directory */
                /* Notice that the list may be terminated with a NULL
                   entry (entry->inode == NULL)*/
        while((size < inode->i_size) && entry->inode) {
            char file_name[EXT2_NAME_LEN+1];
            memcpy(file_name, entry->name, entry->name_len);
            file_name[entry->name_len] = 0;     /* append null character to the file name */
            printf("%10u %s\n", entry->inode, file_name);
            entry = (void*) entry + entry->rec_len;
            size += entry->rec_len;
        }

        free(block);
    }
} /* read_dir() */
rama
  • 1
  • 1
  • 1
    easiest way is to mount it as a loop device and let the kernel do the work for you. But I guess that isn't suitable? – user253751 Jan 23 '23 at 20:24

0 Answers0