1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
#[cfg(test)]
use super::mocks::UPTIME;
use super::{run, SysPath};
use crate::{Error, Result};
use std::process::Command;
#[derive(Debug, Default, Eq, PartialEq)]
pub struct MountPoint {
volume: String,
path: String,
voltype: String,
options: String,
}
impl MountPoint {
pub fn new(volume: &str, path: &str, voltype: &str, options: &str) -> MountPoint {
MountPoint {
volume: volume.to_string(),
path: path.to_string(),
voltype: voltype.to_string(),
options: options.to_string(),
}
}
pub(crate) fn from_line(line: &str) -> Option<MountPoint> {
let mut elems = line.split_ascii_whitespace().take(4);
if elems.clone().count() >= 4 {
let volume = elems.next().unwrap();
let path = elems.next().unwrap();
let voltype = elems.next().unwrap();
let options = elems.next().unwrap();
return Some(Self::new(volume, path, voltype, options));
}
None
}
}
pub type MountPoints = Vec<MountPoint>;
pub fn hostname() -> Result<String> {
Ok(SysPath::ProcHostname.read()?.trim().to_string())
}
fn _uptime(out: &str) -> Result<u64> {
Ok(out
.split_ascii_whitespace()
.take(1)
.collect::<String>()
.parse::<f64>()
.map_err(|e| Error::CommandParseError(e.to_string()))? as u64)
}
pub fn uptime() -> Result<u64> {
_uptime(&SysPath::ProcUptime.read()?)
}
pub fn arch() -> Result<String> {
run(Command::new("uname").arg("-m"))
}
pub fn domainname() -> Result<String> {
Ok(SysPath::ProcDomainName.read()?.trim().to_string())
}
pub fn kernel_version() -> Result<String> {
SysPath::ProcKernelRelease.read()
}
pub fn mounts() -> Result<MountPoints> {
let mut mps = Vec::new();
for line in SysPath::ProcMounts.read()?.split('\n') {
if let Some(mp) = MountPoint::from_line(line) {
mps.push(mp);
}
}
Ok(mps)
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn gets_uptime() {
assert_eq!(_uptime(UPTIME), Ok(5771))
}
}