Skip to content

Commit 8caac44

Browse files
committed
Create volume label dir entry when formatting
Fixes #41
1 parent e2d4127 commit 8caac44

File tree

3 files changed

+32
-2
lines changed

3 files changed

+32
-2
lines changed

CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@ New features:
2525
* Add `Date::new`, `Time::new`, `DateTime::new` functions that instiantiate corresponding structures after ensuring
2626
that arguments are in the supported range. They panic if this is not the case.
2727
* Fix time encoding during a leap second if using `chrono`.
28+
* Create directory entry with `VOLUME_ID` attribute when formatting if volume label was set in `FormatVolumeOptions`.
2829
* Remove `byteorder` dependency.
2930
* Bump up minimal Rust compiler version to 1.46.0.
3031
* Build the crate using the 2018 edition.

src/fs.rs

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ use core::u32;
1111

1212
use crate::boot_sector::{format_boot_sector, BiosParameterBlock, BootSector};
1313
use crate::dir::{Dir, DirRawStream};
14-
use crate::dir_entry::{SFN_PADDING, SFN_SIZE};
14+
use crate::dir_entry::{DirFileEntryData, FileAttributes, SFN_PADDING, SFN_SIZE};
1515
use crate::error::Error;
1616
use crate::file::File;
1717
use crate::io::{self, IoBase, Read, ReadLeExt, Seek, SeekFrom, Write, WriteLeExt};
@@ -1192,7 +1192,12 @@ pub fn format_volume<S: ReadWriteSeek>(storage: &mut S, options: FormatVolumeOpt
11921192
write_zeros(storage, u64::from(bpb.cluster_size()))?;
11931193
}
11941194

1195-
// TODO: create volume label dir entry if volume label is set
1195+
// Create volume label directory entry if volume label is specified in options
1196+
if let Some(volume_label) = options.volume_label {
1197+
storage.seek(SeekFrom::Start(root_dir_pos))?;
1198+
let volume_entry = DirFileEntryData::new(volume_label, FileAttributes::VOLUME_ID);
1199+
volume_entry.serialize(storage)?;
1200+
}
11961201

11971202
storage.seek(SeekFrom::Start(0))?;
11981203
trace!("format_volume end");

tests/format.rs

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -116,3 +116,27 @@ fn test_format_1gb_4096sec() {
116116
let fs = test_format_fs(opts, total_bytes);
117117
assert_eq!(fs.fat_type(), fatfs::FatType::Fat32);
118118
}
119+
120+
#[test]
121+
fn test_format_empty_volume_label() {
122+
let total_bytes = 2 * 1024 * MB;
123+
let opts = fatfs::FormatVolumeOptions::new();
124+
let fs = test_format_fs(opts, total_bytes);
125+
assert_eq!(fs.volume_label(), "NO NAME");
126+
assert_eq!(fs.read_volume_label_from_root_dir().unwrap(), None);
127+
}
128+
129+
#[test]
130+
fn test_format_volume_label_and_id() {
131+
let total_bytes = 2 * 1024 * MB;
132+
let opts = fatfs::FormatVolumeOptions::new()
133+
.volume_id(1234)
134+
.volume_label(*b"VOLUMELABEL");
135+
let fs = test_format_fs(opts, total_bytes);
136+
assert_eq!(fs.volume_label(), "VOLUMELABEL");
137+
assert_eq!(
138+
fs.read_volume_label_from_root_dir().unwrap(),
139+
Some("VOLUMELABEL".to_string())
140+
);
141+
assert_eq!(fs.volume_id(), 1234);
142+
}

0 commit comments

Comments
 (0)