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