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