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