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