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