rust-fatfs/src/dir.rs

219 lines
6.6 KiB
Rust
Raw Normal View History

2017-09-24 06:05:43 +08:00
use std::ascii::AsciiExt;
2017-09-24 09:08:00 +08:00
use std::fmt;
use std::io::prelude::*;
use std::io;
use std::io::{ErrorKind, SeekFrom};
use std::str;
use byteorder::{LittleEndian, ReadBytesExt};
2017-09-23 05:20:06 +08:00
use chrono::{DateTime, Date, TimeZone, Local};
use fs::{FatSharedStateRef, ReadSeek};
2017-09-23 04:27:39 +08:00
use file::FatFile;
2017-09-23 05:36:44 +08:00
bitflags! {
pub struct FatFileAttributes: u8 {
const READ_ONLY = 0x01;
const HIDDEN = 0x02;
const SYSTEM = 0x04;
const VOLUME_ID = 0x08;
const DIRECTORY = 0x10;
const ARCHIVE = 0x20;
const LFN = Self::READ_ONLY.bits | Self::HIDDEN.bits
| Self::SYSTEM.bits | Self::VOLUME_ID.bits;
}
}
#[allow(dead_code)]
2017-09-24 09:08:00 +08:00
#[derive(Clone, Copy, Debug)]
pub struct FatDirEntryData {
name: [u8; 11],
2017-09-23 05:36:44 +08:00
attrs: FatFileAttributes,
reserved_0: u8,
2017-09-23 04:27:39 +08:00
create_time_0: u8,
create_time_1: u16,
create_date: u16,
access_date: u16,
first_cluster_hi: u16,
2017-09-23 04:27:39 +08:00
modify_time: u16,
modify_date: u16,
first_cluster_lo: u16,
size: u32,
}
2017-09-24 09:08:00 +08:00
#[derive(Clone)]
pub struct FatDirEntry {
data: FatDirEntryData,
state: FatSharedStateRef,
}
2017-09-23 04:27:39 +08:00
impl FatDirEntry {
pub fn get_name(&self) -> String {
2017-09-24 09:08:00 +08:00
let name = str::from_utf8(&self.data.name[0..8]).unwrap().trim_right();
let ext = str::from_utf8(&self.data.name[8..11]).unwrap().trim_right();
if ext == "" { name.to_string() } else { format!("{}.{}", name, ext) }
2017-09-23 05:36:44 +08:00
}
pub fn get_attrs(&self) -> FatFileAttributes {
2017-09-24 09:08:00 +08:00
self.data.attrs
2017-09-23 04:27:39 +08:00
}
2017-09-24 06:05:43 +08:00
pub fn is_dir(&self) -> bool {
2017-09-24 09:08:00 +08:00
self.data.attrs.contains(FatFileAttributes::DIRECTORY)
2017-09-24 06:05:43 +08:00
}
2017-09-23 04:27:39 +08:00
pub fn get_cluster(&self) -> u32 {
2017-09-24 09:08:00 +08:00
((self.data.first_cluster_hi as u32) << 16) | self.data.first_cluster_lo as u32
2017-09-23 04:27:39 +08:00
}
pub fn get_file(&self) -> FatFile {
2017-09-24 06:05:43 +08:00
if self.is_dir() {
panic!("This is a directory");
}
2017-09-24 09:08:00 +08:00
FatFile::new(self.get_cluster(), Some(self.data.size), self.state.clone())
2017-09-23 04:27:39 +08:00
}
2017-09-24 06:05:43 +08:00
pub fn get_dir(&self) -> FatDir {
if !self.is_dir() {
panic!("This is a file");
}
let file = FatFile::new(self.get_cluster(), None, self.state.clone());
FatDir::new(Box::new(file), self.state.clone())
}
2017-09-23 04:27:39 +08:00
pub fn get_size(&self) -> u32 {
2017-09-24 09:08:00 +08:00
self.data.size
2017-09-23 04:27:39 +08:00
}
pub fn get_create_time(&self) -> DateTime<Local> {
2017-09-24 09:08:00 +08:00
Self::convert_date_time(self.data.create_date, self.data.create_time_1)
2017-09-23 04:27:39 +08:00
}
pub fn get_access_date(&self) -> Date<Local> {
2017-09-24 09:08:00 +08:00
Self::convert_date(self.data.access_date)
2017-09-23 04:27:39 +08:00
}
pub fn get_modify_time(&self) -> DateTime<Local> {
2017-09-24 09:08:00 +08:00
Self::convert_date_time(self.data.modify_date, self.data.modify_time)
}
fn convert_date(dos_date: u16) -> Date<Local> {
let (year, month, day) = ((dos_date >> 9) + 1980, (dos_date >> 5) & 0xF, dos_date & 0x1F);
Local.ymd(year as i32, month as u32, day as u32)
}
fn convert_date_time(dos_date: u16, dos_time: u16) -> DateTime<Local> {
let (hour, min, sec) = (dos_time >> 11, (dos_time >> 5) & 0x3F, (dos_time & 0x1F) * 2);
Self::convert_date(dos_date).and_hms(hour as u32, min as u32, sec as u32)
}
}
impl fmt::Debug for FatDirEntry {
fn fmt(&self, f: &mut fmt::Formatter) -> Result<(), fmt::Error> {
self.data.fmt(f)
2017-09-23 04:27:39 +08:00
}
}
pub struct FatDir {
rdr: Box<ReadSeek>,
state: FatSharedStateRef,
}
impl FatDir {
pub(crate) fn new(rdr: Box<ReadSeek>, state: FatSharedStateRef) -> FatDir {
FatDir { rdr, state }
}
pub fn list(&mut self) -> io::Result<Vec<FatDirEntry>> {
self.rewind();
2017-09-24 08:24:42 +08:00
Ok(self.map(|x| x.unwrap()).collect())
}
pub fn rewind(&mut self) {
self.rdr.seek(SeekFrom::Start(0)).unwrap();
}
2017-09-24 09:08:00 +08:00
fn read_dir_entry_data(&mut self) -> io::Result<FatDirEntryData> {
let mut name = [0; 11];
self.rdr.read(&mut name)?;
let attrs = FatFileAttributes::from_bits(self.rdr.read_u8()?).expect("invalid attributes");
2017-09-24 09:08:00 +08:00
Ok(FatDirEntryData {
name,
attrs,
reserved_0: self.rdr.read_u8()?,
create_time_0: self.rdr.read_u8()?,
create_time_1: self.rdr.read_u16::<LittleEndian>()?,
create_date: self.rdr.read_u16::<LittleEndian>()?,
access_date: self.rdr.read_u16::<LittleEndian>()?,
first_cluster_hi: self.rdr.read_u16::<LittleEndian>()?,
modify_time: self.rdr.read_u16::<LittleEndian>()?,
modify_date: self.rdr.read_u16::<LittleEndian>()?,
first_cluster_lo: self.rdr.read_u16::<LittleEndian>()?,
size: self.rdr.read_u32::<LittleEndian>()?,
})
}
2017-09-24 06:05:43 +08:00
fn split_path<'a>(path: &'a str) -> (&'a str, Option<&'a str>) {
let mut path_split = path.trim_matches('/').splitn(2, "/");
let comp = path_split.next().unwrap();
let rest_opt = path_split.next();
(comp, rest_opt)
}
fn find_entry(&mut self, name: &str) -> io::Result<FatDirEntry> {
let entries: Vec<FatDirEntry> = self.list()?;
for e in entries {
if e.get_name().eq_ignore_ascii_case(name) {
println!("find entry {}", name);
return Ok(e);
}
}
Err(io::Error::new(ErrorKind::NotFound, "file not found"))
}
pub fn get_dir(&mut self, path: &str) -> io::Result<FatDir> {
let (name, rest_opt) = Self::split_path(path);
let e = self.find_entry(name)?;
match rest_opt {
Some(rest) => e.get_dir().get_dir(rest),
None => Ok(e.get_dir())
}
}
pub fn get_file(&mut self, path: &str) -> io::Result<FatFile> {
let (name, rest_opt) = Self::split_path(path);
let e = self.find_entry(name)?;
match rest_opt {
Some(rest) => e.get_dir().get_file(rest),
None => Ok(e.get_file())
}
}
}
2017-09-24 08:24:42 +08:00
impl Iterator for FatDir {
type Item = io::Result<FatDirEntry>;
fn next(&mut self) -> Option<io::Result<FatDirEntry>> {
loop {
2017-09-24 09:08:00 +08:00
let res = self.read_dir_entry_data();
let data = match res {
Ok(data) => data,
Err(err) => return Some(Err(err)),
2017-09-24 08:24:42 +08:00
};
2017-09-24 09:08:00 +08:00
if data.name[0] == 0 {
2017-09-24 08:24:42 +08:00
return None; // end of dir
}
2017-09-24 09:08:00 +08:00
if data.name[0] == 0xE5 {
2017-09-24 08:24:42 +08:00
continue; // deleted
}
2017-09-24 09:08:00 +08:00
if data.attrs == FatFileAttributes::LFN {
2017-09-24 08:24:42 +08:00
continue; // FIXME: support LFN
}
2017-09-24 09:08:00 +08:00
return Some(Ok(FatDirEntry {
data,
state: self.state.clone(),
}));
2017-09-24 08:24:42 +08:00
}
}
}