sysinfo/unix/linux/
utils.rs

1// Take a look at the license at the top of the repository in the LICENSE file.
2
3#[cfg(any(feature = "disk", feature = "system"))]
4use std::fs::File;
5#[cfg(any(feature = "disk", feature = "system"))]
6use std::io::{self, Read, Seek};
7#[cfg(any(feature = "disk", feature = "system"))]
8use std::path::Path;
9
10#[cfg(feature = "system")]
11pub(crate) fn get_all_data_from_file(file: &mut File, size: usize) -> io::Result<Vec<u8>> {
12    let mut buf = Vec::with_capacity(size);
13    file.rewind()?;
14    file.read_to_end(&mut buf)?;
15    Ok(buf)
16}
17
18#[cfg(any(feature = "disk", feature = "system"))]
19pub(crate) fn get_all_utf8_data_from_file(file: &mut File, size: usize) -> io::Result<String> {
20    let mut buf = String::with_capacity(size);
21    file.rewind()?;
22    file.read_to_string(&mut buf)?;
23    Ok(buf)
24}
25
26#[cfg(any(feature = "disk", feature = "system"))]
27pub(crate) fn get_all_utf8_data<P: AsRef<Path>>(file_path: P, size: usize) -> io::Result<String> {
28    let mut file = File::open(file_path.as_ref())?;
29    get_all_utf8_data_from_file(&mut file, size)
30}
31
32#[cfg(feature = "system")]
33#[allow(clippy::useless_conversion)]
34pub(crate) fn realpath(path: &Path) -> Option<std::path::PathBuf> {
35    match std::fs::read_link(path) {
36        Ok(path) => Some(path),
37        Err(_e) => {
38            sysinfo_debug!("failed to get real path for {:?}: {:?}", path, _e);
39            None
40        }
41    }
42}
43
44/// This type is used in `retrieve_all_new_process_info` because we have a "parent" path and
45/// from it, we `pop`/`join` every time because it's more memory efficient than using `Path::join`.
46#[cfg(feature = "system")]
47pub(crate) struct PathHandler(std::path::PathBuf);
48
49#[cfg(feature = "system")]
50impl PathHandler {
51    pub(crate) fn new(path: &Path) -> Self {
52        // `path` is the "parent" for all paths which will follow so we add a fake element at
53        // the end since every `PathHandler::join` call will first call `pop` internally.
54        Self(path.join("a"))
55    }
56
57    pub(crate) fn as_path(&self) -> &Path {
58        &self.0
59    }
60}
61
62#[cfg(feature = "system")]
63pub(crate) trait PathPush {
64    fn join(&mut self, p: &str) -> &Path;
65}
66
67#[cfg(feature = "system")]
68impl PathPush for PathHandler {
69    fn join(&mut self, p: &str) -> &Path {
70        self.0.pop();
71        self.0.push(p);
72        self.as_path()
73    }
74}
75
76// This implementation allows to skip one allocation that is done in `PathHandler`.
77#[cfg(feature = "system")]
78impl PathPush for std::path::PathBuf {
79    fn join(&mut self, p: &str) -> &Path {
80        self.push(p);
81        self.as_path()
82    }
83}
84
85#[cfg(feature = "system")]
86pub(crate) fn to_u64(v: &[u8]) -> u64 {
87    let mut x = 0;
88
89    for c in v {
90        x *= 10;
91        x += u64::from(c - b'0');
92    }
93    x
94}
95
96/// Converts a path to a NUL-terminated `Vec<u8>` suitable for use with C functions.
97#[cfg(feature = "disk")]
98pub(crate) fn to_cpath(path: &std::path::Path) -> Vec<u8> {
99    use std::{ffi::OsStr, os::unix::ffi::OsStrExt};
100
101    let path_os: &OsStr = path.as_ref();
102    let mut cpath = path_os.as_bytes().to_vec();
103    cpath.push(0);
104    cpath
105}