Skip to content

Commit c4bb769

Browse files
badicsalexrafalh
authored andcommitted
Fix .. cluster number for first-level dirs
On FAT32, the cluster number of / is 0 in dir entries (and not the actually used cluster)
1 parent d0af23f commit c4bb769

File tree

3 files changed

+19
-2
lines changed

3 files changed

+19
-2
lines changed

CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -43,6 +43,7 @@ Bug fixes:
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
4545
* Put '.' and '..' in the first two directory entries. (fixes "Expected a valid '.' entry in this slot." fsck error)
46+
* Set the cluster number to 0 in the ".." directory entry if it points to the root dir
4647

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

src/dir.rs

Lines changed: 14 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,13 @@ impl<IO: ReadWriteSeek, TP, OCC> DirRawStream<'_, IO, TP, OCC> {
3838
DirRawStream::Root(_) => None,
3939
}
4040
}
41+
42+
pub(crate) fn is_root_dir(&self) -> bool {
43+
match self {
44+
DirRawStream::File(file) => file.is_root_dir(),
45+
DirRawStream::Root(_) => true,
46+
}
47+
}
4148
}
4249

4350
// Note: derive cannot be used because of invalid bounds. See: https://github.com/rust-lang/rust/issues/26925
@@ -304,8 +311,13 @@ impl<'a, IO: ReadWriteSeek, TP: TimeProvider, OCC: OemCpConverter> Dir<'a, IO, T
304311
let sfn_entry = self.create_sfn_entry(dot_sfn, FileAttributes::DIRECTORY, entry.first_cluster());
305312
dir.write_entry(".", sfn_entry)?;
306313
let dotdot_sfn = ShortNameGenerator::generate_dotdot();
307-
let sfn_entry =
308-
self.create_sfn_entry(dotdot_sfn, FileAttributes::DIRECTORY, self.stream.first_cluster());
314+
// cluster of the root dir shall be set to 0 in directory entries.
315+
let dotdot_cluster = if self.stream.is_root_dir() {
316+
None
317+
} else {
318+
self.stream.first_cluster()
319+
};
320+
let sfn_entry = self.create_sfn_entry(dotdot_sfn, FileAttributes::DIRECTORY, dotdot_cluster);
309321
dir.write_entry("..", sfn_entry)?;
310322
Ok(dir)
311323
}

src/file.rs

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -214,6 +214,10 @@ impl<'a, IO: ReadWriteSeek, TP, OCC> File<'a, IO, TP, OCC> {
214214
disk.flush()?;
215215
Ok(())
216216
}
217+
218+
pub(crate) fn is_root_dir(&self) -> bool {
219+
self.entry.is_none()
220+
}
217221
}
218222

219223
impl<IO: ReadWriteSeek, TP: TimeProvider, OCC> File<'_, IO, TP, OCC> {

0 commit comments

Comments
 (0)