Skip to content

Commit 0be3eff

Browse files
jongwuretrage
authored andcommitted
fat: Improve handling of longer filenames
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 <jianyong.wu@arm.com>
1 parent 1642135 commit 0be3eff

File tree

1 file changed

+18
-7
lines changed

1 file changed

+18
-7
lines changed

src/fat.rs

Lines changed: 18 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -232,6 +232,17 @@ fn name_to_str(input: &str, output: &mut [u8]) {
232232
break;
233233
}
234234
}
235+
236+
// If name is exactly 11 characters long then ensure separator is correctly added
237+
if (output[10] >= b'a' && output[10] <= b'z' || output[10] >= b'A' && output[10] <= b'Z')
238+
&& output[7] != b' '
239+
&& output[7] != b'.'
240+
{
241+
output[11] = output[10];
242+
output[10] = output[9];
243+
output[9] = output[8];
244+
output[8] = b'.';
245+
}
235246
}
236247

237248
impl<'a> Read for Node<'a> {
@@ -403,9 +414,9 @@ impl<'a> Directory<'a> {
403414
}
404415
}
405416

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

411422
match de.file_type {
@@ -1172,17 +1183,17 @@ mod tests {
11721183

11731184
#[test]
11741185
fn test_name_to_str() {
1175-
let mut s = [0_u8; 11];
1186+
let mut s = [0_u8; 12];
11761187
super::name_to_str("X ABC", &mut s);
11771188
assert_eq!(crate::common::ascii_strip(&s), "X.ABC");
1178-
let mut s = [0_u8; 11];
1189+
let mut s = [0_u8; 12];
11791190
super::name_to_str(".", &mut s);
11801191
assert_eq!(crate::common::ascii_strip(&s), ".");
1181-
let mut s = [0_u8; 11];
1192+
let mut s = [0_u8; 12];
11821193
super::name_to_str("..", &mut s);
11831194
assert_eq!(crate::common::ascii_strip(&s), "..");
1184-
let mut s = [0_u8; 11];
1195+
let mut s = [0_u8; 12];
11851196
super::name_to_str("ABCDEFGHIJK", &mut s);
1186-
assert_eq!(crate::common::ascii_strip(&s), "ABCDEFGHIJK");
1197+
assert_eq!(crate::common::ascii_strip(&s), "ABCDEFGH.IJK");
11871198
}
11881199
}

0 commit comments

Comments
 (0)