src/main.c
author James Bowes <jbowes@redhat.com>
Mon Jun 23 14:51:56 2008 -0400 (2008-06-23)
changeset 264 634e54ca476c
parent 259 5b0601d184ed
child 271 3980d1d9148e
permissions -rw-r--r--
Actually store the files strings in the files string pool
     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, imported_count = 0;
   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 		printf("\rimporting %d", ++imported_count);
   574 		fflush(stdout);
   575 	}
   576 
   577 	if (de != NULL) {
   578 		razor_importer_destroy(importer);
   579 		return -1;
   580 	}
   581 
   582 	printf("\nsaving\n");
   583 	set = razor_importer_finish(importer);
   584 
   585 	razor_set_write(set, repo_filename, RAZOR_REPO_FILE_MAIN);
   586 	razor_set_destroy(set);
   587 	printf("wrote %s\n", repo_filename);
   588 
   589 	return 0;
   590 }
   591 
   592 static const char *
   593 rpm_filename(const char *name, const char *version, const char *arch)
   594 {
   595 	static char file[PATH_MAX];
   596  	const char *v;
   597  
   598  	/* Skip epoch */
   599 	v = strchr(version, ':');
   600  	if (v != NULL)
   601  		v = v + 1;
   602  	else
   603 		v = version;
   604 
   605 	snprintf(file, sizeof file, "%s-%s.%s.rpm", name, v, arch);
   606 
   607 	return file;
   608 }
   609 
   610 static int
   611 download_packages(struct razor_set *system, struct razor_set *next)
   612 {
   613 	struct razor_package_iterator *pi;
   614 	struct razor_package *package;
   615 	const char *name, *version, *arch;
   616 	char file[PATH_MAX], url[256];
   617 	int errors;
   618  
   619 	pi = razor_set_create_install_iterator(system, next);
   620 	errors = 0;
   621 	while (razor_package_iterator_next(pi, &package,
   622 					   &name, &version, &arch)) {
   623 		snprintf(url, sizeof url,
   624 			 "%s/Packages/%s",
   625 			 yum_url, rpm_filename(name, version, arch));
   626 		snprintf(file, sizeof file,
   627 			 "rpms/%s", rpm_filename(name, version, arch));
   628 		if (download_if_missing(url, file) < 0)
   629 			errors++;
   630 	}
   631 	razor_package_iterator_destroy(pi);
   632 
   633 	if (errors > 0) {
   634 		fprintf(stderr, "failed to download %d packages\n", errors);
   635                 return -1;
   636         }
   637 
   638 	return 0;
   639 }
   640 
   641 static int
   642 install_packages(struct razor_set *system, struct razor_set *next)
   643 {
   644 	struct razor_package_iterator *pi;
   645 	struct razor_package *package;
   646 	struct razor_rpm *rpm;
   647 	const char *name, *version, *arch;
   648 	char file[PATH_MAX];
   649 
   650 	pi = razor_set_create_install_iterator(system, next);
   651 	while (razor_package_iterator_next(pi, &package,
   652 					   &name, &version, &arch)) {
   653 		printf("install %s-%s\n", name, version);
   654 
   655 		snprintf(file, sizeof file,
   656 			 "rpms/%s", rpm_filename(name, version, arch));
   657 		rpm = razor_rpm_open(file);
   658 		if (rpm == NULL) {
   659 			fprintf(stderr, "failed to open rpm %s\n", file);
   660 			return -1;
   661 		}
   662 		if (razor_rpm_install(rpm, install_root) < 0) {
   663 			fprintf(stderr,
   664 				"failed to install rpm %s\n", file);
   665 			return -1;
   666 		}
   667 		razor_rpm_close(rpm);
   668 	}
   669 	razor_package_iterator_destroy(pi);
   670 
   671 	return 0;
   672 }
   673 
   674 static int
   675 command_install(int argc, const char *argv[])
   676 {
   677 	struct razor_root *root;
   678 	struct razor_set *system, *upstream, *next;
   679 	struct razor_transaction *trans;
   680 	int i = 0, errors, dependencies = 1;
   681 
   682 	if (i < argc && strcmp(argv[i], "--no-dependencies") == 0) {
   683 		dependencies = 0;
   684 		i++;
   685 	}
   686 
   687 	root = razor_root_open(install_root);
   688 	if (root == NULL)
   689 		return 1;
   690 
   691 	system = razor_root_get_system_set(root);
   692 	upstream = razor_set_open(rawhide_repo_filename);
   693 	trans = razor_transaction_create(system, upstream);
   694 
   695 	for (; i < argc; i++) {
   696 		if (mark_packages_for_update(trans, upstream, argv[i]) == 0) {
   697 			fprintf(stderr, "no package matched %s\n", argv[i]);
   698 			razor_root_close(root);
   699 			return 1;
   700 		}
   701 	}
   702 
   703 	if (dependencies) {
   704 		razor_transaction_resolve(trans);
   705 		if (razor_transaction_describe(trans) > 0) {
   706 			razor_root_close(root);
   707 			return 1;
   708 		}
   709 	}
   710 
   711 	next = razor_transaction_finish(trans);
   712 
   713 	razor_root_update(root, next);
   714 
   715 	if (mkdir("rpms", 0777) && errno != EEXIST) {
   716 		fprintf(stderr, "failed to create rpms directory.\n");
   717 		razor_root_close(root);
   718 		return 1;
   719 	}
   720 
   721 	if (download_packages(system, next) < 0) {
   722 		razor_root_close(root);
   723                 return 1;
   724         }
   725 
   726 	install_packages(system, next);
   727 
   728 	razor_set_destroy(next);
   729 	razor_set_destroy(upstream);
   730 
   731 	return razor_root_commit(root);
   732 }
   733 
   734 static int
   735 command_init(int argc, const char *argv[])
   736 {
   737 	return razor_root_create(install_root);
   738 }
   739 
   740 static int
   741 command_download(int argc, const char *argv[])
   742 {
   743 	struct razor_set *set;
   744 	struct razor_package_iterator *pi;
   745 	struct razor_package *package;
   746 	const char *pattern = argv[0], *name, *version, *arch;
   747 	char url[256], file[256];
   748 	int matches = 0;
   749 
   750 	if (mkdir("rpms", 0777) && errno != EEXIST) {
   751 		fprintf(stderr, "failed to create rpms directory.\n");
   752 		return 1;
   753 	}
   754 
   755 	set = razor_set_open(rawhide_repo_filename);
   756 	pi = razor_package_iterator_create(set);
   757 	while (razor_package_iterator_next(pi, &package,
   758 					   &name, &version, &arch)) {
   759 		if (pattern && fnmatch(pattern, name, 0) != 0)
   760 			continue;
   761 
   762 		matches++;
   763 		snprintf(url, sizeof url,
   764 			 "%s/Packages/%s-%s.%s.rpm",
   765 			 yum_url, name, version, arch);
   766 		snprintf(file, sizeof file,
   767 			 "rpms/%s-%s.%s.rpm", name, version, arch);
   768 		download_if_missing(url, file);
   769 	}
   770 	razor_package_iterator_destroy(pi);
   771 	razor_set_destroy(set);
   772 
   773 	if (matches == 0)
   774 		fprintf(stderr, "no packages matched \"%s\"\n", pattern);
   775 	else if (matches == 1)
   776 		fprintf(stderr, "downloaded 1 package\n");
   777 	else
   778 		fprintf(stderr, "downloaded %d packages\n", matches);
   779 
   780 	return 0;
   781 }
   782 
   783 static int
   784 command_info(int argc, const char *argv[])
   785 {
   786 	struct razor_set *set;
   787 	struct razor_package_iterator *pi;
   788 	struct razor_package *package;
   789 	const char *pattern = argv[0], *name, *version, *arch;
   790 	const char *summary, *description, *url, *license;
   791 
   792 	set = razor_set_open(repo_filename);
   793 	razor_set_open_details(set, "system-details.repo");
   794 	pi = razor_package_iterator_create(set);
   795 	while (razor_package_iterator_next(pi, &package,
   796 					   &name, &version, &arch)) {
   797 		if (pattern && fnmatch(pattern, name, 0) != 0)
   798 			continue;
   799 
   800 		razor_package_get_details (set, package, &summary, &description,
   801 					   &url, &license);
   802 
   803 		printf ("Name:        %s\n", name);
   804 		printf ("Arch:        %s\n", arch);
   805 		printf ("Version:     %s\n", version);
   806 		printf ("URL:         %s\n", url);
   807 		printf ("License:     %s\n", license);
   808 		printf ("Summary:     %s\n", summary);
   809 		printf ("Description:\n");
   810 		printf ("%s\n", description);
   811 		printf ("\n");
   812 	}
   813 	razor_package_iterator_destroy(pi);
   814 	razor_set_destroy(set);
   815 
   816 	return 0;
   817 }
   818 
   819 static struct {
   820 	const char *name;
   821 	const char *description;
   822 	int (*func)(int argc, const char *argv[]);
   823 } razor_commands[] = {
   824 	{ "list", "list all packages", command_list },
   825 	{ "list-requires", "list all requires for the given package", command_list_requires },
   826 	{ "list-provides", "list all provides for the given package", command_list_provides },
   827 	{ "list-obsoletes", "list all obsoletes for the given package", command_list_obsoletes },
   828 	{ "list-conflicts", "list all conflicts for the given package", command_list_conflicts },
   829 	{ "list-files", "list files for package set", command_list_files },
   830 	{ "list-file-packages", "list packages owning file", command_list_file_packages },
   831 	{ "list-package-files", "list files in package", command_list_package_files },
   832 	{ "what-requires", "list the packages that have the given requires", command_what_requires },
   833 	{ "what-provides", "list the packages that have the given provides", command_what_provides },
   834 	{ "import-yum", "import yum metadata files", command_import_yum },
   835 	{ "import-rpmdb", "import the system rpm database", command_import_rpmdb },
   836 	{ "import-rpms", "import rpms from the given directory", command_import_rpms },
   837 	{ "update", "update all or specified packages", command_update },
   838 	{ "remove", "remove specified packages", command_remove },
   839 	{ "diff", "show diff between two package sets", command_diff },
   840 	{ "install", "install rpm", command_install },
   841 	{ "init", "init razor root", command_init },
   842 	{ "download", "download packages", command_download },
   843 	{ "info", "display package details", command_info }
   844 };
   845 
   846 static int
   847 usage(void)
   848 {
   849 	int i;
   850 
   851 	printf("usage:\n");
   852 	for (i = 0; i < ARRAY_SIZE(razor_commands); i++)
   853 		printf("  %-20s%s\n",
   854 		       razor_commands[i].name, razor_commands[i].description);
   855 
   856 	return 1;
   857 }
   858 
   859 int
   860 main(int argc, const char *argv[])
   861 {
   862 	char *repo;
   863 	int i;
   864 
   865 	repo = getenv("RAZOR_REPO");
   866 	if (repo != NULL)
   867 		repo_filename = repo;
   868 
   869 	yum_url = getenv("YUM_URL");
   870 	if (yum_url == NULL)
   871 		yum_url = YUM_URL;
   872 
   873 	if (argc < 2)
   874 		return usage();
   875 
   876 	for (i = 0; i < ARRAY_SIZE(razor_commands); i++)
   877 		if (strcmp(razor_commands[i].name, argv[1]) == 0)
   878 			return razor_commands[i].func(argc - 2, argv + 2);
   879 
   880 	return usage();
   881 }