microfetch/crates/lib/src/release.rs
NotAShelf 472dbfc7e7
treewide: going no_std
Signed-off-by: NotAShelf <raf@notashelf.dev>
Change-Id: Ia1c001eb099ea8cae9bdf76642b873376a6a6964
2026-03-27 21:39:41 +03:00

77 lines
2.1 KiB
Rust

use alloc::string::String;
use crate::{Error, UtsName, syscall::read_file_fast};
#[must_use]
#[cfg_attr(feature = "hotpath", hotpath::measure)]
pub fn get_system_info(utsname: &UtsName) -> String {
let sysname = utsname.sysname().to_str().unwrap_or("Unknown");
let release = utsname.release().to_str().unwrap_or("Unknown");
let machine = utsname.machine().to_str().unwrap_or("Unknown");
// Pre-allocate capacity: sysname + " " + release + " (" + machine + ")"
let capacity = sysname.len() + 1 + release.len() + 2 + machine.len() + 1;
let mut result = String::with_capacity(capacity);
// Manual string construction instead of write! macro
result.push_str(sysname);
result.push(' ');
result.push_str(release);
result.push_str(" (");
result.push_str(machine);
result.push(')');
result
}
/// Gets the pretty name of the OS from `/etc/os-release`.
///
/// # Errors
///
/// Returns an error if `/etc/os-release` cannot be read.
#[cfg_attr(feature = "hotpath", hotpath::measure)]
pub fn get_os_pretty_name() -> Result<String, Error> {
// Fast byte-level scanning for PRETTY_NAME=
const PREFIX: &[u8] = b"PRETTY_NAME=";
let mut buffer = [0u8; 1024];
// Use fast syscall-based file reading
let bytes_read = read_file_fast("/etc/os-release", &mut buffer)
.map_err(Error::from_raw_os_error)?;
let content = &buffer[..bytes_read];
let mut offset = 0;
while offset < content.len() {
let remaining = &content[offset..];
// Find newline or end
let line_end = remaining
.iter()
.position(|&b| b == b'\n')
.unwrap_or(remaining.len());
let line = &remaining[..line_end];
if line.starts_with(PREFIX) {
let value = &line[PREFIX.len()..];
// Strip quotes if present
let trimmed = if value.len() >= 2
&& value[0] == b'"'
&& value[value.len() - 1] == b'"'
{
&value[1..value.len() - 1]
} else {
value
};
// Convert to String - should be valid UTF-8
return Ok(String::from_utf8_lossy(trimmed).into_owned());
}
offset += line_end + 1;
}
Ok(String::from("Unknown"))
}