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