use &Buffer<u8>
instead of &[u8]
on Superblock::find
This commit is contained in:
parent
6b34d3325a
commit
bae7f23b3e
|
@ -1,6 +1,7 @@
|
||||||
use core::mem;
|
use core::mem;
|
||||||
|
|
||||||
use error::Error;
|
use error::Error;
|
||||||
|
use buffer::Buffer;
|
||||||
|
|
||||||
/// Ext2 signature (0xef53), used to help confirm the presence of Ext2 on a
|
/// Ext2 signature (0xef53), used to help confirm the presence of Ext2 on a
|
||||||
/// volume
|
/// volume
|
||||||
|
@ -38,6 +39,7 @@ 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, Copy)]
|
||||||
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
|
||||||
|
@ -143,24 +145,23 @@ pub struct Superblock {
|
||||||
}
|
}
|
||||||
|
|
||||||
impl Superblock {
|
impl Superblock {
|
||||||
pub unsafe fn find<'a>(
|
pub fn find<'a>(
|
||||||
haystack: &'a mut [u8],
|
haystack: &'a Buffer<u8>,
|
||||||
offset: isize,
|
) -> Result<(Superblock, usize), Error> {
|
||||||
) -> Result<&'a mut Superblock, Error> {
|
let offset = 1024;
|
||||||
let offset = (1024 + offset) as usize;
|
|
||||||
let end = offset + mem::size_of::<Superblock>();
|
let end = offset + mem::size_of::<Superblock>();
|
||||||
if haystack.len() < end {
|
if haystack.len() < end {
|
||||||
return Err(Error::OutOfBounds(end));
|
return Err(Error::OutOfBounds(end));
|
||||||
}
|
}
|
||||||
|
|
||||||
let superblock: &mut Superblock = {
|
let superblock = unsafe {
|
||||||
let ptr = haystack.as_mut_ptr().offset(offset as isize)
|
haystack
|
||||||
as *mut Superblock;
|
.slice_unchecked(offset..end)
|
||||||
ptr.as_mut().unwrap()
|
.dynamic_cast::<Superblock>()
|
||||||
};
|
};
|
||||||
|
|
||||||
if superblock.magic != EXT2_MAGIC {
|
if superblock.0.magic != EXT2_MAGIC {
|
||||||
Err(Error::BadMagic(superblock.magic))
|
Err(Error::BadMagic(superblock.0.magic))
|
||||||
} else {
|
} else {
|
||||||
Ok(superblock)
|
Ok(superblock)
|
||||||
}
|
}
|
||||||
|
@ -243,16 +244,14 @@ mod tests {
|
||||||
#[test]
|
#[test]
|
||||||
fn find() {
|
fn find() {
|
||||||
let mut buffer = vec![0_u8; 4096];
|
let mut buffer = vec![0_u8; 4096];
|
||||||
let addr = &buffer[1024] as *const _ as usize;
|
|
||||||
// magic
|
// magic
|
||||||
buffer[1024 + 56] = EXT2_MAGIC as u8;
|
buffer[1024 + 56] = EXT2_MAGIC as u8;
|
||||||
buffer[1024 + 57] = (EXT2_MAGIC >> 8) as u8;
|
buffer[1024 + 57] = (EXT2_MAGIC >> 8) as u8;
|
||||||
let superblock = unsafe { Superblock::find(&mut buffer, 0) };
|
let superblock = Superblock::find(&buffer);
|
||||||
assert!(
|
assert!(
|
||||||
superblock.is_ok(),
|
superblock.is_ok(),
|
||||||
"Err({:?})",
|
"Err({:?})",
|
||||||
superblock.err().unwrap_or_else(|| unreachable!()),
|
superblock.err().unwrap_or_else(|| unreachable!()),
|
||||||
);
|
);
|
||||||
assert_eq!(superblock.unwrap() as *const _ as usize, addr);
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
Reference in a new issue