main.c
author Kristian H?gsberg <krh@jiraiya.boston.redhat.com>
Mon Apr 07 21:29:21 2008 -0400 (2008-04-07)
changeset 206 75d4b6a55593
parent 205 bb1a6b6578a9
child 207 b7d55f3fa183
permissions -rw-r--r--
Create rpms dir on demand.
     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 		curl_easy_setopt(curl, CURLOPT_WRITEDATA, fp);
   271 		curl_easy_setopt(curl, CURLOPT_URL, url);
   272 		res = curl_easy_perform(curl);
   273 		fclose(fp);
   274 		if (res != CURLE_OK) {
   275 			fprintf(stderr, "curl error: %s\n", error);
   276 			unlink(file);
   277 			return -1;
   278 		}
   279 		res = curl_easy_getinfo(curl,
   280 					CURLINFO_RESPONSE_CODE, &response);
   281 		if (res != CURLE_OK) {
   282 			fprintf(stderr, "curl error: %s\n", error);
   283                         unlink(file);
   284                         return -1;
   285 		}
   286 		if (response != 200) {
   287 			fprintf(stderr, " - failed %ld\n", response);
   288                         unlink(file);
   289                         return -1;
   290 		}
   291 		fprintf(stderr, "\n");
   292 	}
   293 
   294 	curl_easy_cleanup(curl);
   295 
   296 	return 0;
   297 }
   298 
   299 #define REPO_URL "http://download.fedora.redhat.com" \
   300 	"/pub/fedora/linux/development/i386/os"
   301 
   302 static int
   303 command_import_yum(int argc, const char *argv[])
   304 {
   305 	struct razor_set *set;
   306 
   307 	if (download_if_missing(REPO_URL "/repodata/primary.xml.gz",
   308 				"primary.xml.gz") < 0)
   309 		return -1;
   310 	if (download_if_missing(REPO_URL "/repodata/filelists.xml.gz",
   311 				"filelists.xml.gz") < 0)
   312 		return -1;
   313 
   314 	set = razor_set_create_from_yum();
   315 	if (set == NULL)
   316 		return 1;
   317 	razor_set_write(set, rawhide_repo_filename);
   318 	razor_set_destroy(set);
   319 	printf("wrote %s\n", rawhide_repo_filename);
   320 
   321 	return 0;
   322 }
   323 
   324 static int
   325 command_import_rpmdb(int argc, const char *argv[])
   326 {
   327 	struct razor_set *set;
   328 
   329 	set = razor_set_create_from_rpmdb();
   330 	if (set == NULL)
   331 		return 1;
   332 	razor_set_write(set, repo_filename);
   333 	razor_set_destroy(set);
   334 	printf("wrote %s\n", repo_filename);
   335 
   336 	return 0;
   337 }
   338 
   339 static int
   340 command_validate(int argc, const char *argv[])
   341 {
   342 	struct razor_set *set;
   343 
   344 	set = razor_set_open(repo_filename);
   345 	if (set == NULL)
   346 		return 1;
   347 	razor_set_list_unsatisfied(set);
   348 	razor_set_destroy(set);
   349 
   350 	return 0;
   351 }
   352 
   353 static int
   354 command_update(int argc, const char *argv[])
   355 {
   356 	struct razor_set *set, *upstream;
   357 	struct razor_transaction *trans;
   358 	int errors;
   359 
   360 	set = razor_set_open(repo_filename);
   361 	upstream = razor_set_open(rawhide_repo_filename);
   362 	if (set == NULL || upstream == NULL)
   363 		return 1;
   364 	trans = razor_transaction_create(set, upstream, argc, argv, 0, NULL);
   365 	errors = razor_transaction_describe(trans);
   366 	if (errors)
   367 		return 1;
   368 
   369 	set = razor_transaction_finish(trans);
   370 	razor_set_write(set, updated_repo_filename);
   371 	razor_set_destroy(set);
   372 	razor_set_destroy(upstream);
   373 	printf("wrote system-updated.repo\n");
   374 
   375 	return 0;
   376 }
   377 
   378 static int
   379 command_remove(int argc, const char *argv[])
   380 {
   381 	struct razor_set *set;
   382 	struct razor_transaction *trans;
   383 	int errors;
   384 
   385 	set = razor_set_open(repo_filename);
   386 	if (set == NULL)
   387 		return 1;
   388 	trans = razor_transaction_create(set, NULL, 0, NULL, argc, argv);
   389 	errors = razor_transaction_describe(trans);
   390 	if (errors)
   391 		return 1;
   392 
   393 	set = razor_transaction_finish(trans);
   394 	razor_set_write(set, updated_repo_filename);
   395 	razor_set_destroy(set);
   396 	printf("wrote system-updated.repo\n");
   397 
   398 	return 0;
   399 }
   400 
   401 static void
   402 print_diff(const char *name,
   403 	   const char *old_version, const char *new_version, const char *arch,
   404 	   void *data)
   405 {
   406 	if (old_version)
   407 		printf("removing %s %s\n", name, old_version);
   408 	else
   409 		printf("install %s %s\n", name, new_version);
   410 }
   411 
   412 static int
   413 command_diff(int argc, const char *argv[])
   414 {
   415 	struct razor_set *set, *updated;
   416 
   417 	set = razor_set_open(repo_filename);
   418 	updated = razor_set_open(updated_repo_filename);
   419 	if (set == NULL || updated == NULL)
   420 		return 1;
   421 
   422 	razor_set_diff(set, updated, print_diff, NULL);	
   423 
   424 	razor_set_destroy(set);
   425 	razor_set_destroy(updated);
   426 
   427 	return 0;
   428 }
   429 
   430 static int
   431 command_import_rpms(int argc, const char *argv[])
   432 {
   433 	DIR *dir;
   434 	struct dirent *de;
   435 	struct razor_importer *importer;
   436 	struct razor_set *set;
   437 	struct razor_rpm *rpm;
   438 	int len;
   439 	char filename[256];
   440 	const char *dirname = argv[0];
   441 
   442 	if (dirname == NULL) {
   443 		fprintf(stderr, "usage: razor import-rpms DIR\n");
   444 		return -1;
   445 	}
   446 
   447 	dir = opendir(dirname);
   448 	if (dir == NULL) {
   449 		fprintf(stderr, "couldn't read dir %s\n", dirname);
   450 		return -1;
   451 	}
   452 
   453 	importer = razor_importer_new();
   454 
   455 	while (de = readdir(dir), de != NULL) {
   456 		len = strlen(de->d_name);
   457 		if (len < 5 || strcmp(de->d_name + len - 4, ".rpm") != 0)
   458 		    continue;
   459 		snprintf(filename, sizeof filename,
   460 			 "%s/%s", dirname, de->d_name);
   461 		rpm = razor_rpm_open(filename);
   462 		if (rpm == NULL) {
   463 			fprintf(stderr,
   464 				"failed to open rpm \"%s\"\n", filename);
   465 			continue;
   466 		}
   467 		if (razor_importer_add_rpm(importer, rpm)) {
   468 			fprintf(stderr, "couldn't import %s\n", filename);
   469 			break;
   470 		}
   471 		razor_rpm_close(rpm);
   472 	}
   473 
   474 	if (de != NULL) {
   475 		razor_importer_destroy(importer);
   476 		return -1;
   477 	}
   478 
   479 	set = razor_importer_finish(importer);
   480 
   481 	razor_set_write(set, repo_filename);
   482 	razor_set_destroy(set);
   483 	printf("wrote %s\n", repo_filename);
   484 
   485 	return 0;
   486 }
   487 
   488 static void
   489 download_package(const char *name,
   490 		 const char *old_version,
   491 		 const char *new_version,
   492 		 const char *arch,
   493 		 void *data)
   494 {
   495 	char file[PATH_MAX], url[256];
   496 	const char *v;
   497 	int *errors = data;
   498 
   499 	if (old_version)
   500 		return;
   501 
   502 	/* Skip epoch */
   503 	v = strchr(new_version, ':');
   504 	if (v != NULL)
   505 		v = v + 1;
   506 	else
   507 		v = new_version;
   508 
   509 	snprintf(url, sizeof url,
   510 		 REPO_URL "/Packages/%s-%s.%s.rpm", name, v, arch);
   511 	snprintf(file, sizeof file,
   512 		 "rpms/%s-%s.%s.rpm", name, v, arch);
   513 	if (download_if_missing(url, file) < 0)
   514 		(*errors)++;
   515 }
   516 
   517 static void
   518 install_package(const char *name,
   519 		const char *old_version,
   520 		const char *new_version,
   521 		const char *arch,
   522 		void *data)
   523 {
   524 	const char *v, *root = data;
   525 	char file[PATH_MAX];
   526 	struct razor_rpm *rpm;
   527 
   528 	if (old_version) {
   529 		printf("removing %s %s not handled\n", name, old_version);
   530 		return;
   531 	}
   532 
   533 	/* Skip epoch */
   534 	v = strchr(new_version, ':');
   535 	if (v != NULL)
   536 		v = v + 1;
   537 	else
   538 		v = new_version;
   539 
   540 	printf("install %s %s\n", name, v);
   541 	snprintf(file, sizeof file, "rpms/%s-%s.%s.rpm", name, v, arch);
   542 
   543  	rpm = razor_rpm_open(file);
   544 	if (rpm == NULL) {
   545 		fprintf(stderr, "failed to open rpm %s\n", file);
   546 		return;
   547 	}
   548 	if (razor_rpm_install(rpm, root) < 0) {
   549 		fprintf(stderr,
   550 			"failed to install rpm %s\n", file);
   551 		return;
   552 	}
   553 	razor_rpm_close(rpm);
   554 }
   555 
   556 static int
   557 command_install(int argc, const char *argv[])
   558 {
   559 	struct razor_set *system, *upstream, *next;
   560 	struct razor_transaction *trans;
   561 	char path[PATH_MAX], new_path[PATH_MAX];
   562 	int errors, fd;
   563 
   564 	/* Create the new next repo file up front to ensure exclusive
   565 	 * access. */
   566 	snprintf(new_path, sizeof new_path,
   567 		 "%s%s/%s", root, razor_root_path, next_repo_filename);
   568 	fd = open(new_path, O_CREAT | O_WRONLY | O_TRUNC | O_EXCL, 0666);
   569 	if (fd < 0) {
   570 		fprintf(stderr, "failed to get lock file, "
   571 			"maybe previous operation crashed?\n");
   572 
   573 		/* FIXME: Use fcntl advisory locking to figure out
   574 		 * whether previous operation crashed or is still in
   575 		 * progress. */
   576 
   577 		return -1;
   578 	}
   579 
   580 	upstream = razor_set_open(rawhide_repo_filename);
   581 	snprintf(path, sizeof path,
   582 		 "%s%s/%s", root, razor_root_path, system_repo_filename);
   583 	system = razor_set_open(path);
   584 	if (system == NULL || upstream == NULL) {
   585 		unlink(new_path);
   586 		return 1;
   587 	}
   588 	trans = razor_transaction_create(system, upstream,
   589 					 argc, argv, 0, NULL);
   590 	errors = razor_transaction_describe(trans);
   591 	if (errors) {
   592 		unlink(new_path);
   593 		return 1;
   594 	}
   595 
   596 	next = razor_transaction_finish(trans);
   597 
   598 	razor_set_write_to_fd(next, fd);
   599 	printf("wrote %s\n", new_path);
   600 
   601 	if (mkdir("rpms", 0777) && errno != EEXIST) {
   602 		fprintf(stderr, "failed to create rpms directory.\n");
   603 		return 1;
   604 	}
   605 
   606 	razor_set_diff(system, next, download_package, &errors);
   607 	if (errors > 0) {
   608 		fprintf(stderr, "failed to download %d packages\n", errors);
   609 		unlink(new_path);
   610                 return 1;
   611         }
   612 
   613 	/* FIXME: We need to figure out the right install order here,
   614 	 * so the post and pre scripts can run. */
   615 	razor_set_diff(system, next, install_package, (void *) root);
   616 
   617 	razor_set_destroy(next);
   618 	razor_set_destroy(system);
   619 	razor_set_destroy(upstream);
   620 
   621 	/* Make it so. */
   622 	rename(new_path, path);
   623 	printf("renamed %s to %s\n", new_path, path);
   624 
   625 	return 0;
   626 }
   627 
   628 static int
   629 command_init(int argc, const char *argv[])
   630 {
   631 	struct stat buf;
   632 	struct razor_set *set;
   633 	char path[PATH_MAX];
   634 
   635 	if (stat(root, &buf) < 0) {
   636 		if (mkdir(root, 0777) < 0) {
   637 			fprintf(stderr,
   638 				"could not create install root \"%s\"\n",
   639 				root);
   640 			return -1;
   641 		}
   642 		fprintf(stderr, "created install root \"%s\"\n", root);
   643 	} else if (!S_ISDIR(buf.st_mode)) {
   644 		fprintf(stderr,
   645 			"install root \"%s\" exists, but is not a directory\n",
   646 			root);
   647 		return -1;
   648 	}
   649 
   650 	if (razor_create_dir(root, razor_root_path) < 0) {
   651 		fprintf(stderr, "could not create %s%s\n",
   652 			root, razor_root_path);
   653 		return -1;
   654 	}
   655 
   656 	set = razor_set_create();
   657 	snprintf(path, sizeof path, "%s%s/%s",
   658 		 root, razor_root_path, system_repo_filename);
   659 	if (razor_set_write(set, path) < 0) {
   660 		fprintf(stderr, "could not write initial package set\n");
   661 		return -1;
   662 	}
   663 	razor_set_destroy(set);
   664 
   665 	return 0;
   666 }
   667 
   668 static int
   669 command_download(int argc, const char *argv[])
   670 {
   671 	struct razor_set *set;
   672 	struct razor_package_iterator *pi;
   673 	struct razor_package *package;
   674 	const char *pattern = argv[0], *name, *version, *arch;
   675 	char url[256], file[256];
   676 	int matches = 0;
   677 
   678 	if (mkdir("rpms", 0777) && errno != EEXIST) {
   679 		fprintf(stderr, "failed to create rpms directory.\n");
   680 		return 1;
   681 	}
   682 
   683 	set = razor_set_open(rawhide_repo_filename);
   684 	pi = razor_package_iterator_create(set);
   685 	while (razor_package_iterator_next(pi, &package,
   686 					   &name, &version, &arch)) {
   687 		if (pattern && fnmatch(pattern, name, 0) != 0)
   688 			continue;
   689 
   690 		matches++;
   691 		snprintf(url, sizeof url,
   692 			 REPO_URL "/Packages/%s-%s.%s.rpm",
   693 			 name, version, arch);
   694 		snprintf(file, sizeof file,
   695 			 "rpms/%s-%s.%s.rpm", name, version, arch);
   696 		download_if_missing(url, file);
   697 	}
   698 	razor_package_iterator_destroy(pi);
   699 	razor_set_destroy(set);
   700 
   701 	if (matches == 0)
   702 		fprintf(stderr, "no packages matched \"%s\"\n", pattern);
   703 	else if (matches == 1)
   704 		fprintf(stderr, "downloaded 1 package\n");
   705 	else
   706 		fprintf(stderr, "downloaded %d packages\n", matches);
   707 
   708 	return 0;
   709 }
   710 
   711 static struct {
   712 	const char *name;
   713 	const char *description;
   714 	int (*func)(int argc, const char *argv[]);
   715 } razor_commands[] = {
   716 	{ "list", "list all packages", command_list },
   717 	{ "list-requires", "list all requires for the given package", command_list_requires },
   718 	{ "list-provides", "list all provides for the given package", command_list_provides },
   719 	{ "list-obsoletes", "list all obsoletes for the given package", command_list_obsoletes },
   720 	{ "list-conflicts", "list all conflicts for the given package", command_list_conflicts },
   721 	{ "list-files", "list files for package set", command_list_files },
   722 	{ "list-file-packages", "list packages owning file", command_list_file_packages },
   723 	{ "list-package-files", "list files in package", command_list_package_files },
   724 	{ "what-requires", "list the packages that have the given requires", command_what_requires },
   725 	{ "what-provides", "list the packages that have the given provides", command_what_provides },
   726 	{ "import-yum", "import yum metadata files", command_import_yum },
   727 	{ "import-rpmdb", "import the system rpm database", command_import_rpmdb },
   728 	{ "import-rpms", "import rpms from the given directory", command_import_rpms },
   729 	{ "validate", "validate a package set", command_validate },
   730 	{ "update", "update all or specified packages", command_update },
   731 	{ "remove", "remove specified packages", command_remove },
   732 	{ "diff", "show diff between two package sets", command_diff },
   733 	{ "install", "install rpm", command_install },
   734 	{ "init", "init razor root", command_init },
   735 	{ "download", "download packages", command_download }
   736 };
   737 
   738 static int
   739 usage(void)
   740 {
   741 	int i;
   742 
   743 	printf("usage:\n");
   744 	for (i = 0; i < ARRAY_SIZE(razor_commands); i++)
   745 		printf("  %-20s%s\n",
   746 		       razor_commands[i].name, razor_commands[i].description);
   747 
   748 	return 1;
   749 }
   750 
   751 int
   752 main(int argc, const char *argv[])
   753 {
   754 	char *repo;
   755 	int i;
   756 
   757 	repo = getenv("RAZOR_REPO");
   758 	if (repo != NULL)
   759 		repo_filename = repo;
   760 
   761 	if (argc < 2)
   762 		return usage();
   763 
   764 	for (i = 0; i < ARRAY_SIZE(razor_commands); i++)
   765 		if (strcmp(razor_commands[i].name, argv[1]) == 0)
   766 			return razor_commands[i].func(argc - 2, argv + 2);
   767 
   768 	return usage();
   769 }