Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

add PackageBuilder::with_files too allows batch addition of files to a rpm #194

Open
wants to merge 3 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
44 changes: 44 additions & 0 deletions src/rpm/builder.rs
Original file line number Diff line number Diff line change
Expand Up @@ -306,6 +306,50 @@ impl PackageBuilder {
Ok(self)
}

/// Add multiple file to the package.
///
/// ```
/// # fn foo() -> Result<(), Box<dyn std::error::Error>> {
/// 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<Path>, impl Into<FileOptions>)>,
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Could we make these explicit generics? It allows the user to specify the types in case of ambiguities.

Copy link
Contributor

@drahnr drahnr Mar 4, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Note: I am not a particular fan of tuples, in this particular case, adding a type just for the sake of a public function input might be overkill.

) -> Result<Self, Error> {
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<u8>,
Expand Down
69 changes: 69 additions & 0 deletions src/tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -84,7 +84,76 @@ However, it does nothing.",
assert_eq!(f.mode, FileMode::from(0o120644));
}
});
Ok(())
}

#[test]
fn test_rpm_batch_builder() -> Result<(), Box<dyn std::error::Error>> {
let mut buff = std::io::Cursor::new(Vec::<u8>::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(())
}

Expand Down