src/main.c
author Richard Hughes <richard@hughsie.com>
Mon Jun 30 08:51:26 2008 +0100 (2008-06-30)
changeset 302 9b71b537d175
parent 294 d7ecddf38c3d
child 304 bf23ba00db03
permissions -rw-r--r--
convert razor_package_get_details() and razor_package_iterator_next() to varargs

The functions for getting package details about a package were limited to a few
things, when in the future we will want to support much more about a package.
The iterator was also limited to name,version,arch when most of the time we
didn't need all this data.
     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, *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, RAZOR_DETAIL_NAME, &name, 0)) {
    63 			if (fnmatch(pattern, name, 0) != 0)
    64 				continue;
    65 
    66 			razor_package_query_add_package(query, package);
    67 			count++;
    68 		}
    69 		razor_package_iterator_destroy(iter);
    70 
    71 		if (count == 0)
    72 			fprintf(stderr,
    73 				"no package matches \"%s\"\n", pattern);
    74 	}
    75 
    76 	return razor_package_query_finish(query);
    77 }
    78 
    79 #define LIST_PACKAGES_ONLY_NAMES 0x01
    80 
    81 static void
    82 list_packages(struct razor_package_iterator *iter, uint32_t flags)
    83 {
    84 	struct razor_package *package;
    85 	const char *name, *version, *arch;
    86 
    87 	while (razor_package_iterator_next(iter, &package,
    88 					   RAZOR_DETAIL_NAME, &name,
    89 					   RAZOR_DETAIL_VERSION, &version,
    90 					   RAZOR_DETAIL_ARCH, &arch, 0)) {
    91 		if (flags & LIST_PACKAGES_ONLY_NAMES)
    92 			printf("%s\n", name);
    93 		else
    94 			printf("%s-%s.%s\n", name, version, arch);
    95 	}
    96 }
    97 
    98 static int
    99 command_list(int argc, const char *argv[])
   100 {
   101 	struct razor_package_iterator *pi;
   102 	struct razor_set *set;
   103 	uint32_t flags = 0;
   104 	int i = 0;
   105 
   106 	if (i < argc && strcmp(argv[i], "--only-names") == 0) {
   107 		flags |= LIST_PACKAGES_ONLY_NAMES;
   108 		i++;
   109 	}
   110 
   111 	set = razor_set_open(repo_filename);
   112 	pi = create_iterator_from_argv(set, argc - i, argv + i);
   113 	list_packages(pi, flags);
   114 	razor_package_iterator_destroy(pi);
   115 	razor_set_destroy(set);
   116 
   117 	return 0;
   118 }
   119 
   120 static int
   121 list_properties(const char *package_name, uint32_t type)
   122 {
   123 	struct razor_set *set;
   124 	struct razor_property *property;
   125 	struct razor_package *package;
   126 	struct razor_property_iterator *pi;
   127 	const char *name, *version;
   128 	uint32_t flags;
   129 
   130 	set = razor_set_open(repo_filename);
   131 	if (package_name) {
   132 		package = razor_set_get_package(set, package_name);
   133 		if (!package) {
   134 			fprintf(stderr, "no package named \"%s\"\n", package_name);
   135 			return 1;
   136 		}
   137 	} else
   138 		package = NULL;
   139 
   140 	pi = razor_property_iterator_create(set, package);
   141 	while (razor_property_iterator_next(pi, &property,
   142 					    &name, &flags, &version)) {
   143 		if ((flags & RAZOR_PROPERTY_TYPE_MASK) != type)
   144 			continue;
   145 		printf("%s", name);
   146 		if (version[0] != '\0')
   147 			printf(" %s %s",
   148 			       razor_property_relation_to_string(property),
   149 			       version);
   150 
   151 		if (flags & ~(RAZOR_PROPERTY_RELATION_MASK | RAZOR_PROPERTY_TYPE_MASK)) {
   152 			printf(" [");
   153 			if (flags & RAZOR_PROPERTY_PRE)
   154 				printf(" pre");
   155 			if (flags & RAZOR_PROPERTY_POST)
   156 				printf(" post");
   157 			if (flags & RAZOR_PROPERTY_PREUN)
   158 				printf(" preun");
   159 			if (flags & RAZOR_PROPERTY_POSTUN)
   160 				printf(" postun");
   161 			printf(" ]");
   162 		}
   163 		printf("\n");
   164 	}
   165 	razor_property_iterator_destroy(pi);
   166 
   167 	razor_set_destroy(set);
   168 
   169 	return 0;
   170 }
   171 
   172 static int
   173 command_list_requires(int argc, const char *argv[])
   174 {
   175 	return list_properties(argv[0], RAZOR_PROPERTY_REQUIRES);
   176 }
   177 
   178 static int
   179 command_list_provides(int argc, const char *argv[])
   180 {
   181 	return list_properties(argv[0], RAZOR_PROPERTY_PROVIDES);
   182 }
   183 
   184 static int
   185 command_list_obsoletes(int argc, const char *argv[])
   186 {
   187 	return list_properties(argv[0], RAZOR_PROPERTY_OBSOLETES);
   188 }
   189 
   190 static int
   191 command_list_conflicts(int argc, const char *argv[])
   192 {
   193 	return list_properties(argv[0], RAZOR_PROPERTY_CONFLICTS);
   194 }
   195 
   196 static int
   197 command_list_files(int argc, const char *argv[])
   198 {
   199 	struct razor_set *set;
   200 
   201 	set = razor_set_open(repo_filename);
   202 	if (set == NULL)
   203 		return 1;
   204 	if (razor_set_open_files(set, "system-files.repo"))
   205 		return 1;
   206 
   207 	razor_set_list_files(set, argv[0]);
   208 	razor_set_destroy(set);
   209 
   210 	return 0;
   211 }
   212 
   213 static int
   214 command_list_file_packages(int argc, const char *argv[])
   215 {
   216 	struct razor_set *set;
   217 	struct razor_package_iterator *pi;
   218 
   219 	set = razor_set_open(repo_filename);
   220 	if (set == NULL)
   221 		return 1;
   222 	if (razor_set_open_files(set, "system-files.repo"))
   223 		return 1;
   224 
   225 	pi = razor_package_iterator_create_for_file(set, argv[0]);
   226 	list_packages(pi, 0);
   227 	razor_package_iterator_destroy(pi);
   228 
   229 	razor_set_destroy(set);
   230 
   231 	return 0;
   232 }
   233 
   234 static int
   235 command_list_package_files(int argc, const char *argv[])
   236 {
   237 	struct razor_set *set;
   238 
   239 	set = razor_set_open(repo_filename);
   240 	if (set == NULL)
   241 		return 1;
   242 	if (razor_set_open_files(set, "system-files.repo"))
   243 		return 1;
   244 
   245 	razor_set_list_package_files(set, argv[0]);
   246 	razor_set_destroy(set);
   247 
   248 	return 0;
   249 }
   250 
   251 static int
   252 list_property_packages(const char *ref_name,
   253 		       const char *ref_version,
   254 		       uint32_t type)
   255 {
   256 	struct razor_set *set;
   257 	struct razor_property *property;
   258 	struct razor_property_iterator *prop_iter;
   259 	struct razor_package_iterator *pkg_iter;
   260 	const char *name, *version;
   261 	uint32_t flags;
   262 
   263 	if (ref_name == NULL)
   264 		return 0;
   265 
   266 	set = razor_set_open(repo_filename);
   267 	if (set == NULL)
   268 		return 1;
   269 
   270 	prop_iter = razor_property_iterator_create(set, NULL);
   271 	while (razor_property_iterator_next(prop_iter, &property,
   272 					    &name, &flags, &version)) {
   273 		if (strcmp(ref_name, name) != 0)
   274 			continue;
   275 		if (ref_version &&
   276 		    (flags & RAZOR_PROPERTY_RELATION_MASK) == RAZOR_PROPERTY_EQUAL &&
   277 		    strcmp(ref_version, version) != 0)
   278 			continue;
   279 		if ((flags & RAZOR_PROPERTY_TYPE_MASK) != type)
   280 			continue;
   281 
   282 		pkg_iter =
   283 			razor_package_iterator_create_for_property(set,
   284 								   property);
   285 		list_packages(pkg_iter, 0);
   286 		razor_package_iterator_destroy(pkg_iter);
   287 	}
   288 	razor_property_iterator_destroy(prop_iter);
   289 
   290 	return 0;
   291 }
   292 
   293 static int
   294 command_what_requires(int argc, const char *argv[])
   295 {
   296 	return list_property_packages(argv[0], argv[1],
   297 				      RAZOR_PROPERTY_REQUIRES);
   298 }
   299 
   300 static int
   301 command_what_provides(int argc, const char *argv[])
   302 {
   303 	return list_property_packages(argv[0], argv[1],
   304 				      RAZOR_PROPERTY_PROVIDES);
   305 }
   306 
   307 static int
   308 show_progress(void *clientp,
   309 	      double dltotal, double dlnow, double ultotal, double ulnow)
   310 {
   311 	const char *file = clientp;
   312 
   313 	if (!dlnow < dltotal)
   314 		fprintf(stderr, "\rdownloading %s, %dkB/%dkB",
   315 			file, (int) dlnow / 1024, (int) dltotal / 1024);
   316 
   317 	return 0;
   318 }
   319 
   320 static int
   321 download_if_missing(const char *url, const char *file)
   322 {
   323 	CURL *curl;
   324 	struct stat buf;
   325 	char error[256];
   326 	FILE *fp;
   327 	CURLcode res;
   328 	long response;
   329 
   330 	curl = curl_easy_init();
   331 	if (curl == NULL)
   332 		return 1;
   333 
   334 	curl_easy_setopt(curl, CURLOPT_ERRORBUFFER, error);
   335 	curl_easy_setopt(curl, CURLOPT_NOPROGRESS, 0);
   336 	curl_easy_setopt(curl, CURLOPT_PROGRESSFUNCTION, show_progress);
   337 	curl_easy_setopt(curl, CURLOPT_PROGRESSDATA, file);
   338 
   339 	if (stat(file, &buf) < 0) {
   340 		fp = fopen(file, "w");
   341 		if (fp == NULL) {
   342 			fprintf(stderr,
   343 				"failed to open %s for writing\n", file);
   344 			return -1;
   345 		}
   346 		curl_easy_setopt(curl, CURLOPT_WRITEDATA, fp);
   347 		curl_easy_setopt(curl, CURLOPT_URL, url);
   348 		res = curl_easy_perform(curl);
   349 		fclose(fp);
   350 		if (res != CURLE_OK) {
   351 			fprintf(stderr, "curl error: %s\n", error);
   352 			unlink(file);
   353 			return -1;
   354 		}
   355 		res = curl_easy_getinfo(curl,
   356 					CURLINFO_RESPONSE_CODE, &response);
   357 		if (res != CURLE_OK) {
   358 			fprintf(stderr, "curl error: %s\n", error);
   359                         unlink(file);
   360                         return -1;
   361 		}
   362 		if (response != 200) {
   363 			fprintf(stderr, " - failed %ld\n", response);
   364                         unlink(file);
   365                         return -1;
   366 		}
   367 		fprintf(stderr, "\n");
   368 	}
   369 
   370 	curl_easy_cleanup(curl);
   371 
   372 	return 0;
   373 }
   374 
   375 #define YUM_URL "http://download.fedora.redhat.com" \
   376 	"/pub/fedora/linux/development/i386/os"
   377 
   378 static int
   379 command_import_yum(int argc, const char *argv[])
   380 {
   381 	struct razor_set *set;
   382 	char buffer[512];
   383 
   384 	printf("downloading from %s.\n", yum_url);
   385 	snprintf(buffer, sizeof buffer,
   386 		 "%s/repodata/primary.xml.gz", yum_url);
   387 	if (download_if_missing(buffer, "primary.xml.gz") < 0)
   388 		return -1;
   389 	snprintf(buffer, sizeof buffer,
   390 		 "%s/repodata/filelists.xml.gz", yum_url);
   391 	if (download_if_missing(buffer, "filelists.xml.gz") < 0)
   392 		return -1;
   393 
   394 	set = razor_set_create_from_yum();
   395 	if (set == NULL)
   396 		return 1;
   397 	razor_set_write(set, rawhide_repo_filename, RAZOR_REPO_FILE_MAIN);
   398 	razor_set_write(set, "rawhide-details.repo", RAZOR_REPO_FILE_DETAILS);
   399 	razor_set_write(set, "rawhide-files.repo", RAZOR_REPO_FILE_FILES);
   400 	razor_set_destroy(set);
   401 	printf("wrote %s\n", rawhide_repo_filename);
   402 
   403 	return 0;
   404 }
   405 
   406 static int
   407 command_import_rpmdb(int argc, const char *argv[])
   408 {
   409 	struct razor_set *set;
   410 
   411 	set = razor_set_create_from_rpmdb();
   412 	if (set == NULL)
   413 		return 1;
   414 	razor_set_write(set, repo_filename, RAZOR_REPO_FILE_MAIN);
   415 	razor_set_write(set, "system-details.repo", RAZOR_REPO_FILE_DETAILS);
   416 	razor_set_write(set, "system-files.repo", RAZOR_REPO_FILE_FILES);
   417 	razor_set_destroy(set);
   418 	printf("wrote %s\n", repo_filename);
   419 
   420 	return 0;
   421 }
   422 
   423 static int
   424 mark_packages_for_update(struct razor_transaction *trans,
   425 			 struct razor_set *set, const char *pattern)
   426 {
   427 	struct razor_package_iterator *pi;
   428 	struct razor_package *package;
   429 	const char *name;
   430 	int matches = 0;
   431 
   432 	pi = razor_package_iterator_create(set);
   433 	while (razor_package_iterator_next(pi, &package,
   434 					   RAZOR_DETAIL_NAME, &name, 0)) {
   435 		if (pattern && fnmatch(pattern, name, 0) == 0) {
   436 			razor_transaction_update_package(trans, package);
   437 			matches++;
   438 		}
   439 	}
   440 	razor_package_iterator_destroy(pi);
   441 
   442 	return matches;
   443 }
   444 
   445 static int
   446 mark_packages_for_removal(struct razor_transaction *trans,
   447 			  struct razor_set *set, const char *pattern)
   448 {
   449 	struct razor_package_iterator *pi;
   450 	struct razor_package *package;
   451 	const char *name;
   452 	int matches = 0;
   453 
   454 	pi = razor_package_iterator_create(set);
   455 	while (razor_package_iterator_next(pi, &package, RAZOR_DETAIL_NAME, &name, 0)) {
   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 					   RAZOR_DETAIL_NAME, &name,
   664 					   RAZOR_DETAIL_VERSION, &version,
   665 					   RAZOR_DETAIL_ARCH, &arch, 0)) {
   666 		snprintf(url, sizeof url,
   667 			 "%s/Packages/%s",
   668 			 yum_url, rpm_filename(name, version, arch));
   669 		snprintf(file, sizeof file,
   670 			 "rpms/%s", rpm_filename(name, version, arch));
   671 		if (download_if_missing(url, file) < 0)
   672 			errors++;
   673 	}
   674 	razor_package_iterator_destroy(pi);
   675 
   676 	if (errors > 0) {
   677 		fprintf(stderr, "failed to download %d packages\n", errors);
   678                 return -1;
   679         }
   680 
   681 	return 0;
   682 }
   683 
   684 static int
   685 install_packages(struct razor_set *system, struct razor_set *next)
   686 {
   687 	struct razor_package_iterator *pi;
   688 	struct razor_package *package;
   689 	struct razor_rpm *rpm;
   690 	const char *name, *version, *arch;
   691 	char file[PATH_MAX];
   692 
   693 	pi = razor_set_create_install_iterator(system, next);
   694 	while (razor_package_iterator_next(pi, &package,
   695 					   RAZOR_DETAIL_NAME, &name,
   696 					   RAZOR_DETAIL_VERSION, &version,
   697 					   RAZOR_DETAIL_ARCH, &arch, 0)) {
   698 		printf("install %s-%s\n", name, version);
   699 
   700 		snprintf(file, sizeof file,
   701 			 "rpms/%s", rpm_filename(name, version, arch));
   702 		rpm = razor_rpm_open(file);
   703 		if (rpm == NULL) {
   704 			fprintf(stderr, "failed to open rpm %s\n", file);
   705 			return -1;
   706 		}
   707 		if (razor_rpm_install(rpm, install_root) < 0) {
   708 			fprintf(stderr,
   709 				"failed to install rpm %s\n", file);
   710 			return -1;
   711 		}
   712 		razor_rpm_close(rpm);
   713 	}
   714 	razor_package_iterator_destroy(pi);
   715 
   716 	return 0;
   717 }
   718 
   719 static int
   720 command_install(int argc, const char *argv[])
   721 {
   722 	struct razor_root *root;
   723 	struct razor_set *system, *upstream, *next;
   724 	struct razor_transaction *trans;
   725 	int i = 0, dependencies = 1;
   726 
   727 	if (i < argc && strcmp(argv[i], "--no-dependencies") == 0) {
   728 		dependencies = 0;
   729 		i++;
   730 	}
   731 
   732 	root = razor_root_open(install_root);
   733 	if (root == NULL)
   734 		return 1;
   735 
   736 	system = razor_root_get_system_set(root);
   737 	upstream = razor_set_open(rawhide_repo_filename);
   738 	trans = razor_transaction_create(system, upstream);
   739 
   740 	for (; i < argc; i++) {
   741 		if (mark_packages_for_update(trans, upstream, argv[i]) == 0) {
   742 			fprintf(stderr, "no package matched %s\n", argv[i]);
   743 			razor_root_close(root);
   744 			return 1;
   745 		}
   746 	}
   747 
   748 	if (dependencies) {
   749 		razor_transaction_resolve(trans);
   750 		if (razor_transaction_describe(trans) > 0) {
   751 			razor_root_close(root);
   752 			return 1;
   753 		}
   754 	}
   755 
   756 	next = razor_transaction_finish(trans);
   757 
   758 	razor_root_update(root, next);
   759 
   760 	if (mkdir("rpms", 0777) && errno != EEXIST) {
   761 		fprintf(stderr, "failed to create rpms directory.\n");
   762 		razor_root_close(root);
   763 		return 1;
   764 	}
   765 
   766 	if (download_packages(system, next) < 0) {
   767 		razor_root_close(root);
   768                 return 1;
   769         }
   770 
   771 	install_packages(system, next);
   772 
   773 	razor_set_destroy(next);
   774 	razor_set_destroy(upstream);
   775 
   776 	return razor_root_commit(root);
   777 }
   778 
   779 static int
   780 command_init(int argc, const char *argv[])
   781 {
   782 	return razor_root_create(install_root);
   783 }
   784 
   785 static int
   786 command_download(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 	char url[256], file[256];
   793 	int matches = 0;
   794 
   795 	if (mkdir("rpms", 0777) && errno != EEXIST) {
   796 		fprintf(stderr, "failed to create rpms directory.\n");
   797 		return 1;
   798 	}
   799 
   800 	set = razor_set_open(rawhide_repo_filename);
   801 	pi = razor_package_iterator_create(set);
   802 	while (razor_package_iterator_next(pi, &package,
   803 					   RAZOR_DETAIL_NAME, &name,
   804 					   RAZOR_DETAIL_VERSION, &version,
   805 					   RAZOR_DETAIL_ARCH, &arch, 0)) {
   806 		if (pattern && fnmatch(pattern, name, 0) != 0)
   807 			continue;
   808 
   809 		matches++;
   810 		snprintf(url, sizeof url,
   811 			 "%s/Packages/%s-%s.%s.rpm",
   812 			 yum_url, name, version, arch);
   813 		snprintf(file, sizeof file,
   814 			 "rpms/%s-%s.%s.rpm", name, version, arch);
   815 		download_if_missing(url, file);
   816 	}
   817 	razor_package_iterator_destroy(pi);
   818 	razor_set_destroy(set);
   819 
   820 	if (matches == 0)
   821 		fprintf(stderr, "no packages matched \"%s\"\n", pattern);
   822 	else if (matches == 1)
   823 		fprintf(stderr, "downloaded 1 package\n");
   824 	else
   825 		fprintf(stderr, "downloaded %d packages\n", matches);
   826 
   827 	return 0;
   828 }
   829 
   830 static int
   831 command_info(int argc, const char *argv[])
   832 {
   833 	struct razor_set *set;
   834 	struct razor_package_iterator *pi;
   835 	struct razor_package *package;
   836 	const char *pattern = argv[0], *name, *version, *arch;
   837 	const char *summary, *description, *url, *license;
   838 
   839 	set = razor_set_open(repo_filename);
   840 	if (set == NULL)
   841 		return 1;
   842 	if (razor_set_open_details(set, "system-details.repo"))
   843 		return 1;
   844 	pi = razor_package_iterator_create(set);
   845 	while (razor_package_iterator_next(pi, &package,
   846 					   RAZOR_DETAIL_NAME, &name,
   847 					   RAZOR_DETAIL_VERSION, &version,
   848 					   RAZOR_DETAIL_ARCH, &arch, 0)) {
   849 		if (pattern && fnmatch(pattern, name, 0) != 0)
   850 			continue;
   851 
   852 		razor_package_get_details (set, package,
   853 					   RAZOR_DETAIL_SUMMARY, &summary,
   854 					   RAZOR_DETAIL_DESCRIPTION, &description,
   855 					   RAZOR_DETAIL_URL, &url,
   856 					   RAZOR_DETAIL_LICENSE, &license,
   857 					   0);
   858 
   859 		printf ("Name:        %s\n", name);
   860 		printf ("Arch:        %s\n", arch);
   861 		printf ("Version:     %s\n", version);
   862 		printf ("URL:         %s\n", url);
   863 		printf ("License:     %s\n", license);
   864 		printf ("Summary:     %s\n", summary);
   865 		printf ("Description:\n");
   866 		printf ("%s\n", description);
   867 		printf ("\n");
   868 	}
   869 	razor_package_iterator_destroy(pi);
   870 	razor_set_destroy(set);
   871 
   872 	return 0;
   873 }
   874 
   875 #define SEARCH_MAX 256
   876 
   877 static int
   878 command_search(int argc, const char *argv[])
   879 {
   880 	struct razor_set *set;
   881 	struct razor_package_iterator *pi;
   882 	struct razor_package *package;
   883 	char pattern[SEARCH_MAX];
   884 	const char *name, *version, *arch;
   885 	const char *summary, *description, *url, *license;
   886 
   887 	if (!argv[0]) {
   888 		fprintf(stderr, "must specify a search term\n");
   889 		return 1;
   890 	}
   891 
   892 	snprintf(pattern, sizeof pattern, "*%s*", argv[0]);
   893 
   894 	set = razor_set_open(repo_filename);
   895 	if (set == NULL)
   896 		return 1;
   897 	if (razor_set_open_details(set, "system-details.repo"))
   898 		return 1;
   899 
   900 	pi = razor_package_iterator_create(set);
   901 	while (razor_package_iterator_next(pi, &package,
   902 					   &name, &version, &arch)) {
   903 		if (!fnmatch(pattern, name, 0))
   904 			printf("%s-%s.%s\n", name, version, arch);
   905 		else {
   906 			razor_package_get_details (set, package, &summary,
   907 						   &description, &url,
   908 						   &license);
   909 			if (!fnmatch(pattern, url, 0) ||
   910 			    !fnmatch(pattern, summary, 0) ||
   911 			    !fnmatch(pattern, description, 0))
   912 				printf("%s-%s.%s\n", name, version, arch);
   913 		}
   914 	}
   915 	razor_package_iterator_destroy(pi);
   916 	razor_set_destroy(set);
   917 
   918 	return 0;
   919 }
   920 
   921 static struct {
   922 	const char *name;
   923 	const char *description;
   924 	int (*func)(int argc, const char *argv[]);
   925 } razor_commands[] = {
   926 	{ "list", "list all packages", command_list },
   927 	{ "list-requires", "list all requires for the given package", command_list_requires },
   928 	{ "list-provides", "list all provides for the given package", command_list_provides },
   929 	{ "list-obsoletes", "list all obsoletes for the given package", command_list_obsoletes },
   930 	{ "list-conflicts", "list all conflicts for the given package", command_list_conflicts },
   931 	{ "list-files", "list files for package set", command_list_files },
   932 	{ "list-file-packages", "list packages owning file", command_list_file_packages },
   933 	{ "list-package-files", "list files in package", command_list_package_files },
   934 	{ "what-requires", "list the packages that have the given requires", command_what_requires },
   935 	{ "what-provides", "list the packages that have the given provides", command_what_provides },
   936 	{ "import-yum", "import yum metadata files", command_import_yum },
   937 	{ "import-rpmdb", "import the system rpm database", command_import_rpmdb },
   938 	{ "import-rpms", "import rpms from the given directory", command_import_rpms },
   939 	{ "update", "update all or specified packages", command_update },
   940 	{ "remove", "remove specified packages", command_remove },
   941 	{ "diff", "show diff between two package sets", command_diff },
   942 	{ "install", "install rpm", command_install },
   943 	{ "init", "init razor root", command_init },
   944 	{ "download", "download packages", command_download },
   945 	{ "info", "display package details", command_info },
   946 	{ "search", "search package details", command_search }
   947 };
   948 
   949 static int
   950 usage(void)
   951 {
   952 	int i;
   953 
   954 	printf("usage:\n");
   955 	for (i = 0; i < ARRAY_SIZE(razor_commands); i++)
   956 		printf("  %-20s%s\n",
   957 		       razor_commands[i].name, razor_commands[i].description);
   958 
   959 	return 1;
   960 }
   961 
   962 int
   963 main(int argc, const char *argv[])
   964 {
   965 	char *repo;
   966 	int i;
   967 
   968 	repo = getenv("RAZOR_REPO");
   969 	if (repo != NULL)
   970 		repo_filename = repo;
   971 
   972 	yum_url = getenv("YUM_URL");
   973 	if (yum_url == NULL)
   974 		yum_url = YUM_URL;
   975 
   976 	if (argc < 2)
   977 		return usage();
   978 
   979 	for (i = 0; i < ARRAY_SIZE(razor_commands); i++)
   980 		if (strcmp(razor_commands[i].name, argv[1]) == 0)
   981 			return razor_commands[i].func(argc - 2, argv + 2);
   982 
   983 	return usage();
   984 }