src/main.c
author J. Ali Harlow <ali@juiblex.co.uk>
Thu Feb 16 17:33:47 2012 +0000 (2012-02-16)
changeset 424 8cbc438cc298
parent 421 408c66ad463d
child 425 0c8bdd8dc942
permissions -rw-r--r--
Allow multiple atomic transactions to be used with one root object.
This allows transactions that include barriers to be performed
while holding an exclusive system lock.
     1 /*
     2  * Copyright (C) 2008  Kristian Høgsberg <krh@redhat.com>
     3  * Copyright (C) 2008  Red Hat, Inc
     4  * Copyright (C) 2009, 2011-2012  J. Ali Harlow <ali@juiblex.co.uk>
     5  *
     6  * This program is free software; you can redistribute it and/or modify
     7  * it under the terms of the GNU General Public License as published by
     8  * the Free Software Foundation; either version 2 of the License, or
     9  * (at your option) any later version.
    10  *
    11  * This program is distributed in the hope that it will be useful,
    12  * but WITHOUT ANY WARRANTY; without even the implied warranty of
    13  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
    14  * GNU General Public License for more details.
    15  *
    16  * You should have received a copy of the GNU General Public License along
    17  * with this program; if not, write to the Free Software Foundation, Inc.,
    18  * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
    19  */
    20 
    21 #define _GNU_SOURCE
    22 
    23 #include "config.h"
    24 
    25 #include <stdlib.h>
    26 #include <stddef.h>
    27 #include <stdio.h>
    28 #include <stdint.h>
    29 #include <string.h>
    30 #include <sys/stat.h>
    31 #include <unistd.h>
    32 #include <fcntl.h>
    33 #include <dirent.h>
    34 #include <limits.h>
    35 #ifdef HAVE_CURL
    36 #include <curl/curl.h>
    37 #endif
    38 #include <fnmatch.h>
    39 #include <errno.h>
    40 #include "razor.h"
    41 
    42 static const char system_repo_filename[] = "system.rzdb";
    43 static const char next_repo_filename[] = "system-next.rzdb";
    44 static const char rawhide_repo_filename[] = "rawhide.rzdb";
    45 static const char *install_root = "";
    46 static const char *repo_filename = system_repo_filename;
    47 static const char *yum_url;
    48 
    49 #define ARRAY_SIZE(a) (sizeof(a) / sizeof((a)[0]))
    50 
    51 static int
    52 update_packages(struct razor_transaction *trans,
    53 		struct razor_install_iterator *ii, struct razor_set *system,
    54 		struct razor_set *next, struct razor_atomic *atomic,
    55 		struct razor_relocations *relocations,
    56 		enum razor_stage_type stage);
    57 static int
    58 update_system(struct razor_root *root, struct razor_relocations *relocations,
    59 	      struct razor_transaction *trans, struct razor_set *next,
    60 	      const char *verb);
    61 
    62 static struct razor_package_iterator *
    63 create_iterator_from_argv(struct razor_set *set, int argc, const char *argv[])
    64 {
    65 	struct razor_package_query *query;
    66 	struct razor_package_iterator *iter;
    67 	struct razor_package *package;
    68 	const char *name, *pattern;
    69 	int i, count;
    70 
    71 	if (argc == 0)
    72 		return razor_package_iterator_create(set);
    73 
    74 	query = razor_package_query_create(set);
    75 
    76 	for (i = 0; i < argc; i++) {
    77 		iter = razor_package_iterator_create(set);
    78 		pattern = argv[i];
    79 		count = 0;
    80 		while (razor_package_iterator_next(iter, &package,
    81 						   RAZOR_DETAIL_NAME, &name,
    82 						   RAZOR_DETAIL_LAST)) {
    83 			if (fnmatch(pattern, name, 0) != 0)
    84 				continue;
    85 
    86 			razor_package_query_add_package(query, package);
    87 			count++;
    88 		}
    89 		razor_package_iterator_destroy(iter);
    90 
    91 		if (count == 0)
    92 			fprintf(stderr,
    93 				"no package matches \"%s\"\n", pattern);
    94 	}
    95 
    96 	return razor_package_query_finish(query);
    97 }
    98 
    99 #define LIST_PACKAGES_ONLY_NAMES 0x01
   100 
   101 static void
   102 list_packages(struct razor_package_iterator *iter, uint32_t flags)
   103 {
   104 	struct razor_package *package;
   105 	const char *name, *version, *arch;
   106 
   107 	while (razor_package_iterator_next(iter, &package,
   108 					   RAZOR_DETAIL_NAME, &name,
   109 					   RAZOR_DETAIL_VERSION, &version,
   110 					   RAZOR_DETAIL_ARCH, &arch,
   111 					   RAZOR_DETAIL_LAST)) {
   112 		if (flags & LIST_PACKAGES_ONLY_NAMES)
   113 			printf("%s\n", name);
   114 		else
   115 			printf("%s-%s.%s\n", name, version, arch);
   116 	}
   117 }
   118 
   119 static int
   120 command_list(int argc, const char *argv[])
   121 {
   122 	struct razor_package_iterator *pi;
   123 	struct razor_error *error = NULL;
   124 	struct razor_set *set;
   125 	uint32_t flags = 0;
   126 	int i = 0;
   127 
   128 	if (i < argc && strcmp(argv[i], "--only-names") == 0) {
   129 		flags |= LIST_PACKAGES_ONLY_NAMES;
   130 		i++;
   131 	}
   132 
   133 	set = razor_root_open_read_only(install_root, &error);
   134 	if (set == NULL) {
   135 		fprintf(stderr, "%s\n", razor_error_get_msg(error));
   136 		razor_error_free(error);
   137 		return 1;
   138 	}
   139 
   140 	pi = create_iterator_from_argv(set, argc - i, argv + i);
   141 	list_packages(pi, flags);
   142 	razor_package_iterator_destroy(pi);
   143 	razor_set_unref(set);
   144 
   145 	return 0;
   146 }
   147 
   148 static void
   149 list_package_properties(struct razor_set *set,
   150 			struct razor_package *package, uint32_t type)
   151 {
   152 	struct razor_property_iterator *pi;
   153 	struct razor_property *property;
   154 	const char *name, *version;
   155 	uint32_t flags;
   156 
   157 	pi = razor_property_iterator_create(set, package);
   158 	while (razor_property_iterator_next(pi, &property,
   159 					    &name, &flags, &version)) {
   160 		if ((flags & RAZOR_PROPERTY_TYPE_MASK) != type)
   161 			continue;
   162 		printf("%s", name);
   163 		if (version[0] != '\0')
   164 			printf(" %s %s",
   165 			       razor_property_relation_to_string(property),
   166 			       version);
   167 
   168 		if (flags & ~(RAZOR_PROPERTY_RELATION_MASK | RAZOR_PROPERTY_TYPE_MASK)) {
   169 			printf(" [");
   170 			if (flags & RAZOR_PROPERTY_PRE)
   171 				printf(" pre");
   172 			if (flags & RAZOR_PROPERTY_POST)
   173 				printf(" post");
   174 			if (flags & RAZOR_PROPERTY_PREUN)
   175 				printf(" preun");
   176 			if (flags & RAZOR_PROPERTY_POSTUN)
   177 				printf(" postun");
   178 			printf(" ]");
   179 		}
   180 		printf("\n");
   181 	}
   182 	razor_property_iterator_destroy(pi);
   183 }
   184 
   185 static int
   186 list_properties(int argc, const char *argv[], uint32_t type)
   187 {
   188 	struct razor_set *set;
   189 	struct razor_error *error = NULL;
   190 	struct razor_package *package;
   191 	struct razor_package_iterator *pi;
   192 	const char *name, *version, *arch;
   193 
   194 	set = razor_root_open_read_only(install_root, &error);
   195 	if (set == NULL) {
   196 		fprintf(stderr, "%s\n", razor_error_get_msg(error));
   197 		razor_error_free(error);
   198 		return 1;
   199 	}
   200 
   201 	pi = create_iterator_from_argv(set, argc, argv);
   202 	while (razor_package_iterator_next(pi, &package,
   203 					   RAZOR_DETAIL_NAME, &name,
   204 					   RAZOR_DETAIL_VERSION, &version,
   205 					   RAZOR_DETAIL_ARCH, &arch,
   206 					   RAZOR_DETAIL_LAST))
   207 		list_package_properties(set, package, type);
   208 	razor_package_iterator_destroy(pi);
   209 	razor_set_unref(set);
   210 
   211 	return 0;
   212 }
   213 
   214 static int
   215 command_list_requires(int argc, const char *argv[])
   216 {
   217 	return list_properties(argc, argv, RAZOR_PROPERTY_REQUIRES);
   218 }
   219 
   220 static int
   221 command_list_provides(int argc, const char *argv[])
   222 {
   223 	return list_properties(argc, argv, RAZOR_PROPERTY_PROVIDES);
   224 }
   225 
   226 static int
   227 command_list_obsoletes(int argc, const char *argv[])
   228 {
   229 	return list_properties(argc, argv, RAZOR_PROPERTY_OBSOLETES);
   230 }
   231 
   232 static int
   233 command_list_conflicts(int argc, const char *argv[])
   234 {
   235 	return list_properties(argc, argv, RAZOR_PROPERTY_CONFLICTS);
   236 }
   237 
   238 static int
   239 command_list_scripts(int argc, const char *argv[])
   240 {
   241 	struct razor_set *set;
   242 	struct razor_error *error = NULL;
   243 	struct razor_package *package;
   244 	struct razor_package_iterator *pi;
   245 	const char *preunprog, *preun, *postunprog, *postun;
   246 
   247 	set = razor_root_open_read_only(install_root, &error);
   248 	if (set == NULL) {
   249 		fprintf(stderr, "%s\n", razor_error_get_msg(error));
   250 		razor_error_free(error);
   251 		return 1;
   252 	}
   253 
   254 	pi = create_iterator_from_argv(set, argc, argv);
   255 	while (razor_package_iterator_next(pi, &package,
   256 					   RAZOR_DETAIL_PREUNPROG, &preunprog,
   257 					   RAZOR_DETAIL_PREUN, &preun,
   258 					   RAZOR_DETAIL_POSTUNPROG, &postunprog,
   259 					   RAZOR_DETAIL_POSTUN, &postun,
   260 					   RAZOR_DETAIL_LAST)) {
   261 		if (preun && *preun) {
   262 			printf("preuninstall scriptlet");
   263 			if (preunprog && *preunprog)
   264 				printf(" (using %s)",preunprog);
   265 			printf(":\n%s\n",preun);
   266 		}
   267 		if (postun && *postun) {
   268 			printf("postuninstall scriptlet");
   269 			if (postunprog && *postunprog)
   270 				printf(" (using %s)",postunprog);
   271 			printf(":\n%s\n",postun);
   272 		}
   273 	}
   274 	razor_package_iterator_destroy(pi);
   275 	razor_set_unref(set);
   276 
   277 	return 0;
   278 }
   279 
   280 static int
   281 command_list_files(int argc, const char *argv[])
   282 {
   283 	struct razor_error *error = NULL;
   284 	struct razor_set *set;
   285 
   286 	set = razor_root_open_read_only(install_root, &error);
   287 	if (set == NULL) {
   288 		fprintf(stderr, "%s\n", razor_error_get_msg(error));
   289 		razor_error_free(error);
   290 		return 1;
   291 	}
   292 
   293 	razor_set_list_files(set, argv[0]);
   294 	razor_set_unref(set);
   295 
   296 	return 0;
   297 }
   298 
   299 static int
   300 command_list_file_packages(int argc, const char *argv[])
   301 {
   302 	struct razor_error *error = NULL;
   303 	struct razor_set *set;
   304 	struct razor_package_iterator *pi;
   305 
   306 	set = razor_root_open_read_only(install_root, &error);
   307 	if (set == NULL) {
   308 		fprintf(stderr, "%s\n", razor_error_get_msg(error));
   309 		razor_error_free(error);
   310 		return 1;
   311 	}
   312 
   313 	pi = razor_package_iterator_create_for_file(set, argv[0]);
   314 	list_packages(pi, 0);
   315 	razor_package_iterator_destroy(pi);
   316 
   317 	razor_set_unref(set);
   318 
   319 	return 0;
   320 }
   321 
   322 static int
   323 command_list_package_files(int argc, const char *argv[])
   324 {
   325 	struct razor_error *error = NULL;
   326 	struct razor_set *set;
   327 	struct razor_package_iterator *pi;
   328 	struct razor_package *package;
   329 	const char *name, *version, *arch;
   330 
   331 	set = razor_root_open_read_only(install_root, &error);
   332 	if (set == NULL) {
   333 		fprintf(stderr, "%s\n", razor_error_get_msg(error));
   334 		razor_error_free(error);
   335 		return 1;
   336 	}
   337 
   338 	pi = create_iterator_from_argv(set, argc, argv);
   339 	while (razor_package_iterator_next(pi, &package,
   340 					   RAZOR_DETAIL_NAME, &name,
   341 					   RAZOR_DETAIL_VERSION, &version,
   342 					   RAZOR_DETAIL_ARCH, &arch,
   343 					   RAZOR_DETAIL_LAST))
   344 		razor_set_list_package_files(set, package);
   345 	razor_package_iterator_destroy(pi);
   346 
   347 	razor_set_unref(set);
   348 
   349 	return 0;
   350 }
   351 
   352 static int
   353 list_property_packages(const char *ref_name,
   354 		       const char *ref_version,
   355 		       uint32_t type)
   356 {
   357 	struct razor_error *error = NULL;
   358 	struct razor_set *set;
   359 	struct razor_property *property;
   360 	struct razor_property_iterator *prop_iter;
   361 	struct razor_package_iterator *pkg_iter;
   362 	const char *name, *version;
   363 	uint32_t flags;
   364 
   365 	if (ref_name == NULL)
   366 		return 0;
   367 
   368 	set = razor_root_open_read_only(install_root, &error);
   369 	if (set == NULL) {
   370 		fprintf(stderr, "%s\n", razor_error_get_msg(error));
   371 		razor_error_free(error);
   372 		return 1;
   373 	}
   374 
   375 	prop_iter = razor_property_iterator_create(set, NULL);
   376 	while (razor_property_iterator_next(prop_iter, &property,
   377 					    &name, &flags, &version)) {
   378 		if (strcmp(ref_name, name) != 0)
   379 			continue;
   380 		if (ref_version &&
   381 		    (flags & RAZOR_PROPERTY_RELATION_MASK) == RAZOR_PROPERTY_EQUAL &&
   382 		    strcmp(ref_version, version) != 0)
   383 			continue;
   384 		if ((flags & RAZOR_PROPERTY_TYPE_MASK) != type)
   385 			continue;
   386 
   387 		pkg_iter =
   388 			razor_package_iterator_create_for_property(set,
   389 								   property);
   390 		list_packages(pkg_iter, 0);
   391 		razor_package_iterator_destroy(pkg_iter);
   392 	}
   393 	razor_property_iterator_destroy(prop_iter);
   394 
   395 	razor_set_unref(set);
   396 
   397 	return 0;
   398 }
   399 
   400 static int
   401 command_what_requires(int argc, const char *argv[])
   402 {
   403 	return list_property_packages(argv[0], argv[1],
   404 				      RAZOR_PROPERTY_REQUIRES);
   405 }
   406 
   407 static int
   408 command_what_provides(int argc, const char *argv[])
   409 {
   410 	return list_property_packages(argv[0], argv[1],
   411 				      RAZOR_PROPERTY_PROVIDES);
   412 }
   413 
   414 static int
   415 show_progress(void *clientp,
   416 	      double dltotal, double dlnow, double ultotal, double ulnow)
   417 {
   418 	const char *file = clientp;
   419 
   420 	if (!dlnow < dltotal)
   421 		fprintf(stderr, "\rdownloading %s, %dkB/%dkB",
   422 			file, (int) dlnow / 1024, (int) dltotal / 1024);
   423 
   424 	return 0;
   425 }
   426 
   427 static int
   428 download_if_missing(const char *url, const char *file)
   429 {
   430 #ifndef HAVE_CURL
   431 	return 1;
   432 #else
   433 	CURL *curl;
   434 	struct stat buf;
   435 	char error[256];
   436 	FILE *fp;
   437 	CURLcode res;
   438 	long response;
   439 
   440 	curl = curl_easy_init();
   441 	if (curl == NULL)
   442 		return 1;
   443 
   444 	curl_easy_setopt(curl, CURLOPT_ERRORBUFFER, error);
   445 	curl_easy_setopt(curl, CURLOPT_NOPROGRESS, 0);
   446 	curl_easy_setopt(curl, CURLOPT_PROGRESSFUNCTION, show_progress);
   447 	curl_easy_setopt(curl, CURLOPT_PROGRESSDATA, file);
   448 
   449 	if (stat(file, &buf) < 0) {
   450 		fp = fopen(file, "wb");
   451 		if (fp == NULL) {
   452 			fprintf(stderr,
   453 				"failed to open %s for writing\n", file);
   454 			return -1;
   455 		}
   456 		curl_easy_setopt(curl, CURLOPT_WRITEDATA, fp);
   457 		curl_easy_setopt(curl, CURLOPT_URL, url);
   458 		res = curl_easy_perform(curl);
   459 		fclose(fp);
   460 		if (res != CURLE_OK) {
   461 			fprintf(stderr, "curl error: %s\n", error);
   462 			unlink(file);
   463 			return -1;
   464 		}
   465 		res = curl_easy_getinfo(curl,
   466 					CURLINFO_RESPONSE_CODE, &response);
   467 		if (res != CURLE_OK) {
   468 			fprintf(stderr, "curl error: %s\n", error);
   469                         unlink(file);
   470                         return -1;
   471 		}
   472 		if (response != 200) {
   473 			fprintf(stderr, " - failed %ld\n", response);
   474                         unlink(file);
   475                         return -1;
   476 		}
   477 		fprintf(stderr, "\n");
   478 	}
   479 
   480 	curl_easy_cleanup(curl);
   481 
   482 	return 0;
   483 #endif	/* HAVE_CURL */
   484 }
   485 
   486 #define YUM_URL "http://download.fedora.redhat.com" \
   487 	"/pub/fedora/linux/development/i386/os"
   488 
   489 static int
   490 command_import_yum(int argc, const char *argv[])
   491 {
   492 	int retval;
   493 	struct razor_set *set;
   494 	struct razor_atomic *atomic;
   495 	char buffer[512];
   496 
   497 	printf("downloading from %s.\n", yum_url);
   498 	snprintf(buffer, sizeof buffer,
   499 		 "%s/repodata/primary.xml.gz", yum_url);
   500 	if (download_if_missing(buffer, "primary.xml.gz") < 0)
   501 		return -1;
   502 	snprintf(buffer, sizeof buffer,
   503 		 "%s/repodata/filelists.xml.gz", yum_url);
   504 	if (download_if_missing(buffer, "filelists.xml.gz") < 0)
   505 		return -1;
   506 
   507 	set = razor_set_create_from_yum();
   508 	if (set == NULL)
   509 		return 1;
   510 	atomic = razor_atomic_open("Yum import repository");
   511 	razor_set_write(set, atomic, rawhide_repo_filename, RAZOR_SECTION_ALL);
   512 	retval = razor_atomic_commit(atomic);
   513 	razor_set_unref(set);
   514 	if (retval)
   515 		fprintf(stderr, "%s\n", razor_atomic_get_error_msg(atomic));
   516 	else
   517 		printf("wrote %s\n", rawhide_repo_filename);
   518 	razor_atomic_destroy(atomic);
   519 
   520 	return retval;
   521 }
   522 
   523 #if HAVE_RPMLIB
   524 static int
   525 command_import_rpmdb(int argc, const char *argv[])
   526 {
   527 	struct razor_set *set;
   528 	struct razor_root *root;
   529 	struct razor_error *error = NULL;
   530 	struct razor_atomic *atomic;
   531 	int retval;
   532 
   533 	root = razor_root_open(install_root, &error);
   534 	if (root == NULL) {
   535 		fprintf(stderr, "%s\n", razor_error_get_msg(error));
   536 		razor_error_free(error);
   537 		return 1;
   538 	}
   539 
   540 	set = razor_set_create_from_rpmdb();
   541 	if (set == NULL)
   542 		return 1;
   543 
   544 	atomic = razor_atomic_open("Import RPM database");
   545 
   546 	retval = razor_root_update(root, set, atomic);
   547 
   548 	if (retval)
   549 		fprintf(stderr, "%s\n", razor_atomic_get_error_msg(atomic));
   550 
   551 	razor_atomic_destroy(atomic);
   552 
   553 	razor_root_close(root);
   554 
   555 	return retval;
   556 }
   557 #endif
   558 
   559 static int
   560 mark_packages_for_update(struct razor_transaction *trans,
   561 			 struct razor_set *set, const char *pattern)
   562 {
   563 	struct razor_package_iterator *pi;
   564 	struct razor_package *package;
   565 	const char *name;
   566 	int matches = 0;
   567 
   568 	pi = razor_package_iterator_create(set);
   569 	while (razor_package_iterator_next(pi, &package,
   570 					   RAZOR_DETAIL_NAME, &name,
   571 					   RAZOR_DETAIL_LAST)) {
   572 		if (pattern && fnmatch(pattern, name, 0) == 0) {
   573 			razor_transaction_update_package(trans, package);
   574 			matches++;
   575 		}
   576 	}
   577 	razor_package_iterator_destroy(pi);
   578 
   579 	return matches;
   580 }
   581 
   582 static int
   583 mark_packages_for_removal(struct razor_transaction *trans,
   584 			  struct razor_set *set, const char *pattern)
   585 {
   586 	struct razor_package_iterator *pi;
   587 	struct razor_package *package;
   588 	const char *name;
   589 	int matches = 0;
   590 
   591 	pi = razor_package_iterator_create(set);
   592 	while (razor_package_iterator_next(pi, &package,
   593 					   RAZOR_DETAIL_NAME, &name,
   594 					   RAZOR_DETAIL_LAST)) {
   595 		if (pattern && fnmatch(pattern, name, 0) == 0) {
   596 			razor_transaction_remove_package(trans, package);
   597 			matches++;
   598 		}
   599 	}
   600 	razor_package_iterator_destroy(pi);
   601 
   602 	return matches;
   603 }
   604 
   605 static int
   606 command_remove(int argc, const char *argv[])
   607 {
   608 	struct razor_set *system, *upstream, *next;
   609 	struct razor_transaction *trans;
   610 	struct razor_error *error = NULL;
   611 	struct razor_root *root;
   612 	int i, retval;
   613 
   614 	root = razor_root_open(install_root, &error);
   615 	if (root == NULL) {
   616 		fprintf(stderr, "%s\n", razor_error_get_msg(error));
   617 		razor_error_free(error);
   618 		return 1;
   619 	}
   620 
   621 	system = razor_root_get_system_set(root);
   622 	upstream = razor_set_create_without_root();
   623 	trans = razor_transaction_create(system, upstream);
   624 	razor_set_unref(upstream);
   625 	for (i = 0; i < argc; i++) {
   626 		if (mark_packages_for_removal(trans, system, argv[i]) == 0) {
   627 			fprintf(stderr, "no match for %s\n", argv[i]);
   628 			razor_transaction_destroy(trans);
   629 			razor_root_close(root);
   630 			return 1;
   631 		}
   632 	}
   633 
   634 	razor_transaction_resolve(trans);
   635 	retval = razor_transaction_describe(trans);
   636 	if (retval) {
   637 		razor_transaction_destroy(trans);
   638 		razor_root_close(root);
   639 		return 1;
   640 	}
   641 
   642 	next = razor_transaction_commit(trans);
   643 
   644 	retval = update_system(root, NULL, trans, next, "Remove");
   645 
   646 	razor_transaction_destroy(trans);
   647 	razor_root_close(root);
   648 	razor_set_unref(next);
   649 
   650 	return retval;
   651 }
   652 
   653 static void
   654 print_diff(enum razor_diff_action action,
   655 	   struct razor_package *package,
   656 	   const char *name,
   657 	   const char *version,
   658 	   const char *arch,
   659 	   void *data)
   660 {
   661 	if (action == RAZOR_DIFF_ACTION_ADD)
   662 		printf("install %s-%s.%s\n", name, version, arch);
   663 	if (action == RAZOR_DIFF_ACTION_REMOVE)
   664 		printf("remove %s-%s.%s\n", name, version, arch);
   665 }
   666 
   667 static int
   668 command_diff(int argc, const char *argv[])
   669 {
   670 	struct razor_error *error = NULL;
   671 	struct razor_set *set, *updated;
   672 
   673 	set = razor_root_open_read_only(install_root, &error);
   674 	if (set)
   675 		updated = razor_set_open(rawhide_repo_filename, 0, &error);
   676 	else
   677 		updated = NULL;
   678 	if (updated == NULL) {
   679 		fprintf(stderr, "%s\n", razor_error_get_msg(error));
   680 		razor_error_free(error);
   681 		if (set)
   682 			razor_set_unref(set);
   683 		return 1;
   684 	}
   685 
   686 	razor_set_diff(set, updated, print_diff, NULL);
   687 
   688 	razor_set_unref(set);
   689 	razor_set_unref(updated);
   690 
   691 	return 0;
   692 }
   693 
   694 static int
   695 command_import_rpms(int argc, const char *argv[])
   696 {
   697 	DIR *dir;
   698 	struct dirent *de;
   699 	struct razor_importer *importer;
   700 	struct razor_set *set;
   701 	struct razor_rpm *rpm;
   702 	struct razor_atomic *atomic;
   703 	int len, imported_count = 0;
   704 	char filename[256];
   705 	const char *dirname = argv[0];
   706 	int retval;
   707 
   708 	if (dirname == NULL) {
   709 		fprintf(stderr, "usage: razor import-rpms DIR\n");
   710 		return -1;
   711 	}
   712 
   713 	dir = opendir(dirname);
   714 	if (dir == NULL) {
   715 		fprintf(stderr, "couldn't read dir %s\n", dirname);
   716 		return -1;
   717 	}
   718 
   719 	importer = razor_importer_create();
   720 
   721 	while (de = readdir(dir), de != NULL) {
   722 		len = strlen(de->d_name);
   723 		if (len < 5 || strcmp(de->d_name + len - 4, ".rpm") != 0)
   724 			continue;
   725 		snprintf(filename, sizeof filename,
   726 			 "%s/%s", dirname, de->d_name);
   727 		atomic = razor_atomic_open("Read RPM");
   728 		rpm = razor_rpm_open(filename, atomic);
   729 		if (rpm == NULL)
   730 			fprintf(stderr, "%s\n",
   731 				razor_atomic_get_error_msg(atomic));
   732 		razor_atomic_destroy(atomic);
   733 		if (rpm == NULL)
   734 			continue;
   735 		if (razor_importer_add_rpm(importer, rpm)) {
   736 			fprintf(stderr, "couldn't import %s\n", filename);
   737 			break;
   738 		}
   739 		razor_rpm_close(rpm);
   740 
   741 		printf("\rimporting %d", ++imported_count);
   742 		fflush(stdout);
   743 	}
   744 
   745 	if (de != NULL) {
   746 		razor_importer_destroy(importer);
   747 		return -1;
   748 	}
   749 
   750 	printf("\nsaving\n");
   751 	set = razor_importer_finish(importer);
   752 
   753 	atomic = razor_atomic_open("Update system database");
   754 	razor_set_write(set, atomic, repo_filename, RAZOR_SECTION_ALL);
   755 	razor_set_unref(set);
   756 	retval = razor_atomic_commit(atomic);
   757 	if (retval)
   758 		fprintf(stderr, "%s\n", razor_atomic_get_error_msg(atomic));
   759 	else
   760 		printf("wrote %s\n", repo_filename);
   761 	razor_atomic_destroy(atomic);
   762 
   763 	return retval;
   764 }
   765 
   766 static char *
   767 rpm_filename(const char *name, const char *version, const char *arch)
   768 {
   769  	const char *v;
   770  
   771  	/* Skip epoch */
   772 	v = strchr(version, ':');
   773  	if (v != NULL)
   774  		v = v + 1;
   775  	else
   776 		v = version;
   777 
   778 	return razor_concat(name, "-", v, ".", arch, ".rpm", NULL);
   779 }
   780 
   781 static int
   782 download_packages(struct razor_set *system, struct razor_set *next)
   783 {
   784 	struct razor_install_iterator *ii;
   785 	struct razor_package *package;
   786 	enum razor_install_action action;
   787 	const char *name, *version, *arch;
   788 	char *file, *url, *s;
   789 	int errors = 0, count;
   790 
   791 	ii = razor_set_create_install_iterator(system, next);
   792 	while (razor_install_iterator_next(ii, &package, &action, &count)) {
   793 		if (action != RAZOR_INSTALL_ACTION_ADD)
   794 			continue;
   795 
   796 		razor_package_get_details(next, package,
   797 					  RAZOR_DETAIL_NAME, &name,
   798 					  RAZOR_DETAIL_VERSION, &version,
   799 					  RAZOR_DETAIL_ARCH, &arch,
   800 					  RAZOR_DETAIL_LAST);
   801 		
   802 		s = rpm_filename(name, version, arch);
   803 		url = razor_concat(yum_url, "/Packages/", s, NULL);
   804 		file = razor_concat("rpms/", s, NULL);
   805 		free(s);
   806 		if (download_if_missing(url, file) < 0)
   807 			errors++;
   808 		free(file);
   809 		free(url);
   810 	}
   811 	razor_install_iterator_destroy(ii);
   812 
   813 	if (errors > 0) {
   814 		fprintf(stderr, "failed to download %d packages\n", errors);
   815                 return -1;
   816         }
   817 
   818 	return 0;
   819 }
   820 
   821 static struct razor_set *
   822 relocate_packages(struct razor_set *set, struct razor_atomic *atomic,
   823 		  struct razor_relocations *relocations)
   824 {
   825 	int i;
   826 	struct razor_importer *importer;
   827 	struct razor_property_iterator *prop_iter;
   828 	struct razor_package_iterator *pkg_iter;
   829  	struct razor_file_iterator *file_iter;
   830  	struct razor_package *package;
   831 	struct razor_property *property;
   832 	struct razor_rpm *rpm;
   833 	const char *name, *version, *arch, *summary, *desc, *url, *license;
   834 	const char *preunprog, *preun, *postunprog, *postun;
   835 	const char *install_prefix;
   836 	const char *const *prefixes;
   837 	char *file, *s;
   838 	uint32_t flags;
   839 
   840 	importer = razor_importer_create();
   841 	pkg_iter = razor_package_iterator_create(set);
   842 
   843 	while (razor_package_iterator_next(pkg_iter, &package,
   844 					   RAZOR_DETAIL_NAME, &name,
   845 					   RAZOR_DETAIL_VERSION, &version,
   846 					   RAZOR_DETAIL_ARCH, &arch,
   847 					   RAZOR_DETAIL_SUMMARY, &summary,
   848 					   RAZOR_DETAIL_DESCRIPTION, &desc,
   849 					   RAZOR_DETAIL_URL, &url,
   850 					   RAZOR_DETAIL_LICENSE, &license,
   851 					   RAZOR_DETAIL_PREUNPROG, &preunprog,
   852 					   RAZOR_DETAIL_PREUN, &preun,
   853 					   RAZOR_DETAIL_POSTUNPROG, &postunprog,
   854 					   RAZOR_DETAIL_POSTUN, &postun,
   855 					   RAZOR_DETAIL_LAST)) {
   856 		s = rpm_filename(name, version, arch);
   857 		file = razor_concat("rpms/", s, NULL);
   858 		free(s);
   859 		rpm = razor_rpm_open(file, atomic);
   860 		free(file);
   861 		if (rpm == NULL) {
   862 			razor_package_iterator_destroy(pkg_iter);
   863 			razor_importer_destroy(importer);
   864 			return NULL;
   865 		}
   866 
   867 		razor_relocations_set_rpm(relocations, rpm);
   868 
   869 		razor_importer_begin_package(importer, name, version, arch);
   870 		razor_importer_add_details(importer,
   871 					   summary, desc, url, license);
   872 
   873 		razor_rpm_get_details(rpm, RAZOR_DETAIL_PREFIXES, &prefixes,
   874 				      RAZOR_DETAIL_LAST);
   875 		for (i = 0; prefixes && prefixes[i]; i++) {
   876 			install_prefix = razor_relocations_apply(relocations,
   877 								 prefixes[i]);
   878 			razor_importer_add_install_prefix(importer,
   879 							  install_prefix);
   880 		}
   881 
   882 		razor_rpm_close(rpm);
   883 
   884 		prop_iter = razor_property_iterator_create(set, package);
   885 		while (razor_property_iterator_next(prop_iter, &property,
   886 						    &name, &flags, &version))
   887 			razor_importer_add_property(importer,
   888 						    name, flags, version);
   889 		razor_property_iterator_destroy(prop_iter);
   890 
   891 		file_iter = razor_file_iterator_create(set, package, 0);
   892 		while (razor_file_iterator_next(file_iter, &name)) {
   893 			name = razor_relocations_apply(relocations, name);
   894 			razor_importer_add_file(importer, name);
   895 		}
   896 		razor_file_iterator_destroy(file_iter);
   897 
   898 		razor_importer_add_script(importer, RAZOR_PROPERTY_PREUN,
   899 					  preunprog, preun);
   900 		razor_importer_add_script(importer, RAZOR_PROPERTY_POSTUN,
   901 					  postunprog, postun);
   902 
   903 		razor_importer_finish_package(importer);
   904 	}
   905 
   906 	razor_package_iterator_destroy(pkg_iter);
   907 	return razor_importer_finish(importer);
   908 }
   909 
   910 static int
   911 install_package(struct razor_transaction *trans, struct razor_set *set,
   912 		struct razor_atomic *atomic, struct razor_package *package,
   913 		struct razor_relocations *relocations, int install_count,
   914 		enum razor_stage_type stage)
   915 {
   916 	int retval;
   917 	const char *name, *version, *arch;
   918 	char *file, *s;
   919 	struct razor_rpm *rpm;
   920 
   921 	razor_package_get_details(set, package,
   922 				  RAZOR_DETAIL_NAME, &name,
   923 				  RAZOR_DETAIL_VERSION, &version,
   924 				  RAZOR_DETAIL_ARCH, &arch,
   925 				  RAZOR_DETAIL_LAST);
   926 
   927 	if (stage & RAZOR_STAGE_SCRIPTS_PRE)
   928 		printf("install %s-%s\n", name, version);
   929 
   930 	s = rpm_filename(name, version, arch);
   931 	file = razor_concat("rpms/", s, NULL);
   932 	free(s);
   933 	rpm = razor_rpm_open(file, atomic);
   934 	free(file);
   935 	if (rpm == NULL) {
   936 		fprintf(stderr, "%s\n",
   937 			razor_atomic_get_error_msg(atomic));
   938 		return -1;
   939 	}
   940 	if (relocations)
   941 		razor_rpm_set_relocations(rpm, relocations);
   942 	razor_transaction_fixup_package(trans, package, rpm);
   943 	retval = razor_rpm_install(rpm, atomic, install_root, install_count,
   944 				   stage);
   945 	if (retval < 0) {
   946 		s = rpm_filename(name, version, arch);
   947 		fprintf(stderr, "%s: %s\n", s,
   948 			razor_atomic_get_error_msg(atomic));
   949 		free(s);
   950 	}
   951 	razor_rpm_close(rpm);
   952 	return retval;
   953 }
   954 
   955 /*
   956  * Returns 0 on success, -1 on failure and 1 if a RAZOR_INSTALL_ACTION_COMMIT
   957  * is met (in which case the action is consumed).
   958  */
   959 static int
   960 update_packages(struct razor_transaction *trans,
   961 		struct razor_install_iterator *ii, struct razor_set *system,
   962 		struct razor_set *next, struct razor_atomic *atomic,
   963 		struct razor_relocations *relocations,
   964 		enum razor_stage_type stage)
   965 {
   966 	struct razor_package *package;
   967 	enum razor_install_action action;
   968 	int retval = 0, count;
   969 
   970 	while (!retval && razor_install_iterator_next(ii, &package, &action,
   971 						      &count)) {
   972 		if (action == RAZOR_INSTALL_ACTION_ADD) {
   973 			if (install_package(trans, next, atomic, package,
   974 					    relocations, count, stage))
   975 				retval = -1;
   976 		} else if (action == RAZOR_INSTALL_ACTION_REMOVE) {
   977 			if (razor_package_remove(system, next, atomic, package,
   978 						 install_root, count, stage))
   979 				retval = -1;
   980 		} else if (action == RAZOR_INSTALL_ACTION_COMMIT)
   981 				retval = 1;
   982 	}
   983 
   984 	return retval;
   985 }
   986 
   987 static int
   988 update_system(struct razor_root *root, struct razor_relocations *relocations,
   989 	      struct razor_transaction *trans, struct razor_set *next,
   990 	      const char *verb)
   991 {
   992 	struct razor_set *system, *set;
   993 	struct razor_atomic *atomic;
   994 	struct razor_install_iterator *ii;
   995 	int r, retval = 0;
   996 	char *description;
   997 	size_t pos;
   998 
   999 	description = razor_concat(verb, " packages", NULL);
  1000 
  1001 	system = razor_set_ref(razor_root_get_system_set(root));
  1002 
  1003 	ii = razor_set_create_install_iterator(system, next);
  1004 
  1005 	do {
  1006 		pos = razor_install_iterator_tell(ii);
  1007 
  1008 		atomic = razor_atomic_open(description);
  1009 
  1010 		r = update_packages(trans, ii, system, next, atomic,
  1011 				    relocations, RAZOR_STAGE_SCRIPTS_PRE);
  1012 		if (r < 0) {
  1013 			fprintf(stderr, "%s aborted\n", verb);
  1014 			retval = r;
  1015 		} else {
  1016 			razor_install_iterator_seek(ii, pos);
  1017 			r = update_packages(trans, ii, system, next, atomic,
  1018 					    relocations, RAZOR_STAGE_FILES);
  1019 
  1020 			if (r == 1) {
  1021 				set = razor_install_iterator_commit_set(ii);
  1022 				razor_root_update(root, set, atomic);
  1023 				razor_set_unref(set);
  1024 			} else if (r == 0)
  1025 				razor_root_update(root, next, atomic);
  1026 
  1027 			retval = razor_atomic_commit(atomic);
  1028 			if (retval)
  1029 				fprintf(stderr, "%s\n",
  1030 					razor_atomic_get_error_msg(atomic));
  1031 			else {
  1032 				razor_install_iterator_seek(ii, pos);
  1033 				update_packages(trans, ii, system, next,
  1034 						atomic, relocations,
  1035 						RAZOR_STAGE_SCRIPTS_POST);
  1036 			}
  1037 		}
  1038 
  1039 		razor_atomic_destroy(atomic);
  1040 	} while(!retval && r == 1);
  1041 
  1042 	razor_set_unref(system);
  1043 
  1044 	free(description);
  1045 
  1046 	return retval;
  1047 }
  1048 
  1049 static int
  1050 command_install_or_update(int argc, const char *argv[], int do_update)
  1051 {
  1052 	struct razor_relocations *relocations = NULL;
  1053 	struct razor_set *system, *upstream, *next, *set;
  1054 	struct razor_transaction *trans;
  1055 	struct razor_error *error = NULL;
  1056 	struct razor_atomic *atomic;
  1057 	struct razor_root *root;
  1058 	int i, retval, len, dependencies = 1;
  1059 	char *oldpath;
  1060 
  1061 	for (i = 0; i < argc; i++) {
  1062 		if (strcmp(argv[i], "--no-dependencies") == 0)
  1063 			dependencies = 0;
  1064 		else if (strcmp(argv[i], "--relocate") == 0) {
  1065 			i++;
  1066 			if (i >= argc || strchr(argv[i], '=') == NULL) {
  1067 				fprintf(stderr,
  1068 				    "Usage: razor %s [OPTION...] RPM\n",
  1069 				    do_update ? "update" : "install");
  1070 				fprintf(stderr, "Options:\n");
  1071 				fprintf(stderr, "    [--no-dependencies]\n");
  1072 				fprintf(stderr,
  1073 				    "    [--relocate OLDPATH=NEWPATH] RPM\n");
  1074 				return -1;
  1075 			}
  1076 			len = strchr(argv[i], '=') - argv[i];
  1077 			oldpath = malloc(len + 1);
  1078 			strncpy(oldpath, argv[i], len);
  1079 			oldpath[len] = '\0';
  1080 			if (!relocations)
  1081 			       relocations = razor_relocations_create();
  1082 			razor_relocations_add(relocations, oldpath,
  1083 					      argv[i] + len + 1);
  1084 			free(oldpath);
  1085 		} else
  1086 			break;
  1087 	}
  1088 
  1089 	upstream = razor_set_open(rawhide_repo_filename, 0, &error);
  1090 	if (upstream == NULL) {
  1091 		fprintf(stderr, "%s\n", razor_error_get_msg(error));
  1092 		razor_error_free(error);
  1093 		return 1;
  1094 	}
  1095 
  1096 	if (do_update)
  1097 		atomic = razor_atomic_open("Update packages");
  1098 	else
  1099 		atomic = razor_atomic_open("Install packages");
  1100 
  1101 	if (relocations) {
  1102 		set = relocate_packages(upstream, atomic, relocations);
  1103 		if (set == NULL) {
  1104 			fprintf(stderr, "%s\n",
  1105 				razor_atomic_get_error_msg(atomic));
  1106 			razor_atomic_destroy(atomic);
  1107 			razor_set_unref(upstream);
  1108 			return 1;
  1109 		}
  1110 		razor_set_unref(upstream);
  1111 		upstream = set;
  1112 	}
  1113 
  1114 	root = razor_root_open(install_root, &error);
  1115 	if (root == NULL) {
  1116 		fprintf(stderr, "%s\n", razor_error_get_msg(error));
  1117 		razor_error_free(error);
  1118 		razor_atomic_destroy(atomic);
  1119 		razor_set_unref(upstream);
  1120 		if (relocations)
  1121 			razor_relocations_destroy(relocations);
  1122 		return 1;
  1123 	}
  1124 
  1125 	system = razor_root_get_system_set(root);
  1126 	trans = razor_transaction_create(system, upstream);
  1127 
  1128 	if (i == argc && do_update)
  1129 		razor_transaction_update_all(trans);
  1130 	for (; i < argc; i++) {
  1131 		if (do_update &&
  1132 		    mark_packages_for_update(trans, system, argv[i]))
  1133 			continue;
  1134 		if (mark_packages_for_update(trans, upstream, argv[i]) == 0) {
  1135 			fprintf(stderr, "no package matched %s\n", argv[i]);
  1136 			razor_transaction_destroy(trans);
  1137 			razor_root_close(root);
  1138 			razor_set_unref(upstream);
  1139 			razor_atomic_destroy(atomic);
  1140 			if (relocations)
  1141 				razor_relocations_destroy(relocations);
  1142 			return 1;
  1143 		}
  1144 	}
  1145 
  1146 	if (dependencies) {
  1147 		razor_transaction_resolve(trans);
  1148 		if (razor_transaction_describe(trans) > 0) {
  1149 			razor_transaction_destroy(trans);
  1150 			razor_set_unref(upstream);
  1151 			razor_root_close(root);
  1152 			razor_atomic_destroy(atomic);
  1153 			if (relocations)
  1154 				razor_relocations_destroy(relocations);
  1155 			return 1;
  1156 		}
  1157 	}
  1158 
  1159 	if (razor_atomic_create_dir(atomic, "rpms",
  1160 				    S_IRWXU | S_IRWXG | S_IRWXO) ||
  1161 	    razor_atomic_commit(atomic)) {
  1162 		fprintf(stderr, "%s\n", razor_atomic_get_error_msg(atomic));
  1163 		razor_transaction_destroy(trans);
  1164 		razor_set_unref(upstream);
  1165 		razor_root_close(root);
  1166 		razor_atomic_destroy(atomic);
  1167 		if (relocations)
  1168 			razor_relocations_destroy(relocations);
  1169 		return 1;
  1170 	}
  1171 
  1172 	razor_atomic_destroy(atomic);
  1173 
  1174 	next = razor_transaction_commit(trans);
  1175 
  1176 	if (download_packages(system, next) < 0) {
  1177 		razor_set_unref(next);
  1178 		razor_transaction_destroy(trans);
  1179 		razor_set_unref(upstream);
  1180 		razor_root_close(root);
  1181 		razor_atomic_destroy(atomic);
  1182 		if (relocations)
  1183 			razor_relocations_destroy(relocations);
  1184                 return 1;
  1185         }
  1186 
  1187 	retval = update_system(root, relocations, trans, next,
  1188 			       do_update ? "Update" : "Install");
  1189 
  1190 	razor_set_unref(upstream);
  1191 	razor_root_close(root);
  1192 
  1193 	razor_transaction_destroy(trans);
  1194 	if (relocations)
  1195 		razor_relocations_destroy(relocations);
  1196 
  1197 	razor_set_unref(next);
  1198 
  1199 	return retval;
  1200 }
  1201 
  1202 static int
  1203 command_update(int argc, const char *argv[])
  1204 {
  1205 	return command_install_or_update(argc, argv, 1);
  1206 }
  1207 
  1208 static int
  1209 command_install(int argc, const char *argv[])
  1210 {
  1211 	return command_install_or_update(argc, argv, 0);
  1212 }
  1213 
  1214 static int
  1215 command_init(int argc, const char *argv[])
  1216 {
  1217 	return razor_root_create(install_root);
  1218 }
  1219 
  1220 static int
  1221 command_download(int argc, const char *argv[])
  1222 {
  1223 	struct razor_error *error = NULL;
  1224 	struct razor_atomic *atomic;
  1225 	struct razor_set *set;
  1226 	struct razor_package_iterator *pi;
  1227 	struct razor_package *package;
  1228 	const char *pattern = argv[0], *name, *version, *arch;
  1229 	char url[256], file[256];
  1230 	int matches = 0;
  1231 
  1232 	set = razor_set_open(rawhide_repo_filename, 0, &error);
  1233 	if (set == NULL) {
  1234 		fprintf(stderr, "%s\n", razor_error_get_msg(error));
  1235 		razor_error_free(error);
  1236 		return 1;
  1237 	}
  1238 
  1239 	atomic = razor_atomic_open("Download packages");
  1240 
  1241 	if (razor_atomic_create_dir(atomic, "rpms", 
  1242 				    S_IRWXU | S_IRWXG | S_IRWXO)) {
  1243 		fprintf(stderr, "%s\n", razor_atomic_get_error_msg(atomic));
  1244 		razor_atomic_destroy(atomic);
  1245 		return 1;
  1246 	}
  1247 
  1248 	if (razor_atomic_commit(atomic)) {
  1249 		fprintf(stderr, "%s\n", razor_atomic_get_error_msg(atomic));
  1250 		razor_atomic_destroy(atomic);
  1251 		return 1;
  1252 	}
  1253 	razor_atomic_destroy(atomic);
  1254 
  1255 	pi = razor_package_iterator_create(set);
  1256 	while (razor_package_iterator_next(pi, &package,
  1257 					   RAZOR_DETAIL_NAME, &name,
  1258 					   RAZOR_DETAIL_VERSION, &version,
  1259 					   RAZOR_DETAIL_ARCH, &arch,
  1260 					   RAZOR_DETAIL_LAST)) {
  1261 		if (pattern && fnmatch(pattern, name, 0) != 0)
  1262 			continue;
  1263 
  1264 		matches++;
  1265 		snprintf(url, sizeof url,
  1266 			 "%s/Packages/%s-%s.%s.rpm",
  1267 			 yum_url, name, version, arch);
  1268 		snprintf(file, sizeof file,
  1269 			 "rpms/%s-%s.%s.rpm", name, version, arch);
  1270 		download_if_missing(url, file);
  1271 	}
  1272 	razor_package_iterator_destroy(pi);
  1273 	razor_set_unref(set);
  1274 
  1275 	if (matches == 0)
  1276 		fprintf(stderr, "no packages matched \"%s\"\n", pattern);
  1277 	else if (matches == 1)
  1278 		fprintf(stderr, "downloaded 1 package\n");
  1279 	else
  1280 		fprintf(stderr, "downloaded %d packages\n", matches);
  1281 
  1282 	return 0;
  1283 }
  1284 
  1285 static int
  1286 command_info(int argc, const char *argv[])
  1287 {
  1288 	struct razor_error *error = NULL;
  1289 	struct razor_set *set;
  1290 	struct razor_package_iterator *pi;
  1291 	struct razor_package *package;
  1292 	const char *pattern = argv[0], *name, *version, *arch;
  1293 	const char *summary, *description, *url, *license;
  1294 
  1295 	set = razor_root_open_read_only(install_root, &error);
  1296 	if (set == NULL) {
  1297 		fprintf(stderr, "%s\n", razor_error_get_msg(error));
  1298 		razor_error_free(error);
  1299 		return 1;
  1300 	}
  1301 
  1302 	pi = razor_package_iterator_create(set);
  1303 	while (razor_package_iterator_next(pi, &package,
  1304 					   RAZOR_DETAIL_NAME, &name,
  1305 					   RAZOR_DETAIL_VERSION, &version,
  1306 					   RAZOR_DETAIL_ARCH, &arch,
  1307 					   RAZOR_DETAIL_LAST)) {
  1308 		if (pattern && fnmatch(pattern, name, 0) != 0)
  1309 			continue;
  1310 
  1311 		razor_package_get_details (set, package,
  1312 					   RAZOR_DETAIL_SUMMARY, &summary,
  1313 					   RAZOR_DETAIL_DESCRIPTION, &description,
  1314 					   RAZOR_DETAIL_URL, &url,
  1315 					   RAZOR_DETAIL_LICENSE, &license,
  1316 					   RAZOR_DETAIL_LAST);
  1317 
  1318 		printf ("Name:        %s\n", name);
  1319 		printf ("Arch:        %s\n", arch);
  1320 		printf ("Version:     %s\n", version);
  1321 		printf ("URL:         %s\n", url);
  1322 		printf ("License:     %s\n", license);
  1323 		printf ("Summary:     %s\n", summary);
  1324 		printf ("Description:\n");
  1325 		printf ("%s\n", description);
  1326 		printf ("\n");
  1327 	}
  1328 	razor_package_iterator_destroy(pi);
  1329 	razor_set_unref(set);
  1330 
  1331 	return 0;
  1332 }
  1333 
  1334 #define SEARCH_MAX 256
  1335 
  1336 static int
  1337 command_search(int argc, const char *argv[])
  1338 {
  1339 	struct razor_error *error = NULL;
  1340 	struct razor_set *set;
  1341 	struct razor_package_iterator *pi;
  1342 	struct razor_package *package;
  1343 	char pattern[SEARCH_MAX];
  1344 	const char *name, *version, *arch;
  1345 	const char *summary, *description, *url, *license;
  1346 
  1347 	if (!argv[0]) {
  1348 		fprintf(stderr, "must specify a search term\n");
  1349 		return 1;
  1350 	}
  1351 
  1352 	snprintf(pattern, sizeof pattern, "*%s*", argv[0]);
  1353 
  1354 	set = razor_set_open(rawhide_repo_filename, 0, &error);
  1355 	if (set == NULL) {
  1356 		fprintf(stderr, "%s\n", razor_error_get_msg(error));
  1357 		razor_error_free(error);
  1358 		return 1;
  1359 	}
  1360 
  1361 	pi = razor_package_iterator_create(set);
  1362 	while (razor_package_iterator_next(pi, &package,
  1363 					   RAZOR_DETAIL_NAME, &name,
  1364 					   RAZOR_DETAIL_VERSION, &version,
  1365 					   RAZOR_DETAIL_ARCH, &arch,
  1366 					   RAZOR_DETAIL_SUMMARY, &summary,
  1367 					   RAZOR_DETAIL_DESCRIPTION, &description,
  1368 					   RAZOR_DETAIL_URL, &url,
  1369 					   RAZOR_DETAIL_LICENSE, &license,
  1370 					   RAZOR_DETAIL_LAST)) {
  1371 		if (!fnmatch(pattern, name, FNM_CASEFOLD) ||
  1372 		    !fnmatch(pattern, url, FNM_CASEFOLD) ||
  1373 		    !fnmatch(pattern, summary, FNM_CASEFOLD) ||
  1374 		    !fnmatch(pattern, description, FNM_CASEFOLD))
  1375 			printf("%s-%s.%s: %s\n", name, version, arch, summary);
  1376 	}
  1377 	razor_package_iterator_destroy(pi);
  1378 	razor_set_unref(set);
  1379 
  1380 	return 0;
  1381 }
  1382 
  1383 static struct {
  1384 	const char *name;
  1385 	const char *description;
  1386 	int (*func)(int argc, const char *argv[]);
  1387 } razor_commands[] = {
  1388 	{ "list", "list all packages", command_list },
  1389 	{ "list-requires", "list all requires for the given package", command_list_requires },
  1390 	{ "list-provides", "list all provides for the given package", command_list_provides },
  1391 	{ "list-obsoletes", "list all obsoletes for the given package", command_list_obsoletes },
  1392 	{ "list-conflicts", "list all conflicts for the given package", command_list_conflicts },
  1393 	{ "list-scripts", "list all scripts for the given package", command_list_scripts },
  1394 	{ "list-files", "list files for package set", command_list_files },
  1395 	{ "list-file-packages", "list packages owning file", command_list_file_packages },
  1396 	{ "list-package-files", "list files in package", command_list_package_files },
  1397 	{ "what-requires", "list the packages that have the given requires", command_what_requires },
  1398 	{ "what-provides", "list the packages that have the given provides", command_what_provides },
  1399 	{ "import-yum", "import yum metadata files", command_import_yum },
  1400 #if HAVE_RPMLIB
  1401 	{ "import-rpmdb", "import the system rpm database", command_import_rpmdb },
  1402 #endif
  1403 	{ "import-rpms", "import rpms from the given directory", command_import_rpms },
  1404 	{ "update", "update all or specified packages", command_update },
  1405 	{ "remove", "remove specified packages", command_remove },
  1406 	{ "diff", "show diff between two package sets", command_diff },
  1407 	{ "install", "install rpm", command_install },
  1408 	{ "init", "init razor root", command_init },
  1409 	{ "download", "download packages", command_download },
  1410 	{ "info", "display package details", command_info },
  1411 	{ "search", "search package details", command_search }
  1412 };
  1413 
  1414 static int
  1415 usage(void)
  1416 {
  1417 	int i;
  1418 
  1419 	printf("usage:\n");
  1420 	for (i = 0; i < ARRAY_SIZE(razor_commands); i++)
  1421 		printf("  %-20s%s\n",
  1422 		       razor_commands[i].name, razor_commands[i].description);
  1423 
  1424 	return 1;
  1425 }
  1426 
  1427 int
  1428 main(int argc, const char *argv[])
  1429 {
  1430 	char *repo, *root;
  1431 	int i;
  1432 
  1433 	repo = getenv("RAZOR_REPO");
  1434 	if (repo != NULL)
  1435 		repo_filename = repo;
  1436 
  1437 	root = getenv("RAZOR_ROOT");
  1438 	if (root != NULL)
  1439 		install_root = root;
  1440 
  1441 	yum_url = getenv("YUM_URL");
  1442 	if (yum_url == NULL)
  1443 		yum_url = YUM_URL;
  1444 
  1445 	if (getenv("RAZOR_NO_ROOT_NAME_CHECKS"))
  1446 		razor_disable_root_name_checks(1);
  1447 
  1448 	if (argc < 2)
  1449 		return usage();
  1450 
  1451 	for (i = 0; i < ARRAY_SIZE(razor_commands); i++)
  1452 		if (strcmp(razor_commands[i].name, argv[1]) == 0)
  1453 			return razor_commands[i].func(argc - 2, argv + 2);
  1454 
  1455 	return usage();
  1456 }