sysinfo/unix/linux/
utils.rs1use 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
33pub(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 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
71pub(crate) struct PathHandler(PathBuf);
74
75impl PathHandler {
76 pub(crate) fn new(path: &Path) -> Self {
77 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
95impl 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
113pub(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}