microfetch_lib/
release.rs1use std::{fmt::Write as _, io};
2
3use crate::{UtsName, syscall::read_file_fast};
4
5#[must_use]
6#[cfg_attr(feature = "hotpath", hotpath::measure)]
7pub fn get_system_info(utsname: &UtsName) -> String {
8 let sysname = utsname.sysname().to_str().unwrap_or("Unknown");
9 let release = utsname.release().to_str().unwrap_or("Unknown");
10 let machine = utsname.machine().to_str().unwrap_or("Unknown");
11
12 let capacity = sysname.len() + 1 + release.len() + 2 + machine.len() + 1;
14 let mut result = String::with_capacity(capacity);
15
16 write!(result, "{sysname} {release} ({machine})").unwrap();
17 result
18}
19
20#[cfg_attr(feature = "hotpath", hotpath::measure)]
26pub fn get_os_pretty_name() -> Result<String, io::Error> {
27 const PREFIX: &[u8] = b"PRETTY_NAME=";
29
30 let mut buffer = [0u8; 1024];
31
32 let bytes_read = read_file_fast("/etc/os-release", &mut buffer)?;
34 let content = &buffer[..bytes_read];
35
36 let mut offset = 0;
37
38 while offset < content.len() {
39 let remaining = &content[offset..];
40
41 let line_end = remaining
43 .iter()
44 .position(|&b| b == b'\n')
45 .unwrap_or(remaining.len());
46 let line = &remaining[..line_end];
47
48 if line.starts_with(PREFIX) {
49 let value = &line[PREFIX.len()..];
50
51 let trimmed = if value.len() >= 2
53 && value[0] == b'"'
54 && value[value.len() - 1] == b'"'
55 {
56 &value[1..value.len() - 1]
57 } else {
58 value
59 };
60
61 return Ok(String::from_utf8_lossy(trimmed).into_owned());
63 }
64
65 offset += line_end + 1;
66 }
67
68 Ok("Unknown".to_owned())
69}