Skip to content

Commit

Permalink
pr-review. align get scriptlet fns to builder
Browse files Browse the repository at this point in the history
doc. update readme w/ example
  • Loading branch information
juliusl committed Dec 9, 2023
1 parent 49492f5 commit e709f57
Show file tree
Hide file tree
Showing 7 changed files with 107 additions and 132 deletions.
5 changes: 5 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,11 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
.prog(vec!["/bin/blah/bash", "-c"])
)
```
- Added `get_*_script` methods to `PackageMetadata` for finding scriptlets
- Example Usage:
```rs
package_metadata.get_pre_install_script()?;
```

## 0.12.1

Expand Down
6 changes: 6 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,12 @@ let pkg = rpm::PackageBuilder::new("test", "1.0.0", "MIT", "x86_64", "some aweso
.user("hugo"),
)?
.pre_install_script("echo preinst")
// Alternatively, use scriptlet builder api to specify flags and interpreter/arguments
.post_trans_script(
Scriptlet::new("echo posttrans")
.flags(ScriptletFlags::EXPAND)
.prog(vec!["/bin/blah/bash", "-c"])
)
// If you don't need reproducible builds,
// you can remove the following line
.source_date(source_date)
Expand Down
3 changes: 0 additions & 3 deletions src/errors.rs
Original file line number Diff line number Diff line change
Expand Up @@ -55,9 +55,6 @@ pub enum Error {
#[error("invalid capabilities specified {caps}")]
InvalidCapabilities { caps: String },

#[error("scriptlet tags are not set, cannot determine scriptlet type")]
NoScriptletTagsSet,

#[error("signature packet not found in what is supposed to be a signature")]
NoSignatureFound,

Expand Down
32 changes: 16 additions & 16 deletions src/rpm/builder.rs
Original file line number Diff line number Diff line change
Expand Up @@ -378,7 +378,7 @@ impl PackageBuilder {
///
#[inline]
pub fn pre_install_script(mut self, content: impl Into<Scriptlet>) -> Self {
self.pre_inst_script = Some(content.into().ty(ScriptletType::PreInstall));
self.pre_inst_script = Some(content.into());
self
}

Expand All @@ -388,7 +388,7 @@ impl PackageBuilder {
///
#[inline]
pub fn post_install_script(mut self, content: impl Into<Scriptlet>) -> Self {
self.post_inst_script = Some(content.into().ty(ScriptletType::PostInstall));
self.post_inst_script = Some(content.into());
self
}

Expand All @@ -398,7 +398,7 @@ impl PackageBuilder {
///
#[inline]
pub fn pre_uninstall_script(mut self, content: impl Into<Scriptlet>) -> Self {
self.pre_uninst_script = Some(content.into().ty(ScriptletType::PreUninstall));
self.pre_uninst_script = Some(content.into());
self
}

Expand All @@ -408,7 +408,7 @@ impl PackageBuilder {
///
#[inline]
pub fn post_uninstall_script(mut self, content: impl Into<Scriptlet>) -> Self {
self.post_uninst_script = Some(content.into().ty(ScriptletType::PostUninstall));
self.post_uninst_script = Some(content.into());
self
}

Expand All @@ -418,7 +418,7 @@ impl PackageBuilder {
///
#[inline]
pub fn pre_trans_script(mut self, content: impl Into<Scriptlet>) -> Self {
self.pre_trans_script = Some(content.into().ty(ScriptletType::PreTransaction));
self.pre_trans_script = Some(content.into());
self
}

Expand All @@ -428,7 +428,7 @@ impl PackageBuilder {
///
#[inline]
pub fn post_trans_script(mut self, content: impl Into<Scriptlet>) -> Self {
self.post_trans_script = Some(content.into().ty(ScriptletType::PostTransaction));
self.post_trans_script = Some(content.into());
self
}

Expand All @@ -438,7 +438,7 @@ impl PackageBuilder {
///
#[inline]
pub fn pre_untrans_script(mut self, content: impl Into<Scriptlet>) -> Self {
self.pre_untrans_script = Some(content.into().ty(ScriptletType::PreUntransaction));
self.pre_untrans_script = Some(content.into());
self
}

Expand All @@ -448,7 +448,7 @@ impl PackageBuilder {
///
#[inline]
pub fn post_untrans_script(mut self, content: impl Into<Scriptlet>) -> Self {
self.post_untrans_script = Some(content.into().ty(ScriptletType::PostUntransaction));
self.post_untrans_script = Some(content.into());
self
}

Expand Down Expand Up @@ -1276,35 +1276,35 @@ impl PackageBuilder {
}

if let Some(script) = self.pre_inst_script {
script.apply(&mut actual_records, offset)?;
script.apply(&mut actual_records, offset, PREIN_TAGS);
}

if let Some(script) = self.post_inst_script {
script.apply(&mut actual_records, offset)?;
script.apply(&mut actual_records, offset, POSTIN_TAGS);
}

if let Some(script) = self.pre_uninst_script {
script.apply(&mut actual_records, offset)?;
script.apply(&mut actual_records, offset, PREUN_TAGS);
}

if let Some(script) = self.post_uninst_script {
script.apply(&mut actual_records, offset)?;
script.apply(&mut actual_records, offset, POSTUN_TAGS);
}

if let Some(script) = self.pre_trans_script {
script.apply(&mut actual_records, offset)?;
script.apply(&mut actual_records, offset, PRETRANS_TAGS);
}

if let Some(script) = self.post_trans_script {
script.apply(&mut actual_records, offset)?;
script.apply(&mut actual_records, offset, POSTTRANS_TAGS);
}

if let Some(script) = self.pre_untrans_script {
script.apply(&mut actual_records, offset)?;
script.apply(&mut actual_records, offset, PREUNTRANS_TAGS);
}

if let Some(script) = self.post_untrans_script {
script.apply(&mut actual_records, offset)?;
script.apply(&mut actual_records, offset, POSTUNTRANS_TAGS);
}

if let Some(vendor) = self.vendor {
Expand Down
117 changes: 28 additions & 89 deletions src/rpm/headers/types.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
//! A collection of types used in various header records.
use crate::{constants::*, errors, Error, FileCaps, IndexData, IndexEntry, Timestamp};
use crate::{constants::*, errors, FileCaps, IndexData, IndexEntry, Timestamp};
use digest::Digest;
use itertools::Itertools;
use std::str::FromStr;
Expand Down Expand Up @@ -385,51 +385,7 @@ impl<W: std::io::Write> std::io::Write for Sha256Writer<W> {

/// Type-alias for a tuple containing index tags for a scriptlet type,
///
pub type ScriptletIndexTags = (IndexTag, IndexTag, IndexTag);

/// Enumeration of different scriptlet types,
///
pub enum ScriptletType {
/// Type for %prein
///
PreInstall,
/// Type for %postin
///
PostInstall,
/// Type for %preun
///
PreUninstall,
/// Type for %postun
///
PostUninstall,
/// Type for %pretrans
///
PreTransaction,
/// Type for %posttrans
///
PostTransaction,
/// Type for %preuntrans
///
PreUntransaction,
/// Type for %postuntrans
///
PostUntransaction,
}

impl From<ScriptletType> for ScriptletIndexTags {
fn from(value: ScriptletType) -> Self {
match value {
ScriptletType::PreInstall => PREIN_TAGS,
ScriptletType::PostInstall => POSTIN_TAGS,
ScriptletType::PreUninstall => PREUN_TAGS,
ScriptletType::PostUninstall => POSTUN_TAGS,
ScriptletType::PreTransaction => PRETRANS_TAGS,
ScriptletType::PostTransaction => POSTTRANS_TAGS,
ScriptletType::PreUntransaction => PREUNTRANS_TAGS,
ScriptletType::PostUntransaction => POSTUNTRANS_TAGS,
}
}
}
pub(crate) type ScriptletIndexTags = (IndexTag, IndexTag, IndexTag);

/// Description of a scriptlet as present in a RPM header record,
pub struct Scriptlet {
Expand All @@ -442,9 +398,6 @@ pub struct Scriptlet {
/// Optional scriptlet interpreter/arguments,
///
pub program: Option<Vec<String>>,
/// Type of scriptlet, (%prein, $postin, etc)
///
pub ty: Option<ScriptletIndexTags>,
}

impl Scriptlet {
Expand All @@ -456,7 +409,6 @@ impl Scriptlet {
script: script.into(),
flags: None,
program: None,
ty: None,
}
}

Expand All @@ -478,47 +430,36 @@ impl Scriptlet {
self
}

/// Sets the scriptlet type by specifying the scriptlet index tags,
///
#[inline]
pub fn ty(mut self, ty: ScriptletType) -> Self {
self.ty = Some(ty.into());
self
}

/// Consumes the receiver and applies all index entries for the scriptlet based on builder state,
///
pub(crate) fn apply(
self,
records: &mut Vec<IndexEntry<IndexTag>>,
offset: i32,
) -> Result<(), crate::errors::Error> {
if let Some((script_tag, flags_tag, prog_tag)) = self.ty {
tags: ScriptletIndexTags,
) {
let (script_tag, flags_tag, prog_tag) = tags;

records.push(IndexEntry::new(
script_tag,
offset,
IndexData::StringTag(self.script),
));

if let Some(flags) = self.flags {
records.push(IndexEntry::new(
script_tag,
flags_tag,
offset,
IndexData::StringTag(self.script),
IndexData::Int32(vec![flags.bits()]),
));
}

if let Some(flags) = self.flags {
records.push(IndexEntry::new(
flags_tag,
offset,
IndexData::Int32(vec![flags.bits()]),
));
}

if let Some(prog) = self.program {
records.push(IndexEntry::new(
prog_tag,
offset,
IndexData::StringArray(prog),
));
}

Ok(())
} else {
Err(Error::NoScriptletTagsSet)
if let Some(prog) = self.program {
records.push(IndexEntry::new(
prog_tag,
offset,
IndexData::StringArray(prog),
));
}
}
}
Expand Down Expand Up @@ -656,20 +597,19 @@ mod test {
#[test]
fn test_scriptlet_builder() {
// Test full state
let _scriptlet = crate::Scriptlet::new(
let scriptlet = crate::Scriptlet::new(
r#"
echo `hello world`
"#
.trim(),
)
.flags(crate::ScriptletFlags::EXPAND)
.prog(vec!["/usr/bin/blah", "-c"])
.ty(crate::ScriptletType::PreInstall);
.prog(vec!["/usr/bin/blah", "-c"]);

let mut records = vec![];
let offset = 0i32;

_scriptlet.apply(&mut records, offset).expect("should work");
scriptlet.apply(&mut records, offset, crate::PREIN_TAGS);

assert!(records.len() == 3);
assert_eq!(records[0].tag, crate::IndexTag::RPMTAG_PREIN as u32);
Expand All @@ -683,18 +623,17 @@ echo `hello world`
);

// Test partial state
let _scriptlet = crate::Scriptlet::new(
let scriptlet = crate::Scriptlet::new(
r#"
echo `hello world`
"#
.trim(),
)
.ty(crate::ScriptletType::PostUninstall);
);

let mut records = vec![];
let offset = 0i32;

_scriptlet.apply(&mut records, offset).expect("should work");
scriptlet.apply(&mut records, offset, crate::POSTUN_TAGS);
assert!(records.len() == 1);
assert_eq!(records[0].tag, crate::IndexTag::RPMTAG_POSTUN as u32);
assert_eq!(records[0].data.as_str(), Some("echo `hello world`"));
Expand Down
Loading

0 comments on commit e709f57

Please sign in to comment.