main.c
author Kristian H?gsberg <krh@redhat.com>
Sat Apr 05 23:55:00 2008 -0400 (2008-04-05)
changeset 187 6ff03223ce4c
parent 186 7f45d0401e37
child 188 a272b6ce454b
permissions -rw-r--r--
Add download command to download packages from yum repo.

Works with bash completion against upstream packages and
supports wildcards suchs as gcc-*.
     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 	}
   269 
   270 	fprintf(stderr, "\n");
   271 
   272 	return 0;
   273 }
   274 
   275 #define REPO_URL "http://download.fedora.redhat.com" \
   276 	"/pub/fedora/linux/development/i386/os"
   277 
   278 static int
   279 command_import_yum(int argc, const char *argv[])
   280 {
   281 	struct razor_set *set;
   282 	CURL *curl;
   283 
   284 	curl = curl_easy_init();
   285 	if (curl == NULL)
   286 		return 1;
   287 
   288 	if (download_if_missing(curl,
   289 				REPO_URL "/repodata/primary.xml.gz",
   290 				"primary.xml.gz") < 0)
   291 		return -1;
   292 	if (download_if_missing(curl,
   293 				REPO_URL "/repodata/filelists.xml.gz",
   294 				"filelists.xml.gz") < 0)
   295 		return -1;
   296 	curl_easy_cleanup(curl);
   297 
   298 	set = razor_set_create_from_yum();
   299 	if (set == NULL)
   300 		return 1;
   301 	razor_set_write(set, rawhide_repo_filename);
   302 	razor_set_destroy(set);
   303 	printf("wrote %s\n", rawhide_repo_filename);
   304 
   305 	return 0;
   306 }
   307 
   308 static int
   309 command_import_rpmdb(int argc, const char *argv[])
   310 {
   311 	struct razor_set *set;
   312 
   313 	set = razor_set_create_from_rpmdb();
   314 	if (set == NULL)
   315 		return 1;
   316 	razor_set_write(set, repo_filename);
   317 	razor_set_destroy(set);
   318 	printf("wrote %s\n", repo_filename);
   319 
   320 	return 0;
   321 }
   322 
   323 static int
   324 command_validate(int argc, const char *argv[])
   325 {
   326 	struct razor_set *set;
   327 
   328 	set = razor_set_open(repo_filename);
   329 	if (set == NULL)
   330 		return 1;
   331 	razor_set_list_unsatisfied(set);
   332 	razor_set_destroy(set);
   333 
   334 	return 0;
   335 }
   336 
   337 static int
   338 command_update(int argc, const char *argv[])
   339 {
   340 	struct razor_set *set, *upstream;
   341 	struct razor_transaction *trans;
   342 
   343 	set = razor_set_open(repo_filename);
   344 	upstream = razor_set_open(rawhide_repo_filename);
   345 	if (set == NULL || upstream == NULL)
   346 		return 1;
   347 	trans = razor_transaction_create(set, upstream, argc, argv, 0, NULL);
   348 	razor_transaction_describe(trans);
   349 	if (trans->errors)
   350 		return 1;
   351 
   352 	set = razor_transaction_run(trans);
   353 	razor_transaction_destroy(trans);
   354 	razor_set_write(set, updated_repo_filename);
   355 	razor_set_destroy(set);
   356 	razor_set_destroy(upstream);
   357 	printf("wrote system-updated.repo\n");
   358 
   359 	return 0;
   360 }
   361 
   362 static int
   363 command_remove(int argc, const char *argv[])
   364 {
   365 	struct razor_set *set;
   366 	struct razor_transaction *trans;
   367 
   368 	set = razor_set_open(repo_filename);
   369 	if (set == NULL)
   370 		return 1;
   371 	trans = razor_transaction_create(set, NULL, 0, NULL, argc, argv);
   372 	razor_transaction_describe(trans);
   373 	if (trans->errors)
   374 		return 1;
   375 
   376 	set = razor_transaction_run(trans);
   377 	razor_transaction_destroy(trans);
   378 	razor_set_write(set, updated_repo_filename);
   379 	razor_set_destroy(set);
   380 	printf("wrote system-updated.repo\n");
   381 
   382 	return 0;
   383 }
   384 
   385 static void
   386 print_diff(const char *name,
   387 	   const char *old_version, const char *new_version, void *data)
   388 {
   389 	if (old_version)
   390 		printf("removing %s %s\n", name, old_version);
   391 	else
   392 		printf("install %s %s\n", name, new_version);
   393 }
   394 
   395 static int
   396 command_diff(int argc, const char *argv[])
   397 {
   398 	struct razor_set *set, *updated;
   399 
   400 	set = razor_set_open(repo_filename);
   401 	updated = razor_set_open(updated_repo_filename);
   402 	if (set == NULL || updated == NULL)
   403 		return 1;
   404 
   405 	razor_set_diff(set, updated, print_diff, NULL);	
   406 
   407 	razor_set_destroy(set);
   408 	razor_set_destroy(updated);
   409 
   410 	return 0;
   411 }
   412 
   413 static int
   414 command_import_rpms(int argc, const char *argv[])
   415 {
   416 	DIR *dir;
   417 	struct dirent *de;
   418 	struct razor_importer *importer;
   419 	struct razor_set *set;
   420 	struct razor_rpm *rpm;
   421 	int len;
   422 	char filename[256];
   423 	const char *dirname = argv[0];
   424 
   425 	if (dirname == NULL) {
   426 		fprintf(stderr, "usage: razor import-rpms DIR\n");
   427 		return -1;
   428 	}
   429 
   430 	dir = opendir(dirname);
   431 	if (dir == NULL) {
   432 		fprintf(stderr, "couldn't read dir %s\n", dirname);
   433 		return -1;
   434 	}
   435 
   436 	importer = razor_importer_new();
   437 
   438 	while (de = readdir(dir), de != NULL) {
   439 		len = strlen(de->d_name);
   440 		if (len < 5 || strcmp(de->d_name + len - 4, ".rpm") != 0)
   441 		    continue;
   442 		snprintf(filename, sizeof filename,
   443 			 "%s/%s", dirname, de->d_name);
   444 		rpm = razor_rpm_open(filename);
   445 		if (rpm == NULL) {
   446 			fprintf(stderr,
   447 				"failed to open rpm \"%s\"\n", filename);
   448 			continue;
   449 		}
   450 		if (razor_importer_add_rpm(importer, rpm)) {
   451 			fprintf(stderr, "couldn't import %s\n", filename);
   452 			break;
   453 		}
   454 		razor_rpm_close(rpm);
   455 	}
   456 
   457 	if (de != NULL) {
   458 		razor_importer_destroy(importer);
   459 		return -1;
   460 	}
   461 
   462 	set = razor_importer_finish(importer);
   463 
   464 	razor_set_write(set, repo_filename);
   465 	razor_set_destroy(set);
   466 	printf("wrote %s\n", repo_filename);
   467 
   468 	return 0;
   469 }
   470 
   471 static struct razor_set *
   472 create_set_from_rpms(int argc, const char *argv[])
   473 {
   474 	struct razor_importer *importer;
   475 	struct razor_rpm *rpm;
   476 	int i;
   477 
   478 	importer = razor_importer_new();
   479 	for (i = 0; i < argc; i++) {
   480 		rpm = razor_rpm_open(argv[i]);
   481 		if (rpm == NULL) {
   482 			fprintf(stderr,
   483 				"failed to open rpm \"%s\"\n", argv[i]);
   484 			continue;
   485 		}
   486 		if (razor_importer_add_rpm(importer, rpm)) {
   487 			fprintf(stderr, "couldn't import %s\n", argv[i]);
   488 			break;
   489 		}
   490 		razor_rpm_close(rpm);
   491 	}
   492 
   493 	return razor_importer_finish(importer);
   494 }
   495 
   496 static char **
   497 list_packages(int count, struct razor_set *set)
   498 {
   499 	struct razor_package_iterator *pi;
   500 	struct razor_package *package;
   501 	const char *name, *version;
   502 	char **packages;
   503 	int i;
   504 
   505 	packages = malloc(count * sizeof *packages);
   506 	pi = razor_package_iterator_create(set);
   507 	i = 0;
   508 	while (razor_package_iterator_next(pi, &package, &name, &version))
   509 		packages[i++] = strdup(name);
   510 	razor_package_iterator_destroy(pi);
   511 
   512 	return packages;
   513 }
   514 
   515 static int
   516 command_install(int argc, const char *argv[])
   517 {
   518 	struct razor_set *system, *upstream, *next;
   519 	struct razor_transaction *trans;
   520 	struct razor_rpm *rpm;
   521 	const char *filename;
   522 	char path[PATH_MAX], new_path[PATH_MAX], **packages;
   523 	int i;
   524 
   525 	upstream = create_set_from_rpms(argc, argv);
   526 	snprintf(path, sizeof path,
   527 		 "%s%s/%s", root, razor_root_path, system_repo_filename);
   528 	system = razor_set_open(path);
   529 	if (system == NULL) {
   530 		fprintf(stderr, "couldn't open system package database\n");
   531 		return -1;
   532 	}
   533 
   534 	packages = list_packages(argc, upstream);
   535 	trans = razor_transaction_create(system, upstream,
   536 					 argc, (const char **)packages,
   537 					 0, NULL);
   538 	free(packages);
   539 	razor_transaction_describe(trans);
   540 	if (trans->errors)
   541 		return 1;
   542 
   543 	/* FIXME: Use _finish() convention here?  That is, a function
   544 	 * that starts the computation and returns the result while
   545 	 * destroying the transaction.  Nice for transient objects
   546 	 * such as the merger and the importer.  Should we do that for
   547 	 * transactions too, that is, razor_transaction_finish()? */
   548 	next = razor_transaction_run(trans);
   549 	razor_transaction_destroy(trans);
   550 
   551 	/* FIXME: Need razor_set_write_to_fd() so we can open it excl
   552 	 * up front here or fail if it already exists. */
   553 	snprintf(new_path, sizeof new_path,
   554 		 "%s%s/%s", root, razor_root_path, next_repo_filename);
   555 	razor_set_write(next, path);
   556 
   557 	razor_set_destroy(next);
   558 	razor_set_destroy(system);
   559 	razor_set_destroy(upstream);
   560 
   561 	printf("wrote %s\n", new_path);
   562 
   563 	for (i = 0; i < argc; i++) {
   564 		filename = argv[i];
   565 		rpm = razor_rpm_open(argv[i]);
   566 		if (rpm == NULL) {
   567 			fprintf(stderr, "failed to open rpm %s\n", filename);
   568 			return -1;
   569 		}
   570 		if (razor_rpm_install(rpm, root) < 0) {
   571 			fprintf(stderr,
   572 				"failed to install rpm %s\n", filename);
   573 			return -1;
   574 		}
   575 		razor_rpm_close(rpm);
   576 	}	
   577 
   578 	/* Make it so. */
   579 	rename(new_path, path);
   580 	printf("renamed %s to %s\n", new_path, path);
   581 
   582 	return 0;
   583 }
   584 
   585 static int
   586 command_init(int argc, const char *argv[])
   587 {
   588 	struct stat buf;
   589 	struct razor_set *set;
   590 	char path[PATH_MAX];
   591 
   592 	if (stat(root, &buf) < 0) {
   593 		if (mkdir(root, 0777) < 0) {
   594 			fprintf(stderr,
   595 				"could not create install root \"%s\"\n",
   596 				root);
   597 			return -1;
   598 		}
   599 		fprintf(stderr, "created install root \"%s\"\n", root);
   600 	} else if (!S_ISDIR(buf.st_mode)) {
   601 		fprintf(stderr,
   602 			"install root \"%s\" exists, but is not a directory\n",
   603 			root);
   604 		return -1;
   605 	}
   606 
   607 	if (razor_create_dir(root, razor_root_path) < 0) {
   608 		fprintf(stderr, "could not create %s%s\n",
   609 			root, razor_root_path);
   610 		return -1;
   611 	}
   612 
   613 	set = razor_set_create();
   614 	snprintf(path, sizeof path, "%s%s/%s",
   615 		 root, razor_root_path, system_repo_filename);
   616 	if (razor_set_write(set, path) < 0) {
   617 		fprintf(stderr, "could not write initial package set\n");
   618 		return -1;
   619 	}
   620 	razor_set_destroy(set);
   621 
   622 	return 0;
   623 }
   624 
   625 static int
   626 command_download(int argc, const char *argv[])
   627 {
   628 	struct razor_set *set;
   629 	struct razor_package_iterator *pi;
   630 	struct razor_package *package;
   631 	const char *pattern = argv[0], *name, *version;
   632 	char url[256], file[256];
   633 	CURL *curl;
   634 
   635 	curl = curl_easy_init();
   636 	if (curl == NULL)
   637 		return 1;
   638 
   639 	set = razor_set_open(rawhide_repo_filename);
   640 	pi = razor_package_iterator_create(set);
   641 	while (razor_package_iterator_next(pi, &package, &name, &version)) {
   642 		if (pattern && fnmatch(pattern, name, 0) != 0)
   643 			continue;
   644 
   645 		snprintf(url, sizeof url,
   646 			 REPO_URL "/Packages/%s-%s.i386.rpm", name, version);
   647 		snprintf(file, sizeof file,
   648 			 "rpms/%s-%s.i386.rpm", name, version);
   649 		if (download_if_missing(curl, url, file) < 0)
   650 			fprintf(stderr, "failed to download %s\n", name);
   651 	}
   652 	razor_package_iterator_destroy(pi);
   653 	razor_set_destroy(set);
   654 	curl_easy_cleanup(curl);
   655 
   656 	return 0;
   657 }
   658 
   659 static struct {
   660 	const char *name;
   661 	const char *description;
   662 	int (*func)(int argc, const char *argv[]);
   663 } razor_commands[] = {
   664 	{ "list", "list all packages", command_list },
   665 	{ "list-requires", "list all requires for the given package", command_list_requires },
   666 	{ "list-provides", "list all provides for the given package", command_list_provides },
   667 	{ "list-obsoletes", "list all obsoletes for the given package", command_list_obsoletes },
   668 	{ "list-conflicts", "list all conflicts for the given package", command_list_conflicts },
   669 	{ "list-files", "list files for package set", command_list_files },
   670 	{ "list-file-packages", "list packages owning file", command_list_file_packages },
   671 	{ "list-package-files", "list files in package", command_list_package_files },
   672 	{ "what-requires", "list the packages that have the given requires", command_what_requires },
   673 	{ "what-provides", "list the packages that have the given provides", command_what_provides },
   674 	{ "import-yum", "import yum metadata files", command_import_yum },
   675 	{ "import-rpmdb", "import the system rpm database", command_import_rpmdb },
   676 	{ "import-rpms", "import rpms from the given directory", command_import_rpms },
   677 	{ "validate", "validate a package set", command_validate },
   678 	{ "update", "update all or specified packages", command_update },
   679 	{ "remove", "remove specified packages", command_remove },
   680 	{ "diff", "show diff between two package sets", command_diff },
   681 	{ "install", "install rpm", command_install },
   682 	{ "init", "init razor root", command_init },
   683 	{ "download", "download packages", command_download }
   684 };
   685 
   686 static int
   687 usage(void)
   688 {
   689 	int i;
   690 
   691 	printf("usage:\n");
   692 	for (i = 0; i < ARRAY_SIZE(razor_commands); i++)
   693 		printf("  %-20s%s\n",
   694 		       razor_commands[i].name, razor_commands[i].description);
   695 
   696 	return 1;
   697 }
   698 
   699 int
   700 main(int argc, const char *argv[])
   701 {
   702 	char *repo;
   703 	int i;
   704 
   705 	repo = getenv("RAZOR_REPO");
   706 	if (repo != NULL)
   707 		repo_filename = repo;
   708 
   709 	if (argc < 2)
   710 		return usage();
   711 
   712 	for (i = 0; i < ARRAY_SIZE(razor_commands); i++)
   713 		if (strcmp(razor_commands[i].name, argv[1]) == 0)
   714 			return razor_commands[i].func(argc - 2, argv + 2);
   715 
   716 	return usage();
   717 }