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