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

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