main.c
author Kristian H?gsberg <krh@redhat.com>
Mon Nov 05 21:55:31 2007 -0500 (2007-11-05)
changeset 72 4fddfa5f29fa
parent 71 befb5208c022
child 73 0ff316e24339
permissions -rw-r--r--
Only download yum files if they're not there.
     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 
     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 #define REPO_URL "http://download.fedora.redhat.com" \
   145 	"/pub/fedora/linux/development/i386/os/repodata"
   146 
   147 static int
   148 command_import_yum(int argc, const char *argv[])
   149 {
   150 	struct razor_set *set;
   151 	CURL *curl;
   152 	CURLcode res;
   153 	FILE *fp;
   154 	struct stat buf;
   155 
   156 	curl = curl_easy_init();
   157 	if (curl == NULL)
   158 		return 1;
   159 
   160 	if (stat("primary.xml.gz", &buf) < 0) {
   161 		curl_easy_setopt(curl, CURLOPT_NOPROGRESS, 0);
   162 		fp = fopen("primary.xml.gz", "w");
   163 		curl_easy_setopt(curl, CURLOPT_WRITEDATA, fp);
   164 		curl_easy_setopt(curl, CURLOPT_URL,
   165 				 REPO_URL "/primary.xml.gz");
   166 		res = curl_easy_perform(curl);
   167 		fclose(fp);
   168 	}
   169 
   170 	if (stat("filelist.xml.gz", &buf) < 0) {
   171 		fp = fopen("filelists.xml.gz", "w");
   172 		curl_easy_setopt(curl, CURLOPT_WRITEDATA, fp);
   173 		curl_easy_setopt(curl, CURLOPT_URL,
   174 				 REPO_URL "/filelists.xml.gz");
   175 		res = curl_easy_perform(curl);
   176 		fclose(fp);
   177 	}
   178 
   179 	curl_easy_cleanup(curl);
   180 
   181 	set = razor_set_create_from_yum();
   182 	if (set == NULL)
   183 		return 1;
   184 	razor_set_write(set, rawhide_repo_filename);
   185 	razor_set_destroy(set);
   186 	printf("wrote %s\n", rawhide_repo_filename);
   187 
   188 	return 0;
   189 }
   190 
   191 static int
   192 command_import_rpmdb(int argc, const char *argv[])
   193 {
   194 	struct razor_set *set;
   195 
   196 	set = razor_set_create_from_rpmdb();
   197 	if (set == NULL)
   198 		return 1;
   199 	razor_set_write(set, repo_filename);
   200 	razor_set_destroy(set);
   201 	printf("wrote %s\n", repo_filename);
   202 
   203 	return 0;
   204 }
   205 
   206 static int
   207 command_validate(int argc, const char *argv[])
   208 {
   209 	struct razor_set *set;
   210 
   211 	set = razor_set_open(repo_filename);
   212 	if (set == NULL)
   213 		return 1;
   214 	razor_set_list_unsatisfied(set);
   215 	razor_set_destroy(set);
   216 
   217 	return 0;
   218 }
   219 
   220 static int
   221 command_update(int argc, const char *argv[])
   222 {
   223 	struct razor_set *set, *upstream;
   224 
   225 	set = razor_set_open(repo_filename);
   226 	upstream = razor_set_open(rawhide_repo_filename);
   227 	if (set == NULL || upstream == NULL)
   228 		return 1;
   229 	set = razor_set_update(set, upstream, argc, argv);
   230 	razor_set_write(set, updated_repo_filename);
   231 	razor_set_destroy(set);
   232 	razor_set_destroy(upstream);
   233 	printf("wrote system-updated.repo\n");
   234 
   235 	return 0;
   236 }
   237 
   238 static void
   239 print_diff(const char *name,
   240 	   const char *old_version, const char *new_version, void *data)
   241 {
   242 	if (old_version)
   243 		printf("removing %s %s\n", name, old_version);
   244 	else
   245 		printf("install %s %s\n", name, new_version);
   246 }
   247 
   248 static int
   249 command_diff(int argc, const char *argv[])
   250 {
   251 	struct razor_set *set, *updated;
   252 
   253 	set = razor_set_open(repo_filename);
   254 	updated = razor_set_open(updated_repo_filename);
   255 	if (set == NULL || updated == NULL)
   256 		return 1;
   257 
   258 	razor_set_diff(set, updated, print_diff, NULL);	
   259 
   260 	razor_set_destroy(set);
   261 	razor_set_destroy(updated);
   262 
   263 	return 0;
   264 }
   265 
   266 static struct {
   267 	const char *name;
   268 	const char *description;
   269 	int (*func)(int argc, const char *argv[]);
   270 } razor_commands[] = {
   271 	{ "list", "list all packages", command_list },
   272 	{ "list-requires", "list all requires for the given package", command_list_requires },
   273 	{ "list-provides", "list all provides for the give package", command_list_provides },
   274 	{ "list-obsoletes", "list all obsoletes for the give package", command_list_obsoletes },
   275 	{ "list-conflicts", "list all conflicts for the give package", command_list_conflicts },
   276 	{ "list-files", "list files for package set", command_list_files },
   277 	{ "list-file-packages", "list packages owning file", command_list_file_packages },
   278 	{ "list-package-files", "list files in package", command_list_package_files },
   279 	{ "what-requires", "list the packages that have the given requires", command_what_requires },
   280 	{ "what-provides", "list the packages that have the given provides", command_what_provides },
   281 	{ "import-yum", "import yum filelist.xml on stdin", command_import_yum },
   282 	{ "import-rpmdb", "import the system rpm database", command_import_rpmdb },
   283 	{ "validate", "validate a package set", command_validate },
   284 	{ "update", "update all or specified packages", command_update },
   285 	{ "diff", "show diff between two package sets", command_diff }
   286 };
   287 
   288 static int
   289 usage(void)
   290 {
   291 	int i;
   292 
   293 	printf("usage:\n");
   294 	for (i = 0; i < ARRAY_SIZE(razor_commands); i++)
   295 		printf("  %-20s%s\n",
   296 		       razor_commands[i].name, razor_commands[i].description);
   297 
   298 	return 1;
   299 }
   300 
   301 int
   302 main(int argc, const char *argv[])
   303 {
   304 	char *repo;
   305 	int i;
   306 
   307 	repo = getenv("RAZOR_REPO");
   308 	if (repo != NULL)
   309 		repo_filename = repo;
   310 
   311 	if (argc < 2)
   312 		return usage();
   313 
   314 	for (i = 0; i < ARRAY_SIZE(razor_commands); i++)
   315 		if (strcmp(razor_commands[i].name, argv[1]) == 0)
   316 			return razor_commands[i].func(argc - 2, argv + 2);
   317 
   318 	return usage();
   319 }