Skip to content

Commit

Permalink
添加ROOTPATH
Browse files Browse the repository at this point in the history
  • Loading branch information
xml committed Apr 15, 2024
1 parent dd3730b commit e8991c8
Show file tree
Hide file tree
Showing 3 changed files with 23 additions and 17 deletions.
16 changes: 9 additions & 7 deletions src/battery.rs
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
static ROOTPATH: &str = "/sys/class/power_supply";

#[derive(Clone, Debug)]
pub struct Battery {
pub name: String,
Expand Down Expand Up @@ -26,7 +28,7 @@ impl Battery {
}

pub fn capacity(&self) -> u32 {
if let Ok(raw_val) = crate::utils::read_line(&format!("/sys/class/power_supply/{}/capacity", self.name)) {
if let Ok(raw_val) = crate::utils::read_line(&format!("{}/{}/capacity", ROOTPATH, self.name)) {
if let Ok(val) = raw_val.parse::<u32>() {
return val
} else {
Expand All @@ -37,7 +39,7 @@ impl Battery {
}

pub fn voltage_now(&self) -> u32 {
if let Ok(raw_val) = crate::utils::read_line(&format!("/sys/class/power_supply/{}/voltage_now", self.name)) {
if let Ok(raw_val) = crate::utils::read_line(&format!("{}/{}/voltage_now", ROOTPATH, self.name)) {
if let Ok(val) = raw_val.parse::<u32>() {
return val
} else {
Expand All @@ -48,7 +50,7 @@ impl Battery {
}

pub fn current_now(&self) -> u32 {
if let Ok(raw_val) = crate::utils::read_line(&format!("/sys/class/power_supply/{}/current_now", self.name)) {
if let Ok(raw_val) = crate::utils::read_line(&format!("{}/{}/current_now", ROOTPATH, self.name)) {
if let Ok(val) = raw_val.parse::<u32>() {
return val
} else {
Expand All @@ -59,7 +61,7 @@ impl Battery {
}

pub fn power_now(&self) -> u32 {
if let Ok(raw_val) = crate::utils::read_line(&format!("/sys/class/power_supply/{}/power_now", self.name)) {
if let Ok(raw_val) = crate::utils::read_line(&format!("{}/{}/power_now", ROOTPATH, self.name)) {
if let Ok(val) = raw_val.parse::<u32>() {
return val
} else {
Expand All @@ -72,7 +74,7 @@ impl Battery {
}

pub fn status(&self) -> String {
if let Ok(raw_val) = crate::utils::read_line(&format!("/sys/class/power_supply/{}/status", self.name)) {
if let Ok(raw_val) = crate::utils::read_line(&format!("{}/{}/status", ROOTPATH, self.name)) {
return raw_val;
}
return "".to_string();
Expand All @@ -83,7 +85,7 @@ pub fn enumerate() -> Vec<Battery> {
fn is_battery(entry: &walkdir::DirEntry) -> bool {
let realpath = if entry.file_type().is_symlink() {
let paths = std::fs::read_link(entry.path()).unwrap();
std::path::Path::new("/sys/class/power_supply").join(paths).canonicalize().unwrap()
std::path::Path::new(ROOTPATH).join(paths).canonicalize().unwrap()
} else if entry.file_type().is_dir() {
entry.path().to_path_buf()
} else {
Expand All @@ -102,7 +104,7 @@ pub fn enumerate() -> Vec<Battery> {
}

let mut batterys = Vec::new();
for entry in walkdir::WalkDir::new("/sys/class/power_supply")
for entry in walkdir::WalkDir::new(ROOTPATH)
.sort_by_file_name()
.max_depth(1)
.into_iter()
Expand Down
16 changes: 9 additions & 7 deletions src/cpu.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@
use std::fs::File;
use std::io::BufRead;

static ROOTPATH: &str = "/sys/devices/system/cpu";

#[derive(Clone, Debug)]
pub struct CPU {
number: u32,
Expand Down Expand Up @@ -33,7 +35,7 @@ impl CPU {
}

pub fn freq(&self) -> u32 {
if let Ok(freq_val) = crate::utils::read_line(&format!("/sys/devices/system/cpu/cpu{}/cpufreq/scaling_cur_freq", self.number)) {
if let Ok(freq_val) = crate::utils::read_line(&format!("{}/cpu{}/cpufreq/scaling_cur_freq", ROOTPATH, self.number)) {
if let Ok(val) = freq_val.parse::<u32>() {
return val
} else {
Expand All @@ -45,19 +47,19 @@ impl CPU {
}

pub fn new_cpu(number: u32, vendor: &str, family: &str, model: &str) -> std::io::Result<CPU> {
let core_str = crate::utils::read_line(&format!("/sys/devices/system/cpu/cpu{}/topology/core_id", number))?;
let core_str = crate::utils::read_line(&format!("{}/cpu{}/topology/core_id", ROOTPATH, number))?;
let core_id = core_str.parse::<u32>().map_err(|e| {std::io::Error::new(std::io::ErrorKind::InvalidData, e.to_string())})?;

let physical_package = crate::utils::read_line(&format!("/sys/devices/system/cpu/cpu{}/topology/physical_package_id", number))?;
let physical_package = crate::utils::read_line(&format!("{}/cpu{}/topology/physical_package_id", ROOTPATH, number))?;
let physical_package_id = physical_package.parse::<u32>().map_err(|e| {std::io::Error::new(std::io::ErrorKind::InvalidData, e.to_string())})?;

let scaling_driver = crate::utils::read_line(&format!("/sys/devices/system/cpu/cpu{}/cpufreq/scaling_driver", number))?;
let scaling_governor = crate::utils::read_line(&format!("/sys/devices/system/cpu/cpu{}/cpufreq/scaling_governor", number))?;
let scaling_driver = crate::utils::read_line(&format!("{}/cpu{}/cpufreq/scaling_driver", ROOTPATH, number))?;
let scaling_governor = crate::utils::read_line(&format!("{}/cpu{}/cpufreq/scaling_governor", ROOTPATH, number))?;

let scaling_min_freq_str = crate::utils::read_line(&format!("/sys/devices/system/cpu/cpu{}/cpufreq/scaling_min_freq", number))?;
let scaling_min_freq_str = crate::utils::read_line(&format!("{}/cpu{}/cpufreq/scaling_min_freq", ROOTPATH, number))?;
let scaling_min_freq = scaling_min_freq_str.parse::<u32>().map_err(|e| {std::io::Error::new(std::io::ErrorKind::InvalidData, e.to_string())})?;

let scaling_max_freq_str = crate::utils::read_line(&format!("/sys/devices/system/cpu/cpu{}/cpufreq/scaling_max_freq", number))?;
let scaling_max_freq_str = crate::utils::read_line(&format!("{}/cpu{}/cpufreq/scaling_max_freq", ROOTPATH, number))?;
let scaling_max_freq = scaling_max_freq_str.parse::<u32>().map_err(|e| {std::io::Error::new(std::io::ErrorKind::InvalidData, e.to_string())})?;

Ok(CPU{
Expand Down
8 changes: 5 additions & 3 deletions src/thermal.rs
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
static ROOTPATH: &str = "/sys/class/thermal";

#[derive(Clone, Debug)]
pub struct Thermal {
pub name: String,
Expand Down Expand Up @@ -29,7 +31,7 @@ impl Thermal {
})
}
pub fn temp(&self) -> u32 {
if let Ok(raw_val) = crate::utils::read_line(&format!("/sys/class/thermal/{}/temp", self.name)) {
if let Ok(raw_val) = crate::utils::read_line(&format!("{}/{}/temp", ROOTPATH, self.name)) {
if let Ok(val) = raw_val.parse::<u32>() {
return val
} else {
Expand All @@ -44,7 +46,7 @@ pub fn enumerate() -> Vec<Thermal> {
fn is_thermal(entry: &walkdir::DirEntry) -> bool {
let realpath = if entry.file_type().is_symlink() {
let paths = std::fs::read_link(entry.path()).unwrap();
std::path::Path::new("/sys/class/thermal/").join(paths).canonicalize().unwrap()
std::path::Path::new(ROOTPATH).join(paths).canonicalize().unwrap()
} else if entry.file_type().is_dir() {
entry.path().to_path_buf()
} else {
Expand All @@ -56,7 +58,7 @@ pub fn enumerate() -> Vec<Thermal> {
}

let mut thermals = Vec::new();
for entry in walkdir::WalkDir::new("/sys/class/thermal")
for entry in walkdir::WalkDir::new(ROOTPATH)
.sort_by_file_name()
.max_depth(1)
.into_iter()
Expand Down

0 comments on commit e8991c8

Please sign in to comment.