Skip to content
Permalink

Comparing changes

This is a direct comparison between two commits made in this repository or its related repositories. View the default comparison for this range or learn more about diff comparisons.

Open a pull request

Create a new pull request by comparing changes across two branches. If you need to, you can also . Learn more about diff comparisons here.
base repository: rafalh/rust-fatfs
Failed to load repositories. Confirm that selected base ref is valid, then try again.
Loading
base: dd867af83b134dbadf572652e0c46cac1b59c3de
Choose a base ref
..
head repository: rafalh/rust-fatfs
Failed to load repositories. Confirm that selected head ref is valid, then try again.
Loading
compare: 8becd5f9a9324cfd15522c0ec9720f04c8656053
Choose a head ref
Showing with 35 additions and 18 deletions.
  1. +1 −0 CHANGELOG.md
  2. +0 −3 src/boot_sector.rs
  3. +0 −1 src/dir.rs
  4. +0 −1 src/dir_entry.rs
  5. +2 −2 src/file.rs
  6. +0 −2 src/fs.rs
  7. +32 −9 tests/format.rs
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -41,6 +41,7 @@ New features:
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

3 changes: 0 additions & 3 deletions src/boot_sector.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,4 @@
use core::slice;
use core::u16;
use core::u8;

use crate::dir_entry::DIR_ENTRY_SIZE;
use crate::error::{Error, IoError};
@@ -820,7 +818,6 @@ pub(crate) fn format_boot_sector<E: IoError>(
#[cfg(test)]
mod tests {
use super::*;
use core::u32;

fn init() {
let _ = env_logger::builder().is_test(true).try_init();
1 change: 0 additions & 1 deletion src/dir.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
#[cfg(all(not(feature = "std"), feature = "alloc", feature = "lfn"))]
use alloc::vec::Vec;
use core::char;
use core::num;
use core::str;
#[cfg(feature = "lfn")]
1 change: 0 additions & 1 deletion src/dir_entry.rs
Original file line number Diff line number Diff line change
@@ -6,7 +6,6 @@ use core::convert::TryInto;
use core::fmt;
#[cfg(not(feature = "unicode"))]
use core::iter;
use core::str;

#[cfg(feature = "lfn")]
use crate::dir::LfnBuffer;
4 changes: 2 additions & 2 deletions src/file.rs
Original file line number Diff line number Diff line change
@@ -6,7 +6,7 @@ use crate::fs::{FileSystem, ReadWriteSeek};
use crate::io::{IoBase, Read, Seek, SeekFrom, Write};
use crate::time::{Date, DateTime, TimeProvider};

const MAX_FILE_SIZE: u32 = core::u32::MAX;
const MAX_FILE_SIZE: u32 = u32::MAX;

/// A FAT filesystem file object used for reading and writing data.
///
@@ -188,7 +188,7 @@ impl<'a, IO: ReadWriteSeek, TP, OCC> File<'a, IO, TP, OCC> {
fn is_dir(&self) -> bool {
match self.entry {
Some(ref e) => e.inner().is_dir(),
None => false,
None => true, // root directory
}
}

2 changes: 0 additions & 2 deletions src/fs.rs
Original file line number Diff line number Diff line change
@@ -2,11 +2,9 @@
use alloc::string::String;
use core::borrow::BorrowMut;
use core::cell::{Cell, RefCell};
use core::char;
use core::convert::TryFrom;
use core::fmt::Debug;
use core::marker::PhantomData;
use core::u32;

use crate::boot_sector::{format_boot_sector, BiosParameterBlock, BootSector};
use crate::dir::{Dir, DirRawStream};
41 changes: 32 additions & 9 deletions tests/format.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
use std::io;
use std::io::prelude::*;

use fatfs::StdIoWrapper;
use fatfs::{FatType, StdIoWrapper};
use fscommon::BufStream;

const KB: u64 = 1024;
@@ -10,6 +10,20 @@ const TEST_STR: &str = "Hi there Rust programmer!\n";

type FileSystem = fatfs::FileSystem<StdIoWrapper<BufStream<io::Cursor<Vec<u8>>>>>;

fn init_logger() {
let _ = env_logger::builder().is_test(true).try_init();
}

fn format_fs(opts: fatfs::FormatVolumeOptions, total_bytes: u64) -> FileSystem {
init_logger();
// Init storage to 0xD1 bytes (value has been choosen to be parsed as normal file)
let storage_vec: Vec<u8> = vec![0xD1_u8; total_bytes as usize];
let storage_cur = io::Cursor::new(storage_vec);
let mut buffered_stream = fatfs::StdIoWrapper::from(BufStream::new(storage_cur));
fatfs::format_volume(&mut buffered_stream, opts).expect("format volume");
fatfs::FileSystem::new(buffered_stream, fatfs::FsOptions::new()).expect("open fs")
}

fn basic_fs_test(fs: &FileSystem) {
let stats = fs.stats().expect("stats");
if fs.fat_type() == fatfs::FatType::Fat32 {
@@ -60,14 +74,7 @@ fn basic_fs_test(fs: &FileSystem) {
}

fn test_format_fs(opts: fatfs::FormatVolumeOptions, total_bytes: u64) -> FileSystem {
let _ = env_logger::builder().is_test(true).try_init();
// Init storage to 0xD1 bytes (value has been choosen to be parsed as normal file)
let storage_vec: Vec<u8> = vec![0xD1_u8; total_bytes as usize];
let storage_cur = io::Cursor::new(storage_vec);
let mut buffered_stream = fatfs::StdIoWrapper::from(BufStream::new(storage_cur));
fatfs::format_volume(&mut buffered_stream, opts).expect("format volume");

let fs = fatfs::FileSystem::new(buffered_stream, fatfs::FsOptions::new()).expect("open fs");
let fs = format_fs(opts, total_bytes);
basic_fs_test(&fs);
fs
}
@@ -135,3 +142,19 @@ fn test_format_volume_label_and_id() {
);
assert_eq!(fs.volume_id(), 1234);
}

#[test]
fn test_zero_root_dir_clusters() {
init_logger();
let total_bytes = 33 * MB;
let opts = fatfs::FormatVolumeOptions::new().fat_type(FatType::Fat32);
let fs = format_fs(opts, total_bytes);
let root_dir = fs.root_dir();

// create a bunch of files to force allocation of second root directory cluster (64 is combined size of LFN + SFN)
let files_to_create = fs.cluster_size() as usize / 64 + 1;
for i in 0..files_to_create {
root_dir.create_file(&format!("f{}", i)).unwrap();
}
assert_eq!(root_dir.iter().count(), files_to_create);
}