Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do I read and traverse inodes

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);
like image 594
Eric Fossum Avatar asked Jun 09 '11 02:06

Eric Fossum


People also ask

How do you read inode?

There are two commands that can be used to view a file or directory's inode, and they are ls and stat . Both of which are covered below. The ls command is useful for discovering the inode number for a list of files in a directory, while the state command is better suited for single files or directories.

How can I see inode information in Linux?

Handy inode commands on Linux Use the command df-i to pull basic data about your inode usage, including the file system on which the inodes are stored, your total inode count, how many are in use (in count and %), and how many remain. Use -inum to find files associated with a certain inode.

How can an inode file be retrieved?

Each file has an inode containing metadata about the file. An application can retrieve this metadata using stat(2) (or related calls), which returns a stat structure, or statx(2), which returns a statx structure.


2 Answers

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

like image 122
bcr Avatar answered Sep 30 '22 08:09

bcr


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.

like image 37
KZcoding Avatar answered Sep 30 '22 08:09

KZcoding