2017-09-24 06:05:43 +08:00
|
|
|
use std::ascii::AsciiExt;
|
2017-09-24 09:08:00 +08:00
|
|
|
use std::fmt;
|
2017-09-23 02:50:41 +08:00
|
|
|
use std::io::prelude::*;
|
|
|
|
use std::io;
|
2017-09-25 04:12:38 +08:00
|
|
|
use std::io::{Cursor, ErrorKind, SeekFrom};
|
2017-10-21 21:35:26 +08:00
|
|
|
use std::cmp;
|
2017-10-09 20:59:52 +08:00
|
|
|
use byteorder::{LittleEndian, ReadBytesExt, WriteBytesExt};
|
2017-10-06 22:42:29 +08:00
|
|
|
|
|
|
|
#[cfg(feature = "chrono")]
|
2017-10-07 20:56:50 +08:00
|
|
|
use chrono::{TimeZone, Local};
|
|
|
|
#[cfg(feature = "chrono")]
|
|
|
|
use chrono;
|
2017-09-23 05:20:06 +08:00
|
|
|
|
2017-11-08 05:52:56 +08:00
|
|
|
use fs::{FileSystemRef, DiskSlice, FatType};
|
2017-10-07 20:56:50 +08:00
|
|
|
use file::File;
|
2017-09-23 02:50:41 +08:00
|
|
|
|
2017-10-06 22:07:11 +08:00
|
|
|
#[derive(Clone)]
|
2017-10-07 21:00:27 +08:00
|
|
|
pub(crate) enum DirRawStream<'a, 'b: 'a> {
|
2017-10-07 20:56:50 +08:00
|
|
|
File(File<'a, 'b>),
|
|
|
|
Root(DiskSlice<'a, 'b>),
|
2017-09-27 20:05:58 +08:00
|
|
|
}
|
|
|
|
|
2017-10-09 20:59:52 +08:00
|
|
|
impl <'a, 'b> DirRawStream<'a, 'b> {
|
2017-10-21 21:51:19 +08:00
|
|
|
pub(crate) fn abs_pos(&self) -> Option<u64> {
|
2017-10-09 20:59:52 +08:00
|
|
|
match self {
|
2017-10-21 21:51:19 +08:00
|
|
|
&DirRawStream::File(ref file) => file.abs_pos(),
|
|
|
|
&DirRawStream::Root(ref slice) => Some(slice.abs_pos()),
|
2017-10-09 20:59:52 +08:00
|
|
|
}
|
|
|
|
}
|
2017-10-27 20:12:53 +08:00
|
|
|
|
|
|
|
pub(crate) fn first_cluster(&self) -> Option<u32> {
|
|
|
|
match self {
|
|
|
|
&DirRawStream::File(ref file) => file.first_cluster(),
|
|
|
|
&DirRawStream::Root(_) => None,
|
|
|
|
}
|
|
|
|
}
|
2017-10-09 20:59:52 +08:00
|
|
|
}
|
|
|
|
|
2017-10-07 21:00:27 +08:00
|
|
|
impl <'a, 'b> Read for DirRawStream<'a, 'b> {
|
2017-09-27 20:05:58 +08:00
|
|
|
fn read(&mut self, buf: &mut [u8]) -> io::Result<usize> {
|
|
|
|
match self {
|
2017-10-07 21:00:27 +08:00
|
|
|
&mut DirRawStream::File(ref mut file) => file.read(buf),
|
|
|
|
&mut DirRawStream::Root(ref mut raw) => raw.read(buf),
|
2017-09-27 20:05:58 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-10-15 21:56:01 +08:00
|
|
|
impl <'a, 'b> Write for DirRawStream<'a, 'b> {
|
|
|
|
fn write(&mut self, buf: &[u8]) -> io::Result<usize> {
|
|
|
|
match self {
|
|
|
|
&mut DirRawStream::File(ref mut file) => file.write(buf),
|
|
|
|
&mut DirRawStream::Root(ref mut raw) => raw.write(buf),
|
|
|
|
}
|
|
|
|
}
|
|
|
|
fn flush(&mut self) -> io::Result<()> {
|
|
|
|
match self {
|
|
|
|
&mut DirRawStream::File(ref mut file) => file.flush(),
|
|
|
|
&mut DirRawStream::Root(ref mut raw) => raw.flush(),
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-10-07 21:00:27 +08:00
|
|
|
impl <'a, 'b> Seek for DirRawStream<'a, 'b> {
|
2017-09-27 20:05:58 +08:00
|
|
|
fn seek(&mut self, pos: SeekFrom) -> io::Result<u64> {
|
|
|
|
match self {
|
2017-10-07 21:00:27 +08:00
|
|
|
&mut DirRawStream::File(ref mut file) => file.seek(pos),
|
|
|
|
&mut DirRawStream::Root(ref mut raw) => raw.seek(pos),
|
2017-09-27 20:05:58 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-09-23 05:36:44 +08:00
|
|
|
bitflags! {
|
2017-10-21 22:25:04 +08:00
|
|
|
/// FAT file attributes
|
2017-09-25 04:12:38 +08:00
|
|
|
#[derive(Default)]
|
2017-10-07 20:56:50 +08:00
|
|
|
pub struct FileAttributes: u8 {
|
2017-09-23 05:36:44 +08:00
|
|
|
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;
|
|
|
|
}
|
2017-09-23 02:50:41 +08:00
|
|
|
}
|
|
|
|
|
2017-10-16 04:42:26 +08:00
|
|
|
const LFN_PART_LEN: usize = 13;
|
|
|
|
const DIR_ENTRY_SIZE: u64 = 32;
|
2017-10-21 21:51:19 +08:00
|
|
|
const DIR_ENTRY_FREE_FLAG: u8 = 0xE5;
|
2017-10-21 21:35:26 +08:00
|
|
|
const LFN_ENTRY_LAST_FLAG: u8 = 0x40;
|
2017-10-16 04:42:26 +08:00
|
|
|
|
2017-09-23 02:50:41 +08:00
|
|
|
#[allow(dead_code)]
|
2017-09-25 04:12:38 +08:00
|
|
|
#[derive(Clone, Debug, Default)]
|
2017-10-09 20:59:52 +08:00
|
|
|
pub(crate) struct DirFileEntryData {
|
2017-09-23 02:50:41 +08:00
|
|
|
name: [u8; 11],
|
2017-10-07 20:56:50 +08:00
|
|
|
attrs: FileAttributes,
|
2017-09-23 02:50:41 +08:00
|
|
|
reserved_0: u8,
|
2017-09-23 04:27:39 +08:00
|
|
|
create_time_0: u8,
|
|
|
|
create_time_1: u16,
|
|
|
|
create_date: u16,
|
2017-09-23 02:50:41 +08:00
|
|
|
access_date: u16,
|
|
|
|
first_cluster_hi: u16,
|
2017-09-23 04:27:39 +08:00
|
|
|
modify_time: u16,
|
|
|
|
modify_date: u16,
|
2017-09-23 02:50:41 +08:00
|
|
|
first_cluster_lo: u16,
|
2017-10-10 22:05:19 +08:00
|
|
|
size: u32,
|
2017-10-09 20:59:52 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
impl DirFileEntryData {
|
2017-11-08 05:52:56 +08:00
|
|
|
pub(crate) fn first_cluster(&self, fat_type: FatType) -> Option<u32> {
|
|
|
|
let first_cluster_hi = if fat_type == FatType::Fat32 { self.first_cluster_hi } else { 0 };
|
|
|
|
let n = ((first_cluster_hi as u32) << 16) | self.first_cluster_lo as u32;
|
2017-10-09 20:59:52 +08:00
|
|
|
if n == 0 { None } else { Some(n) }
|
|
|
|
}
|
2017-10-25 23:20:27 +08:00
|
|
|
|
2017-11-08 05:52:56 +08:00
|
|
|
pub(crate) fn set_first_cluster(&mut self, cluster: Option<u32>, fat_type: FatType) {
|
2017-10-10 03:14:28 +08:00
|
|
|
let n = cluster.unwrap_or(0);
|
2017-11-08 05:52:56 +08:00
|
|
|
if fat_type == FatType::Fat32 {
|
|
|
|
self.first_cluster_hi = (n >> 16) as u16;
|
|
|
|
}
|
2017-10-10 03:14:28 +08:00
|
|
|
self.first_cluster_lo = (n & 0xFFFF) as u16;
|
|
|
|
}
|
2017-10-25 23:20:27 +08:00
|
|
|
|
2017-10-15 21:56:01 +08:00
|
|
|
pub(crate) fn size(&self) -> Option<u32> {
|
|
|
|
if self.is_file() {
|
|
|
|
Some(self.size)
|
|
|
|
} else {
|
|
|
|
None
|
|
|
|
}
|
2017-10-09 20:59:52 +08:00
|
|
|
}
|
2017-10-25 23:20:27 +08:00
|
|
|
|
2017-10-10 22:05:19 +08:00
|
|
|
pub(crate) fn set_size(&mut self, size: u32) {
|
|
|
|
self.size = size;
|
|
|
|
}
|
2017-10-25 23:20:27 +08:00
|
|
|
|
2017-10-09 20:59:52 +08:00
|
|
|
pub fn is_dir(&self) -> bool {
|
|
|
|
self.attrs.contains(FileAttributes::DIRECTORY)
|
|
|
|
}
|
2017-10-25 23:20:27 +08:00
|
|
|
|
2017-10-09 20:59:52 +08:00
|
|
|
pub fn is_file(&self) -> bool {
|
|
|
|
!self.is_dir()
|
|
|
|
}
|
2017-10-25 23:20:27 +08:00
|
|
|
|
2017-10-09 20:59:52 +08:00
|
|
|
pub(crate) fn set_modified(&mut self, date_time: DateTime) {
|
|
|
|
self.modify_date = date_time.date.to_u16();
|
|
|
|
self.modify_time = date_time.time.to_u16();
|
|
|
|
}
|
2017-10-25 23:20:27 +08:00
|
|
|
|
2017-10-09 20:59:52 +08:00
|
|
|
pub(crate) fn serialize(&self, wrt: &mut Write) -> io::Result<()> {
|
2017-10-10 22:05:19 +08:00
|
|
|
wrt.write_all(&self.name)?;
|
2017-10-09 20:59:52 +08:00
|
|
|
wrt.write_u8(self.attrs.bits())?;
|
|
|
|
wrt.write_u8(self.reserved_0)?;
|
|
|
|
wrt.write_u8(self.create_time_0)?;
|
|
|
|
wrt.write_u16::<LittleEndian>(self.create_time_1)?;
|
|
|
|
wrt.write_u16::<LittleEndian>(self.create_date)?;
|
|
|
|
wrt.write_u16::<LittleEndian>(self.access_date)?;
|
|
|
|
wrt.write_u16::<LittleEndian>(self.first_cluster_hi)?;
|
|
|
|
wrt.write_u16::<LittleEndian>(self.modify_time)?;
|
|
|
|
wrt.write_u16::<LittleEndian>(self.modify_date)?;
|
|
|
|
wrt.write_u16::<LittleEndian>(self.first_cluster_lo)?;
|
|
|
|
wrt.write_u32::<LittleEndian>(self.size)?;
|
|
|
|
Ok(())
|
|
|
|
}
|
2017-10-25 23:20:27 +08:00
|
|
|
|
2017-10-21 21:51:19 +08:00
|
|
|
fn is_free(&self) -> bool {
|
|
|
|
self.name[0] == DIR_ENTRY_FREE_FLAG
|
2017-10-16 04:42:26 +08:00
|
|
|
}
|
2017-10-25 23:20:27 +08:00
|
|
|
|
2017-10-16 04:42:26 +08:00
|
|
|
fn is_end(&self) -> bool {
|
|
|
|
self.name[0] == 0
|
|
|
|
}
|
2017-09-23 02:50:41 +08:00
|
|
|
}
|
|
|
|
|
2017-09-25 04:12:38 +08:00
|
|
|
#[allow(dead_code)]
|
|
|
|
#[derive(Clone, Debug, Default)]
|
2017-10-07 20:56:50 +08:00
|
|
|
struct DirLfnEntryData {
|
2017-09-25 04:12:38 +08:00
|
|
|
order: u8,
|
|
|
|
name_0: [u16; 5],
|
2017-10-07 20:56:50 +08:00
|
|
|
attrs: FileAttributes,
|
2017-09-25 04:12:38 +08:00
|
|
|
entry_type: u8,
|
|
|
|
checksum: u8,
|
|
|
|
name_1: [u16; 6],
|
|
|
|
reserved_0: u16,
|
|
|
|
name_2: [u16; 2],
|
|
|
|
}
|
|
|
|
|
2017-10-16 04:42:26 +08:00
|
|
|
impl DirLfnEntryData {
|
|
|
|
fn serialize(&self, wrt: &mut Write) -> io::Result<()> {
|
|
|
|
wrt.write_u8(self.order)?;
|
|
|
|
for ch in self.name_0.iter() {
|
|
|
|
wrt.write_u16::<LittleEndian>(*ch)?;
|
|
|
|
}
|
|
|
|
wrt.write_u8(self.attrs.bits())?;
|
|
|
|
wrt.write_u8(self.entry_type)?;
|
|
|
|
wrt.write_u8(self.checksum)?;
|
|
|
|
for ch in self.name_1.iter() {
|
|
|
|
wrt.write_u16::<LittleEndian>(*ch)?;
|
|
|
|
}
|
|
|
|
wrt.write_u16::<LittleEndian>(self.reserved_0)?;
|
|
|
|
for ch in self.name_2.iter() {
|
|
|
|
wrt.write_u16::<LittleEndian>(*ch)?;
|
|
|
|
}
|
|
|
|
Ok(())
|
|
|
|
}
|
2017-10-25 23:20:27 +08:00
|
|
|
|
2017-10-21 21:51:19 +08:00
|
|
|
fn is_free(&self) -> bool {
|
|
|
|
self.order == DIR_ENTRY_FREE_FLAG
|
2017-10-16 04:42:26 +08:00
|
|
|
}
|
2017-10-25 23:20:27 +08:00
|
|
|
|
2017-10-21 21:35:26 +08:00
|
|
|
fn is_end(&self) -> bool {
|
|
|
|
self.order == 0
|
|
|
|
}
|
2017-10-16 04:42:26 +08:00
|
|
|
}
|
|
|
|
|
2017-09-25 04:12:38 +08:00
|
|
|
#[derive(Clone, Debug)]
|
2017-10-07 20:56:50 +08:00
|
|
|
enum DirEntryData {
|
|
|
|
File(DirFileEntryData),
|
|
|
|
Lfn(DirLfnEntryData),
|
2017-09-25 04:12:38 +08:00
|
|
|
}
|
|
|
|
|
2017-10-16 04:42:26 +08:00
|
|
|
impl DirEntryData {
|
|
|
|
fn serialize(&mut self, wrt: &mut Write) -> io::Result<()> {
|
|
|
|
match self {
|
|
|
|
&mut DirEntryData::File(ref mut file) => file.serialize(wrt),
|
|
|
|
&mut DirEntryData::Lfn(ref mut lfn) => lfn.serialize(wrt),
|
|
|
|
}
|
|
|
|
}
|
2017-10-25 23:20:27 +08:00
|
|
|
|
2017-10-16 04:42:26 +08:00
|
|
|
fn deserialize(rdr: &mut Read) -> io::Result<DirEntryData> {
|
|
|
|
let mut name = [0; 11];
|
2017-10-27 21:03:54 +08:00
|
|
|
match rdr.read_exact(&mut name) {
|
|
|
|
Err(ref err) if err.kind() == io::ErrorKind::UnexpectedEof => {
|
|
|
|
return Ok(DirEntryData::File(DirFileEntryData {
|
|
|
|
..Default::default()
|
|
|
|
}));
|
|
|
|
}
|
|
|
|
Err(err) => return Err(err),
|
|
|
|
_ => {},
|
|
|
|
}
|
2017-10-16 04:42:26 +08:00
|
|
|
let attrs = FileAttributes::from_bits_truncate(rdr.read_u8()?);
|
2017-10-21 21:51:19 +08:00
|
|
|
if attrs & FileAttributes::LFN == FileAttributes::LFN {
|
2017-10-16 04:42:26 +08:00
|
|
|
let mut data = DirLfnEntryData {
|
|
|
|
attrs, ..Default::default()
|
|
|
|
};
|
|
|
|
let mut cur = Cursor::new(&name);
|
|
|
|
data.order = cur.read_u8()?;
|
|
|
|
cur.read_u16_into::<LittleEndian>(&mut data.name_0)?;
|
|
|
|
data.entry_type = rdr.read_u8()?;
|
|
|
|
data.checksum = rdr.read_u8()?;
|
|
|
|
rdr.read_u16_into::<LittleEndian>(&mut data.name_1)?;
|
|
|
|
data.reserved_0 = rdr.read_u16::<LittleEndian>()?;
|
|
|
|
rdr.read_u16_into::<LittleEndian>(&mut data.name_2)?;
|
|
|
|
Ok(DirEntryData::Lfn(data))
|
|
|
|
} else {
|
|
|
|
let data = DirFileEntryData {
|
|
|
|
name,
|
|
|
|
attrs,
|
|
|
|
reserved_0: rdr.read_u8()?,
|
|
|
|
create_time_0: rdr.read_u8()?,
|
|
|
|
create_time_1: rdr.read_u16::<LittleEndian>()?,
|
|
|
|
create_date: rdr.read_u16::<LittleEndian>()?,
|
|
|
|
access_date: rdr.read_u16::<LittleEndian>()?,
|
|
|
|
first_cluster_hi: rdr.read_u16::<LittleEndian>()?,
|
|
|
|
modify_time: rdr.read_u16::<LittleEndian>()?,
|
|
|
|
modify_date: rdr.read_u16::<LittleEndian>()?,
|
|
|
|
first_cluster_lo: rdr.read_u16::<LittleEndian>()?,
|
|
|
|
size: rdr.read_u32::<LittleEndian>()?,
|
|
|
|
};
|
|
|
|
Ok(DirEntryData::File(data))
|
|
|
|
}
|
|
|
|
}
|
2017-10-25 23:20:27 +08:00
|
|
|
|
2017-10-21 21:51:19 +08:00
|
|
|
fn is_free(&self) -> bool {
|
2017-10-21 21:35:26 +08:00
|
|
|
match self {
|
2017-10-21 21:51:19 +08:00
|
|
|
&DirEntryData::File(ref file) => file.is_free(),
|
|
|
|
&DirEntryData::Lfn(ref lfn) => lfn.is_free(),
|
2017-10-21 21:35:26 +08:00
|
|
|
}
|
|
|
|
}
|
2017-10-25 23:20:27 +08:00
|
|
|
|
2017-10-21 21:35:26 +08:00
|
|
|
fn is_end(&self) -> bool {
|
|
|
|
match self {
|
|
|
|
&DirEntryData::File(ref file) => file.is_end(),
|
|
|
|
&DirEntryData::Lfn(ref lfn) => lfn.is_end(),
|
|
|
|
}
|
|
|
|
}
|
2017-10-16 04:42:26 +08:00
|
|
|
}
|
|
|
|
|
2017-10-21 22:25:04 +08:00
|
|
|
/// DOS compatible date
|
2017-10-07 20:56:50 +08:00
|
|
|
#[derive(Clone, Copy, Debug)]
|
|
|
|
pub struct Date {
|
2017-10-06 22:42:29 +08:00
|
|
|
pub year: u16,
|
|
|
|
pub month: u16,
|
|
|
|
pub day: u16,
|
|
|
|
}
|
|
|
|
|
2017-10-07 20:56:50 +08:00
|
|
|
impl Date {
|
2017-10-10 22:05:19 +08:00
|
|
|
pub(crate) fn from_u16(dos_date: u16) -> Self {
|
2017-10-06 22:42:29 +08:00
|
|
|
let (year, month, day) = ((dos_date >> 9) + 1980, (dos_date >> 5) & 0xF, dos_date & 0x1F);
|
2017-10-07 20:56:50 +08:00
|
|
|
Date { year, month, day }
|
2017-10-06 22:42:29 +08:00
|
|
|
}
|
2017-10-25 23:20:27 +08:00
|
|
|
|
2017-10-09 20:59:52 +08:00
|
|
|
fn to_u16(&self) -> u16 {
|
|
|
|
((self.year - 1980) << 9) | (self.month << 5) | self.day
|
|
|
|
}
|
2017-10-06 22:42:29 +08:00
|
|
|
}
|
|
|
|
|
2017-10-21 22:25:04 +08:00
|
|
|
/// DOS compatible time
|
2017-10-07 20:56:50 +08:00
|
|
|
#[derive(Clone, Copy, Debug)]
|
|
|
|
pub struct Time {
|
|
|
|
pub hour: u16,
|
|
|
|
pub min: u16,
|
|
|
|
pub sec: u16,
|
|
|
|
}
|
|
|
|
|
|
|
|
impl Time {
|
2017-10-10 22:05:19 +08:00
|
|
|
pub(crate) fn from_u16(dos_time: u16) -> Self {
|
2017-10-06 22:42:29 +08:00
|
|
|
let (hour, min, sec) = (dos_time >> 11, (dos_time >> 5) & 0x3F, (dos_time & 0x1F) * 2);
|
2017-10-07 20:56:50 +08:00
|
|
|
Time { hour, min, sec }
|
2017-10-06 22:42:29 +08:00
|
|
|
}
|
2017-10-25 23:20:27 +08:00
|
|
|
|
2017-10-09 20:59:52 +08:00
|
|
|
fn to_u16(&self) -> u16 {
|
|
|
|
(self.hour << 11) | (self.min << 5) | (self.sec / 2)
|
|
|
|
}
|
2017-10-06 22:42:29 +08:00
|
|
|
}
|
|
|
|
|
2017-10-21 22:25:04 +08:00
|
|
|
/// DOS compatible date and time
|
2017-10-07 20:56:50 +08:00
|
|
|
#[derive(Clone, Copy, Debug)]
|
|
|
|
pub struct DateTime {
|
|
|
|
pub date: Date,
|
|
|
|
pub time: Time,
|
|
|
|
}
|
|
|
|
|
|
|
|
impl DateTime {
|
2017-10-10 22:05:19 +08:00
|
|
|
pub(crate) fn from_u16(dos_date: u16, dos_time: u16) -> Self {
|
2017-10-07 20:56:50 +08:00
|
|
|
DateTime {
|
2017-10-10 22:05:19 +08:00
|
|
|
date: Date::from_u16(dos_date),
|
|
|
|
time: Time::from_u16(dos_time),
|
2017-10-06 22:42:29 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
#[cfg(feature = "chrono")]
|
2017-10-07 20:56:50 +08:00
|
|
|
impl From<Date> for chrono::Date<Local> {
|
|
|
|
fn from(date: Date) -> Self {
|
2017-10-06 22:42:29 +08:00
|
|
|
Local.ymd(date.year as i32, date.month as u32, date.day as u32)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
#[cfg(feature = "chrono")]
|
2017-10-07 20:56:50 +08:00
|
|
|
impl From<DateTime> for chrono::DateTime<Local> {
|
|
|
|
fn from(date_time: DateTime) -> Self {
|
|
|
|
chrono::Date::<Local>::from(date_time.date)
|
2017-10-06 22:42:29 +08:00
|
|
|
.and_hms(date_time.time.hour as u32, date_time.time.min as u32, date_time.time.sec as u32)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-10-09 20:59:52 +08:00
|
|
|
#[derive(Clone, Debug)]
|
|
|
|
pub(crate) struct FileEntryInfo {
|
|
|
|
pub(crate) data: DirFileEntryData,
|
|
|
|
pos: u64,
|
|
|
|
}
|
|
|
|
|
|
|
|
impl FileEntryInfo {
|
|
|
|
pub(crate) fn write(&self, fs: FileSystemRef) -> io::Result<()> {
|
|
|
|
let mut disk = fs.disk.borrow_mut();
|
|
|
|
disk.seek(io::SeekFrom::Start(self.pos))?;
|
|
|
|
self.data.serialize(&mut *disk)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-10-21 22:25:04 +08:00
|
|
|
/// FAT directory entry.
|
|
|
|
///
|
|
|
|
/// Returned by DirIter.
|
2017-10-09 20:59:52 +08:00
|
|
|
#[derive(Clone)]
|
|
|
|
pub struct DirEntry<'a, 'b: 'a> {
|
|
|
|
data: DirFileEntryData,
|
|
|
|
lfn: Vec<u16>,
|
|
|
|
entry_pos: u64,
|
2017-10-16 04:42:26 +08:00
|
|
|
offset_range: (u64, u64),
|
2017-10-09 20:59:52 +08:00
|
|
|
fs: FileSystemRef<'a, 'b>,
|
|
|
|
}
|
|
|
|
|
2017-10-07 20:56:50 +08:00
|
|
|
impl <'a, 'b> DirEntry<'a, 'b> {
|
2017-10-21 22:25:04 +08:00
|
|
|
/// Returns short file name
|
2017-09-25 04:12:38 +08:00
|
|
|
pub fn short_file_name(&self) -> String {
|
2017-10-06 21:51:01 +08:00
|
|
|
let name_str = String::from_utf8_lossy(&self.data.name[0..8]);
|
|
|
|
let ext_str = String::from_utf8_lossy(&self.data.name[8..11]);
|
|
|
|
let name_trimmed = name_str.trim_right();
|
|
|
|
let ext_trimmed = ext_str.trim_right();
|
|
|
|
if ext_trimmed.is_empty() {
|
|
|
|
name_trimmed.to_string()
|
|
|
|
} else {
|
|
|
|
format!("{}.{}", name_trimmed, ext_trimmed)
|
|
|
|
}
|
2017-09-23 05:36:44 +08:00
|
|
|
}
|
2017-10-25 23:20:27 +08:00
|
|
|
|
2017-10-21 22:25:04 +08:00
|
|
|
/// Returns long file name or if it doesn't exist fallbacks to short file name.
|
2017-09-25 04:12:38 +08:00
|
|
|
pub fn file_name(&self) -> String {
|
|
|
|
if self.lfn.len() > 0 {
|
2017-10-06 21:51:01 +08:00
|
|
|
String::from_utf16_lossy(&self.lfn)
|
2017-09-25 04:12:38 +08:00
|
|
|
} else {
|
|
|
|
self.short_file_name()
|
|
|
|
}
|
|
|
|
}
|
2017-10-25 23:20:27 +08:00
|
|
|
|
2017-10-21 22:25:04 +08:00
|
|
|
/// Returns file attributes
|
2017-10-07 20:56:50 +08:00
|
|
|
pub fn attributes(&self) -> FileAttributes {
|
2017-09-24 09:08:00 +08:00
|
|
|
self.data.attrs
|
2017-09-23 04:27:39 +08:00
|
|
|
}
|
2017-10-25 23:20:27 +08:00
|
|
|
|
2017-10-21 22:25:04 +08:00
|
|
|
/// Checks if entry belongs to directory.
|
2017-09-24 06:05:43 +08:00
|
|
|
pub fn is_dir(&self) -> bool {
|
2017-10-10 22:05:19 +08:00
|
|
|
self.data.is_dir()
|
2017-09-24 06:05:43 +08:00
|
|
|
}
|
2017-10-25 23:20:27 +08:00
|
|
|
|
2017-10-21 22:25:04 +08:00
|
|
|
/// Checks if entry belongs to regular file.
|
2017-09-24 20:34:07 +08:00
|
|
|
pub fn is_file(&self) -> bool {
|
2017-10-10 22:05:19 +08:00
|
|
|
self.data.is_file()
|
2017-09-24 20:34:07 +08:00
|
|
|
}
|
2017-10-25 23:20:27 +08:00
|
|
|
|
2017-10-07 08:37:29 +08:00
|
|
|
pub(crate) fn first_cluster(&self) -> Option<u32> {
|
2017-11-08 06:17:52 +08:00
|
|
|
self.data.first_cluster(self.fs.fat_type)
|
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 entry_info(&self) -> FileEntryInfo {
|
|
|
|
FileEntryInfo {
|
|
|
|
data: self.data.clone(),
|
|
|
|
pos: self.entry_pos,
|
|
|
|
}
|
2017-09-23 04:27:39 +08:00
|
|
|
}
|
2017-10-25 23:20:27 +08:00
|
|
|
|
2017-10-21 22:25:04 +08:00
|
|
|
/// Returns File struct for this entry.
|
|
|
|
///
|
|
|
|
/// Panics if this is not a file.
|
2017-10-07 20:56:50 +08:00
|
|
|
pub fn to_file(&self) -> File<'a, 'b> {
|
2017-10-07 22:31:42 +08:00
|
|
|
assert!(!self.is_dir(), "Not a file entry");
|
2017-10-09 20:59:52 +08:00
|
|
|
File::new(self.first_cluster(), Some(self.entry_info()), self.fs)
|
2017-09-23 04:27:39 +08:00
|
|
|
}
|
2017-10-25 23:20:27 +08:00
|
|
|
|
2017-10-21 22:25:04 +08:00
|
|
|
/// Returns Dir struct for this entry.
|
|
|
|
///
|
|
|
|
/// Panics if this is not a directory.
|
2017-10-07 20:56:50 +08:00
|
|
|
pub fn to_dir(&self) -> Dir<'a, 'b> {
|
2017-10-07 22:31:42 +08:00
|
|
|
assert!(self.is_dir(), "Not a directory entry");
|
|
|
|
match self.first_cluster() {
|
|
|
|
Some(n) => {
|
2017-10-09 20:59:52 +08:00
|
|
|
let file = File::new(Some(n), Some(self.entry_info()), self.fs);
|
2017-10-07 22:31:42 +08:00
|
|
|
Dir::new(DirRawStream::File(file), self.fs)
|
|
|
|
},
|
|
|
|
None => self.fs.root_dir(),
|
2017-09-24 06:05:43 +08:00
|
|
|
}
|
|
|
|
}
|
2017-10-25 23:20:27 +08:00
|
|
|
|
2017-10-21 22:25:04 +08:00
|
|
|
/// Returns file size or 0 for directory.
|
2017-09-24 20:34:07 +08:00
|
|
|
pub fn len(&self) -> u64 {
|
|
|
|
self.data.size as u64
|
2017-09-23 04:27:39 +08:00
|
|
|
}
|
2017-10-25 23:20:27 +08:00
|
|
|
|
2017-10-21 22:25:04 +08:00
|
|
|
/// Returns file creation date and time.
|
2017-10-07 20:56:50 +08:00
|
|
|
pub fn created(&self) -> DateTime {
|
2017-10-10 22:05:19 +08:00
|
|
|
DateTime::from_u16(self.data.create_date, self.data.create_time_1)
|
2017-09-23 04:27:39 +08:00
|
|
|
}
|
2017-10-25 23:20:27 +08:00
|
|
|
|
2017-10-21 22:25:04 +08:00
|
|
|
/// Returns file last access date.
|
2017-10-07 20:56:50 +08:00
|
|
|
pub fn accessed(&self) -> Date {
|
2017-10-10 22:05:19 +08:00
|
|
|
Date::from_u16(self.data.access_date)
|
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 file last modification date and time.
|
2017-10-07 20:56:50 +08:00
|
|
|
pub fn modified(&self) -> DateTime {
|
2017-10-10 22:05:19 +08:00
|
|
|
DateTime::from_u16(self.data.modify_date, self.data.modify_time)
|
2017-09-24 09:08:00 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-10-07 20:56:50 +08:00
|
|
|
impl <'a, 'b> fmt::Debug for DirEntry<'a, 'b> {
|
2017-09-24 09:08:00 +08:00
|
|
|
fn fmt(&self, f: &mut fmt::Formatter) -> Result<(), fmt::Error> {
|
|
|
|
self.data.fmt(f)
|
2017-09-23 04:27:39 +08:00
|
|
|
}
|
2017-09-23 02:50:41 +08:00
|
|
|
}
|
|
|
|
|
2017-10-21 22:25:04 +08:00
|
|
|
/// FAT directory
|
2017-10-06 22:07:11 +08:00
|
|
|
#[derive(Clone)]
|
2017-10-07 20:56:50 +08:00
|
|
|
pub struct Dir<'a, 'b: 'a> {
|
2017-10-21 21:51:19 +08:00
|
|
|
stream: DirRawStream<'a, 'b>,
|
2017-10-07 20:56:50 +08:00
|
|
|
fs: FileSystemRef<'a, 'b>,
|
2017-09-24 01:42:09 +08:00
|
|
|
}
|
|
|
|
|
2017-10-07 20:56:50 +08:00
|
|
|
impl <'a, 'b> Dir<'a, 'b> {
|
2017-10-25 23:20:27 +08:00
|
|
|
|
2017-10-21 21:51:19 +08:00
|
|
|
pub(crate) fn new(stream: DirRawStream<'a, 'b>, fs: FileSystemRef<'a, 'b>) -> Dir<'a, 'b> {
|
|
|
|
Dir { stream, fs }
|
2017-09-24 01:42:09 +08:00
|
|
|
}
|
2017-10-25 23:20:27 +08:00
|
|
|
|
2017-10-21 22:25:04 +08:00
|
|
|
/// Creates directory entries iterator
|
2017-10-07 20:56:50 +08:00
|
|
|
pub fn iter(&self) -> DirIter<'a, 'b> {
|
|
|
|
DirIter {
|
2017-10-21 21:51:19 +08:00
|
|
|
stream: self.stream.clone(),
|
2017-10-06 22:07:11 +08:00
|
|
|
fs: self.fs.clone(),
|
2017-10-07 08:16:34 +08:00
|
|
|
err: false,
|
2017-10-06 22:07:11 +08:00
|
|
|
}
|
|
|
|
}
|
2017-10-25 23:20:27 +08:00
|
|
|
|
2017-10-06 22:07:11 +08:00
|
|
|
fn split_path<'c>(path: &'c str) -> (&'c str, Option<&'c str>) {
|
|
|
|
let mut path_split = path.trim_matches('/').splitn(2, "/");
|
2017-10-07 22:31:42 +08:00
|
|
|
let comp = path_split.next().unwrap(); // safe unwrap - splitn always returns at least one element
|
2017-10-06 22:07:11 +08:00
|
|
|
let rest_opt = path_split.next();
|
|
|
|
(comp, rest_opt)
|
|
|
|
}
|
2017-10-25 23:20:27 +08:00
|
|
|
|
2017-10-07 20:56:50 +08:00
|
|
|
fn find_entry(&mut self, name: &str) -> io::Result<DirEntry<'a, 'b>> {
|
2017-10-06 22:07:11 +08:00
|
|
|
for r in self.iter() {
|
|
|
|
let e = r?;
|
|
|
|
if e.file_name().eq_ignore_ascii_case(name) {
|
|
|
|
return Ok(e);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
Err(io::Error::new(ErrorKind::NotFound, "file not found"))
|
2017-09-24 08:10:59 +08:00
|
|
|
}
|
2017-10-25 23:20:27 +08:00
|
|
|
|
2017-10-21 22:25:04 +08:00
|
|
|
/// Opens existing directory
|
2017-10-07 20:56:50 +08:00
|
|
|
pub fn open_dir(&mut self, path: &str) -> io::Result<Dir<'a, 'b>> {
|
2017-10-06 22:07:11 +08:00
|
|
|
let (name, rest_opt) = Self::split_path(path);
|
|
|
|
let e = self.find_entry(name)?;
|
|
|
|
match rest_opt {
|
|
|
|
Some(rest) => e.to_dir().open_dir(rest),
|
|
|
|
None => Ok(e.to_dir())
|
|
|
|
}
|
|
|
|
}
|
2017-10-25 23:20:27 +08:00
|
|
|
|
2017-10-21 22:25:04 +08:00
|
|
|
/// Opens existing file.
|
2017-10-07 20:56:50 +08:00
|
|
|
pub fn open_file(&mut self, path: &str) -> io::Result<File<'a, 'b>> {
|
2017-10-06 22:07:11 +08:00
|
|
|
let (name, rest_opt) = Self::split_path(path);
|
|
|
|
let e = self.find_entry(name)?;
|
|
|
|
match rest_opt {
|
|
|
|
Some(rest) => e.to_dir().open_file(rest),
|
|
|
|
None => Ok(e.to_file())
|
|
|
|
}
|
|
|
|
}
|
2017-10-25 23:20:27 +08:00
|
|
|
|
2017-10-21 22:25:04 +08:00
|
|
|
/// Creates new file or opens existing.
|
2017-10-21 21:35:26 +08:00
|
|
|
pub fn create_file(&mut self, path: &str) -> io::Result<File<'a, 'b>> {
|
|
|
|
let (name, rest_opt) = Self::split_path(path);
|
|
|
|
let r = self.find_entry(name);
|
|
|
|
match rest_opt {
|
|
|
|
Some(rest) => r?.to_dir().create_file(rest),
|
|
|
|
None => {
|
|
|
|
match r {
|
2017-11-07 08:06:52 +08:00
|
|
|
Err(ref err) if err.kind() == ErrorKind::NotFound =>
|
|
|
|
Ok(self.create_entry(name, FileAttributes::from_bits_truncate(0), None)?.to_file()),
|
|
|
|
Err(err) => Err(err),
|
|
|
|
Ok(e) => Ok(e.to_file()),
|
2017-10-21 21:35:26 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2017-10-25 23:20:27 +08:00
|
|
|
|
2017-10-27 20:12:53 +08:00
|
|
|
/// Creates new directory or opens existing.
|
|
|
|
pub fn create_dir(&mut self, path: &str) -> io::Result<Dir<'a, 'b>> {
|
|
|
|
let (name, rest_opt) = Self::split_path(path);
|
|
|
|
let r = self.find_entry(name);
|
|
|
|
match rest_opt {
|
|
|
|
Some(rest) => r?.to_dir().create_dir(rest),
|
|
|
|
None => {
|
|
|
|
match r {
|
2017-11-07 08:06:52 +08:00
|
|
|
Err(ref err) if err.kind() == ErrorKind::NotFound => {
|
2017-10-27 20:12:53 +08:00
|
|
|
let cluster = self.fs.alloc_cluster(None)?;
|
|
|
|
let entry = self.create_entry(name, FileAttributes::DIRECTORY, Some(cluster))?;
|
|
|
|
let mut dir = entry.to_dir();
|
|
|
|
dir.create_entry(".", FileAttributes::DIRECTORY, entry.first_cluster())?;
|
|
|
|
dir.create_entry("..", FileAttributes::DIRECTORY, self.stream.first_cluster())?;
|
|
|
|
Ok(dir)
|
|
|
|
},
|
2017-11-07 08:06:52 +08:00
|
|
|
Err(err) => Err(err),
|
2017-10-27 20:12:53 +08:00
|
|
|
Ok(e) => Ok(e.to_dir()),
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-10-16 04:42:26 +08:00
|
|
|
fn is_empty(&mut self) -> io::Result<bool> {
|
|
|
|
for r in self.iter() {
|
|
|
|
let e = r?;
|
|
|
|
let name = e.file_name();
|
|
|
|
if name != "." && name != ".." {
|
|
|
|
return Ok(false);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
Ok(true)
|
|
|
|
}
|
2017-10-25 23:20:27 +08:00
|
|
|
|
2017-10-21 22:25:04 +08:00
|
|
|
/// Removes existing file or directory.
|
|
|
|
///
|
|
|
|
/// Make sure there is no reference to this file (no File instance) or filesystem corruption
|
|
|
|
/// can happen.
|
2017-10-16 04:42:26 +08:00
|
|
|
pub fn remove(&mut self, path: &str) -> io::Result<()> {
|
|
|
|
let (name, rest_opt) = Self::split_path(path);
|
|
|
|
let e = self.find_entry(name)?;
|
|
|
|
match rest_opt {
|
|
|
|
Some(rest) => e.to_dir().remove(rest),
|
|
|
|
None => {
|
|
|
|
trace!("removing {}", path);
|
|
|
|
if e.is_dir() && !e.to_dir().is_empty()? {
|
|
|
|
return Err(io::Error::new(ErrorKind::NotFound, "removing non-empty directory is denied"));
|
|
|
|
}
|
|
|
|
match e.first_cluster() {
|
|
|
|
Some(n) => self.fs.cluster_iter(n).free()?,
|
|
|
|
_ => {},
|
|
|
|
}
|
2017-10-21 21:51:19 +08:00
|
|
|
let mut stream = self.stream.clone();
|
2017-10-16 04:42:26 +08:00
|
|
|
stream.seek(SeekFrom::Start(e.offset_range.0 as u64))?;
|
|
|
|
let num = (e.offset_range.1 - e.offset_range.0) as usize / DIR_ENTRY_SIZE as usize;
|
|
|
|
for _ in 0..num {
|
|
|
|
let mut data = DirEntryData::deserialize(&mut stream)?;
|
|
|
|
trace!("removing dir entry {:?}", data);
|
|
|
|
match data {
|
|
|
|
DirEntryData::File(ref mut data) =>
|
2017-10-21 21:51:19 +08:00
|
|
|
data.name[0] = DIR_ENTRY_FREE_FLAG,
|
|
|
|
DirEntryData::Lfn(ref mut data) => data.order = DIR_ENTRY_FREE_FLAG,
|
2017-10-16 04:42:26 +08:00
|
|
|
};
|
|
|
|
stream.seek(SeekFrom::Current(-(DIR_ENTRY_SIZE as i64)))?;
|
|
|
|
data.serialize(&mut stream)?;
|
|
|
|
}
|
|
|
|
Ok(())
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2017-10-25 23:20:27 +08:00
|
|
|
|
2017-10-21 21:35:26 +08:00
|
|
|
fn find_free_entries(&mut self, num_entries: usize) -> io::Result<DirRawStream<'a, 'b>> {
|
2017-10-21 21:51:19 +08:00
|
|
|
let mut stream = self.stream.clone();
|
2017-10-21 21:35:26 +08:00
|
|
|
let mut first_free = 0;
|
|
|
|
let mut num_free = 0;
|
|
|
|
let mut i = 0;
|
|
|
|
loop {
|
2017-10-27 21:03:54 +08:00
|
|
|
let raw_entry = DirEntryData::deserialize(&mut stream)?;
|
|
|
|
if raw_entry.is_end() {
|
|
|
|
if num_free == 0 {
|
|
|
|
first_free = i;
|
|
|
|
}
|
|
|
|
stream.seek(io::SeekFrom::Start(first_free as u64 * DIR_ENTRY_SIZE))?;
|
|
|
|
return Ok(stream);
|
|
|
|
} else if raw_entry.is_free() {
|
2017-10-21 21:35:26 +08:00
|
|
|
if num_free == 0 {
|
|
|
|
first_free = i;
|
|
|
|
}
|
|
|
|
num_free += 1;
|
|
|
|
if num_free == num_entries {
|
|
|
|
stream.seek(io::SeekFrom::Start(first_free as u64 * DIR_ENTRY_SIZE))?;
|
|
|
|
return Ok(stream);
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
num_free = 0;
|
|
|
|
}
|
|
|
|
i += 1;
|
|
|
|
}
|
|
|
|
}
|
2017-10-25 23:20:27 +08:00
|
|
|
|
2017-11-08 07:07:06 +08:00
|
|
|
fn copy_short_name_part(dst: &mut [u8], src: &str) {
|
|
|
|
let mut j = 0;
|
|
|
|
for c in src.chars() {
|
|
|
|
if j == dst.len() { break; }
|
|
|
|
// replace characters allowed in long name but disallowed in short
|
|
|
|
let c2 = match c {
|
|
|
|
'.' | ' ' | '+' | ',' | ';' | '=' | '[' | ']' => '?',
|
|
|
|
_ if c < '\u{80}' => c,
|
|
|
|
_ => '?',
|
|
|
|
};
|
|
|
|
// short name is always uppercase
|
|
|
|
let upper = c2.to_uppercase().next().unwrap(); // SAFE: uppercase must return at least one character
|
|
|
|
let byte = upper as u8; // SAFE: upper is in range 0x20-0x7F
|
|
|
|
dst[j] = byte;
|
|
|
|
j += 1;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-10-21 21:35:26 +08:00
|
|
|
fn gen_short_name(name: &str) -> [u8;11] {
|
|
|
|
// padded by ' '
|
|
|
|
let mut short_name = [0x20u8; 11];
|
|
|
|
// find extension after last dot
|
2017-11-08 07:07:06 +08:00
|
|
|
match name.rfind('.') {
|
2017-10-21 21:35:26 +08:00
|
|
|
Some(index) => {
|
2017-11-08 07:07:06 +08:00
|
|
|
// extension found - copy parts before and after dot
|
|
|
|
Dir::copy_short_name_part(&mut short_name[0..8], &name[..index]);
|
|
|
|
Dir::copy_short_name_part(&mut short_name[8..11], &name[index+1..]);
|
2017-10-21 21:35:26 +08:00
|
|
|
},
|
2017-11-08 07:07:06 +08:00
|
|
|
None => {
|
|
|
|
// no extension - copy name and leave extension empty
|
|
|
|
Dir::copy_short_name_part(&mut short_name[0..8], &name);
|
|
|
|
}
|
2017-10-21 21:35:26 +08:00
|
|
|
}
|
|
|
|
// FIXME: make sure short name is unique...
|
|
|
|
short_name
|
|
|
|
}
|
2017-10-25 23:20:27 +08:00
|
|
|
|
2017-11-08 06:17:52 +08:00
|
|
|
fn validate_name(name: &str) -> io::Result<()> {
|
|
|
|
if name.len() == 0 {
|
|
|
|
return Err(io::Error::new(ErrorKind::InvalidInput, "filename cannot be empty"));
|
|
|
|
}
|
2017-10-21 21:35:26 +08:00
|
|
|
if name.len() > 255 {
|
2017-11-08 06:17:52 +08:00
|
|
|
return Err(io::Error::new(ErrorKind::InvalidInput, "filename is too long"));
|
|
|
|
}
|
|
|
|
for c in name.chars() {
|
|
|
|
match c {
|
|
|
|
'a'...'z' | 'A'...'Z' | '0'...'9' | '\u{80}'...'\u{FFFF}' |
|
|
|
|
'$' | '%' | '\'' | '-' | '_' | '@' | '~' | '`' | '!' | '(' | ')' | '{' | '}' |
|
|
|
|
'.' | ' ' | '+' | ',' | ';' | '=' | '[' | ']' => {},
|
|
|
|
_ => return Err(io::Error::new(ErrorKind::InvalidInput, "invalid character in filename")),
|
|
|
|
}
|
2017-10-21 21:35:26 +08:00
|
|
|
}
|
2017-11-08 06:17:52 +08:00
|
|
|
Ok(())
|
|
|
|
}
|
|
|
|
|
|
|
|
fn create_entry(&mut self, name: &str, attrs: FileAttributes, first_cluster: Option<u32>) -> io::Result<DirEntry<'a, 'b>> {
|
2017-11-08 07:07:06 +08:00
|
|
|
trace!("create_entry {}", name);
|
2017-11-08 06:17:52 +08:00
|
|
|
Self::validate_name(name)?;
|
2017-10-21 21:35:26 +08:00
|
|
|
let num_lfn_entries = (name.len() + LFN_PART_LEN - 1) / LFN_PART_LEN;
|
|
|
|
let num_entries = num_lfn_entries + 1; // multiple lfn entries + one file entry
|
|
|
|
let mut stream = self.find_free_entries(num_entries)?;
|
|
|
|
let start_pos = stream.seek(io::SeekFrom::Current(0))?;
|
|
|
|
let short_name = Self::gen_short_name(name);
|
|
|
|
let lfn_chsum = lfn_checksum(&short_name);
|
|
|
|
let lfn_utf8 = name.encode_utf16().collect::<Vec<u16>>();
|
|
|
|
for i in 0..num_lfn_entries {
|
|
|
|
let lfn_index = num_lfn_entries - i;
|
|
|
|
let mut order = lfn_index as u8;
|
|
|
|
if i == 0 {
|
|
|
|
order |= LFN_ENTRY_LAST_FLAG;
|
|
|
|
}
|
|
|
|
debug_assert!(order > 0);
|
|
|
|
let lfn_pos = (lfn_index - 1) * LFN_PART_LEN;
|
|
|
|
let mut lfn_part = [0xFFFFu16; LFN_PART_LEN];
|
|
|
|
let lfn_part_len = cmp::min(name.len() - lfn_pos, LFN_PART_LEN);
|
|
|
|
lfn_part[..lfn_part_len].copy_from_slice(&lfn_utf8[lfn_pos..lfn_pos+lfn_part_len]);
|
|
|
|
if lfn_part_len < LFN_PART_LEN {
|
|
|
|
lfn_part[lfn_part_len] = 0;
|
|
|
|
}
|
|
|
|
let mut lfn_entry = DirLfnEntryData {
|
|
|
|
order,
|
|
|
|
attrs: FileAttributes::LFN,
|
|
|
|
checksum: lfn_chsum,
|
|
|
|
..Default::default()
|
|
|
|
};
|
|
|
|
lfn_entry.name_0.copy_from_slice(&lfn_part[0..5]);
|
|
|
|
lfn_entry.name_1.copy_from_slice(&lfn_part[5..5+6]);
|
|
|
|
lfn_entry.name_2.copy_from_slice(&lfn_part[11..11+2]);
|
|
|
|
lfn_entry.serialize(&mut stream)?;
|
|
|
|
}
|
2017-10-27 20:12:53 +08:00
|
|
|
let mut raw_entry = DirFileEntryData {
|
2017-10-21 21:35:26 +08:00
|
|
|
name: short_name,
|
2017-10-27 20:12:53 +08:00
|
|
|
attrs,
|
2017-10-21 21:35:26 +08:00
|
|
|
..Default::default()
|
|
|
|
};
|
2017-11-08 05:52:56 +08:00
|
|
|
raw_entry.set_first_cluster(first_cluster, self.fs.fat_type);
|
2017-10-21 21:35:26 +08:00
|
|
|
raw_entry.serialize(&mut stream)?;
|
|
|
|
let end_pos = stream.seek(io::SeekFrom::Current(0))?;
|
2017-10-21 21:51:19 +08:00
|
|
|
let abs_pos = stream.abs_pos().map(|p| p - DIR_ENTRY_SIZE);
|
2017-10-21 21:35:26 +08:00
|
|
|
return Ok(DirEntry {
|
|
|
|
data: raw_entry,
|
|
|
|
lfn: Vec::new(),
|
|
|
|
fs: self.fs,
|
|
|
|
entry_pos: abs_pos.unwrap(), // safe
|
|
|
|
offset_range: (start_pos, end_pos),
|
|
|
|
});
|
|
|
|
}
|
2017-10-06 22:07:11 +08:00
|
|
|
}
|
|
|
|
|
2017-10-21 22:25:04 +08:00
|
|
|
/// Directory entries iterator.
|
2017-10-06 22:07:11 +08:00
|
|
|
#[derive(Clone)]
|
2017-10-07 20:56:50 +08:00
|
|
|
pub struct DirIter<'a, 'b: 'a> {
|
2017-10-21 21:51:19 +08:00
|
|
|
stream: DirRawStream<'a, 'b>,
|
2017-10-07 20:56:50 +08:00
|
|
|
fs: FileSystemRef<'a, 'b>,
|
2017-10-07 08:16:34 +08:00
|
|
|
err: bool,
|
2017-10-06 22:07:11 +08:00
|
|
|
}
|
|
|
|
|
2017-10-07 20:56:50 +08:00
|
|
|
impl <'a, 'b> DirIter<'a, 'b> {
|
2017-10-16 04:42:26 +08:00
|
|
|
fn read_dir_entry(&mut self) -> io::Result<Option<DirEntry<'a, 'b>>> {
|
|
|
|
let mut lfn_buf = LongNameBuilder::new();
|
2017-10-21 21:51:19 +08:00
|
|
|
let mut offset = self.stream.seek(SeekFrom::Current(0))?;
|
2017-10-16 04:42:26 +08:00
|
|
|
let mut begin_offset = offset;
|
|
|
|
loop {
|
2017-10-27 21:03:54 +08:00
|
|
|
let raw_entry = DirEntryData::deserialize(&mut self.stream)?;
|
2017-10-16 04:42:26 +08:00
|
|
|
offset += DIR_ENTRY_SIZE;
|
|
|
|
match raw_entry {
|
|
|
|
DirEntryData::File(data) => {
|
|
|
|
// Check if this is end of dif
|
|
|
|
if data.is_end() {
|
|
|
|
return Ok(None);
|
|
|
|
}
|
|
|
|
// Check if this is deleted or volume ID entry
|
2017-10-21 21:51:19 +08:00
|
|
|
if data.is_free() || data.attrs.contains(FileAttributes::VOLUME_ID) {
|
2017-10-16 04:42:26 +08:00
|
|
|
lfn_buf.clear();
|
|
|
|
begin_offset = offset;
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
// Get entry position on volume
|
2017-10-21 21:51:19 +08:00
|
|
|
let entry_pos = self.stream.abs_pos().map(|p| p - DIR_ENTRY_SIZE);
|
2017-10-16 04:42:26 +08:00
|
|
|
// Check if LFN checksum is valid
|
|
|
|
lfn_buf.validate_chksum(&data.name);
|
|
|
|
return Ok(Some(DirEntry {
|
|
|
|
data,
|
|
|
|
lfn: lfn_buf.to_vec(),
|
|
|
|
fs: self.fs,
|
|
|
|
entry_pos: entry_pos.unwrap(), // safe
|
|
|
|
offset_range: (begin_offset, offset),
|
|
|
|
}));
|
|
|
|
},
|
|
|
|
DirEntryData::Lfn(data) => {
|
|
|
|
// Check if this is deleted entry
|
2017-10-21 21:51:19 +08:00
|
|
|
if data.is_free() {
|
2017-10-16 04:42:26 +08:00
|
|
|
lfn_buf.clear();
|
|
|
|
begin_offset = offset;
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
// Append to LFN buffer
|
|
|
|
lfn_buf.process(&data);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
impl <'a, 'b> Iterator for DirIter<'a, 'b> {
|
|
|
|
type Item = io::Result<DirEntry<'a, 'b>>;
|
|
|
|
|
|
|
|
fn next(&mut self) -> Option<Self::Item> {
|
|
|
|
if self.err {
|
|
|
|
return None;
|
|
|
|
}
|
|
|
|
let r = self.read_dir_entry();
|
|
|
|
match r {
|
|
|
|
Ok(Some(e)) => Some(Ok(e)),
|
|
|
|
Ok(None) => None,
|
|
|
|
Err(err) => {
|
|
|
|
self.err = true;
|
|
|
|
Some(Err(err))
|
|
|
|
},
|
2017-09-25 04:12:38 +08:00
|
|
|
}
|
2017-09-24 01:42:09 +08:00
|
|
|
}
|
2017-09-23 02:50:41 +08:00
|
|
|
}
|
2017-09-24 08:24:42 +08:00
|
|
|
|
2017-10-14 21:24:03 +08:00
|
|
|
struct LongNameBuilder {
|
|
|
|
buf: Vec<u16>,
|
|
|
|
chksum: u8,
|
|
|
|
index: u8,
|
|
|
|
}
|
|
|
|
|
|
|
|
fn lfn_checksum(short_name: &[u8]) -> u8 {
|
|
|
|
let mut chksum = 0u8;
|
|
|
|
for i in 0..11 {
|
|
|
|
chksum = (((chksum & 1) << 7) as u16 + (chksum >> 1) as u16 + short_name[i] as u16) as u8;
|
|
|
|
}
|
|
|
|
chksum
|
|
|
|
}
|
|
|
|
|
|
|
|
impl LongNameBuilder {
|
|
|
|
fn new() -> LongNameBuilder {
|
|
|
|
LongNameBuilder {
|
|
|
|
buf: Vec::<u16>::new(),
|
|
|
|
chksum: 0,
|
|
|
|
index: 0,
|
|
|
|
}
|
|
|
|
}
|
2017-10-25 23:20:27 +08:00
|
|
|
|
2017-10-14 21:24:03 +08:00
|
|
|
fn clear(&mut self) {
|
|
|
|
self.buf.clear();
|
|
|
|
self.index = 0;
|
|
|
|
}
|
2017-10-25 23:20:27 +08:00
|
|
|
|
2017-10-14 21:24:03 +08:00
|
|
|
fn to_vec(mut self) -> Vec<u16> {
|
|
|
|
if self.index == 1 {
|
|
|
|
self.truncate();
|
|
|
|
self.buf
|
|
|
|
} else {
|
2017-10-15 06:55:19 +08:00
|
|
|
warn!("unfinished LFN sequence {}", self.index);
|
2017-10-14 21:24:03 +08:00
|
|
|
Vec::<u16>::new()
|
|
|
|
}
|
|
|
|
}
|
2017-10-25 23:20:27 +08:00
|
|
|
|
2017-10-14 21:24:03 +08:00
|
|
|
fn truncate(&mut self) {
|
|
|
|
// Truncate 0 and 0xFFFF characters from LFN buffer
|
|
|
|
let mut lfn_len = self.buf.len();
|
|
|
|
loop {
|
|
|
|
if lfn_len == 0 {
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
match self.buf[lfn_len-1] {
|
|
|
|
0xFFFF | 0 => lfn_len -= 1,
|
|
|
|
_ => break,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
self.buf.truncate(lfn_len);
|
|
|
|
}
|
2017-10-25 23:20:27 +08:00
|
|
|
|
2017-10-14 21:24:03 +08:00
|
|
|
fn process(&mut self, data: &DirLfnEntryData) {
|
2017-10-21 21:35:26 +08:00
|
|
|
let is_last = (data.order & LFN_ENTRY_LAST_FLAG) != 0;
|
2017-10-14 21:24:03 +08:00
|
|
|
let index = data.order & 0x1F;
|
|
|
|
if index == 0 {
|
|
|
|
// Corrupted entry
|
2017-10-15 06:55:19 +08:00
|
|
|
warn!("currupted lfn entry! {:x}", data.order);
|
2017-10-14 21:24:03 +08:00
|
|
|
self.clear();
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
if is_last {
|
|
|
|
// last entry is actually first entry in stream
|
|
|
|
self.index = index;
|
|
|
|
self.chksum = data.checksum;
|
|
|
|
self.buf.resize(index as usize * LFN_PART_LEN, 0);
|
|
|
|
} else if self.index == 0 || index != self.index - 1 || data.checksum != self.chksum {
|
|
|
|
// Corrupted entry
|
2017-10-15 06:55:19 +08:00
|
|
|
warn!("currupted lfn entry! {:x} {:x} {:x} {:x}", data.order, self.index, data.checksum, self.chksum);
|
2017-10-14 21:24:03 +08:00
|
|
|
self.clear();
|
|
|
|
return;
|
|
|
|
} else {
|
|
|
|
// Decrement LFN index only for non-last entries
|
|
|
|
self.index -= 1;
|
|
|
|
}
|
|
|
|
let pos = LFN_PART_LEN * (index - 1) as usize;
|
|
|
|
// copy name parts into LFN buffer
|
2017-10-21 21:35:26 +08:00
|
|
|
self.buf[pos+0..pos+5].copy_from_slice(&data.name_0);
|
|
|
|
self.buf[pos+5..pos+11].copy_from_slice(&data.name_1);
|
|
|
|
self.buf[pos+11..pos+13].copy_from_slice(&data.name_2);
|
2017-10-14 21:24:03 +08:00
|
|
|
}
|
2017-10-25 23:20:27 +08:00
|
|
|
|
2017-10-14 21:24:03 +08:00
|
|
|
fn validate_chksum(&mut self, short_name: &[u8]) {
|
|
|
|
let chksum = lfn_checksum(short_name);
|
|
|
|
if chksum != self.chksum {
|
2017-10-15 06:55:19 +08:00
|
|
|
warn!("checksum mismatch {:x} {:x} {:?}", chksum, self.chksum, short_name);
|
2017-10-14 21:24:03 +08:00
|
|
|
self.clear();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|