src/main.c
author Kristian H?gsberg <krh@redhat.com>
Mon Jun 30 13:28:59 2008 -0400 (2008-06-30)
changeset 306 cd3954499086
parent 305 e10b4f060a9d
child 307 95b6bcadd6c4
permissions -rw-r--r--
Get rid of razor_set_get_package().

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