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