This repository has been archived on 2022-02-08. You can view files and clone it, but cannot push or open issues or pull requests.
ext2-rs/src/fs.rs

53 lines
1.1 KiB
Rust
Raw Normal View History

2018-03-19 02:39:46 -05:00
use error::Error;
use buffer::Buffer;
use sys::superblock::Superblock;
/// Safe wrapper for raw sys structs
pub struct Ext2<B: Buffer<u8>> {
buffer: B,
superblock: Option<(Superblock, usize)>,
}
impl<B: Buffer<u8>> Ext2<B>
where
Error: From<B::Error>,
{
2018-03-19 02:39:46 -05:00
pub fn new(buffer: B) -> Ext2<B> {
Ext2 {
buffer,
superblock: None,
}
}
pub fn init(&mut self) -> Result<(), Error> {
let superblock = Superblock::find(&self.buffer);
match superblock {
Ok(sb) => Ok(self.superblock = Some(sb)),
Err(err) => Err(err),
}
}
}
2018-03-19 07:35:00 -05:00
#[cfg(test)]
mod tests {
use std::fs::File;
use std::cell::RefCell;
use buffer::Buffer;
2018-03-19 07:35:00 -05:00
use super::Ext2;
#[test]
fn file_len() {
let file = RefCell::new(File::open("ext2.bin").unwrap());
assert_eq!(unsafe { file.slice_unchecked(1024..2048).len() }, 1024);
}
2018-03-19 07:35:00 -05:00
#[test]
fn file() {
let file = RefCell::new(File::open("ext2.bin").unwrap());
let mut fs = Ext2::new(file);
assert_eq!(Ok(()), fs.init());
2018-03-19 07:35:00 -05:00
}
}