2017-09-27 20:05:58 +08:00
|
|
|
use core::cell::RefCell;
|
2017-09-27 20:20:52 +08:00
|
|
|
use core::cmp;
|
2017-09-23 02:50:41 +08:00
|
|
|
use std::io::prelude::*;
|
2017-09-27 20:05:58 +08:00
|
|
|
use std::io::{Error, ErrorKind, SeekFrom};
|
2017-09-23 02:50:41 +08:00
|
|
|
use std::io;
|
|
|
|
use byteorder::{LittleEndian, ReadBytesExt};
|
2017-09-23 05:20:06 +08:00
|
|
|
|
2017-10-07 20:56:50 +08:00
|
|
|
use file::File;
|
2017-10-07 21:00:27 +08:00
|
|
|
use dir::{DirRawStream, Dir};
|
2017-11-09 02:59:03 +08:00
|
|
|
use dir_entry::DIR_ENTRY_SIZE;
|
2017-12-02 00:24:02 +08:00
|
|
|
use table::{ClusterIterator, alloc_cluster, read_fat_flags};
|
2017-09-23 02:50:41 +08:00
|
|
|
|
|
|
|
// FAT implementation based on:
|
|
|
|
// http://wiki.osdev.org/FAT
|
|
|
|
// https://www.win.tue.nl/~aeb/linux/fs/fat/fat-1.html
|
|
|
|
|
|
|
|
#[derive(Debug, Copy, Clone, PartialEq)]
|
|
|
|
pub enum FatType {
|
2017-10-02 03:39:11 +08:00
|
|
|
Fat12, Fat16, Fat32,
|
2017-09-23 02:50:41 +08:00
|
|
|
}
|
|
|
|
|
2017-11-09 02:59:03 +08:00
|
|
|
impl FatType {
|
|
|
|
fn from_clusters(total_clusters: u32) -> FatType {
|
|
|
|
if total_clusters < 4085 {
|
|
|
|
FatType::Fat12
|
|
|
|
} else if total_clusters < 65525 {
|
|
|
|
FatType::Fat16
|
|
|
|
} else {
|
|
|
|
FatType::Fat32
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-12-02 00:24:02 +08:00
|
|
|
pub struct FsStatusFlags {
|
|
|
|
pub dirty: bool,
|
|
|
|
pub io_error: bool,
|
|
|
|
}
|
|
|
|
|
2017-09-24 01:42:09 +08:00
|
|
|
pub trait ReadSeek: Read + Seek {}
|
|
|
|
impl<T> ReadSeek for T where T: Read + Seek {}
|
|
|
|
|
2017-10-09 20:59:52 +08:00
|
|
|
pub trait ReadWriteSeek: Read + Write + Seek {}
|
|
|
|
impl<T> ReadWriteSeek for T where T: Read + Write + Seek {}
|
|
|
|
|
2017-09-23 02:50:41 +08:00
|
|
|
#[allow(dead_code)]
|
2017-09-24 09:08:00 +08:00
|
|
|
#[derive(Default, Debug, Clone)]
|
2017-11-09 02:41:05 +08:00
|
|
|
struct BiosParameterBlock {
|
2017-09-23 02:50:41 +08:00
|
|
|
bytes_per_sector: u16,
|
|
|
|
sectors_per_cluster: u8,
|
2017-10-02 00:13:59 +08:00
|
|
|
reserved_sectors: u16,
|
|
|
|
fats: u8,
|
|
|
|
root_entries: u16,
|
2017-09-23 02:50:41 +08:00
|
|
|
total_sectors_16: u16,
|
2017-10-02 00:13:59 +08:00
|
|
|
media: u8,
|
|
|
|
sectors_per_fat_16: u16,
|
2017-09-23 02:50:41 +08:00
|
|
|
sectors_per_track: u16,
|
2017-10-02 00:13:59 +08:00
|
|
|
heads: u16,
|
|
|
|
hidden_sectors: u32,
|
2017-09-23 02:50:41 +08:00
|
|
|
total_sectors_32: u32,
|
2017-10-25 23:20:27 +08:00
|
|
|
|
2017-09-23 02:50:41 +08:00
|
|
|
// Extended BIOS Parameter Block
|
2017-10-02 00:13:59 +08:00
|
|
|
sectors_per_fat_32: u32,
|
2017-09-23 02:50:41 +08:00
|
|
|
extended_flags: u16,
|
2017-10-02 00:13:59 +08:00
|
|
|
fs_version: u16,
|
|
|
|
root_dir_first_cluster: u32,
|
|
|
|
fs_info_sector: u16,
|
|
|
|
backup_boot_sector: u16,
|
2017-09-23 02:50:41 +08:00
|
|
|
reserved_0: [u8; 12],
|
|
|
|
drive_num: u8,
|
|
|
|
reserved_1: u8,
|
|
|
|
ext_sig: u8,
|
|
|
|
volume_id: u32,
|
|
|
|
volume_label: [u8; 11],
|
2017-10-02 00:13:59 +08:00
|
|
|
fs_type_label: [u8; 8],
|
2017-09-23 02:50:41 +08:00
|
|
|
}
|
|
|
|
|
2017-11-09 02:59:03 +08:00
|
|
|
impl BiosParameterBlock {
|
|
|
|
fn deserialize(rdr: &mut Read) -> io::Result<BiosParameterBlock> {
|
|
|
|
let mut bpb: BiosParameterBlock = Default::default();
|
|
|
|
bpb.bytes_per_sector = rdr.read_u16::<LittleEndian>()?;
|
|
|
|
bpb.sectors_per_cluster = rdr.read_u8()?;
|
|
|
|
bpb.reserved_sectors = rdr.read_u16::<LittleEndian>()?;
|
|
|
|
bpb.fats = rdr.read_u8()?;
|
|
|
|
bpb.root_entries = rdr.read_u16::<LittleEndian>()? ;
|
|
|
|
bpb.total_sectors_16 = rdr.read_u16::<LittleEndian>()?;
|
|
|
|
bpb.media = rdr.read_u8()?;
|
|
|
|
bpb.sectors_per_fat_16 = rdr.read_u16::<LittleEndian>()?;
|
|
|
|
bpb.sectors_per_track = rdr.read_u16::<LittleEndian>()?;
|
|
|
|
bpb.heads = rdr.read_u16::<LittleEndian>()?;
|
|
|
|
bpb.hidden_sectors = rdr.read_u32::<LittleEndian>()?;
|
|
|
|
bpb.total_sectors_32 = rdr.read_u32::<LittleEndian>()?;
|
|
|
|
|
|
|
|
// sanity checks
|
|
|
|
if bpb.bytes_per_sector < 512 {
|
|
|
|
return Err(Error::new(ErrorKind::Other, "invalid bytes_per_sector value in BPB"));
|
|
|
|
}
|
|
|
|
if bpb.sectors_per_cluster < 1 {
|
|
|
|
return Err(Error::new(ErrorKind::Other, "invalid sectors_per_cluster value in BPB"));
|
|
|
|
}
|
|
|
|
if bpb.reserved_sectors < 1 {
|
|
|
|
return Err(Error::new(ErrorKind::Other, "invalid reserved_sectors value in BPB"));
|
|
|
|
}
|
|
|
|
if bpb.fats == 0 {
|
|
|
|
return Err(Error::new(ErrorKind::Other, "invalid fats value in BPB"));
|
|
|
|
}
|
|
|
|
|
|
|
|
if bpb.sectors_per_fat_16 == 0 {
|
|
|
|
bpb.sectors_per_fat_32 = rdr.read_u32::<LittleEndian>()?;
|
|
|
|
bpb.extended_flags = rdr.read_u16::<LittleEndian>()?;
|
|
|
|
bpb.fs_version = rdr.read_u16::<LittleEndian>()?;
|
|
|
|
bpb.root_dir_first_cluster = rdr.read_u32::<LittleEndian>()?;
|
|
|
|
bpb.fs_info_sector = rdr.read_u16::<LittleEndian>()?;
|
|
|
|
bpb.backup_boot_sector = rdr.read_u16::<LittleEndian>()?;
|
|
|
|
rdr.read_exact(&mut bpb.reserved_0)?;
|
|
|
|
bpb.drive_num = rdr.read_u8()?;
|
|
|
|
bpb.reserved_1 = rdr.read_u8()?;
|
|
|
|
bpb.ext_sig = rdr.read_u8()?; // 0x29
|
|
|
|
bpb.volume_id = rdr.read_u32::<LittleEndian>()?;
|
|
|
|
rdr.read_exact(&mut bpb.volume_label)?;
|
|
|
|
rdr.read_exact(&mut bpb.fs_type_label)?;
|
|
|
|
} else {
|
|
|
|
bpb.drive_num = rdr.read_u8()?;
|
|
|
|
bpb.reserved_1 = rdr.read_u8()?;
|
|
|
|
bpb.ext_sig = rdr.read_u8()?; // 0x29
|
|
|
|
bpb.volume_id = rdr.read_u32::<LittleEndian>()?;
|
|
|
|
rdr.read_exact(&mut bpb.volume_label)?;
|
|
|
|
rdr.read_exact(&mut bpb.fs_type_label)?;
|
|
|
|
}
|
2017-11-09 03:03:21 +08:00
|
|
|
if bpb.ext_sig != 0x29 {
|
|
|
|
// fields after ext_sig are not used - clean them
|
|
|
|
bpb.volume_id = 0;
|
|
|
|
bpb.volume_label = [0; 11];
|
|
|
|
bpb.fs_type_label = [0; 8];
|
|
|
|
}
|
2017-11-09 02:59:03 +08:00
|
|
|
Ok(bpb)
|
|
|
|
}
|
2017-11-09 03:26:50 +08:00
|
|
|
|
|
|
|
fn mirroring_enabled(&self) -> bool {
|
|
|
|
self.extended_flags & 0x80 == 0
|
|
|
|
}
|
|
|
|
|
|
|
|
fn active_fat(&self) -> u16 {
|
|
|
|
self.extended_flags & 0x0F
|
|
|
|
}
|
2017-12-02 00:24:02 +08:00
|
|
|
|
|
|
|
fn status_flags(&self) -> FsStatusFlags {
|
|
|
|
FsStatusFlags {
|
|
|
|
dirty: self.reserved_1 & 1 != 0,
|
|
|
|
io_error: self.reserved_1 & 2 != 0,
|
|
|
|
}
|
|
|
|
}
|
2017-11-09 02:59:03 +08:00
|
|
|
}
|
|
|
|
|
2017-09-23 02:50:41 +08:00
|
|
|
#[allow(dead_code)]
|
2017-11-09 02:41:05 +08:00
|
|
|
struct BootRecord {
|
2017-09-23 02:50:41 +08:00
|
|
|
bootjmp: [u8; 3],
|
|
|
|
oem_name: [u8; 8],
|
2017-10-07 20:56:50 +08:00
|
|
|
bpb: BiosParameterBlock,
|
2017-09-23 02:50:41 +08:00
|
|
|
boot_code: [u8; 448],
|
|
|
|
boot_sig: [u8; 2],
|
|
|
|
}
|
|
|
|
|
2017-11-09 02:59:03 +08:00
|
|
|
impl BootRecord {
|
|
|
|
fn deserialize(rdr: &mut Read) -> io::Result<BootRecord> {
|
|
|
|
let mut boot: BootRecord = Default::default();
|
|
|
|
rdr.read_exact(&mut boot.bootjmp)?;
|
|
|
|
rdr.read_exact(&mut boot.oem_name)?;
|
|
|
|
boot.bpb = BiosParameterBlock::deserialize(rdr)?;
|
|
|
|
|
|
|
|
if boot.bpb.sectors_per_fat_16 == 0 {
|
|
|
|
rdr.read_exact(&mut boot.boot_code[0..420])?;
|
|
|
|
} else {
|
|
|
|
rdr.read_exact(&mut boot.boot_code[0..448])?;
|
|
|
|
}
|
|
|
|
rdr.read_exact(&mut boot.boot_sig)?;
|
|
|
|
Ok(boot)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-10-07 20:56:50 +08:00
|
|
|
impl Default for BootRecord {
|
2017-10-25 23:20:27 +08:00
|
|
|
fn default() -> BootRecord {
|
2017-10-07 20:56:50 +08:00
|
|
|
BootRecord {
|
2017-09-23 02:50:41 +08:00
|
|
|
bootjmp: Default::default(),
|
|
|
|
oem_name: Default::default(),
|
|
|
|
bpb: Default::default(),
|
|
|
|
boot_code: [0; 448],
|
|
|
|
boot_sig: Default::default(),
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-11-09 03:26:50 +08:00
|
|
|
/// FAT filesystem options.
|
|
|
|
#[derive(Debug, Clone, Copy)]
|
|
|
|
pub struct FsOptions {
|
|
|
|
pub(crate) update_accessed_date: bool,
|
|
|
|
}
|
|
|
|
|
|
|
|
impl FsOptions {
|
|
|
|
pub fn new() -> Self {
|
|
|
|
FsOptions {
|
|
|
|
update_accessed_date: false,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/// If enabled library updates accessed date field in directory entry when reading
|
|
|
|
pub fn update_accessed_date(mut self, enabled: bool) -> Self {
|
|
|
|
self.update_accessed_date = enabled;
|
|
|
|
self
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-04-28 20:53:32 +08:00
|
|
|
pub(crate) type FileSystemRef<'a, 'b> = &'a FileSystem<'b>;
|
2017-09-27 20:05:58 +08:00
|
|
|
|
2017-10-21 22:25:04 +08:00
|
|
|
/// FAT filesystem main struct.
|
2017-10-07 20:56:50 +08:00
|
|
|
pub struct FileSystem<'a> {
|
2017-10-09 20:59:52 +08:00
|
|
|
pub(crate) disk: RefCell<&'a mut ReadWriteSeek>,
|
2017-11-09 03:26:50 +08:00
|
|
|
pub(crate) options: FsOptions,
|
2017-11-09 02:41:05 +08:00
|
|
|
fat_type: FatType,
|
|
|
|
bpb: BiosParameterBlock,
|
|
|
|
first_data_sector: u32,
|
|
|
|
root_dir_sectors: u32,
|
2017-09-27 20:05:58 +08:00
|
|
|
}
|
|
|
|
|
2017-10-07 20:56:50 +08:00
|
|
|
impl <'a> FileSystem<'a> {
|
2017-10-21 22:25:04 +08:00
|
|
|
/// Creates new filesystem object instance.
|
|
|
|
///
|
2018-05-09 21:34:41 +08:00
|
|
|
/// Supplied disk parameter cannot be seeked. If there is a need to read a fragment of disk image (e.g. partition)
|
|
|
|
/// library user should provide a custom implementation of ReadWriteSeek trait.
|
|
|
|
///
|
2017-10-21 22:25:04 +08:00
|
|
|
/// Note: creating multiple filesystem objects with one underlying device/disk image can
|
|
|
|
/// cause filesystem corruption.
|
2017-11-09 03:26:50 +08:00
|
|
|
pub fn new<T: ReadWriteSeek>(disk: &'a mut T, options: FsOptions) -> io::Result<FileSystem<'a>> {
|
2018-05-09 21:34:41 +08:00
|
|
|
// Make sure given image is not seeked
|
|
|
|
debug_assert!(disk.seek(SeekFrom::Current(0))? == 0);
|
|
|
|
|
|
|
|
// Read boot sector
|
2017-11-09 02:59:03 +08:00
|
|
|
let bpb = {
|
|
|
|
let boot = BootRecord::deserialize(disk)?;
|
|
|
|
if boot.boot_sig != [0x55, 0xAA] {
|
|
|
|
return Err(Error::new(ErrorKind::Other, "invalid signature"));
|
|
|
|
}
|
|
|
|
boot.bpb
|
|
|
|
};
|
2017-10-25 23:20:27 +08:00
|
|
|
|
2017-11-09 02:59:03 +08:00
|
|
|
let total_sectors =
|
|
|
|
if bpb.total_sectors_16 == 0 { bpb.total_sectors_32 }
|
|
|
|
else { bpb.total_sectors_16 as u32 };
|
|
|
|
let sectors_per_fat =
|
|
|
|
if bpb.sectors_per_fat_16 == 0 { bpb.sectors_per_fat_32 }
|
|
|
|
else { bpb.sectors_per_fat_16 as u32 };
|
|
|
|
let root_dir_bytes = bpb.root_entries as u32 * DIR_ENTRY_SIZE as u32;
|
|
|
|
let root_dir_sectors = (root_dir_bytes + (bpb.bytes_per_sector as u32 - 1)) / bpb.bytes_per_sector as u32;
|
|
|
|
let first_data_sector = bpb.reserved_sectors as u32 + (bpb.fats as u32 * sectors_per_fat) + root_dir_sectors;
|
|
|
|
let fat_sectors = bpb.fats as u32 * sectors_per_fat;
|
|
|
|
let data_sectors = total_sectors - (bpb.reserved_sectors as u32 + fat_sectors + root_dir_sectors as u32);
|
|
|
|
let total_clusters = data_sectors / bpb.sectors_per_cluster as u32;
|
|
|
|
let fat_type = FatType::from_clusters(total_clusters);
|
2017-10-25 23:20:27 +08:00
|
|
|
|
2017-10-07 20:56:50 +08:00
|
|
|
Ok(FileSystem {
|
2017-10-09 20:59:52 +08:00
|
|
|
disk: RefCell::new(disk),
|
2017-11-09 03:26:50 +08:00
|
|
|
options,
|
2017-09-24 09:08:00 +08:00
|
|
|
fat_type,
|
2017-11-09 02:59:03 +08:00
|
|
|
bpb: bpb,
|
2017-09-24 09:08:00 +08:00
|
|
|
first_data_sector,
|
|
|
|
root_dir_sectors,
|
2017-09-24 01:42:09 +08:00
|
|
|
})
|
2017-09-23 02:50:41 +08:00
|
|
|
}
|
2017-10-25 23:20:27 +08:00
|
|
|
|
2017-10-21 22:25:04 +08:00
|
|
|
/// Returns type of used File Allocation Table (FAT).
|
2017-09-25 04:12:38 +08:00
|
|
|
pub fn fat_type(&self) -> FatType {
|
2017-10-04 19:56:44 +08:00
|
|
|
self.fat_type
|
2017-09-24 09:08:00 +08:00
|
|
|
}
|
2017-10-25 23:20:27 +08:00
|
|
|
|
2017-10-21 22:25:04 +08:00
|
|
|
/// Returns volume identifier read from BPB in Boot Sector.
|
2017-09-25 04:12:38 +08:00
|
|
|
pub fn volume_id(&self) -> u32 {
|
2017-11-09 02:41:05 +08:00
|
|
|
self.bpb.volume_id
|
2017-09-24 09:08:00 +08:00
|
|
|
}
|
2017-10-25 23:20:27 +08:00
|
|
|
|
2017-10-21 22:25:04 +08:00
|
|
|
/// Returns volume label from BPB in Boot Sector.
|
|
|
|
///
|
|
|
|
/// Note: File with VOLUME_ID attribute in root directory is ignored by this library.
|
|
|
|
/// Only label from BPB is used.
|
2017-09-25 04:12:38 +08:00
|
|
|
pub fn volume_label(&self) -> String {
|
2017-11-09 02:41:05 +08:00
|
|
|
String::from_utf8_lossy(&self.bpb.volume_label).trim_right().to_string()
|
2017-09-24 09:08:00 +08:00
|
|
|
}
|
2017-10-25 23:20:27 +08:00
|
|
|
|
2017-10-21 22:25:04 +08:00
|
|
|
/// Returns root directory object allowing futher penetration of filesystem structure.
|
2017-10-07 20:56:50 +08:00
|
|
|
pub fn root_dir<'b>(&'b self) -> Dir<'b, 'a> {
|
2017-09-27 20:05:58 +08:00
|
|
|
let root_rdr = {
|
2017-10-04 19:56:44 +08:00
|
|
|
match self.fat_type {
|
2017-10-07 21:00:27 +08:00
|
|
|
FatType::Fat12 | FatType::Fat16 => DirRawStream::Root(DiskSlice::from_sectors(
|
2017-10-11 05:18:32 +08:00
|
|
|
self.first_data_sector - self.root_dir_sectors, self.root_dir_sectors, 1, self)),
|
2017-11-09 02:41:05 +08:00
|
|
|
_ => DirRawStream::File(File::new(Some(self.bpb.root_dir_first_cluster), None, self)),
|
2017-09-27 20:05:58 +08:00
|
|
|
}
|
2017-09-24 09:08:00 +08:00
|
|
|
};
|
2017-10-07 20:56:50 +08:00
|
|
|
Dir::new(root_rdr, self)
|
2017-09-24 09:08:00 +08:00
|
|
|
}
|
2017-10-25 23:20:27 +08:00
|
|
|
|
2017-10-04 19:56:44 +08:00
|
|
|
pub(crate) fn offset_from_sector(&self, sector: u32) -> u64 {
|
2017-11-09 02:41:05 +08:00
|
|
|
(sector as u64) * self.bpb.bytes_per_sector as u64
|
2017-10-04 19:56:44 +08:00
|
|
|
}
|
2017-10-25 23:20:27 +08:00
|
|
|
|
2017-10-04 19:56:44 +08:00
|
|
|
pub(crate) fn sector_from_cluster(&self, cluster: u32) -> u32 {
|
2017-11-09 02:41:05 +08:00
|
|
|
((cluster - 2) * self.bpb.sectors_per_cluster as u32) + self.first_data_sector
|
2017-10-04 19:56:44 +08:00
|
|
|
}
|
2017-10-25 23:20:27 +08:00
|
|
|
|
2017-11-09 02:59:03 +08:00
|
|
|
pub(crate) fn cluster_size(&self) -> u32 {
|
2017-11-09 02:41:05 +08:00
|
|
|
self.bpb.sectors_per_cluster as u32 * self.bpb.bytes_per_sector as u32
|
2017-10-04 19:56:44 +08:00
|
|
|
}
|
2017-10-25 23:20:27 +08:00
|
|
|
|
2017-10-04 19:56:44 +08:00
|
|
|
pub(crate) fn offset_from_cluster(&self, cluser: u32) -> u64 {
|
|
|
|
self.offset_from_sector(self.sector_from_cluster(cluser))
|
|
|
|
}
|
2017-10-25 23:20:27 +08:00
|
|
|
|
2017-10-10 03:14:28 +08:00
|
|
|
fn fat_slice<'b>(&'b self) -> DiskSlice<'b, 'a> {
|
2017-10-04 19:56:44 +08:00
|
|
|
let sectors_per_fat =
|
2017-11-09 02:41:05 +08:00
|
|
|
if self.bpb.sectors_per_fat_16 == 0 { self.bpb.sectors_per_fat_32 }
|
|
|
|
else { self.bpb.sectors_per_fat_16 as u32 };
|
2017-11-09 03:26:50 +08:00
|
|
|
let mirroring_enabled = self.bpb.mirroring_enabled();
|
2017-10-11 05:18:32 +08:00
|
|
|
let (fat_first_sector, mirrors) = if mirroring_enabled {
|
2017-11-09 02:41:05 +08:00
|
|
|
(self.bpb.reserved_sectors as u32, self.bpb.fats)
|
2017-10-11 05:18:32 +08:00
|
|
|
} else {
|
2017-11-09 03:26:50 +08:00
|
|
|
let active_fat = self.bpb.active_fat() as u32;
|
2017-11-09 02:41:05 +08:00
|
|
|
let fat_first_sector = (self.bpb.reserved_sectors as u32) + active_fat * sectors_per_fat;
|
2017-10-11 05:18:32 +08:00
|
|
|
(fat_first_sector, 1)
|
|
|
|
};
|
|
|
|
DiskSlice::from_sectors(fat_first_sector, sectors_per_fat, mirrors, self)
|
2017-10-10 03:14:28 +08:00
|
|
|
}
|
2017-10-25 23:20:27 +08:00
|
|
|
|
2017-10-10 03:14:28 +08:00
|
|
|
pub(crate) fn cluster_iter<'b>(&'b self, cluster: u32) -> ClusterIterator<'b, 'a> {
|
|
|
|
let disk_slice = self.fat_slice();
|
2017-10-07 20:56:50 +08:00
|
|
|
ClusterIterator::new(disk_slice, self.fat_type, cluster)
|
2017-10-04 19:56:44 +08:00
|
|
|
}
|
2017-10-25 23:20:27 +08:00
|
|
|
|
2017-10-10 03:14:28 +08:00
|
|
|
pub(crate) fn alloc_cluster(&self, prev_cluster: Option<u32>) -> io::Result<u32> {
|
|
|
|
let mut disk_slice = self.fat_slice();
|
|
|
|
alloc_cluster(&mut disk_slice, self.fat_type, prev_cluster)
|
|
|
|
}
|
2017-12-02 00:24:02 +08:00
|
|
|
|
|
|
|
pub fn read_status_flags(&self) -> io::Result<FsStatusFlags> {
|
|
|
|
let bpb_status = self.bpb.status_flags();
|
|
|
|
let fat_status = read_fat_flags(&mut self.fat_slice(), self.fat_type)?;
|
|
|
|
Ok(FsStatusFlags {
|
|
|
|
dirty: bpb_status.dirty || fat_status.dirty,
|
|
|
|
io_error: bpb_status.io_error || fat_status.io_error,
|
|
|
|
})
|
|
|
|
}
|
2017-09-24 01:42:09 +08:00
|
|
|
}
|
|
|
|
|
2017-10-06 22:07:11 +08:00
|
|
|
#[derive(Clone)]
|
2017-10-07 20:56:50 +08:00
|
|
|
pub(crate) struct DiskSlice<'a, 'b: 'a> {
|
2017-09-24 01:42:09 +08:00
|
|
|
begin: u64,
|
|
|
|
size: u64,
|
|
|
|
offset: u64,
|
2017-10-11 05:18:32 +08:00
|
|
|
mirrors: u8,
|
2017-10-07 20:56:50 +08:00
|
|
|
fs: &'a FileSystem<'b>,
|
2017-09-24 01:42:09 +08:00
|
|
|
}
|
|
|
|
|
2017-10-07 20:56:50 +08:00
|
|
|
impl <'a, 'b> DiskSlice<'a, 'b> {
|
2017-10-11 05:18:32 +08:00
|
|
|
pub(crate) fn new(begin: u64, size: u64, mirrors: u8, fs: FileSystemRef<'a, 'b>) -> Self {
|
|
|
|
DiskSlice { begin, size, mirrors, fs, offset: 0 }
|
2017-09-23 06:22:31 +08:00
|
|
|
}
|
2017-10-25 23:20:27 +08:00
|
|
|
|
2017-10-11 05:18:32 +08:00
|
|
|
pub(crate) fn from_sectors(first_sector: u32, sector_count: u32, mirrors: u8, fs: FileSystemRef<'a, 'b>) -> Self {
|
2017-11-09 02:41:05 +08:00
|
|
|
let bytes_per_sector = fs.bpb.bytes_per_sector as u64;
|
2017-10-11 05:18:32 +08:00
|
|
|
Self::new(first_sector as u64 * bytes_per_sector, sector_count as u64 * bytes_per_sector, mirrors, fs)
|
2017-09-23 04:27:39 +08:00
|
|
|
}
|
2017-10-25 23:20:27 +08:00
|
|
|
|
2017-10-21 21:51:19 +08:00
|
|
|
pub(crate) fn abs_pos(&self) -> u64 {
|
2017-10-09 20:59:52 +08:00
|
|
|
self.begin + self.offset
|
|
|
|
}
|
2017-09-24 01:42:09 +08:00
|
|
|
}
|
2017-09-23 04:27:39 +08:00
|
|
|
|
2017-10-07 20:56:50 +08:00
|
|
|
impl <'a, 'b> Read for DiskSlice<'a, 'b> {
|
2017-09-24 01:42:09 +08:00
|
|
|
fn read(&mut self, buf: &mut [u8]) -> io::Result<usize> {
|
2017-09-24 02:16:32 +08:00
|
|
|
let offset = self.begin + self.offset;
|
|
|
|
let read_size = cmp::min((self.size - self.offset) as usize, buf.len());
|
2017-10-09 20:59:52 +08:00
|
|
|
let mut disk = self.fs.disk.borrow_mut();
|
|
|
|
disk.seek(SeekFrom::Start(offset))?;
|
|
|
|
let size = disk.read(&mut buf[..read_size])?;
|
2017-09-24 01:42:09 +08:00
|
|
|
self.offset += size as u64;
|
|
|
|
Ok(size)
|
2017-09-23 02:50:41 +08:00
|
|
|
}
|
|
|
|
}
|
2017-09-24 08:10:59 +08:00
|
|
|
|
2017-10-09 20:59:52 +08:00
|
|
|
impl <'a, 'b> Write for DiskSlice<'a, 'b> {
|
|
|
|
fn write(&mut self, buf: &[u8]) -> io::Result<usize> {
|
|
|
|
let offset = self.begin + self.offset;
|
|
|
|
let write_size = cmp::min((self.size - self.offset) as usize, buf.len());
|
2017-10-11 05:18:32 +08:00
|
|
|
for i in 0..self.mirrors {
|
|
|
|
let mut disk = self.fs.disk.borrow_mut();
|
|
|
|
disk.seek(SeekFrom::Start(offset + i as u64 * self.size))?;
|
|
|
|
disk.write_all(&buf[..write_size])?;
|
|
|
|
}
|
|
|
|
self.offset += write_size as u64;
|
|
|
|
Ok(write_size)
|
2017-10-09 20:59:52 +08:00
|
|
|
}
|
2017-10-25 23:20:27 +08:00
|
|
|
|
2017-10-09 20:59:52 +08:00
|
|
|
fn flush(&mut self) -> io::Result<()> {
|
|
|
|
let mut disk = self.fs.disk.borrow_mut();
|
|
|
|
disk.flush()
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-10-07 20:56:50 +08:00
|
|
|
impl <'a, 'b> Seek for DiskSlice<'a, 'b> {
|
2017-09-24 08:10:59 +08:00
|
|
|
fn seek(&mut self, pos: SeekFrom) -> io::Result<u64> {
|
|
|
|
let new_offset = match pos {
|
|
|
|
SeekFrom::Current(x) => self.offset as i64 + x,
|
|
|
|
SeekFrom::Start(x) => x as i64,
|
|
|
|
SeekFrom::End(x) => self.size as i64 + x,
|
|
|
|
};
|
|
|
|
if new_offset < 0 || new_offset as u64 > self.size {
|
|
|
|
Err(io::Error::new(ErrorKind::InvalidInput, "invalid seek"))
|
|
|
|
} else {
|
|
|
|
self.offset = new_offset as u64;
|
|
|
|
Ok(self.offset)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|