sysinfo/unix/linux/
utils.rs

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