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