How do I read and traverse inodes
Asked Answered
S

2

7

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);
Suboceanic answered 9/6, 2011 at 2:52 Comment(2)
Why are you reading the raw partition?Grays
It's an assignment, but I'd also like to know how it worksSuboceanic
M
3

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...

Manas answered 15/8, 2011 at 5:34 Comment(1)
I ended up using that website a ton, but no my teacher is quite lacks on what he provided us... including teaching!Suboceanic
A
1

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.

Aspia answered 15/12, 2012 at 22:59 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.