Skip to content

Commit

Permalink
add comments and define enums
Browse files Browse the repository at this point in the history
  • Loading branch information
BijanRegmi committed May 1, 2023
1 parent d43cc0b commit 4148aa7
Show file tree
Hide file tree
Showing 5 changed files with 392 additions and 7 deletions.
13 changes: 9 additions & 4 deletions src/ext4_structs.rs
Original file line number Diff line number Diff line change
@@ -1,8 +1,13 @@
mod block_group_descriptor;
mod directories;
mod extents;
mod inode;
mod loadable;
mod superblock;
pub use self::block_group_descriptor::Ext4GroupDesc;
pub use self::inode::Ext4Inode;
pub use self::loadable::LoadAble;
pub use self::superblock::Ext4SuperBlock;

pub use block_group_descriptor::Ext4GroupDesc;
pub use directories::{Ext4DirEntry, Ext4DirEntry2, Ext4DirEntryHash};
pub use extents::{Ext4Extent, Ext4ExtentHeader, Ext4ExtentIdx, Ext4ExtentTail};
pub use inode::Ext4Inode;
pub use loadable::LoadAble;
pub use superblock::Ext4SuperBlock;
39 changes: 38 additions & 1 deletion src/ext4_structs/block_group_descriptor.rs
Original file line number Diff line number Diff line change
Expand Up @@ -22,23 +22,60 @@ pub struct Ext4GroupDesc {
/// Lower 16-bits of directory count.
pub bg_used_dirs_count_lo: u16,

pub bg_flags: u16,
/// Lower 32-bits of location of snapshot exclusion bitmap.
pub bg_exclude_bitmap_lo: u32,

/// Lower 16-bits of the block bitmap checksum.
pub bg_block_bitmap_csum_lo: u16,

/// Lower 16-bits of the inode bitmap checksum.
pub bg_inode_bitmap_csum_lo: u16,

/// Lower 16-bits of unused inode count. If set, we needn’t scan past the (sb.s_inodes_per_group - gdt.bg_itable_unused) th entry in the inode table for this group.
pub bg_itable_unused_lo: u16,

/// Group descriptor checksum; crc16(sb_uuid+group_num+bg_desc) if the RO_COMPAT_GDT_CSUM feature is set, or crc32c(sb_uuid+group_num+bg_desc) & 0xFFFF if the RO_COMPAT_METADATA_CSUM feature is set. The bg_checksum field in bg_desc is skipped when calculating crc16 checksum, and set to zero if crc32c checksum is used.
pub bg_checksum: u16,

/// Upper 32-bits of location of block bitmap.
pub bg_block_bitmap_hi: u32,

/// Upper 32-bits of location of inodes bitmap.
pub bg_inode_bitmap_hi: u32,

/// Upper 32-bits of location of inodes table.
pub bg_inode_table_hi: u32,

/// Upper 16-bits of free block count.
pub bg_free_blocks_count_hi: u16,

/// Upper 16-bits of free inode count.
pub bg_free_inodes_count_hi: u16,

/// Upper 16-bits of directory count.
pub bg_used_dirs_count_hi: u16,

/// Upper 16-bits of unused inode count.
pub bg_itable_unused_hi: u16,

/// Upper 32-bits of location of snapshot exclusion bitmap.
pub bg_exclude_bitmap_hi: u32,

/// Upper 16-bits of the block bitmap checksum.
pub bg_block_bitmap_csum_hi: u16,

/// Upper 16-bits of the inode bitmap checksum.
pub bg_inode_bitmap_csum_hi: u16,

/// Padding to 64 bytes.
pub bg_reserved: u32,

/// Block group flags.
/// Value Description
/// 0x1 inode table and bitmap are not initialized (EXT4_BG_INODE_UNINIT).
/// 0x2 block bitmap is not initialized (EXT4_BG_BLOCK_UNINIT).
/// 0x3 inode table is zeroed (EXT4_BG_INODE_ZEROED).
pub bg_flags: u16,
}

impl LoadAble for Ext4GroupDesc {}
68 changes: 68 additions & 0 deletions src/ext4_structs/directories.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,68 @@
use super::loadable::LoadAble;

#[allow(dead_code)]
#[derive(Debug)]
#[repr(C)]
pub struct Ext4DirEntry {
/// Number of the inode that this directory entry points to.
pub inode: u32,

/// Length of this directory entry. Must be a multiple of 4.
pub rec_len: u16,

/// Length of the file name.
pub name_len: u16,

/// File name.
pub name: [char; 255],
}
impl LoadAble for Ext4DirEntry {}

#[allow(dead_code)]
#[derive(Debug)]
#[repr(u8)]
pub enum FileType {
Ext4FtUnknown = 0,
Ext4FtRegFile = 1,
Ext4FtDir = 2,
Ext4FtChrdev = 3,
Ext4FtBlkdev = 4,
Ext4FtFifo = 5,
Ext4FtSock = 6,
Ext4FtSymlink = 7,
Ext4FtMax = 8,
Ext4FtDirCsum = 0xDE,
}

#[allow(dead_code)]
#[derive(Debug)]
#[repr(C)]
pub struct Ext4DirEntry2 {
/// Number of the inode that this directory entry points to.
pub inode: u32,

/// Length of this directory entry.
pub rec_len: u16,

/// Length of the file name.
pub name_len: u8,

/// File type code
pub file_type: FileType,

/// File name.
pub name: [char; 255],
}
impl LoadAble for Ext4DirEntry2 {}

#[allow(dead_code)]
#[derive(Debug)]
#[repr(C)]
pub struct Ext4DirEntryHash {
/// The hash of the directory name
pub hash: u32,

/// The minor hash of the directory name
pub minor_hash: u32,
}
impl LoadAble for Ext4DirEntryHash {}
79 changes: 79 additions & 0 deletions src/ext4_structs/extents.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,79 @@
use super::loadable::LoadAble;

#[allow(dead_code)]
#[derive(Debug)]
#[repr(C)]
pub struct Ext4ExtentHeader {
/// Magic number, 0xF30A.
pub eh_magic: u16,

/// Number of valid entries following the header.
pub eh_entries: u16,

/// Maximum number of entries that could follow the header.
pub eh_max: u16,

/// Depth of this extent node in the extent tree.
/// 0 = this extent node points to data blocks;
/// otherwise, this extent node points to other extent nodes.
/// The extent tree can be at most 5 levels deep:
/// a logical block number can be at most 2^32,
/// and the smallest n that satisfies 4*(((blocksize - 12)/12)^n) >= 2^32 is 5.
pub eh_depth: u16,

/// Generation of the tree. (Used by Lustre, but not standard ext4).
pub eh_generation: u32,
}
impl LoadAble for Ext4ExtentHeader {}

#[allow(dead_code)]
#[derive(Debug)]
#[repr(C)]
pub struct Ext4ExtentIdx {
/// This index node covers file blocks from ‘block’ onward.
pub ei_block: u32,

/// Lower 32-bits of the block number of the extent node that is
/// the next level lower in the tree. The tree node pointed to
/// can be either another internal node or a leaf node, described below.
pub ei_leaf_lo: u32,

/// Upper 16-bits of the previous field.
pub ei_leaf_hi: u16,

pub ei_unused: u16,
}
impl LoadAble for Ext4ExtentIdx {}

// Leaf node
#[allow(dead_code)]
#[derive(Debug)]
#[repr(C)]
pub struct Ext4Extent {
/// First file block number that this extent covers.
pub ee_block: u32,

/// Number of blocks covered by extent.
/// If the value of this field is <= 32768, the extent is initialized.
/// If the value of the field is > 32768, the extent is uninitialized
/// and the actual extent length is ee_len - 32768.
/// Therefore, the maximum length of a initialized extent is 32768 blocks,
/// and the maximum length of an uninitialized extent is 32767.
pub ee_len: u16,

/// Upper 16-bits of the block number to which this extent points.
pub ee_start_hi: u16,

/// Lower 32-bits of the block number to which this extent points.
pub ee_start_lo: u32,
}
impl LoadAble for Ext4Extent {}

#[allow(dead_code)]
#[derive(Debug)]
#[repr(C)]
pub struct Ext4ExtentTail {
/// Checksum of the extent block, crc32c(uuid+inum+igeneration+extentblock)
pub et_checksum: u32,
}
impl LoadAble for Ext4ExtentTail {}
Loading

0 comments on commit 4148aa7

Please sign in to comment.