main.c
author Kristian H?gsberg <krh@redhat.com>
Mon Apr 07 00:35:27 2008 -0400 (2008-04-07)
changeset 197 d29026900856
parent 196 b38fc517ea04
child 200 4c4955871c21
permissions -rw-r--r--
Create the new repo file O_EXCL to prevent racing with another razor process.

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