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-09-23 02:50:41 +08:00
|
|
|
use byteorder::{LittleEndian, ReadBytesExt};
|
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-10-07 20:56:50 +08:00
|
|
|
use fs::{FileSystemRef, DiskSlice};
|
|
|
|
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-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-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-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
|
|
|
}
|
|
|
|
|
|
|
|
#[allow(dead_code)]
|
2017-09-25 04:12:38 +08:00
|
|
|
#[derive(Clone, Debug, Default)]
|
2017-10-07 20:56:50 +08:00
|
|
|
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,
|
|
|
|
size: u32,
|
|
|
|
}
|
|
|
|
|
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],
|
|
|
|
}
|
|
|
|
|
|
|
|
#[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-09-24 09:08:00 +08:00
|
|
|
#[derive(Clone)]
|
2017-10-07 20:56:50 +08:00
|
|
|
pub struct DirEntry<'a, 'b: 'a> {
|
|
|
|
data: DirFileEntryData,
|
2017-09-25 04:12:38 +08:00
|
|
|
lfn: Vec<u16>,
|
2017-10-07 20:56:50 +08:00
|
|
|
fs: FileSystemRef<'a, 'b>,
|
2017-09-23 02:50:41 +08:00
|
|
|
}
|
|
|
|
|
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-06 22:42:29 +08:00
|
|
|
pub(crate) fn from_word(dos_date: u16) -> Self {
|
|
|
|
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-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-06 22:42:29 +08:00
|
|
|
pub(crate) fn from_word(dos_time: u16) -> Self {
|
|
|
|
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-07 20:56:50 +08:00
|
|
|
#[derive(Clone, Copy, Debug)]
|
|
|
|
pub struct DateTime {
|
|
|
|
pub date: Date,
|
|
|
|
pub time: Time,
|
|
|
|
}
|
|
|
|
|
|
|
|
impl DateTime {
|
2017-10-06 22:42:29 +08:00
|
|
|
pub(crate) fn from_words(dos_date: u16, dos_time: u16) -> Self {
|
2017-10-07 20:56:50 +08:00
|
|
|
DateTime {
|
|
|
|
date: Date::from_word(dos_date),
|
|
|
|
time: Time::from_word(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-07 20:56:50 +08:00
|
|
|
impl <'a, 'b> DirEntry<'a, 'b> {
|
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-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-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-09-24 06:05:43 +08:00
|
|
|
pub fn is_dir(&self) -> bool {
|
2017-10-07 20:56:50 +08:00
|
|
|
self.data.attrs.contains(FileAttributes::DIRECTORY)
|
2017-09-24 06:05:43 +08:00
|
|
|
}
|
|
|
|
|
2017-09-24 20:34:07 +08:00
|
|
|
pub fn is_file(&self) -> bool {
|
|
|
|
!self.is_dir()
|
|
|
|
}
|
|
|
|
|
2017-10-07 08:37:29 +08:00
|
|
|
pub(crate) fn first_cluster(&self) -> Option<u32> {
|
|
|
|
let n = ((self.data.first_cluster_hi as u32) << 16) | self.data.first_cluster_lo as u32;
|
|
|
|
if n == 0 { None } else { Some(n) }
|
2017-09-23 04:27:39 +08:00
|
|
|
}
|
|
|
|
|
2017-10-07 20:56:50 +08:00
|
|
|
pub fn to_file(&self) -> File<'a, 'b> {
|
2017-09-24 06:05:43 +08:00
|
|
|
if self.is_dir() {
|
|
|
|
panic!("This is a directory");
|
|
|
|
}
|
2017-10-07 20:56:50 +08:00
|
|
|
File::new(self.first_cluster(), Some(self.data.size), self.fs)
|
2017-09-23 04:27:39 +08:00
|
|
|
}
|
|
|
|
|
2017-10-07 20:56:50 +08:00
|
|
|
pub fn to_dir(&self) -> Dir<'a, 'b> {
|
2017-09-24 06:05:43 +08:00
|
|
|
if !self.is_dir() {
|
|
|
|
panic!("This is a file");
|
|
|
|
}
|
2017-10-07 20:56:50 +08:00
|
|
|
let file = File::new(self.first_cluster(), None, self.fs);
|
2017-10-07 21:00:27 +08:00
|
|
|
Dir::new(DirRawStream::File(file), self.fs)
|
2017-09-24 06:05:43 +08:00
|
|
|
}
|
|
|
|
|
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-07 20:56:50 +08:00
|
|
|
pub fn created(&self) -> DateTime {
|
|
|
|
DateTime::from_words(self.data.create_date, self.data.create_time_1)
|
2017-09-23 04:27:39 +08:00
|
|
|
}
|
|
|
|
|
2017-10-07 20:56:50 +08:00
|
|
|
pub fn accessed(&self) -> Date {
|
|
|
|
Date::from_word(self.data.access_date)
|
2017-09-24 09:08:00 +08:00
|
|
|
}
|
|
|
|
|
2017-10-07 20:56:50 +08:00
|
|
|
pub fn modified(&self) -> DateTime {
|
|
|
|
DateTime::from_words(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-06 22:07:11 +08:00
|
|
|
#[derive(Clone)]
|
2017-10-07 20:56:50 +08:00
|
|
|
pub struct Dir<'a, 'b: 'a> {
|
2017-10-07 21:00:27 +08:00
|
|
|
rdr: 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-09-24 01:42:09 +08:00
|
|
|
|
2017-10-07 21:00:27 +08:00
|
|
|
pub(crate) fn new(rdr: DirRawStream<'a, 'b>, fs: FileSystemRef<'a, 'b>) -> Dir<'a, 'b> {
|
2017-10-07 20:56:50 +08:00
|
|
|
Dir { rdr, fs }
|
2017-09-24 01:42:09 +08:00
|
|
|
}
|
|
|
|
|
2017-10-07 20:56:50 +08:00
|
|
|
pub fn iter(&self) -> DirIter<'a, 'b> {
|
|
|
|
DirIter {
|
2017-10-06 22:07:11 +08:00
|
|
|
rdr: self.rdr.clone(),
|
|
|
|
fs: self.fs.clone(),
|
2017-10-07 08:16:34 +08:00
|
|
|
err: false,
|
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, "/");
|
|
|
|
let comp = path_split.next().unwrap();
|
|
|
|
let rest_opt = path_split.next();
|
|
|
|
(comp, rest_opt)
|
|
|
|
}
|
|
|
|
|
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-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-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())
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
#[derive(Clone)]
|
2017-10-07 20:56:50 +08:00
|
|
|
pub struct DirIter<'a, 'b: 'a> {
|
2017-10-07 21:00:27 +08:00
|
|
|
rdr: 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> {
|
|
|
|
fn read_dir_entry_data(&mut self) -> io::Result<DirEntryData> {
|
2017-09-24 01:42:09 +08:00
|
|
|
let mut name = [0; 11];
|
2017-09-24 02:16:32 +08:00
|
|
|
self.rdr.read(&mut name)?;
|
2017-10-07 20:56:50 +08:00
|
|
|
let attrs = FileAttributes::from_bits(self.rdr.read_u8()?).expect("invalid attributes"); // FIXME
|
|
|
|
if attrs == FileAttributes::LFN {
|
|
|
|
let mut data = DirLfnEntryData {
|
2017-09-25 04:12:38 +08:00
|
|
|
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 = self.rdr.read_u8()?;
|
|
|
|
data.checksum = self.rdr.read_u8()?;
|
|
|
|
self.rdr.read_u16_into::<LittleEndian>(&mut data.name_1)?;
|
|
|
|
data.reserved_0 = self.rdr.read_u16::<LittleEndian>()?;
|
|
|
|
self.rdr.read_u16_into::<LittleEndian>(&mut data.name_2)?;
|
2017-10-07 20:56:50 +08:00
|
|
|
Ok(DirEntryData::Lfn(data))
|
2017-09-25 04:12:38 +08:00
|
|
|
} else {
|
2017-10-07 20:56:50 +08:00
|
|
|
let data = DirFileEntryData {
|
2017-09-25 04:12:38 +08:00
|
|
|
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-10-07 20:56:50 +08:00
|
|
|
Ok(DirEntryData::File(data))
|
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-07 20:56:50 +08:00
|
|
|
impl <'a, 'b> Iterator for DirIter<'a, 'b> {
|
|
|
|
type Item = io::Result<DirEntry<'a, 'b>>;
|
2017-09-24 08:24:42 +08:00
|
|
|
|
2017-10-02 03:31:44 +08:00
|
|
|
fn next(&mut self) -> Option<Self::Item> {
|
2017-10-07 08:16:34 +08:00
|
|
|
if self.err {
|
|
|
|
return None;
|
|
|
|
}
|
2017-09-25 04:12:38 +08:00
|
|
|
let mut lfn_buf = Vec::<u16>::new();
|
2017-09-24 08:24:42 +08:00
|
|
|
loop {
|
2017-09-24 09:08:00 +08:00
|
|
|
let res = self.read_dir_entry_data();
|
|
|
|
let data = match res {
|
|
|
|
Ok(data) => data,
|
2017-10-07 08:16:34 +08:00
|
|
|
Err(err) => {
|
|
|
|
self.err = true;
|
|
|
|
return Some(Err(err));
|
|
|
|
},
|
2017-09-24 08:24:42 +08:00
|
|
|
};
|
2017-09-25 04:12:38 +08:00
|
|
|
match data {
|
2017-10-07 20:56:50 +08:00
|
|
|
DirEntryData::File(data) => {
|
2017-09-25 04:12:38 +08:00
|
|
|
// Check if this is end of dif
|
|
|
|
if data.name[0] == 0 {
|
|
|
|
return None;
|
|
|
|
}
|
|
|
|
// Check if this is deleted or volume ID entry
|
2017-10-07 20:56:50 +08:00
|
|
|
if data.name[0] == 0xE5 || data.attrs.contains(FileAttributes::VOLUME_ID) {
|
2017-09-25 04:12:38 +08:00
|
|
|
lfn_buf.clear();
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
// Truncate 0 and 0xFFFF characters from LFN buffer
|
|
|
|
let mut lfn_len = lfn_buf.len();
|
|
|
|
loop {
|
|
|
|
if lfn_len == 0 {
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
match lfn_buf[lfn_len-1] {
|
|
|
|
0xFFFF | 0 => lfn_len -= 1,
|
|
|
|
_ => break,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
lfn_buf.truncate(lfn_len);
|
2017-10-07 20:56:50 +08:00
|
|
|
return Some(Ok(DirEntry {
|
2017-09-25 04:12:38 +08:00
|
|
|
data,
|
|
|
|
lfn: lfn_buf,
|
2017-10-04 19:56:44 +08:00
|
|
|
fs: self.fs,
|
2017-09-25 04:12:38 +08:00
|
|
|
}));
|
|
|
|
},
|
2017-10-07 20:56:50 +08:00
|
|
|
DirEntryData::Lfn(data) => {
|
2017-09-25 04:12:38 +08:00
|
|
|
// Check if this is deleted entry
|
|
|
|
if data.order == 0xE5 {
|
|
|
|
lfn_buf.clear();
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
const LFN_PART_LEN: usize = 13;
|
|
|
|
let index = (data.order & 0x1F) - 1;
|
|
|
|
let pos = LFN_PART_LEN * index as usize;
|
|
|
|
// resize LFN buffer to have enough space for entire name
|
|
|
|
if lfn_buf.len() < pos + LFN_PART_LEN {
|
|
|
|
lfn_buf.resize(pos + LFN_PART_LEN, 0);
|
|
|
|
}
|
|
|
|
// copy name parts into LFN buffer
|
|
|
|
lfn_buf[pos+0..pos+5].clone_from_slice(&data.name_0);
|
|
|
|
lfn_buf[pos+5..pos+11].clone_from_slice(&data.name_1);
|
|
|
|
lfn_buf[pos+11..pos+13].clone_from_slice(&data.name_2);
|
|
|
|
}
|
|
|
|
};
|
2017-09-24 08:24:42 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|