src/import-rpmdb.c
author Kristian H?gsberg <krh@redhat.com>
Thu Jun 19 00:32:24 2008 -0400 (2008-06-19)
changeset 245 0311b3fe72e3
child 247 63444a10fb8e
permissions -rw-r--r--
Make razor install work again after the razor_root changes.
     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 <rpm/rpmlib.h>
    25 #include <rpm/rpmdb.h>
    26 
    27 #include "razor.h"
    28 
    29 union rpm_entry {
    30 	void *p;
    31 	char *string;
    32 	char **list;
    33 	uint_32 *flags;
    34 	uint_32 integer;
    35 };
    36 
    37 static enum razor_version_relation
    38 rpm_to_razor_flags (uint_32 flags)
    39 {
    40 	switch (flags & (RPMSENSE_LESS | RPMSENSE_EQUAL | RPMSENSE_GREATER)) {
    41 	case RPMSENSE_LESS:
    42 		return RAZOR_VERSION_LESS;
    43 	case RPMSENSE_LESS|RPMSENSE_EQUAL:
    44 		return RAZOR_VERSION_LESS_OR_EQUAL;
    45 	case RPMSENSE_EQUAL:
    46 		return RAZOR_VERSION_EQUAL;
    47 	case RPMSENSE_GREATER|RPMSENSE_EQUAL:
    48 		return RAZOR_VERSION_GREATER_OR_EQUAL;
    49 	case RPMSENSE_GREATER:
    50 		return RAZOR_VERSION_GREATER;
    51 	}
    52 
    53 	/* FIXME? */
    54 	return RAZOR_VERSION_EQUAL;
    55 }
    56 
    57 static void
    58 add_properties(struct razor_importer *importer,
    59 	       enum razor_property_type property_type,
    60 	       Header h, int_32 name_tag, int_32 version_tag, int_32 flags_tag)
    61 {
    62 	union rpm_entry names, versions, flags;
    63 	int_32 i, type, count;
    64 
    65 	headerGetEntry(h, name_tag, &type, &names.p, &count);
    66 	headerGetEntry(h, version_tag, &type, &versions.p, &count);
    67 	headerGetEntry(h, flags_tag, &type, &flags.p, &count);
    68 
    69 	for (i = 0; i < count; i++)
    70 		razor_importer_add_property(importer,
    71 					    names.list[i],
    72 					    rpm_to_razor_flags (flags.flags[i]),
    73 					    versions.list[i],
    74 					    property_type);
    75 }
    76 
    77 struct razor_set *
    78 razor_set_create_from_rpmdb(void)
    79 {
    80 	struct razor_importer *importer;
    81 	rpmdbMatchIterator iter;
    82 	Header h;
    83 	int_32 type, count, i;
    84 	union rpm_entry name, epoch, version, release, arch;
    85 	union rpm_entry basenames, dirnames, dirindexes;
    86 	char filename[PATH_MAX], evr[128], buf[16];
    87 	rpmdb db;
    88 
    89 	rpmReadConfigFiles(NULL, NULL);
    90 
    91 	if (rpmdbOpen("", &db, O_RDONLY, 0644) != 0) {
    92 		fprintf(stderr, "cannot open rpm database\n");
    93 		exit(1);
    94 	}
    95 
    96 	importer = razor_importer_new();
    97 
    98 	iter = rpmdbInitIterator(db, 0, NULL, 0);
    99 	while (h = rpmdbNextIterator(iter), h != NULL) {
   100 		headerGetEntry(h, RPMTAG_NAME, &type, &name.p, &count);
   101 		headerGetEntry(h, RPMTAG_EPOCH, &type, &epoch.p, &count);
   102 		headerGetEntry(h, RPMTAG_VERSION, &type, &version.p, &count);
   103 		headerGetEntry(h, RPMTAG_RELEASE, &type, &release.p, &count);
   104 		headerGetEntry(h, RPMTAG_ARCH, &type, &arch.p, &count);
   105 
   106 		if (epoch.flags != NULL) {
   107 			snprintf(buf, sizeof buf, "%u", *epoch.flags);
   108 			razor_build_evr(evr, sizeof evr,
   109 					buf, version.string, release.string);
   110 		} else {
   111 			razor_build_evr(evr, sizeof evr,
   112 					NULL, version.string, release.string);
   113 		}
   114 
   115 		razor_importer_begin_package(importer,
   116 					     name.string, evr, arch.string);
   117 
   118 		add_properties(importer, RAZOR_PROPERTY_REQUIRES, h,
   119 			       RPMTAG_REQUIRENAME,
   120 			       RPMTAG_REQUIREVERSION,
   121 			       RPMTAG_REQUIREFLAGS);
   122 
   123 		add_properties(importer, RAZOR_PROPERTY_PROVIDES, h,
   124 			       RPMTAG_PROVIDENAME,
   125 			       RPMTAG_PROVIDEVERSION,
   126 			       RPMTAG_PROVIDEFLAGS);
   127 
   128 		add_properties(importer, RAZOR_PROPERTY_OBSOLETES, h,
   129 			       RPMTAG_OBSOLETENAME,
   130 			       RPMTAG_OBSOLETEVERSION,
   131 			       RPMTAG_OBSOLETEFLAGS);
   132 
   133 		add_properties(importer, RAZOR_PROPERTY_CONFLICTS, h,
   134 			       RPMTAG_CONFLICTNAME,
   135 			       RPMTAG_CONFLICTVERSION,
   136 			       RPMTAG_CONFLICTFLAGS);
   137 
   138 		headerGetEntry(h, RPMTAG_BASENAMES, &type,
   139 			       &basenames.p, &count);
   140 		headerGetEntry(h, RPMTAG_DIRNAMES, &type,
   141 			       &dirnames.p, &count);
   142 		headerGetEntry(h, RPMTAG_DIRINDEXES, &type,
   143 			       &dirindexes.p, &count);
   144 		for (i = 0; i < count; i++) {
   145 			snprintf(filename, sizeof filename, "%s%s",
   146 				 dirnames.list[dirindexes.flags[i]],
   147 				 basenames.list[i]);
   148 			razor_importer_add_file(importer, filename);
   149 		}
   150 
   151 		razor_importer_finish_package(importer);
   152 	}
   153 
   154 	rpmdbClose(db);
   155 
   156 	return razor_importer_finish(importer);
   157 }