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