-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #4 from sorcerersr/data_from_gpt
Data from gpt (UUIDs )
- Loading branch information
Showing
10 changed files
with
208 additions
and
4 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -8,3 +8,5 @@ Cargo.lock | |
|
||
# These are backup files generated by rustfmt | ||
**/*.rs.bk | ||
|
||
.vscode/* |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -11,12 +11,12 @@ authors = ["Stefan Rabmund <[email protected]>"] | |
readme = "crates-io.md" | ||
|
||
[dependencies] | ||
gpt = {version = "3.1.0", optional=true} | ||
thiserror = "1.0" | ||
|
||
[dev-dependencies] | ||
tempfile = "3" | ||
|
||
|
||
[[example]] | ||
name = "simple_main" | ||
path = "examples/simple_main.rs" |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,6 @@ | ||
coverage: | ||
status: | ||
project: | ||
default: | ||
target: 80% | ||
threshold: 1% |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Binary file not shown.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,123 @@ | ||
use crate::Device; | ||
#[cfg(feature = "gpt")] | ||
use gpt; | ||
|
||
#[cfg(not(test))] | ||
const DEV_DIR: &str = "/dev/"; | ||
|
||
#[cfg(test)] | ||
const DEV_DIR: &str = concat!(env!("CARGO_MANIFEST_DIR"), "/resources", "/test"); | ||
|
||
/// Enumeration for holding the gpt UUID or a reason why it is not available | ||
#[derive(Debug)] | ||
pub enum GptUUID { | ||
/// an io error happened when opening the device for read access | ||
IoError(std::io::Error), | ||
/// the UUID from the partition table (gpt) as a hyphenated string | ||
UUID(String), | ||
/// the feature "gpt" was not enabled | ||
FeatureNotEnabled, | ||
/// reading the gpt was successful, but no header was found | ||
NotAvailable, | ||
} | ||
|
||
// when the feature "gpt" is not enabled this function is used | ||
// to set the GptUUID::FeatureNotEnabled value | ||
#[cfg(not(feature = "gpt"))] | ||
pub fn enrich_with_gpt_uuid(mut device: Device) -> Device { | ||
device.uuid = GptUUID::FeatureNotEnabled; | ||
device | ||
} | ||
|
||
// When the feature "gpt" is enabled then this function will actually read the | ||
// partition table (gpt) to get the UUID for the device and the partitions | ||
#[cfg(feature = "gpt")] | ||
pub fn enrich_with_gpt_uuid(mut device: Device) -> Device { | ||
let diskpath = std::path::Path::new(DEV_DIR).join(device.name.to_string()); | ||
let cfg = gpt::GptConfig::new().writable(false); | ||
match cfg.open(diskpath) { | ||
Err(error) => device.uuid = GptUUID::IoError(error), | ||
Ok(disk) => { | ||
match disk.primary_header() { | ||
None => device.uuid = GptUUID::NotAvailable, | ||
Some(disk_header) => { | ||
device.uuid = GptUUID::UUID(disk_header.disk_guid.as_hyphenated().to_string()); | ||
} | ||
}; | ||
for partition in device.partitions.iter_mut() { | ||
match disk.partitions().get(&partition.number) { | ||
Some(gpt_partition) => { | ||
partition.part_uuid = | ||
GptUUID::UUID(gpt_partition.part_guid.as_hyphenated().to_string()) | ||
} | ||
None => partition.part_uuid = GptUUID::NotAvailable, | ||
} | ||
} | ||
} | ||
}; | ||
|
||
device | ||
} | ||
|
||
#[cfg(test)] | ||
mod tests { | ||
|
||
use super::*; | ||
|
||
#[cfg(feature = "gpt")] | ||
#[test] | ||
fn test_enrich_with_gpt_uuid() { | ||
use crate::{get_devices, Partition, Size}; | ||
|
||
let partition1 = Partition { | ||
name: "sda1".to_string(), | ||
size: Size::new(512), | ||
number: 1, | ||
mountpoint: None, | ||
part_uuid: GptUUID::NotAvailable, | ||
}; | ||
let partition2 = Partition { | ||
name: "sda2".to_string(), | ||
size: Size::new(512), | ||
number: 2, | ||
mountpoint: None, | ||
part_uuid: GptUUID::NotAvailable, | ||
}; | ||
|
||
let mut device = Device { | ||
name: "gptdisk.img".to_string(), | ||
partitions: vec![partition1, partition2], | ||
is_removable: false, | ||
model: None, | ||
serial: None, | ||
size: Size::new(42), | ||
uuid: GptUUID::NotAvailable, | ||
}; | ||
device = enrich_with_gpt_uuid(device); | ||
|
||
match device.uuid { | ||
GptUUID::UUID(uuid) => assert_eq!("f0ce7b2c-74af-47e4-8141-b2fe24ac20cc", uuid), | ||
_ => panic!("No UUID"), | ||
} | ||
match &device | ||
.partitions | ||
.iter() | ||
.find(|&partition| partition.number == 1) | ||
.unwrap() | ||
.part_uuid | ||
{ | ||
GptUUID::UUID(uuid) => assert_eq!("3cdd6997-9b47-46f1-a160-49546976c24e", uuid), | ||
_ => panic!("Partition 1 - no UUID"), | ||
} | ||
match &device | ||
.partitions | ||
.iter() | ||
.find(|&partition| partition.number == 2) | ||
.unwrap() | ||
.part_uuid | ||
{ | ||
GptUUID::UUID(uuid) => assert_eq!("4d3adf65-ff1b-473c-8f5e-b6c8d228b8d4", uuid), | ||
_ => panic!("Partition 2 - no UUID"), | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters