main.c
author Kristian H?gsberg <krh@redhat.com>
Thu Jan 10 23:42:42 2008 -0500 (2008-01-10)
changeset 100 27aada326858
parent 94 0aa93cfbcb3f
child 101 9708f6d5db8c
permissions -rw-r--r--
Move more front-end logic from razor_set_list_property_packages() to main.c.
     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 int
   140 list_property_packages(const char *ref_name,
   141 		       const char *ref_version,
   142 		       enum razor_property_type ref_type)
   143 {
   144 	struct razor_set *set;
   145 	struct razor_property *property;
   146 	struct razor_property_iterator *pi;
   147 	const char *name, *version;
   148 	enum razor_property_type type;
   149 
   150 	if (ref_name == NULL)
   151 		return 0;
   152 
   153 	set = razor_set_open(repo_filename);
   154 	if (set == NULL)
   155 		return 1;
   156 
   157 	pi = razor_property_iterator_create(set, NULL);
   158 	while (razor_property_iterator_next(pi, &property,
   159 					    &name, &version, &type)) {
   160 		if (strcmp(ref_name, name) != 0)
   161 			continue;
   162 		if (ref_version && strcmp(ref_version, version) != 0)
   163 			continue;
   164 		if (ref_type != type)
   165 			continue;
   166 
   167 		razor_set_list_property_packages(set, property);
   168 	}
   169 	razor_property_iterator_destroy(pi);
   170 
   171 	return 0;
   172 }
   173 
   174 static int
   175 command_what_requires(int argc, const char *argv[])
   176 {
   177 	return list_property_packages(argv[0], argv[1],
   178 				      RAZOR_PROPERTY_REQUIRES);
   179 }
   180 
   181 static int
   182 command_what_provides(int argc, const char *argv[])
   183 {
   184 	return list_property_packages(argv[0], argv[1],
   185 				      RAZOR_PROPERTY_PROVIDES);
   186 }
   187 
   188 static int
   189 show_progress(void *clientp,
   190 	      double dltotal, double dlnow, double ultotal, double ulnow)
   191 {
   192 	const char *file = clientp;
   193 
   194 	if (!dlnow < dltotal)
   195 		fprintf(stderr, "\rdownloading %s, %dkB/%dkB",
   196 			file, (int) dlnow / 1024, (int) dltotal / 1024);
   197 	else
   198 		fprintf(stderr, "\n");
   199 
   200 	return 0;
   201 }
   202 
   203 static int
   204 download_if_missing(CURL *curl, const char *url, const char *file)
   205 {
   206 	struct stat buf;
   207 	char error[256];
   208 	FILE *fp;
   209 	CURLcode res;
   210 	char buffer[256];
   211 
   212 	curl_easy_setopt(curl, CURLOPT_ERRORBUFFER, error);
   213 	curl_easy_setopt(curl, CURLOPT_NOPROGRESS, 0);
   214 	curl_easy_setopt(curl, CURLOPT_PROGRESSFUNCTION, show_progress);
   215 	curl_easy_setopt(curl, CURLOPT_PROGRESSDATA, file);
   216 
   217 	if (stat(file, &buf) < 0) {
   218 		fp = fopen(file, "w");
   219 		snprintf(buffer, sizeof buffer, "%s/%s", url, file);
   220 		curl_easy_setopt(curl, CURLOPT_WRITEDATA, fp);
   221 		curl_easy_setopt(curl, CURLOPT_URL, buffer);
   222 		res = curl_easy_perform(curl);
   223 		fclose(fp);
   224 		if (res != CURLE_OK) {
   225 			fprintf(stderr, "curl error: %s\n", error);
   226 			unlink(file);
   227 			return -1;
   228 		}
   229 	}
   230 
   231 	return 0;
   232 }
   233 
   234 #define REPO_URL "http://download.fedora.redhat.com" \
   235 	"/pub/fedora/linux/development/i386/os/repodata"
   236 
   237 static int
   238 command_import_yum(int argc, const char *argv[])
   239 {
   240 	struct razor_set *set;
   241 	CURL *curl;
   242 
   243 	curl = curl_easy_init();
   244 	if (curl == NULL)
   245 		return 1;
   246 
   247 	if (download_if_missing(curl, REPO_URL, "primary.xml.gz") < 0)
   248 		return -1;
   249 	if (download_if_missing(curl, REPO_URL, "filelists.xml.gz") < 0)
   250 		return -1;
   251 	curl_easy_cleanup(curl);
   252 
   253 	set = razor_set_create_from_yum();
   254 	if (set == NULL)
   255 		return 1;
   256 	razor_set_write(set, rawhide_repo_filename);
   257 	razor_set_destroy(set);
   258 	printf("wrote %s\n", rawhide_repo_filename);
   259 
   260 	return 0;
   261 }
   262 
   263 static int
   264 command_import_rpmdb(int argc, const char *argv[])
   265 {
   266 	struct razor_set *set;
   267 
   268 	set = razor_set_create_from_rpmdb();
   269 	if (set == NULL)
   270 		return 1;
   271 	razor_set_write(set, repo_filename);
   272 	razor_set_destroy(set);
   273 	printf("wrote %s\n", repo_filename);
   274 
   275 	return 0;
   276 }
   277 
   278 static int
   279 command_validate(int argc, const char *argv[])
   280 {
   281 	struct razor_set *set;
   282 
   283 	set = razor_set_open(repo_filename);
   284 	if (set == NULL)
   285 		return 1;
   286 	razor_set_list_unsatisfied(set);
   287 	razor_set_destroy(set);
   288 
   289 	return 0;
   290 }
   291 
   292 static int
   293 command_update(int argc, const char *argv[])
   294 {
   295 	struct razor_set *set, *upstream;
   296 
   297 	set = razor_set_open(repo_filename);
   298 	upstream = razor_set_open(rawhide_repo_filename);
   299 	if (set == NULL || upstream == NULL)
   300 		return 1;
   301 	set = razor_set_update(set, upstream, argc, argv);
   302 	razor_set_write(set, updated_repo_filename);
   303 	razor_set_destroy(set);
   304 	razor_set_destroy(upstream);
   305 	printf("wrote system-updated.repo\n");
   306 
   307 	return 0;
   308 }
   309 
   310 static void
   311 print_diff(const char *name,
   312 	   const char *old_version, const char *new_version, void *data)
   313 {
   314 	if (old_version)
   315 		printf("removing %s %s\n", name, old_version);
   316 	else
   317 		printf("install %s %s\n", name, new_version);
   318 }
   319 
   320 static int
   321 command_diff(int argc, const char *argv[])
   322 {
   323 	struct razor_set *set, *updated;
   324 
   325 	set = razor_set_open(repo_filename);
   326 	updated = razor_set_open(updated_repo_filename);
   327 	if (set == NULL || updated == NULL)
   328 		return 1;
   329 
   330 	razor_set_diff(set, updated, print_diff, NULL);	
   331 
   332 	razor_set_destroy(set);
   333 	razor_set_destroy(updated);
   334 
   335 	return 0;
   336 }
   337 
   338 static int
   339 command_import_rpms(int argc, const char *argv[])
   340 {
   341 	DIR *dir;
   342 	struct dirent *de;
   343 	struct razor_importer *importer;
   344 	struct razor_set *set;
   345 	struct razor_rpm *rpm;
   346 	int len;
   347 	char filename[256];
   348 	const char *dirname = argv[0];
   349 
   350 	if (dirname == NULL) {
   351 		fprintf(stderr, "usage: razor import-rpms DIR\n");
   352 		return -1;
   353 	}
   354 
   355 	dir = opendir(dirname);
   356 	if (dir == NULL) {
   357 		fprintf(stderr, "couldn't read dir %s\n", dirname);
   358 		return -1;
   359 	}
   360 
   361 	importer = razor_importer_new();
   362 
   363 	while (de = readdir(dir), de != NULL) {
   364 		len = strlen(de->d_name);
   365 		if (len < 5 || strcmp(de->d_name + len - 4, ".rpm") != 0)
   366 		    continue;
   367 		snprintf(filename, sizeof filename,
   368 			 "%s/%s", dirname, de->d_name);
   369 		rpm = razor_rpm_open(filename);
   370 		if (rpm == NULL) {
   371 			fprintf(stderr,
   372 				"failed to open rpm \"%s\"\n", filename);
   373 			continue;
   374 		}
   375 		if (razor_importer_add_rpm(importer, rpm)) {
   376 			fprintf(stderr, "couldn't import %s\n", filename);
   377 			break;
   378 		}
   379 		razor_rpm_close(rpm);
   380 	}
   381 
   382 	if (de != NULL) {
   383 		razor_importer_destroy(importer);
   384 		return -1;
   385 	}
   386 
   387 	set = razor_importer_finish(importer);
   388 
   389 	razor_set_write(set, repo_filename);
   390 	razor_set_destroy(set);
   391 	printf("wrote %s\n", repo_filename);
   392 
   393 	return 0;
   394 }
   395 
   396 static int
   397 command_install(int argc, const char *argv[])
   398 {
   399 	struct razor_rpm *rpm;
   400 	const char *filename = argv[0];
   401 	struct stat buf;
   402 	const char root[] = "install";
   403 
   404 	if (stat(root, &buf) < 0) {
   405 		if (mkdir(root, 0777) < 0) {
   406 			fprintf(stderr,
   407 				"could not create install root \"%s\"\n",
   408 				root);
   409 			return -1;
   410 		}
   411 		fprintf(stderr, "created install root \"%s\"\n", root);
   412 	} else if (!S_ISDIR(buf.st_mode)) {
   413 		fprintf(stderr,
   414 			"install root \"%s\" exists, but is not a directory\n",
   415 			root);
   416 		return -1;
   417 	}
   418 
   419 	rpm = razor_rpm_open(filename);
   420 	if (rpm == NULL) {
   421 		fprintf(stderr, "failed to open rpm %s\n", filename);
   422 		return -1;
   423 	}
   424 	if (razor_rpm_install(rpm, root) < 0) {
   425 		fprintf(stderr, "failed to install rpm %s\n", filename);
   426 		return -1;
   427 	}
   428 	
   429 	razor_rpm_close(rpm);
   430 
   431 	return 0;
   432 }
   433 
   434 static struct {
   435 	const char *name;
   436 	const char *description;
   437 	int (*func)(int argc, const char *argv[]);
   438 } razor_commands[] = {
   439 	{ "list", "list all packages", command_list },
   440 	{ "list-requires", "list all requires for the given package", command_list_requires },
   441 	{ "list-provides", "list all provides for the given package", command_list_provides },
   442 	{ "list-obsoletes", "list all obsoletes for the given package", command_list_obsoletes },
   443 	{ "list-conflicts", "list all conflicts for the given package", command_list_conflicts },
   444 	{ "list-files", "list files for package set", command_list_files },
   445 	{ "list-file-packages", "list packages owning file", command_list_file_packages },
   446 	{ "list-package-files", "list files in package", command_list_package_files },
   447 	{ "what-requires", "list the packages that have the given requires", command_what_requires },
   448 	{ "what-provides", "list the packages that have the given provides", command_what_provides },
   449 	{ "import-yum", "import yum metadata files", command_import_yum },
   450 	{ "import-rpmdb", "import the system rpm database", command_import_rpmdb },
   451 	{ "import-rpms", "import rpms from the given directory", command_import_rpms },
   452 	{ "validate", "validate a package set", command_validate },
   453 	{ "update", "update all or specified packages", command_update },
   454 	{ "diff", "show diff between two package sets", command_diff },
   455 	{ "install", "install rpm", command_install }
   456 };
   457 
   458 static int
   459 usage(void)
   460 {
   461 	int i;
   462 
   463 	printf("usage:\n");
   464 	for (i = 0; i < ARRAY_SIZE(razor_commands); i++)
   465 		printf("  %-20s%s\n",
   466 		       razor_commands[i].name, razor_commands[i].description);
   467 
   468 	return 1;
   469 }
   470 
   471 int
   472 main(int argc, const char *argv[])
   473 {
   474 	char *repo;
   475 	int i;
   476 
   477 	repo = getenv("RAZOR_REPO");
   478 	if (repo != NULL)
   479 		repo_filename = repo;
   480 
   481 	if (argc < 2)
   482 		return usage();
   483 
   484 	for (i = 0; i < ARRAY_SIZE(razor_commands); i++)
   485 		if (strcmp(razor_commands[i].name, argv[1]) == 0)
   486 			return razor_commands[i].func(argc - 2, argv + 2);
   487 
   488 	return usage();
   489 }