mirror of
https://git.proxmox.com/git/proxmox
synced 2025-05-30 21:51:38 +00:00
sys: fixup error types handling
Signed-off-by: Thomas Lamprecht <t.lamprecht@proxmox.com>
This commit is contained in:
parent
ce389914ff
commit
fbac2f0a0c
@ -106,22 +106,22 @@ pub fn file_get_json<P: AsRef<Path>>(path: P, default: Option<Value>) -> Result<
|
|||||||
.map_err(|err: Error| format_err!("unable to parse json from {:?} - {}", path, err))
|
.map_err(|err: Error| format_err!("unable to parse json from {:?} - {}", path, err))
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/// Read the first line of a file as String in std IO error context
|
||||||
|
pub(crate) fn read_firstline<P: AsRef<Path>>(path: P) -> Result<String, std::io::Error> {
|
||||||
|
let file = std::fs::File::open(path)?;
|
||||||
|
|
||||||
|
let mut reader = BufReader::new(file);
|
||||||
|
let mut line = String::new();
|
||||||
|
|
||||||
|
let _ = reader.read_line(&mut line)?;
|
||||||
|
|
||||||
|
Ok(line)
|
||||||
|
}
|
||||||
|
|
||||||
/// Read the first line of a file as String
|
/// Read the first line of a file as String
|
||||||
pub fn file_read_firstline<P: AsRef<Path>>(path: P) -> Result<String, Error> {
|
pub fn file_read_firstline<P: AsRef<Path>>(path: P) -> Result<String, Error> {
|
||||||
let path = path.as_ref();
|
let path = path.as_ref();
|
||||||
|
read_firstline(path).map_err(|err| format_err!("unable to read {path:?} - {err}"))
|
||||||
try_block!({
|
|
||||||
let file = std::fs::File::open(path)?;
|
|
||||||
|
|
||||||
let mut reader = BufReader::new(file);
|
|
||||||
|
|
||||||
let mut line = String::new();
|
|
||||||
|
|
||||||
let _ = reader.read_line(&mut line)?;
|
|
||||||
|
|
||||||
Ok(line)
|
|
||||||
})
|
|
||||||
.map_err(|err: Error| format_err!("unable to read {:?} - {}", path, err))
|
|
||||||
}
|
}
|
||||||
|
|
||||||
/// Takes a Path and CreateOptions, creates a tmpfile from it and returns
|
/// Takes a Path and CreateOptions, creates a tmpfile from it and returns
|
||||||
|
@ -13,7 +13,7 @@ use lazy_static::lazy_static;
|
|||||||
use nix::unistd::Pid;
|
use nix::unistd::Pid;
|
||||||
use serde::Serialize;
|
use serde::Serialize;
|
||||||
|
|
||||||
use crate::fs::file_read_firstline;
|
use crate::fs::{read_firstline, file_read_firstline};
|
||||||
|
|
||||||
pub mod mountinfo;
|
pub mod mountinfo;
|
||||||
#[doc(inline)]
|
#[doc(inline)]
|
||||||
@ -455,10 +455,10 @@ pub fn read_meminfo() -> Result<ProcFsMemInfo, Error> {
|
|||||||
|
|
||||||
meminfo.swapused = meminfo.swaptotal - meminfo.swapfree;
|
meminfo.swapused = meminfo.swaptotal - meminfo.swapfree;
|
||||||
|
|
||||||
meminfo.memshared = match file_read_firstline("/sys/kernel/mm/ksm/pages_sharing") {
|
meminfo.memshared = match read_firstline("/sys/kernel/mm/ksm/pages_sharing") {
|
||||||
Ok(spages_line) => spages_line.trim_end().parse::<u64>()? * 4096,
|
Ok(spages_line) => spages_line.trim_end().parse::<u64>()? * 4096,
|
||||||
Err(err) if err.kind() == std::io::ErrorKind::NotFound => 0,
|
Err(err) if err.kind() == std::io::ErrorKind::NotFound => 0,
|
||||||
Err(err) => return Err(err),
|
Err(err) => bail!("unable to get KSM pages_sharing - {err}"),
|
||||||
};
|
};
|
||||||
|
|
||||||
Ok(meminfo)
|
Ok(meminfo)
|
||||||
|
Loading…
Reference in New Issue
Block a user