Create volume label dir entry when formatting

Fixes #41
This commit is contained in:
Rafał Harabień 2021-10-09 23:12:16 +02:00
parent 954c68457f
commit 75bb9d1293
3 changed files with 36 additions and 1 deletions

View File

@ -1,6 +1,11 @@
Changelog
=========
0.3.6 (not released yet)
------------------------
Bug fixes:
* Create directory entry with `VOLUME_ID` attribute when formatting if volume label was set in `FormatVolumeOptions`.
0.3.5 (2021-01-23)
------------------------
Bug fixes:

View File

@ -15,6 +15,7 @@ use byteorder_ext::{ReadBytesExt, WriteBytesExt};
use boot_sector::{format_boot_sector, BiosParameterBlock, BootSector};
use dir::{Dir, DirRawStream};
use dir_entry::{DirFileEntryData, FileAttributes};
use file::File;
use table::{alloc_cluster, count_free_clusters, format_fat, read_fat_flags, ClusterIterator, RESERVED_FAT_ENTRIES};
use time::{TimeProvider, DEFAULT_TIME_PROVIDER};
@ -997,7 +998,12 @@ pub fn format_volume<T: ReadWriteSeek>(mut disk: T, options: FormatVolumeOptions
write_zeros(&mut disk, boot.bpb.cluster_size() as u64)?;
}
// TODO: create volume label dir entry if volume label is set
// Create volume label directory entry if volume label is specified in options
if let Some(volume_label) = options.volume_label {
disk.seek(SeekFrom::Start(root_dir_pos))?;
let volume_entry = DirFileEntryData::new(volume_label, FileAttributes::VOLUME_ID);
volume_entry.serialize(&mut disk)?;
}
disk.seek(SeekFrom::Start(0))?;
trace!("format_volume end");

View File

@ -108,3 +108,27 @@ fn test_format_512mb_4096sec() {
let fs = test_format_fs(opts, total_bytes);
assert_eq!(fs.fat_type(), fatfs::FatType::Fat32);
}
#[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);
}