Ext2 image tool - reading files

Programming, for all ages and all languages.
Post Reply
thomasloven
Member
Member
Posts: 89
Joined: Tue Feb 26, 2008 10:47 am
Location: Sweden

Ext2 image tool - reading files

Post by thomasloven »

I'm working on a tool for editing ext2 formated disk images.
So far I got following a path and reading a file working.

Did I get it right?

http://thomasloven.wordpress.com/2010/0 ... -system-2/

http://thomasloven.wordpress.com/2010/0 ... ng-a-file/

/Thomas
User avatar
KotuxGuy
Member
Member
Posts: 96
Joined: Wed Nov 25, 2009 1:28 pm
Location: Somewhere within 10ft of my favorite chubby penguin!

Re: Ext2 image tool - reading files

Post by KotuxGuy »

Well, I didn't look through the whole article, but..

Code: Select all

struct superblock_t{
int num_inodes; //Number of inodes in the filesystem
int num_blocks; //Number of blocks in the filesystem
int num_reserved_blocks; //Number of reserved blocks
int num_free_blocks; //Number of unused blocks
int num_free_inodes; //Number of unused inodes
int group0_first_block; //Block where the first group starts
int block_size; //Size of a block
int fragment_size; //Size of a disk fragment
int blocks_per_group; //Number of blocks in a group
int fragments_per_group; //Number of fragments in a group
int inodes_per_group; //Number of inodes in a group
int last_mounted; //Time of last mount
int last_written; //Time of last write
short mount_count; //Number of mounts
short max_mounts; //Maximum number of mounts allowed
short signature; //Ext2 signature (0xef53)
short FS_state; //File system state flags
short error_method; //Method of handlig errors flag
short version_minor; //Minor version number
int last_consistency_check; //Time of last consistency check
int consistency_check_interval;//Time between forced consistency checks
int creator_OS; //Creating OS flag
int version_major; //Major version number
short SU_UID;//UID who can use reserved blocks
short SU_GID;//GID who can use reserved blocks
int first_unreserved_inode; //First non-reserved inode
short inode_struct_size ;//Size of each inode
short this_block_group; //This block group (not the same in every superblock)
int compatible_features; //Compatible features flag
int incompatible_features; //Incompatible features flag
int readonly_features; //Features while read-only flag
char FS_id[16];//File system ID
char volume_name[16]; //Volume name
char last_mount_path[64];//Path where last mounted
int algoritm_usage; //Algorithm usage bitmap
char num_file_blocks; //Number of blocks to preallocate for files
char num_dir_blocks; //Number of blocks to preallocate for directories
short unused0; //2 unused bytes
char journal_id[16]; //Journal ID
int journal_inode; //Journal inode
int journal_device; //Journal device
int orphan_inode_list_head; //Head of orphan inode list
char unused1[788]; //788 unused bytes
};
That should be :

Code: Select all

struct superblock_t{
int num_inodes; //Number of inodes in the filesystem
int num_blocks; //Number of blocks in the filesystem
int num_reserved_blocks; //Number of reserved blocks
int num_free_blocks; //Number of unused blocks
int num_free_inodes; //Number of unused inodes
int group0_first_block; //Block where the first group starts
int block_size; //Size of a block
int fragment_size; //Size of a disk fragment
int blocks_per_group; //Number of blocks in a group
int fragments_per_group; //Number of fragments in a group
int inodes_per_group; //Number of inodes in a group
int last_mounted; //Time of last mount
int last_written; //Time of last write
short mount_count; //Number of mounts
short max_mounts; //Maximum number of mounts allowed
short signature; //Ext2 signature (0xef53)
short FS_state; //File system state flags
short error_method; //Method of handlig errors flag
short version_minor; //Minor version number
int last_consistency_check; //Time of last consistency check
int consistency_check_interval;//Time between forced consistency checks
int creator_OS; //Creating OS flag
int version_major; //Major version number
short SU_UID;//UID who can use reserved blocks
short SU_GID;//GID who can use reserved blocks
int first_unreserved_inode; //First non-reserved inode
short inode_struct_size ;//Size of each inode
short this_block_group; //This block group (not the same in every superblock)
int compatible_features; //Compatible features flag
int incompatible_features; //Incompatible features flag
int readonly_features; //Features while read-only flag
char FS_id[16];//File system ID
char volume_name[16]; //Volume name
char last_mount_path[64];//Path where last mounted
int algoritm_usage; //Algorithm usage bitmap
char num_file_blocks; //Number of blocks to preallocate for files
char num_dir_blocks; //Number of blocks to preallocate for directories
short unused0; //2 unused bytes
char journal_id[16]; //Journal ID
int journal_inode; //Journal inode
int journal_device; //Journal device
int orphan_inode_list_head; //Head of orphan inode list
char unused1[788]; //788 unused bytes
} __attribute__( ( packed ) );
The problem is, without __attribute__( ( packed ) ), GCC ( or whatever compiler you're using ) will use word alignment on the structure.
Wildly oversimplified, word alignment makes all the variables in a structure align on a 4 byte boundary.
ext2( and all other FSes ) do not conform to word alignment. If you don't turn off word alignment on a structure intended for a FS, regardless of
whatever FS it is, other disk image tools( and other OSes ) won't be able to read your FS with word alignment.

I noticed a similar problem with other structures in your tool.

This link should be helpful with explaining word alignment: http://www.cs.umd.edu/class/sum2003/cms ... igned.html
Give a man Linux, you feed the nearest optician ( Been staring at the PC too long again? ).
Give a man OS X, you feed the nearest NVidia outlet ( I need more GPU power!! )
Give a man Windows, you feed the entire Tylenol company ( Self explanatory :D )
thomasloven
Member
Member
Posts: 89
Joined: Tue Feb 26, 2008 10:47 am
Location: Sweden

Re: Ext2 image tool - reading files

Post by thomasloven »

Ah.
Of course you're right.
This actually explains some problems I've had. Thanks!

I also realized I forgot to explain how directories work, so that's added to the first part.

/Thomas
User avatar
KotuxGuy
Member
Member
Posts: 96
Joined: Wed Nov 25, 2009 1:28 pm
Location: Somewhere within 10ft of my favorite chubby penguin!

Re: Ext2 image tool - reading files

Post by KotuxGuy »

Glad to help! :)
Give a man Linux, you feed the nearest optician ( Been staring at the PC too long again? ).
Give a man OS X, you feed the nearest NVidia outlet ( I need more GPU power!! )
Give a man Windows, you feed the entire Tylenol company ( Self explanatory :D )
User avatar
Solar
Member
Member
Posts: 7615
Joined: Thu Nov 16, 2006 12:01 pm
Location: Germany
Contact:

Re: Ext2 image tool - reading files

Post by Solar »

KotuxGuy wrote:The problem is, without __attribute__( ( packed ) ), GCC ( or whatever compiler you're using ) will use word alignment on the structure.
If you are like me, you frown at compiler-specific statements in your source code. In this case, you could also consider GCC's "-fpack-struct" command line option, which has the same effect (on a complete translation unit).
Every good solution is obvious once you've found it.
thomasloven
Member
Member
Posts: 89
Joined: Tue Feb 26, 2008 10:47 am
Location: Sweden

Re: Ext2 image tool - reading files

Post by thomasloven »

Also, source code has now been posted

http://thomasloven.wordpress.com/2010/0 ... le-source/
Post Reply