src/main.c
author J. Ali Harlow <ali@juiblex.co.uk>
Thu Feb 09 20:15:00 2012 +0000 (2012-02-09)
changeset 415 fbd3a02dcfde
parent 403 e63951c1d0f8
child 418 33b825d3128d
permissions -rw-r--r--
Add mkdtemp to gnulib modules used
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@403
     4
 * Copyright (C) 2009, 2011  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
krh@305
    21
#define _GNU_SOURCE
krh@305
    22
ali@321
    23
#include "config.h"
ali@321
    24
rhughes@241
    25
#include <stdlib.h>
rhughes@241
    26
#include <stddef.h>
rhughes@241
    27
#include <stdio.h>
rhughes@241
    28
#include <stdint.h>
rhughes@241
    29
#include <string.h>
rhughes@241
    30
#include <sys/stat.h>
rhughes@241
    31
#include <unistd.h>
rhughes@241
    32
#include <fcntl.h>
rhughes@241
    33
#include <dirent.h>
ali@325
    34
#include <limits.h>
ali@321
    35
#ifdef HAVE_CURL
rhughes@241
    36
#include <curl/curl.h>
ali@321
    37
#endif
rhughes@241
    38
#include <fnmatch.h>
rhughes@241
    39
#include <errno.h>
rhughes@241
    40
#include "razor.h"
rhughes@241
    41
richard@310
    42
static const char system_repo_filename[] = "system.rzdb";
richard@310
    43
static const char next_repo_filename[] = "system-next.rzdb";
richard@310
    44
static const char rawhide_repo_filename[] = "rawhide.rzdb";
krh@317
    45
static const char *install_root = "";
rhughes@241
    46
static const char *repo_filename = system_repo_filename;
rhughes@241
    47
static const char *yum_url;
rhughes@241
    48
krh@271
    49
#define ARRAY_SIZE(a) (sizeof(a) / sizeof((a)[0]))
krh@271
    50
ali@412
    51
enum update_pass_type {
ali@412
    52
    UPDATE_PASS_PRE_REMOVE,
ali@412
    53
    UPDATE_PASS_MAIN,
ali@412
    54
    UPDATE_PASS_POST_INSTALL,
ali@412
    55
};
ali@412
    56
ali@363
    57
static int
ali@403
    58
update_packages(struct razor_transaction *trans,
ali@403
    59
		struct razor_install_iterator *ii, struct razor_set *system,
ali@403
    60
		struct razor_set *next, struct razor_atomic *atomic,
ali@403
    61
		struct razor_relocations *relocations,
ali@412
    62
		enum update_pass_type stage);
ali@363
    63
krh@281
    64
static struct razor_package_iterator *
krh@281
    65
create_iterator_from_argv(struct razor_set *set, int argc, const char *argv[])
rhughes@241
    66
{
krh@281
    67
	struct razor_package_query *query;
krh@281
    68
	struct razor_package_iterator *iter;
rhughes@241
    69
	struct razor_package *package;
richard@302
    70
	const char *name, *pattern;
krh@281
    71
	int i, count;
rhughes@241
    72
krh@281
    73
	if (argc == 0)
krh@281
    74
		return razor_package_iterator_create(set);
krh@281
    75
krh@281
    76
	query = razor_package_query_create(set);
krh@281
    77
krh@281
    78
	for (i = 0; i < argc; i++) {
krh@281
    79
		iter = razor_package_iterator_create(set);
krh@281
    80
		pattern = argv[i];
krh@281
    81
		count = 0;
richard@307
    82
		while (razor_package_iterator_next(iter, &package,
richard@307
    83
						   RAZOR_DETAIL_NAME, &name,
richard@307
    84
						   RAZOR_DETAIL_LAST)) {
krh@281
    85
			if (fnmatch(pattern, name, 0) != 0)
krh@281
    86
				continue;
krh@281
    87
krh@281
    88
			razor_package_query_add_package(query, package);
krh@281
    89
			count++;
krh@281
    90
		}
krh@281
    91
		razor_package_iterator_destroy(iter);
krh@281
    92
krh@281
    93
		if (count == 0)
krh@281
    94
			fprintf(stderr,
krh@281
    95
				"no package matches \"%s\"\n", pattern);
rhughes@241
    96
	}
rhughes@241
    97
krh@281
    98
	return razor_package_query_finish(query);
krh@281
    99
}
krh@281
   100
krh@281
   101
#define LIST_PACKAGES_ONLY_NAMES 0x01
krh@281
   102
krh@281
   103
static void
krh@281
   104
list_packages(struct razor_package_iterator *iter, uint32_t flags)
krh@281
   105
{
krh@281
   106
	struct razor_package *package;
krh@281
   107
	const char *name, *version, *arch;
krh@281
   108
krh@281
   109
	while (razor_package_iterator_next(iter, &package,
richard@302
   110
					   RAZOR_DETAIL_NAME, &name,
richard@302
   111
					   RAZOR_DETAIL_VERSION, &version,
richard@307
   112
					   RAZOR_DETAIL_ARCH, &arch,
richard@307
   113
					   RAZOR_DETAIL_LAST)) {
krh@281
   114
		if (flags & LIST_PACKAGES_ONLY_NAMES)
rhughes@241
   115
			printf("%s\n", name);
rhughes@241
   116
		else
rhughes@241
   117
			printf("%s-%s.%s\n", name, version, arch);
rhughes@241
   118
	}
krh@281
   119
}
krh@281
   120
krh@281
   121
static int
krh@281
   122
command_list(int argc, const char *argv[])
krh@281
   123
{
krh@281
   124
	struct razor_package_iterator *pi;
ali@403
   125
	struct razor_atomic *atomic;
krh@281
   126
	struct razor_set *set;
krh@281
   127
	uint32_t flags = 0;
krh@281
   128
	int i = 0;
krh@281
   129
krh@281
   130
	if (i < argc && strcmp(argv[i], "--only-names") == 0) {
krh@281
   131
		flags |= LIST_PACKAGES_ONLY_NAMES;
krh@281
   132
		i++;
krh@281
   133
	}
krh@281
   134
ali@403
   135
	atomic = razor_atomic_open("List installed packages");
ali@403
   136
	set = razor_root_open_read_only(install_root, atomic);
ali@403
   137
	if (set == NULL) {
ali@403
   138
		fprintf(stderr, "%s\n", razor_atomic_get_error_msg(atomic));
ali@403
   139
		razor_atomic_destroy(atomic);
krh@317
   140
		return 1;
ali@403
   141
	}
krh@317
   142
krh@281
   143
	pi = create_iterator_from_argv(set, argc - i, argv + i);
krh@281
   144
	list_packages(pi, flags);
rhughes@241
   145
	razor_package_iterator_destroy(pi);
ali@403
   146
	razor_set_unref(set);
ali@403
   147
	razor_atomic_destroy(atomic);
rhughes@241
   148
rhughes@241
   149
	return 0;
rhughes@241
   150
}
rhughes@241
   151
krh@306
   152
static void
krh@306
   153
list_package_properties(struct razor_set *set,
krh@306
   154
			struct razor_package *package, uint32_t type)
rhughes@241
   155
{
krh@306
   156
	struct razor_property_iterator *pi;
rhughes@241
   157
	struct razor_property *property;
rhughes@241
   158
	const char *name, *version;
krh@247
   159
	uint32_t flags;
rhughes@241
   160
rhughes@241
   161
	pi = razor_property_iterator_create(set, package);
rhughes@241
   162
	while (razor_property_iterator_next(pi, &property,
krh@247
   163
					    &name, &flags, &version)) {
krh@247
   164
		if ((flags & RAZOR_PROPERTY_TYPE_MASK) != type)
rhughes@241
   165
			continue;
krh@247
   166
		printf("%s", name);
krh@247
   167
		if (version[0] != '\0')
krh@247
   168
			printf(" %s %s",
krh@247
   169
			       razor_property_relation_to_string(property),
krh@247
   170
			       version);
krh@247
   171
krh@247
   172
		if (flags & ~(RAZOR_PROPERTY_RELATION_MASK | RAZOR_PROPERTY_TYPE_MASK)) {
krh@247
   173
			printf(" [");
krh@247
   174
			if (flags & RAZOR_PROPERTY_PRE)
krh@247
   175
				printf(" pre");
krh@247
   176
			if (flags & RAZOR_PROPERTY_POST)
krh@247
   177
				printf(" post");
krh@247
   178
			if (flags & RAZOR_PROPERTY_PREUN)
krh@247
   179
				printf(" preun");
krh@247
   180
			if (flags & RAZOR_PROPERTY_POSTUN)
krh@247
   181
				printf(" postun");
krh@247
   182
			printf(" ]");
krh@247
   183
		}
krh@247
   184
		printf("\n");
rhughes@241
   185
	}
rhughes@241
   186
	razor_property_iterator_destroy(pi);
krh@306
   187
}
rhughes@241
   188
krh@306
   189
static int
krh@306
   190
list_properties(int argc, const char *argv[], uint32_t type)
krh@306
   191
{
krh@306
   192
	struct razor_set *set;
ali@403
   193
	struct razor_atomic *atomic;
krh@306
   194
	struct razor_package *package;
krh@306
   195
	struct razor_package_iterator *pi;
krh@306
   196
	const char *name, *version, *arch;
krh@306
   197
ali@403
   198
	atomic = razor_atomic_open("List package properties");
ali@403
   199
	set = razor_root_open_read_only(install_root, atomic);
ali@403
   200
	if (set == NULL) {
ali@403
   201
		fprintf(stderr, "%s\n", razor_atomic_get_error_msg(atomic));
ali@403
   202
		razor_atomic_destroy(atomic);
krh@317
   203
		return 1;
ali@403
   204
	}
krh@317
   205
krh@306
   206
	pi = create_iterator_from_argv(set, argc, argv);
krh@306
   207
	while (razor_package_iterator_next(pi, &package,
richard@307
   208
					   RAZOR_DETAIL_NAME, &name,
richard@307
   209
					   RAZOR_DETAIL_VERSION, &version,
richard@307
   210
					   RAZOR_DETAIL_ARCH, &arch,
richard@307
   211
					   RAZOR_DETAIL_LAST))
krh@306
   212
		list_package_properties(set, package, type);
krh@306
   213
	razor_package_iterator_destroy(pi);
ali@403
   214
	razor_set_unref(set);
ali@403
   215
	razor_atomic_destroy(atomic);
rhughes@241
   216
rhughes@241
   217
	return 0;
rhughes@241
   218
}
rhughes@241
   219
rhughes@241
   220
static int
rhughes@241
   221
command_list_requires(int argc, const char *argv[])
rhughes@241
   222
{
krh@306
   223
	return list_properties(argc, argv, RAZOR_PROPERTY_REQUIRES);
rhughes@241
   224
}
rhughes@241
   225
rhughes@241
   226
static int
rhughes@241
   227
command_list_provides(int argc, const char *argv[])
rhughes@241
   228
{
krh@306
   229
	return list_properties(argc, argv, RAZOR_PROPERTY_PROVIDES);
rhughes@241
   230
}
rhughes@241
   231
rhughes@241
   232
static int
rhughes@241
   233
command_list_obsoletes(int argc, const char *argv[])
rhughes@241
   234
{
krh@306
   235
	return list_properties(argc, argv, RAZOR_PROPERTY_OBSOLETES);
rhughes@241
   236
}
rhughes@241
   237
rhughes@241
   238
static int
rhughes@241
   239
command_list_conflicts(int argc, const char *argv[])
rhughes@241
   240
{
krh@306
   241
	return list_properties(argc, argv, RAZOR_PROPERTY_CONFLICTS);
rhughes@241
   242
}
rhughes@241
   243
rhughes@241
   244
static int
ali@369
   245
command_list_scripts(int argc, const char *argv[])
ali@369
   246
{
ali@369
   247
	struct razor_set *set;
ali@403
   248
	struct razor_atomic *atomic;
ali@369
   249
	struct razor_package *package;
ali@369
   250
	struct razor_package_iterator *pi;
ali@369
   251
	const char *preunprog, *preun, *postunprog, *postun;
ali@369
   252
ali@403
   253
	atomic = razor_atomic_open("List package scripts");
ali@403
   254
	set = razor_root_open_read_only(install_root, atomic);
ali@403
   255
	if (set == NULL) {
ali@403
   256
		fprintf(stderr, "%s\n", razor_atomic_get_error_msg(atomic));
ali@403
   257
		razor_atomic_destroy(atomic);
ali@369
   258
		return 1;
ali@403
   259
	}
ali@369
   260
ali@369
   261
	pi = create_iterator_from_argv(set, argc, argv);
ali@369
   262
	while (razor_package_iterator_next(pi, &package,
ali@369
   263
					   RAZOR_DETAIL_PREUNPROG, &preunprog,
ali@369
   264
					   RAZOR_DETAIL_PREUN, &preun,
ali@369
   265
					   RAZOR_DETAIL_POSTUNPROG, &postunprog,
ali@369
   266
					   RAZOR_DETAIL_POSTUN, &postun,
ali@369
   267
					   RAZOR_DETAIL_LAST)) {
ali@369
   268
		if (preun && *preun) {
ali@369
   269
			printf("preuninstall scriptlet");
ali@369
   270
			if (preunprog && *preunprog)
ali@369
   271
				printf(" (using %s)",preunprog);
ali@369
   272
			printf(":\n%s\n",preun);
ali@369
   273
		}
ali@369
   274
		if (postun && *postun) {
ali@369
   275
			printf("postuninstall scriptlet");
ali@369
   276
			if (postunprog && *postunprog)
ali@369
   277
				printf(" (using %s)",postunprog);
ali@369
   278
			printf(":\n%s\n",postun);
ali@369
   279
		}
ali@369
   280
	}
ali@369
   281
	razor_package_iterator_destroy(pi);
ali@403
   282
	razor_set_unref(set);
ali@403
   283
	razor_atomic_destroy(atomic);
ali@369
   284
ali@369
   285
	return 0;
ali@369
   286
}
ali@369
   287
ali@369
   288
static int
rhughes@241
   289
command_list_files(int argc, const char *argv[])
rhughes@241
   290
{
ali@403
   291
	struct razor_atomic *atomic;
rhughes@241
   292
	struct razor_set *set;
rhughes@241
   293
ali@403
   294
	atomic = razor_atomic_open("List package files");
ali@403
   295
	set = razor_root_open_read_only(install_root, atomic);
ali@403
   296
	if (set == NULL) {
ali@403
   297
		fprintf(stderr, "%s\n", razor_atomic_get_error_msg(atomic));
ali@403
   298
		razor_atomic_destroy(atomic);
rhughes@241
   299
		return 1;
ali@403
   300
	}
krh@317
   301
rhughes@241
   302
	razor_set_list_files(set, argv[0]);
ali@403
   303
	razor_set_unref(set);
ali@403
   304
	razor_atomic_destroy(atomic);
rhughes@241
   305
rhughes@241
   306
	return 0;
rhughes@241
   307
}
rhughes@241
   308
rhughes@241
   309
static int
rhughes@241
   310
command_list_file_packages(int argc, const char *argv[])
rhughes@241
   311
{
ali@403
   312
	struct razor_atomic *atomic;
rhughes@241
   313
	struct razor_set *set;
rhughes@241
   314
	struct razor_package_iterator *pi;
rhughes@241
   315
ali@403
   316
	atomic = razor_atomic_open("List file packages");
ali@403
   317
	set = razor_root_open_read_only(install_root, atomic);
ali@403
   318
	if (set == NULL) {
ali@403
   319
		fprintf(stderr, "%s\n", razor_atomic_get_error_msg(atomic));
ali@403
   320
		razor_atomic_destroy(atomic);
rhughes@241
   321
		return 1;
ali@403
   322
	}
rhughes@241
   323
rhughes@241
   324
	pi = razor_package_iterator_create_for_file(set, argv[0]);
krh@281
   325
	list_packages(pi, 0);
rhughes@241
   326
	razor_package_iterator_destroy(pi);
rhughes@241
   327
ali@403
   328
	razor_set_unref(set);
ali@403
   329
	razor_atomic_destroy(atomic);
rhughes@241
   330
rhughes@241
   331
	return 0;
rhughes@241
   332
}
rhughes@241
   333
rhughes@241
   334
static int
rhughes@241
   335
command_list_package_files(int argc, const char *argv[])
rhughes@241
   336
{
ali@403
   337
	struct razor_atomic *atomic;
rhughes@241
   338
	struct razor_set *set;
krh@306
   339
	struct razor_package_iterator *pi;
krh@306
   340
	struct razor_package *package;
krh@306
   341
	const char *name, *version, *arch;
rhughes@241
   342
ali@403
   343
	atomic = razor_atomic_open("List package files");
ali@403
   344
	set = razor_root_open_read_only(install_root, atomic);
ali@403
   345
	if (set == NULL) {
ali@403
   346
		fprintf(stderr, "%s\n", razor_atomic_get_error_msg(atomic));
ali@403
   347
		razor_atomic_destroy(atomic);
rhughes@241
   348
		return 1;
ali@403
   349
	}
jbowes@288
   350
krh@306
   351
	pi = create_iterator_from_argv(set, argc, argv);
krh@306
   352
	while (razor_package_iterator_next(pi, &package,
richard@307
   353
					   RAZOR_DETAIL_NAME, &name,
richard@307
   354
					   RAZOR_DETAIL_VERSION, &version,
richard@307
   355
					   RAZOR_DETAIL_ARCH, &arch,
richard@307
   356
					   RAZOR_DETAIL_LAST))
krh@306
   357
		razor_set_list_package_files(set, package);
krh@306
   358
	razor_package_iterator_destroy(pi);
krh@306
   359
ali@403
   360
	razor_set_unref(set);
ali@403
   361
	razor_atomic_destroy(atomic);
rhughes@241
   362
rhughes@241
   363
	return 0;
rhughes@241
   364
}
rhughes@241
   365
rhughes@241
   366
static int
rhughes@241
   367
list_property_packages(const char *ref_name,
rhughes@241
   368
		       const char *ref_version,
krh@247
   369
		       uint32_t type)
rhughes@241
   370
{
ali@403
   371
	struct razor_atomic *atomic;
rhughes@241
   372
	struct razor_set *set;
rhughes@241
   373
	struct razor_property *property;
krh@281
   374
	struct razor_property_iterator *prop_iter;
krh@281
   375
	struct razor_package_iterator *pkg_iter;
rhughes@241
   376
	const char *name, *version;
krh@247
   377
	uint32_t flags;
rhughes@241
   378
rhughes@241
   379
	if (ref_name == NULL)
rhughes@241
   380
		return 0;
rhughes@241
   381
ali@403
   382
	atomic = razor_atomic_open("List package properties");
ali@403
   383
	set = razor_root_open_read_only(install_root, atomic);
ali@403
   384
	if (set == NULL) {
ali@403
   385
		fprintf(stderr, "%s\n", razor_atomic_get_error_msg(atomic));
ali@403
   386
		razor_atomic_destroy(atomic);
rhughes@241
   387
		return 1;
ali@403
   388
	}
rhughes@241
   389
krh@281
   390
	prop_iter = razor_property_iterator_create(set, NULL);
krh@281
   391
	while (razor_property_iterator_next(prop_iter, &property,
krh@247
   392
					    &name, &flags, &version)) {
rhughes@241
   393
		if (strcmp(ref_name, name) != 0)
rhughes@241
   394
			continue;
krh@247
   395
		if (ref_version &&
krh@247
   396
		    (flags & RAZOR_PROPERTY_RELATION_MASK) == RAZOR_PROPERTY_EQUAL &&
rhughes@241
   397
		    strcmp(ref_version, version) != 0)
rhughes@241
   398
			continue;
krh@247
   399
		if ((flags & RAZOR_PROPERTY_TYPE_MASK) != type)
rhughes@241
   400
			continue;
rhughes@241
   401
krh@281
   402
		pkg_iter =
krh@281
   403
			razor_package_iterator_create_for_property(set,
krh@281
   404
								   property);
krh@281
   405
		list_packages(pkg_iter, 0);
krh@281
   406
		razor_package_iterator_destroy(pkg_iter);
rhughes@241
   407
	}
krh@281
   408
	razor_property_iterator_destroy(prop_iter);
rhughes@241
   409
ali@403
   410
	razor_set_unref(set);
ali@403
   411
	razor_atomic_destroy(atomic);
krh@317
   412
rhughes@241
   413
	return 0;
rhughes@241
   414
}
rhughes@241
   415
rhughes@241
   416
static int
rhughes@241
   417
command_what_requires(int argc, const char *argv[])
rhughes@241
   418
{
rhughes@241
   419
	return list_property_packages(argv[0], argv[1],
rhughes@241
   420
				      RAZOR_PROPERTY_REQUIRES);
rhughes@241
   421
}
rhughes@241
   422
rhughes@241
   423
static int
rhughes@241
   424
command_what_provides(int argc, const char *argv[])
rhughes@241
   425
{
rhughes@241
   426
	return list_property_packages(argv[0], argv[1],
rhughes@241
   427
				      RAZOR_PROPERTY_PROVIDES);
rhughes@241
   428
}
rhughes@241
   429
rhughes@241
   430
static int
rhughes@241
   431
show_progress(void *clientp,
rhughes@241
   432
	      double dltotal, double dlnow, double ultotal, double ulnow)
rhughes@241
   433
{
rhughes@241
   434
	const char *file = clientp;
rhughes@241
   435
rhughes@241
   436
	if (!dlnow < dltotal)
rhughes@241
   437
		fprintf(stderr, "\rdownloading %s, %dkB/%dkB",
rhughes@241
   438
			file, (int) dlnow / 1024, (int) dltotal / 1024);
rhughes@241
   439
rhughes@241
   440
	return 0;
rhughes@241
   441
}
rhughes@241
   442
rhughes@241
   443
static int
rhughes@241
   444
download_if_missing(const char *url, const char *file)
rhughes@241
   445
{
ali@321
   446
#ifndef HAVE_CURL
ali@321
   447
	return 1;
ali@321
   448
#else
rhughes@241
   449
	CURL *curl;
rhughes@241
   450
	struct stat buf;
rhughes@241
   451
	char error[256];
rhughes@241
   452
	FILE *fp;
rhughes@241
   453
	CURLcode res;
rhughes@241
   454
	long response;
rhughes@241
   455
rhughes@241
   456
	curl = curl_easy_init();
rhughes@241
   457
	if (curl == NULL)
rhughes@241
   458
		return 1;
rhughes@241
   459
rhughes@241
   460
	curl_easy_setopt(curl, CURLOPT_ERRORBUFFER, error);
rhughes@241
   461
	curl_easy_setopt(curl, CURLOPT_NOPROGRESS, 0);
rhughes@241
   462
	curl_easy_setopt(curl, CURLOPT_PROGRESSFUNCTION, show_progress);
rhughes@241
   463
	curl_easy_setopt(curl, CURLOPT_PROGRESSDATA, file);
rhughes@241
   464
rhughes@241
   465
	if (stat(file, &buf) < 0) {
ali@345
   466
		fp = fopen(file, "wb");
rhughes@241
   467
		if (fp == NULL) {
rhughes@241
   468
			fprintf(stderr,
rhughes@241
   469
				"failed to open %s for writing\n", file);
rhughes@241
   470
			return -1;
rhughes@241
   471
		}
rhughes@241
   472
		curl_easy_setopt(curl, CURLOPT_WRITEDATA, fp);
rhughes@241
   473
		curl_easy_setopt(curl, CURLOPT_URL, url);
rhughes@241
   474
		res = curl_easy_perform(curl);
rhughes@241
   475
		fclose(fp);
rhughes@241
   476
		if (res != CURLE_OK) {
rhughes@241
   477
			fprintf(stderr, "curl error: %s\n", error);
rhughes@241
   478
			unlink(file);
rhughes@241
   479
			return -1;
rhughes@241
   480
		}
rhughes@241
   481
		res = curl_easy_getinfo(curl,
rhughes@241
   482
					CURLINFO_RESPONSE_CODE, &response);
rhughes@241
   483
		if (res != CURLE_OK) {
rhughes@241
   484
			fprintf(stderr, "curl error: %s\n", error);
rhughes@241
   485
                        unlink(file);
rhughes@241
   486
                        return -1;
rhughes@241
   487
		}
rhughes@241
   488
		if (response != 200) {
rhughes@241
   489
			fprintf(stderr, " - failed %ld\n", response);
rhughes@241
   490
                        unlink(file);
rhughes@241
   491
                        return -1;
rhughes@241
   492
		}
rhughes@241
   493
		fprintf(stderr, "\n");
rhughes@241
   494
	}
rhughes@241
   495
rhughes@241
   496
	curl_easy_cleanup(curl);
rhughes@241
   497
rhughes@241
   498
	return 0;
ali@321
   499
#endif	/* HAVE_CURL */
rhughes@241
   500
}
rhughes@241
   501
rhughes@241
   502
#define YUM_URL "http://download.fedora.redhat.com" \
rhughes@241
   503
	"/pub/fedora/linux/development/i386/os"
rhughes@241
   504
rhughes@241
   505
static int
rhughes@241
   506
command_import_yum(int argc, const char *argv[])
rhughes@241
   507
{
ali@403
   508
	int retval;
rhughes@241
   509
	struct razor_set *set;
ali@403
   510
	struct razor_atomic *atomic;
rhughes@241
   511
	char buffer[512];
rhughes@241
   512
rhughes@241
   513
	printf("downloading from %s.\n", yum_url);
rhughes@241
   514
	snprintf(buffer, sizeof buffer,
rhughes@241
   515
		 "%s/repodata/primary.xml.gz", yum_url);
rhughes@241
   516
	if (download_if_missing(buffer, "primary.xml.gz") < 0)
rhughes@241
   517
		return -1;
rhughes@241
   518
	snprintf(buffer, sizeof buffer,
rhughes@241
   519
		 "%s/repodata/filelists.xml.gz", yum_url);
rhughes@241
   520
	if (download_if_missing(buffer, "filelists.xml.gz") < 0)
rhughes@241
   521
		return -1;
rhughes@241
   522
rhughes@241
   523
	set = razor_set_create_from_yum();
rhughes@241
   524
	if (set == NULL)
rhughes@241
   525
		return 1;
ali@403
   526
	atomic = razor_atomic_open("Yum import repository");
ali@403
   527
	razor_set_write(set, atomic, rawhide_repo_filename, RAZOR_SECTION_ALL);
ali@403
   528
	retval = razor_atomic_commit(atomic);
ali@403
   529
	razor_set_unref(set);
ali@403
   530
	if (retval)
ali@403
   531
		fprintf(stderr, "%s\n", razor_atomic_get_error_msg(atomic));
ali@403
   532
	else
ali@403
   533
		printf("wrote %s\n", rawhide_repo_filename);
ali@403
   534
	razor_atomic_destroy(atomic);
rhughes@241
   535
ali@403
   536
	return retval;
rhughes@241
   537
}
rhughes@241
   538
ali@320
   539
#if HAVE_RPMLIB
rhughes@241
   540
static int
rhughes@241
   541
command_import_rpmdb(int argc, const char *argv[])
rhughes@241
   542
{
rhughes@241
   543
	struct razor_set *set;
krh@317
   544
	struct razor_root *root;
ali@403
   545
	struct razor_atomic *atomic;
ali@403
   546
	int retval;
krh@317
   547
ali@403
   548
	atomic = razor_atomic_open("Import RPM database");
ali@403
   549
ali@403
   550
	root = razor_root_open(install_root, atomic);
ali@403
   551
	if (root == NULL) {
ali@403
   552
		fprintf(stderr, "%s\n", razor_atomic_get_error_msg(atomic));
ali@403
   553
		razor_atomic_destroy(atomic);
krh@317
   554
		return 1;
ali@403
   555
	}
rhughes@241
   556
rhughes@241
   557
	set = razor_set_create_from_rpmdb();
rhughes@241
   558
	if (set == NULL)
rhughes@241
   559
		return 1;
rhughes@241
   560
krh@317
   561
	razor_root_update(root, set);
krh@317
   562
ali@403
   563
	retval = razor_root_commit(root);
ali@403
   564
	if (retval)
ali@403
   565
		fprintf(stderr, "%s\n", razor_atomic_get_error_msg(atomic));
ali@403
   566
ali@403
   567
	razor_atomic_destroy(atomic);
ali@403
   568
ali@403
   569
	return retval;
rhughes@241
   570
}
ali@320
   571
#endif
rhughes@241
   572
rhughes@241
   573
static int
rhughes@241
   574
mark_packages_for_update(struct razor_transaction *trans,
rhughes@241
   575
			 struct razor_set *set, const char *pattern)
rhughes@241
   576
{
rhughes@241
   577
	struct razor_package_iterator *pi;
rhughes@241
   578
	struct razor_package *package;
richard@302
   579
	const char *name;
rhughes@241
   580
	int matches = 0;
rhughes@241
   581
rhughes@241
   582
	pi = razor_package_iterator_create(set);
rhughes@241
   583
	while (razor_package_iterator_next(pi, &package,
richard@307
   584
					   RAZOR_DETAIL_NAME, &name,
richard@307
   585
					   RAZOR_DETAIL_LAST)) {
rhughes@241
   586
		if (pattern && fnmatch(pattern, name, 0) == 0) {
rhughes@241
   587
			razor_transaction_update_package(trans, package);
rhughes@241
   588
			matches++;
rhughes@241
   589
		}
rhughes@241
   590
	}
rhughes@241
   591
	razor_package_iterator_destroy(pi);
rhughes@241
   592
rhughes@241
   593
	return matches;
rhughes@241
   594
}
rhughes@241
   595
rhughes@241
   596
static int
rhughes@241
   597
mark_packages_for_removal(struct razor_transaction *trans,
rhughes@241
   598
			  struct razor_set *set, const char *pattern)
rhughes@241
   599
{
rhughes@241
   600
	struct razor_package_iterator *pi;
rhughes@241
   601
	struct razor_package *package;
richard@302
   602
	const char *name;
rhughes@241
   603
	int matches = 0;
rhughes@241
   604
rhughes@241
   605
	pi = razor_package_iterator_create(set);
richard@307
   606
	while (razor_package_iterator_next(pi, &package,
richard@307
   607
					   RAZOR_DETAIL_NAME, &name,
richard@307
   608
					   RAZOR_DETAIL_LAST)) {
rhughes@241
   609
		if (pattern && fnmatch(pattern, name, 0) == 0) {
rhughes@241
   610
			razor_transaction_remove_package(trans, package);
rhughes@241
   611
			matches++;
rhughes@241
   612
		}
rhughes@241
   613
	}
rhughes@241
   614
	razor_package_iterator_destroy(pi);
rhughes@241
   615
rhughes@241
   616
	return matches;
rhughes@241
   617
}
rhughes@241
   618
rhughes@241
   619
static int
rhughes@241
   620
command_remove(int argc, const char *argv[])
rhughes@241
   621
{
ali@363
   622
	struct razor_root *root;
ali@363
   623
	struct razor_set *system, *upstream, *next;
rhughes@241
   624
	struct razor_transaction *trans;
ali@403
   625
	struct razor_atomic *atomic;
ali@403
   626
	struct razor_install_iterator *ii;
ali@403
   627
	int i, retval;
rhughes@241
   628
ali@403
   629
	atomic = razor_atomic_open("Remove packages");
ali@403
   630
ali@403
   631
	root = razor_root_open(install_root, atomic);
ali@403
   632
	system = razor_set_ref(razor_root_get_system_set(root));
ali@363
   633
	if (system == NULL) {
ali@403
   634
		fprintf(stderr, "%s\n", razor_atomic_get_error_msg(atomic));
ali@363
   635
		razor_root_close(root);
ali@403
   636
		razor_atomic_destroy(atomic);
rhughes@241
   637
		return 1;
ali@363
   638
	}
rhughes@241
   639
ali@363
   640
	upstream = razor_set_create_without_root();
ali@363
   641
	trans = razor_transaction_create(system, upstream);
ali@403
   642
	razor_set_unref(upstream);
rhughes@241
   643
	for (i = 0; i < argc; i++) {
ali@363
   644
		if (mark_packages_for_removal(trans, system, argv[i]) == 0) {
rhughes@241
   645
			fprintf(stderr, "no match for %s\n", argv[i]);
ali@363
   646
			razor_transaction_destroy(trans);
ali@403
   647
			razor_set_unref(system);
ali@363
   648
			razor_root_close(root);
ali@403
   649
			razor_atomic_destroy(atomic);
rhughes@241
   650
			return 1;
rhughes@241
   651
		}
rhughes@241
   652
	}
rhughes@241
   653
ali@350
   654
	razor_transaction_resolve(trans);
ali@403
   655
	retval = razor_transaction_describe(trans);
ali@403
   656
	if (retval) {
ali@363
   657
		razor_transaction_destroy(trans);
ali@403
   658
		razor_set_unref(system);
ali@363
   659
		razor_root_close(root);
ali@403
   660
		razor_atomic_destroy(atomic);
rhughes@241
   661
		return 1;
ali@363
   662
	}
rhughes@241
   663
ali@369
   664
	next = razor_transaction_commit(trans);
ali@403
   665
	ii = razor_set_create_install_iterator(system, next);
ali@412
   666
	retval = update_packages(trans, ii, system, next, atomic, NULL,
ali@412
   667
				 UPDATE_PASS_PRE_REMOVE);
ali@412
   668
	if (retval)
ali@412
   669
		fprintf(stderr, "Remove aborted\n");
ali@412
   670
	else {
ali@412
   671
		update_packages(trans, ii, system, next, atomic, NULL,
ali@412
   672
				UPDATE_PASS_MAIN);
ali@403
   673
ali@412
   674
		razor_root_update(root, next);
ali@363
   675
ali@412
   676
		(void)razor_root_commit(root);
ali@412
   677
		retval = razor_atomic_commit(atomic);
ali@412
   678
		if (retval)
ali@412
   679
			fprintf(stderr, "%s\n",
ali@412
   680
				razor_atomic_get_error_msg(atomic));
ali@412
   681
	}
ali@412
   682
ali@403
   683
	razor_install_iterator_destroy(ii);
ali@369
   684
	razor_transaction_destroy(trans);
ali@403
   685
	razor_atomic_destroy(atomic);
ali@403
   686
	razor_set_unref(system);
ali@403
   687
	razor_set_unref(next);
rhughes@241
   688
ali@403
   689
	return retval;
rhughes@241
   690
}
rhughes@241
   691
rhughes@241
   692
static void
krh@253
   693
print_diff(enum razor_diff_action action,
krh@253
   694
	   struct razor_package *package,
krh@253
   695
	   const char *name,
krh@253
   696
	   const char *version,
krh@253
   697
	   const char *arch,
rhughes@241
   698
	   void *data)
rhughes@241
   699
{
krh@253
   700
	if (action == RAZOR_DIFF_ACTION_ADD)
krh@253
   701
		printf("install %s-%s.%s\n", name, version, arch);
krh@253
   702
	if (action == RAZOR_DIFF_ACTION_REMOVE)
krh@253
   703
		printf("remove %s-%s.%s\n", name, version, arch);
rhughes@241
   704
}
rhughes@241
   705
rhughes@241
   706
static int
rhughes@241
   707
command_diff(int argc, const char *argv[])
rhughes@241
   708
{
ali@403
   709
	struct razor_atomic *atomic;
rhughes@241
   710
	struct razor_set *set, *updated;
rhughes@241
   711
ali@403
   712
	atomic = razor_atomic_open("Show package differences");
ali@403
   713
	set = razor_root_open_read_only(install_root, atomic);
ali@403
   714
	updated = razor_set_open(rawhide_repo_filename, atomic);
ali@403
   715
	if (set == NULL || updated == NULL) {
ali@403
   716
		fprintf(stderr, "%s\n", razor_atomic_get_error_msg(atomic));
ali@403
   717
		razor_atomic_destroy(atomic);
rhughes@241
   718
		return 1;
ali@403
   719
	}
rhughes@241
   720
rhughes@241
   721
	razor_set_diff(set, updated, print_diff, NULL);
rhughes@241
   722
ali@403
   723
	razor_set_unref(set);
ali@403
   724
	razor_set_unref(updated);
ali@403
   725
	razor_atomic_destroy(atomic);
rhughes@241
   726
rhughes@241
   727
	return 0;
rhughes@241
   728
}
rhughes@241
   729
rhughes@241
   730
static int
rhughes@241
   731
command_import_rpms(int argc, const char *argv[])
rhughes@241
   732
{
rhughes@241
   733
	DIR *dir;
rhughes@241
   734
	struct dirent *de;
rhughes@241
   735
	struct razor_importer *importer;
rhughes@241
   736
	struct razor_set *set;
rhughes@241
   737
	struct razor_rpm *rpm;
ali@403
   738
	struct razor_atomic *atomic;
jbowes@263
   739
	int len, imported_count = 0;
rhughes@241
   740
	char filename[256];
rhughes@241
   741
	const char *dirname = argv[0];
ali@403
   742
	int retval;
rhughes@241
   743
rhughes@241
   744
	if (dirname == NULL) {
rhughes@241
   745
		fprintf(stderr, "usage: razor import-rpms DIR\n");
rhughes@241
   746
		return -1;
rhughes@241
   747
	}
rhughes@241
   748
rhughes@241
   749
	dir = opendir(dirname);
rhughes@241
   750
	if (dir == NULL) {
rhughes@241
   751
		fprintf(stderr, "couldn't read dir %s\n", dirname);
rhughes@241
   752
		return -1;
rhughes@241
   753
	}
rhughes@241
   754
krh@249
   755
	importer = razor_importer_create();
rhughes@241
   756
rhughes@241
   757
	while (de = readdir(dir), de != NULL) {
rhughes@241
   758
		len = strlen(de->d_name);
rhughes@241
   759
		if (len < 5 || strcmp(de->d_name + len - 4, ".rpm") != 0)
ali@403
   760
			continue;
rhughes@241
   761
		snprintf(filename, sizeof filename,
rhughes@241
   762
			 "%s/%s", dirname, de->d_name);
ali@403
   763
		atomic = razor_atomic_open("Read RPM");
ali@403
   764
		rpm = razor_rpm_open(filename, atomic);
ali@403
   765
		if (rpm == NULL)
ali@403
   766
			fprintf(stderr, "%s\n",
ali@403
   767
				razor_atomic_get_error_msg(atomic));
ali@403
   768
		razor_atomic_destroy(atomic);
ali@403
   769
		if (rpm == NULL)
rhughes@241
   770
			continue;
rhughes@241
   771
		if (razor_importer_add_rpm(importer, rpm)) {
rhughes@241
   772
			fprintf(stderr, "couldn't import %s\n", filename);
rhughes@241
   773
			break;
rhughes@241
   774
		}
rhughes@241
   775
		razor_rpm_close(rpm);
jbowes@263
   776
jbowes@263
   777
		printf("\rimporting %d", ++imported_count);
jbowes@263
   778
		fflush(stdout);
rhughes@241
   779
	}
rhughes@241
   780
rhughes@241
   781
	if (de != NULL) {
rhughes@241
   782
		razor_importer_destroy(importer);
rhughes@241
   783
		return -1;
rhughes@241
   784
	}
rhughes@241
   785
jbowes@263
   786
	printf("\nsaving\n");
rhughes@241
   787
	set = razor_importer_finish(importer);
rhughes@241
   788
ali@403
   789
	atomic = razor_atomic_open("Update system database");
ali@403
   790
	razor_set_write(set, atomic, repo_filename, RAZOR_SECTION_ALL);
ali@403
   791
	razor_set_unref(set);
ali@403
   792
	retval = razor_atomic_commit(atomic);
ali@403
   793
	if (retval)
ali@403
   794
		fprintf(stderr, "%s\n", razor_atomic_get_error_msg(atomic));
ali@403
   795
	else
ali@403
   796
		printf("wrote %s\n", repo_filename);
ali@403
   797
	razor_atomic_destroy(atomic);
rhughes@241
   798
ali@403
   799
	return retval;
rhughes@241
   800
}
rhughes@241
   801
ali@403
   802
static char *
krh@254
   803
rpm_filename(const char *name, const char *version, const char *arch)
rhughes@241
   804
{
krh@254
   805
 	const char *v;
krh@254
   806
 
krh@254
   807
 	/* Skip epoch */
krh@253
   808
	v = strchr(version, ':');
krh@254
   809
 	if (v != NULL)
krh@254
   810
 		v = v + 1;
krh@254
   811
 	else
krh@253
   812
		v = version;
rhughes@241
   813
ali@403
   814
	return razor_concat(name, "-", v, ".", arch, ".rpm", NULL);
rhughes@241
   815
}
rhughes@241
   816
krh@254
   817
static int
krh@254
   818
download_packages(struct razor_set *system, struct razor_set *next)
rhughes@241
   819
{
krh@316
   820
	struct razor_install_iterator *ii;
krh@254
   821
	struct razor_package *package;
krh@316
   822
	enum razor_install_action action;
krh@254
   823
	const char *name, *version, *arch;
ali@403
   824
	char *file, *url, *s;
krh@316
   825
	int errors = 0, count;
krh@316
   826
krh@316
   827
	ii = razor_set_create_install_iterator(system, next);
ali@382
   828
	while (razor_install_iterator_next(ii, &package, &action, &count)) {
krh@316
   829
		if (action == RAZOR_INSTALL_ACTION_REMOVE)
krh@316
   830
			continue;
krh@316
   831
ali@382
   832
		razor_package_get_details(next, package,
krh@316
   833
					  RAZOR_DETAIL_NAME, &name,
krh@316
   834
					  RAZOR_DETAIL_VERSION, &version,
krh@316
   835
					  RAZOR_DETAIL_ARCH, &arch,
krh@316
   836
					  RAZOR_DETAIL_LAST);
krh@316
   837
		
ali@403
   838
		s = rpm_filename(name, version, arch);
ali@403
   839
		url = razor_concat(yum_url, "/Packages/", s, NULL);
ali@403
   840
		file = razor_concat("rpms/", s, NULL);
ali@403
   841
		free(s);
krh@254
   842
		if (download_if_missing(url, file) < 0)
krh@254
   843
			errors++;
ali@403
   844
		free(file);
ali@403
   845
		free(url);
krh@254
   846
	}
krh@316
   847
	razor_install_iterator_destroy(ii);
krh@254
   848
krh@254
   849
	if (errors > 0) {
krh@254
   850
		fprintf(stderr, "failed to download %d packages\n", errors);
krh@254
   851
                return -1;
krh@254
   852
        }
krh@254
   853
krh@254
   854
	return 0;
krh@254
   855
}
krh@254
   856
ali@351
   857
static struct razor_set *
ali@403
   858
relocate_packages(struct razor_set *set, struct razor_atomic *atomic,
ali@403
   859
		  struct razor_relocations *relocations)
ali@351
   860
{
ali@372
   861
	int i;
ali@351
   862
	struct razor_importer *importer;
ali@351
   863
	struct razor_property_iterator *prop_iter;
ali@351
   864
	struct razor_package_iterator *pkg_iter;
ali@351
   865
 	struct razor_file_iterator *file_iter;
ali@351
   866
 	struct razor_package *package;
ali@351
   867
	struct razor_property *property;
ali@351
   868
	struct razor_rpm *rpm;
ali@351
   869
	const char *name, *version, *arch, *summary, *desc, *url, *license;
ali@369
   870
	const char *preunprog, *preun, *postunprog, *postun;
ali@372
   871
	const char *install_prefix;
ali@372
   872
	const char *const *prefixes;
ali@403
   873
	char *file, *s;
ali@351
   874
	uint32_t flags;
ali@351
   875
ali@351
   876
	importer = razor_importer_create();
ali@351
   877
	pkg_iter = razor_package_iterator_create(set);
ali@351
   878
ali@351
   879
	while (razor_package_iterator_next(pkg_iter, &package,
ali@351
   880
					   RAZOR_DETAIL_NAME, &name,
ali@351
   881
					   RAZOR_DETAIL_VERSION, &version,
ali@351
   882
					   RAZOR_DETAIL_ARCH, &arch,
ali@351
   883
					   RAZOR_DETAIL_SUMMARY, &summary,
ali@351
   884
					   RAZOR_DETAIL_DESCRIPTION, &desc,
ali@351
   885
					   RAZOR_DETAIL_URL, &url,
ali@351
   886
					   RAZOR_DETAIL_LICENSE, &license,
ali@369
   887
					   RAZOR_DETAIL_PREUNPROG, &preunprog,
ali@369
   888
					   RAZOR_DETAIL_PREUN, &preun,
ali@369
   889
					   RAZOR_DETAIL_POSTUNPROG, &postunprog,
ali@369
   890
					   RAZOR_DETAIL_POSTUN, &postun,
ali@351
   891
					   RAZOR_DETAIL_LAST)) {
ali@403
   892
		s = rpm_filename(name, version, arch);
ali@403
   893
		file = razor_concat("rpms/", s, NULL);
ali@403
   894
		free(s);
ali@403
   895
		rpm = razor_rpm_open(file, atomic);
ali@403
   896
		free(file);
ali@351
   897
		if (rpm == NULL) {
ali@351
   898
			razor_package_iterator_destroy(pkg_iter);
ali@351
   899
			razor_importer_destroy(importer);
ali@351
   900
			return NULL;
ali@351
   901
		}
ali@351
   902
ali@351
   903
		razor_relocations_set_rpm(relocations, rpm);
ali@351
   904
ali@351
   905
		razor_importer_begin_package(importer, name, version, arch);
ali@351
   906
		razor_importer_add_details(importer,
ali@351
   907
					   summary, desc, url, license);
ali@351
   908
ali@372
   909
		razor_rpm_get_details(rpm, RAZOR_DETAIL_PREFIXES, &prefixes,
ali@372
   910
				      RAZOR_DETAIL_LAST);
ali@372
   911
		for (i = 0; prefixes && prefixes[i]; i++) {
ali@372
   912
			install_prefix = razor_relocations_apply(relocations,
ali@372
   913
								 prefixes[i]);
ali@372
   914
			razor_importer_add_install_prefix(importer,
ali@372
   915
							  install_prefix);
ali@372
   916
		}
ali@372
   917
ali@372
   918
		razor_rpm_close(rpm);
ali@372
   919
ali@351
   920
		prop_iter = razor_property_iterator_create(set, package);
ali@351
   921
		while (razor_property_iterator_next(prop_iter, &property,
ali@351
   922
						    &name, &flags, &version))
ali@351
   923
			razor_importer_add_property(importer,
ali@351
   924
						    name, flags, version);
ali@351
   925
		razor_property_iterator_destroy(prop_iter);
ali@351
   926
ali@377
   927
		file_iter = razor_file_iterator_create(set, package, 0);
ali@351
   928
		while (razor_file_iterator_next(file_iter, &name)) {
ali@351
   929
			name = razor_relocations_apply(relocations, name);
ali@351
   930
			razor_importer_add_file(importer, name);
ali@351
   931
		}
ali@351
   932
		razor_file_iterator_destroy(file_iter);
ali@351
   933
ali@369
   934
		razor_importer_add_script(importer, RAZOR_PROPERTY_PREUN,
ali@369
   935
					  preunprog, preun);
ali@369
   936
		razor_importer_add_script(importer, RAZOR_PROPERTY_POSTUN,
ali@369
   937
					  postunprog, postun);
ali@369
   938
ali@351
   939
		razor_importer_finish_package(importer);
ali@351
   940
	}
ali@351
   941
ali@351
   942
	razor_package_iterator_destroy(pkg_iter);
ali@351
   943
	return razor_importer_finish(importer);
ali@351
   944
}
ali@351
   945
krh@254
   946
static int
ali@369
   947
install_package(struct razor_transaction *trans, struct razor_set *set,
ali@403
   948
		struct razor_atomic *atomic, struct razor_package *package,
ali@403
   949
		struct razor_relocations *relocations, int install_count,
ali@403
   950
		enum razor_stage_type stage)
ali@363
   951
{
ali@363
   952
	int retval;
ali@363
   953
	const char *name, *version, *arch;
ali@403
   954
	char *file, *s;
ali@363
   955
	struct razor_rpm *rpm;
ali@363
   956
ali@363
   957
	razor_package_get_details(set, package,
ali@363
   958
				  RAZOR_DETAIL_NAME, &name,
ali@363
   959
				  RAZOR_DETAIL_VERSION, &version,
ali@363
   960
				  RAZOR_DETAIL_ARCH, &arch,
ali@363
   961
				  RAZOR_DETAIL_LAST);
ali@363
   962
ali@403
   963
	if (stage & RAZOR_STAGE_SCRIPTS_PRE)
ali@403
   964
		printf("install %s-%s\n", name, version);
ali@363
   965
ali@403
   966
	s = rpm_filename(name, version, arch);
ali@403
   967
	file = razor_concat("rpms/", s, NULL);
ali@403
   968
	free(s);
ali@403
   969
	rpm = razor_rpm_open(file, atomic);
ali@403
   970
	free(file);
ali@363
   971
	if (rpm == NULL) {
ali@403
   972
		fprintf(stderr, "%s\n",
ali@403
   973
			razor_atomic_get_error_msg(atomic));
ali@363
   974
		return -1;
ali@363
   975
	}
ali@363
   976
	if (relocations)
ali@363
   977
		razor_rpm_set_relocations(rpm, relocations);
ali@369
   978
	razor_transaction_fixup_package(trans, package, rpm);
ali@403
   979
	retval = razor_rpm_install(rpm, atomic, install_root, install_count,
ali@403
   980
				   stage);
ali@403
   981
	if (retval < 0) {
ali@403
   982
		s = rpm_filename(name, version, arch);
ali@403
   983
		fprintf(stderr, "%s: %s\n", s,
ali@403
   984
			razor_atomic_get_error_msg(atomic));
ali@403
   985
		free(s);
ali@403
   986
	}
ali@363
   987
	razor_rpm_close(rpm);
ali@363
   988
	return retval;
ali@363
   989
}
ali@363
   990
ali@412
   991
/*
ali@412
   992
 * In the most general case, there should be three passes:
ali@412
   993
 *
ali@412
   994
 * 1) For each package to be removed, run %preun
ali@412
   995
 *
ali@412
   996
 * 2) For each package:
ali@412
   997
 *	If the package is to be installed, run %pre
ali@412
   998
 *	Update the files on disk
ali@412
   999
 *	If the package has been removed, run %postun
ali@412
  1000
 *
ali@412
  1001
 * 3) For each packge installed, run %post
ali@412
  1002
 *
ali@412
  1003
 * This guarantees that:
ali@412
  1004
 *   a)	Save where dependency loops make it impossible, at the time
ali@412
  1005
 *	%pre is run, all required packages are installed (although
ali@412
  1006
 *	their %post script may not have been run). We should support
ali@412
  1007
 *	Requires(%pre) to allow packagers to describe their requirements
ali@412
  1008
 *	more accurately and avoid unnecessary dependency loops.
ali@412
  1009
 *   b)	At the time %preun is run, all required packages are installed.
ali@412
  1010
 *	Supporting Requires(%preun) would make this more complicated
ali@412
  1011
 *	since we might have to install a package in order to remove
ali@412
  1012
 *	another one. For now, treating Requires(%preun) as Requires
ali@412
  1013
 *	seems more sensible.
ali@412
  1014
 *   c)	At the time %post is run, all required packages are installed.
ali@412
  1015
 *	Supporting Requires(%post) would allow us to remove a package
ali@412
  1016
 *	that was only needed to install another, but there seems no
ali@412
  1017
 *	obvious advantage.
ali@412
  1018
 *   d)	Save where dependency loops make it impossible, at the time
ali@412
  1019
 *	%postun is run, all required packages are installed. Again,
ali@412
  1020
 *	we should support Requires(%postun) to avoid unnecessary
ali@412
  1021
 *	dependency loops.
ali@412
  1022
 *
ali@412
  1023
 * Notes:
ali@412
  1024
 *	rpm treats %pre and %preun script failures as fatal errors
ali@412
  1025
 *	and %post and %postun failures as warnings.
ali@412
  1026
 */
ali@363
  1027
static int
ali@403
  1028
update_packages(struct razor_transaction *trans,
ali@403
  1029
		struct razor_install_iterator *ii, struct razor_set *system,
ali@403
  1030
		struct razor_set *next, struct razor_atomic *atomic,
ali@403
  1031
		struct razor_relocations *relocations,
ali@412
  1032
		enum update_pass_type pass)
krh@254
  1033
{
krh@254
  1034
	struct razor_package *package;
krh@316
  1035
	enum razor_install_action action;
ali@363
  1036
	int retval = 0, count;
ali@412
  1037
	enum razor_stage_type remove_stage, add_stage;
ali@412
  1038
ali@412
  1039
	switch (pass) {
ali@412
  1040
	case UPDATE_PASS_PRE_REMOVE:
ali@412
  1041
		add_stage = 0;
ali@412
  1042
		remove_stage = RAZOR_STAGE_SCRIPTS_PRE;
ali@412
  1043
		break;
ali@412
  1044
	case UPDATE_PASS_MAIN:
ali@412
  1045
		add_stage = RAZOR_STAGE_SCRIPTS_PRE | RAZOR_STAGE_FILES;
ali@412
  1046
		remove_stage = RAZOR_STAGE_FILES | RAZOR_STAGE_SCRIPTS_POST;
ali@412
  1047
		break;
ali@412
  1048
	case UPDATE_PASS_POST_INSTALL:
ali@412
  1049
		add_stage = RAZOR_STAGE_SCRIPTS_POST;
ali@412
  1050
		remove_stage = 0;
ali@412
  1051
		break;
ali@412
  1052
	}
rhughes@241
  1053
ali@403
  1054
	razor_install_iterator_rewind(ii);
ali@403
  1055
ali@412
  1056
	while (!retval && razor_install_iterator_next(ii, &package, &action,
ali@412
  1057
						      &count)) {
ali@412
  1058
		if (action == RAZOR_INSTALL_ACTION_ADD && add_stage)
ali@403
  1059
			retval = install_package(trans, next, atomic, package,
ali@412
  1060
						 relocations, count, add_stage);
ali@412
  1061
		else if (action == RAZOR_INSTALL_ACTION_REMOVE && remove_stage)
ali@403
  1062
			retval = razor_package_remove(system, next, atomic,
ali@403
  1063
						      package, install_root,
ali@412
  1064
						      count, remove_stage);
rhughes@241
  1065
	}
rhughes@241
  1066
ali@363
  1067
	return retval;
rhughes@241
  1068
}
rhughes@241
  1069
rhughes@241
  1070
static int
ali@382
  1071
command_install_or_update(int argc, const char *argv[], int do_update)
rhughes@241
  1072
{
rhughes@241
  1073
	struct razor_root *root;
ali@351
  1074
	struct razor_relocations *relocations=NULL;
ali@351
  1075
	struct razor_set *system, *upstream, *next, *set;
rhughes@241
  1076
	struct razor_transaction *trans;
ali@403
  1077
	struct razor_atomic *atomic;
ali@403
  1078
	struct razor_install_iterator *ii;
ali@403
  1079
	int i, retval, len, dependencies = 1;
ali@351
  1080
	char *oldpath;
rhughes@241
  1081
ali@403
  1082
	if (do_update)
ali@403
  1083
		atomic = razor_atomic_open("Update packages");
ali@403
  1084
	else
ali@403
  1085
		atomic = razor_atomic_open("Install packages");
ali@403
  1086
ali@403
  1087
	root = razor_root_open(install_root, atomic);
ali@403
  1088
	if (root == NULL) {
ali@403
  1089
		fprintf(stderr, "%s\n", razor_atomic_get_error_msg(atomic));
ali@403
  1090
		razor_atomic_destroy(atomic);
krh@255
  1091
		return 1;
ali@403
  1092
	}
krh@255
  1093
ali@351
  1094
	for (i = 0; i < argc; i++) {
ali@351
  1095
		if (strcmp(argv[i], "--no-dependencies") == 0)
ali@351
  1096
			dependencies = 0;
ali@351
  1097
		else if (strcmp(argv[i], "--relocate") == 0) {
ali@351
  1098
			i++;
ali@351
  1099
			if (i >= argc || strchr(argv[i], '=') == NULL) {
ali@351
  1100
				fprintf(stderr,
ali@382
  1101
				    "Usage: razor %s [OPTION...] RPM\n",
ali@382
  1102
				    do_update ? "update" : "install");
ali@351
  1103
				fprintf(stderr, "Options:\n");
ali@351
  1104
				fprintf(stderr, "    [--no-dependencies]\n");
ali@351
  1105
				fprintf(stderr,
ali@351
  1106
				    "    [--relocate OLDPATH=NEWPATH] RPM\n");
ali@351
  1107
				return -1;
ali@351
  1108
			}
ali@351
  1109
			len = strchr(argv[i], '=') - argv[i];
ali@351
  1110
			oldpath = malloc(len + 1);
ali@351
  1111
			strncpy(oldpath, argv[i], len);
ali@351
  1112
			oldpath[len] = '\0';
ali@351
  1113
			if (!relocations)
ali@351
  1114
			       relocations = razor_relocations_create();
ali@351
  1115
			razor_relocations_add(relocations, oldpath,
ali@351
  1116
					      argv[i] + len + 1);
ali@351
  1117
			free(oldpath);
ali@351
  1118
		} else
ali@351
  1119
			break;
ali@351
  1120
	}
ali@351
  1121
ali@403
  1122
	upstream = razor_set_open(rawhide_repo_filename, atomic);
krh@373
  1123
	if (upstream == NULL) {
ali@403
  1124
		fprintf(stderr, "%s\n", razor_atomic_get_error_msg(atomic));
krh@373
  1125
		razor_root_close(root);
ali@403
  1126
		razor_atomic_destroy(atomic);
krh@373
  1127
		return 1;
ali@382
  1128
	}
krh@315
  1129
ali@351
  1130
	if (relocations) {
ali@403
  1131
		set = relocate_packages(upstream, atomic, relocations);
ali@403
  1132
		if (set == NULL) {
ali@403
  1133
			fprintf(stderr, "%s\n",
ali@403
  1134
				razor_atomic_get_error_msg(atomic));
ali@403
  1135
			razor_root_close(root);
ali@403
  1136
			razor_atomic_destroy(atomic);
ali@403
  1137
			razor_set_unref(upstream);
ali@403
  1138
			return 1;
ali@403
  1139
		}
ali@403
  1140
		razor_set_unref(upstream);
ali@351
  1141
		upstream = set;
ali@351
  1142
	}
ali@351
  1143
ali@403
  1144
	system = razor_set_ref(razor_root_get_system_set(root));
ali@403
  1145
krh@250
  1146
	trans = razor_transaction_create(system, upstream);
rhughes@241
  1147
ali@382
  1148
	if (i == argc && do_update)
ali@382
  1149
		razor_transaction_update_all(trans);
rhughes@241
  1150
	for (; i < argc; i++) {
ali@389
  1151
		if (do_update &&
ali@389
  1152
		    mark_packages_for_update(trans, system, argv[i]))
ali@389
  1153
			continue;
rhughes@241
  1154
		if (mark_packages_for_update(trans, upstream, argv[i]) == 0) {
rhughes@241
  1155
			fprintf(stderr, "no package matched %s\n", argv[i]);
ali@369
  1156
			razor_transaction_destroy(trans);
ali@403
  1157
			razor_set_unref(upstream);
ali@403
  1158
			razor_set_unref(system);
rhughes@241
  1159
			razor_root_close(root);
ali@403
  1160
			razor_atomic_destroy(atomic);
rhughes@241
  1161
			return 1;
rhughes@241
  1162
		}
rhughes@241
  1163
	}
rhughes@241
  1164
rhughes@241
  1165
	if (dependencies) {
krh@245
  1166
		razor_transaction_resolve(trans);
krh@245
  1167
		if (razor_transaction_describe(trans) > 0) {
ali@369
  1168
			razor_transaction_destroy(trans);
ali@403
  1169
			razor_set_unref(upstream);
ali@403
  1170
			razor_set_unref(system);
rhughes@241
  1171
			razor_root_close(root);
ali@403
  1172
			razor_atomic_destroy(atomic);
rhughes@241
  1173
			return 1;
rhughes@241
  1174
		}
rhughes@241
  1175
	}
rhughes@241
  1176
ali@403
  1177
	if (razor_atomic_create_dir(atomic, "rpms",
ali@403
  1178
				    S_IRWXU | S_IRWXG | S_IRWXO)) {
ali@403
  1179
		fprintf(stderr, "%s\n", razor_atomic_get_error_msg(atomic));
ali@369
  1180
		razor_transaction_destroy(trans);
ali@403
  1181
		razor_set_unref(upstream);
ali@403
  1182
		razor_set_unref(system);
rhughes@241
  1183
		razor_root_close(root);
ali@403
  1184
		razor_atomic_destroy(atomic);
rhughes@241
  1185
		return 1;
rhughes@241
  1186
	}
rhughes@241
  1187
ali@382
  1188
	next = razor_transaction_commit(trans);
ali@382
  1189
krh@254
  1190
	if (download_packages(system, next) < 0) {
ali@403
  1191
		razor_set_unref(next);
ali@369
  1192
		razor_transaction_destroy(trans);
ali@403
  1193
		razor_set_unref(upstream);
ali@403
  1194
		razor_set_unref(system);
rhughes@241
  1195
		razor_root_close(root);
ali@403
  1196
		razor_atomic_destroy(atomic);
rhughes@241
  1197
                return 1;
rhughes@241
  1198
        }
rhughes@241
  1199
ali@403
  1200
	ii = razor_set_create_install_iterator(system, next);
ali@403
  1201
ali@412
  1202
	retval = update_packages(trans, ii, system, next, atomic, relocations,
ali@412
  1203
				 UPDATE_PASS_PRE_REMOVE);
ali@412
  1204
	if (retval)
ali@412
  1205
		fprintf(stderr, "%s aborted\n",
ali@412
  1206
			do_update ? "Update" : "Install");
ali@412
  1207
	else {
ali@412
  1208
		update_packages(trans, ii, system, next, atomic, relocations,
ali@412
  1209
				UPDATE_PASS_MAIN);
rhughes@241
  1210
ali@412
  1211
		razor_root_update(root, next);
ali@369
  1212
ali@412
  1213
		razor_set_unref(upstream);
ali@403
  1214
ali@412
  1215
		(void)razor_root_commit(root);
ali@412
  1216
		retval = razor_atomic_commit(atomic);
ali@412
  1217
		if (retval)
ali@412
  1218
			fprintf(stderr, "%s\n",
ali@412
  1219
				razor_atomic_get_error_msg(atomic));
ali@412
  1220
		else
ali@412
  1221
			(void)update_packages(trans, ii, system, next, atomic,
ali@412
  1222
					      relocations,
ali@412
  1223
					      UPDATE_PASS_POST_INSTALL);
ali@412
  1224
	}
ali@403
  1225
ali@369
  1226
	razor_transaction_destroy(trans);
ali@351
  1227
	if (relocations)
ali@351
  1228
		razor_relocations_destroy(relocations);
ali@403
  1229
	razor_install_iterator_destroy(ii);
rhughes@241
  1230
ali@403
  1231
	razor_atomic_destroy(atomic);
ali@403
  1232
ali@403
  1233
	razor_set_unref(next);
ali@403
  1234
	razor_set_unref(system);
ali@403
  1235
ali@403
  1236
	return retval;
rhughes@241
  1237
}
rhughes@241
  1238
rhughes@241
  1239
static int
ali@382
  1240
command_update(int argc, const char *argv[])
ali@382
  1241
{
ali@382
  1242
	return command_install_or_update(argc, argv, 1);
ali@382
  1243
}
ali@382
  1244
ali@382
  1245
static int
ali@382
  1246
command_install(int argc, const char *argv[])
ali@382
  1247
{
ali@382
  1248
	return command_install_or_update(argc, argv, 0);
ali@382
  1249
}
ali@382
  1250
ali@382
  1251
static int
rhughes@241
  1252
command_init(int argc, const char *argv[])
rhughes@241
  1253
{
rhughes@241
  1254
	return razor_root_create(install_root);
rhughes@241
  1255
}
rhughes@241
  1256
rhughes@241
  1257
static int
rhughes@241
  1258
command_download(int argc, const char *argv[])
rhughes@241
  1259
{
ali@403
  1260
	struct razor_atomic *atomic;
rhughes@241
  1261
	struct razor_set *set;
rhughes@241
  1262
	struct razor_package_iterator *pi;
rhughes@241
  1263
	struct razor_package *package;
rhughes@241
  1264
	const char *pattern = argv[0], *name, *version, *arch;
rhughes@241
  1265
	char url[256], file[256];
rhughes@241
  1266
	int matches = 0;
rhughes@241
  1267
ali@403
  1268
	atomic = razor_atomic_open("Download packages");
ali@403
  1269
ali@403
  1270
	if (razor_atomic_create_dir(atomic, "rpms", 
ali@403
  1271
				    S_IRWXU | S_IRWXG | S_IRWXO)) {
ali@403
  1272
		fprintf(stderr, "%s\n", razor_atomic_get_error_msg(atomic));
ali@403
  1273
		razor_atomic_destroy(atomic);
rhughes@241
  1274
		return 1;
rhughes@241
  1275
	}
rhughes@241
  1276
ali@403
  1277
	set = razor_set_open(rawhide_repo_filename, atomic);
ali@403
  1278
ali@403
  1279
	if (razor_atomic_commit(atomic)) {
ali@403
  1280
		fprintf(stderr, "%s\n", razor_atomic_get_error_msg(atomic));
ali@403
  1281
		razor_atomic_destroy(atomic);
ali@403
  1282
		return 1;
ali@403
  1283
	}
ali@403
  1284
	razor_atomic_destroy(atomic);
ali@403
  1285
rhughes@241
  1286
	pi = razor_package_iterator_create(set);
rhughes@241
  1287
	while (razor_package_iterator_next(pi, &package,
richard@302
  1288
					   RAZOR_DETAIL_NAME, &name,
richard@302
  1289
					   RAZOR_DETAIL_VERSION, &version,
richard@307
  1290
					   RAZOR_DETAIL_ARCH, &arch,
richard@307
  1291
					   RAZOR_DETAIL_LAST)) {
rhughes@241
  1292
		if (pattern && fnmatch(pattern, name, 0) != 0)
rhughes@241
  1293
			continue;
rhughes@241
  1294
rhughes@241
  1295
		matches++;
rhughes@241
  1296
		snprintf(url, sizeof url,
rhughes@241
  1297
			 "%s/Packages/%s-%s.%s.rpm",
rhughes@241
  1298
			 yum_url, name, version, arch);
rhughes@241
  1299
		snprintf(file, sizeof file,
rhughes@241
  1300
			 "rpms/%s-%s.%s.rpm", name, version, arch);
rhughes@241
  1301
		download_if_missing(url, file);
rhughes@241
  1302
	}
rhughes@241
  1303
	razor_package_iterator_destroy(pi);
ali@403
  1304
	razor_set_unref(set);
rhughes@241
  1305
rhughes@241
  1306
	if (matches == 0)
rhughes@241
  1307
		fprintf(stderr, "no packages matched \"%s\"\n", pattern);
rhughes@241
  1308
	else if (matches == 1)
rhughes@241
  1309
		fprintf(stderr, "downloaded 1 package\n");
rhughes@241
  1310
	else
rhughes@241
  1311
		fprintf(stderr, "downloaded %d packages\n", matches);
rhughes@241
  1312
rhughes@241
  1313
	return 0;
rhughes@241
  1314
}
rhughes@241
  1315
jbowes@258
  1316
static int
jbowes@258
  1317
command_info(int argc, const char *argv[])
jbowes@258
  1318
{
ali@403
  1319
	struct razor_atomic *atomic;
jbowes@258
  1320
	struct razor_set *set;
jbowes@258
  1321
	struct razor_package_iterator *pi;
jbowes@258
  1322
	struct razor_package *package;
jbowes@258
  1323
	const char *pattern = argv[0], *name, *version, *arch;
jbowes@258
  1324
	const char *summary, *description, *url, *license;
jbowes@258
  1325
ali@403
  1326
	atomic = razor_atomic_open("Package info");
ali@403
  1327
	set = razor_root_open_read_only(install_root, atomic);
ali@403
  1328
	if (set == NULL) {
ali@403
  1329
		fprintf(stderr, "%s\n", razor_atomic_get_error_msg(atomic));
ali@403
  1330
		razor_atomic_destroy(atomic);
jbowes@288
  1331
		return 1;
ali@403
  1332
	}
krh@317
  1333
jbowes@258
  1334
	pi = razor_package_iterator_create(set);
jbowes@258
  1335
	while (razor_package_iterator_next(pi, &package,
richard@302
  1336
					   RAZOR_DETAIL_NAME, &name,
richard@302
  1337
					   RAZOR_DETAIL_VERSION, &version,
richard@307
  1338
					   RAZOR_DETAIL_ARCH, &arch,
richard@307
  1339
					   RAZOR_DETAIL_LAST)) {
jbowes@258
  1340
		if (pattern && fnmatch(pattern, name, 0) != 0)
jbowes@258
  1341
			continue;
jbowes@258
  1342
richard@302
  1343
		razor_package_get_details (set, package,
richard@302
  1344
					   RAZOR_DETAIL_SUMMARY, &summary,
richard@302
  1345
					   RAZOR_DETAIL_DESCRIPTION, &description,
richard@302
  1346
					   RAZOR_DETAIL_URL, &url,
richard@302
  1347
					   RAZOR_DETAIL_LICENSE, &license,
richard@307
  1348
					   RAZOR_DETAIL_LAST);
jbowes@258
  1349
jbowes@258
  1350
		printf ("Name:        %s\n", name);
jbowes@258
  1351
		printf ("Arch:        %s\n", arch);
jbowes@258
  1352
		printf ("Version:     %s\n", version);
jbowes@258
  1353
		printf ("URL:         %s\n", url);
jbowes@258
  1354
		printf ("License:     %s\n", license);
jbowes@258
  1355
		printf ("Summary:     %s\n", summary);
jbowes@258
  1356
		printf ("Description:\n");
jbowes@258
  1357
		printf ("%s\n", description);
jbowes@258
  1358
		printf ("\n");
jbowes@258
  1359
	}
jbowes@258
  1360
	razor_package_iterator_destroy(pi);
ali@403
  1361
	razor_set_unref(set);
ali@403
  1362
	razor_atomic_destroy(atomic);
jbowes@258
  1363
jbowes@258
  1364
	return 0;
jbowes@258
  1365
}
jbowes@258
  1366
jbowes@292
  1367
#define SEARCH_MAX 256
jbowes@292
  1368
jbowes@292
  1369
static int
jbowes@292
  1370
command_search(int argc, const char *argv[])
jbowes@292
  1371
{
ali@403
  1372
	struct razor_atomic *atomic;
jbowes@292
  1373
	struct razor_set *set;
jbowes@292
  1374
	struct razor_package_iterator *pi;
jbowes@292
  1375
	struct razor_package *package;
jbowes@292
  1376
	char pattern[SEARCH_MAX];
jbowes@292
  1377
	const char *name, *version, *arch;
jbowes@292
  1378
	const char *summary, *description, *url, *license;
jbowes@292
  1379
jbowes@292
  1380
	if (!argv[0]) {
jbowes@292
  1381
		fprintf(stderr, "must specify a search term\n");
jbowes@292
  1382
		return 1;
jbowes@292
  1383
	}
jbowes@292
  1384
krh@294
  1385
	snprintf(pattern, sizeof pattern, "*%s*", argv[0]);
jbowes@292
  1386
ali@403
  1387
	atomic = razor_atomic_open("Search packages");
ali@403
  1388
	set = razor_set_open(rawhide_repo_filename, atomic);
ali@403
  1389
	if (set == NULL || razor_atomic_commit(atomic)) {
ali@403
  1390
		fprintf(stderr, "%s\n", razor_atomic_get_error_msg(atomic));
ali@403
  1391
		razor_atomic_destroy(atomic);
jbowes@292
  1392
		return 1;
ali@403
  1393
	}
ali@403
  1394
	razor_atomic_destroy(atomic);
richard@310
  1395
jbowes@292
  1396
	pi = razor_package_iterator_create(set);
jbowes@292
  1397
	while (razor_package_iterator_next(pi, &package,
richard@304
  1398
					   RAZOR_DETAIL_NAME, &name,
richard@304
  1399
					   RAZOR_DETAIL_VERSION, &version,
krh@305
  1400
					   RAZOR_DETAIL_ARCH, &arch,
krh@305
  1401
					   RAZOR_DETAIL_SUMMARY, &summary,
krh@305
  1402
					   RAZOR_DETAIL_DESCRIPTION, &description,
krh@305
  1403
					   RAZOR_DETAIL_URL, &url,
krh@305
  1404
					   RAZOR_DETAIL_LICENSE, &license,
richard@307
  1405
					   RAZOR_DETAIL_LAST)) {
krh@305
  1406
		if (!fnmatch(pattern, name, FNM_CASEFOLD) ||
krh@305
  1407
		    !fnmatch(pattern, url, FNM_CASEFOLD) ||
krh@305
  1408
		    !fnmatch(pattern, summary, FNM_CASEFOLD) ||
krh@305
  1409
		    !fnmatch(pattern, description, FNM_CASEFOLD))
krh@305
  1410
			printf("%s-%s.%s: %s\n", name, version, arch, summary);
jbowes@292
  1411
	}
jbowes@292
  1412
	razor_package_iterator_destroy(pi);
ali@403
  1413
	razor_set_unref(set);
jbowes@292
  1414
jbowes@292
  1415
	return 0;
jbowes@292
  1416
}
jbowes@292
  1417
rhughes@241
  1418
static struct {
rhughes@241
  1419
	const char *name;
rhughes@241
  1420
	const char *description;
rhughes@241
  1421
	int (*func)(int argc, const char *argv[]);
rhughes@241
  1422
} razor_commands[] = {
rhughes@241
  1423
	{ "list", "list all packages", command_list },
rhughes@241
  1424
	{ "list-requires", "list all requires for the given package", command_list_requires },
rhughes@241
  1425
	{ "list-provides", "list all provides for the given package", command_list_provides },
rhughes@241
  1426
	{ "list-obsoletes", "list all obsoletes for the given package", command_list_obsoletes },
rhughes@241
  1427
	{ "list-conflicts", "list all conflicts for the given package", command_list_conflicts },
ali@369
  1428
	{ "list-scripts", "list all scripts for the given package", command_list_scripts },
rhughes@241
  1429
	{ "list-files", "list files for package set", command_list_files },
rhughes@241
  1430
	{ "list-file-packages", "list packages owning file", command_list_file_packages },
rhughes@241
  1431
	{ "list-package-files", "list files in package", command_list_package_files },
rhughes@241
  1432
	{ "what-requires", "list the packages that have the given requires", command_what_requires },
rhughes@241
  1433
	{ "what-provides", "list the packages that have the given provides", command_what_provides },
rhughes@241
  1434
	{ "import-yum", "import yum metadata files", command_import_yum },
ali@320
  1435
#if HAVE_RPMLIB
rhughes@241
  1436
	{ "import-rpmdb", "import the system rpm database", command_import_rpmdb },
ali@320
  1437
#endif
rhughes@241
  1438
	{ "import-rpms", "import rpms from the given directory", command_import_rpms },
rhughes@241
  1439
	{ "update", "update all or specified packages", command_update },
rhughes@241
  1440
	{ "remove", "remove specified packages", command_remove },
rhughes@241
  1441
	{ "diff", "show diff between two package sets", command_diff },
rhughes@241
  1442
	{ "install", "install rpm", command_install },
rhughes@241
  1443
	{ "init", "init razor root", command_init },
jbowes@258
  1444
	{ "download", "download packages", command_download },
jbowes@292
  1445
	{ "info", "display package details", command_info },
jbowes@292
  1446
	{ "search", "search package details", command_search }
rhughes@241
  1447
};
rhughes@241
  1448
rhughes@241
  1449
static int
rhughes@241
  1450
usage(void)
rhughes@241
  1451
{
rhughes@241
  1452
	int i;
rhughes@241
  1453
rhughes@241
  1454
	printf("usage:\n");
rhughes@241
  1455
	for (i = 0; i < ARRAY_SIZE(razor_commands); i++)
rhughes@241
  1456
		printf("  %-20s%s\n",
rhughes@241
  1457
		       razor_commands[i].name, razor_commands[i].description);
rhughes@241
  1458
rhughes@241
  1459
	return 1;
rhughes@241
  1460
}
rhughes@241
  1461
rhughes@241
  1462
int
rhughes@241
  1463
main(int argc, const char *argv[])
rhughes@241
  1464
{
krh@317
  1465
	char *repo, *root;
rhughes@241
  1466
	int i;
rhughes@241
  1467
rhughes@241
  1468
	repo = getenv("RAZOR_REPO");
rhughes@241
  1469
	if (repo != NULL)
rhughes@241
  1470
		repo_filename = repo;
rhughes@241
  1471
krh@317
  1472
	root = getenv("RAZOR_ROOT");
krh@317
  1473
	if (root != NULL)
krh@317
  1474
		install_root = root;
krh@317
  1475
rhughes@241
  1476
	yum_url = getenv("YUM_URL");
rhughes@241
  1477
	if (yum_url == NULL)
rhughes@241
  1478
		yum_url = YUM_URL;
rhughes@241
  1479
ali@359
  1480
	if (getenv("RAZOR_NO_ROOT_NAME_CHECKS"))
ali@359
  1481
		razor_disable_root_name_checks(1);
ali@359
  1482
rhughes@241
  1483
	if (argc < 2)
rhughes@241
  1484
		return usage();
rhughes@241
  1485
rhughes@241
  1486
	for (i = 0; i < ARRAY_SIZE(razor_commands); i++)
rhughes@241
  1487
		if (strcmp(razor_commands[i].name, argv[1]) == 0)
rhughes@241
  1488
			return razor_commands[i].func(argc - 2, argv + 2);
rhughes@241
  1489
rhughes@241
  1490
	return usage();
rhughes@241
  1491
}