src/main.c
author J. Ali Harlow <ali@juiblex.co.uk>
Sat Aug 23 11:13:48 2014 +0100 (2014-08-23)
changeset 440 48204dea0b9f
parent 425 0c8bdd8dc942
child 442 c4bcba8023a9
permissions -rw-r--r--
Remove INTLLIBS from librazor_la_LIBADD.

This partially reverts 611c84a3f4b4538a65d186050608c17adbf17770.
It's not clear what motivated the initial inclusion of INTLLIBS
here since the net effect is only seen in librazor.la and not
in razor.pc and librazor.la is not normally packaged. Certainly
neither the static nor the dynamic versions of librazor currently
use libintl. At best this would cause the linker to search a
static libintl for undefined symbols without finding any; at worse
it causes a static build of plover using librazor.la to fail if
no static version of libintl is installed.
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@426
   702
	struct razor_error *error=NULL;
ali@403
   703
	struct razor_atomic *atomic;
jbowes@263
   704
	int len, imported_count = 0;
rhughes@241
   705
	char filename[256];
rhughes@241
   706
	const char *dirname = argv[0];
ali@403
   707
	int retval;
rhughes@241
   708
rhughes@241
   709
	if (dirname == NULL) {
rhughes@241
   710
		fprintf(stderr, "usage: razor import-rpms DIR\n");
rhughes@241
   711
		return -1;
rhughes@241
   712
	}
rhughes@241
   713
rhughes@241
   714
	dir = opendir(dirname);
rhughes@241
   715
	if (dir == NULL) {
rhughes@241
   716
		fprintf(stderr, "couldn't read dir %s\n", dirname);
rhughes@241
   717
		return -1;
rhughes@241
   718
	}
rhughes@241
   719
krh@249
   720
	importer = razor_importer_create();
rhughes@241
   721
rhughes@241
   722
	while (de = readdir(dir), de != NULL) {
rhughes@241
   723
		len = strlen(de->d_name);
rhughes@241
   724
		if (len < 5 || strcmp(de->d_name + len - 4, ".rpm") != 0)
ali@403
   725
			continue;
rhughes@241
   726
		snprintf(filename, sizeof filename,
rhughes@241
   727
			 "%s/%s", dirname, de->d_name);
ali@426
   728
		rpm = razor_rpm_open(filename, &error);
ali@426
   729
		if (rpm == NULL) {
ali@426
   730
			fprintf(stderr, "%s\n", razor_error_get_msg(error));
ali@426
   731
			razor_error_free(error);
ali@426
   732
			error=NULL;
rhughes@241
   733
			continue;
ali@426
   734
		}
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@426
   833
	struct razor_error *error=NULL;
ali@351
   834
	const char *name, *version, *arch, *summary, *desc, *url, *license;
ali@369
   835
	const char *preunprog, *preun, *postunprog, *postun;
ali@372
   836
	const char *install_prefix;
ali@372
   837
	const char *const *prefixes;
ali@403
   838
	char *file, *s;
ali@351
   839
	uint32_t flags;
ali@351
   840
ali@351
   841
	importer = razor_importer_create();
ali@351
   842
	pkg_iter = razor_package_iterator_create(set);
ali@351
   843
ali@351
   844
	while (razor_package_iterator_next(pkg_iter, &package,
ali@351
   845
					   RAZOR_DETAIL_NAME, &name,
ali@351
   846
					   RAZOR_DETAIL_VERSION, &version,
ali@351
   847
					   RAZOR_DETAIL_ARCH, &arch,
ali@351
   848
					   RAZOR_DETAIL_SUMMARY, &summary,
ali@351
   849
					   RAZOR_DETAIL_DESCRIPTION, &desc,
ali@351
   850
					   RAZOR_DETAIL_URL, &url,
ali@351
   851
					   RAZOR_DETAIL_LICENSE, &license,
ali@369
   852
					   RAZOR_DETAIL_PREUNPROG, &preunprog,
ali@369
   853
					   RAZOR_DETAIL_PREUN, &preun,
ali@369
   854
					   RAZOR_DETAIL_POSTUNPROG, &postunprog,
ali@369
   855
					   RAZOR_DETAIL_POSTUN, &postun,
ali@351
   856
					   RAZOR_DETAIL_LAST)) {
ali@403
   857
		s = rpm_filename(name, version, arch);
ali@403
   858
		file = razor_concat("rpms/", s, NULL);
ali@403
   859
		free(s);
ali@426
   860
		rpm = razor_rpm_open(file, &error);
ali@403
   861
		free(file);
ali@351
   862
		if (rpm == NULL) {
ali@426
   863
			razor_atomic_abort(atomic, razor_error_get_msg(error));
ali@426
   864
			razor_error_free(error);
ali@351
   865
			razor_package_iterator_destroy(pkg_iter);
ali@351
   866
			razor_importer_destroy(importer);
ali@351
   867
			return NULL;
ali@351
   868
		}
ali@351
   869
ali@351
   870
		razor_relocations_set_rpm(relocations, rpm);
ali@351
   871
ali@351
   872
		razor_importer_begin_package(importer, name, version, arch);
ali@351
   873
		razor_importer_add_details(importer,
ali@351
   874
					   summary, desc, url, license);
ali@351
   875
ali@372
   876
		razor_rpm_get_details(rpm, RAZOR_DETAIL_PREFIXES, &prefixes,
ali@372
   877
				      RAZOR_DETAIL_LAST);
ali@372
   878
		for (i = 0; prefixes && prefixes[i]; i++) {
ali@372
   879
			install_prefix = razor_relocations_apply(relocations,
ali@372
   880
								 prefixes[i]);
ali@372
   881
			razor_importer_add_install_prefix(importer,
ali@372
   882
							  install_prefix);
ali@372
   883
		}
ali@372
   884
ali@372
   885
		razor_rpm_close(rpm);
ali@372
   886
ali@351
   887
		prop_iter = razor_property_iterator_create(set, package);
ali@351
   888
		while (razor_property_iterator_next(prop_iter, &property,
ali@351
   889
						    &name, &flags, &version))
ali@351
   890
			razor_importer_add_property(importer,
ali@351
   891
						    name, flags, version);
ali@351
   892
		razor_property_iterator_destroy(prop_iter);
ali@351
   893
ali@377
   894
		file_iter = razor_file_iterator_create(set, package, 0);
ali@351
   895
		while (razor_file_iterator_next(file_iter, &name)) {
ali@351
   896
			name = razor_relocations_apply(relocations, name);
ali@351
   897
			razor_importer_add_file(importer, name);
ali@351
   898
		}
ali@351
   899
		razor_file_iterator_destroy(file_iter);
ali@351
   900
ali@369
   901
		razor_importer_add_script(importer, RAZOR_PROPERTY_PREUN,
ali@369
   902
					  preunprog, preun);
ali@369
   903
		razor_importer_add_script(importer, RAZOR_PROPERTY_POSTUN,
ali@369
   904
					  postunprog, postun);
ali@369
   905
ali@351
   906
		razor_importer_finish_package(importer);
ali@351
   907
	}
ali@351
   908
ali@351
   909
	razor_package_iterator_destroy(pkg_iter);
ali@351
   910
	return razor_importer_finish(importer);
ali@351
   911
}
ali@351
   912
krh@254
   913
static int
ali@369
   914
install_package(struct razor_transaction *trans, struct razor_set *set,
ali@403
   915
		struct razor_atomic *atomic, struct razor_package *package,
ali@403
   916
		struct razor_relocations *relocations, int install_count,
ali@403
   917
		enum razor_stage_type stage)
ali@363
   918
{
ali@363
   919
	int retval;
ali@363
   920
	const char *name, *version, *arch;
ali@403
   921
	char *file, *s;
ali@363
   922
	struct razor_rpm *rpm;
ali@426
   923
	struct razor_error *error=NULL;
ali@363
   924
ali@363
   925
	razor_package_get_details(set, package,
ali@363
   926
				  RAZOR_DETAIL_NAME, &name,
ali@363
   927
				  RAZOR_DETAIL_VERSION, &version,
ali@363
   928
				  RAZOR_DETAIL_ARCH, &arch,
ali@363
   929
				  RAZOR_DETAIL_LAST);
ali@363
   930
ali@403
   931
	if (stage & RAZOR_STAGE_SCRIPTS_PRE)
ali@403
   932
		printf("install %s-%s\n", name, version);
ali@363
   933
ali@403
   934
	s = rpm_filename(name, version, arch);
ali@403
   935
	file = razor_concat("rpms/", s, NULL);
ali@403
   936
	free(s);
ali@426
   937
	rpm = razor_rpm_open(file, &error);
ali@403
   938
	free(file);
ali@363
   939
	if (rpm == NULL) {
ali@426
   940
		razor_atomic_abort(atomic, razor_error_get_msg(error));
ali@426
   941
		fprintf(stderr, "%s\n", razor_error_get_msg(error));
ali@426
   942
		razor_error_free(error);
ali@363
   943
		return -1;
ali@363
   944
	}
ali@363
   945
	if (relocations)
ali@363
   946
		razor_rpm_set_relocations(rpm, relocations);
ali@369
   947
	razor_transaction_fixup_package(trans, package, rpm);
ali@403
   948
	retval = razor_rpm_install(rpm, atomic, install_root, install_count,
ali@403
   949
				   stage);
ali@403
   950
	if (retval < 0) {
ali@403
   951
		s = rpm_filename(name, version, arch);
ali@403
   952
		fprintf(stderr, "%s: %s\n", s,
ali@403
   953
			razor_atomic_get_error_msg(atomic));
ali@403
   954
		free(s);
ali@403
   955
	}
ali@363
   956
	razor_rpm_close(rpm);
ali@363
   957
	return retval;
ali@363
   958
}
ali@363
   959
ali@412
   960
/*
ali@418
   961
 * Returns 0 on success, -1 on failure and 1 if a RAZOR_INSTALL_ACTION_COMMIT
ali@418
   962
 * is met (in which case the action is consumed).
ali@412
   963
 */
ali@363
   964
static int
ali@403
   965
update_packages(struct razor_transaction *trans,
ali@403
   966
		struct razor_install_iterator *ii, struct razor_set *system,
ali@403
   967
		struct razor_set *next, struct razor_atomic *atomic,
ali@403
   968
		struct razor_relocations *relocations,
ali@418
   969
		enum razor_stage_type stage)
krh@254
   970
{
krh@254
   971
	struct razor_package *package;
krh@316
   972
	enum razor_install_action action;
ali@363
   973
	int retval = 0, count;
ali@403
   974
ali@412
   975
	while (!retval && razor_install_iterator_next(ii, &package, &action,
ali@412
   976
						      &count)) {
ali@418
   977
		if (action == RAZOR_INSTALL_ACTION_ADD) {
ali@418
   978
			if (install_package(trans, next, atomic, package,
ali@418
   979
					    relocations, count, stage))
ali@418
   980
				retval = -1;
ali@418
   981
		} else if (action == RAZOR_INSTALL_ACTION_REMOVE) {
ali@418
   982
			if (razor_package_remove(system, next, atomic, package,
ali@418
   983
						 install_root, count, stage))
ali@418
   984
				retval = -1;
ali@418
   985
		} else if (action == RAZOR_INSTALL_ACTION_COMMIT)
ali@418
   986
				retval = 1;
rhughes@241
   987
	}
rhughes@241
   988
ali@363
   989
	return retval;
rhughes@241
   990
}
rhughes@241
   991
rhughes@241
   992
static int
ali@424
   993
update_system(struct razor_root *root, struct razor_relocations *relocations,
ali@424
   994
	      struct razor_transaction *trans, struct razor_set *next,
ali@424
   995
	      const char *verb)
ali@418
   996
{
ali@424
   997
	struct razor_set *system, *set;
ali@418
   998
	struct razor_atomic *atomic;
ali@418
   999
	struct razor_install_iterator *ii;
ali@418
  1000
	int r, retval = 0;
ali@418
  1001
	char *description;
ali@418
  1002
	size_t pos;
ali@418
  1003
ali@418
  1004
	description = razor_concat(verb, " packages", NULL);
ali@418
  1005
ali@424
  1006
	system = razor_set_ref(razor_root_get_system_set(root));
ali@424
  1007
ali@418
  1008
	ii = razor_set_create_install_iterator(system, next);
ali@418
  1009
ali@418
  1010
	do {
ali@418
  1011
		pos = razor_install_iterator_tell(ii);
ali@418
  1012
ali@418
  1013
		atomic = razor_atomic_open(description);
ali@418
  1014
ali@418
  1015
		r = update_packages(trans, ii, system, next, atomic,
ali@418
  1016
				    relocations, RAZOR_STAGE_SCRIPTS_PRE);
ali@418
  1017
		if (r < 0) {
ali@418
  1018
			fprintf(stderr, "%s aborted\n", verb);
ali@418
  1019
			retval = r;
ali@418
  1020
		} else {
ali@418
  1021
			razor_install_iterator_seek(ii, pos);
ali@418
  1022
			r = update_packages(trans, ii, system, next, atomic,
ali@418
  1023
					    relocations, RAZOR_STAGE_FILES);
ali@418
  1024
ali@418
  1025
			if (r == 1) {
ali@418
  1026
				set = razor_install_iterator_commit_set(ii);
ali@424
  1027
				razor_root_update(root, set, atomic);
ali@418
  1028
				razor_set_unref(set);
ali@418
  1029
			} else if (r == 0)
ali@424
  1030
				razor_root_update(root, next, atomic);
ali@418
  1031
ali@418
  1032
			retval = razor_atomic_commit(atomic);
ali@421
  1033
			if (retval)
ali@418
  1034
				fprintf(stderr, "%s\n",
ali@418
  1035
					razor_atomic_get_error_msg(atomic));
ali@421
  1036
			else {
ali@418
  1037
				razor_install_iterator_seek(ii, pos);
ali@418
  1038
				update_packages(trans, ii, system, next,
ali@418
  1039
						atomic, relocations,
ali@418
  1040
						RAZOR_STAGE_SCRIPTS_POST);
ali@418
  1041
			}
ali@418
  1042
		}
ali@418
  1043
ali@418
  1044
		razor_atomic_destroy(atomic);
ali@418
  1045
	} while(!retval && r == 1);
ali@418
  1046
ali@424
  1047
	razor_set_unref(system);
ali@424
  1048
ali@418
  1049
	free(description);
ali@418
  1050
ali@418
  1051
	return retval;
ali@418
  1052
}
ali@418
  1053
ali@418
  1054
static int
ali@382
  1055
command_install_or_update(int argc, const char *argv[], int do_update)
rhughes@241
  1056
{
ali@424
  1057
	struct razor_relocations *relocations = NULL;
ali@351
  1058
	struct razor_set *system, *upstream, *next, *set;
rhughes@241
  1059
	struct razor_transaction *trans;
ali@424
  1060
	struct razor_error *error = NULL;
ali@403
  1061
	struct razor_atomic *atomic;
ali@424
  1062
	struct razor_root *root;
ali@403
  1063
	int i, retval, len, dependencies = 1;
ali@351
  1064
	char *oldpath;
rhughes@241
  1065
ali@351
  1066
	for (i = 0; i < argc; i++) {
ali@351
  1067
		if (strcmp(argv[i], "--no-dependencies") == 0)
ali@351
  1068
			dependencies = 0;
ali@351
  1069
		else if (strcmp(argv[i], "--relocate") == 0) {
ali@351
  1070
			i++;
ali@351
  1071
			if (i >= argc || strchr(argv[i], '=') == NULL) {
ali@351
  1072
				fprintf(stderr,
ali@382
  1073
				    "Usage: razor %s [OPTION...] RPM\n",
ali@382
  1074
				    do_update ? "update" : "install");
ali@351
  1075
				fprintf(stderr, "Options:\n");
ali@351
  1076
				fprintf(stderr, "    [--no-dependencies]\n");
ali@351
  1077
				fprintf(stderr,
ali@351
  1078
				    "    [--relocate OLDPATH=NEWPATH] RPM\n");
ali@351
  1079
				return -1;
ali@351
  1080
			}
ali@351
  1081
			len = strchr(argv[i], '=') - argv[i];
ali@351
  1082
			oldpath = malloc(len + 1);
ali@351
  1083
			strncpy(oldpath, argv[i], len);
ali@351
  1084
			oldpath[len] = '\0';
ali@351
  1085
			if (!relocations)
ali@351
  1086
			       relocations = razor_relocations_create();
ali@351
  1087
			razor_relocations_add(relocations, oldpath,
ali@351
  1088
					      argv[i] + len + 1);
ali@351
  1089
			free(oldpath);
ali@351
  1090
		} else
ali@351
  1091
			break;
ali@351
  1092
	}
ali@351
  1093
ali@424
  1094
	upstream = razor_set_open(rawhide_repo_filename, 0, &error);
ali@424
  1095
	if (upstream == NULL) {
ali@424
  1096
		fprintf(stderr, "%s\n", razor_error_get_msg(error));
ali@424
  1097
		razor_error_free(error);
ali@424
  1098
		return 1;
ali@424
  1099
	}
ali@424
  1100
ali@418
  1101
	if (do_update)
ali@418
  1102
		atomic = razor_atomic_open("Update packages");
ali@418
  1103
	else
ali@418
  1104
		atomic = razor_atomic_open("Install packages");
ali@418
  1105
ali@351
  1106
	if (relocations) {
ali@403
  1107
		set = relocate_packages(upstream, atomic, relocations);
ali@403
  1108
		if (set == NULL) {
ali@403
  1109
			fprintf(stderr, "%s\n",
ali@403
  1110
				razor_atomic_get_error_msg(atomic));
ali@403
  1111
			razor_atomic_destroy(atomic);
ali@403
  1112
			razor_set_unref(upstream);
ali@403
  1113
			return 1;
ali@403
  1114
		}
ali@403
  1115
		razor_set_unref(upstream);
ali@351
  1116
		upstream = set;
ali@351
  1117
	}
ali@351
  1118
ali@424
  1119
	root = razor_root_open(install_root, &error);
ali@424
  1120
	if (root == NULL) {
ali@424
  1121
		fprintf(stderr, "%s\n", razor_error_get_msg(error));
ali@424
  1122
		razor_error_free(error);
ali@424
  1123
		razor_atomic_destroy(atomic);
ali@424
  1124
		razor_set_unref(upstream);
ali@424
  1125
		if (relocations)
ali@424
  1126
			razor_relocations_destroy(relocations);
ali@424
  1127
		return 1;
ali@424
  1128
	}
ali@403
  1129
ali@424
  1130
	system = razor_root_get_system_set(root);
krh@250
  1131
	trans = razor_transaction_create(system, upstream);
rhughes@241
  1132
ali@382
  1133
	if (i == argc && do_update)
ali@382
  1134
		razor_transaction_update_all(trans);
rhughes@241
  1135
	for (; i < argc; i++) {
ali@389
  1136
		if (do_update &&
ali@389
  1137
		    mark_packages_for_update(trans, system, argv[i]))
ali@389
  1138
			continue;
rhughes@241
  1139
		if (mark_packages_for_update(trans, upstream, argv[i]) == 0) {
rhughes@241
  1140
			fprintf(stderr, "no package matched %s\n", argv[i]);
ali@369
  1141
			razor_transaction_destroy(trans);
ali@424
  1142
			razor_root_close(root);
ali@403
  1143
			razor_set_unref(upstream);
ali@403
  1144
			razor_atomic_destroy(atomic);
ali@424
  1145
			if (relocations)
ali@424
  1146
				razor_relocations_destroy(relocations);
rhughes@241
  1147
			return 1;
rhughes@241
  1148
		}
rhughes@241
  1149
	}
rhughes@241
  1150
rhughes@241
  1151
	if (dependencies) {
krh@245
  1152
		razor_transaction_resolve(trans);
krh@245
  1153
		if (razor_transaction_describe(trans) > 0) {
ali@369
  1154
			razor_transaction_destroy(trans);
ali@403
  1155
			razor_set_unref(upstream);
ali@424
  1156
			razor_root_close(root);
ali@403
  1157
			razor_atomic_destroy(atomic);
ali@424
  1158
			if (relocations)
ali@424
  1159
				razor_relocations_destroy(relocations);
rhughes@241
  1160
			return 1;
rhughes@241
  1161
		}
rhughes@241
  1162
	}
rhughes@241
  1163
ali@403
  1164
	if (razor_atomic_create_dir(atomic, "rpms",
ali@418
  1165
				    S_IRWXU | S_IRWXG | S_IRWXO) ||
ali@418
  1166
	    razor_atomic_commit(atomic)) {
ali@403
  1167
		fprintf(stderr, "%s\n", razor_atomic_get_error_msg(atomic));
ali@369
  1168
		razor_transaction_destroy(trans);
ali@403
  1169
		razor_set_unref(upstream);
ali@424
  1170
		razor_root_close(root);
ali@403
  1171
		razor_atomic_destroy(atomic);
ali@424
  1172
		if (relocations)
ali@424
  1173
			razor_relocations_destroy(relocations);
rhughes@241
  1174
		return 1;
rhughes@241
  1175
	}
rhughes@241
  1176
ali@418
  1177
	razor_atomic_destroy(atomic);
ali@418
  1178
ali@382
  1179
	next = razor_transaction_commit(trans);
ali@382
  1180
krh@254
  1181
	if (download_packages(system, next) < 0) {
ali@403
  1182
		razor_set_unref(next);
ali@369
  1183
		razor_transaction_destroy(trans);
ali@403
  1184
		razor_set_unref(upstream);
ali@424
  1185
		razor_root_close(root);
ali@403
  1186
		razor_atomic_destroy(atomic);
ali@424
  1187
		if (relocations)
ali@424
  1188
			razor_relocations_destroy(relocations);
rhughes@241
  1189
                return 1;
rhughes@241
  1190
        }
rhughes@241
  1191
ali@424
  1192
	retval = update_system(root, relocations, trans, next,
ali@418
  1193
			       do_update ? "Update" : "Install");
ali@403
  1194
ali@418
  1195
	razor_set_unref(upstream);
ali@424
  1196
	razor_root_close(root);
ali@403
  1197
ali@369
  1198
	razor_transaction_destroy(trans);
ali@351
  1199
	if (relocations)
ali@351
  1200
		razor_relocations_destroy(relocations);
ali@403
  1201
ali@403
  1202
	razor_set_unref(next);
ali@403
  1203
ali@403
  1204
	return retval;
rhughes@241
  1205
}
rhughes@241
  1206
rhughes@241
  1207
static int
ali@382
  1208
command_update(int argc, const char *argv[])
ali@382
  1209
{
ali@382
  1210
	return command_install_or_update(argc, argv, 1);
ali@382
  1211
}
ali@382
  1212
ali@382
  1213
static int
ali@382
  1214
command_install(int argc, const char *argv[])
ali@382
  1215
{
ali@382
  1216
	return command_install_or_update(argc, argv, 0);
ali@382
  1217
}
ali@382
  1218
ali@382
  1219
static int
rhughes@241
  1220
command_init(int argc, const char *argv[])
rhughes@241
  1221
{
ali@425
  1222
	int retval;
ali@425
  1223
	struct razor_error *error = NULL;
ali@425
  1224
ali@425
  1225
	retval = razor_root_create(install_root, &error);
ali@425
  1226
	if (retval) {
ali@425
  1227
		fprintf(stderr, "%s\n", razor_error_get_msg(error));
ali@425
  1228
		razor_error_free(error);
ali@425
  1229
	} else
ali@425
  1230
		printf("Created install root\n");
ali@425
  1231
ali@425
  1232
	return retval;
rhughes@241
  1233
}
rhughes@241
  1234
rhughes@241
  1235
static int
rhughes@241
  1236
command_download(int argc, const char *argv[])
rhughes@241
  1237
{
ali@424
  1238
	struct razor_error *error = NULL;
ali@403
  1239
	struct razor_atomic *atomic;
rhughes@241
  1240
	struct razor_set *set;
rhughes@241
  1241
	struct razor_package_iterator *pi;
rhughes@241
  1242
	struct razor_package *package;
rhughes@241
  1243
	const char *pattern = argv[0], *name, *version, *arch;
rhughes@241
  1244
	char url[256], file[256];
rhughes@241
  1245
	int matches = 0;
rhughes@241
  1246
ali@424
  1247
	set = razor_set_open(rawhide_repo_filename, 0, &error);
ali@424
  1248
	if (set == NULL) {
ali@424
  1249
		fprintf(stderr, "%s\n", razor_error_get_msg(error));
ali@424
  1250
		razor_error_free(error);
ali@424
  1251
		return 1;
ali@424
  1252
	}
ali@424
  1253
ali@403
  1254
	atomic = razor_atomic_open("Download packages");
ali@403
  1255
ali@403
  1256
	if (razor_atomic_create_dir(atomic, "rpms", 
ali@403
  1257
				    S_IRWXU | S_IRWXG | S_IRWXO)) {
ali@403
  1258
		fprintf(stderr, "%s\n", razor_atomic_get_error_msg(atomic));
ali@403
  1259
		razor_atomic_destroy(atomic);
rhughes@241
  1260
		return 1;
rhughes@241
  1261
	}
rhughes@241
  1262
ali@403
  1263
	if (razor_atomic_commit(atomic)) {
ali@403
  1264
		fprintf(stderr, "%s\n", razor_atomic_get_error_msg(atomic));
ali@403
  1265
		razor_atomic_destroy(atomic);
ali@403
  1266
		return 1;
ali@403
  1267
	}
ali@403
  1268
	razor_atomic_destroy(atomic);
ali@403
  1269
rhughes@241
  1270
	pi = razor_package_iterator_create(set);
rhughes@241
  1271
	while (razor_package_iterator_next(pi, &package,
richard@302
  1272
					   RAZOR_DETAIL_NAME, &name,
richard@302
  1273
					   RAZOR_DETAIL_VERSION, &version,
richard@307
  1274
					   RAZOR_DETAIL_ARCH, &arch,
richard@307
  1275
					   RAZOR_DETAIL_LAST)) {
rhughes@241
  1276
		if (pattern && fnmatch(pattern, name, 0) != 0)
rhughes@241
  1277
			continue;
rhughes@241
  1278
rhughes@241
  1279
		matches++;
rhughes@241
  1280
		snprintf(url, sizeof url,
rhughes@241
  1281
			 "%s/Packages/%s-%s.%s.rpm",
rhughes@241
  1282
			 yum_url, name, version, arch);
rhughes@241
  1283
		snprintf(file, sizeof file,
rhughes@241
  1284
			 "rpms/%s-%s.%s.rpm", name, version, arch);
rhughes@241
  1285
		download_if_missing(url, file);
rhughes@241
  1286
	}
rhughes@241
  1287
	razor_package_iterator_destroy(pi);
ali@403
  1288
	razor_set_unref(set);
rhughes@241
  1289
rhughes@241
  1290
	if (matches == 0)
rhughes@241
  1291
		fprintf(stderr, "no packages matched \"%s\"\n", pattern);
rhughes@241
  1292
	else if (matches == 1)
rhughes@241
  1293
		fprintf(stderr, "downloaded 1 package\n");
rhughes@241
  1294
	else
rhughes@241
  1295
		fprintf(stderr, "downloaded %d packages\n", matches);
rhughes@241
  1296
rhughes@241
  1297
	return 0;
rhughes@241
  1298
}
rhughes@241
  1299
jbowes@258
  1300
static int
jbowes@258
  1301
command_info(int argc, const char *argv[])
jbowes@258
  1302
{
ali@424
  1303
	struct razor_error *error = NULL;
jbowes@258
  1304
	struct razor_set *set;
jbowes@258
  1305
	struct razor_package_iterator *pi;
jbowes@258
  1306
	struct razor_package *package;
jbowes@258
  1307
	const char *pattern = argv[0], *name, *version, *arch;
jbowes@258
  1308
	const char *summary, *description, *url, *license;
jbowes@258
  1309
ali@424
  1310
	set = razor_root_open_read_only(install_root, &error);
ali@403
  1311
	if (set == NULL) {
ali@424
  1312
		fprintf(stderr, "%s\n", razor_error_get_msg(error));
ali@424
  1313
		razor_error_free(error);
jbowes@288
  1314
		return 1;
ali@403
  1315
	}
krh@317
  1316
jbowes@258
  1317
	pi = razor_package_iterator_create(set);
jbowes@258
  1318
	while (razor_package_iterator_next(pi, &package,
richard@302
  1319
					   RAZOR_DETAIL_NAME, &name,
richard@302
  1320
					   RAZOR_DETAIL_VERSION, &version,
richard@307
  1321
					   RAZOR_DETAIL_ARCH, &arch,
richard@307
  1322
					   RAZOR_DETAIL_LAST)) {
jbowes@258
  1323
		if (pattern && fnmatch(pattern, name, 0) != 0)
jbowes@258
  1324
			continue;
jbowes@258
  1325
richard@302
  1326
		razor_package_get_details (set, package,
richard@302
  1327
					   RAZOR_DETAIL_SUMMARY, &summary,
richard@302
  1328
					   RAZOR_DETAIL_DESCRIPTION, &description,
richard@302
  1329
					   RAZOR_DETAIL_URL, &url,
richard@302
  1330
					   RAZOR_DETAIL_LICENSE, &license,
richard@307
  1331
					   RAZOR_DETAIL_LAST);
jbowes@258
  1332
jbowes@258
  1333
		printf ("Name:        %s\n", name);
jbowes@258
  1334
		printf ("Arch:        %s\n", arch);
jbowes@258
  1335
		printf ("Version:     %s\n", version);
jbowes@258
  1336
		printf ("URL:         %s\n", url);
jbowes@258
  1337
		printf ("License:     %s\n", license);
jbowes@258
  1338
		printf ("Summary:     %s\n", summary);
jbowes@258
  1339
		printf ("Description:\n");
jbowes@258
  1340
		printf ("%s\n", description);
jbowes@258
  1341
		printf ("\n");
jbowes@258
  1342
	}
jbowes@258
  1343
	razor_package_iterator_destroy(pi);
ali@403
  1344
	razor_set_unref(set);
jbowes@258
  1345
jbowes@258
  1346
	return 0;
jbowes@258
  1347
}
jbowes@258
  1348
jbowes@292
  1349
#define SEARCH_MAX 256
jbowes@292
  1350
jbowes@292
  1351
static int
jbowes@292
  1352
command_search(int argc, const char *argv[])
jbowes@292
  1353
{
ali@424
  1354
	struct razor_error *error = NULL;
jbowes@292
  1355
	struct razor_set *set;
jbowes@292
  1356
	struct razor_package_iterator *pi;
jbowes@292
  1357
	struct razor_package *package;
jbowes@292
  1358
	char pattern[SEARCH_MAX];
jbowes@292
  1359
	const char *name, *version, *arch;
jbowes@292
  1360
	const char *summary, *description, *url, *license;
jbowes@292
  1361
jbowes@292
  1362
	if (!argv[0]) {
jbowes@292
  1363
		fprintf(stderr, "must specify a search term\n");
jbowes@292
  1364
		return 1;
jbowes@292
  1365
	}
jbowes@292
  1366
krh@294
  1367
	snprintf(pattern, sizeof pattern, "*%s*", argv[0]);
jbowes@292
  1368
ali@424
  1369
	set = razor_set_open(rawhide_repo_filename, 0, &error);
ali@424
  1370
	if (set == NULL) {
ali@424
  1371
		fprintf(stderr, "%s\n", razor_error_get_msg(error));
ali@424
  1372
		razor_error_free(error);
jbowes@292
  1373
		return 1;
ali@403
  1374
	}
richard@310
  1375
jbowes@292
  1376
	pi = razor_package_iterator_create(set);
jbowes@292
  1377
	while (razor_package_iterator_next(pi, &package,
richard@304
  1378
					   RAZOR_DETAIL_NAME, &name,
richard@304
  1379
					   RAZOR_DETAIL_VERSION, &version,
krh@305
  1380
					   RAZOR_DETAIL_ARCH, &arch,
krh@305
  1381
					   RAZOR_DETAIL_SUMMARY, &summary,
krh@305
  1382
					   RAZOR_DETAIL_DESCRIPTION, &description,
krh@305
  1383
					   RAZOR_DETAIL_URL, &url,
krh@305
  1384
					   RAZOR_DETAIL_LICENSE, &license,
richard@307
  1385
					   RAZOR_DETAIL_LAST)) {
krh@305
  1386
		if (!fnmatch(pattern, name, FNM_CASEFOLD) ||
krh@305
  1387
		    !fnmatch(pattern, url, FNM_CASEFOLD) ||
krh@305
  1388
		    !fnmatch(pattern, summary, FNM_CASEFOLD) ||
krh@305
  1389
		    !fnmatch(pattern, description, FNM_CASEFOLD))
krh@305
  1390
			printf("%s-%s.%s: %s\n", name, version, arch, summary);
jbowes@292
  1391
	}
jbowes@292
  1392
	razor_package_iterator_destroy(pi);
ali@403
  1393
	razor_set_unref(set);
jbowes@292
  1394
jbowes@292
  1395
	return 0;
jbowes@292
  1396
}
jbowes@292
  1397
rhughes@241
  1398
static struct {
rhughes@241
  1399
	const char *name;
rhughes@241
  1400
	const char *description;
rhughes@241
  1401
	int (*func)(int argc, const char *argv[]);
rhughes@241
  1402
} razor_commands[] = {
rhughes@241
  1403
	{ "list", "list all packages", command_list },
rhughes@241
  1404
	{ "list-requires", "list all requires for the given package", command_list_requires },
rhughes@241
  1405
	{ "list-provides", "list all provides for the given package", command_list_provides },
rhughes@241
  1406
	{ "list-obsoletes", "list all obsoletes for the given package", command_list_obsoletes },
rhughes@241
  1407
	{ "list-conflicts", "list all conflicts for the given package", command_list_conflicts },
ali@369
  1408
	{ "list-scripts", "list all scripts for the given package", command_list_scripts },
rhughes@241
  1409
	{ "list-files", "list files for package set", command_list_files },
rhughes@241
  1410
	{ "list-file-packages", "list packages owning file", command_list_file_packages },
rhughes@241
  1411
	{ "list-package-files", "list files in package", command_list_package_files },
rhughes@241
  1412
	{ "what-requires", "list the packages that have the given requires", command_what_requires },
rhughes@241
  1413
	{ "what-provides", "list the packages that have the given provides", command_what_provides },
rhughes@241
  1414
	{ "import-yum", "import yum metadata files", command_import_yum },
ali@320
  1415
#if HAVE_RPMLIB
rhughes@241
  1416
	{ "import-rpmdb", "import the system rpm database", command_import_rpmdb },
ali@320
  1417
#endif
rhughes@241
  1418
	{ "import-rpms", "import rpms from the given directory", command_import_rpms },
rhughes@241
  1419
	{ "update", "update all or specified packages", command_update },
rhughes@241
  1420
	{ "remove", "remove specified packages", command_remove },
rhughes@241
  1421
	{ "diff", "show diff between two package sets", command_diff },
rhughes@241
  1422
	{ "install", "install rpm", command_install },
rhughes@241
  1423
	{ "init", "init razor root", command_init },
jbowes@258
  1424
	{ "download", "download packages", command_download },
jbowes@292
  1425
	{ "info", "display package details", command_info },
jbowes@292
  1426
	{ "search", "search package details", command_search }
rhughes@241
  1427
};
rhughes@241
  1428
rhughes@241
  1429
static int
rhughes@241
  1430
usage(void)
rhughes@241
  1431
{
rhughes@241
  1432
	int i;
rhughes@241
  1433
rhughes@241
  1434
	printf("usage:\n");
rhughes@241
  1435
	for (i = 0; i < ARRAY_SIZE(razor_commands); i++)
rhughes@241
  1436
		printf("  %-20s%s\n",
rhughes@241
  1437
		       razor_commands[i].name, razor_commands[i].description);
rhughes@241
  1438
rhughes@241
  1439
	return 1;
rhughes@241
  1440
}
rhughes@241
  1441
rhughes@241
  1442
int
rhughes@241
  1443
main(int argc, const char *argv[])
rhughes@241
  1444
{
krh@317
  1445
	char *repo, *root;
rhughes@241
  1446
	int i;
rhughes@241
  1447
rhughes@241
  1448
	repo = getenv("RAZOR_REPO");
rhughes@241
  1449
	if (repo != NULL)
rhughes@241
  1450
		repo_filename = repo;
rhughes@241
  1451
krh@317
  1452
	root = getenv("RAZOR_ROOT");
krh@317
  1453
	if (root != NULL)
krh@317
  1454
		install_root = root;
krh@317
  1455
rhughes@241
  1456
	yum_url = getenv("YUM_URL");
rhughes@241
  1457
	if (yum_url == NULL)
rhughes@241
  1458
		yum_url = YUM_URL;
rhughes@241
  1459
ali@359
  1460
	if (getenv("RAZOR_NO_ROOT_NAME_CHECKS"))
ali@359
  1461
		razor_disable_root_name_checks(1);
ali@359
  1462
rhughes@241
  1463
	if (argc < 2)
rhughes@241
  1464
		return usage();
rhughes@241
  1465
rhughes@241
  1466
	for (i = 0; i < ARRAY_SIZE(razor_commands); i++)
rhughes@241
  1467
		if (strcmp(razor_commands[i].name, argv[1]) == 0)
rhughes@241
  1468
			return razor_commands[i].func(argc - 2, argv + 2);
rhughes@241
  1469
rhughes@241
  1470
	return usage();
rhughes@241
  1471
}