1use crate::access::sysfs::Sysfs;
2use crate::bdf::BusDeviceFunction;
3use crate::error::Result;
4use std::fs::read_link;
5
6#[derive(Debug)]
7pub struct Kernel;
8
9impl Kernel {
10 pub fn text(&self, bdf: &BusDeviceFunction, verbosity: u8) -> Result<String> {
11 Ok(format!(
12 "{}\n{}",
13 self.driver_text(bdf, verbosity)?,
14 self.module_text(bdf, verbosity)?
15 ))
16 }
17
18 pub fn driver_text(&self, bdf: &BusDeviceFunction, _verbosity: u8) -> Result<String> {
19 let driver_symlink = Sysfs::get_function_sub_path(bdf, "driver");
20 let driver_path = read_link(driver_symlink);
21
22 if driver_path.is_err() {
23 return Ok(String::new());
24 }
25
26 let driver_path = driver_path?;
27 let driver_path = driver_path.to_str().unwrap_or_default();
28
29 Ok(format!(
30 "\tKernel driver in use: {}",
31 driver_path.split('/').last().unwrap_or_default()
32 ))
33 }
34
35 pub fn module_text(&self, bdf: &BusDeviceFunction, _verbosity: u8) -> Result<String> {
36 let module_symlink = Sysfs::get_function_sub_path(bdf, "driver/module");
37 let module_path = read_link(module_symlink);
38
39 if module_path.is_err() {
40 return Ok(String::new());
41 }
42
43 let module_path = module_path?;
44 let module_path = module_path.to_str().unwrap_or_default();
45
46 Ok(format!(
47 "\tKernel modules: {}",
48 module_path.split('/').last().unwrap_or_default()
49 ))
50 }
51}