find_binary_version/lib.rs
1// Copyright (C) 2019-2021 O.S. Systems Software LTDA
2//
3// SPDX-License-Identifier: MIT OR Apache-2.0
4
5#![deny(
6 missing_copy_implementations,
7 missing_debug_implementations,
8 missing_docs,
9 trivial_casts,
10 trivial_numeric_casts,
11 unsafe_code,
12 unstable_features,
13 unused_import_braces,
14 unused_qualifications,
15 warnings
16)]
17
18//! The library provide a way for reading version from the binaries files.
19//!
20//! ---
21//!
22//! ## Dependencies
23//!
24//! You must have `libarchive` properly installed on your system in order to use
25//! this. If building on *nix systems, `pkg-config` is used to locate the
26//! `libarchive`; on Windows `vcpkg` will be used to locating the `libarchive`.
27//!
28//! The minimum supported Rust version is 1.59.
29//!
30//! ## Features
31//!
32//! The following know patterns are supported allowing the version to be detected
33//! without the need for any user specification:
34//!
35//! * U-Boot
36//! * LinuxKernel
37//!
38//! Other formats are supported through the `version_with_pattern` function,
39//! which will look for a given regular expression on the given binary.
40
41mod custom;
42mod linuxkernel;
43mod strings;
44mod uboot;
45
46use crate::{custom::Custom, linuxkernel::LinuxKernel, uboot::UBoot};
47use tokio::io::{AsyncRead, AsyncSeek};
48
49#[derive(Debug, Copy, Clone)]
50/// Define the binary kind to use for matching.
51pub enum BinaryKind {
52 /// U-Boot binary kind.
53 UBoot,
54 /// Linux Kernel binary kind.
55 LinuxKernel,
56}
57
58#[async_trait::async_trait(?Send)]
59trait VersionFinder {
60 async fn get_version(&mut self) -> Option<String>;
61}
62
63/// Get the version for a specific binary.
64pub async fn version<R: AsyncRead + AsyncSeek + Unpin>(
65 mut buffer: &mut R,
66 kind: BinaryKind,
67) -> Option<String> {
68 match kind {
69 BinaryKind::LinuxKernel => LinuxKernel::from_reader(&mut buffer).get_version().await,
70 BinaryKind::UBoot => UBoot::from_reader(&mut buffer).get_version().await,
71 }
72}
73
74/// Get the version for a specific pattern.
75pub async fn version_with_pattern<R: AsyncRead + Unpin>(
76 mut buffer: &mut R,
77 pattern: &str,
78) -> Option<String> {
79 Custom::from_reader(&mut buffer, pattern)
80 .get_version()
81 .await
82}