How to read contents of the Super Block ext2 — oracle-tech

    Forum Stats

  • 3,702,038 Users
  • 2,239,552 Discussions
  • 7,835,729 Comments

Discussions

How to read contents of the Super Block ext2

there I'm working on traversing an ext2 filesystem to read from it and I'm stumped at the first hurdle. The filesystem has a block size of 1024 bytes, so I need to read from the first 1024 bytes into an array to get Block Group 0. From there I need to go to the position of the super block, but I don't know how to go through the following first steps:

1 Find the Group Descriptor 2 Read the Inode Table Pointer value to find the block containing the first inode 3 Read Inode 2 (the root directory/ top of the filesystem) -- the length of each inode is given in the Group Descriptor 4 Traverse the filesystem as shown above to locate the file and its contents/ data blocks

I have some code below that reads the file in one go.

<span class="pln" style="background: transparent;">   </span><span class="kwd" style="color: #00008b; background: transparent;">try</span><span class="pln" style="background: transparent;"> </span><span class="pun" style="background: transparent;">{</span><span class="pln" style="background: transparent;"> aFile </span><span class="pun" style="background: transparent;">=</span><span class="pln" style="background: transparent;"> </span><span class="kwd" style="color: #00008b; background: transparent;">new</span><span class="pln" style="background: transparent;"> </span><span class="typ" style="color: #2b91af; background: transparent;">RandomAccessFile</span><span class="pun" style="background: transparent;">(</span><span class="str" style="color: #800000; background: transparent;">"ext2fs"</span><span class="pun" style="background: transparent;">,</span><span class="pln" style="background: transparent;"> </span><span class="str" style="color: #800000; background: transparent;">"r"</span><span class="pun" style="background: transparent;">);</span><span class="pln" style="background: transparent;"> </span><span class="typ" style="color: #2b91af; background: transparent;">FileChannel</span><span class="pln" style="background: transparent;"> fileChannel </span><span class="pun" style="background: transparent;">=</span><span class="pln" style="background: transparent;"> aFile</span><span class="pun" style="background: transparent;">.</span><span class="pln" style="background: transparent;">getChannel</span><span class="pun" style="background: transparent;">();</span><span class="pln" style="background: transparent;"> </span><span class="kwd" style="color: #00008b; background: transparent;">long</span><span class="pln" style="background: transparent;"> fsize </span><span class="pun" style="background: transparent;">=</span><span class="pln" style="background: transparent;"> fileChannel</span><span class="pun" style="background: transparent;">.</span><span class="pln" style="background: transparent;">size</span><span class="pun" style="background: transparent;">();</span><span class="pln" style="background: transparent;"> </span><span class="typ" style="color: #2b91af; background: transparent;">ByteBuffer</span><span class="pln" style="background: transparent;"> buff</span><span class="pun" style="background: transparent;">=</span><span class="typ" style="color: #2b91af; background: transparent;">ByteBuffer</span><span class="pun" style="background: transparent;">.</span><span class="pln" style="background: transparent;">allocate</span><span class="pun" style="background: transparent;">(</span><span class="lit" style="color: #800000; background: transparent;">1024</span><span class="pun" style="background: transparent;">);</span><span class="pln" style="background: transparent;"> fileChannel</span><span class="pun" style="background: transparent;">.</span><span class="pln" style="background: transparent;">read</span><span class="pun" style="background: transparent;">(</span><span class="pln" style="background: transparent;">buff</span><span class="pun" style="background: transparent;">,</span><span class="lit" style="color: #800000; background: transparent;">1024L</span><span class="pun" style="background: transparent;">);</span><span class="pln" style="background: transparent;"> buff</span><span class="pun" style="background: transparent;">.</span><span class="pln" style="background: transparent;">flip</span><span class="pun" style="background: transparent;">();</span><span class="pln" style="background: transparent;"> </span><span class="kwd" style="color: #00008b; background: transparent;">byte</span><span class="pun" style="background: transparent;">[]</span><span class="pln" style="background: transparent;"> data </span><span class="pun" style="background: transparent;">=</span><span class="pln" style="background: transparent;"> buff</span><span class="pun" style="background: transparent;">.</span><span class="pln" style="background: transparent;">array</span><span class="pun" style="background: transparent;">();</span><span class="pln" style="background: transparent;"> </span><span class="typ" style="color: #2b91af; background: transparent;">System</span><span class="pun" style="background: transparent;">.</span><span class="pln" style="background: transparent;">out</span><span class="pun" style="background: transparent;">.</span><span class="pln" style="background: transparent;">format</span><span class="pun" style="background: transparent;">(</span><span class="str" style="color: #800000; background: transparent;">"%s\n"</span><span class="pun" style="background: transparent;">,</span><span class="pln" style="background: transparent;"> </span><span class="kwd" style="color: #00008b; background: transparent;">new</span><span class="pln" style="background: transparent;"> </span><span class="typ" style="color: #2b91af; background: transparent;">String</span><span class="pun" style="background: transparent;">(</span><span class="pln" style="background: transparent;">data</span><span class="pun" style="background: transparent;">));</span><span class="pln" style="background: transparent;"> buff</span><span class="pun" style="background: transparent;">.</span><span class="pln" style="background: transparent;">clear</span><span class="pun" style="background: transparent;">();</span><span class="pln" style="background: transparent;"> fileChannel</span><span class="pun" style="background: transparent;">.</span><span class="pln" style="background: transparent;">close</span><span class="pun" style="background: transparent;">();</span><span class="pln" style="background: transparent;"> aFile</span><span class="pun" style="background: transparent;">.</span><span class="pln" style="background: transparent;">close</span><span class="pun" style="background: transparent;">();</span><span class="pln" style="background: transparent;"> </span><span class="pun" style="background: transparent;">}</span><span class="pln" style="background: transparent;">  </span><span class="kwd" style="color: #00008b; background: transparent;">catch</span><span class="pln" style="background: transparent;"> </span><span class="pun" style="background: transparent;">(</span><span class="typ" style="color: #2b91af; background: transparent;">IOException</span><span class="pln" style="background: transparent;"> e</span><span class="pun" style="background: transparent;">)</span><span class="pln" style="background: transparent;"> </span><span class="pun" style="background: transparent;">{</span><span class="pln" style="background: transparent;"> </span><span class="typ" style="color: #2b91af; background: transparent;">System</span><span class="pun" style="background: transparent;">.</span><span class="pln" style="background: transparent;">out</span><span class="pun" style="background: transparent;">.</span><span class="pln" style="background: transparent;">println</span><span class="pun" style="background: transparent;">(</span><span class="str" style="color: #800000; background: transparent;">"File not found"</span><span class="pun" style="background: transparent;">);</span><span class="pln" style="background: transparent;"> </span><span class="pun" style="background: transparent;">}</span><span class="pln" style="background: transparent;"> here</span>

Thanks

Omar  AbdullWahhab
This discussion has been closed.