sysinfo/unix/linux/
utils.rs1#[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#[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 Self(path.join("a"))
56 }
57
58 pub(crate) fn as_path(&self) -> &Path {
59 &self.0
60 }
61}
62
63#[cfg(feature = "system")]
64pub(crate) trait PathPush {
65 fn replace_and_join(&mut self, p: &str) -> &Path;
66}
67
68#[cfg(feature = "system")]
69impl PathPush for PathHandler {
70 fn replace_and_join(&mut self, p: &str) -> &Path {
71 self.0.pop();
72 self.0.push(p);
73 self.as_path()
74 }
75}
76
77#[cfg(feature = "system")]
79impl PathPush for std::path::PathBuf {
80 fn replace_and_join(&mut self, p: &str) -> &Path {
81 self.push(p);
82 self.as_path()
83 }
84}
85
86#[cfg(feature = "system")]
87pub(crate) fn to_u64(v: &[u8]) -> u64 {
88 let mut x = 0;
89
90 for c in v {
91 x *= 10;
92 x += u64::from(c - b'0');
93 }
94 x
95}
96
97#[cfg(feature = "disk")]
99pub(crate) fn to_cpath(path: &std::path::Path) -> Vec<u8> {
100 use std::{ffi::OsStr, os::unix::ffi::OsStrExt};
101
102 let path_os: &OsStr = path.as_ref();
103 let mut cpath = path_os.as_bytes().to_vec();
104 cpath.push(0);
105 cpath
106}