main.c
author Kristian H?gsberg <krh@redhat.com>
Wed Oct 31 22:41:27 2007 -0400 (2007-10-31)
changeset 66 bb4ca4a47f66
parent 56 9c00581c71be
child 67 cfe57117efee
permissions -rw-r--r--
Store requires and provides in the same list.
     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_files(int argc, const char *argv[])
    51 {
    52 	struct razor_set *set;
    53 
    54 	set = razor_set_open(repo_filename);
    55 	if (set == NULL)
    56 		return 1;
    57 	razor_set_list_files(set, argv[0]);
    58 	razor_set_destroy(set);
    59 
    60 	return 0;
    61 }
    62 
    63 static int
    64 command_list_file_packages(int argc, const char *argv[])
    65 {
    66 	struct razor_set *set;
    67 
    68 	set = razor_set_open(repo_filename);
    69 	if (set == NULL)
    70 		return 1;
    71 	razor_set_list_file_packages(set, argv[0]);
    72 	razor_set_destroy(set);
    73 
    74 	return 0;
    75 }
    76 
    77 static int
    78 command_list_package_files(int argc, const char *argv[])
    79 {
    80 	struct razor_set *set;
    81 
    82 	set = razor_set_open(repo_filename);
    83 	if (set == NULL)
    84 		return 1;
    85 	razor_set_list_package_files(set, argv[0]);
    86 	razor_set_destroy(set);
    87 
    88 	return 0;
    89 }
    90 
    91 static int
    92 command_what_requires(int argc, const char *argv[])
    93 {
    94 	struct razor_set *set;
    95 
    96 	set = razor_set_open(repo_filename);
    97 	razor_set_list_property_packages(set, argv[0], argv[1],
    98 					 RAZOR_PROPERTY_REQUIRES);
    99 	razor_set_destroy(set);
   100 
   101 	return 0;
   102 }
   103 
   104 static int
   105 command_what_provides(int argc, const char *argv[])
   106 {
   107 	struct razor_set *set;
   108 
   109 	set = razor_set_open(repo_filename);
   110 	razor_set_list_property_packages(set, argv[0], argv[1],
   111 					 RAZOR_PROPERTY_PROVIDES);
   112 	razor_set_destroy(set);
   113 
   114 	return 0;
   115 }
   116 
   117 static int
   118 command_import_yum(int argc, const char *argv[])
   119 {
   120 	struct razor_set *set;
   121 
   122 	set = razor_set_create_from_yum_filelist(STDIN_FILENO);
   123 	if (set == NULL)
   124 		return 1;
   125 	razor_set_write(set, rawhide_repo_filename);
   126 	razor_set_destroy(set);
   127 	printf("wrote %s\n", rawhide_repo_filename);
   128 
   129 	return 0;
   130 }
   131 
   132 static int
   133 command_import_rpmdb(int argc, const char *argv[])
   134 {
   135 	struct razor_set *set;
   136 
   137 	set = razor_set_create_from_rpmdb();
   138 	if (set == NULL)
   139 		return 1;
   140 	razor_set_write(set, repo_filename);
   141 	razor_set_destroy(set);
   142 	printf("wrote %s\n", repo_filename);
   143 
   144 	return 0;
   145 }
   146 
   147 static int
   148 command_validate(int argc, const char *argv[])
   149 {
   150 	struct razor_set *set;
   151 
   152 	set = razor_set_open(repo_filename);
   153 	if (set == NULL)
   154 		return 1;
   155 	razor_set_list_unsatisfied(set);
   156 	razor_set_destroy(set);
   157 
   158 	return 0;
   159 }
   160 
   161 static int
   162 command_update(int argc, const char *argv[])
   163 {
   164 	struct razor_set *set, *upstream;
   165 
   166 	set = razor_set_open(repo_filename);
   167 	upstream = razor_set_open(rawhide_repo_filename);
   168 	if (set == NULL || upstream == NULL)
   169 		return 1;
   170 	set = razor_set_update(set, upstream, argc, argv);
   171 	razor_set_write(set, updated_repo_filename);
   172 	razor_set_destroy(set);
   173 	razor_set_destroy(upstream);
   174 	printf("wrote system-updated.repo\n");
   175 
   176 	return 0;
   177 }
   178 
   179 static void
   180 print_diff(const char *name,
   181 	   const char *old_version, const char *new_version, void *data)
   182 {
   183 	if (old_version)
   184 		printf("removing %s %s\n", name, old_version);
   185 	else
   186 		printf("install %s %s\n", name, new_version);
   187 }
   188 
   189 static int
   190 command_diff(int argc, const char *argv[])
   191 {
   192 	struct razor_set *set, *updated;
   193 
   194 	set = razor_set_open(repo_filename);
   195 	updated = razor_set_open(updated_repo_filename);
   196 	if (set == NULL || updated == NULL)
   197 		return 1;
   198 
   199 	razor_set_diff(set, updated, print_diff, NULL);	
   200 
   201 	razor_set_destroy(set);
   202 	razor_set_destroy(updated);
   203 
   204 	return 0;
   205 }
   206 
   207 static struct {
   208 	const char *name;
   209 	const char *description;
   210 	int (*func)(int argc, const char *argv[]);
   211 } razor_commands[] = {
   212 	{ "list", "list all packages", command_list },
   213 	{ "list-requires", "list all requires or requires for the given package", command_list_requires },
   214 	{ "list-provides", "list all provides or provides for the give package", command_list_provides },
   215 	{ "list-files", "list files for package set", command_list_files },
   216 	{ "list-file-packages", "list packages owning file", command_list_file_packages },
   217 	{ "list-package-files", "list files in package", command_list_package_files },
   218 	{ "what-requires", "list the packages that have the given requires", command_what_requires },
   219 	{ "what-provides", "list the packages that have the given provides", command_what_provides },
   220 	{ "import-yum", "import yum filelist.xml on stdin", command_import_yum },
   221 	{ "import-rpmdb", "import the system rpm database", command_import_rpmdb },
   222 	{ "validate", "validate a package set", command_validate },
   223 	{ "update", "update all or specified packages", command_update },
   224 	{ "diff", "show diff between two package sets", command_diff }
   225 };
   226 
   227 static int
   228 usage(void)
   229 {
   230 	int i;
   231 
   232 	printf("usage:\n");
   233 	for (i = 0; i < ARRAY_SIZE(razor_commands); i++)
   234 		printf("  %-20s%s\n",
   235 		       razor_commands[i].name, razor_commands[i].description);
   236 
   237 	return 1;
   238 }
   239 
   240 int
   241 main(int argc, const char *argv[])
   242 {
   243 	char *repo;
   244 	int i;
   245 
   246 	repo = getenv("RAZOR_REPO");
   247 	if (repo != NULL)
   248 		repo_filename = repo;
   249 
   250 	if (argc < 2)
   251 		return usage();
   252 
   253 	for (i = 0; i < ARRAY_SIZE(razor_commands); i++)
   254 		if (strcmp(razor_commands[i].name, argv[1]) == 0)
   255 			return razor_commands[i].func(argc - 2, argv + 2);
   256 
   257 	return usage();
   258 }