7
7
//! This module provides functionality to detect and read superblocks from different
8
8
//! filesystem types including Btrfs, Ext4, F2FS, LUKS2, and XFS.
9
9
10
- use std:: io:: { self , BufReader , Cursor , Read , Seek } ;
10
+ use std:: io:: { self , BufRead , Cursor , Read , Seek } ;
11
11
12
12
use thiserror:: Error ;
13
13
use zerocopy:: FromBytes ;
@@ -66,8 +66,7 @@ pub enum Error {
66
66
}
67
67
68
68
/// 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 > {
71
70
reader. seek ( io:: SeekFrom :: Start ( T :: MAGIC_OFFSET ) ) ?;
72
71
let mut magic_buf = vec ! [ 0u8 ; std:: mem:: size_of:: <T :: Magic >( ) ] ;
73
72
reader. read_exact ( & mut magic_buf) ?;
@@ -95,24 +94,24 @@ pub enum Kind {
95
94
/// Ext4 filesystem
96
95
Ext4 ,
97
96
/// LUKS2 encrypted container
98
- LUKS2 ,
97
+ Luks2 ,
99
98
/// F2FS (Flash-Friendly File System)
100
99
F2FS ,
101
100
/// XFS filesystem
102
- XFS ,
101
+ Xfs ,
103
102
/// FAT filesystem
104
- FAT ,
103
+ Fat ,
105
104
}
106
105
107
106
impl std:: fmt:: Display for Kind {
108
107
fn fmt ( & self , f : & mut std:: fmt:: Formatter < ' _ > ) -> std:: fmt:: Result {
109
108
match & self {
110
109
Kind :: Btrfs => f. write_str ( "btrfs" ) ,
111
110
Kind :: Ext4 => f. write_str ( "ext4" ) ,
112
- Kind :: LUKS2 => f. write_str ( "luks2" ) ,
111
+ Kind :: Luks2 => f. write_str ( "luks2" ) ,
113
112
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" ) ,
116
115
}
117
116
}
118
117
}
@@ -121,9 +120,9 @@ pub enum Superblock {
121
120
Btrfs ( Box < btrfs:: Btrfs > ) ,
122
121
Ext4 ( Box < ext4:: Ext4 > ) ,
123
122
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 > ) ,
127
126
}
128
127
129
128
impl Superblock {
@@ -133,9 +132,9 @@ impl Superblock {
133
132
Superblock :: Btrfs ( _) => Kind :: Btrfs ,
134
133
Superblock :: Ext4 ( _) => Kind :: Ext4 ,
135
134
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 ,
139
138
}
140
139
}
141
140
@@ -145,9 +144,9 @@ impl Superblock {
145
144
Superblock :: Btrfs ( block) => block. uuid ( ) ,
146
145
Superblock :: Ext4 ( block) => block. uuid ( ) ,
147
146
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 ( ) ,
151
150
}
152
151
}
153
152
@@ -157,9 +156,9 @@ impl Superblock {
157
156
Superblock :: Btrfs ( block) => block. label ( ) ,
158
157
Superblock :: Ext4 ( block) => block. label ( ) ,
159
158
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 ( ) ,
163
162
}
164
163
}
165
164
}
@@ -181,14 +180,14 @@ impl Superblock {
181
180
if let Some ( sb) = detect_superblock :: < f2fs:: F2FS , _ > ( & mut cursor) ? {
182
181
return Ok ( Self :: F2FS ( Box :: new ( sb) ) ) ;
183
182
}
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) ) ) ;
186
185
}
187
186
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) ) ) ;
189
188
}
190
189
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) ) ) ;
192
191
}
193
192
Err ( Error :: UnknownSuperblock )
194
193
}
@@ -239,10 +238,10 @@ mod tests {
239
238
"blsforme testing" ,
240
239
"d2c85810-4e75-4274-bc7d-a78267af7443" ,
241
240
) ,
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" ) ,
246
245
] ;
247
246
248
247
// Pre-allocate a buffer for determination tests
@@ -268,7 +267,7 @@ mod tests {
268
267
assert_eq ! ( block. uuid( ) . unwrap( ) , uuid) ;
269
268
270
269
// 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 {
272
271
let config = block. read_config ( & mut cursor) . expect ( "Cannot read LUKS2 config" ) ;
273
272
eprintln ! ( "{}" , serde_json:: to_string_pretty( & config) . unwrap( ) ) ;
274
273
assert ! ( config. config. json_size > 0 ) ;
0 commit comments