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