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