How do I read and traverse inodes

Eric Fossum picture Eric Fossum · Jun 9, 2011 · Viewed 7.8k times · Source

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);

Answer

bcr picture bcr · Aug 15, 2011

I'm not totally sure what you're asking, but here goes:

To read the contents of the directory, you'll basically need to look inside its 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:

https://www.nongnu.org/ext2-doc/ext2.html

Also make sure you're clear on the specific structs concerned in your case, which should be provided for you somewhere in the assignment...