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