main.c
author Kristian H?gsberg <krh@redhat.com>
Mon Apr 07 00:06:22 2008 -0400 (2008-04-07)
changeset 196 b38fc517ea04
parent 194 6d25d2fa0935
child 197 d29026900856
permissions -rw-r--r--
Merger razor_transaction_run() and destroy() as finish().
     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_finish(trans);
   355 	razor_set_write(set, updated_repo_filename);
   356 	razor_set_destroy(set);
   357 	razor_set_destroy(upstream);
   358 	printf("wrote system-updated.repo\n");
   359 
   360 	return 0;
   361 }
   362 
   363 static int
   364 command_remove(int argc, const char *argv[])
   365 {
   366 	struct razor_set *set;
   367 	struct razor_transaction *trans;
   368 	int errors;
   369 
   370 	set = razor_set_open(repo_filename);
   371 	if (set == NULL)
   372 		return 1;
   373 	trans = razor_transaction_create(set, NULL, 0, NULL, argc, argv);
   374 	errors = razor_transaction_describe(trans);
   375 	if (errors)
   376 		return 1;
   377 
   378 	set = razor_transaction_finish(trans);
   379 	razor_set_write(set, updated_repo_filename);
   380 	razor_set_destroy(set);
   381 	printf("wrote system-updated.repo\n");
   382 
   383 	return 0;
   384 }
   385 
   386 static void
   387 print_diff(const char *name,
   388 	   const char *old_version, const char *new_version, const char *arch,
   389 	   void *data)
   390 {
   391 	if (old_version)
   392 		printf("removing %s %s\n", name, old_version);
   393 	else
   394 		printf("install %s %s\n", name, new_version);
   395 }
   396 
   397 static int
   398 command_diff(int argc, const char *argv[])
   399 {
   400 	struct razor_set *set, *updated;
   401 
   402 	set = razor_set_open(repo_filename);
   403 	updated = razor_set_open(updated_repo_filename);
   404 	if (set == NULL || updated == NULL)
   405 		return 1;
   406 
   407 	razor_set_diff(set, updated, print_diff, NULL);	
   408 
   409 	razor_set_destroy(set);
   410 	razor_set_destroy(updated);
   411 
   412 	return 0;
   413 }
   414 
   415 static int
   416 command_import_rpms(int argc, const char *argv[])
   417 {
   418 	DIR *dir;
   419 	struct dirent *de;
   420 	struct razor_importer *importer;
   421 	struct razor_set *set;
   422 	struct razor_rpm *rpm;
   423 	int len;
   424 	char filename[256];
   425 	const char *dirname = argv[0];
   426 
   427 	if (dirname == NULL) {
   428 		fprintf(stderr, "usage: razor import-rpms DIR\n");
   429 		return -1;
   430 	}
   431 
   432 	dir = opendir(dirname);
   433 	if (dir == NULL) {
   434 		fprintf(stderr, "couldn't read dir %s\n", dirname);
   435 		return -1;
   436 	}
   437 
   438 	importer = razor_importer_new();
   439 
   440 	while (de = readdir(dir), de != NULL) {
   441 		len = strlen(de->d_name);
   442 		if (len < 5 || strcmp(de->d_name + len - 4, ".rpm") != 0)
   443 		    continue;
   444 		snprintf(filename, sizeof filename,
   445 			 "%s/%s", dirname, de->d_name);
   446 		rpm = razor_rpm_open(filename);
   447 		if (rpm == NULL) {
   448 			fprintf(stderr,
   449 				"failed to open rpm \"%s\"\n", filename);
   450 			continue;
   451 		}
   452 		if (razor_importer_add_rpm(importer, rpm)) {
   453 			fprintf(stderr, "couldn't import %s\n", filename);
   454 			break;
   455 		}
   456 		razor_rpm_close(rpm);
   457 	}
   458 
   459 	if (de != NULL) {
   460 		razor_importer_destroy(importer);
   461 		return -1;
   462 	}
   463 
   464 	set = razor_importer_finish(importer);
   465 
   466 	razor_set_write(set, repo_filename);
   467 	razor_set_destroy(set);
   468 	printf("wrote %s\n", repo_filename);
   469 
   470 	return 0;
   471 }
   472 
   473 static void
   474 download_package(const char *name,
   475 		 const char *old_version,
   476 		 const char *new_version,
   477 		 const char *arch,
   478 		 void *data)
   479 {
   480 	char file[PATH_MAX], url[256];
   481 
   482 	if (old_version)
   483 		return;
   484 
   485 	snprintf(url, sizeof url,
   486 		 REPO_URL "/Packages/%s-%s.%s.rpm", name, new_version, arch);
   487 	snprintf(file, sizeof file,
   488 		 "rpms/%s-%s.%s.rpm", name, new_version, arch);
   489 	if (download_if_missing(url, file) < 0)
   490 		fprintf(stderr, "failed to download %s\n", name);
   491 }
   492 
   493 static void
   494 install_package(const char *name,
   495 		const char *old_version,
   496 		const char *new_version,
   497 		const char *arch,
   498 		void *data)
   499 {
   500 	const char *root = data;
   501 	char file[PATH_MAX];
   502 	struct razor_rpm *rpm;
   503 
   504 	if (old_version) {
   505 		printf("removing %s %s not handled\n", name, old_version);
   506 		return;
   507 	}
   508 
   509 	printf("install %s %s\n", name, new_version);
   510 	snprintf(file, sizeof file,
   511 		 "rpms/%s-%s.%s.rpm", name, new_version, arch);
   512 
   513  	rpm = razor_rpm_open(file);
   514 	if (rpm == NULL) {
   515 		fprintf(stderr, "failed to open rpm %s\n", file);
   516 		return;
   517 	}
   518 	if (razor_rpm_install(rpm, root) < 0) {
   519 		fprintf(stderr,
   520 			"failed to install rpm %s\n", file);
   521 		return;
   522 	}
   523 	razor_rpm_close(rpm);
   524 }
   525 
   526 static int
   527 command_install(int argc, const char *argv[])
   528 {
   529 	struct razor_set *system, *upstream, *next;
   530 	struct razor_transaction *trans;
   531 	char path[PATH_MAX], new_path[PATH_MAX];
   532 	CURL *curl;
   533 	int errors;
   534 
   535 	upstream = razor_set_open(rawhide_repo_filename);
   536 	snprintf(path, sizeof path,
   537 		 "%s%s/%s", root, razor_root_path, system_repo_filename);
   538 	system = razor_set_open(path);
   539 	if (system == NULL || upstream == NULL)
   540 		return 1;
   541 	trans = razor_transaction_create(system, upstream,
   542 					 argc, argv, 0, NULL);
   543 	errors = razor_transaction_describe(trans);
   544 	if (errors)
   545 		return 1;
   546 
   547 	next = razor_transaction_finish(trans);
   548 
   549 	/* FIXME: Need razor_set_write_to_fd() so we can open it excl
   550 	 * up front here or fail if it already exists. */
   551 	snprintf(new_path, sizeof new_path,
   552 		 "%s%s/%s", root, razor_root_path, next_repo_filename);
   553 
   554 	razor_set_write(next, new_path);
   555 	printf("wrote %s\n", new_path);
   556 
   557 	curl = curl_easy_init();
   558 	if (curl == NULL)
   559 		return 1;
   560 	razor_set_diff(system, next, download_package, curl);	
   561 	curl_easy_cleanup(curl);
   562 
   563 	razor_set_diff(system, next, install_package, (void *) root);
   564 
   565 	razor_set_destroy(next);
   566 	razor_set_destroy(system);
   567 	razor_set_destroy(upstream);
   568 
   569 	/* Make it so. */
   570 	rename(new_path, path);
   571 	printf("renamed %s to %s\n", new_path, path);
   572 
   573 	return 0;
   574 }
   575 
   576 static int
   577 command_init(int argc, const char *argv[])
   578 {
   579 	struct stat buf;
   580 	struct razor_set *set;
   581 	char path[PATH_MAX];
   582 
   583 	if (stat(root, &buf) < 0) {
   584 		if (mkdir(root, 0777) < 0) {
   585 			fprintf(stderr,
   586 				"could not create install root \"%s\"\n",
   587 				root);
   588 			return -1;
   589 		}
   590 		fprintf(stderr, "created install root \"%s\"\n", root);
   591 	} else if (!S_ISDIR(buf.st_mode)) {
   592 		fprintf(stderr,
   593 			"install root \"%s\" exists, but is not a directory\n",
   594 			root);
   595 		return -1;
   596 	}
   597 
   598 	if (razor_create_dir(root, razor_root_path) < 0) {
   599 		fprintf(stderr, "could not create %s%s\n",
   600 			root, razor_root_path);
   601 		return -1;
   602 	}
   603 
   604 	set = razor_set_create();
   605 	snprintf(path, sizeof path, "%s%s/%s",
   606 		 root, razor_root_path, system_repo_filename);
   607 	if (razor_set_write(set, path) < 0) {
   608 		fprintf(stderr, "could not write initial package set\n");
   609 		return -1;
   610 	}
   611 	razor_set_destroy(set);
   612 
   613 	return 0;
   614 }
   615 
   616 static int
   617 command_download(int argc, const char *argv[])
   618 {
   619 	struct razor_set *set;
   620 	struct razor_package_iterator *pi;
   621 	struct razor_package *package;
   622 	const char *pattern = argv[0], *name, *version, *arch;
   623 	char url[256], file[256];
   624 
   625 	set = razor_set_open(rawhide_repo_filename);
   626 	pi = razor_package_iterator_create(set);
   627 	while (razor_package_iterator_next(pi, &package,
   628 					   &name, &version, &arch)) {
   629 		if (pattern && fnmatch(pattern, name, 0) != 0)
   630 			continue;
   631 
   632 		snprintf(url, sizeof url,
   633 			 REPO_URL "/Packages/%s-%s.i386.rpm", name, version);
   634 		snprintf(file, sizeof file,
   635 			 "rpms/%s-%s.i386.rpm", name, version);
   636 		if (download_if_missing(url, file) < 0)
   637 			fprintf(stderr, "failed to download %s\n", name);
   638 	}
   639 	razor_package_iterator_destroy(pi);
   640 	razor_set_destroy(set);
   641 
   642 	return 0;
   643 }
   644 
   645 static struct {
   646 	const char *name;
   647 	const char *description;
   648 	int (*func)(int argc, const char *argv[]);
   649 } razor_commands[] = {
   650 	{ "list", "list all packages", command_list },
   651 	{ "list-requires", "list all requires for the given package", command_list_requires },
   652 	{ "list-provides", "list all provides for the given package", command_list_provides },
   653 	{ "list-obsoletes", "list all obsoletes for the given package", command_list_obsoletes },
   654 	{ "list-conflicts", "list all conflicts for the given package", command_list_conflicts },
   655 	{ "list-files", "list files for package set", command_list_files },
   656 	{ "list-file-packages", "list packages owning file", command_list_file_packages },
   657 	{ "list-package-files", "list files in package", command_list_package_files },
   658 	{ "what-requires", "list the packages that have the given requires", command_what_requires },
   659 	{ "what-provides", "list the packages that have the given provides", command_what_provides },
   660 	{ "import-yum", "import yum metadata files", command_import_yum },
   661 	{ "import-rpmdb", "import the system rpm database", command_import_rpmdb },
   662 	{ "import-rpms", "import rpms from the given directory", command_import_rpms },
   663 	{ "validate", "validate a package set", command_validate },
   664 	{ "update", "update all or specified packages", command_update },
   665 	{ "remove", "remove specified packages", command_remove },
   666 	{ "diff", "show diff between two package sets", command_diff },
   667 	{ "install", "install rpm", command_install },
   668 	{ "init", "init razor root", command_init },
   669 	{ "download", "download packages", command_download }
   670 };
   671 
   672 static int
   673 usage(void)
   674 {
   675 	int i;
   676 
   677 	printf("usage:\n");
   678 	for (i = 0; i < ARRAY_SIZE(razor_commands); i++)
   679 		printf("  %-20s%s\n",
   680 		       razor_commands[i].name, razor_commands[i].description);
   681 
   682 	return 1;
   683 }
   684 
   685 int
   686 main(int argc, const char *argv[])
   687 {
   688 	char *repo;
   689 	int i;
   690 
   691 	repo = getenv("RAZOR_REPO");
   692 	if (repo != NULL)
   693 		repo_filename = repo;
   694 
   695 	if (argc < 2)
   696 		return usage();
   697 
   698 	for (i = 0; i < ARRAY_SIZE(razor_commands); i++)
   699 		if (strcmp(razor_commands[i].name, argv[1]) == 0)
   700 			return razor_commands[i].func(argc - 2, argv + 2);
   701 
   702 	return usage();
   703 }