src/main.c
author Kristian H?gsberg <krh@redhat.com>
Tue Jul 08 21:56:53 2008 -0400 (2008-07-08)
changeset 314 df5dc9230730
parent 307 95b6bcadd6c4
child 315 1c52b84bfc33
permissions -rw-r--r--
Fix old typo in file merging code.
rhughes@241
     1
/*
rhughes@241
     2
 * Copyright (C) 2008  Kristian Høgsberg <krh@redhat.com>
rhughes@241
     3
 * Copyright (C) 2008  Red Hat, Inc
rhughes@241
     4
 *
rhughes@241
     5
 * This program is free software; you can redistribute it and/or modify
rhughes@241
     6
 * it under the terms of the GNU General Public License as published by
rhughes@241
     7
 * the Free Software Foundation; either version 2 of the License, or
rhughes@241
     8
 * (at your option) any later version.
rhughes@241
     9
 *
rhughes@241
    10
 * This program is distributed in the hope that it will be useful,
rhughes@241
    11
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
rhughes@241
    12
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
rhughes@241
    13
 * GNU General Public License for more details.
rhughes@241
    14
 *
rhughes@241
    15
 * You should have received a copy of the GNU General Public License along
rhughes@241
    16
 * with this program; if not, write to the Free Software Foundation, Inc.,
rhughes@241
    17
 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
rhughes@241
    18
 */
rhughes@241
    19
krh@305
    20
#define _GNU_SOURCE
krh@305
    21
rhughes@241
    22
#include <stdlib.h>
rhughes@241
    23
#include <stddef.h>
rhughes@241
    24
#include <stdio.h>
rhughes@241
    25
#include <stdint.h>
rhughes@241
    26
#include <string.h>
rhughes@241
    27
#include <sys/stat.h>
rhughes@241
    28
#include <unistd.h>
rhughes@241
    29
#include <fcntl.h>
rhughes@241
    30
#include <dirent.h>
rhughes@241
    31
#include <curl/curl.h>
rhughes@241
    32
#include <fnmatch.h>
rhughes@241
    33
#include <errno.h>
rhughes@241
    34
#include "razor.h"
rhughes@241
    35
richard@310
    36
static const char system_repo_filename[] = "system.rzdb";
richard@310
    37
static const char next_repo_filename[] = "system-next.rzdb";
richard@310
    38
static const char rawhide_repo_filename[] = "rawhide.rzdb";
richard@310
    39
static const char updated_repo_filename[] = "system-updated.rzdb";
rhughes@241
    40
static const char install_root[] = "install";
rhughes@241
    41
static const char *repo_filename = system_repo_filename;
rhughes@241
    42
static const char *yum_url;
rhughes@241
    43
krh@271
    44
#define ARRAY_SIZE(a) (sizeof(a) / sizeof((a)[0]))
krh@271
    45
krh@281
    46
static struct razor_package_iterator *
krh@281
    47
create_iterator_from_argv(struct razor_set *set, int argc, const char *argv[])
rhughes@241
    48
{
krh@281
    49
	struct razor_package_query *query;
krh@281
    50
	struct razor_package_iterator *iter;
rhughes@241
    51
	struct razor_package *package;
richard@302
    52
	const char *name, *pattern;
krh@281
    53
	int i, count;
rhughes@241
    54
krh@281
    55
	if (argc == 0)
krh@281
    56
		return razor_package_iterator_create(set);
krh@281
    57
krh@281
    58
	query = razor_package_query_create(set);
krh@281
    59
krh@281
    60
	for (i = 0; i < argc; i++) {
krh@281
    61
		iter = razor_package_iterator_create(set);
krh@281
    62
		pattern = argv[i];
krh@281
    63
		count = 0;
richard@307
    64
		while (razor_package_iterator_next(iter, &package,
richard@307
    65
						   RAZOR_DETAIL_NAME, &name,
richard@307
    66
						   RAZOR_DETAIL_LAST)) {
krh@281
    67
			if (fnmatch(pattern, name, 0) != 0)
krh@281
    68
				continue;
krh@281
    69
krh@281
    70
			razor_package_query_add_package(query, package);
krh@281
    71
			count++;
krh@281
    72
		}
krh@281
    73
		razor_package_iterator_destroy(iter);
krh@281
    74
krh@281
    75
		if (count == 0)
krh@281
    76
			fprintf(stderr,
krh@281
    77
				"no package matches \"%s\"\n", pattern);
rhughes@241
    78
	}
rhughes@241
    79
krh@281
    80
	return razor_package_query_finish(query);
krh@281
    81
}
krh@281
    82
krh@281
    83
#define LIST_PACKAGES_ONLY_NAMES 0x01
krh@281
    84
krh@281
    85
static void
krh@281
    86
list_packages(struct razor_package_iterator *iter, uint32_t flags)
krh@281
    87
{
krh@281
    88
	struct razor_package *package;
krh@281
    89
	const char *name, *version, *arch;
krh@281
    90
krh@281
    91
	while (razor_package_iterator_next(iter, &package,
richard@302
    92
					   RAZOR_DETAIL_NAME, &name,
richard@302
    93
					   RAZOR_DETAIL_VERSION, &version,
richard@307
    94
					   RAZOR_DETAIL_ARCH, &arch,
richard@307
    95
					   RAZOR_DETAIL_LAST)) {
krh@281
    96
		if (flags & LIST_PACKAGES_ONLY_NAMES)
rhughes@241
    97
			printf("%s\n", name);
rhughes@241
    98
		else
rhughes@241
    99
			printf("%s-%s.%s\n", name, version, arch);
rhughes@241
   100
	}
krh@281
   101
}
krh@281
   102
krh@281
   103
static int
krh@281
   104
command_list(int argc, const char *argv[])
krh@281
   105
{
krh@281
   106
	struct razor_package_iterator *pi;
krh@281
   107
	struct razor_set *set;
krh@281
   108
	uint32_t flags = 0;
krh@281
   109
	int i = 0;
krh@281
   110
krh@281
   111
	if (i < argc && strcmp(argv[i], "--only-names") == 0) {
krh@281
   112
		flags |= LIST_PACKAGES_ONLY_NAMES;
krh@281
   113
		i++;
krh@281
   114
	}
krh@281
   115
krh@281
   116
	set = razor_set_open(repo_filename);
krh@281
   117
	pi = create_iterator_from_argv(set, argc - i, argv + i);
krh@281
   118
	list_packages(pi, flags);
rhughes@241
   119
	razor_package_iterator_destroy(pi);
rhughes@241
   120
	razor_set_destroy(set);
rhughes@241
   121
rhughes@241
   122
	return 0;
rhughes@241
   123
}
rhughes@241
   124
krh@306
   125
static void
krh@306
   126
list_package_properties(struct razor_set *set,
krh@306
   127
			struct razor_package *package, uint32_t type)
rhughes@241
   128
{
krh@306
   129
	struct razor_property_iterator *pi;
rhughes@241
   130
	struct razor_property *property;
rhughes@241
   131
	const char *name, *version;
krh@247
   132
	uint32_t flags;
rhughes@241
   133
rhughes@241
   134
	pi = razor_property_iterator_create(set, package);
rhughes@241
   135
	while (razor_property_iterator_next(pi, &property,
krh@247
   136
					    &name, &flags, &version)) {
krh@247
   137
		if ((flags & RAZOR_PROPERTY_TYPE_MASK) != type)
rhughes@241
   138
			continue;
krh@247
   139
		printf("%s", name);
krh@247
   140
		if (version[0] != '\0')
krh@247
   141
			printf(" %s %s",
krh@247
   142
			       razor_property_relation_to_string(property),
krh@247
   143
			       version);
krh@247
   144
krh@247
   145
		if (flags & ~(RAZOR_PROPERTY_RELATION_MASK | RAZOR_PROPERTY_TYPE_MASK)) {
krh@247
   146
			printf(" [");
krh@247
   147
			if (flags & RAZOR_PROPERTY_PRE)
krh@247
   148
				printf(" pre");
krh@247
   149
			if (flags & RAZOR_PROPERTY_POST)
krh@247
   150
				printf(" post");
krh@247
   151
			if (flags & RAZOR_PROPERTY_PREUN)
krh@247
   152
				printf(" preun");
krh@247
   153
			if (flags & RAZOR_PROPERTY_POSTUN)
krh@247
   154
				printf(" postun");
krh@247
   155
			printf(" ]");
krh@247
   156
		}
krh@247
   157
		printf("\n");
rhughes@241
   158
	}
rhughes@241
   159
	razor_property_iterator_destroy(pi);
krh@306
   160
}
rhughes@241
   161
krh@306
   162
static int
krh@306
   163
list_properties(int argc, const char *argv[], uint32_t type)
krh@306
   164
{
krh@306
   165
	struct razor_set *set;
krh@306
   166
	struct razor_package *package;
krh@306
   167
	struct razor_package_iterator *pi;
krh@306
   168
	const char *name, *version, *arch;
krh@306
   169
krh@306
   170
	set = razor_set_open(repo_filename);
krh@306
   171
	pi = create_iterator_from_argv(set, argc, argv);
krh@306
   172
	while (razor_package_iterator_next(pi, &package,
richard@307
   173
					   RAZOR_DETAIL_NAME, &name,
richard@307
   174
					   RAZOR_DETAIL_VERSION, &version,
richard@307
   175
					   RAZOR_DETAIL_ARCH, &arch,
richard@307
   176
					   RAZOR_DETAIL_LAST))
krh@306
   177
		list_package_properties(set, package, type);
krh@306
   178
	razor_package_iterator_destroy(pi);
rhughes@241
   179
	razor_set_destroy(set);
rhughes@241
   180
rhughes@241
   181
	return 0;
rhughes@241
   182
}
rhughes@241
   183
rhughes@241
   184
static int
rhughes@241
   185
command_list_requires(int argc, const char *argv[])
rhughes@241
   186
{
krh@306
   187
	return list_properties(argc, argv, RAZOR_PROPERTY_REQUIRES);
rhughes@241
   188
}
rhughes@241
   189
rhughes@241
   190
static int
rhughes@241
   191
command_list_provides(int argc, const char *argv[])
rhughes@241
   192
{
krh@306
   193
	return list_properties(argc, argv, RAZOR_PROPERTY_PROVIDES);
rhughes@241
   194
}
rhughes@241
   195
rhughes@241
   196
static int
rhughes@241
   197
command_list_obsoletes(int argc, const char *argv[])
rhughes@241
   198
{
krh@306
   199
	return list_properties(argc, argv, RAZOR_PROPERTY_OBSOLETES);
rhughes@241
   200
}
rhughes@241
   201
rhughes@241
   202
static int
rhughes@241
   203
command_list_conflicts(int argc, const char *argv[])
rhughes@241
   204
{
krh@306
   205
	return list_properties(argc, argv, RAZOR_PROPERTY_CONFLICTS);
rhughes@241
   206
}
rhughes@241
   207
rhughes@241
   208
static int
rhughes@241
   209
command_list_files(int argc, const char *argv[])
rhughes@241
   210
{
rhughes@241
   211
	struct razor_set *set;
rhughes@241
   212
rhughes@241
   213
	set = razor_set_open(repo_filename);
rhughes@241
   214
	if (set == NULL)
rhughes@241
   215
		return 1;
richard@310
   216
	if (razor_set_open_files(set, "system-files.rzdb"))
jbowes@288
   217
		return 1;
jbowes@288
   218
rhughes@241
   219
	razor_set_list_files(set, argv[0]);
rhughes@241
   220
	razor_set_destroy(set);
rhughes@241
   221
rhughes@241
   222
	return 0;
rhughes@241
   223
}
rhughes@241
   224
rhughes@241
   225
static int
rhughes@241
   226
command_list_file_packages(int argc, const char *argv[])
rhughes@241
   227
{
rhughes@241
   228
	struct razor_set *set;
rhughes@241
   229
	struct razor_package_iterator *pi;
rhughes@241
   230
rhughes@241
   231
	set = razor_set_open(repo_filename);
rhughes@241
   232
	if (set == NULL)
rhughes@241
   233
		return 1;
richard@310
   234
	if (razor_set_open_files(set, "system-files.rzdb"))
jbowes@288
   235
		return 1;
rhughes@241
   236
rhughes@241
   237
	pi = razor_package_iterator_create_for_file(set, argv[0]);
krh@281
   238
	list_packages(pi, 0);
rhughes@241
   239
	razor_package_iterator_destroy(pi);
rhughes@241
   240
rhughes@241
   241
	razor_set_destroy(set);
rhughes@241
   242
rhughes@241
   243
	return 0;
rhughes@241
   244
}
rhughes@241
   245
rhughes@241
   246
static int
rhughes@241
   247
command_list_package_files(int argc, const char *argv[])
rhughes@241
   248
{
rhughes@241
   249
	struct razor_set *set;
krh@306
   250
	struct razor_package_iterator *pi;
krh@306
   251
	struct razor_package *package;
krh@306
   252
	const char *name, *version, *arch;
rhughes@241
   253
rhughes@241
   254
	set = razor_set_open(repo_filename);
rhughes@241
   255
	if (set == NULL)
rhughes@241
   256
		return 1;
richard@310
   257
	if (razor_set_open_files(set, "system-files.rzdb"))
jbowes@288
   258
		return 1;
jbowes@288
   259
krh@306
   260
	pi = create_iterator_from_argv(set, argc, argv);
krh@306
   261
	while (razor_package_iterator_next(pi, &package,
richard@307
   262
					   RAZOR_DETAIL_NAME, &name,
richard@307
   263
					   RAZOR_DETAIL_VERSION, &version,
richard@307
   264
					   RAZOR_DETAIL_ARCH, &arch,
richard@307
   265
					   RAZOR_DETAIL_LAST))
krh@306
   266
		razor_set_list_package_files(set, package);
krh@306
   267
	razor_package_iterator_destroy(pi);
krh@306
   268
rhughes@241
   269
	razor_set_destroy(set);
rhughes@241
   270
rhughes@241
   271
	return 0;
rhughes@241
   272
}
rhughes@241
   273
rhughes@241
   274
static int
rhughes@241
   275
list_property_packages(const char *ref_name,
rhughes@241
   276
		       const char *ref_version,
krh@247
   277
		       uint32_t type)
rhughes@241
   278
{
rhughes@241
   279
	struct razor_set *set;
rhughes@241
   280
	struct razor_property *property;
krh@281
   281
	struct razor_property_iterator *prop_iter;
krh@281
   282
	struct razor_package_iterator *pkg_iter;
rhughes@241
   283
	const char *name, *version;
krh@247
   284
	uint32_t flags;
rhughes@241
   285
rhughes@241
   286
	if (ref_name == NULL)
rhughes@241
   287
		return 0;
rhughes@241
   288
rhughes@241
   289
	set = razor_set_open(repo_filename);
rhughes@241
   290
	if (set == NULL)
rhughes@241
   291
		return 1;
rhughes@241
   292
krh@281
   293
	prop_iter = razor_property_iterator_create(set, NULL);
krh@281
   294
	while (razor_property_iterator_next(prop_iter, &property,
krh@247
   295
					    &name, &flags, &version)) {
rhughes@241
   296
		if (strcmp(ref_name, name) != 0)
rhughes@241
   297
			continue;
krh@247
   298
		if (ref_version &&
krh@247
   299
		    (flags & RAZOR_PROPERTY_RELATION_MASK) == RAZOR_PROPERTY_EQUAL &&
rhughes@241
   300
		    strcmp(ref_version, version) != 0)
rhughes@241
   301
			continue;
krh@247
   302
		if ((flags & RAZOR_PROPERTY_TYPE_MASK) != type)
rhughes@241
   303
			continue;
rhughes@241
   304
krh@281
   305
		pkg_iter =
krh@281
   306
			razor_package_iterator_create_for_property(set,
krh@281
   307
								   property);
krh@281
   308
		list_packages(pkg_iter, 0);
krh@281
   309
		razor_package_iterator_destroy(pkg_iter);
rhughes@241
   310
	}
krh@281
   311
	razor_property_iterator_destroy(prop_iter);
rhughes@241
   312
rhughes@241
   313
	return 0;
rhughes@241
   314
}
rhughes@241
   315
rhughes@241
   316
static int
rhughes@241
   317
command_what_requires(int argc, const char *argv[])
rhughes@241
   318
{
rhughes@241
   319
	return list_property_packages(argv[0], argv[1],
rhughes@241
   320
				      RAZOR_PROPERTY_REQUIRES);
rhughes@241
   321
}
rhughes@241
   322
rhughes@241
   323
static int
rhughes@241
   324
command_what_provides(int argc, const char *argv[])
rhughes@241
   325
{
rhughes@241
   326
	return list_property_packages(argv[0], argv[1],
rhughes@241
   327
				      RAZOR_PROPERTY_PROVIDES);
rhughes@241
   328
}
rhughes@241
   329
rhughes@241
   330
static int
rhughes@241
   331
show_progress(void *clientp,
rhughes@241
   332
	      double dltotal, double dlnow, double ultotal, double ulnow)
rhughes@241
   333
{
rhughes@241
   334
	const char *file = clientp;
rhughes@241
   335
rhughes@241
   336
	if (!dlnow < dltotal)
rhughes@241
   337
		fprintf(stderr, "\rdownloading %s, %dkB/%dkB",
rhughes@241
   338
			file, (int) dlnow / 1024, (int) dltotal / 1024);
rhughes@241
   339
rhughes@241
   340
	return 0;
rhughes@241
   341
}
rhughes@241
   342
rhughes@241
   343
static int
rhughes@241
   344
download_if_missing(const char *url, const char *file)
rhughes@241
   345
{
rhughes@241
   346
	CURL *curl;
rhughes@241
   347
	struct stat buf;
rhughes@241
   348
	char error[256];
rhughes@241
   349
	FILE *fp;
rhughes@241
   350
	CURLcode res;
rhughes@241
   351
	long response;
rhughes@241
   352
rhughes@241
   353
	curl = curl_easy_init();
rhughes@241
   354
	if (curl == NULL)
rhughes@241
   355
		return 1;
rhughes@241
   356
rhughes@241
   357
	curl_easy_setopt(curl, CURLOPT_ERRORBUFFER, error);
rhughes@241
   358
	curl_easy_setopt(curl, CURLOPT_NOPROGRESS, 0);
rhughes@241
   359
	curl_easy_setopt(curl, CURLOPT_PROGRESSFUNCTION, show_progress);
rhughes@241
   360
	curl_easy_setopt(curl, CURLOPT_PROGRESSDATA, file);
rhughes@241
   361
rhughes@241
   362
	if (stat(file, &buf) < 0) {
rhughes@241
   363
		fp = fopen(file, "w");
rhughes@241
   364
		if (fp == NULL) {
rhughes@241
   365
			fprintf(stderr,
rhughes@241
   366
				"failed to open %s for writing\n", file);
rhughes@241
   367
			return -1;
rhughes@241
   368
		}
rhughes@241
   369
		curl_easy_setopt(curl, CURLOPT_WRITEDATA, fp);
rhughes@241
   370
		curl_easy_setopt(curl, CURLOPT_URL, url);
rhughes@241
   371
		res = curl_easy_perform(curl);
rhughes@241
   372
		fclose(fp);
rhughes@241
   373
		if (res != CURLE_OK) {
rhughes@241
   374
			fprintf(stderr, "curl error: %s\n", error);
rhughes@241
   375
			unlink(file);
rhughes@241
   376
			return -1;
rhughes@241
   377
		}
rhughes@241
   378
		res = curl_easy_getinfo(curl,
rhughes@241
   379
					CURLINFO_RESPONSE_CODE, &response);
rhughes@241
   380
		if (res != CURLE_OK) {
rhughes@241
   381
			fprintf(stderr, "curl error: %s\n", error);
rhughes@241
   382
                        unlink(file);
rhughes@241
   383
                        return -1;
rhughes@241
   384
		}
rhughes@241
   385
		if (response != 200) {
rhughes@241
   386
			fprintf(stderr, " - failed %ld\n", response);
rhughes@241
   387
                        unlink(file);
rhughes@241
   388
                        return -1;
rhughes@241
   389
		}
rhughes@241
   390
		fprintf(stderr, "\n");
rhughes@241
   391
	}
rhughes@241
   392
rhughes@241
   393
	curl_easy_cleanup(curl);
rhughes@241
   394
rhughes@241
   395
	return 0;
rhughes@241
   396
}
rhughes@241
   397
rhughes@241
   398
#define YUM_URL "http://download.fedora.redhat.com" \
rhughes@241
   399
	"/pub/fedora/linux/development/i386/os"
rhughes@241
   400
rhughes@241
   401
static int
rhughes@241
   402
command_import_yum(int argc, const char *argv[])
rhughes@241
   403
{
rhughes@241
   404
	struct razor_set *set;
rhughes@241
   405
	char buffer[512];
rhughes@241
   406
rhughes@241
   407
	printf("downloading from %s.\n", yum_url);
rhughes@241
   408
	snprintf(buffer, sizeof buffer,
rhughes@241
   409
		 "%s/repodata/primary.xml.gz", yum_url);
rhughes@241
   410
	if (download_if_missing(buffer, "primary.xml.gz") < 0)
rhughes@241
   411
		return -1;
rhughes@241
   412
	snprintf(buffer, sizeof buffer,
rhughes@241
   413
		 "%s/repodata/filelists.xml.gz", yum_url);
rhughes@241
   414
	if (download_if_missing(buffer, "filelists.xml.gz") < 0)
rhughes@241
   415
		return -1;
rhughes@241
   416
rhughes@241
   417
	set = razor_set_create_from_yum();
rhughes@241
   418
	if (set == NULL)
rhughes@241
   419
		return 1;
jbowes@258
   420
	razor_set_write(set, rawhide_repo_filename, RAZOR_REPO_FILE_MAIN);
richard@310
   421
	razor_set_write(set, "rawhide-details.rzdb", RAZOR_REPO_FILE_DETAILS);
richard@310
   422
	razor_set_write(set, "rawhide-files.rzdb", RAZOR_REPO_FILE_FILES);
rhughes@241
   423
	razor_set_destroy(set);
rhughes@241
   424
	printf("wrote %s\n", rawhide_repo_filename);
rhughes@241
   425
rhughes@241
   426
	return 0;
rhughes@241
   427
}
rhughes@241
   428
rhughes@241
   429
static int
rhughes@241
   430
command_import_rpmdb(int argc, const char *argv[])
rhughes@241
   431
{
rhughes@241
   432
	struct razor_set *set;
rhughes@241
   433
rhughes@241
   434
	set = razor_set_create_from_rpmdb();
rhughes@241
   435
	if (set == NULL)
rhughes@241
   436
		return 1;
jbowes@258
   437
	razor_set_write(set, repo_filename, RAZOR_REPO_FILE_MAIN);
richard@310
   438
	razor_set_write(set, "system-details.rzdb", RAZOR_REPO_FILE_DETAILS);
richard@310
   439
	razor_set_write(set, "system-files.rzdb", RAZOR_REPO_FILE_FILES);
rhughes@241
   440
	razor_set_destroy(set);
rhughes@241
   441
	printf("wrote %s\n", repo_filename);
rhughes@241
   442
rhughes@241
   443
	return 0;
rhughes@241
   444
}
rhughes@241
   445
rhughes@241
   446
static int
rhughes@241
   447
mark_packages_for_update(struct razor_transaction *trans,
rhughes@241
   448
			 struct razor_set *set, const char *pattern)
rhughes@241
   449
{
rhughes@241
   450
	struct razor_package_iterator *pi;
rhughes@241
   451
	struct razor_package *package;
richard@302
   452
	const char *name;
rhughes@241
   453
	int matches = 0;
rhughes@241
   454
rhughes@241
   455
	pi = razor_package_iterator_create(set);
rhughes@241
   456
	while (razor_package_iterator_next(pi, &package,
richard@307
   457
					   RAZOR_DETAIL_NAME, &name,
richard@307
   458
					   RAZOR_DETAIL_LAST)) {
rhughes@241
   459
		if (pattern && fnmatch(pattern, name, 0) == 0) {
rhughes@241
   460
			razor_transaction_update_package(trans, package);
rhughes@241
   461
			matches++;
rhughes@241
   462
		}
rhughes@241
   463
	}
rhughes@241
   464
	razor_package_iterator_destroy(pi);
rhughes@241
   465
rhughes@241
   466
	return matches;
rhughes@241
   467
}
rhughes@241
   468
rhughes@241
   469
static int
rhughes@241
   470
mark_packages_for_removal(struct razor_transaction *trans,
rhughes@241
   471
			  struct razor_set *set, const char *pattern)
rhughes@241
   472
{
rhughes@241
   473
	struct razor_package_iterator *pi;
rhughes@241
   474
	struct razor_package *package;
richard@302
   475
	const char *name;
rhughes@241
   476
	int matches = 0;
rhughes@241
   477
rhughes@241
   478
	pi = razor_package_iterator_create(set);
richard@307
   479
	while (razor_package_iterator_next(pi, &package,
richard@307
   480
					   RAZOR_DETAIL_NAME, &name,
richard@307
   481
					   RAZOR_DETAIL_LAST)) {
rhughes@241
   482
		if (pattern && fnmatch(pattern, name, 0) == 0) {
rhughes@241
   483
			razor_transaction_remove_package(trans, package);
rhughes@241
   484
			matches++;
rhughes@241
   485
		}
rhughes@241
   486
	}
rhughes@241
   487
	razor_package_iterator_destroy(pi);
rhughes@241
   488
rhughes@241
   489
	return matches;
rhughes@241
   490
}
rhughes@241
   491
rhughes@241
   492
static int
rhughes@241
   493
command_update(int argc, const char *argv[])
rhughes@241
   494
{
rhughes@241
   495
	struct razor_set *set, *upstream;
rhughes@241
   496
	struct razor_transaction *trans;
rhughes@241
   497
	int i, errors;
rhughes@241
   498
rhughes@241
   499
	set = razor_set_open(repo_filename);
rhughes@241
   500
	upstream = razor_set_open(rawhide_repo_filename);
rhughes@241
   501
	if (set == NULL || upstream == NULL)
rhughes@241
   502
		return 1;
rhughes@241
   503
rhughes@241
   504
	trans = razor_transaction_create(set, upstream);
rhughes@241
   505
	if (argc == 0)
rhughes@241
   506
		razor_transaction_update_all(trans);
rhughes@241
   507
	for (i = 0; i < argc; i++) {
rhughes@241
   508
		if (mark_packages_for_update(trans, set, argv[i]) == 0) {
rhughes@241
   509
			fprintf(stderr, "no match for %s\n", argv[i]);
rhughes@241
   510
			return 1;
rhughes@241
   511
		}
rhughes@241
   512
	}
rhughes@241
   513
krh@252
   514
	razor_transaction_resolve(trans);
krh@252
   515
	errors = razor_transaction_describe(trans);
krh@252
   516
	if (errors) {
krh@252
   517
		fprintf(stderr, "unresolved dependencies\n");
rhughes@241
   518
		return 1;
krh@252
   519
	}
rhughes@241
   520
rhughes@241
   521
	set = razor_transaction_finish(trans);
jbowes@258
   522
	razor_set_write(set, updated_repo_filename, RAZOR_REPO_FILE_MAIN);
rhughes@241
   523
	razor_set_destroy(set);
rhughes@241
   524
	razor_set_destroy(upstream);
richard@310
   525
	printf("wrote system-updated.rzdb\n");
rhughes@241
   526
rhughes@241
   527
	return 0;
rhughes@241
   528
}
rhughes@241
   529
rhughes@241
   530
static int
rhughes@241
   531
command_remove(int argc, const char *argv[])
rhughes@241
   532
{
rhughes@241
   533
	struct razor_set *set, *upstream;
rhughes@241
   534
	struct razor_transaction *trans;
rhughes@241
   535
	int i, errors;
rhughes@241
   536
rhughes@241
   537
	set = razor_set_open(repo_filename);
rhughes@241
   538
	if (set == NULL)
rhughes@241
   539
		return 1;
rhughes@241
   540
rhughes@241
   541
	upstream = razor_set_create();
rhughes@241
   542
	trans = razor_transaction_create(set, upstream);
rhughes@241
   543
	for (i = 0; i < argc; i++) {
rhughes@241
   544
		if (mark_packages_for_removal(trans, set, argv[i]) == 0) {
rhughes@241
   545
			fprintf(stderr, "no match for %s\n", argv[i]);
rhughes@241
   546
			return 1;
rhughes@241
   547
		}
rhughes@241
   548
	}
rhughes@241
   549
rhughes@241
   550
	errors = razor_transaction_resolve(trans);
rhughes@241
   551
	if (errors)
rhughes@241
   552
		return 1;
rhughes@241
   553
rhughes@241
   554
	set = razor_transaction_finish(trans);
jbowes@258
   555
	razor_set_write(set, updated_repo_filename, RAZOR_REPO_FILE_MAIN);
rhughes@241
   556
	razor_set_destroy(set);
rhughes@241
   557
	razor_set_destroy(upstream);
richard@310
   558
	printf("wrote system-updated.rzdb\n");
rhughes@241
   559
rhughes@241
   560
	return 0;
rhughes@241
   561
}
rhughes@241
   562
rhughes@241
   563
static void
krh@253
   564
print_diff(enum razor_diff_action action,
krh@253
   565
	   struct razor_package *package,
krh@253
   566
	   const char *name,
krh@253
   567
	   const char *version,
krh@253
   568
	   const char *arch,
rhughes@241
   569
	   void *data)
rhughes@241
   570
{
krh@253
   571
	if (action == RAZOR_DIFF_ACTION_ADD)
krh@253
   572
		printf("install %s-%s.%s\n", name, version, arch);
krh@253
   573
	if (action == RAZOR_DIFF_ACTION_REMOVE)
krh@253
   574
		printf("remove %s-%s.%s\n", name, version, arch);
rhughes@241
   575
}
rhughes@241
   576
rhughes@241
   577
static int
rhughes@241
   578
command_diff(int argc, const char *argv[])
rhughes@241
   579
{
rhughes@241
   580
	struct razor_set *set, *updated;
rhughes@241
   581
rhughes@241
   582
	set = razor_set_open(repo_filename);
rhughes@241
   583
	updated = razor_set_open(updated_repo_filename);
rhughes@241
   584
	if (set == NULL || updated == NULL)
rhughes@241
   585
		return 1;
rhughes@241
   586
rhughes@241
   587
	razor_set_diff(set, updated, print_diff, NULL);
rhughes@241
   588
rhughes@241
   589
	razor_set_destroy(set);
rhughes@241
   590
	razor_set_destroy(updated);
rhughes@241
   591
rhughes@241
   592
	return 0;
rhughes@241
   593
}
rhughes@241
   594
rhughes@241
   595
static int
rhughes@241
   596
command_import_rpms(int argc, const char *argv[])
rhughes@241
   597
{
rhughes@241
   598
	DIR *dir;
rhughes@241
   599
	struct dirent *de;
rhughes@241
   600
	struct razor_importer *importer;
rhughes@241
   601
	struct razor_set *set;
rhughes@241
   602
	struct razor_rpm *rpm;
jbowes@263
   603
	int len, imported_count = 0;
rhughes@241
   604
	char filename[256];
rhughes@241
   605
	const char *dirname = argv[0];
rhughes@241
   606
rhughes@241
   607
	if (dirname == NULL) {
rhughes@241
   608
		fprintf(stderr, "usage: razor import-rpms DIR\n");
rhughes@241
   609
		return -1;
rhughes@241
   610
	}
rhughes@241
   611
rhughes@241
   612
	dir = opendir(dirname);
rhughes@241
   613
	if (dir == NULL) {
rhughes@241
   614
		fprintf(stderr, "couldn't read dir %s\n", dirname);
rhughes@241
   615
		return -1;
rhughes@241
   616
	}
rhughes@241
   617
krh@249
   618
	importer = razor_importer_create();
rhughes@241
   619
rhughes@241
   620
	while (de = readdir(dir), de != NULL) {
rhughes@241
   621
		len = strlen(de->d_name);
rhughes@241
   622
		if (len < 5 || strcmp(de->d_name + len - 4, ".rpm") != 0)
rhughes@241
   623
		    continue;
rhughes@241
   624
		snprintf(filename, sizeof filename,
rhughes@241
   625
			 "%s/%s", dirname, de->d_name);
rhughes@241
   626
		rpm = razor_rpm_open(filename);
rhughes@241
   627
		if (rpm == NULL) {
rhughes@241
   628
			fprintf(stderr,
rhughes@241
   629
				"failed to open rpm \"%s\"\n", filename);
rhughes@241
   630
			continue;
rhughes@241
   631
		}
rhughes@241
   632
		if (razor_importer_add_rpm(importer, rpm)) {
rhughes@241
   633
			fprintf(stderr, "couldn't import %s\n", filename);
rhughes@241
   634
			break;
rhughes@241
   635
		}
rhughes@241
   636
		razor_rpm_close(rpm);
jbowes@263
   637
jbowes@263
   638
		printf("\rimporting %d", ++imported_count);
jbowes@263
   639
		fflush(stdout);
rhughes@241
   640
	}
rhughes@241
   641
rhughes@241
   642
	if (de != NULL) {
rhughes@241
   643
		razor_importer_destroy(importer);
rhughes@241
   644
		return -1;
rhughes@241
   645
	}
rhughes@241
   646
jbowes@263
   647
	printf("\nsaving\n");
rhughes@241
   648
	set = razor_importer_finish(importer);
rhughes@241
   649
jbowes@258
   650
	razor_set_write(set, repo_filename, RAZOR_REPO_FILE_MAIN);
richard@310
   651
	razor_set_write(set, "system-details.rzdb", RAZOR_REPO_FILE_DETAILS);
richard@310
   652
	razor_set_write(set, "system-files.rzdb", RAZOR_REPO_FILE_FILES);
rhughes@241
   653
	razor_set_destroy(set);
rhughes@241
   654
	printf("wrote %s\n", repo_filename);
rhughes@241
   655
rhughes@241
   656
	return 0;
rhughes@241
   657
}
rhughes@241
   658
krh@254
   659
static const char *
krh@254
   660
rpm_filename(const char *name, const char *version, const char *arch)
rhughes@241
   661
{
krh@254
   662
	static char file[PATH_MAX];
krh@254
   663
 	const char *v;
krh@254
   664
 
krh@254
   665
 	/* Skip epoch */
krh@253
   666
	v = strchr(version, ':');
krh@254
   667
 	if (v != NULL)
krh@254
   668
 		v = v + 1;
krh@254
   669
 	else
krh@253
   670
		v = version;
rhughes@241
   671
krh@254
   672
	snprintf(file, sizeof file, "%s-%s.%s.rpm", name, v, arch);
rhughes@241
   673
krh@254
   674
	return file;
rhughes@241
   675
}
rhughes@241
   676
krh@254
   677
static int
krh@254
   678
download_packages(struct razor_set *system, struct razor_set *next)
rhughes@241
   679
{
krh@254
   680
	struct razor_package_iterator *pi;
krh@254
   681
	struct razor_package *package;
krh@254
   682
	const char *name, *version, *arch;
krh@254
   683
	char file[PATH_MAX], url[256];
krh@254
   684
	int errors;
krh@254
   685
 
krh@254
   686
	pi = razor_set_create_install_iterator(system, next);
krh@254
   687
	errors = 0;
krh@254
   688
	while (razor_package_iterator_next(pi, &package,
richard@302
   689
					   RAZOR_DETAIL_NAME, &name,
richard@302
   690
					   RAZOR_DETAIL_VERSION, &version,
richard@307
   691
					   RAZOR_DETAIL_ARCH, &arch,
richard@307
   692
					   RAZOR_DETAIL_LAST)) {
krh@254
   693
		snprintf(url, sizeof url,
krh@254
   694
			 "%s/Packages/%s",
krh@254
   695
			 yum_url, rpm_filename(name, version, arch));
krh@254
   696
		snprintf(file, sizeof file,
krh@254
   697
			 "rpms/%s", rpm_filename(name, version, arch));
krh@254
   698
		if (download_if_missing(url, file) < 0)
krh@254
   699
			errors++;
krh@254
   700
	}
krh@254
   701
	razor_package_iterator_destroy(pi);
krh@254
   702
krh@254
   703
	if (errors > 0) {
krh@254
   704
		fprintf(stderr, "failed to download %d packages\n", errors);
krh@254
   705
                return -1;
krh@254
   706
        }
krh@254
   707
krh@254
   708
	return 0;
krh@254
   709
}
krh@254
   710
krh@254
   711
static int
krh@254
   712
install_packages(struct razor_set *system, struct razor_set *next)
krh@254
   713
{
krh@254
   714
	struct razor_package_iterator *pi;
krh@254
   715
	struct razor_package *package;
krh@254
   716
	struct razor_rpm *rpm;
krh@254
   717
	const char *name, *version, *arch;
rhughes@241
   718
	char file[PATH_MAX];
rhughes@241
   719
krh@254
   720
	pi = razor_set_create_install_iterator(system, next);
krh@254
   721
	while (razor_package_iterator_next(pi, &package,
richard@302
   722
					   RAZOR_DETAIL_NAME, &name,
richard@302
   723
					   RAZOR_DETAIL_VERSION, &version,
richard@307
   724
					   RAZOR_DETAIL_ARCH, &arch,
richard@307
   725
					   RAZOR_DETAIL_LAST)) {
krh@254
   726
		printf("install %s-%s\n", name, version);
krh@254
   727
krh@254
   728
		snprintf(file, sizeof file,
krh@254
   729
			 "rpms/%s", rpm_filename(name, version, arch));
krh@254
   730
		rpm = razor_rpm_open(file);
krh@254
   731
		if (rpm == NULL) {
krh@254
   732
			fprintf(stderr, "failed to open rpm %s\n", file);
krh@254
   733
			return -1;
krh@254
   734
		}
krh@254
   735
		if (razor_rpm_install(rpm, install_root) < 0) {
krh@254
   736
			fprintf(stderr,
krh@254
   737
				"failed to install rpm %s\n", file);
krh@254
   738
			return -1;
krh@254
   739
		}
krh@254
   740
		razor_rpm_close(rpm);
rhughes@241
   741
	}
krh@254
   742
	razor_package_iterator_destroy(pi);
rhughes@241
   743
krh@254
   744
	return 0;
rhughes@241
   745
}
rhughes@241
   746
rhughes@241
   747
static int
rhughes@241
   748
command_install(int argc, const char *argv[])
rhughes@241
   749
{
rhughes@241
   750
	struct razor_root *root;
krh@250
   751
	struct razor_set *system, *upstream, *next;
rhughes@241
   752
	struct razor_transaction *trans;
krh@281
   753
	int i = 0, dependencies = 1;
rhughes@241
   754
rhughes@241
   755
	if (i < argc && strcmp(argv[i], "--no-dependencies") == 0) {
rhughes@241
   756
		dependencies = 0;
rhughes@241
   757
		i++;
rhughes@241
   758
	}
rhughes@241
   759
krh@250
   760
	root = razor_root_open(install_root);
krh@255
   761
	if (root == NULL)
krh@255
   762
		return 1;
krh@255
   763
krh@250
   764
	system = razor_root_get_system_set(root);
rhughes@241
   765
	upstream = razor_set_open(rawhide_repo_filename);
krh@250
   766
	trans = razor_transaction_create(system, upstream);
rhughes@241
   767
rhughes@241
   768
	for (; i < argc; i++) {
rhughes@241
   769
		if (mark_packages_for_update(trans, upstream, argv[i]) == 0) {
rhughes@241
   770
			fprintf(stderr, "no package matched %s\n", argv[i]);
rhughes@241
   771
			razor_root_close(root);
rhughes@241
   772
			return 1;
rhughes@241
   773
		}
rhughes@241
   774
	}
rhughes@241
   775
rhughes@241
   776
	if (dependencies) {
krh@245
   777
		razor_transaction_resolve(trans);
krh@245
   778
		if (razor_transaction_describe(trans) > 0) {
rhughes@241
   779
			razor_root_close(root);
rhughes@241
   780
			return 1;
rhughes@241
   781
		}
rhughes@241
   782
	}
rhughes@241
   783
rhughes@241
   784
	next = razor_transaction_finish(trans);
rhughes@241
   785
rhughes@241
   786
	razor_root_update(root, next);
rhughes@241
   787
rhughes@241
   788
	if (mkdir("rpms", 0777) && errno != EEXIST) {
rhughes@241
   789
		fprintf(stderr, "failed to create rpms directory.\n");
rhughes@241
   790
		razor_root_close(root);
rhughes@241
   791
		return 1;
rhughes@241
   792
	}
rhughes@241
   793
krh@254
   794
	if (download_packages(system, next) < 0) {
rhughes@241
   795
		razor_root_close(root);
rhughes@241
   796
                return 1;
rhughes@241
   797
        }
rhughes@241
   798
krh@254
   799
	install_packages(system, next);
rhughes@241
   800
rhughes@241
   801
	razor_set_destroy(next);
rhughes@241
   802
	razor_set_destroy(upstream);
rhughes@241
   803
rhughes@241
   804
	return razor_root_commit(root);
rhughes@241
   805
}
rhughes@241
   806
rhughes@241
   807
static int
rhughes@241
   808
command_init(int argc, const char *argv[])
rhughes@241
   809
{
rhughes@241
   810
	return razor_root_create(install_root);
rhughes@241
   811
}
rhughes@241
   812
rhughes@241
   813
static int
rhughes@241
   814
command_download(int argc, const char *argv[])
rhughes@241
   815
{
rhughes@241
   816
	struct razor_set *set;
rhughes@241
   817
	struct razor_package_iterator *pi;
rhughes@241
   818
	struct razor_package *package;
rhughes@241
   819
	const char *pattern = argv[0], *name, *version, *arch;
rhughes@241
   820
	char url[256], file[256];
rhughes@241
   821
	int matches = 0;
rhughes@241
   822
rhughes@241
   823
	if (mkdir("rpms", 0777) && errno != EEXIST) {
rhughes@241
   824
		fprintf(stderr, "failed to create rpms directory.\n");
rhughes@241
   825
		return 1;
rhughes@241
   826
	}
rhughes@241
   827
rhughes@241
   828
	set = razor_set_open(rawhide_repo_filename);
rhughes@241
   829
	pi = razor_package_iterator_create(set);
rhughes@241
   830
	while (razor_package_iterator_next(pi, &package,
richard@302
   831
					   RAZOR_DETAIL_NAME, &name,
richard@302
   832
					   RAZOR_DETAIL_VERSION, &version,
richard@307
   833
					   RAZOR_DETAIL_ARCH, &arch,
richard@307
   834
					   RAZOR_DETAIL_LAST)) {
rhughes@241
   835
		if (pattern && fnmatch(pattern, name, 0) != 0)
rhughes@241
   836
			continue;
rhughes@241
   837
rhughes@241
   838
		matches++;
rhughes@241
   839
		snprintf(url, sizeof url,
rhughes@241
   840
			 "%s/Packages/%s-%s.%s.rpm",
rhughes@241
   841
			 yum_url, name, version, arch);
rhughes@241
   842
		snprintf(file, sizeof file,
rhughes@241
   843
			 "rpms/%s-%s.%s.rpm", name, version, arch);
rhughes@241
   844
		download_if_missing(url, file);
rhughes@241
   845
	}
rhughes@241
   846
	razor_package_iterator_destroy(pi);
rhughes@241
   847
	razor_set_destroy(set);
rhughes@241
   848
rhughes@241
   849
	if (matches == 0)
rhughes@241
   850
		fprintf(stderr, "no packages matched \"%s\"\n", pattern);
rhughes@241
   851
	else if (matches == 1)
rhughes@241
   852
		fprintf(stderr, "downloaded 1 package\n");
rhughes@241
   853
	else
rhughes@241
   854
		fprintf(stderr, "downloaded %d packages\n", matches);
rhughes@241
   855
rhughes@241
   856
	return 0;
rhughes@241
   857
}
rhughes@241
   858
jbowes@258
   859
static int
jbowes@258
   860
command_info(int argc, const char *argv[])
jbowes@258
   861
{
jbowes@258
   862
	struct razor_set *set;
jbowes@258
   863
	struct razor_package_iterator *pi;
jbowes@258
   864
	struct razor_package *package;
jbowes@258
   865
	const char *pattern = argv[0], *name, *version, *arch;
jbowes@258
   866
	const char *summary, *description, *url, *license;
jbowes@258
   867
jbowes@258
   868
	set = razor_set_open(repo_filename);
jbowes@288
   869
	if (set == NULL)
jbowes@288
   870
		return 1;
richard@310
   871
	if (razor_set_open_details(set, "system-details.rzdb"))
jbowes@288
   872
		return 1;
jbowes@258
   873
	pi = razor_package_iterator_create(set);
jbowes@258
   874
	while (razor_package_iterator_next(pi, &package,
richard@302
   875
					   RAZOR_DETAIL_NAME, &name,
richard@302
   876
					   RAZOR_DETAIL_VERSION, &version,
richard@307
   877
					   RAZOR_DETAIL_ARCH, &arch,
richard@307
   878
					   RAZOR_DETAIL_LAST)) {
jbowes@258
   879
		if (pattern && fnmatch(pattern, name, 0) != 0)
jbowes@258
   880
			continue;
jbowes@258
   881
richard@302
   882
		razor_package_get_details (set, package,
richard@302
   883
					   RAZOR_DETAIL_SUMMARY, &summary,
richard@302
   884
					   RAZOR_DETAIL_DESCRIPTION, &description,
richard@302
   885
					   RAZOR_DETAIL_URL, &url,
richard@302
   886
					   RAZOR_DETAIL_LICENSE, &license,
richard@307
   887
					   RAZOR_DETAIL_LAST);
jbowes@258
   888
jbowes@258
   889
		printf ("Name:        %s\n", name);
jbowes@258
   890
		printf ("Arch:        %s\n", arch);
jbowes@258
   891
		printf ("Version:     %s\n", version);
jbowes@258
   892
		printf ("URL:         %s\n", url);
jbowes@258
   893
		printf ("License:     %s\n", license);
jbowes@258
   894
		printf ("Summary:     %s\n", summary);
jbowes@258
   895
		printf ("Description:\n");
jbowes@258
   896
		printf ("%s\n", description);
jbowes@258
   897
		printf ("\n");
jbowes@258
   898
	}
jbowes@258
   899
	razor_package_iterator_destroy(pi);
jbowes@258
   900
	razor_set_destroy(set);
jbowes@258
   901
jbowes@258
   902
	return 0;
jbowes@258
   903
}
jbowes@258
   904
jbowes@292
   905
#define SEARCH_MAX 256
jbowes@292
   906
jbowes@292
   907
static int
jbowes@292
   908
command_search(int argc, const char *argv[])
jbowes@292
   909
{
jbowes@292
   910
	struct razor_set *set;
jbowes@292
   911
	struct razor_package_iterator *pi;
jbowes@292
   912
	struct razor_package *package;
jbowes@292
   913
	char pattern[SEARCH_MAX];
jbowes@292
   914
	const char *name, *version, *arch;
jbowes@292
   915
	const char *summary, *description, *url, *license;
jbowes@292
   916
jbowes@292
   917
	if (!argv[0]) {
jbowes@292
   918
		fprintf(stderr, "must specify a search term\n");
jbowes@292
   919
		return 1;
jbowes@292
   920
	}
jbowes@292
   921
krh@294
   922
	snprintf(pattern, sizeof pattern, "*%s*", argv[0]);
jbowes@292
   923
krh@305
   924
	set = razor_set_open(rawhide_repo_filename);
jbowes@292
   925
	if (set == NULL)
jbowes@292
   926
		return 1;
richard@310
   927
richard@310
   928
	if (razor_set_open_details(set, "rawhide-details.rzdb"))
jbowes@292
   929
		return 1;
jbowes@292
   930
jbowes@292
   931
	pi = razor_package_iterator_create(set);
jbowes@292
   932
	while (razor_package_iterator_next(pi, &package,
richard@304
   933
					   RAZOR_DETAIL_NAME, &name,
richard@304
   934
					   RAZOR_DETAIL_VERSION, &version,
krh@305
   935
					   RAZOR_DETAIL_ARCH, &arch,
krh@305
   936
					   RAZOR_DETAIL_SUMMARY, &summary,
krh@305
   937
					   RAZOR_DETAIL_DESCRIPTION, &description,
krh@305
   938
					   RAZOR_DETAIL_URL, &url,
krh@305
   939
					   RAZOR_DETAIL_LICENSE, &license,
richard@307
   940
					   RAZOR_DETAIL_LAST)) {
krh@305
   941
		if (!fnmatch(pattern, name, FNM_CASEFOLD) ||
krh@305
   942
		    !fnmatch(pattern, url, FNM_CASEFOLD) ||
krh@305
   943
		    !fnmatch(pattern, summary, FNM_CASEFOLD) ||
krh@305
   944
		    !fnmatch(pattern, description, FNM_CASEFOLD))
krh@305
   945
			printf("%s-%s.%s: %s\n", name, version, arch, summary);
jbowes@292
   946
	}
jbowes@292
   947
	razor_package_iterator_destroy(pi);
jbowes@292
   948
	razor_set_destroy(set);
jbowes@292
   949
jbowes@292
   950
	return 0;
jbowes@292
   951
}
jbowes@292
   952
rhughes@241
   953
static struct {
rhughes@241
   954
	const char *name;
rhughes@241
   955
	const char *description;
rhughes@241
   956
	int (*func)(int argc, const char *argv[]);
rhughes@241
   957
} razor_commands[] = {
rhughes@241
   958
	{ "list", "list all packages", command_list },
rhughes@241
   959
	{ "list-requires", "list all requires for the given package", command_list_requires },
rhughes@241
   960
	{ "list-provides", "list all provides for the given package", command_list_provides },
rhughes@241
   961
	{ "list-obsoletes", "list all obsoletes for the given package", command_list_obsoletes },
rhughes@241
   962
	{ "list-conflicts", "list all conflicts for the given package", command_list_conflicts },
rhughes@241
   963
	{ "list-files", "list files for package set", command_list_files },
rhughes@241
   964
	{ "list-file-packages", "list packages owning file", command_list_file_packages },
rhughes@241
   965
	{ "list-package-files", "list files in package", command_list_package_files },
rhughes@241
   966
	{ "what-requires", "list the packages that have the given requires", command_what_requires },
rhughes@241
   967
	{ "what-provides", "list the packages that have the given provides", command_what_provides },
rhughes@241
   968
	{ "import-yum", "import yum metadata files", command_import_yum },
rhughes@241
   969
	{ "import-rpmdb", "import the system rpm database", command_import_rpmdb },
rhughes@241
   970
	{ "import-rpms", "import rpms from the given directory", command_import_rpms },
rhughes@241
   971
	{ "update", "update all or specified packages", command_update },
rhughes@241
   972
	{ "remove", "remove specified packages", command_remove },
rhughes@241
   973
	{ "diff", "show diff between two package sets", command_diff },
rhughes@241
   974
	{ "install", "install rpm", command_install },
rhughes@241
   975
	{ "init", "init razor root", command_init },
jbowes@258
   976
	{ "download", "download packages", command_download },
jbowes@292
   977
	{ "info", "display package details", command_info },
jbowes@292
   978
	{ "search", "search package details", command_search }
rhughes@241
   979
};
rhughes@241
   980
rhughes@241
   981
static int
rhughes@241
   982
usage(void)
rhughes@241
   983
{
rhughes@241
   984
	int i;
rhughes@241
   985
rhughes@241
   986
	printf("usage:\n");
rhughes@241
   987
	for (i = 0; i < ARRAY_SIZE(razor_commands); i++)
rhughes@241
   988
		printf("  %-20s%s\n",
rhughes@241
   989
		       razor_commands[i].name, razor_commands[i].description);
rhughes@241
   990
rhughes@241
   991
	return 1;
rhughes@241
   992
}
rhughes@241
   993
rhughes@241
   994
int
rhughes@241
   995
main(int argc, const char *argv[])
rhughes@241
   996
{
rhughes@241
   997
	char *repo;
rhughes@241
   998
	int i;
rhughes@241
   999
rhughes@241
  1000
	repo = getenv("RAZOR_REPO");
rhughes@241
  1001
	if (repo != NULL)
rhughes@241
  1002
		repo_filename = repo;
rhughes@241
  1003
rhughes@241
  1004
	yum_url = getenv("YUM_URL");
rhughes@241
  1005
	if (yum_url == NULL)
rhughes@241
  1006
		yum_url = YUM_URL;
rhughes@241
  1007
rhughes@241
  1008
	if (argc < 2)
rhughes@241
  1009
		return usage();
rhughes@241
  1010
rhughes@241
  1011
	for (i = 0; i < ARRAY_SIZE(razor_commands); i++)
rhughes@241
  1012
		if (strcmp(razor_commands[i].name, argv[1]) == 0)
rhughes@241
  1013
			return razor_commands[i].func(argc - 2, argv + 2);
rhughes@241
  1014
rhughes@241
  1015
	return usage();
rhughes@241
  1016
}