From 829b9c5f3f20b8d5f89aca1280c73e1b7332aae1 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Rafa=C5=82=20Harabie=C5=84?= Date: Sat, 29 Sep 2018 15:19:46 +0200 Subject: [PATCH] Fix warnings for file entries without LFN entries Fixes #12 and #14. --- src/dir.rs | 18 +++++++++++++++--- 1 file changed, 15 insertions(+), 3 deletions(-) diff --git a/src/dir.rs b/src/dir.rs index 7e5822f..ce59d08 100644 --- a/src/dir.rs +++ b/src/dir.rs @@ -431,6 +431,7 @@ impl<'a, T: ReadWriteSeek + 'a> Dir<'a, T> { validate_long_name(name)?; // convert long name to UTF-16 let lfn_utf16 = Self::encode_lfn_utf16(name); + // write LFN entries let (mut stream, start_pos) = self.alloc_and_write_lfn_entries(&lfn_utf16, raw_entry.name())?; // write short name entry raw_entry.serialize(&mut stream)?; @@ -613,13 +614,14 @@ impl LongNameBuilder { } fn to_vec(mut self) -> Vec { + // Check if last processed entry had index 1 if self.index == 1 { self.truncate(); - self.buf - } else { + } else if !self.is_empty() { warn!("unfinished LFN sequence {}", self.index); - Vec::::new() + self.clear(); } + self.buf } fn truncate(&mut self) { @@ -634,6 +636,12 @@ impl LongNameBuilder { self.buf.truncate(lfn_len); } + fn is_empty(&self) -> bool { + // Check if any LFN entry has been processed + // Note: index 0 is not a valid index in LFN and can be seen only after struct initialization + return self.index == 0; + } + fn process(&mut self, data: &DirLfnEntryData) { let is_last = (data.order() & LFN_ENTRY_LAST_FLAG) != 0; let index = data.order() & 0x1F; @@ -669,6 +677,10 @@ impl LongNameBuilder { } fn validate_chksum(&mut self, short_name: &[u8]) { + if self.is_empty() { + // Nothing to validate - no LFN entries has been processed + return; + } let chksum = lfn_checksum(short_name); if chksum != self.chksum { warn!("checksum mismatch {:x} {:x} {:?}", chksum, self.chksum, short_name);