2017-09-23 06:22:31 +08:00
|
|
|
use std::cmp;
|
2017-09-23 04:27:39 +08:00
|
|
|
use std::io::prelude::*;
|
2017-09-23 06:22:31 +08:00
|
|
|
use std::io::SeekFrom;
|
2017-09-23 04:27:39 +08:00
|
|
|
use std::io;
|
2017-09-23 05:20:06 +08:00
|
|
|
|
2017-09-24 01:42:09 +08:00
|
|
|
use fs::FatSharedStateRef;
|
2017-09-23 04:27:39 +08:00
|
|
|
|
|
|
|
#[allow(dead_code)]
|
|
|
|
pub struct FatFile {
|
|
|
|
first_sector: u32,
|
|
|
|
size: u32,
|
|
|
|
offset: u32,
|
2017-09-24 01:42:09 +08:00
|
|
|
state: FatSharedStateRef,
|
2017-09-23 04:27:39 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
impl FatFile {
|
2017-09-24 01:42:09 +08:00
|
|
|
pub(crate) fn new(first_cluster: u32, size: u32, state: FatSharedStateRef) -> FatFile {
|
|
|
|
let first_sector = state.borrow().sector_from_cluster(first_cluster);
|
2017-09-23 04:27:39 +08:00
|
|
|
FatFile {
|
2017-09-24 01:42:09 +08:00
|
|
|
first_sector, size, state, offset: 0,
|
2017-09-23 04:27:39 +08:00
|
|
|
}
|
|
|
|
}
|
2017-09-24 01:42:09 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
impl Read for FatFile {
|
|
|
|
fn read(&mut self, buf: &mut [u8]) -> io::Result<usize> {
|
|
|
|
let (offset, read_size) = {
|
|
|
|
let state = self.state.borrow();
|
|
|
|
let offset = state.offset_from_sector(self.first_sector) + self.offset as u64;
|
|
|
|
let mut read_size = cmp::min((self.size - self.offset) as usize, buf.len());
|
|
|
|
// FIXME: allow only one cluster for now
|
|
|
|
read_size = cmp::min(read_size, (state.get_cluster_size() - self.offset) as usize);
|
|
|
|
(offset, read_size)
|
|
|
|
};
|
|
|
|
let mut state = self.state.borrow_mut();
|
|
|
|
state.rdr.seek(SeekFrom::Start(offset))?;
|
|
|
|
let size = state.rdr.read(&mut buf[..read_size])?;
|
|
|
|
self.offset += size as u32;
|
2017-09-23 04:27:39 +08:00
|
|
|
Ok(size)
|
|
|
|
}
|
|
|
|
}
|