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