src/main.c
author James Bowes <jbowes@redhat.com>
Fri Jun 20 19:04:47 2008 -0400 (2008-06-20)
changeset 258 29d5002bd17f
parent 247 63444a10fb8e
child 259 5b0601d184ed
permissions -rw-r--r--
Merge branch 'krh/master'

Conflicts:

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