Is there any proper documentation with examples for librpm 6.x? #4168
|
Hello, I'm trying to use librpm mixed into a Go program to get a list of all the packages installed on the system. The documentation at https://rpm-software-management.github.io/rpm/api/index.html I ran across this documentation but it's from the year 2000 and very much out of date: Is there any documentation with examples, or at least an outline of the process of actually querying the rpmDB with this library, anywhere? I just need to dump all the installed RPMs with their names and versions. Cheers. |
Replies: 3 comments 3 replies
|
No, we don't have a tutorial. We have a bunch of Python examples that might help you navigate the C API though: https://github.com/rpm-software-management/rpm/tree/master/python/examples For C examples, you'll need to go read other people's code. |
|
Not ideal, but thanks. Here's a full example for anyone else looking for a pure C implementation. #include <rpm/rpmts.h>
#include <rpm/rpmdb.h>
// #include <rpm/header.h>
#include <rpm/rpmcli.h>
#include <stdio.h>
#include <fcntl.h>
int main(void)
{
rpmReadConfigFiles(NULL, NULL);
rpmts ts = rpmtsCreate();
if (!ts) {
fprintf(stderr, "Failed to create rpmts\n");
return 1;
}
/* Open DB in read-only mode */
if (rpmtsOpenDB(ts, O_RDONLY) != 0) {
fprintf(stderr, "Failed to open RPM database\n");
rpmtsFree(ts);
return 1;
}
/* NULL keyp == sequential iteration over all installed packages */
rpmdbMatchIterator mi = rpmtsInitIterator(ts, RPMDBI_PACKAGES, NULL, 0);
if (!mi) {
fprintf(stderr, "Failed to create match iterator\n");
rpmtsFree(ts);
return 1;
}
Header h;
while ((h = rpmdbNextIterator(mi)) != NULL) {
const char *name = headerGetString(h, RPMTAG_NAME);
const char *epoch = headerGetString(h, RPMTAG_EPOCH);
const char *version = headerGetString(h, RPMTAG_VERSION);
const char *release = headerGetString(h, RPMTAG_RELEASE);
if (name && version && release)
printf("%s-%s-%s-%s\n", name, epoch, version, release);
}
/* Cleanup */
rpmdbFreeIterator(mi);
rpmtsFree(ts);
return 0;
} |
|
Note that if you just want formatted strings from a header, that's easier with headerFormat(): Or for the traditional n-[e:]vr formatted string, just ...or if you prefer a variant that will catch errors at compile-time, the N(E)VR(A) variants are also available as tag extensions, eg RPMTAG_NEVR, RPMTAG_NVR, RPMTAG_NVRA, and so on: |
No, we don't have a tutorial.
We have a bunch of Python examples that might help you navigate the C API though: https://github.com/rpm-software-management/rpm/tree/master/python/examples
For C examples, you'll need to go read other people's code.