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