Skip to content

Commit

Permalink
Merge pull request #94 from dsteeley/support_capabilities
Browse files Browse the repository at this point in the history
Add support for capabilities
  • Loading branch information
cat-in-136 authored Sep 20, 2023
2 parents 01a8182 + 27740ef commit ae2792b
Show file tree
Hide file tree
Showing 4 changed files with 41 additions and 12 deletions.
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,7 @@ from [the `Cargo.toml` file](https://doc.rust-lang.org/cargo/reference/manifest.
* doc: set true if it is a document file.
* user: the owner of the file.
* group: the group owner of the file.
* caps: optional string of capabilities. (e.g. `cap_sys_admin=pe`)
* release: optional string of release.
* epoch: optional number of epoch.
* pre_install_script: optional string or file path of pre_install_script.
Expand Down Expand Up @@ -198,4 +199,3 @@ The default payload compress type of the generated RPM file is zstd.
You can specify the payload compress type with `--payload-compress TYPE`: none, gzip, or zstd.

For the legacy system (e.g. centos7), specify legacy compress type explicitly e.g. `--payload-compress none`.

45 changes: 36 additions & 9 deletions src/config/file_info.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,17 +7,18 @@ use std::path::{Path, PathBuf};
use toml::Value;

#[derive(Debug, Eq, PartialEq, Clone)]
pub struct FileInfo<'a, 'b, 'c, 'd> {
pub struct FileInfo<'a, 'b, 'c, 'd, 'e> {
pub source: &'a str,
pub dest: &'b str,
pub user: Option<&'c str>,
pub group: Option<&'d str>,
pub mode: Option<usize>,
pub config: bool,
pub doc: bool,
pub caps: Option<&'e str>,
}

impl FileInfo<'_, '_, '_, '_> {
impl FileInfo<'_, '_, '_, '_, '_> {
pub fn new(assets: &[Value]) -> Result<Vec<FileInfo>, ConfigError> {
let mut files = Vec::with_capacity(assets.len());
for (idx, value) in assets.iter().enumerate() {
Expand Down Expand Up @@ -53,6 +54,14 @@ impl FileInfo<'_, '_, '_, '_> {
None
};
let mode = Self::get_mode(table, source, idx)?;
let caps = if let Some(caps) = table.get("caps") {
Some(
caps.as_str()
.ok_or(ConfigError::AssetFileWrongType(idx, "caps", "string"))?,
)
} else {
None
};
let config = if let Some(is_config) = table.get("config") {
is_config
.as_bool()
Expand All @@ -76,6 +85,7 @@ impl FileInfo<'_, '_, '_, '_> {
mode,
config,
doc,
caps,
});
}
Ok(files)
Expand Down Expand Up @@ -124,7 +134,11 @@ impl FileInfo<'_, '_, '_, '_> {
Err(ConfigError::AssetFileNotFound(PathBuf::from(source)))
}

fn generate_rpm_file_options<T: ToString>(&self, dest: T) -> rpm::FileOptions {
fn generate_rpm_file_options<T: ToString>(
&self,
dest: T,
idx: usize,
) -> Result<rpm::FileOptions, ConfigError> {
let mut rpm_file_option = rpm::FileOptions::new(dest.to_string());
if let Some(user) = self.user {
rpm_file_option = rpm_file_option.user(user);
Expand All @@ -141,7 +155,12 @@ impl FileInfo<'_, '_, '_, '_> {
if self.doc {
rpm_file_option = rpm_file_option.is_doc();
}
rpm_file_option.into()
if let Some(caps) = self.caps {
rpm_file_option = rpm_file_option
.caps(caps)
.map_err(|err| ConfigError::AssetFileRpm(idx, "caps", err.into()))?;
}
Ok(rpm_file_option.into())
}

pub(crate) fn generate_rpm_file_entry<P: AsRef<Path>>(
Expand All @@ -150,12 +169,13 @@ impl FileInfo<'_, '_, '_, '_> {
parent: P,
idx: usize,
) -> Result<Vec<(PathBuf, rpm::FileOptions)>, ConfigError> {
self.generate_expanded_path(build_target, parent, idx)
.map(|p| {
p.iter()
.map(|(src, dst)| (src.clone(), self.generate_rpm_file_options(dst)))
.collect::<Vec<_>>()
self.generate_expanded_path(build_target, parent, idx)?
.iter()
.map(|(src, dst)| {
self.generate_rpm_file_options(dst, idx)
.map(|v| (src.clone(), v))
})
.collect::<Result<Vec<_>, _>>()
}
}

Expand Down Expand Up @@ -293,6 +313,7 @@ mod test {
mode: Some(0o0100755),
config: false,
doc: false,
caps: None,
},
FileInfo {
source: "LICENSE",
Expand All @@ -302,6 +323,7 @@ mod test {
mode: Some(0o0100644),
config: false,
doc: true,
caps: None,
},
FileInfo {
source: "README.md",
Expand All @@ -311,6 +333,7 @@ mod test {
mode: Some(0o0100644),
config: false,
doc: true,
caps: None,
},
]
);
Expand All @@ -329,6 +352,7 @@ mod test {
mode: None,
config: false,
doc: true,
caps: Some("cap_sys_admin=pe"),
};
let expanded = file_info
.generate_expanded_path(&target, &tempdir, 0)
Expand All @@ -349,6 +373,7 @@ mod test {
mode: None,
config: false,
doc: true,
caps: None,
};
assert!(
matches!(file_info.generate_expanded_path(&target, &tempdir, 0),
Expand All @@ -365,6 +390,7 @@ mod test {
mode: None,
config: false,
doc: false,
caps: None,
};
let expanded = file_info
.generate_expanded_path(&target, &tempdir, 0)
Expand Down Expand Up @@ -434,6 +460,7 @@ mod test {
mode: None,
config: false,
doc: false,
caps: None,
};
let args = crate::cli::Cli {
target_dir: Some(
Expand Down
4 changes: 2 additions & 2 deletions src/config/metadata.rs
Original file line number Diff line number Diff line change
Expand Up @@ -145,8 +145,8 @@ impl ExtraMetaData {
toml_dotted_bare_key_parser::parse_dotted_bare_keys(branch.as_ref())
.map_err(|e| ConfigError::WrongBranchPathOfToml(branch.clone(), e))?
.iter()
.fold(Some(root), |table, key| {
table.and_then(|v| v.get(*key).and_then(|v| v.as_table()))
.try_fold(root, |table, key| {
table.get(*key).and_then(|v| v.as_table())
})
.ok_or(ConfigError::BranchPathNotFoundInToml(branch.to_string()))
} else {
Expand Down
2 changes: 2 additions & 0 deletions src/error.rs
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,8 @@ pub enum ConfigError {
WrongBranchPathOfToml(String, #[source] DottedBareKeyLexError),
#[error("Branch `{0}' not found")]
BranchPathNotFoundInToml(String),
#[error("Field {1} for file {0} has the following error: {2}")]
AssetFileRpm(usize, &'static str, #[source] std::rc::Rc<rpm::Error>),
}

#[derive(thiserror::Error, Debug)]
Expand Down

0 comments on commit ae2792b

Please sign in to comment.