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