diff --git a/src/fs.rs b/src/fs.rs index aa693a6..ba69f7b 100644 --- a/src/fs.rs +++ b/src/fs.rs @@ -1132,14 +1132,14 @@ pub struct FormatOptions { _end: [u8;0], } -const KB: u32 = 1024; -const MB: u32 = KB * 1024; -const GB: u32 = MB * 1024; +const KB: u64 = 1024; +const MB: u64 = KB * 1024; +const GB: u64 = MB * 1024; fn determine_fat_type(total_bytes: u64) -> FatType { - if total_bytes < 4*MB as u64 { + if total_bytes < 4 * MB { FatType::Fat12 - } else if total_bytes < 512*MB as u64 { + } else if total_bytes < 512 * MB { FatType::Fat16 } else { FatType::Fat32 @@ -1148,27 +1148,27 @@ fn determine_fat_type(total_bytes: u64) -> FatType { fn determine_bytes_per_cluster(total_bytes: u64, fat_type: FatType, bytes_per_sector: u16) -> u32 { let bytes_per_cluster = match fat_type { - FatType::Fat12 => (total_bytes.next_power_of_two() / MB as u64) as u32 * 512, + FatType::Fat12 => (total_bytes.next_power_of_two() / MB * 512) as u32, FatType::Fat16 => { - if total_bytes <= 16 * MB as u64 { - 1 * KB - } else if total_bytes <= 128 * MB as u64 { - 2 * KB + if total_bytes <= 16 * MB { + 1 * KB as u32 + } else if total_bytes <= 128 * MB { + 2 * KB as u32 } else { - (total_bytes.next_power_of_two() / (64 * MB as u64)) as u32 * KB + (total_bytes.next_power_of_two() / (64 * MB) * KB) as u32 } }, FatType::Fat32 => { - if total_bytes <= 260 * MB as u64 { + if total_bytes <= 260 * MB { 512 - } else if total_bytes <= 8 * GB as u64 { - 4 * KB + } else if total_bytes <= 8 * GB { + 4 * KB as u32 } else { - (total_bytes.next_power_of_two() / (2 * GB as u64)) as u32 * KB + (total_bytes.next_power_of_two() / (2 * GB) * KB) as u32 } }, }; - const MAX_CLUSTER_SIZE: u32 = 32*KB; + const MAX_CLUSTER_SIZE: u32 = 32 * KB as u32; debug_assert!(bytes_per_cluster.is_power_of_two()); cmp::min(cmp::max(bytes_per_cluster, bytes_per_sector as u32), MAX_CLUSTER_SIZE) } @@ -1404,52 +1404,52 @@ mod tests { #[test] fn test_determine_fat_type() { - assert_eq!(determine_fat_type(3 * MB as u64), FatType::Fat12); - assert_eq!(determine_fat_type(4 * MB as u64), FatType::Fat16); - assert_eq!(determine_fat_type(511 * MB as u64), FatType::Fat16); - assert_eq!(determine_fat_type(512 * MB as u64), FatType::Fat32); + assert_eq!(determine_fat_type(3 * MB), FatType::Fat12); + assert_eq!(determine_fat_type(4 * MB), FatType::Fat16); + assert_eq!(determine_fat_type(511 * MB), FatType::Fat16); + assert_eq!(determine_fat_type(512 * MB), FatType::Fat32); } #[test] fn test_determine_bytes_per_cluster_fat12() { - assert_eq!(determine_bytes_per_cluster(1 * MB as u64 + 0, FatType::Fat12, 512), 512); - assert_eq!(determine_bytes_per_cluster(1 * MB as u64 + 1, FatType::Fat12, 512), 1024); - assert_eq!(determine_bytes_per_cluster(1 * MB as u64, FatType::Fat12, 4096), 4096); + assert_eq!(determine_bytes_per_cluster(1 * MB + 0, FatType::Fat12, 512), 512); + assert_eq!(determine_bytes_per_cluster(1 * MB + 1, FatType::Fat12, 512), 1024); + assert_eq!(determine_bytes_per_cluster(1 * MB, FatType::Fat12, 4096), 4096); } #[test] fn test_determine_bytes_per_cluster_fat16() { - assert_eq!(determine_bytes_per_cluster(1 * MB as u64, FatType::Fat16, 512), 1 * KB); - assert_eq!(determine_bytes_per_cluster(1 * MB as u64, FatType::Fat16, 4 * KB as u16), 4 * KB); - assert_eq!(determine_bytes_per_cluster(16 * MB as u64 + 0, FatType::Fat16, 512), 1 * KB); - assert_eq!(determine_bytes_per_cluster(16 * MB as u64 + 1, FatType::Fat16, 512), 2 * KB); - assert_eq!(determine_bytes_per_cluster(128 * MB as u64 + 0, FatType::Fat16, 512), 2 * KB); - assert_eq!(determine_bytes_per_cluster(128 * MB as u64 + 1, FatType::Fat16, 512), 4 * KB); - assert_eq!(determine_bytes_per_cluster(256 * MB as u64 + 0, FatType::Fat16, 512), 4 * KB); - assert_eq!(determine_bytes_per_cluster(256 * MB as u64 + 1, FatType::Fat16, 512), 8 * KB); - assert_eq!(determine_bytes_per_cluster(512 * MB as u64 + 0, FatType::Fat16, 512), 8 * KB); - assert_eq!(determine_bytes_per_cluster(512 * MB as u64 + 1, FatType::Fat16, 512), 16 * KB); - assert_eq!(determine_bytes_per_cluster(1024 * MB as u64 + 0, FatType::Fat16, 512), 16 * KB); - assert_eq!(determine_bytes_per_cluster(1024 * MB as u64 + 1, FatType::Fat16, 512), 32 * KB); - assert_eq!(determine_bytes_per_cluster(99999 * MB as u64, FatType::Fat16, 512), 32 * KB); + assert_eq!(determine_bytes_per_cluster(1 * MB, FatType::Fat16, 512), 1 * KB as u32); + assert_eq!(determine_bytes_per_cluster(1 * MB, FatType::Fat16, 4 * KB as u16), 4 * KB as u32); + assert_eq!(determine_bytes_per_cluster(16 * MB + 0, FatType::Fat16, 512), 1 * KB as u32); + assert_eq!(determine_bytes_per_cluster(16 * MB + 1, FatType::Fat16, 512), 2 * KB as u32); + assert_eq!(determine_bytes_per_cluster(128 * MB + 0, FatType::Fat16, 512), 2 * KB as u32); + assert_eq!(determine_bytes_per_cluster(128 * MB + 1, FatType::Fat16, 512), 4 * KB as u32); + assert_eq!(determine_bytes_per_cluster(256 * MB + 0, FatType::Fat16, 512), 4 * KB as u32); + assert_eq!(determine_bytes_per_cluster(256 * MB + 1, FatType::Fat16, 512), 8 * KB as u32); + assert_eq!(determine_bytes_per_cluster(512 * MB + 0, FatType::Fat16, 512), 8 * KB as u32); + assert_eq!(determine_bytes_per_cluster(512 * MB + 1, FatType::Fat16, 512), 16 * KB as u32); + assert_eq!(determine_bytes_per_cluster(1024 * MB + 0, FatType::Fat16, 512), 16 * KB as u32); + assert_eq!(determine_bytes_per_cluster(1024 * MB + 1, FatType::Fat16, 512), 32 * KB as u32); + assert_eq!(determine_bytes_per_cluster(99999 * MB, FatType::Fat16, 512), 32 * KB as u32); } #[test] fn test_determine_bytes_per_cluster_fat32() { assert_eq!(determine_bytes_per_cluster(260 * MB as u64, FatType::Fat32, 512), 512); - assert_eq!(determine_bytes_per_cluster(260 * MB as u64, FatType::Fat32, 4 * KB as u16), 4 * KB); - assert_eq!(determine_bytes_per_cluster(260 * MB as u64 + 1, FatType::Fat32, 512), 4 * KB); - assert_eq!(determine_bytes_per_cluster(8 * GB as u64, FatType::Fat32, 512), 4 * KB); - assert_eq!(determine_bytes_per_cluster(8 * GB as u64 + 1, FatType::Fat32, 512), 8 * KB); - assert_eq!(determine_bytes_per_cluster(16 * GB as u64 + 0, FatType::Fat32, 512), 8 * KB); - assert_eq!(determine_bytes_per_cluster(16 * GB as u64 + 1, FatType::Fat32, 512), 16 * KB); - assert_eq!(determine_bytes_per_cluster(32 * GB as u64, FatType::Fat32, 512), 16 * KB); - assert_eq!(determine_bytes_per_cluster(32 * GB as u64 + 1, FatType::Fat32, 512), 32 * KB); - assert_eq!(determine_bytes_per_cluster(999 * GB as u64, FatType::Fat32, 512), 32 * KB); + assert_eq!(determine_bytes_per_cluster(260 * MB as u64, FatType::Fat32, 4 * KB as u16), 4 * KB as u32); + assert_eq!(determine_bytes_per_cluster(260 * MB as u64 + 1, FatType::Fat32, 512), 4 * KB as u32); + assert_eq!(determine_bytes_per_cluster(8 * GB as u64, FatType::Fat32, 512), 4 * KB as u32); + assert_eq!(determine_bytes_per_cluster(8 * GB as u64 + 1, FatType::Fat32, 512), 8 * KB as u32); + assert_eq!(determine_bytes_per_cluster(16 * GB as u64 + 0, FatType::Fat32, 512), 8 * KB as u32); + assert_eq!(determine_bytes_per_cluster(16 * GB as u64 + 1, FatType::Fat32, 512), 16 * KB as u32); + assert_eq!(determine_bytes_per_cluster(32 * GB as u64, FatType::Fat32, 512), 16 * KB as u32); + assert_eq!(determine_bytes_per_cluster(32 * GB as u64 + 1, FatType::Fat32, 512), 32 * KB as u32); + assert_eq!(determine_bytes_per_cluster(999 * GB as u64, FatType::Fat32, 512), 32 * KB as u32); } #[test] fn test_determine_sectors_per_fat() { - assert_eq!(determine_sectors_per_fat(1 * MB / 512, 1, 2, 32, 1, FatType::Fat12), 6); + assert_eq!(determine_sectors_per_fat(1 * MB as u32 / 512, 1, 2, 32, 1, FatType::Fat12), 6); } } diff --git a/tests/format.rs b/tests/format.rs index 36b26b2..10958c9 100644 --- a/tests/format.rs +++ b/tests/format.rs @@ -7,8 +7,8 @@ use std::io::prelude::*; use fscommon::BufStream; -const KB: u32 = 1024; -const MB: u32 = KB * 1024; +const KB: u64 = 1024; +const MB: u64 = KB * 1024; const TEST_STR: &str = "Hi there Rust programmer!\n"; type FileSystem = fatfs::FileSystem>>>; @@ -18,12 +18,34 @@ fn basic_fs_test(fs: &FileSystem) { let entries = root_dir.iter().map(|r| r.unwrap()).collect::>(); assert_eq!(entries.len(), 0); - let mut file = root_dir.create_file("short.txt").expect("create file"); - file.truncate().expect("truncate file"); - file.write_all(&TEST_STR.as_bytes()).expect("write file"); + let subdir1 = root_dir.create_dir("subdir1").expect("create_dir subdir1"); + let subdir2 = root_dir.create_dir("subdir1/subdir2 with long name").expect("create_dir subdir2"); - let entries = root_dir.iter().map(|r| r.unwrap()).collect::>(); - assert_eq!(entries.len(), 1); + let test_str = TEST_STR.repeat(1000); + { + let mut file = subdir2.create_file("test file name.txt").expect("create file"); + file.truncate().expect("truncate file"); + file.write_all(test_str.as_bytes()).expect("write file"); + } + + let mut file = root_dir.open_file("subdir1/subdir2 with long name/test file name.txt").unwrap(); + let mut content = String::new(); + file.read_to_string(&mut content).expect("read_to_string"); + assert_eq!(content, test_str); + + let filenames = root_dir.iter().map(|r| r.unwrap().file_name()).collect::>(); + assert_eq!(filenames, ["subdir1"]); + + let filenames = subdir2.iter().map(|r| r.unwrap().file_name()).collect::>(); + assert_eq!(filenames, [".", "..", "test file name.txt"]); + + subdir1.rename("subdir2 with long name/test file name.txt", &root_dir, "new-name.txt").expect("rename"); + + let filenames = subdir2.iter().map(|r| r.unwrap().file_name()).collect::>(); + assert_eq!(filenames, [".", ".."]); + + let filenames = root_dir.iter().map(|r| r.unwrap().file_name()).collect::>(); + assert_eq!(filenames, ["subdir1", "new-name.txt"]); } fn test_format_fs(opts: fatfs::FormatOptions, total_bytes: u64) -> FileSystem { @@ -40,7 +62,7 @@ fn test_format_fs(opts: fatfs::FormatOptions, total_bytes: u64) -> FileSystem { #[test] fn test_format_1mb() { - let total_bytes = 1 * MB as u64; + let total_bytes = 1 * MB; let mut opts: fatfs::FormatOptions = Default::default(); opts.total_sectors = (total_bytes / 512) as u32; let fs = test_format_fs(opts, total_bytes); @@ -49,7 +71,7 @@ fn test_format_1mb() { #[test] fn test_format_8mb() { - let total_bytes = 8 * MB as u64; + let total_bytes = 8 * MB; let mut opts: fatfs::FormatOptions = Default::default(); opts.total_sectors = (total_bytes / 512) as u32; let fs = test_format_fs(opts, total_bytes); @@ -58,7 +80,7 @@ fn test_format_8mb() { #[test] fn test_format_50mb() { - let total_bytes = 50 * MB as u64; + let total_bytes = 50 * MB; let mut opts: fatfs::FormatOptions = Default::default(); opts.total_sectors = (total_bytes / 512) as u32; let fs = test_format_fs(opts, total_bytes); @@ -68,9 +90,24 @@ fn test_format_50mb() { #[test] fn test_format_512mb() { - let total_bytes = 2 * 1024 * MB as u64; + let total_bytes = 2 * 1024 * MB; let mut opts: fatfs::FormatOptions = Default::default(); opts.total_sectors = (total_bytes / 512) as u32; let fs = test_format_fs(opts, total_bytes); assert_eq!(fs.fat_type(), fatfs::FatType::Fat32); } + +fn create_format_options(total_bytes: u64, bytes_per_sector: Option) -> fatfs::FormatOptions { + let mut opts: fatfs::FormatOptions = Default::default(); + opts.total_sectors = (total_bytes / bytes_per_sector.unwrap_or(512) as u64) as u32; + opts.bytes_per_sector = bytes_per_sector; + opts +} + +#[test] +fn test_format_512mb_4096sec() { + let total_bytes = 1 * 1024 * MB; + let opts = create_format_options(total_bytes, Some(1024)); + let fs = test_format_fs(opts, total_bytes); + assert_eq!(fs.fat_type(), fatfs::FatType::Fat32); +} diff --git a/tests/write.rs b/tests/write.rs index 814b57a..b60473b 100644 --- a/tests/write.rs +++ b/tests/write.rs @@ -76,7 +76,7 @@ fn test_write_long_file(fs: FileSystem) { let root_dir = fs.root_dir(); let mut file = root_dir.open_file("long.txt").expect("open file"); file.truncate().unwrap(); - let test_str = TEST_STR.repeat(100); + let test_str = TEST_STR.repeat(1000); file.write_all(&test_str.as_bytes()).unwrap(); file.seek(io::SeekFrom::Start(0)).unwrap(); let mut buf = Vec::new();