Skip to content

Commit

Permalink
Fix .. cluster number for first-level dirs
Browse files Browse the repository at this point in the history
On FAT32, the cluster number of / is 0 in dir entries (and not the actually
used cluster)
  • Loading branch information
badicsalex authored and rafalh committed Nov 3, 2024
1 parent d0af23f commit c4bb769
Show file tree
Hide file tree
Showing 3 changed files with 19 additions and 2 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,7 @@ Bug fixes:
* Always respect `fat_type` from `FormatVolumeOptions`
* Fill FAT32 root directory clusters with zeros after allocation to avoid interpreting old data as directory entries
* Put '.' and '..' in the first two directory entries. (fixes "Expected a valid '.' entry in this slot." fsck error)
* Set the cluster number to 0 in the ".." directory entry if it points to the root dir

0.3.4 (2020-07-20)
------------------
Expand Down
16 changes: 14 additions & 2 deletions src/dir.rs
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,13 @@ impl<IO: ReadWriteSeek, TP, OCC> DirRawStream<'_, IO, TP, OCC> {
DirRawStream::Root(_) => None,
}
}

pub(crate) fn is_root_dir(&self) -> bool {
match self {
DirRawStream::File(file) => file.is_root_dir(),
DirRawStream::Root(_) => true,
}
}
}

// Note: derive cannot be used because of invalid bounds. See: https://github.com/rust-lang/rust/issues/26925
Expand Down Expand Up @@ -304,8 +311,13 @@ impl<'a, IO: ReadWriteSeek, TP: TimeProvider, OCC: OemCpConverter> Dir<'a, IO, T
let sfn_entry = self.create_sfn_entry(dot_sfn, FileAttributes::DIRECTORY, entry.first_cluster());
dir.write_entry(".", sfn_entry)?;
let dotdot_sfn = ShortNameGenerator::generate_dotdot();
let sfn_entry =
self.create_sfn_entry(dotdot_sfn, FileAttributes::DIRECTORY, self.stream.first_cluster());
// cluster of the root dir shall be set to 0 in directory entries.
let dotdot_cluster = if self.stream.is_root_dir() {
None
} else {
self.stream.first_cluster()
};
let sfn_entry = self.create_sfn_entry(dotdot_sfn, FileAttributes::DIRECTORY, dotdot_cluster);
dir.write_entry("..", sfn_entry)?;
Ok(dir)
}
Expand Down
4 changes: 4 additions & 0 deletions src/file.rs
Original file line number Diff line number Diff line change
Expand Up @@ -214,6 +214,10 @@ impl<'a, IO: ReadWriteSeek, TP, OCC> File<'a, IO, TP, OCC> {
disk.flush()?;
Ok(())
}

pub(crate) fn is_root_dir(&self) -> bool {
self.entry.is_none()
}
}

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

0 comments on commit c4bb769

Please sign in to comment.