Use usize for Disk & Partition id values
This commit is contained in:
parent
485bb9357b
commit
1e223aa56a
4 changed files with 40 additions and 45 deletions
|
|
@ -346,10 +346,9 @@ impl App {
|
|||
let disk_list = self.disk_list.lock().unwrap();
|
||||
if let Some(disk_index) = self.disk_index_dest {
|
||||
if let Some(disk) = disk_list.get(disk_index) {
|
||||
let disk_id = disk.get_id();
|
||||
let table_type = self.table_type.clone().unwrap();
|
||||
let diskpart_script =
|
||||
build_dest_format_script(&disk_id, &table_type);
|
||||
build_dest_format_script(disk.id, &table_type);
|
||||
self.action_tx.send(Action::Diskpart(diskpart_script))?;
|
||||
}
|
||||
}
|
||||
|
|
|
|||
|
|
@ -30,7 +30,7 @@ use crate::system::diskpart;
|
|||
#[derive(Debug, Clone, Default, PartialEq, Eq, Serialize, Deserialize)]
|
||||
pub struct Disk {
|
||||
pub conn_type: String,
|
||||
pub id: String,
|
||||
pub id: usize,
|
||||
pub model: String,
|
||||
pub part_type: PartitionTableType,
|
||||
pub parts: Vec<Partition>,
|
||||
|
|
@ -42,7 +42,7 @@ pub struct Disk {
|
|||
|
||||
#[derive(Clone, Debug, Default, PartialEq, Eq, Serialize, Deserialize)]
|
||||
pub struct Partition {
|
||||
pub id: String,
|
||||
pub id: usize,
|
||||
pub is_selected: bool,
|
||||
pub fs_type: Option<String>,
|
||||
pub label: Option<String>,
|
||||
|
|
@ -65,11 +65,6 @@ impl Disk {
|
|||
}
|
||||
}
|
||||
|
||||
#[must_use]
|
||||
pub fn get_id(&self) -> &str {
|
||||
self.id.as_str()
|
||||
}
|
||||
|
||||
pub fn get_parts(&self) -> Vec<Partition> {
|
||||
self.parts.clone()
|
||||
}
|
||||
|
|
@ -162,37 +157,37 @@ pub fn get_fake_disks() -> Vec<Disk> {
|
|||
let mut disks = vec![
|
||||
Disk {
|
||||
conn_type: "SATA".to_string(),
|
||||
id: "/dev/sda".to_string(),
|
||||
id: 1,
|
||||
model: "Samsung Evo 870".to_string(),
|
||||
part_type: PartitionTableType::Legacy,
|
||||
parts: vec![
|
||||
Partition {
|
||||
id: String::from("1"),
|
||||
fs_type: Some(String::from("NTFS")),
|
||||
id: 1,
|
||||
label: Some(String::from("System Reserved")),
|
||||
part_type: String::from("7"),
|
||||
size: 104_857_600,
|
||||
..Default::default()
|
||||
},
|
||||
Partition {
|
||||
id: String::from("2"),
|
||||
fs_type: None,
|
||||
id: 2,
|
||||
label: None,
|
||||
part_type: String::from("5"),
|
||||
size: 267_806_310_400,
|
||||
..Default::default()
|
||||
},
|
||||
Partition {
|
||||
id: String::from("5"),
|
||||
fs_type: Some(String::from("NTFS")),
|
||||
id: 5,
|
||||
label: Some(String::from("Win7")),
|
||||
part_type: String::from("7"),
|
||||
size: 267_701_452_800,
|
||||
..Default::default()
|
||||
},
|
||||
Partition {
|
||||
id: String::from("6"),
|
||||
fs_type: Some(String::from("NTFS")),
|
||||
id: 6,
|
||||
label: Some(String::from("Tools")),
|
||||
part_type: String::from("7"),
|
||||
size: 524_288_000,
|
||||
|
|
@ -205,12 +200,12 @@ pub fn get_fake_disks() -> Vec<Disk> {
|
|||
},
|
||||
Disk {
|
||||
conn_type: "SATA".to_string(),
|
||||
id: "Disk 2".to_string(),
|
||||
id: 2,
|
||||
model: "ADATA Garbage".to_string(),
|
||||
part_type: PartitionTableType::Legacy,
|
||||
parts: vec![Partition {
|
||||
id: String::from("1"),
|
||||
fs_type: Some(String::from("NTFS")),
|
||||
id: 1,
|
||||
label: Some(String::from("Scratch")),
|
||||
part_type: String::from("7"),
|
||||
size: 249_998_951_424,
|
||||
|
|
@ -222,29 +217,29 @@ pub fn get_fake_disks() -> Vec<Disk> {
|
|||
},
|
||||
Disk {
|
||||
conn_type: "NVMe".to_string(),
|
||||
id: "/dev/nvme0n1".to_string(),
|
||||
id: 3,
|
||||
model: "Crucial P3 Plus".to_string(),
|
||||
part_type: PartitionTableType::Guid,
|
||||
parts: vec![
|
||||
Partition {
|
||||
id: String::from("1"),
|
||||
fs_type: Some(String::from("FAT32")),
|
||||
id: 1,
|
||||
label: Some(String::from("ESP")),
|
||||
part_type: String::from("EFI"),
|
||||
size: 272_629_760,
|
||||
..Default::default()
|
||||
},
|
||||
Partition {
|
||||
id: String::from("2"),
|
||||
fs_type: None,
|
||||
id: 2,
|
||||
label: None,
|
||||
part_type: String::from("MSR"),
|
||||
size: 16_777_216,
|
||||
..Default::default()
|
||||
},
|
||||
Partition {
|
||||
id: String::from("3"),
|
||||
fs_type: Some(String::from("NTFS")),
|
||||
id: 4,
|
||||
label: Some(String::from("Win10")),
|
||||
part_type: String::from("MS Basic Data"),
|
||||
size: 824_340_119_552,
|
||||
|
|
@ -257,21 +252,21 @@ pub fn get_fake_disks() -> Vec<Disk> {
|
|||
},
|
||||
Disk {
|
||||
conn_type: "IDE".to_string(),
|
||||
id: "/dev/hda".to_string(),
|
||||
id: 4,
|
||||
model: "Fireball".to_string(),
|
||||
part_type: PartitionTableType::Guid,
|
||||
parts: vec![
|
||||
Partition {
|
||||
id: String::from("1"),
|
||||
fs_type: Some(String::from("FAT32")),
|
||||
id: 1,
|
||||
label: Some(String::from("EFI Boot")),
|
||||
part_type: String::from("EFI"),
|
||||
size: 209_715_200,
|
||||
..Default::default()
|
||||
},
|
||||
Partition {
|
||||
id: String::from("2"),
|
||||
fs_type: None,
|
||||
id: 2,
|
||||
label: None,
|
||||
part_type: String::from("{48465300-0000-11AA-AA11-00306543ECAC}"),
|
||||
size: 171_586_879_488,
|
||||
|
|
@ -284,7 +279,7 @@ pub fn get_fake_disks() -> Vec<Disk> {
|
|||
},
|
||||
Disk {
|
||||
conn_type: "MISC".to_string(),
|
||||
id: "A:\\".to_string(),
|
||||
id: 5,
|
||||
part_type: PartitionTableType::Legacy,
|
||||
parts: Vec::new(),
|
||||
model: "Iomega".to_string(),
|
||||
|
|
@ -301,7 +296,7 @@ pub fn get_fake_disks() -> Vec<Disk> {
|
|||
}
|
||||
|
||||
#[must_use]
|
||||
pub fn get_disk_serial_number(id: &str) -> String {
|
||||
pub fn get_disk_serial_number(id: usize) -> String {
|
||||
let mut serial = String::new();
|
||||
if cfg!(windows) {
|
||||
let output = Command::new("wmic")
|
||||
|
|
|
|||
|
|
@ -31,7 +31,7 @@ use crate::system::disk::{
|
|||
|
||||
static DEFAULT_MAX_DISKS: usize = 8;
|
||||
|
||||
pub fn get_disk_details(disk_id: &str, disk_size: u64, disk_details: Option<&str>) -> Disk {
|
||||
pub fn get_disk_details(disk_id: usize, disk_size: u64, disk_details: Option<&str>) -> Disk {
|
||||
static RE_DETAILS: Lazy<Regex> = Lazy::new(|| {
|
||||
Regex::new(r"(.*?)\r?\nDisk ID\s*:\s+(.*?)\r?\nType\s*:\s+(.*?)\r?\n").unwrap()
|
||||
});
|
||||
|
|
@ -39,7 +39,7 @@ pub fn get_disk_details(disk_id: &str, disk_size: u64, disk_details: Option<&str
|
|||
Regex::new(r"^\{[0-9A-F]{8}-[0-9A-F]{4}-[0-9A-F]{4}-[0-9A-F]{4}-[0-9A-F]{12}\}$").unwrap()
|
||||
});
|
||||
let mut disk = Disk {
|
||||
id: String::from(disk_id),
|
||||
id: disk_id,
|
||||
size: disk_size,
|
||||
..Default::default()
|
||||
};
|
||||
|
|
@ -72,7 +72,7 @@ pub fn get_disk_details(disk_id: &str, disk_size: u64, disk_details: Option<&str
|
|||
}
|
||||
|
||||
pub fn get_partition_details(
|
||||
disk_id: &str,
|
||||
disk_id: usize,
|
||||
disk_details: Option<&str>,
|
||||
part_details: Option<&str>,
|
||||
) -> Vec<Partition> {
|
||||
|
|
@ -90,7 +90,7 @@ pub fn get_partition_details(
|
|||
};
|
||||
for (_, [number, size]) in RE_LIS.captures_iter(&contents).map(|c| c.extract()) {
|
||||
let part = Partition {
|
||||
id: String::from(number),
|
||||
id: number.parse().unwrap(),
|
||||
size: string_to_bytes(size),
|
||||
..Default::default()
|
||||
};
|
||||
|
|
@ -123,7 +123,8 @@ pub fn get_partition_details(
|
|||
}
|
||||
|
||||
#[must_use]
|
||||
pub fn build_dest_format_script(disk_id: &str, part_type: &PartitionTableType) -> String {
|
||||
pub fn build_dest_format_script(disk_id: usize, part_type: &PartitionTableType) -> String {
|
||||
let disk_id = format!("{disk_id}");
|
||||
let mut script = vec!["select disk {disk_id}", "clean"];
|
||||
match part_type {
|
||||
PartitionTableType::Guid => {
|
||||
|
|
@ -138,7 +139,7 @@ pub fn build_dest_format_script(disk_id: &str, part_type: &PartitionTableType) -
|
|||
script.push("format fs=ntfs quick label=System");
|
||||
}
|
||||
}
|
||||
script.join("\r\n").replace("{disk_id}", disk_id)
|
||||
script.join("\r\n").replace("{disk_id}", &disk_id)
|
||||
}
|
||||
|
||||
#[must_use]
|
||||
|
|
@ -245,7 +246,7 @@ pub fn get_disks() -> Vec<Disk> {
|
|||
disks_map.insert(
|
||||
number,
|
||||
Disk {
|
||||
id: String::from(number),
|
||||
id: number.parse().unwrap(),
|
||||
size: string_to_bytes(size),
|
||||
..Default::default()
|
||||
},
|
||||
|
|
@ -258,8 +259,8 @@ pub fn get_disks() -> Vec<Disk> {
|
|||
for (_, [id, details]) in RE_DIS_DET.captures_iter(section).map(|c| c.extract()) {
|
||||
if let Some(disk) = disks_map.remove(id) {
|
||||
// We remove the disk from the HashMap because we're moving it to the Vec
|
||||
let mut disk = get_disk_details(disk.id.as_str(), disk.size, Some(details));
|
||||
disk.parts = get_partition_details(disk.get_id(), Some(details), None);
|
||||
let mut disk = get_disk_details(disk.id, disk.size, Some(details));
|
||||
disk.parts = get_partition_details(disk.id, Some(details), None);
|
||||
disk.generate_descriptions();
|
||||
disks_raw.push(disk);
|
||||
}
|
||||
|
|
@ -337,8 +338,8 @@ pub fn parse_partition_details(parts: &mut Vec<Partition>, contents: &str) {
|
|||
pub fn refresh_disk_info(disk: &Disk) {
|
||||
// TODO: Needs refactor - assuming add_ functions are replaced with get_ variants
|
||||
info!("Refresh disk info");
|
||||
let mut disk = get_disk_details(disk.get_id(), disk.size, None);
|
||||
disk.parts = get_partition_details(disk.get_id(), None, None);
|
||||
let mut disk = get_disk_details(disk.id, disk.size, None);
|
||||
disk.parts = get_partition_details(disk.id, None, None);
|
||||
disk.generate_descriptions();
|
||||
}
|
||||
|
||||
|
|
|
|||
|
|
@ -22,7 +22,7 @@ mod diskpart {
|
|||
|
||||
#[test]
|
||||
fn get_disk_details_gpt() {
|
||||
let disk = system::diskpart::get_disk_details("", 0, Some(sample_output::DETAIL_DISK_GPT));
|
||||
let disk = system::diskpart::get_disk_details(2, 0, Some(sample_output::DETAIL_DISK_GPT));
|
||||
assert_eq!(disk.model, "Red Hat VirtIO SCSI Disk Device");
|
||||
assert_eq!(disk.part_type, system::disk::PartitionTableType::Guid);
|
||||
assert_eq!(disk.conn_type, "SCSI");
|
||||
|
|
@ -30,7 +30,7 @@ mod diskpart {
|
|||
|
||||
#[test]
|
||||
fn get_disk_details_mbr() {
|
||||
let disk = system::diskpart::get_disk_details("", 0, Some(sample_output::DETAIL_DISK_MBR));
|
||||
let disk = system::diskpart::get_disk_details(2, 0, Some(sample_output::DETAIL_DISK_MBR));
|
||||
assert_eq!(disk.model, "Red Hat VirtIO SCSI Disk Device");
|
||||
assert_eq!(disk.part_type, system::disk::PartitionTableType::Legacy);
|
||||
assert_eq!(disk.conn_type, "SCSI");
|
||||
|
|
@ -40,8 +40,8 @@ mod diskpart {
|
|||
fn get_partition_details() {
|
||||
// Left
|
||||
let partition_1 = system::disk::Partition {
|
||||
id: String::from("1"),
|
||||
fs_type: Some(String::from("FAT32")),
|
||||
id: 1,
|
||||
label: Some(String::from("ESP")),
|
||||
letter: None,
|
||||
part_type: String::from("c12a7328-f81f-11d2-ba4b-00a0c93ec93b"),
|
||||
|
|
@ -49,14 +49,14 @@ mod diskpart {
|
|||
..Default::default()
|
||||
};
|
||||
let partition_2 = system::disk::Partition {
|
||||
id: String::from("2"),
|
||||
id: 2,
|
||||
part_type: String::from("e3c9e316-0b5c-4db8-817d-f92df00215ae"),
|
||||
size: 16777216,
|
||||
..Default::default()
|
||||
};
|
||||
let partition_4 = system::disk::Partition {
|
||||
id: String::from("4"),
|
||||
fs_type: Some(String::from("NTFS")),
|
||||
id: 4,
|
||||
label: Some(String::from("Windows")),
|
||||
letter: Some(String::from("C")),
|
||||
part_type: String::from("ebd0a0a2-b9e5-4433-87c0-68b6b72699c7"),
|
||||
|
|
@ -67,19 +67,19 @@ mod diskpart {
|
|||
// Right
|
||||
let mut disk = system::disk::Disk::default();
|
||||
disk.parts.push(system::disk::Partition {
|
||||
id: String::from("1"),
|
||||
id: 1,
|
||||
..Default::default()
|
||||
});
|
||||
disk.parts.push(system::disk::Partition {
|
||||
id: String::from("2"),
|
||||
id: 2,
|
||||
..Default::default()
|
||||
});
|
||||
disk.parts.push(system::disk::Partition {
|
||||
id: String::from("4"),
|
||||
id: 4,
|
||||
..Default::default()
|
||||
});
|
||||
disk.parts = system::diskpart::get_partition_details(
|
||||
"",
|
||||
2,
|
||||
Some(sample_output::DETAIL_DISK_GPT),
|
||||
Some(sample_output::SELECT_PART_DETAIL_PARTS),
|
||||
);
|
||||
|
|
|
|||
Loading…
Reference in a new issue