src/main.c
author Kristian H?gsberg <krh@redhat.com>
Mon Jun 30 13:28:59 2008 -0400 (2008-06-30)
changeset 306 cd3954499086
parent 305 e10b4f060a9d
child 307 95b6bcadd6c4
permissions -rw-r--r--
Get rid of razor_set_get_package().

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