Skip to content

Commit

Permalink
efi/file: fixup dir/file name
Browse files Browse the repository at this point in the history
Fat filename can be 12, that is filename with length of 8 and extension
name with length of 3 and a separator like '.'. But directory name
length limits to 11. That's say some name can't be shown correctly. For
that case, we can do a fixup to give a correct name to userspace.

Fixes: cloud-hypervisor#261
Signed-off-by: Jianyong Wu <[email protected]>
  • Loading branch information
jongwu committed Jul 6, 2023
1 parent 3e29535 commit 9b9acfa
Showing 1 changed file with 21 additions and 7 deletions.
28 changes: 21 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;
}
}

// Fixup for Director or file name, when the length of its name and extend name equals 11. There maybe no
// separation between them. If so, add it.
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 @@ -282,7 +293,9 @@ impl<'a> Directory<'a> {

match self.filesystem.read(u64::from(sector), data) {
Ok(_) => {}
Err(e) => return Err(Error::Block(e)),
Err(e) => {
return Err(Error::Block(e));
}
};

Ok(())
Expand Down Expand Up @@ -348,6 +361,7 @@ impl<'a> Directory<'a> {

for i in self.offset..dirs.len() {
let d = &dirs[i];
let s = d.size;
// Last entry
if d.name[0] == 0x0 {
return Err(Error::EndOfFile);
Expand Down Expand Up @@ -403,9 +417,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,16 +1186,16 @@ 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");
}
Expand Down

0 comments on commit 9b9acfa

Please sign in to comment.