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

Make RPMPackageMetadata::parse() and RPMPackageMetadata::open() public #140

Merged
merged 1 commit into from
May 16, 2023
Merged
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
3 changes: 3 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,9 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
- The default compression for building a package, if the compression is not overridden using
the above method, is now `Gzip` rather than `None`. This is chosen to keep package
sizes reasonable while maintaining maximum compatibility and minimizing computational cost.
- Exposed `RPMPackageMetadata::parse()` and `RPMPackageMetadata::open()` so that it is
possible to read only package metadata without loading the payload into memory. This saves
time and memory over reading the entire file.

### Changed

Expand Down
10 changes: 9 additions & 1 deletion src/rpm/package.rs
Original file line number Diff line number Diff line change
Expand Up @@ -301,7 +301,15 @@ impl RPMPackageMetadata {
})
}

pub(crate) fn parse<T: std::io::BufRead>(input: &mut T) -> Result<Self, RPMError> {
/// Open and parse RPMPackageMetadata from the file at the provided path
pub fn open<P: AsRef<Path>>(path: P) -> Result<Self, RPMError> {
let rpm_file = std::fs::File::open(path.as_ref())?;
let mut buf_reader = BufReader::new(rpm_file);
Self::parse(&mut buf_reader)
}

/// Parse RPMPackageMetadata from the provided reader
pub fn parse<T: std::io::BufRead>(input: &mut T) -> Result<Self, RPMError> {
let mut lead_buffer = [0; LEAD_SIZE as usize];
input.read_exact(&mut lead_buffer)?;
let lead = Lead::parse(&lead_buffer)?;
Expand Down
3 changes: 1 addition & 2 deletions tests/parsing.rs
Original file line number Diff line number Diff line change
Expand Up @@ -77,8 +77,7 @@ fn test_package_segment_boundaries() -> Result<(), Box<dyn std::error::Error>> {
#[test]
fn test_rpm_file_signatures() -> Result<(), Box<dyn std::error::Error>> {
let rpm_file_path = common::rpm_ima_signed_file_path();
let package = RPMPackage::open(rpm_file_path)?;
let metadata = &package.metadata;
let metadata = RPMPackageMetadata::open(rpm_file_path)?;

let signatures = metadata.get_file_ima_signatures()?;

Expand Down