2018-11-01 05:01:05 +08:00
|
|
|
extern crate env_logger;
|
|
|
|
extern crate fatfs;
|
|
|
|
extern crate fscommon;
|
|
|
|
|
|
|
|
use std::io;
|
|
|
|
use std::io::prelude::*;
|
|
|
|
|
|
|
|
use fscommon::BufStream;
|
|
|
|
|
2018-12-06 04:10:59 +08:00
|
|
|
const KB: u64 = 1024;
|
|
|
|
const MB: u64 = KB * 1024;
|
2018-11-01 05:01:05 +08:00
|
|
|
const TEST_STR: &str = "Hi there Rust programmer!\n";
|
|
|
|
|
2018-12-04 07:06:41 +08:00
|
|
|
type FileSystem = fatfs::FileSystem<BufStream<io::Cursor<Vec<u8>>>>;
|
2018-11-01 05:01:05 +08:00
|
|
|
|
2018-12-04 07:06:41 +08:00
|
|
|
fn basic_fs_test(fs: &FileSystem) {
|
2018-12-13 05:10:00 +08:00
|
|
|
let stats = fs.stats().expect("stats");
|
|
|
|
if fs.fat_type() == fatfs::FatType::Fat32 {
|
|
|
|
// On FAT32 one cluster is allocated for root directory
|
|
|
|
assert_eq!(stats.total_clusters(), stats.free_clusters() + 1);
|
|
|
|
} else {
|
|
|
|
assert_eq!(stats.total_clusters(), stats.free_clusters());
|
|
|
|
}
|
|
|
|
|
2018-11-01 05:01:05 +08:00
|
|
|
let root_dir = fs.root_dir();
|
|
|
|
let entries = root_dir.iter().map(|r| r.unwrap()).collect::<Vec<_>>();
|
|
|
|
assert_eq!(entries.len(), 0);
|
|
|
|
|
2018-12-06 04:10:59 +08:00
|
|
|
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");
|
2018-11-01 05:01:05 +08:00
|
|
|
|
2018-12-06 04:10:59 +08:00
|
|
|
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::<Vec<String>>();
|
|
|
|
assert_eq!(filenames, ["subdir1"]);
|
|
|
|
|
|
|
|
let filenames = subdir2.iter().map(|r| r.unwrap().file_name()).collect::<Vec<String>>();
|
|
|
|
assert_eq!(filenames, [".", "..", "test file name.txt"]);
|
|
|
|
|
2018-12-30 03:21:24 +08:00
|
|
|
subdir1.rename("subdir2 with long name/test file name.txt", &root_dir, "new-name.txt").expect("rename");
|
2018-12-06 04:10:59 +08:00
|
|
|
|
|
|
|
let filenames = subdir2.iter().map(|r| r.unwrap().file_name()).collect::<Vec<String>>();
|
|
|
|
assert_eq!(filenames, [".", ".."]);
|
|
|
|
|
|
|
|
let filenames = root_dir.iter().map(|r| r.unwrap().file_name()).collect::<Vec<String>>();
|
|
|
|
assert_eq!(filenames, ["subdir1", "new-name.txt"]);
|
2018-11-01 05:01:05 +08:00
|
|
|
}
|
2018-12-04 07:06:41 +08:00
|
|
|
|
2018-12-06 05:29:01 +08:00
|
|
|
fn test_format_fs(opts: fatfs::FormatVolumeOptions, total_bytes: u64) -> FileSystem {
|
2018-12-04 07:06:41 +08:00
|
|
|
let _ = env_logger::try_init();
|
2018-12-09 02:33:56 +08:00
|
|
|
// Init storage to 0xD1 bytes (value has been choosen to be parsed as normal file)
|
|
|
|
let storage_vec: Vec<u8> = vec![0xD1u8; total_bytes as usize];
|
2018-12-04 07:06:41 +08:00
|
|
|
let storage_cur = io::Cursor::new(storage_vec);
|
|
|
|
let mut buffered_stream = 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");
|
|
|
|
basic_fs_test(&fs);
|
|
|
|
fs
|
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn test_format_1mb() {
|
2018-12-06 04:10:59 +08:00
|
|
|
let total_bytes = 1 * MB;
|
2018-12-20 08:47:34 +08:00
|
|
|
let opts = fatfs::FormatVolumeOptions::new();
|
2018-12-04 07:06:41 +08:00
|
|
|
let fs = test_format_fs(opts, total_bytes);
|
|
|
|
assert_eq!(fs.fat_type(), fatfs::FatType::Fat12);
|
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
2018-12-30 02:37:46 +08:00
|
|
|
fn test_format_8mb_1fat() {
|
2018-12-06 04:10:59 +08:00
|
|
|
let total_bytes = 8 * MB;
|
2018-12-30 02:37:46 +08:00
|
|
|
let opts = fatfs::FormatVolumeOptions::new().fats(1);
|
2018-12-04 07:06:41 +08:00
|
|
|
let fs = test_format_fs(opts, total_bytes);
|
|
|
|
assert_eq!(fs.fat_type(), fatfs::FatType::Fat16);
|
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn test_format_50mb() {
|
2018-12-06 04:10:59 +08:00
|
|
|
let total_bytes = 50 * MB;
|
2018-12-20 08:47:34 +08:00
|
|
|
let opts = fatfs::FormatVolumeOptions::new();
|
2018-12-04 07:06:41 +08:00
|
|
|
let fs = test_format_fs(opts, total_bytes);
|
|
|
|
assert_eq!(fs.fat_type(), fatfs::FatType::Fat16);
|
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
2018-12-09 02:33:56 +08:00
|
|
|
fn test_format_512mb_512sec() {
|
2018-12-06 04:10:59 +08:00
|
|
|
let total_bytes = 2 * 1024 * MB;
|
2018-12-20 08:47:34 +08:00
|
|
|
let opts = fatfs::FormatVolumeOptions::new();
|
2018-12-04 07:06:41 +08:00
|
|
|
let fs = test_format_fs(opts, total_bytes);
|
|
|
|
assert_eq!(fs.fat_type(), fatfs::FatType::Fat32);
|
|
|
|
}
|
2018-12-06 04:10:59 +08:00
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn test_format_512mb_4096sec() {
|
|
|
|
let total_bytes = 1 * 1024 * MB;
|
2018-12-20 08:47:34 +08:00
|
|
|
let opts = fatfs::FormatVolumeOptions::new().bytes_per_sector(4096);
|
2018-12-06 04:10:59 +08:00
|
|
|
let fs = test_format_fs(opts, total_bytes);
|
|
|
|
assert_eq!(fs.fat_type(), fatfs::FatType::Fat32);
|
|
|
|
}
|
2021-10-10 05:12:16 +08:00
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn test_format_empty_volume_label() {
|
|
|
|
let total_bytes = 2 * 1024 * MB;
|
|
|
|
let opts = fatfs::FormatVolumeOptions::new();
|
|
|
|
let fs = test_format_fs(opts, total_bytes);
|
|
|
|
assert_eq!(fs.volume_label(), "NO NAME");
|
|
|
|
assert_eq!(fs.read_volume_label_from_root_dir().unwrap(), None);
|
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn test_format_volume_label_and_id() {
|
|
|
|
let total_bytes = 2 * 1024 * MB;
|
|
|
|
let opts = fatfs::FormatVolumeOptions::new()
|
|
|
|
.volume_id(1234)
|
|
|
|
.volume_label(*b"VOLUMELABEL");
|
|
|
|
let fs = test_format_fs(opts, total_bytes);
|
|
|
|
assert_eq!(fs.volume_label(), "VOLUMELABEL");
|
|
|
|
assert_eq!(
|
|
|
|
fs.read_volume_label_from_root_dir().unwrap(),
|
|
|
|
Some("VOLUMELABEL".to_string())
|
|
|
|
);
|
|
|
|
assert_eq!(fs.volume_id(), 1234);
|
|
|
|
}
|