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