#[cfg(target_os = "android")]
use crate::sys::ffi;
use crate::sys::utils::get_all_utf8_data;
#[cfg(target_os = "android")]
use std::ffi::CStr;
#[cfg(not(target_os = "android"))]
use std::fs::File;
#[cfg(not(target_os = "android"))]
use std::io::Read;
#[cfg(not(target_os = "android"))]
use std::path::Path;
use std::str::FromStr;
pub(super) fn read_table<F>(filename: &str, colsep: char, mut f: F)
where
F: FnMut(&str, u64),
{
if let Ok(content) = get_all_utf8_data(filename, 16_635) {
content
.split('\n')
.flat_map(|line| {
let mut split = line.split(colsep);
let key = split.next()?;
let value = split.next()?;
let value0 = value.trim_start().split(' ').next()?;
let value0_u64 = u64::from_str(value0).ok()?;
Some((key, value0_u64))
})
.for_each(|(k, v)| f(k, v));
}
}
#[derive(PartialEq, Eq)]
pub(super) enum InfoType {
Name,
OsVersion,
DistributionID,
DistributionIDLike,
}
#[cfg(not(target_os = "android"))]
pub(super) fn get_system_info_linux(
info: InfoType,
path: &Path,
fallback_path: &Path,
) -> Option<String> {
if let Ok(buf) = File::open(path).and_then(|mut f| {
let mut buf = String::new();
f.read_to_string(&mut buf)?;
Ok(buf)
}) {
let info_str = match info {
InfoType::Name => "NAME=",
InfoType::OsVersion => "VERSION_ID=",
InfoType::DistributionID => "ID=",
InfoType::DistributionIDLike => "ID_LIKE=",
};
for line in buf.lines() {
if let Some(stripped) = line.strip_prefix(info_str) {
return Some(stripped.replace('"', ""));
}
}
}
let buf = File::open(fallback_path)
.and_then(|mut f| {
let mut buf = String::new();
f.read_to_string(&mut buf)?;
Ok(buf)
})
.ok()?;
let info_str = match info {
InfoType::OsVersion => "DISTRIB_RELEASE=",
InfoType::Name => "DISTRIB_ID=",
InfoType::DistributionID => {
return None;
}
InfoType::DistributionIDLike => {
return None;
}
};
for line in buf.lines() {
if let Some(stripped) = line.strip_prefix(info_str) {
return Some(stripped.replace('"', ""));
}
}
None
}
pub(super) fn system_info_as_list(sinf: Option<String>) -> Vec<String> {
match sinf {
Some(value) => value.split_ascii_whitespace().map(String::from).collect(),
None => Vec::new(),
}
}
#[cfg(target_os = "android")]
pub(super) fn get_system_info_android(info: InfoType) -> Option<String> {
let name: &'static [u8] = match info {
InfoType::Name => b"ro.product.model\0",
InfoType::OsVersion => b"ro.build.version.release\0",
InfoType::DistributionID => {
return None;
}
InfoType::DistributionIDLike => {
return None;
}
};
let name = CStr::from_bytes_with_nul(name).ok()?;
ffi::system_property(name)
}