main.c
author Kristian H?gsberg <krh@redhat.com>
Sun Oct 07 14:25:06 2007 -0400 (2007-10-07)
changeset 46 8de578466ece
parent 44 3d1a1517fa1d
child 48 458b03594baf
permissions -rw-r--r--
Extend importers to also pick up filelists for packages.
     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);
    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_requires(set, argv[2]);
    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_provides(set, argv[2]);
    44 	razor_set_destroy(set);
    45 
    46 	return 0;
    47 }
    48 
    49 static int
    50 command_what_requires(int argc, const char *argv[])
    51 {
    52 	struct razor_set *set;
    53 
    54 	set = razor_set_open(repo_filename);
    55 	razor_set_list_requires_packages(set, argv[2], argv[3]);
    56 	razor_set_destroy(set);
    57 
    58 	return 0;
    59 }
    60 
    61 static int
    62 command_what_provides(int argc, const char *argv[])
    63 {
    64 	struct razor_set *set;
    65 
    66 	set = razor_set_open(repo_filename);
    67 	razor_set_list_provides_packages(set, argv[2], argv[3]);
    68 	razor_set_destroy(set);
    69 
    70 	return 0;
    71 }
    72 
    73 static int
    74 command_import_yum(int argc, const char *argv[])
    75 {
    76 	struct razor_set *set;
    77 
    78 	set = razor_set_create_from_yum_filelist(STDIN_FILENO);
    79 	if (set == NULL)
    80 		return 1;
    81 	razor_set_write(set, rawhide_repo_filename);
    82 	razor_set_destroy(set);
    83 	printf("wrote %s\n", rawhide_repo_filename);
    84 
    85 	return 0;
    86 }
    87 
    88 static int
    89 command_import_rpmdb(int argc, const char *argv[])
    90 {
    91 	struct razor_set *set;
    92 
    93 	set = razor_set_create_from_rpmdb();
    94 	if (set == NULL)
    95 		return 1;
    96 	razor_set_write(set, repo_filename);
    97 	razor_set_destroy(set);
    98 	printf("wrote %s\n", repo_filename);
    99 
   100 	return 0;
   101 }
   102 
   103 static int
   104 command_validate(int argc, const char *argv[])
   105 {
   106 	struct razor_set *set;
   107 
   108 	set = razor_set_open(repo_filename);
   109 	if (set == NULL)
   110 		return 1;
   111 	razor_set_list_unsatisfied(set);
   112 	razor_set_destroy(set);
   113 
   114 	return 0;
   115 }
   116 
   117 static int
   118 command_update(int argc, const char *argv[])
   119 {
   120 	struct razor_set *set, *upstream;
   121 
   122 	set = razor_set_open(repo_filename);
   123 	upstream = razor_set_open(rawhide_repo_filename);
   124 	if (set == NULL || upstream == NULL)
   125 		return 1;
   126 	set = razor_set_update(set, upstream, argc, argv);
   127 	razor_set_write(set, updated_repo_filename);
   128 	razor_set_destroy(set);
   129 	razor_set_destroy(upstream);
   130 	printf("wrote system-updated.repo\n");
   131 
   132 	return 0;
   133 }
   134 
   135 static void
   136 print_diff(const char *name,
   137 	   const char *old_version, const char *new_version, void *data)
   138 {
   139 	if (old_version)
   140 		printf("removing %s %s\n", name, old_version);
   141 	else
   142 		printf("install %s %s\n", name, new_version);
   143 }
   144 
   145 static int
   146 command_diff(int argc, const char *argv[])
   147 {
   148 	struct razor_set *set, *updated;
   149 
   150 	set = razor_set_open(repo_filename);
   151 	updated = razor_set_open(updated_repo_filename);
   152 	if (set == NULL || updated == NULL)
   153 		return 1;
   154 
   155 	razor_set_diff(set, updated, print_diff, NULL);	
   156 
   157 	razor_set_destroy(set);
   158 	razor_set_destroy(updated);
   159 
   160 	return 0;
   161 }
   162 
   163 static struct {
   164 	const char *name;
   165 	const char *description;
   166 	int (*func)(int argc, const char *argv[]);
   167 } razor_commands[] = {
   168 	{ "list", "list all packages", command_list },
   169 	{ "list-requires", "list all requires or requires for the given package", command_list_requires },
   170 	{ "list-provides", "list all provides or provides for the give package", command_list_provides },
   171 	{ "what-requires", "list the packages that have the given requires", command_what_requires },
   172 	{ "what-provides", "list the packages that have the given provides", command_what_provides },
   173 	{ "import-yum", "import yum filelist.xml on stdin", command_import_yum },
   174 	{ "import-rpmdb", "import the system rpm database", command_import_rpmdb },
   175 	{ "validate", "validate a package set", command_validate },
   176 	{ "update", "update all or specified packages", command_update },
   177 	{ "diff", "show diff between two package sets", command_diff }
   178 };
   179 
   180 static int
   181 usage(void)
   182 {
   183 	int i;
   184 
   185 	printf("usage:\n");
   186 	for (i = 0; i < ARRAY_SIZE(razor_commands); i++)
   187 		printf("  %-20s%s\n",
   188 		       razor_commands[i].name, razor_commands[i].description);
   189 
   190 	return 1;
   191 }
   192 
   193 int
   194 main(int argc, const char *argv[])
   195 {
   196 	char *repo;
   197 	int i;
   198 
   199 	repo = getenv("RAZOR_REPO");
   200 	if (repo != NULL)
   201 		repo_filename = repo;
   202 
   203 	if (argc < 2)
   204 		return usage();
   205 
   206 	for (i = 0; i < ARRAY_SIZE(razor_commands); i++)
   207 		if (strcmp(razor_commands[i].name, argv[1]) == 0)
   208 			return razor_commands[i].func(argc - 2, argv + 2);
   209 
   210 	return usage();
   211 }