src/main.c
author J. Ali Harlow <ali@juiblex.co.uk>
Tue Jan 06 14:06:00 2009 +0000 (2009-01-06)
changeset 320 53e1185e2366
parent 317 019a53b65271
child 321 6f804141872c
permissions -rw-r--r--
Make rpmlib an optional dependency

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