src/import-rpmdb.c
author J. Ali Harlow <ali@juiblex.co.uk>
Fri Jul 08 17:12:36 2016 +0100 (2016-07-08)
changeset 484 51ab167bd416
parent 475 008c75a5e08d
permissions -rw-r--r--
Release 0.6.3.102
     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 #include "import.h"
    36 
    37 union rpm_entry {
    38 	void *p;
    39 	char *string;
    40 	char **list;
    41 	uint32_t *flags;
    42 	uint32_t integer;
    43 };
    44 
    45 static uint32_t
    46 rpm_to_razor_flags(uint32_t flags)
    47 {
    48 	uint32_t razor_flags;
    49 
    50 	razor_flags = 0;
    51 	if (flags & RPMSENSE_LESS)
    52 		razor_flags |= RAZOR_PROPERTY_LESS;
    53 	if (flags & RPMSENSE_EQUAL)
    54 		razor_flags |= RAZOR_PROPERTY_EQUAL;
    55 	if (flags & RPMSENSE_GREATER)
    56 		razor_flags |= RAZOR_PROPERTY_GREATER;
    57 
    58 	if (flags & RPMSENSE_SCRIPT_PRE)
    59 		razor_flags |= RAZOR_PROPERTY_PRE;
    60 	if (flags & RPMSENSE_SCRIPT_POST)
    61 		razor_flags |= RAZOR_PROPERTY_POST;
    62 	if (flags & RPMSENSE_SCRIPT_PREUN)
    63 		razor_flags |= RAZOR_PROPERTY_PREUN;
    64 	if (flags & RPMSENSE_SCRIPT_POSTUN)
    65 		razor_flags |= RAZOR_PROPERTY_POSTUN;
    66 
    67 	return razor_flags;
    68 }
    69 
    70 static void
    71 add_properties(struct razor_importer *importer,
    72 	       uint32_t type_flags, Header h,
    73 	       int32_t name_tag, int32_t version_tag, int32_t flags_tag)
    74 {
    75 	union rpm_entry names, versions, flags;
    76 	int32_t i, type, count;
    77 
    78 	headerGetEntry(h, name_tag, &type, &names.p, &count);
    79 	headerGetEntry(h, version_tag, &type, &versions.p, &count);
    80 	headerGetEntry(h, flags_tag, &type, &flags.p, &count);
    81 
    82 	for (i = 0; i < count; i++)
    83 		razor_importer_add_property(importer,
    84 					    names.list[i],
    85 					    rpm_to_razor_flags (flags.flags[i]) | type_flags,
    86 					    versions.list[i]);
    87 }
    88 
    89 static void
    90 add_script(struct razor_importer *importer,
    91 	   uint32_t type_flags, Header h,
    92 	   int32_t program_tag, int32_t body_tag)
    93 {
    94 	union rpm_entry program, body;
    95 	int32_t type, count;
    96 
    97 	headerGetEntry(h, program_tag, &type, &program.p, &count);
    98 	headerGetEntry(h, body_tag, &type, &body.p, &count);
    99 
   100 	razor_importer_add_script(importer, type_flags,
   101 				  program.string, body.string);
   102 }
   103 
   104 struct razor_set *
   105 razor_set_create_from_rpmdb(void)
   106 {
   107 	struct razor_importer *importer;
   108 	rpmdbMatchIterator iter;
   109 	Header h;
   110 	int32_t type, count, i;
   111 	union rpm_entry name, epoch, version, release, arch;
   112 	union rpm_entry summary, description, url, license;
   113 	union rpm_entry basenames, dirnames, dirindexes;
   114 	union rpm_entry install_prefixes;
   115 	char filename[PATH_MAX], evr[128], buf[16];
   116 	rpmdb db;
   117 	int imported_count = 0;
   118 
   119 	rpmReadConfigFiles(NULL, NULL);
   120 
   121 	if (rpmdbOpen("", &db, O_RDONLY, 0644) != 0) {
   122 		fprintf(stderr, "cannot open rpm database\n");
   123 		exit(1);
   124 	}
   125 
   126 	importer = razor_importer_create();
   127 
   128 	iter = rpmdbInitIterator(db, 0, NULL, 0);
   129 	while (h = rpmdbNextIterator(iter), h != NULL) {
   130 		headerGetEntry(h, RPMTAG_NAME, &type, &name.p, &count);
   131 		headerGetEntry(h, RPMTAG_EPOCH, &type, &epoch.p, &count);
   132 		headerGetEntry(h, RPMTAG_VERSION, &type, &version.p, &count);
   133 		headerGetEntry(h, RPMTAG_RELEASE, &type, &release.p, &count);
   134 		headerGetEntry(h, RPMTAG_ARCH, &type, &arch.p, &count);
   135 		headerGetEntry(h, RPMTAG_SUMMARY, &type, &summary.p, &count);
   136 		headerGetEntry(h, RPMTAG_DESCRIPTION, &type, &description.p,
   137 			       &count);
   138 		headerGetEntry(h, RPMTAG_URL, &type, &url.p, &count);
   139 		headerGetEntry(h, RPMTAG_LICENSE, &type, &license.p, &count);
   140 
   141 		if (epoch.flags != NULL) {
   142 			snprintf(buf, sizeof buf, "%u", *epoch.flags);
   143 			razor_build_evr(evr, sizeof evr,
   144 					buf, version.string, release.string);
   145 		} else {
   146 			razor_build_evr(evr, sizeof evr,
   147 					NULL, version.string, release.string);
   148 		}
   149 
   150 		razor_importer_begin_package(importer,
   151 					     name.string, evr, arch.string);
   152 		razor_importer_add_details(importer, summary.string,
   153 					   description.string, url.string,
   154 					   license.string);
   155 
   156 		add_properties(importer, RAZOR_PROPERTY_REQUIRES, h,
   157 			       RPMTAG_REQUIRENAME,
   158 			       RPMTAG_REQUIREVERSION,
   159 			       RPMTAG_REQUIREFLAGS);
   160 
   161 		add_properties(importer, RAZOR_PROPERTY_PROVIDES, h,
   162 			       RPMTAG_PROVIDENAME,
   163 			       RPMTAG_PROVIDEVERSION,
   164 			       RPMTAG_PROVIDEFLAGS);
   165 
   166 		add_properties(importer, RAZOR_PROPERTY_OBSOLETES, h,
   167 			       RPMTAG_OBSOLETENAME,
   168 			       RPMTAG_OBSOLETEVERSION,
   169 			       RPMTAG_OBSOLETEFLAGS);
   170 
   171 		add_properties(importer, RAZOR_PROPERTY_CONFLICTS, h,
   172 			       RPMTAG_CONFLICTNAME,
   173 			       RPMTAG_CONFLICTVERSION,
   174 			       RPMTAG_CONFLICTFLAGS);
   175 
   176 		headerGetEntry(h, RPMTAG_BASENAMES, &type,
   177 			       &basenames.p, &count);
   178 		headerGetEntry(h, RPMTAG_DIRNAMES, &type,
   179 			       &dirnames.p, &count);
   180 		headerGetEntry(h, RPMTAG_DIRINDEXES, &type,
   181 			       &dirindexes.p, &count);
   182 		for (i = 0; i < count; i++) {
   183 			snprintf(filename, sizeof filename, "%s%s",
   184 				 dirnames.list[dirindexes.flags[i]],
   185 				 basenames.list[i]);
   186 			razor_importer_add_file(importer, filename);
   187 		}
   188 
   189 		add_script(importer, RAZOR_PROPERTY_PREUN, h,
   190 			   RPMTAG_PREUNPROG, RPMTAG_PREUN);
   191 
   192 		add_script(importer, RAZOR_PROPERTY_POSTUN, h,
   193 			   RPMTAG_POSTUNPROG, RPMTAG_POSTUN);
   194 
   195 		headerGetEntry(h, RPMTAG_INSTPREFIXES, &type,
   196 			       &install_prefixes.p, &count);
   197 		for (i = 0; i < count; i++)
   198 			razor_importer_add_install_prefix(importer,
   199 							  install_prefixes.list[i]);
   200 
   201 		razor_importer_finish_package(importer);
   202 
   203 		printf("\rimporting %d", ++imported_count);
   204 		fflush(stdout);
   205 	}
   206 
   207 	rpmdbClose(db);
   208 
   209 	printf("\nsaving\n");
   210 	return razor_importer_finish(importer);
   211 }