src/main.c
author Richard Hughes <richard@hughsie.com>
Wed Jun 25 11:00:25 2008 -0400 (2008-06-25)
changeset 280 25407a621c95
parent 271 3980d1d9148e
child 281 d8d248598628
permissions -rw-r--r--
Error out if package isn't found instead of listing all properties.

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