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