main.c
author Kristian H?gsberg <krh@redhat.com>
Mon Nov 05 19:06:52 2007 -0500 (2007-11-05)
changeset 71 befb5208c022
parent 70 e94d16f789e5
child 72 4fddfa5f29fa
permissions -rw-r--r--
Link to curl and download yum files automatically.
     1 #include <stdlib.h>
     2 #include <stddef.h>
     3 #include <stdio.h>
     4 #include <string.h>
     5 #include <unistd.h>
     6 
     7 #include <curl/curl.h>
     8 #include "razor.h"
     9 
    10 static const char *repo_filename = "system.repo";
    11 static const char *rawhide_repo_filename = "rawhide.repo";
    12 static const char *updated_repo_filename = "system-updated.repo";
    13 
    14 static int
    15 command_list(int argc, const char *argv[])
    16 {
    17 	struct razor_set *set;
    18 
    19 	set = razor_set_open(repo_filename);
    20 	razor_set_list(set, argv[0]);
    21 	razor_set_destroy(set);
    22 
    23 	return 0;
    24 }
    25 
    26 static int
    27 command_list_requires(int argc, const char *argv[])
    28 {
    29 	struct razor_set *set;
    30 
    31 	set = razor_set_open(repo_filename);
    32 	razor_set_list_properties(set, argv[0], RAZOR_PROPERTY_REQUIRES);
    33 	razor_set_destroy(set);
    34 
    35 	return 0;
    36 }
    37 
    38 static int
    39 command_list_provides(int argc, const char *argv[])
    40 {
    41 	struct razor_set *set;
    42 
    43 	set = razor_set_open(repo_filename);
    44 	razor_set_list_properties(set, argv[0], RAZOR_PROPERTY_PROVIDES);
    45 	razor_set_destroy(set);
    46 
    47 	return 0;
    48 }
    49 
    50 static int
    51 command_list_obsoletes(int argc, const char *argv[])
    52 {
    53 	struct razor_set *set;
    54 
    55 	set = razor_set_open(repo_filename);
    56 	razor_set_list_properties(set, argv[0], RAZOR_PROPERTY_OBSOLETES);
    57 	razor_set_destroy(set);
    58 
    59 	return 0;
    60 }
    61 
    62 static int
    63 command_list_conflicts(int argc, const char *argv[])
    64 {
    65 	struct razor_set *set;
    66 
    67 	set = razor_set_open(repo_filename);
    68 	razor_set_list_properties(set, argv[0], RAZOR_PROPERTY_CONFLICTS);
    69 	razor_set_destroy(set);
    70 
    71 	return 0;
    72 }
    73 
    74 static int
    75 command_list_files(int argc, const char *argv[])
    76 {
    77 	struct razor_set *set;
    78 
    79 	set = razor_set_open(repo_filename);
    80 	if (set == NULL)
    81 		return 1;
    82 	razor_set_list_files(set, argv[0]);
    83 	razor_set_destroy(set);
    84 
    85 	return 0;
    86 }
    87 
    88 static int
    89 command_list_file_packages(int argc, const char *argv[])
    90 {
    91 	struct razor_set *set;
    92 
    93 	set = razor_set_open(repo_filename);
    94 	if (set == NULL)
    95 		return 1;
    96 	razor_set_list_file_packages(set, argv[0]);
    97 	razor_set_destroy(set);
    98 
    99 	return 0;
   100 }
   101 
   102 static int
   103 command_list_package_files(int argc, const char *argv[])
   104 {
   105 	struct razor_set *set;
   106 
   107 	set = razor_set_open(repo_filename);
   108 	if (set == NULL)
   109 		return 1;
   110 	razor_set_list_package_files(set, argv[0]);
   111 	razor_set_destroy(set);
   112 
   113 	return 0;
   114 }
   115 
   116 static int
   117 command_what_requires(int argc, const char *argv[])
   118 {
   119 	struct razor_set *set;
   120 
   121 	set = razor_set_open(repo_filename);
   122 	razor_set_list_property_packages(set, argv[0], argv[1],
   123 					 RAZOR_PROPERTY_REQUIRES);
   124 	razor_set_destroy(set);
   125 
   126 	return 0;
   127 }
   128 
   129 static int
   130 command_what_provides(int argc, const char *argv[])
   131 {
   132 	struct razor_set *set;
   133 
   134 	set = razor_set_open(repo_filename);
   135 	razor_set_list_property_packages(set, argv[0], argv[1],
   136 					 RAZOR_PROPERTY_PROVIDES);
   137 	razor_set_destroy(set);
   138 
   139 	return 0;
   140 }
   141 
   142 #define REPO_URL "http://download.fedora.redhat.com" \
   143 	"/pub/fedora/linux/development/i386/os/repodata"
   144 
   145 static int
   146 command_import_yum(int argc, const char *argv[])
   147 {
   148 	struct razor_set *set;
   149 	CURL *curl;
   150 	CURLcode res;
   151 	FILE *fp;
   152 
   153 	curl = curl_easy_init();
   154 	if (curl == NULL)
   155 		return 1;
   156 
   157 	curl_easy_setopt(curl, CURLOPT_NOPROGRESS, 0);
   158 	fp = fopen("primary.xml.gz", "w");
   159 	curl_easy_setopt(curl, CURLOPT_WRITEDATA, fp);
   160 	curl_easy_setopt(curl, CURLOPT_URL, REPO_URL "/primary.xml.gz");
   161 	res = curl_easy_perform(curl);
   162 	fclose(fp);
   163 
   164 	fp = fopen("filelists.xml.gz", "w");
   165 	curl_easy_setopt(curl, CURLOPT_WRITEDATA, fp);
   166 	curl_easy_setopt(curl, CURLOPT_URL, REPO_URL "/filelists.xml.gz");
   167 	res = curl_easy_perform(curl);
   168 	fclose(fp);
   169 
   170 	curl_easy_cleanup(curl);
   171 
   172 	set = razor_set_create_from_yum();
   173 	if (set == NULL)
   174 		return 1;
   175 	razor_set_write(set, rawhide_repo_filename);
   176 	razor_set_destroy(set);
   177 	printf("wrote %s\n", rawhide_repo_filename);
   178 
   179 	return 0;
   180 }
   181 
   182 static int
   183 command_import_rpmdb(int argc, const char *argv[])
   184 {
   185 	struct razor_set *set;
   186 
   187 	set = razor_set_create_from_rpmdb();
   188 	if (set == NULL)
   189 		return 1;
   190 	razor_set_write(set, repo_filename);
   191 	razor_set_destroy(set);
   192 	printf("wrote %s\n", repo_filename);
   193 
   194 	return 0;
   195 }
   196 
   197 static int
   198 command_validate(int argc, const char *argv[])
   199 {
   200 	struct razor_set *set;
   201 
   202 	set = razor_set_open(repo_filename);
   203 	if (set == NULL)
   204 		return 1;
   205 	razor_set_list_unsatisfied(set);
   206 	razor_set_destroy(set);
   207 
   208 	return 0;
   209 }
   210 
   211 static int
   212 command_update(int argc, const char *argv[])
   213 {
   214 	struct razor_set *set, *upstream;
   215 
   216 	set = razor_set_open(repo_filename);
   217 	upstream = razor_set_open(rawhide_repo_filename);
   218 	if (set == NULL || upstream == NULL)
   219 		return 1;
   220 	set = razor_set_update(set, upstream, argc, argv);
   221 	razor_set_write(set, updated_repo_filename);
   222 	razor_set_destroy(set);
   223 	razor_set_destroy(upstream);
   224 	printf("wrote system-updated.repo\n");
   225 
   226 	return 0;
   227 }
   228 
   229 static void
   230 print_diff(const char *name,
   231 	   const char *old_version, const char *new_version, void *data)
   232 {
   233 	if (old_version)
   234 		printf("removing %s %s\n", name, old_version);
   235 	else
   236 		printf("install %s %s\n", name, new_version);
   237 }
   238 
   239 static int
   240 command_diff(int argc, const char *argv[])
   241 {
   242 	struct razor_set *set, *updated;
   243 
   244 	set = razor_set_open(repo_filename);
   245 	updated = razor_set_open(updated_repo_filename);
   246 	if (set == NULL || updated == NULL)
   247 		return 1;
   248 
   249 	razor_set_diff(set, updated, print_diff, NULL);	
   250 
   251 	razor_set_destroy(set);
   252 	razor_set_destroy(updated);
   253 
   254 	return 0;
   255 }
   256 
   257 static struct {
   258 	const char *name;
   259 	const char *description;
   260 	int (*func)(int argc, const char *argv[]);
   261 } razor_commands[] = {
   262 	{ "list", "list all packages", command_list },
   263 	{ "list-requires", "list all requires for the given package", command_list_requires },
   264 	{ "list-provides", "list all provides for the give package", command_list_provides },
   265 	{ "list-obsoletes", "list all obsoletes for the give package", command_list_obsoletes },
   266 	{ "list-conflicts", "list all conflicts for the give package", command_list_conflicts },
   267 	{ "list-files", "list files for package set", command_list_files },
   268 	{ "list-file-packages", "list packages owning file", command_list_file_packages },
   269 	{ "list-package-files", "list files in package", command_list_package_files },
   270 	{ "what-requires", "list the packages that have the given requires", command_what_requires },
   271 	{ "what-provides", "list the packages that have the given provides", command_what_provides },
   272 	{ "import-yum", "import yum filelist.xml on stdin", command_import_yum },
   273 	{ "import-rpmdb", "import the system rpm database", command_import_rpmdb },
   274 	{ "validate", "validate a package set", command_validate },
   275 	{ "update", "update all or specified packages", command_update },
   276 	{ "diff", "show diff between two package sets", command_diff }
   277 };
   278 
   279 static int
   280 usage(void)
   281 {
   282 	int i;
   283 
   284 	printf("usage:\n");
   285 	for (i = 0; i < ARRAY_SIZE(razor_commands); i++)
   286 		printf("  %-20s%s\n",
   287 		       razor_commands[i].name, razor_commands[i].description);
   288 
   289 	return 1;
   290 }
   291 
   292 int
   293 main(int argc, const char *argv[])
   294 {
   295 	char *repo;
   296 	int i;
   297 
   298 	repo = getenv("RAZOR_REPO");
   299 	if (repo != NULL)
   300 		repo_filename = repo;
   301 
   302 	if (argc < 2)
   303 		return usage();
   304 
   305 	for (i = 0; i < ARRAY_SIZE(razor_commands); i++)
   306 		if (strcmp(razor_commands[i].name, argv[1]) == 0)
   307 			return razor_commands[i].func(argc - 2, argv + 2);
   308 
   309 	return usage();
   310 }