src/import-rpmdb.c
author J. Ali Harlow <ali@juiblex.co.uk>
Wed Apr 29 17:00:01 2009 +0100 (2009-04-29)
changeset 361 2523d03a840e
parent 325 73393734833c
child 369 f8c27fe9fe63
permissions -rw-r--r--
Add support for preloading lua modules. This is useful both when
providing lua bindings to applications based on librazor and when
producing static binaries using librazor (where otherwise the lua
POSIX library would need to be included as an additional dynamic
object).
     1 /*
     2  * Copyright (C) 2008  Kristian Høgsberg <krh@redhat.com>
     3  * Copyright (C) 2008  Red Hat, Inc
     4  *
     5  * This program is free software; you can redistribute it and/or modify
     6  * it under the terms of the GNU General Public License as published by
     7  * the Free Software Foundation; either version 2 of the License, or
     8  * (at your option) any later version.
     9  *
    10  * This program is distributed in the hope that it will be useful,
    11  * but WITHOUT ANY WARRANTY; without even the implied warranty of
    12  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
    13  * GNU General Public License for more details.
    14  *
    15  * You should have received a copy of the GNU General Public License along
    16  * with this program; if not, write to the Free Software Foundation, Inc.,
    17  * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
    18  */
    19 
    20 #include <stdio.h>
    21 #include <stddef.h>
    22 #include <string.h>
    23 #include <fcntl.h>
    24 #include <limits.h>
    25 #include <rpm/rpmlib.h>
    26 #include <rpm/rpmdb.h>
    27 
    28 #include "razor.h"
    29 
    30 union rpm_entry {
    31 	void *p;
    32 	char *string;
    33 	char **list;
    34 	uint32_t *flags;
    35 	uint32_t integer;
    36 };
    37 
    38 static uint32_t
    39 rpm_to_razor_flags(uint32_t flags)
    40 {
    41 	uint32_t razor_flags;
    42 
    43 	razor_flags = 0;
    44 	if (flags & RPMSENSE_LESS)
    45 		razor_flags |= RAZOR_PROPERTY_LESS;
    46 	if (flags & RPMSENSE_EQUAL)
    47 		razor_flags |= RAZOR_PROPERTY_EQUAL;
    48 	if (flags & RPMSENSE_GREATER)
    49 		razor_flags |= RAZOR_PROPERTY_GREATER;
    50 
    51 	if (flags & RPMSENSE_SCRIPT_PRE)
    52 		razor_flags |= RAZOR_PROPERTY_PRE;
    53 	if (flags & RPMSENSE_SCRIPT_POST)
    54 		razor_flags |= RAZOR_PROPERTY_POST;
    55 	if (flags & RPMSENSE_SCRIPT_PREUN)
    56 		razor_flags |= RAZOR_PROPERTY_PREUN;
    57 	if (flags & RPMSENSE_SCRIPT_POSTUN)
    58 		razor_flags |= RAZOR_PROPERTY_POSTUN;
    59 
    60 	return razor_flags;
    61 }
    62 
    63 static void
    64 add_properties(struct razor_importer *importer,
    65 	       uint32_t type_flags, Header h,
    66 	       int32_t name_tag, int32_t version_tag, int32_t flags_tag)
    67 {
    68 	union rpm_entry names, versions, flags;
    69 	int32_t i, type, count;
    70 
    71 	headerGetEntry(h, name_tag, &type, &names.p, &count);
    72 	headerGetEntry(h, version_tag, &type, &versions.p, &count);
    73 	headerGetEntry(h, flags_tag, &type, &flags.p, &count);
    74 
    75 	for (i = 0; i < count; i++)
    76 		razor_importer_add_property(importer,
    77 					    names.list[i],
    78 					    rpm_to_razor_flags (flags.flags[i]) | type_flags,
    79 					    versions.list[i]);
    80 }
    81 
    82 struct razor_set *
    83 razor_set_create_from_rpmdb(void)
    84 {
    85 	struct razor_importer *importer;
    86 	rpmdbMatchIterator iter;
    87 	Header h;
    88 	int32_t type, count, i;
    89 	union rpm_entry name, epoch, version, release, arch;
    90 	union rpm_entry summary, description, url, license;
    91 	union rpm_entry basenames, dirnames, dirindexes;
    92 	char filename[PATH_MAX], evr[128], buf[16];
    93 	rpmdb db;
    94 	int imported_count = 0;
    95 
    96 	rpmReadConfigFiles(NULL, NULL);
    97 
    98 	if (rpmdbOpen("", &db, O_RDONLY, 0644) != 0) {
    99 		fprintf(stderr, "cannot open rpm database\n");
   100 		exit(1);
   101 	}
   102 
   103 	importer = razor_importer_create();
   104 
   105 	iter = rpmdbInitIterator(db, 0, NULL, 0);
   106 	while (h = rpmdbNextIterator(iter), h != NULL) {
   107 		headerGetEntry(h, RPMTAG_NAME, &type, &name.p, &count);
   108 		headerGetEntry(h, RPMTAG_EPOCH, &type, &epoch.p, &count);
   109 		headerGetEntry(h, RPMTAG_VERSION, &type, &version.p, &count);
   110 		headerGetEntry(h, RPMTAG_RELEASE, &type, &release.p, &count);
   111 		headerGetEntry(h, RPMTAG_ARCH, &type, &arch.p, &count);
   112 		headerGetEntry(h, RPMTAG_SUMMARY, &type, &summary.p, &count);
   113 		headerGetEntry(h, RPMTAG_DESCRIPTION, &type, &description.p,
   114 			       &count);
   115 		headerGetEntry(h, RPMTAG_URL, &type, &url.p, &count);
   116 		headerGetEntry(h, RPMTAG_LICENSE, &type, &license.p, &count);
   117 
   118 		if (epoch.flags != NULL) {
   119 			snprintf(buf, sizeof buf, "%u", *epoch.flags);
   120 			razor_build_evr(evr, sizeof evr,
   121 					buf, version.string, release.string);
   122 		} else {
   123 			razor_build_evr(evr, sizeof evr,
   124 					NULL, version.string, release.string);
   125 		}
   126 
   127 		razor_importer_begin_package(importer,
   128 					     name.string, evr, arch.string);
   129 		razor_importer_add_details(importer, summary.string,
   130 					   description.string, url.string,
   131 					   license.string);
   132 
   133 		add_properties(importer, RAZOR_PROPERTY_REQUIRES, h,
   134 			       RPMTAG_REQUIRENAME,
   135 			       RPMTAG_REQUIREVERSION,
   136 			       RPMTAG_REQUIREFLAGS);
   137 
   138 		add_properties(importer, RAZOR_PROPERTY_PROVIDES, h,
   139 			       RPMTAG_PROVIDENAME,
   140 			       RPMTAG_PROVIDEVERSION,
   141 			       RPMTAG_PROVIDEFLAGS);
   142 
   143 		add_properties(importer, RAZOR_PROPERTY_OBSOLETES, h,
   144 			       RPMTAG_OBSOLETENAME,
   145 			       RPMTAG_OBSOLETEVERSION,
   146 			       RPMTAG_OBSOLETEFLAGS);
   147 
   148 		add_properties(importer, RAZOR_PROPERTY_CONFLICTS, h,
   149 			       RPMTAG_CONFLICTNAME,
   150 			       RPMTAG_CONFLICTVERSION,
   151 			       RPMTAG_CONFLICTFLAGS);
   152 
   153 		headerGetEntry(h, RPMTAG_BASENAMES, &type,
   154 			       &basenames.p, &count);
   155 		headerGetEntry(h, RPMTAG_DIRNAMES, &type,
   156 			       &dirnames.p, &count);
   157 		headerGetEntry(h, RPMTAG_DIRINDEXES, &type,
   158 			       &dirindexes.p, &count);
   159 		for (i = 0; i < count; i++) {
   160 			snprintf(filename, sizeof filename, "%s%s",
   161 				 dirnames.list[dirindexes.flags[i]],
   162 				 basenames.list[i]);
   163 			razor_importer_add_file(importer, filename);
   164 		}
   165 
   166 		razor_importer_finish_package(importer);
   167 
   168 		printf("\rimporting %d", ++imported_count);
   169 		fflush(stdout);
   170 	}
   171 
   172 	rpmdbClose(db);
   173 
   174 	printf("\nsaving\n");
   175 	return razor_importer_finish(importer);
   176 }