Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix .. cluster number for first-level dirs #99

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,8 @@ Bug fixes:
* Fix formatting volumes with size in range 4096-4199 KB
* 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
31 changes: 27 additions & 4 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 Expand Up @@ -529,6 +541,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<IO::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,
Expand All @@ -539,8 +557,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
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
48 changes: 48 additions & 0 deletions tests/fsck.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
#![cfg(target_os="linux")]
use fatfs::Write;

const KB: u32 = 1024;
const MB: u32 = KB * 1024;

#[test]
fn test_fsck_1mb() {
let _ = env_logger::builder().is_test(true).try_init();

let mut image = std::fs::OpenOptions::new()
.write(true)
.read(true)
.create(true)
.open("/tmp/test.img")
.expect("open temporary image file");
image.set_len(MB as u64).expect("set_len on temp file");

fatfs::format_volume(
&mut fatfs::StdIoWrapper::from(image.try_clone().expect("clone tempfile")),
fatfs::FormatVolumeOptions::new().total_sectors(MB / 512),
)
.expect("format volume");

let fs = fatfs::FileSystem::new(image, fatfs::FsOptions::new()).expect("open fs");
fs.root_dir().create_dir("dir1").expect("create dir1");
fs.root_dir()
.create_file("root file.bin")
.expect("create root file")
.write_all(&[0xab; (16 * KB) as usize])
.expect("root file write");
let dir2 = fs.root_dir().create_dir("dir2").expect("create dir2");
dir2.create_dir("subdir").expect("subdir");
dir2.create_file("file1")
.expect("file1")
.write_all(b"testing 1 2 1 2")
.expect("file 1 write");
core::mem::drop(dir2);
core::mem::drop(fs);

let fsck_status = std::process::Command::new("fsck.vfat")
.args(&["-n", "/tmp/test.img"])
.spawn()
.expect("spawn fsck")
.wait()
.expect("wait on fsck");
assert!(fsck_status.success(), "fsck was not successful ({fsck_status:?})");
}