#![warn(rust_2018_idioms)]
#![deny(missing_docs)]
#[cfg(any(
not(any(feature = "runtime", feature = "compile-time")),
all(feature = "runtime", feature = "compile-time")
))]
compile_error!("Exactly one of the features `runtime` or `compile-time` is required.");
#[cfg(any(feature = "runtime", feature = "compile-time"))]
mod ffi_types;
#[cfg(feature = "compile-time")]
mod ffi;
#[allow(clippy::missing_safety_doc)]
#[cfg(feature = "compile-time")]
pub unsafe fn count() -> Option<u32> {
use ffi::*;
use ffi_types::*;
if rpmReadConfigFiles(std::ptr::null(), std::ptr::null()) != 0 {
return None;
}
let ts = rpmtsCreate();
if ts.is_null() {
return None;
}
let mi = rpmtsInitIterator(ts, rpmDbiTag_e_RPMDBI_LABEL as i32, std::ptr::null(), 0);
if mi.is_null() {
rpmtsFree(ts);
return None;
}
let count = rpmdbGetIteratorCount(mi);
rpmdbFreeIterator(mi);
rpmtsFree(ts);
Some(count as u32)
}
#[allow(clippy::missing_safety_doc, non_snake_case)]
#[cfg(feature = "runtime")]
pub unsafe fn count() -> Option<u32> {
use ffi_types::*;
use libloading::{Library, Symbol};
let lib = Library::new(libloading::library_filename("rpm")).ok()?;
let rpmReadConfigFiles: Symbol<
'_,
unsafe extern "C" fn(
file: *const ::std::os::raw::c_char,
target: *const ::std::os::raw::c_char,
) -> ::std::os::raw::c_int,
> = lib.get(b"rpmReadConfigFiles").ok()?;
let rpmdbFreeIterator: Symbol<
'_,
unsafe extern "C" fn(mi: rpmdbMatchIterator) -> rpmdbMatchIterator,
> = lib.get(b"rpmdbFreeIterator").ok()?;
let rpmdbGetIteratorCount: Symbol<
'_,
unsafe extern "C" fn(mi: rpmdbMatchIterator) -> ::std::os::raw::c_int,
> = lib.get(b"rpmdbGetIteratorCount").ok()?;
let rpmtsCreate: Symbol<'_, unsafe extern "C" fn() -> rpmts> = lib.get(b"rpmtsCreate").ok()?;
let rpmtsFree: Symbol<'_, unsafe extern "C" fn(ts: rpmts) -> rpmts> =
lib.get(b"rpmtsFree").ok()?;
let rpmtsInitIterator: Symbol<
'_,
unsafe extern "C" fn(
ts: rpmts,
rpmtag: rpmDbiTagVal,
keyp: *const ::std::os::raw::c_void,
keylen: size_t,
) -> rpmdbMatchIterator,
> = lib.get(b"rpmtsInitIterator").ok()?;
if rpmReadConfigFiles(std::ptr::null(), std::ptr::null()) != 0 {
return None;
}
let ts = rpmtsCreate();
if ts.is_null() {
return None;
}
let mi = rpmtsInitIterator(ts, rpmDbiTag_e_RPMDBI_LABEL as i32, std::ptr::null(), 0);
if mi.is_null() {
rpmtsFree(ts);
return None;
}
let count = rpmdbGetIteratorCount(mi);
rpmdbFreeIterator(mi);
rpmtsFree(ts);
Some(count as u32)
}