use crate as cindy;
pub mod archive;
pub mod command;
pub mod compress;
pub mod debian;
pub mod decompress;
pub mod fetch;
pub mod group;
pub mod path;
pub mod systemd;
pub mod unarchive;
pub mod user;
#[derive(Clone, Copy, PartialEq, Eq)]
#[crate::wire]
pub enum Codec {
Store,
Gzip,
Xz,
Zstd,
}
#[derive(Clone, Copy, PartialEq, Eq)]
#[crate::wire]
pub enum Format {
Tar(Option<Codec>),
Zip,
SevenZ,
}
impl Format {
pub fn from_path(path: &std::path::Path) -> crate::Result<Self> {
let name = path
.file_name()
.and_then(|n| n.to_str())
.unwrap_or_default()
.to_ascii_lowercase();
let format = if name.ends_with(".tar.gz") || name.ends_with(".tgz") {
Self::Tar(Some(Codec::Gzip))
} else if name.ends_with(".tar.xz") || name.ends_with(".txz") {
Self::Tar(Some(Codec::Xz))
} else if name.ends_with(".tar.zst") || name.ends_with(".tzst") {
Self::Tar(Some(Codec::Zstd))
} else if name.ends_with(".tar") {
Self::Tar(None)
} else if name.ends_with(".zip") {
Self::Zip
} else if name.ends_with(".7z") {
Self::SevenZ
} else {
crate::bail!(
"couldn't infer archive format from {:?}; set `format` explicitly",
path
);
};
Ok(format)
}
}
#[crate::wire]
pub enum Return {
Unchanged,
Changed,
}
impl Return {
pub fn changed(&self) -> bool {
matches!(self, Self::Changed)
}
pub fn from_changed(changed: bool) -> Self {
if changed {
Self::Changed
} else {
Self::Unchanged
}
}
}
pub fn current_owner_names() -> (String, String) {
let uid = nix::unistd::getuid();
let gid = nix::unistd::getgid();
let user = nix::unistd::User::from_uid(uid)
.ok()
.flatten()
.map(|u| u.name)
.unwrap_or_else(|| uid.as_raw().to_string());
let group = nix::unistd::Group::from_gid(gid)
.ok()
.flatten()
.map(|g| g.name)
.unwrap_or_else(|| gid.as_raw().to_string());
(user, group)
}
pub(crate) fn run_check(cmd: &mut std::process::Command) -> crate::Result<()> {
use crate::Context as _;
let out = cmd
.output()
.context(format!("Failed to spawn {:?}", cmd.get_program()))?;
if !out.status.success() {
crate::bail!(
"{:?} failed with {:?}:\n--- stdout ---\n{}\n--- stderr ---\n{}",
cmd.get_program(),
out.status,
String::from_utf8_lossy(&out.stdout),
String::from_utf8_lossy(&out.stderr),
);
}
Ok(())
}