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