use std::path::PathBuf;
use anyhow::{Context, Error, format_err};
use tracing::debug;
#[tracing::instrument]
pub(crate) fn determine_architecture() -> Result<String, Error> {
debug!("Running `dpkg --print-architecture");
let architecture = std::process::Command::new("dpkg")
.arg("--print-architecture")
.output()
.with_context(|| {
"Failed to run `dpkg --print-architecture` to get native architecture".to_string()
})?;
let architecture = String::from_utf8(architecture.stdout)
.context("Failed to parse `dpkg --architecture` output as UTF-8")?;
let architecture = architecture.trim().to_string();
debug!("Architecture: {architecture}");
Ok(architecture)
}
fn base_dirs() -> Result<xdg::BaseDirectories, Error> {
xdg::BaseDirectories::with_prefix(env!("CARGO_BIN_NAME"))
.map_err(|err| format_err!("XDG error: {err}"))
}
pub(crate) fn get_base_dir() -> Result<PathBuf, Error> {
Ok(base_dirs()?.get_cache_home())
}
pub(crate) fn create_or_get_apt_root(suite: &str) -> Result<PathBuf, Error> {
let xdg = base_dirs()?;
xdg.create_cache_directory(format!("aptroot-{suite}"))
.with_context(|| {
let cache = xdg.get_cache_home();
format!("Failed to create aptroot cache dir in {cache:?}")
})
}
pub(crate) fn get_packages_index_path(suite: &str, arch: &str) -> String {
format!("var/lib/apt/lists/deb.debian.org_debian_dists_{suite}_main_binary-{arch}_Packages")
}
pub(crate) fn get_sources_index_path(suite: &str) -> String {
format!("var/lib/apt/lists/deb.debian.org_debian_dists_{suite}_main_source_Sources")
}