Skip to content

Commit

Permalink
manage modules and naming conventions
Browse files Browse the repository at this point in the history
  • Loading branch information
BijanRegmi committed May 3, 2023
1 parent 4e1b039 commit 082ba82
Show file tree
Hide file tree
Showing 10 changed files with 95 additions and 71 deletions.
19 changes: 11 additions & 8 deletions src/disk.rs
Original file line number Diff line number Diff line change
@@ -1,10 +1,11 @@
use std::io::{Read, Seek};

use crate::ext4_structs::{self, Ext4GroupDesc, Ext4Inode, LoadAble};
use crate::ext4;
use crate::ext4::LoadAble;

pub struct Disk {
file: std::fs::File,
pub super_block: ext4_structs::Ext4SuperBlock,
pub super_block: ext4::structs::SuperBlock,
pub block_size: u64,
pub groups_per_flex: u32,
}
Expand All @@ -13,7 +14,7 @@ impl Disk {
pub fn new(path: &str) -> Self {
let mut f = std::fs::File::open(path).expect("Failed to open file.");

let sb = ext4_structs::Ext4SuperBlock::from_file_offset(&mut f, 0x400)
let sb = ext4::structs::SuperBlock::from_file_offset(&mut f, 0x400)
.expect("Failed to read superblock");
let bs: u64 = (2 as u64).pow(10 + sb.s_log_block_size);
let gpf: u32 = (1 as u32) << sb.s_log_groups_per_flex;
Expand All @@ -26,6 +27,7 @@ impl Disk {
}
}

#[allow(dead_code)]
pub fn read_block(&mut self, block_num: u64) -> std::io::Result<Vec<u8>> {
let mut buf = vec![0u8; self.block_size as usize];
self.file
Expand All @@ -34,25 +36,26 @@ impl Disk {
Ok(buf)
}

pub fn get_group_desc(&mut self, group_num: u32) -> Ext4GroupDesc {
pub fn get_group_desc(&mut self, group_num: u32) -> ext4::structs::GroupDesc {
let primary_group_in_flex = group_num - (group_num % self.groups_per_flex);
let block_no = primary_group_in_flex * self.super_block.s_blocks_per_group
+ self.super_block.s_first_data_block
+ 1;
ext4_structs::Ext4GroupDesc::from_file_offset(
ext4::structs::GroupDesc::from_file_offset(
&mut self.file,
(block_no as u64) * (self.block_size as u64),
)
.expect("Cannot read group desc")
}

#[allow(dead_code)]
pub fn block_group_has_superblock(&self, bg_num: u32) -> bool {
if bg_num == 0 {
return true;
} else if self
.super_block
.s_feature_compat
.contains(ext4_structs::Ext4CompatibleFeatures::SPARSE_SUPER2)
.contains(ext4::flags::superblock::CompatibleFeatures::SPARSE_SUPER2)
{
unimplemented!("Check s_backup_bgs")
} else if bg_num <= 1 || !self.super_block.has_sparse_super_feature() {
Expand All @@ -77,7 +80,7 @@ impl Disk {
}
}

pub fn get_inode(&mut self, inode_num: u32) -> Ext4Inode {
pub fn get_inode(&mut self, inode_num: u32) -> ext4::structs::Inode {
// Block group that an inode lives in
let bg = (inode_num - 1) / self.super_block.s_inodes_per_group;
// Get group desc of bg block number
Expand All @@ -88,7 +91,7 @@ impl Disk {
let inode_index_in_table = (inode_num - 1) % self.super_block.s_inodes_per_group;
let inode_offset_in_table = inode_index_in_table * self.super_block.s_inode_size as u32;
let inode_address = inode_table_address * self.block_size + inode_offset_in_table as u64;
ext4_structs::Ext4Inode::from_file_offset(&mut self.file, inode_address)
ext4::structs::Inode::from_file_offset(&mut self.file, inode_address)
.expect("Failed to get inode")
}
}
14 changes: 7 additions & 7 deletions src/ext4_structs/directories.rs → src/ext4/directories.rs
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
use super::loadable::LoadAble;
use super::LoadAble;
use bitflags::bitflags;

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

Expand All @@ -17,7 +17,7 @@ pub struct Ext4DirEntry {
/// File name.
pub name: [char; 255],
}
impl LoadAble for Ext4DirEntry {}
impl LoadAble for DirEntry {}

bitflags! {
#[derive(Debug)]
Expand All @@ -38,7 +38,7 @@ bitflags! {
#[allow(dead_code)]
#[derive(Debug)]
#[repr(C)]
pub struct Ext4DirEntry2 {
pub struct DirEntry2 {
/// Number of the inode that this directory entry points to.
pub inode: u32,

Expand All @@ -54,16 +54,16 @@ pub struct Ext4DirEntry2 {
/// File name.
pub name: [char; 255],
}
impl LoadAble for Ext4DirEntry2 {}
impl LoadAble for DirEntry2 {}

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

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

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

Expand All @@ -24,12 +24,12 @@ pub struct Ext4ExtentHeader {
/// Generation of the tree. (Used by Lustre, but not standard ext4).
pub eh_generation: u32,
}
impl LoadAble for Ext4ExtentHeader {}
impl LoadAble for ExtentHeader {}

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

Expand All @@ -43,13 +43,13 @@ pub struct Ext4ExtentIdx {

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

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

Expand All @@ -67,13 +67,13 @@ pub struct Ext4Extent {
/// Lower 32-bits of the block number to which this extent points.
pub ee_start_lo: u32,
}
impl LoadAble for Ext4Extent {}
impl LoadAble for Extent {}

#[allow(dead_code)]
#[derive(Debug)]
#[repr(C)]
pub struct Ext4ExtentTail {
pub struct ExtentTail {
/// Checksum of the extent block, crc32c(uuid+inum+igeneration+extentblock)
pub et_checksum: u32,
}
impl LoadAble for Ext4ExtentTail {}
impl LoadAble for ExtentTail {}
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
use bitflags::bitflags;

use super::loadable::LoadAble;
use super::LoadAble;

bitflags! {
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
Expand All @@ -17,7 +17,7 @@ bitflags! {
#[allow(dead_code)]
#[derive(Debug)]
#[repr(C)]
pub struct Ext4GroupDesc {
pub struct GroupDesc {
/// Lower 32-bits of location of block bitmap.
pub bg_block_bitmap_lo: u32,

Expand Down Expand Up @@ -56,7 +56,7 @@ pub struct Ext4GroupDesc {
/// 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,
/// 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,

Expand Down Expand Up @@ -94,4 +94,4 @@ pub struct Ext4GroupDesc {
pub bg_reserved: u32,
}

impl LoadAble for Ext4GroupDesc {}
impl LoadAble for GroupDesc {}
6 changes: 3 additions & 3 deletions src/ext4_structs/inode.rs → src/ext4/inode.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
use super::loadable::LoadAble;
use super::LoadAble;
use bitflags::bitflags;

bitflags! {
Expand Down Expand Up @@ -110,7 +110,7 @@ bitflags! {
#[allow(dead_code)]
#[derive(Debug)]
#[repr(C)]
pub struct Ext4Inode {
pub struct Inode {
/// File mode.
pub i_mode: FileMode,

Expand Down Expand Up @@ -237,4 +237,4 @@ pub struct Ext4Inode {
pub i_projid: u32,
}

impl LoadAble for Ext4Inode {}
impl LoadAble for Inode {}
File renamed without changes.
41 changes: 41 additions & 0 deletions src/ext4/mod.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
mod directories;
mod extents;
mod group_desc;
mod inode;
mod loadable;
mod superblock;

pub mod structs {
pub use crate::ext4::group_desc::GroupDesc;
pub use crate::ext4::inode::Inode;
pub use crate::ext4::superblock::SuperBlock;
pub mod dir {
pub use crate::ext4::directories::{
DirEntry as Entry, DirEntry2 as Entry2, DirEntryHash as EntryHash,
};
}
pub mod extent {
pub use crate::ext4::extents::{
Extent, ExtentHeader as Header, ExtentIdx as Idx, ExtentTail as Tail,
};
}
}

pub mod flags {
pub mod superblock {
pub use crate::ext4::superblock::{
CompatibleFeatures, Ext4Defm, IncompatibleFeatures, ROCompatibleFeatures, OS,
};
}
pub mod group_desc {
pub use crate::ext4::group_desc::GroupFlags;
}
pub mod inode {
pub use crate::ext4::inode::{FileMode, IFlags};
}
pub mod dir {
pub use crate::ext4::directories::FileType;
}
}

pub use self::loadable::LoadAble;
30 changes: 17 additions & 13 deletions src/ext4_structs/superblock.rs → src/ext4/superblock.rs
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
use super::loadable::LoadAble;
use super::LoadAble;
use bitflags::bitflags;

bitflags! {
#[derive(Debug, Clone, Copy, Eq, PartialEq)]
pub struct Ext4Os: u32 {
pub struct OS: u32 {
const LINUX = 0;
const HURD = 1;
const MASIX = 2;
Expand All @@ -12,7 +12,7 @@ bitflags! {
}

#[derive(Debug, Clone, Copy, Eq, PartialEq)]
pub struct Ext4CompatibleFeatures: u32{
pub struct CompatibleFeatures: u32{
const DIR_PREALLOC = 0x0001;
const IMAGIC_INODES = 0x0002;
const HAS_JOURNAL = 0x0004;
Expand All @@ -26,7 +26,7 @@ bitflags! {
}

#[derive(Debug, Clone, Copy, Eq, PartialEq)]
pub struct Ext4IncompatibleFeatures: u32 {
pub struct IncompatibleFeatures: u32 {
const COMPRESSION = 0x0001;
const FILETYPE = 0x0002;
const RECOVER = 0x0004;
Expand All @@ -46,7 +46,7 @@ bitflags! {
}

#[derive(Debug, Clone, Copy, Eq, PartialEq)]
pub struct Ext4ROCompatibleFeatures: u32 {
pub struct ROCompatibleFeatures: u32 {
const SPARSE_SUPER = 0x0001;
const LARGE_FILE = 0x0002;
const BTREE_DIR = 0x0004;
Expand Down Expand Up @@ -96,7 +96,7 @@ bitflags! {
#[allow(dead_code)]
#[derive(Debug)]
#[repr(C)]
pub struct Ext4SuperBlock {
pub struct SuperBlock {
/// Total inode count
pub s_inodes_count: u32,
/// Total block count.
Expand Down Expand Up @@ -149,7 +149,7 @@ pub struct Ext4SuperBlock {
/// Maximum time between checks, in seconds.
pub s_checkinterval: u32,
/// Creator OS.
pub s_creator_os: Ext4Os,
pub s_creator_os: OS,
/// Revision level.
/// 0 -> Original format
/// 1 -> v2 format w/ dynamic inode sizes
Expand All @@ -167,15 +167,15 @@ pub struct Ext4SuperBlock {
/// Compatible feature set flags.
/// Kernel can still read/write this fs even if it doesn’t understand a flag;
/// fsck should not do that.
pub s_feature_compat: Ext4CompatibleFeatures,
pub s_feature_compat: CompatibleFeatures,
/// Incompatible feature set.
/// If the kernel or fsck doesn’t understand one of these bits,
/// it should stop.
pub s_feature_incompat: Ext4IncompatibleFeatures,
pub s_feature_incompat: IncompatibleFeatures,
/// Readonly-compatible feature set.
/// If the kernel doesn’t understand one of these bits,
/// it can still mount read-only.
pub s_feature_ro_compat: Ext4ROCompatibleFeatures,
pub s_feature_ro_compat: ROCompatibleFeatures,
/// 128-bit UUID for volume.
pub s_uuid: [u8; 16],
/// Volume label.
Expand Down Expand Up @@ -341,11 +341,15 @@ pub struct Ext4SuperBlock {
pub s_checksum: u32,
}

impl Ext4SuperBlock {
impl SuperBlock {
pub fn has_sparse_super_feature(&self) -> bool {
self.s_feature_ro_compat
.contains(Ext4ROCompatibleFeatures::SPARSE_SUPER)
.contains(ROCompatibleFeatures::SPARSE_SUPER)
}

// pub fn has_sparse_super_2_feature(&self) -> bool {
// self.s_feature_ro_compat
// }
}

impl LoadAble for Ext4SuperBlock {}
impl LoadAble for SuperBlock {}
13 changes: 0 additions & 13 deletions src/ext4_structs.rs

This file was deleted.

Loading

0 comments on commit 082ba82

Please sign in to comment.