librazor/razor.c
author J. Ali Harlow <ali@juiblex.co.uk>
Fri Jun 12 16:59:11 2009 +0100 (2009-06-12)
changeset 369 f8c27fe9fe63
parent 367 e45f50e940b6
child 372 6e93e5485947
permissions -rw-r--r--
Add basic support for uninstall scripts.
RPM_INSTALL_PREFIX{n} is not yet supported and upgrading a package
where an uninstall script changes may need more work to ensure the
old script doesn't get included in the merged set (when it is too
late to remove). I haven't yet tested whether this is a real problem.
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
#define _GNU_SOURCE
rhughes@241
    22
ali@334
    23
#include "config.h"
ali@334
    24
rhughes@241
    25
#include <stdlib.h>
rhughes@241
    26
#include <stddef.h>
rhughes@241
    27
#include <stdint.h>
rhughes@241
    28
#include <stdio.h>
richard@302
    29
#include <stdarg.h>
rhughes@241
    30
#include <string.h>
rhughes@241
    31
#include <sys/types.h>
rhughes@241
    32
#include <sys/stat.h>
rhughes@241
    33
#include <unistd.h>
rhughes@241
    34
#include <fcntl.h>
rhughes@241
    35
#include <errno.h>
rhughes@241
    36
#include <ctype.h>
rhughes@241
    37
#include <fnmatch.h>
ali@369
    38
#include <limits.h>
richard@301
    39
#include <assert.h>
rhughes@241
    40
krh@253
    41
#include "razor-internal.h"
rhughes@241
    42
#include "razor.h"
rhughes@241
    43
ali@345
    44
#ifndef O_BINARY
ali@345
    45
#define O_BINARY	0
ali@345
    46
#endif
ali@345
    47
krh@248
    48
void *
rhughes@241
    49
zalloc(size_t size)
rhughes@241
    50
{
rhughes@241
    51
	void *p;
rhughes@241
    52
rhughes@241
    53
	p = malloc(size);
rhughes@241
    54
	memset(p, 0, size);
rhughes@241
    55
rhughes@241
    56
	return p;
rhughes@241
    57
}
rhughes@241
    58
jbowes@318
    59
struct razor_set_section_index {
jbowes@318
    60
	const char *name;
jbowes@318
    61
	uint32_t offset;
jbowes@318
    62
};
jbowes@318
    63
jbowes@318
    64
struct razor_set_section_index razor_sections[] = {
rhughes@241
    65
	{ RAZOR_STRING_POOL,	offsetof(struct razor_set, string_pool) },
rhughes@241
    66
	{ RAZOR_PACKAGES,	offsetof(struct razor_set, packages) },
rhughes@241
    67
	{ RAZOR_PROPERTIES,	offsetof(struct razor_set, properties) },
rhughes@241
    68
	{ RAZOR_PACKAGE_POOL,	offsetof(struct razor_set, package_pool) },
rhughes@241
    69
	{ RAZOR_PROPERTY_POOL,	offsetof(struct razor_set, property_pool) },
rhughes@241
    70
};
rhughes@241
    71
jbowes@318
    72
struct razor_set_section_index razor_files_sections[] = {
jbowes@258
    73
	{ RAZOR_FILES,			offsetof(struct razor_set, files) },
jbowes@258
    74
	{ RAZOR_FILE_POOL,		offsetof(struct razor_set, file_pool) },
jbowes@258
    75
	{ RAZOR_FILE_STRING_POOL,	offsetof(struct razor_set, file_string_pool) },
jbowes@258
    76
};
jbowes@258
    77
jbowes@318
    78
struct razor_set_section_index razor_details_sections[] = {
jbowes@258
    79
	{ RAZOR_DETAILS_STRING_POOL,	offsetof(struct razor_set, details_string_pool) },
jbowes@258
    80
};
krh@262
    81
krh@269
    82
RAZOR_EXPORT struct razor_set *
ali@363
    83
razor_set_create_without_root(void)
ali@363
    84
{
ali@363
    85
	struct razor_set *set;
ali@363
    86
	char *empty;
ali@363
    87
ali@363
    88
	set = zalloc(sizeof *set);
ali@363
    89
ali@363
    90
	empty = array_add(&set->string_pool, 1);
ali@363
    91
	*empty = '\0';
ali@363
    92
ali@363
    93
	return set;
ali@363
    94
}
ali@363
    95
ali@363
    96
RAZOR_EXPORT struct razor_set *
rhughes@241
    97
razor_set_create(void)
rhughes@241
    98
{
rhughes@241
    99
	struct razor_set *set;
rhughes@241
   100
	struct razor_entry *e;
rhughes@241
   101
ali@363
   102
	set = razor_set_create_without_root();
rhughes@241
   103
rhughes@241
   104
	e = array_add(&set->files, sizeof *e);
rhughes@241
   105
	e->name = 0;
rhughes@241
   106
	e->flags = RAZOR_ENTRY_LAST;
rhughes@241
   107
	e->start = 0;
rhughes@241
   108
	list_set_empty(&e->packages);
rhughes@241
   109
rhughes@241
   110
	return set;
rhughes@241
   111
}
rhughes@241
   112
jbowes@318
   113
static int
jbowes@318
   114
razor_set_bind_sections(struct razor_set *set,
jbowes@318
   115
			struct razor_set_header **header,
jbowes@318
   116
			size_t *header_size,
jbowes@318
   117
			struct razor_set_section_index section_index[],
jbowes@318
   118
			int section_index_size,
jbowes@318
   119
			const char *filename)
rhughes@241
   120
{
jbowes@318
   121
	struct razor_set_section *s, *sections;
rhughes@241
   122
	struct array *array;
jbowes@318
   123
	const char *pool;
ali@322
   124
	int i;
richard@301
   125
ali@322
   126
	*header = razor_file_get_contents(filename, header_size);
ali@322
   127
	if (!*header)
jbowes@288
   128
		return -1;
jbowes@258
   129
jbowes@318
   130
	sections = (void *) *header + sizeof **header;
jbowes@318
   131
	pool = (void *) sections + (*header)->num_sections * sizeof *sections;
jbowes@318
   132
jbowes@318
   133
	for (i = 0; i < (*header)->num_sections; i++) {
jbowes@318
   134
		int j;
jbowes@318
   135
		s = sections + i;
jbowes@318
   136
		for (j = 0; j < section_index_size; j++)
jbowes@318
   137
			if (!strcmp(section_index[j].name,
jbowes@318
   138
				    &pool[s->name]))
jbowes@318
   139
				break;
jbowes@318
   140
		if (j == section_index_size)
jbowes@258
   141
			continue;
jbowes@318
   142
		array = (void *) set + section_index[j].offset;
jbowes@318
   143
		array->data = (void *) *header + s->offset;
jbowes@258
   144
		array->size = s->size;
jbowes@258
   145
		array->alloc = s->size;
jbowes@258
   146
	}
jbowes@288
   147
jbowes@288
   148
	return 0;
jbowes@258
   149
}
jbowes@258
   150
jbowes@318
   151
RAZOR_EXPORT struct razor_set *
jbowes@318
   152
razor_set_open(const char *filename)
jbowes@318
   153
{
jbowes@318
   154
	struct razor_set *set;
jbowes@318
   155
jbowes@318
   156
	set = zalloc(sizeof *set);
jbowes@318
   157
	if (razor_set_bind_sections(set, &set->header, &set->header_size,
jbowes@318
   158
				    razor_sections, ARRAY_SIZE(razor_sections),
jbowes@318
   159
				    filename)){
jbowes@318
   160
		free(set);
jbowes@318
   161
		return NULL;
jbowes@318
   162
	}
jbowes@318
   163
	return set;
jbowes@318
   164
}
jbowes@318
   165
jbowes@318
   166
RAZOR_EXPORT int
jbowes@318
   167
razor_set_open_details(struct razor_set *set, const char *filename)
jbowes@318
   168
{
jbowes@318
   169
	return razor_set_bind_sections(set, &set->details_header,
jbowes@318
   170
				       &set->details_header_size,
jbowes@318
   171
				       razor_details_sections,
jbowes@318
   172
				       ARRAY_SIZE(razor_details_sections),
jbowes@318
   173
				       filename);
jbowes@318
   174
}
jbowes@318
   175
jbowes@288
   176
RAZOR_EXPORT int
jbowes@258
   177
razor_set_open_files(struct razor_set *set, const char *filename)
jbowes@258
   178
{
jbowes@318
   179
	return razor_set_bind_sections(set, &set->files_header,
jbowes@318
   180
				       &set->files_header_size,
jbowes@318
   181
				       razor_files_sections,
jbowes@318
   182
				       ARRAY_SIZE(razor_files_sections),
jbowes@318
   183
				       filename);
jbowes@258
   184
}
jbowes@258
   185
krh@269
   186
RAZOR_EXPORT void
rhughes@241
   187
razor_set_destroy(struct razor_set *set)
rhughes@241
   188
{
rhughes@241
   189
	struct array *a;
rhughes@241
   190
	int i;
rhughes@241
   191
richard@301
   192
	assert (set != NULL);
richard@301
   193
rhughes@241
   194
	if (set->header) {
ali@322
   195
		razor_file_free_contents(set->header, set->header_size);
rhughes@241
   196
	} else {
rhughes@241
   197
		for (i = 0; i < ARRAY_SIZE(razor_sections); i++) {
rhughes@241
   198
			a = (void *) set + razor_sections[i].offset;
rhughes@241
   199
			free(a->data);
rhughes@241
   200
		}
rhughes@241
   201
	}
rhughes@241
   202
jbowes@258
   203
	if (set->details_header) {
ali@322
   204
		razor_file_free_contents(set->details_header,
ali@322
   205
			set->details_header_size);
jbowes@258
   206
	} else {
jbowes@258
   207
		for (i = 0; i < ARRAY_SIZE(razor_details_sections); i++) {
jbowes@258
   208
			a = (void *) set + razor_details_sections[i].offset;
jbowes@258
   209
			free(a->data);
jbowes@258
   210
		}
jbowes@258
   211
	}
jbowes@258
   212
jbowes@258
   213
	if (set->files_header) {
ali@322
   214
		razor_file_free_contents(set->files_header,
ali@322
   215
			set->files_header_size);
jbowes@258
   216
	} else {
jbowes@258
   217
		for (i = 0; i < ARRAY_SIZE(razor_files_sections); i++) {
jbowes@258
   218
			a = (void *) set + razor_files_sections[i].offset;
jbowes@258
   219
			free(a->data);
jbowes@258
   220
		}
jbowes@258
   221
	}
jbowes@258
   222
rhughes@241
   223
	free(set);
rhughes@241
   224
}
rhughes@241
   225
jbowes@258
   226
static int
jbowes@318
   227
razor_set_write_sections_to_fd(struct razor_set *set, int fd,
jbowes@318
   228
			       struct razor_set_section_index *sections,
jbowes@258
   229
			       size_t array_size)
rhughes@241
   230
{
jbowes@318
   231
	struct razor_set_header header;
jbowes@318
   232
	struct razor_set_section *out_sections =
jbowes@318
   233
		malloc(array_size * sizeof *out_sections);
jbowes@318
   234
	struct hashtable table;
jbowes@318
   235
	struct array *a, pool;
rhughes@241
   236
	uint32_t offset;
rhughes@241
   237
	int i;
rhughes@241
   238
jbowes@318
   239
	header.magic = RAZOR_MAGIC;
jbowes@318
   240
	header.version = RAZOR_VERSION;
jbowes@318
   241
	header.num_sections = array_size;
jbowes@318
   242
	offset = sizeof header + array_size * sizeof *out_sections;
jbowes@318
   243
jbowes@318
   244
	array_init(&pool);
jbowes@318
   245
	hashtable_init(&table, &pool);
jbowes@318
   246
jbowes@318
   247
	for (i = 0; i < array_size; i++)
jbowes@318
   248
		out_sections[i].name =
jbowes@318
   249
			hashtable_tokenize(&table, sections[i].name);
jbowes@318
   250
jbowes@318
   251
	offset += pool.size;
rhughes@241
   252
jbowes@258
   253
	for (i = 0; i < array_size; i++) {
jbowes@258
   254
		a = (void *) set + sections[i].offset;
jbowes@318
   255
		out_sections[i].offset = offset;
jbowes@318
   256
		out_sections[i].size = a->size;
jbowes@318
   257
		offset += a->size;
rhughes@241
   258
	}
rhughes@241
   259
jbowes@318
   260
	razor_write(fd, &header, sizeof header);
jbowes@318
   261
	razor_write(fd, out_sections, array_size * sizeof *out_sections);
jbowes@318
   262
	razor_write(fd, pool.data, pool.size);
rhughes@241
   263
jbowes@258
   264
	for (i = 0; i < array_size; i++) {
jbowes@258
   265
		a = (void *) set + sections[i].offset;
rhughes@241
   266
		razor_write(fd, a->data, a->size);
rhughes@241
   267
	}
rhughes@241
   268
jbowes@318
   269
	free(out_sections);
jbowes@318
   270
rhughes@241
   271
	return 0;
rhughes@241
   272
}
rhughes@241
   273
krh@269
   274
RAZOR_EXPORT int
jbowes@258
   275
razor_set_write_to_fd(struct razor_set *set, int fd,
jbowes@258
   276
		      enum razor_repo_file_type type)
jbowes@258
   277
{
jbowes@258
   278
	switch (type) {
jbowes@258
   279
	case RAZOR_REPO_FILE_MAIN:
jbowes@318
   280
		return razor_set_write_sections_to_fd(set, fd,
jbowes@258
   281
						      razor_sections,
jbowes@258
   282
						      ARRAY_SIZE(razor_sections));
jbowes@258
   283
jbowes@258
   284
	case RAZOR_REPO_FILE_DETAILS:
jbowes@318
   285
		return razor_set_write_sections_to_fd(set, fd,
jbowes@258
   286
						      razor_details_sections,
jbowes@258
   287
						      ARRAY_SIZE(razor_details_sections));
jbowes@258
   288
	case RAZOR_REPO_FILE_FILES:
jbowes@318
   289
		return razor_set_write_sections_to_fd(set, fd,
jbowes@258
   290
						      razor_files_sections,
jbowes@258
   291
						      ARRAY_SIZE(razor_files_sections));
jbowes@258
   292
	default:
jbowes@258
   293
		return -1;
jbowes@258
   294
	}
jbowes@258
   295
}
jbowes@258
   296
krh@269
   297
RAZOR_EXPORT int
jbowes@258
   298
razor_set_write(struct razor_set *set, const char *filename,
jbowes@258
   299
		enum razor_repo_file_type type)
rhughes@241
   300
{
rhughes@241
   301
	int fd, status;
rhughes@241
   302
ali@345
   303
	fd = open(filename, O_CREAT | O_WRONLY | O_TRUNC | O_BINARY, 0666);
rhughes@241
   304
	if (fd < 0)
rhughes@241
   305
		return -1;
rhughes@241
   306
jbowes@258
   307
	status = razor_set_write_to_fd(set, fd, type);
rhughes@241
   308
	if (status) {
rhughes@241
   309
	    close(fd);
rhughes@241
   310
	    return status;
rhughes@241
   311
	}
rhughes@241
   312
rhughes@241
   313
	return close(fd);
rhughes@241
   314
}
krh@269
   315
krh@269
   316
RAZOR_EXPORT void
rhughes@241
   317
razor_build_evr(char *evr_buf, int size, const char *epoch,
rhughes@241
   318
		const char *version, const char *release)
rhughes@241
   319
{
rhughes@241
   320
	int len;
rhughes@241
   321
rhughes@241
   322
	if (!version || !*version) {
rhughes@241
   323
		*evr_buf = '\0';
rhughes@241
   324
		return;
rhughes@241
   325
	}
rhughes@241
   326
rhughes@241
   327
	if (epoch && *epoch && strcmp(epoch, "0") != 0) {
rhughes@241
   328
		len = snprintf(evr_buf, size, "%s:", epoch);
rhughes@241
   329
		evr_buf += len;
rhughes@241
   330
		size -= len;
rhughes@241
   331
	}
rhughes@241
   332
	len = snprintf(evr_buf, size, "%s", version);
rhughes@241
   333
	evr_buf += len;
rhughes@241
   334
	size -= len;
rhughes@241
   335
	if (release && *release)
rhughes@241
   336
		snprintf(evr_buf, size, "-%s", release);
rhughes@241
   337
}
rhughes@241
   338
krh@269
   339
RAZOR_EXPORT int
krh@248
   340
razor_versioncmp(const char *s1, const char *s2)
rhughes@241
   341
{
rhughes@241
   342
	const char *p1, *p2;
rhughes@241
   343
	long n1, n2;
rhughes@241
   344
	int res;
rhughes@241
   345
richard@301
   346
	assert (s1 != NULL);
richard@301
   347
	assert (s2 != NULL);
richard@301
   348
rhughes@241
   349
	n1 = strtol(s1, (char **) &p1, 10);
rhughes@241
   350
	n2 = strtol(s2, (char **) &p2, 10);
rhughes@241
   351
rhughes@241
   352
	/* Epoch; if one but not the other has an epoch set, default
rhughes@241
   353
	 * the epoch-less version to 0. */
rhughes@241
   354
	res = (*p1 == ':') - (*p2 == ':');
rhughes@241
   355
	if (res < 0) {
rhughes@241
   356
		n1 = 0;
rhughes@241
   357
		p1 = s1;
rhughes@241
   358
		p2++;
rhughes@241
   359
	} else if (res > 0) {
rhughes@241
   360
		p1++;
rhughes@241
   361
		n2 = 0;
rhughes@241
   362
		p2 = s2;
rhughes@241
   363
	}
rhughes@241
   364
rhughes@241
   365
	if (n1 != n2)
rhughes@241
   366
		return n1 - n2;
rhughes@241
   367
	while (*p1 && *p2) {
rhughes@241
   368
		if (*p1 != *p2)
rhughes@241
   369
			return *p1 - *p2;
rhughes@241
   370
		p1++;
rhughes@241
   371
		p2++;
rhughes@241
   372
		if (isdigit(*p1) && isdigit(*p2))
krh@248
   373
			return razor_versioncmp(p1, p2);
rhughes@241
   374
	}
rhughes@241
   375
rhughes@241
   376
	return *p1 - *p2;
rhughes@241
   377
}
rhughes@241
   378
richard@302
   379
static const char *
richard@302
   380
razor_package_get_details_type(struct razor_set *set,
richard@302
   381
			       struct razor_package *package,
richard@302
   382
			       enum razor_detail_type type)
richard@302
   383
{
richard@302
   384
	const char *pool;
richard@302
   385
richard@302
   386
	switch (type) {
richard@302
   387
	case RAZOR_DETAIL_NAME:
richard@302
   388
		pool = set->string_pool.data;
richard@302
   389
		return &pool[package->name];
richard@302
   390
richard@302
   391
	case RAZOR_DETAIL_VERSION:
richard@302
   392
		pool = set->string_pool.data;
richard@302
   393
		return &pool[package->version];
richard@302
   394
richard@302
   395
	case RAZOR_DETAIL_ARCH:
richard@302
   396
		pool = set->string_pool.data;
richard@302
   397
		return &pool[package->arch];
richard@302
   398
richard@302
   399
	case RAZOR_DETAIL_SUMMARY:
richard@302
   400
		pool = set->details_string_pool.data;
richard@302
   401
		return &pool[package->summary];
richard@302
   402
richard@302
   403
	case RAZOR_DETAIL_DESCRIPTION:
richard@302
   404
		pool = set->details_string_pool.data;
richard@302
   405
		return &pool[package->description];
richard@302
   406
richard@302
   407
	case RAZOR_DETAIL_URL:
richard@302
   408
		pool = set->details_string_pool.data;
richard@302
   409
		return &pool[package->url];
richard@302
   410
richard@302
   411
	case RAZOR_DETAIL_LICENSE:
richard@302
   412
		pool = set->details_string_pool.data;
richard@302
   413
		return &pool[package->license];
richard@302
   414
ali@369
   415
	case RAZOR_DETAIL_PREUNPROG:
ali@369
   416
		pool = set->string_pool.data;
ali@369
   417
		return &pool[package->preun.program];
ali@369
   418
ali@369
   419
	case RAZOR_DETAIL_PREUN:
ali@369
   420
		pool = set->string_pool.data;
ali@369
   421
		return &pool[package->preun.body];
ali@369
   422
ali@369
   423
	case RAZOR_DETAIL_POSTUNPROG:
ali@369
   424
		pool = set->string_pool.data;
ali@369
   425
		return &pool[package->postun.program];
ali@369
   426
ali@369
   427
	case RAZOR_DETAIL_POSTUN:
ali@369
   428
		pool = set->string_pool.data;
ali@369
   429
		return &pool[package->postun.body];
ali@369
   430
richard@302
   431
	default:
richard@302
   432
		fprintf(stderr, "type %u not found\n", type);
richard@302
   433
		return NULL;
richard@302
   434
	}
richard@302
   435
}
richard@302
   436
richard@302
   437
/**
richard@302
   438
 * razor_package_get_details_varg:
richard@302
   439
 * @set: a %razor_set
richard@302
   440
 * @package: a %razor_package
richard@302
   441
 * @args: a va_list of arguments to set
richard@302
   442
 **/
richard@302
   443
void
richard@302
   444
razor_package_get_details_varg(struct razor_set *set,
richard@302
   445
			       struct razor_package *package,
richard@302
   446
			       va_list args)
richard@302
   447
{
richard@302
   448
	int i;
richard@302
   449
	enum razor_detail_type type;
richard@302
   450
	const char **data;
richard@302
   451
richard@302
   452
	for (i = 0;; i += 2) {
richard@302
   453
		type = va_arg(args, enum razor_detail_type);
richard@307
   454
		if (type == RAZOR_DETAIL_LAST)
richard@302
   455
			break;
richard@302
   456
		data = va_arg(args, const char **);
richard@302
   457
		*data = razor_package_get_details_type(set, package, type);
richard@302
   458
	}
richard@302
   459
richard@302
   460
}
richard@302
   461
richard@302
   462
/**
richard@302
   463
 * razor_package_get_details:
richard@302
   464
 * @set: a %razor_set
richard@302
   465
 * @package: a %razor_package
richard@302
   466
 *
richard@302
   467
 * Gets details about a package using a varg interface
krh@308
   468
 * The vararg must be terminated with %RAZOR_DETAIL_LAST.
richard@302
   469
 *
richard@307
   470
 * Example: razor_package_get_details (set, package,
richard@307
   471
 *				       RAZOR_DETAIL_URL, &url,
richard@307
   472
 *				       RAZOR_DETAIL_LAST);
richard@302
   473
 **/
krh@269
   474
RAZOR_EXPORT void
richard@302
   475
razor_package_get_details(struct razor_set *set, struct razor_package *package, ...)
jbowes@258
   476
{
richard@302
   477
	va_list args;
jbowes@258
   478
richard@301
   479
	assert (set != NULL);
richard@301
   480
	assert (package != NULL);
richard@301
   481
richard@302
   482
	va_start(args, NULL);
richard@302
   483
	razor_package_get_details_varg (set, package, args);
richard@302
   484
	va_end (args);
jbowes@258
   485
}
jbowes@258
   486
ali@369
   487
/**
ali@369
   488
 * razor_package_remove:
ali@369
   489
 * @set: a %razor_set
ali@369
   490
 * @package: a %razor_package
ali@369
   491
 * @root: the root into which the package is currently installed
ali@369
   492
 *
ali@369
   493
 * Removes an installed package.
ali@369
   494
 **/
ali@369
   495
RAZOR_EXPORT int
ali@369
   496
razor_package_remove(struct razor_set *set, struct razor_package *package,
ali@369
   497
		     const char *root)
ali@369
   498
{
ali@369
   499
	struct razor_file_iterator *fi;
ali@369
   500
	struct razor_package_iterator *pi;
ali@369
   501
	struct razor_package *p;
ali@369
   502
	char buffer[PATH_MAX];
ali@369
   503
	const char *name, *program, *script;
ali@369
   504
	int retval = 0, count;
ali@369
   505
ali@369
   506
	razor_package_get_details(set, package,
ali@369
   507
				  RAZOR_DETAIL_PREUNPROG, &program,
ali@369
   508
				  RAZOR_DETAIL_PREUN, &script,
ali@369
   509
				  RAZOR_DETAIL_LAST);
ali@369
   510
ali@369
   511
	if (razor_run_script(root, RAZOR_PROPERTY_PREUN, program, script))
ali@369
   512
		return -1;
ali@369
   513
ali@369
   514
	fi = razor_file_iterator_create(set, package);
ali@369
   515
ali@369
   516
	while (!retval && razor_file_iterator_next(fi, &name)) {
ali@369
   517
		pi = razor_package_iterator_create_for_file(set, name);
ali@369
   518
		count = 0;
ali@369
   519
		while (razor_package_iterator_next(pi, &p, RAZOR_DETAIL_LAST))
ali@369
   520
			count++;
ali@369
   521
		razor_package_iterator_destroy(pi);
ali@369
   522
		if (count <= 1) {
ali@369
   523
			snprintf(buffer, sizeof buffer, "%s%s", root, name);
ali@369
   524
			retval = remove(buffer);
ali@369
   525
		}
ali@369
   526
	}
ali@369
   527
ali@369
   528
	razor_file_iterator_destroy(fi);
ali@369
   529
ali@369
   530
	if (retval)
ali@369
   531
		return retval;
ali@369
   532
ali@369
   533
	razor_package_get_details(set, package,
ali@369
   534
				  RAZOR_DETAIL_POSTUNPROG, &program,
ali@369
   535
				  RAZOR_DETAIL_POSTUN, &script,
ali@369
   536
				  RAZOR_DETAIL_LAST);
ali@369
   537
ali@369
   538
	return razor_run_script(root, RAZOR_PROPERTY_POSTUN, program, script);
ali@369
   539
}
ali@369
   540
krh@270
   541
RAZOR_EXPORT const char *
krh@270
   542
razor_property_relation_to_string(struct razor_property *p)
krh@270
   543
{
richard@301
   544
	assert (p != NULL);
richard@301
   545
krh@270
   546
	switch (p->flags & RAZOR_PROPERTY_RELATION_MASK) {
krh@270
   547
	case RAZOR_PROPERTY_LESS:
krh@270
   548
		return "<";
krh@270
   549
krh@270
   550
	case RAZOR_PROPERTY_LESS | RAZOR_PROPERTY_EQUAL:
krh@270
   551
		return "<=";
krh@270
   552
krh@270
   553
	case RAZOR_PROPERTY_EQUAL:
krh@270
   554
		return "=";
krh@270
   555
krh@270
   556
	case RAZOR_PROPERTY_GREATER | RAZOR_PROPERTY_EQUAL:
krh@270
   557
		return ">=";
krh@270
   558
krh@270
   559
	case RAZOR_PROPERTY_GREATER:
krh@270
   560
		return ">";
krh@270
   561
krh@270
   562
	default:
krh@270
   563
		return "?";
krh@270
   564
	}
krh@270
   565
}
krh@270
   566
krh@270
   567
RAZOR_EXPORT const char *
krh@270
   568
razor_property_type_to_string(struct razor_property *p)
krh@270
   569
{
richard@301
   570
	assert (p != NULL);
richard@301
   571
krh@270
   572
	switch (p->flags & RAZOR_PROPERTY_TYPE_MASK) {
krh@270
   573
	case RAZOR_PROPERTY_REQUIRES:
krh@270
   574
		return "requires";
krh@270
   575
	case RAZOR_PROPERTY_PROVIDES:
krh@270
   576
		return "provides";
krh@270
   577
	case RAZOR_PROPERTY_CONFLICTS:
krh@270
   578
		return "conflicts";
krh@270
   579
	case RAZOR_PROPERTY_OBSOLETES:
krh@270
   580
		return "obsoletes";
krh@270
   581
	default:
krh@270
   582
		return NULL;
krh@270
   583
	}
krh@270
   584
}
krh@270
   585
krh@269
   586
RAZOR_EXPORT struct razor_entry *
krh@248
   587
razor_set_find_entry(struct razor_set *set,
krh@248
   588
		     struct razor_entry *dir, const char *pattern)
rhughes@241
   589
{
ali@359
   590
	struct razor_entry *e, *subdir;
jbowes@264
   591
	const char *n, *pool = set->file_string_pool.data;
rhughes@241
   592
	int len;
rhughes@241
   593
richard@301
   594
	assert (set != NULL);
richard@301
   595
	assert (dir != NULL);
richard@301
   596
	assert (pattern != NULL);
richard@301
   597
ali@359
   598
	e = dir;
rhughes@241
   599
	do {
rhughes@241
   600
		n = pool + e->name;
ali@359
   601
		if (strcmp(pattern, n) == 0)
rhughes@241
   602
			return e;
rhughes@241
   603
		len = strlen(n);
ali@359
   604
		if (e->start != 0 && strncmp(pattern, n, len) == 0 &&
ali@359
   605
		    pattern[len] == '/') {
ali@359
   606
			subdir = (struct razor_entry *) set->files.data +
ali@359
   607
				 e->start;
ali@359
   608
			return razor_set_find_entry(set, subdir,
ali@359
   609
						    pattern + len + 1);
rhughes@241
   610
		}
rhughes@241
   611
	} while (!((e++)->flags & RAZOR_ENTRY_LAST));
rhughes@241
   612
rhughes@241
   613
	return NULL;
rhughes@241
   614
}
rhughes@241
   615
rhughes@241
   616
static void
rhughes@241
   617
list_dir(struct razor_set *set, struct razor_entry *dir,
rhughes@241
   618
	 char *prefix, const char *pattern)
rhughes@241
   619
{
ali@359
   620
	struct razor_entry *e, *subdir;
jbowes@274
   621
	const char *n, *pool = set->file_string_pool.data;
rhughes@241
   622
ali@359
   623
	e = dir;
rhughes@241
   624
	do {
rhughes@241
   625
		n = pool + e->name;
rhughes@241
   626
		if (pattern && pattern[0] && fnmatch(pattern, n, 0) != 0)
rhughes@241
   627
			continue;
rhughes@241
   628
		printf("%s/%s\n", prefix, n);
rhughes@241
   629
		if (e->start) {
rhughes@241
   630
			char *sub = prefix + strlen (prefix);
rhughes@241
   631
			*sub = '/';
rhughes@241
   632
			strcpy (sub + 1, n);
ali@359
   633
			subdir = (struct razor_entry *) set->files.data +
ali@359
   634
				 e->start;
ali@359
   635
			list_dir(set, subdir, prefix, pattern);
rhughes@241
   636
			*sub = '\0';
rhughes@241
   637
		}
rhughes@241
   638
	} while (!((e++)->flags & RAZOR_ENTRY_LAST));
rhughes@241
   639
}
rhughes@241
   640
krh@269
   641
RAZOR_EXPORT void
rhughes@241
   642
razor_set_list_files(struct razor_set *set, const char *pattern)
rhughes@241
   643
{
ali@359
   644
	struct razor_entry *root, *e;
rhughes@241
   645
	char buffer[512], *p, *base;
rhughes@241
   646
richard@301
   647
	assert (set != NULL);
richard@301
   648
ali@359
   649
	root = (struct razor_entry *) set->files.data;
ali@359
   650
ali@359
   651
	if (pattern == NULL) {
ali@359
   652
		p = set->file_string_pool.data;
ali@359
   653
		e = root;
ali@359
   654
		do {
ali@359
   655
			if (e->start) {
ali@359
   656
				strcpy(buffer, p + e->name);
ali@359
   657
				list_dir(set, root + e->start, buffer, NULL);
ali@359
   658
			}
ali@359
   659
		} while (!((e++)->flags & RAZOR_ENTRY_LAST));
rhughes@241
   660
		return;
rhughes@241
   661
	}
rhughes@241
   662
rhughes@241
   663
	strcpy(buffer, pattern);
ali@359
   664
	e = razor_set_find_entry(set, root, buffer);
ali@359
   665
	if (e && e->start) {
rhughes@241
   666
		base = NULL;
rhughes@241
   667
	} else {
rhughes@241
   668
		p = strrchr(buffer, '/');
rhughes@241
   669
		if (p) {
rhughes@241
   670
			*p = '\0';
rhughes@241
   671
			base = p + 1;
rhughes@241
   672
		} else {
rhughes@241
   673
			base = NULL;
rhughes@241
   674
		}
rhughes@241
   675
	}
ali@359
   676
	e = razor_set_find_entry(set, root, buffer);
ali@359
   677
	if (e && e->start)
ali@359
   678
		list_dir(set, root + e->start, buffer, base);
rhughes@241
   679
}
rhughes@241
   680
krh@269
   681
RAZOR_EXPORT void
krh@306
   682
razor_set_list_package_files(struct razor_set *set,
krh@306
   683
			     struct razor_package *package)
rhughes@241
   684
{
ali@351
   685
	struct razor_file_iterator *fi;
ali@351
   686
	const char *name;
rhughes@241
   687
richard@301
   688
	assert (set != NULL);
krh@306
   689
	assert (package != NULL);
rhughes@241
   690
ali@351
   691
	fi = razor_file_iterator_create(set, package);
ali@351
   692
ali@351
   693
	while (razor_file_iterator_next(fi, &name))
ali@351
   694
		printf("%s\n", name);
ali@351
   695
ali@351
   696
	razor_file_iterator_destroy(fi);
rhughes@241
   697
}
rhughes@241
   698
krh@269
   699
RAZOR_EXPORT void
rhughes@241
   700
razor_set_diff(struct razor_set *set, struct razor_set *upstream,
krh@253
   701
	       razor_diff_callback_t callback, void *data)
rhughes@241
   702
{
rhughes@241
   703
 	struct razor_package_iterator *pi1, *pi2;
rhughes@241
   704
 	struct razor_package *p1, *p2;
rhughes@241
   705
	const char *name1, *name2, *version1, *version2, *arch1, *arch2;
rhughes@241
   706
	int res;
rhughes@241
   707
richard@301
   708
	assert (set != NULL);
richard@301
   709
	assert (upstream != NULL);
richard@301
   710
rhughes@241
   711
	pi1 = razor_package_iterator_create(set);
rhughes@241
   712
	pi2 = razor_package_iterator_create(upstream);
rhughes@241
   713
richard@302
   714
	razor_package_iterator_next(pi1, &p1,
richard@302
   715
				    RAZOR_DETAIL_NAME, &name1,
richard@302
   716
				    RAZOR_DETAIL_VERSION, &version1,
richard@302
   717
				    RAZOR_DETAIL_ARCH, &arch1,
richard@307
   718
				    RAZOR_DETAIL_LAST);
richard@302
   719
	razor_package_iterator_next(pi2, &p2,
richard@302
   720
				    RAZOR_DETAIL_NAME, &name2,
richard@302
   721
				    RAZOR_DETAIL_VERSION, &version2,
richard@302
   722
				    RAZOR_DETAIL_ARCH, &arch2,
richard@307
   723
				    RAZOR_DETAIL_LAST);
rhughes@241
   724
rhughes@241
   725
	while (p1 || p2) {
rhughes@241
   726
		if (p1 && p2) {
rhughes@241
   727
			res = strcmp(name1, name2);
rhughes@241
   728
			if (res == 0)
krh@248
   729
				res = razor_versioncmp(version1, version2);
rhughes@241
   730
		} else {
rhughes@241
   731
			res = 0;
rhughes@241
   732
		}
rhughes@241
   733
rhughes@241
   734
		if (p2 == NULL || res < 0)
krh@253
   735
			callback(RAZOR_DIFF_ACTION_REMOVE,
krh@253
   736
				 p1, name1, version1, arch1, data);
rhughes@241
   737
		else if (p1 == NULL || res > 0)
krh@253
   738
			callback(RAZOR_DIFF_ACTION_ADD,
krh@253
   739
				 p2, name2, version2, arch2, data);
rhughes@241
   740
rhughes@241
   741
		if (p1 != NULL && res <= 0)
rhughes@241
   742
			razor_package_iterator_next(pi1, &p1,
richard@302
   743
						    RAZOR_DETAIL_NAME, &name1,
richard@302
   744
						    RAZOR_DETAIL_VERSION, &version1,
richard@302
   745
						    RAZOR_DETAIL_ARCH, &arch1,
richard@307
   746
						    RAZOR_DETAIL_LAST);
rhughes@241
   747
		if (p2 != NULL && res >= 0)
rhughes@241
   748
			razor_package_iterator_next(pi2, &p2,
richard@302
   749
						    RAZOR_DETAIL_NAME, &name2,
richard@302
   750
						    RAZOR_DETAIL_VERSION, &version2,
richard@302
   751
						    RAZOR_DETAIL_ARCH, &arch2,
richard@307
   752
						    RAZOR_DETAIL_LAST);
rhughes@241
   753
	}
rhughes@241
   754
rhughes@241
   755
	razor_package_iterator_destroy(pi1);
rhughes@241
   756
	razor_package_iterator_destroy(pi2);
rhughes@241
   757
}
krh@254
   758
krh@316
   759
struct install_action {
krh@316
   760
	enum razor_install_action action;
krh@316
   761
	struct razor_package *package;
krh@316
   762
};
krh@316
   763
krh@316
   764
struct razor_install_iterator {
krh@316
   765
	struct razor_set *set;
krh@316
   766
	struct razor_set *next;
krh@316
   767
	struct array actions;
ali@367
   768
	struct deque *order;
krh@316
   769
};
krh@316
   770
krh@254
   771
static void
krh@316
   772
add_action(enum razor_diff_action action,
krh@316
   773
	   struct razor_package *package,
krh@316
   774
	   const char *name,
krh@316
   775
	   const char *version,
krh@316
   776
	   const char *arch,
krh@316
   777
	   void *data)
krh@254
   778
{
krh@316
   779
	struct razor_install_iterator *ii = data;
krh@316
   780
	struct install_action *a;
krh@316
   781
krh@316
   782
	a = array_add(&ii->actions, sizeof *a);
krh@316
   783
	a->package = package;
krh@316
   784
krh@316
   785
	switch (action) {
krh@316
   786
	case RAZOR_DIFF_ACTION_ADD:
krh@316
   787
		a->action = RAZOR_INSTALL_ACTION_ADD;
krh@316
   788
		break;
krh@316
   789
	case RAZOR_DIFF_ACTION_REMOVE:
krh@316
   790
		a->action = RAZOR_INSTALL_ACTION_REMOVE;
krh@316
   791
		break;
krh@316
   792
	}
krh@254
   793
}
krh@254
   794
krh@316
   795
RAZOR_EXPORT struct razor_install_iterator *
krh@316
   796
razor_set_create_install_iterator(struct razor_set *set,
krh@316
   797
				  struct razor_set *next)
krh@254
   798
{
krh@316
   799
	struct razor_install_iterator *ii;
ali@367
   800
	struct razor_property *prop;
ali@367
   801
	/* A graph of the actions to be perfomed where
ali@367
   802
	 * A->B means action A should follow action B.
ali@367
   803
	 */
ali@367
   804
	struct graph follows;
ali@367
   805
	struct install_action *actions, *ai, *aj;
ali@367
   806
	int i, j, count, vertex_added;
ali@367
   807
	struct list *link;
ali@367
   808
	struct razor_set *rs;
krh@254
   809
richard@301
   810
	assert (set != NULL);
richard@301
   811
	assert (next != NULL);
richard@301
   812
krh@316
   813
	ii = zalloc(sizeof *ii);
krh@316
   814
	ii->set = set;
krh@316
   815
	ii->next = next;
krh@316
   816
	
krh@316
   817
	razor_set_diff(set, next, add_action, ii);
krh@254
   818
ali@367
   819
	actions = ii->actions.data;
ali@367
   820
	count = ii->actions.size / sizeof (struct install_action);
krh@254
   821
ali@367
   822
	graph_init(&follows);
ali@367
   823
ali@367
   824
	for(i = 0; i < count; i++) {
ali@367
   825
		ai = actions + i;
ali@367
   826
		rs = ai->action == RAZOR_INSTALL_ACTION_ADD ? next : set;
ali@367
   827
		vertex_added = 0;
ali@367
   828
		link = list_first(&ai->package->properties, &rs->property_pool);
ali@367
   829
		for(; link; link = list_next(link)) {
ali@367
   830
			prop = rs->properties.data;
ali@367
   831
			prop += link->data;
ali@367
   832
			switch(prop->flags & RAZOR_PROPERTY_TYPE_MASK) {
ali@367
   833
			case RAZOR_PROPERTY_REQUIRES:
ali@367
   834
			case RAZOR_PROPERTY_CONFLICTS:
ali@367
   835
				for(j = 0; j < count; j++) {
ali@367
   836
					if (j == i)
ali@367
   837
						continue;
ali@367
   838
					aj = actions + j;
ali@367
   839
					if (aj->package->name == prop->name) {
ali@367
   840
						if (ai->action ==
ali@367
   841
						    RAZOR_INSTALL_ACTION_ADD)
ali@367
   842
							graph_add_edge(&follows,
ali@367
   843
								       i, j);
ali@367
   844
						else
ali@367
   845
							graph_add_edge(&follows,
ali@367
   846
								       j, i);
ali@367
   847
						vertex_added++;
ali@367
   848
					}
ali@367
   849
				}
ali@367
   850
				break;
ali@367
   851
			}
ali@367
   852
		}
ali@367
   853
		if (ai->action == RAZOR_INSTALL_ACTION_ADD) {
ali@367
   854
			for(j = 0; j < count; j++) {
ali@367
   855
				if (j == i)
ali@367
   856
					continue;
ali@367
   857
				aj = actions + j;
ali@367
   858
				if (aj->package == ai->package &&
ali@367
   859
				    aj->action == RAZOR_INSTALL_ACTION_REMOVE) {
ali@367
   860
					graph_add_edge(&follows, i, j);
ali@367
   861
					vertex_added++;
ali@367
   862
				}
ali@367
   863
			}
ali@367
   864
		}
ali@367
   865
		if (!vertex_added)
ali@367
   866
			graph_add_edge(&follows, i, i);
ali@367
   867
	}
ali@367
   868
ali@367
   869
	ii->order = graph_sort(&follows);
ali@367
   870
	graph_release(&follows);
krh@254
   871
krh@316
   872
	return ii;
krh@254
   873
}
krh@254
   874
krh@316
   875
RAZOR_EXPORT int
krh@316
   876
razor_install_iterator_next(struct razor_install_iterator *ii,
krh@316
   877
			    struct razor_set **set,
krh@316
   878
			    struct razor_package **package,
krh@316
   879
			    enum razor_install_action *action,
krh@316
   880
			    int *count)
krh@254
   881
{
ali@367
   882
	struct install_action *a;
ali@367
   883
	if (deque_empty(ii->order))
krh@316
   884
		return 0;
krh@254
   885
ali@367
   886
	a = (struct install_action *)ii->actions.data + deque_pop(ii->order);
ali@367
   887
	switch (a->action) {
krh@316
   888
	case RAZOR_INSTALL_ACTION_ADD:
krh@316
   889
		*set = ii->next;
krh@316
   890
		break;
krh@316
   891
	case RAZOR_INSTALL_ACTION_REMOVE:
krh@316
   892
		*set = ii->set;
krh@316
   893
		break;
krh@316
   894
	}
richard@301
   895
ali@367
   896
	*package = a->package;
ali@367
   897
	*action = a->action;
krh@316
   898
	*count = 0;
krh@254
   899
krh@316
   900
	return 1;
krh@316
   901
}
krh@254
   902
krh@316
   903
RAZOR_EXPORT void
krh@316
   904
razor_install_iterator_destroy(struct razor_install_iterator *ii)
krh@316
   905
{
krh@316
   906
	array_release(&ii->actions);
ali@367
   907
	deque_free(ii->order);
krh@316
   908
	free(ii);
krh@254
   909
}