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