Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add LFN support #155

Draft
wants to merge 3 commits into
base: develop
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 2 additions & 1 deletion src/fat/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -252,11 +252,12 @@ mod test {
let on_disk_entry = OnDiskDirEntry::new(part);
match expected {
Expected::Lfn(start, index, contents) if on_disk_entry.is_lfn() => {
let (calc_start, calc_index, calc_contents) =
let (calc_start, calc_index, calc_contents, _) =
on_disk_entry.lfn_contents().unwrap();
assert_eq!(*start, calc_start);
assert_eq!(*index, calc_index);
assert_eq!(*contents, calc_contents);
// TODO: Check checksum
}
Expected::Short(expected_entry) if !on_disk_entry.is_lfn() => {
let parsed_entry = on_disk_entry.get_entry(FatType::Fat32, BlockIdx(0), 0);
Expand Down
5 changes: 3 additions & 2 deletions src/fat/ondiskdirentry.rs
Original file line number Diff line number Diff line change
Expand Up @@ -78,11 +78,12 @@ impl<'a> OnDiskDirEntry<'a> {
}

/// If this is an LFN, get the contents so we can re-assemble the filename.
pub fn lfn_contents(&self) -> Option<(bool, u8, [char; 13])> {
pub fn lfn_contents(&self) -> Option<(bool, u8, [char; 13], u8)> {
if self.is_lfn() {
let mut buffer = [' '; 13];
let is_start = (self.data[0] & 0x40) != 0;
let sequence = self.data[0] & 0x1F;
let checksum = self.data[13];
// LFNs store UCS-2, so we can map from 16-bit char to 32-bit char without problem.
buffer[0] =
core::char::from_u32(u32::from(LittleEndian::read_u16(&self.data[1..=2]))).unwrap();
Expand Down Expand Up @@ -118,7 +119,7 @@ impl<'a> OnDiskDirEntry<'a> {
buffer[12] =
core::char::from_u32(u32::from(LittleEndian::read_u16(&self.data[30..=31])))
.unwrap();
Some((is_start, sequence, buffer))
Some((is_start, sequence, buffer, checksum))
} else {
None
}
Expand Down
62 changes: 57 additions & 5 deletions src/fat/volume.rs
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ use crate::{
};
use byteorder::{ByteOrder, LittleEndian};
use core::convert::TryFrom;
use heapless::{String, Vec};

/// An MS-DOS 11 character volume label.
///
Expand Down Expand Up @@ -613,6 +614,11 @@ impl FatVolume {
ClusterId::ROOT_DIR => Some(fat32_info.first_root_dir_cluster),
_ => Some(dir_info.cluster),
};

let mut lfn_stack = Vec::<String<32>, 8>::new();
let mut current_checksum: Option<u8> = None;
let mut current_sequence: Option<u8> = None;

while let Some(cluster) = current_cluster {
let start_block_idx = self.cluster_to_block(cluster);
for block_idx in start_block_idx.range(BlockCount(u32::from(self.blocks_per_cluster))) {
Expand All @@ -623,11 +629,57 @@ impl FatVolume {
if dir_entry.is_end() {
// Can quit early
return Ok(());
} else if dir_entry.is_valid() && !dir_entry.is_lfn() {
// Safe, since Block::LEN always fits on a u32
let start = (i * OnDiskDirEntry::LEN) as u32;
let entry = dir_entry.get_entry(FatType::Fat32, block_idx, start);
func(&entry);
} else if dir_entry.is_valid() {
if dir_entry.is_lfn() {
if let Some((is_start, sequence, data, checksum)) =
dir_entry.lfn_contents()
{
// Case for malformed LFN entries, if they ale placed before
// actual entries
let checksum_ok = match current_checksum {
Some(c) => c == checksum,
None => true,
};
let sequence_ok = match current_sequence {
Some(s) => sequence == s - 1 && sequence < 8,
None => true,
};
if is_start || !checksum_ok || !sequence_ok {
lfn_stack.clear();
current_checksum = None;
current_sequence = None;
} else {
current_checksum = Some(checksum);
current_sequence = Some(sequence);
}

let mut name_chunk: String<32> = String::new();
for i in 0..13 {
if data[i] == '\0' {
break;
}
name_chunk.push(data[i]).unwrap();
}
lfn_stack.push(name_chunk).unwrap();
}
} else {
if !lfn_stack.is_empty() {
let mut filename: String<255> = String::new();
lfn_stack.reverse();
for name_chunk in lfn_stack.iter() {
filename.push_str(name_chunk).unwrap();
}
}

lfn_stack.clear();
current_checksum = None;
current_sequence = None;
// TODO calculate checksum and compare with shorf file name
// Safe, since Block::LEN always fits on a u32
let start = (i * OnDiskDirEntry::LEN) as u32;
let entry = dir_entry.get_entry(FatType::Fat32, block_idx, start);
func(&entry);
}
}
}
}
Expand Down