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