use crate::error::{Error, ErrorKind};
use crate::internal::ConfigState;
use crate::macro_context::MacroContext;
use std::ffi::CString;
use std::os::unix::ffi::OsStrExt;
use std::path::Path;
use std::ptr;
const DB_PATH_MACRO: &str = "_dbpath";
pub(crate) fn read_file(config_file: Option<&Path>) -> Result<(), Error> {
let mut global_state = ConfigState::lock();
if global_state.configured {
fail!(ErrorKind::Config, "already configured");
}
let rc = match config_file {
Some(path) => {
if !path.exists() {
fail!(ErrorKind::Config, "no such file: {}", path.display())
}
let cstr = CString::new(path.as_os_str().as_bytes()).map_err(|e| {
format_err!(
ErrorKind::InvalidArg,
"invalid path: {} ({})",
path.display(),
e
)
})?;
unsafe { librpm_sys::rpmReadConfigFiles(cstr.as_ptr(), ptr::null()) }
}
None => unsafe { librpm_sys::rpmReadConfigFiles(ptr::null(), ptr::null()) },
};
if rc != 0 {
match config_file {
Some(path) => fail!(
ErrorKind::Config,
"error reading RPM config from: {}",
path.display()
),
None => fail!(
ErrorKind::Config,
"error reading RPM config from default location"
),
}
}
global_state.configured = true;
Ok(())
}
pub(crate) fn set_db_path(path: &Path) -> Result<(), Error> {
MacroContext::default().define(&format!("{} {}", DB_PATH_MACRO, path.display()), 0)
}