minor fixes

This commit is contained in:
Szymon Walter 2018-03-18 19:01:31 +01:00
parent 096cc865a4
commit c5f0edeabc
3 changed files with 24 additions and 6 deletions

View file

@ -11,7 +11,6 @@
/// Remember that blocks are numbered starting at 0, and that block numbers /// Remember that blocks are numbered starting at 0, and that block numbers
/// don't usually correspond to physical block addresses. /// don't usually correspond to physical block addresses.
#[repr(C, packed)] #[repr(C, packed)]
#[derive(Clone, Debug, PartialEq, Eq, Hash)]
pub struct BlockGroupDescriptor { pub struct BlockGroupDescriptor {
/// Block address of block usage bitmap /// Block address of block usage bitmap
block_usage_addr: u32, block_usage_addr: u32,

View file

@ -1,5 +1,4 @@
#[repr(C, packed)] #[repr(C, packed)]
#[derive(Clone, Debug, PartialEq, Eq, Hash)]
pub struct Inode { pub struct Inode {
/// Type and Permissions (see below) /// Type and Permissions (see below)
type_perm: u16, type_perm: u16,

View file

@ -47,7 +47,6 @@ pub const OS_LITE: u32 = 4;
/// 512 byte sectors, the Superblock will begin at LBA 2 and will occupy all of /// 512 byte sectors, the Superblock will begin at LBA 2 and will occupy all of
/// sector 2 and 3. /// sector 2 and 3.
#[repr(C, packed)] #[repr(C, packed)]
#[derive(Clone, Debug, PartialEq, Eq, Hash)]
pub struct Superblock { pub struct Superblock {
// taken from https://wiki.osdev.org/Ext2 // taken from https://wiki.osdev.org/Ext2
/// Total number of inodes in file system /// Total number of inodes in file system
@ -163,8 +162,8 @@ impl Superblock {
} }
let superblock: &mut Superblock = unsafe { let superblock: &mut Superblock = unsafe {
let ptr = let ptr = haystack.as_mut_ptr().offset(offset as isize)
haystack.as_ptr().offset(offset as isize) as *mut Superblock; as *mut Superblock;
ptr.as_mut().unwrap() ptr.as_mut().unwrap()
}; };
@ -190,7 +189,7 @@ impl Superblock {
} }
let ptr = unsafe { let ptr = unsafe {
haystack.as_ptr().offset(offset as isize) haystack.as_mut_ptr().offset(offset as isize)
as *mut BlockGroupDescriptor as *mut BlockGroupDescriptor
}; };
let slice = unsafe { slice::from_raw_parts_mut(ptr, count) }; let slice = unsafe { slice::from_raw_parts_mut(ptr, count) };
@ -266,3 +265,24 @@ bitflags! {
const RONLY_BTREE_DIRECTORY = 0x0004; const RONLY_BTREE_DIRECTORY = 0x0004;
} }
} }
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn find() {
let mut buffer = vec![0_u8; 4096];
let addr = &buffer[1024] as *const _ as usize;
// magic
buffer[1024 + 56] = EXT2_MAGIC as u8;
buffer[1024 + 57] = (EXT2_MAGIC >> 8) as u8;
let superblock = Superblock::find(&mut buffer);
assert!(
superblock.is_ok(),
"Err({:?})",
superblock.err().unwrap_or_else(|| unreachable!()),
);
assert_eq!(superblock.unwrap() as *const _ as usize, addr);
}
}