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