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