Skip to content

Commit

Permalink
chore: sanitize termination characters from file read, split model in…
Browse files Browse the repository at this point in the history
…to board_model and board_revision
  • Loading branch information
barrenechea committed May 21, 2024
1 parent 51634b2 commit 1ee8f5e
Show file tree
Hide file tree
Showing 2 changed files with 25 additions and 3 deletions.
26 changes: 23 additions & 3 deletions src/api/legacy.rs
Original file line number Diff line number Diff line change
Expand Up @@ -224,13 +224,14 @@ async fn get_about() -> impl Into<LegacyResponse> {
}

let hostname = read_hostname().await.unwrap_or_default();
let model = tokio::fs::read_to_string("/proc/device-tree/model")
let (board_model, board_revision) = read_board_model()
.await
.unwrap_or_default();

json!(
{
"model": model,
"board_model": board_model,
"board_revision": board_revision,
"hostname": hostname,
"api": API_VERSION,
"version": version,
Expand Down Expand Up @@ -327,7 +328,26 @@ async fn read_os_release() -> std::io::Result<HashMap<String, String>> {
}

async fn read_hostname() -> io::Result<String> {
tokio::fs::read_to_string("/proc/sys/kernel/hostname").await
let hostname = tokio::fs::read_to_string("/proc/sys/kernel/hostname")
.await?
.trim_end_matches(|c| c == '\0' || c == '\n')
.to_string();

Ok(hostname)
}

async fn read_board_model() -> io::Result<(String, String)> {
let raw_model = tokio::fs::read_to_string("/proc/device-tree/model")
.await?
.trim_end_matches(|c| c == '\0' || c == '\n')
.to_string();

let (board_model, board_revision) = raw_model
.split_once(" (v")
.map(|(m, r)| (m, r.trim_end_matches(')')))
.unwrap_or_default();

Ok((board_model.to_string(), board_revision.to_string()))
}

/// function is here for backwards compliance. Data is mostly a duplication of [`get_about`]
Expand Down
2 changes: 2 additions & 0 deletions src/app/bmc_info.rs
Original file line number Diff line number Diff line change
Expand Up @@ -63,6 +63,8 @@ pub async fn get_mac_address(interface: &str) -> String {
tokio::fs::read_to_string(format!("/sys/class/net/{}/address", interface))
.await
.unwrap_or("Unknown".to_owned())
.trim_end_matches(|c| c == '\0' || c == '\n')
.to_string()
}

pub fn get_fs_stat(device: &str) -> anyhow::Result<(u64, u64)> {
Expand Down

0 comments on commit 1ee8f5e

Please sign in to comment.