librazor/importer.c
author James Bowes <jbowes@redhat.com>
Fri Jun 20 19:04:47 2008 -0400 (2008-06-20)
changeset 258 29d5002bd17f
parent 248 057933050c42
child 259 5b0601d184ed
permissions -rw-r--r--
Merge branch 'krh/master'

Conflicts:

librazor/razor.h
librazor/rpm.c
razor.c
src/main.c
krh@248
     1
/*
krh@248
     2
 * Copyright (C) 2008  Kristian Høgsberg <krh@redhat.com>
krh@248
     3
 * Copyright (C) 2008  Red Hat, Inc
krh@248
     4
 *
krh@248
     5
 * This program is free software; you can redistribute it and/or modify
krh@248
     6
 * it under the terms of the GNU General Public License as published by
krh@248
     7
 * the Free Software Foundation; either version 2 of the License, or
krh@248
     8
 * (at your option) any later version.
krh@248
     9
 *
krh@248
    10
 * This program is distributed in the hope that it will be useful,
krh@248
    11
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
krh@248
    12
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
krh@248
    13
 * GNU General Public License for more details.
krh@248
    14
 *
krh@248
    15
 * You should have received a copy of the GNU General Public License along
krh@248
    16
 * with this program; if not, write to the Free Software Foundation, Inc.,
krh@248
    17
 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
krh@248
    18
 */
krh@248
    19
krh@248
    20
#define _GNU_SOURCE
krh@248
    21
krh@248
    22
#include <string.h>
krh@248
    23
#include "razor-internal.h"
krh@248
    24
#include "razor.h"
krh@248
    25
krh@248
    26
void
krh@248
    27
razor_importer_begin_package(struct razor_importer *importer,
krh@248
    28
			     const char *name,
krh@248
    29
			     const char *version,
krh@248
    30
			     const char *arch)
krh@248
    31
{
krh@248
    32
	struct razor_package *p;
krh@248
    33
krh@248
    34
	p = array_add(&importer->set->packages, sizeof *p);
krh@248
    35
	p->name = hashtable_tokenize(&importer->table, name);
krh@248
    36
	p->flags = 0;
krh@248
    37
	p->version = hashtable_tokenize(&importer->table, version);
krh@248
    38
	p->arch = hashtable_tokenize(&importer->table, arch);
krh@248
    39
krh@248
    40
	importer->package = p;
krh@248
    41
	array_init(&importer->properties);
krh@248
    42
}
krh@248
    43
krh@248
    44
krh@248
    45
void
krh@248
    46
razor_importer_finish_package(struct razor_importer *importer)
krh@248
    47
{
krh@248
    48
	list_set_array(&importer->package->properties,
krh@248
    49
		       &importer->set->property_pool,
krh@248
    50
		       &importer->properties,
krh@248
    51
		       1);
krh@248
    52
krh@248
    53
	array_release(&importer->properties);
krh@248
    54
}
krh@248
    55
krh@248
    56
void
jbowes@258
    57
razor_importer_add_details(struct razor_importer *importer,
jbowes@258
    58
			   const char *summary,
jbowes@258
    59
			   const char *description,
jbowes@258
    60
			   const char *url,
jbowes@258
    61
			   const char *license)
jbowes@258
    62
{
jbowes@258
    63
	importer->package->summary = hashtable_tokenize(&importer->details_table, summary);
jbowes@258
    64
	importer->package->description = hashtable_tokenize(&importer->details_table, description);
jbowes@258
    65
	importer->package->url = hashtable_tokenize(&importer->details_table, url);
jbowes@258
    66
	importer->package->license = hashtable_tokenize(&importer->details_table, license);
jbowes@258
    67
}
jbowes@258
    68
jbowes@258
    69
void
krh@248
    70
razor_importer_add_property(struct razor_importer *importer,
krh@248
    71
			    const char *name,
krh@248
    72
			    uint32_t flags,
krh@248
    73
			    const char *version)
krh@248
    74
{
krh@248
    75
	struct razor_property *p;
krh@248
    76
	uint32_t *r;
krh@248
    77
krh@248
    78
	p = array_add(&importer->set->properties, sizeof *p);
krh@248
    79
	p->name = hashtable_tokenize(&importer->table, name);
krh@248
    80
	p->flags = flags;
krh@248
    81
	p->version = hashtable_tokenize(&importer->table, version);
krh@248
    82
	list_set_ptr(&p->packages, importer->package -
krh@248
    83
		     (struct razor_package *) importer->set->packages.data);
krh@248
    84
krh@248
    85
	r = array_add(&importer->properties, sizeof *r);
krh@248
    86
	*r = p - (struct razor_property *) importer->set->properties.data;
krh@248
    87
krh@248
    88
	if (((flags & RAZOR_PROPERTY_TYPE_MASK) == RAZOR_PROPERTY_REQUIRES) &&
krh@248
    89
	    *name == '/') {
krh@248
    90
		r = array_add(&importer->file_requires, sizeof *r);
krh@248
    91
		*r = p->name;
krh@248
    92
	}
krh@248
    93
}
krh@248
    94
krh@248
    95
void
krh@248
    96
razor_importer_add_file(struct razor_importer *importer, const char *name)
krh@248
    97
{
krh@248
    98
	struct import_entry *e;
krh@248
    99
krh@248
   100
	e = array_add(&importer->files, sizeof *e);
krh@248
   101
krh@248
   102
	e->package = importer->package -
krh@248
   103
		(struct razor_package *) importer->set->packages.data;
krh@248
   104
	e->name = strdup(name);
krh@248
   105
}
krh@248
   106
krh@248
   107
struct razor_importer *
krh@248
   108
razor_importer_new(void)
krh@248
   109
{
krh@248
   110
	struct razor_importer *importer;
krh@248
   111
krh@248
   112
	importer = zalloc(sizeof *importer);
krh@248
   113
	importer->set = razor_set_create();
krh@248
   114
	hashtable_init(&importer->table, &importer->set->string_pool);
jbowes@258
   115
	hashtable_init(&importer->details_table,
jbowes@258
   116
		       &importer->set->details_string_pool);
jbowes@258
   117
	hashtable_init(&importer->file_table,
jbowes@258
   118
		       &importer->set->file_string_pool);
krh@248
   119
krh@248
   120
	return importer;
krh@248
   121
}
krh@248
   122
krh@248
   123
/* Destroy an importer without creating the set. */
krh@248
   124
void
krh@248
   125
razor_importer_destroy(struct razor_importer *importer)
krh@248
   126
{
krh@248
   127
	/* FIXME: write this */
krh@248
   128
}
krh@248
   129
krh@248
   130
static int
krh@248
   131
compare_packages(const void *p1, const void *p2, void *data)
krh@248
   132
{
krh@248
   133
	const struct razor_package *pkg1 = p1, *pkg2 = p2;
krh@248
   134
	struct razor_set *set = data;
krh@248
   135
	char *pool = set->string_pool.data;
krh@248
   136
krh@248
   137
	/* FIXME: what if the flags are different? */
krh@248
   138
	if (pkg1->name == pkg2->name)
krh@248
   139
		return razor_versioncmp(&pool[pkg1->version], &pool[pkg2->version]);
krh@248
   140
	else
krh@248
   141
		return strcmp(&pool[pkg1->name], &pool[pkg2->name]);
krh@248
   142
}
krh@248
   143
krh@248
   144
static int
krh@248
   145
compare_properties(const void *p1, const void *p2, void *data)
krh@248
   146
{
krh@248
   147
	const struct razor_property *prop1 = p1, *prop2 = p2;
krh@248
   148
	struct razor_set *set = data;
krh@248
   149
	char *pool = set->string_pool.data;
krh@248
   150
krh@248
   151
	if (prop1->name != prop2->name)
krh@248
   152
		return strcmp(&pool[prop1->name], &pool[prop2->name]);
krh@248
   153
	else if (prop1->flags != prop2->flags)
krh@248
   154
		return prop1->flags - prop2->flags;
krh@248
   155
	else
krh@248
   156
		return razor_versioncmp(&pool[prop1->version], &pool[prop2->version]);
krh@248
   157
}
krh@248
   158
krh@248
   159
static uint32_t *
krh@248
   160
uniqueify_properties(struct razor_set *set)
krh@248
   161
{
krh@248
   162
	struct razor_property *rp, *up, *rp_end;
krh@248
   163
	struct array *pkgs, *p;
krh@248
   164
	struct list_head *r;
krh@248
   165
	uint32_t *map, *rmap;
krh@248
   166
	int i, count, unique;
krh@248
   167
krh@248
   168
	count = set->properties.size / sizeof(struct razor_property);
krh@248
   169
	map = razor_qsort_with_data(set->properties.data,
krh@248
   170
				    count,
krh@248
   171
				    sizeof(struct razor_property),
krh@248
   172
				    compare_properties,
krh@248
   173
				    set);
krh@248
   174
krh@248
   175
	rp_end = set->properties.data + set->properties.size;
krh@248
   176
	rmap = malloc(count * sizeof *map);
krh@248
   177
	pkgs = zalloc(count * sizeof *pkgs);
krh@248
   178
	for (rp = set->properties.data, up = rp, i = 0; rp < rp_end; rp++, i++) {
krh@248
   179
		if (rp->name != up->name ||
krh@248
   180
		    rp->flags != up->flags ||
krh@248
   181
		    rp->version != up->version) {
krh@248
   182
			up++;
krh@248
   183
			up->name = rp->name;
krh@248
   184
			up->flags = rp->flags;
krh@248
   185
			up->version = rp->version;
krh@248
   186
		}
krh@248
   187
krh@248
   188
		unique = up - (struct razor_property *) set->properties.data;
krh@248
   189
		rmap[map[i]] = unique;
krh@248
   190
		r = array_add(&pkgs[unique], sizeof *r);
krh@248
   191
		*r = rp->packages;
krh@248
   192
	}
krh@248
   193
	free(map);
krh@248
   194
krh@248
   195
	if (up != rp)
krh@248
   196
		up++;
krh@248
   197
	set->properties.size = (void *) up - set->properties.data;
krh@248
   198
	rp_end = up;
krh@248
   199
	for (rp = set->properties.data, p = pkgs; rp < rp_end; rp++, p++) {
krh@248
   200
		list_set_array(&rp->packages, &set->package_pool, p, 0);
krh@248
   201
		array_release(p);
krh@248
   202
	}
krh@248
   203
krh@248
   204
	free(pkgs);
krh@248
   205
krh@248
   206
	return rmap;
krh@248
   207
}
krh@248
   208
krh@248
   209
static int
krh@248
   210
compare_filenames(const void *p1, const void *p2, void *data)
krh@248
   211
{
krh@248
   212
	const struct import_entry *e1 = p1;
krh@248
   213
	const struct import_entry *e2 = p2;
krh@248
   214
	const char *n1 = e1->name;
krh@248
   215
	const char *n2 = e2->name;
krh@248
   216
krh@248
   217
	/* Need to make sure that the contents of a directory
krh@248
   218
	 * are sorted immediately after it. So "foo/bar" has to
krh@248
   219
	 * sort before "foo.conf"
krh@248
   220
	 *
krh@248
   221
	 * FIXME: this is about 60% slower than strcmp
krh@248
   222
	 */
krh@248
   223
	while (*n1 && *n2) {
krh@248
   224
		if (*n1 < *n2)
krh@248
   225
			return *n2 == '/' ? 1 : -1;
krh@248
   226
		else if (*n1 > *n2)
krh@248
   227
			return *n1 == '/' ? -1 : 1;
krh@248
   228
		n1++;
krh@248
   229
		n2++;
krh@248
   230
	}
krh@248
   231
	if (*n1)
krh@248
   232
		return 1;
krh@248
   233
	else if (*n2)
krh@248
   234
		return -1;
krh@248
   235
	else
krh@248
   236
		return 0;
krh@248
   237
}
krh@248
   238
krh@248
   239
static void
krh@248
   240
count_entries(struct import_directory *d)
krh@248
   241
{
krh@248
   242
	struct import_directory *p, *end;
krh@248
   243
krh@248
   244
	p = d->files.data;
krh@248
   245
	end = d->files.data + d->files.size;
krh@248
   246
	d->count = 0;
krh@248
   247
	while (p < end) {
krh@248
   248
		count_entries(p);
krh@248
   249
		d->count += p->count + 1;
krh@248
   250
		p++;
krh@248
   251
	}
krh@248
   252
}
krh@248
   253
krh@248
   254
static void
krh@248
   255
serialize_files(struct razor_set *set,
krh@248
   256
		struct import_directory *d, struct array *array)
krh@248
   257
{
krh@248
   258
	struct import_directory *p, *end;
krh@248
   259
	struct razor_entry *e = NULL;
krh@248
   260
	uint32_t s;
krh@248
   261
krh@248
   262
	p = d->files.data;
krh@248
   263
	end = d->files.data + d->files.size;
krh@248
   264
	s = array->size / sizeof *e + d->files.size / sizeof *p;
krh@248
   265
	while (p < end) {
krh@248
   266
		e = array_add(array, sizeof *e);
krh@248
   267
		e->name = p->name;
krh@248
   268
		e->flags = 0;
krh@248
   269
		e->start = p->count > 0 ? s : 0;
krh@248
   270
		s += p->count;
krh@248
   271
krh@248
   272
		list_set_array(&e->packages, &set->package_pool, &p->packages, 0);
krh@248
   273
		array_release(&p->packages);
krh@248
   274
		p++;
krh@248
   275
	}
krh@248
   276
	if (e != NULL)
krh@248
   277
		e->flags |= RAZOR_ENTRY_LAST;
krh@248
   278
krh@248
   279
	p = d->files.data;
krh@248
   280
	end = d->files.data + d->files.size;
krh@248
   281
	while (p < end) {
krh@248
   282
		serialize_files(set, p, array);
krh@248
   283
		p++;
krh@248
   284
	}
krh@248
   285
}
krh@248
   286
krh@248
   287
static void
krh@248
   288
remap_property_package_links(struct array *properties, uint32_t *rmap)
krh@248
   289
{
krh@248
   290
	struct razor_property *p, *end;
krh@248
   291
krh@248
   292
	end = properties->data + properties->size;
krh@248
   293
	for (p = properties->data; p < end; p++)
krh@248
   294
		list_remap_head(&p->packages, rmap);
krh@248
   295
}
krh@248
   296
krh@248
   297
static void
krh@248
   298
build_file_tree(struct razor_importer *importer)
krh@248
   299
{
krh@248
   300
	int count, i, length;
krh@248
   301
	struct import_entry *filenames;
krh@248
   302
	char *f, *end;
krh@248
   303
	uint32_t name, *r;
krh@248
   304
	char dirname[256];
krh@248
   305
	struct import_directory *d, root;
krh@248
   306
	struct razor_entry *e;
krh@248
   307
krh@248
   308
	count = importer->files.size / sizeof (struct import_entry);
krh@248
   309
	razor_qsort_with_data(importer->files.data,
krh@248
   310
			      count,
krh@248
   311
			      sizeof (struct import_entry),
krh@248
   312
			      compare_filenames,
krh@248
   313
			      NULL);
krh@248
   314
krh@248
   315
	root.name = hashtable_tokenize(&importer->table, "");
krh@248
   316
	array_init(&root.files);
krh@248
   317
	array_init(&root.packages);
krh@248
   318
	root.last = NULL;
krh@248
   319
krh@248
   320
	filenames = importer->files.data;
krh@248
   321
	for (i = 0; i < count; i++) {
krh@248
   322
		f = filenames[i].name;
krh@248
   323
		if (*f != '/')
krh@248
   324
			continue;
krh@248
   325
		f++;
krh@248
   326
krh@248
   327
		d = &root;
krh@248
   328
		while (*f) {
krh@248
   329
			end = strchr(f, '/');
krh@248
   330
			if (end == NULL)
krh@248
   331
				end = f + strlen(f);
krh@248
   332
			length = end - f;
krh@248
   333
			memcpy(dirname, f, length);
krh@248
   334
			dirname[length] ='\0';
krh@248
   335
			name = hashtable_tokenize(&importer->table, dirname);
krh@248
   336
			if (d->last == NULL || d->last->name != name) {
krh@248
   337
				d->last = array_add(&d->files, sizeof *d);
krh@248
   338
				d->last->name = name;
krh@248
   339
				d->last->last = NULL;
krh@248
   340
				array_init(&d->last->files);
krh@248
   341
				array_init(&d->last->packages);
krh@248
   342
			}
krh@248
   343
			d = d->last;
krh@248
   344
			f = end + 1;
krh@248
   345
			if (*end == '\0')
krh@248
   346
				break;
krh@248
   347
		}
krh@248
   348
krh@248
   349
		r = array_add(&d->packages, sizeof *r);
krh@248
   350
		*r = filenames[i].package;
krh@248
   351
		free(filenames[i].name);
krh@248
   352
	}
krh@248
   353
krh@248
   354
	count_entries(&root);
krh@248
   355
	e = importer->set->files.data;
krh@248
   356
	e->name = root.name;
krh@248
   357
	e->flags = RAZOR_ENTRY_LAST;
krh@248
   358
	e->start = importer->files.size ? 1 : 0;
krh@248
   359
	list_set_empty(&e->packages);
krh@248
   360
krh@248
   361
	serialize_files(importer->set, &root, &importer->set->files);
krh@248
   362
krh@248
   363
	array_release(&importer->files);
krh@248
   364
}
krh@248
   365
krh@248
   366
static void
krh@248
   367
list_to_array(struct list *list, struct array *array)
krh@248
   368
{
krh@248
   369
	uint32_t *item;
krh@248
   370
krh@248
   371
	while (list) {
krh@248
   372
		 item = array_add(array, sizeof *item);
krh@248
   373
		 *item = list->data;
krh@248
   374
		 list = list_next(list);
krh@248
   375
	}
krh@248
   376
}
krh@248
   377
krh@248
   378
static int
krh@248
   379
compare_file_requires(const void *p1, const void *p2, void *data)
krh@248
   380
{
krh@248
   381
	uint32_t *f1 = (void *)p1, *f2 = (void *)p2;
krh@248
   382
	const char *pool = data;
krh@248
   383
krh@248
   384
	return strcmp(&pool[*f1], &pool[*f2]);
krh@248
   385
}
krh@248
   386
krh@248
   387
static void
krh@248
   388
find_file_provides(struct razor_importer *importer)
krh@248
   389
{
krh@248
   390
	struct razor_property *prop;
krh@248
   391
	struct razor_entry *top, *entry;
krh@248
   392
	struct razor_package *packages;
krh@248
   393
	struct array pkgprops;
krh@248
   394
	struct list *pkg;
krh@248
   395
	uint32_t *req, *req_start, *req_end;
krh@248
   396
	uint32_t *map, *newprop;
krh@248
   397
	char *pool;
krh@248
   398
krh@248
   399
	pool = importer->set->string_pool.data;
krh@248
   400
	packages = importer->set->packages.data;
krh@248
   401
	top = importer->set->files.data;
krh@248
   402
krh@248
   403
	req = req_start = importer->file_requires.data;
krh@248
   404
	req_end = importer->file_requires.data + importer->file_requires.size;
krh@248
   405
	map = razor_qsort_with_data(req, req_end - req, sizeof *req,
krh@248
   406
				    compare_file_requires, pool);
krh@248
   407
	free(map);
krh@248
   408
krh@248
   409
	for (req = req_start; req < req_end; req++) {
krh@248
   410
		if (req > req_start && req[0] == req[-1])
krh@248
   411
			continue;
krh@248
   412
		entry = razor_set_find_entry(importer->set, top, &pool[*req]);
krh@248
   413
		if (!entry)
krh@248
   414
			continue;
krh@248
   415
krh@248
   416
		for (pkg = list_first(&entry->packages, &importer->set->package_pool); pkg; pkg = list_next(pkg)) {
krh@248
   417
			prop = array_add(&importer->set->properties, sizeof *prop);
krh@248
   418
			prop->name = *req;
krh@248
   419
			prop->flags =
krh@248
   420
				RAZOR_PROPERTY_PROVIDES | RAZOR_PROPERTY_EQUAL;
krh@248
   421
			prop->version = hashtable_tokenize(&importer->table, "");
krh@248
   422
			list_set_ptr(&prop->packages, pkg->data);
krh@248
   423
krh@248
   424
			/* Update property list of pkg */
krh@248
   425
			array_init(&pkgprops);
krh@248
   426
			list_to_array(list_first(&packages[pkg->data].properties, &importer->set->property_pool), &pkgprops);
krh@248
   427
			newprop = array_add(&pkgprops, sizeof *newprop);
krh@248
   428
			*newprop = prop - (struct razor_property *)importer->set->properties.data;
krh@248
   429
			list_set_array(&packages[pkg->data].properties, &importer->set->property_pool, &pkgprops, 1);
krh@248
   430
			array_release(&pkgprops);
krh@248
   431
		}
krh@248
   432
	}
krh@248
   433
krh@248
   434
	array_release(&importer->file_requires);
krh@248
   435
}
krh@248
   436
krh@248
   437
static void
krh@248
   438
build_package_file_lists(struct razor_set *set, uint32_t *rmap)
krh@248
   439
{
krh@248
   440
	struct razor_package *p, *packages;
krh@248
   441
	struct array *pkgs;
krh@248
   442
	struct razor_entry *e, *end;
krh@248
   443
	struct list *r;
krh@248
   444
	uint32_t *q;
krh@248
   445
	int i, count;
krh@248
   446
krh@248
   447
	count = set->packages.size / sizeof *p;
krh@248
   448
	pkgs = zalloc(count * sizeof *pkgs);
krh@248
   449
krh@248
   450
	end = set->files.data + set->files.size;
krh@248
   451
	for (e = set->files.data; e < end; e++) {
krh@248
   452
		list_remap_head(&e->packages, rmap);
krh@248
   453
		r = list_first(&e->packages, &set->package_pool);
krh@248
   454
		while (r) {
krh@248
   455
			q = array_add(&pkgs[r->data], sizeof *q);
krh@248
   456
			*q = e - (struct razor_entry *) set->files.data;
krh@248
   457
			r = list_next(r);
krh@248
   458
		}
krh@248
   459
	}
krh@248
   460
krh@248
   461
	packages = set->packages.data;
krh@248
   462
	for (i = 0; i < count; i++) {
krh@248
   463
		list_set_array(&packages[i].files, &set->file_pool, &pkgs[i], 0);
krh@248
   464
		array_release(&pkgs[i]);
krh@248
   465
	}
krh@248
   466
	free(pkgs);
krh@248
   467
}
krh@248
   468
krh@248
   469
struct razor_set *
krh@248
   470
razor_importer_finish(struct razor_importer *importer)
krh@248
   471
{
krh@248
   472
	struct razor_set *set;
krh@248
   473
	uint32_t *map, *rmap;
krh@248
   474
	int i, count;
krh@248
   475
krh@248
   476
	build_file_tree(importer);
krh@248
   477
	find_file_provides(importer);
krh@248
   478
krh@248
   479
	map = uniqueify_properties(importer->set);
krh@248
   480
	list_remap_pool(&importer->set->property_pool, map);
krh@248
   481
	free(map);
krh@248
   482
krh@248
   483
	count = importer->set->packages.size / sizeof(struct razor_package);
krh@248
   484
	map = razor_qsort_with_data(importer->set->packages.data,
krh@248
   485
				    count,
krh@248
   486
				    sizeof(struct razor_package),
krh@248
   487
				    compare_packages,
krh@248
   488
				    importer->set);
krh@248
   489
krh@248
   490
	rmap = malloc(count * sizeof *rmap);
krh@248
   491
	for (i = 0; i < count; i++)
krh@248
   492
		rmap[map[i]] = i;
krh@248
   493
	free(map);
krh@248
   494
krh@248
   495
	list_remap_pool(&importer->set->package_pool, rmap);
krh@248
   496
	build_package_file_lists(importer->set, rmap);
krh@248
   497
	remap_property_package_links(&importer->set->properties, rmap);
krh@248
   498
	free(rmap);
krh@248
   499
krh@248
   500
	set = importer->set;
krh@248
   501
	hashtable_release(&importer->table);
krh@248
   502
	free(importer);
krh@248
   503
krh@248
   504
	return set;
krh@248
   505
}