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