main.c
author Kristian H?gsberg <krh@redhat.com>
Sun Apr 06 18:40:53 2008 -0400 (2008-04-06)
changeset 189 4b7eca63fb6d
parent 187 6ff03223ce4c
child 190 d8b7dd11813d
permissions -rw-r--r--
Make a note about how we don't handle release-less properties.

Such as requires: glibc > 2.6.90.
     1 #include <stdlib.h>
     2 #include <stddef.h>
     3 #include <stdio.h>
     4 #include <stdint.h>
     5 #include <string.h>
     6 #include <sys/stat.h>
     7 #include <unistd.h>
     8 #include <dirent.h>
     9 #include <curl/curl.h>
    10 #include <fnmatch.h>
    11 #include "razor.h"
    12 #include "razor-internal.h"
    13 
    14 static const char system_repo_filename[] = "system.repo";
    15 static const char next_repo_filename[] = "system-next.repo";
    16 static const char rawhide_repo_filename[] = "rawhide.repo";
    17 static const char updated_repo_filename[] = "system-updated.repo";
    18 static const char razor_root_path[] = "/var/lib/razor";
    19 static const char root[] = "install";
    20 static const char *repo_filename = system_repo_filename;
    21 
    22 static int
    23 command_list(int argc, const char *argv[])
    24 {
    25 	struct razor_set *set;
    26 	struct razor_package_iterator *pi;
    27 	struct razor_package *package;
    28 	const char *pattern, *name, *version;
    29 	int only_names = 0, i = 0;
    30 
    31 	if (strcmp(argv[i], "--only-names") == 0) {
    32 		only_names = 1;
    33 		i++;
    34 	}
    35 
    36 	pattern = argv[i];
    37 	set = razor_set_open(repo_filename);
    38 	pi = razor_package_iterator_create(set);
    39 	while (razor_package_iterator_next(pi, &package, &name, &version)) {
    40 		if (pattern && fnmatch(pattern, name, 0) != 0)
    41 			continue;
    42 
    43 		if (only_names)
    44 			printf("%s\n", name);
    45 		else
    46 			printf("%s-%s\n", name, version);
    47 	}
    48 	razor_package_iterator_destroy(pi);
    49 	razor_set_destroy(set);
    50 
    51 	return 0;
    52 }
    53 
    54 static int
    55 list_properties(const char *package_name,
    56 		enum razor_property_type required_type)
    57 {
    58 	struct razor_set *set;
    59 	struct razor_property *property;
    60 	struct razor_package *package;
    61 	struct razor_property_iterator *pi;
    62 	const char *name, *version;
    63 	enum razor_property_type type;
    64 	enum razor_version_relation relation;
    65 
    66 	set = razor_set_open(repo_filename);
    67 	if (package_name)
    68 		package = razor_set_get_package(set, package_name);
    69 	else
    70 		package = NULL;
    71 
    72 	pi = razor_property_iterator_create(set, package);
    73 	while (razor_property_iterator_next(pi, &property,
    74 					    &name, &relation, &version,
    75 					    &type)) {
    76 		if (type != required_type)
    77 			continue;
    78 		if (version[0] == '\0')
    79 			printf("%s\n", name);
    80 		else
    81 			printf("%s %s %s\n", name,
    82 			       razor_version_relations[relation], version);
    83 	}
    84 	razor_property_iterator_destroy(pi);
    85 
    86 	razor_set_destroy(set);
    87 
    88 	return 0;
    89 }
    90 
    91 static int
    92 command_list_requires(int argc, const char *argv[])
    93 {
    94 	return list_properties(argv[0], RAZOR_PROPERTY_REQUIRES);
    95 }
    96 
    97 static int
    98 command_list_provides(int argc, const char *argv[])
    99 {
   100 	return list_properties(argv[0], RAZOR_PROPERTY_PROVIDES);
   101 }
   102 
   103 static int
   104 command_list_obsoletes(int argc, const char *argv[])
   105 {
   106 	return list_properties(argv[0], RAZOR_PROPERTY_OBSOLETES);
   107 }
   108 
   109 static int
   110 command_list_conflicts(int argc, const char *argv[])
   111 {
   112 	return list_properties(argv[0], RAZOR_PROPERTY_CONFLICTS);
   113 }
   114 
   115 static int
   116 command_list_files(int argc, const char *argv[])
   117 {
   118 	struct razor_set *set;
   119 
   120 	set = razor_set_open(repo_filename);
   121 	if (set == NULL)
   122 		return 1;
   123 	razor_set_list_files(set, argv[0]);
   124 	razor_set_destroy(set);
   125 
   126 	return 0;
   127 }
   128 
   129 static int
   130 command_list_file_packages(int argc, const char *argv[])
   131 {
   132 	struct razor_set *set;
   133 	struct razor_package_iterator *pi;
   134 	struct razor_package *package;
   135 	const char *name, *version;
   136 
   137 	set = razor_set_open(repo_filename);
   138 	if (set == NULL)
   139 		return 1;
   140 
   141 	pi = razor_package_iterator_create_for_file(set, argv[0]);
   142 	while (razor_package_iterator_next(pi, &package, &name, &version))
   143 		printf("%s-%s\n", name, version);
   144 	razor_package_iterator_destroy(pi);
   145 
   146 	razor_set_destroy(set);
   147 
   148 	return 0;
   149 }
   150 
   151 static int
   152 command_list_package_files(int argc, const char *argv[])
   153 {
   154 	struct razor_set *set;
   155 
   156 	set = razor_set_open(repo_filename);
   157 	if (set == NULL)
   158 		return 1;
   159 	razor_set_list_package_files(set, argv[0]);
   160 	razor_set_destroy(set);
   161 
   162 	return 0;
   163 }
   164 
   165 static void
   166 list_packages_for_property(struct razor_set *set,
   167 			   struct razor_property *property)
   168 {
   169 	struct razor_package_iterator *pi;
   170 	struct razor_package *package;
   171 	const char *name, *version;
   172 
   173 	pi = razor_package_iterator_create_for_property(set, property);
   174 	while (razor_package_iterator_next(pi, &package, &name, &version))
   175 		printf("%s-%s\n", name, version);
   176 	razor_package_iterator_destroy(pi);
   177 }
   178 
   179 static int
   180 list_property_packages(const char *ref_name,
   181 		       const char *ref_version,
   182 		       enum razor_property_type ref_type)
   183 {
   184 	struct razor_set *set;
   185 	struct razor_property *property;
   186 	struct razor_property_iterator *pi;
   187 	const char *name, *version;
   188 	enum razor_property_type type;
   189 	enum razor_version_relation relation;
   190 
   191 	if (ref_name == NULL)
   192 		return 0;
   193 
   194 	set = razor_set_open(repo_filename);
   195 	if (set == NULL)
   196 		return 1;
   197 
   198 	pi = razor_property_iterator_create(set, NULL);
   199 	while (razor_property_iterator_next(pi, &property,
   200 					    &name, &relation, &version,
   201 					    &type)) {
   202 		if (strcmp(ref_name, name) != 0)
   203 			continue;
   204 		if (ref_version && relation == RAZOR_VERSION_EQUAL &&
   205 		    strcmp(ref_version, version) != 0)
   206 			continue;
   207 		if (ref_type != type)
   208 			continue;
   209 
   210 		list_packages_for_property(set, property);
   211 	}
   212 	razor_property_iterator_destroy(pi);
   213 
   214 	return 0;
   215 }
   216 
   217 static int
   218 command_what_requires(int argc, const char *argv[])
   219 {
   220 	return list_property_packages(argv[0], argv[1],
   221 				      RAZOR_PROPERTY_REQUIRES);
   222 }
   223 
   224 static int
   225 command_what_provides(int argc, const char *argv[])
   226 {
   227 	return list_property_packages(argv[0], argv[1],
   228 				      RAZOR_PROPERTY_PROVIDES);
   229 }
   230 
   231 static int
   232 show_progress(void *clientp,
   233 	      double dltotal, double dlnow, double ultotal, double ulnow)
   234 {
   235 	const char *file = clientp;
   236 
   237 	if (!dlnow < dltotal)
   238 		fprintf(stderr, "\rdownloading %s, %dkB/%dkB",
   239 			file, (int) dlnow / 1024, (int) dltotal / 1024);
   240 
   241 	return 0;
   242 }
   243 
   244 static int
   245 download_if_missing(CURL *curl, const char *url, const char *file)
   246 {
   247 	struct stat buf;
   248 	char error[256];
   249 	FILE *fp;
   250 	CURLcode res;
   251 
   252 	curl_easy_setopt(curl, CURLOPT_ERRORBUFFER, error);
   253 	curl_easy_setopt(curl, CURLOPT_NOPROGRESS, 0);
   254 	curl_easy_setopt(curl, CURLOPT_PROGRESSFUNCTION, show_progress);
   255 	curl_easy_setopt(curl, CURLOPT_PROGRESSDATA, file);
   256 
   257 	if (stat(file, &buf) < 0) {
   258 		fp = fopen(file, "w");
   259 		curl_easy_setopt(curl, CURLOPT_WRITEDATA, fp);
   260 		curl_easy_setopt(curl, CURLOPT_URL, url);
   261 		res = curl_easy_perform(curl);
   262 		fclose(fp);
   263 		if (res != CURLE_OK) {
   264 			fprintf(stderr, "curl error: %s\n", error);
   265 			unlink(file);
   266 			return -1;
   267 		}
   268 		fprintf(stderr, "\n");
   269 	}
   270 
   271 	return 0;
   272 }
   273 
   274 #define REPO_URL "http://download.fedora.redhat.com" \
   275 	"/pub/fedora/linux/development/i386/os"
   276 
   277 static int
   278 command_import_yum(int argc, const char *argv[])
   279 {
   280 	struct razor_set *set;
   281 	CURL *curl;
   282 
   283 	curl = curl_easy_init();
   284 	if (curl == NULL)
   285 		return 1;
   286 
   287 	if (download_if_missing(curl,
   288 				REPO_URL "/repodata/primary.xml.gz",
   289 				"primary.xml.gz") < 0)
   290 		return -1;
   291 	if (download_if_missing(curl,
   292 				REPO_URL "/repodata/filelists.xml.gz",
   293 				"filelists.xml.gz") < 0)
   294 		return -1;
   295 	curl_easy_cleanup(curl);
   296 
   297 	set = razor_set_create_from_yum();
   298 	if (set == NULL)
   299 		return 1;
   300 	razor_set_write(set, rawhide_repo_filename);
   301 	razor_set_destroy(set);
   302 	printf("wrote %s\n", rawhide_repo_filename);
   303 
   304 	return 0;
   305 }
   306 
   307 static int
   308 command_import_rpmdb(int argc, const char *argv[])
   309 {
   310 	struct razor_set *set;
   311 
   312 	set = razor_set_create_from_rpmdb();
   313 	if (set == NULL)
   314 		return 1;
   315 	razor_set_write(set, repo_filename);
   316 	razor_set_destroy(set);
   317 	printf("wrote %s\n", repo_filename);
   318 
   319 	return 0;
   320 }
   321 
   322 static int
   323 command_validate(int argc, const char *argv[])
   324 {
   325 	struct razor_set *set;
   326 
   327 	set = razor_set_open(repo_filename);
   328 	if (set == NULL)
   329 		return 1;
   330 	razor_set_list_unsatisfied(set);
   331 	razor_set_destroy(set);
   332 
   333 	return 0;
   334 }
   335 
   336 static int
   337 command_update(int argc, const char *argv[])
   338 {
   339 	struct razor_set *set, *upstream;
   340 	struct razor_transaction *trans;
   341 
   342 	set = razor_set_open(repo_filename);
   343 	upstream = razor_set_open(rawhide_repo_filename);
   344 	if (set == NULL || upstream == NULL)
   345 		return 1;
   346 	trans = razor_transaction_create(set, upstream, argc, argv, 0, NULL);
   347 	razor_transaction_describe(trans);
   348 	if (trans->errors)
   349 		return 1;
   350 
   351 	set = razor_transaction_run(trans);
   352 	razor_transaction_destroy(trans);
   353 	razor_set_write(set, updated_repo_filename);
   354 	razor_set_destroy(set);
   355 	razor_set_destroy(upstream);
   356 	printf("wrote system-updated.repo\n");
   357 
   358 	return 0;
   359 }
   360 
   361 static int
   362 command_remove(int argc, const char *argv[])
   363 {
   364 	struct razor_set *set;
   365 	struct razor_transaction *trans;
   366 
   367 	set = razor_set_open(repo_filename);
   368 	if (set == NULL)
   369 		return 1;
   370 	trans = razor_transaction_create(set, NULL, 0, NULL, argc, argv);
   371 	razor_transaction_describe(trans);
   372 	if (trans->errors)
   373 		return 1;
   374 
   375 	set = razor_transaction_run(trans);
   376 	razor_transaction_destroy(trans);
   377 	razor_set_write(set, updated_repo_filename);
   378 	razor_set_destroy(set);
   379 	printf("wrote system-updated.repo\n");
   380 
   381 	return 0;
   382 }
   383 
   384 static void
   385 print_diff(const char *name,
   386 	   const char *old_version, const char *new_version, void *data)
   387 {
   388 	if (old_version)
   389 		printf("removing %s %s\n", name, old_version);
   390 	else
   391 		printf("install %s %s\n", name, new_version);
   392 }
   393 
   394 static int
   395 command_diff(int argc, const char *argv[])
   396 {
   397 	struct razor_set *set, *updated;
   398 
   399 	set = razor_set_open(repo_filename);
   400 	updated = razor_set_open(updated_repo_filename);
   401 	if (set == NULL || updated == NULL)
   402 		return 1;
   403 
   404 	razor_set_diff(set, updated, print_diff, NULL);	
   405 
   406 	razor_set_destroy(set);
   407 	razor_set_destroy(updated);
   408 
   409 	return 0;
   410 }
   411 
   412 static int
   413 command_import_rpms(int argc, const char *argv[])
   414 {
   415 	DIR *dir;
   416 	struct dirent *de;
   417 	struct razor_importer *importer;
   418 	struct razor_set *set;
   419 	struct razor_rpm *rpm;
   420 	int len;
   421 	char filename[256];
   422 	const char *dirname = argv[0];
   423 
   424 	if (dirname == NULL) {
   425 		fprintf(stderr, "usage: razor import-rpms DIR\n");
   426 		return -1;
   427 	}
   428 
   429 	dir = opendir(dirname);
   430 	if (dir == NULL) {
   431 		fprintf(stderr, "couldn't read dir %s\n", dirname);
   432 		return -1;
   433 	}
   434 
   435 	importer = razor_importer_new();
   436 
   437 	while (de = readdir(dir), de != NULL) {
   438 		len = strlen(de->d_name);
   439 		if (len < 5 || strcmp(de->d_name + len - 4, ".rpm") != 0)
   440 		    continue;
   441 		snprintf(filename, sizeof filename,
   442 			 "%s/%s", dirname, de->d_name);
   443 		rpm = razor_rpm_open(filename);
   444 		if (rpm == NULL) {
   445 			fprintf(stderr,
   446 				"failed to open rpm \"%s\"\n", filename);
   447 			continue;
   448 		}
   449 		if (razor_importer_add_rpm(importer, rpm)) {
   450 			fprintf(stderr, "couldn't import %s\n", filename);
   451 			break;
   452 		}
   453 		razor_rpm_close(rpm);
   454 	}
   455 
   456 	if (de != NULL) {
   457 		razor_importer_destroy(importer);
   458 		return -1;
   459 	}
   460 
   461 	set = razor_importer_finish(importer);
   462 
   463 	razor_set_write(set, repo_filename);
   464 	razor_set_destroy(set);
   465 	printf("wrote %s\n", repo_filename);
   466 
   467 	return 0;
   468 }
   469 
   470 static struct razor_set *
   471 create_set_from_rpms(int argc, const char *argv[])
   472 {
   473 	struct razor_importer *importer;
   474 	struct razor_rpm *rpm;
   475 	int i;
   476 
   477 	importer = razor_importer_new();
   478 	for (i = 0; i < argc; i++) {
   479 		rpm = razor_rpm_open(argv[i]);
   480 		if (rpm == NULL) {
   481 			fprintf(stderr,
   482 				"failed to open rpm \"%s\"\n", argv[i]);
   483 			continue;
   484 		}
   485 		if (razor_importer_add_rpm(importer, rpm)) {
   486 			fprintf(stderr, "couldn't import %s\n", argv[i]);
   487 			break;
   488 		}
   489 		razor_rpm_close(rpm);
   490 	}
   491 
   492 	return razor_importer_finish(importer);
   493 }
   494 
   495 static char **
   496 list_packages(int count, struct razor_set *set)
   497 {
   498 	struct razor_package_iterator *pi;
   499 	struct razor_package *package;
   500 	const char *name, *version;
   501 	char **packages;
   502 	int i;
   503 
   504 	packages = malloc(count * sizeof *packages);
   505 	pi = razor_package_iterator_create(set);
   506 	i = 0;
   507 	while (razor_package_iterator_next(pi, &package, &name, &version))
   508 		packages[i++] = strdup(name);
   509 	razor_package_iterator_destroy(pi);
   510 
   511 	return packages;
   512 }
   513 
   514 static int
   515 command_install(int argc, const char *argv[])
   516 {
   517 	struct razor_set *system, *upstream, *next;
   518 	struct razor_transaction *trans;
   519 	struct razor_rpm *rpm;
   520 	const char *filename;
   521 	char path[PATH_MAX], new_path[PATH_MAX], **packages;
   522 	int i;
   523 
   524 	upstream = create_set_from_rpms(argc, argv);
   525 	snprintf(path, sizeof path,
   526 		 "%s%s/%s", root, razor_root_path, system_repo_filename);
   527 	system = razor_set_open(path);
   528 	if (system == NULL) {
   529 		fprintf(stderr, "couldn't open system package database\n");
   530 		return -1;
   531 	}
   532 
   533 	packages = list_packages(argc, upstream);
   534 	trans = razor_transaction_create(system, upstream,
   535 					 argc, (const char **)packages,
   536 					 0, NULL);
   537 	free(packages);
   538 	razor_transaction_describe(trans);
   539 	if (trans->errors)
   540 		return 1;
   541 
   542 	/* FIXME: Use _finish() convention here?  That is, a function
   543 	 * that starts the computation and returns the result while
   544 	 * destroying the transaction.  Nice for transient objects
   545 	 * such as the merger and the importer.  Should we do that for
   546 	 * transactions too, that is, razor_transaction_finish()? */
   547 	next = razor_transaction_run(trans);
   548 	razor_transaction_destroy(trans);
   549 
   550 	/* FIXME: Need razor_set_write_to_fd() so we can open it excl
   551 	 * up front here or fail if it already exists. */
   552 	snprintf(new_path, sizeof new_path,
   553 		 "%s%s/%s", root, razor_root_path, next_repo_filename);
   554 	razor_set_write(next, path);
   555 
   556 	razor_set_destroy(next);
   557 	razor_set_destroy(system);
   558 	razor_set_destroy(upstream);
   559 
   560 	printf("wrote %s\n", new_path);
   561 
   562 	for (i = 0; i < argc; i++) {
   563 		filename = argv[i];
   564 		rpm = razor_rpm_open(argv[i]);
   565 		if (rpm == NULL) {
   566 			fprintf(stderr, "failed to open rpm %s\n", filename);
   567 			return -1;
   568 		}
   569 		if (razor_rpm_install(rpm, root) < 0) {
   570 			fprintf(stderr,
   571 				"failed to install rpm %s\n", filename);
   572 			return -1;
   573 		}
   574 		razor_rpm_close(rpm);
   575 	}	
   576 
   577 	/* Make it so. */
   578 	rename(new_path, path);
   579 	printf("renamed %s to %s\n", new_path, path);
   580 
   581 	return 0;
   582 }
   583 
   584 static int
   585 command_init(int argc, const char *argv[])
   586 {
   587 	struct stat buf;
   588 	struct razor_set *set;
   589 	char path[PATH_MAX];
   590 
   591 	if (stat(root, &buf) < 0) {
   592 		if (mkdir(root, 0777) < 0) {
   593 			fprintf(stderr,
   594 				"could not create install root \"%s\"\n",
   595 				root);
   596 			return -1;
   597 		}
   598 		fprintf(stderr, "created install root \"%s\"\n", root);
   599 	} else if (!S_ISDIR(buf.st_mode)) {
   600 		fprintf(stderr,
   601 			"install root \"%s\" exists, but is not a directory\n",
   602 			root);
   603 		return -1;
   604 	}
   605 
   606 	if (razor_create_dir(root, razor_root_path) < 0) {
   607 		fprintf(stderr, "could not create %s%s\n",
   608 			root, razor_root_path);
   609 		return -1;
   610 	}
   611 
   612 	set = razor_set_create();
   613 	snprintf(path, sizeof path, "%s%s/%s",
   614 		 root, razor_root_path, system_repo_filename);
   615 	if (razor_set_write(set, path) < 0) {
   616 		fprintf(stderr, "could not write initial package set\n");
   617 		return -1;
   618 	}
   619 	razor_set_destroy(set);
   620 
   621 	return 0;
   622 }
   623 
   624 static int
   625 command_download(int argc, const char *argv[])
   626 {
   627 	struct razor_set *set;
   628 	struct razor_package_iterator *pi;
   629 	struct razor_package *package;
   630 	const char *pattern = argv[0], *name, *version;
   631 	char url[256], file[256];
   632 	CURL *curl;
   633 
   634 	curl = curl_easy_init();
   635 	if (curl == NULL)
   636 		return 1;
   637 
   638 	set = razor_set_open(rawhide_repo_filename);
   639 	pi = razor_package_iterator_create(set);
   640 	while (razor_package_iterator_next(pi, &package, &name, &version)) {
   641 		if (pattern && fnmatch(pattern, name, 0) != 0)
   642 			continue;
   643 
   644 		snprintf(url, sizeof url,
   645 			 REPO_URL "/Packages/%s-%s.i386.rpm", name, version);
   646 		snprintf(file, sizeof file,
   647 			 "rpms/%s-%s.i386.rpm", name, version);
   648 		if (download_if_missing(curl, url, file) < 0)
   649 			fprintf(stderr, "failed to download %s\n", name);
   650 	}
   651 	razor_package_iterator_destroy(pi);
   652 	razor_set_destroy(set);
   653 	curl_easy_cleanup(curl);
   654 
   655 	return 0;
   656 }
   657 
   658 static struct {
   659 	const char *name;
   660 	const char *description;
   661 	int (*func)(int argc, const char *argv[]);
   662 } razor_commands[] = {
   663 	{ "list", "list all packages", command_list },
   664 	{ "list-requires", "list all requires for the given package", command_list_requires },
   665 	{ "list-provides", "list all provides for the given package", command_list_provides },
   666 	{ "list-obsoletes", "list all obsoletes for the given package", command_list_obsoletes },
   667 	{ "list-conflicts", "list all conflicts for the given package", command_list_conflicts },
   668 	{ "list-files", "list files for package set", command_list_files },
   669 	{ "list-file-packages", "list packages owning file", command_list_file_packages },
   670 	{ "list-package-files", "list files in package", command_list_package_files },
   671 	{ "what-requires", "list the packages that have the given requires", command_what_requires },
   672 	{ "what-provides", "list the packages that have the given provides", command_what_provides },
   673 	{ "import-yum", "import yum metadata files", command_import_yum },
   674 	{ "import-rpmdb", "import the system rpm database", command_import_rpmdb },
   675 	{ "import-rpms", "import rpms from the given directory", command_import_rpms },
   676 	{ "validate", "validate a package set", command_validate },
   677 	{ "update", "update all or specified packages", command_update },
   678 	{ "remove", "remove specified packages", command_remove },
   679 	{ "diff", "show diff between two package sets", command_diff },
   680 	{ "install", "install rpm", command_install },
   681 	{ "init", "init razor root", command_init },
   682 	{ "download", "download packages", command_download }
   683 };
   684 
   685 static int
   686 usage(void)
   687 {
   688 	int i;
   689 
   690 	printf("usage:\n");
   691 	for (i = 0; i < ARRAY_SIZE(razor_commands); i++)
   692 		printf("  %-20s%s\n",
   693 		       razor_commands[i].name, razor_commands[i].description);
   694 
   695 	return 1;
   696 }
   697 
   698 int
   699 main(int argc, const char *argv[])
   700 {
   701 	char *repo;
   702 	int i;
   703 
   704 	repo = getenv("RAZOR_REPO");
   705 	if (repo != NULL)
   706 		repo_filename = repo;
   707 
   708 	if (argc < 2)
   709 		return usage();
   710 
   711 	for (i = 0; i < ARRAY_SIZE(razor_commands); i++)
   712 		if (strcmp(razor_commands[i].name, argv[1]) == 0)
   713 			return razor_commands[i].func(argc - 2, argv + 2);
   714 
   715 	return usage();
   716 }