alpm_mtree/schema.rs
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 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196
//! Schemas for ALPM-MTREE data.
use std::{
fmt::{Display, Formatter},
fs::File,
io::{BufReader, Read},
path::{Path, PathBuf},
str::FromStr,
};
use alpm_common::FileFormatSchema;
use alpm_types::{SchemaVersion, semver_version::Version};
use crate::{Error, mtree_buffer_to_string};
/// An enum tracking all available [ALPM-MTREE] schemas.
///
/// The schema of a ALPM-MTREE refers to its available fields in a specific version.
///
/// [ALPM-MTREE]: https://alpm.archlinux.page/specifications/ALPM-MTREE.5.html
#[derive(Clone, Debug, Eq, PartialEq)]
pub enum MtreeSchema {
V1(SchemaVersion),
V2(SchemaVersion),
}
impl FileFormatSchema for MtreeSchema {
type Err = Error;
/// Returns a reference to the inner [`SchemaVersion`].
fn inner(&self) -> &SchemaVersion {
match self {
MtreeSchema::V1(v) | MtreeSchema::V2(v) => v,
}
}
/// Derives an [`MtreeSchema`] from an ALPM-MTREE file.
///
/// Opens the `file` and defers to [`MtreeSchema::derive_from_reader`].
///
/// # Errors
///
/// Returns an error if
/// - opening `file` for reading fails
/// - or deriving a [`MtreeSchema`] from the contents of `file` fails.
fn derive_from_file(file: impl AsRef<Path>) -> Result<Self, Error>
where
Self: Sized,
{
let file = file.as_ref();
Self::derive_from_reader(File::open(file).map_err(|source| {
Error::IoPath(
PathBuf::from(file),
"deriving schema version from ALPM-MTREE file",
source,
)
})?)
}
/// Derives an [`MtreeSchema`] from ALPM-MTREE data in a `reader`.
///
/// Reads the `reader` to string and defers to [`MtreeSchema::derive_from_str`].
///
/// # Errors
///
/// Returns an error if
/// - reading a [`String`] from `reader` fails
/// - or deriving a [`MtreeSchema`] from the contents of `reader` fails.
fn derive_from_reader(reader: impl std::io::Read) -> Result<Self, Error>
where
Self: Sized,
{
let mut buffer = Vec::new();
let mut buf_reader = BufReader::new(reader);
buf_reader
.read_to_end(&mut buffer)
.map_err(|source| Error::Io("reading ALPM-MTREE data", source))?;
Self::derive_from_str(&mtree_buffer_to_string(buffer)?)
}
/// Derives an [`MtreeSchema`] from a string slice containing ALPM-MTREE data.
///
/// Since the ALPM-MTREE format does not carry any version information, this function checks
/// whether `s` contains `md5=` or `md5digest=`.
/// If it does, the input is considered to be [ALPM-MTREEv2].
/// If the strings are not found, [ALPM-MTREEv1] is assumed.
///
/// # Examples
///
/// ```
/// use alpm_common::FileFormatSchema;
/// use alpm_mtree::MtreeSchema;
/// use alpm_types::{SchemaVersion, semver_version::Version};
///
/// # fn main() -> Result<(), alpm_mtree::Error> {
/// let mtree_v2 = r#"
/// #mtree
/// /set mode=644 uid=0 gid=0 type=file
/// ./some_file time=1700000000.0 size=1337 sha256digest=0123456789abcdef0123456789abcdef0123456789abcdef0123456789abcdef
/// ./some_link type=link link=some_file time=1700000000.0
/// ./some_dir type=dir time=1700000000.0
/// "#;
/// assert_eq!(
/// MtreeSchema::V2(SchemaVersion::new(Version::new(2, 0, 0))),
/// MtreeSchema::derive_from_str(mtree_v2)?
/// );
///
/// let mtree_v1 = r#"
/// #mtree
/// /set mode=644 uid=0 gid=0 type=file
/// ./some_file time=1700000000.0 size=1337 sha256digest=0123456789abcdef0123456789abcdef0123456789abcdef0123456789abcdef md5digest=d3b07384d113edec49eaa6238ad5ff00
/// ./some_link type=link link=some_file time=1700000000.0
/// ./some_dir type=dir time=1700000000.0
/// "#;
/// assert_eq!(
/// MtreeSchema::V1(SchemaVersion::new(Version::new(1, 0, 0))),
/// MtreeSchema::derive_from_str(mtree_v1)?
/// );
/// # Ok(())
/// # }
/// ```
///
/// # Errors
///
/// Returns an error if
/// - the first `xdata` keyword is assigned an empty string,
/// - or the first `xdata` keyword does not assign "pkgtype".
///
/// [ALPM-MTREEv1]: https://alpm.archlinux.page/specifications/ALPM-MTREEv1.5.html
/// [ALPM-MTREEv2]: https://alpm.archlinux.page/specifications/ALPM-MTREEv2.5.html
fn derive_from_str(s: &str) -> Result<MtreeSchema, Error> {
Ok(if s.contains("md5digest=") || s.contains("md5=") {
MtreeSchema::V1(SchemaVersion::new(Version::new(1, 0, 0)))
} else {
MtreeSchema::V2(SchemaVersion::new(Version::new(2, 0, 0)))
})
}
}
impl Default for MtreeSchema {
/// Returns the default [`MtreeSchema`] variant ([`MtreeSchema::V2`]).
fn default() -> Self {
Self::V2(SchemaVersion::new(Version::new(2, 0, 0)))
}
}
impl FromStr for MtreeSchema {
type Err = Error;
/// Creates an [`MtreeSchema`] from string slice `s`.
///
/// Relies on [`SchemaVersion::from_str`] to create a corresponding [`MtreeSchema`] from
/// `s`.
///
/// # Errors
///
/// Returns an error if
/// - no [`SchemaVersion`] can be created from `s`,
/// - or the conversion from [`SchemaVersion`] to [`MtreeSchema`] fails.
fn from_str(s: &str) -> Result<MtreeSchema, Self::Err> {
match SchemaVersion::from_str(s) {
Ok(version) => Self::try_from(version),
Err(_) => Err(Error::UnsupportedSchemaVersion(s.to_string())),
}
}
}
impl TryFrom<SchemaVersion> for MtreeSchema {
type Error = Error;
/// Converts a [`SchemaVersion`] to an [`MtreeSchema`].
///
/// # Errors
///
/// Returns an error if the [`SchemaVersion`]'s inner [`Version`] does not provide a major
/// version that corresponds to an [`MtreeSchema`] variant.
fn try_from(value: SchemaVersion) -> Result<Self, Self::Error> {
match value.inner().major {
1 => Ok(MtreeSchema::V1(value)),
2 => Ok(MtreeSchema::V2(value)),
_ => Err(Error::UnsupportedSchemaVersion(value.to_string())),
}
}
}
impl Display for MtreeSchema {
fn fmt(&self, fmt: &mut Formatter) -> std::fmt::Result {
write!(
fmt,
"{}",
match self {
MtreeSchema::V1(version) | MtreeSchema::V2(version) => version.inner().major,
}
)
}
}