Skip to content

Commit 677376a

Browse files
authored
Merge branch 'develop' into support_delete
2 parents 6103787 + 6071df4 commit 677376a

File tree

5 files changed

+215
-190
lines changed

5 files changed

+215
-190
lines changed

Cargo.toml

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,10 +11,11 @@ edition = "2018"
1111
readme = "README.md"
1212

1313
[dependencies]
14-
embedded-hal = "0.2.2"
14+
embedded-hal = "^0.2.2"
1515
byteorder = { version = "1", default-features = false }
1616
nb = "0.1"
1717
log = "0.4"
1818

1919
[dev-dependencies]
2020
env_logger = "0.7"
21+
hex-literal = "0.3"

src/fat.rs

Lines changed: 34 additions & 39 deletions
Original file line numberDiff line numberDiff line change
@@ -1665,45 +1665,40 @@ mod test {
16651665
#[test]
16661666
fn test_bpb() {
16671667
// Taken from a Raspberry Pi bootable SD-Card
1668-
const BPB_EXAMPLE: [u8; 512] = [
1669-
0xeb, 0x3c, 0x90, 0x6d, 0x6b, 0x66, 0x73, 0x2e, 0x66, 0x61, 0x74, 0x00, 0x02, 0x10,
1670-
0x01, 0x00, 0x02, 0x00, 0x02, 0x00, 0x00, 0xf8, 0x20, 0x00, 0x3f, 0x00, 0xff, 0x00,
1671-
0x00, 0x00, 0x00, 0x00, 0x00, 0xe0, 0x01, 0x00, 0x80, 0x01, 0x29, 0xbb, 0xb0, 0x71,
1672-
0x77, 0x62, 0x6f, 0x6f, 0x74, 0x20, 0x20, 0x20, 0x20, 0x20, 0x20, 0x20, 0x46, 0x41,
1673-
0x54, 0x31, 0x36, 0x20, 0x20, 0x20, 0x0e, 0x1f, 0xbe, 0x5b, 0x7c, 0xac, 0x22, 0xc0,
1674-
0x74, 0x0b, 0x56, 0xb4, 0x0e, 0xbb, 0x07, 0x00, 0xcd, 0x10, 0x5e, 0xeb, 0xf0, 0x32,
1675-
0xe4, 0xcd, 0x16, 0xcd, 0x19, 0xeb, 0xfe, 0x54, 0x68, 0x69, 0x73, 0x20, 0x69, 0x73,
1676-
0x20, 0x6e, 0x6f, 0x74, 0x20, 0x61, 0x20, 0x62, 0x6f, 0x6f, 0x74, 0x61, 0x62, 0x6c,
1677-
0x65, 0x20, 0x64, 0x69, 0x73, 0x6b, 0x2e, 0x20, 0x20, 0x50, 0x6c, 0x65, 0x61, 0x73,
1678-
0x65, 0x20, 0x69, 0x6e, 0x73, 0x65, 0x72, 0x74, 0x20, 0x61, 0x20, 0x62, 0x6f, 0x6f,
1679-
0x74, 0x61, 0x62, 0x6c, 0x65, 0x20, 0x66, 0x6c, 0x6f, 0x70, 0x70, 0x79, 0x20, 0x61,
1680-
0x6e, 0x64, 0x0d, 0x0a, 0x70, 0x72, 0x65, 0x73, 0x73, 0x20, 0x61, 0x6e, 0x79, 0x20,
1681-
0x6b, 0x65, 0x79, 0x20, 0x74, 0x6f, 0x20, 0x74, 0x72, 0x79, 0x20, 0x61, 0x67, 0x61,
1682-
0x69, 0x6e, 0x20, 0x2e, 0x2e, 0x2e, 0x20, 0x0d, 0x0a, 0x00, 0x00, 0x00, 0x00, 0x00,
1683-
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
1684-
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
1685-
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
1686-
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
1687-
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
1688-
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
1689-
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
1690-
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
1691-
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
1692-
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
1693-
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
1694-
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
1695-
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
1696-
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
1697-
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
1698-
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
1699-
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
1700-
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
1701-
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
1702-
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
1703-
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
1704-
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
1705-
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x55, 0xaa,
1706-
];
1668+
const BPB_EXAMPLE: [u8; 512] = hex!(
1669+
"EB 3C 90 6D 6B 66 73 2E 66 61 74 00 02 10 01 00
1670+
02 00 02 00 00 F8 20 00 3F 00 FF 00 00 00 00 00
1671+
00 E0 01 00 80 01 29 BB B0 71 77 62 6F 6F 74 20
1672+
20 20 20 20 20 20 46 41 54 31 36 20 20 20 0E 1F
1673+
BE 5B 7C AC 22 C0 74 0B 56 B4 0E BB 07 00 CD 10
1674+
5E EB F0 32 E4 CD 16 CD 19 EB FE 54 68 69 73 20
1675+
69 73 20 6E 6F 74 20 61 20 62 6F 6F 74 61 62 6C
1676+
65 20 64 69 73 6B 2E 20 20 50 6C 65 61 73 65 20
1677+
69 6E 73 65 72 74 20 61 20 62 6F 6F 74 61 62 6C
1678+
65 20 66 6C 6F 70 70 79 20 61 6E 64 0D 0A 70 72
1679+
65 73 73 20 61 6E 79 20 6B 65 79 20 74 6F 20 74
1680+
72 79 20 61 67 61 69 6E 20 2E 2E 2E 20 0D 0A 00
1681+
00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00
1682+
00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00
1683+
00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00
1684+
00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00
1685+
00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00
1686+
00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00
1687+
00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00
1688+
00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00
1689+
00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00
1690+
00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00
1691+
00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00
1692+
00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00
1693+
00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00
1694+
00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00
1695+
00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00
1696+
00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00
1697+
00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00
1698+
00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00
1699+
00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00
1700+
00 00 00 00 00 00 00 00 00 00 00 00 00 00 55 AA"
1701+
);
17071702
let bpb = Bpb::create_from_bytes(&BPB_EXAMPLE).unwrap();
17081703
assert_eq!(bpb.footer(), Bpb::FOOTER_VALUE);
17091704
assert_eq!(bpb.oem_name(), b"mkfs.fat");

src/filesystem.rs

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -142,6 +142,8 @@ pub enum FilenameError {
142142
NameTooLong,
143143
/// Can't start a file with a period, or after 8 characters.
144144
MisplacedPeriod,
145+
/// Can't extract utf8 from file name
146+
Utf8Error,
145147
}
146148

147149
// ****************************************************************************
@@ -268,6 +270,20 @@ impl ShortFileName {
268270
const FILENAME_BASE_MAX_LEN: usize = 8;
269271
const FILENAME_MAX_LEN: usize = 11;
270272

273+
/// Get base name (name without extension) of file name
274+
pub fn base_name(&self) -> &[u8] {
275+
Self::bytes_before_space(&self.contents[..Self::FILENAME_BASE_MAX_LEN])
276+
}
277+
278+
/// Get base name (name without extension) of file name
279+
pub fn extension(&self) -> &[u8] {
280+
Self::bytes_before_space(&self.contents[Self::FILENAME_BASE_MAX_LEN..])
281+
}
282+
283+
fn bytes_before_space(bytes: &[u8]) -> &[u8] {
284+
bytes.split(|b| *b == b' ').next().unwrap_or(&bytes[0..0])
285+
}
286+
271287
/// Create a new MS-DOS 8.3 space-padded file name as stored in the directory entry.
272288
pub fn create_from_str(name: &str) -> Result<ShortFileName, FilenameError> {
273289
let mut sfn = ShortFileName {
@@ -735,6 +751,26 @@ mod test {
735751
assert_eq!(sfn, ShortFileName::create_from_str("HELLO.TXT").unwrap());
736752
}
737753

754+
#[test]
755+
fn filename_get_extension() {
756+
let mut sfn = ShortFileName::create_from_str("hello.txt").unwrap();
757+
assert_eq!(sfn.extension(), "TXT".as_bytes());
758+
sfn = ShortFileName::create_from_str("hello").unwrap();
759+
assert_eq!(sfn.extension(), "".as_bytes());
760+
sfn = ShortFileName::create_from_str("hello.a").unwrap();
761+
assert_eq!(sfn.extension(), "A".as_bytes());
762+
}
763+
764+
#[test]
765+
fn filename_get_base_name() {
766+
let mut sfn = ShortFileName::create_from_str("hello.txt").unwrap();
767+
assert_eq!(sfn.base_name(), "HELLO".as_bytes());
768+
sfn = ShortFileName::create_from_str("12345678").unwrap();
769+
assert_eq!(sfn.base_name(), "12345678".as_bytes());
770+
sfn = ShortFileName::create_from_str("1").unwrap();
771+
assert_eq!(sfn.base_name(), "1".as_bytes());
772+
}
773+
738774
#[test]
739775
fn filename_fulllength() {
740776
let sfn = ShortFileName {

0 commit comments

Comments
 (0)