src/main.c
author J. Ali Harlow <ali@juiblex.co.uk>
Thu May 14 05:55:19 2009 +0100 (2009-05-14)
changeset 367 e45f50e940b6
parent 359 c9c90315ea24
child 369 f8c27fe9fe63
permissions -rw-r--r--
Order the installation and removal of packages correctly so scripts can run
     1 /*
     2  * Copyright (C) 2008  Kristian Høgsberg <krh@redhat.com>
     3  * Copyright (C) 2008  Red Hat, Inc
     4  * Copyright (C) 2009  J. Ali Harlow <ali@juiblex.co.uk>
     5  *
     6  * This program is free software; you can redistribute it and/or modify
     7  * it under the terms of the GNU General Public License as published by
     8  * the Free Software Foundation; either version 2 of the License, or
     9  * (at your option) any later version.
    10  *
    11  * This program is distributed in the hope that it will be useful,
    12  * but WITHOUT ANY WARRANTY; without even the implied warranty of
    13  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
    14  * GNU General Public License for more details.
    15  *
    16  * You should have received a copy of the GNU General Public License along
    17  * with this program; if not, write to the Free Software Foundation, Inc.,
    18  * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
    19  */
    20 
    21 #define _GNU_SOURCE
    22 
    23 #include "config.h"
    24 
    25 #include <stdlib.h>
    26 #include <stddef.h>
    27 #include <stdio.h>
    28 #include <stdint.h>
    29 #include <string.h>
    30 #include <sys/stat.h>
    31 #include <unistd.h>
    32 #include <fcntl.h>
    33 #include <dirent.h>
    34 #include <limits.h>
    35 #ifdef HAVE_CURL
    36 #include <curl/curl.h>
    37 #endif
    38 #include <fnmatch.h>
    39 #include <errno.h>
    40 #include "razor.h"
    41 
    42 static const char system_repo_filename[] = "system.rzdb";
    43 static const char next_repo_filename[] = "system-next.rzdb";
    44 static const char rawhide_repo_filename[] = "rawhide.rzdb";
    45 static const char updated_repo_filename[] = "system-updated.rzdb";
    46 static const char *install_root = "";
    47 static const char *repo_filename = system_repo_filename;
    48 static const char *yum_url;
    49 
    50 #define ARRAY_SIZE(a) (sizeof(a) / sizeof((a)[0]))
    51 
    52 static int
    53 update_packages(struct razor_set *system, struct razor_set *next,
    54 		struct razor_relocations *relocations);
    55 
    56 static struct razor_package_iterator *
    57 create_iterator_from_argv(struct razor_set *set, int argc, const char *argv[])
    58 {
    59 	struct razor_package_query *query;
    60 	struct razor_package_iterator *iter;
    61 	struct razor_package *package;
    62 	const char *name, *pattern;
    63 	int i, count;
    64 
    65 	if (argc == 0)
    66 		return razor_package_iterator_create(set);
    67 
    68 	query = razor_package_query_create(set);
    69 
    70 	for (i = 0; i < argc; i++) {
    71 		iter = razor_package_iterator_create(set);
    72 		pattern = argv[i];
    73 		count = 0;
    74 		while (razor_package_iterator_next(iter, &package,
    75 						   RAZOR_DETAIL_NAME, &name,
    76 						   RAZOR_DETAIL_LAST)) {
    77 			if (fnmatch(pattern, name, 0) != 0)
    78 				continue;
    79 
    80 			razor_package_query_add_package(query, package);
    81 			count++;
    82 		}
    83 		razor_package_iterator_destroy(iter);
    84 
    85 		if (count == 0)
    86 			fprintf(stderr,
    87 				"no package matches \"%s\"\n", pattern);
    88 	}
    89 
    90 	return razor_package_query_finish(query);
    91 }
    92 
    93 #define LIST_PACKAGES_ONLY_NAMES 0x01
    94 
    95 static void
    96 list_packages(struct razor_package_iterator *iter, uint32_t flags)
    97 {
    98 	struct razor_package *package;
    99 	const char *name, *version, *arch;
   100 
   101 	while (razor_package_iterator_next(iter, &package,
   102 					   RAZOR_DETAIL_NAME, &name,
   103 					   RAZOR_DETAIL_VERSION, &version,
   104 					   RAZOR_DETAIL_ARCH, &arch,
   105 					   RAZOR_DETAIL_LAST)) {
   106 		if (flags & LIST_PACKAGES_ONLY_NAMES)
   107 			printf("%s\n", name);
   108 		else
   109 			printf("%s-%s.%s\n", name, version, arch);
   110 	}
   111 }
   112 
   113 static int
   114 command_list(int argc, const char *argv[])
   115 {
   116 	struct razor_package_iterator *pi;
   117 	struct razor_set *set;
   118 	uint32_t flags = 0;
   119 	int i = 0;
   120 
   121 	if (i < argc && strcmp(argv[i], "--only-names") == 0) {
   122 		flags |= LIST_PACKAGES_ONLY_NAMES;
   123 		i++;
   124 	}
   125 
   126 	set = razor_root_open_read_only(install_root);
   127 	if (set == NULL)
   128 		return 1;
   129 
   130 	pi = create_iterator_from_argv(set, argc - i, argv + i);
   131 	list_packages(pi, flags);
   132 	razor_package_iterator_destroy(pi);
   133 	razor_set_destroy(set);
   134 
   135 	return 0;
   136 }
   137 
   138 static void
   139 list_package_properties(struct razor_set *set,
   140 			struct razor_package *package, uint32_t type)
   141 {
   142 	struct razor_property_iterator *pi;
   143 	struct razor_property *property;
   144 	const char *name, *version;
   145 	uint32_t flags;
   146 
   147 	pi = razor_property_iterator_create(set, package);
   148 	while (razor_property_iterator_next(pi, &property,
   149 					    &name, &flags, &version)) {
   150 		if ((flags & RAZOR_PROPERTY_TYPE_MASK) != type)
   151 			continue;
   152 		printf("%s", name);
   153 		if (version[0] != '\0')
   154 			printf(" %s %s",
   155 			       razor_property_relation_to_string(property),
   156 			       version);
   157 
   158 		if (flags & ~(RAZOR_PROPERTY_RELATION_MASK | RAZOR_PROPERTY_TYPE_MASK)) {
   159 			printf(" [");
   160 			if (flags & RAZOR_PROPERTY_PRE)
   161 				printf(" pre");
   162 			if (flags & RAZOR_PROPERTY_POST)
   163 				printf(" post");
   164 			if (flags & RAZOR_PROPERTY_PREUN)
   165 				printf(" preun");
   166 			if (flags & RAZOR_PROPERTY_POSTUN)
   167 				printf(" postun");
   168 			printf(" ]");
   169 		}
   170 		printf("\n");
   171 	}
   172 	razor_property_iterator_destroy(pi);
   173 }
   174 
   175 static int
   176 list_properties(int argc, const char *argv[], uint32_t type)
   177 {
   178 	struct razor_set *set;
   179 	struct razor_package *package;
   180 	struct razor_package_iterator *pi;
   181 	const char *name, *version, *arch;
   182 
   183 	set = razor_root_open_read_only(install_root);
   184 	if (set == NULL)
   185 		return 1;
   186 
   187 	pi = create_iterator_from_argv(set, argc, argv);
   188 	while (razor_package_iterator_next(pi, &package,
   189 					   RAZOR_DETAIL_NAME, &name,
   190 					   RAZOR_DETAIL_VERSION, &version,
   191 					   RAZOR_DETAIL_ARCH, &arch,
   192 					   RAZOR_DETAIL_LAST))
   193 		list_package_properties(set, package, type);
   194 	razor_package_iterator_destroy(pi);
   195 	razor_set_destroy(set);
   196 
   197 	return 0;
   198 }
   199 
   200 static int
   201 command_list_requires(int argc, const char *argv[])
   202 {
   203 	return list_properties(argc, argv, RAZOR_PROPERTY_REQUIRES);
   204 }
   205 
   206 static int
   207 command_list_provides(int argc, const char *argv[])
   208 {
   209 	return list_properties(argc, argv, RAZOR_PROPERTY_PROVIDES);
   210 }
   211 
   212 static int
   213 command_list_obsoletes(int argc, const char *argv[])
   214 {
   215 	return list_properties(argc, argv, RAZOR_PROPERTY_OBSOLETES);
   216 }
   217 
   218 static int
   219 command_list_conflicts(int argc, const char *argv[])
   220 {
   221 	return list_properties(argc, argv, RAZOR_PROPERTY_CONFLICTS);
   222 }
   223 
   224 static int
   225 command_list_files(int argc, const char *argv[])
   226 {
   227 	struct razor_set *set;
   228 
   229 	set = razor_root_open_read_only(install_root);
   230 	if (set == NULL)
   231 		return 1;
   232 
   233 	razor_set_list_files(set, argv[0]);
   234 	razor_set_destroy(set);
   235 
   236 	return 0;
   237 }
   238 
   239 static int
   240 command_list_file_packages(int argc, const char *argv[])
   241 {
   242 	struct razor_set *set;
   243 	struct razor_package_iterator *pi;
   244 
   245 	set = razor_root_open_read_only(install_root);
   246 	if (set == NULL)
   247 		return 1;
   248 
   249 	pi = razor_package_iterator_create_for_file(set, argv[0]);
   250 	list_packages(pi, 0);
   251 	razor_package_iterator_destroy(pi);
   252 
   253 	razor_set_destroy(set);
   254 
   255 	return 0;
   256 }
   257 
   258 static int
   259 command_list_package_files(int argc, const char *argv[])
   260 {
   261 	struct razor_set *set;
   262 	struct razor_package_iterator *pi;
   263 	struct razor_package *package;
   264 	const char *name, *version, *arch;
   265 
   266 	set = razor_root_open_read_only(install_root);
   267 	if (set == NULL)
   268 		return 1;
   269 
   270 	pi = create_iterator_from_argv(set, argc, argv);
   271 	while (razor_package_iterator_next(pi, &package,
   272 					   RAZOR_DETAIL_NAME, &name,
   273 					   RAZOR_DETAIL_VERSION, &version,
   274 					   RAZOR_DETAIL_ARCH, &arch,
   275 					   RAZOR_DETAIL_LAST))
   276 		razor_set_list_package_files(set, package);
   277 	razor_package_iterator_destroy(pi);
   278 
   279 	razor_set_destroy(set);
   280 
   281 	return 0;
   282 }
   283 
   284 static int
   285 list_property_packages(const char *ref_name,
   286 		       const char *ref_version,
   287 		       uint32_t type)
   288 {
   289 	struct razor_set *set;
   290 	struct razor_property *property;
   291 	struct razor_property_iterator *prop_iter;
   292 	struct razor_package_iterator *pkg_iter;
   293 	const char *name, *version;
   294 	uint32_t flags;
   295 
   296 	if (ref_name == NULL)
   297 		return 0;
   298 
   299 	set = razor_root_open_read_only(install_root);
   300 	if (set == NULL)
   301 		return 1;
   302 
   303 	prop_iter = razor_property_iterator_create(set, NULL);
   304 	while (razor_property_iterator_next(prop_iter, &property,
   305 					    &name, &flags, &version)) {
   306 		if (strcmp(ref_name, name) != 0)
   307 			continue;
   308 		if (ref_version &&
   309 		    (flags & RAZOR_PROPERTY_RELATION_MASK) == RAZOR_PROPERTY_EQUAL &&
   310 		    strcmp(ref_version, version) != 0)
   311 			continue;
   312 		if ((flags & RAZOR_PROPERTY_TYPE_MASK) != type)
   313 			continue;
   314 
   315 		pkg_iter =
   316 			razor_package_iterator_create_for_property(set,
   317 								   property);
   318 		list_packages(pkg_iter, 0);
   319 		razor_package_iterator_destroy(pkg_iter);
   320 	}
   321 	razor_property_iterator_destroy(prop_iter);
   322 
   323 	razor_set_destroy(set);
   324 
   325 	return 0;
   326 }
   327 
   328 static int
   329 command_what_requires(int argc, const char *argv[])
   330 {
   331 	return list_property_packages(argv[0], argv[1],
   332 				      RAZOR_PROPERTY_REQUIRES);
   333 }
   334 
   335 static int
   336 command_what_provides(int argc, const char *argv[])
   337 {
   338 	return list_property_packages(argv[0], argv[1],
   339 				      RAZOR_PROPERTY_PROVIDES);
   340 }
   341 
   342 static int
   343 show_progress(void *clientp,
   344 	      double dltotal, double dlnow, double ultotal, double ulnow)
   345 {
   346 	const char *file = clientp;
   347 
   348 	if (!dlnow < dltotal)
   349 		fprintf(stderr, "\rdownloading %s, %dkB/%dkB",
   350 			file, (int) dlnow / 1024, (int) dltotal / 1024);
   351 
   352 	return 0;
   353 }
   354 
   355 static int
   356 download_if_missing(const char *url, const char *file)
   357 {
   358 #ifndef HAVE_CURL
   359 	return 1;
   360 #else
   361 	CURL *curl;
   362 	struct stat buf;
   363 	char error[256];
   364 	FILE *fp;
   365 	CURLcode res;
   366 	long response;
   367 
   368 	curl = curl_easy_init();
   369 	if (curl == NULL)
   370 		return 1;
   371 
   372 	curl_easy_setopt(curl, CURLOPT_ERRORBUFFER, error);
   373 	curl_easy_setopt(curl, CURLOPT_NOPROGRESS, 0);
   374 	curl_easy_setopt(curl, CURLOPT_PROGRESSFUNCTION, show_progress);
   375 	curl_easy_setopt(curl, CURLOPT_PROGRESSDATA, file);
   376 
   377 	if (stat(file, &buf) < 0) {
   378 		fp = fopen(file, "wb");
   379 		if (fp == NULL) {
   380 			fprintf(stderr,
   381 				"failed to open %s for writing\n", file);
   382 			return -1;
   383 		}
   384 		curl_easy_setopt(curl, CURLOPT_WRITEDATA, fp);
   385 		curl_easy_setopt(curl, CURLOPT_URL, url);
   386 		res = curl_easy_perform(curl);
   387 		fclose(fp);
   388 		if (res != CURLE_OK) {
   389 			fprintf(stderr, "curl error: %s\n", error);
   390 			unlink(file);
   391 			return -1;
   392 		}
   393 		res = curl_easy_getinfo(curl,
   394 					CURLINFO_RESPONSE_CODE, &response);
   395 		if (res != CURLE_OK) {
   396 			fprintf(stderr, "curl error: %s\n", error);
   397                         unlink(file);
   398                         return -1;
   399 		}
   400 		if (response != 200) {
   401 			fprintf(stderr, " - failed %ld\n", response);
   402                         unlink(file);
   403                         return -1;
   404 		}
   405 		fprintf(stderr, "\n");
   406 	}
   407 
   408 	curl_easy_cleanup(curl);
   409 
   410 	return 0;
   411 #endif	/* HAVE_CURL */
   412 }
   413 
   414 #define YUM_URL "http://download.fedora.redhat.com" \
   415 	"/pub/fedora/linux/development/i386/os"
   416 
   417 static int
   418 command_import_yum(int argc, const char *argv[])
   419 {
   420 	struct razor_set *set;
   421 	char buffer[512];
   422 
   423 	printf("downloading from %s.\n", yum_url);
   424 	snprintf(buffer, sizeof buffer,
   425 		 "%s/repodata/primary.xml.gz", yum_url);
   426 	if (download_if_missing(buffer, "primary.xml.gz") < 0)
   427 		return -1;
   428 	snprintf(buffer, sizeof buffer,
   429 		 "%s/repodata/filelists.xml.gz", yum_url);
   430 	if (download_if_missing(buffer, "filelists.xml.gz") < 0)
   431 		return -1;
   432 
   433 	set = razor_set_create_from_yum();
   434 	if (set == NULL)
   435 		return 1;
   436 	if (razor_set_write(set, rawhide_repo_filename, RAZOR_REPO_FILE_MAIN)) {
   437 		perror(rawhide_repo_filename);
   438 		return -1;
   439 	}
   440 	if (razor_set_write(set, "rawhide-details.rzdb",
   441 	    RAZOR_REPO_FILE_DETAILS)) {
   442 		perror("rawhide-details.rzdb");
   443 		return -1;
   444 	}
   445 	if (razor_set_write(set, "rawhide-files.rzdb", RAZOR_REPO_FILE_FILES)) {
   446 		perror("rawhide-files.rzdb");
   447 		return -1;
   448 	}
   449 	razor_set_destroy(set);
   450 	printf("wrote %s\n", rawhide_repo_filename);
   451 
   452 	return 0;
   453 }
   454 
   455 #if HAVE_RPMLIB
   456 static int
   457 command_import_rpmdb(int argc, const char *argv[])
   458 {
   459 	struct razor_set *set;
   460 	struct razor_root *root;
   461 
   462 	root = razor_root_open(install_root);
   463 	if (root == NULL)
   464 		return 1;
   465 
   466 	set = razor_set_create_from_rpmdb();
   467 	if (set == NULL)
   468 		return 1;
   469 
   470 	razor_root_update(root, set);
   471 
   472 	return razor_root_commit(root);
   473 }
   474 #endif
   475 
   476 static int
   477 mark_packages_for_update(struct razor_transaction *trans,
   478 			 struct razor_set *set, const char *pattern)
   479 {
   480 	struct razor_package_iterator *pi;
   481 	struct razor_package *package;
   482 	const char *name;
   483 	int matches = 0;
   484 
   485 	pi = razor_package_iterator_create(set);
   486 	while (razor_package_iterator_next(pi, &package,
   487 					   RAZOR_DETAIL_NAME, &name,
   488 					   RAZOR_DETAIL_LAST)) {
   489 		if (pattern && fnmatch(pattern, name, 0) == 0) {
   490 			razor_transaction_update_package(trans, package);
   491 			matches++;
   492 		}
   493 	}
   494 	razor_package_iterator_destroy(pi);
   495 
   496 	return matches;
   497 }
   498 
   499 static int
   500 mark_packages_for_removal(struct razor_transaction *trans,
   501 			  struct razor_set *set, const char *pattern)
   502 {
   503 	struct razor_package_iterator *pi;
   504 	struct razor_package *package;
   505 	const char *name;
   506 	int matches = 0;
   507 
   508 	pi = razor_package_iterator_create(set);
   509 	while (razor_package_iterator_next(pi, &package,
   510 					   RAZOR_DETAIL_NAME, &name,
   511 					   RAZOR_DETAIL_LAST)) {
   512 		if (pattern && fnmatch(pattern, name, 0) == 0) {
   513 			razor_transaction_remove_package(trans, package);
   514 			matches++;
   515 		}
   516 	}
   517 	razor_package_iterator_destroy(pi);
   518 
   519 	return matches;
   520 }
   521 
   522 static int
   523 command_update(int argc, const char *argv[])
   524 {
   525 	struct razor_set *set, *upstream;
   526 	struct razor_transaction *trans;
   527 	int i, errors;
   528 
   529 	set = razor_root_open_read_only(install_root);
   530 	if (set == NULL)
   531 		return 1;
   532 
   533 	upstream = razor_set_open(rawhide_repo_filename);
   534 	if (upstream == NULL ||
   535 	    razor_set_open_details(upstream, "rawhide-details.rzdb") ||
   536 	    razor_set_open_files(upstream, "rawhide-files.rzdb"))
   537 		return 1;
   538 
   539 	trans = razor_transaction_create(set, upstream);
   540 	if (argc == 0)
   541 		razor_transaction_update_all(trans);
   542 	for (i = 0; i < argc; i++) {
   543 		if (mark_packages_for_update(trans, set, argv[i]) == 0) {
   544 			fprintf(stderr, "no match for %s\n", argv[i]);
   545 			return 1;
   546 		}
   547 	}
   548 
   549 	razor_transaction_resolve(trans);
   550 	errors = razor_transaction_describe(trans);
   551 	if (errors) {
   552 		fprintf(stderr, "unresolved dependencies\n");
   553 		return 1;
   554 	}
   555 
   556 	set = razor_transaction_finish(trans);
   557 	razor_set_write(set, updated_repo_filename, RAZOR_REPO_FILE_MAIN);
   558 	razor_set_destroy(set);
   559 	razor_set_destroy(upstream);
   560 	printf("wrote system-updated.rzdb\n");
   561 
   562 	return 0;
   563 }
   564 
   565 static int
   566 command_remove(int argc, const char *argv[])
   567 {
   568 	struct razor_root *root;
   569 	struct razor_set *system, *upstream, *next;
   570 	struct razor_transaction *trans;
   571 	int i, errors;
   572 
   573 	root = razor_root_open(install_root);
   574 	system = razor_root_get_system_set(root);
   575 	if (system == NULL) {
   576 		razor_root_close(root);
   577 		return 1;
   578 	}
   579 
   580 	upstream = razor_set_create_without_root();
   581 	trans = razor_transaction_create(system, upstream);
   582 	for (i = 0; i < argc; i++) {
   583 		if (mark_packages_for_removal(trans, system, argv[i]) == 0) {
   584 			fprintf(stderr, "no match for %s\n", argv[i]);
   585 			razor_transaction_destroy(trans);
   586 			razor_set_destroy(upstream);
   587 			razor_root_close(root);
   588 			return 1;
   589 		}
   590 	}
   591 
   592 	razor_transaction_resolve(trans);
   593 	errors = razor_transaction_describe(trans);
   594 	if (errors) {
   595 		razor_transaction_destroy(trans);
   596 		razor_set_destroy(upstream);
   597 		razor_root_close(root);
   598 		return 1;
   599 	}
   600 
   601 	next = razor_transaction_finish(trans);
   602 	update_packages(system, next, NULL);
   603 	razor_root_update(root, next);
   604 
   605 	razor_set_destroy(next);
   606 	razor_set_destroy(upstream);
   607 
   608 	return razor_root_commit(root);
   609 }
   610 
   611 static void
   612 print_diff(enum razor_diff_action action,
   613 	   struct razor_package *package,
   614 	   const char *name,
   615 	   const char *version,
   616 	   const char *arch,
   617 	   void *data)
   618 {
   619 	if (action == RAZOR_DIFF_ACTION_ADD)
   620 		printf("install %s-%s.%s\n", name, version, arch);
   621 	if (action == RAZOR_DIFF_ACTION_REMOVE)
   622 		printf("remove %s-%s.%s\n", name, version, arch);
   623 }
   624 
   625 static int
   626 command_diff(int argc, const char *argv[])
   627 {
   628 	struct razor_set *set, *updated;
   629 
   630 	set = razor_root_open_read_only(install_root);
   631 	updated = razor_set_open(updated_repo_filename);
   632 	if (set == NULL || updated == NULL)
   633 		return 1;
   634 
   635 	razor_set_diff(set, updated, print_diff, NULL);
   636 
   637 	razor_set_destroy(set);
   638 	razor_set_destroy(updated);
   639 
   640 	return 0;
   641 }
   642 
   643 static int
   644 command_import_rpms(int argc, const char *argv[])
   645 {
   646 	DIR *dir;
   647 	struct dirent *de;
   648 	struct razor_importer *importer;
   649 	struct razor_set *set;
   650 	struct razor_rpm *rpm;
   651 	int len, imported_count = 0;
   652 	char filename[256];
   653 	const char *dirname = argv[0];
   654 
   655 	if (dirname == NULL) {
   656 		fprintf(stderr, "usage: razor import-rpms DIR\n");
   657 		return -1;
   658 	}
   659 
   660 	dir = opendir(dirname);
   661 	if (dir == NULL) {
   662 		fprintf(stderr, "couldn't read dir %s\n", dirname);
   663 		return -1;
   664 	}
   665 
   666 	importer = razor_importer_create();
   667 
   668 	while (de = readdir(dir), de != NULL) {
   669 		len = strlen(de->d_name);
   670 		if (len < 5 || strcmp(de->d_name + len - 4, ".rpm") != 0)
   671 		    continue;
   672 		snprintf(filename, sizeof filename,
   673 			 "%s/%s", dirname, de->d_name);
   674 		rpm = razor_rpm_open(filename);
   675 		if (rpm == NULL) {
   676 			fprintf(stderr,
   677 				"failed to open rpm \"%s\"\n", filename);
   678 			continue;
   679 		}
   680 		if (razor_importer_add_rpm(importer, rpm)) {
   681 			fprintf(stderr, "couldn't import %s\n", filename);
   682 			break;
   683 		}
   684 		razor_rpm_close(rpm);
   685 
   686 		printf("\rimporting %d", ++imported_count);
   687 		fflush(stdout);
   688 	}
   689 
   690 	if (de != NULL) {
   691 		razor_importer_destroy(importer);
   692 		return -1;
   693 	}
   694 
   695 	printf("\nsaving\n");
   696 	set = razor_importer_finish(importer);
   697 
   698 	razor_set_write(set, repo_filename, RAZOR_REPO_FILE_MAIN);
   699 	razor_set_write(set, "system-details.rzdb", RAZOR_REPO_FILE_DETAILS);
   700 	razor_set_write(set, "system-files.rzdb", RAZOR_REPO_FILE_FILES);
   701 	razor_set_destroy(set);
   702 	printf("wrote %s\n", repo_filename);
   703 
   704 	return 0;
   705 }
   706 
   707 static const char *
   708 rpm_filename(const char *name, const char *version, const char *arch)
   709 {
   710 	static char file[PATH_MAX];
   711  	const char *v;
   712  
   713  	/* Skip epoch */
   714 	v = strchr(version, ':');
   715  	if (v != NULL)
   716  		v = v + 1;
   717  	else
   718 		v = version;
   719 
   720 	snprintf(file, sizeof file, "%s-%s.%s.rpm", name, v, arch);
   721 
   722 	return file;
   723 }
   724 
   725 static int
   726 download_packages(struct razor_set *system, struct razor_set *next)
   727 {
   728 	struct razor_install_iterator *ii;
   729 	struct razor_package *package;
   730 	struct razor_set *set;
   731 	enum razor_install_action action;
   732 	const char *name, *version, *arch;
   733 	char file[PATH_MAX], url[256];
   734 	int errors = 0, count;
   735 
   736 	ii = razor_set_create_install_iterator(system, next);
   737 	while (razor_install_iterator_next(ii, &set, &package,
   738 					   &action, &count)) {
   739 		if (action == RAZOR_INSTALL_ACTION_REMOVE)
   740 			continue;
   741 
   742 		razor_package_get_details(set, package,
   743 					  RAZOR_DETAIL_NAME, &name,
   744 					  RAZOR_DETAIL_VERSION, &version,
   745 					  RAZOR_DETAIL_ARCH, &arch,
   746 					  RAZOR_DETAIL_LAST);
   747 		
   748 		snprintf(url, sizeof url,
   749 			 "%s/Packages/%s",
   750 			 yum_url, rpm_filename(name, version, arch));
   751 		snprintf(file, sizeof file,
   752 			 "rpms/%s", rpm_filename(name, version, arch));
   753 		if (download_if_missing(url, file) < 0)
   754 			errors++;
   755 	}
   756 	razor_install_iterator_destroy(ii);
   757 
   758 	if (errors > 0) {
   759 		fprintf(stderr, "failed to download %d packages\n", errors);
   760                 return -1;
   761         }
   762 
   763 	return 0;
   764 }
   765 
   766 static struct razor_set *
   767 relocate_packages(struct razor_set *set, struct razor_relocations *relocations)
   768 {
   769 	struct razor_importer *importer;
   770 	struct razor_property_iterator *prop_iter;
   771 	struct razor_package_iterator *pkg_iter;
   772  	struct razor_file_iterator *file_iter;
   773  	struct razor_package *package;
   774 	struct razor_property *property;
   775 	struct razor_rpm *rpm;
   776 	const char *name, *version, *arch, *summary, *desc, *url, *license;
   777 	char file[PATH_MAX];
   778 	uint32_t flags;
   779 
   780 	importer = razor_importer_create();
   781 	pkg_iter = razor_package_iterator_create(set);
   782 
   783 	while (razor_package_iterator_next(pkg_iter, &package,
   784 					   RAZOR_DETAIL_NAME, &name,
   785 					   RAZOR_DETAIL_VERSION, &version,
   786 					   RAZOR_DETAIL_ARCH, &arch,
   787 					   RAZOR_DETAIL_SUMMARY, &summary,
   788 					   RAZOR_DETAIL_DESCRIPTION, &desc,
   789 					   RAZOR_DETAIL_URL, &url,
   790 					   RAZOR_DETAIL_LICENSE, &license,
   791 					   RAZOR_DETAIL_LAST)) {
   792 		snprintf(file, sizeof file,
   793 			 "rpms/%s", rpm_filename(name, version, arch));
   794 		rpm = razor_rpm_open(file);
   795 		if (rpm == NULL) {
   796 			fprintf(stderr, "failed to open rpm %s\n", file);
   797 			razor_package_iterator_destroy(pkg_iter);
   798 			razor_importer_destroy(importer);
   799 			return NULL;
   800 		}
   801 
   802 		razor_relocations_set_rpm(relocations, rpm);
   803 		razor_rpm_close(rpm);
   804 
   805 		razor_importer_begin_package(importer, name, version, arch);
   806 		razor_importer_add_details(importer,
   807 					   summary, desc, url, license);
   808 
   809 		prop_iter = razor_property_iterator_create(set, package);
   810 		while (razor_property_iterator_next(prop_iter, &property,
   811 						    &name, &flags, &version))
   812 			razor_importer_add_property(importer,
   813 						    name, flags, version);
   814 		razor_property_iterator_destroy(prop_iter);
   815 
   816 		file_iter = razor_file_iterator_create(set, package);
   817 		while (razor_file_iterator_next(file_iter, &name)) {
   818 			name = razor_relocations_apply(relocations, name);
   819 			razor_importer_add_file(importer, name);
   820 		}
   821 		razor_file_iterator_destroy(file_iter);
   822 
   823 		razor_importer_finish_package(importer);
   824 	}
   825 
   826 	razor_package_iterator_destroy(pkg_iter);
   827 	return razor_importer_finish(importer);
   828 }
   829 
   830 static int
   831 install_package(struct razor_set *set, struct razor_package *package,
   832 		struct razor_relocations *relocations)
   833 {
   834 	int retval;
   835 	const char *name, *version, *arch;
   836 	char file[PATH_MAX];
   837 	struct razor_rpm *rpm;
   838 
   839 	razor_package_get_details(set, package,
   840 				  RAZOR_DETAIL_NAME, &name,
   841 				  RAZOR_DETAIL_VERSION, &version,
   842 				  RAZOR_DETAIL_ARCH, &arch,
   843 				  RAZOR_DETAIL_LAST);
   844 
   845 	printf("install %s-%s\n", name, version);
   846 
   847 	snprintf(file, sizeof file,
   848 		 "rpms/%s", rpm_filename(name, version, arch));
   849 	rpm = razor_rpm_open(file);
   850 	if (rpm == NULL) {
   851 		fprintf(stderr, "failed to open rpm %s\n", file);
   852 		return -1;
   853 	}
   854 	if (relocations)
   855 		razor_rpm_set_relocations(rpm, relocations);
   856 	retval = razor_rpm_install(rpm, install_root);
   857 	if (retval < 0)
   858 		fprintf(stderr, "failed to install rpm %s\n", file);
   859 	razor_rpm_close(rpm);
   860 	return retval;
   861 }
   862 
   863 static int
   864 remove_package(struct razor_set *set, struct razor_package *package)
   865 {
   866 	struct razor_file_iterator *fi;
   867 	struct razor_package_iterator *pi;
   868 	struct razor_package *p;
   869 	char buffer[PATH_MAX];
   870 	const char *name;
   871 	int retval = 0, count;
   872 
   873 	fi = razor_file_iterator_create(set, package);
   874 
   875 	while (!retval && razor_file_iterator_next(fi, &name)) {
   876 		pi = razor_package_iterator_create_for_file(set, name);
   877 		count = 0;
   878 		while (razor_package_iterator_next(pi, &p, RAZOR_DETAIL_LAST))
   879 			count++;
   880 		razor_package_iterator_destroy(pi);
   881 		if (count <= 1) {
   882 			snprintf(buffer, sizeof buffer, "%s%s", install_root,
   883 				 name);
   884 			retval = remove(buffer);
   885 		}
   886 	}
   887 
   888 	razor_file_iterator_destroy(fi);
   889 	return retval;
   890 }
   891 
   892 static int
   893 update_packages(struct razor_set *system, struct razor_set *next,
   894 		struct razor_relocations *relocations)
   895 {
   896 	struct razor_install_iterator *ii;
   897 	struct razor_package *package;
   898 	struct razor_set *set;
   899 	enum razor_install_action action;
   900 	int retval = 0, count;
   901 
   902 	ii = razor_set_create_install_iterator(system, next);
   903 	while (!retval && razor_install_iterator_next(ii, &set, &package,
   904 						      &action, &count)) {
   905 		if (action == RAZOR_INSTALL_ACTION_ADD)
   906 			retval = install_package(set, package, relocations);
   907 		else if (action == RAZOR_INSTALL_ACTION_REMOVE)
   908 			retval = remove_package(set, package);
   909 	}
   910 	razor_install_iterator_destroy(ii);
   911 
   912 	return retval;
   913 }
   914 
   915 static int
   916 command_install(int argc, const char *argv[])
   917 {
   918 	struct razor_root *root;
   919 	struct razor_relocations *relocations=NULL;
   920 	struct razor_set *system, *upstream, *next, *set;
   921 	struct razor_transaction *trans;
   922 	int i, len, dependencies = 1;
   923 	char *oldpath;
   924 
   925 	root = razor_root_open(install_root);
   926 	if (root == NULL)
   927 		return 1;
   928 
   929 	for (i = 0; i < argc; i++) {
   930 		if (strcmp(argv[i], "--no-dependencies") == 0)
   931 			dependencies = 0;
   932 		else if (strcmp(argv[i], "--relocate") == 0) {
   933 			i++;
   934 			if (i >= argc || strchr(argv[i], '=') == NULL) {
   935 				fprintf(stderr,
   936 				    "Usage: razor install [OPTION...] RPM\n");
   937 				fprintf(stderr, "Options:\n");
   938 				fprintf(stderr, "    [--no-dependencies]\n");
   939 				fprintf(stderr,
   940 				    "    [--relocate OLDPATH=NEWPATH] RPM\n");
   941 				return -1;
   942 			}
   943 			len = strchr(argv[i], '=') - argv[i];
   944 			oldpath = malloc(len + 1);
   945 			strncpy(oldpath, argv[i], len);
   946 			oldpath[len] = '\0';
   947 			if (!relocations)
   948 			       relocations = razor_relocations_create();
   949 			razor_relocations_add(relocations, oldpath,
   950 					      argv[i] + len + 1);
   951 			free(oldpath);
   952 		} else
   953 			break;
   954 	}
   955 
   956 	system = razor_root_get_system_set(root);
   957 	upstream = razor_set_open(rawhide_repo_filename);
   958 	if (upstream == NULL ||
   959 	    razor_set_open_details(upstream, "rawhide-details.rzdb") ||
   960 	    razor_set_open_files(upstream, "rawhide-files.rzdb")) {
   961 			fprintf(stderr, "couldn't open rawhide repo\n");
   962 			razor_root_close(root);
   963 			return 1;
   964 	}		
   965 
   966 	if (relocations) {
   967 		set = relocate_packages(upstream, relocations);
   968 		razor_set_destroy(upstream);
   969 		upstream = set;
   970 	}
   971 
   972 	trans = razor_transaction_create(system, upstream);
   973 
   974 	for (; i < argc; i++) {
   975 		if (mark_packages_for_update(trans, upstream, argv[i]) == 0) {
   976 			fprintf(stderr, "no package matched %s\n", argv[i]);
   977 			razor_root_close(root);
   978 			return 1;
   979 		}
   980 	}
   981 
   982 	if (dependencies) {
   983 		razor_transaction_resolve(trans);
   984 		if (razor_transaction_describe(trans) > 0) {
   985 			razor_root_close(root);
   986 			return 1;
   987 		}
   988 	}
   989 
   990 	next = razor_transaction_finish(trans);
   991 
   992 	razor_root_update(root, next);
   993 
   994 	if (mkdir("rpms", 0777) && errno != EEXIST) {
   995 		fprintf(stderr, "failed to create rpms directory.\n");
   996 		razor_root_close(root);
   997 		return 1;
   998 	}
   999 
  1000 	if (download_packages(system, next) < 0) {
  1001 		razor_root_close(root);
  1002                 return 1;
  1003         }
  1004 
  1005 	update_packages(system, next, relocations);
  1006 
  1007 	if (relocations)
  1008 		razor_relocations_destroy(relocations);
  1009 	razor_set_destroy(next);
  1010 	razor_set_destroy(upstream);
  1011 
  1012 	return razor_root_commit(root);
  1013 }
  1014 
  1015 static int
  1016 command_init(int argc, const char *argv[])
  1017 {
  1018 	return razor_root_create(install_root);
  1019 }
  1020 
  1021 static int
  1022 command_download(int argc, const char *argv[])
  1023 {
  1024 	struct razor_set *set;
  1025 	struct razor_package_iterator *pi;
  1026 	struct razor_package *package;
  1027 	const char *pattern = argv[0], *name, *version, *arch;
  1028 	char url[256], file[256];
  1029 	int matches = 0;
  1030 
  1031 	if (mkdir("rpms", 0777) && errno != EEXIST) {
  1032 		fprintf(stderr, "failed to create rpms directory.\n");
  1033 		return 1;
  1034 	}
  1035 
  1036 	set = razor_set_open(rawhide_repo_filename);
  1037 	pi = razor_package_iterator_create(set);
  1038 	while (razor_package_iterator_next(pi, &package,
  1039 					   RAZOR_DETAIL_NAME, &name,
  1040 					   RAZOR_DETAIL_VERSION, &version,
  1041 					   RAZOR_DETAIL_ARCH, &arch,
  1042 					   RAZOR_DETAIL_LAST)) {
  1043 		if (pattern && fnmatch(pattern, name, 0) != 0)
  1044 			continue;
  1045 
  1046 		matches++;
  1047 		snprintf(url, sizeof url,
  1048 			 "%s/Packages/%s-%s.%s.rpm",
  1049 			 yum_url, name, version, arch);
  1050 		snprintf(file, sizeof file,
  1051 			 "rpms/%s-%s.%s.rpm", name, version, arch);
  1052 		download_if_missing(url, file);
  1053 	}
  1054 	razor_package_iterator_destroy(pi);
  1055 	razor_set_destroy(set);
  1056 
  1057 	if (matches == 0)
  1058 		fprintf(stderr, "no packages matched \"%s\"\n", pattern);
  1059 	else if (matches == 1)
  1060 		fprintf(stderr, "downloaded 1 package\n");
  1061 	else
  1062 		fprintf(stderr, "downloaded %d packages\n", matches);
  1063 
  1064 	return 0;
  1065 }
  1066 
  1067 static int
  1068 command_info(int argc, const char *argv[])
  1069 {
  1070 	struct razor_set *set;
  1071 	struct razor_package_iterator *pi;
  1072 	struct razor_package *package;
  1073 	const char *pattern = argv[0], *name, *version, *arch;
  1074 	const char *summary, *description, *url, *license;
  1075 
  1076 	set = razor_root_open_read_only(install_root);
  1077 	if (set == NULL)
  1078 		return 1;
  1079 
  1080 	pi = razor_package_iterator_create(set);
  1081 	while (razor_package_iterator_next(pi, &package,
  1082 					   RAZOR_DETAIL_NAME, &name,
  1083 					   RAZOR_DETAIL_VERSION, &version,
  1084 					   RAZOR_DETAIL_ARCH, &arch,
  1085 					   RAZOR_DETAIL_LAST)) {
  1086 		if (pattern && fnmatch(pattern, name, 0) != 0)
  1087 			continue;
  1088 
  1089 		razor_package_get_details (set, package,
  1090 					   RAZOR_DETAIL_SUMMARY, &summary,
  1091 					   RAZOR_DETAIL_DESCRIPTION, &description,
  1092 					   RAZOR_DETAIL_URL, &url,
  1093 					   RAZOR_DETAIL_LICENSE, &license,
  1094 					   RAZOR_DETAIL_LAST);
  1095 
  1096 		printf ("Name:        %s\n", name);
  1097 		printf ("Arch:        %s\n", arch);
  1098 		printf ("Version:     %s\n", version);
  1099 		printf ("URL:         %s\n", url);
  1100 		printf ("License:     %s\n", license);
  1101 		printf ("Summary:     %s\n", summary);
  1102 		printf ("Description:\n");
  1103 		printf ("%s\n", description);
  1104 		printf ("\n");
  1105 	}
  1106 	razor_package_iterator_destroy(pi);
  1107 	razor_set_destroy(set);
  1108 
  1109 	return 0;
  1110 }
  1111 
  1112 #define SEARCH_MAX 256
  1113 
  1114 static int
  1115 command_search(int argc, const char *argv[])
  1116 {
  1117 	struct razor_set *set;
  1118 	struct razor_package_iterator *pi;
  1119 	struct razor_package *package;
  1120 	char pattern[SEARCH_MAX];
  1121 	const char *name, *version, *arch;
  1122 	const char *summary, *description, *url, *license;
  1123 
  1124 	if (!argv[0]) {
  1125 		fprintf(stderr, "must specify a search term\n");
  1126 		return 1;
  1127 	}
  1128 
  1129 	snprintf(pattern, sizeof pattern, "*%s*", argv[0]);
  1130 
  1131 	set = razor_set_open(rawhide_repo_filename);
  1132 	if (set == NULL)
  1133 		return 1;
  1134 
  1135 	if (razor_set_open_details(set, "rawhide-details.rzdb"))
  1136 		return 1;
  1137 
  1138 	pi = razor_package_iterator_create(set);
  1139 	while (razor_package_iterator_next(pi, &package,
  1140 					   RAZOR_DETAIL_NAME, &name,
  1141 					   RAZOR_DETAIL_VERSION, &version,
  1142 					   RAZOR_DETAIL_ARCH, &arch,
  1143 					   RAZOR_DETAIL_SUMMARY, &summary,
  1144 					   RAZOR_DETAIL_DESCRIPTION, &description,
  1145 					   RAZOR_DETAIL_URL, &url,
  1146 					   RAZOR_DETAIL_LICENSE, &license,
  1147 					   RAZOR_DETAIL_LAST)) {
  1148 		if (!fnmatch(pattern, name, FNM_CASEFOLD) ||
  1149 		    !fnmatch(pattern, url, FNM_CASEFOLD) ||
  1150 		    !fnmatch(pattern, summary, FNM_CASEFOLD) ||
  1151 		    !fnmatch(pattern, description, FNM_CASEFOLD))
  1152 			printf("%s-%s.%s: %s\n", name, version, arch, summary);
  1153 	}
  1154 	razor_package_iterator_destroy(pi);
  1155 	razor_set_destroy(set);
  1156 
  1157 	return 0;
  1158 }
  1159 
  1160 static struct {
  1161 	const char *name;
  1162 	const char *description;
  1163 	int (*func)(int argc, const char *argv[]);
  1164 } razor_commands[] = {
  1165 	{ "list", "list all packages", command_list },
  1166 	{ "list-requires", "list all requires for the given package", command_list_requires },
  1167 	{ "list-provides", "list all provides for the given package", command_list_provides },
  1168 	{ "list-obsoletes", "list all obsoletes for the given package", command_list_obsoletes },
  1169 	{ "list-conflicts", "list all conflicts for the given package", command_list_conflicts },
  1170 	{ "list-files", "list files for package set", command_list_files },
  1171 	{ "list-file-packages", "list packages owning file", command_list_file_packages },
  1172 	{ "list-package-files", "list files in package", command_list_package_files },
  1173 	{ "what-requires", "list the packages that have the given requires", command_what_requires },
  1174 	{ "what-provides", "list the packages that have the given provides", command_what_provides },
  1175 	{ "import-yum", "import yum metadata files", command_import_yum },
  1176 #if HAVE_RPMLIB
  1177 	{ "import-rpmdb", "import the system rpm database", command_import_rpmdb },
  1178 #endif
  1179 	{ "import-rpms", "import rpms from the given directory", command_import_rpms },
  1180 	{ "update", "update all or specified packages", command_update },
  1181 	{ "remove", "remove specified packages", command_remove },
  1182 	{ "diff", "show diff between two package sets", command_diff },
  1183 	{ "install", "install rpm", command_install },
  1184 	{ "init", "init razor root", command_init },
  1185 	{ "download", "download packages", command_download },
  1186 	{ "info", "display package details", command_info },
  1187 	{ "search", "search package details", command_search }
  1188 };
  1189 
  1190 static int
  1191 usage(void)
  1192 {
  1193 	int i;
  1194 
  1195 	printf("usage:\n");
  1196 	for (i = 0; i < ARRAY_SIZE(razor_commands); i++)
  1197 		printf("  %-20s%s\n",
  1198 		       razor_commands[i].name, razor_commands[i].description);
  1199 
  1200 	return 1;
  1201 }
  1202 
  1203 int
  1204 main(int argc, const char *argv[])
  1205 {
  1206 	char *repo, *root;
  1207 	int i;
  1208 
  1209 	repo = getenv("RAZOR_REPO");
  1210 	if (repo != NULL)
  1211 		repo_filename = repo;
  1212 
  1213 	root = getenv("RAZOR_ROOT");
  1214 	if (root != NULL)
  1215 		install_root = root;
  1216 
  1217 	yum_url = getenv("YUM_URL");
  1218 	if (yum_url == NULL)
  1219 		yum_url = YUM_URL;
  1220 
  1221 	if (getenv("RAZOR_NO_ROOT_NAME_CHECKS"))
  1222 		razor_disable_root_name_checks(1);
  1223 
  1224 	if (argc < 2)
  1225 		return usage();
  1226 
  1227 	for (i = 0; i < ARRAY_SIZE(razor_commands); i++)
  1228 		if (strcmp(razor_commands[i].name, argv[1]) == 0)
  1229 			return razor_commands[i].func(argc - 2, argv + 2);
  1230 
  1231 	return usage();
  1232 }