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