From e709f5720db3bbcd54642190f6b68169fea2d26b Mon Sep 17 00:00:00 2001 From: Julius Liu Date: Sat, 9 Dec 2023 13:29:23 -0800 Subject: [PATCH] pr-review. align get scriptlet fns to builder doc. update readme w/ example --- CHANGELOG.md | 5 ++ README.md | 6 ++ src/errors.rs | 3 - src/rpm/builder.rs | 32 +++++------ src/rpm/headers/types.rs | 117 ++++++++++----------------------------- src/rpm/package.rs | 62 ++++++++++++++++----- src/tests.rs | 14 ++--- 7 files changed, 107 insertions(+), 132 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index d3965306..d95a1393 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -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 diff --git a/README.md b/README.md index daf11771..f7f49c9c 100644 --- a/README.md +++ b/README.md @@ -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) diff --git a/src/errors.rs b/src/errors.rs index 56503ebc..ff179aa8 100644 --- a/src/errors.rs +++ b/src/errors.rs @@ -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, diff --git a/src/rpm/builder.rs b/src/rpm/builder.rs index 6b1e9fe7..3c2d6acd 100644 --- a/src/rpm/builder.rs +++ b/src/rpm/builder.rs @@ -378,7 +378,7 @@ impl PackageBuilder { /// #[inline] pub fn pre_install_script(mut self, content: impl Into) -> Self { - self.pre_inst_script = Some(content.into().ty(ScriptletType::PreInstall)); + self.pre_inst_script = Some(content.into()); self } @@ -388,7 +388,7 @@ impl PackageBuilder { /// #[inline] pub fn post_install_script(mut self, content: impl Into) -> Self { - self.post_inst_script = Some(content.into().ty(ScriptletType::PostInstall)); + self.post_inst_script = Some(content.into()); self } @@ -398,7 +398,7 @@ impl PackageBuilder { /// #[inline] pub fn pre_uninstall_script(mut self, content: impl Into) -> Self { - self.pre_uninst_script = Some(content.into().ty(ScriptletType::PreUninstall)); + self.pre_uninst_script = Some(content.into()); self } @@ -408,7 +408,7 @@ impl PackageBuilder { /// #[inline] pub fn post_uninstall_script(mut self, content: impl Into) -> Self { - self.post_uninst_script = Some(content.into().ty(ScriptletType::PostUninstall)); + self.post_uninst_script = Some(content.into()); self } @@ -418,7 +418,7 @@ impl PackageBuilder { /// #[inline] pub fn pre_trans_script(mut self, content: impl Into) -> Self { - self.pre_trans_script = Some(content.into().ty(ScriptletType::PreTransaction)); + self.pre_trans_script = Some(content.into()); self } @@ -428,7 +428,7 @@ impl PackageBuilder { /// #[inline] pub fn post_trans_script(mut self, content: impl Into) -> Self { - self.post_trans_script = Some(content.into().ty(ScriptletType::PostTransaction)); + self.post_trans_script = Some(content.into()); self } @@ -438,7 +438,7 @@ impl PackageBuilder { /// #[inline] pub fn pre_untrans_script(mut self, content: impl Into) -> Self { - self.pre_untrans_script = Some(content.into().ty(ScriptletType::PreUntransaction)); + self.pre_untrans_script = Some(content.into()); self } @@ -448,7 +448,7 @@ impl PackageBuilder { /// #[inline] pub fn post_untrans_script(mut self, content: impl Into) -> Self { - self.post_untrans_script = Some(content.into().ty(ScriptletType::PostUntransaction)); + self.post_untrans_script = Some(content.into()); self } @@ -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 { diff --git a/src/rpm/headers/types.rs b/src/rpm/headers/types.rs index 926ab251..67a1eb8f 100644 --- a/src/rpm/headers/types.rs +++ b/src/rpm/headers/types.rs @@ -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; @@ -385,51 +385,7 @@ impl std::io::Write for Sha256Writer { /// 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 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 { @@ -442,9 +398,6 @@ pub struct Scriptlet { /// Optional scriptlet interpreter/arguments, /// pub program: Option>, - /// Type of scriptlet, (%prein, $postin, etc) - /// - pub ty: Option, } impl Scriptlet { @@ -456,7 +409,6 @@ impl Scriptlet { script: script.into(), flags: None, program: None, - ty: None, } } @@ -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>, 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), + )); } } } @@ -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); @@ -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`")); diff --git a/src/rpm/package.rs b/src/rpm/package.rs index 21661105..b0e37a49 100644 --- a/src/rpm/package.rs +++ b/src/rpm/package.rs @@ -504,19 +504,52 @@ impl PackageMetadata { .get_entry_data_as_string(IndexTag::RPMTAG_SOURCERPM) } - /// Finds a scriptlet by scriptlet type, - /// - /// **Example** - /// - /// ```rs norun - /// pkg.metadata.find_scriptlet(ScriptletType::PreInstall)? - /// ``` - /// + /// Get the %prein scriptlet for this package + #[inline] + pub fn get_pre_install_script(&self) -> Result { + self.get_scriptlet(PREIN_TAGS) + } + + /// Get the %postin scriptlet for this package + #[inline] + pub fn get_post_install_script(&self) -> Result { + self.get_scriptlet(POSTIN_TAGS) + } + + /// Get the %preun scriptlet for this package + #[inline] + pub fn get_pre_uninstall_script(&self) -> Result { + self.get_scriptlet(PREUN_TAGS) + } + + /// Get the %postun scriptlet for this package #[inline] - pub fn find_scriptlet(&self, ty: ScriptletType) -> Result { - let (s, f, p) = ty.into(); + pub fn get_post_uninstall_script(&self) -> Result { + self.get_scriptlet(POSTUN_TAGS) + } - self.get_scriptlet(s, f, p) + /// Get the %pretrans scriptlet for this package + #[inline] + pub fn get_pre_trans_script(&self) -> Result { + self.get_scriptlet(PRETRANS_TAGS) + } + + /// Get the %posttrans scriptlet for this package + #[inline] + pub fn get_post_trans_script(&self) -> Result { + self.get_scriptlet(POSTTRANS_TAGS) + } + + /// Get the %preuntrans scriptlet for this package + #[inline] + pub fn get_pre_untrans_script(&self) -> Result { + self.get_scriptlet(PREUNTRANS_TAGS) + } + + /// Get the %postuntrans scriptlet for this package + #[inline] + pub fn get_post_untrans_script(&self) -> Result { + self.get_scriptlet(POSTUNTRANS_TAGS) } fn get_dependencies( @@ -557,10 +590,10 @@ impl PackageMetadata { fn get_scriptlet( &self, - scriptlet_tag: IndexTag, - flags_tag: IndexTag, - program_tag: IndexTag, + tags: ScriptletIndexTags, ) -> Result { + let (scriptlet_tag, flags_tag, program_tag) = tags; + let script = self .header .get_entry_data_as_string(scriptlet_tag) @@ -580,7 +613,6 @@ impl PackageMetadata { script, flags, program, - ty: Some((scriptlet_tag, flags_tag, program_tag)), }) } diff --git a/src/tests.rs b/src/tests.rs index 4005c04b..efa2230c 100644 --- a/src/tests.rs +++ b/src/tests.rs @@ -99,31 +99,27 @@ However, it does nothing.", }); // Test scriptlet builder fn branches - let preinst = pkg.metadata.find_scriptlet(ScriptletType::PreInstall)?; + let preinst = pkg.metadata.get_pre_install_script()?; assert_eq!(preinst.script.as_str(), "echo preinst"); assert!(preinst.flags.is_none()); assert!(preinst.program.is_none()); - assert_eq!(preinst.ty, Some(PREIN_TAGS)); - let postinst = pkg.metadata.find_scriptlet(ScriptletType::PostInstall)?; + let postinst = pkg.metadata.get_post_install_script()?; assert_eq!(postinst.script.as_str(), "echo postinst"); assert!(postinst.flags.is_none()); assert_eq!(postinst.program, Some(vec!["/bin/blah/bash".to_string(), "-c".to_string()])); - assert_eq!(postinst.ty, Some(POSTIN_TAGS)); - let pretrans = pkg.metadata.find_scriptlet(ScriptletType::PreTransaction)?; + let pretrans = pkg.metadata.get_pre_trans_script()?; assert_eq!(pretrans.script.as_str(), "echo pretrans"); assert_eq!(pretrans.flags, Some(ScriptletFlags::EXPAND)); assert!(pretrans.program.is_none()); - assert_eq!(pretrans.ty, Some(PRETRANS_TAGS)); - let posttrans = pkg.metadata.find_scriptlet(ScriptletType::PostTransaction)?; + let posttrans = pkg.metadata.get_post_trans_script()?; assert_eq!(posttrans.script.as_str(), "echo posttrans"); assert_eq!(posttrans.flags, Some(ScriptletFlags::EXPAND)); assert_eq!(posttrans.program, Some(vec!["/bin/blah/bash".to_string(), "-c".to_string()])); - assert_eq!(posttrans.ty, Some(POSTTRANS_TAGS)); - assert!(pkg.metadata.find_scriptlet(ScriptletType::PreUntransaction).is_err()); + assert!(pkg.metadata.get_pre_untrans_script().is_err()); Ok(()) }