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