src/main.c
author J. Ali Harlow <ali@juiblex.co.uk>
Sat Aug 23 17:24:34 2014 +0100 (2014-08-23)
changeset 443 b2d6a8273459
parent 426 2e896ad9754b
child 445 aada48958b92
permissions -rw-r--r--
Update libtool versioning in preparation for release
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
ali@442
   414
#ifdef HAVE_CURL
rhughes@241
   415
static int
rhughes@241
   416
show_progress(void *clientp,
rhughes@241
   417
	      double dltotal, double dlnow, double ultotal, double ulnow)
rhughes@241
   418
{
rhughes@241
   419
	const char *file = clientp;
rhughes@241
   420
rhughes@241
   421
	if (!dlnow < dltotal)
rhughes@241
   422
		fprintf(stderr, "\rdownloading %s, %dkB/%dkB",
rhughes@241
   423
			file, (int) dlnow / 1024, (int) dltotal / 1024);
rhughes@241
   424
rhughes@241
   425
	return 0;
rhughes@241
   426
}
ali@442
   427
#endif	/* HAVE_CURL */
rhughes@241
   428
rhughes@241
   429
static int
rhughes@241
   430
download_if_missing(const char *url, const char *file)
rhughes@241
   431
{
ali@321
   432
#ifndef HAVE_CURL
ali@321
   433
	return 1;
ali@321
   434
#else
rhughes@241
   435
	CURL *curl;
rhughes@241
   436
	struct stat buf;
rhughes@241
   437
	char error[256];
rhughes@241
   438
	FILE *fp;
rhughes@241
   439
	CURLcode res;
rhughes@241
   440
	long response;
rhughes@241
   441
rhughes@241
   442
	curl = curl_easy_init();
rhughes@241
   443
	if (curl == NULL)
rhughes@241
   444
		return 1;
rhughes@241
   445
rhughes@241
   446
	curl_easy_setopt(curl, CURLOPT_ERRORBUFFER, error);
rhughes@241
   447
	curl_easy_setopt(curl, CURLOPT_NOPROGRESS, 0);
rhughes@241
   448
	curl_easy_setopt(curl, CURLOPT_PROGRESSFUNCTION, show_progress);
rhughes@241
   449
	curl_easy_setopt(curl, CURLOPT_PROGRESSDATA, file);
rhughes@241
   450
rhughes@241
   451
	if (stat(file, &buf) < 0) {
ali@345
   452
		fp = fopen(file, "wb");
rhughes@241
   453
		if (fp == NULL) {
rhughes@241
   454
			fprintf(stderr,
rhughes@241
   455
				"failed to open %s for writing\n", file);
rhughes@241
   456
			return -1;
rhughes@241
   457
		}
rhughes@241
   458
		curl_easy_setopt(curl, CURLOPT_WRITEDATA, fp);
rhughes@241
   459
		curl_easy_setopt(curl, CURLOPT_URL, url);
rhughes@241
   460
		res = curl_easy_perform(curl);
rhughes@241
   461
		fclose(fp);
rhughes@241
   462
		if (res != CURLE_OK) {
rhughes@241
   463
			fprintf(stderr, "curl error: %s\n", error);
rhughes@241
   464
			unlink(file);
rhughes@241
   465
			return -1;
rhughes@241
   466
		}
rhughes@241
   467
		res = curl_easy_getinfo(curl,
rhughes@241
   468
					CURLINFO_RESPONSE_CODE, &response);
rhughes@241
   469
		if (res != CURLE_OK) {
rhughes@241
   470
			fprintf(stderr, "curl error: %s\n", error);
rhughes@241
   471
                        unlink(file);
rhughes@241
   472
                        return -1;
rhughes@241
   473
		}
rhughes@241
   474
		if (response != 200) {
rhughes@241
   475
			fprintf(stderr, " - failed %ld\n", response);
rhughes@241
   476
                        unlink(file);
rhughes@241
   477
                        return -1;
rhughes@241
   478
		}
rhughes@241
   479
		fprintf(stderr, "\n");
rhughes@241
   480
	}
rhughes@241
   481
rhughes@241
   482
	curl_easy_cleanup(curl);
rhughes@241
   483
rhughes@241
   484
	return 0;
ali@321
   485
#endif	/* HAVE_CURL */
rhughes@241
   486
}
rhughes@241
   487
rhughes@241
   488
#define YUM_URL "http://download.fedora.redhat.com" \
rhughes@241
   489
	"/pub/fedora/linux/development/i386/os"
rhughes@241
   490
rhughes@241
   491
static int
rhughes@241
   492
command_import_yum(int argc, const char *argv[])
rhughes@241
   493
{
ali@403
   494
	int retval;
rhughes@241
   495
	struct razor_set *set;
ali@403
   496
	struct razor_atomic *atomic;
rhughes@241
   497
	char buffer[512];
rhughes@241
   498
rhughes@241
   499
	printf("downloading from %s.\n", yum_url);
rhughes@241
   500
	snprintf(buffer, sizeof buffer,
rhughes@241
   501
		 "%s/repodata/primary.xml.gz", yum_url);
rhughes@241
   502
	if (download_if_missing(buffer, "primary.xml.gz") < 0)
rhughes@241
   503
		return -1;
rhughes@241
   504
	snprintf(buffer, sizeof buffer,
rhughes@241
   505
		 "%s/repodata/filelists.xml.gz", yum_url);
rhughes@241
   506
	if (download_if_missing(buffer, "filelists.xml.gz") < 0)
rhughes@241
   507
		return -1;
rhughes@241
   508
rhughes@241
   509
	set = razor_set_create_from_yum();
rhughes@241
   510
	if (set == NULL)
rhughes@241
   511
		return 1;
ali@403
   512
	atomic = razor_atomic_open("Yum import repository");
ali@403
   513
	razor_set_write(set, atomic, rawhide_repo_filename, RAZOR_SECTION_ALL);
ali@403
   514
	retval = razor_atomic_commit(atomic);
ali@403
   515
	razor_set_unref(set);
ali@403
   516
	if (retval)
ali@403
   517
		fprintf(stderr, "%s\n", razor_atomic_get_error_msg(atomic));
ali@403
   518
	else
ali@403
   519
		printf("wrote %s\n", rawhide_repo_filename);
ali@403
   520
	razor_atomic_destroy(atomic);
rhughes@241
   521
ali@403
   522
	return retval;
rhughes@241
   523
}
rhughes@241
   524
ali@320
   525
#if HAVE_RPMLIB
rhughes@241
   526
static int
rhughes@241
   527
command_import_rpmdb(int argc, const char *argv[])
rhughes@241
   528
{
rhughes@241
   529
	struct razor_set *set;
krh@317
   530
	struct razor_root *root;
ali@424
   531
	struct razor_error *error = NULL;
ali@403
   532
	struct razor_atomic *atomic;
ali@403
   533
	int retval;
krh@317
   534
ali@424
   535
	root = razor_root_open(install_root, &error);
ali@403
   536
	if (root == NULL) {
ali@424
   537
		fprintf(stderr, "%s\n", razor_error_get_msg(error));
ali@424
   538
		razor_error_free(error);
krh@317
   539
		return 1;
ali@403
   540
	}
rhughes@241
   541
rhughes@241
   542
	set = razor_set_create_from_rpmdb();
rhughes@241
   543
	if (set == NULL)
rhughes@241
   544
		return 1;
rhughes@241
   545
ali@424
   546
	atomic = razor_atomic_open("Import RPM database");
krh@317
   547
ali@424
   548
	retval = razor_root_update(root, set, atomic);
ali@424
   549
ali@403
   550
	if (retval)
ali@403
   551
		fprintf(stderr, "%s\n", razor_atomic_get_error_msg(atomic));
ali@403
   552
ali@403
   553
	razor_atomic_destroy(atomic);
ali@403
   554
ali@424
   555
	razor_root_close(root);
ali@424
   556
ali@403
   557
	return retval;
rhughes@241
   558
}
ali@320
   559
#endif
rhughes@241
   560
rhughes@241
   561
static int
rhughes@241
   562
mark_packages_for_update(struct razor_transaction *trans,
rhughes@241
   563
			 struct razor_set *set, const char *pattern)
rhughes@241
   564
{
rhughes@241
   565
	struct razor_package_iterator *pi;
rhughes@241
   566
	struct razor_package *package;
richard@302
   567
	const char *name;
rhughes@241
   568
	int matches = 0;
rhughes@241
   569
rhughes@241
   570
	pi = razor_package_iterator_create(set);
rhughes@241
   571
	while (razor_package_iterator_next(pi, &package,
richard@307
   572
					   RAZOR_DETAIL_NAME, &name,
richard@307
   573
					   RAZOR_DETAIL_LAST)) {
rhughes@241
   574
		if (pattern && fnmatch(pattern, name, 0) == 0) {
rhughes@241
   575
			razor_transaction_update_package(trans, package);
rhughes@241
   576
			matches++;
rhughes@241
   577
		}
rhughes@241
   578
	}
rhughes@241
   579
	razor_package_iterator_destroy(pi);
rhughes@241
   580
rhughes@241
   581
	return matches;
rhughes@241
   582
}
rhughes@241
   583
rhughes@241
   584
static int
rhughes@241
   585
mark_packages_for_removal(struct razor_transaction *trans,
rhughes@241
   586
			  struct razor_set *set, const char *pattern)
rhughes@241
   587
{
rhughes@241
   588
	struct razor_package_iterator *pi;
rhughes@241
   589
	struct razor_package *package;
richard@302
   590
	const char *name;
rhughes@241
   591
	int matches = 0;
rhughes@241
   592
rhughes@241
   593
	pi = razor_package_iterator_create(set);
richard@307
   594
	while (razor_package_iterator_next(pi, &package,
richard@307
   595
					   RAZOR_DETAIL_NAME, &name,
richard@307
   596
					   RAZOR_DETAIL_LAST)) {
rhughes@241
   597
		if (pattern && fnmatch(pattern, name, 0) == 0) {
rhughes@241
   598
			razor_transaction_remove_package(trans, package);
rhughes@241
   599
			matches++;
rhughes@241
   600
		}
rhughes@241
   601
	}
rhughes@241
   602
	razor_package_iterator_destroy(pi);
rhughes@241
   603
rhughes@241
   604
	return matches;
rhughes@241
   605
}
rhughes@241
   606
rhughes@241
   607
static int
rhughes@241
   608
command_remove(int argc, const char *argv[])
rhughes@241
   609
{
ali@363
   610
	struct razor_set *system, *upstream, *next;
rhughes@241
   611
	struct razor_transaction *trans;
ali@424
   612
	struct razor_error *error = NULL;
ali@424
   613
	struct razor_root *root;
ali@403
   614
	int i, retval;
rhughes@241
   615
ali@424
   616
	root = razor_root_open(install_root, &error);
ali@424
   617
	if (root == NULL) {
ali@424
   618
		fprintf(stderr, "%s\n", razor_error_get_msg(error));
ali@424
   619
		razor_error_free(error);
rhughes@241
   620
		return 1;
ali@363
   621
	}
rhughes@241
   622
ali@424
   623
	system = razor_root_get_system_set(root);
ali@363
   624
	upstream = razor_set_create_without_root();
ali@363
   625
	trans = razor_transaction_create(system, upstream);
ali@403
   626
	razor_set_unref(upstream);
rhughes@241
   627
	for (i = 0; i < argc; i++) {
ali@363
   628
		if (mark_packages_for_removal(trans, system, argv[i]) == 0) {
rhughes@241
   629
			fprintf(stderr, "no match for %s\n", argv[i]);
ali@363
   630
			razor_transaction_destroy(trans);
ali@424
   631
			razor_root_close(root);
rhughes@241
   632
			return 1;
rhughes@241
   633
		}
rhughes@241
   634
	}
rhughes@241
   635
ali@350
   636
	razor_transaction_resolve(trans);
ali@403
   637
	retval = razor_transaction_describe(trans);
ali@403
   638
	if (retval) {
ali@363
   639
		razor_transaction_destroy(trans);
ali@424
   640
		razor_root_close(root);
rhughes@241
   641
		return 1;
ali@363
   642
	}
rhughes@241
   643
ali@369
   644
	next = razor_transaction_commit(trans);
ali@403
   645
ali@424
   646
	retval = update_system(root, NULL, trans, next, "Remove");
ali@363
   647
ali@369
   648
	razor_transaction_destroy(trans);
ali@424
   649
	razor_root_close(root);
ali@403
   650
	razor_set_unref(next);
rhughes@241
   651
ali@403
   652
	return retval;
rhughes@241
   653
}
rhughes@241
   654
rhughes@241
   655
static void
krh@253
   656
print_diff(enum razor_diff_action action,
krh@253
   657
	   struct razor_package *package,
krh@253
   658
	   const char *name,
krh@253
   659
	   const char *version,
krh@253
   660
	   const char *arch,
rhughes@241
   661
	   void *data)
rhughes@241
   662
{
krh@253
   663
	if (action == RAZOR_DIFF_ACTION_ADD)
krh@253
   664
		printf("install %s-%s.%s\n", name, version, arch);
krh@253
   665
	if (action == RAZOR_DIFF_ACTION_REMOVE)
krh@253
   666
		printf("remove %s-%s.%s\n", name, version, arch);
rhughes@241
   667
}
rhughes@241
   668
rhughes@241
   669
static int
rhughes@241
   670
command_diff(int argc, const char *argv[])
rhughes@241
   671
{
ali@424
   672
	struct razor_error *error = NULL;
rhughes@241
   673
	struct razor_set *set, *updated;
rhughes@241
   674
ali@424
   675
	set = razor_root_open_read_only(install_root, &error);
ali@424
   676
	if (set)
ali@424
   677
		updated = razor_set_open(rawhide_repo_filename, 0, &error);
ali@424
   678
	else
ali@424
   679
		updated = NULL;
ali@424
   680
	if (updated == NULL) {
ali@424
   681
		fprintf(stderr, "%s\n", razor_error_get_msg(error));
ali@424
   682
		razor_error_free(error);
ali@424
   683
		if (set)
ali@424
   684
			razor_set_unref(set);
rhughes@241
   685
		return 1;
ali@403
   686
	}
rhughes@241
   687
rhughes@241
   688
	razor_set_diff(set, updated, print_diff, NULL);
rhughes@241
   689
ali@403
   690
	razor_set_unref(set);
ali@403
   691
	razor_set_unref(updated);
rhughes@241
   692
rhughes@241
   693
	return 0;
rhughes@241
   694
}
rhughes@241
   695
rhughes@241
   696
static int
rhughes@241
   697
command_import_rpms(int argc, const char *argv[])
rhughes@241
   698
{
rhughes@241
   699
	DIR *dir;
rhughes@241
   700
	struct dirent *de;
rhughes@241
   701
	struct razor_importer *importer;
rhughes@241
   702
	struct razor_set *set;
rhughes@241
   703
	struct razor_rpm *rpm;
ali@426
   704
	struct razor_error *error=NULL;
ali@403
   705
	struct razor_atomic *atomic;
jbowes@263
   706
	int len, imported_count = 0;
rhughes@241
   707
	char filename[256];
rhughes@241
   708
	const char *dirname = argv[0];
ali@403
   709
	int retval;
rhughes@241
   710
rhughes@241
   711
	if (dirname == NULL) {
rhughes@241
   712
		fprintf(stderr, "usage: razor import-rpms DIR\n");
rhughes@241
   713
		return -1;
rhughes@241
   714
	}
rhughes@241
   715
rhughes@241
   716
	dir = opendir(dirname);
rhughes@241
   717
	if (dir == NULL) {
rhughes@241
   718
		fprintf(stderr, "couldn't read dir %s\n", dirname);
rhughes@241
   719
		return -1;
rhughes@241
   720
	}
rhughes@241
   721
krh@249
   722
	importer = razor_importer_create();
rhughes@241
   723
rhughes@241
   724
	while (de = readdir(dir), de != NULL) {
rhughes@241
   725
		len = strlen(de->d_name);
rhughes@241
   726
		if (len < 5 || strcmp(de->d_name + len - 4, ".rpm") != 0)
ali@403
   727
			continue;
rhughes@241
   728
		snprintf(filename, sizeof filename,
rhughes@241
   729
			 "%s/%s", dirname, de->d_name);
ali@426
   730
		rpm = razor_rpm_open(filename, &error);
ali@426
   731
		if (rpm == NULL) {
ali@426
   732
			fprintf(stderr, "%s\n", razor_error_get_msg(error));
ali@426
   733
			razor_error_free(error);
ali@426
   734
			error=NULL;
rhughes@241
   735
			continue;
ali@426
   736
		}
rhughes@241
   737
		if (razor_importer_add_rpm(importer, rpm)) {
rhughes@241
   738
			fprintf(stderr, "couldn't import %s\n", filename);
rhughes@241
   739
			break;
rhughes@241
   740
		}
rhughes@241
   741
		razor_rpm_close(rpm);
jbowes@263
   742
jbowes@263
   743
		printf("\rimporting %d", ++imported_count);
jbowes@263
   744
		fflush(stdout);
rhughes@241
   745
	}
rhughes@241
   746
rhughes@241
   747
	if (de != NULL) {
rhughes@241
   748
		razor_importer_destroy(importer);
rhughes@241
   749
		return -1;
rhughes@241
   750
	}
rhughes@241
   751
jbowes@263
   752
	printf("\nsaving\n");
rhughes@241
   753
	set = razor_importer_finish(importer);
rhughes@241
   754
ali@403
   755
	atomic = razor_atomic_open("Update system database");
ali@403
   756
	razor_set_write(set, atomic, repo_filename, RAZOR_SECTION_ALL);
ali@403
   757
	razor_set_unref(set);
ali@403
   758
	retval = razor_atomic_commit(atomic);
ali@403
   759
	if (retval)
ali@403
   760
		fprintf(stderr, "%s\n", razor_atomic_get_error_msg(atomic));
ali@403
   761
	else
ali@403
   762
		printf("wrote %s\n", repo_filename);
ali@403
   763
	razor_atomic_destroy(atomic);
rhughes@241
   764
ali@403
   765
	return retval;
rhughes@241
   766
}
rhughes@241
   767
ali@403
   768
static char *
krh@254
   769
rpm_filename(const char *name, const char *version, const char *arch)
rhughes@241
   770
{
krh@254
   771
 	const char *v;
krh@254
   772
 
krh@254
   773
 	/* Skip epoch */
krh@253
   774
	v = strchr(version, ':');
krh@254
   775
 	if (v != NULL)
krh@254
   776
 		v = v + 1;
krh@254
   777
 	else
krh@253
   778
		v = version;
rhughes@241
   779
ali@403
   780
	return razor_concat(name, "-", v, ".", arch, ".rpm", NULL);
rhughes@241
   781
}
rhughes@241
   782
krh@254
   783
static int
krh@254
   784
download_packages(struct razor_set *system, struct razor_set *next)
rhughes@241
   785
{
krh@316
   786
	struct razor_install_iterator *ii;
krh@254
   787
	struct razor_package *package;
krh@316
   788
	enum razor_install_action action;
krh@254
   789
	const char *name, *version, *arch;
ali@403
   790
	char *file, *url, *s;
krh@316
   791
	int errors = 0, count;
krh@316
   792
krh@316
   793
	ii = razor_set_create_install_iterator(system, next);
ali@382
   794
	while (razor_install_iterator_next(ii, &package, &action, &count)) {
ali@418
   795
		if (action != RAZOR_INSTALL_ACTION_ADD)
krh@316
   796
			continue;
krh@316
   797
ali@382
   798
		razor_package_get_details(next, package,
krh@316
   799
					  RAZOR_DETAIL_NAME, &name,
krh@316
   800
					  RAZOR_DETAIL_VERSION, &version,
krh@316
   801
					  RAZOR_DETAIL_ARCH, &arch,
krh@316
   802
					  RAZOR_DETAIL_LAST);
krh@316
   803
		
ali@403
   804
		s = rpm_filename(name, version, arch);
ali@403
   805
		url = razor_concat(yum_url, "/Packages/", s, NULL);
ali@403
   806
		file = razor_concat("rpms/", s, NULL);
ali@403
   807
		free(s);
krh@254
   808
		if (download_if_missing(url, file) < 0)
krh@254
   809
			errors++;
ali@403
   810
		free(file);
ali@403
   811
		free(url);
krh@254
   812
	}
krh@316
   813
	razor_install_iterator_destroy(ii);
krh@254
   814
krh@254
   815
	if (errors > 0) {
krh@254
   816
		fprintf(stderr, "failed to download %d packages\n", errors);
krh@254
   817
                return -1;
krh@254
   818
        }
krh@254
   819
krh@254
   820
	return 0;
krh@254
   821
}
krh@254
   822
ali@351
   823
static struct razor_set *
ali@403
   824
relocate_packages(struct razor_set *set, struct razor_atomic *atomic,
ali@403
   825
		  struct razor_relocations *relocations)
ali@351
   826
{
ali@372
   827
	int i;
ali@351
   828
	struct razor_importer *importer;
ali@351
   829
	struct razor_property_iterator *prop_iter;
ali@351
   830
	struct razor_package_iterator *pkg_iter;
ali@351
   831
 	struct razor_file_iterator *file_iter;
ali@351
   832
 	struct razor_package *package;
ali@351
   833
	struct razor_property *property;
ali@351
   834
	struct razor_rpm *rpm;
ali@426
   835
	struct razor_error *error=NULL;
ali@351
   836
	const char *name, *version, *arch, *summary, *desc, *url, *license;
ali@369
   837
	const char *preunprog, *preun, *postunprog, *postun;
ali@372
   838
	const char *install_prefix;
ali@372
   839
	const char *const *prefixes;
ali@403
   840
	char *file, *s;
ali@351
   841
	uint32_t flags;
ali@351
   842
ali@351
   843
	importer = razor_importer_create();
ali@351
   844
	pkg_iter = razor_package_iterator_create(set);
ali@351
   845
ali@351
   846
	while (razor_package_iterator_next(pkg_iter, &package,
ali@351
   847
					   RAZOR_DETAIL_NAME, &name,
ali@351
   848
					   RAZOR_DETAIL_VERSION, &version,
ali@351
   849
					   RAZOR_DETAIL_ARCH, &arch,
ali@351
   850
					   RAZOR_DETAIL_SUMMARY, &summary,
ali@351
   851
					   RAZOR_DETAIL_DESCRIPTION, &desc,
ali@351
   852
					   RAZOR_DETAIL_URL, &url,
ali@351
   853
					   RAZOR_DETAIL_LICENSE, &license,
ali@369
   854
					   RAZOR_DETAIL_PREUNPROG, &preunprog,
ali@369
   855
					   RAZOR_DETAIL_PREUN, &preun,
ali@369
   856
					   RAZOR_DETAIL_POSTUNPROG, &postunprog,
ali@369
   857
					   RAZOR_DETAIL_POSTUN, &postun,
ali@351
   858
					   RAZOR_DETAIL_LAST)) {
ali@403
   859
		s = rpm_filename(name, version, arch);
ali@403
   860
		file = razor_concat("rpms/", s, NULL);
ali@403
   861
		free(s);
ali@426
   862
		rpm = razor_rpm_open(file, &error);
ali@403
   863
		free(file);
ali@351
   864
		if (rpm == NULL) {
ali@426
   865
			razor_atomic_abort(atomic, razor_error_get_msg(error));
ali@426
   866
			razor_error_free(error);
ali@351
   867
			razor_package_iterator_destroy(pkg_iter);
ali@351
   868
			razor_importer_destroy(importer);
ali@351
   869
			return NULL;
ali@351
   870
		}
ali@351
   871
ali@351
   872
		razor_relocations_set_rpm(relocations, rpm);
ali@351
   873
ali@351
   874
		razor_importer_begin_package(importer, name, version, arch);
ali@351
   875
		razor_importer_add_details(importer,
ali@351
   876
					   summary, desc, url, license);
ali@351
   877
ali@372
   878
		razor_rpm_get_details(rpm, RAZOR_DETAIL_PREFIXES, &prefixes,
ali@372
   879
				      RAZOR_DETAIL_LAST);
ali@372
   880
		for (i = 0; prefixes && prefixes[i]; i++) {
ali@372
   881
			install_prefix = razor_relocations_apply(relocations,
ali@372
   882
								 prefixes[i]);
ali@372
   883
			razor_importer_add_install_prefix(importer,
ali@372
   884
							  install_prefix);
ali@372
   885
		}
ali@372
   886
ali@372
   887
		razor_rpm_close(rpm);
ali@372
   888
ali@351
   889
		prop_iter = razor_property_iterator_create(set, package);
ali@351
   890
		while (razor_property_iterator_next(prop_iter, &property,
ali@351
   891
						    &name, &flags, &version))
ali@351
   892
			razor_importer_add_property(importer,
ali@351
   893
						    name, flags, version);
ali@351
   894
		razor_property_iterator_destroy(prop_iter);
ali@351
   895
ali@377
   896
		file_iter = razor_file_iterator_create(set, package, 0);
ali@351
   897
		while (razor_file_iterator_next(file_iter, &name)) {
ali@351
   898
			name = razor_relocations_apply(relocations, name);
ali@351
   899
			razor_importer_add_file(importer, name);
ali@351
   900
		}
ali@351
   901
		razor_file_iterator_destroy(file_iter);
ali@351
   902
ali@369
   903
		razor_importer_add_script(importer, RAZOR_PROPERTY_PREUN,
ali@369
   904
					  preunprog, preun);
ali@369
   905
		razor_importer_add_script(importer, RAZOR_PROPERTY_POSTUN,
ali@369
   906
					  postunprog, postun);
ali@369
   907
ali@351
   908
		razor_importer_finish_package(importer);
ali@351
   909
	}
ali@351
   910
ali@351
   911
	razor_package_iterator_destroy(pkg_iter);
ali@351
   912
	return razor_importer_finish(importer);
ali@351
   913
}
ali@351
   914
krh@254
   915
static int
ali@369
   916
install_package(struct razor_transaction *trans, struct razor_set *set,
ali@403
   917
		struct razor_atomic *atomic, struct razor_package *package,
ali@403
   918
		struct razor_relocations *relocations, int install_count,
ali@403
   919
		enum razor_stage_type stage)
ali@363
   920
{
ali@363
   921
	int retval;
ali@363
   922
	const char *name, *version, *arch;
ali@403
   923
	char *file, *s;
ali@363
   924
	struct razor_rpm *rpm;
ali@426
   925
	struct razor_error *error=NULL;
ali@363
   926
ali@363
   927
	razor_package_get_details(set, package,
ali@363
   928
				  RAZOR_DETAIL_NAME, &name,
ali@363
   929
				  RAZOR_DETAIL_VERSION, &version,
ali@363
   930
				  RAZOR_DETAIL_ARCH, &arch,
ali@363
   931
				  RAZOR_DETAIL_LAST);
ali@363
   932
ali@403
   933
	if (stage & RAZOR_STAGE_SCRIPTS_PRE)
ali@403
   934
		printf("install %s-%s\n", name, version);
ali@363
   935
ali@403
   936
	s = rpm_filename(name, version, arch);
ali@403
   937
	file = razor_concat("rpms/", s, NULL);
ali@403
   938
	free(s);
ali@426
   939
	rpm = razor_rpm_open(file, &error);
ali@403
   940
	free(file);
ali@363
   941
	if (rpm == NULL) {
ali@426
   942
		razor_atomic_abort(atomic, razor_error_get_msg(error));
ali@426
   943
		fprintf(stderr, "%s\n", razor_error_get_msg(error));
ali@426
   944
		razor_error_free(error);
ali@363
   945
		return -1;
ali@363
   946
	}
ali@363
   947
	if (relocations)
ali@363
   948
		razor_rpm_set_relocations(rpm, relocations);
ali@369
   949
	razor_transaction_fixup_package(trans, package, rpm);
ali@403
   950
	retval = razor_rpm_install(rpm, atomic, install_root, install_count,
ali@403
   951
				   stage);
ali@403
   952
	if (retval < 0) {
ali@403
   953
		s = rpm_filename(name, version, arch);
ali@403
   954
		fprintf(stderr, "%s: %s\n", s,
ali@403
   955
			razor_atomic_get_error_msg(atomic));
ali@403
   956
		free(s);
ali@403
   957
	}
ali@363
   958
	razor_rpm_close(rpm);
ali@363
   959
	return retval;
ali@363
   960
}
ali@363
   961
ali@412
   962
/*
ali@418
   963
 * Returns 0 on success, -1 on failure and 1 if a RAZOR_INSTALL_ACTION_COMMIT
ali@418
   964
 * is met (in which case the action is consumed).
ali@412
   965
 */
ali@363
   966
static int
ali@403
   967
update_packages(struct razor_transaction *trans,
ali@403
   968
		struct razor_install_iterator *ii, struct razor_set *system,
ali@403
   969
		struct razor_set *next, struct razor_atomic *atomic,
ali@403
   970
		struct razor_relocations *relocations,
ali@418
   971
		enum razor_stage_type stage)
krh@254
   972
{
krh@254
   973
	struct razor_package *package;
krh@316
   974
	enum razor_install_action action;
ali@363
   975
	int retval = 0, count;
ali@403
   976
ali@412
   977
	while (!retval && razor_install_iterator_next(ii, &package, &action,
ali@412
   978
						      &count)) {
ali@418
   979
		if (action == RAZOR_INSTALL_ACTION_ADD) {
ali@418
   980
			if (install_package(trans, next, atomic, package,
ali@418
   981
					    relocations, count, stage))
ali@418
   982
				retval = -1;
ali@418
   983
		} else if (action == RAZOR_INSTALL_ACTION_REMOVE) {
ali@418
   984
			if (razor_package_remove(system, next, atomic, package,
ali@418
   985
						 install_root, count, stage))
ali@418
   986
				retval = -1;
ali@418
   987
		} else if (action == RAZOR_INSTALL_ACTION_COMMIT)
ali@418
   988
				retval = 1;
rhughes@241
   989
	}
rhughes@241
   990
ali@363
   991
	return retval;
rhughes@241
   992
}
rhughes@241
   993
rhughes@241
   994
static int
ali@424
   995
update_system(struct razor_root *root, struct razor_relocations *relocations,
ali@424
   996
	      struct razor_transaction *trans, struct razor_set *next,
ali@424
   997
	      const char *verb)
ali@418
   998
{
ali@424
   999
	struct razor_set *system, *set;
ali@418
  1000
	struct razor_atomic *atomic;
ali@418
  1001
	struct razor_install_iterator *ii;
ali@418
  1002
	int r, retval = 0;
ali@418
  1003
	char *description;
ali@418
  1004
	size_t pos;
ali@418
  1005
ali@418
  1006
	description = razor_concat(verb, " packages", NULL);
ali@418
  1007
ali@424
  1008
	system = razor_set_ref(razor_root_get_system_set(root));
ali@424
  1009
ali@418
  1010
	ii = razor_set_create_install_iterator(system, next);
ali@418
  1011
ali@418
  1012
	do {
ali@418
  1013
		pos = razor_install_iterator_tell(ii);
ali@418
  1014
ali@418
  1015
		atomic = razor_atomic_open(description);
ali@418
  1016
ali@418
  1017
		r = update_packages(trans, ii, system, next, atomic,
ali@418
  1018
				    relocations, RAZOR_STAGE_SCRIPTS_PRE);
ali@418
  1019
		if (r < 0) {
ali@418
  1020
			fprintf(stderr, "%s aborted\n", verb);
ali@418
  1021
			retval = r;
ali@418
  1022
		} else {
ali@418
  1023
			razor_install_iterator_seek(ii, pos);
ali@418
  1024
			r = update_packages(trans, ii, system, next, atomic,
ali@418
  1025
					    relocations, RAZOR_STAGE_FILES);
ali@418
  1026
ali@418
  1027
			if (r == 1) {
ali@418
  1028
				set = razor_install_iterator_commit_set(ii);
ali@424
  1029
				razor_root_update(root, set, atomic);
ali@418
  1030
				razor_set_unref(set);
ali@418
  1031
			} else if (r == 0)
ali@424
  1032
				razor_root_update(root, next, atomic);
ali@418
  1033
ali@418
  1034
			retval = razor_atomic_commit(atomic);
ali@421
  1035
			if (retval)
ali@418
  1036
				fprintf(stderr, "%s\n",
ali@418
  1037
					razor_atomic_get_error_msg(atomic));
ali@421
  1038
			else {
ali@418
  1039
				razor_install_iterator_seek(ii, pos);
ali@418
  1040
				update_packages(trans, ii, system, next,
ali@418
  1041
						atomic, relocations,
ali@418
  1042
						RAZOR_STAGE_SCRIPTS_POST);
ali@418
  1043
			}
ali@418
  1044
		}
ali@418
  1045
ali@418
  1046
		razor_atomic_destroy(atomic);
ali@418
  1047
	} while(!retval && r == 1);
ali@418
  1048
ali@424
  1049
	razor_set_unref(system);
ali@424
  1050
ali@418
  1051
	free(description);
ali@418
  1052
ali@418
  1053
	return retval;
ali@418
  1054
}
ali@418
  1055
ali@418
  1056
static int
ali@382
  1057
command_install_or_update(int argc, const char *argv[], int do_update)
rhughes@241
  1058
{
ali@424
  1059
	struct razor_relocations *relocations = NULL;
ali@351
  1060
	struct razor_set *system, *upstream, *next, *set;
rhughes@241
  1061
	struct razor_transaction *trans;
ali@424
  1062
	struct razor_error *error = NULL;
ali@403
  1063
	struct razor_atomic *atomic;
ali@424
  1064
	struct razor_root *root;
ali@403
  1065
	int i, retval, len, dependencies = 1;
ali@351
  1066
	char *oldpath;
rhughes@241
  1067
ali@351
  1068
	for (i = 0; i < argc; i++) {
ali@351
  1069
		if (strcmp(argv[i], "--no-dependencies") == 0)
ali@351
  1070
			dependencies = 0;
ali@351
  1071
		else if (strcmp(argv[i], "--relocate") == 0) {
ali@351
  1072
			i++;
ali@351
  1073
			if (i >= argc || strchr(argv[i], '=') == NULL) {
ali@351
  1074
				fprintf(stderr,
ali@382
  1075
				    "Usage: razor %s [OPTION...] RPM\n",
ali@382
  1076
				    do_update ? "update" : "install");
ali@351
  1077
				fprintf(stderr, "Options:\n");
ali@351
  1078
				fprintf(stderr, "    [--no-dependencies]\n");
ali@351
  1079
				fprintf(stderr,
ali@351
  1080
				    "    [--relocate OLDPATH=NEWPATH] RPM\n");
ali@351
  1081
				return -1;
ali@351
  1082
			}
ali@351
  1083
			len = strchr(argv[i], '=') - argv[i];
ali@351
  1084
			oldpath = malloc(len + 1);
ali@351
  1085
			strncpy(oldpath, argv[i], len);
ali@351
  1086
			oldpath[len] = '\0';
ali@351
  1087
			if (!relocations)
ali@351
  1088
			       relocations = razor_relocations_create();
ali@351
  1089
			razor_relocations_add(relocations, oldpath,
ali@351
  1090
					      argv[i] + len + 1);
ali@351
  1091
			free(oldpath);
ali@351
  1092
		} else
ali@351
  1093
			break;
ali@351
  1094
	}
ali@351
  1095
ali@424
  1096
	upstream = razor_set_open(rawhide_repo_filename, 0, &error);
ali@424
  1097
	if (upstream == NULL) {
ali@424
  1098
		fprintf(stderr, "%s\n", razor_error_get_msg(error));
ali@424
  1099
		razor_error_free(error);
ali@424
  1100
		return 1;
ali@424
  1101
	}
ali@424
  1102
ali@418
  1103
	if (do_update)
ali@418
  1104
		atomic = razor_atomic_open("Update packages");
ali@418
  1105
	else
ali@418
  1106
		atomic = razor_atomic_open("Install packages");
ali@418
  1107
ali@351
  1108
	if (relocations) {
ali@403
  1109
		set = relocate_packages(upstream, atomic, relocations);
ali@403
  1110
		if (set == NULL) {
ali@403
  1111
			fprintf(stderr, "%s\n",
ali@403
  1112
				razor_atomic_get_error_msg(atomic));
ali@403
  1113
			razor_atomic_destroy(atomic);
ali@403
  1114
			razor_set_unref(upstream);
ali@403
  1115
			return 1;
ali@403
  1116
		}
ali@403
  1117
		razor_set_unref(upstream);
ali@351
  1118
		upstream = set;
ali@351
  1119
	}
ali@351
  1120
ali@424
  1121
	root = razor_root_open(install_root, &error);
ali@424
  1122
	if (root == NULL) {
ali@424
  1123
		fprintf(stderr, "%s\n", razor_error_get_msg(error));
ali@424
  1124
		razor_error_free(error);
ali@424
  1125
		razor_atomic_destroy(atomic);
ali@424
  1126
		razor_set_unref(upstream);
ali@424
  1127
		if (relocations)
ali@424
  1128
			razor_relocations_destroy(relocations);
ali@424
  1129
		return 1;
ali@424
  1130
	}
ali@403
  1131
ali@424
  1132
	system = razor_root_get_system_set(root);
krh@250
  1133
	trans = razor_transaction_create(system, upstream);
rhughes@241
  1134
ali@382
  1135
	if (i == argc && do_update)
ali@382
  1136
		razor_transaction_update_all(trans);
rhughes@241
  1137
	for (; i < argc; i++) {
ali@389
  1138
		if (do_update &&
ali@389
  1139
		    mark_packages_for_update(trans, system, argv[i]))
ali@389
  1140
			continue;
rhughes@241
  1141
		if (mark_packages_for_update(trans, upstream, argv[i]) == 0) {
rhughes@241
  1142
			fprintf(stderr, "no package matched %s\n", argv[i]);
ali@369
  1143
			razor_transaction_destroy(trans);
ali@424
  1144
			razor_root_close(root);
ali@403
  1145
			razor_set_unref(upstream);
ali@403
  1146
			razor_atomic_destroy(atomic);
ali@424
  1147
			if (relocations)
ali@424
  1148
				razor_relocations_destroy(relocations);
rhughes@241
  1149
			return 1;
rhughes@241
  1150
		}
rhughes@241
  1151
	}
rhughes@241
  1152
rhughes@241
  1153
	if (dependencies) {
krh@245
  1154
		razor_transaction_resolve(trans);
krh@245
  1155
		if (razor_transaction_describe(trans) > 0) {
ali@369
  1156
			razor_transaction_destroy(trans);
ali@403
  1157
			razor_set_unref(upstream);
ali@424
  1158
			razor_root_close(root);
ali@403
  1159
			razor_atomic_destroy(atomic);
ali@424
  1160
			if (relocations)
ali@424
  1161
				razor_relocations_destroy(relocations);
rhughes@241
  1162
			return 1;
rhughes@241
  1163
		}
rhughes@241
  1164
	}
rhughes@241
  1165
ali@403
  1166
	if (razor_atomic_create_dir(atomic, "rpms",
ali@418
  1167
				    S_IRWXU | S_IRWXG | S_IRWXO) ||
ali@418
  1168
	    razor_atomic_commit(atomic)) {
ali@403
  1169
		fprintf(stderr, "%s\n", razor_atomic_get_error_msg(atomic));
ali@369
  1170
		razor_transaction_destroy(trans);
ali@403
  1171
		razor_set_unref(upstream);
ali@424
  1172
		razor_root_close(root);
ali@403
  1173
		razor_atomic_destroy(atomic);
ali@424
  1174
		if (relocations)
ali@424
  1175
			razor_relocations_destroy(relocations);
rhughes@241
  1176
		return 1;
rhughes@241
  1177
	}
rhughes@241
  1178
ali@418
  1179
	razor_atomic_destroy(atomic);
ali@418
  1180
ali@382
  1181
	next = razor_transaction_commit(trans);
ali@382
  1182
krh@254
  1183
	if (download_packages(system, next) < 0) {
ali@403
  1184
		razor_set_unref(next);
ali@369
  1185
		razor_transaction_destroy(trans);
ali@403
  1186
		razor_set_unref(upstream);
ali@424
  1187
		razor_root_close(root);
ali@403
  1188
		razor_atomic_destroy(atomic);
ali@424
  1189
		if (relocations)
ali@424
  1190
			razor_relocations_destroy(relocations);
rhughes@241
  1191
                return 1;
rhughes@241
  1192
        }
rhughes@241
  1193
ali@424
  1194
	retval = update_system(root, relocations, trans, next,
ali@418
  1195
			       do_update ? "Update" : "Install");
ali@403
  1196
ali@418
  1197
	razor_set_unref(upstream);
ali@424
  1198
	razor_root_close(root);
ali@403
  1199
ali@369
  1200
	razor_transaction_destroy(trans);
ali@351
  1201
	if (relocations)
ali@351
  1202
		razor_relocations_destroy(relocations);
ali@403
  1203
ali@403
  1204
	razor_set_unref(next);
ali@403
  1205
ali@403
  1206
	return retval;
rhughes@241
  1207
}
rhughes@241
  1208
rhughes@241
  1209
static int
ali@382
  1210
command_update(int argc, const char *argv[])
ali@382
  1211
{
ali@382
  1212
	return command_install_or_update(argc, argv, 1);
ali@382
  1213
}
ali@382
  1214
ali@382
  1215
static int
ali@382
  1216
command_install(int argc, const char *argv[])
ali@382
  1217
{
ali@382
  1218
	return command_install_or_update(argc, argv, 0);
ali@382
  1219
}
ali@382
  1220
ali@382
  1221
static int
rhughes@241
  1222
command_init(int argc, const char *argv[])
rhughes@241
  1223
{
ali@425
  1224
	int retval;
ali@425
  1225
	struct razor_error *error = NULL;
ali@425
  1226
ali@425
  1227
	retval = razor_root_create(install_root, &error);
ali@425
  1228
	if (retval) {
ali@425
  1229
		fprintf(stderr, "%s\n", razor_error_get_msg(error));
ali@425
  1230
		razor_error_free(error);
ali@425
  1231
	} else
ali@425
  1232
		printf("Created install root\n");
ali@425
  1233
ali@425
  1234
	return retval;
rhughes@241
  1235
}
rhughes@241
  1236
rhughes@241
  1237
static int
rhughes@241
  1238
command_download(int argc, const char *argv[])
rhughes@241
  1239
{
ali@424
  1240
	struct razor_error *error = NULL;
ali@403
  1241
	struct razor_atomic *atomic;
rhughes@241
  1242
	struct razor_set *set;
rhughes@241
  1243
	struct razor_package_iterator *pi;
rhughes@241
  1244
	struct razor_package *package;
rhughes@241
  1245
	const char *pattern = argv[0], *name, *version, *arch;
rhughes@241
  1246
	char url[256], file[256];
rhughes@241
  1247
	int matches = 0;
rhughes@241
  1248
ali@424
  1249
	set = razor_set_open(rawhide_repo_filename, 0, &error);
ali@424
  1250
	if (set == NULL) {
ali@424
  1251
		fprintf(stderr, "%s\n", razor_error_get_msg(error));
ali@424
  1252
		razor_error_free(error);
ali@424
  1253
		return 1;
ali@424
  1254
	}
ali@424
  1255
ali@403
  1256
	atomic = razor_atomic_open("Download packages");
ali@403
  1257
ali@403
  1258
	if (razor_atomic_create_dir(atomic, "rpms", 
ali@403
  1259
				    S_IRWXU | S_IRWXG | S_IRWXO)) {
ali@403
  1260
		fprintf(stderr, "%s\n", razor_atomic_get_error_msg(atomic));
ali@403
  1261
		razor_atomic_destroy(atomic);
rhughes@241
  1262
		return 1;
rhughes@241
  1263
	}
rhughes@241
  1264
ali@403
  1265
	if (razor_atomic_commit(atomic)) {
ali@403
  1266
		fprintf(stderr, "%s\n", razor_atomic_get_error_msg(atomic));
ali@403
  1267
		razor_atomic_destroy(atomic);
ali@403
  1268
		return 1;
ali@403
  1269
	}
ali@403
  1270
	razor_atomic_destroy(atomic);
ali@403
  1271
rhughes@241
  1272
	pi = razor_package_iterator_create(set);
rhughes@241
  1273
	while (razor_package_iterator_next(pi, &package,
richard@302
  1274
					   RAZOR_DETAIL_NAME, &name,
richard@302
  1275
					   RAZOR_DETAIL_VERSION, &version,
richard@307
  1276
					   RAZOR_DETAIL_ARCH, &arch,
richard@307
  1277
					   RAZOR_DETAIL_LAST)) {
rhughes@241
  1278
		if (pattern && fnmatch(pattern, name, 0) != 0)
rhughes@241
  1279
			continue;
rhughes@241
  1280
rhughes@241
  1281
		matches++;
rhughes@241
  1282
		snprintf(url, sizeof url,
rhughes@241
  1283
			 "%s/Packages/%s-%s.%s.rpm",
rhughes@241
  1284
			 yum_url, name, version, arch);
rhughes@241
  1285
		snprintf(file, sizeof file,
rhughes@241
  1286
			 "rpms/%s-%s.%s.rpm", name, version, arch);
rhughes@241
  1287
		download_if_missing(url, file);
rhughes@241
  1288
	}
rhughes@241
  1289
	razor_package_iterator_destroy(pi);
ali@403
  1290
	razor_set_unref(set);
rhughes@241
  1291
rhughes@241
  1292
	if (matches == 0)
rhughes@241
  1293
		fprintf(stderr, "no packages matched \"%s\"\n", pattern);
rhughes@241
  1294
	else if (matches == 1)
rhughes@241
  1295
		fprintf(stderr, "downloaded 1 package\n");
rhughes@241
  1296
	else
rhughes@241
  1297
		fprintf(stderr, "downloaded %d packages\n", matches);
rhughes@241
  1298
rhughes@241
  1299
	return 0;
rhughes@241
  1300
}
rhughes@241
  1301
jbowes@258
  1302
static int
jbowes@258
  1303
command_info(int argc, const char *argv[])
jbowes@258
  1304
{
ali@424
  1305
	struct razor_error *error = NULL;
jbowes@258
  1306
	struct razor_set *set;
jbowes@258
  1307
	struct razor_package_iterator *pi;
jbowes@258
  1308
	struct razor_package *package;
jbowes@258
  1309
	const char *pattern = argv[0], *name, *version, *arch;
jbowes@258
  1310
	const char *summary, *description, *url, *license;
jbowes@258
  1311
ali@424
  1312
	set = razor_root_open_read_only(install_root, &error);
ali@403
  1313
	if (set == NULL) {
ali@424
  1314
		fprintf(stderr, "%s\n", razor_error_get_msg(error));
ali@424
  1315
		razor_error_free(error);
jbowes@288
  1316
		return 1;
ali@403
  1317
	}
krh@317
  1318
jbowes@258
  1319
	pi = razor_package_iterator_create(set);
jbowes@258
  1320
	while (razor_package_iterator_next(pi, &package,
richard@302
  1321
					   RAZOR_DETAIL_NAME, &name,
richard@302
  1322
					   RAZOR_DETAIL_VERSION, &version,
richard@307
  1323
					   RAZOR_DETAIL_ARCH, &arch,
richard@307
  1324
					   RAZOR_DETAIL_LAST)) {
jbowes@258
  1325
		if (pattern && fnmatch(pattern, name, 0) != 0)
jbowes@258
  1326
			continue;
jbowes@258
  1327
richard@302
  1328
		razor_package_get_details (set, package,
richard@302
  1329
					   RAZOR_DETAIL_SUMMARY, &summary,
richard@302
  1330
					   RAZOR_DETAIL_DESCRIPTION, &description,
richard@302
  1331
					   RAZOR_DETAIL_URL, &url,
richard@302
  1332
					   RAZOR_DETAIL_LICENSE, &license,
richard@307
  1333
					   RAZOR_DETAIL_LAST);
jbowes@258
  1334
jbowes@258
  1335
		printf ("Name:        %s\n", name);
jbowes@258
  1336
		printf ("Arch:        %s\n", arch);
jbowes@258
  1337
		printf ("Version:     %s\n", version);
jbowes@258
  1338
		printf ("URL:         %s\n", url);
jbowes@258
  1339
		printf ("License:     %s\n", license);
jbowes@258
  1340
		printf ("Summary:     %s\n", summary);
jbowes@258
  1341
		printf ("Description:\n");
jbowes@258
  1342
		printf ("%s\n", description);
jbowes@258
  1343
		printf ("\n");
jbowes@258
  1344
	}
jbowes@258
  1345
	razor_package_iterator_destroy(pi);
ali@403
  1346
	razor_set_unref(set);
jbowes@258
  1347
jbowes@258
  1348
	return 0;
jbowes@258
  1349
}
jbowes@258
  1350
jbowes@292
  1351
#define SEARCH_MAX 256
jbowes@292
  1352
jbowes@292
  1353
static int
jbowes@292
  1354
command_search(int argc, const char *argv[])
jbowes@292
  1355
{
ali@424
  1356
	struct razor_error *error = NULL;
jbowes@292
  1357
	struct razor_set *set;
jbowes@292
  1358
	struct razor_package_iterator *pi;
jbowes@292
  1359
	struct razor_package *package;
jbowes@292
  1360
	char pattern[SEARCH_MAX];
jbowes@292
  1361
	const char *name, *version, *arch;
jbowes@292
  1362
	const char *summary, *description, *url, *license;
jbowes@292
  1363
jbowes@292
  1364
	if (!argv[0]) {
jbowes@292
  1365
		fprintf(stderr, "must specify a search term\n");
jbowes@292
  1366
		return 1;
jbowes@292
  1367
	}
jbowes@292
  1368
krh@294
  1369
	snprintf(pattern, sizeof pattern, "*%s*", argv[0]);
jbowes@292
  1370
ali@424
  1371
	set = razor_set_open(rawhide_repo_filename, 0, &error);
ali@424
  1372
	if (set == NULL) {
ali@424
  1373
		fprintf(stderr, "%s\n", razor_error_get_msg(error));
ali@424
  1374
		razor_error_free(error);
jbowes@292
  1375
		return 1;
ali@403
  1376
	}
richard@310
  1377
jbowes@292
  1378
	pi = razor_package_iterator_create(set);
jbowes@292
  1379
	while (razor_package_iterator_next(pi, &package,
richard@304
  1380
					   RAZOR_DETAIL_NAME, &name,
richard@304
  1381
					   RAZOR_DETAIL_VERSION, &version,
krh@305
  1382
					   RAZOR_DETAIL_ARCH, &arch,
krh@305
  1383
					   RAZOR_DETAIL_SUMMARY, &summary,
krh@305
  1384
					   RAZOR_DETAIL_DESCRIPTION, &description,
krh@305
  1385
					   RAZOR_DETAIL_URL, &url,
krh@305
  1386
					   RAZOR_DETAIL_LICENSE, &license,
richard@307
  1387
					   RAZOR_DETAIL_LAST)) {
krh@305
  1388
		if (!fnmatch(pattern, name, FNM_CASEFOLD) ||
krh@305
  1389
		    !fnmatch(pattern, url, FNM_CASEFOLD) ||
krh@305
  1390
		    !fnmatch(pattern, summary, FNM_CASEFOLD) ||
krh@305
  1391
		    !fnmatch(pattern, description, FNM_CASEFOLD))
krh@305
  1392
			printf("%s-%s.%s: %s\n", name, version, arch, summary);
jbowes@292
  1393
	}
jbowes@292
  1394
	razor_package_iterator_destroy(pi);
ali@403
  1395
	razor_set_unref(set);
jbowes@292
  1396
jbowes@292
  1397
	return 0;
jbowes@292
  1398
}
jbowes@292
  1399
rhughes@241
  1400
static struct {
rhughes@241
  1401
	const char *name;
rhughes@241
  1402
	const char *description;
rhughes@241
  1403
	int (*func)(int argc, const char *argv[]);
rhughes@241
  1404
} razor_commands[] = {
rhughes@241
  1405
	{ "list", "list all packages", command_list },
rhughes@241
  1406
	{ "list-requires", "list all requires for the given package", command_list_requires },
rhughes@241
  1407
	{ "list-provides", "list all provides for the given package", command_list_provides },
rhughes@241
  1408
	{ "list-obsoletes", "list all obsoletes for the given package", command_list_obsoletes },
rhughes@241
  1409
	{ "list-conflicts", "list all conflicts for the given package", command_list_conflicts },
ali@369
  1410
	{ "list-scripts", "list all scripts for the given package", command_list_scripts },
rhughes@241
  1411
	{ "list-files", "list files for package set", command_list_files },
rhughes@241
  1412
	{ "list-file-packages", "list packages owning file", command_list_file_packages },
rhughes@241
  1413
	{ "list-package-files", "list files in package", command_list_package_files },
rhughes@241
  1414
	{ "what-requires", "list the packages that have the given requires", command_what_requires },
rhughes@241
  1415
	{ "what-provides", "list the packages that have the given provides", command_what_provides },
rhughes@241
  1416
	{ "import-yum", "import yum metadata files", command_import_yum },
ali@320
  1417
#if HAVE_RPMLIB
rhughes@241
  1418
	{ "import-rpmdb", "import the system rpm database", command_import_rpmdb },
ali@320
  1419
#endif
rhughes@241
  1420
	{ "import-rpms", "import rpms from the given directory", command_import_rpms },
rhughes@241
  1421
	{ "update", "update all or specified packages", command_update },
rhughes@241
  1422
	{ "remove", "remove specified packages", command_remove },
rhughes@241
  1423
	{ "diff", "show diff between two package sets", command_diff },
rhughes@241
  1424
	{ "install", "install rpm", command_install },
rhughes@241
  1425
	{ "init", "init razor root", command_init },
jbowes@258
  1426
	{ "download", "download packages", command_download },
jbowes@292
  1427
	{ "info", "display package details", command_info },
jbowes@292
  1428
	{ "search", "search package details", command_search }
rhughes@241
  1429
};
rhughes@241
  1430
rhughes@241
  1431
static int
rhughes@241
  1432
usage(void)
rhughes@241
  1433
{
rhughes@241
  1434
	int i;
rhughes@241
  1435
rhughes@241
  1436
	printf("usage:\n");
rhughes@241
  1437
	for (i = 0; i < ARRAY_SIZE(razor_commands); i++)
rhughes@241
  1438
		printf("  %-20s%s\n",
rhughes@241
  1439
		       razor_commands[i].name, razor_commands[i].description);
rhughes@241
  1440
rhughes@241
  1441
	return 1;
rhughes@241
  1442
}
rhughes@241
  1443
rhughes@241
  1444
int
rhughes@241
  1445
main(int argc, const char *argv[])
rhughes@241
  1446
{
krh@317
  1447
	char *repo, *root;
rhughes@241
  1448
	int i;
rhughes@241
  1449
rhughes@241
  1450
	repo = getenv("RAZOR_REPO");
rhughes@241
  1451
	if (repo != NULL)
rhughes@241
  1452
		repo_filename = repo;
rhughes@241
  1453
krh@317
  1454
	root = getenv("RAZOR_ROOT");
krh@317
  1455
	if (root != NULL)
krh@317
  1456
		install_root = root;
krh@317
  1457
rhughes@241
  1458
	yum_url = getenv("YUM_URL");
rhughes@241
  1459
	if (yum_url == NULL)
rhughes@241
  1460
		yum_url = YUM_URL;
rhughes@241
  1461
ali@359
  1462
	if (getenv("RAZOR_NO_ROOT_NAME_CHECKS"))
ali@359
  1463
		razor_disable_root_name_checks(1);
ali@359
  1464
rhughes@241
  1465
	if (argc < 2)
rhughes@241
  1466
		return usage();
rhughes@241
  1467
rhughes@241
  1468
	for (i = 0; i < ARRAY_SIZE(razor_commands); i++)
rhughes@241
  1469
		if (strcmp(razor_commands[i].name, argv[1]) == 0)
rhughes@241
  1470
			return razor_commands[i].func(argc - 2, argv + 2);
rhughes@241
  1471
rhughes@241
  1472
	return usage();
rhughes@241
  1473
}