Skip to content

Commit

Permalink
fat: Improve handling of longer filenames
Browse files Browse the repository at this point in the history
This patch increases the size of the byte array to 12 bytes (so that the
'.' can be included for files that are named the full 8 + 3 bytes. If
the string presented is exactly 11 bytes then adjust it to correctly
included the '.' separator.

Fixes: #261
Signed-off-by: Jianyong Wu <[email protected]>
  • Loading branch information
jongwu authored and retrage committed Jul 21, 2023
1 parent 1642135 commit 0be3eff
Showing 1 changed file with 18 additions and 7 deletions.
25 changes: 18 additions & 7 deletions src/fat.rs
Original file line number Diff line number Diff line change
Expand Up @@ -232,6 +232,17 @@ fn name_to_str(input: &str, output: &mut [u8]) {
break;
}
}

// If name is exactly 11 characters long then ensure separator is correctly added
if (output[10] >= b'a' && output[10] <= b'z' || output[10] >= b'A' && output[10] <= b'Z')
&& output[7] != b' '
&& output[7] != b'.'
{
output[11] = output[10];
output[10] = output[9];
output[9] = output[8];
output[8] = b'.';
}
}

impl<'a> Read for Node<'a> {
Expand Down Expand Up @@ -403,9 +414,9 @@ impl<'a> Directory<'a> {
}
}

pub fn next_node(&mut self) -> Result<(Node, [u8; 11]), Error> {
pub fn next_node(&mut self) -> Result<(Node, [u8; 12]), Error> {
let de = self.next_entry()?;
let mut name = [0_u8; 11];
let mut name = [0_u8; 12];
name_to_str(core::str::from_utf8(&de.name).unwrap(), &mut name);

match de.file_type {
Expand Down Expand Up @@ -1172,17 +1183,17 @@ mod tests {

#[test]
fn test_name_to_str() {
let mut s = [0_u8; 11];
let mut s = [0_u8; 12];
super::name_to_str("X ABC", &mut s);
assert_eq!(crate::common::ascii_strip(&s), "X.ABC");
let mut s = [0_u8; 11];
let mut s = [0_u8; 12];
super::name_to_str(".", &mut s);
assert_eq!(crate::common::ascii_strip(&s), ".");
let mut s = [0_u8; 11];
let mut s = [0_u8; 12];
super::name_to_str("..", &mut s);
assert_eq!(crate::common::ascii_strip(&s), "..");
let mut s = [0_u8; 11];
let mut s = [0_u8; 12];
super::name_to_str("ABCDEFGHIJK", &mut s);
assert_eq!(crate::common::ascii_strip(&s), "ABCDEFGHIJK");
assert_eq!(crate::common::ascii_strip(&s), "ABCDEFGH.IJK");
}
}

0 comments on commit 0be3eff

Please sign in to comment.