#
alpm_utils/
conf.rs

    1use alpm::{Alpm, SigLevel, Usage};
    2
    3/// Re-export of pacmanconf.
    4pub mod config {
    5    pub use pacmanconf::*;
    6}
    7
    8use pacmanconf::Config;
    9
    10/// Initiates and configures Alpm using a pacman config.
    11///
    12/// ```no_run
    13/// use pacmanconf::Config;
    14/// use alpm_utils::alpm_with_conf;
    15///
    16/// # fn main() {
    17/// let conf = Config::new().unwrap();
    18/// let alpm = alpm_with_conf(&conf).unwrap();
    19/// # }
    20/// ```
    21pub fn alpm_with_conf(conf: &Config) -> alpm::Result<Alpm> {
    22    let mut alpm = Alpm::new(&*conf.root_dir, &*conf.db_path)?;
    23    configure_alpm(&mut alpm, conf)?;
    24    Ok(alpm)
    25}
    26
    27/// Configures an exsting Alpm handle  using a pacman config.
    28///
    29/// You probably just want to use alpm_with_conf unless you need to do something before the
    30/// repos are registered such as setting the db ext.
    31///
    32/// ```no_run
    33/// use pacmanconf::Config;
    34/// use alpm_utils::configure_alpm;
    35/// use alpm::Alpm;
    36///
    37/// # fn main() {
    38/// let conf = Config::new().unwrap();
    39/// let mut alpm = Alpm::new(&*conf.root_dir, &*conf.db_path).unwrap();
    40/// let alpm = configure_alpm(&mut alpm, &conf).unwrap();
    41/// # }
    42/// ```
    43pub fn configure_alpm(alpm: &mut Alpm, conf: &Config) -> alpm::Result<()> {
    44    alpm.set_cachedirs(conf.cache_dir.iter())?;
    45    alpm.set_hookdirs(conf.hook_dir.iter())?;
    46    alpm.set_gpgdir(&*conf.gpg_dir)?;
    47    alpm.set_logfile(&*conf.log_file)?;
    48    alpm.set_ignorepkgs(conf.ignore_pkg.iter())?;
    49    alpm.set_ignorepkgs(conf.ignore_pkg.iter())?;
    50    alpm.set_architectures(conf.architecture.iter())?;
    51    alpm.set_noupgrades(conf.no_upgrade.iter())?;
    52    alpm.set_noextracts(conf.no_extract.iter())?;
    53    alpm.set_default_siglevel(parse_sig_level(&conf.sig_level))?;
    54    alpm.set_local_file_siglevel(parse_sig_level(&conf.local_file_sig_level))?;
    55    alpm.set_remote_file_siglevel(parse_sig_level(&conf.remote_file_sig_level))?;
    56    alpm.set_use_syslog(conf.use_syslog);
    57    alpm.set_check_space(conf.check_space);
    58    alpm.set_disable_dl_timeout(conf.disable_download_timeout);
    59    alpm.set_parallel_downloads(conf.parallel_downloads as u32);
    60    alpm.set_disable_sandbox_filesystem(conf.disable_sandbox_filesystem || conf.disable_sandbox);
    61    alpm.set_disable_sandbox_syscalls(conf.disable_sandbox_syscalls || conf.disable_sandbox);
    62    alpm.set_sandbox_user(conf.download_user.clone())?;
    63
    64    for repo in &conf.repos {
    65        register_db(alpm, repo)?;
    66    }
    67
    68    Ok(())
    69}
    70
    71fn parse_sig_level(levels: &[String]) -> SigLevel {
    72    let mut sig = SigLevel::NONE;
    73
    74    for level in levels {
    75        match level.as_str() {
    76            "PackageNever" => sig.remove(SigLevel::PACKAGE),
    77            "PackageOptional" => sig.insert(SigLevel::PACKAGE | SigLevel::PACKAGE_OPTIONAL),
    78            "PackageRequired" => {
    79                sig.insert(SigLevel::PACKAGE);
    80                sig.remove(SigLevel::PACKAGE_OPTIONAL);
    81            }
    82            "PackageTrustOnly" => {
    83                sig.remove(SigLevel::PACKAGE_MARGINAL_OK | SigLevel::PACKAGE_UNKNOWN_OK)
    84            }
    85            "PackageTrustAll" => {
    86                sig.insert(SigLevel::PACKAGE_MARGINAL_OK | SigLevel::PACKAGE_UNKNOWN_OK)
    87            }
    88            "DatabaseNever" => sig.remove(SigLevel::DATABASE),
    89            "DatabaseOptional" => sig.insert(SigLevel::DATABASE | SigLevel::DATABASE_OPTIONAL),
    90            "DatabaseRequired" => {
    91                sig.insert(SigLevel::DATABASE);
    92                sig.remove(SigLevel::DATABASE_OPTIONAL);
    93            }
    94            "DatabaseTrustOnly" => {
    95                sig.remove(SigLevel::DATABASE_MARGINAL_OK | SigLevel::DATABASE_UNKNOWN_OK)
    96            }
    97            "DatabaseTrustAll" => {
    98                sig.insert(SigLevel::DATABASE_MARGINAL_OK | SigLevel::DATABASE_UNKNOWN_OK)
    99            }
    100            _ => {}
    101        }
    102    }
    103
    104    sig
    105}
    106
    107fn register_db(alpm: &mut alpm::Alpm, repo: &pacmanconf::Repository) -> alpm::Result<()> {
    108    let sig = if repo.sig_level.is_empty() {
    109        SigLevel::USE_DEFAULT
    110    } else {
    111        parse_sig_level(&repo.sig_level)
    112    };
    113
    114    let db = alpm.register_syncdb_mut(&*repo.name, sig)?;
    115    db.set_servers(repo.servers.iter())?;
    116
    117    let mut usage = Usage::NONE;
    118
    119    for v in &repo.usage {
    120        match v.as_str() {
    121            "Sync" => usage |= Usage::SYNC,
    122            "Search" => usage |= Usage::SEARCH,
    123            "Install" => usage |= Usage::INSTALL,
    124            "Upgrade" => usage |= Usage::UPGRADE,
    125            _ => {}
    126        }
    127
    128        if usage == Usage::NONE {
    129            usage = Usage::ALL
    130        }
    131    }
    132
    133    db.set_usage(usage)?;
    134    Ok(())
    135}