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