use std::path::{Path, PathBuf};
#[derive(Debug, Clone)]
pub struct NetworkDevice {
pub current_name: String,
pub path: PathBuf,
pub predictable_name: String,
}
pub fn get_predictable_names() -> Result<Vec<NetworkDevice>, std::io::Error> {
let mut results = Vec::new();
let dir = Path::new("/sys/class/net").read_dir()?;
for entry in dir {
let entry = match entry {
Err(_) => continue,
Ok(ent) => ent,
};
if entry.file_name().eq_ignore_ascii_case(".") {
continue;
}
if entry.file_name().eq_ignore_ascii_case("..") {
continue;
}
let path = entry.path();
let name = entry.file_name();
let realpath = if let Ok(p) = std::fs::canonicalize(&path) {
p
} else {
continue;
};
if realpath.components().any(|p| p.as_os_str().to_str().unwrap() == "virtual") {
continue;
}
let is_wlan = realpath.join("wireless").is_dir();
let dev_port = realpath.join("dev_port");
let dev_port = if dev_port.is_file() {
if let Ok(c) = std::fs::read_to_string(dev_port) {
if let Ok(port) = c.trim().parse::<u32>() {
Some(port)
} else {
None
}
} else {
None
}
} else {
None
};
let device = if let Some(p) = realpath.parent() {
p
} else {
continue;
};
let device = if let Some(p) = device.parent() {
p
} else {
continue;
};
let device = if device.file_name().unwrap().to_str().unwrap_or("").starts_with("virtio") {
if let Some(p) = device.parent() {
p
} else {
continue;
}
} else {
device
};
let function_index = u32::from_str_radix(
device.file_name().unwrap().to_str().unwrap().split(".").collect::<Vec<_>>()
.get(1).map_or("0", |n| *n),
16,
).unwrap_or(0);
let mut onboard_index = None;
let mut slot_index = None;
let mut pci_port_index = None;
let mut pci_slot_index = None;
'acpi_index: loop {
let acpi_index = device.join("acpi_index");
if acpi_index.is_file() {
let content = if let Ok(c) = std::fs::read_to_string(&acpi_index) {
c
} else {
break 'acpi_index;
};
let index = if let Ok(i) = content.trim().parse::<u32>() {
i
} else {
break 'acpi_index;
};
if index == 0 {
break 'acpi_index;
}
onboard_index = Some(index);
}
break 'acpi_index;
}
'bios_index: loop {
if onboard_index.is_some() {
break 'bios_index;
}
let bios_index = device.join("index");
if bios_index.is_file() {
let content = if let Ok(c) = std::fs::read_to_string(&bios_index) {
c
} else {
break 'bios_index;
};
let index = if let Ok(i) = content.trim().parse::<u32>() {
i
} else {
break 'bios_index;
};
if index == 0 {
break 'bios_index;
}
onboard_index = Some(index);
}
break 'bios_index;
}
'slot_index: loop {
let path = device.join("firmware_node").join("sun");
if !path.is_file() {
break 'slot_index;
}
let index = if let Ok(s) = std::fs::read_to_string(&path) {
s
} else {
break 'slot_index;
};
let index = if let Ok(i) = index.trim().parse::<u32>() {
i
} else {
break 'slot_index;
};
slot_index = Some(index);
break 'slot_index;
}
'pci_index: loop {
let parts = device.file_name().unwrap().to_str().unwrap().split(":").collect::<Vec<_>>();
if parts.len() != 3 {
break 'pci_index;
}
let port = if let Ok(p) = u32::from_str_radix(parts[1], 16) {
p
} else {
break 'pci_index;
};
let slot = if let Ok(s) = u32::from_str_radix(parts[2].split(".").next().unwrap_or("0"), 16) {
s
} else {
break 'pci_index;
};
pci_port_index = Some(port);
pci_slot_index = Some(slot);
break 'pci_index;
}
let addr_path = realpath.join("address");
if !addr_path.is_file() {
continue;
}
let addr = if let Ok(s) = std::fs::read_to_string(&addr_path) {
s
} else {
continue
};
let addr = addr.trim();
let addr = addr.split(":").collect::<Vec<_>>().join("");
let mut allow_function_index = false;
let mut allow_dev_port_index = false;
let mut final_name = String::new();
if is_wlan {
final_name.push_str("wl");
} else {
final_name.push_str("en");
}
if let Some(index) = onboard_index {
final_name.push_str(&format!("o{}", index));
allow_dev_port_index = true;
} else if let Some(index) = slot_index {
final_name.push_str(&format!("s{}", index));
allow_function_index = true;
allow_dev_port_index = true;
} else if let (Some(port_index), Some(slot_index)) = (pci_port_index, pci_slot_index) {
final_name.push_str(&format!("p{}s{}", port_index, slot_index));
allow_function_index = true;
allow_dev_port_index = true;
} else {
final_name.push('x');
final_name.push_str(&addr);
}
if allow_function_index && function_index != 0 {
final_name.push_str(&format!("f{}", function_index));
}
if allow_dev_port_index && dev_port.is_some() {
let dev_port = dev_port.unwrap();
if dev_port != 0 {
final_name.push_str(&format!("d{}", dev_port));
}
}
results.push(NetworkDevice {
current_name: name.to_string_lossy().into_owned(),
path: realpath,
predictable_name: final_name,
});
}
Ok(results)
}