-
Notifications
You must be signed in to change notification settings - Fork 77
Expand file tree
/
Copy pathlibc_version.rs
More file actions
105 lines (88 loc) · 2.82 KB
/
Copy pathlibc_version.rs
File metadata and controls
105 lines (88 loc) · 2.82 KB
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
97
98
99
100
101
102
103
104
105
//! Libc version operations
use crate::cpu_arch;
use crate::cpu_arch::CpuArch;
use std::fmt;
use std::path::Path;
use std::str;
use ex::fs;
use ex::io;
use snafu::OptionExt;
use snafu::ResultExt;
use snafu::Snafu;
use twoway::find_bytes;
#[derive(Copy, Clone)]
pub enum Distro {
Ubuntu,
Debian,
}
/// Libc version information
pub struct LibcVersion {
/// Long string representation of a libc version
///
/// Example: `"2.23-0ubuntu10"`
pub string: String,
/// Short string representation of a libc version
///
/// Example: `"2.23"`
pub string_short: String,
/// Architecture of libc
pub arch: CpuArch,
/// Distribution of libc
pub distro: Distro,
}
impl fmt::Display for LibcVersion {
/// Write libc version in format used by Ubuntu repositories
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
write!(f, "{}_{}", self.string, self.arch)
}
}
#[derive(Debug, Snafu)]
#[allow(clippy::enum_variant_names)]
pub enum Error {
#[snafu(display("failed reading file: {}", source))]
ReadError { source: io::Error },
#[snafu(display("failed finding version string"))]
NotFoundError,
#[snafu(display("invalid architecture: {}", source))]
ArchError { source: cpu_arch::Error },
#[snafu(display("invalid UTF-8 in version string: {}", source))]
Utf8Error { source: str::Utf8Error },
}
pub type Result<T> = std::result::Result<T, Error>;
const IDENTIFIER: [(&[u8], Distro); 3] = [
(b"GNU C Library (Ubuntu GLIBC ", Distro::Ubuntu),
(b"GNU C Library (Ubuntu EGLIBC ", Distro::Ubuntu),
(b"GNU C Library (Debian GLIBC ", Distro::Debian),
];
impl LibcVersion {
/// Detect the version of a libc
pub fn detect(libc: &Path) -> Result<Self> {
let bytes = fs::read(libc).context(ReadSnafu)?;
let (string, distro) = Self::version_str_distro_from_bytes(&bytes)?;
let string_short = string.split('-').next().context(NotFoundSnafu)?.to_string();
Ok(Self {
string,
string_short,
arch: CpuArch::from_elf_bytes(libc, &bytes).context(ArchSnafu)?,
distro,
})
}
/// Extract the long version string from the bytes of a libc
fn version_str_distro_from_bytes(libc: &[u8]) -> Result<(String, Distro)> {
let (pos, distro) = IDENTIFIER
.iter()
.find_map(|(cut, distro)| {
let pos = find_bytes(libc, cut);
Some((pos? + cut.len(), distro))
})
.context(NotFoundSnafu)?;
let ver_str = &libc[pos..];
let pos = ver_str
.iter()
.position(|&c| c == b')')
.context(NotFoundSnafu)?;
let ver_str = &ver_str[..pos];
let ver_str = std::str::from_utf8(ver_str).context(Utf8Snafu)?.to_string();
Ok((ver_str, *distro))
}
}