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