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