src/main.c
author Kristian H?gsberg <krh@redhat.com>
Tue Jul 01 09:44:46 2008 -0400 (2008-07-01)
changeset 308 f4761f529b9e
parent 306 cd3954499086
child 310 9a7691262ce6
permissions -rw-r--r--
Update docs for the RAZOR_DETAIL_LAST change.
     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,
    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.repo"))
   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.repo"))
   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.repo"))
   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.repo", RAZOR_REPO_FILE_DETAILS);
   422 	razor_set_write(set, "rawhide-files.repo", 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.repo", RAZOR_REPO_FILE_DETAILS);
   439 	razor_set_write(set, "system-files.repo", 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 	upstream = razor_set_open(rawhide_repo_filename);
   501 	if (set == NULL || upstream == NULL)
   502 		return 1;
   503 
   504 	trans = razor_transaction_create(set, upstream);
   505 	if (argc == 0)
   506 		razor_transaction_update_all(trans);
   507 	for (i = 0; i < argc; i++) {
   508 		if (mark_packages_for_update(trans, set, argv[i]) == 0) {
   509 			fprintf(stderr, "no match for %s\n", argv[i]);
   510 			return 1;
   511 		}
   512 	}
   513 
   514 	razor_transaction_resolve(trans);
   515 	errors = razor_transaction_describe(trans);
   516 	if (errors) {
   517 		fprintf(stderr, "unresolved dependencies\n");
   518 		return 1;
   519 	}
   520 
   521 	set = razor_transaction_finish(trans);
   522 	razor_set_write(set, updated_repo_filename, RAZOR_REPO_FILE_MAIN);
   523 	razor_set_destroy(set);
   524 	razor_set_destroy(upstream);
   525 	printf("wrote system-updated.repo\n");
   526 
   527 	return 0;
   528 }
   529 
   530 static int
   531 command_remove(int argc, const char *argv[])
   532 {
   533 	struct razor_set *set, *upstream;
   534 	struct razor_transaction *trans;
   535 	int i, errors;
   536 
   537 	set = razor_set_open(repo_filename);
   538 	if (set == NULL)
   539 		return 1;
   540 
   541 	upstream = razor_set_create();
   542 	trans = razor_transaction_create(set, upstream);
   543 	for (i = 0; i < argc; i++) {
   544 		if (mark_packages_for_removal(trans, set, argv[i]) == 0) {
   545 			fprintf(stderr, "no match for %s\n", argv[i]);
   546 			return 1;
   547 		}
   548 	}
   549 
   550 	errors = razor_transaction_resolve(trans);
   551 	if (errors)
   552 		return 1;
   553 
   554 	set = razor_transaction_finish(trans);
   555 	razor_set_write(set, updated_repo_filename, RAZOR_REPO_FILE_MAIN);
   556 	razor_set_destroy(set);
   557 	razor_set_destroy(upstream);
   558 	printf("wrote system-updated.repo\n");
   559 
   560 	return 0;
   561 }
   562 
   563 static void
   564 print_diff(enum razor_diff_action action,
   565 	   struct razor_package *package,
   566 	   const char *name,
   567 	   const char *version,
   568 	   const char *arch,
   569 	   void *data)
   570 {
   571 	if (action == RAZOR_DIFF_ACTION_ADD)
   572 		printf("install %s-%s.%s\n", name, version, arch);
   573 	if (action == RAZOR_DIFF_ACTION_REMOVE)
   574 		printf("remove %s-%s.%s\n", name, version, arch);
   575 }
   576 
   577 static int
   578 command_diff(int argc, const char *argv[])
   579 {
   580 	struct razor_set *set, *updated;
   581 
   582 	set = razor_set_open(repo_filename);
   583 	updated = razor_set_open(updated_repo_filename);
   584 	if (set == NULL || updated == NULL)
   585 		return 1;
   586 
   587 	razor_set_diff(set, updated, print_diff, NULL);
   588 
   589 	razor_set_destroy(set);
   590 	razor_set_destroy(updated);
   591 
   592 	return 0;
   593 }
   594 
   595 static int
   596 command_import_rpms(int argc, const char *argv[])
   597 {
   598 	DIR *dir;
   599 	struct dirent *de;
   600 	struct razor_importer *importer;
   601 	struct razor_set *set;
   602 	struct razor_rpm *rpm;
   603 	int len, imported_count = 0;
   604 	char filename[256];
   605 	const char *dirname = argv[0];
   606 
   607 	if (dirname == NULL) {
   608 		fprintf(stderr, "usage: razor import-rpms DIR\n");
   609 		return -1;
   610 	}
   611 
   612 	dir = opendir(dirname);
   613 	if (dir == NULL) {
   614 		fprintf(stderr, "couldn't read dir %s\n", dirname);
   615 		return -1;
   616 	}
   617 
   618 	importer = razor_importer_create();
   619 
   620 	while (de = readdir(dir), de != NULL) {
   621 		len = strlen(de->d_name);
   622 		if (len < 5 || strcmp(de->d_name + len - 4, ".rpm") != 0)
   623 		    continue;
   624 		snprintf(filename, sizeof filename,
   625 			 "%s/%s", dirname, de->d_name);
   626 		rpm = razor_rpm_open(filename);
   627 		if (rpm == NULL) {
   628 			fprintf(stderr,
   629 				"failed to open rpm \"%s\"\n", filename);
   630 			continue;
   631 		}
   632 		if (razor_importer_add_rpm(importer, rpm)) {
   633 			fprintf(stderr, "couldn't import %s\n", filename);
   634 			break;
   635 		}
   636 		razor_rpm_close(rpm);
   637 
   638 		printf("\rimporting %d", ++imported_count);
   639 		fflush(stdout);
   640 	}
   641 
   642 	if (de != NULL) {
   643 		razor_importer_destroy(importer);
   644 		return -1;
   645 	}
   646 
   647 	printf("\nsaving\n");
   648 	set = razor_importer_finish(importer);
   649 
   650 	razor_set_write(set, repo_filename, RAZOR_REPO_FILE_MAIN);
   651 	razor_set_write(set, "system-details.repo", RAZOR_REPO_FILE_DETAILS);
   652 	razor_set_write(set, "system-files.repo", RAZOR_REPO_FILE_FILES);
   653 	razor_set_destroy(set);
   654 	printf("wrote %s\n", repo_filename);
   655 
   656 	return 0;
   657 }
   658 
   659 static const char *
   660 rpm_filename(const char *name, const char *version, const char *arch)
   661 {
   662 	static char file[PATH_MAX];
   663  	const char *v;
   664  
   665  	/* Skip epoch */
   666 	v = strchr(version, ':');
   667  	if (v != NULL)
   668  		v = v + 1;
   669  	else
   670 		v = version;
   671 
   672 	snprintf(file, sizeof file, "%s-%s.%s.rpm", name, v, arch);
   673 
   674 	return file;
   675 }
   676 
   677 static int
   678 download_packages(struct razor_set *system, struct razor_set *next)
   679 {
   680 	struct razor_package_iterator *pi;
   681 	struct razor_package *package;
   682 	const char *name, *version, *arch;
   683 	char file[PATH_MAX], url[256];
   684 	int errors;
   685  
   686 	pi = razor_set_create_install_iterator(system, next);
   687 	errors = 0;
   688 	while (razor_package_iterator_next(pi, &package,
   689 					   RAZOR_DETAIL_NAME, &name,
   690 					   RAZOR_DETAIL_VERSION, &version,
   691 					   RAZOR_DETAIL_ARCH, &arch,
   692 					   RAZOR_DETAIL_LAST)) {
   693 		snprintf(url, sizeof url,
   694 			 "%s/Packages/%s",
   695 			 yum_url, rpm_filename(name, version, arch));
   696 		snprintf(file, sizeof file,
   697 			 "rpms/%s", rpm_filename(name, version, arch));
   698 		if (download_if_missing(url, file) < 0)
   699 			errors++;
   700 	}
   701 	razor_package_iterator_destroy(pi);
   702 
   703 	if (errors > 0) {
   704 		fprintf(stderr, "failed to download %d packages\n", errors);
   705                 return -1;
   706         }
   707 
   708 	return 0;
   709 }
   710 
   711 static int
   712 install_packages(struct razor_set *system, struct razor_set *next)
   713 {
   714 	struct razor_package_iterator *pi;
   715 	struct razor_package *package;
   716 	struct razor_rpm *rpm;
   717 	const char *name, *version, *arch;
   718 	char file[PATH_MAX];
   719 
   720 	pi = razor_set_create_install_iterator(system, next);
   721 	while (razor_package_iterator_next(pi, &package,
   722 					   RAZOR_DETAIL_NAME, &name,
   723 					   RAZOR_DETAIL_VERSION, &version,
   724 					   RAZOR_DETAIL_ARCH, &arch,
   725 					   RAZOR_DETAIL_LAST)) {
   726 		printf("install %s-%s\n", name, version);
   727 
   728 		snprintf(file, sizeof file,
   729 			 "rpms/%s", rpm_filename(name, version, arch));
   730 		rpm = razor_rpm_open(file);
   731 		if (rpm == NULL) {
   732 			fprintf(stderr, "failed to open rpm %s\n", file);
   733 			return -1;
   734 		}
   735 		if (razor_rpm_install(rpm, install_root) < 0) {
   736 			fprintf(stderr,
   737 				"failed to install rpm %s\n", file);
   738 			return -1;
   739 		}
   740 		razor_rpm_close(rpm);
   741 	}
   742 	razor_package_iterator_destroy(pi);
   743 
   744 	return 0;
   745 }
   746 
   747 static int
   748 command_install(int argc, const char *argv[])
   749 {
   750 	struct razor_root *root;
   751 	struct razor_set *system, *upstream, *next;
   752 	struct razor_transaction *trans;
   753 	int i = 0, dependencies = 1;
   754 
   755 	if (i < argc && strcmp(argv[i], "--no-dependencies") == 0) {
   756 		dependencies = 0;
   757 		i++;
   758 	}
   759 
   760 	root = razor_root_open(install_root);
   761 	if (root == NULL)
   762 		return 1;
   763 
   764 	system = razor_root_get_system_set(root);
   765 	upstream = razor_set_open(rawhide_repo_filename);
   766 	trans = razor_transaction_create(system, upstream);
   767 
   768 	for (; i < argc; i++) {
   769 		if (mark_packages_for_update(trans, upstream, argv[i]) == 0) {
   770 			fprintf(stderr, "no package matched %s\n", argv[i]);
   771 			razor_root_close(root);
   772 			return 1;
   773 		}
   774 	}
   775 
   776 	if (dependencies) {
   777 		razor_transaction_resolve(trans);
   778 		if (razor_transaction_describe(trans) > 0) {
   779 			razor_root_close(root);
   780 			return 1;
   781 		}
   782 	}
   783 
   784 	next = razor_transaction_finish(trans);
   785 
   786 	razor_root_update(root, next);
   787 
   788 	if (mkdir("rpms", 0777) && errno != EEXIST) {
   789 		fprintf(stderr, "failed to create rpms directory.\n");
   790 		razor_root_close(root);
   791 		return 1;
   792 	}
   793 
   794 	if (download_packages(system, next) < 0) {
   795 		razor_root_close(root);
   796                 return 1;
   797         }
   798 
   799 	install_packages(system, next);
   800 
   801 	razor_set_destroy(next);
   802 	razor_set_destroy(upstream);
   803 
   804 	return razor_root_commit(root);
   805 }
   806 
   807 static int
   808 command_init(int argc, const char *argv[])
   809 {
   810 	return razor_root_create(install_root);
   811 }
   812 
   813 static int
   814 command_download(int argc, const char *argv[])
   815 {
   816 	struct razor_set *set;
   817 	struct razor_package_iterator *pi;
   818 	struct razor_package *package;
   819 	const char *pattern = argv[0], *name, *version, *arch;
   820 	char url[256], file[256];
   821 	int matches = 0;
   822 
   823 	if (mkdir("rpms", 0777) && errno != EEXIST) {
   824 		fprintf(stderr, "failed to create rpms directory.\n");
   825 		return 1;
   826 	}
   827 
   828 	set = razor_set_open(rawhide_repo_filename);
   829 	pi = razor_package_iterator_create(set);
   830 	while (razor_package_iterator_next(pi, &package,
   831 					   RAZOR_DETAIL_NAME, &name,
   832 					   RAZOR_DETAIL_VERSION, &version,
   833 					   RAZOR_DETAIL_ARCH, &arch,
   834 					   RAZOR_DETAIL_LAST)) {
   835 		if (pattern && fnmatch(pattern, name, 0) != 0)
   836 			continue;
   837 
   838 		matches++;
   839 		snprintf(url, sizeof url,
   840 			 "%s/Packages/%s-%s.%s.rpm",
   841 			 yum_url, name, version, arch);
   842 		snprintf(file, sizeof file,
   843 			 "rpms/%s-%s.%s.rpm", name, version, arch);
   844 		download_if_missing(url, file);
   845 	}
   846 	razor_package_iterator_destroy(pi);
   847 	razor_set_destroy(set);
   848 
   849 	if (matches == 0)
   850 		fprintf(stderr, "no packages matched \"%s\"\n", pattern);
   851 	else if (matches == 1)
   852 		fprintf(stderr, "downloaded 1 package\n");
   853 	else
   854 		fprintf(stderr, "downloaded %d packages\n", matches);
   855 
   856 	return 0;
   857 }
   858 
   859 static int
   860 command_info(int argc, const char *argv[])
   861 {
   862 	struct razor_set *set;
   863 	struct razor_package_iterator *pi;
   864 	struct razor_package *package;
   865 	const char *pattern = argv[0], *name, *version, *arch;
   866 	const char *summary, *description, *url, *license;
   867 
   868 	set = razor_set_open(repo_filename);
   869 	if (set == NULL)
   870 		return 1;
   871 	if (razor_set_open_details(set, "system-details.repo"))
   872 		return 1;
   873 	pi = razor_package_iterator_create(set);
   874 	while (razor_package_iterator_next(pi, &package,
   875 					   RAZOR_DETAIL_NAME, &name,
   876 					   RAZOR_DETAIL_VERSION, &version,
   877 					   RAZOR_DETAIL_ARCH, &arch,
   878 					   RAZOR_DETAIL_LAST)) {
   879 		if (pattern && fnmatch(pattern, name, 0) != 0)
   880 			continue;
   881 
   882 		razor_package_get_details (set, package,
   883 					   RAZOR_DETAIL_SUMMARY, &summary,
   884 					   RAZOR_DETAIL_DESCRIPTION, &description,
   885 					   RAZOR_DETAIL_URL, &url,
   886 					   RAZOR_DETAIL_LICENSE, &license,
   887 					   RAZOR_DETAIL_LAST);
   888 
   889 		printf ("Name:        %s\n", name);
   890 		printf ("Arch:        %s\n", arch);
   891 		printf ("Version:     %s\n", version);
   892 		printf ("URL:         %s\n", url);
   893 		printf ("License:     %s\n", license);
   894 		printf ("Summary:     %s\n", summary);
   895 		printf ("Description:\n");
   896 		printf ("%s\n", description);
   897 		printf ("\n");
   898 	}
   899 	razor_package_iterator_destroy(pi);
   900 	razor_set_destroy(set);
   901 
   902 	return 0;
   903 }
   904 
   905 #define SEARCH_MAX 256
   906 
   907 static int
   908 command_search(int argc, const char *argv[])
   909 {
   910 	struct razor_set *set;
   911 	struct razor_package_iterator *pi;
   912 	struct razor_package *package;
   913 	char pattern[SEARCH_MAX];
   914 	const char *name, *version, *arch;
   915 	const char *summary, *description, *url, *license;
   916 
   917 	if (!argv[0]) {
   918 		fprintf(stderr, "must specify a search term\n");
   919 		return 1;
   920 	}
   921 
   922 	snprintf(pattern, sizeof pattern, "*%s*", argv[0]);
   923 
   924 	set = razor_set_open(rawhide_repo_filename);
   925 	if (set == NULL)
   926 		return 1;
   927 	if (razor_set_open_details(set, "rawhide-details.repo"))
   928 		return 1;
   929 
   930 	pi = razor_package_iterator_create(set);
   931 	while (razor_package_iterator_next(pi, &package,
   932 					   RAZOR_DETAIL_NAME, &name,
   933 					   RAZOR_DETAIL_VERSION, &version,
   934 					   RAZOR_DETAIL_ARCH, &arch,
   935 					   RAZOR_DETAIL_SUMMARY, &summary,
   936 					   RAZOR_DETAIL_DESCRIPTION, &description,
   937 					   RAZOR_DETAIL_URL, &url,
   938 					   RAZOR_DETAIL_LICENSE, &license,
   939 					   RAZOR_DETAIL_LAST)) {
   940 		if (!fnmatch(pattern, name, FNM_CASEFOLD) ||
   941 		    !fnmatch(pattern, url, FNM_CASEFOLD) ||
   942 		    !fnmatch(pattern, summary, FNM_CASEFOLD) ||
   943 		    !fnmatch(pattern, description, FNM_CASEFOLD))
   944 			printf("%s-%s.%s: %s\n", name, version, arch, summary);
   945 	}
   946 	razor_package_iterator_destroy(pi);
   947 	razor_set_destroy(set);
   948 
   949 	return 0;
   950 }
   951 
   952 static struct {
   953 	const char *name;
   954 	const char *description;
   955 	int (*func)(int argc, const char *argv[]);
   956 } razor_commands[] = {
   957 	{ "list", "list all packages", command_list },
   958 	{ "list-requires", "list all requires for the given package", command_list_requires },
   959 	{ "list-provides", "list all provides for the given package", command_list_provides },
   960 	{ "list-obsoletes", "list all obsoletes for the given package", command_list_obsoletes },
   961 	{ "list-conflicts", "list all conflicts for the given package", command_list_conflicts },
   962 	{ "list-files", "list files for package set", command_list_files },
   963 	{ "list-file-packages", "list packages owning file", command_list_file_packages },
   964 	{ "list-package-files", "list files in package", command_list_package_files },
   965 	{ "what-requires", "list the packages that have the given requires", command_what_requires },
   966 	{ "what-provides", "list the packages that have the given provides", command_what_provides },
   967 	{ "import-yum", "import yum metadata files", command_import_yum },
   968 	{ "import-rpmdb", "import the system rpm database", command_import_rpmdb },
   969 	{ "import-rpms", "import rpms from the given directory", command_import_rpms },
   970 	{ "update", "update all or specified packages", command_update },
   971 	{ "remove", "remove specified packages", command_remove },
   972 	{ "diff", "show diff between two package sets", command_diff },
   973 	{ "install", "install rpm", command_install },
   974 	{ "init", "init razor root", command_init },
   975 	{ "download", "download packages", command_download },
   976 	{ "info", "display package details", command_info },
   977 	{ "search", "search package details", command_search }
   978 };
   979 
   980 static int
   981 usage(void)
   982 {
   983 	int i;
   984 
   985 	printf("usage:\n");
   986 	for (i = 0; i < ARRAY_SIZE(razor_commands); i++)
   987 		printf("  %-20s%s\n",
   988 		       razor_commands[i].name, razor_commands[i].description);
   989 
   990 	return 1;
   991 }
   992 
   993 int
   994 main(int argc, const char *argv[])
   995 {
   996 	char *repo;
   997 	int i;
   998 
   999 	repo = getenv("RAZOR_REPO");
  1000 	if (repo != NULL)
  1001 		repo_filename = repo;
  1002 
  1003 	yum_url = getenv("YUM_URL");
  1004 	if (yum_url == NULL)
  1005 		yum_url = YUM_URL;
  1006 
  1007 	if (argc < 2)
  1008 		return usage();
  1009 
  1010 	for (i = 0; i < ARRAY_SIZE(razor_commands); i++)
  1011 		if (strcmp(razor_commands[i].name, argv[1]) == 0)
  1012 			return razor_commands[i].func(argc - 2, argv + 2);
  1013 
  1014 	return usage();
  1015 }