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