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