Skip to content

Commit

Permalink
Fix C-API calls to resolve some failures with local/offline databases (
Browse files Browse the repository at this point in the history
…#45)

- Making the codebase work with offline databases.
- Passing DBIndexTag to rpmtsInitIterator instead of the previous Tag. Added tests
  • Loading branch information
Tomer Lev authored Nov 12, 2023
1 parent d06b744 commit cc7e7c9
Show file tree
Hide file tree
Showing 33 changed files with 205 additions and 385 deletions.
262 changes: 25 additions & 237 deletions Cargo.lock

Large diffs are not rendered by default.

28 changes: 12 additions & 16 deletions Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,22 +1,22 @@
[package]
name = "librpm"
version = "0.1.1"
description = "RPM Package Manager library binding with an idiomatic Rust API"
license = "MPL-2.0"
authors = ["Tony Arcieri <tony@iqlusion.io>"]
categories = ["api-bindings", "os", "parsing"]
keywords = ["rpm", "linux", "redhat", "fedora", "centos"]
readme = "README.md"
homepage = "https://github.com/rpm-software-management/librpm.rs/"
name = "librpm"
version = "0.2.0-pre"
description = "RPM Package Manager library binding with an idiomatic Rust API"
license = "MPL-2.0"
authors = ["Tony Arcieri <tony@iqlusion.io>"]
categories = ["api-bindings", "os", "parsing"]
keywords = ["rpm", "linux", "redhat", "fedora", "centos"]
readme = "README.md"
homepage = "https://github.com/rpm-software-management/librpm.rs/"
documentation = "https://rustrpm.org/librpm/"
edition = "2018"
edition = "2021"

[badges]
maintenance = { status = "passively-maintained" }

[dependencies]
once_cell = "1.8.0"
librpm-sys = { version = "0.1", path = "librpm-sys" }
librpm-sys = { version = "0.2.0-pre", path = "librpm-sys" }
streaming-iterator = "0.1.5"

[features]
Expand All @@ -25,8 +25,4 @@ librpm-4-14 = []
default = ["librpm-4-14"]

[workspace]
members = [
"librpm-sys",
"librpmbuild-sys",
"librpmsign-sys"
]
members = ["librpm-sys", "librpmbuild-sys", "librpmsign-sys"]
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
# librpm.rs: RPM Package Manager binding for Rust

[![Crate][crate-image]][crate-link]
[![Build Status][build-image]][build-link]
[![Build Status](https://github.com/rpm-software-management/librpm.rs/actions/workflows/ci.yml/badge.svg)](https://github.com/rpm-software-management/librpm.rs/actions/workflows/ci.yml)
[![MPL-2.0 Licensed][license-image]][license-link]

The [librpm] C library (available in the `rpm-devel` RPM package) exposes a
Expand Down
2 changes: 1 addition & 1 deletion librpm-sys/Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
[package]
name = "librpm-sys"
description = "RPM Package Manager library FFI bindings (librpm.so)"
version = "0.1.1"
version = "0.2.0-pre"
license = "MPL-2.0"
authors = ["Tony Arcieri <tony@iqlusion.io>"]
categories = ["external-ffi-bindings", "os"]
Expand Down
25 changes: 12 additions & 13 deletions librpmbuild-sys/Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,21 +1,20 @@
[package]
name = "librpmbuild-sys"
description = "rpmbuild library FFI bindings (librpmbuild.so)"
version = "0.1.1"
license = "MPL-2.0"
authors = ["Tony Arcieri <tony@iqlusion.io>"]
categories = ["external-ffi-bindings", "os"]
keywords = ["rpm", "linux", "redhat", "fedora", "centos"]
readme = "README.md"
homepage = "https://github.com/rpm-software-management/librpm.rs/"
repository = "https://github.com/rpm-software-management/librpm.rs/tree/master/librpmbuild-sys"
name = "librpmbuild-sys"
description = "rpmbuild library FFI bindings (librpmbuild.so)"
version = "0.2.0-pre"
license = "MPL-2.0"
authors = ["Tony Arcieri <tony@iqlusion.io>"]
categories = ["external-ffi-bindings", "os"]
keywords = ["rpm", "linux", "redhat", "fedora", "centos"]
readme = "README.md"
homepage = "https://github.com/rpm-software-management/librpm.rs/"
repository = "https://github.com/rpm-software-management/librpm.rs/tree/master/librpmbuild-sys"
documentation = "https://rustrpm.org/librpmbuild_sys/"
edition = "2018"
edition = "2021"

[badges]
travis-ci = { repository = "rpm-software-management/librpm.rs" }
maintenance = { status = "passively-maintained" }

[build-dependencies]
bindgen = "0.59.1"

bindgen = "0.69.1"
17 changes: 3 additions & 14 deletions src/db.rs
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,8 @@
//! println!("package version: {}", package.version());
//! ```

use crate::internal::{iterator::MatchIterator, tag::Tag};
use crate::internal::iterator::MatchIterator;
use crate::internal::tag::DBIndexTag;
use crate::package::Package;
use streaming_iterator::StreamingIterator;

Expand All @@ -60,18 +61,6 @@ impl Iterator for Iter {
pub enum Index {
/// Search by package name.
Name,

/// Search by package version.
Version,

/// Search by package license.
License,

/// Search by package summary.
Summary,

/// Search by package description.
Description,
}

impl Index {
Expand All @@ -83,7 +72,7 @@ impl Index {

/// Find all packages installed on the local system.
pub fn installed_packages() -> Iter {
Iter(MatchIterator::new(Tag::NAME, None))
Iter(MatchIterator::new(DBIndexTag::PACKAGES, None))
}

/// Find installed packages with a search key that exactly matches the given tag.
Expand Down
4 changes: 3 additions & 1 deletion src/internal/header.rs
Original file line number Diff line number Diff line change
Expand Up @@ -74,7 +74,9 @@ impl Header {
pub(crate) fn to_package(&self) -> Package {
Package {
name: self.get(Tag::NAME).unwrap().as_str().unwrap().to_owned(),
epoch: self.get(Tag::EPOCH).map(|d| d.as_str().unwrap().to_owned()),
epoch: self
.get(Tag::EPOCH)
.map(|d| d.to_int32().unwrap().to_owned()),
version: self.get(Tag::VERSION).unwrap().as_str().unwrap().to_owned(),
release: self.get(Tag::RELEASE).unwrap().as_str().unwrap().to_owned(),
arch: self.get(Tag::ARCH).map(|d| d.as_str().unwrap().to_owned()),
Expand Down
4 changes: 2 additions & 2 deletions src/internal/iterator.rs
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@

//! Iterators for matches in the RPM database

use super::{header::Header, tag::Tag, ts::GlobalTS};
use super::{header::Header, tag::DBIndexTag, ts::GlobalTS};
#[cfg(feature = "regex")]
use regex::Regex;
use std::{os::raw::c_void, ptr};
Expand All @@ -43,7 +43,7 @@ pub(crate) struct MatchIterator {
impl MatchIterator {
/// Create a new `MatchIterator` for the current RPM database, searching
/// by the (optionally) given search key.
pub(crate) fn new(tag: Tag, key_opt: Option<&str>) -> Self {
pub(crate) fn new(tag: DBIndexTag, key_opt: Option<&str>) -> Self {
let mut txn = GlobalTS::create();
let next_item = None;
let finished = false;
Expand Down
8 changes: 2 additions & 6 deletions src/internal/tag.rs
Original file line number Diff line number Diff line change
Expand Up @@ -275,14 +275,10 @@ pub enum Tag {
FILENLINKS = librpm_sys::rpmTag_e_RPMTAG_FILENLINKS as isize,
}

impl From<Index> for Tag {
impl From<Index> for DBIndexTag {
fn from(i: Index) -> Self {
match i {
Index::Name => Tag::NAME,
Index::Version => Tag::VERSION,
Index::License => Tag::LICENSE,
Index::Summary => Tag::SUMMARY,
Index::Description => Tag::DESCRIPTION,
Index::Name => DBIndexTag::NAME,
}
}
}
Expand Down
6 changes: 3 additions & 3 deletions src/package.rs
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ use std::{fmt, time};
#[derive(Clone, Debug, Eq, PartialEq, Hash)]
pub struct Package {
pub(crate) name: String,
pub(crate) epoch: Option<String>,
pub(crate) epoch: Option<i32>,
pub(crate) version: String,
pub(crate) release: String,
pub(crate) arch: Option<String>,
Expand All @@ -40,8 +40,8 @@ impl Package {
}

/// Epoch of the package
pub fn epoch(&self) -> Option<&str> {
self.epoch.as_deref()
pub fn epoch(&self) -> Option<i32> {
self.epoch
}

/// Version of the package
Expand Down
12 changes: 12 additions & 0 deletions testdata/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
# Test Data Files

## Create test files from the command line

Within this directory, run the following commands to create new test files:

```bash
# Fetching the centos:7 rpm databases
id=$(docker create centos:7)
docker cp $id:/var/lib/rpm centos7
docker rm -v $id
```
Empty file added testdata/centos7/.dbenv.lock
Empty file.
Empty file added testdata/centos7/.rpm.lock
Empty file.
Binary file added testdata/centos7/Basenames
Binary file not shown.
Binary file added testdata/centos7/Conflictname
Binary file not shown.
Binary file added testdata/centos7/Dirnames
Binary file not shown.
Binary file added testdata/centos7/Group
Binary file not shown.
Binary file added testdata/centos7/Installtid
Binary file not shown.
Binary file added testdata/centos7/Name
Binary file not shown.
Binary file added testdata/centos7/Obsoletename
Binary file not shown.
Binary file added testdata/centos7/Packages
Binary file not shown.
Binary file added testdata/centos7/Providename
Binary file not shown.
Binary file added testdata/centos7/Requirename
Binary file not shown.
Binary file added testdata/centos7/Sha1header
Binary file not shown.
Binary file added testdata/centos7/Sigmd5
Binary file not shown.
Binary file added testdata/centos7/Triggername
Binary file not shown.
Binary file added testdata/centos7/__db.001
Binary file not shown.
Binary file added testdata/centos7/__db.002
Binary file not shown.
Binary file added testdata/centos7/__db.003
Binary file not shown.
19 changes: 19 additions & 0 deletions tests/common/mod.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
use std::{
path::{Path, PathBuf},
sync::Once,
};

use librpm::config;

static CONFIGURE: Once = Once::new();

// Read the default config
pub fn configure() {
CONFIGURE.call_once(|| {
config::read_file(None).unwrap();
});
}

pub fn get_assets_path() -> PathBuf {
Path::new(env!("CARGO_MANIFEST_DIR")).join("testdata")
}
91 changes: 0 additions & 91 deletions tests/integration.rs

This file was deleted.

26 changes: 26 additions & 0 deletions tests/test-centos7.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
use librpm::{config::set_db_path, db::installed_packages, Package};

mod common;

#[test]
fn test_centos_7_rpm_database() {
common::configure();
set_db_path(&common::get_assets_path().join("centos7")).unwrap();

let mut packages: Vec<Package> = installed_packages().collect();
packages.sort_by_key(|p| p.name().to_string());

assert_eq!(packages.len(), 148);
let sample_package = &packages[0];
assert_eq!(sample_package.name(), "acl");
assert_eq!(sample_package.epoch(), None);
assert_eq!(sample_package.version(), "2.2.51");
assert_eq!(sample_package.release(), "15.el7");
assert_eq!(sample_package.arch(), Some("x86_64"));
assert_eq!(sample_package.license(), "GPLv2+");
assert_eq!(sample_package.summary(), "Access control list utilities");
assert_eq!(
sample_package.description(),
"This package contains the getfacl and setfacl utilities needed for\nmanipulating access control lists."
);
}
Loading

0 comments on commit cc7e7c9

Please sign in to comment.