Add importer for system rpm database.
authorKristian Høgsberg <krh@redhat.com>
Wed, 19 Sep 2007 18:09:03 +0000 (14:09 -0400)
committerKristian Høgsberg <krh@redhat.com>
Wed, 19 Sep 2007 18:09:03 +0000 (14:09 -0400)
Makefile
import.c
razor.c
razor.h

index 43b5620..7effe2e 100644 (file)
--- a/Makefile
+++ b/Makefile
@@ -1,5 +1,5 @@
 CFLAGS = -Wall -g -O2
-LDLIBS = -lexpat -g
+LDLIBS = -lexpat -g -lrpm
 
 razor : razor.o import.o sha1.o
 
index a5ccc98..75a675a 100644 (file)
--- a/import.c
+++ b/import.c
@@ -9,6 +9,8 @@
 #include <errno.h>
 
 #include <expat.h>
+#include <rpm/rpmlib.h>
+#include <rpm/rpmdb.h>
 #include "sha1.h"
 #include "razor.h"
 
@@ -257,3 +259,62 @@ razor_set_create_from_yum_filelist(int fd)
 
        return razor_finish_import(&ctx.ctx);
 }
+
+struct razor_set *
+razor_set_create_from_rpmdb(void)
+{
+       struct import_context ctx;
+       rpmdbMatchIterator iter;
+       Header h;
+       int_32 type, count, i;
+       char *name, *version, *release;
+       char **properties, **property_versions;
+       rpmdb db;
+
+       rpmReadConfigFiles(NULL, NULL);
+
+       if (rpmdbOpen("", &db, O_RDONLY, 0644) != 0) {
+               fprintf(stderr, "cannot open rpm database\n");
+               exit(1);
+       }
+
+       razor_prepare_import(&ctx);
+
+       iter = rpmdbInitIterator(db, 0, NULL, 0);
+       while (h = rpmdbNextIterator(iter), h != NULL) {
+               headerGetEntry(h, RPMTAG_NAME, &type,
+                              (void **) &name, &count);
+               headerGetEntry(h, RPMTAG_VERSION, &type,
+                              (void **) &version, &count);
+               headerGetEntry(h, RPMTAG_RELEASE, &type,
+                              (void **) &release, &count);
+               import_context_add_package(&ctx, name, version);
+
+
+               headerGetEntry(h, RPMTAG_REQUIRES, &type,
+                              (void **) &properties, &count);
+               headerGetEntry(h, RPMTAG_REQUIREVERSION, &type,
+                              (void **) &property_versions, &count);
+               for (i = 0; i < count; i++)
+                       import_context_add_property(&ctx,
+                                                   &ctx.requires,
+                                                   properties[i],
+                                                   property_versions[i]);
+
+               headerGetEntry(h, RPMTAG_PROVIDES, &type,
+                              (void **) &properties, &count);
+               headerGetEntry(h, RPMTAG_PROVIDEVERSION, &type,
+                              (void **) &property_versions, &count);
+               for (i = 0; i < count; i++)
+                       import_context_add_property(&ctx,
+                                                   &ctx.provides,
+                                                   properties[i],
+                                                   property_versions[i]);
+
+               import_context_finish_package(&ctx);
+       }
+
+       rpmdbClose(db);
+
+       return razor_finish_import(&ctx);
+}
diff --git a/razor.c b/razor.c
index 541d0c1..d6a7002 100644 (file)
--- a/razor.c
+++ b/razor.c
@@ -928,6 +928,13 @@ main(int argc, const char *argv[])
                razor_set_write(set, rawhide_repo_filename);
                razor_set_destroy(set);
                printf("wrote %s\n", rawhide_repo_filename);
+       } else if (strcmp(argv[1], "import-rpmdb") == 0) {
+               set = razor_set_create_from_rpmdb();
+               if (set == NULL)
+                       return 1;
+               razor_set_write(set, repo_filename);
+               razor_set_destroy(set);
+               printf("wrote %s\n", repo_filename);
        } else if (strcmp(argv[1], "validate") == 0) {
                set = razor_set_open(repo_filename);
                if (set == NULL)
diff --git a/razor.h b/razor.h
index 8912ad6..6bb2542 100644 (file)
--- a/razor.h
+++ b/razor.h
@@ -79,5 +79,6 @@ struct razor_set *razor_finish_import(struct import_context *ctx);
 
 struct razor_set *razor_import_rzr_files(int count, const char **files);
 struct razor_set *razor_set_create_from_yum_filelist(int fd);
+struct razor_set *razor_set_create_from_rpmdb(void);
 
 #endif /* _RAZOR_H_ */