Skip to content

Commit d0af23f

Browse files
badicsalexrafalh
authored andcommitted
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.
1 parent bd40fe2 commit d0af23f

File tree

2 files changed

+14
-2
lines changed

2 files changed

+14
-2
lines changed

CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,7 @@ Bug fixes:
4242
* Fix formatting volumes with size in range 4096-4199 KB
4343
* Always respect `fat_type` from `FormatVolumeOptions`
4444
* Fill FAT32 root directory clusters with zeros after allocation to avoid interpreting old data as directory entries
45+
* Put '.' and '..' in the first two directory entries. (fixes "Expected a valid '.' entry in this slot." fsck error)
4546

4647
0.3.4 (2020-07-20)
4748
------------------

src/dir.rs

Lines changed: 13 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -529,6 +529,12 @@ impl<'a, IO: ReadWriteSeek, TP: TimeProvider, OCC: OemCpConverter> Dir<'a, IO, T
529529
Ok((stream, start_pos))
530530
}
531531

532+
fn alloc_sfn_entry(&self) -> Result<(DirRawStream<'a, IO, TP, OCC>, u64), Error<IO::Error>> {
533+
let mut stream = self.find_free_entries(1)?;
534+
let start_pos = stream.seek(io::SeekFrom::Current(0))?;
535+
Ok((stream, start_pos))
536+
}
537+
532538
fn write_entry(
533539
&self,
534540
name: &str,
@@ -539,8 +545,13 @@ impl<'a, IO: ReadWriteSeek, TP: TimeProvider, OCC: OemCpConverter> Dir<'a, IO, T
539545
validate_long_name(name)?;
540546
// convert long name to UTF-16
541547
let lfn_utf16 = Self::encode_lfn_utf16(name);
542-
// write LFN entries
543-
let (mut stream, start_pos) = self.alloc_and_write_lfn_entries(&lfn_utf16, raw_entry.name())?;
548+
// write LFN entries, except for . and .., which need to be at
549+
// the first two slots and don't need LFNs anyway
550+
let (mut stream, start_pos) = if name == "." || name == ".." {
551+
self.alloc_sfn_entry()?
552+
} else {
553+
self.alloc_and_write_lfn_entries(&lfn_utf16, raw_entry.name())?
554+
};
544555
// write short name entry
545556
raw_entry.serialize(&mut stream)?;
546557
// Get position directory stream after entries were written

0 commit comments

Comments
 (0)