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