src/import-rpmdb.c
author Kristian H?gsberg <krh@redhat.com>
Fri Jun 20 21:56:43 2008 -0400 (2008-06-20)
changeset 254 ccb1c11968ab
parent 247 63444a10fb8e
child 259 5b0601d184ed
permissions -rw-r--r--
Introduce install/remove iterators.

These iterator constructors lets you pass in two sets and creates
an iterator for the packages to remove or the packages to install.
The iterators will step through the packages in a sequence that respects
the pre, post, preun and postun modifiers.

Right now, the install order isn't actually implemented, this patch just
implements the API changes and updates the applications.
     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 uint32_t
    38 rpm_to_razor_flags(uint32_t flags)
    39 {
    40 	uint32_t razor_flags;
    41 
    42 	razor_flags = 0;
    43 	if (flags & RPMSENSE_LESS)
    44 		razor_flags |= RAZOR_PROPERTY_LESS;
    45 	if (flags & RPMSENSE_EQUAL)
    46 		razor_flags |= RAZOR_PROPERTY_EQUAL;
    47 	if (flags & RPMSENSE_GREATER)
    48 		razor_flags |= RAZOR_PROPERTY_GREATER;
    49 
    50 	if (flags & RPMSENSE_SCRIPT_PRE)
    51 		razor_flags |= RAZOR_PROPERTY_PRE;
    52 	if (flags & RPMSENSE_SCRIPT_POST)
    53 		razor_flags |= RAZOR_PROPERTY_POST;
    54 	if (flags & RPMSENSE_SCRIPT_PREUN)
    55 		razor_flags |= RAZOR_PROPERTY_PREUN;
    56 	if (flags & RPMSENSE_SCRIPT_POSTUN)
    57 		razor_flags |= RAZOR_PROPERTY_POSTUN;
    58 
    59 	return razor_flags;
    60 }
    61 
    62 static void
    63 add_properties(struct razor_importer *importer,
    64 	       uint32_t type_flags,
    65 	       Header h, int_32 name_tag, int_32 version_tag, int_32 flags_tag)
    66 {
    67 	union rpm_entry names, versions, flags;
    68 	int_32 i, type, count;
    69 
    70 	headerGetEntry(h, name_tag, &type, &names.p, &count);
    71 	headerGetEntry(h, version_tag, &type, &versions.p, &count);
    72 	headerGetEntry(h, flags_tag, &type, &flags.p, &count);
    73 
    74 	for (i = 0; i < count; i++)
    75 		razor_importer_add_property(importer,
    76 					    names.list[i],
    77 					    rpm_to_razor_flags (flags.flags[i]) | type_flags,
    78 					    versions.list[i]);
    79 }
    80 
    81 struct razor_set *
    82 razor_set_create_from_rpmdb(void)
    83 {
    84 	struct razor_importer *importer;
    85 	rpmdbMatchIterator iter;
    86 	Header h;
    87 	int_32 type, count, i;
    88 	union rpm_entry name, epoch, version, release, arch;
    89 	union rpm_entry basenames, dirnames, dirindexes;
    90 	char filename[PATH_MAX], evr[128], buf[16];
    91 	rpmdb db;
    92 
    93 	rpmReadConfigFiles(NULL, NULL);
    94 
    95 	if (rpmdbOpen("", &db, O_RDONLY, 0644) != 0) {
    96 		fprintf(stderr, "cannot open rpm database\n");
    97 		exit(1);
    98 	}
    99 
   100 	importer = razor_importer_create();
   101 
   102 	iter = rpmdbInitIterator(db, 0, NULL, 0);
   103 	while (h = rpmdbNextIterator(iter), h != NULL) {
   104 		headerGetEntry(h, RPMTAG_NAME, &type, &name.p, &count);
   105 		headerGetEntry(h, RPMTAG_EPOCH, &type, &epoch.p, &count);
   106 		headerGetEntry(h, RPMTAG_VERSION, &type, &version.p, &count);
   107 		headerGetEntry(h, RPMTAG_RELEASE, &type, &release.p, &count);
   108 		headerGetEntry(h, RPMTAG_ARCH, &type, &arch.p, &count);
   109 
   110 		if (epoch.flags != NULL) {
   111 			snprintf(buf, sizeof buf, "%u", *epoch.flags);
   112 			razor_build_evr(evr, sizeof evr,
   113 					buf, version.string, release.string);
   114 		} else {
   115 			razor_build_evr(evr, sizeof evr,
   116 					NULL, version.string, release.string);
   117 		}
   118 
   119 		razor_importer_begin_package(importer,
   120 					     name.string, evr, arch.string);
   121 
   122 		add_properties(importer, RAZOR_PROPERTY_REQUIRES, h,
   123 			       RPMTAG_REQUIRENAME,
   124 			       RPMTAG_REQUIREVERSION,
   125 			       RPMTAG_REQUIREFLAGS);
   126 
   127 		add_properties(importer, RAZOR_PROPERTY_PROVIDES, h,
   128 			       RPMTAG_PROVIDENAME,
   129 			       RPMTAG_PROVIDEVERSION,
   130 			       RPMTAG_PROVIDEFLAGS);
   131 
   132 		add_properties(importer, RAZOR_PROPERTY_OBSOLETES, h,
   133 			       RPMTAG_OBSOLETENAME,
   134 			       RPMTAG_OBSOLETEVERSION,
   135 			       RPMTAG_OBSOLETEFLAGS);
   136 
   137 		add_properties(importer, RAZOR_PROPERTY_CONFLICTS, h,
   138 			       RPMTAG_CONFLICTNAME,
   139 			       RPMTAG_CONFLICTVERSION,
   140 			       RPMTAG_CONFLICTFLAGS);
   141 
   142 		headerGetEntry(h, RPMTAG_BASENAMES, &type,
   143 			       &basenames.p, &count);
   144 		headerGetEntry(h, RPMTAG_DIRNAMES, &type,
   145 			       &dirnames.p, &count);
   146 		headerGetEntry(h, RPMTAG_DIRINDEXES, &type,
   147 			       &dirindexes.p, &count);
   148 		for (i = 0; i < count; i++) {
   149 			snprintf(filename, sizeof filename, "%s%s",
   150 				 dirnames.list[dirindexes.flags[i]],
   151 				 basenames.list[i]);
   152 			razor_importer_add_file(importer, filename);
   153 		}
   154 
   155 		razor_importer_finish_package(importer);
   156 	}
   157 
   158 	rpmdbClose(db);
   159 
   160 	return razor_importer_finish(importer);
   161 }