I've opened the super-block and group descriptor in an EXT2 filesystem, but I don't know how to read for instance the root directory or files in it...
Here's some of what i got
fd=open("/dev/sdb2", O_RDONLY);
lseek(fd, SuperSize, SEEK_SET);
read(fd, &super_block, SuperSize);
lseek(fd, 4096, SEEK_SET);
read(fd, &groupDesc, DescriptSize);
but this next part doesn't seem to work...
lseek(fd, super_block.s_log_block_size*groupDesc.bg_inode_table, SEEK_SET);
lseek(fd, InodeSize*(EXT2_ROOT_INO-1), SEEK_CUR);
read(fd, &root, InodeSize);
I'm not totally clear what you're asking, but here goes:
To read the contents of the directory, you'll basically need to look inside it's pointer block, look at the corresponding blocks on disk specified by the pointers, and read the contents found there to get descriptions of the files in the directory.
That's a pretty high level suggestion, but the rest really comes down to mucking with the details of the system structures themselves.
I'd recommend looking at chapter 4 of this : http://www.nongnu.org/ext2-doc/ext2.html
and also make sure you're clear on the specific structs concerned in your case, which should be provided for you somewhere in the assignment...
The Block Group Descriptor is all you need to traverse an ext file system. The superblock gives you general information about the file system, as well as the location of the block group descriptor (BGD). Once inside the BGD, you have information about each and every block group inside the file system.
To look for the root directory, then you need to look into the FIRST block group, and inspect the second inode; otherwise known as inode number 2. This can be reached from the the location of the first inode, + sizeof(inode). In turn, the location of the first inode can be found inside the BGD entry for the first block group.
Let me know if you need more info.
来源:https://stackoverflow.com/questions/6287606/how-do-i-read-and-traverse-inodes