Skip to content

Commit cc65440

Browse files
authored
Merge pull request #25 from jplatte/refactor
Some refactoring
2 parents fa082ca + d19991e commit cc65440

File tree

2 files changed

+33
-34
lines changed

2 files changed

+33
-34
lines changed

crates/superblock/src/lib.rs

Lines changed: 29 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@
77
//! This module provides functionality to detect and read superblocks from different
88
//! filesystem types including Btrfs, Ext4, F2FS, LUKS2, and XFS.
99
10-
use std::io::{self, BufReader, Cursor, Read, Seek};
10+
use std::io::{self, BufRead, Cursor, Read, Seek};
1111

1212
use thiserror::Error;
1313
use zerocopy::FromBytes;
@@ -66,8 +66,7 @@ pub enum Error {
6666
}
6767

6868
/// Attempts to detect a superblock of the given type from the reader
69-
pub fn detect_superblock<T: Detection, R: Read + Seek>(reader: &mut R) -> Result<Option<T>, Error> {
70-
let mut reader = BufReader::new(reader);
69+
pub fn detect_superblock<T: Detection, R: BufRead + Seek>(reader: &mut R) -> Result<Option<T>, Error> {
7170
reader.seek(io::SeekFrom::Start(T::MAGIC_OFFSET))?;
7271
let mut magic_buf = vec![0u8; std::mem::size_of::<T::Magic>()];
7372
reader.read_exact(&mut magic_buf)?;
@@ -95,24 +94,24 @@ pub enum Kind {
9594
/// Ext4 filesystem
9695
Ext4,
9796
/// LUKS2 encrypted container
98-
LUKS2,
97+
Luks2,
9998
/// F2FS (Flash-Friendly File System)
10099
F2FS,
101100
/// XFS filesystem
102-
XFS,
101+
Xfs,
103102
/// FAT filesystem
104-
FAT,
103+
Fat,
105104
}
106105

107106
impl std::fmt::Display for Kind {
108107
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
109108
match &self {
110109
Kind::Btrfs => f.write_str("btrfs"),
111110
Kind::Ext4 => f.write_str("ext4"),
112-
Kind::LUKS2 => f.write_str("luks2"),
111+
Kind::Luks2 => f.write_str("luks2"),
113112
Kind::F2FS => f.write_str("f2fs"),
114-
Kind::XFS => f.write_str("xfs"),
115-
Kind::FAT => f.write_str("fat"),
113+
Kind::Xfs => f.write_str("xfs"),
114+
Kind::Fat => f.write_str("fat"),
116115
}
117116
}
118117
}
@@ -121,9 +120,9 @@ pub enum Superblock {
121120
Btrfs(Box<btrfs::Btrfs>),
122121
Ext4(Box<ext4::Ext4>),
123122
F2FS(Box<f2fs::F2FS>),
124-
LUKS2(Box<luks2::Luks2>),
125-
XFS(Box<xfs::XFS>),
126-
FAT(Box<fat::Fat>),
123+
Luks2(Box<luks2::Luks2>),
124+
Xfs(Box<xfs::Xfs>),
125+
Fat(Box<fat::Fat>),
127126
}
128127

129128
impl Superblock {
@@ -133,9 +132,9 @@ impl Superblock {
133132
Superblock::Btrfs(_) => Kind::Btrfs,
134133
Superblock::Ext4(_) => Kind::Ext4,
135134
Superblock::F2FS(_) => Kind::F2FS,
136-
Superblock::LUKS2(_) => Kind::LUKS2,
137-
Superblock::XFS(_) => Kind::XFS,
138-
Superblock::FAT(_) => Kind::FAT,
135+
Superblock::Luks2(_) => Kind::Luks2,
136+
Superblock::Xfs(_) => Kind::Xfs,
137+
Superblock::Fat(_) => Kind::Fat,
139138
}
140139
}
141140

@@ -145,9 +144,9 @@ impl Superblock {
145144
Superblock::Btrfs(block) => block.uuid(),
146145
Superblock::Ext4(block) => block.uuid(),
147146
Superblock::F2FS(block) => block.uuid(),
148-
Superblock::LUKS2(block) => block.uuid(),
149-
Superblock::XFS(block) => block.uuid(),
150-
Superblock::FAT(block) => block.uuid(),
147+
Superblock::Luks2(block) => block.uuid(),
148+
Superblock::Xfs(block) => block.uuid(),
149+
Superblock::Fat(block) => block.uuid(),
151150
}
152151
}
153152

@@ -157,9 +156,9 @@ impl Superblock {
157156
Superblock::Btrfs(block) => block.label(),
158157
Superblock::Ext4(block) => block.label(),
159158
Superblock::F2FS(block) => block.label(),
160-
Superblock::LUKS2(block) => block.label(),
161-
Superblock::XFS(block) => block.label(),
162-
Superblock::FAT(block) => block.label(),
159+
Superblock::Luks2(block) => block.label(),
160+
Superblock::Xfs(block) => block.label(),
161+
Superblock::Fat(block) => block.label(),
163162
}
164163
}
165164
}
@@ -181,14 +180,14 @@ impl Superblock {
181180
if let Some(sb) = detect_superblock::<f2fs::F2FS, _>(&mut cursor)? {
182181
return Ok(Self::F2FS(Box::new(sb)));
183182
}
184-
if let Some(sb) = detect_superblock::<xfs::XFS, _>(&mut cursor)? {
185-
return Ok(Self::XFS(Box::new(sb)));
183+
if let Some(sb) = detect_superblock::<xfs::Xfs, _>(&mut cursor)? {
184+
return Ok(Self::Xfs(Box::new(sb)));
186185
}
187186
if let Some(sb) = detect_superblock::<luks2::Luks2, _>(&mut cursor)? {
188-
return Ok(Self::LUKS2(Box::new(sb)));
187+
return Ok(Self::Luks2(Box::new(sb)));
189188
}
190189
if let Some(sb) = detect_superblock::<fat::Fat, _>(&mut cursor)? {
191-
return Ok(Self::FAT(Box::new(sb)));
190+
return Ok(Self::Fat(Box::new(sb)));
192191
}
193192
Err(Error::UnknownSuperblock)
194193
}
@@ -239,10 +238,10 @@ mod tests {
239238
"blsforme testing",
240239
"d2c85810-4e75-4274-bc7d-a78267af7443",
241240
),
242-
("luks+ext4", Kind::LUKS2, "", "be373cae-2bd1-4ad5-953f-3463b2e53e59"),
243-
("xfs", Kind::XFS, "BLSFORME", "45e8a3bf-8114-400f-95b0-380d0fb7d42d"),
244-
("fat16", Kind::FAT, "TESTLABEL", "A1B2-C3D4"),
245-
("fat32", Kind::FAT, "TESTLABEL", "A1B2-C3D4"),
241+
("luks+ext4", Kind::Luks2, "", "be373cae-2bd1-4ad5-953f-3463b2e53e59"),
242+
("xfs", Kind::Xfs, "BLSFORME", "45e8a3bf-8114-400f-95b0-380d0fb7d42d"),
243+
("fat16", Kind::Fat, "TESTLABEL", "A1B2-C3D4"),
244+
("fat32", Kind::Fat, "TESTLABEL", "A1B2-C3D4"),
246245
];
247246

248247
// Pre-allocate a buffer for determination tests
@@ -268,7 +267,7 @@ mod tests {
268267
assert_eq!(block.uuid().unwrap(), uuid);
269268

270269
// Is it possible to get the JSON config out of LUKS2?
271-
if let Superblock::LUKS2(block) = block {
270+
if let Superblock::Luks2(block) = block {
272271
let config = block.read_config(&mut cursor).expect("Cannot read LUKS2 config");
273272
eprintln!("{}", serde_json::to_string_pretty(&config).unwrap());
274273
assert!(config.config.json_size > 0);

crates/superblock/src/xfs.rs

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -42,7 +42,7 @@ pub const MAX_LABEL_LEN: usize = 12;
4242
/// All multi-byte integer fields are stored in big-endian byte order.
4343
#[derive(FromBytes, Debug)]
4444
#[repr(C, align(8))]
45-
pub struct XFS {
45+
pub struct Xfs {
4646
/// Magic number, must contain 'XFSB'
4747
pub magicnum: U32<BigEndian>,
4848
/// Filesystem block size in bytes
@@ -164,7 +164,7 @@ pub struct XFS {
164164
/// XFS superblock magic number ('XFSB' in ASCII)
165165
pub const MAGIC: U32<BigEndian> = U32::new(0x58465342);
166166

167-
impl XFS {
167+
impl Xfs {
168168
/// Returns the filesystem UUID as a properly formatted string
169169
pub fn uuid(&self) -> Result<String, super::Error> {
170170
Ok(Uuid::from_bytes(self.uuid).hyphenated().to_string())
@@ -176,14 +176,14 @@ impl XFS {
176176
}
177177
}
178178

179-
impl Detection for XFS {
179+
impl Detection for Xfs {
180180
type Magic = U32<BigEndian>;
181181

182182
const OFFSET: u64 = 0x0;
183183

184184
const MAGIC_OFFSET: u64 = 0x0;
185185

186-
const SIZE: usize = std::mem::size_of::<XFS>();
186+
const SIZE: usize = std::mem::size_of::<Xfs>();
187187

188188
fn is_valid_magic(magic: &Self::Magic) -> bool {
189189
*magic == MAGIC

0 commit comments

Comments
 (0)