From b0cc7c75b9d6aab47d76cb5e45f3154914eafc39 Mon Sep 17 00:00:00 2001 From: Luna Date: Thu, 9 Nov 2023 11:41:00 +0530 Subject: [PATCH 1/3] add batch addition for file operations Signed-off-by: Luna --- src/rpm/builder.rs | 20 ++++++++++++++ src/tests.rs | 69 ++++++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 89 insertions(+) diff --git a/src/rpm/builder.rs b/src/rpm/builder.rs index 0c5f3631..f7a7d33f 100644 --- a/src/rpm/builder.rs +++ b/src/rpm/builder.rs @@ -306,6 +306,26 @@ impl PackageBuilder { Ok(self) } + pub fn with_files( + mut self, + files: Vec<(impl AsRef, impl Into)>, + ) -> Result { + for (source, options) in files { + let mut input = fs::File::open(source)?; + let mut content = Vec::new(); + input.read_to_end(&mut content)?; + let mut options = options.into(); + if options.inherit_permissions { + options.mode = (file_mode(&input)? as i32).into(); + } + + let modified_at = input.metadata()?.modified()?.try_into()?; + + self.add_data(content, modified_at, options)?; + } + Ok(self) + } + fn add_data( &mut self, content: Vec, diff --git a/src/tests.rs b/src/tests.rs index 6da8b8f7..0479c794 100644 --- a/src/tests.rs +++ b/src/tests.rs @@ -84,7 +84,76 @@ However, it does nothing.", assert_eq!(f.mode, FileMode::from(0o120644)); } }); + Ok(()) +} + +#[test] +fn test_rpm_batch_builder() -> Result<(), Box> { + let mut buff = std::io::Cursor::new(Vec::::new()); + let files = vec![ + ( + "Cargo.toml", + FileOptions::new("/etc/awesome/config.toml") + .is_config() + .is_no_replace(), + ), + ("Cargo.toml", FileOptions::new("/usr/bin/awesome")), + ( + "Cargo.toml", + // you can set a custom mode and custom user too + FileOptions::new("/etc/awesome/second.toml") + .mode(0o100744) + .caps("cap_sys_admin,cap_sys_ptrace=pe")? + .user("hugo"), + ), + ( + "./test_assets/empty_file_for_symlink_create", + FileOptions::new("/usr/bin/awesome_link") + .mode(0o120644) + .symlink("/usr/bin/awesome"), + ), + ]; + let pkg = PackageBuilder::new("test", "1.0.0", "MIT", "x86_64", "some awesome package") + .description( + "This is an awesome package. that was built in a batch. + +However, it does nothing.", + ) + .compression(rpm::CompressionType::Gzip) + .with_files(files)? + .pre_install_script("echo preinst") + .add_changelog_entry("me", "was awesome, eh?", 1_681_411_811) + .add_changelog_entry("you", "yeah, it was", 850_984_797) + .requires(Dependency::any("wget")) + .vendor("dummy vendor") + .url("dummy url") + .vcs("dummy vcs") + .build()?; + pkg.write(&mut buff)?; + + // check that generated packages has source rpm tag + // to be more compatibly recognized as RPM binary packages + pkg.metadata.get_source_rpm()?; + + pkg.verify_digests()?; + // check various metadata on the files + pkg.metadata.get_file_entries()?.iter().for_each(|f| { + if f.path.as_os_str() == "/etc/awesome/second.toml" { + assert_eq!( + f.clone().caps.unwrap(), + "cap_sys_ptrace,cap_sys_admin=ep".to_string() + ); + assert_eq!(f.ownership.user, "hugo".to_string()); + } else if f.path.as_os_str() == "/etc/awesome/config.toml" { + assert_eq!(f.caps, Some("".to_string())); + assert_eq!(f.flags, FileFlags::CONFIG | FileFlags::NOREPLACE); + } else if f.path.as_os_str() == "/usr/bin/awesome" { + assert_eq!(f.mode, FileMode::from(0o100644)); + } else if f.path.as_os_str() == "/usr/bin/awesome_link" { + assert_eq!(f.mode, FileMode::from(0o120644)); + } + }); Ok(()) } From 149a7a106e387c3d717ded666fb2fa74a3c041e5 Mon Sep 17 00:00:00 2001 From: Luna Date: Thu, 9 Nov 2023 13:50:41 +0530 Subject: [PATCH 2/3] add docs Signed-off-by: Luna --- src/rpm/builder.rs | 24 ++++++++++++++++++++++++ 1 file changed, 24 insertions(+) diff --git a/src/rpm/builder.rs b/src/rpm/builder.rs index f7a7d33f..ccab636b 100644 --- a/src/rpm/builder.rs +++ b/src/rpm/builder.rs @@ -306,6 +306,30 @@ impl PackageBuilder { Ok(self) } + /// Add multiple file to the package. + /// + /// ``` + /// # fn foo() -> Result<(), Box> { + /// let files =vec![( + /// "./awesome-config.toml", + /// rpm::FileOptions::new("/etc/awesome/config.toml").is_config(), + /// ), /// // file mode is inherited from source file + /// ( + /// "./awesome-bin", + /// rpm::FileOptions::new("/usr/bin/awesome"), + /// ), + /// ( + /// "./awesome-config.toml", + /// // you can set a custom mode, capabilities and custom user too + /// rpm::FileOptions::new("/etc/awesome/second.toml").mode(0o100744).caps("cap_sys_admin=pe")?.user("hugo"), + /// )]; + /// let pkg = rpm::PackageBuilder::new("foo", "1.0.0", "Apache-2.0", "x86_64", "some baz package") + /// .with_files(files)? + /// .build()?; + /// # Ok(()) + /// # } + /// ``` + pub fn with_files( mut self, files: Vec<(impl AsRef, impl Into)>, From de60bc7ce36e57008fa2c5431228302c3d792db3 Mon Sep 17 00:00:00 2001 From: Luna Date: Thu, 9 Nov 2023 14:20:15 +0530 Subject: [PATCH 3/3] add changelog entry --- CHANGELOG.md | 1 + 1 file changed, 1 insertion(+) diff --git a/CHANGELOG.md b/CHANGELOG.md index 840a5895..c58d10b6 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -23,6 +23,7 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0 of a `description`. A new method `PackageBuilder::description` can be used to set a detailed description for a package; if not set, the description defaults to the `summary`. +- Add method `PackageBuilder::with_files` to allow addition of multiple files to rpm at one shot - Add method `with_key_passphrase` to `signature::pgp::Signer`, to provide the passphrase when the PGP secret key is passphrase-protected. - Add method `is_no_replace` to `FileOptionsBuilder`, used to set the