main.c
author Kristian H?gsberg <krh@redhat.com>
Mon Jun 09 16:14:05 2008 -0400 (2008-06-09)
changeset 234 7f5d32472bef
parent 232 2389f44500bb
child 239 7a1b0282ae3c
permissions -rw-r--r--
Move razor root code to its own file.
     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;
   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 	trans = razor_transaction_create(set, NULL);
   458 	for (i = 0; i < argc; i++) {
   459 		if (mark_packages_for_removal(trans, set, argv[i]) == 0) {
   460 			fprintf(stderr, "no match for %s\n", argv[i]);
   461 			return 1;
   462 		}
   463 	}
   464 
   465 	errors = razor_transaction_resolve(trans);
   466 	if (errors)
   467 		return 1;
   468 
   469 	set = razor_transaction_finish(trans);
   470 	razor_set_write(set, updated_repo_filename);
   471 	razor_set_destroy(set);
   472 	printf("wrote system-updated.repo\n");
   473 
   474 	return 0;
   475 }
   476 
   477 static void
   478 print_diff(const char *name,
   479 	   const char *old_version, const char *new_version, const char *arch,
   480 	   void *data)
   481 {
   482 	if (old_version)
   483 		printf("removing %s %s\n", name, old_version);
   484 	else
   485 		printf("install %s %s\n", name, new_version);
   486 }
   487 
   488 static int
   489 command_diff(int argc, const char *argv[])
   490 {
   491 	struct razor_set *set, *updated;
   492 
   493 	set = razor_set_open(repo_filename);
   494 	updated = razor_set_open(updated_repo_filename);
   495 	if (set == NULL || updated == NULL)
   496 		return 1;
   497 
   498 	razor_set_diff(set, updated, print_diff, NULL);	
   499 
   500 	razor_set_destroy(set);
   501 	razor_set_destroy(updated);
   502 
   503 	return 0;
   504 }
   505 
   506 static int
   507 command_import_rpms(int argc, const char *argv[])
   508 {
   509 	DIR *dir;
   510 	struct dirent *de;
   511 	struct razor_importer *importer;
   512 	struct razor_set *set;
   513 	struct razor_rpm *rpm;
   514 	int len;
   515 	char filename[256];
   516 	const char *dirname = argv[0];
   517 
   518 	if (dirname == NULL) {
   519 		fprintf(stderr, "usage: razor import-rpms DIR\n");
   520 		return -1;
   521 	}
   522 
   523 	dir = opendir(dirname);
   524 	if (dir == NULL) {
   525 		fprintf(stderr, "couldn't read dir %s\n", dirname);
   526 		return -1;
   527 	}
   528 
   529 	importer = razor_importer_new();
   530 
   531 	while (de = readdir(dir), de != NULL) {
   532 		len = strlen(de->d_name);
   533 		if (len < 5 || strcmp(de->d_name + len - 4, ".rpm") != 0)
   534 		    continue;
   535 		snprintf(filename, sizeof filename,
   536 			 "%s/%s", dirname, de->d_name);
   537 		rpm = razor_rpm_open(filename);
   538 		if (rpm == NULL) {
   539 			fprintf(stderr,
   540 				"failed to open rpm \"%s\"\n", filename);
   541 			continue;
   542 		}
   543 		if (razor_importer_add_rpm(importer, rpm)) {
   544 			fprintf(stderr, "couldn't import %s\n", filename);
   545 			break;
   546 		}
   547 		razor_rpm_close(rpm);
   548 	}
   549 
   550 	if (de != NULL) {
   551 		razor_importer_destroy(importer);
   552 		return -1;
   553 	}
   554 
   555 	set = razor_importer_finish(importer);
   556 
   557 	razor_set_write(set, repo_filename);
   558 	razor_set_destroy(set);
   559 	printf("wrote %s\n", repo_filename);
   560 
   561 	return 0;
   562 }
   563 
   564 static void
   565 download_package(const char *name,
   566 		 const char *old_version,
   567 		 const char *new_version,
   568 		 const char *arch,
   569 		 void *data)
   570 {
   571 	char file[PATH_MAX], url[256];
   572 	const char *v;
   573 	int *errors = data;
   574 
   575 	if (old_version)
   576 		return;
   577 
   578 	/* Skip epoch */
   579 	v = strchr(new_version, ':');
   580 	if (v != NULL)
   581 		v = v + 1;
   582 	else
   583 		v = new_version;
   584 
   585 	snprintf(url, sizeof url,
   586 		 "%s/Packages/%s-%s.%s.rpm", yum_url, name, v, arch);
   587 	snprintf(file, sizeof file,
   588 		 "rpms/%s-%s.%s.rpm", name, v, arch);
   589 	if (download_if_missing(url, file) < 0)
   590 		(*errors)++;
   591 }
   592 
   593 static void
   594 install_package(const char *name,
   595 		const char *old_version,
   596 		const char *new_version,
   597 		const char *arch,
   598 		void *data)
   599 {
   600 	const char *v, *root = data;
   601 	char file[PATH_MAX];
   602 	struct razor_rpm *rpm;
   603 
   604 	if (old_version) {
   605 		printf("removing %s %s not handled\n", name, old_version);
   606 		return;
   607 	}
   608 
   609 	/* Skip epoch */
   610 	v = strchr(new_version, ':');
   611 	if (v != NULL)
   612 		v = v + 1;
   613 	else
   614 		v = new_version;
   615 
   616 	printf("install %s %s\n", name, v);
   617 	snprintf(file, sizeof file, "rpms/%s-%s.%s.rpm", name, v, arch);
   618 
   619  	rpm = razor_rpm_open(file);
   620 	if (rpm == NULL) {
   621 		fprintf(stderr, "failed to open rpm %s\n", file);
   622 		return;
   623 	}
   624 	if (razor_rpm_install(rpm, root) < 0) {
   625 		fprintf(stderr,
   626 			"failed to install rpm %s\n", file);
   627 		return;
   628 	}
   629 	razor_rpm_close(rpm);
   630 }
   631 
   632 static int
   633 command_install(int argc, const char *argv[])
   634 {
   635 	struct razor_root *root;
   636 	struct razor_set *upstream, *next;
   637 	struct razor_transaction *trans;
   638 	int i = 0, errors, dependencies = 1;
   639 
   640 	if (i < argc && strcmp(argv[i], "--no-dependencies") == 0) {
   641 		dependencies = 0;
   642 		i++;
   643 	}
   644 
   645 	root = razor_root_open(install_root, RAZOR_ROOT_OPEN_WRITE);
   646 	upstream = razor_set_open(rawhide_repo_filename);
   647 	trans = razor_root_create_transaction(root, upstream);
   648 
   649 	for (; i < argc; i++) {
   650 		if (mark_packages_for_update(trans, upstream, argv[i]) == 0) {
   651 			fprintf(stderr, "no package matched %s\n", argv[i]);
   652 			razor_root_close(root);
   653 			return 1;
   654 		}
   655 	}
   656 
   657 	if (dependencies) {
   658 		errors = razor_transaction_resolve(trans);
   659 		if (errors) {
   660 			razor_root_close(root);
   661 			return 1;
   662 		}
   663 	}
   664 
   665 	next = razor_transaction_finish(trans);
   666 
   667 	razor_root_update(root, next);
   668 
   669 	if (mkdir("rpms", 0777) && errno != EEXIST) {
   670 		fprintf(stderr, "failed to create rpms directory.\n");
   671 		razor_root_close(root);
   672 		return 1;
   673 	}
   674 
   675 	razor_root_diff(root, download_package, &errors);
   676 	if (errors > 0) {
   677 		fprintf(stderr, "failed to download %d packages\n", errors);
   678 		razor_root_close(root);
   679                 return 1;
   680         }
   681 
   682 	/* FIXME: We need to figure out the right install order here,
   683 	 * so the post and pre scripts can run. */
   684 	razor_root_diff(root, install_package, (void *) root);
   685 
   686 	razor_set_destroy(next);
   687 	razor_set_destroy(upstream);
   688 
   689 	return razor_root_commit(root);
   690 }
   691 
   692 static int
   693 command_init(int argc, const char *argv[])
   694 {
   695 	return razor_root_create(install_root);
   696 }
   697 
   698 static int
   699 command_download(int argc, const char *argv[])
   700 {
   701 	struct razor_set *set;
   702 	struct razor_package_iterator *pi;
   703 	struct razor_package *package;
   704 	const char *pattern = argv[0], *name, *version, *arch;
   705 	char url[256], file[256];
   706 	int matches = 0;
   707 
   708 	if (mkdir("rpms", 0777) && errno != EEXIST) {
   709 		fprintf(stderr, "failed to create rpms directory.\n");
   710 		return 1;
   711 	}
   712 
   713 	set = razor_set_open(rawhide_repo_filename);
   714 	pi = razor_package_iterator_create(set);
   715 	while (razor_package_iterator_next(pi, &package,
   716 					   &name, &version, &arch)) {
   717 		if (pattern && fnmatch(pattern, name, 0) != 0)
   718 			continue;
   719 
   720 		matches++;
   721 		snprintf(url, sizeof url,
   722 			 "%s/Packages/%s-%s.%s.rpm",
   723 			 yum_url, name, version, arch);
   724 		snprintf(file, sizeof file,
   725 			 "rpms/%s-%s.%s.rpm", name, version, arch);
   726 		download_if_missing(url, file);
   727 	}
   728 	razor_package_iterator_destroy(pi);
   729 	razor_set_destroy(set);
   730 
   731 	if (matches == 0)
   732 		fprintf(stderr, "no packages matched \"%s\"\n", pattern);
   733 	else if (matches == 1)
   734 		fprintf(stderr, "downloaded 1 package\n");
   735 	else
   736 		fprintf(stderr, "downloaded %d packages\n", matches);
   737 
   738 	return 0;
   739 }
   740 
   741 static struct {
   742 	const char *name;
   743 	const char *description;
   744 	int (*func)(int argc, const char *argv[]);
   745 } razor_commands[] = {
   746 	{ "list", "list all packages", command_list },
   747 	{ "list-requires", "list all requires for the given package", command_list_requires },
   748 	{ "list-provides", "list all provides for the given package", command_list_provides },
   749 	{ "list-obsoletes", "list all obsoletes for the given package", command_list_obsoletes },
   750 	{ "list-conflicts", "list all conflicts for the given package", command_list_conflicts },
   751 	{ "list-files", "list files for package set", command_list_files },
   752 	{ "list-file-packages", "list packages owning file", command_list_file_packages },
   753 	{ "list-package-files", "list files in package", command_list_package_files },
   754 	{ "what-requires", "list the packages that have the given requires", command_what_requires },
   755 	{ "what-provides", "list the packages that have the given provides", command_what_provides },
   756 	{ "import-yum", "import yum metadata files", command_import_yum },
   757 	{ "import-rpmdb", "import the system rpm database", command_import_rpmdb },
   758 	{ "import-rpms", "import rpms from the given directory", command_import_rpms },
   759 	{ "update", "update all or specified packages", command_update },
   760 	{ "remove", "remove specified packages", command_remove },
   761 	{ "diff", "show diff between two package sets", command_diff },
   762 	{ "install", "install rpm", command_install },
   763 	{ "init", "init razor root", command_init },
   764 	{ "download", "download packages", command_download }
   765 };
   766 
   767 static int
   768 usage(void)
   769 {
   770 	int i;
   771 
   772 	printf("usage:\n");
   773 	for (i = 0; i < ARRAY_SIZE(razor_commands); i++)
   774 		printf("  %-20s%s\n",
   775 		       razor_commands[i].name, razor_commands[i].description);
   776 
   777 	return 1;
   778 }
   779 
   780 int
   781 main(int argc, const char *argv[])
   782 {
   783 	char *repo;
   784 	int i;
   785 
   786 	repo = getenv("RAZOR_REPO");
   787 	if (repo != NULL)
   788 		repo_filename = repo;
   789 
   790 	yum_url = getenv("YUM_URL");
   791 	if (yum_url == NULL)
   792 		yum_url = YUM_URL;
   793 
   794 	if (argc < 2)
   795 		return usage();
   796 
   797 	for (i = 0; i < ARRAY_SIZE(razor_commands); i++)
   798 		if (strcmp(razor_commands[i].name, argv[1]) == 0)
   799 			return razor_commands[i].func(argc - 2, argv + 2);
   800 
   801 	return usage();
   802 }