main.c
author Kristian H?gsberg <krh@redhat.com>
Mon Jun 09 15:39:23 2008 -0400 (2008-06-09)
changeset 232 2389f44500bb
parent 231 fef9808171ff
child 234 7f5d32472bef
permissions -rw-r--r--
Introduce struct razor_root.

This object encapsulates the directory layout and locking protocol for
a razor install root.
     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 /* The image data struct encapsulates filesystem conventions and the
   567  * locking protocol.  FIXME: Should this be razor_root?*/
   568 struct razor_root {
   569 	struct razor_set *system;
   570 	int fd;
   571 	char path[PATH_MAX];
   572 	char new_path[PATH_MAX];
   573 };
   574 
   575 #define RAZOR_ROOT_OPEN_WRITE 0x01
   576 
   577 int
   578 razor_root_create(const char *root);
   579 struct razor_root *
   580 razor_root_open(const char *root, int flags);
   581 struct razor_transaction *
   582 razor_root_create_transaction(struct razor_root *image,
   583 			      struct razor_set *upstream);
   584 int
   585 razor_root_close(struct razor_root *image);
   586 void
   587 razor_root_update(struct razor_root *image, struct razor_set *next);
   588 int
   589 razor_root_commit(struct razor_root *image);
   590 
   591 int
   592 razor_root_create(const char *root)
   593 {	
   594 	struct stat buf;
   595 	struct razor_set *set;
   596 	char path[PATH_MAX];
   597 
   598 	if (stat(root, &buf) < 0) {
   599 		if (mkdir(root, 0777) < 0) {
   600 			fprintf(stderr,
   601 				"could not create install root \"%s\"\n",
   602 				root);
   603 			return -1;
   604 		}
   605 		fprintf(stderr, "created install root \"%s\"\n", root);
   606 	} else if (!S_ISDIR(buf.st_mode)) {
   607 		fprintf(stderr,
   608 			"install root \"%s\" exists, but is not a directory\n",
   609 			root);
   610 		return -1;
   611 	}
   612 
   613 	snprintf(path, sizeof path, "%s/%s",
   614 		 razor_root_path, system_repo_filename);
   615 	if (razor_create_dir(root, path) < 0) {
   616 		fprintf(stderr, "could not create %s%s\n",
   617 			root, razor_root_path);
   618 		return -1;
   619 	}
   620 
   621 	set = razor_set_create();
   622 	snprintf(path, sizeof path, "%s%s/%s",
   623 		 root, razor_root_path, system_repo_filename);
   624 	if (stat(root, &buf) == 0) {
   625 		fprintf(stderr,
   626 			"a razor install root is already initialized\n");
   627 		return -1;
   628 	}
   629 	if (razor_set_write(set, path) < 0) {
   630 		fprintf(stderr, "could not write initial package set\n");
   631 		return -1;
   632 	}
   633 	razor_set_destroy(set);
   634 
   635 	return 0;
   636 }
   637 
   638 struct razor_root *
   639 razor_root_open(const char *root, int flags)
   640 {
   641 	struct razor_root *image;
   642 
   643 	image = malloc(sizeof *image);
   644 	if (image == NULL)
   645 		return NULL;
   646 
   647 	/* Create the new next repo file up front to ensure exclusive
   648 	 * access. */
   649 	snprintf(image->new_path, sizeof image->new_path,
   650 		 "%s%s/%s", root, root, next_repo_filename);
   651 	image->fd = open(image->new_path,
   652 			 O_CREAT | O_WRONLY | O_TRUNC | O_EXCL, 0666);
   653 	if (image->fd < 0) {
   654 		fprintf(stderr, "failed to get lock file, "
   655 			"maybe previous operation crashed?\n");
   656 
   657 		/* FIXME: Use fcntl advisory locking on the system
   658 		 * package set file to figure out whether previous
   659 		 * operation crashed or is still in progress. */
   660 
   661 		free(image);
   662 		return NULL;
   663 	}
   664 
   665 	snprintf(image->path, sizeof image->path,
   666 		 "%s%s/%s", root, razor_root_path, system_repo_filename);
   667 	image->system = razor_set_open(image->path);
   668 	if (image->system == NULL) {
   669 		unlink(image->new_path);
   670 		close(image->fd);
   671 		free(image);
   672 		return NULL;
   673 	}
   674 
   675 	return image;
   676 }
   677 
   678 struct razor_transaction *
   679 razor_root_create_transaction(struct razor_root *image,
   680 			      struct razor_set *upstream)
   681 {
   682 	/* FIXME: This should take a number of upstream repos. */
   683 	return razor_transaction_create(image->system, upstream);
   684 }
   685 
   686 int
   687 razor_root_close(struct razor_root *image)
   688 {
   689 	unlink(image->new_path);
   690 	close(image->fd);
   691 	free(image);
   692 
   693 	return 0;
   694 }
   695 
   696 void
   697 razor_root_update(struct razor_root *image, struct razor_set *next)
   698 {
   699 	razor_set_write_to_fd(next, image->fd);
   700 
   701 	/* Sync the new repo file so the new package set is on disk
   702 	 * before we start upgrading. */
   703 	fsync(image->fd);
   704 	printf("wrote %s\n", image->new_path);
   705 }
   706 
   707 int
   708 razor_root_commit(struct razor_root *image)
   709 {
   710 	/* Make it so. */
   711 	rename(image->new_path, image->path);
   712 	printf("renamed %s to %s\n", image->new_path, image->path);
   713 	close(image->fd);
   714 	free(image);
   715 
   716 	return 0;
   717 }
   718 
   719 static void
   720 download_package(const char *name,
   721 		 const char *old_version,
   722 		 const char *new_version,
   723 		 const char *arch,
   724 		 void *data)
   725 {
   726 	char file[PATH_MAX], url[256];
   727 	const char *v;
   728 	int *errors = data;
   729 
   730 	if (old_version)
   731 		return;
   732 
   733 	/* Skip epoch */
   734 	v = strchr(new_version, ':');
   735 	if (v != NULL)
   736 		v = v + 1;
   737 	else
   738 		v = new_version;
   739 
   740 	snprintf(url, sizeof url,
   741 		 "%s/Packages/%s-%s.%s.rpm", yum_url, name, v, arch);
   742 	snprintf(file, sizeof file,
   743 		 "rpms/%s-%s.%s.rpm", name, v, arch);
   744 	if (download_if_missing(url, file) < 0)
   745 		(*errors)++;
   746 }
   747 
   748 static void
   749 install_package(const char *name,
   750 		const char *old_version,
   751 		const char *new_version,
   752 		const char *arch,
   753 		void *data)
   754 {
   755 	const char *v, *root = data;
   756 	char file[PATH_MAX];
   757 	struct razor_rpm *rpm;
   758 
   759 	if (old_version) {
   760 		printf("removing %s %s not handled\n", name, old_version);
   761 		return;
   762 	}
   763 
   764 	/* Skip epoch */
   765 	v = strchr(new_version, ':');
   766 	if (v != NULL)
   767 		v = v + 1;
   768 	else
   769 		v = new_version;
   770 
   771 	printf("install %s %s\n", name, v);
   772 	snprintf(file, sizeof file, "rpms/%s-%s.%s.rpm", name, v, arch);
   773 
   774  	rpm = razor_rpm_open(file);
   775 	if (rpm == NULL) {
   776 		fprintf(stderr, "failed to open rpm %s\n", file);
   777 		return;
   778 	}
   779 	if (razor_rpm_install(rpm, root) < 0) {
   780 		fprintf(stderr,
   781 			"failed to install rpm %s\n", file);
   782 		return;
   783 	}
   784 	razor_rpm_close(rpm);
   785 }
   786 
   787 static int
   788 command_install(int argc, const char *argv[])
   789 {
   790 	struct razor_root *root;
   791 	struct razor_set *upstream, *next;
   792 	struct razor_transaction *trans;
   793 	int i = 0, errors, dependencies = 1;
   794 
   795 	if (i < argc && strcmp(argv[i], "--no-dependencies") == 0) {
   796 		dependencies = 0;
   797 		i++;
   798 	}
   799 
   800 	root = razor_root_open(razor_root_path, RAZOR_ROOT_OPEN_WRITE);
   801 	upstream = razor_set_open(rawhide_repo_filename);
   802 	trans = razor_root_create_transaction(root, upstream);
   803 
   804 	for (; i < argc; i++) {
   805 		if (mark_packages_for_update(trans, upstream, argv[i]) == 0) {
   806 			fprintf(stderr, "no package matched %s\n", argv[i]);
   807 			razor_root_close(root);
   808 			return 1;
   809 		}
   810 	}
   811 
   812 	if (dependencies) {
   813 		errors = razor_transaction_resolve(trans);
   814 		if (errors) {
   815 			razor_root_close(root);
   816 			return 1;
   817 		}
   818 	}
   819 
   820 	next = razor_transaction_finish(trans);
   821 
   822 	razor_root_update(root, next);
   823 
   824 	if (mkdir("rpms", 0777) && errno != EEXIST) {
   825 		fprintf(stderr, "failed to create rpms directory.\n");
   826 		razor_root_close(root);
   827 		return 1;
   828 	}
   829 
   830 	razor_set_diff(root->system, next, download_package, &errors);
   831 	if (errors > 0) {
   832 		fprintf(stderr, "failed to download %d packages\n", errors);
   833 		razor_root_close(root);
   834                 return 1;
   835         }
   836 
   837 	/* FIXME: We need to figure out the right install order here,
   838 	 * so the post and pre scripts can run. */
   839 	razor_set_diff(root->system, next, install_package, (void *) root);
   840 
   841 	razor_set_destroy(next);
   842 	razor_set_destroy(upstream);
   843 
   844 	return razor_root_commit(root);
   845 }
   846 
   847 static int
   848 command_init(int argc, const char *argv[])
   849 {
   850 	return razor_root_create(root);
   851 }
   852 
   853 static int
   854 command_download(int argc, const char *argv[])
   855 {
   856 	struct razor_set *set;
   857 	struct razor_package_iterator *pi;
   858 	struct razor_package *package;
   859 	const char *pattern = argv[0], *name, *version, *arch;
   860 	char url[256], file[256];
   861 	int matches = 0;
   862 
   863 	if (mkdir("rpms", 0777) && errno != EEXIST) {
   864 		fprintf(stderr, "failed to create rpms directory.\n");
   865 		return 1;
   866 	}
   867 
   868 	set = razor_set_open(rawhide_repo_filename);
   869 	pi = razor_package_iterator_create(set);
   870 	while (razor_package_iterator_next(pi, &package,
   871 					   &name, &version, &arch)) {
   872 		if (pattern && fnmatch(pattern, name, 0) != 0)
   873 			continue;
   874 
   875 		matches++;
   876 		snprintf(url, sizeof url,
   877 			 "%s/Packages/%s-%s.%s.rpm",
   878 			 yum_url, name, version, arch);
   879 		snprintf(file, sizeof file,
   880 			 "rpms/%s-%s.%s.rpm", name, version, arch);
   881 		download_if_missing(url, file);
   882 	}
   883 	razor_package_iterator_destroy(pi);
   884 	razor_set_destroy(set);
   885 
   886 	if (matches == 0)
   887 		fprintf(stderr, "no packages matched \"%s\"\n", pattern);
   888 	else if (matches == 1)
   889 		fprintf(stderr, "downloaded 1 package\n");
   890 	else
   891 		fprintf(stderr, "downloaded %d packages\n", matches);
   892 
   893 	return 0;
   894 }
   895 
   896 static struct {
   897 	const char *name;
   898 	const char *description;
   899 	int (*func)(int argc, const char *argv[]);
   900 } razor_commands[] = {
   901 	{ "list", "list all packages", command_list },
   902 	{ "list-requires", "list all requires for the given package", command_list_requires },
   903 	{ "list-provides", "list all provides for the given package", command_list_provides },
   904 	{ "list-obsoletes", "list all obsoletes for the given package", command_list_obsoletes },
   905 	{ "list-conflicts", "list all conflicts for the given package", command_list_conflicts },
   906 	{ "list-files", "list files for package set", command_list_files },
   907 	{ "list-file-packages", "list packages owning file", command_list_file_packages },
   908 	{ "list-package-files", "list files in package", command_list_package_files },
   909 	{ "what-requires", "list the packages that have the given requires", command_what_requires },
   910 	{ "what-provides", "list the packages that have the given provides", command_what_provides },
   911 	{ "import-yum", "import yum metadata files", command_import_yum },
   912 	{ "import-rpmdb", "import the system rpm database", command_import_rpmdb },
   913 	{ "import-rpms", "import rpms from the given directory", command_import_rpms },
   914 	{ "update", "update all or specified packages", command_update },
   915 	{ "remove", "remove specified packages", command_remove },
   916 	{ "diff", "show diff between two package sets", command_diff },
   917 	{ "install", "install rpm", command_install },
   918 	{ "init", "init razor root", command_init },
   919 	{ "download", "download packages", command_download }
   920 };
   921 
   922 static int
   923 usage(void)
   924 {
   925 	int i;
   926 
   927 	printf("usage:\n");
   928 	for (i = 0; i < ARRAY_SIZE(razor_commands); i++)
   929 		printf("  %-20s%s\n",
   930 		       razor_commands[i].name, razor_commands[i].description);
   931 
   932 	return 1;
   933 }
   934 
   935 int
   936 main(int argc, const char *argv[])
   937 {
   938 	char *repo;
   939 	int i;
   940 
   941 	repo = getenv("RAZOR_REPO");
   942 	if (repo != NULL)
   943 		repo_filename = repo;
   944 
   945 	yum_url = getenv("YUM_URL");
   946 	if (yum_url == NULL)
   947 		yum_url = YUM_URL;
   948 
   949 	if (argc < 2)
   950 		return usage();
   951 
   952 	for (i = 0; i < ARRAY_SIZE(razor_commands); i++)
   953 		if (strcmp(razor_commands[i].name, argv[1]) == 0)
   954 			return razor_commands[i].func(argc - 2, argv + 2);
   955 
   956 	return usage();
   957 }