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