From ec6066a76f07d1b1223c57f1d79f1795cff2fc4a Mon Sep 17 00:00:00 2001 From: Alex Badics Date: Wed, 23 Oct 2024 21:00:18 +0200 Subject: [PATCH] Don't create LFNs for . and .. The current code makes invalid dir entries, because the first two slots should be . and .., but with LFNs, it's LFN for . . LFN for .. .. We should probably not create LFN's when not needed anyway, but that's not implemented in this patch. --- src/dir.rs | 15 +++++++++++++-- 1 file changed, 13 insertions(+), 2 deletions(-) diff --git a/src/dir.rs b/src/dir.rs index b23fe21..69efefd 100644 --- a/src/dir.rs +++ b/src/dir.rs @@ -530,6 +530,12 @@ impl<'a, IO: ReadWriteSeek, TP: TimeProvider, OCC: OemCpConverter> Dir<'a, IO, T Ok((stream, start_pos)) } + fn alloc_sfn_entry(&self) -> Result<(DirRawStream<'a, IO, TP, OCC>, u64), Error> { + let mut stream = self.find_free_entries(1)?; + let start_pos = stream.seek(io::SeekFrom::Current(0))?; + Ok((stream, start_pos)) + } + fn write_entry( &self, name: &str, @@ -540,8 +546,13 @@ impl<'a, IO: ReadWriteSeek, TP: TimeProvider, OCC: OemCpConverter> Dir<'a, IO, 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 LFN entries, except for . and .., which need to be at + // the first two slots and don't need LFNs anyway + let (mut stream, start_pos) = if name == "." || name == ".." { + self.alloc_sfn_entry()? + } else { + self.alloc_and_write_lfn_entries(&lfn_utf16, raw_entry.name())? + }; // write short name entry raw_entry.serialize(&mut stream)?; // Get position directory stream after entries were written