src/import-rpmdb.c
author James Bowes <jbowes@redhat.com>
Wed Jul 09 10:11:13 2008 -0400 (2008-07-09)
changeset 318 829d6711b316
parent 259 5b0601d184ed
child 325 73393734833c
permissions -rw-r--r--
Use strings to identify section types in the on-disk repo format.

Previously, a given razor file type had a fixed number of sections in a
fixed order, identified by an integer type. Now, sections are identified
by a named string (stored in a string pool after the section lists).

This will allow for razor files to contain arbitrary sections.

For bonus points, also drop the 4k section alignment and change the
magic byte string to "RZDB".

committer: Kristian H?gsberg <krh@redhat.com>
     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 summary, description, url, license;
    90 	union rpm_entry basenames, dirnames, dirindexes;
    91 	char filename[PATH_MAX], evr[128], buf[16];
    92 	rpmdb db;
    93 	int imported_count = 0;
    94 
    95 	rpmReadConfigFiles(NULL, NULL);
    96 
    97 	if (rpmdbOpen("", &db, O_RDONLY, 0644) != 0) {
    98 		fprintf(stderr, "cannot open rpm database\n");
    99 		exit(1);
   100 	}
   101 
   102 	importer = razor_importer_create();
   103 
   104 	iter = rpmdbInitIterator(db, 0, NULL, 0);
   105 	while (h = rpmdbNextIterator(iter), h != NULL) {
   106 		headerGetEntry(h, RPMTAG_NAME, &type, &name.p, &count);
   107 		headerGetEntry(h, RPMTAG_EPOCH, &type, &epoch.p, &count);
   108 		headerGetEntry(h, RPMTAG_VERSION, &type, &version.p, &count);
   109 		headerGetEntry(h, RPMTAG_RELEASE, &type, &release.p, &count);
   110 		headerGetEntry(h, RPMTAG_ARCH, &type, &arch.p, &count);
   111 		headerGetEntry(h, RPMTAG_SUMMARY, &type, &summary.p, &count);
   112 		headerGetEntry(h, RPMTAG_DESCRIPTION, &type, &description.p,
   113 			       &count);
   114 		headerGetEntry(h, RPMTAG_URL, &type, &url.p, &count);
   115 		headerGetEntry(h, RPMTAG_LICENSE, &type, &license.p, &count);
   116 
   117 		if (epoch.flags != NULL) {
   118 			snprintf(buf, sizeof buf, "%u", *epoch.flags);
   119 			razor_build_evr(evr, sizeof evr,
   120 					buf, version.string, release.string);
   121 		} else {
   122 			razor_build_evr(evr, sizeof evr,
   123 					NULL, version.string, release.string);
   124 		}
   125 
   126 		razor_importer_begin_package(importer,
   127 					     name.string, evr, arch.string);
   128 		razor_importer_add_details(importer, summary.string,
   129 					   description.string, url.string,
   130 					   license.string);
   131 
   132 		add_properties(importer, RAZOR_PROPERTY_REQUIRES, h,
   133 			       RPMTAG_REQUIRENAME,
   134 			       RPMTAG_REQUIREVERSION,
   135 			       RPMTAG_REQUIREFLAGS);
   136 
   137 		add_properties(importer, RAZOR_PROPERTY_PROVIDES, h,
   138 			       RPMTAG_PROVIDENAME,
   139 			       RPMTAG_PROVIDEVERSION,
   140 			       RPMTAG_PROVIDEFLAGS);
   141 
   142 		add_properties(importer, RAZOR_PROPERTY_OBSOLETES, h,
   143 			       RPMTAG_OBSOLETENAME,
   144 			       RPMTAG_OBSOLETEVERSION,
   145 			       RPMTAG_OBSOLETEFLAGS);
   146 
   147 		add_properties(importer, RAZOR_PROPERTY_CONFLICTS, h,
   148 			       RPMTAG_CONFLICTNAME,
   149 			       RPMTAG_CONFLICTVERSION,
   150 			       RPMTAG_CONFLICTFLAGS);
   151 
   152 		headerGetEntry(h, RPMTAG_BASENAMES, &type,
   153 			       &basenames.p, &count);
   154 		headerGetEntry(h, RPMTAG_DIRNAMES, &type,
   155 			       &dirnames.p, &count);
   156 		headerGetEntry(h, RPMTAG_DIRINDEXES, &type,
   157 			       &dirindexes.p, &count);
   158 		for (i = 0; i < count; i++) {
   159 			snprintf(filename, sizeof filename, "%s%s",
   160 				 dirnames.list[dirindexes.flags[i]],
   161 				 basenames.list[i]);
   162 			razor_importer_add_file(importer, filename);
   163 		}
   164 
   165 		razor_importer_finish_package(importer);
   166 
   167 		printf("\rimporting %d", ++imported_count);
   168 		fflush(stdout);
   169 	}
   170 
   171 	rpmdbClose(db);
   172 
   173 	printf("\nsaving\n");
   174 	return razor_importer_finish(importer);
   175 }